From 0f8c9449b8f6b2c0a74e9a99114745fb911db9ed Mon Sep 17 00:00:00 2001 From: blindchaser Date: Thu, 21 Nov 2024 13:03:40 -0500 Subject: [PATCH] unit test --- evmrpc/tx_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/evmrpc/tx_test.go b/evmrpc/tx_test.go index b41c07f2c..e2d1896bf 100644 --- a/evmrpc/tx_test.go +++ b/evmrpc/tx_test.go @@ -243,3 +243,49 @@ func TestGetVMError(t *testing.T) { resObj = sendRequestGood(t, "getVMError", "0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872f") require.Equal(t, "not found", resObj["error"].(map[string]interface{})["message"]) } + +func TestGetTransactionReceiptFailedTx(t *testing.T) { + fromAddr := "0x5b4eba929f3811980f5ae0c5d04fa200f837df4e" // Use the actual address from the block + + // Create a failed receipt with 0 gas used for a contract creation transaction + failedReceipt := &types.Receipt{ + Status: 0, // failed status + GasUsed: 0, + BlockNumber: 8, + TransactionIndex: 0, + TxHashHex: "0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e", + From: fromAddr, // Use the actual from address + } + + // Mock the receipt in the keeper + txHash := common.HexToHash("0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e") + EVMKeeper.MockReceipt(Ctx, txHash, failedReceipt) + + // Create JSON-RPC request + body := "{\"jsonrpc\": \"2.0\",\"method\": \"eth_getTransactionReceipt\",\"params\":[\"0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e\"],\"id\":\"test\"}" + + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s:%d", TestAddr, TestPort), strings.NewReader(body)) + require.Nil(t, err) + req.Header.Set("Content-Type", "application/json") + + res, err := http.DefaultClient.Do(req) + require.Nil(t, err) + + resBody, err := io.ReadAll(res.Body) + require.Nil(t, err) + + resObj := map[string]interface{}{} + require.Nil(t, json.Unmarshal(resBody, &resObj)) + resObj = resObj["result"].(map[string]interface{}) + + // Verify the receipt was filled with correct information for failed tx + require.Equal(t, "0x0", resObj["status"].(string)) // Failed status + require.Equal(t, "0x0", resObj["gasUsed"].(string)) // 0 gas used + require.Equal(t, "0x8", resObj["blockNumber"].(string)) + require.Equal(t, "0xf02362077ac075a397344172496b28e913ce5294879d811bb0269b3be20a872e", resObj["transactionHash"].(string)) + require.Equal(t, fromAddr, resObj["from"].(string)) + + // For contract creation transaction + require.Equal(t, "0x0000000000000000000000000000000000010203", resObj["to"].(string)) + require.Nil(t, resObj["contractAddress"]) +}