-
Notifications
You must be signed in to change notification settings - Fork 136
dkg/pedersen: buffer channels to not drop early bundles #4683
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
KaloyanTanev
wants to merge
2
commits into
main
Choose a base branch
from
kalo/dkg-buffer-early-bundles
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.
+266
−9
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| // Copyright © 2022-2026 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
|
||
| package pedersen | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/drand/kyber" | ||
| kdkg "github.com/drand/kyber/share/dkg" | ||
| "github.com/libp2p/go-libp2p/core/peer" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/obolnetwork/charon/cluster" | ||
| "github.com/obolnetwork/charon/testutil" | ||
| ) | ||
|
|
||
| // bundleKind abstracts delivering a bundle of one type via its p2p handler and | ||
| // receiving it from the corresponding incoming board channel. | ||
| type bundleKind struct { | ||
| name string | ||
| deliver func(ctx context.Context, t *testing.T, board *Board, from peer.ID, idx uint32, session, sig []byte) | ||
| receive func(t *testing.T, board *Board) (uint32, bool) | ||
| } | ||
|
|
||
| func bundleKinds() []bundleKind { | ||
| return []bundleKind{ | ||
| { | ||
| name: "deal", | ||
| deliver: func(ctx context.Context, t *testing.T, board *Board, from peer.ID, idx uint32, session, sig []byte) { | ||
| t.Helper() | ||
|
|
||
| protoBundle, err := DealBundleToProto(kdkg.DealBundle{ | ||
| DealerIndex: idx, | ||
| Deals: []kdkg.Deal{ | ||
| { | ||
| ShareIndex: 1, | ||
| EncryptedShare: []byte{1, 2, 3}, | ||
| }, | ||
| }, | ||
| Public: []kyber.Point{RandomPoint(t)}, | ||
| SessionID: session, | ||
| Signature: sig, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| _, _, err = board.handleDealBundleMessage(ctx, from, protoBundle) | ||
| require.NoError(t, err) | ||
| }, | ||
| receive: func(t *testing.T, board *Board) (uint32, bool) { | ||
| t.Helper() | ||
|
|
||
| select { | ||
| case bundle := <-board.IncomingDeal(): | ||
| return bundle.DealerIndex, true | ||
| case <-time.After(time.Second): | ||
| return 0, false | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "response", | ||
| deliver: func(ctx context.Context, t *testing.T, board *Board, from peer.ID, idx uint32, session, sig []byte) { | ||
| t.Helper() | ||
|
|
||
| protoBundle, err := ResponseBundleToProto(kdkg.ResponseBundle{ | ||
| ShareIndex: idx, | ||
| Responses: []kdkg.Response{ | ||
| { | ||
| DealerIndex: 1, | ||
| Status: true, | ||
| }, | ||
| }, | ||
| SessionID: session, | ||
| Signature: sig, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| _, _, err = board.handleResponseBundleMessage(ctx, from, protoBundle) | ||
| require.NoError(t, err) | ||
| }, | ||
| receive: func(t *testing.T, board *Board) (uint32, bool) { | ||
| t.Helper() | ||
|
|
||
| select { | ||
| case bundle := <-board.IncomingResponse(): | ||
| return bundle.ShareIndex, true | ||
| case <-time.After(time.Second): | ||
| return 0, false | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| name: "justification", | ||
| deliver: func(ctx context.Context, t *testing.T, board *Board, from peer.ID, idx uint32, session, sig []byte) { | ||
| t.Helper() | ||
|
|
||
| protoBundle, err := JustificationBundleToProto(kdkg.JustificationBundle{ | ||
| DealerIndex: idx, | ||
| Justifications: []kdkg.Justification{ | ||
| { | ||
| ShareIndex: 1, | ||
| Share: RandomScalar(t), | ||
| }, | ||
| }, | ||
| SessionID: session, | ||
| Signature: sig, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| _, _, err = board.handleJustificationBundleMessage(ctx, from, protoBundle) | ||
| require.NoError(t, err) | ||
| }, | ||
| receive: func(t *testing.T, board *Board) (uint32, bool) { | ||
| t.Helper() | ||
|
|
||
| select { | ||
| case bundle := <-board.IncomingJustification(): | ||
| return bundle.DealerIndex, true | ||
| case <-time.After(time.Second): | ||
| return 0, false | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // TestBoardBuffersEarlyBundles verifies that bundles arriving before the kyber | ||
| // protocol starts reading the incoming board channels are buffered and delivered | ||
| // later, instead of being dropped when the p2p request context expires. This | ||
| // happens when peers start the DKG a few seconds earlier than this node. | ||
| func TestBoardBuffersEarlyBundles(t *testing.T) { | ||
| const ( | ||
| numNodes = 4 | ||
| threshold = 3 | ||
| ) | ||
|
|
||
| for _, kind := range bundleKinds() { | ||
| t.Run(kind.name, func(t *testing.T) { | ||
| board, nodes, session := newTestBoard(t, numNodes, threshold) | ||
|
|
||
| // Deliver bundles from all other peers before anything reads the | ||
| // incoming channel. Each handler call gets a short-lived context, | ||
| // like an expiring p2p request context. | ||
| for i := 1; i < numNodes; i++ { | ||
| ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond) | ||
| kind.deliver(ctx, t, board, nodes[i].NodeHost.ID(), uint32(i), session, []byte{byte(i)}) | ||
| cancel() | ||
| } | ||
|
|
||
| // All early bundles must be delivered once the protocol starts reading. | ||
| received := make(map[uint32]bool) | ||
|
|
||
| for i := 1; i < numNodes; i++ { | ||
| idx, ok := kind.receive(t, board) | ||
| if !ok { | ||
| require.Failf(t, "early bundle dropped", "received %d of %d bundles", len(received), numNodes-1) | ||
| } | ||
|
|
||
| received[idx] = true | ||
| } | ||
|
|
||
| require.Len(t, received, numNodes-1) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestBoardAcceptsRedeliveryAfterDrop verifies that a bundle dropped because the | ||
| // request context expired is not recorded as a duplicate, so a redelivery of the | ||
| // same bundle is accepted. | ||
| func TestBoardAcceptsRedeliveryAfterDrop(t *testing.T) { | ||
| const ( | ||
| numNodes = 4 | ||
| threshold = 3 | ||
| ) | ||
|
|
||
| for _, kind := range bundleKinds() { | ||
| t.Run(kind.name, func(t *testing.T) { | ||
| board, nodes, session := newTestBoard(t, numNodes, threshold) | ||
| from := nodes[1].NodeHost.ID() | ||
|
|
||
| // Fill the channel buffer plus the forwarder's in-flight slot so the | ||
| // next delivery blocks and gets dropped. | ||
| total := numNodes + 1 | ||
| for i := range total { | ||
| ctx, cancel := context.WithTimeout(t.Context(), time.Second) | ||
| kind.deliver(ctx, t, board, from, uint32(i), session, []byte{byte(i)}) | ||
| cancel() | ||
| } | ||
|
|
||
| // This bundle is dropped since nothing is reading yet and the buffer is full. | ||
| droppedIdx := uint32(total) | ||
| droppedSig := []byte{byte(total)} | ||
|
|
||
| ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond) | ||
| kind.deliver(ctx, t, board, from, droppedIdx, session, droppedSig) | ||
| cancel() | ||
|
|
||
| // Drain the buffered bundles. | ||
| for range total { | ||
| _, ok := kind.receive(t, board) | ||
| require.True(t, ok, "buffered bundle not delivered") | ||
| } | ||
|
|
||
| // Redeliver the dropped bundle. It must not be refused as a duplicate. | ||
| ctx, cancel = context.WithTimeout(t.Context(), time.Second) | ||
| defer cancel() | ||
|
|
||
| kind.deliver(ctx, t, board, from, droppedIdx, session, droppedSig) | ||
|
|
||
| idx, ok := kind.receive(t, board) | ||
| require.True(t, ok, "redelivered bundle refused as duplicate") | ||
| require.Equal(t, droppedIdx, idx) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func newTestBoard(t *testing.T, numNodes, threshold int) (*Board, []*TestNode, []byte) { | ||
| t.Helper() | ||
|
|
||
| var ( | ||
| peers []peer.ID | ||
| peerMap = make(map[peer.ID]cluster.NodeIdx) | ||
| session = testutil.RandomArray32() | ||
| ) | ||
|
|
||
| nodes := make([]*TestNode, numNodes) | ||
| for i := range numNodes { | ||
| nodes[i] = NewTestNode(t, i) | ||
| peerMap[nodes[i].NodeHost.ID()] = nodes[i].NodeIdx | ||
| peers = append(peers, nodes[i].NodeHost.ID()) | ||
| } | ||
|
|
||
| nodes[0].InitBoard(t, threshold, peers, peerMap, session[:]) | ||
|
|
||
| return nodes[0].Board, nodes, session[:] | ||
| } |
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.