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

from-rpc does not convert difficulty right if difficulty is a number #726

Closed
jochem-brouwer opened this issue Apr 24, 2020 · 7 comments
Closed

Comments

@jochem-brouwer
Copy link
Member

jochem-brouwer commented Apr 24, 2020

Geth's web3.eth.getBlock returns difficulty as a number. One has to manually convert this to hex before feeding it to from-rpc.

rom_RPC without difficulty to hex conversion 0x32333032393139373438313334363734 actual difficulty: 2302919748134674 number 9643516
from_RPC with difficulty to hex conversion 0x82e7e4988a712 actual difficulty: 0x82e7e4988a712 number 9643516

If you feed these to blockchain you get an Invalid difficulty error.

@jochem-brouwer
Copy link
Member Author

This seems to have to do with types, we should either enforce that users only input Buffers/hex strings or we should manually convert numbers to hex strings if that's the case. (It seems like ethereumjs-util will convert them to Buffers). I think we should do the latter, we can check if something is a number and then convert it to a hex string.

@jochem-brouwer
Copy link
Member Author

jochem-brouwer commented Sep 19, 2020

Right now on the latest web3 version and on return values of web3.eth.getBlock(x) from a Geth node version 1.9.19 the problems are:

  • difficulty of Block
  • gasPrice value of Transaction
  • value of Transaction

In general, maybe strings which are not 0x-prefixed should be assumed as decimal number strings and thus treated as such?

Also adding TX here since TX throws the errors regarding gasPrice and value.

@holgerd77
Copy link
Member

Is this still an issue?

@jochem-brouwer
Copy link
Member Author

Yes this is still an issue. Should be easy to fix though.

@scorbajio
Copy link
Contributor

scorbajio commented May 13, 2021

Hello! I am a new contributor and am looking forward to working with the developer community. I have opened PR #1254 to begin addressing this issue. Please let me know if you have any ideas on how to improve the approach I took to checking and converting the difficulty parameter for the block package before I apply it to the gasPrice and value parameters of the tx package.

@scorbajio
Copy link
Contributor

If you feed these to blockchain you get an Invalid difficulty error.

@jochem-brouwer Passing a JSON object that has a difficulty parameter of type decimal string throws an error in block:

Error: Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: 82459156443011
    at Object.exports.toBuffer (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/packages/util/src/bytes.ts:141:13)
    at Function.fromHeaderData (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/packages/block/src/header.ts:83:27)
    at Object.blockHeaderFromRpc [as default] (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/packages/block/src/header-from-rpc.ts:51:35)
    at Object.blockFromRpc [as default] (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/packages/block/src/from-rpc.ts:33:36)
    at Test.<anonymous> (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/packages/block/test/from-rpc.spec.ts:42:31)
    at Test.bound [as _cb] (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/node_modules/tape/lib/test.js:80:32)
    at Test.run (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/node_modules/tape/lib/test.js:96:10)
    at Test.bound [as run] (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/node_modules/tape/lib/test.js:80:32)
    at Test._end (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/node_modules/tape/lib/test.js:165:11)
    at Test.bound [as _end] (/home/indigoomega021/code/projects/ethereumjs/ethereumjs-monorepo/node_modules/tape/lib/test.js:80:32)

Here is the JSON object that resulted in the error, for reference:

