-
Notifications
You must be signed in to change notification settings - Fork 165
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
V2 chunk assignment #736
Open
mooselumph
wants to merge
3
commits into
Layr-Labs:master
Choose a base branch
from
mooselumph:new-assignment
base: master
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.
Open
V2 chunk assignment #736
Changes from all commits
Commits
Show all changes
3 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 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 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,159 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"math/big" | ||
"sort" | ||
) | ||
|
||
type BlobParameters struct { | ||
CodingRate uint | ||
ReconstructionThreshold float64 | ||
NumChunks uint | ||
} | ||
|
||
func (p BlobParameters) MaxNumOperators() uint { | ||
|
||
return uint(math.Floor(float64(p.NumChunks) * (1 - 1/(p.ReconstructionThreshold*float64(p.CodingRate))))) | ||
|
||
} | ||
|
||
var ( | ||
ParametersMap = map[byte]BlobParameters{ | ||
0: {CodingRate: 8, ReconstructionThreshold: 0.22, NumChunks: 8192}, | ||
} | ||
) | ||
|
||
// AssignmentCoordinator is responsible for assigning chunks to operators in a way that satisfies the security | ||
// requirements of the protocol, as well as the constraints imposed by the specific blob version. | ||
type AssignmentCoordinatorV2 interface { | ||
|
||
// GetAssignments calculates the full set of node assignments | ||
GetAssignments(state *OperatorState, blobVersion byte, quorum QuorumID) (map[OperatorID]Assignment, error) | ||
|
||
// GetAssignment calculates the assignment for a specific operator | ||
GetAssignment(state *OperatorState, blobVersion byte, quorum QuorumID, id OperatorID) (Assignment, error) | ||
|
||
// GetChunkLength determines the length of a chunk given the blob version and blob length | ||
GetChunkLength(blobVersion byte, blobLength uint) (uint, error) | ||
} | ||
|
||
type StdAssignmentCoordinatorV2 struct { | ||
} | ||
|
||
var _ AssignmentCoordinatorV2 = (*StdAssignmentCoordinatorV2)(nil) | ||
|
||
func (c *StdAssignmentCoordinatorV2) GetAssignments(state *OperatorState, blobVersion byte, quorum QuorumID) (map[OperatorID]Assignment, error) { | ||
|
||
params, ok := ParametersMap[blobVersion] | ||
if !ok { | ||
return nil, fmt.Errorf("blob version %d not found", blobVersion) | ||
} | ||
|
||
ops, ok := state.Operators[quorum] | ||
if !ok { | ||
return nil, fmt.Errorf("no operators found for quorum %d", quorum) | ||
} | ||
|
||
if len(ops) > int(params.MaxNumOperators()) { | ||
return nil, fmt.Errorf("too many operators for blob version %d", blobVersion) | ||
} | ||
|
||
n := big.NewInt(int64(len(ops))) | ||
m := big.NewInt(int64(params.NumChunks)) | ||
|
||
type assignment struct { | ||
id OperatorID | ||
index uint | ||
chunks uint | ||
stake *big.Int | ||
} | ||
|
||
chunkAssignments := make([]assignment, 0, len(ops)) | ||
for ID, r := range state.Operators[quorum] { | ||
|
||
num := new(big.Int).Mul(r.Stake, new(big.Int).Sub(m, n)) | ||
denom := state.Totals[quorum].Stake | ||
|
||
chunks := roundUpDivideBig(num, denom) | ||
|
||
chunkAssignments = append(chunkAssignments, assignment{id: ID, index: r.Index, chunks: uint(chunks.Uint64()), stake: r.Stake}) | ||
} | ||
|
||
// Sort chunk decreasing by stake or operator ID in case of a tie | ||
sort.Slice(chunkAssignments, func(i, j int) bool { | ||
if chunkAssignments[i].stake.Cmp(chunkAssignments[j].stake) == 0 { | ||
return chunkAssignments[i].index < chunkAssignments[j].index | ||
} | ||
return chunkAssignments[i].stake.Cmp(chunkAssignments[j].stake) == 1 | ||
}) | ||
|
||
mp := 0 | ||
for _, a := range chunkAssignments { | ||
mp += int(a.chunks) | ||
} | ||
|
||
delta := int(params.NumChunks) - mp | ||
if delta < 0 { | ||
return nil, fmt.Errorf("total chunks %d exceeds maximum %d", mp, params.NumChunks) | ||
} | ||
|
||
assignments := make(map[OperatorID]Assignment, len(chunkAssignments)) | ||
index := uint(0) | ||
for i, a := range chunkAssignments { | ||
if i < delta { | ||
a.chunks++ | ||
} | ||
|
||
assignment := Assignment{ | ||
StartIndex: index, | ||
NumChunks: a.chunks, | ||
} | ||
|
||
assignments[a.id] = assignment | ||
index += a.chunks | ||
} | ||
|
||
return assignments, nil | ||
|
||
} | ||
|
||
func (c *StdAssignmentCoordinatorV2) GetAssignment(state *OperatorState, blobVersion byte, quorum QuorumID, id OperatorID) (Assignment, error) { | ||
|
||
assignments, err := c.GetAssignments(state, blobVersion, quorum) | ||
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. I'm not sure on the planned call patterns for these methods, so it's possible what I'm about to suggest is premature optimization. Would it make sense to cache the value of |
||
if err != nil { | ||
return Assignment{}, err | ||
} | ||
|
||
assignment, ok := assignments[id] | ||
if !ok { | ||
return Assignment{}, ErrNotFound | ||
} | ||
|
||
return assignment, nil | ||
} | ||
|
||
func (c *StdAssignmentCoordinatorV2) GetChunkLength(blobVersion byte, blobLength uint) (uint, error) { | ||
|
||
if blobLength == 0 { | ||
return 0, fmt.Errorf("blob length must be greater than 0") | ||
} | ||
|
||
// Check that the blob length is a power of 2 | ||
if blobLength&(blobLength-1) != 0 { | ||
return 0, fmt.Errorf("blob length %d is not a power of 2", blobLength) | ||
} | ||
|
||
if _, ok := ParametersMap[blobVersion]; !ok { | ||
return 0, fmt.Errorf("blob version %d not found", blobVersion) | ||
} | ||
|
||
chunkLength := blobLength * ParametersMap[blobVersion].CodingRate / ParametersMap[blobVersion].NumChunks | ||
if chunkLength == 0 { | ||
chunkLength = 1 | ||
} | ||
|
||
return chunkLength, nil | ||
|
||
} |
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.
Why do we need this struct (as opposed to having methods that don't operate on a struct)? Is the idea that we may end up having multiple algorithms, and we can toggle between which algorithm we use by using a different struct that implements
AssignmentCoordinatorV2
?