Skip to content

Commit

Permalink
refine balanceChangeReason and replace CaptureArbitrumTransfer purpos…
Browse files Browse the repository at this point in the history
…e with balanceChangeReason
  • Loading branch information
ganeshvanahalli committed Nov 18, 2024
1 parent ef29d1e commit 23b7093
Show file tree
Hide file tree
Showing 31 changed files with 224 additions and 215 deletions.
8 changes: 4 additions & 4 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (st *StateTransition) buyGas() error {

// Arbitrum: record fee payment
if tracer := st.evm.Config.Tracer; tracer != nil && tracer.CaptureArbitrumTransfer != nil {
tracer.CaptureArbitrumTransfer(&st.msg.From, nil, mgval, true, "feePayment")
tracer.CaptureArbitrumTransfer(&st.msg.From, nil, mgval, true, tracing.BalanceDecreaseGasBuy)
}

return nil
Expand Down Expand Up @@ -529,7 +529,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {

// Arbitrum: record the tip
if tracer := st.evm.Config.Tracer; tracer != nil && !st.evm.ProcessingHook.DropTip() && tracer.CaptureArbitrumTransfer != nil {
tracer.CaptureArbitrumTransfer(nil, &tipReceipient, tipAmount, false, "tip")
tracer.CaptureArbitrumTransfer(nil, &tipReceipient, tipAmount, false, tracing.BalanceIncreaseRewardTransactionFee)
}

st.evm.ProcessingHook.EndTxHook(st.gasRemaining, vmerr == nil)
Expand All @@ -539,7 +539,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
suicides := st.evm.StateDB.GetSelfDestructs()
for i, address := range suicides {
balance := st.evm.StateDB.GetBalance(address)
tracer.CaptureArbitrumTransfer(&suicides[i], nil, balance.ToBig(), false, "selfDestruct")
tracer.CaptureArbitrumTransfer(&suicides[i], nil, balance.ToBig(), false, tracing.BalanceDecreaseSelfdestruct)
}
}

Expand Down Expand Up @@ -583,7 +583,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64) uint64 {

// Arbitrum: record the gas refund
if tracer := st.evm.Config.Tracer; tracer != nil && tracer.CaptureArbitrumTransfer != nil {
tracer.CaptureArbitrumTransfer(nil, &st.msg.From, remaining.ToBig(), false, "gasRefund")
tracer.CaptureArbitrumTransfer(nil, &st.msg.From, remaining.ToBig(), false, tracing.BalanceIncreaseGasReturn)
}

// Also return remaining gas to the block gas counter so it is
Expand Down
69 changes: 40 additions & 29 deletions core/tracing/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ type (
// LogHook is called when a log is emitted.
LogHook = func(log *types.Log)

CaptureArbitrumTransferHook = func(from, to *common.Address, value *big.Int, before bool, purpose string)
CaptureArbitrumTransferHook = func(from, to *common.Address, value *big.Int, before bool, reason BalanceChangeReason)
CaptureArbitrumStorageGetHook = func(key common.Hash, depth int, before bool)
CaptureArbitrumStorageSetHook = func(key, value common.Hash, depth int, before bool)

Expand Down Expand Up @@ -262,67 +262,78 @@ const (
)

func (b BalanceChangeReason) String(prev, new *big.Int) string {
// When both prev and new are nil, we only return the main reason without specifying if the balance incrased or decreased
// useful for CaptureArbitrumTransfer
var reason string

prependString := func() string {
if prev == nil || new == nil {
return ""
}
if prev != nil && new != nil {
if new.Cmp(prev) == 1 {
return "balance increase due to a "
reason = "balance increase due to "
} else if new.Cmp(prev) == -1 {
return "balance decrease due to a "
reason = "balance decrease due to "
}
return ""
} else if new != nil {
reason = "balance increase due to "
} else if prev != nil {
reason = "balance decrease due to "
}

// Append main reason for the balance change
switch b {
case BalanceIncreaseRewardTransactionFee:
reason += "payment of transaction tip"
case BalanceDecreaseGasBuy:
reason += "purchase of gas for execution of a transaction"
case BalanceIncreaseGasReturn:
reason += "refund for unused gas at the end of execution"
case BalanceChangeTransfer:
reason = prependString() + "transfer via a call"
reason += "transfer via a call"
case BalanceDecreaseSelfdestruct:
reason += "selfDestruct"
case BalanceIncreaseDeposit:
reason = "balance increase via a deposit"
reason += "deposit"
case BalanceDecreaseWithdrawToL1:
reason = "balance decrease via a withdrawal to L1"
reason += "withdrawal to L1"
case BalanceIncreaseL1PosterFee:
reason = "balance increase via a fee collection for L1 posting"
reason += "fee collection for L1 posting"
case BalanceIncreaseInfraFee:
reason = "balance increase via a fee collection by infrastructure fee account"
reason += "fee collection by infrastructure fee account"
case BalanceIncreaseNetworkFee:
reason = "balance increase via a fee collection by network fee account"
reason += "fee collection by network fee account"
// ArbitrumRetryTx
case BalanceIncreaseRetryTxPrepaid:
reason = "balance increase by prepaid value for a tx of ArbitrumRetryTx type"
reason += "deposit of prepaid value in a tx of ArbitrumRetryTx type"
case BalanceDecreaseRetryTxUndoRefund:
reason = "balance decrease by undoing Geth's refund for a tx of ArbitrumRetryTx type"
reason += "undoing of Geth's refund for a tx of ArbitrumRetryTx type"
case BalanceChangeTransferRetryTxToEscrow:
reason = prependString() + "transfer to escrow in a tx of ArbitrumRetryTx type"
reason += "transfer to escrow in a tx of ArbitrumRetryTx type"
case BalanceChangeTransferRetryTxFromEscrow:
reason = prependString() + "transfer from escrow in a tx of ArbitrumRetryTx type"
reason += "transfer from escrow in a tx of ArbitrumRetryTx type"
// ArbitrumSubmitRetryableTx
case BalanceChangeTransferRetryableToFeeRefundAddr:
reason = prependString() + "transfer to FeeRefundAddr in a tx of ArbitrumSubmitRetryableTx type"
reason += "transfer to FeeRefundAddr in a tx of ArbitrumSubmitRetryableTx type"
case BalanceChangeTransferRetryableToEscrow:
reason = prependString() + "transfer to escrow in a tx of ArbitrumSubmitRetryableTx type"
reason += "transfer to escrow in a tx of ArbitrumSubmitRetryableTx type"
case BalanceChangeTransferRetryableToInfra:
reason = prependString() + "transfer to infrastructure fee account in a tx of ArbitrumSubmitRetryableTx type"
reason += "transfer to infrastructure fee account in a tx of ArbitrumSubmitRetryableTx type"
case BalanceChangeTransferRetryableFromInfra:
reason = prependString() + "transfer from infrastructure fee account in a tx of ArbitrumSubmitRetryableTx type"
reason += "transfer from infrastructure fee account in a tx of ArbitrumSubmitRetryableTx type"
case BalanceChangeTransferRetryableToNetwork:
reason = prependString() + "transfer to network fee account in a tx of ArbitrumSubmitRetryableTx type"
reason += "transfer to network fee account in a tx of ArbitrumSubmitRetryableTx type"
case BalanceChangeTransferRetryableFromNetwork:
reason = prependString() + "transfer from network fee account in a tx of ArbitrumSubmitRetryableTx type"
reason += "transfer from network fee account in a tx of ArbitrumSubmitRetryableTx type"
// Batchposter
case BalanceChangeTransferBatchposterReward:
reason = prependString() + "transfer from L1PricerFundsPoolAddress as batchPosterReward"
reason += "transfer from L1PricerFundsPoolAddress as batchPosterReward"
case BalanceChangeTransferBatchposterRefund:
reason = prependString() + "transfer from L1PricerFundsPoolAddress as batchPosterRefund"
reason += "transfer from L1PricerFundsPoolAddress as batchPosterRefund"
// Stylus
case BalanceChangeTransferActivationFee:
reason = prependString() + "transfer of activation fee to network fee account"
reason += "transfer of activation fee to network fee account"
case BalanceChangeTransferActivationReimburse:
reason = prependString() + "transfer of reimburse amount after charging the activation fee"
reason += "transfer of reimburse amount after charging the activation fee"
default:
reason = "unspecified"
return "unspecified"
}

return reason
Expand Down
6 changes: 3 additions & 3 deletions eth/tracers/internal/tracetest/calltrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,13 @@ func TestInternals(t *testing.T) {
byte(vm.CALL),
},
tracer: mkTracer("callTracer", nil),
want: fmt.Sprintf(`{"beforeEVMTransfers":[{"purpose":"feePayment","from":"%s","to":null,"value":"0x13880"}],"afterEVMTransfers":[{"purpose":"gasRefund","from":null,"to":"%s","value":"0xe3a8"},{"purpose":"tip","from":null,"to":"0x0000000000000000000000000000000000000000","value":"0x54d8"}],"balanceChanges":[{"addr":"%s","prev":"0x1c6bf52634000","new":"0x1c6bf52620780","reason":"unspecified"},{"addr":"%s","prev":"0x1c6bf52620780","new":"0x1c6bf5262eb28","reason":"unspecified"},{"addr":"0x0000000000000000000000000000000000000000","prev":"0x0","new":"0x54d8","reason":"unspecified"}],"from":"%s","gas":"0x13880","gasUsed":"0x54d8","to":"0x00000000000000000000000000000000deadbeef","input":"0x","calls":[{"from":"0x00000000000000000000000000000000deadbeef","gas":"0xe01a","gasUsed":"0x0","to":"0x00000000000000000000000000000000000000ff","input":"0x","value":"0x0","type":"CALL"}],"value":"0x0","type":"CALL"}`, origin.Hex(), origin.Hex(), origin.Hex(), origin.Hex(), originHex),
want: fmt.Sprintf(`{"beforeEVMTransfers":[{"purpose":"purchase of gas for execution of a transaction","from":"%s","to":null,"value":"0x13880"}],"afterEVMTransfers":[{"purpose":"refund for unused gas at the end of execution","from":null,"to":"%s","value":"0xe3a8"},{"purpose":"payment of transaction tip","from":null,"to":"0x0000000000000000000000000000000000000000","value":"0x54d8"}],"balanceChanges":[{"addr":"%s","prev":"0x1c6bf52634000","new":"0x1c6bf52620780","reason":"balance decrease due to purchase of gas for execution of a transaction"},{"addr":"%s","prev":"0x1c6bf52620780","new":"0x1c6bf5262eb28","reason":"balance increase due to refund for unused gas at the end of execution"},{"addr":"0x0000000000000000000000000000000000000000","prev":"0x0","new":"0x54d8","reason":"balance increase due to payment of transaction tip"}],"from":"%s","gas":"0x13880","gasUsed":"0x54d8","to":"0x00000000000000000000000000000000deadbeef","input":"0x","calls":[{"from":"0x00000000000000000000000000000000deadbeef","gas":"0xe01a","gasUsed":"0x0","to":"0x00000000000000000000000000000000000000ff","input":"0x","value":"0x0","type":"CALL"}],"value":"0x0","type":"CALL"}`, origin.Hex(), origin.Hex(), origin.Hex(), origin.Hex(), originHex),
},
{
name: "Stack depletion in LOG0",
code: []byte{byte(vm.LOG3)},
tracer: mkTracer("callTracer", json.RawMessage(`{ "withLog": true }`)),
want: fmt.Sprintf(`{"beforeEVMTransfers":[{"purpose":"feePayment","from":"%s","to":null,"value":"0x13880"}],"afterEVMTransfers":[{"purpose":"gasRefund","from":null,"to":"%s","value":"0x0"},{"purpose":"tip","from":null,"to":"0x0000000000000000000000000000000000000000","value":"0x13880"}],"balanceChanges":[{"addr":"%s","prev":"0x1c6bf52634000","new":"0x1c6bf52620780","reason":"unspecified"},{"addr":"0x0000000000000000000000000000000000000000","prev":"0x0","new":"0x13880","reason":"unspecified"}],"from":"%s","gas":"0x13880","gasUsed":"0x13880","to":"0x00000000000000000000000000000000deadbeef","input":"0x","error":"stack underflow (0 \u003c=\u003e 5)","value":"0x0","type":"CALL"}`, origin.Hex(), origin.Hex(), origin.Hex(), originHex),
want: fmt.Sprintf(`{"beforeEVMTransfers":[{"purpose":"purchase of gas for execution of a transaction","from":"%s","to":null,"value":"0x13880"}],"afterEVMTransfers":[{"purpose":"refund for unused gas at the end of execution","from":null,"to":"%s","value":"0x0"},{"purpose":"payment of transaction tip","from":null,"to":"0x0000000000000000000000000000000000000000","value":"0x13880"}],"balanceChanges":[{"addr":"%s","prev":"0x1c6bf52634000","new":"0x1c6bf52620780","reason":"balance decrease due to purchase of gas for execution of a transaction"},{"addr":"0x0000000000000000000000000000000000000000","prev":"0x0","new":"0x13880","reason":"balance increase due to payment of transaction tip"}],"from":"%s","gas":"0x13880","gasUsed":"0x13880","to":"0x00000000000000000000000000000000deadbeef","input":"0x","error":"stack underflow (0 \u003c=\u003e 5)","value":"0x0","type":"CALL"}`, origin.Hex(), origin.Hex(), origin.Hex(), originHex),
},
{
name: "Mem expansion in LOG0",
Expand All @@ -328,7 +328,7 @@ func TestInternals(t *testing.T) {
byte(vm.LOG0),
},
tracer: mkTracer("callTracer", json.RawMessage(`{ "withLog": true }`)),
want: fmt.Sprintf(`{"beforeEVMTransfers":[{"purpose":"feePayment","from":"%s","to":null,"value":"0x13880"}],"afterEVMTransfers":[{"purpose":"gasRefund","from":null,"to":"%s","value":"0xdce2"},{"purpose":"tip","from":null,"to":"0x0000000000000000000000000000000000000000","value":"0x5b9e"}],"balanceChanges":[{"addr":"%s","prev":"0x1c6bf52634000","new":"0x1c6bf52620780","reason":"unspecified"},{"addr":"%s","prev":"0x1c6bf52620780","new":"0x1c6bf5262e462","reason":"unspecified"},{"addr":"0x0000000000000000000000000000000000000000","prev":"0x0","new":"0x5b9e","reason":"unspecified"}],"from":"%s","gas":"0x13880","gasUsed":"0x5b9e","to":"0x00000000000000000000000000000000deadbeef","input":"0x","logs":[{"address":"0x00000000000000000000000000000000deadbeef","topics":[],"data":"0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","position":"0x0"}],"value":"0x0","type":"CALL"}`, origin.Hex(), origin.Hex(), origin.Hex(), origin.Hex(), originHex),
want: fmt.Sprintf(`{"beforeEVMTransfers":[{"purpose":"purchase of gas for execution of a transaction","from":"%s","to":null,"value":"0x13880"}],"afterEVMTransfers":[{"purpose":"refund for unused gas at the end of execution","from":null,"to":"%s","value":"0xdce2"},{"purpose":"payment of transaction tip","from":null,"to":"0x0000000000000000000000000000000000000000","value":"0x5b9e"}],"balanceChanges":[{"addr":"%s","prev":"0x1c6bf52634000","new":"0x1c6bf52620780","reason":"balance decrease due to purchase of gas for execution of a transaction"},{"addr":"%s","prev":"0x1c6bf52620780","new":"0x1c6bf5262e462","reason":"balance increase due to refund for unused gas at the end of execution"},{"addr":"0x0000000000000000000000000000000000000000","prev":"0x0","new":"0x5b9e","reason":"balance increase due to payment of transaction tip"}],"from":"%s","gas":"0x13880","gasUsed":"0x5b9e","to":"0x00000000000000000000000000000000deadbeef","input":"0x","logs":[{"address":"0x00000000000000000000000000000000deadbeef","topics":[],"data":"0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","position":"0x0"}],"value":"0x0","type":"CALL"}`, origin.Hex(), origin.Hex(), origin.Hex(), origin.Hex(), originHex),
},
{
// Leads to OOM on the prestate tracer
Expand Down
10 changes: 5 additions & 5 deletions eth/tracers/internal/tracetest/testdata/call_tracer/blob_tx.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@
"result": {
"beforeEVMTransfers": [
{
"purpose": "feePayment",
"purpose": "purchase of gas for execution of a transaction",
"from": "0x0c2c51a0990AeE1d73C1228de158688341557508",
"to": null,
"value": "0x2de364958"
}
],
"afterEVMTransfers": [
{
"purpose": "gasRefund",
"purpose": "refund for unused gas at the end of execution",
"from": null,
"to": "0x0c2c51a0990AeE1d73C1228de158688341557508",
"value": "0x0"
},
{
"purpose": "tip",
"purpose": "payment of transaction tip",
"from": null,
"to": "0x0000000000000000000000000000000000000000",
"value": "0x200b20"
Expand All @@ -83,13 +83,13 @@
"addr": "0x0c2c51a0990AeE1d73C1228de158688341557508",
"prev": "0xde0b6b3a7640000",
"new": "0xde0b6b0c92db6a8",
"reason": "unspecified"
"reason": "balance decrease due to purchase of gas for execution of a transaction"
},
{
"addr": "0x0000000000000000000000000000000000000000",
"prev": "0x272e0528",
"new": "0x274e1048",
"reason": "unspecified"
"reason": "balance increase due to payment of transaction tip"
}
],
"from": "0x0c2c51a0990aee1d73c1228de158688341557508",
Expand Down
10 changes: 5 additions & 5 deletions eth/tracers/internal/tracetest/testdata/call_tracer/create.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@
"result": {
"beforeEVMTransfers": [
{
"purpose": "feePayment",
"purpose": "purchase of gas for execution of a transaction",
"from": "0x13e4ACeFE6a6700604929946E70E6443E4E73447",
"to": null,
"value": "0x2a0383e2a65c00"
}
],
"afterEVMTransfers": [
{
"purpose": "gasRefund",
"purpose": "refund for unused gas at the end of execution",
"from": null,
"to": "0x13e4ACeFE6a6700604929946E70E6443E4E73447",
"value": "0x0"
},
{
"purpose": "tip",
"purpose": "payment of transaction tip",
"from": null,
"to": "0xD049bfd667cB46Aa3Ef5Df0dA3e57DB3Be39E511",
"value": "0x2a0383e2a65c00"
Expand All @@ -73,13 +73,13 @@
"addr": "0x13e4ACeFE6a6700604929946E70E6443E4E73447",
"prev": "0xcf3e0938579f000",
"new": "0xcc9dd0fa2d39400",
"reason": "unspecified"
"reason": "balance decrease due to purchase of gas for execution of a transaction"
},
{
"addr": "0xD049bfd667cB46Aa3Ef5Df0dA3e57DB3Be39E511",
"prev": "0x0",
"new": "0x2a0383e2a65c00",
"reason": "unspecified"
"reason": "balance increase due to payment of transaction tip"
}
],
"from": "0x13e4acefe6a6700604929946e70e6443e4e73447",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,21 @@
"result": {
"beforeEVMTransfers": [
{
"purpose": "feePayment",
"purpose": "purchase of gas for execution of a transaction",
"from": "0x70C9217D814985faEF62B124420F8dFbdDD96433",
"to": null,
"value": "0x11c37937e08000"
}
],
"afterEVMTransfers": [
{
"purpose": "gasRefund",
"purpose": "refund for unused gas at the end of execution",
"from": null,
"to": "0x70C9217D814985faEF62B124420F8dFbdDD96433",
"value": "0xac27a3b12e800"
},
{
"purpose": "tip",
"purpose": "payment of transaction tip",
"from": null,
"to": "0x1977C248e1014Cc103929Dd7f154199C916E39Ec",
"value": "0x700fefccd9800"
Expand All @@ -136,19 +136,19 @@
"addr": "0x70C9217D814985faEF62B124420F8dFbdDD96433",
"prev": "0x4ef436dcbda6cd4a",
"new": "0x4ee2736385c64d4a",
"reason": "unspecified"
"reason": "balance decrease due to purchase of gas for execution of a transaction"
},
{
"addr": "0x70C9217D814985faEF62B124420F8dFbdDD96433",
"prev": "0x4ee2736385c64d4a",
"new": "0x4eed35ddc0d9354a",
"reason": "unspecified"
"reason": "balance increase due to refund for unused gas at the end of execution"
},
{
"addr": "0x1977C248e1014Cc103929Dd7f154199C916E39Ec",
"prev": "0x0",
"new": "0x700fefccd9800",
"reason": "unspecified"
"reason": "balance increase due to payment of transaction tip"
}
],
"calls": [
Expand Down
Loading

0 comments on commit 23b7093

Please sign in to comment.