@@ -23,6 +23,7 @@ import (
23
23
24
24
"github.com/ethereum/go-ethereum/common"
25
25
"github.com/ethereum/go-ethereum/common/math"
26
+ "github.com/ethereum/go-ethereum/consensus/ethash"
26
27
"github.com/ethereum/go-ethereum/consensus/misc"
27
28
"github.com/ethereum/go-ethereum/core"
28
29
"github.com/ethereum/go-ethereum/core/rawdb"
@@ -46,13 +47,14 @@ type Prestate struct {
46
47
// ExecutionResult contains the execution status after running a state test, any
47
48
// error that might have occurred and a dump of the final state if requested.
48
49
type ExecutionResult struct {
49
- StateRoot common.Hash `json:"stateRoot"`
50
- TxRoot common.Hash `json:"txRoot"`
51
- ReceiptRoot common.Hash `json:"receiptRoot"`
52
- LogsHash common.Hash `json:"logsHash"`
53
- Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
54
- Receipts types.Receipts `json:"receipts"`
55
- Rejected []* rejectedTx `json:"rejected,omitempty"`
50
+ StateRoot common.Hash `json:"stateRoot"`
51
+ TxRoot common.Hash `json:"txRoot"`
52
+ ReceiptRoot common.Hash `json:"receiptRoot"`
53
+ LogsHash common.Hash `json:"logsHash"`
54
+ Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
55
+ Receipts types.Receipts `json:"receipts"`
56
+ Rejected []* rejectedTx `json:"rejected,omitempty"`
57
+ Difficulty * math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
56
58
}
57
59
58
60
type ommer struct {
@@ -62,23 +64,28 @@ type ommer struct {
62
64
63
65
//go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
64
66
type stEnv struct {
65
- Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
66
- Difficulty * big.Int `json:"currentDifficulty" gencodec:"required"`
67
- GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
68
- Number uint64 `json:"currentNumber" gencodec:"required"`
69
- Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
70
- BlockHashes map [math.HexOrDecimal64 ]common.Hash `json:"blockHashes,omitempty"`
71
- Ommers []ommer `json:"ommers,omitempty"`
72
- BaseFee * big.Int `json:"currentBaseFee,omitempty"`
67
+ Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
68
+ Difficulty * big.Int `json:"currentDifficulty"`
69
+ ParentDifficulty * big.Int `json:"parentDifficulty"`
70
+ GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
71
+ Number uint64 `json:"currentNumber" gencodec:"required"`
72
+ Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
73
+ ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
74
+ BlockHashes map [math.HexOrDecimal64 ]common.Hash `json:"blockHashes,omitempty"`
75
+ Ommers []ommer `json:"ommers,omitempty"`
76
+ BaseFee * big.Int `json:"currentBaseFee,omitempty"`
77
+ ParentUncleHash common.Hash `json:"parentUncleHash"`
73
78
}
74
79
75
80
type stEnvMarshaling struct {
76
- Coinbase common.UnprefixedAddress
77
- Difficulty * math.HexOrDecimal256
78
- GasLimit math.HexOrDecimal64
79
- Number math.HexOrDecimal64
80
- Timestamp math.HexOrDecimal64
81
- BaseFee * math.HexOrDecimal256
81
+ Coinbase common.UnprefixedAddress
82
+ Difficulty * math.HexOrDecimal256
83
+ ParentDifficulty * math.HexOrDecimal256
84
+ GasLimit math.HexOrDecimal64
85
+ Number math.HexOrDecimal64
86
+ Timestamp math.HexOrDecimal64
87
+ ParentTimestamp math.HexOrDecimal64
88
+ BaseFee * math.HexOrDecimal256
82
89
}
83
90
84
91
type rejectedTx struct {
@@ -247,6 +254,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
247
254
LogsHash : rlpHash (statedb .Logs ()),
248
255
Receipts : receipts ,
249
256
Rejected : rejectedTxs ,
257
+ Difficulty : (* math .HexOrDecimal256 )(vmContext .Difficulty ),
250
258
}
251
259
return statedb , execRs , nil
252
260
}
@@ -274,3 +282,23 @@ func rlpHash(x interface{}) (h common.Hash) {
274
282
hw .Sum (h [:0 ])
275
283
return h
276
284
}
285
+
286
+ // calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
287
+ // the caller does not provide an explicit difficulty, but instead provides only
288
+ // parent timestamp + difficulty.
289
+ // Note: this method only works for ethash engine.
290
+ func calcDifficulty (config * params.ChainConfig , number , currentTime , parentTime uint64 ,
291
+ parentDifficulty * big.Int , parentUncleHash common.Hash ) * big.Int {
292
+ uncleHash := parentUncleHash
293
+ if uncleHash == (common.Hash {}) {
294
+ uncleHash = types .EmptyUncleHash
295
+ }
296
+ parent := & types.Header {
297
+ ParentHash : common.Hash {},
298
+ UncleHash : uncleHash ,
299
+ Difficulty : parentDifficulty ,
300
+ Number : new (big.Int ).SetUint64 (number - 1 ),
301
+ Time : parentTime ,
302
+ }
303
+ return ethash .CalcDifficulty (config , currentTime , parent )
304
+ }
0 commit comments