Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
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/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) {
b.SetCoinbase(common.Address{})
}
b.statedb.Prepare(tx.Hash(), len(b.txs))
receipt, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vm.Config{})
receipt, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vm.Config{}, nil)
if err != nil {
panic(err)
}
Expand Down
16 changes: 12 additions & 4 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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.Prepare(tx.Hash(), i)
receipt, err := applyTransaction(msg, p.config, nil, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv)
receipt, err := applyTransaction(msg, p.config, nil, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv, nil)
if err != nil {
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
Expand All @@ -92,16 +92,24 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
return receipts, allLogs, *usedGas, nil
}

func applyTransaction(msg types.Message, config *params.ChainConfig, author *common.Address, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
func applyTransaction(msg types.Message, config *params.ChainConfig, author *common.Address, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, preFinalizeHook func() error) (*types.Receipt, error) {
// Create a new context to be used in the EVM environment.
txContext := NewEVMTxContext(msg)
evm.Reset(txContext, statedb)

snapshot := statedb.Snapshot()
// Apply the transaction to the current state (included in the env).
result, err := ApplyMessage(evm, msg, gp)
if err != nil {
return nil, err
}
if preFinalizeHook != nil {
err = preFinalizeHook()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should preFinalizeHook accept result?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but I decided to not add args before we actually need them.
For 1tx bundle revert we would need results, state and maybe something else too.

if err != nil {
statedb.RevertToSnapshot(snapshot)
return nil, err
}
}

// Update the state with pending changes.
var root []byte
Expand Down Expand Up @@ -186,15 +194,15 @@ func applyTransactionWithResult(msg types.Message, config *params.ChainConfig, b
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config, preFinalizeHook func() error) (*types.Receipt, error) {
msg, err := tx.AsMessage(types.MakeSigner(config, header.Number), header.BaseFee)
if err != nil {
return nil, err
}
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg)
return applyTransaction(msg, config, author, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv)
return applyTransaction(msg, config, author, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv, preFinalizeHook)
}

func ApplyTransactionWithResult(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *ExecutionResult, error) {
Expand Down
31 changes: 15 additions & 16 deletions miner/algo_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func applyTransactionWithBlacklist(signer types.Signer, config *params.ChainConf
// short circuit if blacklist is empty
if len(blacklist) == 0 {
snap := statedb.Snapshot()
receipt, err := core.ApplyTransaction(config, bc, author, gp, statedb, header, tx, usedGas, cfg)
receipt, err := core.ApplyTransaction(config, bc, author, gp, statedb, header, tx, usedGas, cfg, nil)
if err != nil {
statedb.RevertToSnapshot(snap)
}
Expand All @@ -114,29 +114,28 @@ func applyTransactionWithBlacklist(signer types.Signer, config *params.ChainConf
cfg.Tracer = touchTracer
cfg.Debug = true

hook := func() error {
for _, address := range touchTracer.TouchedAddresses() {
if _, in := blacklist[address]; in {
return errors.New("blacklist violation, tx trace")
}
}
return nil
}

usedGasTmp := *usedGas
gasPoolTmp := new(core.GasPool).AddGas(gp.Gas())
stateCopy := statedb.Copy()
snap := stateCopy.Snapshot()
snap := statedb.Snapshot()

stateCopy.Prepare(tx.Hash(), statedb.TxIndex())
receipt, err := core.ApplyTransaction(config, bc, author, gasPoolTmp, stateCopy, header, tx, &usedGasTmp, cfg)
receipt, err := core.ApplyTransaction(config, bc, author, gasPoolTmp, statedb, header, tx, &usedGasTmp, cfg, hook)
if err != nil {
stateCopy.RevertToSnapshot(snap)
*usedGas = usedGasTmp
*gp = *gasPoolTmp
return receipt, stateCopy, err
}

for _, address := range touchTracer.TouchedAddresses() {
if _, in := blacklist[address]; in {
return nil, statedb, errors.New("blacklist violation, tx trace")
}
statedb.RevertToSnapshot(snap)
return receipt, statedb, err
}

*usedGas = usedGasTmp
*gp = *gasPoolTmp
return receipt, stateCopy, nil
return receipt, statedb, err
}

// commit tx to envDiff
Expand Down
9 changes: 8 additions & 1 deletion miner/algo_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func simulateBundle(env *environment, bundle types.MevBundle, chData chainData,
coinbaseBalanceBefore := stateDB.GetBalance(env.coinbase)

var tempGasUsed uint64
receipt, err := core.ApplyTransaction(chData.chainConfig, chData.chain, &env.coinbase, gasPool, stateDB, env.header, tx, &tempGasUsed, *chData.chain.GetVMConfig())
receipt, err := core.ApplyTransaction(chData.chainConfig, chData.chain, &env.coinbase, gasPool, stateDB, env.header, tx, &tempGasUsed, *chData.chain.GetVMConfig(), nil)
if err != nil {
return types.SimulatedBundle{}, err
}
Expand Down Expand Up @@ -439,6 +439,8 @@ func TestBlacklist(t *testing.T) {
env := newEnvironment(chData, statedb, signers.addresses[0], GasLimit, big.NewInt(1))
envDiff := newEnvironmentDiff(env)

beforeRoot := statedb.IntermediateRoot(true)

blacklist := map[common.Address]struct{}{
signers.addresses[3]: {},
}
Expand Down Expand Up @@ -494,6 +496,11 @@ func TestBlacklist(t *testing.T) {
if len(envDiff.newReceipts) != 0 {
t.Fatal("newReceipts changed")
}

afterRoot := statedb.IntermediateRoot(true)
if beforeRoot != afterRoot {
t.Fatal("statedb root changed")
}
}

func TestGetSealingWorkAlgos(t *testing.T) {
Expand Down
27 changes: 12 additions & 15 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,12 +919,7 @@ func (w *worker) updateSnapshot(env *environment) {
func (w *worker) commitTransaction(env *environment, tx *types.Transaction) ([]*types.Log, error) {
gasPool := *env.gasPool
envGasUsed := env.header.GasUsed
var stateDB *state.StateDB
if len(w.blockList) != 0 {
stateDB = env.state.Copy()
} else {
stateDB = env.state
}
stateDB := env.state

// It's important to copy then .Prepare() - don't reorder.
stateDB.Prepare(tx.Hash(), env.tcount)
Expand All @@ -937,25 +932,27 @@ func (w *worker) commitTransaction(env *environment, tx *types.Transaction) ([]*
}

var tracer *logger.AccountTouchTracer
var hook func() error
config := *w.chain.GetVMConfig()
if len(w.blockList) != 0 {
tracer = logger.NewAccountTouchTracer()
config.Tracer = tracer
config.Debug = true
hook = func() error {
for _, address := range tracer.TouchedAddresses() {
if _, in := w.blockList[address]; in {
return errBlocklistViolation
}
}
return nil
}
}

receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &env.coinbase, &gasPool, stateDB, env.header, tx, &envGasUsed, config)
receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &env.coinbase, &gasPool, stateDB, env.header, tx, &envGasUsed, config, hook)
if err != nil {
stateDB.RevertToSnapshot(snapshot)
return nil, err
}
if len(w.blockList) != 0 {
for _, address := range tracer.TouchedAddresses() {
if _, in := w.blockList[address]; in {
return nil, errBlocklistViolation
}
}
}

*env.gasPool = gasPool
env.header.GasUsed = envGasUsed
Expand Down Expand Up @@ -1769,7 +1766,7 @@ func (w *worker) computeBundleGas(env *environment, bundle types.MevBundle, stat
config.Tracer = tracer
config.Debug = true
}
receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &env.coinbase, gasPool, state, env.header, tx, &tempGasUsed, config)
receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &env.coinbase, gasPool, state, env.header, tx, &tempGasUsed, config, nil)
if err != nil {
return simulatedBundle{}, err
}
Expand Down