circom/test_old/mainTest.js
test_old/mainTest.jsRaw
1const assert = require("assert");
2
3const StaticMerkle = require("../src/StaticMerkle.js");
4const MemDB = require("../src/dbMem.js");
5const hash = require("../src/hashKeccak.js");
6const buffUtils = require("../src/buffUtils.js");
7const claimUtils = require("../src/claimUtils.js");
8
9describe("static merkle", () => {
10 before(async () => {
11
12 });
13
14 it("Create an empty tring of 0 levels", async () => {
15 const dbPrv0 = await MemDB();
16 const SM0 = await StaticMerkle(hash, dbPrv0, 0);
17 const empty = SM0.root;
18 assert.equal(buffUtils.toHex(empty), "0x0000000000000000000000000000000000000000000000000000000000000000");
19 });
20
21 it("create an empty", async () => {
22 const dbPrv = await MemDB();
23 const SM140 = await StaticMerkle(hash, dbPrv, 140);
24 const empty = SM140.root;
25 assert.equal(buffUtils.toHex(empty), "0x0000000000000000000000000000000000000000000000000000000000000000");
26 });
27
28 it("should add and remove a claim", async() => {
29 const dbPrv = await MemDB();
30 const SM140 = await StaticMerkle(hash, dbPrv, 140);
31 const empty = SM140.root;
32 const claim = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x04");
33 await SM140.addClaim(claim);
34 assert.equal(buffUtils.toHex(SM140.root), "0xd3d9ad5e3c0b38c4e3eb411e9e3114b5ed8fb5c4bc69158329feb1a62743cda1");
35 await SM140.removeClaim(claim);
36 assert.equal(buffUtils.toHex(SM140.root), buffUtils.toHex(empty));
37
38 assert.equal(SM140.tx.inserts.length, 0);
39
40 });
41
42 it("should add two claims in different order and should be the same", async () => {
43 const dbPrv_1 = await MemDB();
44 const SM140_1 = await StaticMerkle(hash, dbPrv_1, 140);
45 const dbPrv_2 = await MemDB();
46 const SM140_2 = await StaticMerkle(hash, dbPrv_2, 140);
47 const empty = SM140_1.root;
48 const claim1 = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x04");
49 const claim2 = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x05");
50
51 await SM140_1.addClaim(claim1);
52 await SM140_1.addClaim(claim2);
53
54 await SM140_2.addClaim(claim2);
55 await SM140_2.addClaim(claim1);
56
57 assert.equal(buffUtils.toHex(SM140_1.root), buffUtils.toHex(SM140_2.root));
58
59 await SM140_1.removeClaim(claim1);
60 await SM140_1.removeClaim(claim2);
61 assert.equal(buffUtils.toHex(SM140_1.root), buffUtils.toHex(empty));
62
63 await SM140_2.removeClaim(claim2);
64 await SM140_2.removeClaim(claim1);
65 assert.equal(buffUtils.toHex(SM140_2.root), buffUtils.toHex(empty));
66
67 });
68
69 it("should add 10 claims and remove them in different order", async () => {
70 const dbPrv = await MemDB();
71 const SM140 = await StaticMerkle(hash, dbPrv, 140);
72 const empty = SM140.root;
73 const claims = [];
74 let i;
75 for (i=0; i<10; i++) {
76 const b = Buffer.from([ i / 256, i % 256 ]);
77 claims[i] = claimUtils.buildClaim("0x01", "0x02", "0x03", b);
78 }
79
80 for (i=0;i<claims.length; i++) {
81 await SM140.addClaim(claims[i]);
82 }
83
84 assert.equal(buffUtils.toHex(SM140.root), "0xb57c288d5c018c56610a3fb783062c9b199221734c8c5617795b57cbdbd4347f");
85
86 for (i=0;i<claims.length; i++) {
87 await SM140.removeClaim(claims[i]);
88 }
89
90 assert.equal(buffUtils.toHex(SM140.root), buffUtils.toHex(empty));
91 assert.equal(SM140.tx.inserts.length, 0);
92 });
93
94 it("Should give the same root when added a repeated claim", async () => {
95 const dbPrv = await MemDB();
96 const SM140 = await StaticMerkle(hash, dbPrv, 140);
97 const empty = SM140.root;
98 const claims = [];
99 let i;
100 for (i=0; i<100; i++) {
101 const b = Buffer.from([ i % 10 ]);
102 claims[i] = claimUtils.buildClaim("0x01", "0x02", "0x03", b);
103 }
104
105 for (i=0;i<claims.length; i++) {
106 await SM140.addClaim(claims[i]);
107 }
108
109 assert.equal(buffUtils.toHex(SM140.root), "0xb57c288d5c018c56610a3fb783062c9b199221734c8c5617795b57cbdbd4347f");
110
111 for (i=0;i<claims.length; i++) {
112 await SM140.removeClaim(claims[i]);
113 }
114
115 assert.equal(buffUtils.toHex(SM140.root), buffUtils.toHex(empty));
116 assert.equal(SM140.tx.inserts.length, 0);
117 }).timeout(20000);
118
119 it("Should create a merkle proof and verify it ok", async () => {
120 const dbPrv = await MemDB();
121 const SM140 = await StaticMerkle(hash, dbPrv, 140);
122 const empty = SM140.root;
123 const claim1 = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x04");
124 const claim2 = claimUtils.buildClaim("0x01", "0x02", "0x03", "0x05");
125
126 await SM140.addClaim(claim1);
127 await SM140.addClaim(claim2);
128
129 const mp = await SM140.getMerkeProof(claim1);
130
131 assert.equal(SM140.checkClaim(SM140.root, claim1, mp), true);
132 assert.equal(SM140.checkClaim(empty, claim1, mp), false);
133 assert.equal(SM140.checkClaim(empty, claim2, mp), false);
134
135 const mp1 = await SM140.getMerkeProof(claim1);
136 assert.equal(SM140.checkClaim(SM140.root, claim1, mp1), true);
137 const mp2 = await SM140.getMerkeProof(claim2);
138 assert.equal(SM140.checkClaim(SM140.root, claim2, mp2), true);
139 });
140
141});