-
Notifications
You must be signed in to change notification settings - Fork 96
[P0] Make handling of warm keys deterministic #986
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
base: upgrade-v0.2.12
Are you sure you want to change the base?
Changes from 1 commit
8410fbb
ab44f95
36bc095
a334443
7b8f359
5f3e1ae
1e98450
1b54432
7b60578
fbf4922
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package host | ||
|
|
||
| import ( | ||
| "subnet/storage" | ||
| "subnet/types" | ||
| ) | ||
|
|
||
| // computeFinalizedNonce returns the highest nonce F such that >=2/3 of the | ||
| // session group (by slot count) has signed at nonce >= F. | ||
| // | ||
| // A slot that signed at nonce n is considered to have implicitly confirmed all | ||
| // nonces <= n by building on top of them. | ||
| func computeFinalizedNonce(store storage.Storage, escrowID string, latestNonce uint64, group []types.SlotAssignment) uint64 { | ||
| // confirmedBy[n] = bitmap of slots that have signed at nonce >= n. | ||
| // Bitmap128 is a value type (16 bytes); max group size is 128. | ||
| confirmedBy := make(map[uint64]types.Bitmap128) | ||
|
|
||
| for n := uint64(1); n <= latestNonce; n++ { | ||
| sigs, err := store.GetSignatures(escrowID, n) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| for slotID := range sigs { | ||
| // This slot signed at n, confirming all nonces 1..n. | ||
| for prev := uint64(1); prev <= n; prev++ { | ||
| bm := confirmedBy[prev] // zero value if absent | ||
| bm.Set(slotID) | ||
| confirmedBy[prev] = bm | ||
heitor-lassarote marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
||
|
|
||
| threshold := twoThirdsWeight(group) | ||
| for f := latestNonce; f > 0; f-- { | ||
| if bitmapSlotWeight(confirmedBy[f], group) >= threshold { | ||
| return f | ||
|
||
| } | ||
| } | ||
heitor-lassarote marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return 0 // warm-up period: not yet finalized | ||
| } | ||
|
|
||
| // bitmapSlotWeight counts the number of group slots whose bit is set in bm. | ||
| func bitmapSlotWeight(bm types.Bitmap128, group []types.SlotAssignment) uint32 { | ||
| var total uint32 | ||
| for _, sa := range group { | ||
| if bm.IsSet(sa.SlotID) { | ||
| total++ | ||
| } | ||
| } | ||
| return total | ||
| } | ||
|
|
||
| // twoThirdsWeight returns ceil(2/3 * totalSlots). | ||
| func twoThirdsWeight(group []types.SlotAssignment) uint32 { | ||
| total := uint32(len(group)) | ||
| if total == 0 { | ||
| return 0 | ||
| } | ||
heitor-lassarote marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return (total*2 + 2) / 3 | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
We should add log at error level here if
err != nilThere 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.
Fixed