Skip to content

Commit

Permalink
refactor!: make Header.Height uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Aug 24, 2023
1 parent e85c71e commit 1a5c808
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Header[H any] interface {
// Hash returns hash of a header.
Hash() Hash
// Height returns the height of a header.
Height() int64
Height() uint64
// LastHeader returns the hash of last header before this header (aka. previous header hash).
LastHeader() Hash
// Time returns time when header was created.
Expand Down
10 changes: 5 additions & 5 deletions headertest/dummy_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var ErrDummyVerify = errors.New("dummy verify error")
type DummyHeader struct {
Chainid string
PreviousHash header.Hash
HeightI int64
HeightI uint64
Timestamp time.Time

hash header.Hash
Expand All @@ -34,7 +34,7 @@ func RandDummyHeader(t *testing.T) *DummyHeader {

dh := &DummyHeader{
PreviousHash: RandBytes(32),
HeightI: randInt63(),
HeightI: randUint63(),
Timestamp: time.Now().UTC(),
}
err := dh.rehash()
Expand Down Expand Up @@ -75,7 +75,7 @@ func (d *DummyHeader) rehash() error {
return nil
}

func (d *DummyHeader) Height() int64 {
func (d *DummyHeader) Height() uint64 {
return d.HeightI
}

Expand Down Expand Up @@ -128,13 +128,13 @@ func RandBytes(n int) []byte {
return buf
}

func randInt63() int64 {
func randUint63() uint64 {
var buf [8]byte

_, err := rand.Read(buf[:])
if err != nil {
return math.MaxInt64
}

return int64(binary.BigEndian.Uint64(buf[:]) & math.MaxInt64)
return binary.BigEndian.Uint64(buf[:]) & math.MaxInt64
}
12 changes: 6 additions & 6 deletions headertest/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Generator[H header.Header[H]] interface {
}

type Store[H header.Header[H]] struct {
Headers map[int64]H
HeadHeight int64
Headers map[uint64]H
HeadHeight uint64
}

// NewDummyStore creates a store for DummyHeader.
Expand All @@ -25,7 +25,7 @@ func NewDummyStore(t *testing.T) *Store[*DummyHeader] {
// NewStore creates a generic mock store supporting different type of Headers based on Generator.
func NewStore[H header.Header[H]](t *testing.T, gen Generator[H], numHeaders int) *Store[H] {
store := &Store[H]{
Headers: make(map[int64]H),
Headers: make(map[uint64]H),
HeadHeight: 0,
}

Expand Down Expand Up @@ -63,7 +63,7 @@ func (m *Store[H]) Get(ctx context.Context, hash header.Hash) (H, error) {
}

func (m *Store[H]) GetByHeight(ctx context.Context, height uint64) (H, error) {
return m.Headers[int64(height)], nil
return m.Headers[height], nil
}

func (m *Store[H]) GetRangeByHeight(ctx context.Context, from, to uint64) ([]H, error) {
Expand All @@ -75,7 +75,7 @@ func (m *Store[H]) GetRangeByHeight(ctx context.Context, from, to uint64) ([]H,
return nil, header.ErrNotFound
}
for i := range headers {
headers[i] = m.Headers[int64(from)]
headers[i] = m.Headers[from]
from++
}
return headers, nil
Expand All @@ -94,7 +94,7 @@ func (m *Store[H]) Has(context.Context, header.Hash) (bool, error) {
}

func (m *Store[H]) HasAt(_ context.Context, height uint64) bool {
return height != 0 && m.HeadHeight >= int64(height)
return height != 0 && m.HeadHeight >= height
}

func (m *Store[H]) Append(ctx context.Context, headers ...H) error {
Expand Down
4 changes: 2 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ var (

// ErrNonAdjacent is returned when Store is appended with a header not adjacent to the stored head.
type ErrNonAdjacent struct {
Head int64
Attempted int64
Head uint64
Attempted uint64
}

func (ena *ErrNonAdjacent) Error() string {
Expand Down
2 changes: 1 addition & 1 deletion metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func WithMetrics[H Header[H]](store Store[H]) error {
metric.WithAttributes(
attribute.String("err", err.Error())))
} else {
observer.ObserveInt64(headC, head.Height())
observer.ObserveInt64(headC, int64(head.Height()))
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestExchange_RequestHead(t *testing.T) {
tests := []struct {
requestFromTrusted bool
lastHeader header.Header[*headertest.DummyHeader]
expectedHeight int64
expectedHeight uint64
expectedHash header.Hash
}{
// routes to trusted peer only
Expand Down Expand Up @@ -297,7 +297,7 @@ func Test_bestHead(t *testing.T) {
}
testCases := []struct {
precondition func() []*headertest.DummyHeader
expectedHeight int64
expectedHeight uint64
}{
/*
Height -> Amount
Expand Down
4 changes: 2 additions & 2 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (serv *ExchangeServer[H]) handleRequestByHash(hash []byte) ([]H, error) {

span.AddEvent("fetched-header-from-store", trace.WithAttributes(
attribute.String("hash", header.Hash(hash).String()),
attribute.Int64("height", h.Height())),
attribute.Int64("height", int64(h.Height()))),
)
span.SetStatus(codes.Ok, "")
return []H{h}, nil
Expand Down Expand Up @@ -263,7 +263,7 @@ func (serv *ExchangeServer[H]) handleHeadRequest() ([]H, error) {

span.AddEvent("fetched-head", trace.WithAttributes(
attribute.String("hash", head.Hash().String()),
attribute.Int64("height", head.Height())),
attribute.Int64("height", int64(head.Height()))),
)
span.SetStatus(codes.Ok, "")
return []H{head}, nil
Expand Down
4 changes: 2 additions & 2 deletions store/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (b *batch[H]) getByHeight(height uint64) H {
return zero
}

head := uint64(b.headers[ln-1].Height())
head := b.headers[ln-1].Height()
base := head - ln
if height > head || height <= base {
return zero
Expand All @@ -85,7 +85,7 @@ func (b *batch[H]) Append(headers ...H) {
defer b.lk.Unlock()
for _, h := range headers {
b.headers = append(b.headers, h)
b.heights[h.Hash().String()] = uint64(h.Height())
b.heights[h.Hash().String()] = h.Height()
}
}

Expand Down
2 changes: 1 addition & 1 deletion store/height_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (hi *heightIndexer[H]) HashByHeight(ctx context.Context, h uint64) (header.
// IndexTo saves mapping between header Height and Hash to the given batch.
func (hi *heightIndexer[H]) IndexTo(ctx context.Context, batch datastore.Batch, headers ...H) error {
for _, h := range headers {
err := batch.Put(ctx, heightKey(uint64(h.Height())), h.Hash())
err := batch.Put(ctx, heightKey(h.Height()), h.Hash())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion store/heightsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (hs *heightSub[H]) Pub(headers ...H) {
}

height := hs.Height()
from, to := uint64(headers[0].Height()), uint64(headers[ln-1].Height())
from, to := headers[0].Height(), headers[ln-1].Height()
if height+1 != from && height != 0 { // height != 0 is needed to enable init from any height and not only 1
log.Fatalf("PLEASE FILE A BUG REPORT: headers given to the heightSub are in the wrong order: expected %d, got %d", height+1, from)
return
Expand Down
4 changes: 2 additions & 2 deletions sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ func (s *Syncer[H]) verify(ctx context.Context, newHead H) (bool, error) {
return true, &header.VerifyError{Reason: err, SoftFailure: true}
}

var heightThreshold int64
var heightThreshold uint64
if s.Params.TrustingPeriod != 0 && s.Params.blockTime != 0 {
buffer := time.Hour * 6 / s.Params.blockTime // small buffer to account for network delays
heightThreshold = int64(s.Params.TrustingPeriod/s.Params.blockTime + buffer)
heightThreshold = uint64(s.Params.TrustingPeriod/s.Params.blockTime + buffer)
}

err = header.Verify(sbjHead, newHead, heightThreshold)
Expand Down
4 changes: 2 additions & 2 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestSyncPendingRangesWithMisses(t *testing.T) {

lastHead, err := syncer.store.Head(ctx)
require.NoError(t, err)
require.Equal(t, lastHead.Height(), int64(43))
require.Equal(t, lastHead.Height(), uint64(43))
exp, err := remoteStore.Head(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -247,7 +247,7 @@ func TestSyncer_FindHeadersReturnsCorrectRange(t *testing.T) {

head, err = syncer.store.Head(ctx)
require.NoError(t, err)
assert.Equal(t, head.Height(), int64(21))
assert.Equal(t, head.Height(), uint64(21))
}

func TestSyncerIncomingDuplicate(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

// DefaultHeightThreshold defines default height threshold beyond which headers are rejected
// NOTE: Compared against subjective head which is guaranteed to be non-expired
const DefaultHeightThreshold int64 = 80000 // ~ 14 days of 15 second headers
const DefaultHeightThreshold uint64 = 80000 // ~ 14 days of 15 second headers

// Verify verifies untrusted Header against trusted following general Header checks and
// custom user-specific checks defined in Header.Verify
//
// If heightThreshold is zero, uses DefaultHeightThreshold.
// Always returns VerifyError.
func Verify[H Header[H]](trstd, untrstd H, heightThreshold int64) error {
func Verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error {
// general mandatory verification
err := verify[H](trstd, untrstd, heightThreshold)
if err != nil {
Expand Down Expand Up @@ -45,7 +45,7 @@ func Verify[H Header[H]](trstd, untrstd H, heightThreshold int64) error {

// verify is a little bro of Verify yet performs mandatory Header checks
// for any Header implementation.
func verify[H Header[H]](trstd, untrstd H, heightThreshold int64) error {
func verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error {
if heightThreshold == 0 {
heightThreshold = DefaultHeightThreshold
}
Expand Down

0 comments on commit 1a5c808

Please sign in to comment.