Skip to content

Commit

Permalink
chore: use uint64 for heights
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id committed Jul 19, 2024
1 parent 2c53cb0 commit 8bc6a66
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
10 changes: 5 additions & 5 deletions nodebuilder/header/data_root_tuple_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const dataRootTupleRootBlocksLimit = 10_000 // ~33 hours of blocks assuming 12-s
// the defined set of heights.
func (s *Service) validateDataRootTupleRootRange(ctx context.Context, start, end uint64) error {
if start == 0 {
return ErrHeightNegative
return ErrHeightZero
}
if start >= end {
return fmt.Errorf("end block is smaller or equal to the start block")
Expand Down Expand Up @@ -192,12 +192,12 @@ 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 uint64) (*merkle.Proof, error) {
if len(tuples) == 0 {
return nil, fmt.Errorf("cannot prove an empty list of tuples")
}
if height <= 0 {
return nil, ErrHeightNegative
if height == 0 {
return nil, ErrHeightZero
}
currentHeight := tuples[0].height - 1
for _, tuple := range tuples {
Expand All @@ -218,7 +218,7 @@ func proveDataRootTuples(tuples []dataRootTuple, height int64) (*merkle.Proof, e
dataRootEncodedTuples = append(dataRootEncodedTuples, encodedTuple)
}
_, proofs := merkle.ProofsFromByteSlices(dataRootEncodedTuples)
return proofs[height-int64(tuples[0].height)], nil
return proofs[height-tuples[0].height], nil
}

// fetchDataRootTuples takes an end exclusive range of heights and fetches its
Expand Down
9 changes: 3 additions & 6 deletions nodebuilder/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ type Module interface {
// It's in the header module because it only needs access to the headers to generate the proof.
GetDataRootTupleInclusionProof(
ctx context.Context,
height int64,
start, end uint64,
height, start, end uint64,
) (*DataRootTupleInclusionProof, error)
}

Expand All @@ -83,8 +82,7 @@ type API struct {
GetDataRootTupleRoot func(ctx context.Context, start, end uint64) (*DataRootTupleRoot, error) `perm:"read"`
GetDataRootTupleInclusionProof func(
ctx context.Context,
height int64,
start, end uint64,
height, start, end uint64,
) (*DataRootTupleInclusionProof, error) `perm:"read"`
}
}
Expand Down Expand Up @@ -135,8 +133,7 @@ func (api *API) GetDataRootTupleRoot(ctx context.Context, start, end uint64) (*D

func (api *API) GetDataRootTupleInclusionProof(
ctx context.Context,
height int64,
start, end uint64,
height, start, end uint64,
) (*DataRootTupleInclusionProof, error) {
return api.Internal.GetDataRootTupleInclusionProof(ctx, height, start, end)
}
10 changes: 4 additions & 6 deletions nodebuilder/header/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
modfraud "github.com/celestiaorg/celestia-node/nodebuilder/fraud"
)

// ErrHeightNegative returned when the provided block height is <= 0.
var ErrHeightNegative = errors.New("height is negative")
// ErrHeightZero returned when the provided block height is equal to 0.
var ErrHeightZero = errors.New("height is equal to 0")

// Service represents the header Service that can be started / stopped on a node.
// Service's main function is to manage its sub-services. Service can contain several
Expand Down Expand Up @@ -68,7 +68,7 @@ func (s *Service) GetRangeByHeight(

func (s *Service) GetByHeight(ctx context.Context, height uint64) (*header.ExtendedHeader, error) {
if height == 0 {
return nil, ErrHeightNegative
return nil, ErrHeightZero
}
head, err := s.syncer.Head(ctx)
switch {
Expand Down Expand Up @@ -176,9 +176,7 @@ func (s *Service) GetDataRootTupleRoot(ctx context.Context, start, end uint64) (
// is end exclusive.
func (s *Service) GetDataRootTupleInclusionProof(
ctx context.Context,
height int64,
start,
end uint64,
height, start, end uint64,
) (*DataRootTupleInclusionProof, error) {
log.Debugw(
"validating the data root inclusion proof request",
Expand Down
12 changes: 1 addition & 11 deletions nodebuilder/header/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,11 @@ func TestHashDataRootTuples(t *testing.T) {
func TestProveDataRootTuples(t *testing.T) {
tests := map[string]struct {
tuples []dataRootTuple
height int64
height uint64
expectedProof merkle.Proof
expectErr bool
}{
"empty tuples list": {tuples: nil, expectErr: true},
"strictly negative height": {
height: -1,
tuples: []dataRootTuple{
{
height: 1,
dataRoot: [32]byte{0x1},
},
},
expectErr: true,
},
"non consecutive list of tuples at the beginning": {
tuples: []dataRootTuple{
{
Expand Down

0 comments on commit 8bc6a66

Please sign in to comment.