From 6432b70f7d66c0cc5a18cead7afa7d45e5bc4301 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 28 Nov 2019 10:05:08 +0100 Subject: [PATCH 1/5] defaultBlock property handling, related types, and documentation updated --- docs/web3-eth-contract.rst | 3 ++- docs/web3-eth.rst | 3 ++- packages/web3-core-helpers/src/formatters.js | 19 ++++++++++++------- packages/web3-core/types/index.d.ts | 2 +- packages/web3-eth-contract/types/index.d.ts | 2 +- packages/web3-eth/types/index.d.ts | 2 +- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/docs/web3-eth-contract.rst b/docs/web3-eth-contract.rst index 5e22ce4aa8e..43db0d793c1 100644 --- a/docs/web3-eth-contract.rst +++ b/docs/web3-eth-contract.rst @@ -140,10 +140,11 @@ Property The default block parameters can be one of the following: -- ``Number``: A block number +- ``Number|BN|BigNumber``: A block number - ``"genesis"`` - ``String``: The genesis block - ``"latest"`` - ``String``: The latest block (current head of the blockchain) - ``"pending"`` - ``String``: The currently mined block (including pending transactions) +- ``"earliest"`` - ``String``: The genesis block Default is ``"latest"`` diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 3a5f25c3799..905af97035d 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -187,10 +187,11 @@ Property Default block parameters can be one of the following: -- ``Number``: A block number +- ``Number|BN|BigNumber``: A block number - ``"genesis"`` - ``String``: The genesis block - ``"latest"`` - ``String``: The latest block (current head of the blockchain) - ``"pending"`` - ``String``: The currently mined block (including pending transactions) +- ``"earliest"`` - ``String``: The genesis block Default is ``"latest"`` diff --git a/packages/web3-core-helpers/src/formatters.js b/packages/web3-core-helpers/src/formatters.js index d96a17330fb..41d8d69e1fa 100644 --- a/packages/web3-core-helpers/src/formatters.js +++ b/packages/web3-core-helpers/src/formatters.js @@ -95,27 +95,32 @@ var isPredefinedBlockNumber = function (blockNumber) { */ var inputDefaultBlockNumberFormatter = function (blockNumber) { if (this && (blockNumber === undefined || blockNumber === null)) { - return this.defaultBlock; - } - if (blockNumber === 'genesis' || blockNumber === 'earliest') { - return '0x0'; + return inputBlockNumberFormatter(this.defaultBlock); } + return inputBlockNumberFormatter(blockNumber); }; /** * Returns the given block number as hex string or the predefined block number 'latest', 'pending', 'earliest', 'genesis' * - * @param {String|Number|undefined} blockNumber + * @param {String|Number|BN|BigNumber} blockNumber * - * @returns {String|Number|BN|BigNumber} + * @returns {String} */ var inputBlockNumberFormatter = function (blockNumber) { if (blockNumber === undefined) { return undefined; - } else if (isPredefinedBlockNumber(blockNumber)) { + } + + if (isPredefinedBlockNumber(blockNumber)) { return blockNumber; } + + if (blockNumber === 'genesis') { + return '0x0'; + } + return (utils.isHexStrict(blockNumber)) ? ((_.isString(blockNumber)) ? blockNumber.toLowerCase() : blockNumber) : utils.numberToHex(blockNumber); }; diff --git a/packages/web3-core/types/index.d.ts b/packages/web3-core/types/index.d.ts index e4c561b19c0..06c23cccd8c 100644 --- a/packages/web3-core/types/index.d.ts +++ b/packages/web3-core/types/index.d.ts @@ -408,7 +408,7 @@ export interface LogsOptions { topics?: Array; } -export type BlockNumber = string | number | BN | BigNumber | 'latest' | 'pending' | 'earliest'; +export type BlockNumber = string | number | BN | BigNumber | 'latest' | 'pending' | 'earliest' | 'genesis'; export type provider = | HttpProvider diff --git a/packages/web3-eth-contract/types/index.d.ts b/packages/web3-eth-contract/types/index.d.ts index a9e2dabf478..6f141abaf35 100644 --- a/packages/web3-eth-contract/types/index.d.ts +++ b/packages/web3-eth-contract/types/index.d.ts @@ -31,7 +31,7 @@ export class Contract { private _address: string; private _jsonInterface: AbiItem[]; defaultAccount: string | null; - defaultBlock: string | number; + defaultBlock: BlockNumber; defaultCommon: Common; defaultHardfork: hardfork; defaultChain: chain; diff --git a/packages/web3-eth/types/index.d.ts b/packages/web3-eth/types/index.d.ts index 83d67ffd0ce..e4c685caa14 100644 --- a/packages/web3-eth/types/index.d.ts +++ b/packages/web3-eth/types/index.d.ts @@ -80,7 +80,7 @@ export class Eth { readonly givenProvider: any; static readonly givenProvider: any; defaultAccount: string | null; - defaultBlock: string | number; + defaultBlock: BlockNumber; defaultCommon: Common; defaultHardfork: hardfork; defaultChain: chain; From bb0ddf463c646ec78a0eb7ba6498330b5510f375 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 28 Nov 2019 10:13:50 +0100 Subject: [PATCH 2/5] types tests updated --- .../types/tests/contract-test.ts | 2 +- packages/web3-eth/types/tests/eth.tests.ts | 20 +++++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/packages/web3-eth-contract/types/tests/contract-test.ts b/packages/web3-eth-contract/types/tests/contract-test.ts index 631e08e88ad..e8e10a72f5e 100644 --- a/packages/web3-eth-contract/types/tests/contract-test.ts +++ b/packages/web3-eth-contract/types/tests/contract-test.ts @@ -25,7 +25,7 @@ const contract = new Contract([]); // $ExpectType string | null contract.defaultAccount; -// $ExpectType string | number +// $ExpectType BlockNumber contract.defaultBlock; // $ExpectType Common diff --git a/packages/web3-eth/types/tests/eth.tests.ts b/packages/web3-eth/types/tests/eth.tests.ts index c62a89c8365..c47603d6d39 100644 --- a/packages/web3-eth/types/tests/eth.tests.ts +++ b/packages/web3-eth/types/tests/eth.tests.ts @@ -43,10 +43,16 @@ const eth_empty = new Eth(); // $ExpectType Eth const eth = new Eth('http://localhost:8545'); +// $ExpectType provider +eth.currentProvider; + +// $ExpectType any +eth.givenProvider; + // $ExpectType string | null eth.defaultAccount; -// $ExpectType string | number +// $ExpectType BlockNumber eth.defaultBlock; // $ExpectType Common @@ -123,18 +129,6 @@ eth.subscribe( (error: Error, transactionHash: string) => {} ); -// $ExpectType provider -eth.currentProvider; - -// $ExpectType any -eth.givenProvider; - -// $ExpectType string | null -eth.defaultAccount; - -// $ExpectType string | number -eth.defaultBlock; - // $ExpectType boolean eth.setProvider('https://localhost:2100'); From dfdd52d42f109edfa9ff17a2df502ad569640013 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 28 Nov 2019 10:17:01 +0100 Subject: [PATCH 3/5] inputDefaultBlockFormatter test updated --- test/formatters.inputDefaultBlockFormatter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/formatters.inputDefaultBlockFormatter.js b/test/formatters.inputDefaultBlockFormatter.js index 40b132ab810..41714929204 100644 --- a/test/formatters.inputDefaultBlockFormatter.js +++ b/test/formatters.inputDefaultBlockFormatter.js @@ -6,7 +6,7 @@ var tests = [ { value: 'genesis', expected: '0x0' }, { value: 'latest', expected: 'latest' }, { value: 'pending', expected: 'pending' }, - { value: 'earliest', expected: '0x0' }, + { value: 'earliest', expected: 'earliest' }, { value: 1, expected: '0x1' }, { value: '0x1', expected: '0x1' } ]; From 2c3b9d105325cbdd0f1a02060fd85b7bcc177002 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 28 Nov 2019 10:18:25 +0100 Subject: [PATCH 4/5] CHANGELOG.md updated --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91679178aef..2f95e7a6250 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,3 +109,5 @@ Released with 1.0.0-beta.37 code base. - ``getPendingTransactions`` added to web3-eth package (#3239) ### Fixed + +- ``defaultBlock`` property handling fixed (#3247) From db1f3c470ce7711df063c3d9c974a5f2adfd0883 Mon Sep 17 00:00:00 2001 From: nivida Date: Fri, 29 Nov 2019 09:13:10 +0100 Subject: [PATCH 5/5] test helper test.method.js extended with the possibility to pass custom default options and eth.getBalance tests extended with a defaultBlock fallback case --- test/eth.getBalance.js | 37 +++++++++++++++++++------------- test/helpers/test.method.js | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/test/eth.getBalance.js b/test/eth.getBalance.js index e7599c46868..77e8cfc8f14 100644 --- a/test/eth.getBalance.js +++ b/test/eth.getBalance.js @@ -10,74 +10,83 @@ var tests = [{ formattedArgs: ['0x000000000000000000000000000000000000012d', '0x2'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method -},{ + call: 'eth_' + method +}, { args: ['0x000000000000000000000000000000000000012d', '0x1'], formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['0x000000000000000000000000000000000000012d', 0x1], formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['0x000000000000000000000000000000000000012d'], formattedArgs: ['0x000000000000000000000000000000000000012d', eth.defaultBlock], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['0XDBDBDB2CBD23B783741E8D7FCF51E459B497E4A6', 0x1], formattedArgs: ['0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', '0x1'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['0xdbdbdB2cBD23b783741e8d7fcF51e459b497e4a6', 0x1], // checksum address formattedArgs: ['0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', '0x1'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, - { +{ args: ['0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', 0x1], formattedArgs: ['0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', '0x1'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', 0x1], formattedArgs: ['0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', '0x1'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['0x000000000000000000000000000000000000012d', 0x1], formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['0x000000000000000000000000000000000000012d'], formattedArgs: ['0x000000000000000000000000000000000000012d', 'latest'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['000000000000000000000000000000000000012d'], formattedArgs: ['0x000000000000000000000000000000000000012d', 'latest'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method }, { args: ['XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS'], // iban address formattedArgs: ['0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 'latest'], result: '0x31981', formattedResult: '203137', - call: 'eth_'+ method + call: 'eth_' + method +}, { + args: ['0x000000000000000000000000000000000000012d'], + formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'], + result: '0x31981', + formattedResult: '203137', + call: 'eth_' + method, + defaultOptions: [ + ['defaultBlock', 1] + ] }]; testMethod.runTests('eth', method, tests); diff --git a/test/helpers/test.method.js b/test/helpers/test.method.js index 0d0e4300c49..3afa2958e5c 100644 --- a/test/helpers/test.method.js +++ b/test/helpers/test.method.js @@ -104,8 +104,20 @@ var runTests = function (obj, method, tests) { w3 = web3[obj]; } + if (test.defaultOptions) { + test.defaultOptions.forEach(function(option) { + w3[option[0]] = option[1]; + }); + } + assert.throws(function(){ w3[method].apply(w3, args); }); } else { + if (test.defaultOptions) { + test.defaultOptions.forEach(function(option) { + w3[option[0]] = option[1]; + }); + } + assert.throws(function(){ web3[method].apply(web3, args); }); } @@ -120,8 +132,20 @@ var runTests = function (obj, method, tests) { w3 = web3[obj]; } + if (test.defaultOptions) { + test.defaultOptions.forEach(function(option) { + w3[option[0]] = option[1]; + }); + } + result = w3[method].apply(w3, args); } else { + if (test.defaultOptions) { + test.defaultOptions.forEach(function(option) { + w3[option[0]] = option[1]; + }); + } + result = web3[method].apply(web3, args); } @@ -186,6 +210,12 @@ var runTests = function (obj, method, tests) { w3 = web3[obj]; } + if (test.defaultOptions) { + test.defaultOptions.forEach(function(option) { + w3[option[0]] = option[1]; + }); + } + assert.throws(function(){ w3[method].apply(w3, args); }); } else { assert.throws(function(){ web3[method].apply(web3, args); }); @@ -209,8 +239,20 @@ var runTests = function (obj, method, tests) { w3 = web3[obj]; } + if (test.defaultOptions) { + test.defaultOptions.forEach(function(option) { + w3[option[0]] = option[1]; + }); + } + w3[method].apply(w3, args); } else { + if (test.defaultOptions) { + test.defaultOptions.forEach(function(option) { + w3[option[0]] = option[1]; + }); + } + web3[method].apply(web3, args); } }