Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block timestamp - "Error: Number can only safely store up to 53 bits" #1905

Closed
fritzmatias opened this issue Aug 28, 2018 · 6 comments · Fixed by #2791
Closed

block timestamp - "Error: Number can only safely store up to 53 bits" #1905

fritzmatias opened this issue Aug 28, 2018 · 6 comments · Fixed by #2791
Labels
Bug Addressing a bug
Projects

Comments

@fritzmatias
Copy link

fritzmatias commented Aug 28, 2018

Hi, this issue is related to 1,2. And because it is a problem related to 3rd parties implementation of the blockchain, must be the responsibility of them to manage the particular implementation of the precision related to the timestamp.
As the documentation defines, the timestamp must be interpreted as unix time, but currently, an error breaks the execution of the application (if the timestamp value is bigger than expected) throwing an error.
It is expected that the block timestamp data could change over time --given the limitations of unix time spec-- but the behavior of the web3 must not break because of it.

Finally, the problem is related to a timestamp value bigger than a 32bit representation value. So, the right solution must be managing the timestamp as BN (in little endian) and then convert it to the first 32bit representable values by the developer.
This means that a decimal number with more than 10 digits must be truncated to be interpreted as 32bit length.

My solution at release @beta.35 file web3-core-helpers/src/formatters.js 235

< block.timestamp = utils.hexToNumber(block.timestamp);
> block.timestamp = outputBigNumberFormatter(block.timestamp);

at relase @beta.36 file web3-core-helpers/src/formatters.js:185

< block.timestamp = utils.toDecimal(block.timestamp);
> block.timestamp = utils.toBigNumber(block.timestamp);

a substring(0,10) operation is not recomended here to avoid problems with big-endian implementations

Example:
Phisical block: {"difficulty":"0x20000","extraData":"0x","gasLimit":"0xdee8afcd","gasUsed":"0xc705","hash":"0x71ef9a9e5e3575b9861e81da7ebd0fe465879602f6aff27964d9de6bd62e8bdd","logsBloom":"0x00000000000000000000000000000000000000080000000000000000000000000000000008000000000000000000000000000000000000000000400000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000010000000080000000000000000000000000000000000000000000000000021000000000002000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x5","parentHash":"0xcc0cd85704ccee6615b5d4a182c18d98080685acee0eb6aeb2816b5c14ebb26e","receiptsRoot":"0x90889007d6b3836f3bbdaa22ed5cae062ccfc6c5758fc92e9e042b715f10cc26","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x2ae","stateRoot":"0xdf0a5715a8e861f689d3262b84fd02a95bbe76c536501cc34b519a3d051813a6","timestamp":"0x154f146681969c9e","totalDifficulty":"0xa0000","transactions":["0x406cdd51e197c04cc31b9bf0548d61f411375b7c63791f155d297e14402ed0bc"],"transactionsRoot":"0x513fe6d019912f97352635b19bb0144d035752c320a41e17680382237289c878","uncles":[]}

Help Logs:
timestamp (orig): 0x154f146681969c9e
BigNumber: 1535468428449979550 timestamp: 1535468428 (equal to BigNumber.substring(0,10))
timestamp date: 2018-08-28T15:00:28.000Z
jstimestamp: 1535468428449

At the application level: Using a wrapper to override the buggy function.

wrapperFile.js

// Bug Fix on web3 code version @1.0.0.beta.35
// Override Dependencies 
var _ = require('underscore');
var utils = require('web3-utils');
/**
 * Same as web3-utils formatters, copied & pasted.
 * @param {String} number 
 */
var outputBigNumberFormatter = function (number) {
    return utils.toBN(number).toString(10);
};
/**
 * @override
 * Formats the output of a block to its proper values
 *
 * @method outputBlockFormatter
 * @param {Object} block
 * @returns {Object}
*/
function outputBlockFormatterOverride(block){
   console.log("WARN: Override of internal web3 function."); 
   // transform to number
   block.gasLimit = utils.hexToNumber(block.gasLimit);
   block.gasUsed = utils.hexToNumber(block.gasUsed);
   block.size = utils.hexToNumber(block.size);
   // don't do a substring(0,10) here, parse the timestamp outside this function.
   block.timestamp = outputBigNumberFormatter(block.timestamp);
   if (block.number !== null)
       block.number = utils.hexToNumber(block.number);

   if(block.difficulty)
       block.difficulty = outputBigNumberFormatter(block.difficulty);
   if(block.totalDifficulty)
       block.totalDifficulty = outputBigNumberFormatter(block.totalDifficulty);

   if (_.isArray(block.transactions)) {
       block.transactions.forEach(function(item){
           if(!_.isString(item))
               return outputTransactionFormatter(item);
       });
   }

   if (block.miner)
       block.miner = utils.toChecksumAddress(block.miner);

   return block;
}
// trying to force override and cache of helpers 
var helpers = require('web3-core-helpers');
helpers.formatters.outputBlockFormatter=outputBlockFormatterOverride;
const web3Client =require( 'web3');
@thekevinbrown
Copy link

