Skip to content

Commit

Permalink
refactor(nodebuilder/tests/swamp): Add SetBootstrapper method on suite
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed May 31, 2023
1 parent 7279e34 commit 2883a67
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
33 changes: 27 additions & 6 deletions nodebuilder/tests/swamp/swamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ const DefaultTestTimeout = time.Minute * 5
// - Slices of created Bridge/Full/Light Nodes
// - trustedHash taken from the CoreClient and shared between nodes
type Swamp struct {
t *testing.T
Network mocknet.Mocknet
t *testing.T
Network mocknet.Mocknet

Bootstrappers []ma.Multiaddr

BridgeNodes []*nodebuilder.Node
FullNodes []*nodebuilder.Node
LightNodes []*nodebuilder.Node
comps *Config

comps *Config

ClientContext testnode.Context
Accounts []string
Expand Down Expand Up @@ -204,6 +208,10 @@ func (s *Swamp) NewFullNode(options ...fx.Option) *nodebuilder.Node {
cfg.Header.TrustedPeers = []string{
"/ip4/1.2.3.4/tcp/12345/p2p/12D3KooWNaJ1y1Yio3fFJEXCZyd1Cat3jmrPdgkYCrHfKD3Ce21p",
}
// add all bootstrappers in suite as trusted peers
for _, bootstrapper := range s.Bootstrappers {
cfg.Header.TrustedPeers = append(cfg.Header.TrustedPeers, bootstrapper.String())
}
store := nodebuilder.MockStore(s.t, cfg)

return s.NewNodeWithStore(node.Full, store, options...)
Expand All @@ -216,6 +224,10 @@ func (s *Swamp) NewLightNode(options ...fx.Option) *nodebuilder.Node {
cfg.Header.TrustedPeers = []string{
"/ip4/1.2.3.4/tcp/12345/p2p/12D3KooWNaJ1y1Yio3fFJEXCZyd1Cat3jmrPdgkYCrHfKD3Ce21p",
}
// add all bootstrappers in suite as trusted peers
for _, bootstrapper := range s.Bootstrappers {
cfg.Header.TrustedPeers = append(cfg.Header.TrustedPeers, bootstrapper.String())
}

store := nodebuilder.MockStore(s.t, cfg)

Expand All @@ -224,6 +236,10 @@ func (s *Swamp) NewLightNode(options ...fx.Option) *nodebuilder.Node {

func (s *Swamp) NewNodeWithConfig(nodeType node.Type, cfg *nodebuilder.Config, options ...fx.Option) *nodebuilder.Node {
store := nodebuilder.MockStore(s.t, cfg)
// add all bootstrappers in suite as trusted peers
for _, bootstrapper := range s.Bootstrappers {
cfg.Header.TrustedPeers = append(cfg.Header.TrustedPeers, bootstrapper.String())
}
return s.NewNodeWithStore(nodeType, store, options...)
}

Expand Down Expand Up @@ -340,10 +356,15 @@ func (s *Swamp) Disconnect(t *testing.T, peerA, peerB *nodebuilder.Node) {
require.NoError(t, s.Network.DisconnectPeers(peerA.Host.ID(), peerB.Host.ID()))
}

func WithTrustedPeers(t *testing.T, cfg *nodebuilder.Config, trustedPeers ...*nodebuilder.Node) {
for _, trusted := range trustedPeers {
// SetBootstrapper sets the given bootstrappers as the "bootstrappers" for the
// Swamp test suite. Every new full or light node created on the suite afterwards
// will automatically add the suite's bootstrappers as trusted peers to their config.
// NOTE: Bridge nodes do not automaatically add the bootstrappers as trusted peers.
// NOTE: Use `NewNodeWithStore` to avoid this automatic configuration.
func (s *Swamp) SetBootstrapper(t *testing.T, bootstrappers ...*nodebuilder.Node) {
for _, trusted := range bootstrappers {
addrs, err := peer.AddrInfoToP2pAddrs(host.InfoFromHost(trusted.Host))
require.NoError(t, err)
cfg.Header.TrustedPeers = append(cfg.Header.TrustedPeers, addrs[0].String())
s.Bootstrappers = append(s.Bootstrappers, addrs[0])
}
}
41 changes: 24 additions & 17 deletions nodebuilder/tests/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -51,8 +52,10 @@ func TestSyncAgainstBridge_NonEmptyChain(t *testing.T) {
fillDn := swamp.FillBlocks(ctx, sw.ClientContext, sw.Accounts, bsize, numBlocks)
sw.WaitTillHeight(ctx, numBlocks)

// start a bridge and wait for it to sync to 20
// create a bridge node and set it as the bootstrapper for the suite
bridge := sw.NewBridgeNode()
sw.SetBootstrapper(t, bridge)
// start bridge and wait for it to sync to 20
err := bridge.Start(ctx)
require.NoError(t, err)

Expand All @@ -64,7 +67,6 @@ func TestSyncAgainstBridge_NonEmptyChain(t *testing.T) {
// create a light node that is connected to the bridge node as
// a bootstrapper
cfg := nodebuilder.DefaultConfig(node.Light)
swamp.WithTrustedPeers(t, cfg, bridge)
light := sw.NewNodeWithConfig(node.Light, cfg)
// start light node and wait for it to sync 20 blocks
err = light.Start(ctx)
Expand All @@ -85,7 +87,6 @@ func TestSyncAgainstBridge_NonEmptyChain(t *testing.T) {
t.Run("full sync against bridge", func(t *testing.T) {
// create a full node with bridge node as its bootstrapper
cfg := nodebuilder.DefaultConfig(node.Full)
swamp.WithTrustedPeers(t, cfg, bridge)
full := sw.NewNodeWithConfig(node.Full, cfg)

// let full node sync 20 blocks
Expand Down Expand Up @@ -142,8 +143,10 @@ func TestSyncAgainstBridge_EmptyChain(t *testing.T) {
sw := swamp.NewSwamp(t, swamp.WithBlockTime(btime))
sw.WaitTillHeight(ctx, numBlocks)

// start a bridge and wait for it to sync to 20
// create bridge node and set it as the bootstrapper for the suite
bridge := sw.NewBridgeNode()
sw.SetBootstrapper(t, bridge)
// start bridge and wait for it to sync to 20
err := bridge.Start(ctx)
require.NoError(t, err)
h, err := bridge.HeaderServ.WaitForHeight(ctx, numBlocks)
Expand All @@ -154,7 +157,6 @@ func TestSyncAgainstBridge_EmptyChain(t *testing.T) {
// create a light node that is connected to the bridge node as
// a bootstrapper
cfg := nodebuilder.DefaultConfig(node.Light)
swamp.WithTrustedPeers(t, cfg, bridge)
light := sw.NewNodeWithConfig(node.Light, cfg)
// start light node and wait for it to sync 20 blocks
err = light.Start(ctx)
Expand All @@ -175,7 +177,6 @@ func TestSyncAgainstBridge_EmptyChain(t *testing.T) {
t.Run("full sync against bridge", func(t *testing.T) {
// create a full node with bridge node as its bootstrapper
cfg := nodebuilder.DefaultConfig(node.Full)
swamp.WithTrustedPeers(t, cfg, bridge)
full := sw.NewNodeWithConfig(node.Full, cfg)

// let full node sync 20 blocks
Expand Down Expand Up @@ -220,19 +221,18 @@ func TestSyncStartStopLightWithBridge(t *testing.T) {
fillDn := swamp.FillBlocks(ctx, sw.ClientContext, sw.Accounts, bsize, numBlocks)
sw.WaitTillHeight(ctx, numBlocks)

// create bridge
// create bridge and set it as a bootstrapper
bridge := sw.NewBridgeNode()
// and let bridge node sync up with core network
sw.SetBootstrapper(t, bridge)
// and let bridge node sync up 20 blocks
err := bridge.Start(ctx)
require.NoError(t, err)

h, err := bridge.HeaderServ.WaitForHeight(ctx, numBlocks)
require.NoError(t, err)
require.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, numBlocks))

// create a light node and connect it to the bridge node as a bootstrapper
cfg := nodebuilder.DefaultConfig(node.Light)
swamp.WithTrustedPeers(t, cfg, bridge)
light := sw.NewNodeWithConfig(node.Light, cfg)

// start light node and let it sync to 20
Expand Down Expand Up @@ -288,18 +288,18 @@ func TestSyncLightAgainstFull(t *testing.T) {
fillDn := swamp.FillBlocks(ctx, sw.ClientContext, sw.Accounts, bsize, numBlocks)
sw.WaitTillHeight(ctx, numBlocks)

// start a bridge node and wait for it to sync up 20 blocks
// create bridge and set it as a bootstrapper
bridge := sw.NewBridgeNode()
sw.SetBootstrapper(t, bridge)
// start a bridge node and wait for it to sync up 20 blocks
err := bridge.Start(ctx)
require.NoError(t, err)

h, err := bridge.HeaderServ.WaitForHeight(ctx, numBlocks)
require.NoError(t, err)
assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, numBlocks))

// create a FN with BN as a trusted peer
cfg := nodebuilder.DefaultConfig(node.Full)
swamp.WithTrustedPeers(t, cfg, bridge)
full := sw.NewNodeWithConfig(node.Full, cfg)

// start FN and wait for it to sync up to BN
Expand All @@ -308,9 +308,13 @@ func TestSyncLightAgainstFull(t *testing.T) {
err = full.HeaderServ.SyncWait(ctx)
require.NoError(t, err)

// reset suite bootstrapper list and set full node as a bootstrapper for
// LN to connect to
sw.Bootstrappers = make([]ma.Multiaddr, 0)
sw.SetBootstrapper(t, full)

// create an LN with FN as a trusted peer
cfg = nodebuilder.DefaultConfig(node.Light)
swamp.WithTrustedPeers(t, cfg, full)
light := sw.NewNodeWithConfig(node.Light, cfg)

// ensure there is no direct connection between LN and BN so that
Expand Down Expand Up @@ -357,16 +361,17 @@ func TestSyncLightWithTrustedPeers(t *testing.T) {
fillDn := swamp.FillBlocks(ctx, sw.ClientContext, sw.Accounts, bsize, numBlocks)
sw.WaitTillHeight(ctx, numBlocks)

// create a BN and let it sync to network head
// create a BN and set as a bootstrapper
bridge := sw.NewBridgeNode()
sw.SetBootstrapper(t, bridge)
// let it sync to network head
err := bridge.Start(ctx)
require.NoError(t, err)
err = bridge.HeaderServ.SyncWait(ctx)
require.NoError(t, err)

// create a FN with BN as trusted peer
cfg := nodebuilder.DefaultConfig(node.Full)
swamp.WithTrustedPeers(t, cfg, bridge)
full := sw.NewNodeWithConfig(node.Full, cfg)

// let FN sync to network head
Expand All @@ -375,9 +380,11 @@ func TestSyncLightWithTrustedPeers(t *testing.T) {
err = full.HeaderServ.SyncWait(ctx)
require.NoError(t, err)

// add full node as a bootstrapper for the suite
sw.SetBootstrapper(t, full)

// create a LN with both FN and BN as trusted peers
cfg = nodebuilder.DefaultConfig(node.Light)
swamp.WithTrustedPeers(t, cfg, bridge, full)
light := sw.NewNodeWithConfig(node.Light, cfg)

// let LN sync to network head
Expand Down

0 comments on commit 2883a67

Please sign in to comment.