-
Notifications
You must be signed in to change notification settings - Fork 383
feat: import batches from exported postage contract events log file #5094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
146d738
feat: import batches from exported postage contract events log file
martinconic 2d956ed
fix: linter errors and added some more checks
martinconic e33366e
fix: remove listener closer
martinconic 88f4498
feat: improve error handling and small refactorings
martinconic 9c2c4af
Merge branch 'master' into batches-snapshot
martinconic 46f5044
feat: improve parsing of logs and filter events
martinconic ee22ee0
fix: remove redundant code
martinconic 981f36a
feat: add new snapshot config option
martinconic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,153 @@ | ||
| // Copyright 2025 The Swarm Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package node | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "compress/gzip" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "slices" | ||
|
|
||
| "github.com/ethereum/go-ethereum" | ||
| "github.com/ethereum/go-ethereum/core/types" | ||
| archive "github.com/ethersphere/batch-archive" | ||
| "github.com/ethersphere/bee/v2/pkg/log" | ||
| ) | ||
|
|
||
| type SnapshotLogFilterer struct { | ||
martinconic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger log.Logger | ||
| loadedLogs []types.Log | ||
| maxBlockHeight uint64 | ||
| isLoaded bool | ||
| } | ||
|
|
||
| func NewSnapshotLogFilterer(logger log.Logger) (*SnapshotLogFilterer, error) { | ||
| f := &SnapshotLogFilterer{ | ||
| logger: logger, | ||
| } | ||
|
|
||
| if err := f.loadAndProcessSnapshot(); err != nil { | ||
martinconic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return nil, fmt.Errorf("failed to load and process snapshot during initialization: %w", err) | ||
| } | ||
|
|
||
| return f, nil | ||
| } | ||
|
|
||
| func (f *SnapshotLogFilterer) loadAndProcessSnapshot() error { | ||
| f.logger.Info("loading batch snapshot during construction") | ||
| data := archive.GetBatchSnapshot(true) | ||
martinconic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| dataReader := bytes.NewReader(data) | ||
| gzipReader, err := gzip.NewReader(dataReader) | ||
| if err != nil { | ||
| f.logger.Error(err, "failed to create gzip reader for batch import") | ||
| return fmt.Errorf("create gzip reader: %w", err) | ||
| } | ||
| defer gzipReader.Close() | ||
|
|
||
| if err := f.parseLogs(gzipReader); err != nil { | ||
| f.logger.Error(err, "failed to parse logs from snapshot") | ||
| return err | ||
| } | ||
|
|
||
| f.isLoaded = true | ||
| f.logger.Info("batch snapshot loaded successfully during construction", "log_count", len(f.loadedLogs), "max_block_height", f.maxBlockHeight) | ||
| return nil | ||
| } | ||
|
|
||
| func (f *SnapshotLogFilterer) parseLogs(reader io.Reader) error { | ||
| var parsedLogs []types.Log | ||
| var currentMaxBlockHeight uint64 | ||
| scanner := bufio.NewScanner(reader) | ||
|
|
||
| for scanner.Scan() { | ||
| line := scanner.Bytes() | ||
| if len(bytes.TrimSpace(line)) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| var logEntry types.Log | ||
| if err := json.Unmarshal(line, &logEntry); err != nil { | ||
| f.logger.Warning("failed to unmarshal log event, skipping line", "error", err, "line_snippet", string(line[:min(len(line), 100)])) | ||
| continue | ||
| } | ||
|
|
||
| if logEntry.BlockNumber > currentMaxBlockHeight { | ||
| currentMaxBlockHeight = logEntry.BlockNumber | ||
| } | ||
| parsedLogs = append(parsedLogs, logEntry) | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return fmt.Errorf("error scanning batch import data: %w", err) | ||
| } | ||
|
|
||
| f.loadedLogs = parsedLogs | ||
| f.maxBlockHeight = currentMaxBlockHeight | ||
| f.isLoaded = true | ||
| return nil | ||
| } | ||
|
|
||
| func (f *SnapshotLogFilterer) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) { | ||
martinconic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| f.logger.Debug("filtering pre-loaded logs", "total_logs", len(f.loadedLogs), "query", query) | ||
|
|
||
| filtered := make([]types.Log, 0, len(f.loadedLogs)) | ||
|
|
||
| for _, log := range f.loadedLogs { | ||
| if query.FromBlock != nil && log.BlockNumber < query.FromBlock.Uint64() { | ||
| continue | ||
| } | ||
| if query.ToBlock != nil && log.BlockNumber > query.ToBlock.Uint64() { | ||
| continue | ||
| } | ||
|
|
||
| if len(query.Addresses) > 0 { | ||
| addressMatch := slices.Contains(query.Addresses, log.Address) | ||
| if !addressMatch { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| if len(query.Topics) > 0 { | ||
| topicMatch := true | ||
|
|
||
| for i := 0; i < len(query.Topics) && i < 4; i++ { | ||
| if i >= len(query.Topics) || len(query.Topics[i]) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| if i >= len(log.Topics) { | ||
| topicMatch = false | ||
| break | ||
| } | ||
|
|
||
| hasMatch := slices.Contains(query.Topics[i], log.Topics[i]) | ||
|
|
||
| if !hasMatch { | ||
| topicMatch = false | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if !topicMatch { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| filtered = append(filtered, log) | ||
| } | ||
|
|
||
| f.logger.Debug("filtered logs", "input_count", len(f.loadedLogs), "output_count", len(filtered)) | ||
| return filtered, nil | ||
| } | ||
|
|
||
| func (f *SnapshotLogFilterer) BlockNumber(_ context.Context) (uint64, error) { | ||
martinconic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return f.maxBlockHeight, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.