|
| 1 | +package node |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "compress/gzip" |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + |
| 12 | + "github.com/ethereum/go-ethereum" |
| 13 | + "github.com/ethereum/go-ethereum/core/types" |
| 14 | + archive "github.com/ethersphere/batch-archive" |
| 15 | + "github.com/ethersphere/bee/v2/pkg/log" |
| 16 | +) |
| 17 | + |
| 18 | +type SnapshotBlockHeightContractFilterer struct { |
| 19 | + logger log.Logger |
| 20 | + loadedLogs []types.Log |
| 21 | + maxBlockHeight uint64 |
| 22 | + isLoaded bool |
| 23 | +} |
| 24 | + |
| 25 | +func NewSnapshotBlockHeightContractFilterer(logger log.Logger) (*SnapshotBlockHeightContractFilterer, error) { |
| 26 | + f := &SnapshotBlockHeightContractFilterer{ |
| 27 | + logger: logger, |
| 28 | + } |
| 29 | + |
| 30 | + if err := f.loadAndProcessSnapshot(); err != nil { |
| 31 | + return nil, fmt.Errorf("failed to load and process snapshot during initialization: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + return f, nil |
| 35 | +} |
| 36 | + |
| 37 | +func (f *SnapshotBlockHeightContractFilterer) loadAndProcessSnapshot() error { |
| 38 | + f.logger.Info("loading batch snapshot during construction") |
| 39 | + data := archive.GetBatchSnapshot(true) |
| 40 | + |
| 41 | + dataReader := bytes.NewReader(data) |
| 42 | + gzipReader, err := gzip.NewReader(dataReader) |
| 43 | + if err != nil { |
| 44 | + f.logger.Error(err, "failed to create gzip reader for batch import") |
| 45 | + return fmt.Errorf("create gzip reader: %w", err) |
| 46 | + } |
| 47 | + defer gzipReader.Close() |
| 48 | + |
| 49 | + if err := f.parseLogs(gzipReader); err != nil { |
| 50 | + f.logger.Error(err, "failed to parse logs from snapshot") |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + f.isLoaded = true |
| 55 | + f.logger.Info("batch snapshot loaded successfully during construction", "log_count", len(f.loadedLogs), "max_block_height", f.maxBlockHeight) |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func (f *SnapshotBlockHeightContractFilterer) parseLogs(reader io.Reader) error { |
| 60 | + var parsedLogs []types.Log |
| 61 | + var currentMaxBlockHeight uint64 |
| 62 | + scanner := bufio.NewScanner(reader) |
| 63 | + |
| 64 | + for scanner.Scan() { |
| 65 | + line := scanner.Bytes() |
| 66 | + if len(bytes.TrimSpace(line)) == 0 { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + var logEntry types.Log |
| 71 | + if err := json.Unmarshal(line, &logEntry); err != nil { |
| 72 | + f.logger.Warning("failed to unmarshal log event, skipping line", "error", err, "line_snippet", string(line[:min(len(line), 100)])) |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + if logEntry.BlockNumber > currentMaxBlockHeight { |
| 77 | + currentMaxBlockHeight = logEntry.BlockNumber |
| 78 | + } |
| 79 | + parsedLogs = append(parsedLogs, logEntry) |
| 80 | + } |
| 81 | + |
| 82 | + if err := scanner.Err(); err != nil { |
| 83 | + return fmt.Errorf("error scanning batch import data: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + f.loadedLogs = parsedLogs |
| 87 | + f.maxBlockHeight = currentMaxBlockHeight |
| 88 | + f.isLoaded = true |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func (f *SnapshotBlockHeightContractFilterer) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) { |
| 93 | + f.logger.Debug("filtering pre-loaded logs", "total_logs", len(f.loadedLogs), "query", query) |
| 94 | + |
| 95 | + var filtered []types.Log |
| 96 | + |
| 97 | + for _, log := range f.loadedLogs { |
| 98 | + // Filter by block range |
| 99 | + if query.FromBlock != nil && log.BlockNumber < query.FromBlock.Uint64() { |
| 100 | + continue |
| 101 | + } |
| 102 | + if query.ToBlock != nil && log.BlockNumber > query.ToBlock.Uint64() { |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + // Filter by addresses |
| 107 | + if len(query.Addresses) > 0 { |
| 108 | + addressMatch := false |
| 109 | + for _, addr := range query.Addresses { |
| 110 | + if log.Address == addr { |
| 111 | + addressMatch = true |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + if !addressMatch { |
| 116 | + continue |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Filter by topics |
| 121 | + if len(query.Topics) > 0 { |
| 122 | + topicMatch := true |
| 123 | + |
| 124 | + // We have a max of 4 topics in Ethereum logs |
| 125 | + for i := 0; i < len(query.Topics) && i < 4; i++ { |
| 126 | + // Skip if no filter for this topic position or empty filter array |
| 127 | + if i >= len(query.Topics) || len(query.Topics[i]) == 0 { |
| 128 | + continue |
| 129 | + } |
| 130 | + |
| 131 | + // If we're filtering for this topic position but log doesn't have enough topics |
| 132 | + if i >= len(log.Topics) { |
| 133 | + topicMatch = false |
| 134 | + break |
| 135 | + } |
| 136 | + |
| 137 | + // Check if this topic matches any in the filter array for this position |
| 138 | + hasMatch := false |
| 139 | + for _, topic := range query.Topics[i] { |
| 140 | + if log.Topics[i] == topic { |
| 141 | + hasMatch = true |
| 142 | + break |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if !hasMatch { |
| 147 | + topicMatch = false |
| 148 | + break |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if !topicMatch { |
| 153 | + continue |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // If we got here, log passed all filters |
| 158 | + filtered = append(filtered, log) |
| 159 | + } |
| 160 | + |
| 161 | + f.logger.Debug("filtered logs", "input_count", len(f.loadedLogs), "output_count", len(filtered)) |
| 162 | + return filtered, nil |
| 163 | +} |
| 164 | + |
| 165 | +func (f *SnapshotBlockHeightContractFilterer) BlockNumber(_ context.Context) (uint64, error) { |
| 166 | + return f.maxBlockHeight, nil |
| 167 | +} |
0 commit comments