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

cmd/evm: add difficulty calculation to t8n tool #23353

Merged
merged 3 commits into from
Aug 25, 2021

Conversation

holiman
Copy link
Contributor

@holiman holiman commented Aug 8, 2021

This PR adds functionality to the evm t8n to calculate ethash difficulty. If the caller does not provide a currentDifficulty, but instead provides the parentTimestamp (well, semi-optional, will default to 0 if not given), and parentDifficulty, we can calculate it for him.

The caller can also provide a parentUncleHash. In most, but not all cases, the parent uncle hash also affects the formula. If no such hash is provided (or, if the empty all-zero hash is provided), it's assumed that there were no uncles.

Documented in testdata/14/readme.md.

Closes #23249

Example run

[user@work evm]$ ./evm t8n --input.alloc=./testdata/14/alloc.json --input.txs=./testdata/14/txs.json --input.env=./testdata/14/env.json --output.result=stdout --state.fork=London
INFO [08-08|17:35:46.876] Trie dumping started                     root=6f0588..7f4bdc
INFO [08-08|17:35:46.876] Trie dumping complete                    accounts=2 elapsed="89.313µs"
INFO [08-08|17:35:46.877] Wrote file                               file=alloc.json
{
 "result": {
  "stateRoot": "0x6f058887ca01549716789c380ede95aecc510e6d1fdc4dbf67d053c7c07f4bdc",
  "txRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  "receiptRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  "logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "receipts": [],
  "currentDifficulty": 3311729559732224
 }
}

@holiman
Copy link
Contributor Author

holiman commented Aug 10, 2021

@winsvega does this work for you?

@@ -18,6 +18,7 @@ package t8ntool

import (
"fmt"
"github.com/ethereum/go-ethereum/consensus/ethash"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please gofmt it

@holiman
Copy link
Contributor Author

holiman commented Aug 17, 2021

@winsvega ping -- does this fit your needs?

@winsvega
Copy link
Contributor

I guess. I can chek tomorrow.

@winsvega
Copy link
Contributor

winsvega commented Aug 19, 2021

we used to treat every field as string.
the currentDifficulty is returned as int in json

like on the input:

{
  "currentCoinbase": "0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b",
  "currentGasLimit": "0x750a163df65e8a",
  "currentBaseFee": "0x500",
  "currentNumber": "12800000",
  "currentTimestamp": "10015",
  "parentTimestamp" : "99999",
  "parentDifficulty" : "0x2000000000000"
}

this started because there was some team. like JS implementation and they complained that it is difficult to parse different types in test jsons. so we have all the types in jsons as string.

@holiman
Copy link
Contributor Author

holiman commented Aug 25, 2021

@winsvega fixed:

]$  ./evm t8n --input.alloc=./testdata/14/alloc.json --input.txs=./testdata/14/txs.json --input.env=./testdata/14/env.json --output.result=stdout --state.fork=London
INFO [08-25|11:30:17.118] Trie dumping started                     root=6f0588..7f4bdc
INFO [08-25|11:30:17.118] Trie dumping complete                    accounts=2 elapsed="85.624µs"
INFO [08-25|11:30:17.118] Wrote file                               file=alloc.json
{
 "result": {
  "stateRoot": "0x6f058887ca01549716789c380ede95aecc510e6d1fdc4dbf67d053c7c07f4bdc",
  "txRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  "receiptRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  "logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "receipts": [],
  "currentDifficulty": "0xbc40020000000"
 }
}

Copy link
Member

@MariusVanDerWijden MariusVanDerWijden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a tiny question

cmd/evm/internal/t8ntool/transition.go Show resolved Hide resolved
@holiman holiman merged commit 84c51bc into ethereum:master Aug 25, 2021
@holiman holiman added this to the 1.10.9 milestone Aug 25, 2021
sidhujag pushed a commit to sidhujag/go-ethereum that referenced this pull request Aug 25, 2021
This PR adds functionality to the evm t8n to calculate ethash difficulty. If the caller does not provide a currentDifficulty, but instead provides the parentTimestamp (well, semi-optional, will default to 0 if not given), and parentDifficulty, we can calculate it for him.

The caller can also provide a parentUncleHash. In most, but not all cases, the parent uncle hash also affects the formula. If no such hash is provided (or, if the empty all-zero hash is provided), it's assumed that there were no uncles.
yongjun925 pushed a commit to DODOEX/go-ethereum that referenced this pull request Dec 3, 2022
This PR adds functionality to the evm t8n to calculate ethash difficulty. If the caller does not provide a currentDifficulty, but instead provides the parentTimestamp (well, semi-optional, will default to 0 if not given), and parentDifficulty, we can calculate it for him.

The caller can also provide a parentUncleHash. In most, but not all cases, the parent uncle hash also affects the formula. If no such hash is provided (or, if the empty all-zero hash is provided), it's assumed that there were no uncles.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

evm t8n calculate difficulty
4 participants