Skip to content

Commit 20f9e6f

Browse files
committed
Add LogsFilterFunc signature and sample implementation instead of optional validation method
1 parent 045e075 commit 20f9e6f

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

cmd/ethkit/block.go

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -291,40 +291,35 @@ func CheckLogs(block *types.Block, provider *ethrpc.Provider, ignoreZeroGasLogs
291291
}
292292

293293
filteredLogs := logs
294-
var optCheck []ethutil.LogsBloomCheckFunc
295-
296294
if ignoreZeroGasLogs {
297-
// Build a quick lookup of tx hash -> gas price so we can drop system (zero price) tx logs.
298-
gasPriceByTx := make(map[common.Hash]*big.Int, len(block.Transactions()))
299-
for _, tx := range block.Transactions() {
300-
gasPriceByTx[tx.Hash()] = tx.GasPrice()
301-
}
302-
303-
filter := func(ls []types.Log) []types.Log {
304-
out := make([]types.Log, 0, len(ls))
305-
for _, l := range ls {
306-
if gp, ok := gasPriceByTx[l.TxHash]; ok && gp.Sign() == 0 {
307-
// HyperEVM system tx (gas price = 0) — ignore for bloom validation.
308-
continue
309-
}
310-
out = append(out, l)
311-
}
312-
return out
313-
}
314-
315-
filteredLogs = filter(logs)
316-
317-
optCheck = append(optCheck, func(ls []types.Log, header *types.Header) bool {
318-
return ethutil.ValidateLogsWithBlockHeader(filter(ls), header)
319-
})
295+
filteredLogs = zeroGasLogsFilter(logs, h, block)
320296
}
321297

322298
fmt.Printf("Block: %d\n", h.Number.Uint64())
323299
fmt.Printf("Logs Count: %d\n", len(filteredLogs))
324-
fmt.Printf("Match: %v\n", ethutil.ValidateLogsWithBlockHeader(filteredLogs, h, optCheck...))
300+
fmt.Printf("Match: %v\n", ethutil.ValidateLogsWithBlockHeader(filteredLogs, h))
325301
fmt.Println()
326302
fmt.Printf("Calculated Log Bloom: 0x%x\n", ethutil.ConvertLogsToBloom(filteredLogs).Bytes())
327303
fmt.Println()
328304
fmt.Printf("Header Log Bloom: 0x%x\n", h.Bloom.Bytes())
329305
fmt.Println()
330306
}
307+
308+
// zeroGasLogsFilter removes logs from transactions whose gas price is zero
309+
// (HyperEVM system transactions).
310+
func zeroGasLogsFilter(ls []types.Log, _ *types.Header, block *types.Block) []types.Log {
311+
gasPriceByTx := make(map[common.Hash]*big.Int, len(block.Transactions()))
312+
for _, tx := range block.Transactions() {
313+
gasPriceByTx[tx.Hash()] = tx.GasPrice()
314+
}
315+
316+
out := make([]types.Log, 0, len(ls))
317+
for _, l := range ls {
318+
if gp, ok := gasPriceByTx[l.TxHash]; ok && gp.Sign() == 0 {
319+
// HyperEVM system tx (gas price = 0) — ignore for bloom validation.
320+
continue
321+
}
322+
out = append(out, l)
323+
}
324+
return out
325+
}

ethutil/validate_logs_with_block.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ import (
66
"github.com/0xsequence/ethkit/go-ethereum/core/types"
77
)
88

9-
// LogsBloomCheckFunc allows callers to override how logs bloom validation is performed.
9+
// LogsBloomCheckFunc is the shape of a logs bloom validation function.
1010
// Returning true means the logs match the header; false means they do not.
1111
type LogsBloomCheckFunc func(logs []types.Log, header *types.Header) bool
1212

13-
// ValidateLogsWithBlockHeader validates that the logs comes from given block.
14-
// If the list of logs is not complete or the logs are not from the block, it
15-
// will return false.
16-
func ValidateLogsWithBlockHeader(logs []types.Log, header *types.Header, optLogsBloomCheck ...LogsBloomCheckFunc) bool {
17-
// Allow callers to override the check logic (e.g. filtering certain logs).
18-
if len(optLogsBloomCheck) > 0 && optLogsBloomCheck[0] != nil {
19-
return optLogsBloomCheck[0](logs, header)
20-
}
13+
// LogsFilterFunc transforms or filters logs before validation.
14+
// The block is provided for cases where filtering depends on transaction data.
15+
type LogsFilterFunc func(logs []types.Log, header *types.Header, block *types.Block) []types.Log
2116

17+
// ValidateLogsWithBlockHeader validates that the logs come from the given block
18+
// by comparing the calculated bloom against the header bloom.
19+
func ValidateLogsWithBlockHeader(logs []types.Log, header *types.Header) bool {
2220
return bytes.Equal(ConvertLogsToBloom(logs).Bytes(), header.Bloom.Bytes())
2321
}
2422

ethutil/validate_logs_with_block_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestValidateLogsWithBlockHeader(t *testing.T) {
3030
require.True(t, ValidateLogsWithBlockHeader(logs, header))
3131
}
3232

33-
func TestValidateLogsWithBlockHeaderWithCustomCheck(t *testing.T) {
33+
func TestValidateLogsWithBlockHeaderWithFilter(t *testing.T) {
3434
logs := []types.Log{
3535
{
3636
Address: common.HexToAddress("0x0000000000000000000000000000000000000001"),
@@ -48,11 +48,16 @@ func TestValidateLogsWithBlockHeaderWithCustomCheck(t *testing.T) {
4848
require.True(t, ValidateLogsWithBlockHeader(logs, headerFull))
4949
require.False(t, ValidateLogsWithBlockHeader(logs, headerFiltered))
5050

51-
customCheck := func(ls []types.Log, header *types.Header) bool {
51+
var filter LogsFilterFunc = func(ls []types.Log, _ *types.Header, _ *types.Block) []types.Log {
5252
// Ignore the first log (e.g., system tx) and validate bloom against the remainder.
53-
filtered := ls[1:]
54-
return bytes.Equal(ConvertLogsToBloom(filtered).Bytes(), header.Bloom.Bytes())
53+
return ls[1:]
5554
}
55+
filteredLogs := filter(logs, headerFiltered, nil)
5656

57-
require.True(t, ValidateLogsWithBlockHeader(logs, headerFiltered, customCheck))
57+
require.True(t, ValidateLogsWithBlockHeader(filteredLogs, headerFiltered))
58+
59+
var customCheck LogsBloomCheckFunc = func(ls []types.Log, header *types.Header) bool {
60+
return bytes.Equal(ConvertLogsToBloom(ls[1:]).Bytes(), header.Bloom.Bytes())
61+
}
62+
require.True(t, customCheck(logs, headerFiltered))
5863
}

0 commit comments

Comments
 (0)