Skip to content

Commit 3a3af97

Browse files
authored
Set log index across all transactions in a block (#2067)
1 parent 9595df6 commit 3a3af97

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

app/receipt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (app *App) AddCosmosEventsToEVMReceiptIfApplicable(ctx sdk.Context, tx sdk.
118118
for i, l := range r.Logs {
119119
l.Index = uint32(i)
120120
}
121-
bloom = ethtypes.CreateBloom(ethtypes.Receipts{&ethtypes.Receipt{Logs: evmkeeper.GetLogsForTx(r)}})
121+
bloom = ethtypes.CreateBloom(ethtypes.Receipts{&ethtypes.Receipt{Logs: evmkeeper.GetLogsForTx(r, 0)}})
122122
r.LogsBloom = bloom[:]
123123
_ = app.EvmKeeper.SetTransientReceipt(wasmToEvmEventCtx, txHash, r)
124124
} else {

evmrpc/filter.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ func (f *LogFetcher) GetLogsForBlock(block *coretypes.ResultBlock, crit filters.
423423

424424
func (f *LogFetcher) FindLogsByBloom(block *coretypes.ResultBlock, filters [][]bloomIndexes) (res []*ethtypes.Log) {
425425
ctx := f.ctxProvider(LatestCtxHeight)
426+
totalLogs := uint(0)
426427
for _, hash := range getTxHashesFromBlock(block, f.txConfig, f.includeSyntheticReceipts) {
427428
receipt, err := f.k.GetReceipt(ctx, hash)
428429
if err != nil {
@@ -436,8 +437,9 @@ func (f *LogFetcher) FindLogsByBloom(block *coretypes.ResultBlock, filters [][]b
436437
continue
437438
}
438439
if len(receipt.LogsBloom) > 0 && MatchFilters(ethtypes.Bloom(receipt.LogsBloom), filters) {
439-
res = append(res, keeper.GetLogsForTx(receipt)...)
440+
res = append(res, keeper.GetLogsForTx(receipt, totalLogs)...)
440441
}
442+
totalLogs += uint(len(receipt.Logs))
441443
}
442444
return
443445
}

evmrpc/tx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func GetEvmTxIndex(txs tmtypes.Txs, txIndex uint32, decoder sdk.TxDecoder, recei
350350
func encodeReceipt(receipt *types.Receipt, decoder sdk.TxDecoder, block *coretypes.ResultBlock, receiptChecker func(common.Hash) bool) (map[string]interface{}, error) {
351351
blockHash := block.BlockID.Hash
352352
bh := common.HexToHash(blockHash.String())
353-
logs := keeper.GetLogsForTx(receipt)
353+
logs := keeper.GetLogsForTx(receipt, 0)
354354
for _, log := range logs {
355355
log.BlockHash = bh
356356
}

x/evm/keeper/log.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ func (k *Keeper) GetLegacyBlockBloomCutoffHeight(ctx sdk.Context) int64 {
6161
return int64(binary.BigEndian.Uint64(bz))
6262
}
6363

64-
func GetLogsForTx(receipt *types.Receipt) []*ethtypes.Log {
65-
return utils.Map(receipt.Logs, func(l *types.Log) *ethtypes.Log { return convertLog(l, receipt) })
64+
func GetLogsForTx(receipt *types.Receipt, logStartIndex uint) []*ethtypes.Log {
65+
return utils.Map(receipt.Logs, func(l *types.Log) *ethtypes.Log { return convertLog(l, receipt, logStartIndex) })
6666
}
6767

68-
func convertLog(l *types.Log, receipt *types.Receipt) *ethtypes.Log {
68+
func convertLog(l *types.Log, receipt *types.Receipt, logStartIndex uint) *ethtypes.Log {
6969
return &ethtypes.Log{
7070
Address: common.HexToAddress(l.Address),
7171
Topics: utils.Map(l.Topics, common.HexToHash),
7272
Data: l.Data,
7373
BlockNumber: receipt.BlockNumber,
7474
TxHash: common.HexToHash(receipt.TxHashHex),
7575
TxIndex: uint(receipt.TransactionIndex),
76-
Index: uint(l.Index)}
76+
Index: uint(l.Index) + logStartIndex}
7777
}
7878

7979
func ConvertEthLog(l *ethtypes.Log) *types.Log {

x/evm/keeper/log_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestGetLogsForTx(t *testing.T) {
6969
}
7070

7171
// Convert the types.Receipt to a list of ethtypes.Log objects
72-
logs := keeper.GetLogsForTx(receipt)
72+
logs := keeper.GetLogsForTx(receipt, 0)
7373

7474
// Check that the fields match
7575
require.Equal(t, len(receipt.Logs), len(logs))
@@ -82,6 +82,13 @@ func TestGetLogsForTx(t *testing.T) {
8282
require.Equal(t, receipt.Logs[i].Data, log.Data)
8383
require.Equal(t, uint(receipt.Logs[i].Index), log.Index)
8484
}
85+
86+
// non-zero starting index
87+
logs = keeper.GetLogsForTx(receipt, 5)
88+
require.Equal(t, len(receipt.Logs), len(logs))
89+
for i, log := range logs {
90+
require.Equal(t, uint(receipt.Logs[i].Index+5), log.Index)
91+
}
8592
}
8693

8794
func TestLegacyBlockBloomCutoffHeight(t *testing.T) {

0 commit comments

Comments
 (0)