From 6311abe8cedd70a2863edee8adc381eb63515da4 Mon Sep 17 00:00:00 2001 From: Junaid <86780488+jdevcs@users.noreply.github.com> Date: Wed, 7 Sep 2022 16:53:12 +0200 Subject: [PATCH] Merge block tags support 5199 (#5410) * formatter with POS block tags support * doc update for POS blocktags * POS tags in types * compareBlockNumbers with finalized tag support * test cases for compareBlockNumbers * change log update * eth call tests with new block tags * balance tests with new block tags * get block tests with new block tags * tx count tests with new block tags * get code tests with new block tags * get proof tests with new block tags * get storage tests with new block tags * formatter tests with new block tags * log formatter tests with new block tags * change log update * Update packages/web3-utils/src/index.js * return undefined for safe vs non zero number * additional test cases * Update packages/web3-utils/src/index.js Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> --- CHANGELOG.md | 1 + docs/web3-eth-contract.rst | 12 +- docs/web3-eth.rst | 30 ++-- packages/web3-core-helpers/src/formatters.js | 6 +- packages/web3-core/types/index.d.ts | 2 +- packages/web3-utils/src/index.js | 30 ++-- test/eth.call.js | 32 ++++ test/eth.getBalance.js | 14 +- test/eth.getBlock.js | 148 ++++++++++++++++++ test/eth.getBlockTransactionCount.js | 12 ++ test/eth.getCode.js | 12 ++ test/eth.getProof.js | 14 +- test/eth.getStorageAt.js | 12 ++ test/formatters.inputDefaultBlockFormatter.js | 4 +- test/formatters.inputLogFormatter.js | 32 ++++ test/utils.compareBlockNumbers.js | 29 +++- 16 files changed, 351 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6740d7aa638..f3ae96a3e33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -600,3 +600,4 @@ Released with 1.0.0-beta.37 code base. ### Added - Add missing function type "receive" in `AbiType` (#5165) +- Support of `safe` and `finalized` block tags added (#5410) \ No newline at end of file diff --git a/docs/web3-eth-contract.rst b/docs/web3-eth-contract.rst index 12b3312c252..b2b9098db0f 100644 --- a/docs/web3-eth-contract.rst +++ b/docs/web3-eth-contract.rst @@ -144,6 +144,8 @@ The default block parameters can be one of the following: - ``"earliest"`` - ``String``: The genesis block - ``"latest"`` - ``String``: The latest block (current head of the blockchain) - ``"pending"`` - ``String``: The currently mined block (including pending transactions) +- ``"finalized"`` - ``String``: (For POS networks) The finalized block is one which has been accepted as canonical by >2/3 of validators. +- ``"safe"`` - ``String``: (For POS networks) The safe head block is one which under normal network conditions, is expected to be included in the canonical chain. Under normal network conditions the safe head and the actual tip of the chain will be equivalent (with safe head trailing only by a few seconds). Safe heads will be less likely to be reorged than the proof of work networks latest blocks. Default is ``"latest"``. @@ -792,7 +794,7 @@ Parameters * ``from`` - ``String`` (optional): The address the call "transaction" should be made from. For calls the ``from`` property is optional however it is highly recommended to explicitly set it or it may default to `address(0)` depending on your node or provider. * ``gasPrice`` - ``String`` (optional): The gas price in wei to use for this call "transaction". * ``gas`` - ``Number`` (optional): The maximum gas provided for this call "transaction" (gas limit). -2. ``defaultBlock`` - ``Number|String|BN|BigNumber`` (optional): If you pass this parameter it will not use the default block set with :ref:`contract.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"``, and ``"pending"`` can also be used. Useful for requesting data from or replaying transactions in past blocks. +2. ``defaultBlock`` - ``Number|String|BN|BigNumber`` (optional): If you pass this parameter it will not use the default block set with :ref:`contract.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"``, ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used. Useful for requesting data from or replaying transactions in past blocks. 3. ``callback`` - ``Function`` (optional): This callback will be fired with the result of the smart contract method execution as the second argument, or with an error object as the first argument. ------- @@ -1083,7 +1085,7 @@ Parameters 1. ``options`` - ``Object``: The options used for calling. * ``from`` - ``String``: The address the call "transaction" should be made from. * ``gas`` - ``Number`` (optional): The maximum gas provided for this call "transaction" (gas limit). Setting a specific value helps to detect out of gas errors. Access list response will return amount of gas used. -2. ``block`` - ``String|Number|BN|BigNumber`` (optional): The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter `. +2. ``block`` - ``String|Number|BN|BigNumber`` (optional): The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"`` , ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter `. 3. ``callback`` - ``Function`` (optional): This callback will be fired with the result of the access list generation as the second argument, or with an error object as the first argument. ------- @@ -1215,7 +1217,7 @@ Parameters 1. ``options`` - ``Object`` (optional): The options used for deployment. * ``filter`` - ``Object`` (optional): Let you filter events by indexed parameters, e.g. ``{filter: {myNumber: [12,13]}}`` means all events where "myNumber" is 12 or 13. - * ``fromBlock`` - ``Number|String|BN|BigNumber`` (optional): The block number (greater than or equal to) from which to get events on. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. For specific range use :ref:`getPastEvents `. + * ``fromBlock`` - ``Number|String|BN|BigNumber`` (optional): The block number (greater than or equal to) from which to get events on. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"`` , ``"safe"`` or ``"finalized"`` can also be used. For specific range use :ref:`getPastEvents `. * ``topics`` - ``Array`` (optional): This allows to manually set the topics for the event filter. If given the filter property and event signature, (topic[0]) will not be set automatically. Each topic can also be a nested array of topics that behaves as “or” operation between the given nested topics. 2. ``callback`` - ``Function`` (optional): This callback will be fired for each *event* as the second argument, or an error as the first argument. @@ -1324,8 +1326,8 @@ Parameters 1. ``event`` - ``String``: The name of the event in the contract, or ``"allEvents"`` to get all events. 2. ``options`` - ``Object`` (optional): The options used for deployment. * ``filter`` - ``Object`` (optional): Lets you filter events by indexed parameters, e.g. ``{filter: {myNumber: [12,13]}}`` means all events where "myNumber" is 12 or 13. - * ``fromBlock`` - ``Number|String|BN|BigNumber`` (optional): The block number (greater than or equal to) from which to get events on. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. - * ``toBlock`` - ``Number|String|BN|BigNumber`` (optional): The block number (less than or equal to) to get events up to (Defaults to ``"latest"``). Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. + * ``fromBlock`` - ``Number|String|BN|BigNumber`` (optional): The block number (greater than or equal to) from which to get events on. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"`` , ``"safe"`` or ``"finalized"`` can also be used. + * ``toBlock`` - ``Number|String|BN|BigNumber`` (optional): The block number (less than or equal to) to get events up to (Defaults to ``"latest"``). Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"`` , ``"safe"`` or ``"finalized"`` can also be used. * ``topics`` - ``Array`` (optional): This allows manually setting the topics for the event filter. If given the filter property and event signature, (topic[0]) will not be set automatically. Each topic can also be a nested array of topics that behaves as “or” operation between the given nested topics. 3. ``callback`` - ``Function`` (optional): This callback will be fired with an array of event logs as the second argument, or an error as the first argument. diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 49d385a8619..17f3f8f381d 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -191,6 +191,8 @@ Default block parameters can be one of the following: - ``"earliest"`` - ``String``: The genesis block - ``"latest"`` - ``String``: The latest block (current head of the blockchain) - ``"pending"`` - ``String``: The currently mined block (including pending transactions) +- ``"finalized"`` - ``String``: (For POS networks) The finalized block is one which has been accepted as canonical by >2/3 of validators. +- ``"safe"`` - ``String``: (For POS networks) The safe head block is one which under normal network conditions, is expected to be included in the canonical chain. Under normal network conditions the safe head and the actual tip of the chain will be equivalent (with safe head trailing only by a few seconds). Safe heads will be less likely to be reorged than the proof of work networks latest blocks. Default is ``"latest"`` @@ -884,7 +886,7 @@ Parameters ---------- 1. ``String`` - The address to get the balance of. -2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. +2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used. 3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. ------- @@ -925,7 +927,7 @@ Parameters 1. ``String`` - The address to get the storage from. 2. ``Number|String|BN|BigNumber`` - The index position of the storage. -3. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. +3. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used. 4. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. @@ -963,7 +965,7 @@ Parameters ---------- 1. ``String`` - The address to get the code from. -2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. +2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used. 3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. ------- @@ -1002,7 +1004,7 @@ Returns a block matching the block number or block hash. Parameters ---------- -1. ``String|Number|BN|BigNumber`` - The block number or block hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter `. +1. ``String|Number|BN|BigNumber`` - The block number or block hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter `. 2. ``Boolean`` - (optional, default ``false``) If specified ``true``, the returned block will contain all transactions as objects. If ``false`` it will only contains the transaction hashes. 3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. @@ -1085,7 +1087,7 @@ Parameters ---------- -1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter `. +1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter `. 2. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. ------- @@ -1125,7 +1127,7 @@ Parameters ---------- -1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter `. +1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter `. 2. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. ------- @@ -1162,7 +1164,7 @@ Returns a blocks uncle by a given uncle index position. Parameters ---------- -1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter `. +1. ``String|Number|BN|BigNumber`` - The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter `. 2. ``Number`` - The index position of the uncle. 3. ``Boolean`` - (optional, default ``false``) If specified ``true``, the returned block will contain all transactions as objects. By default it is ``false`` so, there is no need to explictly specify false. And, if ``false`` it will only contains the transaction hashes. 4. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. @@ -1356,7 +1358,7 @@ Parameters ---------- -1. ``String|Number|BN|BigNumber`` - A block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter `. +1. ``String|Number|BN|BigNumber`` - A block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter `. 2. ``Number`` - The transaction's index position. 3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. @@ -1486,7 +1488,7 @@ Parameters ---------- 1. ``String`` - The address to get the numbers of transactions from. -2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. +2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used. 3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. ------- @@ -1804,7 +1806,7 @@ Parameters ---------- 1. ``Object`` - A transaction object, see :ref:`web3.eth.sendTransaction `. For calls the ``from`` property is optional however it is highly recommended to explicitly set it or it may default to `address(0)` depending on your node or provider. -2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. +2. ``Number|String|BN|BigNumber`` - (optional) If you pass this parameter it will not use the default block set with :ref:`web3.eth.defaultBlock `. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used. 3. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. ------- @@ -1888,8 +1890,8 @@ Parameters ---------- 1. ``Object`` - The filter options as follows: - - ``fromBlock`` - ``Number|String``: The number of the earliest block (``"latest"`` may be given to mean the most recent and ``"pending"`` currently mining, block). By default ``"latest"``. - - ``toBlock`` - ``Number|String``: The number of the latest block (``"latest"`` may be given to mean the most recent and ``"pending"`` currently mining, block). By default ``"latest"``. + - ``fromBlock`` - ``Number|String``: The number of the earliest block (or any of block tag ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used). By default ``"latest"``. + - ``toBlock`` - ``Number|String``: The number of the latest block (or any of block tag ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used). By default ``"latest"``. - ``address`` - ``String|Array``: An address or a list of addresses to only get logs from particular account(s). - ``topics`` - ``Array``: An array of values which must each appear in the log entries. The order is important, if you want to leave topics out use ``null``, e.g. ``[null, '0x12...']``. You can also pass an array for each topic with options for that topic e.g. ``[null, ['option1', 'option2']]`` @@ -2133,7 +2135,7 @@ Parameters 1. ``String`` 20 Bytes: The Address of the account or contract. 2. ``Number[] | BigNumber[] | BN[] | String[]`` 32 Bytes: Array of storage-keys which should be proofed and included. See :ref:`web3.eth.getStorageAt `. -3. ``Number | String | BN | BigNumber``: Integer block number. Pre-defined block numbers as ``"earliest"``, ``"latest"`` and ``"pending"`` can also be used. +3. ``Number | String | BN | BigNumber``: Integer block number. Pre-defined block numbers as ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` can also be used. 4. ``Function`` - (optional) Optional callback, returns an error object as first parameter and the result as second. ------- @@ -2208,7 +2210,7 @@ Parameters ---------- 1. A transaction object, see :ref:`web3.eth.sendTransaction ` with the difference that this method is specifically for contract method executions. -2. ``block`` - ``String|Number|BN|BigNumber`` (optional): The block number or hash. Or the string ``"earliest"``, ``"latest"`` or ``"pending"`` as in the :ref:`default block parameter `. +2. ``block`` - ``String|Number|BN|BigNumber`` (optional): The block number or hash. Or the string ``"earliest"``, ``"latest"`` , ``"pending"``, ``"safe"`` or ``"finalized"`` as in the :ref:`default block parameter `. 3. ``callback`` - ``Function`` (optional): This callback will be fired with the result of the access list generation as the second argument, or with an error object as the first argument. diff --git a/packages/web3-core-helpers/src/formatters.js b/packages/web3-core-helpers/src/formatters.js index 93409763aa9..77b2e5f0190 100644 --- a/packages/web3-core-helpers/src/formatters.js +++ b/packages/web3-core-helpers/src/formatters.js @@ -71,7 +71,7 @@ var outputBigNumberFormatter = function (number) { }; /** - * Returns true if the given blockNumber is 'latest', 'pending', or 'earliest. + * Returns true if the given blockNumber is 'latest', 'pending', 'earliest, 'finalized' or 'safe' * * @method isPredefinedBlockNumber * @@ -80,7 +80,7 @@ var outputBigNumberFormatter = function (number) { * @returns {Boolean} */ var isPredefinedBlockNumber = function (blockNumber) { - return blockNumber === 'latest' || blockNumber === 'pending' || blockNumber === 'earliest'; + return ['latest','pending','earliest','finalized','safe'].includes(blockNumber); }; /** @@ -101,7 +101,7 @@ var inputDefaultBlockNumberFormatter = function (blockNumber) { }; /** - * Returns the given block number as hex string or the predefined block number 'latest', 'pending', 'earliest', 'genesis' + * Returns the given block number as hex string or the predefined block number 'latest', 'pending', 'earliest', 'finalized', 'safe', 'genesis' * * @param {String|Number|BN|BigNumber} blockNumber * diff --git a/packages/web3-core/types/index.d.ts b/packages/web3-core/types/index.d.ts index 44c65c32409..c3be2b5af3c 100644 --- a/packages/web3-core/types/index.d.ts +++ b/packages/web3-core/types/index.d.ts @@ -433,7 +433,7 @@ export interface LogsOptions { topics?: Array; } -export type BlockNumber = string | number | BN | BigNumber | 'latest' | 'pending' | 'earliest' | 'genesis'; +export type BlockNumber = string | number | BN | BigNumber | 'latest' | 'pending' | 'earliest' | 'genesis' | 'finalized' | 'safe'; export interface RequestArguments { method: string; diff --git a/packages/web3-utils/src/index.js b/packages/web3-utils/src/index.js index adef45bd653..c3b2ded707c 100644 --- a/packages/web3-utils/src/index.js +++ b/packages/web3-utils/src/index.js @@ -318,7 +318,7 @@ var toChecksumAddress = function (address) { * Returns -1 if ab; 0 if a == b. * For more details on this type of function, see * developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort - * + * Block tag `safe` and `block number` combination param is not supported * @method compareBlockNumbers * * @param {String|Number|BN} a @@ -328,36 +328,42 @@ var toChecksumAddress = function (address) { * @returns {Number} -1, 0, or 1 */ var compareBlockNumbers = function(a, b) { - if (a == b) { + // Increasing order: (genesis = earliest), safe, (finalized ~ latest), pending + // safe vs block-num cant be compared as block number provided can be on left or right side of safe tag, until safe tag block number is extracted and compared + if (a === b) { return 0; - } else if (("genesis" == a || "earliest" == a || 0 == a) && ("genesis" == b || "earliest" == b || 0 == b)) { + } else if (("genesis" === a || "earliest" === a || 0 === a) && ("genesis" === b || "earliest" === b || 0 === b)) { return 0; - } else if ("genesis" == a || "earliest" == a) { + } else if ("genesis" === a || "earliest" === a || a === 0) { // b !== a, thus a < b return -1; - } else if ("genesis" == b || "earliest" == b) { + } else if ("genesis" === b || "earliest" === b || b === 0) { // b !== a, thus a > b return 1; - } else if (a == "latest") { - if (b == "pending") { + } else if (a === "latest" || a === "finalized") { + if (b === "pending") { return -1; } else { // b !== ("pending" OR "latest"), thus a > b return 1; } - } else if (b === "latest") { - if (a == "pending") { + } else if (b === "latest" || b === "finalized") { + if (a === "pending") { return 1; } else { // b !== ("pending" OR "latest"), thus a > b return -1 } - } else if (a == "pending") { + } else if (a === "pending") { // b (== OR <) "latest", thus a > b return 1; - } else if (b == "pending") { + } else if (b === "pending") { return -1; - } else { + } else if(a === "safe" || b === "safe") { + // either a or b is "safe" and the other one did not fall into any of the conditions above, so the other one is a number + return undefined; + } + else { let bnA = new BN(a); let bnB = new BN(b); if(bnA.lt(bnB)) { diff --git a/test/eth.call.js b/test/eth.call.js index eea4b019cab..c8fd7f49dee 100644 --- a/test/eth.call.js +++ b/test/eth.call.js @@ -34,6 +34,38 @@ var tests = [{ result: '0x31981', formattedResult: '0x31981', call: 'eth_'+ method +},{ + args: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: 11, + gasPrice: 11 + }, 'safe'], + formattedArgs: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: '0xb', + gasPrice: '0xb' + }, 'safe'], + result: '0x31981', + formattedResult: '0x31981', + call: 'eth_'+ method +},{ + args: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: 11, + gasPrice: 11 + }, 'finalized'], + formattedArgs: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: '0xb', + gasPrice: '0xb' + }, 'finalized'], + result: '0x31981', + formattedResult: '0x31981', + call: 'eth_'+ method }]; testMethod.runTests('eth', method, tests); diff --git a/test/eth.getBalance.js b/test/eth.getBalance.js index 77e8cfc8f14..fcd69dd7d68 100644 --- a/test/eth.getBalance.js +++ b/test/eth.getBalance.js @@ -87,7 +87,19 @@ var tests = [{ defaultOptions: [ ['defaultBlock', 1] ] -}]; +}, { + args: ['0x000000000000000000000000000000000000012d', 'finalized'], + formattedArgs: ['0x000000000000000000000000000000000000012d', 'finalized'], + result: '0x31981', + formattedResult: '203137', + call: 'eth_' + method +}, { + args: ['0x000000000000000000000000000000000000012d', 'safe'], + formattedArgs: ['0x000000000000000000000000000000000000012d', 'safe'], + result: '0x31981', + formattedResult: '203137', + call: 'eth_' + method +} ]; testMethod.runTests('eth', method, tests); diff --git a/test/eth.getBlock.js b/test/eth.getBlock.js index 1fe66a346da..f5e17717b24 100644 --- a/test/eth.getBlock.js +++ b/test/eth.getBlock.js @@ -109,6 +109,142 @@ var formattedBlockResultWithTx = { "uncles": ["0x460cfb8472af2c5fd05b5a2", "0xd5460cfb8472af2c5fd05b5a2"] }; +var safeFormattedBlockResult = { + "baseFeePerGas":44803, + "difficulty":"0", + "extraData":"0x", + "gasLimit":30000000, + "gasUsed":6477149, + "hash":"0xcb51d46341a9a316f4e143b04be9e644b1e143e41848f7dd67e1b9442b4876cc", + "logsBloom":"0x00000000000002040010000004000000000000000000000000000000020200008000000000002000000000000003000200000000000010801004000000200000040000000001000100800008404000000000200002001000000020000801000800000000800000000000000000000000000010000000400000000090000100400041000000000000000000000800000004000020000000000008000000000010020000000000000000100000000000000000000002200000000000000000000000000002000000100000000010200000000000000000000000000000000000000a10000000010040001000000000000000000000000000000000001000004000", + "miner":"0xf36F155486299eCAff2D4F5160ed5114C1f66000", + "mixHash":"0x4d5d15ca1eda6bba206d8bb97a770167c04c49a46787c7c0568fdc02aeba432b", + "nonce":"0x0000000000000000", + "number":7517710, + "parentHash":"0xb0be07f2c34ebac06481618e957642a15c593083242395f473d76e01270ae309", + "receiptsRoot":"0x3dbe3153f6e06a6bf17ca3d386bf00750425dd6b76e802b0f7f4888972271dd0", + "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "size":10925, + "stateRoot":"0x9c2bb3ab08ec4936ba3782901101c282dc64f0705e20b6f09d354dbc40e84fe6", + "timestamp":1662145956, + "totalDifficulty":"10790000", + "transactions":[ + "0x2f5d625dfd7bc02add3061896a7e29d098b7dc71b6df355614b8213492696ffb", + "0xc5053eda6c5e111d76719f48b63facfea393ec7141201d4b97a8c5d25e68dc12", + "0xa1d61daa2ddd626e317a8f0fe20e6f7a3f6a28cfadb7145afc7e828310d526e1", + "0xb92d5a06a1d650885126eb6e77e77b17928fac0fa920a3be13e55ce7503c50c1", + "0x6312bb1438deecd3e8f5415a7d37ac1a7918d76adfe6c7360cac6281b23e1e03", + "0x6ccfbdb3bdce06c1976eee513566f9a822c7f043646c7858aeafde693a9a3866", + "0xb9ef13df852886f9c408feb788584b418845e648d3d9fad8f822fc6992e9ee17", + "0x047da374b18f729f1e10e6a417c7dd95391878d620a0cffa805fdd4a07f428b0" + ], + "transactionsRoot":"0xa45b7dc2896ae34bd76ceed185decbf2aa1316fc4b23b26d64cdb116692aa368", + "uncles":[ + + ] + }; + +var safebBockResult = { + "baseFeePerGas": "0xaf03", + "difficulty": "0x0", + "extraData": "0x", + "gasLimit": "0x1c9c380", + "gasUsed": "0x62d55d", + "hash": "0xcb51d46341a9a316f4e143b04be9e644b1e143e41848f7dd67e1b9442b4876cc", + "logsBloom": "0x00000000000002040010000004000000000000000000000000000000020200008000000000002000000000000003000200000000000010801004000000200000040000000001000100800008404000000000200002001000000020000801000800000000800000000000000000000000000010000000400000000090000100400041000000000000000000000800000004000020000000000008000000000010020000000000000000100000000000000000000002200000000000000000000000000002000000100000000010200000000000000000000000000000000000000a10000000010040001000000000000000000000000000000000001000004000", + "miner": "0xf36f155486299ecaff2d4f5160ed5114c1f66000", + "mixHash": "0x4d5d15ca1eda6bba206d8bb97a770167c04c49a46787c7c0568fdc02aeba432b", + "nonce": "0x0000000000000000", + "number": "0x72b60e", + "parentHash": "0xb0be07f2c34ebac06481618e957642a15c593083242395f473d76e01270ae309", + "receiptsRoot": "0x3dbe3153f6e06a6bf17ca3d386bf00750425dd6b76e802b0f7f4888972271dd0", + "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "size": "0x2aad", + "stateRoot": "0x9c2bb3ab08ec4936ba3782901101c282dc64f0705e20b6f09d354dbc40e84fe6", + "timestamp": "0x631255a4", + "totalDifficulty": "0xa4a470", + "transactions": [ + "0x2f5d625dfd7bc02add3061896a7e29d098b7dc71b6df355614b8213492696ffb", + "0xc5053eda6c5e111d76719f48b63facfea393ec7141201d4b97a8c5d25e68dc12", + "0xa1d61daa2ddd626e317a8f0fe20e6f7a3f6a28cfadb7145afc7e828310d526e1", + "0xb92d5a06a1d650885126eb6e77e77b17928fac0fa920a3be13e55ce7503c50c1", + "0x6312bb1438deecd3e8f5415a7d37ac1a7918d76adfe6c7360cac6281b23e1e03", + "0x6ccfbdb3bdce06c1976eee513566f9a822c7f043646c7858aeafde693a9a3866", + "0xb9ef13df852886f9c408feb788584b418845e648d3d9fad8f822fc6992e9ee17", + "0x047da374b18f729f1e10e6a417c7dd95391878d620a0cffa805fdd4a07f428b0" + ], + "transactionsRoot": "0xa45b7dc2896ae34bd76ceed185decbf2aa1316fc4b23b26d64cdb116692aa368", + "uncles": [] +}; + +var finalizedFormattedBlockResult = { + "baseFeePerGas":44968, + "difficulty":"0", + "extraData":"0x", + "gasLimit":30000000, + "gasUsed":7333379, + "hash":"0x7e382e9aa0e4dd7dab6db4b779f8f61c455042b1a823ccf58ffca6c9c2f2d275", + "logsBloom":"0x0404000000000000001088800000020200000000000001000000000000021000008204000802000240280100000400840004810000001084100410000420000000000001000100004080000800400000005020000000000000000000000100080000000000000000200000000000000400000002000040000000001000000040000100000000000000000000000010000000000020000101000800004000080002080000000000004000010000140000000000044000000001020000000000000000800a000000080000000000020000000000000000000200800000000000000010010000000000000000000000000000000042088000000000000000000000", + "miner":"0xf36F155486299eCAff2D4F5160ed5114C1f66000", + "mixHash":"0x1192b7ba3c7182ba4846966f806329c7be8b86ec8c71cf8249b241a97ba37cee", + "nonce":"0x0000000000000000", + "number":7517712, + "parentHash":"0xc9603eaeb821c54caa41572df370e93ee961d36da1f0f8a51c55482927d6aa39", + "receiptsRoot":"0xb8f30f6c441e5b084a5f0dba5a75e500c3c579029d578412a5c8a4c382af8360", + "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "size":22157, + "stateRoot":"0xf40589dfe7bbfbc5ce8fe46b2bc7a06514e409c1983d82943e986a338484d611", + "timestamp":1662145980, + "totalDifficulty":"10790000", + "transactions":[ + "0x3bde2fdd34b02f186d636c93158424f1b91ffbb45483fd7faadfbf07f33d0fe7", + "0x572e35d98626280598b4199d506ad49eeb38664abbec0ac054418327c7e0a324", + "0x1d349d2638aaefea8474adcd8c5780d8045f5e9c85c2768d18831511439bde75", + "0xa8d65c4d94ff8807e6ef5d51c41f6a022e34dfedf2d5e503ee6278417a8995b9", + "0x1438bca1e8a0d9afb6605f4bb2154fa57151a073e667e1029ce9ea9b2f0c6174", + "0xd0eef7f30f5709528b498e54ada5bbfc3507c446f95600722171f4cf95da41ce", + "0x900039cfea67e39af52571720f86fb3e08de9e1f69cb037fbe61229b46103b67", + "0x19c347dfae117e936b3310935286b33a3115db4d4adfa6cf3d6ee4e980c1122d" + ], + "transactionsRoot":"0x8e5f3c430729222e3b152ca820a44b3bec9ae8b3d5162b38f4d1365934d3fadf", + "uncles":[ + + ] + }; + +var finalizedbBockResult = { + "baseFeePerGas": "0xafa8", + "difficulty": "0x0", + "extraData": "0x", + "gasLimit": "0x1c9c380", + "gasUsed": "0x6fe603", + "hash": "0x7e382e9aa0e4dd7dab6db4b779f8f61c455042b1a823ccf58ffca6c9c2f2d275", + "logsBloom": "0x0404000000000000001088800000020200000000000001000000000000021000008204000802000240280100000400840004810000001084100410000420000000000001000100004080000800400000005020000000000000000000000100080000000000000000200000000000000400000002000040000000001000000040000100000000000000000000000010000000000020000101000800004000080002080000000000004000010000140000000000044000000001020000000000000000800a000000080000000000020000000000000000000200800000000000000010010000000000000000000000000000000042088000000000000000000000", + "miner": "0xf36f155486299ecaff2d4f5160ed5114c1f66000", + "mixHash": "0x1192b7ba3c7182ba4846966f806329c7be8b86ec8c71cf8249b241a97ba37cee", + "nonce": "0x0000000000000000", + "number": "0x72b610", + "parentHash": "0xc9603eaeb821c54caa41572df370e93ee961d36da1f0f8a51c55482927d6aa39", + "receiptsRoot": "0xb8f30f6c441e5b084a5f0dba5a75e500c3c579029d578412a5c8a4c382af8360", + "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "size": "0x568d", + "stateRoot": "0xf40589dfe7bbfbc5ce8fe46b2bc7a06514e409c1983d82943e986a338484d611", + "timestamp": "0x631255bc", + "totalDifficulty": "0xa4a470", + "transactions": [ + "0x3bde2fdd34b02f186d636c93158424f1b91ffbb45483fd7faadfbf07f33d0fe7", + "0x572e35d98626280598b4199d506ad49eeb38664abbec0ac054418327c7e0a324", + "0x1d349d2638aaefea8474adcd8c5780d8045f5e9c85c2768d18831511439bde75", + "0xa8d65c4d94ff8807e6ef5d51c41f6a022e34dfedf2d5e503ee6278417a8995b9", + "0x1438bca1e8a0d9afb6605f4bb2154fa57151a073e667e1029ce9ea9b2f0c6174", + "0xd0eef7f30f5709528b498e54ada5bbfc3507c446f95600722171f4cf95da41ce", + "0x900039cfea67e39af52571720f86fb3e08de9e1f69cb037fbe61229b46103b67", + "0x19c347dfae117e936b3310935286b33a3115db4d4adfa6cf3d6ee4e980c1122d" + ], + "transactionsRoot": "0x8e5f3c430729222e3b152ca820a44b3bec9ae8b3d5162b38f4d1365934d3fadf", + "uncles": [] +}; + var tests = [{ args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', false], @@ -127,6 +263,18 @@ var tests = [{ result: blockResultWithTx, formattedResult: formattedBlockResultWithTx, call: 'eth_'+ method + 'ByNumber' +},{ + args: ['safe', false], + formattedArgs: ['safe', false], + result: safebBockResult, + formattedResult: safeFormattedBlockResult, + call: 'eth_'+ method + 'ByNumber' +},{ + args: ['finalized', false], + formattedArgs: ['finalized', false], + result: finalizedbBockResult, + formattedResult: finalizedFormattedBlockResult, + call: 'eth_'+ method + 'ByNumber' }]; testMethod.runTests('eth', method, tests); diff --git a/test/eth.getBlockTransactionCount.js b/test/eth.getBlockTransactionCount.js index e443250d5d3..66fb182cbe4 100644 --- a/test/eth.getBlockTransactionCount.js +++ b/test/eth.getBlockTransactionCount.js @@ -27,6 +27,18 @@ var tests = [{ result: '0xb', formattedResult: 11, call: 'eth_getBlockTransactionCountByNumber' +},{ + args: ['safe'], + formattedArgs: ['safe'], + result: '0xb', + formattedResult: 11, + call: 'eth_getBlockTransactionCountByNumber' +},{ + args: ['finalized'], + formattedArgs: ['finalized'], + result: '0xb', + formattedResult: 11, + call: 'eth_getBlockTransactionCountByNumber' }]; testMethod.runTests('eth', method, tests); diff --git a/test/eth.getCode.js b/test/eth.getCode.js index 548ecc04ac3..3a521c0085d 100644 --- a/test/eth.getCode.js +++ b/test/eth.getCode.js @@ -24,6 +24,18 @@ var tests = [{ result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', call: 'eth_'+ method +},{ + args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'safe'], + formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'safe'], + result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', + formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', + call: 'eth_'+ method +},{ + args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'finalized'], + formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'finalized'], + result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', + formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', + call: 'eth_'+ method }]; testMethod.runTests('eth', method, tests); diff --git a/test/eth.getProof.js b/test/eth.getProof.js index 412d9b27972..b22e2492825 100644 --- a/test/eth.getProof.js +++ b/test/eth.getProof.js @@ -81,7 +81,19 @@ var tests = [{ result: proof, formattedResult: formattedProof, call: call -} +}, { + args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', [0xb], 'safe'], + formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', ['0xb'], 'safe'], + result: proof, + formattedResult: formattedProof, + call: call +}, { + args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', [0xb], 'finalized'], + formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', ['0xb'], 'finalized'], + result: proof, + formattedResult: formattedProof, + call: call +}, // TODO: Fixing of test.method.js cloning of the arguments does destroy the BN object. Manually calling of utils.numberToHex with new BN('11'); does work. // , { // args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', [new BN('11')], 'latest'], diff --git a/test/eth.getStorageAt.js b/test/eth.getStorageAt.js index f8997131aaf..b0029ea9547 100644 --- a/test/eth.getStorageAt.js +++ b/test/eth.getStorageAt.js @@ -48,6 +48,18 @@ var tests = [{ result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', call: 'eth_'+ method +}, { + args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0xb', 'finalized'], + formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0xb', 'finalized'], + result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', + formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', + call: 'eth_'+ method +}, { + args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0xb', 'safe'], + formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0xb', 'safe'], + result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', + formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', + call: 'eth_'+ method } // TODO: Fixing of test.method.js cloning of the arguments does destroy the BN object. Manually calling of utils.numberToHex with new BN('11'); does work. // , { diff --git a/test/formatters.inputDefaultBlockFormatter.js b/test/formatters.inputDefaultBlockFormatter.js index 41714929204..6efe9be618b 100644 --- a/test/formatters.inputDefaultBlockFormatter.js +++ b/test/formatters.inputDefaultBlockFormatter.js @@ -8,7 +8,9 @@ var tests = [ { value: 'pending', expected: 'pending' }, { value: 'earliest', expected: 'earliest' }, { value: 1, expected: '0x1' }, - { value: '0x1', expected: '0x1' } + { value: '0x1', expected: '0x1' }, + { value: 'safe', expected: 'safe' }, + { value: 'finalized', expected: 'finalized' } ]; describe('lib/web3/formatters', function () { diff --git a/test/formatters.inputLogFormatter.js b/test/formatters.inputLogFormatter.js index eee62490e39..284541468b8 100644 --- a/test/formatters.inputLogFormatter.js +++ b/test/formatters.inputLogFormatter.js @@ -105,4 +105,36 @@ describe('InputLogFormatterTest', function() { address: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' }); }); + + it('call inputLogFormatter with a valid log and safe tag toBlock', function() { + var log = { + fromBlock: 'earliest', + toBlock: 'safe', + topics: ['0x0'], + address: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078' + }; + + assert.deepEqual(formatters.inputLogFormatter(log), { + fromBlock: 'earliest', + toBlock: 'safe', + topics: ['0x0'], + address: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' + }); + }); + + it('call inputLogFormatter with a valid log and finalized tag toBlock', function() { + var log = { + fromBlock: 'earliest', + toBlock: 'finalized', + topics: ['0x0'], + address: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078' + }; + + assert.deepEqual(formatters.inputLogFormatter(log), { + fromBlock: 'earliest', + toBlock: 'finalized', + topics: ['0x0'], + address: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' + }); + }); }); diff --git a/test/utils.compareBlockNumbers.js b/test/utils.compareBlockNumbers.js index 71617da26b5..6f19c060d5e 100644 --- a/test/utils.compareBlockNumbers.js +++ b/test/utils.compareBlockNumbers.js @@ -7,6 +7,8 @@ const pending = "pending"; const latest = "latest"; const genesis = "genesis"; const earliest = "earliest"; +const finalized = "finalized"; +const safe = "safe"; const tests = [ // Base cases for numbers @@ -27,6 +29,8 @@ const tests = [ { input: {a: earliest, b: 0}, result: 0 }, { input: {a: latest, b: latest}, result: 0 }, { input: {a: pending, b: pending}, result: 0 }, + { input: {a: finalized, b: finalized}, result: 0 }, + { input: {a: safe, b: safe}, result: 0 }, // Complex Strings // Genesis { input: {a: earliest, b: 2}, result: -1 }, @@ -37,6 +41,10 @@ const tests = [ { input: {a: genesis, b: new BN(2)}, result: -1 }, { input: {a: genesis, b: latest}, result: -1 }, { input: {a: genesis, b: pending}, result: -1 }, + { input: {a: earliest, b: finalized}, result: -1 }, + { input: {a: genesis, b: finalized}, result: -1 }, + { input: {a: earliest, b: safe}, result: -1 }, + { input: {a: genesis, b: safe}, result: -1 }, // latest { input: {a: latest, b: 0}, result: 1 }, { input: {a: latest, b: new BN(1)}, result: 1 }, @@ -44,13 +52,32 @@ const tests = [ // pending { input: {a: pending, b: 0}, result: 1 }, { input: {a: pending, b: new BN(1)}, result: 1 }, + { input: {a: finalized, b: 0}, result: 1 }, + { input: {a: finalized, b: new BN(1)}, result: 1 }, + // finalized + { input: {a: finalized, b: 0}, result: 1 }, + { input: {a: finalized, b: new BN(1)}, result: 1 }, + { input: {a: finalized, b: pending}, result: -1 }, + { input: {b: finalized, a: 0}, result: -1 }, + { input: {b: finalized, a: new BN(1)}, result: -1 }, + { input: {b: finalized, a: pending}, result: 1 }, + // safe + { input: {a: safe, b: 0}, result: 1 }, + { input: {a: safe, b: pending}, result: -1 }, + { input: {b: safe, a: 0}, result: -1 }, + { input: {b: safe, a: pending}, result: 1 }, + { input: {a: safe, b: 23}, result: undefined }, + { input: {a: 5000, b: safe}, result: undefined }, + { input: {a: safe, b: new BN(1322)}, result: undefined }, + { input: {a: new BN(123), b: safe}, result: undefined }, + ]; describe('formatters', function () { describe('compare blocknumbers', function () { tests.forEach(function(test){ it('should return the correct value', function () { - assert.deepEqual(formatters.compareBlockNumbers(test.input.a, test.input.b), test.result); + assert.deepEqual(formatters.compareBlockNumbers(test.input.a, test.input.b), test.result); }); }); });