-
Notifications
You must be signed in to change notification settings - Fork 37
Fix DecisionsInView reset to zero during same-height sync #663
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
ivan-atme
wants to merge
3
commits into
hyperledger-labs:main
Choose a base branch
from
ivan-atme:fix-orderer-stuck
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.
+251
−0
Open
Changes from all commits
Commits
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,150 @@ | ||
| // Copyright IBM Corp. All Rights Reserved. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| package bft | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/hyperledger-labs/SmartBFT/pkg/types" | ||
| protos "github.com/hyperledger-labs/SmartBFT/smartbftprotos" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // stubSynchronizer returns a fixed SyncResponse. | ||
| type stubSynchronizer struct { | ||
| response types.SyncResponse | ||
| } | ||
|
|
||
| func (s *stubSynchronizer) Sync() types.SyncResponse { return s.response } | ||
|
|
||
| // stubComm is a no-op implementation of api.Comm. | ||
| type stubComm struct{} | ||
|
|
||
| func (s *stubComm) SendConsensus(targetID uint64, m *protos.Message) {} | ||
| func (s *stubComm) SendTransaction(targetID uint64, request []byte) {} | ||
| func (s *stubComm) Nodes() []uint64 { return []uint64{1, 2, 3, 4} } | ||
| func (s *stubComm) BroadcastConsensus(m *protos.Message) {} | ||
|
|
||
| // TestSyncDecisionsInView tests that the sync() method returns the correct | ||
| // DecisionsInView value in two scenarios: | ||
| // - same height: sync returns the same sequence as the controller, so | ||
| // newDecisionsInView must come from the controller's in-memory state | ||
| // (getCurrentDecisionsInView), not remain zero-initialized. | ||
| // - higher height: sync returns a higher sequence, so newDecisionsInView | ||
| // is overridden from the sync response metadata (+1). | ||
| func TestSyncDecisionsInView(t *testing.T) { | ||
| const ( | ||
| controllerSeq = uint64(2365908) | ||
| controllerView = uint64(782) | ||
| controllerDecisions = uint64(9578) | ||
| ) | ||
|
|
||
| // newController creates a minimal Controller with only the fields | ||
| // needed by sync(). The checkpoint metadata sets the controller's | ||
| // current sequence, and the synchronizer controls what sync returns. | ||
| newController := func(t *testing.T, syncResponse types.SyncResponse) *Controller { | ||
| t.Helper() | ||
|
|
||
| basicLog, err := zap.NewDevelopment() | ||
| assert.NoError(t, err) | ||
| log := basicLog.Sugar() | ||
|
|
||
| // Checkpoint metadata determines latestSeq() return value. | ||
| checkpoint := &types.Checkpoint{} | ||
| checkpoint.Set(types.Proposal{ | ||
| Metadata: MarshalOrPanic(&protos.ViewMetadata{ | ||
| LatestSequence: controllerSeq, | ||
| ViewId: controllerView, | ||
| DecisionsInView: controllerDecisions, | ||
| }), | ||
| }, nil) | ||
|
|
||
| // StateCollector with very short timeout so fetchState() returns nil | ||
| // without needing real consensus broadcast responses. | ||
| collector := &StateCollector{ | ||
| SelfID: 1, | ||
| N: 4, | ||
| Logger: log, | ||
| CollectTimeout: time.Millisecond, | ||
| } | ||
| collector.Start() | ||
| t.Cleanup(collector.Stop) | ||
|
|
||
| c := &Controller{ | ||
| ID: 1, | ||
| N: 4, | ||
| Logger: log, | ||
| Comm: &stubComm{}, | ||
| Synchronizer: &stubSynchronizer{response: syncResponse}, | ||
| Checkpoint: checkpoint, | ||
| Collector: collector, | ||
| InFlight: &InFlightData{}, | ||
| ViewChanger: &ViewChanger{}, | ||
| currViewNumber: controllerView, | ||
| currDecisionsInView: controllerDecisions + 1, // checkpoint stores N, decide() increments to N+1 | ||
| } | ||
| // sync() uses grabSyncToken/relinquishSyncToken which need syncChan. | ||
| c.syncChan = make(chan struct{}, 1) | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| t.Run("same_height_must_preserve_decisions", func(t *testing.T) { | ||
| // Synchronizer returns the same sequence as the controller. | ||
| // This simulates the "already at target height" scenario where | ||
| // the orderer is not behind but sync was triggered anyway. | ||
| c := newController(t, types.SyncResponse{ | ||
| Latest: types.Decision{ | ||
| Proposal: types.Proposal{ | ||
| Metadata: MarshalOrPanic(&protos.ViewMetadata{ | ||
| LatestSequence: controllerSeq, | ||
| ViewId: controllerView, | ||
| DecisionsInView: controllerDecisions, | ||
| }), | ||
| VerificationSequence: 0, | ||
| }, | ||
| }, | ||
| Reconfig: types.ReconfigSync{InReplicatedDecisions: false}, | ||
| }) | ||
|
|
||
| viewNum, seq, decisions := c.sync() | ||
|
|
||
| assert.Equal(t, controllerView, viewNum, "view number should be preserved") | ||
| assert.Equal(t, controllerSeq+1, seq, "proposal sequence should be controllerSeq+1") | ||
| // newDecisionsInView should be initialized from the controller's in-memory | ||
| // state (getCurrentDecisionsInView), which is controllerDecisions+1. | ||
| assert.Equal(t, controllerDecisions+1, decisions, | ||
| "DecisionsInView must be preserved when sync returns same height") | ||
| }) | ||
|
|
||
| t.Run("higher_height_overrides_decisions", func(t *testing.T) { | ||
| // Synchronizer returns a higher sequence than the controller. | ||
| // In this case newDecisionsInView is overridden from the sync response (+1). | ||
| syncDecisions := controllerDecisions + 1 | ||
| c := newController(t, types.SyncResponse{ | ||
| Latest: types.Decision{ | ||
| Proposal: types.Proposal{ | ||
| Metadata: MarshalOrPanic(&protos.ViewMetadata{ | ||
| LatestSequence: controllerSeq + 1, | ||
| ViewId: controllerView, | ||
| DecisionsInView: syncDecisions, | ||
| }), | ||
| VerificationSequence: 0, | ||
| }, | ||
| }, | ||
| Reconfig: types.ReconfigSync{InReplicatedDecisions: false}, | ||
| }) | ||
|
|
||
| viewNum, seq, decisions := c.sync() | ||
|
|
||
| assert.Equal(t, controllerView, viewNum, "view number should be preserved") | ||
| assert.Equal(t, controllerSeq+2, seq, "proposal sequence should be syncSeq+1") | ||
| assert.Equal(t, syncDecisions+1, decisions, | ||
| "DecisionsInView should be latestDecisionDecisions+1 when sync returns higher height") | ||
| }) | ||
| } | ||
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
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.
please also add a test in
test/basic_test.goThere 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 test should fail without the fix you are offering (and should pass with it)