Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Nov 5, 2024
1 parent b6ddbc1 commit 160f1bf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
34 changes: 10 additions & 24 deletions evmrpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,38 @@ func TestGetBlockReceipts(t *testing.T) {
// Query by block height
resObj := sendRequestGood(t, "getBlockReceipts", "0x2")
result := resObj["result"].([]interface{})
require.Equal(t, 3, len(result))
require.Equal(t, 6, len(result))
receipt1 := result[0].(map[string]interface{})
require.Equal(t, "0x2", receipt1["blockNumber"])
require.Equal(t, "0x0", receipt1["transactionIndex"])
require.Equal(t, multiTxBlockTx1.Hash().Hex(), receipt1["transactionHash"])
require.Equal(t, "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", receipt1["transactionHash"])
receipt2 := result[1].(map[string]interface{})
require.Equal(t, "0x2", receipt2["blockNumber"])
require.Equal(t, "0x1", receipt2["transactionIndex"])
require.Equal(t, multiTxBlockTx2.Hash().Hex(), receipt2["transactionHash"])
require.Equal(t, multiTxBlockTx1.Hash().Hex(), receipt2["transactionHash"])
receipt3 := result[2].(map[string]interface{})
require.Equal(t, "0x2", receipt3["blockNumber"])
require.Equal(t, "0x2", receipt3["transactionIndex"])
require.Equal(t, multiTxBlockTx3.Hash().Hex(), receipt3["transactionHash"])
require.Equal(t, "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", receipt3["transactionHash"])

// Query by block hash
resObj2 := sendRequestGood(t, "getBlockReceipts", "0x0000000000000000000000000000000000000000000000000000000000000002")
result = resObj2["result"].([]interface{})
require.Equal(t, 3, len(result))
require.Equal(t, 6, len(result))
receipt1 = result[0].(map[string]interface{})
require.Equal(t, "0x2", receipt1["blockNumber"])
require.Equal(t, "0x0", receipt1["transactionIndex"])
require.Equal(t, multiTxBlockTx1.Hash().Hex(), receipt1["transactionHash"])
require.Equal(t, "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", receipt1["transactionHash"])
receipt2 = result[1].(map[string]interface{})
require.Equal(t, "0x2", receipt2["blockNumber"])
require.Equal(t, "0x1", receipt2["transactionIndex"])
require.Equal(t, multiTxBlockTx2.Hash().Hex(), receipt2["transactionHash"])
require.Equal(t, multiTxBlockTx1.Hash().Hex(), receipt2["transactionHash"])
receipt3 = result[2].(map[string]interface{})
require.Equal(t, "0x2", receipt3["blockNumber"])
require.Equal(t, "0x2", receipt3["transactionIndex"])
require.Equal(t, multiTxBlockTx3.Hash().Hex(), receipt3["transactionHash"])
require.Equal(t, "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", receipt3["transactionHash"])

// Query by tag latest => retrieves block 8
resObj3 := sendRequestGood(t, "getBlockReceipts", "latest")
result = resObj3["result"].([]interface{})
require.Equal(t, 2, len(result))
receipt1 = result[0].(map[string]interface{})
require.Equal(t, "0x8", receipt1["blockNumber"])
require.Equal(t, "0x0", receipt1["transactionIndex"])
require.Equal(t, multiTxBlockTx4.Hash().Hex(), receipt1["transactionHash"])
receiptWithSyntheticLog := result[1].(map[string]interface{})
require.Equal(t, "0x8", receiptWithSyntheticLog["blockNumber"])
logs := receiptWithSyntheticLog["logs"].([]interface{})
firstLog := logs[0].(map[string]interface{})
topics := firstLog["topics"].([]interface{})
syntheticLogFirstTopic := "0x0000000000000000000000000000000000000000000000000000000000000234"
require.Equal(t, syntheticLogFirstTopic, topics[0].(string))
require.Equal(t, tx1.Hash().Hex(), receipt1["transactionHash"])
}

