-
Notifications
You must be signed in to change notification settings - Fork 2
test: cover the ADR-0012 accelerate gate on the block path #97
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
swarna1101
merged 5 commits into
feat/adr-0012-accelerate-slots
from
test/adr-0012-accelerate-gate
Aug 27, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ec1bb6
test: cover the ADR-0012 accelerate gate on the block path
CryptoFewka fa787b4
test: address review of the accelerate gate test
CryptoFewka 5fd32e8
config: seed derived state in InitDerived, not Validate
CryptoFewka 9aa6310
test: make the withholding assertion order-independent
CryptoFewka 4b68446
fix
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package gossipsub_gateway | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/binary" | ||
| "encoding/hex" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/golang/snappy" | ||
| "github.com/libp2p/go-libp2p" | ||
| libp2ppubsub "github.com/libp2p/go-libp2p-pubsub" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| commonentities "github.com/getoptimum/optimum-common/pkg/entities" | ||
| chainstate "github.com/getoptimum/optimum-gateway/pkg/protocol/chain_state" | ||
| "github.com/getoptimum/optimum-gateway/pkg/protocol/consensus" | ||
| "github.com/getoptimum/optimum-gateway/pkg/service/streamhub" | ||
| "github.com/getoptimum/optimum-gateway/pkg/test_utils" | ||
| "github.com/getoptimum/optimum-gateway/pkg/utils" | ||
| ) | ||
|
|
||
| // blockAtSlot rewrites the gossip block's slot; DecodeBeaconBlockHeader must read it back. | ||
| func blockAtSlot(t *testing.T, hexBlock string, slot uint64) []byte { | ||
| t.Helper() | ||
| raw, err := hex.DecodeString(hexBlock) | ||
| require.NoError(t, err) | ||
| ssz, err := utils.DecodeSnappy(raw, utils.MaxGossipPayloadSize) | ||
| require.NoError(t, err) | ||
| off := 4 + 96 // SSZ prefix + BLS signature | ||
| require.GreaterOrEqual(t, len(ssz), off+8, "fixture is too short to hold a slot") | ||
| binary.LittleEndian.PutUint64(ssz[off:off+8], slot) | ||
| encoded := snappy.Encode(nil, ssz) | ||
| hdr, err := consensus.DecodeBeaconBlockHeader(encoded) | ||
| require.NoError(t, err) | ||
| require.Equal(t, slot, hdr.Header.Slot, "slot rewrite landed at the wrong offset") | ||
| return encoded | ||
| } | ||
|
|
||
| // joinCLTopic subscribes to a real gossipsub topic so CL publishes can be read back. | ||
| func joinCLTopic(t *testing.T, svc *Service, topic string) *libp2ppubsub.Subscription { | ||
| t.Helper() | ||
| h, err := libp2p.New(libp2p.NoListenAddrs) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = h.Close() }) | ||
| ps, err := libp2ppubsub.NewGossipSub(t.Context(), h) | ||
| require.NoError(t, err) | ||
| tp, err := ps.Join(topic) | ||
| require.NoError(t, err) | ||
| sub, err := tp.Subscribe() | ||
| require.NoError(t, err) | ||
| t.Cleanup(sub.Cancel) | ||
| svc.libP2PTopics.Store(topic, tp) | ||
| return sub | ||
| } | ||
|
|
||
| // Unselected slot is withheld from the CL; both blocks are still streamed (gate is after arrival). | ||
| func TestMumP2PBeaconBlockAccelerateGate(t *testing.T) { | ||
| svc, bootstrap := newGateway(t) | ||
| topic := "/eth2/deadbeef/beacon_block/ssz_snappy" | ||
| clSub := joinCLTopic(t, svc, topic) | ||
| hub := streamhub.New() | ||
| svc.streamHub = hub | ||
| sub := hub.Subscribe(4) | ||
| t.Cleanup(sub.Close) | ||
| t.Cleanup(svc.messagesMap.Close) | ||
|
|
||
| cur := chainstate.CurrentSlot(time.Now()) | ||
| bootstrap.SetAccelerateResponse(map[string]any{ | ||
| "to_slot": cur + 10, | ||
| "slots": []int64{int64(cur)}, | ||
| "generated_at_ms": 1, | ||
| }) | ||
| svc.srvMsgRouter.RefreshAccelerateSlots(t.Context()) | ||
|
|
||
| fixture := test_utils.HoodiBeaconBlockMessage1 | ||
| svc.processMumP2PMessage(svc.log, &commonentities.P2PMessage{ | ||
| SourceNodeID: "peer-1", Topic: topic, Message: blockAtSlot(t, fixture, cur+1), | ||
| }) | ||
| svc.processMumP2PMessage(svc.log, &commonentities.P2PMessage{ | ||
| SourceNodeID: "peer-1", Topic: topic, Message: blockAtSlot(t, fixture, cur), | ||
| }) | ||
|
swarna1101 marked this conversation as resolved.
|
||
|
|
||
| ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) | ||
| defer cancel() | ||
| got, err := clSub.Next(ctx) | ||
| require.NoError(t, err, "on-list slot must reach the CL") | ||
| delivered, err := consensus.DecodeBeaconBlockHeader(got.Data) | ||
| require.NoError(t, err) | ||
| require.Equal(t, cur, delivered.Header.Slot, "examined but unselected slot must be withheld from the CL") | ||
|
|
||
| // Covers gossipsub delivering the two publishes in either order. | ||
| idle, cancelIdle := context.WithTimeout(t.Context(), 250*time.Millisecond) | ||
| defer cancelIdle() | ||
| _, err = clSub.Next(idle) | ||
| require.ErrorIs(t, err, context.DeadlineExceeded, "only the on-list slot may reach the CL") | ||
|
|
||
| require.Len(t, sub.Events(), 2, "both blocks are streamed regardless of the verdict") | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
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 was deleted.
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
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
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.