Skip to content

Transaction trace #6706

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

Closed
zees-dev opened this issue Nov 9, 2024 · 0 comments · Fixed by #7196
Closed

Transaction trace #6706

zees-dev opened this issue Nov 9, 2024 · 0 comments · Fixed by #7196
Assignees
Labels
enhancement New feature or request

Comments

@zees-dev
Copy link
Contributor

zees-dev commented Nov 9, 2024

Related Component

forc

Problem you are trying to solve

The sway tooling needs to support transaction traces such that one can analyze the pathways of code executed within a transaction.

For example; when using the foundry tooling one can do this:

# > cast send --private-key $RAW_PRIVATE_KEY 0x0000B3965781EEF2BdB59A9dB31AB854B44A4Bff --value 31ether                                            

blockHash               0xca7e0a17b131d10e24f7b87759f1b3534425987f7beae14afa8727341f781d24
blockNumber             1
contractAddress         
cumulativeGasUsed       21000
effectiveGasPrice       1000000001
from                    0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
gasUsed                 21000
logs                    []
logsBloom               0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
root                    0x4c86387576fcdd67cdda3b8339bc0c37006346c79b03a8d818d386053fc06b85
status                  1 (success)
transactionHash         0x80bf15b8150af8572ba4d62bfa8afe499ffba7fc9110439eb5510113846515bf
transactionIndex        0
type                    2
blobGasPrice            1
blobGasUsed             
authorizationList       
to                      0x0000B3965781EEF2BdB59A9dB31AB854B44A4Bff
# > cast run 0x80bf15b8150af8572ba4d62bfa8afe499ffba7fc9110439eb5510113846515bf 

Executing previous transactions from the block.
Traces:
  [0] 0x0000B3965781EEF2BdB59A9dB31AB854B44A4Bff::fallback{value: 31000000000000000000}()
    └─ ← [Stop] 


Transaction successfully executed.
Gas used: 21000

In the example above a simple ether transfer transaction is executed using the cast tooling; one can get a call trace of the transaction using cast run command.
It would be great to have an equivalent, or atleast some way to dive deeper into the call trace of a transaction on the fuelvm.

Solution you'd like

A forc tooling equivalent to obtain a call trace. Will most likely need to be a sub-command or a new CLI tool equivalent to cast.

Notes

No response

@zees-dev zees-dev added enhancement New feature or request triage This issue was opened with a template and needs to be triaged by code owners. labels Nov 9, 2024
@zees-dev zees-dev added forc and removed forc labels Dec 8, 2024
@JoshuaBatty JoshuaBatty added the Migrated label Jan 29, 2025 — with Linear
@JoshuaBatty JoshuaBatty removed triage This issue was opened with a template and needs to be triaged by code owners. Migrated labels Jan 29, 2025
@zees-dev zees-dev mentioned this issue May 23, 2025
8 tasks
zees-dev added a commit that referenced this issue May 30, 2025
## Description

Resolves #6706

Introduces `forc-call` transaction tracing - for verbosity levels `3+`
(`-v=3` or `-vvv`).
The tracing primarily utilizes transaction receipts, generating a call
tree structure, then rendering this tree to `stdout`.

Example usage output:
```sh
> forc-call --abi ./demo/demo-caller/out/release/demo-caller-abi.json 9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965 call_increment_count -vvv
```

<img width="629" alt="image"
src="https://github.com/user-attachments/assets/6ad73c2a-a08e-4829-8d46-0b951e9b723b"
/>

