From ad6e7b6f899014d86328b33e1e0d4e6bb94f9fb5 Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Wed, 9 Mar 2022 17:48:12 +0200 Subject: [PATCH 1/4] swamp: add FullNode sync tests --- node/tests/swamp/swamp.go | 35 +++++++++++++++++- node/tests/sync_test.go | 75 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/node/tests/swamp/swamp.go b/node/tests/swamp/swamp.go index efe4d522de..1616aaf7da 100644 --- a/node/tests/swamp/swamp.go +++ b/node/tests/swamp/swamp.go @@ -32,13 +32,14 @@ var queryEvent string = types.QueryForEvent(types.EventNewBlock).String() // Swamp represents the main functionality that is needed for the test-case: // - Network to link the nodes // - CoreClient to share between Bridge nodes -// - Slices of created Bridge/Light Nodes +// - 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 CoreClient core.Client BridgeNodes []*node.Node + FullNodes []*node.Node LightNodes []*node.Node trustedHash string } @@ -185,6 +186,15 @@ func (s *Swamp) NewBridgeNode(options ...node.Option) *node.Node { return s.NewBridgeNodeWithStore(store, options...) } +// NewFullNode creates a new instance of a FullNode providing a default config +// and a mockstore to the NewFullNodeWithStore method +func (s *Swamp) NewFullNode(options ...node.Option) *node.Node { + cfg := node.DefaultConfig(node.Full) + store := node.MockStore(s.t, cfg) + + return s.NewFullNodeWithStore(store, options...) +} + // NewLightNode creates a new instance of a LightNode providing a default config // and a mockstore to the NewLightNodeWithStore method func (s *Swamp) NewLightNode(options ...node.Option) *node.Node { @@ -215,6 +225,26 @@ func (s *Swamp) NewBridgeNodeWithStore(store node.Store, options ...node.Option) return node } +// NewFullNodeWithStore creates a new instance of FullNode with predefined Store. +// Afterwards, the instance is stored in the swamp's FullNodes slice +func (s *Swamp) NewFullNodeWithStore(store node.Store, options ...node.Option) *node.Node { + ks, err := store.Keystore() + require.NoError(s.t, err) + + // TODO(@Bidon15): If for some reason, we receive one of existing options + // like from the test case, we need to check them and not use + // default that are set here + options = append(options, + node.WithHost(s.createPeer(ks)), + node.WithTrustedHash(s.trustedHash), + ) + + node, err := node.New(node.Full, store, options...) + require.NoError(s.t, err) + s.FullNodes = append(s.FullNodes, node) + return node +} + // NewLightNodeWithStore creates a new instance of LightNode with predefined Store. // Afterwards, the instance is stored in the swamp's LightNodes slice func (s *Swamp) NewLightNodeWithStore(store node.Store, options ...node.Option) *node.Node { @@ -248,6 +278,9 @@ func (s *Swamp) RemoveNode(n *node.Node, t node.Type) error { case node.Bridge: s.BridgeNodes, err = s.remove(n, s.BridgeNodes) return err + case node.Full: + s.FullNodes, err = s.remove(n, s.FullNodes) + return err default: return fmt.Errorf("no such type or node") } diff --git a/node/tests/sync_test.go b/node/tests/sync_test.go index b7766a09d9..3fd5f5490f 100644 --- a/node/tests/sync_test.go +++ b/node/tests/sync_test.go @@ -112,3 +112,78 @@ func TestSyncStartStopLightWithBridge(t *testing.T) { assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 40)) } + +func TestSyncFullWithBridge(t *testing.T) { + sw := swamp.NewSwamp(t, swamp.DefaultComponents()) + + bridge := sw.NewBridgeNode() + + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + t.Cleanup(cancel) + + sw.WaitTillHeight(ctx, 20) + + err := bridge.Start(ctx) + require.NoError(t, err) + + h, err := bridge.HeaderServ.GetByHeight(ctx, 20) + require.NoError(t, err) + + assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 20)) + + addrs, err := peer.AddrInfoToP2pAddrs(host.InfoFromHost(bridge.Host)) + require.NoError(t, err) + + full := sw.NewFullNode(node.WithTrustedPeer(addrs[0].String())) + require.NoError(t, full.Start(ctx)) + + h, err = full.HeaderServ.GetByHeight(ctx, 30) + require.NoError(t, err) + + assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 30)) +} + +func TestSyncLightWithFull(t *testing.T) { + sw := swamp.NewSwamp(t, swamp.DefaultComponents()) + + bridge := sw.NewBridgeNode() + + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + t.Cleanup(cancel) + + sw.WaitTillHeight(ctx, 20) + + err := bridge.Start(ctx) + require.NoError(t, err) + + h, err := bridge.HeaderServ.GetByHeight(ctx, 20) + require.NoError(t, err) + + assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 20)) + + addrs, err := peer.AddrInfoToP2pAddrs(host.InfoFromHost(bridge.Host)) + require.NoError(t, err) + + full := sw.NewFullNode(node.WithTrustedPeer(addrs[0].String())) + require.NoError(t, full.Start(ctx)) + + h, err = full.HeaderServ.GetByHeight(ctx, 30) + require.NoError(t, err) + + assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 30)) + + addrs, err = peer.AddrInfoToP2pAddrs(host.InfoFromHost(full.Host)) + require.NoError(t, err) + light := sw.NewLightNode(node.WithTrustedPeer(addrs[0].String())) + + err = sw.Network.UnlinkPeers(bridge.Host.ID(), light.Host.ID()) + require.NoError(t, err) + + err = light.Start(ctx) + require.NoError(t, err) + + h, err = light.HeaderServ.GetByHeight(ctx, 60) + require.NoError(t, err) + + assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 60)) +} From c958552f848db63b5b49bd07edc86c971d5277b9 Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Wed, 9 Mar 2022 18:09:53 +0200 Subject: [PATCH 2/4] add test-case comments --- node/tests/sync_test.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/node/tests/sync_test.go b/node/tests/sync_test.go index 3fd5f5490f..43090de63a 100644 --- a/node/tests/sync_test.go +++ b/node/tests/sync_test.go @@ -113,6 +113,16 @@ func TestSyncStartStopLightWithBridge(t *testing.T) { assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 40)) } +/* +Test-Case: Sync a Full Node with a Bridge Node +Steps: +1. Create a Bridge Node(BN) +2. Start a BN +3. Check BN is synced to height 20 +4. Create a Full Node(FN) with a trusted peer +5. Start a FN with a defined connection to the BN +6. Check FN is synced to height 30 +*/ func TestSyncFullWithBridge(t *testing.T) { sw := swamp.NewSwamp(t, swamp.DefaultComponents()) @@ -143,6 +153,22 @@ func TestSyncFullWithBridge(t *testing.T) { assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 30)) } +/* +Test-Case: Sync a Light Node from a Full Node +Pre-Requisites: +- CoreClient is started by swamp +- CoreClient has generated 20 blocks +Steps: +1. Create a Bridge Node(BN) +2. Start a BN +3. Check BN is synced to height 20 +4. Create a Full Node(FN) with a trusted peer +5. Start a FN with a defined connection to the BN +6. Check FN is synced to height 30 +7. Start a LN with a defined connection to the FN +8. Start LN +9. Check LN is synced to height 50 +*/ func TestSyncLightWithFull(t *testing.T) { sw := swamp.NewSwamp(t, swamp.DefaultComponents()) @@ -182,8 +208,8 @@ func TestSyncLightWithFull(t *testing.T) { err = light.Start(ctx) require.NoError(t, err) - h, err = light.HeaderServ.GetByHeight(ctx, 60) + h, err = light.HeaderServ.GetByHeight(ctx, 50) require.NoError(t, err) - assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 60)) + assert.EqualValues(t, h.Commit.BlockID.Hash, sw.GetCoreBlockHashByHeight(ctx, 50)) } From 85a670aa88d3357ff0839d1dc320001e6c429fef Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Wed, 9 Mar 2022 18:21:03 +0200 Subject: [PATCH 3/4] add FullNodes to stop process after test-execution --- node/tests/swamp/swamp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/tests/swamp/swamp.go b/node/tests/swamp/swamp.go index 1616aaf7da..a106c25b61 100644 --- a/node/tests/swamp/swamp.go +++ b/node/tests/swamp/swamp.go @@ -66,7 +66,7 @@ func NewSwamp(t *testing.T, ic *Components) *Swamp { require.NoError(t, err) swp.t.Cleanup(func() { - swp.stopAllNodes(ctx, swp.BridgeNodes, swp.LightNodes) + swp.stopAllNodes(ctx, swp.BridgeNodes, swp.FullNodes, swp.LightNodes) }) return swp From 5a1bbbe2d9a31dcd420e7ef0c9854d3fe04fdd1a Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Thu, 10 Mar 2022 12:13:17 +0200 Subject: [PATCH 4/4] swamp: edit test-case desc to a more readable state --- node/tests/sync_test.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/node/tests/sync_test.go b/node/tests/sync_test.go index 43090de63a..904364d831 100644 --- a/node/tests/sync_test.go +++ b/node/tests/sync_test.go @@ -119,8 +119,8 @@ Steps: 1. Create a Bridge Node(BN) 2. Start a BN 3. Check BN is synced to height 20 -4. Create a Full Node(FN) with a trusted peer -5. Start a FN with a defined connection to the BN +4. Create a Full Node(FN) with a connection to BN as a trusted peer +5. Start a FN 6. Check FN is synced to height 30 */ func TestSyncFullWithBridge(t *testing.T) { @@ -162,12 +162,13 @@ Steps: 1. Create a Bridge Node(BN) 2. Start a BN 3. Check BN is synced to height 20 -4. Create a Full Node(FN) with a trusted peer -5. Start a FN with a defined connection to the BN +4. Create a Full Node(FN) with a connection to BN as a trusted peer +5. Start a FN 6. Check FN is synced to height 30 -7. Start a LN with a defined connection to the FN -8. Start LN -9. Check LN is synced to height 50 +7. Create a Light Node(LN) with a connection to FN as a trusted peer +8. Start a LN with a defined connection to the FN +9. Start LN +10. Check LN is synced to height 50 */ func TestSyncLightWithFull(t *testing.T) { sw := swamp.NewSwamp(t, swamp.DefaultComponents())