From c45da12e3fbe7bb7ca4e148829927994a0bd1a62 Mon Sep 17 00:00:00 2001 From: sonhv0212 Date: Tue, 13 May 2025 15:14:43 +0700 Subject: [PATCH 1/4] core/state: add BalanceChangeReason to state object --- accounts/abi/bind/backends/simulated.go | 3 ++- core/state/state_object.go | 12 ++++++------ core/state/state_test.go | 9 +++++---- core/state/statedb.go | 6 +++--- core/state/statedb_test.go | 9 +++++---- core/state/sync_test.go | 3 ++- les/odr_test.go | 3 ++- 7 files changed, 25 insertions(+), 20 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index b8d2ffa56..b98ce94e9 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -35,6 +35,7 @@ import ( "github.com/ethereum/go-ethereum/core/bloombits" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/eth/filters" @@ -620,7 +621,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM } // Set infinite balance to the fake caller account. from := stateDB.GetOrNewStateObject(call.From) - from.SetBalance(math.MaxBig256) + from.SetBalance(math.MaxBig256, tracing.BalanceChangeUnspecified) // Execute the call. msg := core.NewMessage( diff --git a/core/state/state_object.go b/core/state/state_object.go index 368a1466c..41cd8b372 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -477,7 +477,7 @@ func (s *stateObject) commit() (*trienode.NodeSet, error) { // AddBalance adds amount to s's balance. // It is used to add funds to the destination account of a transfer. -func (s *stateObject) AddBalance(amount *big.Int) { +func (s *stateObject) AddBalance(amount *big.Int, reason tracing.BalanceChangeReason) { // EIP161: We must check emptiness for the objects such that the account // clearing (0,0,0 objects) can take effect. if amount.Sign() == 0 { @@ -486,25 +486,25 @@ func (s *stateObject) AddBalance(amount *big.Int) { } return } - s.SetBalance(new(big.Int).Add(s.Balance(), amount)) + s.SetBalance(new(big.Int).Add(s.Balance(), amount), reason) } // SubBalance removes amount from s's balance. // It is used to remove funds from the origin account of a transfer. -func (s *stateObject) SubBalance(amount *big.Int) { +func (s *stateObject) SubBalance(amount *big.Int, reason tracing.BalanceChangeReason) { if amount.Sign() == 0 { return } - s.SetBalance(new(big.Int).Sub(s.Balance(), amount)) + s.SetBalance(new(big.Int).Sub(s.Balance(), amount), reason) } -func (s *stateObject) SetBalance(amount *big.Int) { +func (s *stateObject) SetBalance(amount *big.Int, reason tracing.BalanceChangeReason) { s.db.journal.append(balanceChange{ account: &s.address, prev: new(big.Int).Set(s.data.Balance), }) if s.db.logger != nil && s.db.logger.OnBalanceChange != nil { - s.db.logger.OnBalanceChange(s.address, s.Balance(), amount, tracing.BalanceChangeUnspecified) + s.db.logger.OnBalanceChange(s.address, s.Balance(), amount, reason) } s.setBalance(amount) } diff --git a/core/state/state_test.go b/core/state/state_test.go index a66e87de0..fe8dee64c 100644 --- a/core/state/state_test.go +++ b/core/state/state_test.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -48,11 +49,11 @@ func TestDump(t *testing.T) { // generate a few entries obj1 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01})) - obj1.AddBalance(big.NewInt(22)) + obj1.AddBalance(big.NewInt(22), tracing.BalanceChangeUnspecified) obj2 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x01, 0x02})) obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3}) obj3 := s.state.GetOrNewStateObject(common.BytesToAddress([]byte{0x02})) - obj3.SetBalance(big.NewInt(44)) + obj3.SetBalance(big.NewInt(44), tracing.BalanceChangeUnspecified) // write some of them to the trie s.state.updateStateObject(obj1) @@ -168,7 +169,7 @@ func TestSnapshot2(t *testing.T) { // db, trie are already non-empty values so0 := state.getStateObject(stateobjaddr0) - so0.SetBalance(big.NewInt(42)) + so0.SetBalance(big.NewInt(42), tracing.BalanceChangeUnspecified) so0.SetNonce(43) so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'}) so0.selfDestructed = false @@ -180,7 +181,7 @@ func TestSnapshot2(t *testing.T) { // and one with deleted == true so1 := state.getStateObject(stateobjaddr1) - so1.SetBalance(big.NewInt(52)) + so1.SetBalance(big.NewInt(52), tracing.BalanceChangeUnspecified) so1.SetNonce(53) so1.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e', '2'}), []byte{'c', 'a', 'f', 'e', '2'}) so1.selfDestructed = true diff --git a/core/state/statedb.go b/core/state/statedb.go index b9be786d4..3060ff04a 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -419,7 +419,7 @@ func (s *StateDB) HasSelfDestructed(addr common.Address) bool { func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { stateObject := s.GetOrNewStateObject(addr) if stateObject != nil { - stateObject.AddBalance(amount) + stateObject.AddBalance(amount, tracing.BalanceChangeUnspecified) } } @@ -427,14 +427,14 @@ func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { stateObject := s.GetOrNewStateObject(addr) if stateObject != nil { - stateObject.SubBalance(amount) + stateObject.SubBalance(amount, tracing.BalanceChangeUnspecified) } } func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) { stateObject := s.GetOrNewStateObject(addr) if stateObject != nil { - stateObject.SetBalance(amount) + stateObject.SetBalance(amount, tracing.BalanceChangeUnspecified) } } diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 841702f98..86013ca04 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/trie" ) @@ -154,7 +155,7 @@ func TestCopy(t *testing.T) { for i := byte(0); i < 255; i++ { obj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i})) - obj.AddBalance(big.NewInt(int64(i))) + obj.AddBalance(big.NewInt(int64(i)), tracing.BalanceChangeUnspecified) orig.updateStateObject(obj) } orig.Finalise(false) @@ -171,9 +172,9 @@ func TestCopy(t *testing.T) { copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i})) ccopyObj := ccopy.GetOrNewStateObject(common.BytesToAddress([]byte{i})) - origObj.AddBalance(big.NewInt(2 * int64(i))) - copyObj.AddBalance(big.NewInt(3 * int64(i))) - ccopyObj.AddBalance(big.NewInt(4 * int64(i))) + origObj.AddBalance(big.NewInt(2*int64(i)), tracing.BalanceChangeUnspecified) + copyObj.AddBalance(big.NewInt(3*int64(i)), tracing.BalanceChangeUnspecified) + ccopyObj.AddBalance(big.NewInt(4*int64(i)), tracing.BalanceChangeUnspecified) orig.updateStateObject(origObj) copy.updateStateObject(copyObj) diff --git a/core/state/sync_test.go b/core/state/sync_test.go index 559f37044..3b8347341 100644 --- a/core/state/sync_test.go +++ b/core/state/sync_test.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -60,7 +61,7 @@ func makeTestState(scheme string) (ethdb.Database, Database, *trie.Database, com for i := byte(0); i < 96; i++ { obj := state.GetOrNewStateObject(common.BytesToAddress([]byte{i})) acc := &testAccount{address: common.BytesToAddress([]byte{i})} - obj.AddBalance(big.NewInt(int64(11 * i))) + obj.AddBalance(big.NewInt(int64(11*i)), tracing.BalanceChangeUnspecified) acc.balance = big.NewInt(int64(11 * i)) obj.SetNonce(uint64(42 * i)) acc.nonce = uint64(42 * i) diff --git a/les/odr_test.go b/les/odr_test.go index 6973dc675..4b3ef5a13 100644 --- a/les/odr_test.go +++ b/les/odr_test.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/txpool" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" @@ -128,7 +129,7 @@ func odrContractCall(ctx context.Context, db ethdb.Database, config *params.Chai if err == nil { from := statedb.GetOrNewStateObject(bankAddr) - from.SetBalance(math.MaxBig256) + from.SetBalance(math.MaxBig256, tracing.BalanceChangeUnspecified) msg := core.NewMessage( from.Address(), From 0218980e20d5013d7bd242b4bfbbb1592bc1b002 Mon Sep 17 00:00:00 2001 From: sonhv0212 Date: Tue, 13 May 2025 15:32:43 +0700 Subject: [PATCH 2/4] core/state: add BalanceChangeReason to StateDB.AddBalance --- cmd/evm/internal/t8ntool/execution.go | 5 ++- consensus/consortium/common/contract.go | 3 +- consensus/consortium/main.go | 3 +- consensus/ethash/consensus.go | 5 ++- consensus/misc/dao.go | 12 +++--- core/evm.go | 3 +- core/genesis.go | 5 ++- core/state/statedb.go | 4 +- core/state/statedb_test.go | 6 +-- core/state_transition.go | 11 +++--- core/txpool/blobpool/blobpool_test.go | 43 +++++++++++----------- core/txpool/legacypool/legacypool2_test.go | 13 ++++--- core/txpool/legacypool/legacypool_test.go | 15 ++++---- core/vm/evm.go | 3 +- core/vm/instructions.go | 5 ++- core/vm/interface.go | 3 +- miner/worker_test.go | 19 +++++----- tests/state_test_util.go | 3 +- 18 files changed, 89 insertions(+), 72 deletions(-) diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index be964e49e..5bbab5fc7 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -237,9 +238,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, reward.Sub(reward, big.NewInt(0).SetUint64(ommer.Delta)) reward.Mul(reward, blockReward) reward.Div(reward, big.NewInt(8)) - statedb.AddBalance(ommer.Address, reward) + statedb.AddBalance(ommer.Address, reward, tracing.BalanceIncreaseRewardMineUncle) } - statedb.AddBalance(pre.Env.Coinbase, minerReward) + statedb.AddBalance(pre.Env.Coinbase, minerReward, tracing.BalanceIncreaseRewardMineBlock) } // Commit block root, err := statedb.Commit(vmContext.BlockNumber.Uint64(), chainConfig.IsEIP158(vmContext.BlockNumber)) diff --git a/consensus/consortium/common/contract.go b/consensus/consortium/common/contract.go index ff820045b..1063d8446 100644 --- a/consensus/consortium/common/contract.go +++ b/consensus/consortium/common/contract.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/consensus/consortium/generated_contracts/staking" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -224,7 +225,7 @@ func (c *ContractIntegrator) SubmitBlockReward(opts *ApplyTransactOpts) error { coinbase := opts.Header.Coinbase balance := opts.State.GetBalance(consensus.SystemAddress) opts.State.SetBalance(consensus.SystemAddress, big.NewInt(0)) - opts.State.AddBalance(coinbase, balance) + opts.State.AddBalance(coinbase, balance, tracing.BalanceIncreaseRewardMineBlock) nonce := opts.State.GetNonce(c.coinbase) isVenoki := c.chainConfig.IsVenoki(opts.Header.Number) diff --git a/consensus/consortium/main.go b/consensus/consortium/main.go index 095a932a7..419d15734 100644 --- a/consensus/consortium/main.go +++ b/consensus/consortium/main.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/consensus/consortium/v2/finality" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/internal/ethapi" @@ -284,7 +285,7 @@ func HandleSystemTransaction(engine consensus.Engine, statedb *state.StateDB, ms if msg.Amount.Cmp(common.Big0) > 0 { balance := statedb.GetBalance(consensus.SystemAddress) statedb.SetBalance(consensus.SystemAddress, big.NewInt(0)) - statedb.AddBalance(block.Coinbase(), balance) + statedb.AddBalance(block.Coinbase(), balance, tracing.BalanceIncreaseRewardMineBlock) } return true diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 495ae86ff..0a5d14ec7 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/consensus/misc/eip1559" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" @@ -675,10 +676,10 @@ func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header r.Sub(r, header.Number) r.Mul(r, blockReward) r.Div(r, big8) - state.AddBalance(uncle.Coinbase, r) + state.AddBalance(uncle.Coinbase, r, tracing.BalanceIncreaseRewardMineUncle) r.Div(blockReward, big32) reward.Add(reward, r) } - state.AddBalance(header.Coinbase, reward) + state.AddBalance(header.Coinbase, reward, tracing.BalanceIncreaseRewardMineBlock) } diff --git a/consensus/misc/dao.go b/consensus/misc/dao.go index 36df036f2..752a91908 100644 --- a/consensus/misc/dao.go +++ b/consensus/misc/dao.go @@ -22,6 +22,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" ) @@ -40,10 +41,11 @@ var ( // ensure it conforms to DAO hard-fork rules. // // DAO hard-fork extension to the header validity: -// a) if the node is no-fork, do not accept blocks in the [fork, fork+10) range -// with the fork specific extra-data set -// b) if the node is pro-fork, require blocks in the specific range to have the -// unique extra-data set. +// +// a) if the node is no-fork, do not accept blocks in the [fork, fork+10) range +// with the fork specific extra-data set +// b) if the node is pro-fork, require blocks in the specific range to have the +// unique extra-data set. func VerifyDAOHeaderExtraData(config *params.ChainConfig, header *types.Header) error { // Short circuit validation if the node doesn't care about the DAO fork if config.DAOForkBlock == nil { @@ -79,7 +81,7 @@ func ApplyDAOHardFork(statedb *state.StateDB) { // Move every DAO account and extra-balance account funds into the refund contract for _, addr := range params.DAODrainList() { - statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr)) + statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr), tracing.BalanceIncreaseDaoContract) statedb.SetBalance(addr, new(big.Int)) } } diff --git a/core/evm.go b/core/evm.go index dffc48fb7..b6929597f 100644 --- a/core/evm.go +++ b/core/evm.go @@ -22,6 +22,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc/eip4844" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" ) @@ -135,5 +136,5 @@ func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { // Transfer subtracts amount from sender and adds amount to recipient using the given Db func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { db.SubBalance(sender, amount) - db.AddBalance(recipient, amount) + db.AddBalance(recipient, amount, tracing.BalanceChangeTransfer) } diff --git a/core/genesis.go b/core/genesis.go index 024438843..a35f7bf43 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -80,7 +81,7 @@ func hashAlloc(ga *types.GenesisAlloc) (common.Hash, error) { return common.Hash{}, err } for addr, account := range *ga { - statedb.AddBalance(addr, account.Balance) + statedb.AddBalance(addr, account.Balance, tracing.BalanceIncreaseGenesisBalance) statedb.SetCode(addr, account.Code) statedb.SetNonce(addr, account.Nonce) for key, value := range account.Storage { @@ -100,7 +101,7 @@ func flushAlloc(ga *types.GenesisAlloc, db ethdb.Database, triedb *trie.Database } for addr, account := range *ga { if account.Balance != nil { - statedb.AddBalance(addr, account.Balance) + statedb.AddBalance(addr, account.Balance, tracing.BalanceIncreaseGenesisBalance) } statedb.SetCode(addr, account.Code) statedb.SetNonce(addr, account.Nonce) diff --git a/core/state/statedb.go b/core/state/statedb.go index 3060ff04a..60254a990 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -416,10 +416,10 @@ func (s *StateDB) HasSelfDestructed(addr common.Address) bool { */ // AddBalance adds amount to the account associated with addr. -func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { +func (s *StateDB) AddBalance(addr common.Address, amount *big.Int, reason tracing.BalanceChangeReason) { stateObject := s.GetOrNewStateObject(addr) if stateObject != nil { - stateObject.AddBalance(amount, tracing.BalanceChangeUnspecified) + stateObject.AddBalance(amount, reason) } } diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 86013ca04..4e8226934 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -46,7 +46,7 @@ func TestUpdateLeaks(t *testing.T) { // Update it with some accounts for i := byte(0); i < 255; i++ { addr := common.BytesToAddress([]byte{i}) - state.AddBalance(addr, big.NewInt(int64(11*i))) + state.AddBalance(addr, big.NewInt(int64(11*i)), tracing.BalanceChangeUnspecified) state.SetNonce(addr, uint64(42*i)) if i%2 == 0 { state.SetState(addr, common.BytesToHash([]byte{i, i, i}), common.BytesToHash([]byte{i, i, i, i})) @@ -261,7 +261,7 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction { { name: "AddBalance", fn: func(a testAction, s *StateDB) { - s.AddBalance(addr, big.NewInt(a.args[0])) + s.AddBalance(addr, big.NewInt(a.args[0]), tracing.BalanceChangeUnspecified) }, args: make([]int64, 1), }, @@ -489,7 +489,7 @@ func TestTouchDelete(t *testing.T) { s.state, _ = New(root, s.state.db, s.state.snaps) snapshot := s.state.Snapshot() - s.state.AddBalance(common.Address{}, new(big.Int)) + s.state.AddBalance(common.Address{}, new(big.Int), tracing.BalanceChangeUnspecified) if len(s.state.journal.dirties) != 1 { t.Fatal("expected one dirty state object") diff --git a/core/state_transition.go b/core/state_transition.go index 05267d232..d8e99427c 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/common" cmath "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -358,7 +359,7 @@ func (st *StateTransition) buyGas() error { // // Unless the Ronin treasury address is specified, the blob fee amount will be burned. if st.evm.ChainConfig().RoninTreasuryAddress != nil && blobFee != nil && blobFee.Cmp(common.Big0) == 1 { - st.state.AddBalance(*st.evm.ChainConfig().RoninTreasuryAddress, blobFee) + st.state.AddBalance(*st.evm.ChainConfig().RoninTreasuryAddress, blobFee, tracing.BalanceIncreaseGasReturn) } // Subtract the gas fee from balance of the fee payer, @@ -571,9 +572,9 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { // if currentBlock is ConsortiumV2 then add balance to system address newEffectiveTip := new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip) if st.evm.ChainConfig().IsConsortiumV2(st.evm.Context.BlockNumber) { - st.state.AddBalance(consensus.SystemAddress, newEffectiveTip) + st.state.AddBalance(consensus.SystemAddress, newEffectiveTip, tracing.BalanceIncreaseRewardTransactionFee) } else { - st.state.AddBalance(st.evm.Context.Coinbase, newEffectiveTip) + st.state.AddBalance(st.evm.Context.Coinbase, newEffectiveTip, tracing.BalanceIncreaseRewardTransactionFee) } // After Venoki the base fee is non-zero and the fee is transferred to treasury @@ -581,7 +582,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { treasuryAddress := st.evm.ChainConfig().RoninTreasuryAddress if treasuryAddress != nil { fee := new(big.Int).Mul(big.NewInt(int64(st.gasUsed())), st.evm.Context.BaseFee) - st.state.AddBalance(*treasuryAddress, fee) + st.state.AddBalance(*treasuryAddress, fee, tracing.BalanceIncreaseRewardTransactionFee) } } } @@ -604,7 +605,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64) uint64 { // Return ETH for remaining gas, exchanged at the original rate. remaining := new(big.Int).Mul(new(big.Int).SetUint64(st.gas), st.gasPrice) - st.state.AddBalance(st.msg.Payer, remaining) + st.state.AddBalance(st.msg.Payer, remaining, tracing.BalanceIncreaseGasReturn) // Also return remaining gas to the block gas counter so it is // available for the next transaction. diff --git a/core/txpool/blobpool/blobpool_test.go b/core/txpool/blobpool/blobpool_test.go index 9fec61edc..d0da589fb 100644 --- a/core/txpool/blobpool/blobpool_test.go +++ b/core/txpool/blobpool/blobpool_test.go @@ -34,6 +34,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/txpool" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -639,19 +640,19 @@ func TestOpenDrops(t *testing.T) { // Create a blob pool out of the pre-seeded data statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewDatabase(memorydb.New())), nil) - statedb.AddBalance(crypto.PubkeyToAddress(gapper.PublicKey), big.NewInt(1000000)) - statedb.AddBalance(crypto.PubkeyToAddress(dangler.PublicKey), big.NewInt(1000000)) - statedb.AddBalance(crypto.PubkeyToAddress(filler.PublicKey), big.NewInt(1000000)) + statedb.AddBalance(crypto.PubkeyToAddress(gapper.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(crypto.PubkeyToAddress(dangler.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(crypto.PubkeyToAddress(filler.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) statedb.SetNonce(crypto.PubkeyToAddress(filler.PublicKey), 3) - statedb.AddBalance(crypto.PubkeyToAddress(overlapper.PublicKey), big.NewInt(1000000)) + statedb.AddBalance(crypto.PubkeyToAddress(overlapper.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) statedb.SetNonce(crypto.PubkeyToAddress(overlapper.PublicKey), 2) - statedb.AddBalance(crypto.PubkeyToAddress(underpayer.PublicKey), big.NewInt(1000000)) - statedb.AddBalance(crypto.PubkeyToAddress(outpricer.PublicKey), big.NewInt(1000000)) - statedb.AddBalance(crypto.PubkeyToAddress(exceeder.PublicKey), big.NewInt(1000000)) - statedb.AddBalance(crypto.PubkeyToAddress(overdrafter.PublicKey), big.NewInt(1000000)) - statedb.AddBalance(crypto.PubkeyToAddress(overcapper.PublicKey), big.NewInt(10000000)) - statedb.AddBalance(crypto.PubkeyToAddress(duplicater.PublicKey), big.NewInt(1000000)) - statedb.AddBalance(crypto.PubkeyToAddress(repeater.PublicKey), big.NewInt(1000000)) + statedb.AddBalance(crypto.PubkeyToAddress(underpayer.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(crypto.PubkeyToAddress(outpricer.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(crypto.PubkeyToAddress(exceeder.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(crypto.PubkeyToAddress(overdrafter.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(crypto.PubkeyToAddress(overcapper.PublicKey), big.NewInt(10000000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(crypto.PubkeyToAddress(duplicater.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(crypto.PubkeyToAddress(repeater.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) statedb.Commit(0, true) chain := &testBlockChain{ @@ -747,7 +748,7 @@ func TestOpenDropsFeeCapUnderpriced(t *testing.T) { chainConfig := *testChainConfig chainConfig.VenokiBlock = common.Big0 statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewDatabase(memorydb.New())), nil) - statedb.AddBalance(crypto.PubkeyToAddress(underpayer.PublicKey), big.NewInt(1000000)) + statedb.AddBalance(crypto.PubkeyToAddress(underpayer.PublicKey), big.NewInt(1000000), tracing.BalanceChangeUnspecified) chain := &testBlockChain{ config: &chainConfig, basefee: uint256.NewInt(params.InitialBaseFee), @@ -811,7 +812,7 @@ func TestOpenIndex(t *testing.T) { // Create a blob pool out of the pre-seeded data statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewDatabase(memorydb.New())), nil) - statedb.AddBalance(addr, big.NewInt(1_000_000_000)) + statedb.AddBalance(addr, big.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified) statedb.Commit(0, true) chain := &testBlockChain{ @@ -910,9 +911,9 @@ func TestOpenHeap(t *testing.T) { // Create a blob pool out of the pre-seeded data statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewDatabase(memorydb.New())), nil) - statedb.AddBalance(addr1, big.NewInt(1_000_000_000)) - statedb.AddBalance(addr2, big.NewInt(1_000_000_000)) - statedb.AddBalance(addr3, big.NewInt(1_000_000_000)) + statedb.AddBalance(addr1, big.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(addr2, big.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(addr3, big.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified) statedb.Commit(0, true) chain := &testBlockChain{ @@ -989,9 +990,9 @@ func TestOpenCap(t *testing.T) { for _, datacap := range []uint64{2 * (txAvgSize + blobSize), 100 * (txAvgSize + blobSize)} { // Create a blob pool out of the pre-seeded data, but cap it to 2 blob transaction statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewDatabase(memorydb.New())), nil) - statedb.AddBalance(addr1, big.NewInt(1_000_000_000)) - statedb.AddBalance(addr2, big.NewInt(1_000_000_000)) - statedb.AddBalance(addr3, big.NewInt(1_000_000_000)) + statedb.AddBalance(addr1, big.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(addr2, big.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified) + statedb.AddBalance(addr3, big.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified) statedb.Commit(0, true) chain := &testBlockChain{ @@ -1404,7 +1405,7 @@ func TestAdd(t *testing.T) { addrs[acc] = crypto.PubkeyToAddress(keys[acc].PublicKey) // Seed the state database with this account - statedb.AddBalance(addrs[acc], new(big.Int).SetUint64(seed.balance)) + statedb.AddBalance(addrs[acc], new(big.Int).SetUint64(seed.balance), tracing.BalanceChangeUnspecified) statedb.SetNonce(addrs[acc], seed.nonce) // Sign the seed transactions and store them in the data store @@ -1484,7 +1485,7 @@ func benchmarkPoolPending(b *testing.B, datacap uint64) { if err != nil { b.Fatal(err) } - statedb.AddBalance(addr, big.NewInt(1_000_000_000)) + statedb.AddBalance(addr, big.NewInt(1_000_000_000), tracing.BalanceChangeUnspecified) pool.add(tx) } statedb.Commit(0, true) diff --git a/core/txpool/legacypool/legacypool2_test.go b/core/txpool/legacypool/legacypool2_test.go index 58fdd1ba7..77fe870d3 100644 --- a/core/txpool/legacypool/legacypool2_test.go +++ b/core/txpool/legacypool/legacypool2_test.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/event" @@ -49,7 +50,7 @@ func fillPool(t *testing.T, pool *LegacyPool) { nonExecutableTxs := types.Transactions{} for i := 0; i < 384; i++ { key, _ := crypto.GenerateKey() - pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(10000000000)) + pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(10000000000), tracing.BalanceChangeUnspecified) // Add executable ones for j := 0; j < int(pool.config.AccountSlots); j++ { executableTxs = append(executableTxs, pricedTransaction(uint64(j), 100000, big.NewInt(300), key)) @@ -95,7 +96,7 @@ func TestTransactionFutureAttack(t *testing.T) { // Now, future transaction attack starts, let's add a bunch of expensive non-executables, and see if the pending-count drops { key, _ := crypto.GenerateKey() - pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000)) + pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000), tracing.BalanceChangeUnspecified) futureTxs := types.Transactions{} for j := 0; j < int(pool.config.GlobalSlots+pool.config.GlobalQueue); j++ { futureTxs = append(futureTxs, pricedTransaction(1000+uint64(j), 100000, big.NewInt(500), key)) @@ -129,7 +130,7 @@ func TestTransactionFuture1559(t *testing.T) { // Now, future transaction attack starts, let's add a bunch of expensive non-executables, and see if the pending-count drops { key, _ := crypto.GenerateKey() - pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000)) + pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000), tracing.BalanceChangeUnspecified) futureTxs := types.Transactions{} for j := 0; j < int(pool.config.GlobalSlots+pool.config.GlobalQueue); j++ { futureTxs = append(futureTxs, dynamicFeeTx(1000+uint64(j), 100000, big.NewInt(200), big.NewInt(101), key)) @@ -201,7 +202,7 @@ func TestTransactionZAttack(t *testing.T) { for j := 0; j < int(pool.config.GlobalQueue); j++ { futureTxs := types.Transactions{} key, _ := crypto.GenerateKey() - pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000)) + pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000), tracing.BalanceChangeUnspecified) futureTxs = append(futureTxs, pricedTransaction(1000+uint64(j), 21000, big.NewInt(500), key)) pool.AddRemotesSync(futureTxs) } @@ -209,7 +210,7 @@ func TestTransactionZAttack(t *testing.T) { overDraftTxs := types.Transactions{} { key, _ := crypto.GenerateKey() - pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000)) + pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000), tracing.BalanceChangeUnspecified) for j := 0; j < int(pool.config.GlobalSlots); j++ { overDraftTxs = append(overDraftTxs, pricedValuedTransaction(uint64(j), 60000000000, 21000, big.NewInt(500), key)) } @@ -238,7 +239,7 @@ func TestTransactionZAttack(t *testing.T) { overDraftSenderSponsoredTxs := types.Transactions{} { key, _ := crypto.GenerateKey() - pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000)) + pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(100000000000), tracing.BalanceChangeUnspecified) for j := 0; j < int(pool.config.GlobalSlots); j++ { innerTx := types.SponsoredTx{ diff --git a/core/txpool/legacypool/legacypool_test.go b/core/txpool/legacypool/legacypool_test.go index 8b94cd02d..02024db82 100644 --- a/core/txpool/legacypool/legacypool_test.go +++ b/core/txpool/legacypool/legacypool_test.go @@ -32,6 +32,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/txpool" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -282,7 +283,7 @@ func TestStateChangeDuringReset(t *testing.T) { func testAddBalance(pool *LegacyPool, addr common.Address, amount *big.Int) { pool.mu.Lock() - pool.currentState.AddBalance(addr, amount) + pool.currentState.AddBalance(addr, amount, tracing.BalanceChangeUnspecified) pool.mu.Unlock() } @@ -443,7 +444,7 @@ func TestChainFork(t *testing.T) { addr := crypto.PubkeyToAddress(key.PublicKey) resetState := func() { statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) - statedb.AddBalance(addr, big.NewInt(100000000000000)) + statedb.AddBalance(addr, big.NewInt(100000000000000), tracing.BalanceChangeUnspecified) pool.chain = &testBlockChain{1000000, statedb, new(event.Feed), 0} <-pool.requestReset(nil, nil) @@ -472,7 +473,7 @@ func TestDoubleNonce(t *testing.T) { addr := crypto.PubkeyToAddress(key.PublicKey) resetState := func() { statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) - statedb.AddBalance(addr, big.NewInt(100000000000000)) + statedb.AddBalance(addr, big.NewInt(100000000000000), tracing.BalanceChangeUnspecified) pool.chain = &testBlockChain{1000000, statedb, new(event.Feed), 0} <-pool.requestReset(nil, nil) @@ -2768,7 +2769,7 @@ func BenchmarkMultiAccountBatchInsert(b *testing.B) { for i := 0; i < b.N; i++ { key, _ := crypto.GenerateKey() account := crypto.PubkeyToAddress(key.PublicKey) - pool.currentState.AddBalance(account, big.NewInt(1000000)) + pool.currentState.AddBalance(account, big.NewInt(1000000), tracing.BalanceChangeUnspecified) tx := transaction(uint64(0), 100000, key) batches[i] = tx } @@ -3139,7 +3140,7 @@ func TestSponsoredTxInTxPoolQueue(t *testing.T) { } // 2. Sender fund is insufficient, 2 txs are removed from pending and queued - statedb.AddBalance(payerAddr, common.Big1) + statedb.AddBalance(payerAddr, common.Big1, tracing.BalanceChangeUnspecified) errs = txpool.AddRemotesSync([]*types.Transaction{tx1, tx2}) for _, err := range errs { if err != nil { @@ -3166,7 +3167,7 @@ func TestSponsoredTxInTxPoolQueue(t *testing.T) { } // 3. Payer fund is insufficient, 2 txs with the same payer in a queue - statedb.AddBalance(senderAddr, common.Big1) + statedb.AddBalance(senderAddr, common.Big1, tracing.BalanceChangeUnspecified) sponsoredTx3 := types.SponsoredTx{ ChainID: big.NewInt(2020), Nonce: 3, @@ -3304,7 +3305,7 @@ func TestFeeCapCheckVenoki(t *testing.T) { senderAddr := crypto.PubkeyToAddress(senderKey.PublicKey) statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) - statedb.AddBalance(senderAddr, new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) + statedb.AddBalance(senderAddr, new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil), tracing.BalanceChangeUnspecified) blockchain := &testBlockChain{10000000, statedb, new(event.Feed), 0} diff --git a/core/vm/evm.go b/core/vm/evm.go index 21e5d1590..d5dcbbe44 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -21,6 +21,7 @@ import ( "sync/atomic" "time" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" @@ -457,7 +458,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte // This doesn't matter on Mainnet, where all empties are gone at the time of Byzantium, // but is the correct thing to do and matters on other networks, in tests, and potential // future scenarios - evm.StateDB.AddBalance(addr, big0) + evm.StateDB.AddBalance(addr, big0, tracing.BalanceChangeTouchAccount) // Invoke tracer hooks that signal entering/exiting a call frame if evm.Config.Tracer != nil { diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 4f6cd37f9..b86e4a42f 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -18,6 +18,7 @@ package vm import ( "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" @@ -804,7 +805,7 @@ func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byt func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { beneficiary := scope.Stack.pop() balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address()) - interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance) + interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct) interpreter.evm.StateDB.SelfDestruct(scope.Contract.Address()) if interpreter.evm.Config.Tracer != nil { interpreter.cfg.Tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance) @@ -817,7 +818,7 @@ func opSelfdestruct6780(pc *uint64, interpreter *EVMInterpreter, scope *ScopeCon beneficiary := scope.Stack.pop() balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address()) interpreter.evm.StateDB.SubBalance(scope.Contract.Address(), balance) - interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance) + interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct) interpreter.evm.StateDB.SelfDestruct6780(scope.Contract.Address()) if tracer := interpreter.evm.Config.Tracer; tracer != nil { tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance) diff --git a/core/vm/interface.go b/core/vm/interface.go index bb10a69f0..61c359dbc 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -20,6 +20,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" ) @@ -29,7 +30,7 @@ type StateDB interface { CreateAccount(common.Address) SubBalance(common.Address, *big.Int) - AddBalance(common.Address, *big.Int) + AddBalance(common.Address, *big.Int, tracing.BalanceChangeReason) GetBalance(common.Address) *big.Int GetNonce(common.Address) uint64 diff --git a/miner/worker_test.go b/miner/worker_test.go index c33840365..9789dc648 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -33,6 +33,7 @@ import ( "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/txpool" "github.com/ethereum/go-ethereum/core/txpool/legacypool" "github.com/ethereum/go-ethereum/core/types" @@ -713,8 +714,8 @@ func TestCommitBlobTransaction(t *testing.T) { blobTxsByPrice := NewTransactionsByPriceAndNonce(signer, blobTxs, common.Big0) w.current = newCurrent(t, signer, w.chain) - w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) - w.current.state.AddBalance(senderAddress2, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) + w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) + w.current.state.AddBalance(senderAddress2, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) // Case 1: Not Cancun, blob transactions are not committed but plain transactions are committed // normally without error @@ -731,8 +732,8 @@ func TestCommitBlobTransaction(t *testing.T) { // Case 2: Higher blob transaction tip is prioritized w.current = newCurrent(t, signer, w.chain) - w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) - w.current.state.AddBalance(senderAddress2, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) + w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) + w.current.state.AddBalance(senderAddress2, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) w.current.header.BlobGasUsed = new(uint64) chainConfig.CancunBlock = common.Big0 @@ -795,8 +796,8 @@ func TestCommitBlobTransaction(t *testing.T) { // Case 3: Choose the blob transaction that does not make the blob gas used exceed the limit w.current = newCurrent(t, signer, w.chain) - w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) - w.current.state.AddBalance(senderAddress2, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) + w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) + w.current.state.AddBalance(senderAddress2, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) w.current.header.BlobGasUsed = new(uint64) *w.current.header.BlobGasUsed = 3 * params.BlobTxBlobGasPerBlob @@ -820,7 +821,7 @@ func TestCommitBlobTransaction(t *testing.T) { // Case 4: Blob sidecars should be discarded when commit blob transactions w.current = newCurrent(t, signer, w.chain) - w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) + w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) w.current.header.BlobGasUsed = new(uint64) chainConfig.CancunBlock = common.Big0 w.chainConfig = chainConfig @@ -847,8 +848,8 @@ func TestCommitBlobTransaction(t *testing.T) { // Case 5: correctly handle evicted pending transaction w.current = newCurrent(t, signer, w.chain) - w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) - w.current.state.AddBalance(senderAddress2, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil)) + w.current.state.AddBalance(senderAddress1, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) + w.current.state.AddBalance(senderAddress2, new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil), tracing.BalanceIncreaseSelfdestruct) w.current.header.BlobGasUsed = new(uint64) chainConfig.CancunBlock = common.Big0 w.chainConfig = chainConfig diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 272fbde5f..2db5ea10b 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -33,6 +33,7 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state/snapshot" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -276,7 +277,7 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh // - the coinbase suicided, or // - there are only 'bad' transactions, which aren't executed. In those cases, // the coinbase gets no txfee, so isn't created, and thus needs to be touched - statedb.AddBalance(block.Coinbase(), new(big.Int)) + statedb.AddBalance(block.Coinbase(), new(big.Int), tracing.BalanceChangeUnspecified) // Commit block statedb.Commit(block.NumberU64(), config.IsEIP158(block.Number())) // And _now_ get the state root From 887aa11753edb208725d00f35af02b971e827b79 Mon Sep 17 00:00:00 2001 From: sonhv0212 Date: Tue, 13 May 2025 15:35:20 +0700 Subject: [PATCH 3/4] core/state: add BalanceChangeReason to StateDB.SubBalance --- core/evm.go | 2 +- core/state/statedb.go | 4 ++-- core/state_transition.go | 2 +- core/txpool/legacypool/legacypool_test.go | 6 +++--- core/vm/instructions.go | 2 +- core/vm/interface.go | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/evm.go b/core/evm.go index b6929597f..74e145fbd 100644 --- a/core/evm.go +++ b/core/evm.go @@ -135,6 +135,6 @@ func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { // Transfer subtracts amount from sender and adds amount to recipient using the given Db func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { - db.SubBalance(sender, amount) + db.SubBalance(sender, amount, tracing.BalanceChangeTransfer) db.AddBalance(recipient, amount, tracing.BalanceChangeTransfer) } diff --git a/core/state/statedb.go b/core/state/statedb.go index 60254a990..49c49ed87 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -424,10 +424,10 @@ func (s *StateDB) AddBalance(addr common.Address, amount *big.Int, reason tracin } // SubBalance subtracts amount from the account associated with addr. -func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { +func (s *StateDB) SubBalance(addr common.Address, amount *big.Int, reason tracing.BalanceChangeReason) { stateObject := s.GetOrNewStateObject(addr) if stateObject != nil { - stateObject.SubBalance(amount, tracing.BalanceChangeUnspecified) + stateObject.SubBalance(amount, reason) } } diff --git a/core/state_transition.go b/core/state_transition.go index d8e99427c..c58ee39a5 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -364,7 +364,7 @@ func (st *StateTransition) buyGas() error { // Subtract the gas fee from balance of the fee payer, // the msg.value is transfered to the recipient in later step. - st.state.SubBalance(msg.Payer, effectiveGasFee) + st.state.SubBalance(msg.Payer, effectiveGasFee, tracing.BalanceDecreaseGasBuy) return nil } diff --git a/core/txpool/legacypool/legacypool_test.go b/core/txpool/legacypool/legacypool_test.go index 02024db82..9827eb195 100644 --- a/core/txpool/legacypool/legacypool_test.go +++ b/core/txpool/legacypool/legacypool_test.go @@ -3129,7 +3129,7 @@ func TestSponsoredTxInTxPoolQueue(t *testing.T) { } // 1. Payer fund is insufficient, 2 txs are removed from pending and queued - statedb.SubBalance(payerAddr, common.Big1) + statedb.SubBalance(payerAddr, common.Big1, tracing.BalanceChangeUnspecified) <-txpool.requestReset(nil, nil) pending, queued = txpool.Stats() if pending != 0 { @@ -3156,7 +3156,7 @@ func TestSponsoredTxInTxPoolQueue(t *testing.T) { t.Fatalf("Queued txpool, expect %d get %d", 1, queued) } - statedb.SubBalance(senderAddr, common.Big1) + statedb.SubBalance(senderAddr, common.Big1, tracing.BalanceChangeUnspecified) <-txpool.requestReset(nil, nil) pending, queued = txpool.Stats() if pending != 0 { @@ -3216,7 +3216,7 @@ func TestSponsoredTxInTxPoolQueue(t *testing.T) { t.Fatalf("Queued txpool, expect %d get %d", 1, queued) } - statedb.SubBalance(payerAddr, common.Big1) + statedb.SubBalance(payerAddr, common.Big1, tracing.BalanceChangeUnspecified) <-txpool.requestReset(nil, nil) // tx3 must be removed now _, queued = txpool.Stats() diff --git a/core/vm/instructions.go b/core/vm/instructions.go index b86e4a42f..e39b833ea 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -817,7 +817,7 @@ func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext func opSelfdestruct6780(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { beneficiary := scope.Stack.pop() balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address()) - interpreter.evm.StateDB.SubBalance(scope.Contract.Address(), balance) + interpreter.evm.StateDB.SubBalance(scope.Contract.Address(), balance, tracing.BalanceDecreaseSelfdestruct) interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct) interpreter.evm.StateDB.SelfDestruct6780(scope.Contract.Address()) if tracer := interpreter.evm.Config.Tracer; tracer != nil { diff --git a/core/vm/interface.go b/core/vm/interface.go index 61c359dbc..3e407e050 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -29,7 +29,7 @@ import ( type StateDB interface { CreateAccount(common.Address) - SubBalance(common.Address, *big.Int) + SubBalance(common.Address, *big.Int, tracing.BalanceChangeReason) AddBalance(common.Address, *big.Int, tracing.BalanceChangeReason) GetBalance(common.Address) *big.Int From 1853c9fc486b612307b38f45fee02cdce5641a36 Mon Sep 17 00:00:00 2001 From: sonhv0212 Date: Tue, 13 May 2025 15:47:13 +0700 Subject: [PATCH 4/4] core/state: add BalanceChangeReason to StateDB.SetBalance --- cmd/evm/internal/t8ntool/execution.go | 2 +- consensus/consortium/common/contract.go | 2 +- consensus/consortium/common/contract_test.go | 3 +- consensus/consortium/main.go | 2 +- consensus/misc/dao.go | 2 +- core/state/statedb.go | 4 +-- core/state/statedb_fuzz_test.go | 3 +- core/state/statedb_test.go | 34 ++++++++++---------- core/state/trie_prefetcher_test.go | 7 ++-- core/tracing/hooks.go | 5 +++ core/txpool/blobpool/blobpool_test.go | 4 +-- core/txpool/legacypool/legacypool2_test.go | 4 +-- core/txpool/legacypool/legacypool_test.go | 18 +++++------ eth/api_test.go | 3 +- internal/ethapi/api.go | 3 +- les/odr_test.go | 2 +- light/odr_test.go | 3 +- tests/state_test_util.go | 2 +- 18 files changed, 57 insertions(+), 46 deletions(-) diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index 5bbab5fc7..db3828219 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -274,7 +274,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB for addr, a := range accounts { statedb.SetCode(addr, a.Code) statedb.SetNonce(addr, a.Nonce) - statedb.SetBalance(addr, a.Balance) + statedb.SetBalance(addr, a.Balance, tracing.BalanceIncreaseGenesisBalance) for k, v := range a.Storage { statedb.SetState(addr, k, v) } diff --git a/consensus/consortium/common/contract.go b/consensus/consortium/common/contract.go index 1063d8446..71fdd6585 100644 --- a/consensus/consortium/common/contract.go +++ b/consensus/consortium/common/contract.go @@ -224,7 +224,7 @@ func (c *ContractIntegrator) WrapUpEpoch(opts *ApplyTransactOpts) error { func (c *ContractIntegrator) SubmitBlockReward(opts *ApplyTransactOpts) error { coinbase := opts.Header.Coinbase balance := opts.State.GetBalance(consensus.SystemAddress) - opts.State.SetBalance(consensus.SystemAddress, big.NewInt(0)) + opts.State.SetBalance(consensus.SystemAddress, big.NewInt(0), tracing.BalanceDecreaseSystemAddress) opts.State.AddBalance(coinbase, balance, tracing.BalanceIncreaseRewardMineBlock) nonce := opts.State.GetNonce(c.coinbase) diff --git a/consensus/consortium/common/contract_test.go b/consensus/consortium/common/contract_test.go index 8a0c7f45b..110944ba7 100644 --- a/consensus/consortium/common/contract_test.go +++ b/consensus/consortium/common/contract_test.go @@ -17,6 +17,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -64,7 +65,7 @@ func TestApplyTransactionSender(t *testing.T) { // Sender is not an empty account but still has no code, we must // not get core.ErrSenderNoEOA - state.SetBalance(sender, common.Big1) + state.SetBalance(sender, common.Big1, tracing.BalanceChangeUnspecified) err = ApplyTransaction( msg, &ApplyTransactOpts{ diff --git a/consensus/consortium/main.go b/consensus/consortium/main.go index 419d15734..7e86dc44e 100644 --- a/consensus/consortium/main.go +++ b/consensus/consortium/main.go @@ -284,7 +284,7 @@ func HandleSystemTransaction(engine consensus.Engine, statedb *state.StateDB, ms if isSystemMsg { if msg.Amount.Cmp(common.Big0) > 0 { balance := statedb.GetBalance(consensus.SystemAddress) - statedb.SetBalance(consensus.SystemAddress, big.NewInt(0)) + statedb.SetBalance(consensus.SystemAddress, big.NewInt(0), tracing.BalanceDecreaseSystemAddress) statedb.AddBalance(block.Coinbase(), balance, tracing.BalanceIncreaseRewardMineBlock) } diff --git a/consensus/misc/dao.go b/consensus/misc/dao.go index 752a91908..20f5524dc 100644 --- a/consensus/misc/dao.go +++ b/consensus/misc/dao.go @@ -82,6 +82,6 @@ func ApplyDAOHardFork(statedb *state.StateDB) { // Move every DAO account and extra-balance account funds into the refund contract for _, addr := range params.DAODrainList() { statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr), tracing.BalanceIncreaseDaoContract) - statedb.SetBalance(addr, new(big.Int)) + statedb.SetBalance(addr, new(big.Int), tracing.BalanceDecreaseDaoAccount) } } diff --git a/core/state/statedb.go b/core/state/statedb.go index 49c49ed87..f7f7702dd 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -431,10 +431,10 @@ func (s *StateDB) SubBalance(addr common.Address, amount *big.Int, reason tracin } } -func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) { +func (s *StateDB) SetBalance(addr common.Address, amount *big.Int, reason tracing.BalanceChangeReason) { stateObject := s.GetOrNewStateObject(addr) if stateObject != nil { - stateObject.SetBalance(amount, tracing.BalanceChangeUnspecified) + stateObject.SetBalance(amount, reason) } } diff --git a/core/state/statedb_fuzz_test.go b/core/state/statedb_fuzz_test.go index 1bef564ed..795884ee5 100644 --- a/core/state/statedb_fuzz_test.go +++ b/core/state/statedb_fuzz_test.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" @@ -58,7 +59,7 @@ func newStateTestAction(addr common.Address, r *rand.Rand, index int) testAction { name: "SetBalance", fn: func(a testAction, s *StateDB) { - s.SetBalance(addr, big.NewInt(a.args[0])) + s.SetBalance(addr, big.NewInt(a.args[0]), tracing.BalanceChangeUnspecified) }, args: make([]int64, 1), }, diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 4e8226934..183eb469d 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -79,7 +79,7 @@ func TestIntermediateLeaks(t *testing.T) { finalState, _ := New(common.Hash{}, NewDatabase(finalDb), nil) modify := func(state *StateDB, addr common.Address, i, tweak byte) { - state.SetBalance(addr, big.NewInt(int64(11*i)+int64(tweak))) + state.SetBalance(addr, big.NewInt(int64(11*i)+int64(tweak)), tracing.BalanceChangeUnspecified) state.SetNonce(addr, uint64(42*i+tweak)) if i%2 == 0 { state.SetState(addr, common.Hash{i, i, i, 0}, common.Hash{}) @@ -254,7 +254,7 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction { { name: "SetBalance", fn: func(a testAction, s *StateDB) { - s.SetBalance(addr, big.NewInt(a.args[0])) + s.SetBalance(addr, big.NewInt(a.args[0]), tracing.BalanceChangeUnspecified) }, args: make([]int64, 1), }, @@ -505,7 +505,7 @@ func TestTouchDelete(t *testing.T) { func TestCopyOfCopy(t *testing.T) { state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil) addr := common.HexToAddress("aaaa") - state.SetBalance(addr, big.NewInt(42)) + state.SetBalance(addr, big.NewInt(42), tracing.BalanceChangeUnspecified) if got := state.Copy().GetBalance(addr).Uint64(); got != 42 { t.Fatalf("1st copy fail, expected 42, got %v", got) @@ -528,9 +528,9 @@ func TestCopyCommitCopy(t *testing.T) { skey := common.HexToHash("aaa") sval := common.HexToHash("bbb") - state.SetBalance(addr, big.NewInt(42)) // Change the account trie - state.SetCode(addr, []byte("hello")) // Change an external metadata - state.SetState(addr, skey, sval) // Change the storage trie + state.SetBalance(addr, big.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie + state.SetCode(addr, []byte("hello")) // Change an external metadata + state.SetState(addr, skey, sval) // Change the storage trie if balance := state.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 { t.Fatalf("initial balance mismatch: have %v, want %v", balance, 42) @@ -602,9 +602,9 @@ func TestCopyCopyCommitCopy(t *testing.T) { skey := common.HexToHash("aaa") sval := common.HexToHash("bbb") - state.SetBalance(addr, big.NewInt(42)) // Change the account trie - state.SetCode(addr, []byte("hello")) // Change an external metadata - state.SetState(addr, skey, sval) // Change the storage trie + state.SetBalance(addr, big.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie + state.SetCode(addr, []byte("hello")) // Change an external metadata + state.SetState(addr, skey, sval) // Change the storage trie if balance := state.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 { t.Fatalf("initial balance mismatch: have %v, want %v", balance, 42) @@ -672,9 +672,9 @@ func TestCommitCopy(t *testing.T) { skey := common.HexToHash("aaa") sval := common.HexToHash("bbb") - state.SetBalance(addr, big.NewInt(42)) // Change the account trie - state.SetCode(addr, []byte("hello")) // Change an external metadata - state.SetState(addr, skey, sval) // Change the storage trie + state.SetBalance(addr, big.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie + state.SetCode(addr, []byte("hello")) // Change an external metadata + state.SetState(addr, skey, sval) // Change the storage trie if balance := state.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 { t.Fatalf("initial balance mismatch: have %v, want %v", balance, 42) @@ -725,7 +725,7 @@ func TestDeleteCreateRevert(t *testing.T) { state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase()), nil) addr := common.BytesToAddress([]byte("so")) - state.SetBalance(addr, big.NewInt(1)) + state.SetBalance(addr, big.NewInt(1), tracing.BalanceChangeUnspecified) root, _ := state.Commit(0, false) state, _ = New(root, state.db, state.snaps) @@ -735,7 +735,7 @@ func TestDeleteCreateRevert(t *testing.T) { state.Finalise(true) id := state.Snapshot() - state.SetBalance(addr, big.NewInt(2)) + state.SetBalance(addr, big.NewInt(2), tracing.BalanceChangeUnspecified) state.RevertToSnapshot(id) // Commit the entire state and make sure we don't crash and have the correct state @@ -759,10 +759,10 @@ func TestMissingTrieNodes(t *testing.T) { state, _ := New(common.Hash{}, db, nil) addr := common.BytesToAddress([]byte("so")) { - state.SetBalance(addr, big.NewInt(1)) + state.SetBalance(addr, big.NewInt(1), tracing.BalanceChangeUnspecified) state.SetCode(addr, []byte{1, 2, 3}) a2 := common.BytesToAddress([]byte("another")) - state.SetBalance(a2, big.NewInt(100)) + state.SetBalance(a2, big.NewInt(100), tracing.BalanceChangeUnspecified) state.SetCode(a2, []byte{1, 2, 4}) root, _ = state.Commit(0, false) t.Logf("root: %x", root) @@ -787,7 +787,7 @@ func TestMissingTrieNodes(t *testing.T) { t.Errorf("expected %d, got %d", exp, got) } // Modify the state - state.SetBalance(addr, big.NewInt(2)) + state.SetBalance(addr, big.NewInt(2), tracing.BalanceChangeUnspecified) root, err := state.Commit(0, false) if err == nil { t.Fatalf("expected error, got root :%x", root) diff --git a/core/state/trie_prefetcher_test.go b/core/state/trie_prefetcher_test.go index cb0b67d7e..98f6bb092 100644 --- a/core/state/trie_prefetcher_test.go +++ b/core/state/trie_prefetcher_test.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/tracing" ) func filledStateDB() *StateDB { @@ -33,9 +34,9 @@ func filledStateDB() *StateDB { skey := common.HexToHash("aaa") sval := common.HexToHash("bbb") - state.SetBalance(addr, big.NewInt(42)) // Change the account trie - state.SetCode(addr, []byte("hello")) // Change an external metadata - state.SetState(addr, skey, sval) // Change the storage trie + state.SetBalance(addr, big.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie + state.SetCode(addr, []byte("hello")) // Change an external metadata + state.SetState(addr, skey, sval) // Change the storage trie for i := 0; i < 100; i++ { sk := common.BigToHash(big.NewInt(int64(i))) state.SetState(addr, sk, sk) // Change the storage trie diff --git a/core/tracing/hooks.go b/core/tracing/hooks.go index 0485f7a3e..1f1a199aa 100644 --- a/core/tracing/hooks.go +++ b/core/tracing/hooks.go @@ -270,6 +270,11 @@ const ( // BalanceChangeRevert is emitted when the balance is reverted back to a previous value due to call failure. // It is only emitted when the tracer has opted in to use the journaling wrapper (WrapWithJournal). BalanceChangeRevert BalanceChangeReason = 15 + + // Consortium consensus + // BalanceDecreaseSystemAddress is emitted when the balance of a system address is transferred to the + // coinbase and then set to 0. + BalanceDecreaseSystemAddress BalanceChangeReason = 16 ) // GasChangeReason is used to indicate the reason for a gas change, useful diff --git a/core/txpool/blobpool/blobpool_test.go b/core/txpool/blobpool/blobpool_test.go index d0da589fb..4ddf1c5db 100644 --- a/core/txpool/blobpool/blobpool_test.go +++ b/core/txpool/blobpool/blobpool_test.go @@ -1538,8 +1538,8 @@ func TestSponsoredTxRejection(t *testing.T) { } recipient := common.HexToAddress("1000000000000000000000000000000000000001") - statedb.SetBalance(crypto.PubkeyToAddress(payerKey.PublicKey), new(big.Int).Mul(big.NewInt(100000), big.NewInt(22000))) - statedb.SetBalance(crypto.PubkeyToAddress(senderKey.PublicKey), big.NewInt(10)) + statedb.SetBalance(crypto.PubkeyToAddress(payerKey.PublicKey), new(big.Int).Mul(big.NewInt(100000), big.NewInt(22000)), tracing.BalanceChangeUnspecified) + statedb.SetBalance(crypto.PubkeyToAddress(senderKey.PublicKey), big.NewInt(10), tracing.BalanceChangeUnspecified) innerTx := types.SponsoredTx{ ChainID: big.NewInt(2020), diff --git a/core/txpool/legacypool/legacypool2_test.go b/core/txpool/legacypool/legacypool2_test.go index 77fe870d3..6520986d5 100644 --- a/core/txpool/legacypool/legacypool2_test.go +++ b/core/txpool/legacypool/legacypool2_test.go @@ -235,7 +235,7 @@ func TestTransactionZAttack(t *testing.T) { payerKey, _ := crypto.GenerateKey() payerAccount := crypto.PubkeyToAddress(payerKey.PublicKey) - pool.currentState.SetBalance(payerAccount, new(big.Int).SetUint64(1000*21000*pool.config.GlobalSlots)) + pool.currentState.SetBalance(payerAccount, new(big.Int).SetUint64(1000*21000*pool.config.GlobalSlots), tracing.BalanceChangeUnspecified) overDraftSenderSponsoredTxs := types.Transactions{} { key, _ := crypto.GenerateKey() @@ -286,7 +286,7 @@ func TestTransactionZAttack(t *testing.T) { payerKey2, _ := crypto.GenerateKey() payerAccount2 := crypto.PubkeyToAddress(payerKey2.PublicKey) - pool.currentState.SetBalance(payerAccount2, new(big.Int).SetUint64(21000*600)) + pool.currentState.SetBalance(payerAccount2, new(big.Int).SetUint64(21000*600), tracing.BalanceChangeUnspecified) overDraftPayerSponsoredTxs := types.Transactions{} { key, _ := crypto.GenerateKey() diff --git a/core/txpool/legacypool/legacypool_test.go b/core/txpool/legacypool/legacypool_test.go index 9827eb195..ae4292941 100644 --- a/core/txpool/legacypool/legacypool_test.go +++ b/core/txpool/legacypool/legacypool_test.go @@ -225,7 +225,7 @@ func (c *testChain) State() (*state.StateDB, error) { c.statedb, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) // simulate that the new head block included tx0 and tx1 c.statedb.SetNonce(c.address, 2) - c.statedb.SetBalance(c.address, new(big.Int).SetUint64(params.Ether)) + c.statedb.SetBalance(c.address, new(big.Int).SetUint64(params.Ether), tracing.BalanceChangeUnspecified) *c.trigger = false } return stdb, nil @@ -245,7 +245,7 @@ func TestStateChangeDuringReset(t *testing.T) { ) // setup pool with 2 transaction in it - statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether)) + statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether), tracing.BalanceChangeUnspecified) blockchain := &testChain{&testBlockChain{1000000000, statedb, new(event.Feed), 0}, address, &trigger} tx0 := transaction(0, 100000, key) @@ -2961,14 +2961,14 @@ func TestExpiredTimeAndGasCheckSponsoredTx(t *testing.T) { } // 5. Failed when sender does not have sufficient fund for msg.value - statedb.SetBalance(crypto.PubkeyToAddress(payerKey.PublicKey), new(big.Int).Mul(big.NewInt(100000), big.NewInt(22000))) + statedb.SetBalance(crypto.PubkeyToAddress(payerKey.PublicKey), new(big.Int).Mul(big.NewInt(100000), big.NewInt(22000)), tracing.BalanceChangeUnspecified) err = txpool.addRemoteSync(tx) if err == nil || !errors.Is(err, core.ErrInsufficientSenderFunds) { t.Fatalf("Expect error %s, get %s", core.ErrInsufficientSenderFunds, err) } // 6. Successfully add tx - statedb.SetBalance(crypto.PubkeyToAddress(senderKey.PublicKey), big.NewInt(10)) + statedb.SetBalance(crypto.PubkeyToAddress(senderKey.PublicKey), big.NewInt(10), tracing.BalanceChangeUnspecified) err = txpool.addRemoteSync(tx) if err != nil { t.Fatalf("Expect successfully add tx, get %s", err) @@ -3002,7 +3002,7 @@ func TestExpiredTimeAndGasCheckSponsoredTx(t *testing.T) { innerTx.Nonce = 3 innerTx.GasFeeCap = big.NewInt(params.MinimumBaseFee + 1) innerTx.Value = common.Big0 - statedb.SetBalance(crypto.PubkeyToAddress(payerKey.PublicKey), new(big.Int).Mul(innerTx.GasFeeCap, big.NewInt(22000))) + statedb.SetBalance(crypto.PubkeyToAddress(payerKey.PublicKey), new(big.Int).Mul(innerTx.GasFeeCap, big.NewInt(22000)), tracing.BalanceChangeUnspecified) innerTx.PayerR, innerTx.PayerS, innerTx.PayerV, err = types.PayerSign( payerKey, mikoSigner, @@ -3079,8 +3079,8 @@ func TestSponsoredTxInTxPoolQueue(t *testing.T) { ExpiredTime: 100, } gasFee := new(big.Int).Mul(sponsoredTx1.GasFeeCap, new(big.Int).SetUint64(sponsoredTx1.Gas)) - statedb.SetBalance(payerAddr, gasFee) - statedb.SetBalance(senderAddr, sponsoredTx1.Value) + statedb.SetBalance(payerAddr, gasFee, tracing.BalanceChangeUnspecified) + statedb.SetBalance(senderAddr, sponsoredTx1.Value, tracing.BalanceChangeUnspecified) mikoSigner := types.NewMikoSigner(big.NewInt(2020)) sponsoredTx1.PayerR, sponsoredTx1.PayerS, sponsoredTx1.PayerV, err = types.PayerSign( @@ -3207,7 +3207,7 @@ func TestSponsoredTxInTxPoolQueue(t *testing.T) { } gasFee = new(big.Int).Mul(sponsoredTx3.GasFeeCap, new(big.Int).SetUint64(sponsoredTx3.Gas)) - statedb.SetBalance(payerAddr, gasFee) + statedb.SetBalance(payerAddr, gasFee, tracing.BalanceChangeUnspecified) <-txpool.requestReset(nil, nil) // tx2 must be removed from queue but not tx3 @@ -3226,7 +3226,7 @@ func TestSponsoredTxInTxPoolQueue(t *testing.T) { // 4. Expired txs are removed from pending and queued gasFee = new(big.Int).Mul(sponsoredTx1.GasFeeCap, new(big.Int).SetUint64(sponsoredTx1.Gas)) - statedb.SetBalance(payerAddr, gasFee) + statedb.SetBalance(payerAddr, gasFee, tracing.BalanceChangeUnspecified) errs = txpool.AddRemotesSync([]*types.Transaction{tx1, tx2}) for _, err := range errs { if err != nil { diff --git a/eth/api_test.go b/eth/api_test.go index 1819b1ec0..fbd250f58 100644 --- a/eth/api_test.go +++ b/eth/api_test.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/trie" @@ -78,7 +79,7 @@ func TestAccountRange(t *testing.T) { hash := common.HexToHash(fmt.Sprintf("%x", i)) addr := common.BytesToAddress(crypto.Keccak256Hash(hash.Bytes()).Bytes()) addrs[i] = addr - sdb.SetBalance(addrs[i], big.NewInt(1)) + sdb.SetBalance(addrs[i], big.NewInt(1), tracing.BalanceChangeUnspecified) if _, ok := m[addr]; ok { t.Fatalf("bad") } else { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 39a90f873..6de45bfd4 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -37,6 +37,7 @@ import ( "github.com/ethereum/go-ethereum/consensus/misc/eip1559" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -942,7 +943,7 @@ func (diff *StateOverride) Apply(state *state.StateDB) error { } // Override account balance. if account.Balance != nil { - state.SetBalance(addr, (*big.Int)(*account.Balance)) + state.SetBalance(addr, (*big.Int)(*account.Balance), tracing.BalanceChangeUnspecified) } if account.State != nil && account.StateDiff != nil { return fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex()) diff --git a/les/odr_test.go b/les/odr_test.go index 4b3ef5a13..e04069b70 100644 --- a/les/odr_test.go +++ b/les/odr_test.go @@ -159,7 +159,7 @@ func odrContractCall(ctx context.Context, db ethdb.Database, config *params.Chai } else { header := lc.GetHeaderByHash(bhash) state := light.NewState(ctx, header, lc.Odr()) - state.SetBalance(bankAddr, math.MaxBig256) + state.SetBalance(bankAddr, math.MaxBig256, tracing.BalanceChangeUnspecified) msg := core.NewMessage( bankAddr, &testContractAddr, diff --git a/light/odr_test.go b/light/odr_test.go index 16bbe701e..e7b440b71 100644 --- a/light/odr_test.go +++ b/light/odr_test.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -187,7 +188,7 @@ func odrContractCall(ctx context.Context, db ethdb.Database, bc *core.BlockChain } // Perform read-only call. - st.SetBalance(testBankAddress, math.MaxBig256) + st.SetBalance(testBankAddress, math.MaxBig256, tracing.BalanceChangeUnspecified) msg := core.NewMessage( testBankAddress, &testContractAddr, diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 2db5ea10b..64043a9e4 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -302,7 +302,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc, snapshotter bo for addr, a := range accounts { statedb.SetCode(addr, a.Code) statedb.SetNonce(addr, a.Nonce) - statedb.SetBalance(addr, a.Balance) + statedb.SetBalance(addr, a.Balance, tracing.BalanceChangeUnspecified) for k, v := range a.Storage { statedb.SetState(addr, k, v) }