<details>
<summary>Output receipts for the trace above</summary>
```json
{
  "receipts": [
    {
      "Call": {
        "id": "0000000000000000000000000000000000000000000000000000000000000000",
        "to": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "amount": 0,
        "asset_id": "f8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07",
        "gas": 124116,
        "param1": 10480,
        "param2": 10508,
        "pc": 11928,
        "is": 11928
      }
    },
    {
      "Call": {
        "id": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "to": "b792b1e233a2c06bccec611711acc3bb61bdcb28f16abdde86d1478ee02f6e42",
        "amount": 0,
        "asset_id": "0000000000000000000000000000000000000000000000000000000000000000",
        "gas": 111500,
        "param1": 67107840,
        "param2": 67106816,
        "pc": 19064,
        "is": 19064
      }
    },
    {
      "ReturnData": {
        "id": "b792b1e233a2c06bccec611711acc3bb61bdcb28f16abdde86d1478ee02f6e42",
        "ptr": 0,
        "len": 0,
        "digest": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
        "pc": 21004,
        "is": 19064,
        "data": []
      }
    },
    {
      "LogData": {
        "id": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "ra": 0,
        "rb": 10098701174489624218,
        "ptr": 67104256,
        "len": 25,
        "digest": "392232ec5cf9a0ef3c155ad19684907344847572e913a7a374d703fb9c9d8b5d",
        "pc": 15804,
        "is": 11928,
        "data": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          17,
          105,
          110,
          99,
          114,
          101,
          109,
          101,
          110,
          116,
          101,
          100,
          32,
          99,
          111,
          117,
          110,
          116
        ]
      }
    },
    {
      "Call": {
        "id": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "to": "b792b1e233a2c06bccec611711acc3bb61bdcb28f16abdde86d1478ee02f6e42",
        "amount": 0,
        "asset_id": "0000000000000000000000000000000000000000000000000000000000000000",
        "gas": 86284,
        "param1": 67103232,
        "param2": 67102208,
        "pc": 19072,
        "is": 19072
      }
    },
    {
      "ReturnData": {
        "id": "b792b1e233a2c06bccec611711acc3bb61bdcb28f16abdde86d1478ee02f6e42",
        "ptr": 67099904,
        "len": 8,
        "digest": "cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70",
        "pc": 21352,
        "is": 19072,
        "data": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          2
        ]
      }
    },
    {
      "LogData": {
        "id": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "ra": 0,
        "rb": 1515152261580153489,
        "ptr": 67098880,
        "len": 8,
        "digest": "cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70",
        "pc": 15976,
        "is": 11928,
        "data": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          2
        ]
      }
    },
    {
      "LogData": {
        "id": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "ra": 0,
        "rb": 10098701174489624218,
        "ptr": 67097856,
        "len": 12,
        "digest": "a3d2743e2a3ab241ba31ffc7133a43daabe6a8e624c7edc92410068a3896c871",
        "pc": 16072,
        "is": 11928,
        "data": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          4,
          100,
          111,
          110,
          101
        ]
      }
    },
    {
      "Call": {
        "id": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "to": "b792b1e233a2c06bccec611711acc3bb61bdcb28f16abdde86d1478ee02f6e42",
        "amount": 0,
        "asset_id": "0000000000000000000000000000000000000000000000000000000000000000",
        "gas": 72699,
        "param1": 67096832,
        "param2": 67095808,
        "pc": 19064,
        "is": 19064
      }
    },
    {
      "ReturnData": {
        "id": "b792b1e233a2c06bccec611711acc3bb61bdcb28f16abdde86d1478ee02f6e42",
        "ptr": 0,
        "len": 0,
        "digest": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
        "pc": 21004,
        "is": 19064,
        "data": []
      }
    },
    {
      "Call": {
        "id": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "to": "b792b1e233a2c06bccec611711acc3bb61bdcb28f16abdde86d1478ee02f6e42",
        "amount": 0,
        "asset_id": "0000000000000000000000000000000000000000000000000000000000000000",
        "gas": 48287,
        "param1": 67093248,
        "param2": 67092224,
        "pc": 19072,
        "is": 19072
      }
    },
    {
      "ReturnData": {
        "id": "b792b1e233a2c06bccec611711acc3bb61bdcb28f16abdde86d1478ee02f6e42",
        "ptr": 67089920,
        "len": 8,
        "digest": "d5688a52d55a02ec4aea5ec1eadfffe1c9e0ee6a4ddbe2377f98326d42dfc975",
        "pc": 21352,
        "is": 19072,
        "data": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          3
        ]
      }
    },
    {
      "ReturnData": {
        "id": "9275a76531bce733cfafdbcb6727ea533ebbdc358d685152169b3c4eaa47b965",
        "ptr": 67088896,
        "len": 8,
        "digest": "d5688a52d55a02ec4aea5ec1eadfffe1c9e0ee6a4ddbe2377f98326d42dfc975",
        "pc": 12608,
        "is": 11928,
        "data": [
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          3
        ]
      }
    },
    {
      "Return": {
        "id": "0000000000000000000000000000000000000000000000000000000000000000",
        "val": 1,
        "pc": 10388,
        "is": 10368
      }
    },
    {
      "ScriptResult": {
        "result": "Success",
        "gas_used": 89279
      }
    }
  ]
}
```
</details>

