Skip to content

Commit

Permalink
bump fork on batch truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Jan 3, 2025
1 parent f4f3472 commit 0541a11
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ class Hypercore extends EventEmitter {

get fork () {
if (this.opened === false) return 0
return this.core.tree.fork
return this.state.tree.fork
}

get peers () {
Expand Down
2 changes: 1 addition & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ module.exports = class Core {
signature = this.verifier.sign(state.tree.batch(), keyPair)
}

const tree = await this.state._overwrite(state, length, treeLength, signature, false, atom)
const tree = await this.state._overwrite(state, this.tree.fork, length, treeLength, signature, false, atom)

// gc blocks from source
if (treeLength < length) {
Expand Down
19 changes: 13 additions & 6 deletions lib/session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ module.exports = class SessionState {
}
}

async _overwrite (source, length, treeLength, signature, isDependent, atom) {
async _overwrite (source, fork, length, treeLength, signature, isDependent, atom) {
const blockPromises = []
const treePromises = []
const rootPromises = []
Expand Down Expand Up @@ -481,21 +481,27 @@ module.exports = class SessionState {
}

const tree = {
fork: this.tree.fork,
fork,
length,
rootHash: crypto.tree(roots),
signature
}

const upgraded = treeLength < this.tree.length || this.tree.length < length
const upgraded = treeLength < this.tree.length || this.tree.length < length || tree.fork !== this.tree.fork

if (upgraded) writer.setCoreHead(tree)
if (upgraded) {
writer.setCoreHead(tree)
}

const dependency = isDependent ? updateDependency(this, length) : null

await this.flushWriteBatch()

if (upgraded) this.tree.setRoots(roots, signature)
if (upgraded) {
this.tree.fork = fork
this.tree.setRoots(roots, signature)
}

if (dependency) this.storage.updateDependencies(dependency.length)

return tree
Expand All @@ -509,8 +515,9 @@ module.exports = class SessionState {

try {
const origLength = this.tree.length
const fork = treeLength < origLength ? this.tree.fork + 1 : this.tree.fork

const tree = await this._overwrite(state, length, treeLength, null, state === this.core.state, atom)
const tree = await this._overwrite(state, fork, length, treeLength, null, state === this.core.state, atom)

const bitfield = { start: treeLength, length: tree.length - treeLength, drop: false }

Expand Down

0 comments on commit 0541a11

Please sign in to comment.