-
Notifications
You must be signed in to change notification settings - Fork 885
feat(autobahn): implement new lane ID for epoch (CON-358) #3862
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
wen-coding
wants to merge
10
commits into
main
Choose a base branch
from
wen/lane_id_in_epoch
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.
+1,659
−523
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d37679b
feat(autobahn): identify lanes as (validator, e_join) (CON-358)
wen-coding 7c89dbb
Merge branch 'main' into wen/lane_id_in_epoch
wen-coding af26d95
fix(autobahn): allowCreate API, waitLaneBound, restore QC tests
wen-coding 0fddc55
Merge branch 'wen/lane_id_in_epoch' of github.com:sei-protocol/sei-ch…
wen-coding c275fcf
docs(autobahn): succinct LaneID/avail lifecycle; drop waitLaneBound
wen-coding a4db9b8
fix(autobahn): capacity wait map presence; session scope.Run
wen-coding c130d1f
fix(autobahn): drop newInner head-gap skip
wen-coding 20f3aae
refactor(autobahn): document ordering; easy LaneID/committee nits
wen-coding dac0323
chore(autobahn): regenerate pb after lane_id source reorder
wen-coding 6d22fe7
refactor(autobahn): make LaneID a plain value with Joined field
wen-coding 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
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 |
|---|---|---|
|
|
@@ -22,8 +22,14 @@ func (s ImSlice[T]) At(i int) T { return s.s[i] } | |
| func (s ImSlice[T]) All() iter.Seq[T] { return slices.Values(s.s) } | ||
|
|
||
| // Committee represents the consensus committee. | ||
| // Lanes carry membership (validator + joined); weights are voting stake. | ||
| // | ||
| // Membership order is replica order (PublicKey.Compare). Leader/EvmShard and | ||
| // tipcut header concatenation walk that order. Lanes() follows the same order | ||
| // (one LaneID per replica); LaneID.Compare is for sorting lane lists elsewhere. | ||
| type Committee struct { | ||
| replicas ImSlice[PublicKey] | ||
| lanes ImSlice[LaneID] // in Replicas() order; one per member | ||
| byValidator map[PublicKey]LaneID | ||
| weights map[PublicKey]uint64 | ||
| totalWeight uint64 | ||
| } | ||
|
|
@@ -36,25 +42,42 @@ func (c *Committee) HasReplica(k PublicKey) bool { | |
| } | ||
|
|
||
| func (c *Committee) HasLane(l LaneID) bool { | ||
| _, ok := c.weights[l] | ||
| return ok | ||
| got, ok := c.byValidator[l.Validator] | ||
| return ok && got.Joined == l.Joined | ||
| } | ||
|
|
||
| func (c *Committee) Lane(v PublicKey) utils.Option[LaneID] { | ||
|
wen-coding marked this conversation as resolved.
|
||
| lane, ok := c.byValidator[v] | ||
| if !ok { | ||
| return utils.None[LaneID]() | ||
| } | ||
| return utils.Some(lane) | ||
| } | ||
|
|
||
| // Lanes is the list of nodes which are eligible to produce blocks. | ||
| func (c *Committee) Lanes() ImSlice[LaneID] { return c.replicas } | ||
| // Replicas yields validators in PublicKey order (membership order). | ||
| func (c *Committee) Replicas() iter.Seq[PublicKey] { | ||
| return func(yield func(PublicKey) bool) { | ||
| for lane := range c.lanes.All() { | ||
| if !yield(lane.Validator) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Replicas is the list of nodes which are eligible to participate in the consensus. | ||
| func (c *Committee) Replicas() ImSlice[PublicKey] { return c.replicas } | ||
| // Lanes returns each replica's LaneID in Replicas() order. | ||
| func (c *Committee) Lanes() ImSlice[LaneID] { return c.lanes } | ||
|
|
||
| // Deterministic random oracle selecting a replica with probability proportional to the weight. | ||
| // Walks membership (Replicas) order so seed → PublicKey is network-wide deterministic. | ||
| func (c *Committee) randomReplica(seed []byte) PublicKey { | ||
| h := sha256.Sum256(seed[:]) | ||
| var x, total uint256.Int | ||
| x.SetBytes32(h[:]) | ||
| total.SetUint64(c.totalWeight) | ||
| y := x.Mod(&x, &total).Uint64() | ||
| // TODO(gprusak): this can be optimized to O(1) lookup | ||
| for k := range c.replicas.All() { | ||
| for k := range c.Replicas() { | ||
| w := c.weights[k] | ||
| if y < w { | ||
| return k | ||
|
|
@@ -116,37 +139,83 @@ func (c *Committee) LaneQuorum() uint64 { | |
| return c.Faulty() + 1 | ||
| } | ||
|
|
||
| // NewCommittee is genesis: joined = 0 for every member. | ||
| func NewCommittee(weights map[PublicKey]uint64) (*Committee, error) { | ||
| weights, totalWeight, err := normalizeWeights(weights) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| lanes := make([]LaneID, 0, len(weights)) | ||
| for v := range weights { | ||
| lanes = append(lanes, NewLaneID(v, 0)) | ||
| } | ||
| return newCommittee(lanes, weights, totalWeight) | ||
| } | ||
|
|
||
| // DeriveNext builds the committee for epoch e>0 from this committee: | ||
| // copy joined on stay, stamp e on join. EpochIndex stays on Epoch, not Committee. | ||
| func (c *Committee) DeriveNext(weights map[PublicKey]uint64, e EpochIndex) (*Committee, error) { | ||
| if e == 0 { | ||
| return nil, errors.New("DeriveNext: epoch must be > 0") | ||
| } | ||
| weights, totalWeight, err := normalizeWeights(weights) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| lanes := make([]LaneID, 0, len(weights)) | ||
| for v := range weights { | ||
| lanes = append(lanes, c.Lane(v).Or(NewLaneID(v, e))) | ||
| } | ||
| return newCommittee(lanes, weights, totalWeight) | ||
| } | ||
|
|
||
| // normalizeWeights clones weights, drops zero entries, and returns the filtered | ||
| // map plus total stake. Errors on overflow or empty total. | ||
| func normalizeWeights(weights map[PublicKey]uint64) (map[PublicKey]uint64, uint64, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. document what "normalization" actually mean here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| weights = maps.Clone(weights) | ||
| totalWeight := uint64(0) | ||
| for k, w := range weights { | ||
| if w == 0 { | ||
| delete(weights, k) | ||
| continue | ||
| } | ||
| if utils.Max[uint64]()-totalWeight < w { | ||
| return nil, fmt.Errorf("total weight overflow") | ||
| return nil, 0, fmt.Errorf("total weight overflow") | ||
| } | ||
| totalWeight += w | ||
| } | ||
| if totalWeight == 0 { | ||
| return nil, errors.New("total weight is 0") | ||
| return nil, 0, errors.New("total weight is 0") | ||
| } | ||
| if len(weights) > MaxValidators { | ||
| return nil, fmt.Errorf("too many validators: got %d, want <= %d", len(weights), MaxValidators) | ||
| return nil, 0, fmt.Errorf("too many validators: got %d, want <= %d", len(weights), MaxValidators) | ||
| } | ||
| return weights, totalWeight, nil | ||
| } | ||
|
|
||
| // newCommittee rejects duplicate validators, orders replicas by PublicKey, | ||
| // and stores lanes in that same order (one LaneID per replica). | ||
| func newCommittee(lanes []LaneID, weights map[PublicKey]uint64, totalWeight uint64) (*Committee, error) { | ||
| byValidator := make(map[PublicKey]LaneID, len(lanes)) | ||
| for _, lane := range lanes { | ||
| if _, ok := byValidator[lane.Validator]; ok { | ||
| return nil, fmt.Errorf( | ||
| "duplicate validator in committee lanes: %q with joined %d and %d", | ||
| lane.Validator, byValidator[lane.Validator].Joined, lane.Joined, | ||
| ) | ||
| } | ||
| byValidator[lane.Validator] = lane | ||
| } | ||
| replicas := slices.Collect(maps.Keys(byValidator)) | ||
| slices.SortFunc(replicas, PublicKey.Compare) | ||
| ordered := make([]LaneID, len(replicas)) | ||
| for i, v := range replicas { | ||
| ordered[i] = byValidator[v] | ||
| } | ||
| replicas := slices.SortedFunc(maps.Keys(weights), func(a, b PublicKey) int { return a.Compare(b) }) | ||
| return &Committee{ | ||
| replicas: ImSlice[PublicKey]{replicas}, | ||
| lanes: ImSlice[LaneID]{ordered}, | ||
| byValidator: byValidator, | ||
| weights: weights, | ||
| totalWeight: totalWeight, | ||
| }, nil | ||
| } | ||
|
|
||
| // NewRoundRobinElection creates a Committee with equal weights for each replica. | ||
| func NewRoundRobinElection(replicas []PublicKey) (*Committee, error) { | ||
| weights := map[PublicKey]uint64{} | ||
| for _, k := range replicas { | ||
| weights[k] = 1 | ||
| } | ||
| return NewCommittee(weights) | ||
| } | ||
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,71 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" | ||
| "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require" | ||
| ) | ||
|
|
||
| func TestDeriveNext_StayLeaveRejoin(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| a := GenSecretKey(rng).Public() | ||
| b := GenSecretKey(rng).Public() | ||
| c := GenSecretKey(rng).Public() | ||
| d := GenSecretKey(rng).Public() | ||
|
|
||
| requireLanesSorted := func(t *testing.T, committee *Committee) { | ||
| t.Helper() | ||
| lanes := committee.Lanes() | ||
| for i := 1; i < lanes.Len(); i++ { | ||
| require.Less(t, lanes.At(i-1).Compare(lanes.At(i)), 0) | ||
| } | ||
| } | ||
|
|
||
| // Epoch 0: A,B,D join. | ||
| c0, err := NewCommittee(map[PublicKey]uint64{a: 1, b: 1, d: 1}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, NewLaneID(a, 0), c0.Lane(a).OrPanic("a")) | ||
| require.Equal(t, NewLaneID(b, 0), c0.Lane(b).OrPanic("b")) | ||
| require.Equal(t, NewLaneID(d, 0), c0.Lane(d).OrPanic("d")) | ||
| require.False(t, c0.HasLane(NewLaneID(c, 0))) | ||
| requireLanesSorted(t, c0) | ||
|
|
||
| // Epoch 1: A,B,D stay → copy joined=0. | ||
| c1, err := c0.DeriveNext(map[PublicKey]uint64{a: 1, b: 1, d: 1}, 1) | ||
| require.NoError(t, err) | ||
| require.Equal(t, NewLaneID(a, 0), c1.Lane(a).OrPanic("a")) | ||
| require.Equal(t, NewLaneID(b, 0), c1.Lane(b).OrPanic("b")) | ||
| require.Equal(t, NewLaneID(d, 0), c1.Lane(d).OrPanic("d")) | ||
| requireLanesSorted(t, c1) | ||
|
|
||
| // Epoch 2: B,D leave; C joins. A stays. | ||
| c2, err := c1.DeriveNext(map[PublicKey]uint64{a: 1, c: 1}, 2) | ||
| require.NoError(t, err) | ||
| require.Equal(t, NewLaneID(a, 0), c2.Lane(a).OrPanic("a")) | ||
| require.Equal(t, NewLaneID(c, 2), c2.Lane(c).OrPanic("c")) | ||
| require.False(t, c2.HasLane(NewLaneID(b, 0))) | ||
| require.False(t, c2.HasLane(NewLaneID(d, 0))) | ||
| require.False(t, c2.HasReplica(b)) | ||
| requireLanesSorted(t, c2) | ||
|
|
||
| // Epoch 3: D rejoins; C and A stay. | ||
| c3, err := c2.DeriveNext(map[PublicKey]uint64{a: 1, c: 1, d: 1}, 3) | ||
| require.NoError(t, err) | ||
| require.Equal(t, NewLaneID(a, 0), c3.Lane(a).OrPanic("a")) | ||
| require.Equal(t, NewLaneID(c, 2), c3.Lane(c).OrPanic("c")) | ||
| require.Equal(t, NewLaneID(d, 3), c3.Lane(d).OrPanic("d")) | ||
| require.False(t, c3.HasLane(NewLaneID(d, 0))) | ||
| requireLanesSorted(t, c3) | ||
| } | ||
|
|
||
| func TestFinalizeCommittee_RejectsDuplicatePubKeyDifferentJoined(t *testing.T) { | ||
| rng := utils.TestRng() | ||
| v := GenSecretKey(rng).Public() | ||
| _, err := newCommittee( | ||
| []LaneID{NewLaneID(v, 0), NewLaneID(v, 1)}, | ||
| map[PublicKey]uint64{v: 1}, | ||
| 1, | ||
| ) | ||
| require.Error(t, err) | ||
| } |
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.