Skip to content

Commit

Permalink
Cherry pick of commit de63e75
Browse files Browse the repository at this point in the history
bug fix: eth_getLogs missing events from early return
#1941
  • Loading branch information
jewei1997 committed Nov 17, 2024
1 parent 3872ee3 commit a527bd3
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 45 deletions.
38 changes: 19 additions & 19 deletions app/test_state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,25 +264,25 @@ func (s *InMemoryStateStore) RawImport(ch <-chan types.RawSnapshotNode) error {
return nil
}

func (s *InMemoryStateStore) SetLatestMigratedModule(module string) error {
// TODO: Add set call here
return nil
}

func (s *InMemoryStateStore) GetLatestMigratedModule() (string, error) {
// TODO: Add get call here
return "", nil
}

func (s *InMemoryStateStore) SetLatestMigratedKey(key []byte) error {
// TODO: Add set call here
return nil
}

func (s *InMemoryStateStore) GetLatestMigratedKey() ([]byte, error) {
// TODO: Add get call here
return nil, nil
}
// func (s *InMemoryStateStore) SetLatestMigratedModule(module string) error {
// // TODO: Add set call here
// return nil
// }

// func (s *InMemoryStateStore) GetLatestMigratedModule() (string, error) {
// // TODO: Add get call here
// return "", nil
// }

// func (s *InMemoryStateStore) SetLatestMigratedKey(key []byte) error {
// // TODO: Add set call here
// return nil
// }

// func (s *InMemoryStateStore) GetLatestMigratedKey() ([]byte, error) {
// // TODO: Add get call here
// return nil, nil
// }

