Skip to content

Commit

Permalink
fix(header): GetByHeight requests for syncer head +1 shouldnt error (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd authored Jun 14, 2023
1 parent 162e438 commit f291cf3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
5 changes: 3 additions & 2 deletions nodebuilder/header/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func newHeaderService(
sub libhead.Subscriber[*header.ExtendedHeader],
p2pServer *p2p.ExchangeServer[*header.ExtendedHeader],
ex libhead.Exchange[*header.ExtendedHeader],
store libhead.Store[*header.ExtendedHeader]) Module {
store libhead.Store[*header.ExtendedHeader],
) Module {
return &Service{
syncer: syncer,
sub: sub,
Expand Down Expand Up @@ -66,7 +67,7 @@ func (s *Service) GetByHeight(ctx context.Context, height uint64) (*header.Exten
return nil, err
case uint64(head.Height()) == height:
return head, nil
case uint64(head.Height()) < height:
case uint64(head.Height())+1 < height:
return nil, fmt.Errorf("header: given height is from the future: "+
"networkHeight: %d, requestedHeight: %d", head.Height(), height)
}
Expand Down
38 changes: 36 additions & 2 deletions nodebuilder/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package tests

import (
"context"
"fmt"
"testing"
"time"

"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/libp2p/go-libp2p/core/host"
Expand All @@ -19,6 +19,41 @@ import (
"github.com/celestiaorg/celestia-node/nodebuilder/tests/swamp"
)

func TestGetByHeight(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), swamp.DefaultTestTimeout)
t.Cleanup(cancel)

sw := swamp.NewSwamp(t, swamp.WithBlockTime(time.Second))

// start a bridge node
bridge := sw.NewBridgeNode()
err := bridge.Start(ctx)
require.NoError(t, err)

signer := bridge.AdminSigner
bridgeAddr := "http://" + bridge.RPCServer.ListenAddr()

jwt, err := authtoken.NewSignedJWT(signer, []auth.Permission{"public", "read", "write", "admin"})
require.NoError(t, err)

client, err := client.NewClient(ctx, bridgeAddr, jwt)
require.NoError(t, err)

// let a few blocks be produced
_, err = client.Header.WaitForHeight(ctx, 3)
require.NoError(t, err)

networkHead, err := client.Header.NetworkHead(ctx)
require.NoError(t, err)
_, err = client.Header.GetByHeight(ctx, uint64(networkHead.Height()+1))
require.Nil(t, err, "Requesting syncer.Head()+1 shouldn't return an error")

networkHead, err = client.Header.NetworkHead(ctx)
require.NoError(t, err)
_, err = client.Header.GetByHeight(ctx, uint64(networkHead.Height()+2))
require.ErrorContains(t, err, "given height is from the future")
}

// TestBlobRPC ensures that blobs can be submited via rpc
func TestBlobRPC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), swamp.DefaultTestTimeout)
Expand All @@ -33,7 +68,6 @@ func TestBlobRPC(t *testing.T) {

signer := bridge.AdminSigner
bridgeAddr := "http://" + bridge.RPCServer.ListenAddr()
fmt.Println("bridgeAddr: ", bridgeAddr)

jwt, err := authtoken.NewSignedJWT(signer, []auth.Permission{"public", "read", "write", "admin"})
require.NoError(t, err)
Expand Down

0 comments on commit f291cf3

Please sign in to comment.