@@ -39,13 +39,15 @@ import (
39
39
"github.com/ethereum/go-ethereum/rlp"
40
40
"github.com/ethereum/go-ethereum/trie"
41
41
"github.com/ethereum/go-ethereum/triedb"
42
+ "github.com/ethereum/go-verkle"
42
43
"github.com/holiman/uint256"
43
44
"golang.org/x/crypto/sha3"
44
45
)
45
46
46
47
type Prestate struct {
47
- Env stEnv `json:"env"`
48
- Pre types.GenesisAlloc `json:"pre"`
48
+ Env stEnv `json:"env"`
49
+ Pre types.GenesisAlloc `json:"pre"`
50
+ VKT map [common.Hash ]hexutil.Bytes `json:"vkt,omitempty"`
49
51
}
50
52
51
53
//go:generate go run github.com/fjl/gencodec -type ExecutionResult -field-override executionResultMarshaling -out gen_execresult.go
@@ -68,6 +70,11 @@ type ExecutionResult struct {
68
70
CurrentBlobGasUsed * math.HexOrDecimal64 `json:"blobGasUsed,omitempty"`
69
71
RequestsHash * common.Hash `json:"requestsHash,omitempty"`
70
72
Requests [][]byte `json:"requests"`
73
+
74
+ // Verkle witness
75
+ VerkleProof * verkle.VerkleProof `json:"verkleProof,omitempty"`
76
+ StateDiff verkle.StateDiff `json:"stateDiff,omitempty"`
77
+ ParentRoot common.Hash `json:"parentStateRoot,omitempty"`
71
78
}
72
79
73
80
type executionResultMarshaling struct {
@@ -101,6 +108,7 @@ type stEnv struct {
101
108
ParentExcessBlobGas * uint64 `json:"parentExcessBlobGas,omitempty"`
102
109
ParentBlobGasUsed * uint64 `json:"parentBlobGasUsed,omitempty"`
103
110
ParentBeaconBlockRoot * common.Hash `json:"parentBeaconBlockRoot"`
111
+ ParentHash * common.Hash `json:"parentHash,omitempty"`
104
112
}
105
113
106
114
type stEnvMarshaling struct {
@@ -143,16 +151,17 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
143
151
return h
144
152
}
145
153
var (
146
- statedb = MakePreState (rawdb .NewMemoryDatabase (), pre .Pre )
147
- signer = types .MakeSigner (chainConfig , new (big.Int ).SetUint64 (pre .Env .Number ), pre .Env .Timestamp )
148
- gaspool = new (core.GasPool )
149
- blockHash = common.Hash {0x13 , 0x37 }
150
- rejectedTxs []* rejectedTx
151
- includedTxs types.Transactions
152
- gasUsed = uint64 (0 )
153
- blobGasUsed = uint64 (0 )
154
- receipts = make (types.Receipts , 0 )
155
- txIndex = 0
154
+ parentStateRoot , statedb = MakePreState (rawdb .NewMemoryDatabase (), chainConfig , pre , chainConfig .IsVerkle (big .NewInt (int64 (pre .Env .Number )), pre .Env .Timestamp ))
155
+ signer = types .MakeSigner (chainConfig , new (big.Int ).SetUint64 (pre .Env .Number ), pre .Env .Timestamp )
156
+ gaspool = new (core.GasPool )
157
+ blockHash = common.Hash {0x13 , 0x37 }
158
+ rejectedTxs []* rejectedTx
159
+ includedTxs types.Transactions
160
+ gasUsed = uint64 (0 )
161
+ blobGasUsed = uint64 (0 )
162
+ receipts = make (types.Receipts , 0 )
163
+ txIndex = 0
164
+ vtrpre * trie.VerkleTrie
156
165
)
157
166
gaspool .AddGas (pre .Env .GasLimit )
158
167
vmContext := vm.BlockContext {
@@ -165,6 +174,14 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
165
174
GasLimit : pre .Env .GasLimit ,
166
175
GetHash : getHash ,
167
176
}
177
+
178
+ // We save the current state of the Verkle Tree before applying the transactions.
179
+ // Note that if the Verkle fork isn't active, this will be a noop.
180
+ switch tr := statedb .GetTrie ().(type ) {
181
+ case * trie.VerkleTrie :
182
+ vtrpre = tr .Copy ()
183
+ }
184
+
168
185
// If currentBaseFee is defined, add it to the vmContext.
169
186
if pre .Env .BaseFee != nil {
170
187
vmContext .BaseFee = new (big.Int ).Set (pre .Env .BaseFee )
@@ -251,6 +268,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
251
268
}
252
269
}
253
270
statedb .SetTxContext (tx .Hash (), txIndex )
271
+ evm .AccessEvents = state .NewAccessEvents (evm .StateDB .PointCache ())
254
272
var (
255
273
snapshot = statedb .Snapshot ()
256
274
prevGas = gaspool .Gas ()
@@ -315,7 +333,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
315
333
evm .Config .Tracer .OnTxEnd (receipt , nil )
316
334
}
317
335
}
318
-
336
+ statedb . AccessEvents (). Merge ( evm . AccessEvents )
319
337
txIndex ++
320
338
}
321
339
statedb .IntermediateRoot (chainConfig .IsEIP158 (vmContext .BlockNumber ))
@@ -348,6 +366,8 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
348
366
// Amount is in gwei, turn into wei
349
367
amount := new (big.Int ).Mul (new (big.Int ).SetUint64 (w .Amount ), big .NewInt (params .GWei ))
350
368
statedb .AddBalance (w .Address , uint256 .MustFromBig (amount ), tracing .BalanceIncreaseWithdrawal )
369
+
370
+ statedb .AccessEvents ().AddAccount (w .Address , true )
351
371
}
352
372
353
373
// Gather the execution-layer triggered requests.
@@ -373,6 +393,25 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
373
393
if err != nil {
374
394
return nil , nil , nil , NewError (ErrorEVM , fmt .Errorf ("could not commit state: %v" , err ))
375
395
}
396
+ // Add the witness to the execution result
397
+ var vktProof * verkle.VerkleProof
398
+ var vktStateDiff verkle.StateDiff
399
+ if chainConfig .IsVerkle (big .NewInt (int64 (pre .Env .Number )), pre .Env .Timestamp ) {
400
+ keys := statedb .AccessEvents ().Keys ()
401
+ if len (keys ) > 0 && vtrpre != nil {
402
+ var proofTrie * trie.VerkleTrie
403
+ switch tr := statedb .GetTrie ().(type ) {
404
+ case * trie.VerkleTrie :
405
+ proofTrie = tr
406
+ default :
407
+ return nil , nil , nil , fmt .Errorf ("invalid tree type in proof generation: %v" , tr )
408
+ }
409
+ vktProof , vktStateDiff , err = vtrpre .Proof (proofTrie , keys )
410
+ if err != nil {
411
+ return nil , nil , nil , fmt .Errorf ("error generating verkle proof for block %d: %w" , pre .Env .Number , err )
412
+ }
413
+ }
414
+ }
376
415
execRs := & ExecutionResult {
377
416
StateRoot : root ,
378
417
TxRoot : types .DeriveSha (includedTxs , trie .NewStackTrie (nil )),
@@ -384,6 +423,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
384
423
Difficulty : (* math .HexOrDecimal256 )(vmContext .Difficulty ),
385
424
GasUsed : (math .HexOrDecimal64 )(gasUsed ),
386
425
BaseFee : (* math .HexOrDecimal256 )(vmContext .BaseFee ),
426
+ VerkleProof : vktProof ,
427
+ StateDiff : vktStateDiff ,
428
+ ParentRoot : parentStateRoot ,
387
429
}
388
430
if pre .Env .Withdrawals != nil {
389
431
h := types .DeriveSha (types .Withdrawals (pre .Env .Withdrawals ), trie .NewStackTrie (nil ))
@@ -414,11 +456,13 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
414
456
return statedb , execRs , body , nil
415
457
}
416
458
417
- func MakePreState (db ethdb.Database , accounts types.GenesisAlloc ) * state.StateDB {
459
+ // XXX peut-etre pas besoin de changer les parametres tant qu'on n'a pas la conversion
460
+ func MakePreState (db ethdb.Database , chainConfig * params.ChainConfig , pre * Prestate , verkle bool ) (common.Hash , * state.StateDB ) {
418
461
tdb := triedb .NewDatabase (db , & triedb.Config {Preimages : true })
419
462
sdb := state .NewDatabase (tdb , nil )
463
+
420
464
statedb , _ := state .New (types .EmptyRootHash , sdb )
421
- for addr , a := range accounts {
465
+ for addr , a := range pre . Pre {
422
466
statedb .SetCode (addr , a .Code )
423
467
statedb .SetNonce (addr , a .Nonce , tracing .NonceChangeGenesis )
424
468
statedb .SetBalance (addr , uint256 .MustFromBig (a .Balance ), tracing .BalanceIncreaseGenesisBalance )
@@ -427,9 +471,19 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
427
471
}
428
472
}
429
473
// Commit and re-open to start with a clean state.
430
- root , _ := statedb .Commit (0 , false , false )
431
- statedb , _ = state .New (root , sdb )
432
- return statedb
474
+ mptRoot , err := statedb .Commit (0 , false , false )
475
+ if err != nil {
476
+ panic (err )
477
+ }
478
+ parentRoot := mptRoot
479
+ // If verkle mode started, establish the conversion
480
+ if verkle {
481
+ if _ , ok := statedb .GetTrie ().(* trie.VerkleTrie ); ok {
482
+ return parentRoot , statedb
483
+ }
484
+ }
485
+ statedb , _ = state .New (mptRoot , sdb )
486
+ return parentRoot , statedb
433
487
}
434
488
435
489
func rlpHash (x interface {}) (h common.Hash ) {
0 commit comments