Skip to content

Commit

Permalink
wallet: get node client options from node on connect.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Dec 27, 2023
1 parent 16dc3d4 commit cdb3bd5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ wallet plugin attached.
- `nextCompaction` - when will the next compaction trigger after restart.
- `lastCompaction` - when was the last compaction run.
- Introduce `scan interactive` hook (start, filter)
- Add WS call `get options` to get `chain` options: `prune` and `spv`. This
allows wallet to detect capabilities of the client it connected to. E.g.
`prune` can be used to check the maximum rescan depth available from the node.
This is only called on wallet -> node connect.

### Node HTTP Client:
- Introduce `scanInteractive` method that starts interactive rescan.
Expand Down
9 changes: 9 additions & 0 deletions lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ class NodeClient extends Client {
return this.call('watch mempool');
}

/**
* Get node options.
* @returns {Promise}
*/

getOptions() {
return this.call('get options');
}

/**
* Get chain tip.
* @returns {Promise}
Expand Down
11 changes: 11 additions & 0 deletions lib/node/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,17 @@ class HTTP extends Server {
return null;
});

socket.hook('get options', () => {
const options = {
chain: {
spv: this.chain.options.spv,
prune: this.chain.options.prune
}
};

return options;
});

socket.hook('get tip', () => {
return this.chain.tip.encode();
});
Expand Down
27 changes: 24 additions & 3 deletions lib/wallet/nodeclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ class NodeClient extends AsyncEmitter {
return this.on(type, handler);
}

/**
* Get node options.
* @returns {Promise}
*/

async getOptions() {
return {
chain: {
spv: this.node.chain.options.spv,
prune: this.node.chain.options.prune
}
};
}

/**
* Get chain tip.
* @returns {Promise}
Expand Down Expand Up @@ -212,6 +226,16 @@ class NodeClient extends AsyncEmitter {
return this.node.chain.getHashes(start, end);
}

/**
* Reset the chain.
* @param {Number} height
* @returns {Promise}
*/

async reset(height) {
return this.node.chain.reset(height);
}

/**
* Rescan for any missed transactions.
* @param {Number|Hash} start - Start block.
Expand All @@ -221,9 +245,6 @@ class NodeClient extends AsyncEmitter {
*/

async rescan(start) {
if (this.node.spv)
return this.node.chain.reset(start);

return this.node.chain.scan(start, this.filter, (entry, txs) => {
return this.emitAsync('block rescan', entry, txs);
});
Expand Down
14 changes: 14 additions & 0 deletions lib/wallet/nullclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ class NullClient extends EventEmitter {
return this.on(type, handler);
}

/**
* Get options.
* @returns {Promise}
*/

async getOptions() {
return {
chain: {
spv: false,
chain: false
}
};
}

/**
* Get chain tip.
* @returns {Promise}
Expand Down
14 changes: 11 additions & 3 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ class WalletDB extends EventEmitter {
this.network = this.options.network;
this.logger = this.options.logger.context('wallet');
this.workers = this.options.workers;
this.client = this.options.client || new NullClient(this);
this.feeRate = this.options.feeRate;
this.db = bdb.create(this.options);
this.name = 'wallet';
this.version = 2;

// Client state
this.client = this.options.client || new NullClient(this);
this.clientInfo = null;

// chain state.
this.hasStateCache = false;
this.state = new ChainState();
Expand Down Expand Up @@ -140,6 +143,7 @@ class WalletDB extends EventEmitter {
this.client.on('disconnect', async () => {
this.emit('disconnect');
this.filterSent = false;
this.clientInfo = null;
});

this.client.bind('block connect', async (entry, txs) => {
Expand Down Expand Up @@ -391,7 +395,8 @@ class WalletDB extends EventEmitter {
*/

async connect() {
return this.client.open();
await this.client.open();
this.clientInfo = await this.client.getOptions();
}

/**
Expand All @@ -400,6 +405,7 @@ class WalletDB extends EventEmitter {
*/

async disconnect() {
this.clientInfo = null;
return this.client.close();
}

Expand Down Expand Up @@ -531,6 +537,9 @@ class WalletDB extends EventEmitter {

const tip = await this.getTip();

if (this.clientInfo.chain.spv)
return this.client.reset(tip.height);

return this.client.rescan(tip.hash);
}

Expand Down Expand Up @@ -1364,7 +1373,6 @@ class WalletDB extends EventEmitter {

/**
* Get an account from the database by wid.
* @private
* @param {Number} wid
* @param {Number} index - Account index.
* @returns {Promise} - Returns {@link Wallet}.
Expand Down

0 comments on commit cdb3bd5

Please sign in to comment.