Skip to content

Commit

Permalink
Merge pull request #83 from Rate-Limiting-Nullifier/build/update-example
Browse files Browse the repository at this point in the history
Examples use the latest API
  • Loading branch information
mhchia authored Jul 1, 2023
2 parents abf19d1 + 34c7981 commit a118459
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 56 deletions.
8 changes: 4 additions & 4 deletions examples/browser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"rollup-plugin-polyfill-node": "^0.12.0",
"rollup-plugin-typescript2": "^0.34.1",
"rollup-plugin-visualizer": "^5.9.0",
"rlnjs": "^3.1.3"
"rlnjs": "^3.1.4"
}
}
30 changes: 23 additions & 7 deletions examples/browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "ethers";
import { RLN } from "rlnjs";
import { deployERC20, deployRLNContract, deployVerifier, rlnParams, treeDepth, url, withdrawParams } from "./configs";
import { ICache, MemoryCache, RLN, Status } from "rlnjs";
import { deployERC20, deployRLNContract, deployVerifier, rlnParams, treeDepth, url, withdrawParams } from "./configs.js";


async function main() {
Expand Down Expand Up @@ -42,8 +42,8 @@ async function main() {
const rlnContractAtBlock = await provider.getBlockNumber()
console.log(`Deployed RLN contract at ${rlnContractAddress} at block ${rlnContractAtBlock}`)

function createRLNInstance() {
return new RLN({
function createRLNInstance(cache?: ICache) {
return RLN.createWithContractRegistry({
/* Required */
rlnIdentifier,
provider,
Expand All @@ -57,6 +57,7 @@ async function main() {
verificationKey: rlnParams.verificationKey,
withdrawWasmFilePath: withdrawParams.wasmFilePath,
withdrawFinalZkeyPath: withdrawParams.finalZkeyPath,
cache,
})
}

Expand Down Expand Up @@ -116,7 +117,13 @@ async function main() {
/* Slash */

console.log("Try `slash` by making rlnAnother create more than " + `${messageLimit} proofs and get slashed by rln`)
const rlnAnother = createRLNInstance()
class ResettableCache extends MemoryCache {
async reset() {
this.cache = {}
}
}
const resettableCache = new ResettableCache()
const rlnAnother = createRLNInstance(resettableCache)
console.log(`rlnAnother created: identityCommitment=${rlnAnother.identityCommitment}`)
class FaultyMessageIDCounter {
private counter: bigint = BigInt(0)
Expand All @@ -130,16 +137,24 @@ async function main() {
}
}
console.log(`Registering rlnAnother...`)
// Intentionally uses a faulty message ID counter, so that it will use the same message ID
// and exceed the message limit. This will cause it to get slashed.
await rlnAnother.register(messageLimit, new FaultyMessageIDCounter(messageLimit));
console.log(`Creating proof0 for rlnAnother...`)
const proof0 = await rlnAnother.createProof(epoch, message0);
console.log(`Creating proof1 for rlnAnother...`)
// Intentionally clear the cache of rlnAnother, so that it will create a proof which
// will cause a breach and get slashed.
resettableCache.reset()
const proof1 = await rlnAnother.createProof(epoch, message1);
console.log(`rln saving proof0 from rlnAnother...`)
await rln.saveProof(proof0);
const res0 = await rln.saveProof(proof0);
if (res0.status != Status.VALID) {
throw new Error(`rlnAnother's proof should have been valid`);
}
console.log(`rln saving proof1 for rlnAnother...`)
const res1 = await rln.saveProof(proof1);
if (res1.status != "breach") {
if (res1.status != Status.BREACH) {
throw new Error(`rlnAnother's secret should have been breached`);
}
const secret = res1.secret as bigint
Expand All @@ -149,6 +164,7 @@ async function main() {
throw new Error(`rlnAnother should have been slashed`);
}
console.log(`Successfully slashed rlnAnother`);
RLN.cleanUp()
}

main().catch((e) => {
Expand Down
Loading

0 comments on commit a118459

Please sign in to comment.