Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
blindchaser committed Nov 21, 2024
1 parent 3630b26 commit 0f8c944
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions evmrpc/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}

0 comments on commit 0f8c944

Please sign in to comment.