@@ -18,6 +18,7 @@ package core
18
18
19
19
import (
20
20
"crypto/ecdsa"
21
+ "encoding/binary"
21
22
"math/big"
22
23
"testing"
23
24
@@ -29,11 +30,14 @@ import (
29
30
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
30
31
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
31
32
"github.com/ethereum/go-ethereum/core/rawdb"
33
+ "github.com/ethereum/go-ethereum/core/state"
32
34
"github.com/ethereum/go-ethereum/core/types"
33
35
"github.com/ethereum/go-ethereum/core/vm"
34
36
"github.com/ethereum/go-ethereum/crypto"
37
+ "github.com/ethereum/go-ethereum/ethdb/memorydb"
35
38
"github.com/ethereum/go-ethereum/params"
36
39
"github.com/ethereum/go-ethereum/trie"
40
+ "github.com/ethereum/go-ethereum/triedb"
37
41
"github.com/holiman/uint256"
38
42
"golang.org/x/crypto/sha3"
39
43
)
@@ -528,3 +532,54 @@ func TestProcessVerkle(t *testing.T) {
528
532
}
529
533
}
530
534
}
535
+
536
+ func TestProcessParentBlockHash (t * testing.T ) {
537
+ var (
538
+ chainConfig = params .MergedTestChainConfig
539
+ hashA = common.Hash {0x01 }
540
+ hashB = common.Hash {0x02 }
541
+ header = & types.Header {ParentHash : hashA , Number : big .NewInt (2 ), Difficulty : big .NewInt (0 )}
542
+ parent = & types.Header {ParentHash : hashB , Number : big .NewInt (1 ), Difficulty : big .NewInt (0 )}
543
+ coinbase = common.Address {}
544
+ )
545
+ test := func (statedb * state.StateDB ) {
546
+ statedb .SetNonce (params .HistoryStorageAddress , 1 )
547
+ statedb .SetCode (params .HistoryStorageAddress , params .HistoryStorageCode )
548
+ statedb .IntermediateRoot (true )
549
+
550
+ vmContext := NewEVMBlockContext (header , nil , & coinbase )
551
+ evm := vm .NewEVM (vmContext , vm.TxContext {}, statedb , chainConfig , vm.Config {})
552
+ ProcessParentBlockHash (header .ParentHash , evm , statedb )
553
+
554
+ vmContext = NewEVMBlockContext (parent , nil , & coinbase )
555
+ evm = vm .NewEVM (vmContext , vm.TxContext {}, statedb , chainConfig , vm.Config {})
556
+ ProcessParentBlockHash (parent .ParentHash , evm , statedb )
557
+
558
+ // make sure that the state is correct
559
+ if have := getParentBlockHash (statedb , 1 ); have != hashA {
560
+ t .Errorf ("want parent hash %v, have %v" , hashA , have )
561
+ }
562
+ if have := getParentBlockHash (statedb , 0 ); have != hashB {
563
+ t .Errorf ("want parent hash %v, have %v" , hashB , have )
564
+ }
565
+ }
566
+ t .Run ("MPT" , func (t * testing.T ) {
567
+ statedb , _ := state .New (types .EmptyRootHash , state .NewDatabase (rawdb .NewDatabase (memorydb .New ())), nil )
568
+ test (statedb )
569
+ })
570
+ t .Run ("Verkle" , func (t * testing.T ) {
571
+ db := rawdb .NewMemoryDatabase ()
572
+ cacheConfig := DefaultCacheConfigWithScheme (rawdb .PathScheme )
573
+ cacheConfig .SnapshotLimit = 0
574
+ triedb := triedb .NewDatabase (db , cacheConfig .triedbConfig (true ))
575
+ statedb , _ := state .New (types .EmptyVerkleHash , state .NewDatabaseWithNodeDB (db , triedb ), nil )
576
+ test (statedb )
577
+ })
578
+ }
579
+
580
+ func getParentBlockHash (statedb * state.StateDB , number uint64 ) common.Hash {
581
+ ringIndex := number % params .HistoryServeWindow
582
+ var key common.Hash
583
+ binary .BigEndian .PutUint64 (key [24 :], ringIndex )
584
+ return statedb .GetState (params .HistoryStorageAddress , key )
585
+ }
0 commit comments