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.

181 lines
5.9 KiB

6 years ago
  1. const chai = require("chai");
  2. const bigInt = require("big-integer");
  3. const smt = require("../src/smt.js");
  4. const assert = chai.assert;
  5. function stringifyBigInts(o) {
  6. if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
  7. return o.toString(10);
  8. } else if (Array.isArray(o)) {
  9. return o.map(stringifyBigInts);
  10. } else if (typeof o == "object") {
  11. const res = {};
  12. for (let k in o) {
  13. res[k] = stringifyBigInts(o[k]);
  14. }
  15. return res;
  16. } else {
  17. return o;
  18. }
  19. }
  20. describe("SMT Javascript test", function () {
  21. this.timeout(100000);
  22. before( async () => {
  23. });
  24. it("Should insert 2 elements and empty them", async () => {
  25. const tree = await smt.newMemEmptyTrie();
  26. const key1 = bigInt(111);
  27. const value1 = bigInt(222);
  28. const key2 = bigInt(333);
  29. const value2 = bigInt(444);
  30. await tree.insert(key1,value1);
  31. await tree.insert(key2,value2);
  32. await tree.delete(key2);
  33. await tree.delete(key1);
  34. assert(tree.root.isZero());
  35. });
  36. it("Should insert 3 elements in dferent order and should be the same", async () => {
  37. const keys = [bigInt(8), bigInt(9), bigInt(32)];
  38. const values = [bigInt(88), bigInt(99), bigInt(3232)];
  39. const tree1 = await smt.newMemEmptyTrie();
  40. const tree2 = await smt.newMemEmptyTrie();
  41. const tree3 = await smt.newMemEmptyTrie();
  42. const tree4 = await smt.newMemEmptyTrie();
  43. const tree5 = await smt.newMemEmptyTrie();
  44. const tree6 = await smt.newMemEmptyTrie();
  45. await tree1.insert(keys[0],values[0]);
  46. await tree1.insert(keys[1],values[1]);
  47. await tree1.insert(keys[2],values[2]);
  48. await tree2.insert(keys[0],values[0]);
  49. await tree2.insert(keys[2],values[2]);
  50. await tree2.insert(keys[1],values[1]);
  51. await tree3.insert(keys[1],values[1]);
  52. await tree3.insert(keys[0],values[0]);
  53. await tree3.insert(keys[2],values[2]);
  54. await tree4.insert(keys[1],values[1]);
  55. await tree4.insert(keys[2],values[2]);
  56. await tree4.insert(keys[0],values[0]);
  57. await tree5.insert(keys[2],values[2]);
  58. await tree5.insert(keys[0],values[0]);
  59. await tree5.insert(keys[1],values[1]);
  60. await tree6.insert(keys[2],values[2]);
  61. await tree6.insert(keys[1],values[1]);
  62. await tree6.insert(keys[0],values[0]);
  63. assert(tree1.root.equals(tree2.root));
  64. assert(tree2.root.equals(tree3.root));
  65. assert(tree3.root.equals(tree4.root));
  66. assert(tree4.root.equals(tree5.root));
  67. assert(tree5.root.equals(tree6.root));
  68. assert.equal(Object.keys(tree1.db.nodes).length, Object.keys(tree2.db.nodes).length);
  69. assert.equal(Object.keys(tree2.db.nodes).length, Object.keys(tree3.db.nodes).length);
  70. assert.equal(Object.keys(tree3.db.nodes).length, Object.keys(tree4.db.nodes).length);
  71. assert.equal(Object.keys(tree4.db.nodes).length, Object.keys(tree5.db.nodes).length);
  72. assert.equal(Object.keys(tree5.db.nodes).length, Object.keys(tree6.db.nodes).length);
  73. await tree1.delete(keys[0]);
  74. await tree1.delete(keys[1]);
  75. await tree2.delete(keys[1]);
  76. await tree2.delete(keys[0]);
  77. assert(tree1.root.equals(tree2.root));
  78. await tree3.delete(keys[0]);
  79. await tree3.delete(keys[2]);
  80. await tree4.delete(keys[2]);
  81. await tree4.delete(keys[0]);
  82. assert(tree3.root.equals(tree4.root));
  83. await tree5.delete(keys[1]);
  84. await tree5.delete(keys[2]);
  85. await tree6.delete(keys[2]);
  86. await tree6.delete(keys[1]);
  87. assert(tree5.root.equals(tree6.root));
  88. await tree1.delete(keys[2]);
  89. await tree2.delete(keys[2]);
  90. await tree3.delete(keys[1]);
  91. await tree4.delete(keys[1]);
  92. await tree5.delete(keys[0]);
  93. await tree6.delete(keys[0]);
  94. assert(tree1.root.isZero());
  95. assert(tree2.root.isZero());
  96. assert(tree3.root.isZero());
  97. assert(tree4.root.isZero());
  98. assert(tree5.root.isZero());
  99. assert(tree6.root.isZero());
  100. assert.equal(Object.keys(tree1.db.nodes).length, 0);
  101. assert.equal(Object.keys(tree2.db.nodes).length, 0);
  102. assert.equal(Object.keys(tree3.db.nodes).length, 0);
  103. assert.equal(Object.keys(tree4.db.nodes).length, 0);
  104. assert.equal(Object.keys(tree5.db.nodes).length, 0);
  105. assert.equal(Object.keys(tree6.db.nodes).length, 0);
  106. });
  107. it("Insert and remove 100 numbers randomly", async () => {
  108. function perm(a) {
  109. const arr = a.slice();
  110. const rArr = [];
  111. for (let i=0; i<arr.length; i++) {
  112. let rIdx = Math.floor(Math.random() * (arr.length - i));
  113. rArr.push(arr[rIdx]);
  114. arr[rIdx] = arr[arr.length - i - 1];
  115. }
  116. return rArr;
  117. }
  118. const tree = await smt.newMemEmptyTrie();
  119. const arr = [];
  120. const N = 100;
  121. for (let i=0; i<N; i++) {
  122. arr.push(bigInt(i));
  123. }
  124. const insArr = perm(arr);
  125. for (let i=0; i<N; i++) {
  126. await tree.insert(insArr[i], i);
  127. }
  128. const delArr = perm(insArr);
  129. for (let i=0; i<N; i++) {
  130. await tree.delete(delArr[i]);
  131. }
  132. assert(tree.root.isZero());
  133. assert.equal(Object.keys(tree.db.nodes).length, 0);
  134. });
  135. it("Should test update", async () => {
  136. const tree1 = await smt.newMemEmptyTrie();
  137. const tree2 = await smt.newMemEmptyTrie();
  138. await tree1.insert(8,88);
  139. await tree1.insert(9,99,);
  140. await tree1.insert(32,3232);
  141. await tree2.insert(8,888);
  142. await tree2.insert(9,999);
  143. await tree2.insert(32,323232);
  144. await tree1.update(8, 888);
  145. await tree1.update(9, 999);
  146. await tree1.update(32, 323232);
  147. assert(tree1.root.equals(tree2.root));
  148. });
  149. });