Skip to content

Commit

Permalink
JsonRpcServer: Add method to get an accounts tree chunk
Browse files Browse the repository at this point in the history
Add RPC method to get an accounts tree chunk: `getAccountsTreeChunk`.
The method receives as `string` arguments` the block hash on which
the snapshot will be taken and the `startPrefix` of the accounts tree
chunk. Then it returns an `AccountsTreeChunk` object.
  • Loading branch information
jsdanielh committed Aug 24, 2023
1 parent c554d14 commit 8a591de
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions clients/nodejs/modules/JsonRpcServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class JsonRpcServer {
this._methods.set('createAccount', this.createAccount.bind(this));
this._methods.set('getBalance', this.getBalance.bind(this));
this._methods.set('getAccount', this.getAccount.bind(this));
this._methods.set('getAccountsTreeChunk', this.getAccountsTreeChunk.bind(this));

// Blockchain
this._methods.set('blockNumber', this.blockNumber.bind(this));
Expand Down Expand Up @@ -593,6 +594,11 @@ class JsonRpcServer {
return this._accountToObj(account, address);
}

async getAccountsTreeChunk(blockHash, startPrefix) {
const chunk = await this._consensus.blockchain.getAccountsTreeChunk(Nimiq.Hash.fromString(blockHash), startPrefix);
return chunk ? this._accountsTreeChunkToObj(chunk) : null;
}

/*
* Blockchain
*/
Expand Down Expand Up @@ -718,6 +724,31 @@ class JsonRpcServer {
return obj;
}

/**
* @param {AccountsTreeNode} node
* @returns {object}
* @private
*/
_accountsTreeNodeObj(node) {
return {
prefix: node.prefix,
account: this._accountToObj(node.account, Nimiq.Address.fromString(node.prefix))
}
}

/**
* @param {AccountsTreeChunk} chunk
* @returns {object}
* @private
*/
_accountsTreeChunkToObj(chunk) {
return {
nodes: chunk.terminalNodes.filter((node) => node.isTerminal()).map((node) => this._accountsTreeNodeObj(node)),
proof: chunk.proof.toString(),
tail: chunk.tail.prefix,
};
}

/**
* @param {Client.PeerInfo} peerInfo
* @param {Client.AddressInfo} addressInfo
Expand Down

0 comments on commit 8a591de

Please sign in to comment.