From 714918a6efb9073aa6e518c908cb7d27667a58e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kami=C5=84ski?= Date: Wed, 12 Mar 2025 12:12:10 +0000 Subject: [PATCH 1/5] Onboarding task: add 2 scenarios around Peer Discovery (#46) * add test connect using static peers * try test start disc * update and comment out test start disc * add negative scenario for discv5 * add more comments * refactor * use fewer nodes * stop and destroy in single defer block --- waku/peer_connections_test.go | 100 +++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/waku/peer_connections_test.go b/waku/peer_connections_test.go index 4971dc1..095a4ab 100644 --- a/waku/peer_connections_test.go +++ b/waku/peer_connections_test.go @@ -90,6 +90,60 @@ func TestConnectMultipleNodesToSingleNode(t *testing.T) { Debug("Test completed successfully: multiple nodes connected to a single node and verified peers") } +func TestConnectUsingMultipleStaticPeers(t *testing.T) { + Debug("Starting TestConnectUsingMultipleStaticPeers") + + node1, err := StartWakuNode("node1", nil) + require.NoError(t, err, "Failed to start Node 1") + + node2, err := StartWakuNode("node2", nil) + require.NoError(t, err, "Failed to start Node 2") + + node3, err := StartWakuNode("node3", nil) + require.NoError(t, err, "Failed to start Node 3") + + addr1, err := node1.ListenAddresses() + require.NoError(t, err, "Failed to get listen addresses for Node 1") + + addr2, err := node2.ListenAddresses() + require.NoError(t, err, "Failed to get listen addresses for Node 2") + + addr3, err := node3.ListenAddresses() + require.NoError(t, err, "Failed to get listen addresses for Node 3") + + node4Config := DefaultWakuConfig + node4Config.Discv5Discovery = false + node4Config.Staticnodes = []string{addr1[0].String(), addr2[0].String(), addr3[0].String()} + + node4, err := StartWakuNode("node4", &node4Config) + require.NoError(t, err, "Failed to start Node 4") + + defer func() { + Debug("Stopping and destroying all Waku nodes") + node1.StopAndDestroy() + node2.StopAndDestroy() + node3.StopAndDestroy() + node4.StopAndDestroy() + }() + + Debug("Verifying connected peers for Node 4") + connectedPeers, err := node4.GetConnectedPeers() + require.NoError(t, err, "Failed to get connected peers for Node 4") + + node1PeerID, err := node1.PeerID() + require.NoError(t, err, "Failed to get PeerID for Node 1") + node2PeerID, err := node2.PeerID() + require.NoError(t, err, "Failed to get PeerID for Node 2") + node3PeerID, err := node3.PeerID() + require.NoError(t, err, "Failed to get PeerID for Node 3") + + require.True(t, slices.Contains(connectedPeers, node1PeerID), "Node 1 should be a peer of Node 4") + require.True(t, slices.Contains(connectedPeers, node2PeerID), "Node 2 should be a peer of Node 4") + require.True(t, slices.Contains(connectedPeers, node3PeerID), "Node 3 should be a peer of Node 4") + + Debug("Test passed: multiple nodes connected to a single node using Static Peers") +} + func TestDiscv5PeerMeshCount(t *testing.T) { Debug("Starting test to verify peer count in mesh using Discv5 after topic subscription") @@ -158,7 +212,49 @@ func TestDiscv5PeerMeshCount(t *testing.T) { Debug("Test successfully verified peer count change after stopping Node3") } -// this test commented as it will fail will be changed to have external ip in future task +func TestDiscv5DisabledNoPeersConnected(t *testing.T) { + Debug("Starting TestDiscv5DisabledNoPeersConnected") + + nodeConfig := DefaultWakuConfig + nodeConfig.Discv5Discovery = false + nodeConfig.Relay = true + + Debug("Creating Node1") + node1, err := StartWakuNode("Node1", &nodeConfig) + require.NoError(t, err, "Failed to start Node1") + + enrNode1, err := node1.ENR() + require.NoError(t, err, "Failed to get ENR for Node1") + nodeConfig.Discv5BootstrapNodes = []string{enrNode1.String()} + + Debug("Creating Node2 with Node1 as Discv5 bootstrap") + node2, err := StartWakuNode("Node2", &nodeConfig) + require.NoError(t, err, "Failed to start Node2") + + defer func() { + Debug("Stopping and destroying all Waku nodes") + node1.StopAndDestroy() + node2.StopAndDestroy() + }() + + Debug("Waiting to ensure no auto-connection") + time.Sleep(15 * time.Second) + + Debug("Verifying number of peers connected to Nodes") + peerCount, err := node1.GetNumConnectedPeers() + require.NoError(t, err, "Failed to get number of peers in mesh for Node1") + Debug("Total number of connected peers for Node1: %d", peerCount) + require.Equal(t, 0, peerCount, "Expected Node1 to have exactly 0 peers in the mesh") + + peerCount, err = node2.GetNumConnectedPeers() + require.NoError(t, err, "Failed to get number of peers in mesh for Node2") + Debug("Total number of connected peers for Node2: %d", peerCount) + require.Equal(t, 0, peerCount, "Expected Node2 to have exactly 0 peers in the mesh") + + Debug("Test passed: all the nodes have 0 peers") +} + +// this test commented as it will fail will be changed to have external ip in future task /* func TestDiscv5GetPeersConnected(t *testing.T) { Debug("Starting test to verify peer count in mesh with 4 nodes using Discv5 (Chained Connection)") @@ -224,4 +320,4 @@ func TestDiscv5GetPeersConnected(t *testing.T) { Debug("Test successfully verified peer count in mesh with 4 nodes using Discv5 (Chained Connection)") } -*/ \ No newline at end of file +*/ From dbbf4b7e7074a25783e75ecb742421e3e49ab221 Mon Sep 17 00:00:00 2001 From: aya Date: Thu, 13 Mar 2025 12:44:08 +0200 Subject: [PATCH 2/5] Change logging to use zap instead of logrus --- go.mod | 7 +------ go.sum | 4 ---- waku/logging.go | 46 +++++++++++++++++++++++++--------------------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/go.mod b/go.mod index 166611d..dcfaad8 100644 --- a/go.mod +++ b/go.mod @@ -38,10 +38,7 @@ require ( google.golang.org/protobuf v1.34.2 ) -require ( - github.com/sirupsen/logrus v1.2.0 - github.com/waku-org/go-waku v0.8.1-0.20241028194639-dd82c24e0057 -) +require github.com/waku-org/go-waku v0.8.1-0.20241028194639-dd82c24e0057 require ( github.com/beorn7/perks v1.0.1 // indirect @@ -59,7 +56,6 @@ require ( github.com/ipfs/go-log/v2 v2.5.1 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/klauspost/cpuid/v2 v2.2.8 // indirect - github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect github.com/libp2p/go-buffer-pool v0.1.0 // indirect github.com/libp2p/go-msgio v0.3.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -83,7 +79,6 @@ require ( golang.org/x/net v0.28.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.24.0 // indirect - golang.org/x/term v0.23.0 // indirect golang.org/x/time v0.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/blake3 v1.3.0 // indirect diff --git a/go.sum b/go.sum index 1d53bd8..dcf97e7 100644 --- a/go.sum +++ b/go.sum @@ -297,7 +297,6 @@ github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg= github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= -github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/koron/go-ssdp v0.0.4 h1:1IDwrghSKYM7yLf7XCzbByg2sJ/JcNOZRXS2jczTwz0= github.com/koron/go-ssdp v0.0.4/go.mod h1:oDXq+E5IL5q0U8uSBcoAXzTzInwy5lEgC91HoKtbmZk= @@ -515,7 +514,6 @@ github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfP github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= @@ -734,8 +732,6 @@ golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= -golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/waku/logging.go b/waku/logging.go index 6d610aa..8be327c 100644 --- a/waku/logging.go +++ b/waku/logging.go @@ -3,41 +3,45 @@ package waku import ( "sync" - "github.com/sirupsen/logrus" + "go.uber.org/zap" ) var ( - once sync.Once - instance *logrus.Logger + once sync.Once + sugar *zap.SugaredLogger ) -// _getLogger ensures we always return the same logger instance (private function) -func _getLogger() *logrus.Logger { +func _getLogger() *zap.SugaredLogger { once.Do(func() { - instance = logrus.New() - instance.SetFormatter(&logrus.TextFormatter{ - FullTimestamp: true, - }) - instance.SetLevel(logrus.DebugLevel) // Set default log level + + config := zap.NewDevelopmentConfig() + l, err := config.Build() + if err != nil { + panic(err) + } + sugar = l.Sugar() }) - return instance + return sugar +} + +func SetLogger(newLogger *zap.Logger) { + once.Do(func() {}) + + sugar = newLogger.Sugar() } -// Debug logs a debug message func Debug(msg string, args ...interface{}) { - _getLogger().WithFields(logrus.Fields{}).Debugf(msg, args...) + _getLogger().Debugf(msg, args...) } -// Info logs an info message func Info(msg string, args ...interface{}) { - _getLogger().WithFields(logrus.Fields{}).Infof(msg, args...) -} - -// Error logs an error message -func Error(msg string, args ...interface{}) { - _getLogger().WithFields(logrus.Fields{}).Errorf(msg, args...) + _getLogger().Infof(msg, args...) } func Warn(msg string, args ...interface{}) { - _getLogger().WithFields(logrus.Fields{}).Warnf(msg, args...) + _getLogger().Warnf(msg, args...) +} + +func Error(msg string, args ...interface{}) { + _getLogger().Errorf(msg, args...) } From 5a9a4ce4b351d2b211bb56905ac3aa008a74a402 Mon Sep 17 00:00:00 2001 From: aya Date: Thu, 13 Mar 2025 15:06:12 +0200 Subject: [PATCH 3/5] update logger --- waku/logging.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/waku/logging.go b/waku/logging.go index 8be327c..66292fe 100644 --- a/waku/logging.go +++ b/waku/logging.go @@ -13,8 +13,9 @@ var ( func _getLogger() *zap.SugaredLogger { once.Do(func() { - config := zap.NewDevelopmentConfig() + config.DisableCaller = true + config.EncoderConfig.CallerKey = "" l, err := config.Build() if err != nil { panic(err) From 6f95d51df46cf8bfa9f64df18690f3a322917b68 Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Thu, 13 Mar 2025 15:22:58 +0200 Subject: [PATCH 4/5] chore: updating README (#52) --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index d4adc8b..8759e0c 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,17 @@ Follow these steps to install and set up the module: ``` Now the module is ready for use in your project. + +### Note + +In order to easily build the libwaku library on demand, it is recommended to add the following target in your project's Makefile: + +``` +LIBWAKU_DEP_PATH=$(shell go list -m -f '{{.Dir}}' github.com/waku-org/waku-go-bindings) + +buildlib: + cd $(LIBWAKU_DEP_PATH) &&\ + sudo mkdir -p third_party &&\ + sudo chown $(USER) third_party &&\ + make -C waku +``` From b4f68a640c33b16d2dd9b04ceac9bf809cd802be Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Wed, 26 Mar 2025 16:29:51 +0200 Subject: [PATCH 5/5] chore: adding link to example-go-bindings repo in readme (#54) --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 8759e0c..206f5b7 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,7 @@ buildlib: sudo chown $(USER) third_party &&\ make -C waku ``` + +## Example Usage + +For an example on how to use this package, please take a look at our [example-go-bindings](https://github.com/gabrielmer/example-waku-go-bindings) repo