---

The tracing is inspired by transaction trace provided by `cast run
<tx-hash>` tool from foundry.

Example usage/output of `cast run`:

<img width="711" alt="image"
src="https://github.com/user-attachments/assets/ad403c45-8944-4e26-95c3-a6804ef62fe3"
/>

---

## Copilot Description

This pull request introduces changes to the `forc-call` CLI, focusing
on:
- improving ABI handling
- error reporting
- transaction processing
It refactors the `call_function` and `transfer` operations, introduces
new structures for ABI management, and enhances the verbosity options
for debugging transaction outputs.

### Enhancements to ABI Handling:
* Introduced a new `Abi` struct to encapsulate `ProgramABI`,
`UnifiedProgramABI`, and a type lookup map, simplifying ABI parsing and
usage (`forc-plugins/forc-client/src/op/call/mod.rs`).
* Updated `call_function` to use the new `Abi` struct and refactored ABI
parsing to improve error handling during contract function calls
(`forc-plugins/forc-client/src/op/call/call_function.rs`)
[[1]](diffhunk://#diff-7da99987eeed69f070b781a5692d3acc7028308b8538ca129d79452ccc057fdeL49-R62)
[[2]](diffhunk://#diff-7da99987eeed69f070b781a5692d3acc7028308b8538ca129d79452ccc057fdeL213-R245).

### Improvements in Transaction Processing:
* Enhanced the `process_transaction_output` function to include
`CallData` for better handling of ABI and contract-related data. Also,
added support for printing transaction traces and logs with higher
verbosity levels (`forc-plugins/forc-client/src/op/call/mod.rs`).
* Refactored receipt handling in `call_function` to include detailed
error reporting and optional ABI mappings for debugging
(`forc-plugins/forc-client/src/op/call/call_function.rs`).

### Code Simplification and Refactoring:
* Removed redundant `ProgramABI` usage in the `transfer` function and
updated it to use the new transaction processing logic
(`forc-plugins/forc-client/src/op/call/transfer.rs`).
* Consolidated imports and removed unused dependencies across multiple
files to streamline the codebase
(`forc-plugins/forc-client/src/op/call/call_function.rs`,
`forc-plugins/forc-client/src/op/call/mod.rs`,
`forc-plugins/forc-client/src/op/call/transfer.rs`)
[[1]](diffhunk://#diff-7da99987eeed69f070b781a5692d3acc7028308b8538ca129d79452ccc057fdeL6-R18)
[[2]](diffhunk://#diff-7da99987eeed69f070b781a5692d3acc7028308b8538ca129d79452ccc057fdeL32-R33)
[[3]](diffhunk://#diff-6b8144a652cd418525566ff9da96c87495dd239934e78788f2f79422657e5960L2-L8).

### Enhanced Debugging and Verbosity:
* Added a new `print_receipts_and_trace` function to format and display
transaction receipts and traces based on verbosity levels, aiding in
debugging complex transactions
(`forc-plugins/forc-client/src/op/call/mod.rs`).
* Renamed `script` to `script_json` in `CallResponse` for clarity and
consistency with JSON output conventions
(`forc-plugins/forc-client/src/op/call/mod.rs`).

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: z <[email protected]>
Co-authored-by: Joshua Batty <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants