@@ -65,10 +65,16 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
6565 if p .config .DAOForkSupport && p .config .DAOForkBlock != nil && p .config .DAOForkBlock .Cmp (block .Number ()) == 0 {
6666 misc .ApplyDAOHardFork (statedb )
6767 }
68+ blockContext := NewEVMBlockContext (header , p .bc , nil )
69+ vmenv := vm .NewEVM (blockContext , vm.TxContext {}, statedb , p .config , cfg )
6870 // Iterate over and process the individual transactions
6971 for i , tx := range block .Transactions () {
72+ msg , err := tx .AsMessage (types .MakeSigner (p .config , header .Number ))
73+ if err != nil {
74+ return nil , nil , 0 , err
75+ }
7076 statedb .Prepare (tx .Hash (), block .Hash (), i )
71- receipt , err := ApplyTransaction ( p .config , p .bc , nil , gp , statedb , header , tx , usedGas , cfg )
77+ receipt , err := applyTransaction ( msg , p .config , p .bc , nil , gp , statedb , header , tx , usedGas , vmenv )
7278 if err != nil {
7379 return nil , nil , 0 , err
7480 }
@@ -81,34 +87,25 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
8187 return receipts , allLogs , * usedGas , nil
8288}
8389
84- // ApplyTransaction attempts to apply a transaction to the given state database
85- // and uses the input parameters for its environment. It returns the receipt
86- // for the transaction, gas used and an error if the transaction failed,
87- // indicating the block was invalid.
88- 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 ) {
89- msg , err := tx .AsMessage (types .MakeSigner (config , header .Number ))
90- if err != nil {
91- return nil , err
92- }
90+ func applyTransaction (msg types.Message , config * params.ChainConfig , bc ChainContext , author * common.Address , gp * GasPool , statedb * state.StateDB , header * types.Header , tx * types.Transaction , usedGas * uint64 , evm * vm.EVM ) (* types.Receipt , error ) {
9391 // Create a new context to be used in the EVM environment
94- context := NewEVMContext (msg , header , bc , author )
95- // Create a new environment which holds all relevant information
96- // about the transaction and calling mechanisms.
97- vmenv := vm .NewEVM (context , statedb , config , cfg )
98-
92+ txContext := NewEVMTxContext (msg )
93+ // Add addresses to access list if applicable
9994 if config .IsYoloV2 (header .Number ) {
10095 statedb .AddAddressToAccessList (msg .From ())
10196 if dst := msg .To (); dst != nil {
10297 statedb .AddAddressToAccessList (* dst )
10398 // If it's a create-tx, the destination will be added inside evm.create
10499 }
105- for _ , addr := range vmenv .ActivePrecompiles () {
100+ for _ , addr := range evm .ActivePrecompiles () {
106101 statedb .AddAddressToAccessList (addr )
107102 }
108103 }
109104
105+ // Update the evm with the new transaction context.
106+ evm .Reset (txContext , statedb )
110107 // Apply the transaction to the current state (included in the env)
111- result , err := ApplyMessage (vmenv , msg , gp )
108+ result , err := ApplyMessage (evm , msg , gp )
112109 if err != nil {
113110 return nil , err
114111 }
@@ -128,7 +125,7 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
128125 receipt .GasUsed = result .UsedGas
129126 // if the transaction created a contract, store the creation address in the receipt.
130127 if msg .To () == nil {
131- receipt .ContractAddress = crypto .CreateAddress (vmenv . Context .Origin , tx .Nonce ())
128+ receipt .ContractAddress = crypto .CreateAddress (evm . TxContext .Origin , tx .Nonce ())
132129 }
133130 // Set the receipt logs and create a bloom for filtering
134131 receipt .Logs = statedb .GetLogs (tx .Hash ())
@@ -139,3 +136,18 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
139136
140137 return receipt , err
141138}
139+
140+ // ApplyTransaction attempts to apply a transaction to the given state database
141+ // and uses the input parameters for its environment. It returns the receipt
142+ // for the transaction, gas used and an error if the transaction failed,
143+ // indicating the block was invalid.
144+ 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 ) {
145+ msg , err := tx .AsMessage (types .MakeSigner (config , header .Number ))
146+ if err != nil {
147+ return nil , err
148+ }
149+ // Create a new context to be used in the EVM environment
150+ blockContext := NewEVMBlockContext (header , bc , author )
151+ vmenv := vm .NewEVM (blockContext , vm.TxContext {}, statedb , config , cfg )
152+ return applyTransaction (msg , config , bc , author , gp , statedb , header , tx , usedGas , vmenv )
153+ }
0 commit comments