@@ -18,6 +18,7 @@ package core
1818
1919import (
2020 "crypto/ecdsa"
21+ "encoding/binary"
2122 "math/big"
2223 "testing"
2324
@@ -29,9 +30,11 @@ import (
2930 "github.com/ethereum/go-ethereum/consensus/misc/eip1559"
3031 "github.com/ethereum/go-ethereum/consensus/misc/eip4844"
3132 "github.com/ethereum/go-ethereum/core/rawdb"
33+ "github.com/ethereum/go-ethereum/core/state"
3234 "github.com/ethereum/go-ethereum/core/types"
3335 "github.com/ethereum/go-ethereum/core/vm"
3436 "github.com/ethereum/go-ethereum/crypto"
37+ "github.com/ethereum/go-ethereum/ethdb/memorydb"
3538 "github.com/ethereum/go-ethereum/params"
3639 "github.com/ethereum/go-ethereum/trie"
3740 "github.com/holiman/uint256"
@@ -528,3 +531,55 @@ func TestProcessVerkle(t *testing.T) {
528531 }
529532 }
530533}
534+
535+ type MockChain struct {
536+ chain map [common.Hash ]* types.Header
537+ }
538+
539+ func (m * MockChain ) Config () * params.ChainConfig { return nil }
540+
541+ func (m * MockChain ) CurrentHeader () * types.Header { return nil }
542+
543+ func (m * MockChain ) GetHeaderByNumber (number uint64 ) * types.Header { return nil }
544+
545+ func (m * MockChain ) GetTd (hash common.Hash , number uint64 ) * big.Int { return nil }
546+
547+ func (m * MockChain ) GetHeaderByHash (hash common.Hash ) * types.Header {
548+ return m .chain [hash ]
549+ }
550+
551+ func (m * MockChain ) GetHeader (hash common.Hash , number uint64 ) * types.Header {
552+ return m .chain [hash ]
553+ }
554+
555+ func TestProcessBlockHashHistory (t * testing.T ) {
556+ hashA := common.Hash {0x01 }
557+ hashB := common.Hash {0x02 }
558+ statedb , _ := state .New (types .EmptyRootHash , state .NewDatabase (rawdb .NewDatabase (memorydb .New ())), nil )
559+ header := & types.Header {ParentHash : hashA , Number : big .NewInt (2 )}
560+ parent := & types.Header {ParentHash : hashB , Number : big .NewInt (1 )}
561+ parentParent := & types.Header {ParentHash : common.Hash {}, Number : big .NewInt (0 )}
562+ chainConfig := params .AllDevChainProtocolChanges
563+ chainConfig .PragueTime = nil
564+ chain := new (MockChain )
565+ chain .chain = make (map [common.Hash ]* types.Header )
566+ chain .chain [hashA ] = parent
567+ chain .chain [hashB ] = parentParent
568+
569+ ProcessBlockHashHistory (statedb , header , chainConfig , chain )
570+
571+ // make sure that the state is correct
572+ if have := getParentBlockHash (statedb , 1 ); have != hashA {
573+ t .Fail ()
574+ }
575+ if have := getParentBlockHash (statedb , 0 ); have != hashB {
576+ t .Fail ()
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