diff --git a/core/blockchain.go b/core/blockchain.go index 59578158ca..f915bccf80 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -295,7 +295,7 @@ func NewBlockChain(db ctxcdb.Database, cacheConfig *CacheConfig, chainConfig *pa } bc.validator = NewBlockValidator(chainConfig, bc, engine) bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine) - bc.processor = NewStateProcessor(chainConfig, bc, engine) + bc.processor = NewStateProcessor(bc) bc.utcNow = time.Now().Unix() diff --git a/core/cvm.go b/core/cvm.go index d22a897346..1347e51816 100644 --- a/core/cvm.go +++ b/core/cvm.go @@ -29,11 +29,9 @@ import ( // ChainContext supports retrieving headers and consensus parameters from the // current blockchain to be used during transaction processing. type ChainContext interface { + consensus.ChainHeaderReader // Engine retrieves the chain's consensus engine. Engine() consensus.Engine - - // GetHeader returns the header corresponding to the hash/number argument pair. - GetHeader(common.Hash, uint64) *types.Header } // NewCVMContext creates a new context for use in the CVM. diff --git a/core/state_processor.go b/core/state_processor.go index f5a7e9eb08..f88eb0d3d6 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -21,7 +21,6 @@ import ( "math/big" "github.com/CortexFoundation/CortexTheseus/common" - "github.com/CortexFoundation/CortexTheseus/consensus" "github.com/CortexFoundation/CortexTheseus/core/state" "github.com/CortexFoundation/CortexTheseus/core/types" "github.com/CortexFoundation/CortexTheseus/core/vm" @@ -34,20 +33,21 @@ import ( // // StateProcessor implements Processor. type StateProcessor struct { - config *params.ChainConfig // Chain configuration options - bc *BlockChain // Canonical block chain - engine consensus.Engine // Consensus engine used for block rewards + chain ChainContext // Chain context interface } // NewStateProcessor initialises a new StateProcessor. -func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *StateProcessor { +func NewStateProcessor(chain ChainContext) *StateProcessor { return &StateProcessor{ - config: config, - bc: bc, - engine: engine, + chain: chain, } } +// chainConfig returns the chain configuration. +func (p *StateProcessor) chainConfig() *params.ChainConfig { + return p.chain.Config() +} + // Process processes the state changes according to the Cortex rules by running // the transaction messages using the statedb and applying any rewards to both // the processor (coinbase) and any included uncles. @@ -57,6 +57,7 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen // transactions failed to execute due to insufficient gas it will return an error. func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) { var ( + config = p.chainConfig() receipts types.Receipts usedGas = new(uint64) //quotaLimit = big.NewInt(0)//parent.quota+64k - parent.quotaUsed @@ -76,9 +77,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg // misc.ApplyDAOHardFork(statedb) //} var ( - blockContext = NewCVMBlockContext(header, p.bc, nil) - vmenv = vm.NewCVM(blockContext, statedb, p.config, cfg) - signer = types.MakeSigner(p.config, header.Number, header.Time) + blockContext = NewCVMBlockContext(header, p.chain, nil) + vmenv = vm.NewCVM(blockContext, statedb, config, cfg) + signer = types.MakeSigner(config, header.Number, header.Time) ) // Iterate over and process the individual transactions for i, tx := range block.Transactions() { @@ -87,7 +88,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } statedb.SetTxContext(tx.Hash(), i) - receipt, err := applyTransaction(msg, p.config, gp, qp, statedb, header, blockNumber, blockHash, tx, usedGas, vmenv) + receipt, err := applyTransaction(msg, config, gp, qp, statedb, header, blockNumber, blockHash, tx, usedGas, vmenv) if err != nil { return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } @@ -100,7 +101,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg // return nil,nil,0,consensus.ErrUnknownAncestor //} // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) - p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles()) + p.chain.Engine().Finalize(p.chain, header, statedb, block.Transactions(), block.Uncles()) return receipts, allLogs, *usedGas, nil } diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index 1f2c8faa6e..a5281075ee 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -259,6 +259,11 @@ type dummyChain struct { counter int } +func (d *dummyChain) Config() *params.ChainConfig { return nil } +func (d *dummyChain) CurrentHeader() *types.Header { return nil } +func (d *dummyChain) GetHeaderByNumber(n uint64) *types.Header { return nil } +func (d *dummyChain) GetHeaderByHash(h common.Hash) *types.Header { return nil } + // Engine retrieves the chain's consensus engine. func (d *dummyChain) Engine() consensus.Engine { return nil diff --git a/ctxc/tracers/api.go b/ctxc/tracers/api.go index b0febb8ef1..1522ac423c 100644 --- a/ctxc/tracers/api.go +++ b/ctxc/tracers/api.go @@ -73,6 +73,7 @@ type StateReleaseFunc func() type Backend interface { HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) + CurrentHeader() *types.Header BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) GetTransaction(txHash common.Hash) (bool, *types.Transaction, common.Hash, uint64, uint64) @@ -100,6 +101,24 @@ type chainContext struct { ctx context.Context } +func (context *chainContext) Config() *params.ChainConfig { + return context.api.backend.ChainConfig() +} + +func (context *chainContext) CurrentHeader() *types.Header { + return context.api.backend.CurrentHeader() +} + +func (context *chainContext) GetHeaderByNumber(number uint64) *types.Header { + header, _ := context.api.backend.HeaderByNumber(context.ctx, rpc.BlockNumber(number)) + return header +} + +func (context *chainContext) GetHeaderByHash(hash common.Hash) *types.Header { + header, _ := context.api.backend.HeaderByHash(context.ctx, hash) + return header +} + func (context *chainContext) Engine() consensus.Engine { return context.api.backend.Engine() }