I'm having the same issue when using Truffle and Ganache. I fast forward time to simulate things happening in the future, and then hit this error.

@nivida nivida added the Needs Clarification Requires additional input label Nov 28, 2018
@nivida nivida added this to To do in 1.0 Nov 28, 2018
@usd-yamazaki
Copy link

I am having the same problem with "block.gasLimit".
Since the theoretical upper limit of gas is 0x7fffffffffffffff, 53bit is not sufficient.

block.gasLimit = utils.hexToNumber (block.gasLimit);

@nivida
Copy link
Contributor

nivida commented Apr 16, 2019

Does this error still occur with the latest version of Web3?

@nivida nivida added more information needed and removed Needs Clarification Requires additional input labels Apr 16, 2019
@red-0ne
Copy link

red-0ne commented Apr 29, 2019

This bug still exists. It happens for us when we launched geth with --miner.gastarget to a too high value for javascript.

@nivida nivida added Bug Addressing a bug and removed more information needed labels Apr 29, 2019
@nivida nivida moved this from Proposed To Do's to Accepted To Do's in 1.0 Apr 29, 2019
@nivida nivida moved this from To Do's to In progress in 1.0 May 2, 2019
@jimmy-dg
Copy link

jimmy-dg commented May 7, 2019

Hi @nivida this bug still exist in latest web3js release (beta.54).

I'm getting the exact same error reported here and in #2766

Versions
web3.js: 1.0.0-beta.54
nodejs: v10.15.0
Quorum node: 2.1.1 (reproduced in 2.2.1)

@nivida nivida mentioned this issue May 7, 2019
12 tasks
1.0 automation moved this from In progress to Done May 8, 2019
@nivida nivida mentioned this issue May 9, 2019
12 tasks
@snoeber
Copy link

snoeber commented Jul 2, 2019

Hi @nivida ,

this issue still exists for me in beta.55 on Quorum with this block:
{
difficulty: 1,
extraData: "0xd78301080c846765746887676f312e392e35856c696e75780000000000000000f8b3ea942d3cb3d83ab7366d3427eeb0738e91c212397d5b94b87dc349944cc47474775dde627a8a171fc94532b841f545e1f5297c72b08bf716d6b352abaa355ccbaa830244d83cd92b9a1793cb794abf72f7a3369af88f84d0bbc82f440eff162b66bd90571192edea3d9ba84fb900f843b8419fce7d7aca45a0247cac66b5563e094b898a5fda1df844464af3f160ef04748d518799a120402d7aefd0a73f341843acf59da924969f4462705a7f5ddf3b79ab00",
gasLimit: 9007439183264788,
gasUsed: 0,
hash: "0xc052e2d19afd0fa5e7ac856f488bf608e9613b3e211e22b90e82f59c02ed8d58",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x0000000000000000000000000000000000000000",
mixHash: "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
nonce: "0x0000000000000000",
number: 66492,
parentHash: "0xb5e74e601df7c314f11babf05152c89ceb401f77b4ab59c2f48b5fc843a25c50",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 729,
stateRoot: "0x59dbebb0c8951ee8dfd1508a2f98739fca468588232979485f3c37674ea22329",
timestamp: 1548847241,
totalDifficulty: 66493,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}

eth.getBlock(66492, true)
{
difficulty: 1,
extraData: "0xd78301080c846765746887676f312e392e35856c696e75780000000000000000f8b3ea942d3cb3d83ab7366d3427eeb0738e91c212397d5b94b87dc349944cc47474775dde627a8a171fc94532b841f545e1f5297c72b08bf716d6b352abaa355ccbaa830244d83cd92b9a1793cb794abf72f7a3369af88f84d0bbc82f440eff162b66bd90571192edea3d9ba84fb900f843b8419fce7d7aca45a0247cac66b5563e094b898a5fda1df844464af3f160ef04748d518799a120402d7aefd0a73f341843acf59da924969f4462705a7f5ddf3b79ab00",
gasLimit: 9007439183264788,
gasUsed: 0,
hash: "0xc052e2d19afd0fa5e7ac856f488bf608e9613b3e211e22b90e82f59c02ed8d58",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x0000000000000000000000000000000000000000",
mixHash: "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
nonce: "0x0000000000000000",
number: 66492,
parentHash: "0xb5e74e601df7c314f11babf05152c89ceb401f77b4ab59c2f48b5fc843a25c50",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 729,
stateRoot: "0x59dbebb0c8951ee8dfd1508a2f98739fca468588232979485f3c37674ea22329",
timestamp: 1548847241,
totalDifficulty: 66493,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}

Could you please double check?

Thank you in advance,
kc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Addressing a bug
Projects
No open projects
1.0
  
Done
Development

Successfully merging a pull request may close this issue.

7 participants