2018-05-11 19:43:07 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/binary"
|
2018-05-21 11:30:37 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-05-11 19:43:07 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-05-21 11:30:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2021-05-14 10:55:42 +00:00
|
|
|
waku "github.com/status-im/status-go/waku"
|
|
|
|
wakucommon "github.com/status-im/status-go/waku/common"
|
2018-05-11 19:43:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const powRequirement = 0.00001
|
|
|
|
|
|
|
|
var keyID string
|
|
|
|
var seed = time.Now().Unix()
|
2018-07-16 09:07:17 +00:00
|
|
|
var testPayload = []byte("test payload")
|
2018-05-11 19:43:07 +00:00
|
|
|
|
|
|
|
type ServerTestParams struct {
|
2019-11-23 17:57:05 +00:00
|
|
|
topic types.TopicType
|
2018-05-17 11:21:04 +00:00
|
|
|
birth uint32
|
2018-05-11 19:43:07 +00:00
|
|
|
low uint32
|
|
|
|
upp uint32
|
2018-07-02 07:38:10 +00:00
|
|
|
limit uint32
|
2018-05-11 19:43:07 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
func TestMailserverSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MailserverSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MailserverSuite struct {
|
|
|
|
suite.Suite
|
2021-05-14 10:55:42 +00:00
|
|
|
server *WakuMailServer
|
|
|
|
shh *waku.Waku
|
|
|
|
config *params.WakuConfig
|
2018-07-02 07:44:02 +00:00
|
|
|
dataDir string
|
2018-05-21 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MailserverSuite) SetupTest() {
|
2021-05-14 10:55:42 +00:00
|
|
|
s.server = &WakuMailServer{}
|
|
|
|
s.shh = waku.New(&waku.DefaultConfig, nil)
|
2020-01-08 11:12:23 +00:00
|
|
|
s.shh.RegisterMailServer(s.server)
|
2018-07-02 07:44:02 +00:00
|
|
|
|
2023-04-26 20:39:51 +00:00
|
|
|
tmpDir := s.T().TempDir()
|
2018-07-04 09:30:57 +00:00
|
|
|
s.dataDir = tmpDir
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
s.config = ¶ms.WakuConfig{
|
2018-09-13 16:31:29 +00:00
|
|
|
DataDir: tmpDir,
|
|
|
|
MailServerPassword: "testpassword",
|
2018-05-21 11:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MailserverSuite) TestInit() {
|
|
|
|
testCases := []struct {
|
2021-05-14 10:55:42 +00:00
|
|
|
config params.WakuConfig
|
2018-05-21 11:30:37 +00:00
|
|
|
expectedError error
|
|
|
|
info string
|
|
|
|
}{
|
|
|
|
{
|
2021-05-14 10:55:42 +00:00
|
|
|
config: params.WakuConfig{DataDir: ""},
|
2018-05-21 11:30:37 +00:00
|
|
|
expectedError: errDirectoryNotProvided,
|
2018-07-04 09:30:57 +00:00
|
|
|
info: "config with empty DataDir",
|
2018-05-21 11:30:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-05-14 10:55:42 +00:00
|
|
|
config: params.WakuConfig{
|
2018-07-04 09:30:57 +00:00
|
|
|
DataDir: s.config.DataDir,
|
|
|
|
MailServerPassword: "pwd",
|
|
|
|
},
|
2018-05-21 11:30:37 +00:00
|
|
|
expectedError: nil,
|
2018-07-04 09:30:57 +00:00
|
|
|
info: "config with correct DataDir and Password",
|
2018-05-21 11:30:37 +00:00
|
|
|
},
|
|
|
|
{
|
2021-05-14 10:55:42 +00:00
|
|
|
config: params.WakuConfig{
|
2018-07-04 09:30:57 +00:00
|
|
|
DataDir: s.config.DataDir,
|
|
|
|
MailServerPassword: "pwd",
|
|
|
|
MailServerRateLimit: 5,
|
|
|
|
},
|
|
|
|
expectedError: nil,
|
|
|
|
info: "config with rate limit",
|
2018-05-21 11:30:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.T().Run(tc.info, func(*testing.T) {
|
2021-05-14 10:55:42 +00:00
|
|
|
mailServer := &WakuMailServer{}
|
|
|
|
shh := waku.New(&waku.DefaultConfig, nil)
|
2020-01-08 11:12:23 +00:00
|
|
|
shh.RegisterMailServer(mailServer)
|
2018-07-04 09:30:57 +00:00
|
|
|
|
|
|
|
err := mailServer.Init(shh, &tc.config)
|
2021-05-14 10:55:42 +00:00
|
|
|
s.Require().Equal(tc.expectedError, err)
|
|
|
|
if err == nil {
|
|
|
|
defer mailServer.Close()
|
|
|
|
}
|
2018-07-04 09:30:57 +00:00
|
|
|
|
|
|
|
// db should be open only if there was no error
|
|
|
|
if tc.expectedError == nil {
|
2020-01-08 11:12:23 +00:00
|
|
|
s.NotNil(mailServer.ms.db)
|
2018-07-04 09:30:57 +00:00
|
|
|
} else {
|
2020-01-08 11:12:23 +00:00
|
|
|
s.Nil(mailServer.ms)
|
2018-07-04 09:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.config.MailServerRateLimit > 0 {
|
2020-01-08 11:12:23 +00:00
|
|
|
s.NotNil(mailServer.ms.rateLimiter)
|
2018-07-04 09:30:57 +00:00
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
})
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
func (s *MailserverSuite) TestArchive() {
|
2018-09-13 16:31:29 +00:00
|
|
|
config := *s.config
|
2018-07-04 09:30:57 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
err := s.server.Init(s.shh, &config)
|
2018-07-04 09:30:57 +00:00
|
|
|
s.Require().NoError(err)
|
2018-05-21 11:30:37 +00:00
|
|
|
defer s.server.Close()
|
|
|
|
|
|
|
|
env, err := generateEnvelope(time.Now())
|
|
|
|
s.NoError(err)
|
|
|
|
rawEnvelope, err := rlp.EncodeToBytes(env)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
s.server.Archive(env)
|
2019-11-23 17:57:05 +00:00
|
|
|
key := NewDBKey(env.Expiry-env.TTL, types.TopicType(env.Topic), types.Hash(env.Hash()))
|
2020-01-08 11:12:23 +00:00
|
|
|
archivedEnvelope, err := s.server.ms.db.GetEnvelope(key)
|
2018-05-21 11:30:37 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
s.Equal(rawEnvelope, archivedEnvelope)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MailserverSuite) TestManageLimits() {
|
2020-01-08 11:12:23 +00:00
|
|
|
err := s.server.Init(s.shh, s.config)
|
|
|
|
s.NoError(err)
|
|
|
|
s.server.ms.rateLimiter = newRateLimiter(time.Duration(5) * time.Millisecond)
|
|
|
|
s.False(s.server.ms.exceedsPeerRequests(types.BytesToHash([]byte("peerID"))))
|
|
|
|
s.Equal(1, len(s.server.ms.rateLimiter.db))
|
|
|
|
firstSaved := s.server.ms.rateLimiter.db["peerID"]
|
2018-05-21 11:30:37 +00:00
|
|
|
|
|
|
|
// second call when limit is not accomplished does not store a new limit
|
2020-01-08 11:12:23 +00:00
|
|
|
s.True(s.server.ms.exceedsPeerRequests(types.BytesToHash([]byte("peerID"))))
|
|
|
|
s.Equal(1, len(s.server.ms.rateLimiter.db))
|
|
|
|
s.Equal(firstSaved, s.server.ms.rateLimiter.db["peerID"])
|
2018-05-21 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MailserverSuite) TestDBKey() {
|
2019-11-23 17:57:05 +00:00
|
|
|
var h types.Hash
|
|
|
|
var emptyTopic types.TopicType
|
2018-05-11 19:43:07 +00:00
|
|
|
i := uint32(time.Now().Unix())
|
2019-05-09 10:58:02 +00:00
|
|
|
k := NewDBKey(i, emptyTopic, h)
|
2018-12-11 10:23:47 +00:00
|
|
|
s.Equal(len(k.Bytes()), DBKeyLength, "wrong DB key length")
|
|
|
|
s.Equal(byte(i%0x100), k.Bytes()[3], "raw representation should be big endian")
|
|
|
|
s.Equal(byte(i/0x1000000), k.Bytes()[0], "big endian expected")
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
func (s *MailserverSuite) TestRequestPaginationLimit() {
|
|
|
|
s.setupServer(s.server)
|
|
|
|
defer s.server.Close()
|
|
|
|
|
|
|
|
var (
|
2021-05-14 10:55:42 +00:00
|
|
|
sentEnvelopes []*wakucommon.Envelope
|
2019-04-23 07:04:58 +00:00
|
|
|
sentHashes []common.Hash
|
|
|
|
receivedHashes []common.Hash
|
|
|
|
archiveKeys []string
|
2018-07-02 07:38:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
count := uint32(10)
|
|
|
|
|
|
|
|
for i := count; i > 0; i-- {
|
|
|
|
sentTime := now.Add(time.Duration(-i) * time.Second)
|
|
|
|
env, err := generateEnvelope(sentTime)
|
|
|
|
s.NoError(err)
|
|
|
|
s.server.Archive(env)
|
2019-11-23 17:57:05 +00:00
|
|
|
key := NewDBKey(env.Expiry-env.TTL, types.TopicType(env.Topic), types.Hash(env.Hash()))
|
2019-05-09 10:58:02 +00:00
|
|
|
archiveKeys = append(archiveKeys, fmt.Sprintf("%x", key.Cursor()))
|
2018-07-02 07:38:10 +00:00
|
|
|
sentEnvelopes = append(sentEnvelopes, env)
|
2019-04-23 07:04:58 +00:00
|
|
|
sentHashes = append(sentHashes, env.Hash())
|
2018-07-02 07:38:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 08:55:37 +00:00
|
|
|
reqLimit := uint32(6)
|
|
|
|
peerID, request, err := s.prepareRequest(sentEnvelopes, reqLimit)
|
|
|
|
s.NoError(err)
|
2020-01-08 11:12:23 +00:00
|
|
|
payload, err := s.server.decompositeRequest(peerID, request)
|
2019-02-22 08:55:37 +00:00
|
|
|
s.NoError(err)
|
2020-01-08 11:12:23 +00:00
|
|
|
s.Nil(payload.Cursor)
|
|
|
|
s.Equal(reqLimit, payload.Limit)
|
2018-07-02 07:38:10 +00:00
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
receivedHashes, cursor, _ := processRequestAndCollectHashes(s.server, payload)
|
2018-07-02 07:38:10 +00:00
|
|
|
|
|
|
|
// 10 envelopes sent
|
|
|
|
s.Equal(count, uint32(len(sentEnvelopes)))
|
|
|
|
// 6 envelopes received
|
2020-01-08 11:12:23 +00:00
|
|
|
s.Len(receivedHashes, int(payload.Limit))
|
2019-04-23 07:04:58 +00:00
|
|
|
// the 6 envelopes received should be in forward order
|
2020-01-08 11:12:23 +00:00
|
|
|
s.Equal(sentHashes[:payload.Limit], receivedHashes)
|
2018-10-19 09:09:13 +00:00
|
|
|
// cursor should be the key of the last envelope of the last page
|
2020-01-08 11:12:23 +00:00
|
|
|
s.Equal(archiveKeys[payload.Limit-1], fmt.Sprintf("%x", cursor))
|
2018-07-02 07:38:10 +00:00
|
|
|
|
|
|
|
// second page
|
2020-01-08 11:12:23 +00:00
|
|
|
payload.Cursor = cursor
|
|
|
|
receivedHashes, cursor, _ = processRequestAndCollectHashes(s.server, payload)
|
2018-07-02 07:38:10 +00:00
|
|
|
|
|
|
|
// 4 envelopes received
|
2020-01-08 11:12:23 +00:00
|
|
|
s.Equal(int(count-payload.Limit), len(receivedHashes))
|
2018-07-02 07:38:10 +00:00
|
|
|
// cursor is nil because there are no other pages
|
|
|
|
s.Nil(cursor)
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
func (s *MailserverSuite) TestMailServer() {
|
2018-05-22 15:51:05 +00:00
|
|
|
s.setupServer(s.server)
|
|
|
|
defer s.server.Close()
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
env, err := generateEnvelope(time.Now())
|
|
|
|
s.NoError(err)
|
|
|
|
|
2018-05-22 15:51:05 +00:00
|
|
|
s.server.Archive(env)
|
2018-07-02 07:38:10 +00:00
|
|
|
|
2018-05-21 11:30:37 +00:00
|
|
|
testCases := []struct {
|
2018-05-22 15:51:05 +00:00
|
|
|
params *ServerTestParams
|
|
|
|
expect bool
|
|
|
|
isOK bool
|
|
|
|
info string
|
2018-05-21 11:30:37 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-05-22 15:51:05 +00:00
|
|
|
params: s.defaultServerParams(env),
|
|
|
|
expect: true,
|
|
|
|
isOK: true,
|
|
|
|
info: "Processing a request where from and to are equal to an existing register, should provide results",
|
2018-05-21 11:30:37 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-22 15:51:05 +00:00
|
|
|
params: func() *ServerTestParams {
|
|
|
|
params := s.defaultServerParams(env)
|
|
|
|
params.low = params.birth + 1
|
|
|
|
params.upp = params.birth + 1
|
|
|
|
|
|
|
|
return params
|
|
|
|
}(),
|
|
|
|
expect: false,
|
|
|
|
isOK: true,
|
|
|
|
info: "Processing a request where from and to are greater than any existing register, should not provide results",
|
2018-05-21 11:30:37 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-22 15:51:05 +00:00
|
|
|
params: func() *ServerTestParams {
|
|
|
|
params := s.defaultServerParams(env)
|
|
|
|
params.upp = params.birth + 1
|
|
|
|
params.topic[0] = 0xFF
|
|
|
|
|
|
|
|
return params
|
|
|
|
}(),
|
|
|
|
expect: false,
|
|
|
|
isOK: true,
|
|
|
|
info: "Processing a request where to is greater than any existing register and with a specific topic, should not provide results",
|
2018-05-21 11:30:37 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-22 15:51:05 +00:00
|
|
|
params: func() *ServerTestParams {
|
|
|
|
params := s.defaultServerParams(env)
|
2018-06-26 07:33:05 +00:00
|
|
|
params.low = params.birth
|
2018-05-22 15:51:05 +00:00
|
|
|
params.upp = params.birth - 1
|
|
|
|
|
|
|
|
return params
|
|
|
|
}(),
|
|
|
|
isOK: false,
|
|
|
|
info: "Processing a request where to is lower than from should fail",
|
2018-05-21 11:30:37 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-22 15:51:05 +00:00
|
|
|
params: func() *ServerTestParams {
|
|
|
|
params := s.defaultServerParams(env)
|
|
|
|
params.low = 0
|
|
|
|
params.upp = params.birth + 24
|
|
|
|
|
|
|
|
return params
|
|
|
|
}(),
|
|
|
|
isOK: false,
|
|
|
|
info: "Processing a request where difference between from and to is > 24 should fail",
|
2018-05-21 11:30:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.T().Run(tc.info, func(*testing.T) {
|
|
|
|
request := s.createRequest(tc.params)
|
|
|
|
src := crypto.FromECDSAPub(&tc.params.key.PublicKey)
|
2020-01-08 11:12:23 +00:00
|
|
|
payload, err := s.server.decompositeRequest(src, request)
|
2018-10-18 10:25:00 +00:00
|
|
|
s.Equal(tc.isOK, err == nil)
|
|
|
|
if err == nil {
|
2020-01-08 11:12:23 +00:00
|
|
|
s.Equal(tc.params.low, payload.Lower)
|
|
|
|
s.Equal(tc.params.upp, payload.Upper)
|
|
|
|
s.Equal(tc.params.limit, payload.Limit)
|
|
|
|
s.Equal(types.TopicToBloom(tc.params.topic), payload.Bloom)
|
|
|
|
s.Equal(tc.expect, s.messageExists(env, tc.params.low, tc.params.upp, payload.Bloom, tc.params.limit))
|
2018-05-21 11:30:37 +00:00
|
|
|
|
2018-05-22 15:51:05 +00:00
|
|
|
src[0]++
|
2020-01-08 11:12:23 +00:00
|
|
|
_, err = s.server.decompositeRequest(src, request)
|
2018-10-18 10:25:00 +00:00
|
|
|
s.True(err == nil)
|
2018-05-21 11:30:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
func (s *MailserverSuite) TestDecodeRequest() {
|
|
|
|
s.setupServer(s.server)
|
|
|
|
defer s.server.Close()
|
|
|
|
|
2018-12-11 10:23:47 +00:00
|
|
|
payload := MessagesRequestPayload{
|
2018-10-19 09:09:13 +00:00
|
|
|
Lower: 50,
|
|
|
|
Upper: 100,
|
|
|
|
Bloom: []byte{0x01},
|
2020-01-21 07:11:24 +00:00
|
|
|
Topics: [][]byte{},
|
2018-10-19 09:09:13 +00:00
|
|
|
Limit: 10,
|
|
|
|
Cursor: []byte{},
|
|
|
|
Batch: true,
|
|
|
|
}
|
|
|
|
data, err := rlp.EncodeToBytes(payload)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
id, err := s.shh.NewKeyPair()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
srcKey, err := s.shh.GetPrivateKey(id)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
env := s.createEnvelope(types.TopicType{0x01}, data, srcKey)
|
2018-10-19 09:09:13 +00:00
|
|
|
|
|
|
|
decodedPayload, err := s.server.decodeRequest(nil, env)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Equal(payload, decodedPayload)
|
|
|
|
}
|
|
|
|
|
2019-03-12 11:14:35 +00:00
|
|
|
func (s *MailserverSuite) TestDecodeRequestNoUpper() {
|
|
|
|
s.setupServer(s.server)
|
|
|
|
defer s.server.Close()
|
|
|
|
|
|
|
|
payload := MessagesRequestPayload{
|
|
|
|
Lower: 50,
|
|
|
|
Bloom: []byte{0x01},
|
|
|
|
Limit: 10,
|
|
|
|
Cursor: []byte{},
|
|
|
|
Batch: true,
|
|
|
|
}
|
|
|
|
data, err := rlp.EncodeToBytes(payload)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
id, err := s.shh.NewKeyPair()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
srcKey, err := s.shh.GetPrivateKey(id)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
env := s.createEnvelope(types.TopicType{0x01}, data, srcKey)
|
2019-03-12 11:14:35 +00:00
|
|
|
|
|
|
|
decodedPayload, err := s.server.decodeRequest(nil, env)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.NotEqual(0, decodedPayload.Upper)
|
|
|
|
}
|
|
|
|
|
2019-02-22 08:55:37 +00:00
|
|
|
func (s *MailserverSuite) TestProcessRequestDeadlockHandling() {
|
|
|
|
s.setupServer(s.server)
|
|
|
|
defer s.server.Close()
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
var archievedEnvelopes []*wakucommon.Envelope
|
2019-02-22 08:55:37 +00:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
count := uint32(10)
|
|
|
|
|
|
|
|
// Archieve some envelopes.
|
|
|
|
for i := count; i > 0; i-- {
|
|
|
|
sentTime := now.Add(time.Duration(-i) * time.Second)
|
|
|
|
env, err := generateEnvelope(sentTime)
|
|
|
|
s.NoError(err)
|
|
|
|
s.server.Archive(env)
|
|
|
|
archievedEnvelopes = append(archievedEnvelopes, env)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare a request.
|
|
|
|
peerID, request, err := s.prepareRequest(archievedEnvelopes, 5)
|
|
|
|
s.NoError(err)
|
2020-01-08 11:12:23 +00:00
|
|
|
payload, err := s.server.decompositeRequest(peerID, request)
|
2019-02-22 08:55:37 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Timeout time.Duration
|
|
|
|
Verify func(
|
2019-05-10 10:26:57 +00:00
|
|
|
Iterator,
|
2019-02-22 08:55:37 +00:00
|
|
|
time.Duration, // processRequestInBundles timeout
|
2019-05-09 10:58:02 +00:00
|
|
|
chan []rlp.RawValue,
|
2019-02-22 08:55:37 +00:00
|
|
|
)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "finish processing using `done` channel",
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
Verify: func(
|
2019-05-10 10:26:57 +00:00
|
|
|
iter Iterator,
|
2019-02-22 08:55:37 +00:00
|
|
|
timeout time.Duration,
|
2019-05-09 10:58:02 +00:00
|
|
|
bundles chan []rlp.RawValue,
|
2019-02-22 08:55:37 +00:00
|
|
|
) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
processFinished := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
2020-01-21 07:11:24 +00:00
|
|
|
s.server.ms.processRequestInBundles(iter, payload.Bloom, payload.Topics, int(payload.Limit), timeout, "req-01", bundles, done)
|
2019-02-22 08:55:37 +00:00
|
|
|
close(processFinished)
|
|
|
|
}()
|
|
|
|
go close(done)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processFinished:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
s.FailNow("waiting for processing finish timed out")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "finish processing due to timeout",
|
|
|
|
Timeout: time.Second,
|
|
|
|
Verify: func(
|
2019-05-10 10:26:57 +00:00
|
|
|
iter Iterator,
|
2019-02-22 08:55:37 +00:00
|
|
|
timeout time.Duration,
|
2019-05-09 10:58:02 +00:00
|
|
|
bundles chan []rlp.RawValue,
|
2019-02-22 08:55:37 +00:00
|
|
|
) {
|
|
|
|
done := make(chan struct{}) // won't be closed because we test timeout of `processRequestInBundles()`
|
|
|
|
processFinished := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
2020-01-21 07:11:24 +00:00
|
|
|
s.server.ms.processRequestInBundles(iter, payload.Bloom, payload.Topics, int(payload.Limit), time.Second, "req-01", bundles, done)
|
2019-02-22 08:55:37 +00:00
|
|
|
close(processFinished)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processFinished:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
s.FailNow("waiting for processing finish timed out")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.T().Run(tc.Name, func(t *testing.T) {
|
2020-01-08 11:12:23 +00:00
|
|
|
iter, err := s.server.ms.createIterator(payload)
|
2019-05-13 09:25:46 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-01-21 07:11:24 +00:00
|
|
|
defer func() { _ = iter.Release() }()
|
2019-02-22 08:55:37 +00:00
|
|
|
|
|
|
|
// Nothing reads from this unbuffered channel which simulates a situation
|
|
|
|
// when a connection between a peer and mail server was dropped.
|
2019-05-09 10:58:02 +00:00
|
|
|
bundles := make(chan []rlp.RawValue)
|
2019-02-22 08:55:37 +00:00
|
|
|
|
|
|
|
tc.Verify(iter, tc.Timeout, bundles)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func (s *MailserverSuite) messageExists(envelope *wakucommon.Envelope, low, upp uint32, bloom []byte, limit uint32) bool {
|
2020-01-08 11:12:23 +00:00
|
|
|
receivedHashes, _, _ := processRequestAndCollectHashes(s.server, MessagesRequestPayload{
|
|
|
|
Lower: low,
|
|
|
|
Upper: upp,
|
|
|
|
Bloom: bloom,
|
|
|
|
Limit: limit,
|
|
|
|
})
|
2018-12-06 09:48:28 +00:00
|
|
|
for _, hash := range receivedHashes {
|
|
|
|
if hash == envelope.Hash() {
|
|
|
|
return true
|
2018-05-22 15:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 09:48:28 +00:00
|
|
|
return false
|
2018-05-22 15:51:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func (s *MailserverSuite) setupServer(server *WakuMailServer) {
|
2018-05-21 11:30:37 +00:00
|
|
|
const password = "password_for_this_test"
|
2018-05-11 19:43:07 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
s.shh = waku.New(&waku.DefaultConfig, nil)
|
2020-01-08 11:12:23 +00:00
|
|
|
s.shh.RegisterMailServer(server)
|
2018-05-21 11:30:37 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
err := server.Init(s.shh, ¶ms.WakuConfig{
|
2018-07-04 09:30:57 +00:00
|
|
|
DataDir: s.dataDir,
|
|
|
|
MailServerPassword: password,
|
|
|
|
MinimumPoW: powRequirement,
|
|
|
|
})
|
2018-05-11 19:43:07 +00:00
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
s.T().Fatal(err)
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
|
|
|
|
keyID, err = s.shh.AddSymKeyFromPassword(password)
|
2018-05-11 19:43:07 +00:00
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
s.T().Fatalf("failed to create symmetric key for mail request: %s", err)
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func (s *MailserverSuite) prepareRequest(envelopes []*wakucommon.Envelope, limit uint32) (
|
|
|
|
[]byte, *wakucommon.Envelope, error,
|
2019-02-22 08:55:37 +00:00
|
|
|
) {
|
|
|
|
if len(envelopes) == 0 {
|
|
|
|
return nil, nil, errors.New("envelopes is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
params := s.defaultServerParams(envelopes[0])
|
|
|
|
params.low = uint32(now.Add(time.Duration(-len(envelopes)) * time.Second).Unix())
|
|
|
|
params.upp = uint32(now.Unix())
|
|
|
|
params.limit = limit
|
|
|
|
|
|
|
|
request := s.createRequest(params)
|
|
|
|
peerID := crypto.FromECDSAPub(¶ms.key.PublicKey)
|
|
|
|
|
|
|
|
return peerID, request, nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func (s *MailserverSuite) defaultServerParams(env *wakucommon.Envelope) *ServerTestParams {
|
2018-05-21 11:30:37 +00:00
|
|
|
id, err := s.shh.NewKeyPair()
|
2018-05-11 19:43:07 +00:00
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
s.T().Fatalf("failed to generate new key pair with seed %d: %s.", seed, err)
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
testPeerID, err := s.shh.GetPrivateKey(id)
|
2018-05-11 19:43:07 +00:00
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
s.T().Fatalf("failed to retrieve new key pair with seed %d: %s.", seed, err)
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
birth := env.Expiry - env.TTL
|
2018-05-17 11:21:04 +00:00
|
|
|
|
|
|
|
return &ServerTestParams{
|
2019-11-23 17:57:05 +00:00
|
|
|
topic: types.TopicType(env.Topic),
|
2018-05-17 11:21:04 +00:00
|
|
|
birth: birth,
|
2018-05-11 19:43:07 +00:00
|
|
|
low: birth - 1,
|
|
|
|
upp: birth + 1,
|
2018-07-02 07:38:10 +00:00
|
|
|
limit: 0,
|
2018-05-11 19:43:07 +00:00
|
|
|
key: testPeerID,
|
|
|
|
}
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
2018-05-11 19:43:07 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func (s *MailserverSuite) createRequest(p *ServerTestParams) *wakucommon.Envelope {
|
2019-11-23 17:57:05 +00:00
|
|
|
bloom := types.TopicToBloom(p.topic)
|
2018-05-11 19:43:07 +00:00
|
|
|
data := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint32(data, p.low)
|
|
|
|
binary.BigEndian.PutUint32(data[4:], p.upp)
|
|
|
|
data = append(data, bloom...)
|
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
if p.limit != 0 {
|
|
|
|
limitData := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(limitData, p.limit)
|
|
|
|
data = append(data, limitData...)
|
|
|
|
}
|
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
return s.createEnvelope(p.topic, data, p.key)
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func (s *MailserverSuite) createEnvelope(topic types.TopicType, data []byte, srcKey *ecdsa.PrivateKey) *wakucommon.Envelope {
|
2018-05-21 11:30:37 +00:00
|
|
|
key, err := s.shh.GetSymKey(keyID)
|
2018-05-11 19:43:07 +00:00
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
s.T().Fatalf("failed to retrieve sym key with seed %d: %s.", seed, err)
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
params := &wakucommon.MessageParams{
|
2018-05-11 19:43:07 +00:00
|
|
|
KeySym: key,
|
2021-05-14 10:55:42 +00:00
|
|
|
Topic: wakucommon.TopicType(topic),
|
2018-05-11 19:43:07 +00:00
|
|
|
Payload: data,
|
|
|
|
PoW: powRequirement * 2,
|
|
|
|
WorkTime: 2,
|
2018-10-19 09:09:13 +00:00
|
|
|
Src: srcKey,
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
msg, err := wakucommon.NewSentMessage(params)
|
2018-05-11 19:43:07 +00:00
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
s.T().Fatalf("failed to create new message with seed %d: %s.", seed, err)
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
2018-10-19 09:09:13 +00:00
|
|
|
|
2018-05-11 19:43:07 +00:00
|
|
|
env, err := msg.Wrap(params, time.Now())
|
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
s.T().Fatalf("failed to wrap with seed %d: %s.", seed, err)
|
2018-05-11 19:43:07 +00:00
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
2018-05-17 11:21:04 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func generateEnvelopeWithKeys(sentTime time.Time, keySym []byte, keyAsym *ecdsa.PublicKey) (*wakucommon.Envelope, error) {
|
|
|
|
params := &wakucommon.MessageParams{
|
|
|
|
Topic: wakucommon.TopicType{0x1F, 0x7E, 0xA1, 0x7F},
|
2018-07-16 09:07:17 +00:00
|
|
|
Payload: testPayload,
|
2018-05-21 11:30:37 +00:00
|
|
|
PoW: powRequirement,
|
|
|
|
WorkTime: 2,
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 09:07:17 +00:00
|
|
|
if len(keySym) > 0 {
|
|
|
|
params.KeySym = keySym
|
|
|
|
} else if keyAsym != nil {
|
|
|
|
params.Dst = keyAsym
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
msg, err := wakucommon.NewSentMessage(params)
|
2018-05-17 11:21:04 +00:00
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to create new message with seed %d: %s", seed, err)
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
env, err := msg.Wrap(params, sentTime)
|
2018-05-17 11:21:04 +00:00
|
|
|
if err != nil {
|
2018-05-21 11:30:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to wrap with seed %d: %s", seed, err)
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
2018-05-21 11:30:37 +00:00
|
|
|
|
|
|
|
return env, nil
|
2018-05-17 11:21:04 +00:00
|
|
|
}
|
2018-07-16 09:07:17 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func generateEnvelope(sentTime time.Time) (*wakucommon.Envelope, error) {
|
2018-07-16 09:07:17 +00:00
|
|
|
h := crypto.Keccak256Hash([]byte("test sample data"))
|
|
|
|
return generateEnvelopeWithKeys(sentTime, h[:], nil)
|
|
|
|
}
|
2018-10-28 22:33:58 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func processRequestAndCollectHashes(server *WakuMailServer, payload MessagesRequestPayload) ([]common.Hash, []byte, types.Hash) {
|
2020-01-08 11:12:23 +00:00
|
|
|
iter, _ := server.ms.createIterator(payload)
|
2020-01-21 07:11:24 +00:00
|
|
|
defer func() { _ = iter.Release() }()
|
2019-05-09 10:58:02 +00:00
|
|
|
bundles := make(chan []rlp.RawValue, 10)
|
2018-12-06 09:48:28 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
var hashes []common.Hash
|
|
|
|
go func() {
|
|
|
|
for bundle := range bundles {
|
2019-05-09 10:58:02 +00:00
|
|
|
for _, rawEnvelope := range bundle {
|
2021-05-14 10:55:42 +00:00
|
|
|
var env *wakucommon.Envelope
|
2019-05-09 10:58:02 +00:00
|
|
|
if err := rlp.DecodeBytes(rawEnvelope, &env); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-06 09:48:28 +00:00
|
|
|
hashes = append(hashes, env.Hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2020-01-21 07:11:24 +00:00
|
|
|
cursor, lastHash := server.ms.processRequestInBundles(iter, payload.Bloom, payload.Topics, int(payload.Limit), time.Minute, "req-01", bundles, done)
|
2018-12-06 09:48:28 +00:00
|
|
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
return hashes, cursor, lastHash
|
|
|
|
}
|