Skip to content

Commit

Permalink
add logs validation method in order to check if the log come from giv…
Browse files Browse the repository at this point in the history
…en block (#122)
  • Loading branch information
marino39 authored May 17, 2024
1 parent 8838f77 commit d096aa7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ethutil/validate_logs_with_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ethutil

import (
"bytes"

"github.com/0xsequence/ethkit/go-ethereum/core/types"
)

// ValidateLogsWithBlockHeader validates that the logs comes from given block.
// If the list of logs is not complete or the logs are not from the block, it
// will return false.
func ValidateLogsWithBlockHeader(logs []types.Log, header *types.Header) bool {
return bytes.Compare(logsToBloom(logs).Bytes(), header.Bloom.Bytes()) == 0
}

func logsToBloom(logs []types.Log) types.Bloom {
var logBloom types.Bloom
for _, log := range logs {
logBloom.Add(log.Address.Bytes())
for _, b := range log.Topics {
logBloom.Add(b[:])
}
}
return logBloom
}
28 changes: 28 additions & 0 deletions ethutil/validate_logs_with_block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ethutil

import (
"context"
"math/big"
"testing"

"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/go-ethereum"
"github.com/stretchr/testify/require"
)

func TestValidateLogsWithBlockHeader(t *testing.T) {
p, err := ethrpc.NewProvider("https://nodes.sequence.app/polygon")
require.NoError(t, err)

header, err := p.HeaderByNumber(context.Background(), big.NewInt(20_000_003))
require.NoError(t, err)
require.NotNil(t, header)

logs, err := p.FilterLogs(context.Background(), ethereum.FilterQuery{
FromBlock: big.NewInt(20_000_003),
ToBlock: big.NewInt(20_000_003),
})
require.NoError(t, err)

require.True(t, ValidateLogsWithBlockHeader(logs, header))
}

0 comments on commit d096aa7

Please sign in to comment.