Skip to content

Commit

Permalink
cmd/evm: add parent uncle hash to difficulty calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Aug 8, 2021
1 parent 8c5e89c commit 1b4bac6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
10 changes: 8 additions & 2 deletions cmd/evm/internal/t8ntool/execution.go
Expand Up @@ -74,6 +74,7 @@ type stEnv struct {
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
Ommers []ommer `json:"ommers,omitempty"`
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
ParentUncleHash common.Hash `json:"parentUncleHash"`
}

type stEnvMarshaling struct {
Expand Down Expand Up @@ -286,10 +287,15 @@ func rlpHash(x interface{}) (h common.Hash) {
// the caller does not provide an explicit difficulty, but instead provides only
// parent timestamp + difficulty.
// Note: this method only works for ethash engine.
func calcDifficulty(config *params.ChainConfig, number, currentTime, parentTime uint64, parentDifficulty *big.Int) *big.Int {
func calcDifficulty(config *params.ChainConfig, number, currentTime, parentTime uint64,
parentDifficulty *big.Int, parentUncleHash common.Hash) *big.Int {
uncleHash := parentUncleHash
if uncleHash == (common.Hash{}) {
uncleHash = types.EmptyUncleHash
}
parent := &types.Header{
ParentHash: common.Hash{},
UncleHash: types.EmptyUncleHash,
UncleHash: uncleHash,
Difficulty: parentDifficulty,
Number: new(big.Int).SetUint64(number - 1),
Time: parentTime,
Expand Down
6 changes: 6 additions & 0 deletions cmd/evm/internal/t8ntool/gen_stenv.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cmd/evm/internal/t8ntool/transition.go
Expand Up @@ -262,7 +262,8 @@ func Main(ctx *cli.Context) error {
case env.Timestamp == 0:
return NewError(ErrorVMConfig, errors.New("currentDifficulty cannot be calculated if time is set to 0"))
}
prestate.Env.Difficulty = calcDifficulty(chainConfig, env.Number, env.Timestamp, env.ParentTimestamp, env.ParentDifficulty)
prestate.Env.Difficulty = calcDifficulty(chainConfig, env.Number, env.Timestamp,
env.ParentTimestamp, env.ParentDifficulty, env.ParentUncleHash)
}
// Run the test and aggregate the result
s, result, err := prestate.Apply(vmConfig, chainConfig, txs, ctx.Int64(RewardFlag.Name), getTracer)
Expand Down
9 changes: 5 additions & 4 deletions cmd/evm/testdata/14/env.json
@@ -1,8 +1,9 @@
{
"currentCoinbase": "0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"currentGasLimit": "0x750a163df65e8a",
"currentNumber": "12301230",
"currentTimestamp": "10000",
"parentTimestamp" : "999",
"parentDifficulty" : "0x2000"
"currentBaseFee": "0x500",
"currentNumber": "12800000",
"currentTimestamp": "10015",
"parentTimestamp" : "99999",
"parentDifficulty" : "0x2000000000000"
}
10 changes: 10 additions & 0 deletions cmd/evm/testdata/14/env.uncles.json
@@ -0,0 +1,10 @@
{
"currentCoinbase": "0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"currentGasLimit": "0x750a163df65e8a",
"currentBaseFee": "0x500",
"currentNumber": "12800000",
"currentTimestamp": "10035",
"parentTimestamp" : "99999",
"parentDifficulty" : "0x2000000000000",
"parentUncleHash" : "0x000000000000000000000000000000000000000000000000000000000000beef"
}
29 changes: 24 additions & 5 deletions cmd/evm/testdata/14/readme.md
Expand Up @@ -2,11 +2,12 @@

This test shows how the `evm t8n` can be used to calculate the (ethash) difficulty, if none is provided by the caller.

Calculating it (with an empty set of txs) using `London` rules (and no provided unclehash for the parent block):
```
./evm t8n --input.alloc=./testdata/14/alloc.json --input.txs=./testdata/14/txs.json --input.env=./testdata/14/env.json --output.result=stdout
INFO [08-08|16:53:45.535] Trie dumping started root=6f0588..7f4bdc
INFO [08-08|16:53:45.535] Trie dumping complete accounts=2 elapsed="75.275µs"
INFO [08-08|16:53:45.535] Wrote file file=alloc.json
[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",
Expand All @@ -15,7 +16,25 @@ INFO [08-08|16:53:45.535] Wrote file file=alloc.js
"logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"receipts": [],
"currentDifficulty": 2361183241434822737920
"currentDifficulty": 3311729559732224
}
}
```
Same thing, but this time providing a non-empty (and non-`emptyKeccak`) unclehash, which leads to a slightly different result:
```
[user@work evm]$ ./evm t8n --input.alloc=./testdata/14/alloc.json --input.txs=./testdata/14/txs.json --input.env=./testdata/14/env.uncles.json --output.result=stdout --state.fork=London
INFO [08-08|17:35:49.232] Trie dumping started root=6f0588..7f4bdc
INFO [08-08|17:35:49.232] Trie dumping complete accounts=2 elapsed="83.069µs"
INFO [08-08|17:35:49.233] Wrote file file=alloc.json
{
"result": {
"stateRoot": "0x6f058887ca01549716789c380ede95aecc510e6d1fdc4dbf67d053c7c07f4bdc",
"txRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"receiptRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"receipts": [],
"currentDifficulty": 3311179803918336
}
}
```
Expand Down

0 comments on commit 1b4bac6

Please sign in to comment.