Skip to content

Commit

Permalink
chore: un-export data root tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id committed Jul 19, 2024
1 parent 4bc4d7d commit 76a1aaf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
30 changes: 15 additions & 15 deletions nodebuilder/header/data_root_tuple_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func padBytes(byt []byte, length int) ([]byte, error) {
return tmp, nil
}

// To32PaddedHexBytes takes a number and returns its hex representation padded to 32 bytes.
// to32PaddedHexBytes takes a number and returns its hex representation padded to 32 bytes.
// Used to mimic the result of `abi.encode(number)` in Ethereum.
func To32PaddedHexBytes(number uint64) ([]byte, error) {
func to32PaddedHexBytes(number uint64) ([]byte, error) {
hexRepresentation := strconv.FormatUint(number, 16)
// Make sure hex representation has even length.
// The `strconv.FormatUint` can return odd length hex encodings.
Expand All @@ -58,17 +58,17 @@ func To32PaddedHexBytes(number uint64) ([]byte, error) {
return paddedBytes, nil
}

// DataRootTuple contains the data that will be used to generate the Blobstream data root tuple
// dataRootTuple contains the data that will be used to generate the Blobstream data root tuple
// roots. For more information:
// https://github.com/celestiaorg/blobstream-contracts/blob/master/src/DataRootTuple.sol
type DataRootTuple struct {
type dataRootTuple struct {
height uint64
dataRoot [32]byte
}

// EncodeDataRootTuple takes a height and a data root, and returns the equivalent of
// encodeDataRootTuple takes a height and a data root, and returns the equivalent of
// `abi.encode(...)` in Ethereum.
// The encoded type is a DataRootTuple, which has the following ABI:
// The encoded type is a dataRootTuple, which has the following ABI:
//
// {
// "components":[
Expand All @@ -93,8 +93,8 @@ type DataRootTuple struct {
// padding the hex representation of the height padded to 32 bytes concatenated to the data root.
// For more information, refer to:
// https://github.com/celestiaorg/blobstream-contracts/blob/master/src/DataRootTuple.sol
func EncodeDataRootTuple(height uint64, dataRoot [32]byte) ([]byte, error) {
paddedHeight, err := To32PaddedHexBytes(height)
func encodeDataRootTuple(height uint64, dataRoot [32]byte) ([]byte, error) {
paddedHeight, err := to32PaddedHexBytes(height)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -151,13 +151,13 @@ func (s *Service) validateDataRootTupleRootRange(ctx context.Context, start, end

// hashDataRootTuples hashes a list of blocks data root tuples, i.e., height, data root and square
// size, then returns their merkle root.
func hashDataRootTuples(tuples []DataRootTuple) ([]byte, error) {
func hashDataRootTuples(tuples []dataRootTuple) ([]byte, error) {
if len(tuples) == 0 {
return nil, fmt.Errorf("cannot hash an empty list of data root tuples")
}
dataRootEncodedTuples := make([][]byte, 0, len(tuples))
for _, tuple := range tuples {
encodedTuple, err := EncodeDataRootTuple(
encodedTuple, err := encodeDataRootTuple(
tuple.height,
tuple.dataRoot,
)
Expand Down Expand Up @@ -192,7 +192,7 @@ func (s *Service) validateDataRootInclusionProofRequest(
}

// proveDataRootTuples returns the merkle inclusion proof for a height.
func proveDataRootTuples(tuples []DataRootTuple, height int64) (*merkle.Proof, error) {
func proveDataRootTuples(tuples []dataRootTuple, height int64) (*merkle.Proof, error) {
if len(tuples) == 0 {
return nil, fmt.Errorf("cannot prove an empty list of tuples")
}
Expand All @@ -208,7 +208,7 @@ func proveDataRootTuples(tuples []DataRootTuple, height int64) (*merkle.Proof, e
}
dataRootEncodedTuples := make([][]byte, 0, len(tuples))
for _, tuple := range tuples {
encodedTuple, err := EncodeDataRootTuple(
encodedTuple, err := encodeDataRootTuple(
tuple.height,
tuple.dataRoot,
)
Expand All @@ -223,8 +223,8 @@ func proveDataRootTuples(tuples []DataRootTuple, height int64) (*merkle.Proof, e

// fetchDataRootTuples takes an end exclusive range of heights and fetches its
// corresponding data root tuples.
func (s *Service) fetchDataRootTuples(ctx context.Context, start, end uint64) ([]DataRootTuple, error) {
tuples := make([]DataRootTuple, 0, end-start)
func (s *Service) fetchDataRootTuples(ctx context.Context, start, end uint64) ([]dataRootTuple, error) {
tuples := make([]dataRootTuple, 0, end-start)
for height := start; height < end; height++ {
block, err := s.GetByHeight(ctx, height)
if err != nil {
Expand All @@ -233,7 +233,7 @@ func (s *Service) fetchDataRootTuples(ctx context.Context, start, end uint64) ([
if block == nil {
return nil, fmt.Errorf("couldn't load block %d", height)
}
tuples = append(tuples, DataRootTuple{
tuples = append(tuples, dataRootTuple{
height: block.Height(),
dataRoot: *(*[32]byte)(block.DataHash),
})
Expand Down
38 changes: 19 additions & 19 deletions nodebuilder/header/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestTo32PaddedHexBytes(t *testing.T) {

for _, test := range tests {
t.Run(fmt.Sprintf("number: %d", test.number), func(t *testing.T) {
result, err := To32PaddedHexBytes(test.number)
result, err := to32PaddedHexBytes(test.number)
if test.expectError {
assert.Error(t, err)
} else {
Expand All @@ -130,7 +130,7 @@ func TestEncodeDataRootTuple(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, expectedEncoding)

actualEncoding, err := EncodeDataRootTuple(height, *(*[32]byte)(dataRoot))
actualEncoding, err := encodeDataRootTuple(height, *(*[32]byte)(dataRoot))
require.NoError(t, err)
require.NotNil(t, actualEncoding)

Expand All @@ -141,13 +141,13 @@ func TestEncodeDataRootTuple(t *testing.T) {

func TestHashDataRootTuples(t *testing.T) {
tests := map[string]struct {
tuples []DataRootTuple
tuples []dataRootTuple
expectedHash []byte
expectErr bool
}{
"empty tuples list": {tuples: nil, expectErr: true},
"valid list of data root tuples": {
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -158,8 +158,8 @@ func TestHashDataRootTuples(t *testing.T) {
},
},
expectedHash: func() []byte {
tuple1, _ := EncodeDataRootTuple(1, [32]byte{0x1})
tuple2, _ := EncodeDataRootTuple(2, [32]byte{0x2})
tuple1, _ := encodeDataRootTuple(1, [32]byte{0x1})
tuple2, _ := encodeDataRootTuple(2, [32]byte{0x2})

return merkle.HashFromByteSlices([][]byte{tuple1, tuple2})
}(),
Expand All @@ -181,15 +181,15 @@ func TestHashDataRootTuples(t *testing.T) {

func TestProveDataRootTuples(t *testing.T) {
tests := map[string]struct {
tuples []DataRootTuple
tuples []dataRootTuple
height int64
expectedProof merkle.Proof
expectErr bool
}{
"empty tuples list": {tuples: nil, expectErr: true},
"strictly negative height": {
height: -1,
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -198,7 +198,7 @@ func TestProveDataRootTuples(t *testing.T) {
expectErr: true,
},
"non consecutive list of tuples at the beginning": {
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -215,7 +215,7 @@ func TestProveDataRootTuples(t *testing.T) {
expectErr: true,
},
"non consecutive list of tuples in the middle": {
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -240,7 +240,7 @@ func TestProveDataRootTuples(t *testing.T) {
expectErr: true,
},
"non consecutive list of tuples at the end": {
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -257,7 +257,7 @@ func TestProveDataRootTuples(t *testing.T) {
expectErr: true,
},
"duplicate height at the beginning": {
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -274,7 +274,7 @@ func TestProveDataRootTuples(t *testing.T) {
expectErr: true,
},
"duplicate height in the middle": {
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -295,7 +295,7 @@ func TestProveDataRootTuples(t *testing.T) {
expectErr: true,
},
"duplicate height at the end": {
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -313,7 +313,7 @@ func TestProveDataRootTuples(t *testing.T) {
},
"valid proof": {
height: 3,
tuples: []DataRootTuple{
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
Expand All @@ -332,10 +332,10 @@ func TestProveDataRootTuples(t *testing.T) {
},
},
expectedProof: func() merkle.Proof {
encodedTuple1, _ := EncodeDataRootTuple(1, [32]byte{0x1})
encodedTuple2, _ := EncodeDataRootTuple(2, [32]byte{0x2})
encodedTuple3, _ := EncodeDataRootTuple(3, [32]byte{0x3})
encodedTuple4, _ := EncodeDataRootTuple(4, [32]byte{0x4})
encodedTuple1, _ := encodeDataRootTuple(1, [32]byte{0x1})
encodedTuple2, _ := encodeDataRootTuple(2, [32]byte{0x2})
encodedTuple3, _ := encodeDataRootTuple(3, [32]byte{0x3})
encodedTuple4, _ := encodeDataRootTuple(4, [32]byte{0x4})
_, proofs := merkle.ProofsFromByteSlices([][]byte{encodedTuple1, encodedTuple2, encodedTuple3, encodedTuple4})
return *proofs[2]
}(),
Expand Down

0 comments on commit 76a1aaf

Please sign in to comment.