{
  "author": "0x4bb96091ee9d802ed039c4d1a5f6216f90f81b01",
  "difficulty": "82459156443011",
  "extraData": "0x657468706f6f6c202d20555331",
  "gasLimit": "0x3d37da",
  "gasUsed": "0x1551b",
  "hash": "0xc596cb892b649b4917da8c6b78611346d55daf7bcf4375da86a2d98810888e84",
  "logsBloom": "0x00000000004000000000000000000000000000000400000000000000000000000000000000000000000000000080000008000000000000000000000000000002000000000000000000000080000000000001800000000000000000008000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000004000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020001000000000000000000000000000000000000000000100000010000000000000000000000400000400000000000000000",
  "miner": "0x4bb96091ee9d802ed039c4d1a5f6216f90f81b01",
  "mixHash": "0xcb3723ab82e84594d0ec9ed5a45fd976d5aba09903826e5ed5e06ae893011eaa",
  "nonce": "0xa76a9a500301e044",
  "number": "0x2ca14a",
  "parentHash": "0x24f155bdf17217767531f464636fe4b84b87a38c53127541f952b3052adbac95",
  "receiptsRoot": "0x0eb6a50257911130ea2fa2e500e71e55e2dfc38eb97f64ca9cfca6faeabd693e",
  "sealFields": [
    "0xcb3723ab82e84594d0ec9ed5a45fd976d5aba09903826e5ed5e06ae893011eaa",
    "0xa76a9a500301e044"
  ],
  "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  "size": "0x3d7",
  "stateRoot": "0xdf97474b1b492cb6491cff267ca53c33aa42b611ba15239b4027a77275afeffc",
  "timestamp": "0x586afa54",
  "totalDifficulty": "0x61decf2ca7d9bbbf8",
  "transactions": [
    {
      "blockHash": "0xc596cb892b649b4917da8c6b78611346d55daf7bcf4375da86a2d98810888e84",
      "blockNumber": "0x2ca14a",
      "creates": null,
      "from": "0x41959417325160f8952bc933ae8317b4e5140dda",
      "gas": "0x5e1b",
      "gasPrice": "0x98bca5a00",
      "hash": "0x542e06b2b8beb71305bf1bfd4d2088da9848d8795971d93d5f7893ecceef095b",
      "input": "0x",
      "nonce": "0x0",
      "publicKey": "0x0ddc5f895f174466d0278ed91297d18346632fbcd98d902b4f36c1e3ac0fc2b14e3024c25280415a37ef1dccdb8118ea2232305485c0a87e17417637a972c7bd",
      "r": "0x7150d00a9dcd8a8287ad220010c52ff2608906b746de23c993999768091ff210",
      "raw": "0xf86c8085098bca5a00825e1b940c7c0b72004a7a66ffa780637427fed0c4faac478844004c09e76a00008025a07150d00a9dcd8a8287ad220010c52ff2608906b746de23c993999768091ff210a05585fabcd1dc415e1668d4cbc2d419cf0381bf9707480ad2f86d0800732f6d7e",
      "s": "0x5585fabcd1dc415e1668d4cbc2d419cf0381bf9707480ad2f86d0800732f6d7e",
      "to": "0x0c7c0b72004a7a66ffa780637427fed0c4faac47",
      "transactionIndex": "0x0",
      "v": 0,
      "value": "0x44004c09e76a0000"
    },
    {
      "blockHash": "0xc596cb892b649b4917da8c6b78611346d55daf7bcf4375da86a2d98810888e84",
      "blockNumber": "0x2ca14a",
      "creates": null,
      "from": "0x56ce1399be2831f8a3f918a0408c05bbad658ef3",
      "gas": "0x5208",
      "gasPrice": "0x4e3b29200",
      "hash": "0xe9e15dd4f1070ec30ca4bfbe70738e78b0bb7d126a512e7dc9b22df5b64af791",
      "input": "0x",
      "nonce": "0x9d",
      "publicKey": "0x82a51957d31375120093359ab7beefe9d7d68579d6e40b99d85bff1283a687f8a88c0f3c0a1160f68df814a3bfe7f649a4c16f9b6dbdaf289e6dec93bf083287",
      "r": "0x5d92c10b5789801d4ce0fc558eedc6e6cccbaf0105a7c1f909feabcedfe56cd9",
      "raw": "0xf86d819d8504e3b2920082520894f4702bb51b8270729db362b0d4f82a56bdd66c6588120a871cc0020000801ba05d92c10b5789801d4ce0fc558eedc6e6cccbaf0105a7c1f909feabcedfe56cd9a072cc370fa5fd3b43c2ba4e9e70fea1b5e950b4261ab4274982d8ae15a3403a33",
      "s": "0x72cc370fa5fd3b43c2ba4e9e70fea1b5e950b4261ab4274982d8ae15a3403a33",
      "to": "0xf4702bb51b8270729db362b0d4f82a56bdd66c65",
      "transactionIndex": "0x1",
      "v": 0,
      "value": "0x120a871cc0020000"
    },
    {
      "blockHash": "0xc596cb892b649b4917da8c6b78611346d55daf7bcf4375da86a2d98810888e84",
      "blockNumber": "0x2ca14a",
      "creates": null,
      "from": "0x1e9939daaad6924ad004c2560e90804164900341",
      "gas": "0x9858",
      "gasPrice": "0x4a817c800",
      "hash": "0x3078eeb8227d104338666de260aac59c141a08f519856fd8b7253398d9347f51",
      "input": "0x",
      "nonce": "0x22f5d",
      "publicKey": "0x9fd89da696721cb906af81fe5d8bc3d0a4a7368be52db98d9e3a2d7c5f7aef545e8e578c2a158236d6221512c5df892b23bbe1087966f134799d6e36d3097973",
      "r": "0x7ee15b226f6c767ccace78a4b5b4cbf0be6ec20a899e058d3c95977bacd0cbd5",
      "raw": "0xf86f83022f5d8504a817c80082985894b8201140a49b0d5b65a23b4b2fa8a6efff87c576880de4ea09ac8f1e888025a07ee15b226f6c767ccace78a4b5b4cbf0be6ec20a899e058d3c95977bacd0cbd5a027e75bcd3bfd199e8c3e3f0c90b0d39f01b773b3da64060e06c0d568ae5c7523",
      "s": "0x27e75bcd3bfd199e8c3e3f0c90b0d39f01b773b3da64060e06c0d568ae5c7523",
      "to": "0xb8201140a49b0d5b65a23b4b2fa8a6efff87c576",
      "transactionIndex": "0x2",
      "v": 0,
      "value": "0xde4ea09ac8f1e88"
    },
    {
      "blockHash": "0xc596cb892b649b4917da8c6b78611346d55daf7bcf4375da86a2d98810888e84",
      "blockNumber": "0x2ca14a",
      "creates": null,
      "from": "0xea674fdde714fd979de3edf0f56aa9716b898ec8",
      "gas": "0x15f90",
      "gasPrice": "0x4a817c800",
      "hash": "0x9de43b061e5286ab1ad7494f50fac1ec9b541998800f9388ae6e7119f312c5cd",
      "input": "0x",
      "nonce": "0xfc02d",
      "publicKey": "0x5d8af02c52b1fb40e45d6e04ae2351d443d1f7327e9b6dbb5ae1d918414899c625690c4c236889e23d9705f9db88876542b959d947a3497a30d0d922ee30015f",
      "r": "0x059934eeace580cc2bdc292415976692c751f0bcb025930bd40fcc31e91208f3",
      "raw": "0xf870830fc02d8504a817c80083015f9094c4f381af25c41786110242623373cc9c7647f3f1880e139507cd50c0188026a0059934eeace580cc2bdc292415976692c751f0bcb025930bd40fcc31e91208f3a077ff34a10a3de0d906a0363b4bdbc0e9a06cb4378476d96dfd446225d8d9949c",
      "s": "0x77ff34a10a3de0d906a0363b4bdbc0e9a06cb4378476d96dfd446225d8d9949c",
      "to": "0xc4f381af25c41786110242623373cc9c7647f3f1",
      "transactionIndex": "0x3",
      "v": 1,
      "value": "0xe139507cd50c018"
    }
  ],
  "transactionsRoot": "0xe307e6d0e13f41ed336e09d71deb59a354eee4121449f0286cfb076e767fd45b",
  "uncles": []
}

@ryanio
Copy link
Contributor

ryanio commented Jun 26, 2021

Fixed with #1316

@ryanio ryanio closed this as completed Jun 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants