From 7943f8f65765c194fe05e16e40210daad3722798 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 18 Aug 2022 12:27:10 -0400 Subject: [PATCH] fix: close eth connection on stop --- waku/v2/node/wakunode2.go | 2 ++ waku/v2/node/wakunode2_no_rln.go | 5 ++++- waku/v2/node/wakunode2_rln.go | 5 +++++ waku/v2/protocol/rln/onchain_test.go | 12 +++++++++++- waku/v2/protocol/rln/waku_rln_relay.go | 11 ++++++++++- waku/v2/protocol/rln/web3.go | 8 +++++--- 6 files changed, 37 insertions(+), 6 deletions(-) diff --git a/waku/v2/node/wakunode2.go b/waku/v2/node/wakunode2.go index 03709035..e4792216 100644 --- a/waku/v2/node/wakunode2.go +++ b/waku/v2/node/wakunode2.go @@ -57,6 +57,7 @@ type RLNRelay interface { MembershipKeyPair() MembershipKeyPair MembershipIndex() uint AppendRLNProof(msg *pb.WakuMessage, senderEpochTime time.Time) error + Stop() } type WakuNode struct { @@ -363,6 +364,7 @@ func (w *WakuNode) Stop() { w.relay.Stop() w.lightPush.Stop() w.store.Stop() + w.stopRlnRelay() w.host.Close() diff --git a/waku/v2/node/wakunode2_no_rln.go b/waku/v2/node/wakunode2_no_rln.go index 6b798f73..b700ab36 100644 --- a/waku/v2/node/wakunode2_no_rln.go +++ b/waku/v2/node/wakunode2_no_rln.go @@ -3,7 +3,6 @@ package node -// RLNRelay is used to access any operation related to Waku RLN protocol func (w *WakuNode) RLNRelay() RLNRelay { return nil } @@ -11,3 +10,7 @@ func (w *WakuNode) RLNRelay() RLNRelay { func (w *WakuNode) mountRlnRelay() error { return nil } + +func (w *WakuNode) stopRlnRelay() error { + return nil +} diff --git a/waku/v2/node/wakunode2_rln.go b/waku/v2/node/wakunode2_rln.go index 745e7349..03dbe3a6 100644 --- a/waku/v2/node/wakunode2_rln.go +++ b/waku/v2/node/wakunode2_rln.go @@ -94,3 +94,8 @@ func (w *WakuNode) mountRlnRelay() error { return nil } + +func (w *WakuNode) stopRlnRelay() error { + w.rlnRelay.Stop() + return nil +} diff --git a/waku/v2/protocol/rln/onchain_test.go b/waku/v2/protocol/rln/onchain_test.go index 610a4099..1e0566ad 100644 --- a/waku/v2/protocol/rln/onchain_test.go +++ b/waku/v2/protocol/rln/onchain_test.go @@ -140,8 +140,10 @@ func (s *WakuRLNRelayDynamicSuite) TestDynamicGroupManagement() { return nil } + errChan := make(chan error, 1) + // mount the handler for listening to the contract events - go rlnPeer.HandleGroupUpdates(handler) + go rlnPeer.HandleGroupUpdates(handler, errChan) // Register first member s.register(s.u1PrivKey, toBigInt(keyPair.IDCommitment[:])) @@ -150,6 +152,14 @@ func (s *WakuRLNRelayDynamicSuite) TestDynamicGroupManagement() { s.register(s.u2PrivKey, toBigInt(keyPair2.IDCommitment[:])) wg.Wait() + + timer1 := time.NewTimer(2 * time.Second) + select { + case <-timer1.C: + return + case err = <-errChan: + s.Require().NoError(err) + } } func (s *WakuRLNRelayDynamicSuite) TestInsertKeyMembershipContract() { diff --git a/waku/v2/protocol/rln/waku_rln_relay.go b/waku/v2/protocol/rln/waku_rln_relay.go index 7a8fc14f..917eb2eb 100644 --- a/waku/v2/protocol/rln/waku_rln_relay.go +++ b/waku/v2/protocol/rln/waku_rln_relay.go @@ -10,6 +10,7 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient" proto "github.com/golang/protobuf/proto" "github.com/libp2p/go-libp2p-core/peer" pubsub "github.com/libp2p/go-libp2p-pubsub" @@ -40,7 +41,9 @@ type WakuRLNRelay struct { // TODO may need to erase this ethAccountPrivateKey when is not used // TODO may need to make ethAccountPrivateKey mandatory ethAccountPrivateKey *ecdsa.PrivateKey - RLN *r.RLN + ethClient *ethclient.Client + + RLN *r.RLN // pubsubTopic is the topic for which rln relay is mounted pubsubTopic string contentTopic string @@ -50,6 +53,12 @@ type WakuRLNRelay struct { log *zap.Logger } +func (rln *WakuRLNRelay) Stop() { + if rln.ethClient != nil { + rln.ethClient.Close() + } +} + func StaticSetup(rlnRelayMemIndex r.MembershipIndex) ([]r.IDCommitment, r.MembershipKeyPair, r.MembershipIndex, error) { // static group groupKeys := r.STATIC_GROUP_KEYS diff --git a/waku/v2/protocol/rln/web3.go b/waku/v2/protocol/rln/web3.go index 36ed9f95..919d5cb1 100644 --- a/waku/v2/protocol/rln/web3.go +++ b/waku/v2/protocol/rln/web3.go @@ -125,7 +125,7 @@ func (rln *WakuRLNRelay) HandleGroupUpdates(handler RegistrationEventHandler, er errChan <- err return } - defer backend.Close() + rln.ethClient = backend rlnContract, err := contracts.NewRLN(rln.membershipContractAddress, backend) if err != nil { @@ -181,6 +181,7 @@ func (rln *WakuRLNRelay) watchNewEvents(rlnContract *contracts.RLN, handler Regi if err != nil { errCh <- err } + defer subs.Unsubscribe() close(doneCh) @@ -189,7 +190,6 @@ func (rln *WakuRLNRelay) watchNewEvents(rlnContract *contracts.RLN, handler Regi case evt := <-logSink: err = processLogs(evt, handler) if err != nil { - // TODO: should this stop the chat app? rln.log.Error("processing rln log", zap.Error(err)) } case <-rln.ctx.Done(): @@ -197,7 +197,9 @@ func (rln *WakuRLNRelay) watchNewEvents(rlnContract *contracts.RLN, handler Regi close(logSink) return case err := <-subs.Err(): - rln.log.Error("watching new events", zap.Error(err)) + if err != nil { + rln.log.Error("watching new events", zap.Error(err)) + } close(logSink) return }