-
Notifications
You must be signed in to change notification settings - Fork 1.3k
BlobSidecarsByRoot #12420
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
BlobSidecarsByRoot #12420
Changes from all commits
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 |
|---|---|---|
|
|
@@ -4,9 +4,13 @@ | |
| package types | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "sort" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ssz "github.com/prysmaticlabs/fastssz" | ||
| "github.com/prysmaticlabs/prysm/v4/config/params" | ||
| eth "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" | ||
| ) | ||
|
|
||
| const rootLength = 32 | ||
|
|
@@ -64,7 +68,7 @@ func (r *BeaconBlockByRootsReq) UnmarshalSSZ(buf []byte) error { | |
| bufLen := len(buf) | ||
| maxLength := int(params.BeaconNetworkConfig().MaxRequestBlocks * rootLength) | ||
| if bufLen > maxLength { | ||
| return errors.Errorf("expected buffer with length of upto %d but received length %d", maxLength, bufLen) | ||
| return errors.Errorf("expected buffer with length of up to %d but received length %d", maxLength, bufLen) | ||
| } | ||
| if bufLen%rootLength != 0 { | ||
| return ssz.ErrIncorrectByteSize | ||
|
|
@@ -120,3 +124,90 @@ func (m *ErrorMessage) UnmarshalSSZ(buf []byte) error { | |
| *m = errMsg | ||
| return nil | ||
| } | ||
|
|
||
| // BlobSidecarsByRootReq is used to specify a list of blob targets (root+index) in a BlobSidecarsByRoot RPC request. | ||
| type BlobSidecarsByRootReq []*eth.BlobIdentifier | ||
|
|
||
| // BlobIdentifier is a fixed size value, so we can compute its fixed size at start time (see init below) | ||
| var blobIdSize int | ||
|
|
||
| // SizeSSZ returns the size of the serialized representation. | ||
| func (b *BlobSidecarsByRootReq) SizeSSZ() int { | ||
| return len(*b) * blobIdSize | ||
| } | ||
|
|
||
| // MarshalSSZTo appends the serialized BlobSidecarsByRootReq value to the provided byte slice. | ||
| func (b *BlobSidecarsByRootReq) MarshalSSZTo(dst []byte) ([]byte, error) { | ||
| // A List without an enclosing container is marshaled exactly like a vector, no length offset required. | ||
| marshalledObj, err := b.MarshalSSZ() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return append(dst, marshalledObj...), nil | ||
| } | ||
|
|
||
| // MarshalSSZ serializes the BlobSidecarsByRootReq value to a byte slice. | ||
| func (b *BlobSidecarsByRootReq) MarshalSSZ() ([]byte, error) { | ||
| buf := make([]byte, len(*b)*blobIdSize) | ||
| for i, id := range *b { | ||
| by, err := id.MarshalSSZ() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| copy(buf[i*blobIdSize:(i+1)*blobIdSize], by) | ||
| } | ||
| return buf, nil | ||
| } | ||
|
|
||
| // UnmarshalSSZ unmarshals the provided bytes buffer into the | ||
| // BlobSidecarsByRootReq value. | ||
| func (b *BlobSidecarsByRootReq) UnmarshalSSZ(buf []byte) error { | ||
| bufLen := len(buf) | ||
| maxLength := int(params.BeaconNetworkConfig().MaxRequestBlobSidecars) * blobIdSize | ||
| if bufLen > maxLength { | ||
| return errors.Errorf("expected buffer with length of up to %d but received length %d", maxLength, bufLen) | ||
| } | ||
| if bufLen%blobIdSize != 0 { | ||
| return errors.Wrapf(ssz.ErrIncorrectByteSize, "size=%d", bufLen) | ||
| } | ||
| count := bufLen / blobIdSize | ||
| *b = make([]*eth.BlobIdentifier, count) | ||
| for i := 0; i < count; i++ { | ||
| id := ð.BlobIdentifier{} | ||
| err := id.UnmarshalSSZ(buf[i*blobIdSize : (i+1)*blobIdSize]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| (*b)[i] = id | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| var _ sort.Interface = BlobSidecarsByRootReq{} | ||
|
|
||
| // Less reports whether the element with index i must sort before the element with index j. | ||
| // BlobIdentifier will be sorted in lexicographic order by root, with Blob Index as tiebreaker for a given root. | ||
| func (s BlobSidecarsByRootReq) Less(i, j int) bool { | ||
| rootCmp := bytes.Compare(s[i].BlockRoot, s[j].BlockRoot) | ||
| if rootCmp != 0 { | ||
| // They aren't equal; return true if i < j, false if i > j. | ||
| return rootCmp < 0 | ||
| } | ||
| // They are equal; blob index is the tie breaker. | ||
| return s[i].Index < s[j].Index | ||
| } | ||
|
|
||
| // Swap swaps the elements with indexes i and j. | ||
| func (s BlobSidecarsByRootReq) Swap(i, j int) { | ||
| s[i], s[j] = s[j], s[i] | ||
| } | ||
|
|
||
| // Len is the number of elements in the collection. | ||
| func (s BlobSidecarsByRootReq) Len() int { | ||
| return len(s) | ||
| } | ||
|
|
||
| func init() { | ||
| sizer := ð.BlobIdentifier{} | ||
| blobIdSize = sizer.SizeSSZ() | ||
| } | ||
|
Comment on lines
+210
to
+213
Collaborator
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. should we put init() at the start of the file?
Collaborator
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. This is consistent with how I've use init elsewhere, always at the end of the file. It's not a strongly held opinion, but keeping it at the end would be consistent. My reasoning is that it's usually not the most interesting thing in the file, it only runs once to do basic initialization of values that tend to be declared at the top, and it is evaluated after everything else in the file (for instance, any other package scoped var declarations). |
||
Uh oh!
There was an error while loading. Please reload this page.