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.

60 lines
1.3 KiB

4 years ago
  1. const F = require("./poseidon.js").F;
  2. const Scalar = require("ffjavascript").Scalar;
  3. class SMTMemDb {
  4. constructor() {
  5. this.nodes = {};
  6. this.root = F.zero;
  7. }
  8. async getRoot() {
  9. return this.root;
  10. }
  11. _key2str(k) {
  12. // const keyS = bigInt(key).leInt2Buff(32).toString("hex");
  13. const keyS = Scalar.e(k);
  14. return keyS;
  15. }
  16. _normalize(n) {
  17. for (let i=0; i<n.length; i++) {
  18. n[i] = F.e(n[i]);
  19. }
  20. }
  21. async get(key) {
  22. const keyS = this._key2str(key);
  23. return this.nodes[keyS];
  24. }
  25. async multiGet(keys) {
  26. const promises = [];
  27. for (let i=0; i<keys.length; i++) {
  28. promises.push(this.get(keys[i]));
  29. }
  30. return await Promise.all(promises);
  31. }
  32. async setRoot(rt) {
  33. this.root = rt;
  34. }
  35. async multiIns(inserts) {
  36. for (let i=0; i<inserts.length; i++) {
  37. const keyS = this._key2str(inserts[i][0]);
  38. this._normalize(inserts[i][1]);
  39. this.nodes[keyS] = inserts[i][1];
  40. }
  41. }
  42. async multiDel(dels) {
  43. for (let i=0; i<dels.length; i++) {
  44. const keyS = this._key2str(dels[i]);
  45. delete this.nodes[keyS];
  46. }
  47. }
  48. }
  49. module.exports = SMTMemDb;