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

     1  package header
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/ipfs/go-datastore"
     9  	"github.com/libp2p/go-libp2p"
    10  	pubsub "github.com/libp2p/go-libp2p-pubsub"
    11  	"github.com/libp2p/go-libp2p/p2p/net/conngater"
    12  	"github.com/stretchr/testify/require"
    13  	"go.uber.org/fx"
    14  	"go.uber.org/fx/fxtest"
    15  
    16  	"github.com/celestiaorg/go-fraud"
    17  	libhead "github.com/celestiaorg/go-header"
    18  	"github.com/celestiaorg/go-header/p2p"
    19  	"github.com/celestiaorg/go-header/store"
    20  	"github.com/celestiaorg/go-header/sync"
    21  
    22  	"github.com/celestiaorg/celestia-node/header"
    23  	"github.com/celestiaorg/celestia-node/libs/pidstore"
    24  	"github.com/celestiaorg/celestia-node/nodebuilder/node"
    25  	modp2p "github.com/celestiaorg/celestia-node/nodebuilder/p2p"
    26  )
    27  
    28  // TestConstructModule_StoreParams ensures that all passed via functional options
    29  // params are set in store correctly.
    30  func TestConstructModule_StoreParams(t *testing.T) {
    31  	cfg := DefaultConfig(node.Light)
    32  	cfg.Store.StoreCacheSize = 15
    33  	cfg.Store.IndexCacheSize = 25
    34  	cfg.Store.WriteBatchSize = 35
    35  	var headerStore *store.Store[*header.ExtendedHeader]
    36  
    37  	app := fxtest.New(t,
    38  		fx.Supply(modp2p.Private),
    39  		fx.Supply(modp2p.Bootstrappers{}),
    40  		fx.Provide(context.Background),
    41  		fx.Provide(libp2p.New),
    42  		fx.Provide(conngater.NewBasicConnectionGater),
    43  		fx.Provide(func() (datastore.Batching, datastore.Datastore) {
    44  			ds := datastore.NewMapDatastore()
    45  			return ds, ds
    46  		}),
    47  		ConstructModule[*header.ExtendedHeader](node.Light, &cfg),
    48  		fx.Invoke(
    49  			func(s libhead.Store[*header.ExtendedHeader]) {
    50  				ss := s.(*store.Store[*header.ExtendedHeader])
    51  				headerStore = ss
    52  			}),
    53  	)
    54  	require.NoError(t, app.Err())
    55  	require.Equal(t, headerStore.Params.StoreCacheSize, cfg.Store.StoreCacheSize)
    56  	require.Equal(t, headerStore.Params.IndexCacheSize, cfg.Store.IndexCacheSize)
    57  	require.Equal(t, headerStore.Params.WriteBatchSize, cfg.Store.WriteBatchSize)
    58  }
    59  
    60  // TestConstructModule_SyncerParams ensures that all passed via functional options
    61  // params are set in syncer correctly.
    62  func TestConstructModule_SyncerParams(t *testing.T) {
    63  	cfg := DefaultConfig(node.Light)
    64  	cfg.Syncer.TrustingPeriod = time.Hour
    65  	cfg.TrustedPeers = []string{"/ip4/1.2.3.4/tcp/12345/p2p/12D3KooWNaJ1y1Yio3fFJEXCZyd1Cat3jmrPdgkYCrHfKD3Ce21p"}
    66  	var syncer *sync.Syncer[*header.ExtendedHeader]
    67  	app := fxtest.New(t,
    68  		fx.Supply(modp2p.Private),
    69  		fx.Supply(modp2p.Bootstrappers{}),
    70  		fx.Provide(context.Background),
    71  		fx.Provide(libp2p.New),
    72  		fx.Provide(func(b datastore.Batching) (*conngater.BasicConnectionGater, error) {
    73  			return conngater.NewBasicConnectionGater(b)
    74  		}),
    75  		fx.Provide(func() *pubsub.PubSub {
    76  			return nil
    77  		}),
    78  		fx.Provide(func() datastore.Batching {
    79  			return datastore.NewMapDatastore()
    80  		}),
    81  		fx.Provide(func() fraud.Service[*header.ExtendedHeader] {
    82  			return nil
    83  		}),
    84  		ConstructModule[*header.ExtendedHeader](node.Light, &cfg),
    85  		fx.Invoke(func(s *sync.Syncer[*header.ExtendedHeader]) {
    86  			syncer = s
    87  		}),
    88  	)
    89  	require.Equal(t, cfg.Syncer.TrustingPeriod, syncer.Params.TrustingPeriod)
    90  	require.NoError(t, app.Err())
    91  }
    92  
    93  // TestConstructModule_ExchangeParams ensures that all passed via functional options
    94  // params are set in store correctly.
    95  func TestConstructModule_ExchangeParams(t *testing.T) {
    96  	cfg := DefaultConfig(node.Light)
    97  	cfg.Client.MaxHeadersPerRangeRequest = 15
    98  	cfg.TrustedPeers = []string{"/ip4/1.2.3.4/tcp/12345/p2p/12D3KooWNaJ1y1Yio3fFJEXCZyd1Cat3jmrPdgkYCrHfKD3Ce21p"}
    99  	var exchange *p2p.Exchange[*header.ExtendedHeader]
   100  	var exchangeServer *p2p.ExchangeServer[*header.ExtendedHeader]
   101  
   102  	app := fxtest.New(t,
   103  		fx.Provide(pidstore.NewPeerIDStore),
   104  		fx.Provide(context.Background),
   105  		fx.Supply(modp2p.Private),
   106  		fx.Supply(modp2p.Bootstrappers{}),
   107  		fx.Provide(libp2p.New),
   108  		fx.Provide(func() datastore.Batching {
   109  			return datastore.NewMapDatastore()
   110  		}),
   111  		ConstructModule[*header.ExtendedHeader](node.Light, &cfg),
   112  		fx.Provide(func(b datastore.Batching) (*conngater.BasicConnectionGater, error) {
   113  			return conngater.NewBasicConnectionGater(b)
   114  		}),
   115  		fx.Invoke(
   116  			func(e libhead.Exchange[*header.ExtendedHeader], server *p2p.ExchangeServer[*header.ExtendedHeader]) {
   117  				ex := e.(*p2p.Exchange[*header.ExtendedHeader])
   118  				exchange = ex
   119  				exchangeServer = server
   120  			}),
   121  	)
   122  	require.NoError(t, app.Err())
   123  	require.Equal(t, exchange.Params.MaxHeadersPerRangeRequest, cfg.Client.MaxHeadersPerRangeRequest)
   124  	require.Equal(t, exchange.Params.RangeRequestTimeout, cfg.Client.RangeRequestTimeout)
   125  
   126  	require.Equal(t, exchangeServer.Params.WriteDeadline, cfg.Server.WriteDeadline)
   127  	require.Equal(t, exchangeServer.Params.ReadDeadline, cfg.Server.ReadDeadline)
   128  	require.Equal(t, exchangeServer.Params.RangeRequestTimeout, cfg.Server.RangeRequestTimeout)
   129  }