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

     1  package nodebuilder
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cosmos/cosmos-sdk/crypto/hd"
     7  	"github.com/cosmos/cosmos-sdk/crypto/keyring"
     8  	"github.com/stretchr/testify/require"
     9  	"go.uber.org/fx"
    10  
    11  	apptypes "github.com/celestiaorg/celestia-app/x/blob/types"
    12  	libhead "github.com/celestiaorg/go-header"
    13  
    14  	"github.com/celestiaorg/celestia-node/core"
    15  	"github.com/celestiaorg/celestia-node/header"
    16  	"github.com/celestiaorg/celestia-node/header/headertest"
    17  	"github.com/celestiaorg/celestia-node/libs/fxutil"
    18  	"github.com/celestiaorg/celestia-node/nodebuilder/node"
    19  	"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
    20  	"github.com/celestiaorg/celestia-node/nodebuilder/state"
    21  )
    22  
    23  // MockStore provides mock in memory Store for testing purposes.
    24  func MockStore(t *testing.T, cfg *Config) Store {
    25  	t.Helper()
    26  	store := NewMemStore()
    27  	err := store.PutConfig(cfg)
    28  	require.NoError(t, err)
    29  	return store
    30  }
    31  
    32  func TestNode(t *testing.T, tp node.Type, opts ...fx.Option) *Node {
    33  	return TestNodeWithConfig(t, tp, DefaultConfig(tp), opts...)
    34  }
    35  
    36  func TestNodeWithConfig(t *testing.T, tp node.Type, cfg *Config, opts ...fx.Option) *Node {
    37  	// avoids port conflicts
    38  	cfg.RPC.Port = "0"
    39  	cfg.Header.TrustedPeers = []string{"/ip4/1.2.3.4/tcp/12345/p2p/12D3KooWNaJ1y1Yio3fFJEXCZyd1Cat3jmrPdgkYCrHfKD3Ce21p"}
    40  
    41  	store := MockStore(t, cfg)
    42  	ks, err := store.Keystore()
    43  	require.NoError(t, err)
    44  
    45  	opts = append(opts,
    46  		// avoid writing keyring on disk
    47  		state.WithKeyringSigner(TestKeyringSigner(t, ks.Keyring())),
    48  		// temp dir for the eds store FIXME: Should be in mem
    49  		fx.Replace(node.StorePath(t.TempDir())),
    50  		// avoid requesting trustedPeer during initialization
    51  		fxutil.ReplaceAs(headertest.NewStore(t), new(libhead.Store[*header.ExtendedHeader])),
    52  	)
    53  
    54  	// in fact, we don't need core.Client in tests, but Bridge requires is a valid one
    55  	// or fails otherwise with failed attempt to connect with custom build client
    56  	if tp == node.Bridge {
    57  		cctx := core.StartTestNode(t)
    58  		opts = append(opts,
    59  			fxutil.ReplaceAs(cctx.Client, new(core.Client)),
    60  		)
    61  	}
    62  
    63  	nd, err := New(tp, p2p.Private, store, opts...)
    64  	require.NoError(t, err)
    65  	return nd
    66  }
    67  
    68  func TestKeyringSigner(t *testing.T, ring keyring.Keyring) *apptypes.KeyringSigner {
    69  	signer := apptypes.NewKeyringSigner(ring, "", string(p2p.Private))
    70  	_, _, err := signer.NewMnemonic("test_celes", keyring.English, "",
    71  		"", hd.Secp256k1)
    72  	require.NoError(t, err)
    73  	return signer
    74  }