2017-12-07 13:37:43 +00:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
2018-06-15 15:12:31 +00:00
|
|
|
"encoding/hex"
|
2017-12-07 13:37:43 +00:00
|
|
|
"encoding/json"
|
2018-07-02 07:38:10 +00:00
|
|
|
"fmt"
|
2018-02-08 12:52:47 +00:00
|
|
|
"path/filepath"
|
2018-07-02 07:38:10 +00:00
|
|
|
"sort"
|
2017-12-07 13:37:43 +00:00
|
|
|
"strconv"
|
2018-06-15 15:12:31 +00:00
|
|
|
"strings"
|
2017-12-07 13:37:43 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-12-26 12:21:07 +00:00
|
|
|
"os"
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-12-07 13:37:43 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-07-02 07:38:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/sha3"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-03-02 09:25:30 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-07-04 15:53:02 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2018-02-08 12:52:47 +00:00
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2017-12-07 13:37:43 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
const mailboxPassword = "status-offline-inbox"
|
|
|
|
|
2017-12-07 13:37:43 +00:00
|
|
|
type WhisperMailboxSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhisperMailboxTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(WhisperMailboxSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperMailboxSuite) TestRequestMessageFromMailboxAsync() {
|
2018-04-16 12:36:09 +00:00
|
|
|
var err error
|
2018-03-26 17:05:24 +00:00
|
|
|
// Start mailbox and status node.
|
2017-12-07 13:37:43 +00:00
|
|
|
mailboxBackend, stop := s.startMailboxBackend()
|
|
|
|
defer stop()
|
2018-04-16 12:36:09 +00:00
|
|
|
s.Require().True(mailboxBackend.IsNodeRunning())
|
|
|
|
mailboxNode := mailboxBackend.StatusNode().GethNode()
|
2017-12-07 13:37:43 +00:00
|
|
|
mailboxEnode := mailboxNode.Server().NodeInfo().Enode
|
|
|
|
|
2017-12-26 12:21:07 +00:00
|
|
|
sender, stop := s.startBackend("sender")
|
2017-12-07 13:37:43 +00:00
|
|
|
defer stop()
|
2018-04-16 12:36:09 +00:00
|
|
|
s.Require().True(sender.IsNodeRunning())
|
|
|
|
node := sender.StatusNode().GethNode()
|
2017-12-07 13:37:43 +00:00
|
|
|
|
|
|
|
s.Require().NotEqual(mailboxEnode, node.Server().NodeInfo().Enode)
|
|
|
|
|
2018-07-04 15:53:02 +00:00
|
|
|
errCh := helpers.WaitForPeerAsync(sender.StatusNode().Server(), mailboxEnode, p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
s.Require().NoError(sender.StatusNode().AddPeer(mailboxEnode))
|
|
|
|
s.Require().NoError(<-errCh)
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
senderWhisperService, err := sender.StatusNode().WhisperService()
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2018-07-20 06:34:38 +00:00
|
|
|
// This sleep still is required because there is still a race condition
|
|
|
|
// when adding a peer to p2p.Server and to Whisper service.
|
|
|
|
// It may happen that the event about the added peer
|
|
|
|
// was emitted by p2p.Server but Whisper service is not aware of it yet.
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Mark mailbox node trusted.
|
2018-07-20 06:34:38 +00:00
|
|
|
err = senderWhisperService.AllowP2PMessagesFromPeer(mailboxNode.Server().Self().ID[:])
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Generate mailbox symkey.
|
2018-06-12 16:50:25 +00:00
|
|
|
password := mailboxPassword
|
2017-12-26 12:21:07 +00:00
|
|
|
MailServerKeyID, err := senderWhisperService.AddSymKeyFromPassword(password)
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2018-08-02 06:48:44 +00:00
|
|
|
rpcClient := sender.StatusNode().RPCPrivateClient()
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().NotNil(rpcClient)
|
|
|
|
|
2018-06-15 17:25:10 +00:00
|
|
|
mailboxWhisperService, err := mailboxBackend.StatusNode().WhisperService()
|
|
|
|
s.Require().NoError(err)
|
2018-06-13 14:46:47 +00:00
|
|
|
s.Require().NotNil(mailboxWhisperService)
|
|
|
|
|
2018-07-04 11:07:40 +00:00
|
|
|
// watch envelopes to be archived on mailserver
|
|
|
|
envelopeArchivedWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
|
|
|
sub := mailboxWhisperService.SubscribeEnvelopeEvents(envelopeArchivedWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// watch envelopes to be available for filters in the client
|
|
|
|
envelopeAvailableWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
|
|
|
sub = senderWhisperService.SubscribeEnvelopeEvents(envelopeAvailableWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// watch mailserver responses in the client
|
|
|
|
mailServerResponseWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
|
|
|
sub = senderWhisperService.SubscribeEnvelopeEvents(mailServerResponseWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
2018-06-13 14:46:47 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Create topic.
|
2018-03-02 09:25:30 +00:00
|
|
|
topic := whisper.BytesToTopic([]byte("topic name"))
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Add key pair to whisper.
|
2017-12-26 12:21:07 +00:00
|
|
|
keyID, err := senderWhisperService.NewKeyPair()
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().NoError(err)
|
2017-12-26 12:21:07 +00:00
|
|
|
key, err := senderWhisperService.GetPrivateKey(keyID)
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
pubkey := hexutil.Bytes(crypto.FromECDSAPub(&key.PublicKey))
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Create message filter.
|
2017-12-26 12:21:07 +00:00
|
|
|
messageFilterID := s.createPrivateChatMessageFilter(rpcClient, keyID, topic.String())
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// There are no messages at filter.
|
2018-01-24 08:25:28 +00:00
|
|
|
messages := s.getMessagesByMessageFilterID(rpcClient, messageFilterID)
|
2018-06-13 14:46:47 +00:00
|
|
|
s.Require().Empty(messages)
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-03-27 15:35:58 +00:00
|
|
|
// Post message matching with filter (key and topic).
|
2018-06-15 17:25:10 +00:00
|
|
|
messageHash := s.postMessageToPrivate(rpcClient, pubkey.String(), topic.String(), hexutil.Encode([]byte("Hello world!")))
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Get message to make sure that it will come from the mailbox later.
|
2018-07-04 11:07:40 +00:00
|
|
|
s.waitForEnvelopeEvents(envelopeAvailableWatcher, []string{messageHash}, whisper.EventEnvelopeAvailable)
|
|
|
|
messages = s.getMessagesByMessageFilterID(rpcClient, messageFilterID)
|
2018-02-05 13:40:27 +00:00
|
|
|
s.Require().Equal(1, len(messages))
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Act.
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-07-04 11:07:40 +00:00
|
|
|
// wait for mailserver to archive all the envelopes
|
|
|
|
s.waitForEnvelopeEvents(envelopeArchivedWatcher, []string{messageHash}, whisper.EventMailServerEnvelopeArchived)
|
2018-06-15 15:12:31 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Request messages (including the previous one, expired) from mailbox.
|
2018-07-20 06:34:38 +00:00
|
|
|
requestID := s.requestHistoricMessagesFromLast12Hours(senderWhisperService, rpcClient, mailboxNode.Server().Self().String(), MailServerKeyID, topic.String(), 0, "")
|
2017-12-12 11:00:17 +00:00
|
|
|
|
2018-07-04 11:07:40 +00:00
|
|
|
// wait for mail server response
|
|
|
|
resp := s.waitForMailServerResponse(mailServerResponseWatcher, requestID)
|
2018-07-21 15:32:53 +00:00
|
|
|
s.Require().Equal(messageHash, resp.LastEnvelopeHash.String())
|
|
|
|
s.Require().Empty(resp.Cursor)
|
2018-07-04 11:07:40 +00:00
|
|
|
|
|
|
|
// wait for last envelope sent by the mailserver to be available for filters
|
|
|
|
s.waitForEnvelopeEvents(envelopeAvailableWatcher, []string{resp.LastEnvelopeHash.String()}, whisper.EventEnvelopeAvailable)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// And we receive message, it comes from mailbox.
|
2018-07-04 11:07:40 +00:00
|
|
|
messages = s.getMessagesByMessageFilterID(rpcClient, messageFilterID)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().Equal(1, len(messages))
|
2017-12-12 11:00:17 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Check that there are no messages.
|
2018-01-24 08:25:28 +00:00
|
|
|
messages = s.getMessagesByMessageFilterID(rpcClient, messageFilterID)
|
2018-06-13 14:46:47 +00:00
|
|
|
s.Require().Empty(messages)
|
2017-12-26 12:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperMailboxSuite) TestRequestMessagesInGroupChat() {
|
2018-04-16 12:36:09 +00:00
|
|
|
var err error
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Start mailbox, alice, bob, charlie node.
|
2017-12-26 12:21:07 +00:00
|
|
|
mailboxBackend, stop := s.startMailboxBackend()
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
aliceBackend, stop := s.startBackend("alice")
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
bobBackend, stop := s.startBackend("bob")
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
charlieBackend, stop := s.startBackend("charlie")
|
|
|
|
defer stop()
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Add mailbox to static peers.
|
2018-04-16 12:36:09 +00:00
|
|
|
s.Require().True(mailboxBackend.IsNodeRunning())
|
|
|
|
mailboxNode := mailboxBackend.StatusNode().GethNode()
|
2017-12-26 12:21:07 +00:00
|
|
|
mailboxEnode := mailboxNode.Server().NodeInfo().Enode
|
2017-12-12 11:00:17 +00:00
|
|
|
|
2018-07-04 15:53:02 +00:00
|
|
|
aliceErrCh := helpers.WaitForPeerAsync(aliceBackend.StatusNode().Server(), mailboxEnode, p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
bobErrCh := helpers.WaitForPeerAsync(bobBackend.StatusNode().Server(), mailboxEnode, p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
charlieErrCh := helpers.WaitForPeerAsync(charlieBackend.StatusNode().Server(), mailboxEnode, p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
|
|
|
|
s.Require().NoError(aliceBackend.StatusNode().AddPeer(mailboxEnode))
|
|
|
|
s.Require().NoError(bobBackend.StatusNode().AddPeer(mailboxEnode))
|
|
|
|
s.Require().NoError(charlieBackend.StatusNode().AddPeer(mailboxEnode))
|
|
|
|
|
|
|
|
s.Require().NoError(<-aliceErrCh)
|
|
|
|
s.Require().NoError(<-bobErrCh)
|
|
|
|
s.Require().NoError(<-charlieErrCh)
|
2017-12-12 11:00:17 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Get whisper service.
|
2018-07-04 11:07:40 +00:00
|
|
|
mailboxWhisperService, err := mailboxBackend.StatusNode().WhisperService()
|
|
|
|
s.Require().NoError(err)
|
2018-04-05 09:45:26 +00:00
|
|
|
aliceWhisperService, err := aliceBackend.StatusNode().WhisperService()
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().NoError(err)
|
2018-04-05 09:45:26 +00:00
|
|
|
bobWhisperService, err := bobBackend.StatusNode().WhisperService()
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().NoError(err)
|
2018-04-05 09:45:26 +00:00
|
|
|
charlieWhisperService, err := charlieBackend.StatusNode().WhisperService()
|
2017-12-12 11:00:17 +00:00
|
|
|
s.Require().NoError(err)
|
2018-03-26 17:05:24 +00:00
|
|
|
// Get rpc client.
|
2018-08-02 06:48:44 +00:00
|
|
|
aliceRPCClient := aliceBackend.StatusNode().RPCPrivateClient()
|
|
|
|
bobRPCClient := bobBackend.StatusNode().RPCPrivateClient()
|
|
|
|
charlieRPCClient := charlieBackend.StatusNode().RPCPrivateClient()
|
2017-12-12 11:00:17 +00:00
|
|
|
|
2018-07-04 11:07:40 +00:00
|
|
|
// watchers
|
|
|
|
envelopeArchivedWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
|
|
|
sub := mailboxWhisperService.SubscribeEnvelopeEvents(envelopeArchivedWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
bobEnvelopeAvailableWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
|
|
|
sub = bobWhisperService.SubscribeEnvelopeEvents(bobEnvelopeAvailableWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
charlieEnvelopeAvailableWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
|
|
|
sub = charlieWhisperService.SubscribeEnvelopeEvents(charlieEnvelopeAvailableWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
2018-06-13 14:46:47 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Bob and charlie add the mailserver key.
|
2018-06-12 16:50:25 +00:00
|
|
|
password := mailboxPassword
|
2017-12-26 12:21:07 +00:00
|
|
|
bobMailServerKeyID, err := bobWhisperService.AddSymKeyFromPassword(password)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
charlieMailServerKeyID, err := charlieWhisperService.AddSymKeyFromPassword(password)
|
2017-12-12 11:00:17 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Generate a group chat symkey and topic.
|
2017-12-26 12:21:07 +00:00
|
|
|
groupChatKeyID, err := aliceWhisperService.GenerateSymKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
groupChatKey, err := aliceWhisperService.GetSymKey(groupChatKeyID)
|
|
|
|
s.Require().NoError(err)
|
2018-03-26 17:05:24 +00:00
|
|
|
// Generate a group chat topic.
|
2018-03-02 09:25:30 +00:00
|
|
|
groupChatTopic := whisper.BytesToTopic([]byte("groupChatTopic"))
|
2018-04-24 15:50:26 +00:00
|
|
|
// sender must be subscribed to message topic it sends
|
|
|
|
s.NotNil(s.createGroupChatMessageFilter(aliceRPCClient, groupChatKeyID, groupChatTopic.String()))
|
2017-12-26 12:21:07 +00:00
|
|
|
groupChatPayload := newGroupChatParams(groupChatKey, groupChatTopic)
|
|
|
|
payloadStr, err := groupChatPayload.Encode()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Add Bob and Charlie's key pairs to receive the symmetric key for the group chat from Alice.
|
2017-12-26 12:21:07 +00:00
|
|
|
bobKeyID, err := bobWhisperService.NewKeyPair()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
bobKey, err := bobWhisperService.GetPrivateKey(bobKeyID)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
bobPubkey := hexutil.Bytes(crypto.FromECDSAPub(&bobKey.PublicKey))
|
2018-03-02 09:25:30 +00:00
|
|
|
bobAliceKeySendTopic := whisper.BytesToTopic([]byte("bobAliceKeySendTopic "))
|
2017-12-12 11:00:17 +00:00
|
|
|
|
2017-12-26 12:21:07 +00:00
|
|
|
charlieKeyID, err := charlieWhisperService.NewKeyPair()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
charlieKey, err := charlieWhisperService.GetPrivateKey(charlieKeyID)
|
2017-12-12 11:00:17 +00:00
|
|
|
s.Require().NoError(err)
|
2017-12-26 12:21:07 +00:00
|
|
|
charliePubkey := hexutil.Bytes(crypto.FromECDSAPub(&charlieKey.PublicKey))
|
2018-03-02 09:25:30 +00:00
|
|
|
charlieAliceKeySendTopic := whisper.BytesToTopic([]byte("charlieAliceKeySendTopic "))
|
2017-12-12 11:00:17 +00:00
|
|
|
|
2018-05-04 05:57:34 +00:00
|
|
|
// Alice must add peers topics into her own bloom filter.
|
|
|
|
aliceKeyID, err := aliceWhisperService.NewKeyPair()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.createPrivateChatMessageFilter(aliceRPCClient, aliceKeyID, bobAliceKeySendTopic.String())
|
|
|
|
s.createPrivateChatMessageFilter(aliceRPCClient, aliceKeyID, charlieAliceKeySendTopic.String())
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Bob and charlie create message filter.
|
2018-01-24 08:25:28 +00:00
|
|
|
bobMessageFilterID := s.createPrivateChatMessageFilter(bobRPCClient, bobKeyID, bobAliceKeySendTopic.String())
|
|
|
|
charlieMessageFilterID := s.createPrivateChatMessageFilter(charlieRPCClient, charlieKeyID, charlieAliceKeySendTopic.String())
|
2017-12-26 12:21:07 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Alice send message with symkey and topic to Bob and Charlie.
|
2018-06-15 17:25:10 +00:00
|
|
|
aliceToBobMessageHash := s.postMessageToPrivate(aliceRPCClient, bobPubkey.String(), bobAliceKeySendTopic.String(), payloadStr)
|
|
|
|
aliceToCharlieMessageHash := s.postMessageToPrivate(aliceRPCClient, charliePubkey.String(), charlieAliceKeySendTopic.String(), payloadStr)
|
2017-12-26 12:21:07 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Bob receive group chat data and add it to his node.
|
|
|
|
// Bob get group chat details.
|
2018-07-04 11:07:40 +00:00
|
|
|
s.waitForEnvelopeEvents(bobEnvelopeAvailableWatcher, []string{aliceToBobMessageHash}, whisper.EventEnvelopeAvailable)
|
|
|
|
messages := s.getMessagesByMessageFilterID(bobRPCClient, bobMessageFilterID)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().Equal(1, len(messages))
|
|
|
|
bobGroupChatData := groupChatParams{}
|
2018-02-12 11:16:06 +00:00
|
|
|
err = bobGroupChatData.Decode(messages[0]["payload"].(string))
|
|
|
|
s.Require().NoError(err)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.EqualValues(groupChatPayload, bobGroupChatData)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Bob add symkey to his node.
|
2018-01-24 08:25:28 +00:00
|
|
|
bobGroupChatSymkeyID := s.addSymKey(bobRPCClient, bobGroupChatData.Key)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().NotEmpty(bobGroupChatSymkeyID)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Bob create message filter to node by group chat topic.
|
2018-01-24 08:25:28 +00:00
|
|
|
bobGroupChatMessageFilterID := s.createGroupChatMessageFilter(bobRPCClient, bobGroupChatSymkeyID, bobGroupChatData.Topic)
|
2017-12-26 12:21:07 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Charlie receive group chat data and add it to his node.
|
|
|
|
// Charlie get group chat details.
|
2018-07-04 11:07:40 +00:00
|
|
|
s.waitForEnvelopeEvents(charlieEnvelopeAvailableWatcher, []string{aliceToCharlieMessageHash}, whisper.EventEnvelopeAvailable)
|
|
|
|
messages = s.getMessagesByMessageFilterID(charlieRPCClient, charlieMessageFilterID)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().Equal(1, len(messages))
|
|
|
|
charlieGroupChatData := groupChatParams{}
|
2018-02-12 11:16:06 +00:00
|
|
|
err = charlieGroupChatData.Decode(messages[0]["payload"].(string))
|
|
|
|
s.Require().NoError(err)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.EqualValues(groupChatPayload, charlieGroupChatData)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Charlie add symkey to his node.
|
2018-01-24 08:25:28 +00:00
|
|
|
charlieGroupChatSymkeyID := s.addSymKey(charlieRPCClient, charlieGroupChatData.Key)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().NotEmpty(charlieGroupChatSymkeyID)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Charlie create message filter to node by group chat topic.
|
2018-01-24 08:25:28 +00:00
|
|
|
charlieGroupChatMessageFilterID := s.createGroupChatMessageFilter(charlieRPCClient, charlieGroupChatSymkeyID, charlieGroupChatData.Topic)
|
2017-12-26 12:21:07 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Alice send message to group chat.
|
2017-12-26 12:21:07 +00:00
|
|
|
helloWorldMessage := hexutil.Encode([]byte("Hello world!"))
|
2018-06-15 17:25:10 +00:00
|
|
|
groupChatMessageHash := s.postMessageToGroup(aliceRPCClient, groupChatKeyID, groupChatTopic.String(), helloWorldMessage)
|
2017-12-26 12:21:07 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Bob receive group chat message.
|
2018-07-04 11:07:40 +00:00
|
|
|
s.waitForEnvelopeEvents(bobEnvelopeAvailableWatcher, []string{groupChatMessageHash}, whisper.EventEnvelopeAvailable)
|
|
|
|
messages = s.getMessagesByMessageFilterID(bobRPCClient, bobGroupChatMessageFilterID)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().Equal(1, len(messages))
|
|
|
|
s.Require().Equal(helloWorldMessage, messages[0]["payload"].(string))
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Charlie receive group chat message.
|
2018-07-04 11:07:40 +00:00
|
|
|
s.waitForEnvelopeEvents(charlieEnvelopeAvailableWatcher, []string{groupChatMessageHash}, whisper.EventEnvelopeAvailable)
|
|
|
|
messages = s.getMessagesByMessageFilterID(charlieRPCClient, charlieGroupChatMessageFilterID)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().Equal(1, len(messages))
|
|
|
|
s.Require().Equal(helloWorldMessage, messages[0]["payload"].(string))
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Check that we don't receive messages each one time.
|
2018-01-24 08:25:28 +00:00
|
|
|
messages = s.getMessagesByMessageFilterID(bobRPCClient, bobGroupChatMessageFilterID)
|
2018-06-13 14:46:47 +00:00
|
|
|
s.Require().Empty(messages)
|
2018-01-24 08:25:28 +00:00
|
|
|
messages = s.getMessagesByMessageFilterID(charlieRPCClient, charlieGroupChatMessageFilterID)
|
2018-06-13 14:46:47 +00:00
|
|
|
s.Require().Empty(messages)
|
2017-12-26 12:21:07 +00:00
|
|
|
|
2018-07-04 11:07:40 +00:00
|
|
|
// be sure that message has been archived
|
|
|
|
s.waitForEnvelopeEvents(envelopeArchivedWatcher, []string{groupChatMessageHash}, whisper.EventMailServerEnvelopeArchived)
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Request each one messages from mailbox using enode.
|
2018-07-02 07:38:10 +00:00
|
|
|
s.requestHistoricMessagesFromLast12Hours(bobWhisperService, bobRPCClient, mailboxEnode, bobMailServerKeyID, groupChatTopic.String(), 0, "")
|
|
|
|
s.requestHistoricMessagesFromLast12Hours(charlieWhisperService, charlieRPCClient, mailboxEnode, charlieMailServerKeyID, groupChatTopic.String(), 0, "")
|
2017-12-26 12:21:07 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Bob receive p2p message from group chat filter.
|
2018-07-04 11:07:40 +00:00
|
|
|
s.waitForEnvelopeEvents(bobEnvelopeAvailableWatcher, []string{groupChatMessageHash}, whisper.EventEnvelopeAvailable)
|
|
|
|
messages = s.getMessagesByMessageFilterID(bobRPCClient, bobGroupChatMessageFilterID)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().Equal(1, len(messages))
|
|
|
|
s.Require().Equal(helloWorldMessage, messages[0]["payload"].(string))
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Charlie receive p2p message from group chat filter.
|
2018-07-04 11:07:40 +00:00
|
|
|
s.waitForEnvelopeEvents(charlieEnvelopeAvailableWatcher, []string{groupChatMessageHash}, whisper.EventEnvelopeAvailable)
|
|
|
|
messages = s.getMessagesByMessageFilterID(charlieRPCClient, charlieGroupChatMessageFilterID)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().Equal(1, len(messages))
|
|
|
|
s.Require().Equal(helloWorldMessage, messages[0]["payload"].(string))
|
|
|
|
}
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
func (s *WhisperMailboxSuite) TestRequestMessagesWithPagination() {
|
|
|
|
// Start mailbox
|
|
|
|
mailbox, stop := s.startMailboxBackend()
|
|
|
|
defer stop()
|
|
|
|
s.Require().True(mailbox.IsNodeRunning())
|
|
|
|
mailboxEnode := mailbox.StatusNode().GethNode().Server().NodeInfo().Enode
|
|
|
|
|
|
|
|
// Start client
|
|
|
|
client, stop := s.startBackend("client")
|
|
|
|
defer stop()
|
|
|
|
s.Require().True(client.IsNodeRunning())
|
2018-08-02 06:48:44 +00:00
|
|
|
clientRPCClient := client.StatusNode().RPCPrivateClient()
|
2018-07-02 07:38:10 +00:00
|
|
|
|
2018-07-12 14:50:49 +00:00
|
|
|
// Add mailbox to client's peers
|
2018-07-04 15:53:02 +00:00
|
|
|
errCh := helpers.WaitForPeerAsync(client.StatusNode().Server(), mailboxEnode, p2p.PeerEventTypeAdd, time.Second)
|
|
|
|
s.Require().NoError(client.StatusNode().AddPeer(mailboxEnode))
|
|
|
|
s.Require().NoError(<-errCh)
|
2018-07-02 07:38:10 +00:00
|
|
|
|
|
|
|
// Whisper services
|
|
|
|
mailboxWhisperService, err := mailbox.StatusNode().WhisperService()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
clientWhisperService, err := client.StatusNode().WhisperService()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
// mailserver sym key
|
|
|
|
mailServerKeyID, err := clientWhisperService.AddSymKeyFromPassword(mailboxPassword)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// public chat
|
|
|
|
var (
|
|
|
|
keyID string
|
|
|
|
topic whisper.TopicType
|
|
|
|
filterID string
|
|
|
|
)
|
|
|
|
publicChatName := "test public chat"
|
|
|
|
keyID, topic, filterID = s.joinPublicChat(clientWhisperService, clientRPCClient, publicChatName)
|
|
|
|
|
|
|
|
// envelopes to be sent
|
|
|
|
envelopesCount := 5
|
|
|
|
sentEnvelopesHashes := make([]string, 0)
|
|
|
|
|
|
|
|
// watch envelopes to be archived on mailserver
|
|
|
|
envelopeArchivedWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
2018-07-04 11:07:40 +00:00
|
|
|
sub := mailboxWhisperService.SubscribeEnvelopeEvents(envelopeArchivedWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
2018-07-02 07:38:10 +00:00
|
|
|
|
|
|
|
// watch envelopes to be available for filters in the client
|
|
|
|
envelopeAvailableWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
2018-07-04 11:07:40 +00:00
|
|
|
sub = clientWhisperService.SubscribeEnvelopeEvents(envelopeAvailableWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
2018-07-02 07:38:10 +00:00
|
|
|
|
|
|
|
// watch mailserver responses in the client
|
|
|
|
mailServerResponseWatcher := make(chan whisper.EnvelopeEvent, 1024)
|
2018-07-04 11:07:40 +00:00
|
|
|
sub = clientWhisperService.SubscribeEnvelopeEvents(mailServerResponseWatcher)
|
|
|
|
defer sub.Unsubscribe()
|
2018-07-02 07:38:10 +00:00
|
|
|
|
|
|
|
// send envelopes
|
|
|
|
for i := 0; i < envelopesCount; i++ {
|
|
|
|
hash := s.postMessageToGroup(clientRPCClient, keyID, topic.String(), "")
|
|
|
|
sentEnvelopesHashes = append(sentEnvelopesHashes, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get messages from filter before requesting them to mailserver
|
|
|
|
lastEnvelopeHash := sentEnvelopesHashes[len(sentEnvelopesHashes)-1]
|
|
|
|
s.waitForEnvelopeEvents(envelopeAvailableWatcher, []string{lastEnvelopeHash}, whisper.EventEnvelopeAvailable)
|
|
|
|
messages := s.getMessagesByMessageFilterID(clientRPCClient, filterID)
|
|
|
|
s.Equal(envelopesCount, len(messages))
|
|
|
|
|
|
|
|
messages = s.getMessagesByMessageFilterID(clientRPCClient, filterID)
|
|
|
|
s.Equal(0, len(messages))
|
|
|
|
|
|
|
|
limit := 3
|
|
|
|
|
|
|
|
getMessages := func() []string {
|
|
|
|
envelopes := s.getMessagesByMessageFilterID(clientRPCClient, filterID)
|
|
|
|
hashes := make([]string, 0)
|
|
|
|
for _, e := range envelopes {
|
|
|
|
hashes = append(hashes, e["hash"].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashes
|
|
|
|
}
|
|
|
|
|
|
|
|
requestMessages := func(cursor string) common.Hash {
|
|
|
|
return s.requestHistoricMessagesFromLast12Hours(clientWhisperService, clientRPCClient, mailboxEnode, mailServerKeyID, topic.String(), limit, cursor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for mailserver to archive all the envelopes
|
|
|
|
s.waitForEnvelopeEvents(envelopeArchivedWatcher, sentEnvelopesHashes, whisper.EventMailServerEnvelopeArchived)
|
|
|
|
|
|
|
|
// first page
|
|
|
|
// send request
|
|
|
|
requestID := requestMessages("")
|
|
|
|
// wait for mail server response
|
|
|
|
resp := s.waitForMailServerResponse(mailServerResponseWatcher, requestID)
|
|
|
|
s.NotEmpty(resp.LastEnvelopeHash)
|
|
|
|
s.NotEmpty(resp.Cursor)
|
|
|
|
// wait for last envelope sent by the mailserver to be available for filters
|
|
|
|
s.waitForEnvelopeEvents(envelopeAvailableWatcher, []string{resp.LastEnvelopeHash.String()}, whisper.EventEnvelopeAvailable)
|
|
|
|
// get messages
|
|
|
|
firstPageHashes := getMessages()
|
|
|
|
s.Equal(3, len(firstPageHashes))
|
|
|
|
|
|
|
|
// second page
|
|
|
|
// send request
|
|
|
|
requestID = requestMessages(fmt.Sprintf("%x", resp.Cursor))
|
|
|
|
// wait for mail server response
|
|
|
|
resp = s.waitForMailServerResponse(mailServerResponseWatcher, requestID)
|
|
|
|
s.NotEmpty(resp.LastEnvelopeHash)
|
|
|
|
// all messages have been sent, no more pages available
|
|
|
|
s.Empty(resp.Cursor)
|
|
|
|
// wait for last envelope sent by the mailserver to be available for filters
|
|
|
|
s.waitForEnvelopeEvents(envelopeAvailableWatcher, []string{resp.LastEnvelopeHash.String()}, whisper.EventEnvelopeAvailable)
|
|
|
|
// get messages
|
|
|
|
secondPageHashes := getMessages()
|
|
|
|
s.Equal(2, len(secondPageHashes))
|
|
|
|
|
|
|
|
allReceivedHashes := append(firstPageHashes, secondPageHashes...)
|
|
|
|
s.Equal(envelopesCount, len(allReceivedHashes))
|
|
|
|
|
|
|
|
// check that all the envelopes have been received
|
|
|
|
sort.Strings(sentEnvelopesHashes)
|
|
|
|
sort.Strings(allReceivedHashes)
|
|
|
|
s.Equal(sentEnvelopesHashes, allReceivedHashes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperMailboxSuite) waitForEnvelopeEvents(events chan whisper.EnvelopeEvent, hashes []string, event whisper.EventType) {
|
|
|
|
check := make(map[string]struct{})
|
|
|
|
for _, hash := range hashes {
|
|
|
|
check[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.NewTimer(time.Second * 5)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-events:
|
|
|
|
if e.Event == event {
|
|
|
|
delete(check, e.Hash.String())
|
|
|
|
if len(check) == 0 {
|
|
|
|
timeout.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeout.C:
|
|
|
|
s.FailNow("timed out while waiting for event on envelopes", "event: %s", event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperMailboxSuite) waitForMailServerResponse(events chan whisper.EnvelopeEvent, requestID common.Hash) *whisper.MailServerResponse {
|
|
|
|
timeout := time.NewTimer(time.Second * 5)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-events:
|
|
|
|
if event.Event == whisper.EventMailServerRequestCompleted && event.Hash == requestID {
|
|
|
|
timeout.Stop()
|
|
|
|
resp, ok := event.Data.(*whisper.MailServerResponse)
|
|
|
|
if !ok {
|
|
|
|
s.FailNow("mailserver response error", "expected whisper.MailServerResponse, got: %+v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
case <-timeout.C:
|
|
|
|
s.FailNow("timed out while waiting for mailserver response")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 09:25:30 +00:00
|
|
|
func newGroupChatParams(symkey []byte, topic whisper.TopicType) groupChatParams {
|
2017-12-26 12:21:07 +00:00
|
|
|
groupChatKeyStr := hexutil.Bytes(symkey).String()
|
|
|
|
return groupChatParams{
|
|
|
|
Key: groupChatKeyStr,
|
|
|
|
Topic: topic.String(),
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2017-12-26 12:21:07 +00:00
|
|
|
type groupChatParams struct {
|
|
|
|
Key string
|
|
|
|
Topic string
|
|
|
|
}
|
2017-12-12 11:00:17 +00:00
|
|
|
|
2017-12-26 12:21:07 +00:00
|
|
|
func (d *groupChatParams) Decode(i string) error {
|
|
|
|
b, err := hexutil.Decode(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(b, &d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *groupChatParams) Encode() (string, error) {
|
|
|
|
payload, err := json.Marshal(d)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hexutil.Bytes(payload).String(), nil
|
2017-12-07 13:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Start status node.
|
2017-12-26 12:21:07 +00:00
|
|
|
func (s *WhisperMailboxSuite) startBackend(name string) (*api.StatusBackend, func()) {
|
2018-02-08 12:52:47 +00:00
|
|
|
datadir := filepath.Join(RootDir, ".ethereumtest/mailbox", name)
|
2017-12-07 13:37:43 +00:00
|
|
|
backend := api.NewStatusBackend()
|
2018-03-02 09:25:30 +00:00
|
|
|
nodeConfig, err := MakeTestNodeConfig(GetNetworkID())
|
2017-12-26 12:21:07 +00:00
|
|
|
nodeConfig.DataDir = datadir
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().False(backend.IsNodeRunning())
|
2018-02-27 10:39:30 +00:00
|
|
|
|
2018-03-26 16:01:26 +00:00
|
|
|
nodeConfig.WhisperConfig.LightClient = true
|
|
|
|
|
2018-02-27 10:39:30 +00:00
|
|
|
if addr, err := GetRemoteURL(); err == nil {
|
|
|
|
nodeConfig.UpstreamConfig.Enabled = true
|
|
|
|
nodeConfig.UpstreamConfig.URL = addr
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
s.Require().NoError(backend.StartNode(nodeConfig))
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().True(backend.IsNodeRunning())
|
|
|
|
|
|
|
|
return backend, func() {
|
|
|
|
s.True(backend.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(backend.StopNode())
|
2017-12-07 13:37:43 +00:00
|
|
|
s.False(backend.IsNodeRunning())
|
2018-02-12 11:16:06 +00:00
|
|
|
err = os.RemoveAll(datadir)
|
|
|
|
s.Require().NoError(err)
|
2017-12-07 13:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-12-26 12:21:07 +00:00
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// Start mailbox node.
|
2017-12-07 13:37:43 +00:00
|
|
|
func (s *WhisperMailboxSuite) startMailboxBackend() (*api.StatusBackend, func()) {
|
|
|
|
mailboxBackend := api.NewStatusBackend()
|
2018-03-02 09:25:30 +00:00
|
|
|
mailboxConfig, err := MakeTestNodeConfig(GetNetworkID())
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().NoError(err)
|
2018-02-08 12:52:47 +00:00
|
|
|
datadir := filepath.Join(RootDir, ".ethereumtest/mailbox/mailserver")
|
2017-12-07 13:37:43 +00:00
|
|
|
|
|
|
|
mailboxConfig.LightEthConfig.Enabled = false
|
|
|
|
mailboxConfig.WhisperConfig.Enabled = true
|
2018-02-08 12:52:47 +00:00
|
|
|
mailboxConfig.KeyStoreDir = datadir
|
2017-12-07 13:37:43 +00:00
|
|
|
mailboxConfig.WhisperConfig.EnableMailServer = true
|
2018-07-04 09:30:57 +00:00
|
|
|
mailboxConfig.WhisperConfig.MailServerPasswordFile = filepath.Join(RootDir, "/static/keys/wnodepassword")
|
2018-02-08 12:52:47 +00:00
|
|
|
mailboxConfig.WhisperConfig.DataDir = filepath.Join(datadir, "data")
|
2017-12-26 12:21:07 +00:00
|
|
|
mailboxConfig.DataDir = datadir
|
2017-12-07 13:37:43 +00:00
|
|
|
|
2018-02-21 12:18:32 +00:00
|
|
|
s.Require().False(mailboxBackend.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.Require().NoError(mailboxBackend.StartNode(mailboxConfig))
|
2017-12-07 13:37:43 +00:00
|
|
|
s.Require().True(mailboxBackend.IsNodeRunning())
|
|
|
|
return mailboxBackend, func() {
|
|
|
|
s.True(mailboxBackend.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(mailboxBackend.StopNode())
|
2017-12-07 13:37:43 +00:00
|
|
|
s.False(mailboxBackend.IsNodeRunning())
|
2018-02-12 11:16:06 +00:00
|
|
|
err = os.RemoveAll(datadir)
|
|
|
|
s.Require().NoError(err)
|
2017-12-07 13:37:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// createPrivateChatMessageFilter create message filter with asymmetric encryption.
|
2017-12-26 12:21:07 +00:00
|
|
|
func (s *WhisperMailboxSuite) createPrivateChatMessageFilter(rpcCli *rpc.Client, privateKeyID string, topic string) string {
|
|
|
|
resp := rpcCli.CallRaw(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "shh_newMessageFilter", "params": [
|
|
|
|
{"privateKeyID": "` + privateKeyID + `", "topics": [ "` + topic + `"], "allowP2P":true}
|
|
|
|
],
|
|
|
|
"id": 1
|
|
|
|
}`)
|
|
|
|
|
|
|
|
msgFilterResp := returnedIDResponse{}
|
|
|
|
err := json.Unmarshal([]byte(resp), &msgFilterResp)
|
|
|
|
messageFilterID := msgFilterResp.Result
|
|
|
|
s.Require().NoError(err)
|
2018-03-26 16:01:26 +00:00
|
|
|
s.Require().Nil(msgFilterResp.Error)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().NotEqual("", messageFilterID, resp)
|
|
|
|
return messageFilterID
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// createGroupChatMessageFilter create message filter with symmetric encryption.
|
2017-12-26 12:21:07 +00:00
|
|
|
func (s *WhisperMailboxSuite) createGroupChatMessageFilter(rpcCli *rpc.Client, symkeyID string, topic string) string {
|
|
|
|
resp := rpcCli.CallRaw(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "shh_newMessageFilter", "params": [
|
|
|
|
{"symKeyID": "` + symkeyID + `", "topics": [ "` + topic + `"], "allowP2P":true}
|
|
|
|
],
|
|
|
|
"id": 1
|
|
|
|
}`)
|
|
|
|
|
|
|
|
msgFilterResp := returnedIDResponse{}
|
|
|
|
err := json.Unmarshal([]byte(resp), &msgFilterResp)
|
|
|
|
messageFilterID := msgFilterResp.Result
|
|
|
|
s.Require().NoError(err)
|
2018-03-26 16:01:26 +00:00
|
|
|
s.Require().Nil(msgFilterResp.Error)
|
2017-12-26 12:21:07 +00:00
|
|
|
s.Require().NotEqual("", messageFilterID, resp)
|
|
|
|
return messageFilterID
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:07:40 +00:00
|
|
|
func (s *WhisperMailboxSuite) postMessageToPrivate(rpcCli *rpc.Client, recipientPubkey string, topic string, payload string) string {
|
2017-12-26 12:21:07 +00:00
|
|
|
resp := rpcCli.CallRaw(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "shh_post",
|
|
|
|
"params": [
|
|
|
|
{
|
2018-07-04 11:07:40 +00:00
|
|
|
"pubKey": "` + recipientPubkey + `",
|
2017-12-26 12:21:07 +00:00
|
|
|
"topic": "` + topic + `",
|
|
|
|
"payload": "` + payload + `",
|
|
|
|
"powTarget": 0.001,
|
|
|
|
"powTime": 2
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"id": 1}`)
|
|
|
|
postResp := baseRPCResponse{}
|
|
|
|
err := json.Unmarshal([]byte(resp), &postResp)
|
|
|
|
s.Require().NoError(err)
|
2018-03-26 16:01:26 +00:00
|
|
|
s.Require().Nil(postResp.Error)
|
2018-06-15 17:25:10 +00:00
|
|
|
|
|
|
|
return postResp.Result.(string)
|
2017-12-26 12:21:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 17:25:10 +00:00
|
|
|
func (s *WhisperMailboxSuite) postMessageToGroup(rpcCli *rpc.Client, groupChatKeyID string, topic string, payload string) string {
|
2017-12-26 12:21:07 +00:00
|
|
|
resp := rpcCli.CallRaw(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "shh_post",
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"symKeyID": "` + groupChatKeyID + `",
|
|
|
|
"topic": "` + topic + `",
|
|
|
|
"payload": "` + payload + `",
|
|
|
|
"powTarget": 0.001,
|
|
|
|
"powTime": 2
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"id": 1}`)
|
|
|
|
postResp := baseRPCResponse{}
|
|
|
|
err := json.Unmarshal([]byte(resp), &postResp)
|
|
|
|
s.Require().NoError(err)
|
2018-03-26 16:01:26 +00:00
|
|
|
s.Require().Nil(postResp.Error)
|
2018-06-15 17:25:10 +00:00
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
hash, ok := postResp.Result.(string)
|
|
|
|
if !ok {
|
|
|
|
s.FailNow("error decoding result", "expected string, got: %+v", postResp.Result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(hash, "0x") {
|
|
|
|
s.FailNow("hash format error", "expected hex string, got: %s", hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash
|
2017-12-26 12:21:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// getMessagesByMessageFilterID gets received messages by messageFilterID.
|
2018-01-24 08:25:28 +00:00
|
|
|
func (s *WhisperMailboxSuite) getMessagesByMessageFilterID(rpcCli *rpc.Client, messageFilterID string) []map[string]interface{} {
|
2017-12-26 12:21:07 +00:00
|
|
|
resp := rpcCli.CallRaw(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"method": "shh_getFilterMessages",
|
|
|
|
"params": ["` + messageFilterID + `"],
|
|
|
|
"id": 1}`)
|
|
|
|
messages := getFilterMessagesResponse{}
|
|
|
|
err := json.Unmarshal([]byte(resp), &messages)
|
|
|
|
s.Require().NoError(err)
|
2018-03-26 16:01:26 +00:00
|
|
|
s.Require().Nil(messages.Error)
|
2017-12-26 12:21:07 +00:00
|
|
|
return messages.Result
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:05:24 +00:00
|
|
|
// addSymKey added symkey to node and return symkeyID.
|
2017-12-26 12:21:07 +00:00
|
|
|
func (s *WhisperMailboxSuite) addSymKey(rpcCli *rpc.Client, symkey string) string {
|
|
|
|
resp := rpcCli.CallRaw(`{"jsonrpc":"2.0","method":"shh_addSymKey",
|
|
|
|
"params":["` + symkey + `"],
|
|
|
|
"id":1}`)
|
|
|
|
symkeyAddResp := returnedIDResponse{}
|
|
|
|
err := json.Unmarshal([]byte(resp), &symkeyAddResp)
|
|
|
|
s.Require().NoError(err)
|
2018-03-26 16:01:26 +00:00
|
|
|
s.Require().Nil(symkeyAddResp.Error)
|
2017-12-26 12:21:07 +00:00
|
|
|
symkeyID := symkeyAddResp.Result
|
|
|
|
s.Require().NotEmpty(symkeyID)
|
|
|
|
return symkeyID
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:13:12 +00:00
|
|
|
// requestHistoricMessagesFromLast12Hours asks a mailnode to resend messages from last 12 hours.
|
2018-07-02 07:38:10 +00:00
|
|
|
func (s *WhisperMailboxSuite) requestHistoricMessagesFromLast12Hours(w *whisper.Whisper, rpcCli *rpc.Client, mailboxEnode, mailServerKeyID, topic string, limit int, cursor string) common.Hash {
|
2018-06-13 14:46:47 +00:00
|
|
|
currentTime := w.GetCurrentTime()
|
|
|
|
from := currentTime.Add(-12 * time.Hour)
|
|
|
|
to := currentTime
|
2018-07-02 07:38:10 +00:00
|
|
|
return s.requestHistoricMessages(w, rpcCli, mailboxEnode, mailServerKeyID, topic, from, to, limit, cursor)
|
2018-06-26 09:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// requestHistoricMessages asks a mailnode to resend messages.
|
2018-07-02 07:38:10 +00:00
|
|
|
func (s *WhisperMailboxSuite) requestHistoricMessages(w *whisper.Whisper, rpcCli *rpc.Client, mailboxEnode, mailServerKeyID, topic string, from, to time.Time, limit int, cursor string) common.Hash {
|
2017-12-26 12:21:07 +00:00
|
|
|
resp := rpcCli.CallRaw(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 2,
|
2018-04-26 05:56:19 +00:00
|
|
|
"method": "shhext_requestMessages",
|
2017-12-26 12:21:07 +00:00
|
|
|
"params": [{
|
|
|
|
"mailServerPeer":"` + mailboxEnode + `",
|
|
|
|
"topic":"` + topic + `",
|
|
|
|
"symKeyID":"` + mailServerKeyID + `",
|
2018-05-17 11:21:04 +00:00
|
|
|
"from":` + strconv.FormatInt(from.Unix(), 10) + `,
|
2018-07-02 07:38:10 +00:00
|
|
|
"to":` + strconv.FormatInt(to.Unix(), 10) + `,
|
|
|
|
"limit": ` + fmt.Sprintf("%d", limit) + `,
|
|
|
|
"cursor": "` + cursor + `"
|
2017-12-26 12:21:07 +00:00
|
|
|
}]
|
|
|
|
}`)
|
|
|
|
reqMessagesResp := baseRPCResponse{}
|
|
|
|
err := json.Unmarshal([]byte(resp), &reqMessagesResp)
|
|
|
|
s.Require().NoError(err)
|
2018-03-26 16:01:26 +00:00
|
|
|
s.Require().Nil(reqMessagesResp.Error)
|
2018-06-15 15:12:31 +00:00
|
|
|
|
|
|
|
switch hash := reqMessagesResp.Result.(type) {
|
|
|
|
case string:
|
|
|
|
s.Require().True(strings.HasPrefix(hash, "0x"))
|
|
|
|
b, err := hex.DecodeString(hash[2:])
|
|
|
|
s.Require().NoError(err)
|
2018-07-02 07:38:10 +00:00
|
|
|
return common.BytesToHash(b)
|
2018-06-15 15:12:31 +00:00
|
|
|
default:
|
|
|
|
s.Failf("failed reading shh_newMessageFilter result", "expected a hash, got: %+v", reqMessagesResp.Result)
|
|
|
|
}
|
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperMailboxSuite) joinPublicChat(w *whisper.Whisper, rpcClient *rpc.Client, name string) (string, whisper.TopicType, string) {
|
|
|
|
keyID, err := w.AddSymKeyFromPassword(name)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
h := sha3.NewKeccak256()
|
|
|
|
_, err = h.Write([]byte(name))
|
|
|
|
if err != nil {
|
|
|
|
s.Fail("error generating topic", "failed gerating topic from chat name, %+v", err)
|
|
|
|
}
|
|
|
|
fullTopic := h.Sum(nil)
|
|
|
|
topic := whisper.BytesToTopic(fullTopic)
|
|
|
|
|
|
|
|
filterID := s.createGroupChatMessageFilter(rpcClient, keyID, topic.String())
|
|
|
|
|
|
|
|
return keyID, topic, filterID
|
2017-12-26 12:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 13:37:43 +00:00
|
|
|
type getFilterMessagesResponse struct {
|
|
|
|
Result []map[string]interface{}
|
2018-03-26 16:01:26 +00:00
|
|
|
Error interface{}
|
2017-12-07 13:37:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 12:21:07 +00:00
|
|
|
type returnedIDResponse struct {
|
2017-12-07 13:37:43 +00:00
|
|
|
Result string
|
2018-03-26 16:01:26 +00:00
|
|
|
Error interface{}
|
2017-12-07 13:37:43 +00:00
|
|
|
}
|
2017-12-12 11:00:17 +00:00
|
|
|
type baseRPCResponse struct {
|
|
|
|
Result interface{}
|
2018-03-26 16:01:26 +00:00
|
|
|
Error interface{}
|
2017-12-07 13:37:43 +00:00
|
|
|
}
|