|
| 1 | +// Copyright 2025 The Swarm Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package node_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "compress/gzip" |
| 10 | + "context" |
| 11 | + "encoding/json" |
| 12 | + "math/big" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/ethereum/go-ethereum" |
| 16 | + "github.com/ethereum/go-ethereum/common" |
| 17 | + "github.com/ethereum/go-ethereum/core/types" |
| 18 | + "github.com/ethersphere/bee/v2/pkg/log" |
| 19 | + "github.com/ethersphere/bee/v2/pkg/node" |
| 20 | + "github.com/ethersphere/bee/v2/pkg/postage/listener" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +type mockSnapshotGetter struct { |
| 26 | + data []byte |
| 27 | +} |
| 28 | + |
| 29 | +func newMockSnapshotGetter(data []byte) mockSnapshotGetter { |
| 30 | + return mockSnapshotGetter{data} |
| 31 | +} |
| 32 | +func (m mockSnapshotGetter) GetBatchSnapshot() []byte { |
| 33 | + return m.data |
| 34 | +} |
| 35 | + |
| 36 | +func makeSnapshotData(logs []types.Log) []byte { |
| 37 | + var buf bytes.Buffer |
| 38 | + gz := gzip.NewWriter(&buf) |
| 39 | + enc := json.NewEncoder(gz) |
| 40 | + for _, l := range logs { |
| 41 | + _ = enc.Encode(l) |
| 42 | + } |
| 43 | + gz.Close() |
| 44 | + return buf.Bytes() |
| 45 | +} |
| 46 | + |
| 47 | +func TestNewSnapshotLogFilterer(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + t.Run("invalid gzip", func(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + getter := newMockSnapshotGetter([]byte("not-gzip")) |
| 52 | + filterer := node.NewSnapshotLogFilterer(log.Noop, getter) |
| 53 | + _, err := filterer.BlockNumber(context.Background()) |
| 54 | + assert.Error(t, err) |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("invalid log entry", func(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + var buf bytes.Buffer |
| 60 | + gz := gzip.NewWriter(&buf) |
| 61 | + _, err := gz.Write([]byte("not-a-log-entry")) |
| 62 | + require.NoError(t, err) |
| 63 | + gz.Close() |
| 64 | + getter := newMockSnapshotGetter(buf.Bytes()) |
| 65 | + filterer := node.NewSnapshotLogFilterer(log.Noop, getter) |
| 66 | + _, err = filterer.BlockNumber(context.Background()) |
| 67 | + assert.ErrorIs(t, err, listener.ErrParseSnapshot) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("non-sorted", func(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + logs := []types.Log{ |
| 73 | + {BlockNumber: 1, Topics: []common.Hash{}}, |
| 74 | + {BlockNumber: 3, Topics: []common.Hash{}}, |
| 75 | + {BlockNumber: 2, Topics: []common.Hash{}}, |
| 76 | + } |
| 77 | + getter := newMockSnapshotGetter(makeSnapshotData(logs)) |
| 78 | + filterer := node.NewSnapshotLogFilterer(log.Noop, getter) |
| 79 | + |
| 80 | + _, err := filterer.BlockNumber(context.Background()) |
| 81 | + assert.ErrorIs(t, err, listener.ErrParseSnapshot) |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("get block number", func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + logs := []types.Log{ |
| 87 | + {BlockNumber: 1, Topics: []common.Hash{}}, |
| 88 | + {BlockNumber: 2, Topics: []common.Hash{}}, |
| 89 | + {BlockNumber: 2, Topics: []common.Hash{}}, |
| 90 | + {BlockNumber: 3, Topics: []common.Hash{}}, |
| 91 | + } |
| 92 | + getter := newMockSnapshotGetter(makeSnapshotData(logs)) |
| 93 | + filterer := node.NewSnapshotLogFilterer(log.Noop, getter) |
| 94 | + |
| 95 | + blockNumber, err := filterer.BlockNumber(context.Background()) |
| 96 | + assert.NoError(t, err) |
| 97 | + assert.Equal(t, uint64(3), blockNumber) |
| 98 | + }) |
| 99 | + t.Run("filter", func(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + logs := []types.Log{ |
| 102 | + {BlockNumber: 1, Address: common.HexToAddress("0x1"), TxHash: common.HexToHash("0x1"), Topics: []common.Hash{common.HexToHash("0xa1")}}, |
| 103 | + {BlockNumber: 2, Address: common.HexToAddress("0x2"), TxHash: common.HexToHash("0x2"), Topics: []common.Hash{common.HexToHash("0xa1")}}, |
| 104 | + {BlockNumber: 3, Address: common.HexToAddress("0x3"), TxHash: common.HexToHash("0x3"), Topics: []common.Hash{common.HexToHash("0xa3")}}, |
| 105 | + {BlockNumber: 4, Address: common.HexToAddress("0x4"), TxHash: common.HexToHash("0x4"), Topics: []common.Hash{common.HexToHash("0xa4")}}, |
| 106 | + {BlockNumber: 5, Address: common.HexToAddress("0x4"), TxHash: common.HexToHash("0x4"), Topics: []common.Hash{common.HexToHash("0xa4"), common.HexToHash("0xa5")}}, |
| 107 | + } |
| 108 | + getter := newMockSnapshotGetter(makeSnapshotData(logs)) |
| 109 | + filterer := node.NewSnapshotLogFilterer(log.Noop, getter) |
| 110 | + |
| 111 | + res, err := filterer.FilterLogs(context.Background(), ethereum.FilterQuery{ |
| 112 | + FromBlock: big.NewInt(2), |
| 113 | + ToBlock: big.NewInt(3), |
| 114 | + }) |
| 115 | + require.NoError(t, err) |
| 116 | + require.Len(t, res, 2) |
| 117 | + assert.Equal(t, uint64(2), res[0].BlockNumber) |
| 118 | + assert.Equal(t, uint64(3), res[1].BlockNumber) |
| 119 | + |
| 120 | + res, err = filterer.FilterLogs(context.Background(), ethereum.FilterQuery{ |
| 121 | + Addresses: []common.Address{common.HexToAddress("0x3"), common.HexToAddress("0x4")}, |
| 122 | + }) |
| 123 | + require.NoError(t, err) |
| 124 | + require.Len(t, res, 3) |
| 125 | + assert.Equal(t, 0, res[0].Address.Cmp(common.HexToAddress("0x3"))) |
| 126 | + assert.Equal(t, 0, res[1].Address.Cmp(common.HexToAddress("0x4"))) |
| 127 | + assert.Equal(t, 0, res[2].Address.Cmp(common.HexToAddress("0x4"))) |
| 128 | + |
| 129 | + res, err = filterer.FilterLogs(context.Background(), ethereum.FilterQuery{}) |
| 130 | + require.NoError(t, err) |
| 131 | + require.Len(t, res, 5) |
| 132 | + |
| 133 | + res, err = filterer.FilterLogs(context.Background(), ethereum.FilterQuery{ |
| 134 | + Topics: [][]common.Hash{}, |
| 135 | + }) |
| 136 | + require.NoError(t, err) |
| 137 | + require.Len(t, res, 5) |
| 138 | + |
| 139 | + res, err = filterer.FilterLogs(context.Background(), ethereum.FilterQuery{ |
| 140 | + Topics: [][]common.Hash{ |
| 141 | + {common.HexToHash("0xa1"), common.HexToHash("0xa4"), common.HexToHash("0xa5")}, |
| 142 | + }, |
| 143 | + }) |
| 144 | + require.NoError(t, err) |
| 145 | + require.Len(t, res, 4) |
| 146 | + assert.Equal(t, 0, res[0].Topics[0].Cmp(common.HexToHash("0xa1"))) |
| 147 | + assert.Equal(t, 0, res[1].Topics[0].Cmp(common.HexToHash("0xa1"))) |
| 148 | + assert.Equal(t, 0, res[2].Topics[0].Cmp(common.HexToHash("0xa4"))) |
| 149 | + assert.Equal(t, 0, res[3].Topics[0].Cmp(common.HexToHash("0xa4"))) |
| 150 | + }) |
| 151 | +} |
0 commit comments