Debug a transaction via RPC
Introduction
Polyjuice is an Ethereum-compatible layer that runs on top of Godwoken, a layer 2 framework for Nervos CKB. Polyjuice allows you to deploy and run Ethereum smart contracts on Nervos network with minimal changes.
When you send an Ethereum transaction to Godwoken's RPC, it will be transformed into a Godwoken transaction and executed by the EVM backend, a.k.a. Polyjuice. If your transaction fails, you may want to debug it and find out the root cause of the error.
In this document, we will show you how to debug a failed transaction through gw_debug_replay_transaction
RPC. You will need:
- A node running Godwoken with
debug
module enabled - The transaction hash of an Ethereum compatible transaction
- A tool that can send JSON-RPC requests, such as curl or Postman
Steps
Find out the transaction hash of the failed Ethereum transaction. You can use an explorer like gwscan to search for your transaction by its sender address, receiver address, or just transcation hash.
Connect to your node's JSON-RPC endpoint using curl or Postman. For example, if your node is running on localhost:8024, you can use curl as follows:
curl -X POST http://localhost:8024 -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"<method>","params":["<params>"],"id":1
- Use the RPC method
poly_getGwTxHashByEthTxHash
to transform your Ethereum transaction hash into a Godwoken transaction hash. You need to pass the Ethereum transaction hash as the parameter. For example:
curl -X POST https://v1.mainnet.godwoken.io/rpc/ -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"poly_getGwTxHashByEthTxHash","params":["0x86a575206f2ee718aa2614dee59f9fd2c5250016465ea6c2c81cb6443750bb19"],"id":1}'
- The response will contain a JSON object that shows the Godwoken transaction hash in hexadecimal format. You can copy this value for later use.
{"jsonrpc":"2.0","id":1,"result":"0xc1b075433c01b73bb91064bbcbd1cc92f7c6c5a57977569de816f6d88ddd64a1"}
- Use the debug RPC method
gw_debug_replay_transaction
to get the contract logs of the failed transaction. You need to pass the Godwoken transaction hash as the parameter. For example:
curl --location --request POST https://v1.mainnet.godwoken.io/rpc/ --header 'Content-Type: application/json' --data-raw '{"id": 42, "jsonrpc": "2.0", "method": "gw_debug_replay_transaction", "params": ["0xc1b075433c01b73bb91064bbcbd1cc92f7c6c5a57977569de816f6d88ddd64a1"]}'
- The response will contain a JSON object that shows an array of contract logs generated by the execution of the Godwoken transaction.
"END call",
"[handle_message] used_memory(Bytes) => 5696",
"[handle_message] gas left => 0",
"[handle_message] status_code => 3",
"[call] revert with snapshot id => 12",
"inner call failed (transfer/contract call contract) => 3",
"call.res.status_code => 3",
"END call", more logs ...
- You can inspect these logs to find out where and why your transaction failed. For example, you can look for logs that contain error messages or revert reasons from your smart contract code.