Skip to content

Commit 883f349

Browse files
committed
add a unit test
1 parent 4f63c7b commit 883f349

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed

evmrpc/filter.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func (a *FilterAPI) GetLogs(
216216
ctx context.Context,
217217
crit filters.FilterCriteria,
218218
) (res []*ethtypes.Log, err error) {
219+
fmt.Println("In GetLogs", "crit", crit)
219220
defer recordMetrics(fmt.Sprintf("%s_getLogs", a.namespace), a.connectionType, time.Now(), err == nil)
220221
logs, _, err := a.logFetcher.GetLogsByFilters(ctx, crit, 0)
221222
return logs, err
@@ -311,6 +312,7 @@ func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCr
311312
if !applyOpenEndedLogLimit && f.filterConfig.maxBlock > 0 && end >= (begin+f.filterConfig.maxBlock) {
312313
end = begin + f.filterConfig.maxBlock - 1
313314
}
315+
fmt.Println("In GetLogsByFilters", "begin", begin, "end", end)
314316
// begin should always be <= end block at this point
315317
if begin > end {
316318
return nil, 0, fmt.Errorf("fromBlock %d is after toBlock %d", begin, end)
@@ -372,11 +374,17 @@ func (f *LogFetcher) FindLogsByBloom(height int64, filters [][]bloomIndexes) (re
372374
ctx.Logger().Error(fmt.Sprintf("FindLogsByBloom: unable to find receipt for hash %s", hash.Hex()))
373375
continue
374376
}
377+
fmt.Println("In FindLogsByBloom", "considering hash", hash.Hex())
378+
fmt.Println("In FindLogsByBloom", "includeSyntheticReceipts", f.includeSyntheticReceipts, "receipt.TxType", receipt.TxType, "receipt.EffectiveGasPrice", receipt.EffectiveGasPrice)
375379
if !f.includeSyntheticReceipts && (receipt.TxType == ShellEVMTxType || receipt.EffectiveGasPrice == 0) {
380+
fmt.Println("In FindLogsByBloom", "skipping hash", hash.Hex())
376381
continue
377382
}
383+
fmt.Println("In FindLogsByBloom", "no skipping hash", hash.Hex())
378384
if len(receipt.LogsBloom) > 0 && MatchFilters(ethtypes.Bloom(receipt.LogsBloom), filters) {
379-
res = append(res, keeper.GetLogsForTx(receipt)...)
385+
logs := keeper.GetLogsForTx(receipt)
386+
fmt.Println("Adding", len(logs), "logs")
387+
res = append(res, logs...)
380388
}
381389
}
382390
return

evmrpc/filter_test.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,11 @@ func getCommonFilterLogTests() []GetFilterLogTests {
130130
wantLen: 4,
131131
},
132132
{
133-
name: "filter by single topic with default range",
134-
topics: [][]common.Hash{{common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")}},
135-
wantErr: false,
133+
name: "filter by single topic with block range",
134+
fromBlock: "0x8",
135+
toBlock: "0x8",
136+
topics: [][]common.Hash{{common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")}},
137+
wantErr: false,
136138
check: func(t *testing.T, log map[string]interface{}) {
137139
require.Equal(t, "0x1111111111111111111111111111111111111111111111111111111111111111", log["topics"].([]interface{})[0].(string))
138140
},
@@ -192,7 +194,7 @@ func TestFilterGetLogs(t *testing.T) {
192194
testFilterGetLogs(t, "eth", getCommonFilterLogTests())
193195
}
194196

195-
func TestSeiFilterGetLogs(t *testing.T) {
197+
func TestFilterSeiGetLogs(t *testing.T) {
196198
// make sure we pass all the eth_ namespace tests
197199
testFilterGetLogs(t, "sei", getCommonFilterLogTests())
198200

@@ -221,10 +223,10 @@ func TestSeiFilterGetLogs(t *testing.T) {
221223
})
222224
}
223225

224-
func TestEthEndpointCanReturnSyntheticLogs(t *testing.T) {
226+
func TestFilterEthEndpointCanReturnSyntheticLogs(t *testing.T) {
225227
testFilterGetLogs(t, "eth", []GetFilterLogTests{
226228
{
227-
name: "filter by single topic with default range, exclude synethetic logs",
229+
name: "filter by single topic with default range, still include synthetic logs",
228230
topics: [][]common.Hash{{common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000234")}},
229231
wantErr: false,
230232
check: func(t *testing.T, log map[string]interface{}) {
@@ -235,6 +237,23 @@ func TestEthEndpointCanReturnSyntheticLogs(t *testing.T) {
235237
})
236238
}
237239

240+
func TestFilterEthEndpointReturnsNormalEvmLogEvenIfSyntheticLogIsInSameBlock(t *testing.T) {
241+
testFilterGetLogs(t, "eth", []GetFilterLogTests{
242+
{
243+
name: "normal evm log is returned even if synthetic log is in the same block",
244+
fromBlock: "0x64", // 100
245+
toBlock: "0x64",
246+
wantErr: false,
247+
check: func(t *testing.T, log map[string]interface{}) {
248+
// check that none of the events have the synthetic hash
249+
syntheticHash := multiTxBlockSynthTx.Hash()
250+
require.NotEqual(t, syntheticHash.Hex(), log["transactionHash"].(string))
251+
},
252+
wantLen: 2,
253+
},
254+
})
255+
}
256+
238257
func testFilterGetLogs(t *testing.T, namespace string, tests []GetFilterLogTests) {
239258
for _, tt := range tests {
240259
t.Run(tt.name, func(t *testing.T) {
@@ -283,7 +302,7 @@ func TestFilterGetFilterLogs(t *testing.T) {
283302

284303
resObj = sendRequest(t, TestPort, "getFilterLogs", filterId)
285304
logs := resObj["result"].([]interface{})
286-
require.Equal(t, 7, len(logs))
305+
require.Equal(t, 6, len(logs))
287306
for _, log := range logs {
288307
logObj := log.(map[string]interface{})
289308
require.Equal(t, "0x2", logObj["blockNumber"].(string))

evmrpc/info.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package evmrpc
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"math/big"
78
"slices"
89
"time"
@@ -41,6 +42,7 @@ type FeeHistoryResult struct {
4142
func (i *InfoAPI) BlockNumber() hexutil.Uint64 {
4243
startTime := time.Now()
4344
defer recordMetrics("eth_BlockNumber", i.connectionType, startTime, true)
45+
fmt.Println("In BlockNumber", "i.ctxProvider(LatestCtxHeight).BlockHeight() = ", i.ctxProvider(LatestCtxHeight).BlockHeight())
4446
return hexutil.Uint64(i.ctxProvider(LatestCtxHeight).BlockHeight())
4547
}
4648

evmrpc/setup_test.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const TestBadPort = 7779
5050
const MockHeight = 8
5151
const MultiTxBlockHeight = 2
5252
const DebugTraceMockHeight = 101
53+
const SyntheticBlockHeight = 100
5354

5455
var DebugTraceHashHex = "0x1234567890123456789023456789012345678901234567890123456789000004"
5556
var DebugTraceBlockHash = "BE17E0261E539CB7E9A91E123A6D794E0163D656FCF9B8EAC07823F7ED28512B"
@@ -167,6 +168,26 @@ func (c *MockClient) mockBlock(height int64) *coretypes.ResultBlock {
167168
},
168169
}
169170
}
171+
if height == SyntheticBlockHeight {
172+
return &coretypes.ResultBlock{
173+
BlockID: MockBlockID,
174+
Block: &tmtypes.Block{
175+
Header: mockBlockHeader(height),
176+
Data: tmtypes.Data{
177+
Txs: []tmtypes.Tx{
178+
func() []byte {
179+
bz, _ := Encoder(MultiTxBlockSynthTx)
180+
return bz
181+
}(),
182+
func() []byte {
183+
bz, _ := Encoder(MultiTxBlockTx1)
184+
return bz
185+
}(),
186+
},
187+
},
188+
},
189+
}
190+
}
170191
res := &coretypes.ResultBlock{
171192
BlockID: MockBlockID,
172193
Block: &tmtypes.Block{
@@ -576,7 +597,7 @@ func generateTxData() {
576597
From: "0x1234567890123456789012345678901234567890",
577598
To: "0x1234567890123456789012345678901234567890",
578599
TransactionIndex: 0,
579-
BlockNumber: 8,
600+
BlockNumber: MockHeight,
580601
TxType: 1,
581602
ContractAddress: "0x1234567890123456789012345678901234567890",
582603
CumulativeGasUsed: 123,
@@ -679,7 +700,8 @@ func setupLogs() {
679700
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000123"),
680701
},
681702
}}}})
682-
EVMKeeper.MockReceipt(Ctx, multiTxBlockTx1.Hash(), &types.Receipt{
703+
CtxMultiTx := Ctx.WithBlockHeight(MultiTxBlockHeight)
704+
EVMKeeper.MockReceipt(CtxMultiTx, multiTxBlockTx1.Hash(), &types.Receipt{
683705
BlockNumber: MultiTxBlockHeight,
684706
TransactionIndex: 1, // start at 1 bc 0 is the non-evm tx
685707
TxHashHex: multiTxBlockTx1.Hash().Hex(),
@@ -700,7 +722,7 @@ func setupLogs() {
700722
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000456"),
701723
},
702724
}}}})
703-
EVMKeeper.MockReceipt(Ctx, multiTxBlockTx2.Hash(), &types.Receipt{
725+
EVMKeeper.MockReceipt(CtxMultiTx, multiTxBlockTx2.Hash(), &types.Receipt{
704726
BlockNumber: MultiTxBlockHeight,
705727
TransactionIndex: 3,
706728
TxHashHex: multiTxBlockTx2.Hash().Hex(),
@@ -718,7 +740,7 @@ func setupLogs() {
718740
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000456"),
719741
},
720742
}}}})
721-
EVMKeeper.MockReceipt(Ctx, multiTxBlockTx3.Hash(), &types.Receipt{
743+
EVMKeeper.MockReceipt(CtxMultiTx, multiTxBlockTx3.Hash(), &types.Receipt{
722744
BlockNumber: MultiTxBlockHeight,
723745
TransactionIndex: 4,
724746
TxHashHex: multiTxBlockTx3.Hash().Hex(),
@@ -736,9 +758,10 @@ func setupLogs() {
736758
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000456"),
737759
},
738760
}}}})
739-
EVMKeeper.MockReceipt(Ctx, multiTxBlockTx4.Hash(), &types.Receipt{
761+
CtxMock := Ctx.WithBlockHeight(MockHeight)
762+
EVMKeeper.MockReceipt(CtxMock, multiTxBlockTx4.Hash(), &types.Receipt{
740763
BlockNumber: MockHeight,
741-
TransactionIndex: 0,
764+
TransactionIndex: 1,
742765
TxHashHex: multiTxBlockTx4.Hash().Hex(),
743766
LogsBloom: bloom4[:],
744767
Logs: []*types.Log{{
@@ -755,26 +778,28 @@ func setupLogs() {
755778
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000456"),
756779
},
757780
}}}})
758-
EVMKeeper.MockReceipt(Ctx, multiTxBlockSynthTx.Hash(), &types.Receipt{
759-
BlockNumber: MultiTxBlockHeight,
760-
TransactionIndex: 5,
781+
fmt.Println("multiTxBlockSynthTx.Hash()", multiTxBlockSynthTx.Hash().Hex())
782+
EVMKeeper.MockReceipt(CtxMock, multiTxBlockSynthTx.Hash(), &types.Receipt{
783+
BlockNumber: MockHeight,
784+
TransactionIndex: 0,
761785
TxHashHex: multiTxBlockSynthTx.Hash().Hex(),
762786
LogsBloom: bloomSynth[:],
763787
Logs: []*types.Log{{
764788
Address: "0x1111111111111111111111111111111111111116",
765789
Topics: []string{"0x0000000000000000000000000000000000000000000000000000000000000234", "0x0000000000000000000000000000000000000000000000000000000000000789"},
766790
Synthetic: true,
767791
}},
768-
EffectiveGasPrice: 100,
792+
EffectiveGasPrice: 0,
769793
})
770-
EVMKeeper.MockReceipt(Ctx, common.HexToHash(DebugTraceHashHex), &types.Receipt{
794+
CtxDebugTrace := Ctx.WithBlockHeight(DebugTraceMockHeight)
795+
EVMKeeper.MockReceipt(CtxDebugTrace, common.HexToHash(DebugTraceHashHex), &types.Receipt{
771796
BlockNumber: DebugTraceMockHeight,
772797
TransactionIndex: 0,
773798
TxHashHex: DebugTraceHashHex,
774799
})
775800
txNonEvmBz, _ := Encoder(TxNonEvmWithSyntheticLog)
776801
txNonEvmHash := sha256.Sum256(txNonEvmBz)
777-
EVMKeeper.MockReceipt(Ctx, txNonEvmHash, &types.Receipt{
802+
EVMKeeper.MockReceipt(CtxMultiTx, txNonEvmHash, &types.Receipt{
778803
BlockNumber: MultiTxBlockHeight,
779804
TransactionIndex: 1,
780805
TxHashHex: common.Hash(txNonEvmHash).Hex(),
@@ -786,22 +811,28 @@ func setupLogs() {
786811
}},
787812
EffectiveGasPrice: 100,
788813
})
814+
815+
// block 8
789816
EVMKeeper.SetTxHashesOnHeight(Ctx, MultiTxBlockHeight, []common.Hash{
790817
multiTxBlockTx1.Hash(),
791818
multiTxBlockTx2.Hash(),
792819
multiTxBlockTx3.Hash(),
793820
})
821+
EVMKeeper.SetBlockBloom(MultiTxCtx, []ethtypes.Bloom{bloom1, bloom2, bloom3})
822+
fmt.Println("Should be on block 8", multiTxBlockTx1.Hash(), multiTxBlockTx2.Hash(), multiTxBlockTx3.Hash())
823+
824+
// block 2
794825
EVMKeeper.SetTxHashesOnHeight(Ctx, MockHeight, []common.Hash{
795-
multiTxBlockTx4.Hash(),
796826
multiTxBlockSynthTx.Hash(),
827+
multiTxBlockTx4.Hash(),
797828
})
798829
bloomTx1 := ethtypes.CreateBloom(ethtypes.Receipts{&ethtypes.Receipt{Logs: []*ethtypes.Log{{
799830
Address: common.HexToAddress("0x1111111111111111111111111111111111111111"),
800831
Topics: []common.Hash{common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111"),
801832
common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111112")},
802833
}}}})
803-
EVMKeeper.SetBlockBloom(MultiTxCtx, []ethtypes.Bloom{bloom1, bloom2, bloom3})
804-
EVMKeeper.SetBlockBloom(Ctx, []ethtypes.Bloom{bloom4, bloomSynth, bloomTx1})
834+
EVMKeeper.SetBlockBloom(Ctx, []ethtypes.Bloom{bloomSynth, bloom4, bloomTx1})
835+
fmt.Println("Should be on block 2", multiTxBlockSynthTx.Hash(), multiTxBlockTx4.Hash())
805836
}
806837

807838
//nolint:deadcode

0 commit comments

Comments
 (0)