-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(group): update group with lean-imt
- Loading branch information
Showing
6 changed files
with
116 additions
and
131 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
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,112 +1,96 @@ | ||
import Group from "./group" | ||
import hash from "./hash" | ||
|
||
describe("Group", () => { | ||
describe("# Group", () => { | ||
it("Should create a group", () => { | ||
const group = new Group(1) | ||
const group = new Group() | ||
|
||
expect(group.id).toBe(1) | ||
expect(group.root.toString()).toContain("103543") | ||
expect(group.depth).toBe(20) | ||
expect(group.zeroValue).toBe(hash(1)) | ||
expect(group.members).toHaveLength(0) | ||
}) | ||
|
||
it("Should not create a group with a wrong tree depth", () => { | ||
const fun = () => new Group(1, 33) | ||
|
||
expect(fun).toThrow("The tree depth must be between 16 and 32") | ||
}) | ||
|
||
it("Should create a group with a different tree depth", () => { | ||
const group = new Group(1, 32) | ||
|
||
expect(group.root.toString()).toContain("460373") | ||
expect(group.depth).toBe(32) | ||
expect(group.zeroValue).toBe(hash(1)) | ||
expect(group.members).toHaveLength(0) | ||
expect(group.root).toBeUndefined() | ||
expect(group.depth).toBe(0) | ||
expect(group.size).toBe(0) | ||
}) | ||
|
||
it("Should create a group with a list of members", () => { | ||
const group = new Group(2, 20, [1, 2, 3]) | ||
const group = new Group([1, 2, 3]) | ||
|
||
const group2 = new Group(2, 20) | ||
const group2 = new Group() | ||
|
||
group2.addMember(1) | ||
group2.addMember(2) | ||
group2.addMember(3) | ||
|
||
expect(group.root.toString()).toContain(group2.root.toString()) | ||
expect(group.depth).toBe(20) | ||
expect(group.zeroValue).toBe(hash(2)) | ||
expect(group.members).toHaveLength(3) | ||
expect(group.root).toContain(group2.root) | ||
expect(group.depth).toBe(2) | ||
expect(group.size).toBe(3) | ||
}) | ||
}) | ||
|
||
describe("# addMember", () => { | ||
it("Should add a member to a group", () => { | ||
const group = new Group(1) | ||
const group = new Group() | ||
|
||
group.addMember(BigInt(3)) | ||
group.addMember(3) | ||
|
||
expect(group.members).toHaveLength(1) | ||
expect(group.size).toBe(1) | ||
}) | ||
}) | ||
|
||
describe("# addMembers", () => { | ||
it("Should add many members to a group", () => { | ||
const group = new Group(1) | ||
const group = new Group() | ||
|
||
group.addMembers([BigInt(1), BigInt(3)]) | ||
group.addMembers([1, 3]) | ||
|
||
expect(group.members).toHaveLength(2) | ||
expect(group.size).toBe(2) | ||
}) | ||
}) | ||
|
||
describe("# indexOf", () => { | ||
it("Should return the index of a member in a group", () => { | ||
const group = new Group(1) | ||
group.addMembers([BigInt(1), BigInt(3)]) | ||
const group = new Group() | ||
group.addMembers([1, 3]) | ||
|
||
const index = group.indexOf(BigInt(3)) | ||
const index = group.indexOf(3) | ||
|
||
expect(index).toBe(1) | ||
}) | ||
}) | ||
|
||
describe("# updateMember", () => { | ||
it("Should update a member in a group", () => { | ||
const group = new Group(1) | ||
group.addMembers([BigInt(1), BigInt(3)]) | ||
const group = new Group() | ||
group.addMembers([1, 3]) | ||
|
||
group.updateMember(0, BigInt(1)) | ||
group.updateMember(0, 1) | ||
|
||
expect(group.members).toHaveLength(2) | ||
expect(group.members[0]).toBe(BigInt(1)) | ||
expect(group.size).toBe(2) | ||
expect(group.members[0]).toBe("1") | ||
}) | ||
}) | ||
|
||
describe("# removeMember", () => { | ||
it("Should remove a member from a group", () => { | ||
const group = new Group(1) | ||
group.addMembers([BigInt(1), BigInt(3)]) | ||
const group = new Group() | ||
group.addMembers([1, 3]) | ||
|
||
group.removeMember(0) | ||
|
||
expect(group.members).toHaveLength(2) | ||
expect(group.members[0]).toBe(group.zeroValue) | ||
expect(group.size).toBe(2) | ||
expect(group.members[0]).toBe("0") | ||
}) | ||
}) | ||
|
||
describe("# generateMerkleProof", () => { | ||
it("Should generate a proof of membership", () => { | ||
const group = new Group(1) | ||
group.addMembers([BigInt(1), BigInt(3)]) | ||
const group = new Group() | ||
|
||
group.addMembers([1, 3]) | ||
|
||
const proof = group.generateMerkleProof(0) | ||
|
||
expect(proof.leaf).toBe(BigInt(1)) | ||
console.log(proof) | ||
|
||
expect(proof.leaf).toBe("1") | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.