You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.2 KiB

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