downgrade syntax for node 12

This commit is contained in:
poma 2020-07-31 15:02:01 +03:00
parent 6663aec38c
commit 3aff8f4515
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657

View File

@ -25,7 +25,7 @@ class MerkleTree {
this.levels = levels this.levels = levels
this.capacity = 2 << levels this.capacity = 2 << levels
this.zeroElement = zeroElement this.zeroElement = zeroElement
this._hash = hashFunction ?? defaultHash this._hash = hashFunction || defaultHash
this._zeros = [] this._zeros = []
this._layers = [] this._layers = []
@ -43,7 +43,9 @@ class MerkleTree {
for (let i = 0; i < Math.ceil(this._layers[level - 1].length / 2); i++) { for (let i = 0; i < Math.ceil(this._layers[level - 1].length / 2); i++) {
this._layers[level][i] = this._hash( this._layers[level][i] = this._hash(
this._layers[level - 1][i * 2], this._layers[level - 1][i * 2],
this._layers[level - 1]?.[i * 2 + 1] ?? this._zeros[level - 1], i * 2 + 1 < this._layers[level - 1].length ?
this._layers[level - 1][i * 2 + 1] :
this._zeros[level - 1],
) )
} }
} }
@ -54,7 +56,7 @@ class MerkleTree {
* @returns {*} * @returns {*}
*/ */
root() { root() {
return this._layers[this.levels]?.[0] ?? this._zeros[this.levels] return this._layers[this.levels].length > 0 ? this._layers[this.levels][0] : this._zeros[this.levels]
} }
/** /**
@ -94,7 +96,9 @@ class MerkleTree {
index >>= 1 index >>= 1
this._layers[level][index] = this._hash( this._layers[level][index] = this._hash(
this._layers[level - 1][index * 2], this._layers[level - 1][index * 2],
this._layers[level - 1]?.[index * 2 + 1] ?? this._zeros[level - 1], index * 2 + 1 < this._layers[level - 1].length ?
this._layers[level - 1][index * 2 + 1] :
this._zeros[level - 1],
) )
} }
} }
@ -112,7 +116,9 @@ class MerkleTree {
const pathIndex = [] const pathIndex = []
for (let level = 0; level < this.levels; level++) { for (let level = 0; level < this.levels; level++) {
pathIndex[level] = index % 2 pathIndex[level] = index % 2
pathElements[level] = this._layers[level]?.[index ^ 1] ?? this._zeros[level] pathElements[level] = (index ^ 1) < this._layers[level].length ?
this._layers[level][index ^ 1] :
this._zeros[level]
index >>= 1 index >>= 1
} }
return { return {