Skip to content

Commit 23b7093

Browse files
refine balanceChangeReason and replace CaptureArbitrumTransfer purpose with balanceChangeReason
1 parent ef29d1e commit 23b7093

31 files changed

+224
-215
lines changed

core/state_transition.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (st *StateTransition) buyGas() error {
310310

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

316316
return nil
@@ -529,7 +529,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
529529

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

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

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

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

589589
// Also return remaining gas to the block gas counter so it is

core/tracing/hooks.go

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ type (
147147
// LogHook is called when a log is emitted.
148148
LogHook = func(log *types.Log)
149149

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

@@ -262,67 +262,78 @@ const (
262262
)
263263

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

267-
prependString := func() string {
268-
if prev == nil || new == nil {
269-
return ""
270-
}
269+
if prev != nil && new != nil {
271270
if new.Cmp(prev) == 1 {
272-
return "balance increase due to a "
271+
reason = "balance increase due to "
273272
} else if new.Cmp(prev) == -1 {
274-
return "balance decrease due to a "
273+
reason = "balance decrease due to "
275274
}
276-
return ""
275+
} else if new != nil {
276+
reason = "balance increase due to "
277+
} else if prev != nil {
278+
reason = "balance decrease due to "
277279
}
278280

281+
// Append main reason for the balance change
279282
switch b {
283+
case BalanceIncreaseRewardTransactionFee:
284+
reason += "payment of transaction tip"
285+
case BalanceDecreaseGasBuy:
286+
reason += "purchase of gas for execution of a transaction"
287+
case BalanceIncreaseGasReturn:
288+
reason += "refund for unused gas at the end of execution"
280289
case BalanceChangeTransfer:
281-
reason = prependString() + "transfer via a call"
290+
reason += "transfer via a call"
291+
case BalanceDecreaseSelfdestruct:
292+
reason += "selfDestruct"
282293
case BalanceIncreaseDeposit:
283-
reason = "balance increase via a deposit"
294+
reason += "deposit"
284295
case BalanceDecreaseWithdrawToL1:
285-
reason = "balance decrease via a withdrawal to L1"
296+
reason += "withdrawal to L1"
286297
case BalanceIncreaseL1PosterFee:
287-
reason = "balance increase via a fee collection for L1 posting"
298+
reason += "fee collection for L1 posting"
288299
case BalanceIncreaseInfraFee:
289-
reason = "balance increase via a fee collection by infrastructure fee account"
300+
reason += "fee collection by infrastructure fee account"
290301
case BalanceIncreaseNetworkFee:
291-
reason = "balance increase via a fee collection by network fee account"
302+
reason += "fee collection by network fee account"
292303
// ArbitrumRetryTx
293304
case BalanceIncreaseRetryTxPrepaid:
294-
reason = "balance increase by prepaid value for a tx of ArbitrumRetryTx type"
305+
reason += "deposit of prepaid value in a tx of ArbitrumRetryTx type"
295306
case BalanceDecreaseRetryTxUndoRefund:
296-
reason = "balance decrease by undoing Geth's refund for a tx of ArbitrumRetryTx type"
307+
reason += "undoing of Geth's refund for a tx of ArbitrumRetryTx type"
297308
case BalanceChangeTransferRetryTxToEscrow:
298-
reason = prependString() + "transfer to escrow in a tx of ArbitrumRetryTx type"
309+
reason += "transfer to escrow in a tx of ArbitrumRetryTx type"
299310
case BalanceChangeTransferRetryTxFromEscrow:
300-
reason = prependString() + "transfer from escrow in a tx of ArbitrumRetryTx type"
311+
reason += "transfer from escrow in a tx of ArbitrumRetryTx type"
301312
// ArbitrumSubmitRetryableTx
302313
case BalanceChangeTransferRetryableToFeeRefundAddr:
303-
reason = prependString() + "transfer to FeeRefundAddr in a tx of ArbitrumSubmitRetryableTx type"
314+
reason += "transfer to FeeRefundAddr in a tx of ArbitrumSubmitRetryableTx type"
304315
case BalanceChangeTransferRetryableToEscrow:
305-
reason = prependString() + "transfer to escrow in a tx of ArbitrumSubmitRetryableTx type"
316+
reason += "transfer to escrow in a tx of ArbitrumSubmitRetryableTx type"
306317
case BalanceChangeTransferRetryableToInfra:
307-
reason = prependString() + "transfer to infrastructure fee account in a tx of ArbitrumSubmitRetryableTx type"
318+
reason += "transfer to infrastructure fee account in a tx of ArbitrumSubmitRetryableTx type"
308319
case BalanceChangeTransferRetryableFromInfra:
309-
reason = prependString() + "transfer from infrastructure fee account in a tx of ArbitrumSubmitRetryableTx type"
320+
reason += "transfer from infrastructure fee account in a tx of ArbitrumSubmitRetryableTx type"
310321
case BalanceChangeTransferRetryableToNetwork:
311-
reason = prependString() + "transfer to network fee account in a tx of ArbitrumSubmitRetryableTx type"
322+
reason += "transfer to network fee account in a tx of ArbitrumSubmitRetryableTx type"
312323
case BalanceChangeTransferRetryableFromNetwork:
313-
reason = prependString() + "transfer from network fee account in a tx of ArbitrumSubmitRetryableTx type"
324+
reason += "transfer from network fee account in a tx of ArbitrumSubmitRetryableTx type"
314325
// Batchposter
315326
case BalanceChangeTransferBatchposterReward:
316-
reason = prependString() + "transfer from L1PricerFundsPoolAddress as batchPosterReward"
327+
reason += "transfer from L1PricerFundsPoolAddress as batchPosterReward"
317328
case BalanceChangeTransferBatchposterRefund:
318-
reason = prependString() + "transfer from L1PricerFundsPoolAddress as batchPosterRefund"
329+
reason += "transfer from L1PricerFundsPoolAddress as batchPosterRefund"
319330
// Stylus
320331
case BalanceChangeTransferActivationFee:
321-
reason = prependString() + "transfer of activation fee to network fee account"
332+
reason += "transfer of activation fee to network fee account"
322333
case BalanceChangeTransferActivationReimburse:
323-
reason = prependString() + "transfer of reimburse amount after charging the activation fee"
334+
reason += "transfer of reimburse amount after charging the activation fee"
324335
default:
325-
reason = "unspecified"
336+
return "unspecified"
326337
}
327338

328339
return reason

eth/tracers/internal/tracetest/calltrace_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ func TestInternals(t *testing.T) {
309309
byte(vm.CALL),
310310
},
311311
tracer: mkTracer("callTracer", nil),
312-
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),
312+
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),
313313
},
314314
{
315315
name: "Stack depletion in LOG0",
316316
code: []byte{byte(vm.LOG3)},
317317
tracer: mkTracer("callTracer", json.RawMessage(`{ "withLog": true }`)),
318-
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),
318+
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),
319319
},
320320
{
321321
name: "Mem expansion in LOG0",
@@ -328,7 +328,7 @@ func TestInternals(t *testing.T) {
328328
byte(vm.LOG0),
329329
},
330330
tracer: mkTracer("callTracer", json.RawMessage(`{ "withLog": true }`)),
331-
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),
331+
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),
332332
},
333333
{
334334
// Leads to OOM on the prestate tracer

eth/tracers/internal/tracetest/testdata/call_tracer/blob_tx.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@
5858
"result": {
5959
"beforeEVMTransfers": [
6060
{
61-
"purpose": "feePayment",
61+
"purpose": "purchase of gas for execution of a transaction",
6262
"from": "0x0c2c51a0990AeE1d73C1228de158688341557508",
6363
"to": null,
6464
"value": "0x2de364958"
6565
}
6666
],
6767
"afterEVMTransfers": [
6868
{
69-
"purpose": "gasRefund",
69+
"purpose": "refund for unused gas at the end of execution",
7070
"from": null,
7171
"to": "0x0c2c51a0990AeE1d73C1228de158688341557508",
7272
"value": "0x0"
7373
},
7474
{
75-
"purpose": "tip",
75+
"purpose": "payment of transaction tip",
7676
"from": null,
7777
"to": "0x0000000000000000000000000000000000000000",
7878
"value": "0x200b20"
@@ -83,13 +83,13 @@
8383
"addr": "0x0c2c51a0990AeE1d73C1228de158688341557508",
8484
"prev": "0xde0b6b3a7640000",
8585
"new": "0xde0b6b0c92db6a8",
86-
"reason": "unspecified"
86+
"reason": "balance decrease due to purchase of gas for execution of a transaction"
8787
},
8888
{
8989
"addr": "0x0000000000000000000000000000000000000000",
9090
"prev": "0x272e0528",
9191
"new": "0x274e1048",
92-
"reason": "unspecified"
92+
"reason": "balance increase due to payment of transaction tip"
9393
}
9494
],
9595
"from": "0x0c2c51a0990aee1d73c1228de158688341557508",

eth/tracers/internal/tracetest/testdata/call_tracer/create.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@
4848
"result": {
4949
"beforeEVMTransfers": [
5050
{
51-
"purpose": "feePayment",
51+
"purpose": "purchase of gas for execution of a transaction",
5252
"from": "0x13e4ACeFE6a6700604929946E70E6443E4E73447",
5353
"to": null,
5454
"value": "0x2a0383e2a65c00"
5555
}
5656
],
5757
"afterEVMTransfers": [
5858
{
59-
"purpose": "gasRefund",
59+
"purpose": "refund for unused gas at the end of execution",
6060
"from": null,
6161
"to": "0x13e4ACeFE6a6700604929946E70E6443E4E73447",
6262
"value": "0x0"
6363
},
6464
{
65-
"purpose": "tip",
65+
"purpose": "payment of transaction tip",
6666
"from": null,
6767
"to": "0xD049bfd667cB46Aa3Ef5Df0dA3e57DB3Be39E511",
6868
"value": "0x2a0383e2a65c00"
@@ -73,13 +73,13 @@
7373
"addr": "0x13e4ACeFE6a6700604929946E70E6443E4E73447",
7474
"prev": "0xcf3e0938579f000",
7575
"new": "0xcc9dd0fa2d39400",
76-
"reason": "unspecified"
76+
"reason": "balance decrease due to purchase of gas for execution of a transaction"
7777
},
7878
{
7979
"addr": "0xD049bfd667cB46Aa3Ef5Df0dA3e57DB3Be39E511",
8080
"prev": "0x0",
8181
"new": "0x2a0383e2a65c00",
82-
"reason": "unspecified"
82+
"reason": "balance increase due to payment of transaction tip"
8383
}
8484
],
8585
"from": "0x13e4acefe6a6700604929946e70e6443e4e73447",

eth/tracers/internal/tracetest/testdata/call_tracer/deep_calls.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,21 @@
111111
"result": {
112112
"beforeEVMTransfers": [
113113
{
114-
"purpose": "feePayment",
114+
"purpose": "purchase of gas for execution of a transaction",
115115
"from": "0x70C9217D814985faEF62B124420F8dFbdDD96433",
116116
"to": null,
117117
"value": "0x11c37937e08000"
118118
}
119119
],
120120
"afterEVMTransfers": [
121121
{
122-
"purpose": "gasRefund",
122+
"purpose": "refund for unused gas at the end of execution",
123123
"from": null,
124124
"to": "0x70C9217D814985faEF62B124420F8dFbdDD96433",
125125
"value": "0xac27a3b12e800"
126126
},
127127
{
128-
"purpose": "tip",
128+
"purpose": "payment of transaction tip",
129129
"from": null,
130130
"to": "0x1977C248e1014Cc103929Dd7f154199C916E39Ec",
131131
"value": "0x700fefccd9800"
@@ -136,19 +136,19 @@
136136
"addr": "0x70C9217D814985faEF62B124420F8dFbdDD96433",
137137
"prev": "0x4ef436dcbda6cd4a",
138138
"new": "0x4ee2736385c64d4a",
139-
"reason": "unspecified"
139+
"reason": "balance decrease due to purchase of gas for execution of a transaction"
140140
},
141141
{
142142
"addr": "0x70C9217D814985faEF62B124420F8dFbdDD96433",
143143
"prev": "0x4ee2736385c64d4a",
144144
"new": "0x4eed35ddc0d9354a",
145-
"reason": "unspecified"
145+
"reason": "balance increase due to refund for unused gas at the end of execution"
146146
},
147147
{
148148
"addr": "0x1977C248e1014Cc103929Dd7f154199C916E39Ec",
149149
"prev": "0x0",
150150
"new": "0x700fefccd9800",
151-
"reason": "unspecified"
151+
"reason": "balance increase due to payment of transaction tip"
152152
}
153153
],
154154
"calls": [

0 commit comments

Comments
 (0)