mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
added buildTree method
This commit is contained in:
parent
f6f94f60cf
commit
2992aafcc8
@ -43,6 +43,33 @@ class MerkleTree {
|
||||
this.originalArray = array
|
||||
this.buildTree()
|
||||
}
|
||||
buildTree() {
|
||||
// Hash all the inputs with inputHash()
|
||||
this.hashedArray = this.originalArray.map(item => {
|
||||
return this.inputHash(item)
|
||||
})
|
||||
// Initiate a 2D map to store all the hash values
|
||||
this.fullPath = []
|
||||
// Hash first row
|
||||
let currentRow = this.hashedArray.map(item => {
|
||||
return this.hash(item)
|
||||
})
|
||||
while (currentRow.length > 1) {
|
||||
let nextRow = []
|
||||
if ((currentRow.length % 2) !== 0) {
|
||||
// Duplicate last node in the row if the node is alone
|
||||
currentRow.push(currentRow[currentRow.length-1])
|
||||
}
|
||||
for (let i = 0; i < currentRow.length; i += 2) {
|
||||
nextRow.push(this.hash(currentRow[i]+currentRow[i+1]))
|
||||
}
|
||||
this.fullPath.unshift(currentRow)
|
||||
currentRow = nextRow
|
||||
}
|
||||
this.fullPath.unshift(currentRow)
|
||||
// Set the final hash as root
|
||||
this.root = currentRow[0]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user