Skip to content

Commit

Permalink
EVM-677: Fix log index issue on eth_getTransactionReceipt (#1576)
Browse files Browse the repository at this point in the history
* Fix log index issue

* Comments fix
  • Loading branch information
goran-ethernal committed Jun 1, 2023
1 parent e1db725 commit de626d9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
42 changes: 35 additions & 7 deletions jsonrpc/eth_blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,30 +188,58 @@ func TestEth_GetTransactionReceipt(t *testing.T) {
eth := newTestEthEndpoint(store)
block := newTestBlock(1, hash4)
store.add(block)
txn := newTestTransaction(uint64(0), addr0)
block.Transactions = append(block.Transactions, txn)
rec := &types.Receipt{
txn0 := newTestTransaction(uint64(0), addr0)
txn1 := newTestTransaction(uint64(1), addr1)
block.Transactions = []*types.Transaction{txn0, txn1}
receipt1 := &types.Receipt{
Logs: []*types.Log{
{
// log 0
Topics: []types.Hash{
hash1,
},
},
{
// log 1
Topics: []types.Hash{
hash2,
},
},
{
// log 2
Topics: []types.Hash{
hash3,
},
},
},
}
receipt1.SetStatus(types.ReceiptSuccess)
receipt2 := &types.Receipt{
Logs: []*types.Log{
{
// log 3
Topics: []types.Hash{
hash4,
},
},
},
}
rec.SetStatus(types.ReceiptSuccess)
store.receipts[hash4] = []*types.Receipt{rec}
receipt2.SetStatus(types.ReceiptSuccess)
store.receipts[hash4] = []*types.Receipt{receipt1, receipt2}

res, err := eth.GetTransactionReceipt(txn.Hash)
res, err := eth.GetTransactionReceipt(txn1.Hash)

assert.NoError(t, err)
assert.NotNil(t, res)

//nolint:forcetypeassert
response := res.(*receipt)
assert.Equal(t, txn.Hash, response.TxHash)
assert.Equal(t, txn1.Hash, response.TxHash)
assert.Equal(t, block.Hash(), response.BlockHash)
assert.NotNil(t, response.Logs)
assert.Len(t, response.Logs, 1)
assert.Equal(t, uint64(3), uint64(response.Logs[0].LogIndex))
assert.Equal(t, uint64(1), uint64(response.Logs[0].TxIndex))
})
}

Expand Down
25 changes: 15 additions & 10 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,35 +315,40 @@ func (e *Eth) GetTransactionReceipt(hash types.Hash) (interface{}, error) {
return nil, nil
}
// find the transaction in the body
indx := -1
txIndex := -1
logIndex := 0

for i, txn := range block.Transactions {
if txn.Hash == hash {
indx = i
txIndex = i

break
}

// accumulate receipt logs indexes from block transactions
// that are before the desired transaction
logIndex += len(receipts[i].Logs)
}

if indx == -1 {
if txIndex == -1 {
// txn not found
return nil, nil
}

txn := block.Transactions[indx]
raw := receipts[indx]
txn := block.Transactions[txIndex]
raw := receipts[txIndex]

logs := make([]*Log, len(raw.Logs))
for indx, elem := range raw.Logs {
logs[indx] = &Log{
for i, elem := range raw.Logs {
logs[i] = &Log{
Address: elem.Address,
Topics: elem.Topics,
Data: argBytes(elem.Data),
BlockHash: block.Hash(),
BlockNumber: argUint64(block.Number()),
TxHash: txn.Hash,
TxIndex: argUint64(indx),
LogIndex: argUint64(indx),
TxIndex: argUint64(txIndex),
LogIndex: argUint64(logIndex + i),
Removed: false,
}
}
Expand All @@ -354,7 +359,7 @@ func (e *Eth) GetTransactionReceipt(hash types.Hash) (interface{}, error) {
LogsBloom: raw.LogsBloom,
Status: argUint64(*raw.Status),
TxHash: txn.Hash,
TxIndex: argUint64(indx),
TxIndex: argUint64(txIndex),
BlockHash: block.Hash(),
BlockNumber: argUint64(block.Number()),
GasUsed: argUint64(raw.GasUsed),
Expand Down

0 comments on commit de626d9

Please sign in to comment.