@@ -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.
@@ -377,6 +397,25 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
377
397
if err != nil {
378
398
return nil , nil , nil , NewError (ErrorEVM , fmt .Errorf ("could not commit state: %v" , err ))
379
399
}
400
+ // Add the witness to the execution result
401
+ var vktProof * verkle.VerkleProof
402
+ var vktStateDiff verkle.StateDiff
403
+ if chainConfig .IsVerkle (big .NewInt (int64 (pre .Env .Number )), pre .Env .Timestamp ) {
404
+ keys := statedb .AccessEvents ().Keys ()
405
+ if len (keys ) > 0 && vtrpre != nil {
406
+ var proofTrie * trie.VerkleTrie
407
+ switch tr := statedb .GetTrie ().(type ) {
408
+ case * trie.VerkleTrie :
409
+ proofTrie = tr
410
+ default :
411
+ return nil , nil , nil , fmt .Errorf ("invalid tree type in proof generation: %v" , tr )
412
+ }
413
+ vktProof , vktStateDiff , err = vtrpre .Proof (proofTrie , keys )
414
+ if err != nil {
415
+ return nil , nil , nil , fmt .Errorf ("error generating verkle proof for block %d: %w" , pre .Env .Number , err )
416
+ }
417
+ }
418
+ }
380
419
execRs := & ExecutionResult {
381
420
StateRoot : root ,
382
421
TxRoot : types .DeriveSha (includedTxs , trie .NewStackTrie (nil )),
@@ -388,6 +427,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
388
427
Difficulty : (* math .HexOrDecimal256 )(vmContext .Difficulty ),
389
428
GasUsed : (math .HexOrDecimal64 )(gasUsed ),
390
429
BaseFee : (* math .HexOrDecimal256 )(vmContext .BaseFee ),
430
+ VerkleProof : vktProof ,
431
+ StateDiff : vktStateDiff ,
432
+ ParentRoot : parentStateRoot ,
391
433
}
392
434
if pre .Env .Withdrawals != nil {
393
435
h := types .DeriveSha (types .Withdrawals (pre .Env .Withdrawals ), trie .NewStackTrie (nil ))
@@ -418,11 +460,13 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
418
460
return statedb , execRs , body , nil
419
461
}
420
462
421
- func MakePreState (db ethdb.Database , accounts types.GenesisAlloc ) * state.StateDB {
463
+ // XXX peut-etre pas besoin de changer les parametres tant qu'on n'a pas la conversion
464
+ func MakePreState (db ethdb.Database , chainConfig * params.ChainConfig , pre * Prestate , verkle bool ) (common.Hash , * state.StateDB ) {
422
465
tdb := triedb .NewDatabase (db , & triedb.Config {Preimages : true })
423
466
sdb := state .NewDatabase (tdb , nil )
467
+
424
468
statedb , _ := state .New (types .EmptyRootHash , sdb )
425
- for addr , a := range accounts {
469
+ for addr , a := range pre . Pre {
426
470
statedb .SetCode (addr , a .Code )
427
471
statedb .SetNonce (addr , a .Nonce , tracing .NonceChangeGenesis )
428
472
statedb .SetBalance (addr , uint256 .MustFromBig (a .Balance ), tracing .BalanceIncreaseGenesisBalance )
@@ -431,9 +475,19 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
431
475
}
432
476
}
433
477
// Commit and re-open to start with a clean state.
434
- root , _ := statedb .Commit (0 , false , false )
435
- statedb , _ = state .New (root , sdb )
436
- return statedb
478
+ mptRoot , err := statedb .Commit (0 , false , false )
479
+ if err != nil {
480
+ panic (err )
481
+ }
482
+ parentRoot := mptRoot
483
+ // If verkle mode started, establish the conversion
484
+ if verkle {
485
+ if _ , ok := statedb .GetTrie ().(* trie.VerkleTrie ); ok {
486
+ return parentRoot , statedb
487
+ }
488
+ }
489
+ statedb , _ = state .New (mptRoot , sdb )
490
+ return parentRoot , statedb
437
491
}
438
492
439
493
func rlpHash (x interface {}) (h common.Hash ) {
0 commit comments