Documentation ¶
Overview ¶
Package shares contains the Share data structure.
Index ¶
- Constants
- Variables
- func AvailableBytesFromCompactShares(n int) int
- func AvailableBytesFromSparseShares(n int) int
- func CheckSubArray(txList [][]byte, subTxList [][]byte) bool
- func CompactSharesNeeded(sequenceLen int) (sharesNeeded int)
- func DelimLen(size uint64) int
- func ExtractShareIndexes(txs [][]byte) []uint32
- func GenerateRandomTxs(count, size int) [][]byte
- func GenerateRandomlySizedTxs(count, maxSize int) [][]byte
- func GetRandomSubSlice(size int) (start int, length int)
- func IsPowerOfTwo[I constraints.Integer](input I) bool
- func MarshalDelimitedTx(tx []byte) ([]byte, error)
- func NewReservedBytes(byteIndex uint32) ([]byte, error)
- func ParseBlobs(shares []Share) ([]*blob.Blob, error)
- func ParseDelimiter(input []byte) (inputWithoutLenDelimiter []byte, unitLen uint64, err error)
- func ParseReservedBytes(reservedBytes []byte) (uint32, error)
- func ParseTxs(shares []Share) ([][]byte, error)
- func RawTxSize(desiredSize int) int
- func RoundDownPowerOfTwo[I constraints.Integer](input I) (I, error)
- func RoundUpPowerOfTwo[I constraints.Integer](input I) I
- func RoundUpPowerOfTwoStrict[I constraints.Integer](input I) I
- func SparseSharesNeeded(sequenceLen uint32) (sharesNeeded int)
- func SplitTxs(txs [][]byte) (txShares []Share, pfbShares []Share, shareRanges map[[sha256.Size]byte]Range, ...)
- func ToBytes(shares []Share) (bytes [][]byte)
- type Builder
- func (b *Builder) AddData(rawData []byte) (rawDataLeftOver []byte)
- func (b *Builder) AvailableBytes() int
- func (b *Builder) Build() (*Share, error)
- func (b *Builder) FlipSequenceStart()
- func (b *Builder) ImportRawShare(rawBytes []byte) *Builder
- func (b *Builder) IsEmptyShare() bool
- func (b *Builder) MaybeWriteReservedBytes() error
- func (b *Builder) WriteSequenceLen(sequenceLen uint32) error
- func (b *Builder) ZeroPadIfNecessary() (bytesOfPadding int)
- type CompactShareCounter
- type CompactShareSplitter
- type InfoByte
- type Range
- type Share
- func FromBytes(bytes [][]byte) (shares []Share, err error)
- func NamespacePaddingShare(ns namespace.Namespace, shareVersion uint8) (Share, error)
- func NamespacePaddingShares(ns namespace.Namespace, shareVersion uint8, n int) ([]Share, error)
- func NewShare(data []byte) (*Share, error)
- func ReservedPaddingShare() Share
- func ReservedPaddingShares(n int) []Share
- func SplitBlobs(blobs ...*blob.Blob) ([]Share, error)
- func TailPaddingShare() Share
- func TailPaddingShares(n int) []Share
- func (s *Share) DoesSupportVersions(supportedShareVersions []uint8) error
- func (s *Share) InfoByte() (InfoByte, error)
- func (s Share) IsCompactShare() (bool, error)
- func (s *Share) IsPadding() (bool, error)
- func (s *Share) IsSequenceStart() (bool, error)
- func (s *Share) Len() int
- func (s *Share) Namespace() (namespace.Namespace, error)
- func (s *Share) RawData() (rawData []byte, err error)
- func (s *Share) RawDataUsingReserved() (rawData []byte, err error)
- func (s *Share) SequenceLen() (sequenceLen uint32, err error)
- func (s *Share) ToBytes() []byte
- func (s *Share) Validate() error
- func (s *Share) Version() (uint8, error)
- type ShareSequence
- type SparseShareSplitter
Constants ¶
const ( // byte contains the share version and a sequence start idicator. ShareInfoBytes = 1 // SequenceLenBytes is the number of bytes reserved for the sequence length // that is present in the first share of a sequence. SequenceLenBytes = 4 ShareVersionZero = uint8(0) // unsure of which version to use. DefaultShareVersion = ShareVersionZero // the first unit (transaction, ISR) in a compact share. // Deprecated: use ShareReservedBytes instead. CompactShareReservedBytes = ShareReservedBytes // the first unit (transaction, ISR) in a compact share. ShareReservedBytes = 4 // the first compact share of a sequence. FirstCompactShareContentSize = ShareSize - namespace.NamespaceSize - ShareInfoBytes - SequenceLenBytes - ShareReservedBytes // data in a continuation compact share of a sequence. ContinuationCompactShareContentSize = ShareSize - namespace.NamespaceSize - ShareInfoBytes - ShareReservedBytes // first sparse share of a sequence. FirstSparseShareContentSize = ShareSize - namespace.NamespaceSize - ShareInfoBytes - SequenceLenBytes // in a continuation sparse share of a sequence. ContinuationSparseShareContentSize = ShareSize - namespace.NamespaceSize - ShareInfoBytes // MinSquareSize is the smallest original square width. MinSquareSize = 1 // data square. MinShareCount = MinSquareSize * MinSquareSize MaxShareVersion = 127 )ShareSize = 512
Variables ¶
var ( ErrIncorrectNumberOfIndexes = errors.New( "number of indexes is not identical to the number of blobs", ) "the first blob started at an unexpected index", ) )
SupportedShareVersions is a list of supported share versions.
Functions ¶
func AvailableBytesFromCompactShares ¶
AvailableBytesFromCompactShares returns the maximum amount of bytes that could fit in `n` compact shares. Note that all compact shares are length prefixed. To account for this use `RawTxSize`.
func AvailableBytesFromSparseShares ¶
AvailableBytesFromSparseShares returns the maximum amount of bytes that could fit in `n` sparse shares
func CheckSubArray ¶
CheckSubArray returns whether subTxList is a subarray of txList
func CompactSharesNeeded ¶
CompactSharesNeeded returns the number of compact shares needed to store a sequence of length sequenceLen. The parameter sequenceLen is the number of bytes of transactions or intermediate state roots in a sequence.
func ExtractShareIndexes ¶
ExtractShareIndexes iterates over the transactions and extracts the share indexes from wrapped transactions. It returns nil if the transactions are from an old block that did not have share indexes in the wrapped txs.
func GenerateRandomTxs ¶
func GetRandomSubSlice ¶
GetRandomSubSlice returns two integers representing a randomly sized range in the interval [0, size]
func IsPowerOfTwo ¶
func IsPowerOfTwo[I constraints.Integer](input I) bool
IsPowerOfTwo returns true if input is a power of two.
func MarshalDelimitedTx ¶
MarshalDelimitedTx prefixes a transaction with the length of the transaction encoded as a varint.
func NewReservedBytes ¶
NewReservedBytes returns a byte slice of length ShareReservedBytes that contains the byteIndex of the first unit that starts in a compact share.
func ParseBlobs ¶
ParseBlobs collects all blobs from the shares provided
func ParseDelimiter ¶
ParseDelimiter attempts to parse a varint length delimiter from the input provided. It returns the input without the len delimiter bytes, the length parsed from the varint optionally an error. Unit length delimiters are used in compact shares where units (i.e. a transaction) are prefixed with a length delimiter that is encoded as a varint. Input should not contain the namespace ID or info byte of a share.
func ParseReservedBytes ¶
ParseReservedBytes parses a byte slice of length ShareReservedBytes into a byteIndex.
func RawTxSize ¶
RawTxSize returns the raw tx size that can be used to construct a tx of desiredSize bytes. This function is useful in tests to account for the length delimiter that is prefixed to a tx when it is converted into a compact share
func RoundDownPowerOfTwo ¶
func RoundDownPowerOfTwo[I constraints.Integer](input I) (I, error)
RoundDownPowerOfTwo returns the next power of two less than or equal to input.
func RoundUpPowerOfTwo ¶
func RoundUpPowerOfTwo[I constraints.Integer](input I) I
RoundUpPowerOfTwo returns the next power of two greater than or equal to input.
func RoundUpPowerOfTwoStrict ¶
func RoundUpPowerOfTwoStrict[I constraints.Integer](input I) I
RoundUpPowerOfTwoStrict returns the next power of two that is strictly greater than input.
func SparseSharesNeeded ¶
SparseSharesNeeded returns the number of shares needed to store a sequence of length sequenceLen.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
func NewBuilder ¶
NewBuilder returns a new share builder.
func NewEmptyBuilder ¶
func NewEmptyBuilder() *Builder
func (*Builder) AvailableBytes ¶
func (*Builder) FlipSequenceStart ¶
func (b *Builder) FlipSequenceStart()
FlipSequenceStart flips the sequence start indicator of the share provided
func (*Builder) ImportRawShare ¶
func (*Builder) IsEmptyShare ¶
IsEmptyShare returns true if no data has been written to the share
func (*Builder) MaybeWriteReservedBytes ¶
MaybeWriteReservedBytes will be a no-op if the reserved bytes have already been populated. If the reserved bytes are empty, it will write the location of the next unit of data to the reserved bytes.
func (*Builder) WriteSequenceLen ¶
WriteSequenceLen writes the sequence length to the first share.
func (*Builder) ZeroPadIfNecessary ¶
type CompactShareCounter ¶
type CompactShareCounter struct {
// contains filtered or unexported fields
}
func NewCompactShareCounter ¶
func NewCompactShareCounter() *CompactShareCounter
NewCompactShareCounter creates a new instance of a counter which calculates the amount of compact shares a set of data will be split into.
func (*CompactShareCounter) Add ¶
func (c *CompactShareCounter) Add(dataLen int) int
Add adds the length of the data to the counter and returns the amount of shares the counter has been increased by.
func (*CompactShareCounter) Remainder ¶
func (c *CompactShareCounter) Remainder() int
func (*CompactShareCounter) Revert ¶
func (c *CompactShareCounter) Revert()
Revert reverts the last Add operation. This can be called multiple times but only works the first time after an add operation.
func (*CompactShareCounter) Size ¶
func (c *CompactShareCounter) Size() int
Size returns the amount of shares the compact share counter has counted.
type CompactShareSplitter ¶
type CompactShareSplitter struct {
// contains filtered or unexported fields
}
CompactShareSplitter will write raw data compactly across a progressively increasing set of shares. It is used to lazily split block data such as transactions or intermediate state roots into shares.
func NewCompactShareSplitter ¶
func NewCompactShareSplitter(ns namespace.Namespace, shareVersion uint8) *CompactShareSplitter
NewCompactShareSplitter returns a CompactShareSplitter using the provided namespace and shareVersion.
func (*CompactShareSplitter) Count ¶
func (css *CompactShareSplitter) Count() int
Count returns the number of shares that would be made if `Export` was invoked on this compact share splitter.
func (*CompactShareSplitter) Export ¶
func (css *CompactShareSplitter) Export() ([]Share, error)
Export returns the underlying compact shares
func (*CompactShareSplitter) ShareRanges ¶
ShareRanges returns a map of share ranges to the corresponding tx keys. All share ranges in the map of shareRanges will be offset (i.e. incremented) by the shareRangeOffset provided. shareRangeOffset should be 0 for the first compact share sequence in the data square (transactions) but should be some non-zero number for subsequent compact share sequences (e.g. pfb txs).
func (*CompactShareSplitter) WriteTx ¶
func (css *CompactShareSplitter) WriteTx(tx []byte) error
WriteTx adds the delimited data for the provided tx to the underlying compact share splitter.
type InfoByte ¶
type InfoByte byte
InfoByte is a byte with the following structure: the first 7 bits are reserved for version information in big endian form (initially `0000000`). The last bit is a "sequence start indicator", that is `1` if this is the first share of a sequence and `0` if this is a continuation share.
func ParseInfoByte ¶
func (InfoByte) IsSequenceStart ¶
IsSequenceStart returns whether this share is the start of a sequence.
type Range ¶
type Range struct { // Start is the index of the first share occupied by this range. Start int // End is the next index after the last share occupied by this range. End int }
Range is an end exclusive set of share indexes.
func EmptyRange ¶
func EmptyRange() Range
func GetShareRangeForNamespace ¶
GetShareRangeForNamespace returns all shares that belong to a given namespace. It will return an empty range if the namespace could not be found. This assumes that the slice of shares are lexicographically sorted by namespace. Ranges here are always end exclusive.
type Share ¶
type Share struct {
// contains filtered or unexported fields
}
Share contains the raw share data (including namespace ID).
func NamespacePaddingShare ¶
NamespacePaddingShare returns a share that acts as padding. Namespace padding shares follow a blob so that the next blob may start at an index that conforms to blob share commitment rules. The ns and shareVersion parameters provided should be the namespace and shareVersion of the blob that precedes this padding in the data square.
func NamespacePaddingShares ¶
NamespacePaddingShares returns n namespace padding shares.
func ReservedPaddingShare ¶
func ReservedPaddingShare() Share
ReservedPaddingShare returns a share that acts as padding. Reserved padding shares follow all significant shares in the reserved namespace so that the first blob can start at an index that conforms to non-interactive default rules.
func ReservedPaddingShares ¶
ReservedPaddingShares returns n reserved padding shares.
func SplitBlobs ¶
SplitBlobs splits the provided blobs into shares.
func TailPaddingShare ¶
func TailPaddingShare() Share
TailPaddingShare is a share that is used to pad a data square to the desired square size. Tail padding shares follow the last blob share in the data square.
func TailPaddingShares ¶
TailPaddingShares returns n tail padding shares.
func (*Share) DoesSupportVersions ¶
func (Share) IsCompactShare ¶
IsCompactShare returns true if this is a compact share.
func (*Share) IsSequenceStart ¶
IsSequenceStart returns true if this is the first share in a sequence.
func (*Share) RawData ¶
RawData returns the raw share data. The raw share data does not contain the namespace ID, info byte, sequence length, or reserved bytes.
func (*Share) RawDataUsingReserved ¶
RawDataUsingReserved returns the raw share data while taking reserved bytes into account.
func (*Share) SequenceLen ¶
SequenceLen returns the sequence length of this *share and optionally an error. It returns 0, nil if this is a continuation share (i.e. doesn't contain a sequence length).
type ShareSequence ¶
type ShareSequence struct {}
ShareSequence represents a contiguous sequence of shares that are part of the same namespace and blob. For compact shares, one share sequence exists per reserved namespace. For sparse shares, one share sequence exists per blob.
func ParseShares ¶
func ParseShares(shares []Share, ignorePadding bool) ([]ShareSequence, error)
ParseShares parses the shares provided and returns a list of ShareSequences. If ignorePadding is true then the returned ShareSequences will not contain any padding sequences.
func (ShareSequence) RawData ¶
func (s ShareSequence) RawData() (data []byte, err error)
RawData returns the raw share data of this share sequence. The raw data does not contain the namespace ID, info byte, sequence length, or reserved bytes.
func (ShareSequence) SequenceLen ¶
func (s ShareSequence) SequenceLen() (uint32, error)
type SparseShareSplitter ¶
type SparseShareSplitter struct {
// contains filtered or unexported fields
}
SparseShareSplitter lazily splits blobs into shares that will eventually be included in a data square. It also has methods to help progressively count how many shares the blobs written take up.
func NewSparseShareSplitter ¶
func NewSparseShareSplitter() *SparseShareSplitter
func (*SparseShareSplitter) Count ¶
func (sss *SparseShareSplitter) Count() int
Count returns the current number of shares that will be made if exporting.
func (*SparseShareSplitter) Export ¶
func (sss *SparseShareSplitter) Export() []Share
Export finalizes and returns the underlying shares.
func (*SparseShareSplitter) Write ¶
func (sss *SparseShareSplitter) Write(blob *blob.Blob) error
Write writes the provided blob to this sparse share splitter. It returns an error or nil if no error is encountered.
func (*SparseShareSplitter) WriteNamespacePaddingShares ¶
func (sss *SparseShareSplitter) WriteNamespacePaddingShares(count int) error
WriteNamespacePaddingShares adds padding shares with the namespace of the last written share. This is useful to follow the non-interactive default rules. This function assumes that at least one share has already been written.