Skip to content

Commit

Permalink
Fix some JSON-RPC bugs+regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in authored and styppo committed Jul 29, 2019
1 parent 3359347 commit a9f4008
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
6 changes: 3 additions & 3 deletions clients/nodejs/modules/JsonRpcServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class JsonRpcServer {

async getTransactionsByAddress(addr, limit = 1000) {
const address = Nimiq.Address.fromString(addr);
const txs = await this._client.getTransactionsByAddress(address);
const txs = await this._client.getTransactionsByAddress(address, 0, [], limit);
const result = [];
for (const tx of txs) {
try {
Expand Down Expand Up @@ -710,7 +710,7 @@ class JsonRpcServer {
accountsHash: block.accountsHash.toHex(),
difficulty: block.difficulty,
timestamp: block.timestamp,
confirmations: this._blockchain.height - block.height
confirmations: (await this._client.getHeadHeight()) - block.height
};
if (block.isFull()) {
obj.miner = block.minerAddr.toHex();
Expand Down Expand Up @@ -756,7 +756,7 @@ class JsonRpcServer {
_transactionDetailsToObj(tx) {
return {
hash: tx.transactionHash.toHex(),
blockHash: tx.blockHash.toHex(),
blockHash: tx.blockHash ? tx.blockHash.toHex() : undefined,
blockNumber: tx.blockHeight,
timestamp: tx.timestamp,
confirmations: tx.confirmations,
Expand Down
7 changes: 4 additions & 3 deletions src/main/generic/api/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,10 @@ class Client {
* @param {Address|string} address Address of an account
* @param {number} [sinceBlockHeight=0] Minimum block height to consider for updates
* @param {Array.<Client.TransactionDetails>} [knownTransactionDetails] List of transaction details on already known transactions since {@param sinceBlockHeight}
* @param {number} [limit=NumberUtils.UINT64_MAX] Maximum number of transactions to return, this number may be exceeded for large knownTransactionDetails sets.
* @return {Promise.<Array.<Client.TransactionDetails>>}
*/
async getTransactionsByAddress(address, sinceBlockHeight = 0, knownTransactionDetails) {
async getTransactionsByAddress(address, sinceBlockHeight = 0, knownTransactionDetails, limit = NumberUtils.UINT64_MAX) {
address = Address.fromAny(address);
const knownTxs = new HashMap();
if (knownTransactionDetails) {
Expand All @@ -740,7 +741,7 @@ class Client {
const consensus = await this._consensus;
const txs = new HashSet((i) => i instanceof Hash ? i : i.transactionHash);
try {
const pending = await consensus.getPendingTransactionsByAddress(address);
const pending = await consensus.getPendingTransactionsByAddress(address, limit);
for (const tx of pending) {
this._txWaitForExpire(tx);
txs.add(new Client.TransactionDetails(tx, Client.TransactionState.PENDING));
Expand All @@ -751,7 +752,7 @@ class Client {

// Fetch transaction receipts.
const receipts = new HashSet((receipt) => receipt.transactionHash);
receipts.addAll(await consensus.getTransactionReceiptsByAddress(address));
if (txs.length < count) receipts.addAll(await consensus.getTransactionReceiptsByAddress(address, txs.length - limit));

/** @type {HashMap.<string, HashSet.<Hash>>} */
const requestProofs = new HashMap();
Expand Down
11 changes: 7 additions & 4 deletions src/main/generic/consensus/BaseConsensus.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ class BaseConsensus extends Observable {

/**
* @param {Address} address
* @param {number} limit
* @returns {Promise.<Array.<Transaction>>}
*/
async getPendingTransactionsByAddress(address) { // eslint-disable-line require-await, no-unused-vars
async getPendingTransactionsByAddress(address, limit) { // eslint-disable-line require-await, no-unused-vars
throw new Error('not implemented: getPendingTransactionsByAddress');
}

Expand Down Expand Up @@ -187,10 +188,11 @@ class BaseConsensus extends Observable {

/**
* @param {Address} address
* @param {number} limit
* @returns {Promise.<Array.<TransactionReceipt>>}
*/
getTransactionReceiptsByAddress(address) {
return this._requestTransactionReceiptsByAddress(address);
getTransactionReceiptsByAddress(address, limit) {
return this._requestTransactionReceiptsByAddress(address, limit);
}

/**
Expand Down Expand Up @@ -810,10 +812,11 @@ class BaseConsensus extends Observable {

/**
* @param {Address} address
* @param {number} limit
* @returns {Promise.<Array.<TransactionReceipt>>}
* @protected
*/
async _requestTransactionReceiptsByAddress(address) {
async _requestTransactionReceiptsByAddress(address, limit) {
/** @type {Array.<BaseConsensusAgent>} */
const agents = [];
for (const agent of this._agents.valueIterator()) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/generic/consensus/BaseMiniConsensus.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,12 @@ class BaseMiniConsensus extends BaseConsensus {

/**
* @param {Address} address
* @param {number} limit
* @returns {Promise.<Array.<Transaction>>}
*/
async getPendingTransactionsByAddress(address) { // eslint-disable-line require-await
async getPendingTransactionsByAddress(address, limit) { // eslint-disable-line require-await
if (this._subscription.addresses && this._subscription.addresses.some(a => a.equals(address))) {
return this._mempool.getTransactionsByAddresses([address]);
return this._mempool.getTransactionsByAddresses([address], limit);
} else {
throw new Error('Can not provide pending transactions without prior subscription');
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/generic/consensus/full/FullConsensus.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ class FullConsensus extends BaseConsensus {
* @param {Address} address
* @returns {Promise.<Array.<Transaction>>}
*/
async getPendingTransactionsByAddress(address) { // eslint-disable-line require-await
return this._mempool.getTransactionsByAddresses([address]);
async getPendingTransactionsByAddress(address, limit) { // eslint-disable-line require-await
return this._mempool.getTransactionsByAddresses([address], limit);
}

/**
Expand All @@ -113,11 +113,12 @@ class FullConsensus extends BaseConsensus {

/**
* @param {Address} address
* @param {number} limit
* @returns {Promise.<Array.<TransactionReceipt>>}
*/
getTransactionReceiptsByAddress(address) {
getTransactionReceiptsByAddress(address, limit) {
// XXX Assumes that blockchain supports transaction index.
return this._blockchain.getTransactionReceiptsByAddress(address);
return this._blockchain.getTransactionReceiptsByAddress(address, limit);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/generic/consensus/light/LightConsensus.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ class LightConsensus extends BaseConsensus {

/**
* @param {Address} address
* @param {number} limit
* @returns {Promise.<Array.<Transaction>>}
*/
async getPendingTransactionsByAddress(address) { // eslint-disable-line require-await
return this._mempool.getTransactionsByAddresses([address]);
async getPendingTransactionsByAddress(address, limit) { // eslint-disable-line require-await
return this._mempool.getTransactionsByAddresses([address], limit);
}

/**
Expand Down

0 comments on commit a9f4008

Please sign in to comment.