2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
2018-04-26 05:56:19 +00:00
|
|
|
"context"
|
2019-03-15 13:27:08 +00:00
|
|
|
"encoding/hex"
|
2019-04-30 06:46:12 +00:00
|
|
|
"errors"
|
2018-04-11 15:41:51 +00:00
|
|
|
"fmt"
|
2018-12-10 13:11:19 +00:00
|
|
|
"io/ioutil"
|
2018-04-26 05:56:19 +00:00
|
|
|
"math"
|
2019-01-21 14:00:10 +00:00
|
|
|
"net"
|
2018-09-24 18:07:34 +00:00
|
|
|
"os"
|
2019-03-01 13:36:21 +00:00
|
|
|
"sync/atomic"
|
2018-04-11 15:41:51 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-01-21 14:00:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-11-21 10:22:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/status-im/status-go/mailserver"
|
2019-01-17 12:56:22 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-07-04 09:30:57 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2019-02-20 17:10:59 +00:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2018-09-25 07:05:38 +00:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2018-12-12 09:39:00 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
2019-01-25 10:31:51 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
2019-01-21 14:00:10 +00:00
|
|
|
const (
|
|
|
|
// internal whisper protocol codes
|
|
|
|
statusCode = 0
|
2019-02-20 17:10:59 +00:00
|
|
|
messagesCode = 1
|
2019-03-01 13:36:21 +00:00
|
|
|
batchAcknowledgeCode = 11
|
2019-01-21 14:00:10 +00:00
|
|
|
p2pRequestCompleteCode = 125
|
|
|
|
)
|
|
|
|
|
2019-04-02 10:40:45 +00:00
|
|
|
type failureMessage struct {
|
|
|
|
Hash common.Hash
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:52:22 +00:00
|
|
|
func newHandlerMock(buf int) handlerMock {
|
|
|
|
return handlerMock{
|
2018-06-15 15:12:31 +00:00
|
|
|
confirmations: make(chan common.Hash, buf),
|
2019-04-02 10:40:45 +00:00
|
|
|
expirations: make(chan failureMessage, buf),
|
2018-06-15 15:12:31 +00:00
|
|
|
requestsCompleted: make(chan common.Hash, buf),
|
|
|
|
requestsExpired: make(chan common.Hash, buf),
|
2018-10-18 10:25:00 +00:00
|
|
|
requestsFailed: make(chan common.Hash, buf),
|
2018-04-13 05:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type handlerMock struct {
|
2018-06-15 15:12:31 +00:00
|
|
|
confirmations chan common.Hash
|
2019-04-02 10:40:45 +00:00
|
|
|
expirations chan failureMessage
|
2018-06-15 15:12:31 +00:00
|
|
|
requestsCompleted chan common.Hash
|
|
|
|
requestsExpired chan common.Hash
|
2018-10-18 10:25:00 +00:00
|
|
|
requestsFailed chan common.Hash
|
2018-04-13 05:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t handlerMock) EnvelopeSent(hash common.Hash) {
|
|
|
|
t.confirmations <- hash
|
|
|
|
}
|
|
|
|
|
2019-04-02 10:40:45 +00:00
|
|
|
func (t handlerMock) EnvelopeExpired(hash common.Hash, err error) {
|
|
|
|
t.expirations <- failureMessage{Hash: hash, Error: err}
|
2018-04-13 05:52:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 10:25:00 +00:00
|
|
|
func (t handlerMock) MailServerRequestCompleted(requestID common.Hash, lastEnvelopeHash common.Hash, cursor []byte, err error) {
|
|
|
|
if err == nil {
|
|
|
|
t.requestsCompleted <- requestID
|
|
|
|
} else {
|
|
|
|
t.requestsFailed <- requestID
|
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t handlerMock) MailServerRequestExpired(hash common.Hash) {
|
|
|
|
t.requestsExpired <- hash
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
func TestShhExtSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ShhExtSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShhExtSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
nodes []*node.Node
|
|
|
|
services []*Service
|
|
|
|
whisper []*whisper.Whisper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) SetupTest() {
|
|
|
|
s.nodes = make([]*node.Node, 2)
|
|
|
|
s.services = make([]*Service, 2)
|
|
|
|
s.whisper = make([]*whisper.Whisper, 2)
|
2018-12-10 13:11:19 +00:00
|
|
|
|
|
|
|
directory, err := ioutil.TempDir("", "status-go-testing")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
for i := range s.nodes {
|
|
|
|
i := i // bind i to be usable in service constructors
|
|
|
|
cfg := &node.Config{
|
|
|
|
Name: fmt.Sprintf("node-%d", i),
|
|
|
|
P2P: p2p.Config{
|
|
|
|
NoDiscovery: true,
|
2018-04-13 05:52:22 +00:00
|
|
|
MaxPeers: 1,
|
|
|
|
ListenAddr: ":0",
|
2018-04-11 15:41:51 +00:00
|
|
|
},
|
2019-02-01 17:02:52 +00:00
|
|
|
NoUSB: true,
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
stack, err := node.New(cfg)
|
|
|
|
s.NoError(err)
|
|
|
|
s.whisper[i] = whisper.New(nil)
|
|
|
|
s.NoError(stack.Register(func(n *node.ServiceContext) (node.Service, error) {
|
|
|
|
return s.whisper[i], nil
|
|
|
|
}))
|
2018-12-10 13:11:19 +00:00
|
|
|
|
2019-01-17 12:56:22 +00:00
|
|
|
config := params.ShhextConfig{
|
2018-11-21 10:22:30 +00:00
|
|
|
InstallationID: "1",
|
2019-01-17 12:56:22 +00:00
|
|
|
BackupDisabledDataDir: directory,
|
2018-12-10 13:11:19 +00:00
|
|
|
PFSEnabled: true,
|
2018-11-21 10:22:30 +00:00
|
|
|
MailServerConfirmations: true,
|
2018-12-05 13:57:05 +00:00
|
|
|
ConnectionTarget: 10,
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
db, err := leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.services[i] = New(s.whisper[i], nil, db, config)
|
2018-04-11 15:41:51 +00:00
|
|
|
s.NoError(stack.Register(func(n *node.ServiceContext) (node.Service, error) {
|
|
|
|
return s.services[i], nil
|
|
|
|
}))
|
|
|
|
s.Require().NoError(stack.Start())
|
|
|
|
s.nodes[i] = stack
|
|
|
|
}
|
2019-02-20 06:57:57 +00:00
|
|
|
s.services[0].envelopesMonitor.handler = newHandlerMock(1)
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 13:11:19 +00:00
|
|
|
func (s *ShhExtSuite) TestInitProtocol() {
|
2019-01-25 10:31:51 +00:00
|
|
|
err := s.services[0].InitProtocolWithPassword("example-address", "`090///\nhtaa\rhta9x8923)$$'23")
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
digest := sha3.Sum256([]byte("`090///\nhtaa\rhta9x8923)$$'23"))
|
|
|
|
encKey := fmt.Sprintf("%x", digest)
|
|
|
|
err = s.services[0].InitProtocolWithEncyptionKey("example-address", encKey)
|
2018-12-10 13:11:19 +00:00
|
|
|
s.NoError(err)
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
func (s *ShhExtSuite) TestPostMessageWithConfirmation() {
|
2018-04-13 05:52:22 +00:00
|
|
|
mock := newHandlerMock(1)
|
2019-02-20 06:57:57 +00:00
|
|
|
s.services[0].envelopesMonitor.handler = mock
|
2018-12-12 09:39:00 +00:00
|
|
|
s.Require().NoError(s.services[0].UpdateMailservers([]*enode.Node{s.nodes[1].Server().Self()}))
|
2018-04-13 05:52:22 +00:00
|
|
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
2018-04-11 15:41:51 +00:00
|
|
|
symID, err := s.whisper[0].GenerateSymKey()
|
|
|
|
s.NoError(err)
|
|
|
|
client, err := s.nodes[0].Attach()
|
|
|
|
s.NoError(err)
|
|
|
|
var hash common.Hash
|
2019-03-01 13:36:21 +00:00
|
|
|
message := whisper.NewMessage{
|
2018-04-11 15:41:51 +00:00
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
2019-03-01 13:36:21 +00:00
|
|
|
}
|
|
|
|
mid := messageID(message)
|
|
|
|
s.NoError(client.Call(&hash, "shhext_post", message))
|
2018-04-11 15:41:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
select {
|
2018-04-13 05:52:22 +00:00
|
|
|
case confirmed := <-mock.confirmations:
|
2019-03-01 13:36:21 +00:00
|
|
|
s.Equal(mid, confirmed)
|
2018-05-04 09:08:04 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2018-04-11 15:41:51 +00:00
|
|
|
s.Fail("timed out while waiting for confirmation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 10:40:45 +00:00
|
|
|
func (s *ShhExtSuite) testWaitMessageExpired(expectedError string, ttl uint32) {
|
2018-04-13 05:52:22 +00:00
|
|
|
mock := newHandlerMock(1)
|
2019-02-20 06:57:57 +00:00
|
|
|
s.services[0].envelopesMonitor.handler = mock
|
2018-04-13 05:52:22 +00:00
|
|
|
symID, err := s.whisper[0].GenerateSymKey()
|
|
|
|
s.NoError(err)
|
|
|
|
client, err := s.nodes[0].Attach()
|
|
|
|
s.NoError(err)
|
|
|
|
var hash common.Hash
|
2019-03-01 13:36:21 +00:00
|
|
|
message := whisper.NewMessage{
|
2018-04-13 05:52:22 +00:00
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
2019-04-02 10:40:45 +00:00
|
|
|
TTL: ttl,
|
2018-04-13 05:52:22 +00:00
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
2019-03-01 13:36:21 +00:00
|
|
|
}
|
|
|
|
mid := messageID(message)
|
|
|
|
s.NoError(client.Call(&hash, "shhext_post", message))
|
2018-04-13 05:52:22 +00:00
|
|
|
s.NoError(err)
|
|
|
|
select {
|
|
|
|
case expired := <-mock.expirations:
|
2019-04-02 10:40:45 +00:00
|
|
|
s.Equal(mid, expired.Hash)
|
|
|
|
s.EqualError(expired.Error, expectedError)
|
2018-04-13 05:52:22 +00:00
|
|
|
case confirmed := <-mock.confirmations:
|
|
|
|
s.Fail("unexpected confirmation for hash", confirmed)
|
2018-05-04 09:08:04 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
2018-04-13 05:52:22 +00:00
|
|
|
s.Fail("timed out while waiting for confirmation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 10:40:45 +00:00
|
|
|
func (s *ShhExtSuite) TestWaitMessageExpired() {
|
|
|
|
s.testWaitMessageExpired("envelope expired due to connectivity issues", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShhExtSuite) TestErrorOnEnvelopeDelivery() {
|
|
|
|
// in the test we are sending message from peer 0 to peer 1
|
|
|
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
|
|
|
s.Require().NoError(s.services[0].UpdateMailservers([]*enode.Node{s.nodes[1].Server().Self()}))
|
|
|
|
s.whisper[1].SetTimeSource(func() time.Time {
|
|
|
|
return time.Now().Add(time.Hour)
|
|
|
|
})
|
|
|
|
s.testWaitMessageExpired("envelope wasn't delivered due to time sync issues", 100)
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
func (s *ShhExtSuite) TestRequestMessagesErrors() {
|
2018-04-26 05:56:19 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
shh := whisper.New(nil)
|
|
|
|
aNode, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
2019-02-01 17:02:52 +00:00
|
|
|
NoUSB: true,
|
2018-04-26 05:56:19 +00:00
|
|
|
}) // in-memory node as no data dir
|
|
|
|
s.NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
err = aNode.Register(func(*node.ServiceContext) (node.Service, error) {
|
2018-04-26 05:56:19 +00:00
|
|
|
return shh, nil
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
err = aNode.Start()
|
|
|
|
s.NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
defer func() { s.NoError(aNode.Stop()) }()
|
2018-04-26 05:56:19 +00:00
|
|
|
|
|
|
|
mock := newHandlerMock(1)
|
2019-01-17 12:56:22 +00:00
|
|
|
config := params.ShhextConfig{
|
|
|
|
InstallationID: "1",
|
|
|
|
BackupDisabledDataDir: os.TempDir(),
|
|
|
|
PFSEnabled: true,
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
service := New(shh, mock, nil, config)
|
2018-04-26 05:56:19 +00:00
|
|
|
api := NewPublicAPI(service)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mailServerPeer = "enode://b7e65e1bedc2499ee6cbd806945af5e7df0e59e4070c96821570bd581473eade24a489f5ec95d060c0db118c879403ab88d827d3766978f28708989d35474f87@[::]:51920"
|
|
|
|
)
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
var hash []byte
|
2018-04-26 05:56:19 +00:00
|
|
|
|
|
|
|
// invalid MailServer enode address
|
2018-06-15 15:12:31 +00:00
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{MailServerPeer: "invalid-address"})
|
|
|
|
s.Nil(hash)
|
2018-04-26 05:56:19 +00:00
|
|
|
s.EqualError(err, "invalid mailServerPeer value: invalid URL scheme, want \"enode\"")
|
|
|
|
|
|
|
|
// non-existent symmetric key
|
2018-06-15 15:12:31 +00:00
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
2018-04-26 05:56:19 +00:00
|
|
|
MailServerPeer: mailServerPeer,
|
2018-07-04 09:30:57 +00:00
|
|
|
SymKeyID: "invalid-sym-key-id",
|
2018-04-26 05:56:19 +00:00
|
|
|
})
|
2018-06-15 15:12:31 +00:00
|
|
|
s.Nil(hash)
|
2018-04-26 05:56:19 +00:00
|
|
|
s.EqualError(err, "invalid symKeyID value: non-existent key ID")
|
|
|
|
|
|
|
|
// with a symmetric key
|
|
|
|
symKeyID, symKeyErr := shh.AddSymKeyFromPassword("some-pass")
|
|
|
|
s.NoError(symKeyErr)
|
2018-06-15 15:12:31 +00:00
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
2018-04-26 05:56:19 +00:00
|
|
|
MailServerPeer: mailServerPeer,
|
|
|
|
SymKeyID: symKeyID,
|
|
|
|
})
|
2018-06-15 15:12:31 +00:00
|
|
|
s.Nil(hash)
|
2018-07-04 09:30:57 +00:00
|
|
|
s.Contains(err.Error(), "Could not find peer with ID")
|
2018-04-26 05:56:19 +00:00
|
|
|
|
2018-06-26 08:41:03 +00:00
|
|
|
// from is greater than to
|
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
|
|
|
From: 10,
|
|
|
|
To: 5,
|
|
|
|
})
|
|
|
|
s.Nil(hash)
|
2018-07-04 09:30:57 +00:00
|
|
|
s.Contains(err.Error(), "Query range is invalid: from > to (10 > 5)")
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:21:33 +00:00
|
|
|
func (s *ShhExtSuite) TestMultipleRequestMessagesWithoutForce() {
|
|
|
|
waitErr := helpers.WaitForPeerAsync(s.nodes[0].Server(), s.nodes[1].Server().Self().String(), p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
|
|
|
s.Require().NoError(<-waitErr)
|
|
|
|
client, err := s.nodes[0].Attach()
|
|
|
|
s.NoError(err)
|
|
|
|
s.NoError(client.Call(nil, "shhext_requestMessages", MessagesRequest{
|
|
|
|
MailServerPeer: s.nodes[1].Server().Self().String(),
|
|
|
|
Topics: []whisper.TopicType{{1}},
|
|
|
|
}))
|
|
|
|
s.EqualError(client.Call(nil, "shhext_requestMessages", MessagesRequest{
|
|
|
|
MailServerPeer: s.nodes[1].Server().Self().String(),
|
|
|
|
Topics: []whisper.TopicType{{1}},
|
|
|
|
}), "another request with the same topics was sent less than 3s ago. Please wait for a bit longer, or set `force` to true in request parameters")
|
|
|
|
s.NoError(client.Call(nil, "shhext_requestMessages", MessagesRequest{
|
|
|
|
MailServerPeer: s.nodes[1].Server().Self().String(),
|
|
|
|
Topics: []whisper.TopicType{{2}},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:02:08 +00:00
|
|
|
func (s *ShhExtSuite) TestFailedRequestUnregistered() {
|
|
|
|
waitErr := helpers.WaitForPeerAsync(s.nodes[0].Server(), s.nodes[1].Server().Self().String(), p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
|
|
|
s.Require().NoError(<-waitErr)
|
|
|
|
client, err := s.nodes[0].Attach()
|
|
|
|
topics := []whisper.TopicType{{1}}
|
|
|
|
s.NoError(err)
|
|
|
|
s.EqualError(client.Call(nil, "shhext_requestMessages", MessagesRequest{
|
|
|
|
MailServerPeer: "enode://19872f94b1e776da3a13e25afa71b47dfa99e658afd6427ea8d6e03c22a99f13590205a8826443e95a37eee1d815fc433af7a8ca9a8d0df7943d1f55684045b7@0.0.0.0:30305",
|
|
|
|
Topics: topics,
|
|
|
|
}), "Could not find peer with ID: 10841e6db5c02fc331bf36a8d2a9137a1696d9d3b6b1f872f780e02aa8ec5bba")
|
|
|
|
s.NoError(client.Call(nil, "shhext_requestMessages", MessagesRequest{
|
|
|
|
MailServerPeer: s.nodes[1].Server().Self().String(),
|
|
|
|
Topics: topics,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
func (s *ShhExtSuite) TestRequestMessagesSuccess() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
shh := whisper.New(nil)
|
|
|
|
aNode, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
2019-02-01 17:02:52 +00:00
|
|
|
NoUSB: true,
|
2018-07-04 09:30:57 +00:00
|
|
|
}) // in-memory node as no data dir
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
err = aNode.Register(func(*node.ServiceContext) (node.Service, error) { return shh, nil })
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
|
|
|
|
err = aNode.Start()
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
defer func() { err := aNode.Stop(); s.NoError(err) }()
|
|
|
|
|
|
|
|
mock := newHandlerMock(1)
|
2019-01-17 12:56:22 +00:00
|
|
|
config := params.ShhextConfig{
|
|
|
|
InstallationID: "1",
|
|
|
|
BackupDisabledDataDir: os.TempDir(),
|
|
|
|
PFSEnabled: true,
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
service := New(shh, mock, nil, config)
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(service.Start(aNode.Server()))
|
2018-07-04 09:30:57 +00:00
|
|
|
api := NewPublicAPI(service)
|
2018-06-26 08:41:03 +00:00
|
|
|
|
2018-04-26 05:56:19 +00:00
|
|
|
// with a peer acting as a mailserver
|
|
|
|
// prepare a node first
|
|
|
|
mailNode, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
ListenAddr: ":0",
|
|
|
|
},
|
2019-02-01 17:02:52 +00:00
|
|
|
NoUSB: true,
|
2018-04-26 05:56:19 +00:00
|
|
|
}) // in-memory node as no data dir
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
err = mailNode.Register(func(*node.ServiceContext) (node.Service, error) {
|
2018-04-26 05:56:19 +00:00
|
|
|
return whisper.New(nil), nil
|
|
|
|
})
|
|
|
|
s.NoError(err)
|
|
|
|
err = mailNode.Start()
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(err)
|
2018-07-04 09:30:57 +00:00
|
|
|
defer func() { s.NoError(mailNode.Stop()) }()
|
2018-04-26 05:56:19 +00:00
|
|
|
|
|
|
|
// add mailPeer as a peer
|
2018-07-04 09:30:57 +00:00
|
|
|
waitErr := helpers.WaitForPeerAsync(aNode.Server(), mailNode.Server().Self().String(), p2p.PeerEventTypeAdd, time.Second)
|
2018-07-11 14:42:51 +00:00
|
|
|
aNode.Server().AddPeer(mailNode.Server().Self())
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(<-waitErr)
|
2018-07-04 09:30:57 +00:00
|
|
|
|
|
|
|
var hash []byte
|
2018-04-26 05:56:19 +00:00
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
// send a request with a symmetric key
|
|
|
|
symKeyID, symKeyErr := shh.AddSymKeyFromPassword("some-pass")
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(symKeyErr)
|
2018-06-15 15:12:31 +00:00
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
2018-04-26 05:56:19 +00:00
|
|
|
MailServerPeer: mailNode.Server().Self().String(),
|
|
|
|
SymKeyID: symKeyID,
|
2019-01-15 09:21:33 +00:00
|
|
|
Force: true,
|
2018-04-26 05:56:19 +00:00
|
|
|
})
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(hash)
|
2019-02-20 06:57:57 +00:00
|
|
|
s.Require().NoError(waitForHashInMonitor(api.service.mailMonitor, common.BytesToHash(hash), MailServerRequestSent, time.Second))
|
2018-07-04 09:30:57 +00:00
|
|
|
// Send a request without a symmetric key. In this case,
|
|
|
|
// a public key extracted from MailServerPeer will be used.
|
|
|
|
hash, err = api.RequestMessages(context.TODO(), MessagesRequest{
|
|
|
|
MailServerPeer: mailNode.Server().Self().String(),
|
2019-01-15 09:21:33 +00:00
|
|
|
Force: true,
|
2018-07-04 09:30:57 +00:00
|
|
|
})
|
2018-12-05 13:57:05 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(hash)
|
2019-02-20 06:57:57 +00:00
|
|
|
s.Require().NoError(waitForHashInMonitor(api.service.mailMonitor, common.BytesToHash(hash), MailServerRequestSent, time.Second))
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
func (s *ShhExtSuite) TearDown() {
|
|
|
|
for _, n := range s.nodes {
|
|
|
|
s.NoError(n.Stop())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 06:57:57 +00:00
|
|
|
func waitForHashInMonitor(mon *MailRequestMonitor, hash common.Hash, state EnvelopeState, deadline time.Duration) error {
|
2018-12-05 13:57:05 +00:00
|
|
|
after := time.After(deadline)
|
|
|
|
ticker := time.Tick(100 * time.Millisecond)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
return fmt.Errorf("failed while waiting for %s to get into state %d", hash, state)
|
|
|
|
case <-ticker:
|
2019-02-20 06:57:57 +00:00
|
|
|
if mon.GetState(hash) == state {
|
2018-12-05 13:57:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-21 14:00:10 +00:00
|
|
|
|
2019-02-20 17:10:59 +00:00
|
|
|
type WhisperNodeMockSuite struct {
|
2019-01-21 14:00:10 +00:00
|
|
|
suite.Suite
|
|
|
|
|
2019-02-20 17:10:59 +00:00
|
|
|
localWhisperAPI *whisper.PublicWhisperAPI
|
|
|
|
localAPI *PublicAPI
|
|
|
|
localNode *enode.Node
|
|
|
|
remoteRW *p2p.MsgPipeRW
|
|
|
|
|
2019-03-01 13:36:21 +00:00
|
|
|
localService *Service
|
|
|
|
localEnvelopesMonitor *EnvelopesMonitor
|
2019-01-21 14:00:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 17:10:59 +00:00
|
|
|
func (s *WhisperNodeMockSuite) SetupTest() {
|
2019-01-21 14:00:10 +00:00
|
|
|
db, err := leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
conf := &whisper.Config{
|
|
|
|
MinimumAcceptedPOW: 0,
|
|
|
|
MaxMessageSize: 100 << 10,
|
|
|
|
}
|
|
|
|
w := whisper.New(conf)
|
2019-03-01 13:36:21 +00:00
|
|
|
s.Require().NoError(w.Start(nil))
|
2019-01-21 14:00:10 +00:00
|
|
|
pkey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
node := enode.NewV4(&pkey.PublicKey, net.ParseIP("127.0.0.1"), 1, 1)
|
|
|
|
peer := p2p.NewPeer(node.ID(), "1", []p2p.Cap{{"shh", 6}})
|
|
|
|
rw1, rw2 := p2p.MsgPipe()
|
|
|
|
errorc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
err := w.HandlePeer(peer, rw2)
|
|
|
|
errorc <- err
|
|
|
|
}()
|
|
|
|
s.Require().NoError(p2p.ExpectMsg(rw1, statusCode, []interface{}{whisper.ProtocolVersion, math.Float64bits(w.MinPow()), w.BloomFilter(), false, true}))
|
|
|
|
s.Require().NoError(p2p.SendItems(rw1, statusCode, whisper.ProtocolVersion, whisper.ProtocolVersion, math.Float64bits(w.MinPow()), w.BloomFilter(), true, true))
|
|
|
|
|
2019-03-01 13:36:21 +00:00
|
|
|
s.localService = New(w, nil, db, params.ShhextConfig{MailServerConfirmations: true, MaxMessageDeliveryAttempts: 3})
|
2019-02-20 17:10:59 +00:00
|
|
|
s.Require().NoError(s.localService.UpdateMailservers([]*enode.Node{node}))
|
2019-03-01 13:36:21 +00:00
|
|
|
|
|
|
|
s.localEnvelopesMonitor = s.localService.envelopesMonitor
|
|
|
|
s.localEnvelopesMonitor.Start()
|
2019-01-21 14:00:10 +00:00
|
|
|
|
2019-02-20 17:10:59 +00:00
|
|
|
s.localWhisperAPI = whisper.NewPublicWhisperAPI(w)
|
|
|
|
s.localAPI = NewPublicAPI(s.localService)
|
2019-01-21 14:00:10 +00:00
|
|
|
s.localNode = node
|
|
|
|
s.remoteRW = rw1
|
|
|
|
}
|
|
|
|
|
2019-03-01 13:36:21 +00:00
|
|
|
func (s *WhisperNodeMockSuite) PostMessage(message whisper.NewMessage) common.Hash {
|
|
|
|
envBytes, err := s.localAPI.Post(context.TODO(), message)
|
|
|
|
envHash := common.BytesToHash(envBytes)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(utils.Eventually(func() error {
|
|
|
|
if state := s.localEnvelopesMonitor.GetMessageState(envHash); state != EnvelopePosted {
|
|
|
|
return fmt.Errorf("envelope with hash %s wasn't posted", envHash.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, 2*time.Second, 100*time.Millisecond))
|
|
|
|
return envHash
|
|
|
|
}
|
|
|
|
|
2019-02-20 17:10:59 +00:00
|
|
|
func TestRequestMessagesSync(t *testing.T) {
|
|
|
|
suite.Run(t, new(RequestMessagesSyncSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestMessagesSyncSuite struct {
|
|
|
|
WhisperNodeMockSuite
|
|
|
|
}
|
|
|
|
|
2019-01-21 14:00:10 +00:00
|
|
|
func (s *RequestMessagesSyncSuite) TestExpired() {
|
|
|
|
// intentionally discarding all requests, so that request will timeout
|
|
|
|
go func() {
|
|
|
|
msg, err := s.remoteRW.ReadMsg()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(msg.Discard())
|
|
|
|
}()
|
2019-03-15 13:27:08 +00:00
|
|
|
_, err := s.localAPI.RequestMessagesSync(
|
|
|
|
RetryConfig{
|
|
|
|
BaseTimeout: time.Second,
|
|
|
|
},
|
|
|
|
MessagesRequest{
|
|
|
|
MailServerPeer: s.localNode.String(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
s.Require().EqualError(err, "failed to request messages after 1 retries")
|
2019-01-21 14:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestMessagesSyncSuite) testCompletedFromAttempt(target int) {
|
2019-03-15 13:27:08 +00:00
|
|
|
const cursorSize = 36 // taken from mailserver_response.go from whisperv6 package
|
|
|
|
cursor := [cursorSize]byte{}
|
|
|
|
cursor[0] = 0x01
|
|
|
|
|
2019-01-21 14:00:10 +00:00
|
|
|
go func() {
|
|
|
|
attempt := 0
|
|
|
|
for {
|
|
|
|
attempt++
|
|
|
|
msg, err := s.remoteRW.ReadMsg()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
if attempt < target {
|
|
|
|
s.Require().NoError(msg.Discard())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var e whisper.Envelope
|
|
|
|
s.Require().NoError(msg.Decode(&e))
|
2019-03-15 13:27:08 +00:00
|
|
|
s.Require().NoError(p2p.Send(s.remoteRW, p2pRequestCompleteCode, whisper.CreateMailServerRequestCompletedPayload(e.Hash(), common.Hash{}, cursor[:])))
|
2019-01-21 14:00:10 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-03-15 13:27:08 +00:00
|
|
|
resp, err := s.localAPI.RequestMessagesSync(
|
|
|
|
RetryConfig{
|
|
|
|
BaseTimeout: time.Second,
|
|
|
|
MaxRetries: target,
|
|
|
|
},
|
|
|
|
MessagesRequest{
|
|
|
|
MailServerPeer: s.localNode.String(),
|
|
|
|
Force: true, // force true is convenient here because timeout is less then default delay (3s)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(MessagesResponse{Cursor: hex.EncodeToString(cursor[:])}, resp)
|
2019-01-21 14:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestMessagesSyncSuite) TestCompletedFromFirstAttempt() {
|
|
|
|
s.testCompletedFromAttempt(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestMessagesSyncSuite) TestCompletedFromSecondAttempt() {
|
|
|
|
s.testCompletedFromAttempt(2)
|
|
|
|
}
|
2019-02-20 17:10:59 +00:00
|
|
|
|
|
|
|
func TestWhisperConfirmations(t *testing.T) {
|
|
|
|
suite.Run(t, new(WhisperConfirmationSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type WhisperConfirmationSuite struct {
|
|
|
|
WhisperNodeMockSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperConfirmationSuite) TestEnvelopeReceived() {
|
|
|
|
symID, err := s.localWhisperAPI.GenerateSymKeyFromPassword(context.TODO(), "test")
|
|
|
|
s.Require().NoError(err)
|
2019-03-01 13:36:21 +00:00
|
|
|
envHash := s.PostMessage(whisper.NewMessage{
|
2019-02-20 17:10:59 +00:00
|
|
|
SymKeyID: symID,
|
|
|
|
TTL: 1000,
|
|
|
|
Topic: whisper.TopicType{0x01},
|
|
|
|
})
|
|
|
|
|
|
|
|
// enable auto-replies once message got registered internally
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msg, err := s.remoteRW.ReadMsg()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
if msg.Code != messagesCode {
|
|
|
|
s.Require().NoError(msg.Discard())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// reply with same envelopes. we could probably just write same data to remoteRW, but this works too.
|
|
|
|
var envs []*whisper.Envelope
|
|
|
|
s.Require().NoError(msg.Decode(&envs))
|
|
|
|
s.Require().NoError(p2p.Send(s.remoteRW, messagesCode, envs))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for message to be removed because it was delivered by remoteRW
|
|
|
|
s.Require().NoError(utils.Eventually(func() error {
|
2019-03-01 13:36:21 +00:00
|
|
|
if state := s.localEnvelopesMonitor.GetMessageState(envHash); state == EnvelopePosted {
|
|
|
|
return fmt.Errorf("envelope with hash %s wasn't posted", envHash.String())
|
2019-02-20 17:10:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, 2*time.Second, 100*time.Millisecond))
|
|
|
|
}
|
2019-03-01 13:36:21 +00:00
|
|
|
|
|
|
|
func TestWhisperRetriesSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(WhisperRetriesSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type WhisperRetriesSuite struct {
|
|
|
|
WhisperNodeMockSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperRetriesSuite) TestUseAllAvaiableAttempts() {
|
|
|
|
var attempts int32
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msg, err := s.remoteRW.ReadMsg()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(msg.Discard())
|
|
|
|
if msg.Code != messagesCode {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
atomic.AddInt32(&attempts, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
symID, err := s.localWhisperAPI.GenerateSymKeyFromPassword(context.TODO(), "test")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
message := whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
TTL: 1,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
}
|
|
|
|
s.Require().NotNil(s.PostMessage(message))
|
|
|
|
s.Require().NoError(utils.Eventually(func() error {
|
|
|
|
madeAttempts := atomic.LoadInt32(&attempts)
|
|
|
|
if madeAttempts != int32(s.localEnvelopesMonitor.maxAttempts) {
|
|
|
|
return fmt.Errorf("made unexpected number of attempts to deliver a message: %d != %d", s.localEnvelopesMonitor.maxAttempts, madeAttempts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, 10*time.Second, 500*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperRetriesSuite) testDelivery(target int) {
|
|
|
|
go func() {
|
|
|
|
attempt := 0
|
|
|
|
for {
|
|
|
|
msg, err := s.remoteRW.ReadMsg()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
if msg.Code != messagesCode {
|
|
|
|
s.Require().NoError(msg.Discard())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attempt++
|
|
|
|
if attempt != target {
|
|
|
|
s.Require().NoError(msg.Discard())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(msg.Payload)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
// without this hack event from the whisper read loop will be sent sooner than event from write loop
|
|
|
|
// i don't think that this is realistic situation and can be reproduced only in test with in-memory
|
|
|
|
// connection mock
|
|
|
|
time.Sleep(time.Nanosecond)
|
|
|
|
s.Require().NoError(p2p.Send(s.remoteRW, batchAcknowledgeCode, crypto.Keccak256Hash(data)))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
symID, err := s.localWhisperAPI.GenerateSymKeyFromPassword(context.TODO(), "test")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
message := whisper.NewMessage{
|
|
|
|
SymKeyID: symID,
|
|
|
|
PowTarget: whisper.DefaultMinimumPoW,
|
|
|
|
PowTime: 200,
|
|
|
|
TTL: 1,
|
|
|
|
Topic: whisper.TopicType{0x01, 0x01, 0x01, 0x01},
|
|
|
|
Payload: []byte("hello"),
|
|
|
|
}
|
|
|
|
mID := messageID(message)
|
|
|
|
s.Require().NotNil(s.PostMessage(message))
|
|
|
|
s.Require().NoError(utils.Eventually(func() error {
|
|
|
|
if state := s.localEnvelopesMonitor.GetMessageState(mID); state != EnvelopeSent {
|
|
|
|
return fmt.Errorf("message with ID %s wasn't sent", mID.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, 3*time.Second, 100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperRetriesSuite) TestDeliveredFromFirstAttempt() {
|
|
|
|
s.testDelivery(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperRetriesSuite) TestDeliveredFromSecondAttempt() {
|
|
|
|
s.testDelivery(2)
|
|
|
|
}
|
2019-04-30 06:46:12 +00:00
|
|
|
|
|
|
|
func TestRequestWithTrackingHistorySuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(RequestWithTrackingHistorySuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestWithTrackingHistorySuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
envelopeSymkey string
|
|
|
|
envelopeSymkeyID string
|
|
|
|
|
|
|
|
localWhisperAPI *whisper.PublicWhisperAPI
|
|
|
|
localAPI *PublicAPI
|
|
|
|
localService *Service
|
2019-05-06 06:33:19 +00:00
|
|
|
localContext Context
|
2019-04-30 06:46:12 +00:00
|
|
|
mailSymKey string
|
|
|
|
|
|
|
|
remoteMailserver *mailserver.WMailServer
|
|
|
|
remoteNode *enode.Node
|
|
|
|
remoteWhisper *whisper.Whisper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) SetupTest() {
|
|
|
|
db, err := leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
conf := &whisper.Config{
|
|
|
|
MinimumAcceptedPOW: 0,
|
|
|
|
MaxMessageSize: 100 << 10,
|
|
|
|
}
|
|
|
|
local := whisper.New(conf)
|
|
|
|
s.Require().NoError(local.Start(nil))
|
|
|
|
|
|
|
|
s.localWhisperAPI = whisper.NewPublicWhisperAPI(local)
|
|
|
|
s.localService = New(local, nil, db, params.ShhextConfig{})
|
2019-05-06 06:33:19 +00:00
|
|
|
s.localContext = NewContextFromService(context.Background(), s.localService, s.localService.storage)
|
2019-04-30 06:46:12 +00:00
|
|
|
localPkey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(s.localService.Start(&p2p.Server{Config: p2p.Config{PrivateKey: localPkey}}))
|
|
|
|
s.localAPI = NewPublicAPI(s.localService)
|
|
|
|
|
|
|
|
remote := whisper.New(conf)
|
|
|
|
s.remoteWhisper = remote
|
|
|
|
s.Require().NoError(remote.Start(nil))
|
|
|
|
s.remoteMailserver = &mailserver.WMailServer{}
|
|
|
|
remote.RegisterServer(s.remoteMailserver)
|
|
|
|
password := "test"
|
|
|
|
tmpdir, err := ioutil.TempDir("", "tracking-history-tests-")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(s.remoteMailserver.Init(remote, ¶ms.WhisperConfig{
|
|
|
|
DataDir: tmpdir,
|
|
|
|
MailServerPassword: password,
|
|
|
|
}))
|
|
|
|
|
|
|
|
pkey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
// we need proper enode for a remote node. it will be used when mail server request is made
|
|
|
|
s.remoteNode = enode.NewV4(&pkey.PublicKey, net.ParseIP("127.0.0.1"), 1, 1)
|
|
|
|
remotePeer := p2p.NewPeer(s.remoteNode.ID(), "1", []p2p.Cap{{"shh", 6}})
|
|
|
|
localPeer := p2p.NewPeer(enode.ID{2}, "2", []p2p.Cap{{"shh", 6}})
|
|
|
|
// FIXME close this in tear down
|
|
|
|
rw1, rw2 := p2p.MsgPipe()
|
|
|
|
go func() {
|
|
|
|
err := local.HandlePeer(remotePeer, rw1)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
err := remote.HandlePeer(localPeer, rw2)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}()
|
|
|
|
s.mailSymKey, err = s.localWhisperAPI.GenerateSymKeyFromPassword(context.Background(), password)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.envelopeSymkey = "topics"
|
|
|
|
s.envelopeSymkeyID, err = s.localWhisperAPI.GenerateSymKeyFromPassword(context.Background(), s.envelopeSymkey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) postEnvelopes(topics ...whisper.TopicType) []hexutil.Bytes {
|
|
|
|
var (
|
|
|
|
rst = make([]hexutil.Bytes, len(topics))
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
for i, t := range topics {
|
|
|
|
rst[i], err = s.localWhisperAPI.Post(context.Background(), whisper.NewMessage{
|
|
|
|
SymKeyID: s.envelopeSymkeyID,
|
|
|
|
TTL: 10,
|
|
|
|
Topic: t,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
return rst
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) waitForArchival(hexes []hexutil.Bytes) {
|
|
|
|
events := make(chan whisper.EnvelopeEvent, 2)
|
|
|
|
sub := s.remoteWhisper.SubscribeEnvelopeEvents(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
s.Require().NoError(waitForArchival(events, 2*time.Second, hexes...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) createEmptyFilter(topics ...whisper.TopicType) string {
|
|
|
|
filterid, err := s.localWhisperAPI.NewMessageFilter(whisper.Criteria{
|
|
|
|
SymKeyID: s.envelopeSymkeyID,
|
|
|
|
Topics: topics,
|
|
|
|
AllowP2P: true,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(filterid)
|
|
|
|
|
|
|
|
messages, err := s.localWhisperAPI.GetFilterMessages(filterid)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Empty(messages)
|
|
|
|
return filterid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) initiateHistoryRequest(topics ...TopicRequest) []hexutil.Bytes {
|
2019-05-06 06:33:19 +00:00
|
|
|
requests, err := s.localAPI.InitiateHistoryRequests(context.Background(), InitiateHistoryRequestParams{
|
2019-04-30 06:46:12 +00:00
|
|
|
Peer: s.remoteNode.String(),
|
|
|
|
SymKeyID: s.mailSymKey,
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
Requests: topics,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
return requests
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) waitMessagesDelivered(filterid string, hexes ...hexutil.Bytes) {
|
|
|
|
var received int
|
|
|
|
s.Require().NoError(utils.Eventually(func() error {
|
|
|
|
messages, err := s.localWhisperAPI.GetFilterMessages(filterid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
received += len(messages)
|
|
|
|
if received != len(hexes) {
|
|
|
|
return fmt.Errorf("expecting to receive %d messages, received %d", len(hexes), received)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, 2*time.Second, 200*time.Millisecond))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) waitNoRequests() {
|
2019-05-06 06:33:19 +00:00
|
|
|
store := s.localContext.HistoryStore()
|
2019-04-30 06:46:12 +00:00
|
|
|
s.Require().NoError(utils.Eventually(func() error {
|
|
|
|
reqs, err := store.GetAllRequests()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(reqs) != 0 {
|
|
|
|
return fmt.Errorf("not all requests were removed. count %d", len(reqs))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, 2*time.Second, 200*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) TestMultipleMergeIntoOne() {
|
|
|
|
topic1 := whisper.TopicType{1, 1, 1, 1}
|
|
|
|
topic2 := whisper.TopicType{2, 2, 2, 2}
|
|
|
|
topic3 := whisper.TopicType{3, 3, 3, 3}
|
|
|
|
hexes := s.postEnvelopes(topic1, topic2, topic3)
|
|
|
|
s.waitForArchival(hexes)
|
|
|
|
|
|
|
|
filterid := s.createEmptyFilter(topic1, topic2, topic3)
|
|
|
|
requests := s.initiateHistoryRequest(
|
|
|
|
TopicRequest{Topic: topic1, Duration: time.Hour},
|
|
|
|
TopicRequest{Topic: topic2, Duration: time.Hour},
|
|
|
|
TopicRequest{Topic: topic3, Duration: 10 * time.Hour},
|
|
|
|
)
|
|
|
|
// since we are using different duration for 3rd topic there will be 2 requests
|
|
|
|
s.Require().Len(requests, 2)
|
|
|
|
s.waitMessagesDelivered(filterid, hexes...)
|
|
|
|
|
2019-05-06 06:33:19 +00:00
|
|
|
s.Require().NoError(s.localService.historyUpdates.UpdateTopicHistory(s.localContext, topic1, time.Now()))
|
|
|
|
s.Require().NoError(s.localService.historyUpdates.UpdateTopicHistory(s.localContext, topic2, time.Now()))
|
|
|
|
s.Require().NoError(s.localService.historyUpdates.UpdateTopicHistory(s.localContext, topic3, time.Now()))
|
2019-04-30 06:46:12 +00:00
|
|
|
for _, r := range requests {
|
|
|
|
s.Require().NoError(s.localAPI.CompleteRequest(context.TODO(), r.String()))
|
|
|
|
}
|
|
|
|
s.waitNoRequests()
|
|
|
|
|
|
|
|
requests = s.initiateHistoryRequest(
|
|
|
|
TopicRequest{Topic: topic1, Duration: time.Hour},
|
|
|
|
TopicRequest{Topic: topic2, Duration: time.Hour},
|
|
|
|
TopicRequest{Topic: topic3, Duration: 10 * time.Hour},
|
|
|
|
)
|
|
|
|
s.Len(requests, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RequestWithTrackingHistorySuite) TestSingleRequest() {
|
|
|
|
topic1 := whisper.TopicType{1, 1, 1, 1}
|
|
|
|
topic2 := whisper.TopicType{255, 255, 255, 255}
|
|
|
|
hexes := s.postEnvelopes(topic1, topic2)
|
|
|
|
s.waitForArchival(hexes)
|
|
|
|
|
|
|
|
filterid := s.createEmptyFilter(topic1, topic2)
|
|
|
|
requests := s.initiateHistoryRequest(
|
|
|
|
TopicRequest{Topic: topic1, Duration: time.Hour},
|
|
|
|
TopicRequest{Topic: topic2, Duration: time.Hour},
|
|
|
|
)
|
|
|
|
s.Require().Len(requests, 1)
|
|
|
|
s.waitMessagesDelivered(filterid, hexes...)
|
|
|
|
}
|
|
|
|
|
2019-05-01 10:33:18 +00:00
|
|
|
func (s *RequestWithTrackingHistorySuite) TestPreviousRequestReplaced() {
|
|
|
|
topic1 := whisper.TopicType{1, 1, 1, 1}
|
|
|
|
topic2 := whisper.TopicType{255, 255, 255, 255}
|
|
|
|
|
|
|
|
requests := s.initiateHistoryRequest(
|
|
|
|
TopicRequest{Topic: topic1, Duration: time.Hour},
|
|
|
|
TopicRequest{Topic: topic2, Duration: time.Hour},
|
|
|
|
)
|
|
|
|
s.Require().Len(requests, 1)
|
|
|
|
s.localService.requestsRegistry.Clear()
|
|
|
|
replaced := s.initiateHistoryRequest(
|
|
|
|
TopicRequest{Topic: topic1, Duration: time.Hour},
|
|
|
|
TopicRequest{Topic: topic2, Duration: time.Hour},
|
|
|
|
)
|
|
|
|
s.Require().Len(replaced, 1)
|
|
|
|
s.Require().NotEqual(requests[0], replaced[0])
|
|
|
|
}
|
|
|
|
|
2019-04-30 06:46:12 +00:00
|
|
|
func waitForArchival(events chan whisper.EnvelopeEvent, duration time.Duration, hashes ...hexutil.Bytes) error {
|
|
|
|
waiting := map[common.Hash]struct{}{}
|
|
|
|
for _, hash := range hashes {
|
|
|
|
waiting[common.BytesToHash(hash)] = struct{}{}
|
|
|
|
}
|
|
|
|
timeout := time.After(duration)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return errors.New("timed out while waiting for mailserver to archive envelopes")
|
|
|
|
case ev := <-events:
|
|
|
|
if ev.Event != whisper.EventMailServerEnvelopeArchived {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, exist := waiting[ev.Hash]; exist {
|
|
|
|
delete(waiting, ev.Hash)
|
|
|
|
if len(waiting) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|