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

Relay Call Trace API #30

Merged
merged 7 commits into from
Apr 14, 2023
Merged

Relay Call Trace API #30

merged 7 commits into from
Apr 14, 2023

Conversation

pcw109550
Copy link
Member

@pcw109550 pcw109550 commented Apr 5, 2023

This PR enables prebedrock relay for debug_traceTransaction and ots_traceTransaction related methods.

debug_traceTransaction

Directly relay API response from l2geth. Stream out json response.

ots_traceTransaction related methods

Related methods: ots_traceTransaction, ots_getTransactionError, ots_getInternalOperations are all relayed. This is possible because I relayed runTracer method which is used by all otterscan tracing RPCs.

You may wonder l2geth does not have ots_traceTransaction implemented, and how could relaying be possible.

ots_traceTransaction can be relayed by rearranging response from debug_traceTransaction.
For example, lets examine the following prebedrock tx with hash txhash = 0xd2c3b3187d003fa65dcb991aba552f867d3b3e23ccfb6e87de15ed53ad819945

API response from debug_traceTransaction(txhash):

{
        "jsonrpc": "2.0",
        "method": "debug_traceTransaction",
        "params": [
            "0xd2c3b3187d003fa65dcb991aba552f867d3b3e23ccfb6e87de15ed53ad819945",
             {"tracer": "callTracer"}
        ],
        "id": 2
}

Response:

{
    "jsonrpc": "2.0",
    "result": {
        "calls": [
            {
                "from": "0x8c0fc35fca520134b1af215e80ce16762dc33629",
                "gas": "0x95cdb4",
                "gasUsed": "0x254b",
                "input": "0xa2fb6d6f00000000000000000000000000000000000000000000000000000000000003490000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000c4c6f735f416e67656c6573000000000000000000000000000000000000000000",
                "output": "0x00000000000000000000000000000000000000000000000001c0000000000aed",
                "to": "0xc894aede7c6f4e7c17bfb959b438704dc4339931",
                "type": "CALL",
                "value": "0x0"
            }
        ],
        "from": "0xad0470d15222810ad47f4cd977493ab5fee8258e",
        "gas": "0x98421c",
        "gasUsed": "0x3a50",
        "input": "0x661eec460000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b4c6f735f416e67656c6573000000000000000000000000000000000000000000",
        "output": "0x",
        "time": "23.999465ms",
        "to": "0x8c0fc35fca520134b1af215e80ce16762dc33629",
        "type": "CALL",
        "value": "0x0"
    },
    "id": 2
}

Desired output from ots_traceTransaction(txhash):

{
    "jsonrpc": "2.0",
    "id": 6,
    "result": [
        {
            "type": "CALL",
            "depth": 0,
            "from": "0xad0470d15222810ad47f4cd977493ab5fee8258e",
            "to": "0x8c0fc35fca520134b1af215e80ce16762dc33629",
            "value": "0x0",
            "input": "0x661eec460000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b4c6f735f416e67656c6573000000000000000000000000000000000000000000"
        },
        {
            "type": "CALL",
            "depth": 1,
            "from": "0x8c0fc35fca520134b1af215e80ce16762dc33629",
            "to": "0xc894aede7c6f4e7c17bfb959b438704dc4339931",
            "value": "0x0",
            "input": "0xa2fb6d6f00000000000000000000000000000000000000000000000000000000000003490000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000c4c6f735f416e67656c6573000000000000000000000000000000000000000000"
        }
    ]
}

Therefore to generate response of ots_traceTransaction, we must do iterative postorder traversal by parsing debug_traceTransaction result. I chose iterating not recursing to avoid stack memory consumption.

So,

  1. Get trace result using debug_traceTransaction(txhash)
  2. Flatten nested response in iterative postorder traversal manner.
  3. While iterating, call methods for vm.EVMLogger for stacking up call traces. This automatically adds depth field to each call frames from l2geth.

@pcw109550 pcw109550 requested review from ImTei and EdEd1234 April 5, 2023 15:51
@pcw109550
Copy link
Member Author

pcw109550 commented Apr 7, 2023

Tested using temp pytest: https://github.com/testinprod-io/erigon/tree/op-erigon-diff-checker 's commit: testinprod-io@1ddc66a

debug_traceTransaction: tested by comparing response of op-erigon and op-geth for bedrock and prebedrock
ots_traceTransaction: tested by comparing response of op-erigon which is set to be always relayed(temporarily made to make every requests relayed) and not, for bedrock. Only comparing in bedrock era is enough because we already tested debug_traceTransaction's output and response of ots_traceTransaction is generated using debug_traceTransaction's output when relayed.

Minor issue: for debug_traceTransaction, erigon sometimes ignore value key when its value is 0x0, comparing with geth. This diff is not introduced in this PR. By comparing relayed result of erigon and non-relayed result of erigon, they are equal.

Test all pass by ignoring value key when its value is 0x0.

if err := api.translateCaptureExit(top.gethTrace, tracer); err != nil {
return err
}
for len(callStacks) > 0 && top.idx == len(callStacks[len(callStacks)-1].gethTrace.Calls)-1 {
Copy link
Member

Choose a reason for hiding this comment

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

It's a little bit confusing to understand the meaning and purpose of idx. Please add some comments to explain the defenition of idx, managing rootindex, and the condition on L259.

@ImTei
Copy link
Member

ImTei commented Apr 10, 2023

Nice PR! Just one small comment, it could be confusing that you mentioned this is a postorder traversal because the result is preorder.

I think you said this is postorder because we had to call translateCaptureExit in postorder, but the translation result is appended in translateCaptureEnter/Start in preorder. So this code has both preorder and postorder traversal.

@pcw109550 pcw109550 requested a review from ImTei April 12, 2023 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants