From 70e87677b36b8dcee9e9900575cc24283833631b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 3 May 2017 14:12:06 +0200 Subject: [PATCH] Retrieve block headers only for header-only info (#5480) * Add parity_getBlockHeaderByNumber * Use parity_getBlockHeaderByNumber for retrieval --- js/src/api/rpc/parity/parity.js | 12 ++-- js/src/dapps/registry/Events/actions.js | 2 +- js/src/dapps/signaturereg/services.js | 2 +- .../dapps/tokendeploy/Deploy/Event/event.js | 2 +- .../dapps/tokendeploy/Transfer/Event/event.js | 4 +- js/src/jsonrpc/interfaces/parity.js | 13 ++++ js/src/redux/providers/status.js | 6 +- js/src/ui/TxList/store.js | 67 ++++++++++--------- js/src/views/Contract/Events/Event/event.js | 2 +- 9 files changed, 65 insertions(+), 45 deletions(-) diff --git a/js/src/api/rpc/parity/parity.js b/js/src/api/rpc/parity/parity.js index 16b14eaef..53610545a 100644 --- a/js/src/api/rpc/parity/parity.js +++ b/js/src/api/rpc/parity/parity.js @@ -14,10 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -import { - inAddress, inAddresses, inData, inHex, inNumber16, inOptions, inBlockNumber, inDeriveHash, inDeriveIndex -} from '../../format/input'; -import { outAccountInfo, outAddress, outAddresses, outChainStatus, outHistogram, outHwAccountInfo, outNodeKind, outNumber, outPeers, outRecentDapps, outTransaction, outVaultMeta } from '../../format/output'; +import { inAddress, inAddresses, inBlockNumber, inData, inDeriveHash, inDeriveIndex, inHex, inNumber16, inOptions } from '../../format/input'; +import { outAccountInfo, outAddress, outAddresses, outBlock, outChainStatus, outHistogram, outHwAccountInfo, outNodeKind, outNumber, outPeers, outRecentDapps, outTransaction, outVaultMeta } from '../../format/output'; export default class Parity { constructor (transport) { @@ -189,6 +187,12 @@ export default class Parity { .execute('parity_generateSecretPhrase'); } + getBlockHeaderByNumber (blockNumber = 'latest') { + return this._transport + .execute('parity_getBlockHeaderByNumber', inBlockNumber(blockNumber)) + .then(outBlock); + } + getDappAddresses (dappId) { return this._transport .execute('parity_getDappAddresses', dappId) diff --git a/js/src/dapps/registry/Events/actions.js b/js/src/dapps/registry/Events/actions.js index 2b15b0f13..e1f316718 100644 --- a/js/src/dapps/registry/Events/actions.js +++ b/js/src/dapps/registry/Events/actions.js @@ -43,7 +43,7 @@ export const subscribe = (name, from = 0, to = 'pending') => events.forEach((e) => { Promise.all([ - api.eth.getBlockByNumber(e.blockNumber), + api.parity.getBlockHeaderByNumber(e.blockNumber), api.eth.getTransactionByHash(e.transactionHash) ]) .then(([block, tx]) => { diff --git a/js/src/dapps/signaturereg/services.js b/js/src/dapps/signaturereg/services.js index d9c60da6b..9c01e49fb 100644 --- a/js/src/dapps/signaturereg/services.js +++ b/js/src/dapps/signaturereg/services.js @@ -158,7 +158,7 @@ export function attachEvents (contract, callback) { } export function getBlock (blockNumber) { - return api.eth.getBlockByNumber(blockNumber); + return api.parity.getBlockHeaderByNumber(blockNumber); } export function callRegister (instance, id, options = {}) { diff --git a/js/src/dapps/tokendeploy/Deploy/Event/event.js b/js/src/dapps/tokendeploy/Deploy/Event/event.js index 4fa0726da..9503c67c2 100644 --- a/js/src/dapps/tokendeploy/Deploy/Event/event.js +++ b/js/src/dapps/tokendeploy/Deploy/Event/event.js @@ -94,7 +94,7 @@ export default class Event extends Component { Promise .all([ - api.eth.getBlockByNumber(event.blockNumber), + api.parity.getBlockHeaderByNumber(event.blockNumber), getCoin(event.params.tokenreg, event.params.coin) ]) .then(([block, coin]) => { diff --git a/js/src/dapps/tokendeploy/Transfer/Event/event.js b/js/src/dapps/tokendeploy/Transfer/Event/event.js index 623220baf..bc28bd8b1 100644 --- a/js/src/dapps/tokendeploy/Transfer/Event/event.js +++ b/js/src/dapps/tokendeploy/Transfer/Event/event.js @@ -97,8 +97,8 @@ export default class Event extends Component { return; } - api.eth - .getBlockByNumber(event.blockNumber) + api.parity + .getBlockHeaderByNumber(event.blockNumber) .then((block) => { this.setState({ block }); }); diff --git a/js/src/jsonrpc/interfaces/parity.js b/js/src/jsonrpc/interfaces/parity.js index 5d71c54df..797052037 100644 --- a/js/src/jsonrpc/interfaces/parity.js +++ b/js/src/jsonrpc/interfaces/parity.js @@ -296,6 +296,19 @@ export default { } }, + getBlockHeaderByNumber: { + section: SECTION_NET, + desc: 'Returns block header information by number (same as eth_getBlockByNumber without transactions and uncles)', + params: [ + { + type: BlockNumber, + desc: 'integer of a block number, or the string `\'earliest\'`, `\'latest\'` or `\'pending\'`, as in the [default block parameter](#the-default-block-parameter).', + example: fromDecimal(436) + } + ], + returns: 'See [eth_getBlockByHash](#eth_getblockbyhash) (without transactions and uncles)' + }, + getVaultMeta: { section: SECTION_VAULT, desc: 'Returns the metadata for a specific vault', diff --git a/js/src/redux/providers/status.js b/js/src/redux/providers/status.js index 19bec2c0a..e2e4b81de 100644 --- a/js/src/redux/providers/status.js +++ b/js/src/redux/providers/status.js @@ -134,8 +134,8 @@ export default class Status { this._store.dispatch(statusBlockNumber(blockNumber)); - this._api.eth - .getBlockByNumber(blockNumber) + this._api.parity + .getBlockHeaderByNumber(blockNumber) .then((block) => { if (!block) { return; @@ -147,7 +147,7 @@ export default class Status { })); }) .catch((error) => { - console.warn('status._subscribeBlockNumber', 'getBlockByNumber', error); + console.warn('status._subscribeBlockNumber', 'getBlockHeaderByNumber', error); }); }) .then((blockNumberSubscriptionId) => { diff --git a/js/src/ui/TxList/store.js b/js/src/ui/TxList/store.js index 99a081d57..180882b2d 100644 --- a/js/src/ui/TxList/store.js +++ b/js/src/ui/TxList/store.js @@ -70,48 +70,52 @@ export default class Store { } loadTransactions (_txhashes) { - const { eth } = this._api; - // Ignore special cases and if the contents of _txhashes && this.sortedHashes are the same if (Array.isArray(_txhashes) || this.sameHashList(_txhashes)) { return; } // Remove any tx that are edited/cancelled - this.sortedHashes - .forEach((hash) => { - if (!_txhashes.includes(hash)) { - this.removeHash(hash); - } - }); + this.sortedHashes.forEach((hash) => { + if (!_txhashes.includes(hash)) { + this.removeHash(hash); + } + }); // Add any new tx - _txhashes - .forEach((txhash) => { - if (this.sortedHashes.includes(txhash)) { return; } - eth.getTransactionByHash(txhash) - .then((tx) => { - if (!tx) { return; } - this.transactions[txhash] = tx; - // If the tx has a blockHash, let's get the blockNumber, otherwise it's ready to be added - if (tx.blockHash) { - eth.getBlockByNumber(tx.blockNumber) - .then((block) => { - this.blocks[tx.blockNumber] = block; - this.addHash(txhash); - }); - } else { - this.addHash(txhash); - } - }); - }); + _txhashes.forEach((txhash) => { + if (this.sortedHashes.includes(txhash)) { + return; + } + + this._api.eth + .getTransactionByHash(txhash) + .then((tx) => { + if (!tx) { + return; + } + + this.transactions[txhash] = tx; + + // If the tx has a blockHash, let's get the blockNumber, otherwise it's ready to be added + if (tx.blockHash) { + this._api.parity + .getBlockHeaderByNumber(tx.blockNumber) + .then((block) => { + this.blocks[tx.blockNumber] = block; + this.addHash(txhash); + }); + } else { + this.addHash(txhash); + } + }); + }); } cancelTransaction = (txComponent, tx) => { - const { parity } = this._api; const { hash } = tx; - parity + this._api.parity .removeTransaction(hash) .then(() => { txComponent.setState({ canceled: true }); @@ -122,13 +126,12 @@ export default class Store { } editTransaction = (txComponent, tx) => { - const { parity } = this._api; const { hash, gas, gasPrice, to, from, value, input, condition } = tx; - parity + this._api.parity .removeTransaction(hash) .then(() => { - parity.postTransaction({ + return this._api.parity.postTransaction({ from, to, gas, diff --git a/js/src/views/Contract/Events/Event/event.js b/js/src/views/Contract/Events/Event/event.js index 3c1572b51..eb370e7f1 100644 --- a/js/src/views/Contract/Events/Event/event.js +++ b/js/src/views/Contract/Events/Event/event.js @@ -146,7 +146,7 @@ export default class Event extends Component { Promise .all([ - api.eth.getBlockByNumber(event.blockNumber), + api.parity.getBlockHeaderByNumber(event.blockNumber), api.eth.getTransactionByHash(event.transactionHash) ]) .then(([block, transaction]) => {