From a605442de5e361f33fa465df7d9c7cb146a878a2 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Tue, 12 May 2020 15:05:24 +0200 Subject: [PATCH] Re-introduce deprecated method of requesting messages During the move to waku/1 we removed handing of a deprecated format. https://github.com/status-im/status-go/commit/aa7f5915873b8cd7a0a06662fa80dbf93aeec32f#diff-ea5e44cf3db4a12b3a2a246c7fa39602R290 Turns out that no client is using the new format and the only live client is using the deprecated format. This commit re-introduces the functionality. --- VERSION | 2 +- waku/common/protocol.go | 5 ++++ waku/v0/peer.go | 7 +++++ waku/v1/peer.go | 7 +++++ waku/waku.go | 5 ++++ waku/waku_version_test.go | 61 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 7f422a161..3f99a6d57 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.53.0 +0.53.1 diff --git a/waku/common/protocol.go b/waku/common/protocol.go index ab503709a..90bc0aa5b 100644 --- a/waku/common/protocol.go +++ b/waku/common/protocol.go @@ -91,6 +91,11 @@ type WakuHost interface { // OnMessagesRequest handles when the peer receive a message request // this only works if the peer is a mailserver OnMessagesRequest(MessagesRequest, Peer) error + // OnDeprecatedMessagesRequest handles when the peer receive a message request + // using the *Envelope format. Currently the only production client (status-react) + // is exclusively using this one. + OnDeprecatedMessagesRequest(*Envelope, Peer) error + OnBatchAcknowledged(common.Hash, Peer) error OnP2PRequestCompleted([]byte, Peer) error } diff --git a/waku/v0/peer.go b/waku/v0/peer.go index 401390d78..843e73af7 100644 --- a/waku/v0/peer.go +++ b/waku/v0/peer.go @@ -301,6 +301,13 @@ func (p *Peer) handleP2PRequestCode(packet p2p.Msg) error { r := bytes.NewReader(data) packet.Payload = r + var requestDeprecated common.Envelope + errDepReq := packet.Decode(&requestDeprecated) + if errDepReq == nil { + return p.host.OnDeprecatedMessagesRequest(&requestDeprecated, p) + } + p.logger.Info("failed to decode p2p request message (deprecated)", zap.Binary("peer", p.ID()), zap.Error(errDepReq)) + // As we failed to decode the request, let's set the offset // to the beginning and try decode it again. if _, err := r.Seek(0, io.SeekStart); err != nil { diff --git a/waku/v1/peer.go b/waku/v1/peer.go index 2891797af..e9277b0f9 100644 --- a/waku/v1/peer.go +++ b/waku/v1/peer.go @@ -301,6 +301,13 @@ func (p *Peer) handleP2PRequestCode(packet p2p.Msg) error { r := bytes.NewReader(data) packet.Payload = r + var requestDeprecated common.Envelope + errDepReq := packet.Decode(&requestDeprecated) + if errDepReq == nil { + return p.host.OnDeprecatedMessagesRequest(&requestDeprecated, p) + } + p.logger.Info("failed to decode p2p request message (deprecated)", zap.Binary("peer", p.ID()), zap.Error(errDepReq)) + // As we failed to decode the request, let's set the offset // to the beginning and try decode it again. if _, err := r.Seek(0, io.SeekStart); err != nil { diff --git a/waku/waku.go b/waku/waku.go index 1e83241be..78557314e 100644 --- a/waku/waku.go +++ b/waku/waku.go @@ -1103,6 +1103,11 @@ func (w *Waku) OnMessagesRequest(request common.MessagesRequest, p common.Peer) return nil } +func (w *Waku) OnDeprecatedMessagesRequest(request *common.Envelope, p common.Peer) error { + w.mailServer.DeliverMail(p.ID(), request) + return nil +} + func (w *Waku) OnP2PRequestCompleted(payload []byte, p common.Peer) error { msEvent, err := CreateMailServerEvent(p.EnodeID(), payload) if err != nil { diff --git a/waku/waku_version_test.go b/waku/waku_version_test.go index b88910889..8029e5dcd 100644 --- a/waku/waku_version_test.go +++ b/waku/waku_version_test.go @@ -19,6 +19,7 @@ package waku import ( + "errors" mrand "math/rand" "testing" "time" @@ -35,6 +36,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" + "github.com/status-im/status-go/protocol/tt" ) func TestWakuV0(t *testing.T) { @@ -457,6 +459,65 @@ func (s *WakuTestSuite) TestRequestSentEventWithExpiry() { verifyEvent(common.EventMailServerRequestExpired) } +type MockMailserver struct { + deliverMail func([]byte, *common.Envelope) +} + +func (*MockMailserver) Archive(e *common.Envelope) { +} + +func (*MockMailserver) Deliver(peerID []byte, r common.MessagesRequest) { +} + +func (m *MockMailserver) DeliverMail(peerID []byte, e *common.Envelope) { + + if m.deliverMail != nil { + m.deliverMail(peerID, e) + } +} + +func (s *WakuTestSuite) TestDeprecatedDeliverMail() { + + w1 := New(nil, nil) + w2 := New(nil, nil) + + var deliverMailCalled bool + + w2.RegisterMailServer(&MockMailserver{ + deliverMail: func(peerID []byte, e *common.Envelope) { + deliverMailCalled = true + }, + }) + + rw1, rw2 := p2p.MsgPipe() + p1 := s.newPeer(w1, p2p.NewPeer(enode.ID{1}, "1", []p2p.Cap{{"waku", 0}}), rw2, nil) + + go func() { handleError(s.T(), w1.HandlePeer(p1, rw2)) }() + + timer := time.AfterFunc(5*time.Second, func() { + handleError(s.T(), rw1.Close()) + }) + peer2 := s.newPeer(w2, p2p.NewPeer(enode.ID{1}, "1", nil), rw1, nil) + s.Require().NoError(peer2.Start()) + + go func() { handleError(s.T(), peer2.Run()) }() + + s.Require().NoError(w1.RequestHistoricMessages(p1.ID(), &common.Envelope{Data: []byte{1}})) + + err := tt.RetryWithBackOff(func() error { + if !deliverMailCalled { + return errors.New("DeliverMail not called") + } + return nil + }) + s.Require().NoError(err) + s.Require().NoError(rw1.Close()) + s.Require().NoError(rw2.Close()) + + timer.Stop() + +} + func (s *WakuTestSuite) TestSendMessagesRequest() { validMessagesRequest := common.MessagesRequest{ ID: make([]byte, 32),