Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPV Updates #721

Merged
merged 6 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,6 @@ class Chain extends AsyncEmitter {
100);
}

// Skip all blocks in spv mode once
// we've verified the network target.
if (this.options.spv)
return this.state;

// Ensure the timestamp is correct.
const mtp = await this.getMedianTime(prev);

Expand All @@ -501,6 +496,11 @@ class Chain extends AsyncEmitter {
true);
}

// Skip all blocks in spv mode once
// we've verified the network target.
if (this.options.spv)
return this.state;

// Calculate height of current block.
const height = prev.height + 1;

Expand Down Expand Up @@ -1567,7 +1567,7 @@ class Chain extends AsyncEmitter {
competitor.height
);

await this.emitAsync('reorganize', tip, competitor);
await this.emitAsync('reorganize', tip, competitor, fork);

return fork;
}
Expand Down Expand Up @@ -1622,7 +1622,7 @@ class Chain extends AsyncEmitter {
);

// Treat as a reorganize event.
await this.emitAsync('reorganize', tip, last);
await this.emitAsync('reorganize', tip, last, fork);
}

/**
Expand Down Expand Up @@ -1652,7 +1652,7 @@ class Chain extends AsyncEmitter {
// to the fork block, causing
// us to redownload the blocks
// on the new main chain.
await this._reset(fork.hash, true);
await this._reset(fork.hash);

// Emit disconnection events now that
// the chain has successfully reset.
Expand All @@ -1674,7 +1674,7 @@ class Chain extends AsyncEmitter {
'Chain replay from height %d necessary.',
fork.height);

return this.emitAsync('reorganize', tip, competitor);
return this.emitAsync('reorganize', tip, competitor, fork);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,8 @@ class ChainDB {
*/

async prune() {
assert(!this.options.spv, 'Cannot prune chain in SPV mode.');

const options = this.options;
const keepBlocks = this.network.block.keepBlocks;
const pruneAfter = this.network.block.pruneAfterHeight;
Expand Down Expand Up @@ -1831,7 +1833,8 @@ class ChainDB {
this.del(layout.h.encode(tip.hash));
this.del(layout.e.encode(tip.hash));

this.blocksBatch.pruneBlock(tip.hash);
if (this.blocks)
this.blocksBatch.pruneBlock(tip.hash);

// Queue up hash to be removed
// on successful write.
Expand Down
4 changes: 2 additions & 2 deletions lib/node/fullnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ class FullNode extends Node {
this.emit('disconnect', entry, block);
});

this.chain.on('reorganize', async (tip, competitor) => {
this.chain.on('reorganize', async (tip, competitor, fork) => {
try {
await this.mempool._handleReorg();
} catch (e) {
this.error(e);
}
this.emit('reorganize', tip, competitor);
this.emit('reorganize', tip, competitor, fork);
});

this.chain.on('reset', async (tip) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/node/spvnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ class SPVNode extends Node {
this.emit('disconnect', entry, block);
});

this.chain.on('reorganize', (tip, competitor) => {
this.emit('reorganize', tip, competitor);
this.chain.on('reorganize', (tip, competitor, fork) => {
this.emit('reorganize', tip, competitor, fork);
});

this.chain.on('reset', (tip) => {
Expand Down
Loading