Skip to content

Commit 0c263a8

Browse files
committed
refactor: utils function
1 parent 1d933c0 commit 0c263a8

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

rpc/backend/comet_to_eth.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,7 @@ func (b *Backend) ReceiptsFromCometBlock(
310310
// reassign log indices to be block-global per Ethereum spec.
311311
// statedb assigns per-tx indices starting at 0 for Block STM compatibility,
312312
// so we apply the cumulative offset here at the RPC layer.
313-
for _, log := range logs {
314-
log.Index = cumulatedLogIndex
315-
cumulatedLogIndex++
316-
}
313+
cumulatedLogIndex = assignBlockGlobalLogIndices(logs, cumulatedLogIndex)
317314

318315
bloom := ethtypes.CreateBloom(&ethtypes.Receipt{Logs: logs})
319316

rpc/backend/tx_info.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
rpctypes "github.com/cosmos/evm/rpc/types"
2626
servertypes "github.com/cosmos/evm/server/types"
2727
evmtrace "github.com/cosmos/evm/trace"
28-
"github.com/cosmos/evm/utils"
2928
evmtypes "github.com/cosmos/evm/x/vm/types"
3029

3130
errorsmod "cosmossdk.io/errors"
@@ -210,6 +209,10 @@ func (b *Backend) GetTransactionReceipt(ctx context.Context, hash common.Hash) (
210209
return nil, fmt.Errorf("failed to get receipts from comet block")
211210
}
212211

212+
// fix log indices to be block-global per Ethereum spec
213+
offset := countPriorBlockLogs(blockRes.TxsResults, res.TxIndex, blockRes.Height)
214+
assignBlockGlobalLogIndices(receipts[0].Logs, offset)
215+
213216
var signer ethtypes.Signer
214217
ethTx := ethMsg.AsTransaction()
215218
if ethTx.Protected() {
@@ -248,36 +251,21 @@ func (b *Backend) GetTransactionLogs(ctx context.Context, hash common.Hash) (res
248251
b.Logger.Debug("block result not found", "number", res.Height, "error", err.Error())
249252
return nil, nil
250253
}
251-
height, err := utils.SafeUint64(resBlockResult.Height)
252-
if err != nil {
253-
return nil, err
254-
}
255-
// compute cumulative log index offset from prior txs in the block
256-
cumulatedLogIndex := uint(0)
257-
for ti := int64(0); ti < int64(res.TxIndex); ti++ {
258-
priorLogs, err := evmtypes.DecodeTxLogs(resBlockResult.TxsResults[ti].Data, height)
259-
if err != nil {
260-
continue
261-
}
262-
cumulatedLogIndex += uint(len(priorLogs))
263-
}
254+
offset := countPriorBlockLogs(resBlockResult.TxsResults, res.TxIndex, resBlockResult.Height)
264255

265256
// parse tx logs from events
266257
index := int(res.MsgIndex) // #nosec G701
267258
logs, err := evmtypes.DecodeMsgLogs(
268259
resBlockResult.TxsResults[res.TxIndex].Data,
269260
index,
270-
height,
261+
uint64(resBlockResult.Height), // #nosec G115 -- block heights are always positive
271262
)
272263
if err != nil {
273264
b.Logger.Debug("failed to parse tx logs", "error", err.Error())
274265
}
275266

276-
// reassign log indices to be block-global per Ethereum spec
277-
for _, log := range logs {
278-
log.Index = cumulatedLogIndex
279-
cumulatedLogIndex++
280-
}
267+
// fix log indices to be block-global per Ethereum spec
268+
assignBlockGlobalLogIndices(logs, offset)
281269

282270
return logs, nil
283271
}

rpc/backend/utils.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,39 @@ func GetLogsFromBlockResults(blockRes *cmtrpctypes.ResultBlockResults) ([][]*eth
326326
return nil, err
327327
}
328328
// reassign log indices to be block-global per Ethereum spec
329-
for _, log := range logs {
330-
log.Index = cumulatedLogIndex
331-
cumulatedLogIndex++
332-
}
329+
cumulatedLogIndex = assignBlockGlobalLogIndices(logs, cumulatedLogIndex)
333330
blockLogs = append(blockLogs, logs)
334331
}
335332
return blockLogs, nil
336333
}
337334

335+
// countPriorBlockLogs returns the total number of EVM logs in all Cosmos txs
336+
// before txIndex within the given block results. This offset is needed to
337+
// produce block-global log indices per the Ethereum JSON-RPC spec.
338+
// TODO: store latest N blocks logindex for optimization
339+
func countPriorBlockLogs(txsResults []*abci.ExecTxResult, txIndex uint32, blockHeight int64) uint {
340+
height := uint64(blockHeight) // #nosec G115 -- block heights are always positive
341+
offset := uint(0)
342+
for i := uint32(0); i < txIndex; i++ {
343+
logs, err := evmtypes.DecodeTxLogs(txsResults[i].Data, height)
344+
if err != nil {
345+
continue
346+
}
347+
offset += uint(len(logs))
348+
}
349+
return offset
350+
}
351+
352+
// assignBlockGlobalLogIndices sets block-global log indices on the given logs
353+
// starting from startIndex. Returns the next available index.
354+
func assignBlockGlobalLogIndices(logs []*ethtypes.Log, startIndex uint) uint {
355+
for _, log := range logs {
356+
log.Index = startIndex
357+
startIndex++
358+
}
359+
return startIndex
360+
}
361+
338362
// GetHexProofs returns list of hex data of proof op
339363
func GetHexProofs(proof *crypto.ProofOps) []string {
340364
if proof == nil {

tests/systemtests/main_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/cosmos/evm/tests/systemtests/chainupgrade"
1010
"github.com/cosmos/evm/tests/systemtests/eip712"
1111
"github.com/cosmos/evm/tests/systemtests/logindex"
12-
1312
"github.com/cosmos/evm/tests/systemtests/mempool"
1413
"github.com/cosmos/evm/tests/systemtests/suite"
1514

0 commit comments

Comments
 (0)