Skip to content

Commit

Permalink
update with latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed May 24, 2024
1 parent 7d3f5a7 commit ceb8b87
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 74 deletions.
66 changes: 66 additions & 0 deletions share/datahash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package share

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"

"github.com/celestiaorg/rsmt2d"
)

// DataHash is a representation of the Root hash.
type DataHash []byte

func (dh DataHash) Validate() error {
if len(dh) != 32 {
return fmt.Errorf("invalid hash size, expected 32, got %d", len(dh))
}
return nil
}

func (dh DataHash) String() string {
return fmt.Sprintf("%X", []byte(dh))
}

// IsEmptyRoot check whether DataHash corresponds to the root of an empty block EDS.
func (dh DataHash) IsEmptyRoot() bool {
return bytes.Equal(EmptyRoot().Hash(), dh)
}

// MustDataHashFromString converts a hex string to a valid datahash.
func MustDataHashFromString(datahash string) DataHash {
dh, err := hex.DecodeString(datahash)
if err != nil {
panic(fmt.Sprintf("datahash conversion: passed string was not valid hex: %s", datahash))
}
err = DataHash(dh).Validate()
if err != nil {
panic(fmt.Sprintf("datahash validation: passed hex string failed: %s", err))
}
return dh
}

// NewSHA256Hasher returns a new instance of a SHA-256 hasher.
func NewSHA256Hasher() hash.Hash {
return sha256.New()
}

// RootHashForCoordinates returns the root hash for the given coordinates.
func RootHashForCoordinates(r *Root, axisType rsmt2d.Axis, rowIdx, colIdx uint) []byte {
if axisType == rsmt2d.Row {
return r.RowRoots[rowIdx]
}
return r.ColumnRoots[colIdx]
}

// RowsWithNamespace returns indexes of rows, that might contain namespace.
func RowsWithNamespace(root *Root, namespace Namespace) (idxs []int) {
for i, row := range root.RowRoots {
if !namespace.IsOutsideRange(row, row) {
idxs = append(idxs, i)
}
}
return idxs
}
10 changes: 5 additions & 5 deletions share/eds/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ func CollectSharesByNamespace(
utils.SetStatusAndEnd(span, err)
}()

rowRoots, _, _ := share.FilterRootByNamespace(root, namespace)
if len(rowRoots) == 0 {
rowIdxs := share.RowsWithNamespace(root, namespace)
if len(rowIdxs) == 0 {
return []share.NamespacedRow{}, nil
}

errGroup, ctx := errgroup.WithContext(ctx)
shares = make([]share.NamespacedRow, len(rowRoots))
for i, rowRoot := range rowRoots {
shares = make([]share.NamespacedRow, len(rowIdxs))
for i, rowIdx := range rowIdxs {
// shadow loop variables, to ensure correct values are captured
i, rowRoot := i, rowRoot
i, rowRoot := i, root.RowRoots[rowIdx]
errGroup.Go(func() error {
row, proof, err := ipld.GetSharesByNamespace(ctx, bg, rowRoot, namespace, len(root.RowRoots))
shares[i] = share.NamespacedRow{
Expand Down
48 changes: 0 additions & 48 deletions share/share.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package share

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/nmt"
Expand Down Expand Up @@ -72,48 +69,3 @@ func (s *ShareWithProof) Validate(rootHash []byte, x, y, edsSize int) bool {
rootHash,
)
}

// DataHash is a representation of the Root hash.
type DataHash []byte

func (dh DataHash) Validate() error {
if len(dh) != 32 {
return fmt.Errorf("invalid hash size, expected 32, got %d", len(dh))
}
return nil
}

func (dh DataHash) String() string {
return fmt.Sprintf("%X", []byte(dh))
}

// IsEmptyRoot check whether DataHash corresponds to the root of an empty block EDS.
func (dh DataHash) IsEmptyRoot() bool {
return bytes.Equal(EmptyRoot().Hash(), dh)
}

// MustDataHashFromString converts a hex string to a valid datahash.
func MustDataHashFromString(datahash string) DataHash {
dh, err := hex.DecodeString(datahash)
if err != nil {
panic(fmt.Sprintf("datahash conversion: passed string was not valid hex: %s", datahash))
}
err = DataHash(dh).Validate()
if err != nil {
panic(fmt.Sprintf("datahash validation: passed hex string failed: %s", err))
}
return dh
}

// NewSHA256Hasher returns a new instance of a SHA-256 hasher.
func NewSHA256Hasher() hash.Hash {
return sha256.New()
}

// RootHashForCoordinates returns the root hash for the given coordinates.
func RootHashForCoordinates(r *Root, axisType rsmt2d.Axis, rowIdx, colIdx uint) []byte {
if axisType == rsmt2d.Row {
return r.RowRoots[rowIdx]
}
return r.ColumnRoots[colIdx]
}
29 changes: 14 additions & 15 deletions share/shwap/namespaced_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (
// within a namespace.
type NamespacedData []RowNamespaceData

// RowNamespaceData holds shares and their corresponding proof for a single row within a namespace.
type RowNamespaceData struct {
Shares []share.Share `json:"shares"` // Shares within the namespace.
Proof *nmt.Proof `json:"proof"` // Proof of the shares' inclusion in the namespace.
}

// Flatten combines all shares from all rows within the namespace into a single slice.
func (ns NamespacedData) Flatten() []share.Share {
var shares []share.Share
Expand All @@ -25,26 +31,19 @@ func (ns NamespacedData) Flatten() []share.Share {
return shares
}

// RowNamespaceData holds shares and their corresponding proof for a single row within a namespace.
type RowNamespaceData struct {
Shares []share.Share `json:"shares"` // Shares within the namespace.
Proof *nmt.Proof `json:"proof"` // Proof of the shares' inclusion in the namespace.
}

// Verify checks the integrity of the NamespacedData against a provided root and namespace.
func (ns NamespacedData) Verify(root *share.Root, namespace share.Namespace) error {
rowRoots, _, _ := share.FilterRootByNamespace(root, namespace)
rowIdxs := share.RowsWithNamespace(root, namespace)

if len(rowRoots) != len(ns) {
return fmt.Errorf("expected %d rows, found %d rows", len(rowRoots), len(ns))
if len(rowIdxs) != len(ns) {
return fmt.Errorf("expected %d rows, found %d rows", len(rowIdxs), len(ns))
}

for i, row := range ns {
if row.Proof == nil || len(row.Shares) == 0 {
return fmt.Errorf("row %d is missing proofs or shares", i)
}
if !row.VerifyInclusion(rowRoots[i], namespace) {
return fmt.Errorf("failed to verify row %d", i)
for i, rowIdx := range rowIdxs {
row := ns[i]
err := row.Validate(root, namespace, rowIdx)
if err != nil {
return fmt.Errorf("failed to validate row %d: %w", i, err)
}
}
return nil
Expand Down
12 changes: 6 additions & 6 deletions share/shwap/namespaced_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func TestValidateNamespacedRow(t *testing.T) {
require.NoError(t, err)
require.True(t, len(nd) > 0)

_, from, to := share.FilterRootByNamespace(root, namespace)
require.Len(t, nd, to-from)
idx := from
for _, row := range nd {
err = row.Validate(root, namespace, idx)
rowidxs := share.RowsWithNamespace(root, namespace)
require.Equal(t, len(rowidxs), len(nd))

for i, rowidx := range rowidxs {
row := nd[i]
err = row.Validate(root, namespace, rowidx)
require.NoError(t, err)
idx++
}
}
}
Expand Down

0 comments on commit ceb8b87

Please sign in to comment.