-
Notifications
You must be signed in to change notification settings - Fork 968
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shwap): Shwap add readFrom Writeto methods to shwap ids (#3605)
- Loading branch information
Showing
11 changed files
with
272 additions
and
6 deletions.
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
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 |
---|---|---|
@@ -1,35 +1,59 @@ | ||
package shwap | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/celestiaorg/go-libp2p-messenger/serde" | ||
|
||
"github.com/celestiaorg/celestia-node/share" | ||
"github.com/celestiaorg/celestia-node/share/shwap/pb" | ||
) | ||
|
||
// NamespacedData stores collections of RowNamespaceData, each representing shares and their proofs | ||
// within a namespace. | ||
type NamespacedData []RowNamespaceData | ||
|
||
// Flatten combines all shares from all rows within the namespace into a single slice. | ||
func (ns NamespacedData) Flatten() []share.Share { | ||
func (nd NamespacedData) Flatten() []share.Share { | ||
var shares []share.Share | ||
for _, row := range ns { | ||
for _, row := range nd { | ||
shares = append(shares, row.Shares...) | ||
} | ||
return shares | ||
} | ||
|
||
// Validate checks the integrity of the NamespacedData against a provided root and namespace. | ||
func (ns NamespacedData) Validate(root *share.AxisRoots, namespace share.Namespace) error { | ||
func (nd NamespacedData) Validate(root *share.AxisRoots, namespace share.Namespace) error { | ||
rowIdxs := share.RowsWithNamespace(root, namespace) | ||
if len(rowIdxs) != len(ns) { | ||
return fmt.Errorf("expected %d rows, found %d rows", len(rowIdxs), len(ns)) | ||
if len(rowIdxs) != len(nd) { | ||
return fmt.Errorf("expected %d rows, found %d rows", len(rowIdxs), len(nd)) | ||
} | ||
|
||
for i, row := range ns { | ||
for i, row := range nd { | ||
if err := row.Validate(root, namespace, rowIdxs[i]); err != nil { | ||
return fmt.Errorf("validating row: %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// ReadFrom reads the binary form of NamespacedData from the provided reader. | ||
func (nd *NamespacedData) ReadFrom(reader io.Reader) error { | ||
var data []RowNamespaceData | ||
for { | ||
var pbRow pb.RowNamespaceData | ||
_, err := serde.Read(reader, &pbRow) | ||
if err != nil { | ||
if errors.Is(err, io.EOF) { | ||
// all rows have been read | ||
*nd = data | ||
return nil | ||
} | ||
return err | ||
} | ||
row := RowNamespaceDataFromProto(&pbRow) | ||
data = append(data, row) | ||
} | ||
} |
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
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
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
Oops, something went wrong.