-
Notifications
You must be signed in to change notification settings - Fork 245
[WIP] feat: Subscribe to da events #2962
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
Draft
alpe
wants to merge
2
commits into
main
Choose a base branch
from
2003_gemini_3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ import ( | |
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "sort" | ||
| "sync" | ||
|
|
||
| "github.com/rs/zerolog" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
@@ -21,6 +23,7 @@ import ( | |
| // DARetriever defines the interface for retrieving events from the DA layer | ||
| type DARetriever interface { | ||
| RetrieveFromDA(ctx context.Context, daHeight uint64) ([]common.DAHeightEvent, error) | ||
| Subscribe(ctx context.Context, ch chan common.DAHeightEvent) error | ||
| } | ||
|
|
||
| // daRetriever handles DA retrieval operations for syncing | ||
|
|
@@ -34,6 +37,7 @@ type daRetriever struct { | |
| // on restart, will be refetch as da height is updated by syncer | ||
| pendingHeaders map[uint64]*types.SignedHeader | ||
| pendingData map[uint64]*types.Data | ||
| mu sync.Mutex | ||
|
|
||
| // strictMode indicates if the node has seen a valid DAHeaderEnvelope | ||
| // and should now reject all legacy/unsigned headers. | ||
|
|
@@ -75,6 +79,70 @@ func (r *daRetriever) RetrieveFromDA(ctx context.Context, daHeight uint64) ([]co | |
| return r.processBlobs(ctx, blobsResp.Data, daHeight), nil | ||
| } | ||
|
|
||
| // Subscribe subscribes to specific DA namespace | ||
| func (r *daRetriever) Subscribe(ctx context.Context, outCh chan common.DAHeightEvent) error { | ||
| subChHeader, err := r.client.Subscribe(ctx, r.client.GetHeaderNamespace()) | ||
| if err != nil { | ||
| return fmt.Errorf("subscribe to headers: %w", err) | ||
| } | ||
|
|
||
| var subChData <-chan datypes.ResultRetrieve | ||
| if !bytes.Equal(r.client.GetHeaderNamespace(), r.client.GetDataNamespace()) { | ||
| var err error | ||
| subChData, err = r.client.Subscribe(ctx, r.client.GetDataNamespace()) | ||
| if err != nil { | ||
| return fmt.Errorf("subscribe to data: %w", err) | ||
| } | ||
| } | ||
|
|
||
| go func() { | ||
| defer close(outCh) | ||
| for { | ||
| var blobs [][]byte | ||
| var height uint64 | ||
| var errCode datypes.StatusCode | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case res, ok := <-subChHeader: | ||
| if !ok { | ||
| return | ||
| } | ||
| blobs = res.Data | ||
| height = res.Height | ||
| errCode = res.Code | ||
| case res, ok := <-subChData: | ||
| if subChData == nil { | ||
| continue | ||
| } | ||
|
Comment on lines
+116
to
+118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if !ok { | ||
| return | ||
| } | ||
| blobs = res.Data | ||
| height = res.Height | ||
| errCode = res.Code | ||
| } | ||
|
|
||
| if errCode != datypes.StatusSuccess { | ||
| r.logger.Error().Uint64("code", uint64(errCode)).Msg("subscription error") | ||
| continue | ||
| } | ||
|
|
||
| events := r.processBlobs(ctx, blobs, height) | ||
| for _, ev := range events { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case outCh <- ev: | ||
| } | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // fetchBlobs retrieves blobs from both header and data namespaces | ||
| func (r *daRetriever) fetchBlobs(ctx context.Context, daHeight uint64) (datypes.ResultRetrieve, error) { | ||
| // Retrieve from both namespaces using the DA client | ||
|
|
@@ -150,6 +218,9 @@ func (r *daRetriever) validateBlobResponse(res datypes.ResultRetrieve, daHeight | |
|
|
||
| // processBlobs processes retrieved blobs to extract headers and data and returns height events | ||
| func (r *daRetriever) processBlobs(ctx context.Context, blobs [][]byte, daHeight uint64) []common.DAHeightEvent { | ||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
|
|
||
| // Decode all blobs | ||
| for _, bz := range blobs { | ||
| if len(bz) == 0 { | ||
|
|
@@ -212,18 +283,17 @@ func (r *daRetriever) processBlobs(ctx context.Context, blobs [][]byte, daHeight | |
| events = append(events, event) | ||
| } | ||
|
|
||
| // Sort events by height to match execution order | ||
| sort.Slice(events, func(i, j int) bool { | ||
| if events[i].DaHeight != events[j].DaHeight { | ||
| return events[i].DaHeight < events[j].DaHeight | ||
| } | ||
| return events[i].Header.Height() < events[j].Header.Height() | ||
| }) | ||
|
|
||
| if len(events) > 0 { | ||
| startHeight := events[0].Header.Height() | ||
| endHeight := events[0].Header.Height() | ||
| for _, event := range events { | ||
| h := event.Header.Height() | ||
| if h < startHeight { | ||
| startHeight = h | ||
| } | ||
| if h > endHeight { | ||
| endHeight = h | ||
| } | ||
| } | ||
| endHeight := events[len(events)-1].Header.Height() | ||
| r.logger.Info().Uint64("da_height", daHeight).Uint64("start_height", startHeight).Uint64("end_height", endHeight).Msg("processed blocks from DA") | ||
| } | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This goroutine has a potential to leak. The send operation on
outChat line 488 is blocking. If the consumer ofoutChis not reading from it, this goroutine will block indefinitely on the send, even if the context is canceled. Theselectstatement at line 466 will be stuck waiting foroutChto be ready for a send and won't be able to process the<-ctx.Done()case.To fix this, you should use a
selectstatement for sending tooutChto also handle context cancellation.