-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from Rate-Limiting-Nullifier/fix/slashing
Fix/slashing
- Loading branch information
Showing
5 changed files
with
136 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,139 @@ | ||
import Registry, { DEFAULT_REGISTRY_TREE_DEPTH } from "../src/registry" | ||
|
||
|
||
describe("Registry", () => { | ||
describe("Registry Creation", () => { | ||
test("Should create a registry", () => { | ||
const registry = new Registry() | ||
|
||
expect(registry.root.toString()).toContain("150197") | ||
expect(registry._treeDepth).toBe(DEFAULT_REGISTRY_TREE_DEPTH) | ||
expect(registry._zeroValue).toBe(BigInt(0)) | ||
expect(registry.members).toHaveLength(0) | ||
}) | ||
import Registry, { DEFAULT_REGISTRY_TREE_DEPTH } from '../src/registry' | ||
import poseidon from 'poseidon-lite' | ||
|
||
const zeroValue = BigInt(0) | ||
const secret1 = BigInt(1) | ||
const secret2 = BigInt(2) | ||
const secret3 = BigInt(3) | ||
const id1 = poseidon([secret1]) | ||
const id2 = poseidon([secret2]) | ||
const id3 = poseidon([secret3]) | ||
|
||
describe('Registry', () => { | ||
describe('Registry Creation', () => { | ||
test('Should create a registry', () => { | ||
const registry = new Registry() | ||
|
||
expect(registry.root.toString()).toContain('150197') | ||
expect(registry._treeDepth).toBe(DEFAULT_REGISTRY_TREE_DEPTH) | ||
expect(registry._zeroValue).toBe(zeroValue) | ||
expect(registry.members).toHaveLength(0) | ||
}) | ||
|
||
test("Should not create a registry with a wrong tree depth", () => { | ||
const wrongRegistry = () => new Registry(33) | ||
test('Should not create a registry with a wrong tree depth', () => { | ||
const wrongRegistry = () => new Registry(33) | ||
|
||
expect(wrongRegistry).toThrow("The tree depth must be between 16 and 32") | ||
}) | ||
expect(wrongRegistry).toThrow('The tree depth must be between 16 and 32') | ||
}) | ||
|
||
test("Should create a group with different parameters", () => { | ||
const registry = new Registry(32, BigInt(1)) | ||
test('Should create a group with different parameters', () => { | ||
const registry = new Registry(32, BigInt(1)) | ||
|
||
expect(registry.root.toString()).toContain("640470") | ||
expect(registry._treeDepth).toBe(32) | ||
expect(registry._zeroValue).toBe(BigInt(1)) | ||
expect(registry.members).toHaveLength(0) | ||
}) | ||
expect(registry.root.toString()).toContain('640470') | ||
expect(registry._treeDepth).toBe(32) | ||
expect(registry._zeroValue).toBe(BigInt(1)) | ||
expect(registry.members).toHaveLength(0) | ||
}) | ||
}) | ||
|
||
describe("Add Member", () => { | ||
test("Should add a member to a group", () => { | ||
const registry = new Registry() | ||
describe('Add Member', () => { | ||
test('Should add a member to a group', () => { | ||
const registry = new Registry() | ||
|
||
registry.addMember(BigInt(3)) | ||
registry.addMember(BigInt(3)) | ||
|
||
expect(registry.members).toHaveLength(1) | ||
}) | ||
test("Shouldn't be able to add Zero Value as member", () => { | ||
const registry = new Registry() | ||
expect(registry.members).toHaveLength(1) | ||
}) | ||
test("Shouldn't be able to add Zero Value as member", () => { | ||
const registry = new Registry() | ||
|
||
const result = () => registry.addMember(BigInt(0)) | ||
const result = () => registry.addMember(zeroValue) | ||
|
||
expect(result).toThrow("Can't add zero value as member.") | ||
}) | ||
expect(result).toThrow("Can't add zero value as member.") | ||
}) | ||
}) | ||
|
||
describe("Add Members", () => { | ||
test("Should add many members to a group", () => { | ||
const registry = new Registry() | ||
// make test to do large batch insertions | ||
registry.addMembers([BigInt(1), BigInt(3)]) | ||
describe('Add Members', () => { | ||
test('Should add many members to a group', () => { | ||
const registry = new Registry() | ||
// make test to do large batch insertions | ||
registry.addMembers([id1, id3]) | ||
|
||
expect(registry.members).toHaveLength(2) | ||
}) | ||
expect(registry.members).toHaveLength(2) | ||
}) | ||
}) | ||
|
||
describe("Index Member", () => { | ||
test("Should return the index of a member in a group", () => { | ||
const registry = new Registry() | ||
describe('Index Member', () => { | ||
test('Should return the index of a member in a group', () => { | ||
const registry = new Registry() | ||
|
||
registry.addMembers([BigInt(1), BigInt(3)]) | ||
registry.addMembers([id1, id3]) | ||
|
||
const index = registry.indexOf(BigInt(3)) | ||
const index = registry.indexOf(id3) | ||
|
||
expect(index).toBe(1) | ||
}) | ||
expect(index).toBe(1) | ||
}) | ||
}) | ||
|
||
describe("Remove Member", () => { | ||
test("Should remove a member from a group", () => { | ||
const registry = new Registry() | ||
registry.addMembers([BigInt(1), BigInt(2)]) | ||
registry.removeMember(BigInt(1)) | ||
describe('Remove Member', () => { | ||
test('Should remove a member from a group', () => { | ||
const registry = new Registry() | ||
registry.addMembers([id1, id2]) | ||
registry._removeMember(id1) | ||
|
||
expect(registry.members).toHaveLength(2) | ||
expect(registry.members[0]).toBe(registry._zeroValue) | ||
}) | ||
expect(registry.members).toHaveLength(2) | ||
expect(registry.members[0]).toBe(registry._zeroValue) | ||
}) | ||
|
||
describe("Slash Member", () => { | ||
test("Should slash a member from a group", () => { | ||
const registry = new Registry() | ||
registry.addMembers([BigInt(1), BigInt(2)]) | ||
registry.slashMember(BigInt(1)) | ||
expect(registry.slashedMembers).toHaveLength(1) | ||
expect(registry.slashedMembers[0]).toBe(BigInt(1)) | ||
expect(registry.slashedRoot.toString()).toContain("8796144249463725711720918130641160729715802427308818390609092244052653115670") | ||
}) | ||
test("Should not be able to add slashed member", () => { | ||
const registry = new Registry() | ||
registry.addMembers([BigInt(1), BigInt(2)]) | ||
registry.slashMember(BigInt(1)) | ||
expect(() => registry.addMember(BigInt(1))).toThrow("Can't add slashed member.") | ||
}) | ||
}) | ||
|
||
describe('Slash Member', () => { | ||
test('Should slash a member from a group', () => { | ||
const registry = new Registry() | ||
registry.addMembers([id1, id2]) | ||
registry.slashMember(secret1) | ||
expect(registry.slashedMembers).toHaveLength(1) | ||
expect(registry.slashedMembers[0]).toBe(id1) | ||
expect(registry.slashedRoot.toString()).toContain('9338483204925821039601825167556410297845868743886253952480975212723134036120') | ||
}) | ||
test('Should not be able to add slashed member', () => { | ||
const registry = new Registry() | ||
registry.addMembers([id1, id2]) | ||
registry.slashMember(secret1) | ||
expect(() => registry.addMember(id1)).toThrow("Can't add slashed member.") | ||
}) | ||
}) | ||
|
||
describe('Merkle Proof', () => { | ||
test('Should return a merkle proof', () => { | ||
const registry = new Registry() | ||
registry.addMember(id1) | ||
const proof = registry.generateMerkleProof(id1) | ||
console.log(proof.root) | ||
expect(String(proof.root)).toContain('9338483204925821039601825167556410297845868743886253952480975212723134036120') | ||
}) | ||
test('Should throw error when given invalid leaf', () => { | ||
const registry = new Registry() | ||
registry.addMember(id1) | ||
const treeDepth = registry._treeDepth | ||
|
||
describe("Merkle Proof", () => { | ||
test("Should return a merkle proof", () => { | ||
const registry = new Registry() | ||
registry.addMember(BigInt(1)) | ||
const proof = registry.generateMerkleProof(BigInt(1)) | ||
expect(String(proof.root)).toContain("879614424946372571172091813064116") | ||
}) | ||
test("Should throw error when given invalid leaf", () => { | ||
const registry = new Registry() | ||
registry.addMember(BigInt(1)) | ||
const treeDepth = registry._treeDepth; | ||
|
||
const result = () => Registry.generateMerkleProof(treeDepth, BigInt(0), [BigInt(1), BigInt(2)], BigInt(3)) | ||
|
||
expect(result).toThrow("The leaf does not exist") | ||
const result = () => Registry.generateMerkleProof(treeDepth, zeroValue, [id1, id2], id3) | ||
|
||
const result2 = () => Registry.generateMerkleProof(treeDepth, BigInt(0), [BigInt(1), BigInt(2)], BigInt(0)) | ||
expect(result).toThrow('The leaf does not exist') | ||
|
||
expect(result2).toThrow("Can't generate a proof for a zero leaf") | ||
}) | ||
}) | ||
const result2 = () => Registry.generateMerkleProof(treeDepth, zeroValue, [id1, id2], zeroValue) | ||
|
||
test("Should export/import to json", () => { | ||
const registry_json_test = new Registry() | ||
registry_json_test.addMembers([BigInt(1), BigInt(2)]) | ||
const json = registry_json_test.export() | ||
console.debug(json) | ||
const registry_from_json = Registry.import(json) | ||
expect(registry_from_json.members).toHaveLength(2) | ||
expect(registry_from_json.root).toEqual(registry_json_test.root) | ||
expect(registry_from_json.slashedRoot).toEqual(registry_json_test.slashedRoot) | ||
expect(result2).toThrow("Can't generate a proof for a zero leaf") | ||
}) | ||
}) | ||
|
||
test('Should export/import to json', () => { | ||
const registryJsonTest = new Registry() | ||
registryJsonTest.addMembers([id1, id2]) | ||
const json = registryJsonTest.export() | ||
console.debug(json) | ||
const registryFromJson = Registry.import(json) | ||
expect(registryFromJson.members).toHaveLength(2) | ||
expect(registryFromJson.root).toEqual(registryJsonTest.root) | ||
expect(registryFromJson.slashedRoot).toEqual(registryJsonTest.slashedRoot) | ||
}) | ||
}) |