Transaction Receipt Parsing

Tags: “java-sdk” “Receipt Parsing” “Event Parsing”


A FISCO BCOS transaction is a request data sent to the blockchain system for deploying contracts, invoking contract interfaces, maintaining the life cycle of contracts, managing assets, and exchanging value。When the transaction is confirmed, a transaction receipt will be generated, transaction receiptand transactionsAll are stored in blocks and are used to record information generated during the transaction execution process, such as result codes, events, and the amount of gas consumed。Users can use the transaction hash to query the transaction receipt to determine whether the transaction is complete。

The transaction receipt contains three key fields: input, output, and logs:

Field Type 描述
input String ABI-encoded hexadecimal string of transaction input
output String The ABI-encoded hexadecimal string returned by the transaction
logs List\<Log> The event log list, which stores the event information of the transaction

The transaction parsing function helps users parse transaction receipts into json data。

1. Construct the TransactionDecoderInterface

Create a TransactionDecoderService object。

/ / Initialize the SDK
BcosSDK sdk =  BcosSDK.build(configFile);
/ / Send group
Client client = sdk.getClient("group0");
/ / Obtain the cryptographic interface corresponding to the current group
CryptoSuite cryptoSuite = client.getCryptoSuite();
/ / Construct a TransactionDecoderService instance and enter the key type parameter。and whether the incoming uses scale decoding
TransactionDecoderInterface decoder = new TransactionDecoderService(cryptoSuite, client.isWASM());

TransactionDecoderInterface mainly includes the following features: abi In the java client folder generated by the contract, take HelloWorld.sol as an example, the json string in HelloWorld.abi。

  • public TransactionResponse decodeReceiptWithValues(String abi, String functionName, TransactionReceipt receipt): Parsing transaction receipts with function return values。

  • public TransactionResponse decodeReceiptWithoutValues(String abi, TransactionReceipt transactionReceipt): Parsing transaction receipts without function return values。

  • public Map<String, List<List<Object>>>> decodeEvents(String abi, List<Logs> logs): Parsing transaction events。

  • public TransactionResponse decodeReceiptStatus(TransactionReceipt receipt): Parse the status of the receipt and error information, etc。

  • **public String decodeRevertMessage(String output)**If the receipt error code is a rollback, parse the revert information in the output

Parsing contract function example

Let’s take a simple increasing function as an example to demonstrate how to parse a transaction。The solidity code corresponding to the increment function is as follows:

function incrementUint256(uint256 v) public returns(uint256){
    _uint256V = v + 1 ;
    emit LogIncrement(msg.sender, v);
    return _uint256V;
}

In the above code, first add 1 to the incoming parameter, then record the incremental event (event), and finally return the result。

2. Resolve transactions with return values

The abi file of the incoming contract, the name of the calling function, and the transaction receipt to parse the transaction result。

TransactionResponse transactionResponse = decoder.decodeReceiptWithValues(abi, "incrementUint256", transactionReceipt);

Example of parsing results

In the above function definition, there is a function return value, which also triggers the event call。Our incoming value v is 1. After parsing the TransactionReceipt returned by the transaction execution, the corresponding result is as follows

{
        "id" : 2,
        "jsonrpc" : "2.0",
        "result" :
        {
                "blockNumber" : 10,
                "checksumContractAddress" : "",
                "contractAddress" : "",
                "extraData" : "",
                "from" : "0xc3e90bebf6dd43ef62e96ba622e324266f7dde1e",
                "gasUsed" : "8880",
                "hash" : "0x0d387a50debfe9332ba91080a0c3c7bc4ac023d08b583939048d819c33a5162e",
                "logEntries" :
                [
                        {
                                "address" : "0102e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94",
                                "data" : "0x000000000000000000000000c3e90bebf6dd43ef62e96ba622e324266f7dde1e0000000000000000000000000000000000000000000000000000000000000001",
                                "topics" :
                                [
                                        "0xaca9a02cfe513f3f88c54a860469369849c8fa0a2119a8d1f3f75c67ac0c9547"
                                ]
                        }
                ],
                "message" : "",
                "output" : "0x0000000000000000000000000000000000000000000000000000000000000002",
                "status" : 0,
                "to" : "0x0102e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94",
                "transactionHash" : "0xaa333f8679673d14ff641b388b00cc7a54d80e3dd6051198518a6294075b310a",
                "version" : 0
        }
}

The above parsed message contains the detailed field values of the data structure of the blockchain receipt。In addition, the event and return value of the function are parsed。

