Skip to content

Commit

Permalink
EVM-666: Fix eth_getLogs index issue (#1533)
Browse files Browse the repository at this point in the history
* Fix logIndex value setting

* Comments fix

* Exclude ethereum tests folder from sonar
  • Loading branch information
goran-ethernal committed May 23, 2023
1 parent 60b21b4 commit 3f3e071
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
5 changes: 4 additions & 1 deletion jsonrpc/filter_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,11 @@ func (f *FilterManager) getLogsFromBlock(query *LogQuery, block *types.Block) ([
return nil, err
}

logIdx := uint64(0)
logs := make([]*Log, 0)

for idx, receipt := range receipts {
for logIdx, log := range receipt.Logs {
for _, log := range receipt.Logs {
if query.Match(log) {
logs = append(logs, &Log{
Address: log.Address,
Expand All @@ -391,6 +392,8 @@ func (f *FilterManager) getLogsFromBlock(query *LogQuery, block *types.Block) ([
LogIndex: argUint64(logIdx),
})
}

logIdx++
}
}

Expand Down
77 changes: 77 additions & 0 deletions jsonrpc/filter_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/gorilla/websocket"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_GetLogsForQuery(t *testing.T) {
Expand Down Expand Up @@ -154,6 +155,82 @@ func Test_GetLogsForQuery(t *testing.T) {
}
}

func Test_getLogsFromBlock(t *testing.T) {
t.Parallel()

numOfLogs := 4

block := &types.Block{
Header: &types.Header{Hash: types.StringToHash("someHash"), Number: 1},
Transactions: []*types.Transaction{
createTestTransaction(types.StringToHash("tx1")),
createTestTransaction(types.StringToHash("tx2")),
createTestTransaction(types.StringToHash("tx3")),
},
}

// setup test with block with 3 transactions and 4 logs
store := &mockBlockStore{receipts: map[types.Hash][]*types.Receipt{
block.Header.Hash: {
{
// transaction 1 logs
Logs: []*types.Log{
{
Topics: []types.Hash{
hash1,
},
},
{
Topics: []types.Hash{
hash2,
},
},
},
},
{
// transaction 2 logs
Logs: []*types.Log{
{
Topics: []types.Hash{
hash3,
},
},
},
},
{
// transaction 3 logs
Logs: []*types.Log{
{
Topics: []types.Hash{
hash4,
},
},
},
},
},
}}

store.appendBlocksToStore([]*types.Block{block})

f := NewFilterManager(hclog.NewNullLogger(), store, 1000)

t.Cleanup(func() {
defer f.Close()
})

logs, err := f.getLogsFromBlock(&LogQuery{
fromBlock: 1,
toBlock: 1,
}, block)

require.NoError(t, err)
require.Len(t, logs, numOfLogs)

for i := 0; i < numOfLogs; i++ {
require.Equal(t, uint64(i), uint64(logs[i].LogIndex))
}
}

func Test_GetLogFilterFromID(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 2 additions & 2 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
sonar.projectKey=polygon-edge

sonar.sources=.
sonar.exclusions=**/*_test.go,**/vendor/**,**/*.py,**/core-contracts/**
sonar.exclusions=**/*_test.go,**/vendor/**,**/*.py,**/core-contracts/**,**/tests/**

sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**

sonar.coverage.exclusions=**/vendor/**,**/*.py
sonar.coverage.exclusions=**/vendor/**,**/*.py,**/tests/**

# =====================================================
# Properties specific to Go
Expand Down

0 comments on commit 3f3e071

Please sign in to comment.