github.com/celestiaorg/celestia-node@v0.15.0-beta.1/share/share.go (about)

     1  package share
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"fmt"
     7  
     8  	"github.com/celestiaorg/celestia-app/pkg/appconsts"
     9  )
    10  
    11  var (
    12  	// DefaultRSMT2DCodec sets the default rsmt2d.Codec for shares.
    13  	DefaultRSMT2DCodec = appconsts.DefaultCodec
    14  )
    15  
    16  const (
    17  	// Size is a system-wide size of a share, including both data and namespace GetNamespace
    18  	Size = appconsts.ShareSize
    19  )
    20  
    21  var (
    22  	// MaxSquareSize is currently the maximum size supported for unerasured data in
    23  	// rsmt2d.ExtendedDataSquare.
    24  	MaxSquareSize = appconsts.SquareSizeUpperBound(appconsts.LatestVersion)
    25  )
    26  
    27  // Share contains the raw share data without the corresponding namespace.
    28  // NOTE: Alias for the byte is chosen to keep maximal compatibility, especially with rsmt2d.
    29  // Ideally, we should define reusable type elsewhere and make everyone(Core, rsmt2d, ipld) to rely
    30  // on it.
    31  type Share = []byte
    32  
    33  // GetNamespace slices Namespace out of the Share.
    34  func GetNamespace(s Share) Namespace {
    35  	return s[:NamespaceSize]
    36  }
    37  
    38  // GetData slices out data of the Share.
    39  func GetData(s Share) []byte {
    40  	return s[NamespaceSize:]
    41  }
    42  
    43  // DataHash is a representation of the Root hash.
    44  type DataHash []byte
    45  
    46  func (dh DataHash) Validate() error {
    47  	if len(dh) != 32 {
    48  		return fmt.Errorf("invalid hash size, expected 32, got %d", len(dh))
    49  	}
    50  	return nil
    51  }
    52  
    53  func (dh DataHash) String() string {
    54  	return fmt.Sprintf("%X", []byte(dh))
    55  }
    56  
    57  // IsEmptyRoot check whether DataHash corresponds to the root of an empty block EDS.
    58  func (dh DataHash) IsEmptyRoot() bool {
    59  	return bytes.Equal(EmptyRoot().Hash(), dh)
    60  }
    61  
    62  // MustDataHashFromString converts a hex string to a valid datahash.
    63  func MustDataHashFromString(datahash string) DataHash {
    64  	dh, err := hex.DecodeString(datahash)
    65  	if err != nil {
    66  		panic(fmt.Sprintf("datahash conversion: passed string was not valid hex: %s", datahash))
    67  	}
    68  	err = DataHash(dh).Validate()
    69  	if err != nil {
    70  		panic(fmt.Sprintf("datahash validation: passed hex string failed: %s", err))
    71  	}
    72  	return dh
    73  }