From 14307d89c1366ffaa7cea4af27284d5c6feb2f94 Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Mon, 24 Jan 2022 13:35:58 +0200 Subject: [PATCH 1/5] node/config: add WithBootstrapPeers option Add tests for WithMutualPeers and WithBootstrapPeers for both bridge/light types Remove redundancy in WithMockClient test --- node/bridge_test.go | 29 ++++++++++++++++++++++++++--- node/config_opts.go | 9 +++++++++ node/light_test.go | 13 +++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/node/bridge_test.go b/node/bridge_test.go index 7b05fa79f..26a19fcc9 100644 --- a/node/bridge_test.go +++ b/node/bridge_test.go @@ -151,9 +151,32 @@ func TestBridge_WithMockedCoreClient(t *testing.T) { err = node.Start(ctx) require.NoError(t, err) - ctx, cancel = context.WithCancel(context.Background()) - t.Cleanup(cancel) - err = node.Stop(ctx) require.NoError(t, err) } + +func TestBridge_WithMutualPeers(t *testing.T) { + repo := MockStore(t, DefaultConfig(Bridge)) + peers := []string{ + "/ip6/100:0:114b:abc5:e13a:c32f:7a9e:f00a/tcp/2121/p2p/12D3KooWSRqDfpLsQxpyUhLC9oXHD2WuZ2y5FWzDri7LT4Dw9fSi", + "/ip4/192.168.1.10/tcp/2121/p2p/12D3KooWSRqDfpLsQxpyUhLC9oXHD2WuZ2y5FWzDri7LT4Dw9fSi", + } + node, err := New(Bridge, repo, WithMutualPeers(peers)) + require.NoError(t, err) + require.NotNil(t, node) + + assert.Equal(t, node.Config.P2P.MutualPeers, peers) +} + +func TestBridge_WithBootstrapPeers(t *testing.T) { + repo := MockStore(t, DefaultConfig(Bridge)) + peers := []string{ + "/ip6/100:0:114b:abc5:e13a:c32f:7a9e:f00a/tcp/2121/p2p/12D3KooWSRqDfpLsQxpyUhLC9oXHD2WuZ2y5FWzDri7LT4Dw9fSi", + "/ip4/192.168.1.10/tcp/2121/p2p/12D3KooWSRqDfpLsQxpyUhLC9oXHD2WuZ2y5FWzDri7LT4Dw9fSi", + } + node, err := New(Bridge, repo, WithBootstrapPeers(peers)) + require.NoError(t, err) + require.NotNil(t, node) + + assert.Equal(t, node.Config.P2P.BootstrapPeers, peers) +} diff --git a/node/config_opts.go b/node/config_opts.go index 3e217bcd4..dce0bb35b 100644 --- a/node/config_opts.go +++ b/node/config_opts.go @@ -34,9 +34,18 @@ func WithConfig(custom *Config) Option { } } +// WithMutualPeers sets MutualPeers[] config. func WithMutualPeers(addrs []string) Option { return func(cfg *Config, _ *settings) (_ error) { cfg.P2P.MutualPeers = addrs return nil } } + +// WithBootstrapPeers sets BootstrapPeers[] config. +func WithBootstrapPeers(addrs []string) Option { + return func(cfg *Config, _ *settings) (_ error) { + cfg.P2P.BootstrapPeers = addrs + return nil + } +} diff --git a/node/light_test.go b/node/light_test.go index 0cb5c6407..02fac0b6f 100644 --- a/node/light_test.go +++ b/node/light_test.go @@ -62,3 +62,16 @@ func TestNewLightWithHost(t *testing.T) { require.NoError(t, err) assert.Equal(t, node.Host.ID(), nw.Peers()[0]) } + +func TestLight_WithBootstrapPeers(t *testing.T) { + repo := MockStore(t, DefaultConfig(Light)) + peers := []string{ + "/ip6/100:0:114b:abc5:e13a:c32f:7a9e:f00a/tcp/2121/p2p/12D3KooWSRqDfpLsQxpyUhLC9oXHD2WuZ2y5FWzDri7LT4Dw9fSi", + "/ip4/192.168.1.10/tcp/2121/p2p/12D3KooWSRqDfpLsQxpyUhLC9oXHD2WuZ2y5FWzDri7LT4Dw9fSi", + } + node, err := New(Light, repo, WithBootstrapPeers(peers)) + require.NoError(t, err) + require.NotNil(t, node) + + assert.Equal(t, node.Config.P2P.BootstrapPeers, peers) +} From 9a35768928a1db9bf6276271b506bf3c340b0f65 Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Mon, 24 Jan 2022 13:42:34 +0200 Subject: [PATCH 2/5] update changelog --- CHANGELOG-PENDING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-PENDING.md b/CHANGELOG-PENDING.md index d99375467..e62ca4607 100644 --- a/CHANGELOG-PENDING.md +++ b/CHANGELOG-PENDING.md @@ -12,6 +12,7 @@ Month, DD, YYYY ### FEATURES +- [node/config: add WithBootstrapPeers option #375](https://github.com/celestiaorg/celestia-node/pull/375) [@Bidon15](https://github.com/Bidon15) - [feat(cmd): give a birth to cel-shed and p2p key utilities #281](https://github.com/celestiaorg/celestia-node/pull/281) [@Wondertan](https://github.com/Wondertan) - [feat(cmd|node): MutualPeers Node option and CLI flag #280](https://github.com/celestiaorg/celestia-node/pull/280) [@Wondertan](https://github.com/Wondertan) - [node: enhance DI allowing overriding of dependencies](https://github.com/celestiaorg/celestia-node/pull/290) [@Wondertan](https://github.com/Wondertan) From 39e67242559d2954d76e42cdbf07bfd75cffff48 Mon Sep 17 00:00:00 2001 From: Nguyen Nhu Viet Date: Mon, 24 Jan 2022 14:51:52 +0200 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: rene <41963722+renaynay@users.noreply.github.com> --- node/config_opts.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/node/config_opts.go b/node/config_opts.go index dce0bb35b..fc8e8a31e 100644 --- a/node/config_opts.go +++ b/node/config_opts.go @@ -34,7 +34,7 @@ func WithConfig(custom *Config) Option { } } -// WithMutualPeers sets MutualPeers[] config. +// WithMutualPeers sets the `MutualPeers` field in the config. func WithMutualPeers(addrs []string) Option { return func(cfg *Config, _ *settings) (_ error) { cfg.P2P.MutualPeers = addrs @@ -42,7 +42,7 @@ func WithMutualPeers(addrs []string) Option { } } -// WithBootstrapPeers sets BootstrapPeers[] config. +// WithBootstrapPeers sets the `BootstrapPeers` field in the config. func WithBootstrapPeers(addrs []string) Option { return func(cfg *Config, _ *settings) (_ error) { cfg.P2P.BootstrapPeers = addrs From c16b551feb6003a35507baf83c50b2992fefa78a Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Mon, 24 Jan 2022 15:08:08 +0200 Subject: [PATCH 4/5] removing changelog --- CHANGELOG-PENDING.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG-PENDING.md b/CHANGELOG-PENDING.md index e62ca4607..d99375467 100644 --- a/CHANGELOG-PENDING.md +++ b/CHANGELOG-PENDING.md @@ -12,7 +12,6 @@ Month, DD, YYYY ### FEATURES -- [node/config: add WithBootstrapPeers option #375](https://github.com/celestiaorg/celestia-node/pull/375) [@Bidon15](https://github.com/Bidon15) - [feat(cmd): give a birth to cel-shed and p2p key utilities #281](https://github.com/celestiaorg/celestia-node/pull/281) [@Wondertan](https://github.com/Wondertan) - [feat(cmd|node): MutualPeers Node option and CLI flag #280](https://github.com/celestiaorg/celestia-node/pull/280) [@Wondertan](https://github.com/Wondertan) - [node: enhance DI allowing overriding of dependencies](https://github.com/celestiaorg/celestia-node/pull/290) [@Wondertan](https://github.com/Wondertan) From 828a327dd0692fcb2c5597b41464cbf3222e26cd Mon Sep 17 00:00:00 2001 From: Nhu Viet Nguyen Date: Mon, 24 Jan 2022 16:18:05 +0200 Subject: [PATCH 5/5] node/test add withMutualPeers test for the light node --- node/light_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/node/light_test.go b/node/light_test.go index 02fac0b6f..b929757ea 100644 --- a/node/light_test.go +++ b/node/light_test.go @@ -63,6 +63,19 @@ func TestNewLightWithHost(t *testing.T) { assert.Equal(t, node.Host.ID(), nw.Peers()[0]) } +func TestLight_WithMutualPeers(t *testing.T) { + repo := MockStore(t, DefaultConfig(Light)) + peers := []string{ + "/ip6/100:0:114b:abc5:e13a:c32f:7a9e:f00a/tcp/2121/p2p/12D3KooWSRqDfpLsQxpyUhLC9oXHD2WuZ2y5FWzDri7LT4Dw9fSi", + "/ip4/192.168.1.10/tcp/2121/p2p/12D3KooWSRqDfpLsQxpyUhLC9oXHD2WuZ2y5FWzDri7LT4Dw9fSi", + } + node, err := New(Light, repo, WithMutualPeers(peers)) + require.NoError(t, err) + require.NotNil(t, node) + + assert.Equal(t, node.Config.P2P.MutualPeers, peers) +} + func TestLight_WithBootstrapPeers(t *testing.T) { repo := MockStore(t, DefaultConfig(Light)) peers := []string{