Skip to content

Commit

Permalink
do not reconcile if source tree is shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Oct 7, 2024
1 parent 1dd49ed commit 5db484a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,8 @@ module.exports = class Core {
const values = await Promise.all(promises)

const batch = await this.tree.reconcile(state.tree, length, treeLength)
if (batch === null) return null // could not reconcile

if (batch.upgraded) batch.signature = signature || this.verifier.sign(batch, keyPair)

await this.state.upgrade(treeLength, length, batch, values, keyPair)
Expand Down
8 changes: 6 additions & 2 deletions lib/merkle-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,18 @@ module.exports = class MerkleTree {
}

async reconcile (tree, length, treeLength) {
if (this.length > length) return null

const nodes = []
const data = []

const from = this.length * 2
const to = length * 2

const batch = this.batch()

if (from === to) return batch

const reader = tree.storage.createReadBatch()

// upgrade
Expand Down Expand Up @@ -573,8 +579,6 @@ module.exports = class MerkleTree {

reader.tryFlush()

const batch = this.batch()

// copy tree nodes
for (const node of await Promise.all(data)) {
if (node) batch.nodes.push(node)
Expand Down

0 comments on commit 5db484a

Please sign in to comment.