Parsed function event(event)and the return value, you can view the ‘events’ or ‘eventResultMap’ and ‘values’ or ‘valuesList’ fields。

{
   ……
  "logEntries": {
    "LogIncrement": [
      [
        "0xc3e90bebf6dd43ef62e96ba622e324266f7dde1e",
        1
      ]
    ]
  }
}

3. Resolve transactions with no return value

In some scenarios, we don’t care about the return value of the transaction, just parse the event triggered in the function(event)and the detailed data structure of the transaction receipt。

The abi file and transaction receipt of the incoming contract, parsing the transaction results。

TransactionResponse transactionResponseWithoutValues = decoder.decodeReceiptWithoutValues(abi, transactionReceipt);

Example of parsing results

Again, the above section calls the incrementUint256 function as an example, we still parse this transaction receipt, but do not parse the function return value, the return result is as follows

{
        "id" : 2,
        "jsonrpc" : "2.0",
        "result" :
        {
                "blockNumber" : 10,
                "checksumContractAddress" : "",
                "contractAddress" : "",
                "extraData" : "",
                "from" : "0xc3e90bebf6dd43ef62e96ba622e324266f7dde1e",
                "gasUsed" : "8880",
                "hash" : "0x0d387a50debfe9332ba91080a0c3c7bc4ac023d08b583939048d819c33a5162e",
                "logEntries" :
                [
                        {
                                "address" : "0102e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94",
                                "data" : "0x000000000000000000000000c3e90bebf6dd43ef62e96ba622e324266f7dde1e0000000000000000000000000000000000000000000000000000000000000001",
                                "topics" :
                                [
                                        "0xaca9a02cfe513f3f88c54a860469369849c8fa0a2119a8d1f3f75c67ac0c9547"
                                ]
                        }
                ],
                "message" : "",
                "output" : "0x0000000000000000000000000000000000000000000000000000000000000002",
                "status" : 0,
                "to" : "0x0102e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94",
                "transactionHash" : "0xaa333f8679673d14ff641b388b00cc7a54d80e3dd6051198518a6294075b310a",
                "version" : 0
        }
}

The result of the above parsed message contains the detailed field values of the data structure of the blockchain receipt and the parsed function event(event)。

Parsed function event(event)to view the ‘events’ or ‘eventResultMap’ field。

{
   ……
  "logEntries": {
    "LogIncrement": [
      [
        "0xc3e90bebf6dd43ef62e96ba622e324266f7dde1e",
        1
      ]
    ]
  }
}

4. Parsing events

Parses only events triggered during function call。The abi file of the incoming contract and the logs of the transaction receipt, parsing the transaction results;Map that returns the event name and event list。

Map<String, List<List<Object>>>> events = decoder.decodeEvents(abi, transactionReceipt.getLogs());

Example of parsing results

Or the above section calls the incrementUint256 function as an example, now the demonstration only parses the event (event), the return result is as follows:

{
  "LogIncrement": [
    [
      "0x7c8000530ae01adb3f8f77e7096b335eef83172f",
      1
    ]
  ]
}

5. Parse the error message of the receipt

Incoming transaction receipt, parse the returned data, and parse it into a TransactionResponse object。

TransactionResponse transactionResponse = decoder.decodeReceiptStatus(transactionReceipt);

Example of parsing results

The corresponding solidity code:

function setBytesMapping(bytes[] bytesArray) public returns (bool) {
    require(bytesArray.length>1, "Bytes array is less than 2");
    _bytesMapping[bytesArray[0]] = bytesArray;
    return true;
}

During the execution of the following function, the transaction execution fails and an error is reported after the require statement is executed。After parsing the TransactionReceipt returned by the transaction execution, the corresponding results are as follows

{
        "id" : 16,
        "jsonrpc" : "2.0",
        "result" :
        {
                "blockNumber" : 12,
                "checksumContractAddress" : "",
                "contractAddress" : "",
                "extraData" : "",
                "from" : "0xa38e104bb4a92a52452b48342c935f68df20c2ce",
                "gasUsed" : "1132",
                "hash" : "0x",
                "logEntries" : [],
                "message" : "",
                "output" : "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4279746573206172726179206973206c657373207468616e2032000000000000",
                "status" : 16,
                "to" : "0x19a6434154de51c7a7406edf312f01527441b561",
                "transactionHash" : "0x8075a4f016dec5147eb60df219529c3d504c5fad2c16691b39cff457692afeb1",
                "version" : 0
        }
}