func verifyGenesisBlockResult(t *testing.T, resObj map[string]interface{}) {
Expand Down Expand Up @@ -160,7 +146,7 @@ func verifyBlockResult(t *testing.T, resObj map[string]interface{}) {
require.Equal(t, "0x5", resObj["gasUsed"])
require.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000001", resObj["hash"])
// see setup_tests.go, which have one transaction for block 0x8 (latest)
require.Equal(t, "0x00002000040000000000000000000080000000200000000000000000000000080000000000000000000000000000000000000000000000000800000000000000001000000000000000000000000000000000000000000000000000000000000100000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000200000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000010200000000000000", resObj["logsBloom"])
require.Equal(t, "0x00002000040000000000000000000080000000200000000000002000000000080000000000000000000000000000000000000000000000000800000000000000001000000000000000000000000000000000020000000000000000000000000100000000000000002000000000200000000000000000000000000000000000100000000000000000000000000400000000000000200000000000000000000000000000000000000100000000000000020000200000000000000000002000000000000000000000000000000000000000000000000000000000000000000200000000010000000002000000000000000000000000000000010200000000000000", resObj["logsBloom"])
require.Equal(t, "0x0000000000000000000000000000000000000005", resObj["miner"])
require.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000000", resObj["mixHash"])
require.Equal(t, "0x0000000000000000", resObj["nonce"])
Expand Down
6 changes: 3 additions & 3 deletions evmrpc/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ func getCommonFilterLogTests() []GetFilterLogTests {
},
{
name: "filter by single topic with default range",
topics: [][]common.Hash{{common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000123")}},
topics: [][]common.Hash{{common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")}},
wantErr: false,
check: func(t *testing.T, log map[string]interface{}) {
require.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000123", log["topics"].([]interface{})[0].(string))
require.Equal(t, "0x1111111111111111111111111111111111111111111111111111111111111111", log["topics"].([]interface{})[0].(string))
},
wantLen: 1,
},
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestFilterGetFilterLogs(t *testing.T) {

resObj = sendRequest(t, TestPort, "getFilterLogs", filterId)
logs := resObj["result"].([]interface{})
require.Equal(t, 4, len(logs))
require.Equal(t, 7, len(logs))
for _, log := range logs {
logObj := log.(map[string]interface{})
require.Equal(t, "0x2", logObj["blockNumber"].(string))
Expand Down
36 changes: 32 additions & 4 deletions evmrpc/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evmrpc_test

import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
Expand Down Expand Up @@ -76,6 +77,7 @@ var multiTxBlockSynthTx *ethtypes.Transaction

var DebugTraceTx sdk.Tx
var TxNonEvm sdk.Tx
var TxNonEvmWithSyntheticLog sdk.Tx
var UnconfirmedTx sdk.Tx

var SConfig = evmrpc.SimulateConfig{GasCap: 10000000}
Expand Down Expand Up @@ -176,7 +178,7 @@ func (c *MockClient) mockBlock(height int64) *coretypes.ResultBlock {
return bz
}(),
func() []byte {
bz, _ := Encoder(TxNonEvm)
bz, _ := Encoder(TxNonEvmWithSyntheticLog)
return bz
}(),
},
Expand Down Expand Up @@ -564,6 +566,12 @@ func generateTxData() {
MultiTxBlockSynthTx = synthTxBuilder.GetTx()
DebugTraceTx = debugTraceTxBuilder.GetTx()
TxNonEvm = app.TestTx{}
TxNonEvmWithSyntheticLog = app.TestTx{}
bloomTx1 := ethtypes.CreateBloom(ethtypes.Receipts{&ethtypes.Receipt{Logs: []*ethtypes.Log{{
Address: common.HexToAddress("0x1111111111111111111111111111111111111111"),
Topics: []common.Hash{common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111"),
common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111112")},
}}}})
if err := EVMKeeper.MockReceipt(Ctx, tx1.Hash(), &types.Receipt{
From: "0x1234567890123456789012345678901234567890",
To: "0x1234567890123456789012345678901234567890",
Expand All @@ -580,6 +588,7 @@ func generateTxData() {
Address: "0x1111111111111111111111111111111111111111",
Topics: []string{"0x1111111111111111111111111111111111111111111111111111111111111111", "0x1111111111111111111111111111111111111111111111111111111111111112"},
}},
LogsBloom: bloomTx1[:],
}); err != nil {
panic(err)
}
Expand Down Expand Up @@ -747,8 +756,8 @@ func setupLogs() {
},
}}}})
EVMKeeper.MockReceipt(Ctx, multiTxBlockSynthTx.Hash(), &types.Receipt{
BlockNumber: MockHeight,
TransactionIndex: 0,
BlockNumber: MultiTxBlockHeight,
TransactionIndex: 5,
TxHashHex: multiTxBlockSynthTx.Hash().Hex(),
LogsBloom: bloomSynth[:],
Logs: []*types.Log{{
Expand All @@ -763,6 +772,20 @@ func setupLogs() {
TransactionIndex: 0,
TxHashHex: DebugTraceHashHex,
})
txNonEvmBz, _ := Encoder(TxNonEvmWithSyntheticLog)
txNonEvmHash := sha256.Sum256(txNonEvmBz)
EVMKeeper.MockReceipt(Ctx, txNonEvmHash, &types.Receipt{
BlockNumber: MultiTxBlockHeight,
TransactionIndex: 1,
TxHashHex: common.Hash(txNonEvmHash).Hex(),
LogsBloom: bloomSynth[:],
Logs: []*types.Log{{
Address: "0x1111111111111111111111111111111111111116",
Topics: []string{"0x0000000000000000000000000000000000000000000000000000000000000234", "0x0000000000000000000000000000000000000000000000000000000000000789"},
Synthetic: true,
}},
EffectiveGasPrice: 100,
})
EVMKeeper.SetTxHashesOnHeight(Ctx, MultiTxBlockHeight, []common.Hash{
multiTxBlockTx1.Hash(),
multiTxBlockTx2.Hash(),
Expand All @@ -772,8 +795,13 @@ func setupLogs() {
multiTxBlockTx4.Hash(),
multiTxBlockSynthTx.Hash(),
})
bloomTx1 := ethtypes.CreateBloom(ethtypes.Receipts{&ethtypes.Receipt{Logs: []*ethtypes.Log{{
Address: common.HexToAddress("0x1111111111111111111111111111111111111111"),
Topics: []common.Hash{common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111"),
common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111112")},
}}}})
EVMKeeper.SetBlockBloom(MultiTxCtx, []ethtypes.Bloom{bloom1, bloom2, bloom3})
EVMKeeper.SetBlockBloom(Ctx, []ethtypes.Bloom{bloom4, bloomSynth})
EVMKeeper.SetBlockBloom(Ctx, []ethtypes.Bloom{bloom4, bloomSynth, bloomTx1})
}

//nolint:deadcode
Expand Down
3 changes: 3 additions & 0 deletions evmrpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package evmrpc
import (
"context"
"crypto/ecdsa"
"crypto/sha256"
"encoding/hex"
"fmt"
"math/big"
Expand Down Expand Up @@ -215,6 +216,7 @@ func getEvmTxHashesFromBlock(block *coretypes.ResultBlock, txConfig client.TxCon
continue

Check warning on line 216 in evmrpc/utils.go

View check run for this annotation

Codecov / codecov/patch

evmrpc/utils.go#L215-L216

Added lines #L215 - L216 were not covered by tests
}
if len(sdkTx.GetMsgs()) == 0 {
txHashes = append(txHashes, sha256.Sum256(tx))
continue
}
if evmTx, ok := sdkTx.GetMsgs()[0].(*types.MsgEVMTransaction); ok {
Expand All @@ -224,6 +226,7 @@ func getEvmTxHashesFromBlock(block *coretypes.ResultBlock, txConfig client.TxCon
ethtx, _ := evmTx.AsTransaction()
txHashes = append(txHashes, ethtx.Hash())
}
txHashes = append(txHashes, sha256.Sum256(tx))
}
return txHashes
}

0 comments on commit 160f1bf

Please sign in to comment.