Skip to content

Commit

Permalink
Fix 'CumulativeGasUsed' not being calculated correctly (#38)
Browse files Browse the repository at this point in the history
* Fix 'CumulativeGasUsed' not being calculated correctly

* call Prepare to initialize warm addresses

* remove prints

---------

Co-authored-by: Dror Tirosh <[email protected]>
  • Loading branch information
forshtat and drortirosh authored Sep 16, 2024
1 parent ae32a25 commit fe783a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
if tx.Type() == types.Rip7560Type {
// HandleRip7560Transactions accepts a transaction array and in the future bundle handling will need this
tmpTxs := [1]*types.Transaction{tx}
_, validatedTxsReceipts, _, validateTxsLogs, err := HandleRip7560Transactions(tmpTxs[:], 0, statedb, &context.Coinbase, header, gp, p.config, p.bc, cfg, false)
_, validatedTxsReceipts, _, validateTxsLogs, err := HandleRip7560Transactions(tmpTxs[:], 0, statedb, &context.Coinbase, header, gp, p.config, p.bc, cfg, false, usedGas)
receipts = append(receipts, validatedTxsReceipts...)
allLogs = append(allLogs, validateTxsLogs...)
if err != nil {
Expand Down
27 changes: 23 additions & 4 deletions core/state_processor_rip7560.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ func HandleRip7560Transactions(
bc ChainContext,
cfg vm.Config,
skipInvalid bool,
usedGas *uint64,
) ([]*types.Transaction, types.Receipts, []*types.Rip7560TransactionDebugInfo, []*types.Log, error) {
validatedTransactions := make([]*types.Transaction, 0)
receipts := make([]*types.Receipt, 0)
allLogs := make([]*types.Log, 0)

iTransactions, iReceipts, validationFailureReceipts, iLogs, err := handleRip7560Transactions(
transactions, index, statedb, coinbase, header, gp, chainConfig, bc, cfg, skipInvalid,
transactions, index, statedb, coinbase, header, gp, chainConfig, bc, cfg, skipInvalid, usedGas,
)
if err != nil {
return nil, nil, nil, nil, err
Expand All @@ -142,6 +143,7 @@ func handleRip7560Transactions(
bc ChainContext,
cfg vm.Config,
skipInvalid bool,
usedGas *uint64,
) ([]*types.Transaction, types.Receipts, []*types.Rip7560TransactionDebugInfo, []*types.Log, error) {
validationPhaseResults := make([]*ValidationPhaseResult, 0)
validatedTransactions := make([]*types.Transaction, 0)
Expand Down Expand Up @@ -187,7 +189,7 @@ func handleRip7560Transactions(
// TODO: this will miss all validation phase events - pass in 'vpr'
// statedb.SetTxContext(vpr.Tx.Hash(), i)

receipt, err := ApplyRip7560ExecutionPhase(chainConfig, vpr, bc, coinbase, gp, statedb, header, cfg)
receipt, err := ApplyRip7560ExecutionPhase(chainConfig, vpr, bc, coinbase, gp, statedb, header, cfg, usedGas)

if err != nil {
return nil, nil, nil, nil, err
Expand Down Expand Up @@ -316,6 +318,10 @@ func ApplyRip7560ValidationPhases(
GasPrice: gasPrice,
}
evm := vm.NewEVM(blockContext, txContext, statedb, chainConfig, cfg)
rules := evm.ChainConfig().Rules(evm.Context.BlockNumber, evm.Context.Random != nil, evm.Context.Time)

statedb.Prepare(rules, *sender, evm.Context.Coinbase, &AA_ENTRY_POINT, vm.ActivePrecompiles(rules), tx.AccessList())

epc := &EntryPointCall{}

if evm.Config.Tracer == nil {
Expand Down Expand Up @@ -480,7 +486,17 @@ func applyPaymasterPostOpFrame(st *StateTransition, aatx *types.Rip7560AccountAb
return paymasterPostOpResult
}

func ApplyRip7560ExecutionPhase(config *params.ChainConfig, vpr *ValidationPhaseResult, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, cfg vm.Config) (*types.Receipt, error) {
func ApplyRip7560ExecutionPhase(
config *params.ChainConfig,
vpr *ValidationPhaseResult,
bc ChainContext,
author *common.Address,
gp *GasPool,
statedb *state.StateDB,
header *types.Header,
cfg vm.Config,
usedGas *uint64,
) (*types.Receipt, error) {

blockContext := NewEVMBlockContext(header, bc, author)
aatx := vpr.Tx.Rip7560TransactionData()
Expand Down Expand Up @@ -555,7 +571,10 @@ func ApplyRip7560ExecutionPhase(config *params.ChainConfig, vpr *ValidationPhase
}
}

receipt := &types.Receipt{Type: vpr.Tx.Type(), TxHash: vpr.Tx.Hash(), GasUsed: gasUsed, CumulativeGasUsed: gasUsed}
// TODO: naming convention hell!!! 'usedGas' is 'CumulativeGasUsed' in block processing
*usedGas += gasUsed

receipt := &types.Receipt{Type: vpr.Tx.Type(), TxHash: vpr.Tx.Hash(), GasUsed: gasUsed, CumulativeGasUsed: *usedGas}

receipt.Status = receiptStatus

Expand Down
2 changes: 1 addition & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (miner *Miner) commitRip7560TransactionsBundle(env *environment, txs *types
env.gasPool = new(core.GasPool).AddGas(gasLimit)
}

validatedTxs, receipts, validationFailureInfos, _, err := core.HandleRip7560Transactions(txs.Transactions, 0, env.state, &env.coinbase, env.header, env.gasPool, miner.chainConfig, miner.chain, vm.Config{}, true)
validatedTxs, receipts, validationFailureInfos, _, err := core.HandleRip7560Transactions(txs.Transactions, 0, env.state, &env.coinbase, env.header, env.gasPool, miner.chainConfig, miner.chain, vm.Config{}, true, &env.header.GasUsed)
miner.chain.SetRip7560TransactionDebugInfo(validationFailureInfos)
if err != nil {
return err
Expand Down

0 comments on commit fe783a4

Please sign in to comment.