Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 1 addition & 3 deletions core/cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 14 additions & 13 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions core/vm/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions ctxc/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
Expand Down