core: implement ChainContext interface in chainMaker

This commit is contained in:
Felix Lange 2023-10-30 18:37:47 +01:00
parent c983208c54
commit 619605e13f
2 changed files with 13 additions and 6 deletions

View file

@ -97,7 +97,7 @@ func (b *BlockGen) Difficulty() *big.Int {
func (b *BlockGen) SetParentBeaconRoot(root common.Hash) { func (b *BlockGen) SetParentBeaconRoot(root common.Hash) {
b.header.ParentBeaconRoot = &root b.header.ParentBeaconRoot = &root
var ( var (
blockContext = NewEVMBlockContext(b.header, nil, &b.header.Coinbase) blockContext = NewEVMBlockContext(b.header, b.cm, &b.header.Coinbase)
vmenv = vm.NewEVM(blockContext, vm.TxContext{}, b.statedb, b.cm.config, vm.Config{}) vmenv = vm.NewEVM(blockContext, vm.TxContext{}, b.statedb, b.cm.config, vm.Config{})
) )
ProcessBeaconBlockRoot(root, vmenv, b.statedb) ProcessBeaconBlockRoot(root, vmenv, b.statedb)
@ -309,7 +309,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
if engine == nil { if engine == nil {
panic("nil consensus engine") panic("nil consensus engine")
} }
cm := newChainMaker(parent, config) cm := newChainMaker(parent, config, engine)
genblock := func(i int, parent *types.Block, triedb *trie.Database, statedb *state.StateDB) (*types.Block, types.Receipts) { genblock := func(i int, parent *types.Block, triedb *trie.Database, statedb *state.StateDB) (*types.Block, types.Receipts) {
b := &BlockGen{i: i, cm: cm, parent: parent, statedb: statedb, engine: engine} b := &BlockGen{i: i, cm: cm, parent: parent, statedb: statedb, engine: engine}
@ -472,16 +472,18 @@ func makeBlockChainWithGenesis(genesis *Genesis, n int, engine consensus.Engine,
// chainMaker contains the state of chain generation. // chainMaker contains the state of chain generation.
type chainMaker struct { type chainMaker struct {
bottom *types.Block bottom *types.Block
engine consensus.Engine
config *params.ChainConfig config *params.ChainConfig
chain []*types.Block chain []*types.Block
chainByHash map[common.Hash]*types.Block chainByHash map[common.Hash]*types.Block
receipts []types.Receipts receipts []types.Receipts
} }
func newChainMaker(bottom *types.Block, config *params.ChainConfig) *chainMaker { func newChainMaker(bottom *types.Block, config *params.ChainConfig, engine consensus.Engine) *chainMaker {
return &chainMaker{ return &chainMaker{
bottom: bottom, bottom: bottom,
config: config, config: config,
engine: engine,
chainByHash: make(map[common.Hash]*types.Block), chainByHash: make(map[common.Hash]*types.Block),
} }
} }
@ -504,13 +506,18 @@ func (cm *chainMaker) blockByNumber(number uint64) *types.Block {
return cm.chain[number-lowest] return cm.chain[number-lowest]
} }
// ChainReader implementation // ChainReader/ChainContext implementation
// Config returns the chain configuration. // Config returns the chain configuration (for consensus.ChainReader).
func (cm *chainMaker) Config() *params.ChainConfig { func (cm *chainMaker) Config() *params.ChainConfig {
return cm.config return cm.config
} }
// Engine returns the consensus engine (for ChainContext).
func (cm *chainMaker) Engine() consensus.Engine {
return cm.engine
}
func (cm *chainMaker) CurrentHeader() *types.Header { func (cm *chainMaker) CurrentHeader() *types.Header {
if len(cm.chain) == 0 { if len(cm.chain) == 0 {
return cm.bottom.Header() return cm.bottom.Header()

View file

@ -359,7 +359,7 @@ func TestStateProcessorErrors(t *testing.T) {
func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Transactions, config *params.ChainConfig) *types.Block { func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Transactions, config *params.ChainConfig) *types.Block {
difficulty := big.NewInt(0) difficulty := big.NewInt(0)
if !config.TerminalTotalDifficultyPassed { if !config.TerminalTotalDifficultyPassed {
fakeChainReader := newChainMaker(nil, config) fakeChainReader := newChainMaker(nil, config, engine)
difficulty = engine.CalcDifficulty(fakeChainReader, parent.Time()+10, &types.Header{ difficulty = engine.CalcDifficulty(fakeChainReader, parent.Time()+10, &types.Header{
Number: parent.Number(), Number: parent.Number(),
Time: parent.Time(), Time: parent.Time(),