-
Notifications
You must be signed in to change notification settings - Fork 886
(Autobahn) Share vote aggregation (CON-423) #4152
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
Merged
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
68 changes: 0 additions & 68 deletions
68
sei-tendermint/internal/autobahn/consensus/commit_votes.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package consensus | ||
|
|
||
| import ( | ||
| "github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types" | ||
| "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" | ||
| ) | ||
|
|
||
| type spv = *types.Signed[*types.PrepareVote] | ||
| type scv = *types.Signed[*types.CommitVote] | ||
| type hpv = types.Hash[*types.PrepareVote] | ||
| type hcv = types.Hash[*types.CommitVote] | ||
|
|
||
| type prepareVotes = phaseVotes[spv, hpv, *types.PrepareQC] | ||
| type commitVotes = phaseVotes[scv, hcv, *types.CommitQC] | ||
| type timeoutVotes = phaseVotes[*types.FullTimeoutVote, types.View, *types.TimeoutQC] | ||
|
|
||
| // votePhase is the wiring of one consensus phase: how a vote of that phase yields the | ||
| // aggregator's inputs, and how a quorum of such votes becomes that phase's QC. | ||
| type votePhase[V any, B comparable, QC any] struct { | ||
| key func(V) types.PublicKey | ||
| view func(V) types.View | ||
| bucket func(V) B | ||
| quorum func(*types.Committee) uint64 | ||
| qcView func(QC) types.View | ||
| newQC func([]V) QC | ||
| } | ||
|
|
||
| // phaseVotes holds the votes of one consensus phase and publishes the QC they form. | ||
| type phaseVotes[V any, B comparable, QC any] struct { | ||
| phase votePhase[V, B, QC] | ||
| votes *voteAggregator[V, B] | ||
| qc utils.AtomicSend[utils.Option[QC]] | ||
| } | ||
|
|
||
| func newPhaseVotes[V any, B comparable, QC any](phase votePhase[V, B, QC]) *phaseVotes[V, B, QC] { | ||
| return &phaseVotes[V, B, QC]{ | ||
| phase: phase, | ||
| votes: newVoteAggregator[V, B](), | ||
| qc: utils.NewAtomicSend(utils.None[QC]()), | ||
| } | ||
| } | ||
|
|
||
| // pushVerifiedVote inserts a vote the caller has already verified against c, publishing a QC | ||
| // once the vote completes a quorum at a view later than the last QC published. | ||
| func (p *phaseVotes[V, B, QC]) pushVerifiedVote(c *types.Committee, vote V) { | ||
| ph := p.phase | ||
| votes, ok := p.votes.pushVote(c, ph.key(vote), ph.view(vote), ph.bucket(vote), vote, ph.quorum(c)).Get() | ||
| if !ok { | ||
| return | ||
| } | ||
| // Construct a QC from the votes. | ||
| if old, ok := p.qc.Load().Get(); ok && !ph.qcView(old).Less(ph.view(vote)) { | ||
| return | ||
| } | ||
| p.qc.Store(utils.Some(ph.newQC(votes))) | ||
| } | ||
|
|
||
| // newPrepareVotes returns an empty prepare-phase vote aggregator. | ||
| func newPrepareVotes() *prepareVotes { | ||
| return newPhaseVotes(votePhase[spv, hpv, *types.PrepareQC]{ | ||
| key: func(v spv) types.PublicKey { return v.Key() }, | ||
| view: func(v spv) types.View { return v.Msg().Proposal().View() }, | ||
| bucket: func(v spv) hpv { return v.Hash() }, | ||
| quorum: (*types.Committee).PrepareQuorum, | ||
| qcView: func(qc *types.PrepareQC) types.View { return qc.Proposal().View() }, | ||
| newQC: types.NewPrepareQC, | ||
| }) | ||
| } | ||
|
|
||
| // newCommitVotes returns an empty commit-phase vote aggregator. | ||
| func newCommitVotes() *commitVotes { | ||
| return newPhaseVotes(votePhase[scv, hcv, *types.CommitQC]{ | ||
| key: func(v scv) types.PublicKey { return v.Key() }, | ||
| view: func(v scv) types.View { return v.Msg().Proposal().View() }, | ||
| bucket: func(v scv) hcv { return v.Hash() }, | ||
| quorum: (*types.Committee).CommitQuorum, | ||
| qcView: func(qc *types.CommitQC) types.View { return qc.Proposal().View() }, | ||
| newQC: types.NewCommitQC, | ||
| }) | ||
| } | ||
|
|
||
| // newTimeoutVotes returns an empty timeout-phase vote aggregator. | ||
| func newTimeoutVotes() *timeoutVotes { | ||
| return newPhaseVotes(votePhase[*types.FullTimeoutVote, types.View, *types.TimeoutQC]{ | ||
| key: func(v *types.FullTimeoutVote) types.PublicKey { return v.Vote().Key() }, | ||
| view: (*types.FullTimeoutVote).View, | ||
| bucket: (*types.FullTimeoutVote).View, | ||
| quorum: (*types.Committee).TimeoutQuorum, | ||
| qcView: (*types.TimeoutQC).View, | ||
| newQC: types.NewTimeoutQC, | ||
| }) | ||
| } | ||
158 changes: 158 additions & 0 deletions
158
sei-tendermint/internal/autobahn/consensus/phase_votes_test.go
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,158 @@ | ||
| package consensus | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types" | ||
| "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" | ||
| "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" | ||
| ) | ||
|
|
||
| func TestPrepareVotes_QuorumFormsQC(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| e := newVoteTestEnv(rng) | ||
| pv := newPrepareVotes() | ||
| proposal := types.GenProposalForEpoch(rng, e.ep, e.view) | ||
|
|
||
| for _, k := range e.quorum { | ||
| pv.pushVerifiedVote(e.ep.Committee(), types.Sign(k, types.NewPrepareVote(proposal))) | ||
| } | ||
| got, ok := pv.qc.Load().Get() | ||
| require.True(t, ok) | ||
| require.Equal(t, e.view, got.Proposal().View()) | ||
| require.NoError(t, got.Verify(e.ep)) | ||
| } | ||
|
|
||
| func TestPrepareVotes_DoesNotReplaceQCAtSameView(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| e := newVoteTestEnv(rng) | ||
| pv := newPrepareVotes() | ||
| view1 := e.view.Next() | ||
| proposal := types.GenProposalForEpoch(rng, e.ep, view1) | ||
|
|
||
| for _, k := range e.quorum { | ||
| pv.pushVerifiedVote(e.ep.Committee(), types.Sign(k, types.NewPrepareVote(proposal))) | ||
| } | ||
| before, ok := pv.qc.Load().Get() | ||
| require.True(t, ok) | ||
| require.Equal(t, view1, before.Proposal().View()) | ||
| pv.pushVerifiedVote(e.ep.Committee(), types.Sign(e.keys[len(e.quorum)], types.NewPrepareVote(proposal))) | ||
| after, ok := pv.qc.Load().Get() | ||
| require.True(t, ok) | ||
| require.True(t, before == after) | ||
| } | ||
|
|
||
| func TestPrepareVotes_ReplacesQCAtNewerView(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| e := newVoteTestEnv(rng) | ||
| pv := newPrepareVotes() | ||
| view1 := e.view.Next() | ||
| c := e.ep.Committee() | ||
|
|
||
| p0 := types.GenProposalForEpoch(rng, e.ep, e.view) | ||
| for _, k := range e.quorum { | ||
| pv.pushVerifiedVote(c, types.Sign(k, types.NewPrepareVote(p0))) | ||
| } | ||
| got, ok := pv.qc.Load().Get() | ||
| require.True(t, ok) | ||
| require.Equal(t, e.view, got.Proposal().View()) | ||
|
|
||
| p1 := types.GenProposalForEpoch(rng, e.ep, view1) | ||
| for _, k := range e.quorum { | ||
| pv.pushVerifiedVote(c, types.Sign(k, types.NewPrepareVote(p1))) | ||
| } | ||
| got, ok = pv.qc.Load().Get() | ||
| require.True(t, ok) | ||
| require.Equal(t, view1, got.Proposal().View()) | ||
| require.NoError(t, got.Verify(e.ep)) | ||
| } | ||
|
|
||
| // Prepare votes are bucketed by vote hash, so votes for conflicting proposals at the | ||
| // same view never combine into a QC. | ||
| func TestPrepareVotes_ConflictingProposalsDoNotFormQC(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| e := newVoteTestEnv(rng) | ||
| pv := newPrepareVotes() | ||
| first, second := e.splitBelowQuorum(t) | ||
| a := types.GenProposalForEpoch(rng, e.ep, e.view) | ||
| b := types.GenProposalForEpoch(rng, e.ep, e.view) | ||
|
|
||
| for _, k := range first { | ||
| pv.pushVerifiedVote(e.ep.Committee(), types.Sign(k, types.NewPrepareVote(a))) | ||
| } | ||
| for _, k := range second { | ||
| pv.pushVerifiedVote(e.ep.Committee(), types.Sign(k, types.NewPrepareVote(b))) | ||
| } | ||
| require.False(t, pv.qc.Load().IsPresent()) | ||
| } | ||
|
|
||
| func TestCommitVotes_QuorumFormsQC(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| e := newVoteTestEnv(rng) | ||
| cv := newCommitVotes() | ||
| proposal := types.GenProposalForEpoch(rng, e.ep, e.view) | ||
|
|
||
| for _, k := range e.quorum { | ||
| cv.pushVerifiedVote(e.ep.Committee(), types.Sign(k, types.NewCommitVote(proposal))) | ||
| } | ||
| got, ok := cv.qc.Load().Get() | ||
| require.True(t, ok) | ||
| require.Equal(t, e.view, got.Proposal().View()) | ||
| require.NoError(t, got.Verify(e.ep)) | ||
| } | ||
|
|
||
| // Commit votes are bucketed by vote hash, so votes for conflicting proposals at the same | ||
| // view never combine into a QC. | ||
| func TestCommitVotes_ConflictingProposalsDoNotFormQC(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| e := newVoteTestEnv(rng) | ||
| cv := newCommitVotes() | ||
| first, second := e.splitBelowQuorum(t) | ||
| a := types.GenProposalForEpoch(rng, e.ep, e.view) | ||
| b := types.GenProposalForEpoch(rng, e.ep, e.view) | ||
|
|
||
| for _, k := range first { | ||
| cv.pushVerifiedVote(e.ep.Committee(), types.Sign(k, types.NewCommitVote(a))) | ||
| } | ||
| for _, k := range second { | ||
| cv.pushVerifiedVote(e.ep.Committee(), types.Sign(k, types.NewCommitVote(b))) | ||
| } | ||
| require.False(t, cv.qc.Load().IsPresent()) | ||
| } | ||
|
|
||
| func TestTimeoutVotes_QuorumFormsQC(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| e := newVoteTestEnv(rng) | ||
| tv := newTimeoutVotes() | ||
| pqc := makePrepareQC(e.keys, types.GenProposalForEpoch(rng, e.ep, e.view)) | ||
|
|
||
| for _, k := range e.quorum { | ||
| tv.pushVerifiedVote(e.ep.Committee(), types.NewFullTimeoutVote(k, e.view, utils.Some(pqc))) | ||
| } | ||
| got, ok := tv.qc.Load().Get() | ||
| require.True(t, ok) | ||
| require.Equal(t, e.view, got.View()) | ||
| require.NoError(t, got.Verify(e.ep, utils.None[*types.CommitQC]())) | ||
| require.True(t, got.LatestPrepareQC().IsPresent()) | ||
| } | ||
|
|
||
| // Timeout votes are bucketed by view rather than by content, so a quorum still forms when | ||
| // signers report different prepare QCs. | ||
| func TestTimeoutVotes_DifferingPrepareQCsFormQC(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| e := newVoteTestEnv(rng) | ||
| tv := newTimeoutVotes() | ||
| pqc := makePrepareQC(e.keys, types.GenProposalForEpoch(rng, e.ep, e.view)) | ||
|
|
||
| for i, k := range e.quorum { | ||
| latest := utils.None[*types.PrepareQC]() | ||
| if i == 0 { | ||
| latest = utils.Some(pqc) | ||
| } | ||
| tv.pushVerifiedVote(e.ep.Committee(), types.NewFullTimeoutVote(k, e.view, latest)) | ||
| } | ||
| got, ok := tv.qc.Load().Get() | ||
| require.True(t, ok) | ||
| require.NoError(t, got.Verify(e.ep, utils.None[*types.CommitQC]())) | ||
| require.True(t, got.LatestPrepareQC().IsPresent()) | ||
| } |
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.
[suggestion] The branch where this guard passes — an already-published QC being replaced by one at a later view — is no longer covered.
TestTimeoutVotes_ReplacesOlderVoteAndAdvancesQCexercised exactly that (QC at view0, quorum migrates to view1, published QC becomes view1) and was deleted withtimeout_votes_test.go. Its replacement,TestVoteAggregator_ReplacesOlderVotesAndEmitsAtNewView, stops at the aggregator and never reachesphaseVotes; everyphaseVotestest either publishes the first QC fromNoneor asserts the same-view no-op.That leaves a liveness-critical property untested: a mis-wired
qcView(returning a view that never compares as less) would stall the published QC at its first view and still pass this suite. Restoring one phase-level test that pushes a quorum at view0, then a quorum atview0.Next(), and asserts the published QC advances would close it — and would let the PR's "existing tests pass unchanged" claim hold for the QC-publication logic, not just for the aggregator.