Skip to content

Commit

Permalink
Merge pull request #3247 from ethereum/issue/3244
Browse files Browse the repository at this point in the history
fixes defaultBlock property handling
  • Loading branch information
nivida authored Nov 29, 2019
2 parents 02b695a + ddf979f commit 043cb81
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 42 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ Released with 1.0.0-beta.37 code base.

### Changed

- ``eth-lib`` dependency updated (0.2.7 => 0.2.8) (#3242)
- ``eth-lib`` dependency updated (0.2.7 => ^0.2.8) (#3242)

### Fixed

- ``defaultBlock`` property handling fixed (#3247)
- ``clearSubscriptions`` does no longer throw an error if no running subscriptions do exist (#3246)
3 changes: 2 additions & 1 deletion docs/web3-eth-contract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"``

Expand Down
3 changes: 2 additions & 1 deletion docs/web3-eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"``

Expand Down
19 changes: 12 additions & 7 deletions packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export interface LogsOptions {
topics?: Array<string | string[] | null>;
}

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
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-contract/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-contract/types/tests/contract-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const contract = new Contract([]);
// $ExpectType string | null
contract.defaultAccount;

// $ExpectType string | number
// $ExpectType BlockNumber
contract.defaultBlock;

// $ExpectType Common
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 7 additions & 13 deletions packages/web3-eth/types/tests/eth.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');

Expand Down
37 changes: 23 additions & 14 deletions test/eth.getBalance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/formatters.inputDefaultBlockFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
];
Expand Down
42 changes: 42 additions & 0 deletions test/helpers/test.method.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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); });
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 043cb81

Please sign in to comment.