Re-introduce deprecated method of requesting messages
During the move to waku/1 we removed handing of a deprecated format.
aa7f591587 (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.
This commit is contained in:
parent
a21f8a39bc
commit
a605442de5
|
@ -91,6 +91,11 @@ type WakuHost interface {
|
||||||
// OnMessagesRequest handles when the peer receive a message request
|
// OnMessagesRequest handles when the peer receive a message request
|
||||||
// this only works if the peer is a mailserver
|
// this only works if the peer is a mailserver
|
||||||
OnMessagesRequest(MessagesRequest, Peer) error
|
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
|
OnBatchAcknowledged(common.Hash, Peer) error
|
||||||
OnP2PRequestCompleted([]byte, Peer) error
|
OnP2PRequestCompleted([]byte, Peer) error
|
||||||
}
|
}
|
||||||
|
|
|
@ -301,6 +301,13 @@ func (p *Peer) handleP2PRequestCode(packet p2p.Msg) error {
|
||||||
r := bytes.NewReader(data)
|
r := bytes.NewReader(data)
|
||||||
packet.Payload = r
|
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
|
// As we failed to decode the request, let's set the offset
|
||||||
// to the beginning and try decode it again.
|
// to the beginning and try decode it again.
|
||||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||||
|
|
|
@ -301,6 +301,13 @@ func (p *Peer) handleP2PRequestCode(packet p2p.Msg) error {
|
||||||
r := bytes.NewReader(data)
|
r := bytes.NewReader(data)
|
||||||
packet.Payload = r
|
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
|
// As we failed to decode the request, let's set the offset
|
||||||
// to the beginning and try decode it again.
|
// to the beginning and try decode it again.
|
||||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||||
|
|
|
@ -1103,6 +1103,11 @@ func (w *Waku) OnMessagesRequest(request common.MessagesRequest, p common.Peer)
|
||||||
return nil
|
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 {
|
func (w *Waku) OnP2PRequestCompleted(payload []byte, p common.Peer) error {
|
||||||
msEvent, err := CreateMailServerEvent(p.EnodeID(), payload)
|
msEvent, err := CreateMailServerEvent(p.EnodeID(), payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
package waku
|
package waku
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
mrand "math/rand"
|
mrand "math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -35,6 +36,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
"github.com/status-im/status-go/protocol/tt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWakuV0(t *testing.T) {
|
func TestWakuV0(t *testing.T) {
|
||||||
|
@ -457,6 +459,65 @@ func (s *WakuTestSuite) TestRequestSentEventWithExpiry() {
|
||||||
verifyEvent(common.EventMailServerRequestExpired)
|
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() {
|
func (s *WakuTestSuite) TestSendMessagesRequest() {
|
||||||
validMessagesRequest := common.MessagesRequest{
|
validMessagesRequest := common.MessagesRequest{
|
||||||
ID: make([]byte, 32),
|
ID: make([]byte, 32),
|
||||||
|
|
Loading…
Reference in New Issue