Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize BalanceChangeReason in tracing #375

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ganeshvanahalli
Copy link
Contributor

@ganeshvanahalli ganeshvanahalli commented Nov 15, 2024

This PR enables usage of BalanceChangeReason type in tracing, such as in CaptureArbitrumTransfer and also to trace balance changes themself using jsTracer.
We replace the string in purpose field of both beforeEVMTransfers and afterEVMTransfers with the reasoning derived from tracing.BalanceChangeReason, the strings themself are kept the same to maintain backwards compatibility.

We added following balanceChangeReasons-

// Arbitrum specific
const (
	BalanceChangeDuringEVMExecution BalanceChangeReason = 128 + iota
	BalanceIncreaseDeposit
	BalanceDecreaseWithdrawToL1
	BalanceIncreaseL1PosterFee
	BalanceIncreaseInfraFee
	BalanceIncreaseNetworkFee
	BalanceChangeTransferInfraRefund
	BalanceChangeTransferNetworkRefund
	BalanceIncreasePrepaid
	BalanceDecreaseUndoRefund
	BalanceChangeEscrowTransfer
	BalanceChangeTransferBatchposterReward
	BalanceChangeTransferBatchposterRefund
	// Stylus
	BalanceChangeTransferActivationFee
	BalanceChangeTransferActivationReimburse
)

Corresponding nitro PR- OffchainLabs/nitro#2789
Part of NIT-2943

@cla-bot cla-bot bot added the s CLA signed label Nov 15, 2024
eljobe
eljobe previously approved these changes Nov 18, 2024
Copy link
Member

@eljobe eljobe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@tsahee tsahee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One change request, two others are just questions.

core/tracing/hooks.go Outdated Show resolved Hide resolved
@@ -50,6 +50,7 @@ type callFrame struct {
// Arbitrum: we add these here due to the tracer returning the top frame
BeforeEVMTransfers *[]arbitrumTransfer `json:"beforeEVMTransfers,omitempty"`
AfterEVMTransfers *[]arbitrumTransfer `json:"afterEVMTransfers,omitempty"`
BalanceChanges *[]balanceChange `json:"balanceChanges,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the motivation for adding these here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added balanceChanges right after beforeEVMTransfers and afterEVMTransfers to show all the balance changes step-by-step that happened when a tx gets executed, leading to a trace something like

{
    "beforeEVMTransfers": [
        {
            "purpose": "purchase of gas for execution of a transaction",
            "from": "0x26E554a8acF9003b83495c7f45F06edCB803d4e3",
            "to": null,
            "value": "0x0"
        }
    ],
    "afterEVMTransfers": [
        {
            "purpose": "refund for unused gas at the end of execution",
            "from": null,
            "to": "0x26E554a8acF9003b83495c7f45F06edCB803d4e3",
            "value": "0x0"
        },
        {
            "purpose": "fee collection by network fee account",
            "from": null,
            "to": "0x0000000000000000000000000000000000000000",
            "value": "0x6260d337500"
        },
        {
            "purpose": "fee collection for L1 posting",
            "from": null,
            "to": "0xa4B00000000000000000000000000000000000F6",
            "value": "0x68e6df69800"
        }
    ],
    "balanceChanges": [
        {
            "addr": "0x26E554a8acF9003b83495c7f45F06edCB803d4e3",
            "prev": "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7",
            "new": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffc46535f7",
            "reason": "balance decrease due to transfer via a call"
        },
        {
            "addr": "0x0000000000000000000000000000000000000064",
            "prev": "0x0",
            "new": "0x3b9aca00",
            "reason": "balance increase due to transfer via a call"
        },
        {
            "addr": "0x0000000000000000000000000000000000000064",
            "prev": "0x3b9aca00",
            "new": "0x0",
            "reason": "balance decrease due to withdrawal to L1"
        },
        {
            "addr": "0x0000000000000000000000000000000000000000",
            "prev": "0x0",
            "new": "0x6260d337500",
            "reason": "balance increase due to fee collection by network fee account"
        },
        {
            "addr": "0xa4B00000000000000000000000000000000000F6",
            "prev": "0x0",
            "new": "0x68e6df69800",
            "reason": "balance increase due to fee collection for L1 posting"
        }
    ],
    ...
}

Let me know if I got it wrong

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's "wrong", but we should try to only change geth code when there is a strong customer-affecting motivation to do so, and I don't think there is one here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh got it! I've reverted the changes

eth/tracers/native/mux.go Show resolved Hide resolved
)

func (b BalanceChangeReason) String(prev, new *big.Int) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the motivation for the new strings? Are they formatted after anything standard?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is geth itself doesn't use BalanceChangeReason type for anything meaningful (as of 1.14.0) i.e in any of the tracing code, but gives us the freedom to decide how and where we use it.

They aren't formatted after anything standard but gives the reason for the balance change itself, which I thought we could use in tracing, let me know if I got it wrong.

For example geth's current master uses BalanceChangeReason this way https://github.com/ethereum/go-ethereum/blob/6d3d252a5ece0687dcaaf197e0bfc03466161ff1/eth/tracers/live/supply.go#L191-L208

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

earlier in the purpose field of beforeEVMTransfers and afterEVMTransfers we had single word strings like feeCollection, tip etc.. but now we could have a more descriptive reasons with the String() impl like in the above comment

...
...

{
    "beforeEVMTransfers": [
        {
            "purpose": "purchase of gas for execution of a transaction",
            "from": "0x26E554a8acF9003b83495c7f45F06edCB803d4e3",
            "to": null,
            "value": "0x0"
        }
    ],
    "afterEVMTransfers": [
        {
            "purpose": "refund for unused gas at the end of execution",
            "from": null,
            "to": "0x26E554a8acF9003b83495c7f45F06edCB803d4e3",
            "value": "0x0"
        },

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, but I guess someone is already parsing the exiting short srings.
If they are - this change would hurt them, and I rather avoid it.
We could still have a "String" function but have it return the old string to avoid compatibility errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the String() to be backwards compatible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s CLA signed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants