-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add consumer block-stream hub #57
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
Open
swarna1101
wants to merge
15
commits into
main
Choose a base branch
from
feat/stream-hub-pr1
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.
Open
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
903943d
feat: add consumer block-stream hub
swarna1101 910f7bd
fix
swarna1101 02d6774
simplify
swarna1101 1d0e681
chore: refresh generated artifacts
5b31b69
fix
swarna1101 d0fbe01
Merge branch 'main' into feat/stream-hub-pr1
swarna1101 e7cff91
feat: add consumer stream auth seam (#61)
swarna1101 cf8da98
feat: add consumer block-stream ws transport (#65)
swarna1101 b9951b9
Merge branch 'main' into feat/stream-hub-pr1
swarna1101 64f0652
chore: refresh generated artifacts
9c3fbe5
feat: add consumer block-stream gRPC transport (#68)
swarna1101 9676938
chore: refresh generated artifacts
b0617dc
Merge branch 'main' into feat/stream-hub-pr1
swarna1101 41a4517
fix: align ADR to implementation
swarna1101 4b28093
docs: add consumer block-stream config and guide (#75)
swarna1101 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
Some comments aren't visible on the classic Files Changed page.
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
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,41 @@ | ||
| package gossipsub_gateway | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/getoptimum/optimum-gateway/pkg/entities" | ||
| "github.com/getoptimum/optimum-gateway/pkg/service/streamhub" | ||
| "github.com/getoptimum/optimum-gateway/pkg/test_utils" | ||
| ) | ||
|
|
||
| // A decoded beacon block is emitted to the hub with its metadata and raw bytes. | ||
| func TestProcessBeaconBlockArrivalEmitsToStreamHub(t *testing.T) { | ||
| svc, _ := newGateway(t) | ||
| hub := streamhub.New() | ||
| svc.streamHub = hub | ||
| sub := hub.Subscribe(4) | ||
|
|
||
| raw, err := hex.DecodeString(test_utils.HoodiBeaconBlockMessage1) | ||
| require.NoError(t, err) | ||
| topic := "/eth2/deadbeef/beacon_block/ssz_snappy" | ||
|
|
||
| slot, _ := svc.processBeaconBlockArrival(svc.log, topic, raw, time.Now().UnixMilli(), entities.SourceLibP2P, "", "peer-x") | ||
| require.Equal(t, uint64(3435697), slot) | ||
|
|
||
| select { | ||
| case ev := <-sub.Events(): | ||
| require.Equal(t, uint64(3435697), ev.Slot) | ||
| require.Equal(t, uint64(526417), ev.ProposerIndex) | ||
| require.Equal(t, entities.SourceLibP2P, ev.Source) | ||
| require.Equal(t, topic, ev.Topic) | ||
| require.Equal(t, "deadbeef", ev.ForkDigest) | ||
| require.Equal(t, raw, ev.Raw) | ||
| require.True(t, ev.Stale, "an old-slot fixture is flagged stale but still streamed") | ||
| default: | ||
| t.Fatal("expected a block event on the hub") | ||
| } | ||
| } |
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,25 @@ | ||
| // Package streamhub fans decoded beacon-block observations out to downstream | ||
| // consumers (ADR-0011). Emit is non-blocking: a slow consumer's oldest events | ||
| // are dropped rather than backpressuring the ingest goroutines. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| package streamhub | ||
|
|
||
| import "github.com/getoptimum/optimum-gateway/pkg/entities" | ||
|
|
||
| // BlockEvent is one beacon-block observation. It is produced once per source, | ||
| // so (Slot, ProposerIndex) is the block identity and Source tells the libp2p | ||
| // and mump2p views apart. Raw holds the verbatim ssz_snappy bytes for raw-mode | ||
| // consumers; metadata-mode transports omit it. | ||
| type BlockEvent struct { | ||
| Slot uint64 | ||
| ProposerIndex uint64 | ||
| ParentRoot []byte | ||
| StateRoot []byte | ||
| BlockSizeBytes uint64 | ||
| Topic string | ||
| Source entities.Source | ||
| ReceivedAtMs int64 | ||
| GatewayID string | ||
| ForkDigest string | ||
| Stale bool | ||
| Raw []byte | ||
| } | ||
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,97 @@ | ||
| package streamhub | ||
|
|
||
| import ( | ||
| "sync" | ||
| "sync/atomic" | ||
|
|
||
| "github.com/getoptimum/optimum-gateway/pkg/service/telemetry" | ||
| ) | ||
|
|
||
| // DefaultBufferSize is the per-subscriber ring depth used when Subscribe is | ||
| // given a non-positive size. | ||
| const DefaultBufferSize = 64 | ||
|
|
||
| // Hub broadcasts each BlockEvent to all subscribers. | ||
| type Hub struct { | ||
|
swarna1101 marked this conversation as resolved.
Outdated
|
||
| mu sync.RWMutex | ||
| subs map[*Subscription]struct{} | ||
|
swarna1101 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| func New() *Hub { | ||
| return &Hub{subs: make(map[*Subscription]struct{})} | ||
| } | ||
|
|
||
| // Subscribe registers a consumer with a bounded ring of bufSize events. The | ||
| // caller drains Events() and must Close() when done. | ||
| func (h *Hub) Subscribe(bufSize int) *Subscription { | ||
| if bufSize <= 0 { | ||
| bufSize = DefaultBufferSize | ||
| } | ||
| sub := &Subscription{hub: h, events: make(chan *BlockEvent, bufSize)} | ||
| h.mu.Lock() | ||
| h.subs[sub] = struct{}{} | ||
| h.mu.Unlock() | ||
| return sub | ||
| } | ||
|
|
||
| // Emit broadcasts ev without blocking; the event is shared read-only, so | ||
| // callers must not mutate it afterwards. | ||
| func (h *Hub) Emit(ev *BlockEvent) { | ||
| h.mu.RLock() | ||
| for sub := range h.subs { | ||
| sub.offer(ev) | ||
| } | ||
| h.mu.RUnlock() | ||
| } | ||
|
|
||
| // Subscription is one consumer's bounded, drop-oldest view of the stream. | ||
| type Subscription struct { | ||
| hub *Hub | ||
| events chan *BlockEvent | ||
| mu sync.Mutex // serializes concurrent emits' evict+send | ||
| dropped atomic.Uint64 | ||
| } | ||
|
|
||
| // Events is the read side of the ring; Close() closes it. | ||
| func (s *Subscription) Events() <-chan *BlockEvent { return s.events } | ||
|
|
||
| // Dropped is the cumulative count of events dropped on overflow. | ||
| func (s *Subscription) Dropped() uint64 { return s.dropped.Load() } | ||
|
|
||
| // Close unregisters the subscriber and closes its channel. Holding the hub | ||
| // write lock guarantees no Emit is mid-send when the channel closes. | ||
| func (s *Subscription) Close() { | ||
| s.hub.mu.Lock() | ||
| if _, ok := s.hub.subs[s]; ok { | ||
| delete(s.hub.subs, s) | ||
| close(s.events) | ||
| } | ||
| s.hub.mu.Unlock() | ||
| } | ||
|
|
||
| // offer enqueues ev, evicting the oldest event when the ring is full. | ||
| func (s *Subscription) offer(ev *BlockEvent) { | ||
|
swarna1101 marked this conversation as resolved.
Outdated
|
||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
|
|
||
| select { | ||
| case s.events <- ev: | ||
| return | ||
| default: | ||
| } | ||
| select { | ||
| case <-s.events: | ||
| s.recordDrop() | ||
| default: | ||
| } | ||
| select { | ||
| case s.events <- ev: | ||
| default: | ||
| s.recordDrop() | ||
| } | ||
| } | ||
|
|
||
| func (s *Subscription) recordDrop() { | ||
| s.dropped.Add(1) | ||
| telemetry.RecordStreamEventDropped() | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.