func (s *InMemoryStateStore) Prune(version int64) error {
s.mu.Lock()
Expand Down
8 changes: 6 additions & 2 deletions evmrpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ 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, 5, len(result))
receipt1 := result[0].(map[string]interface{})
require.Equal(t, "0x2", receipt1["blockNumber"])
require.Equal(t, "0x0", receipt1["transactionIndex"])
Expand All @@ -94,10 +94,14 @@ func TestGetBlockReceipts(t *testing.T) {
require.Equal(t, "0x2", receipt3["transactionIndex"])
require.Equal(t, multiTxBlockTx3.Hash().Hex(), receipt3["transactionHash"])

resObjSei := sendSeiRequestGood(t, "getBlockReceipts", "0x2")
result = resObjSei["result"].([]interface{})
require.Equal(t, 6, len(result))

// Query by block hash
resObj2 := sendRequestGood(t, "getBlockReceipts", "0x0000000000000000000000000000000000000000000000000000000000000002")
result = resObj2["result"].([]interface{})
require.Equal(t, 3, len(result))
require.Equal(t, 5, len(result))
receipt1 = result[0].(map[string]interface{})
require.Equal(t, "0x2", receipt1["blockNumber"])
require.Equal(t, "0x0", receipt1["transactionIndex"])
Expand Down
6 changes: 1 addition & 5 deletions evmrpc/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,9 @@ func (f *LogFetcher) FindLogsByBloom(height int64, filters [][]bloomIndexes) (re
ctx.Logger().Error(fmt.Sprintf("FindLogsByBloom: unable to find receipt for hash %s", hash.Hex()))
continue
}
// if includeShellReceipts is false, include receipts with synthetic logs but exclude shell tx receipts
if !f.includeSyntheticReceipts && receipt.TxType == ShellEVMTxType {
if !f.includeSyntheticReceipts && (receipt.TxType == ShellEVMTxType || receipt.EffectiveGasPrice == 0) {
continue
}
if !f.includeSyntheticReceipts && receipt.EffectiveGasPrice == 0 {
return
}
if len(receipt.LogsBloom) > 0 && MatchFilters(ethtypes.Bloom(receipt.LogsBloom), filters) {
res = append(res, keeper.GetLogsForTx(receipt)...)
}
Expand Down
33 changes: 26 additions & 7 deletions evmrpc/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ func getCommonFilterLogTests() []GetFilterLogTests {
wantLen: 4,
},
{
name: "filter by single topic with default range",
topics: [][]common.Hash{{common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000123")}},
wantErr: false,
name: "filter by single topic with block range",
fromBlock: "0x8",
toBlock: "0x8",
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))
},
Expand Down Expand Up @@ -192,7 +194,7 @@ func TestFilterGetLogs(t *testing.T) {
testFilterGetLogs(t, "eth", getCommonFilterLogTests())
}

func TestSeiFilterGetLogs(t *testing.T) {
func TestFilterSeiGetLogs(t *testing.T) {
// make sure we pass all the eth_ namespace tests
testFilterGetLogs(t, "sei", getCommonFilterLogTests())

Expand Down Expand Up @@ -221,10 +223,10 @@ func TestSeiFilterGetLogs(t *testing.T) {
})
}

func TestEthEndpointCanReturnSyntheticLogs(t *testing.T) {
func TestFilterEthEndpointCanReturnSyntheticLogs(t *testing.T) {
testFilterGetLogs(t, "eth", []GetFilterLogTests{
{
name: "filter by single topic with default range, exclude synethetic logs",
name: "filter by single topic with default range, still include synthetic logs",
topics: [][]common.Hash{{common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000234")}},
wantErr: false,
check: func(t *testing.T, log map[string]interface{}) {
Expand All @@ -235,6 +237,23 @@ func TestEthEndpointCanReturnSyntheticLogs(t *testing.T) {
})
}

func TestFilterEthEndpointReturnsNormalEvmLogEvenIfSyntheticLogIsInSameBlock(t *testing.T) {
testFilterGetLogs(t, "eth", []GetFilterLogTests{
{
name: "normal evm log is returned even if synthetic log is in the same block",
fromBlock: "0x64", // 100
toBlock: "0x64",
wantErr: false,
check: func(t *testing.T, log map[string]interface{}) {
// check that none of the events have the synthetic hash
syntheticHash := multiTxBlockSynthTx.Hash()
require.NotEqual(t, syntheticHash.Hex(), log["transactionHash"].(string))
},
wantLen: 2,
},
})
}

func testFilterGetLogs(t *testing.T, namespace string, tests []GetFilterLogTests) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -283,7 +302,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, 6, len(logs))
for _, log := range logs {
logObj := log.(map[string]interface{})
require.Equal(t, "0x2", logObj["blockNumber"].(string))
Expand Down
74 changes: 62 additions & 12 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 @@ -49,6 +50,7 @@ const TestBadPort = 7779
const MockHeight = 8
const MultiTxBlockHeight = 2
const DebugTraceMockHeight = 101
const SyntheticBlockHeight = 100

var DebugTraceHashHex = "0x1234567890123456789023456789012345678901234567890123456789000004"
var DebugTraceBlockHash = "BE17E0261E539CB7E9A91E123A6D794E0163D656FCF9B8EAC07823F7ED28512B"
Expand Down Expand Up @@ -76,6 +78,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 @@ -165,6 +168,26 @@ func (c *MockClient) mockBlock(height int64) *coretypes.ResultBlock {
},
}
}
if height == SyntheticBlockHeight {
return &coretypes.ResultBlock{
BlockID: MockBlockID,
Block: &tmtypes.Block{
Header: mockBlockHeader(height),
Data: tmtypes.Data{
Txs: []tmtypes.Tx{
func() []byte {
bz, _ := Encoder(MultiTxBlockSynthTx)
return bz
}(),
func() []byte {
bz, _ := Encoder(MultiTxBlockTx1)
return bz
}(),
},
},
},
}
}
res := &coretypes.ResultBlock{
BlockID: MockBlockID,
Block: &tmtypes.Block{
Expand Down Expand Up @@ -568,7 +591,7 @@ func generateTxData() {
From: "0x1234567890123456789012345678901234567890",
To: "0x1234567890123456789012345678901234567890",
TransactionIndex: 0,
BlockNumber: 8,
BlockNumber: MockHeight,
TxType: 1,
ContractAddress: "0x1234567890123456789012345678901234567890",
CumulativeGasUsed: 123,
Expand Down Expand Up @@ -670,7 +693,8 @@ func setupLogs() {
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000123"),
},
}}}})
EVMKeeper.MockReceipt(Ctx, multiTxBlockTx1.Hash(), &types.Receipt{
CtxMultiTx := Ctx.WithBlockHeight(MultiTxBlockHeight)
EVMKeeper.MockReceipt(CtxMultiTx, multiTxBlockTx1.Hash(), &types.Receipt{
BlockNumber: MultiTxBlockHeight,
TransactionIndex: 1, // start at 1 bc 0 is the non-evm tx
TxHashHex: multiTxBlockTx1.Hash().Hex(),
Expand All @@ -691,7 +715,7 @@ func setupLogs() {
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000456"),
},
}}}})
EVMKeeper.MockReceipt(Ctx, multiTxBlockTx2.Hash(), &types.Receipt{
EVMKeeper.MockReceipt(CtxMultiTx, multiTxBlockTx2.Hash(), &types.Receipt{
BlockNumber: MultiTxBlockHeight,
TransactionIndex: 3,
TxHashHex: multiTxBlockTx2.Hash().Hex(),
Expand All @@ -709,7 +733,7 @@ func setupLogs() {
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000456"),
},
}}}})
EVMKeeper.MockReceipt(Ctx, multiTxBlockTx3.Hash(), &types.Receipt{
EVMKeeper.MockReceipt(CtxMultiTx, multiTxBlockTx3.Hash(), &types.Receipt{
BlockNumber: MultiTxBlockHeight,
TransactionIndex: 4,
TxHashHex: multiTxBlockTx3.Hash().Hex(),
Expand All @@ -727,9 +751,10 @@ func setupLogs() {
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000456"),
},
}}}})
EVMKeeper.MockReceipt(Ctx, multiTxBlockTx4.Hash(), &types.Receipt{
CtxMock := Ctx.WithBlockHeight(MockHeight)
EVMKeeper.MockReceipt(CtxMock, multiTxBlockTx4.Hash(), &types.Receipt{
BlockNumber: MockHeight,
TransactionIndex: 0,
TransactionIndex: 1,
TxHashHex: multiTxBlockTx4.Hash().Hex(),
LogsBloom: bloom4[:],
Logs: []*types.Log{{
Expand All @@ -746,7 +771,8 @@ func setupLogs() {
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000456"),
},
}}}})
EVMKeeper.MockReceipt(Ctx, multiTxBlockSynthTx.Hash(), &types.Receipt{
EVMKeeper.MockReceipt(CtxMock, multiTxBlockSynthTx.Hash(), &types.Receipt{
TxType: evmrpc.ShellEVMTxType,
BlockNumber: MockHeight,
TransactionIndex: 0,
TxHashHex: multiTxBlockSynthTx.Hash().Hex(),
Expand All @@ -756,24 +782,48 @@ func setupLogs() {
Topics: []string{"0x0000000000000000000000000000000000000000000000000000000000000234", "0x0000000000000000000000000000000000000000000000000000000000000789"},
Synthetic: true,
}},
EffectiveGasPrice: 100,
EffectiveGasPrice: 0,
})
EVMKeeper.MockReceipt(Ctx, common.HexToHash(DebugTraceHashHex), &types.Receipt{
CtxDebugTrace := Ctx.WithBlockHeight(DebugTraceMockHeight)
EVMKeeper.MockReceipt(CtxDebugTrace, common.HexToHash(DebugTraceHashHex), &types.Receipt{
BlockNumber: DebugTraceMockHeight,
TransactionIndex: 0,
TxHashHex: DebugTraceHashHex,
})
txNonEvmBz, _ := Encoder(TxNonEvmWithSyntheticLog)
txNonEvmHash := sha256.Sum256(txNonEvmBz)
EVMKeeper.MockReceipt(CtxMultiTx, 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,
})

// block 8
EVMKeeper.SetTxHashesOnHeight(Ctx, MultiTxBlockHeight, []common.Hash{
multiTxBlockTx1.Hash(),
multiTxBlockTx2.Hash(),
multiTxBlockTx3.Hash(),
})
EVMKeeper.SetBlockBloom(MultiTxCtx, []ethtypes.Bloom{bloom1, bloom2, bloom3})

// block 2
EVMKeeper.SetTxHashesOnHeight(Ctx, MockHeight, []common.Hash{
multiTxBlockTx4.Hash(),
multiTxBlockSynthTx.Hash(),
multiTxBlockTx4.Hash(),
})
EVMKeeper.SetBlockBloom(MultiTxCtx, []ethtypes.Bloom{bloom1, bloom2, bloom3})
EVMKeeper.SetBlockBloom(Ctx, []ethtypes.Bloom{bloom4, bloomSynth})
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(Ctx, []ethtypes.Bloom{bloomSynth, bloom4, bloomTx1})
}

//nolint:deadcode
Expand Down

0 comments on commit a527bd3

Please sign in to comment.