Skip to content

Commit 628ecc1

Browse files
committed
core,miner: clean prague3 code flow to fix linter and UTs (#21)
* core,miner: clean prague3 code flow to fix linter and UTs * nit: fix mispell * fix broken UT by using nil PostState in receipt (cherry picked from commit 1db5dec) (cherry picked from commit 156ced5)
1 parent fa20072 commit 628ecc1

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

core/block_validator.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222

2323
"github.com/davecgh/go-spew/spew"
24-
"github.com/ethereum/go-ethereum/common"
2524
"github.com/ethereum/go-ethereum/consensus"
2625
"github.com/ethereum/go-ethereum/core/state"
2726
"github.com/ethereum/go-ethereum/core/types"
@@ -30,13 +29,6 @@ import (
3029
"github.com/ethereum/go-ethereum/trie"
3130
)
3231

33-
var (
34-
// ERC20 Transfer event signature: keccak256("Transfer(address,address,uint256)")
35-
transferSig = common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
36-
// InternalBalanceChanged event signature: keccak256("InternalBalanceChanged(address,address,int256)")
37-
internalBalanceChangedSig = common.HexToHash("0x18e1ea4139e68413d7d08aa752e71568e36b2c5bf940893314c2c5b01eaa0c42")
38-
)
39-
4032
// BlockValidator is responsible for validating block headers, uncles and
4133
// processed state.
4234
//
@@ -182,7 +174,7 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
182174
return fmt.Errorf("invalid gas used (remote: %d local: %d)", block.GasUsed(), res.GasUsed)
183175
}
184176

185-
// Prague3 validation: Check for ERC20 transfers involving blocked addresses.
177+
// Berachain: If Prague3, check for ERC20 transfers involving blocked addresses.
186178
if v.config.IsPrague3(block.Number(), block.Time()) {
187179
for _, receipt := range res.Receipts {
188180
if err := ValidatePrague3Transaction(&v.config.Berachain.Prague3, receipt); err != nil {

core/state_processor.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
231241
func 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())

miner/worker.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ import (
3737
"github.com/holiman/uint256"
3838
)
3939

40-
var (
41-
// ERC20 Transfer event signature: keccak256("Transfer(address,address,uint256)")
42-
transferSig = common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
43-
// InternalBalanceChanged event signature: keccak256("InternalBalanceChanged(address,address,int256)")
44-
internalBalanceChangedSig = common.HexToHash("0x18e1ea4139e68413d7d08aa752e71568e36b2c5bf940893314c2c5b01eaa0c42")
45-
)
46-
4740
var (
4841
errBlockInterruptedByNewHead = errors.New("new head arrived while building block")
4942
errBlockInterruptedByRecommit = errors.New("recommit interrupt while building block")

0 commit comments

Comments
 (0)