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.

InnerNode.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const errors_1 = require("../../../errors");
  7. const HashPrefix_1 = __importDefault(require("../HashPrefix"));
  8. const sha512Half_1 = __importDefault(require("../sha512Half"));
  9. const LeafNode_1 = __importDefault(require("./LeafNode"));
  10. const node_1 = require("./node");
  11. const HEX_ZERO = '0000000000000000000000000000000000000000000000000000000000000000';
  12. const SLOT_MAX = 15;
  13. const HEX = 16;
  14. class InnerNode extends node_1.Node {
  15. constructor(depth = 0) {
  16. super();
  17. this.leaves = {};
  18. this.type = node_1.NodeType.INNER;
  19. this.depth = depth;
  20. this.empty = true;
  21. }
  22. get hash() {
  23. if (this.empty) {
  24. return HEX_ZERO;
  25. }
  26. let hex = '';
  27. for (let iter = 0; iter <= SLOT_MAX; iter++) {
  28. const child = this.leaves[iter];
  29. const hash = child == null ? HEX_ZERO : child.hash;
  30. hex += hash;
  31. }
  32. const prefix = HashPrefix_1.default.INNER_NODE.toString(HEX);
  33. return (0, sha512Half_1.default)(prefix + hex);
  34. }
  35. addItem(tag, node) {
  36. const existingNode = this.getNode(parseInt(tag[this.depth], HEX));
  37. if (existingNode === undefined) {
  38. this.setNode(parseInt(tag[this.depth], HEX), node);
  39. return;
  40. }
  41. if (existingNode instanceof InnerNode) {
  42. existingNode.addItem(tag, node);
  43. }
  44. else if (existingNode instanceof LeafNode_1.default) {
  45. if (existingNode.tag === tag) {
  46. throw new errors_1.XrplError('Tried to add a node to a SHAMap that was already in there.');
  47. }
  48. else {
  49. const newInnerNode = new InnerNode(this.depth + 1);
  50. newInnerNode.addItem(existingNode.tag, existingNode);
  51. newInnerNode.addItem(tag, node);
  52. this.setNode(parseInt(tag[this.depth], HEX), newInnerNode);
  53. }
  54. }
  55. }
  56. setNode(slot, node) {
  57. if (slot < 0 || slot > SLOT_MAX) {
  58. throw new errors_1.XrplError('Invalid slot: slot must be between 0-15.');
  59. }
  60. this.leaves[slot] = node;
  61. this.empty = false;
  62. }
  63. getNode(slot) {
  64. if (slot < 0 || slot > SLOT_MAX) {
  65. throw new errors_1.XrplError('Invalid slot: slot must be between 0-15.');
  66. }
  67. return this.leaves[slot];
  68. }
  69. }
  70. exports.default = InnerNode;
  71. //# sourceMappingURL=InnerNode.js.map