-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add logs validation method in order to check if the log come from giv…
…en block (#122)
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |