Skip to content

Commit b292dee

Browse files
core: properly fixed processBlockHashHistory and added test
1 parent dcdb0f3 commit b292dee

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

core/state_processor.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,10 @@ func ProcessBlockHashHistory(statedb *state.StateDB, header *types.Header, chain
296296
var low uint64
297297
if number > params.HistoryServeWindow {
298298
low = number - params.HistoryServeWindow
299-
if number < params.HistoryServeWindow {
300-
low = 0
301-
}
302299
}
303-
for i := prevNumber - 1; i >= low; i-- {
304-
ProcessParentBlockHash(statedb, i, parent.ParentHash)
305-
parent = chain.GetHeader(parent.ParentHash, i)
300+
for i := prevNumber; i > low; i-- {
301+
ProcessParentBlockHash(statedb, i-1, parent.ParentHash)
302+
parent = chain.GetHeader(parent.ParentHash, i-1)
306303
}
307304
}
308305

core/state_processor_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core
1818

1919
import (
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

Comments
 (0)