Skip to content

Commit

Permalink
Faster wallet synchronization (#443)
Browse files Browse the repository at this point in the history
* Download all blocks up to the actual chain tip

* Finish initial sync without waiting for the next block

* update broken tests

* Remove outdated comment

* invalidate the balance cache after getLatestBlock call
  • Loading branch information
panleone authored Nov 4, 2024
1 parent d85eea5 commit 4750196
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
9 changes: 5 additions & 4 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@ export class Wallet {
await this.#syncShield();
}
this.#isSynced = true;
// At this point download the last missing blocks in the range (blockCount -5, blockCount]
await this.getLatestBlocks(blockCount);

// Update both activities post sync
getEventEmitter().enableEvent('balance-update');
getEventEmitter().emit('balance-update');
Expand Down Expand Up @@ -836,9 +839,9 @@ export class Wallet {
subscribeToNetworkEvents() {
getEventEmitter().on('new-block', async (block) => {
if (this.#isSynced) {
await this.getLatestBlocks(block);
// Invalidate the balance cache to keep immature balance updated
this.#mempool.invalidateBalanceCache();
await this.getLatestBlocks(block);
getEventEmitter().emit('new-tx');
}
});
Expand All @@ -851,11 +854,9 @@ export class Wallet {
async (blockCount) => {
const cNet = getNetwork();
let block;
// Don't ask for the exact last block that arrived,
// since it takes around 1 minute for blockbook to make it API available
for (
let blockHeight = this.#lastProcessedBlock + 1;
blockHeight < blockCount;
blockHeight <= blockCount;
blockHeight++
) {
try {
Expand Down
11 changes: 2 additions & 9 deletions tests/integration/wallet/sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ describe('Wallet sync tests', () => {
);
// Mint the block with the transaction
await mineAndSync();
// getLatestBlocks sync up until chain tip - 1 block,
// so at this point walletHD doesn't still know about the UTXO he received
expect(walletHD.balance).toBe(1 * 10 ** 8);
// mine an empty block and verify that the tx arrived
await mineAndSync();
// getLatestBlocks sync up until the actual chain tip
expect(walletHD.balance).toBe((1 + 0.05) * 10 ** 8);

// Sends funds back to the legacy wallet and verify that he also correctly receives funds
Expand All @@ -102,7 +98,7 @@ describe('Wallet sync tests', () => {
let nAddress = 0;
// So according to BIP32 standard
// wallets must be aware of addresses up to nAddress + MAX_ACCOUNT_GAP
for (let i = 0; i < 5; i++) {
for (let i = 1; i <= 5; i++) {
nAddress += 20;
let newAddress = walletHD.getAddressFromPath(
path.slice(0, -1) + String(nAddress)
Expand All @@ -114,10 +110,7 @@ describe('Wallet sync tests', () => {
0.01 * 10 ** 8
);
await mineAndSync();
// Validate the balance of the HD wallet pre-tx-confirm
expect(walletHD.balance).toBe((1 + 0.01 * i) * 10 ** 8);
// Mine a block with the Tx
await mineAndSync();
}
});
it('recognizes immature balance', async () => {
Expand Down

0 comments on commit 4750196

Please sign in to comment.