@@ -31,6 +31,13 @@ import (
3131 "github.com/ethereum/go-ethereum/params"
3232)
3333
34+ var (
35+ // ERC20 Transfer event signature: keccak256("Transfer(address,address,uint256)")
36+ transferSig = common .HexToHash ("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" )
37+ // InternalBalanceChanged event signature: keccak256("InternalBalanceChanged(address,address,int256)")
38+ internalBalanceChangedSig = common .HexToHash ("0x18e1ea4139e68413d7d08aa752e71568e36b2c5bf940893314c2c5b01eaa0c42" )
39+ )
40+
3441// StateProcessor is a basic Processor, which takes care of transitioning
3542// state from one point to another.
3643//
@@ -152,15 +159,17 @@ func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB,
152159 return nil , err
153160 }
154161
155- // Calculate items for receipt.
156- * usedGas += result .UsedGas
157- receipt = MakeReceipt (evm , result , statedb , blockNumber , blockHash , blockTime , tx , * usedGas , []byte {})
162+ // Berachain: Pre-calculate items for receipt to do Prague3 validation.
163+ // NOTE: Important to enforce that the statedb usage inside of MakeReceipt is not affected by
164+ // calling Finalise or IntermediateRoot; before modification, MakeReceipt was called after
165+ // statedb was updated with pending changes. Currently MakeReceipt only uses statedb.logs and
166+ // statedb.txIndex, both of which are unaffected by Finalise or IntermediateRoot.
167+ blockGasUsed := * usedGas + result .UsedGas
168+ receipt = MakeReceipt (evm , result , statedb , blockNumber , blockHash , blockTime , tx , blockGasUsed , nil )
158169
159- // Prague3 validation: Check for ERC20 transfers involving blocked addresses.
170+ // Berachain: If Prague3, check for ERC20 transfers involving blocked addresses.
160171 if evm .ChainConfig ().IsPrague3 (blockNumber , blockTime ) {
161172 if err := ValidatePrague3Transaction (& evm .ChainConfig ().Berachain .Prague3 , receipt ); err != nil {
162- // We must decrement the pointer that was increased
163- * usedGas -= result .UsedGas
164173 return nil , err
165174 }
166175 }
@@ -172,6 +181,7 @@ func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB,
172181 // Re-update the receipt with the new intermediate root.
173182 receipt .PostState = statedb .IntermediateRoot (evm .ChainConfig ().IsEIP158 (blockNumber )).Bytes ()
174183 }
184+ * usedGas = blockGasUsed
175185
176186 // Merge the tx-local access event into the "block-local" one, in order to collect
177187 // all values, so that the witness can be built.
@@ -230,7 +240,7 @@ func ApplyTransaction(evm *vm.EVM, gp *GasPool, statedb *state.StateDB, header *
230240// transfers from/to certain blocked addresses and InternalBalanceChanged events from the BEX vault.
231241func ValidatePrague3Transaction (cfg * params.Prague3Config , receipt * types.Receipt ) error {
232242 for _ , log := range receipt .Logs {
233- // Check if this is a Transfer event (first topic is the event signature)
243+ // Check if this is a ERC20 Transfer event (first topic is the event signature)
234244 if len (log .Topics ) >= 3 && log .Topics [0 ] == transferSig {
235245 // Transfer event has indexed from (topics[1]) and to (topics[2]) addresses
236246 fromAddr := common .BytesToAddress (log .Topics [1 ].Bytes ())
0 commit comments