2018-09-20 11:25:32 +00:00
|
|
|
// node-canary tests whether a P2P peer is responding correctly.
|
2018-07-12 14:50:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
stdlog "log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
|
2018-07-12 14:50:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-11-19 18:29:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2018-07-12 14:50:49 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
2019-11-23 17:57:05 +00:00
|
|
|
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-07-12 14:50:49 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/services/ext"
|
2020-08-25 22:04:10 +00:00
|
|
|
"github.com/status-im/status-go/services/wakuext"
|
2018-07-12 14:50:49 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mailboxPassword = "status-offline-inbox"
|
|
|
|
)
|
|
|
|
|
|
|
|
// All general log messages in this package should be routed through this logger.
|
2018-09-20 17:15:35 +00:00
|
|
|
var logger = log.New("package", "status-go/cmd/node-canary")
|
2018-07-12 14:50:49 +00:00
|
|
|
|
|
|
|
var (
|
2020-08-25 22:04:10 +00:00
|
|
|
staticEnodeAddr = flag.String("staticnode", "", "checks if static node talks waku protocol (e.g. enode://abc123@1.2.3.4:30303)")
|
2019-10-22 14:55:18 +00:00
|
|
|
mailserverEnodeAddr = flag.String("mailserver", "", "queries mail server for historic messages (e.g. enode://123abc@4.3.2.1:30504)")
|
2018-09-20 11:25:32 +00:00
|
|
|
publicChannel = flag.String("channel", "status", "The public channel name to retrieve historic messages from (used with 'mailserver' flag)")
|
2018-10-27 15:39:06 +00:00
|
|
|
timeout = flag.Int("timeout", 10, "Timeout when connecting to node or fetching messages from mailserver, in seconds")
|
2018-09-20 11:25:32 +00:00
|
|
|
period = flag.Int("period", 24*60*60, "How far in the past to request messages from mailserver, in seconds")
|
2020-08-25 22:04:10 +00:00
|
|
|
minPow = flag.Float64("waku.pow", params.WakuMinimumPoW, "PoW for messages to be added to queue, in float format")
|
|
|
|
ttl = flag.Int("waku.ttl", params.WakuTTL, "Time to live for messages, in seconds")
|
2018-09-20 11:25:32 +00:00
|
|
|
homePath = flag.String("home-dir", ".", "Home directory where state is stored")
|
|
|
|
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
|
|
|
|
logFile = flag.String("logfile", "", "Path to the log file")
|
|
|
|
logWithoutColors = flag.Bool("log-without-color", false, "Disables log colors")
|
2018-07-12 14:50:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2018-09-20 11:25:32 +00:00
|
|
|
var err error
|
2018-11-19 18:29:15 +00:00
|
|
|
var staticParsedNode, mailserverParsedNode *enode.Node
|
2018-09-20 11:25:32 +00:00
|
|
|
if *staticEnodeAddr != "" {
|
2018-11-19 18:29:15 +00:00
|
|
|
staticParsedNode, err = enode.ParseV4(*staticEnodeAddr)
|
2018-09-20 11:25:32 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Crit("Invalid static address specified", "staticEnodeAddr", *staticEnodeAddr, "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-07-12 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 11:25:32 +00:00
|
|
|
if *mailserverEnodeAddr != "" {
|
2018-11-19 18:29:15 +00:00
|
|
|
mailserverParsedNode, err = enode.ParseV4(*mailserverEnodeAddr)
|
2018-09-20 11:25:32 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Crit("Invalid mailserver address specified", "mailserverEnodeAddr", *mailserverEnodeAddr, "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if staticParsedNode != nil {
|
|
|
|
verifyStaticNodeBehavior(staticParsedNode)
|
|
|
|
logger.Info("Connected to static node correctly", "address", *staticEnodeAddr)
|
|
|
|
os.Exit(0)
|
2018-07-12 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 11:25:32 +00:00
|
|
|
if mailserverParsedNode != nil {
|
|
|
|
verifyMailserverBehavior(mailserverParsedNode)
|
|
|
|
logger.Info("Mailserver responded correctly", "address", *mailserverEnodeAddr)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Crit("No address specified")
|
|
|
|
os.Exit(1)
|
2018-07-12 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
colors := !(*logWithoutColors)
|
|
|
|
if colors {
|
|
|
|
colors = terminal.IsTerminal(int(os.Stdin.Fd()))
|
|
|
|
}
|
|
|
|
|
2019-03-01 13:37:13 +00:00
|
|
|
if err := logutils.OverrideRootLog(*logLevel != "", *logLevel, logutils.FileOptions{Filename: *logFile}, colors); err != nil {
|
2018-07-12 14:50:49 +00:00
|
|
|
stdlog.Fatalf("Error initializing logger: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 18:29:15 +00:00
|
|
|
func verifyMailserverBehavior(mailserverNode *enode.Node) {
|
2018-07-12 14:50:49 +00:00
|
|
|
clientBackend, err := startClientNode()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Node start failed", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer func() { _ = clientBackend.StopNode() }()
|
|
|
|
|
|
|
|
clientNode := clientBackend.StatusNode()
|
2020-08-25 22:04:10 +00:00
|
|
|
clientGethWakuService, err := clientNode.WakuService()
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
2020-08-25 22:04:10 +00:00
|
|
|
logger.Error("Could not retrieve waku service", "error", err)
|
2018-07-12 14:50:49 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-08-25 22:04:10 +00:00
|
|
|
clientWakuService := gethbridge.NewGethWakuWrapper(clientGethWakuService)
|
|
|
|
clientWakuExtService, err := clientNode.WakuExtService()
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
2020-08-25 22:04:10 +00:00
|
|
|
logger.Error("Could not retrieve wakuext service", "error", err)
|
2018-07-12 14:50:49 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add mailserver peer to client
|
2018-10-27 15:39:06 +00:00
|
|
|
clientErrCh := helpers.WaitForPeerAsync(clientNode.Server(), *mailserverEnodeAddr, p2p.PeerEventTypeAdd, time.Duration(*timeout)*time.Second)
|
|
|
|
|
2018-09-20 11:25:32 +00:00
|
|
|
err = clientNode.AddPeer(*mailserverEnodeAddr)
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to add mailserver peer to client", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = <-clientErrCh
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error detected while waiting for mailserver peer to be added", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add mailserver sym key
|
2020-08-25 22:04:10 +00:00
|
|
|
mailServerKeyID, err := clientWakuService.AddSymKeyFromPassword(mailboxPassword)
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error adding mailserver sym key to client peer", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-11-19 18:29:15 +00:00
|
|
|
mailboxPeer := mailserverNode.ID().Bytes()
|
2020-08-25 22:04:10 +00:00
|
|
|
err = clientGethWakuService.AllowP2PMessagesFromPeer(mailboxPeer)
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
2018-11-19 18:29:15 +00:00
|
|
|
logger.Error("Failed to allow P2P messages from mailserver peer", "error", err, mailserverNode.String())
|
2018-07-12 14:50:49 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientRPCClient := clientNode.RPCClient()
|
|
|
|
|
2020-08-25 22:04:10 +00:00
|
|
|
_, topic, _, err := joinPublicChat(clientWakuService, clientRPCClient, *publicChannel)
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to join public chat", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch for envelopes to be available in filters in the client
|
2019-11-23 17:57:05 +00:00
|
|
|
envelopeAvailableWatcher := make(chan types.EnvelopeEvent, 1024)
|
2020-08-25 22:04:10 +00:00
|
|
|
sub := clientWakuService.SubscribeEnvelopeEvents(envelopeAvailableWatcher)
|
2018-07-12 14:50:49 +00:00
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// watch for mailserver responses in the client
|
2019-11-23 17:57:05 +00:00
|
|
|
mailServerResponseWatcher := make(chan types.EnvelopeEvent, 1024)
|
2020-08-25 22:04:10 +00:00
|
|
|
sub = clientWakuService.SubscribeEnvelopeEvents(mailServerResponseWatcher)
|
2018-07-12 14:50:49 +00:00
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// request messages from mailbox
|
2020-08-25 22:04:10 +00:00
|
|
|
wakuextAPI := wakuext.NewPublicAPI(clientWakuExtService)
|
|
|
|
requestIDBytes, err := wakuextAPI.RequestMessages(context.TODO(),
|
2020-01-20 20:56:06 +00:00
|
|
|
ext.MessagesRequest{
|
2018-11-19 18:29:15 +00:00
|
|
|
MailServerPeer: mailserverNode.String(),
|
2020-08-25 22:04:10 +00:00
|
|
|
From: uint32(clientWakuService.GetCurrentTime().Add(-time.Duration(*period) * time.Second).Unix()),
|
2018-07-12 14:50:49 +00:00
|
|
|
Limit: 1,
|
|
|
|
Topic: topic,
|
|
|
|
SymKeyID: mailServerKeyID,
|
2018-12-05 13:57:05 +00:00
|
|
|
Timeout: time.Duration(*timeout),
|
2018-07-12 14:50:49 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error requesting historic messages from mailserver", "error", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
requestID := types.BytesToHash(requestIDBytes)
|
2018-07-12 14:50:49 +00:00
|
|
|
|
2018-12-06 09:48:28 +00:00
|
|
|
// wait for mailserver request sent event
|
|
|
|
err = waitForMailServerRequestSent(mailServerResponseWatcher, requestID, time.Duration(*timeout)*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error waiting for mailserver request sent event", "error", err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:50:49 +00:00
|
|
|
// wait for mailserver response
|
2018-10-27 15:39:06 +00:00
|
|
|
resp, err := waitForMailServerResponse(mailServerResponseWatcher, requestID, time.Duration(*timeout)*time.Second)
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error waiting for mailserver response", "error", err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:55:18 +00:00
|
|
|
// if last envelope is empty there are no messages to receive
|
|
|
|
if isEmptyEnvelope(resp.LastEnvelopeHash) {
|
|
|
|
logger.Warn("No messages available from mailserver")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:50:49 +00:00
|
|
|
// wait for last envelope sent by the mailserver to be available for filters
|
2019-11-23 17:57:05 +00:00
|
|
|
err = waitForEnvelopeEvents(envelopeAvailableWatcher, []string{resp.LastEnvelopeHash.String()}, types.EventEnvelopeAvailable)
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error waiting for envelopes to be available to client filter", "error", err)
|
|
|
|
os.Exit(4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 18:29:15 +00:00
|
|
|
func verifyStaticNodeBehavior(staticNode *enode.Node) {
|
2018-09-20 11:25:32 +00:00
|
|
|
clientBackend, err := startClientNode()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Node start failed", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer func() { _ = clientBackend.StopNode() }()
|
|
|
|
|
|
|
|
clientNode := clientBackend.StatusNode()
|
|
|
|
|
|
|
|
// wait for peer to be added to client
|
|
|
|
clientErrCh := helpers.WaitForPeerAsync(clientNode.Server(), *staticEnodeAddr, p2p.PeerEventTypeAdd, 5*time.Second)
|
|
|
|
err = <-clientErrCh
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error detected while waiting for static peer to be added", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait to check if peer remains connected to client
|
|
|
|
clientErrCh = helpers.WaitForPeerAsync(clientNode.Server(), *staticEnodeAddr, p2p.PeerEventTypeDrop, 5*time.Second)
|
|
|
|
err = <-clientErrCh
|
|
|
|
peers := clientNode.GethNode().Server().Peers()
|
|
|
|
if len(peers) != 1 {
|
|
|
|
logger.Error("Failed to add static peer", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:50:49 +00:00
|
|
|
// makeNodeConfig parses incoming CLI options and returns node configuration object
|
|
|
|
func makeNodeConfig() (*params.NodeConfig, error) {
|
|
|
|
err := error(nil)
|
|
|
|
|
|
|
|
workDir := ""
|
|
|
|
if path.IsAbs(*homePath) {
|
|
|
|
workDir = *homePath
|
|
|
|
} else {
|
|
|
|
workDir, err = filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
|
if err == nil {
|
|
|
|
workDir = path.Join(workDir, *homePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:09:31 +00:00
|
|
|
nodeConfig, err := params.NewNodeConfigWithDefaults(path.Join(workDir, ".ethereum"), uint64(params.RopstenNetworkID))
|
2018-07-12 14:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if *logLevel != "" {
|
|
|
|
nodeConfig.LogLevel = *logLevel
|
|
|
|
nodeConfig.LogEnabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if *logFile != "" {
|
|
|
|
nodeConfig.LogFile = *logFile
|
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
nodeConfig.EnableNTPSync = true
|
2018-10-27 13:20:17 +00:00
|
|
|
nodeConfig.NoDiscovery = true
|
2018-09-19 12:45:43 +00:00
|
|
|
nodeConfig.ListenAddr = ""
|
2018-09-20 11:25:32 +00:00
|
|
|
if *staticEnodeAddr != "" {
|
|
|
|
nodeConfig.ClusterConfig.Enabled = true
|
2018-10-04 10:40:26 +00:00
|
|
|
nodeConfig.ClusterConfig.Fleet = params.FleetUndefined
|
2018-09-20 11:25:32 +00:00
|
|
|
nodeConfig.ClusterConfig.StaticNodes = []string{
|
|
|
|
*staticEnodeAddr,
|
|
|
|
}
|
|
|
|
}
|
2018-07-12 14:50:49 +00:00
|
|
|
|
2020-08-25 22:04:10 +00:00
|
|
|
return wakuConfig(nodeConfig)
|
2018-07-12 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 22:04:10 +00:00
|
|
|
// wakuConfig creates node configuration object from flags
|
|
|
|
func wakuConfig(nodeConfig *params.NodeConfig) (*params.NodeConfig, error) {
|
|
|
|
wakuConfig := &nodeConfig.WakuConfig
|
|
|
|
wakuConfig.Enabled = true
|
|
|
|
wakuConfig.LightClient = true
|
|
|
|
wakuConfig.MinimumPoW = *minPow
|
|
|
|
wakuConfig.TTL = *ttl
|
2018-07-12 14:50:49 +00:00
|
|
|
|
|
|
|
return nodeConfig, nil
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func startClientNode() (*api.GethStatusBackend, error) {
|
2018-07-12 14:50:49 +00:00
|
|
|
config, err := makeNodeConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
clientBackend := api.NewGethStatusBackend()
|
2019-10-22 14:55:18 +00:00
|
|
|
err = clientBackend.AccountManager().InitKeystore(config.KeyStoreDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-12 14:50:49 +00:00
|
|
|
err = clientBackend.StartNode(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return clientBackend, err
|
|
|
|
}
|
|
|
|
|
2020-08-25 22:04:10 +00:00
|
|
|
func joinPublicChat(w types.Waku, rpcClient *rpc.Client, name string) (string, types.TopicType, string, error) {
|
2018-07-12 14:50:49 +00:00
|
|
|
keyID, err := w.AddSymKeyFromPassword(name)
|
|
|
|
if err != nil {
|
2019-11-23 17:57:05 +00:00
|
|
|
return "", types.TopicType{}, "", err
|
2018-07-12 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 14:42:00 +00:00
|
|
|
h := sha3.NewLegacyKeccak256()
|
2018-07-12 14:50:49 +00:00
|
|
|
_, err = h.Write([]byte(name))
|
|
|
|
if err != nil {
|
2019-11-23 17:57:05 +00:00
|
|
|
return "", types.TopicType{}, "", err
|
2018-07-12 14:50:49 +00:00
|
|
|
}
|
|
|
|
fullTopic := h.Sum(nil)
|
2019-11-23 17:57:05 +00:00
|
|
|
topic := types.BytesToTopic(fullTopic)
|
2018-07-12 14:50:49 +00:00
|
|
|
|
2020-08-25 22:04:10 +00:00
|
|
|
wakuAPI := w.PublicWakuAPI()
|
|
|
|
filterID, err := wakuAPI.NewMessageFilter(types.Criteria{SymKeyID: keyID, Topics: []types.TopicType{topic}})
|
2018-07-12 14:50:49 +00:00
|
|
|
|
|
|
|
return keyID, topic, filterID, err
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func waitForMailServerRequestSent(events chan types.EnvelopeEvent, requestID types.Hash, timeout time.Duration) error {
|
2018-12-06 09:48:28 +00:00
|
|
|
timeoutTimer := time.NewTimer(timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-events:
|
2019-11-23 17:57:05 +00:00
|
|
|
if event.Hash == requestID && event.Event == types.EventMailServerRequestSent {
|
2018-12-06 09:48:28 +00:00
|
|
|
timeoutTimer.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case <-timeoutTimer.C:
|
|
|
|
return errors.New("timed out waiting for mailserver request sent")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func waitForMailServerResponse(events chan types.EnvelopeEvent, requestID types.Hash, timeout time.Duration) (*types.MailServerResponse, error) {
|
2018-07-12 14:50:49 +00:00
|
|
|
timeoutTimer := time.NewTimer(timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-events:
|
|
|
|
if event.Hash == requestID {
|
|
|
|
resp, err := decodeMailServerResponse(event)
|
|
|
|
if resp != nil || err != nil {
|
|
|
|
timeoutTimer.Stop()
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeoutTimer.C:
|
|
|
|
return nil, errors.New("timed out waiting for mailserver response")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func decodeMailServerResponse(event types.EnvelopeEvent) (*types.MailServerResponse, error) {
|
2018-07-12 14:50:49 +00:00
|
|
|
switch event.Event {
|
2019-11-23 17:57:05 +00:00
|
|
|
case types.EventMailServerRequestSent:
|
2018-12-05 13:57:05 +00:00
|
|
|
return nil, nil
|
2019-11-23 17:57:05 +00:00
|
|
|
case types.EventMailServerRequestCompleted:
|
|
|
|
resp, ok := event.Data.(*types.MailServerResponse)
|
2018-07-12 14:50:49 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("failed to convert event to a *MailServerResponse")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2019-11-23 17:57:05 +00:00
|
|
|
case types.EventMailServerRequestExpired:
|
2018-07-12 14:50:49 +00:00
|
|
|
return nil, errors.New("no messages available from mailserver")
|
|
|
|
default:
|
2018-12-06 09:48:28 +00:00
|
|
|
return nil, fmt.Errorf("unexpected event type: %v", event.Event)
|
2018-07-12 14:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func waitForEnvelopeEvents(events chan types.EnvelopeEvent, hashes []string, event types.EventType) error {
|
2018-07-12 14:50:49 +00:00
|
|
|
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 nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeout.C:
|
|
|
|
return fmt.Errorf("timed out while waiting for event on envelopes. event: %s", event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-22 14:55:18 +00:00
|
|
|
|
|
|
|
// helper for checking LastEnvelopeHash
|
2019-11-23 17:57:05 +00:00
|
|
|
func isEmptyEnvelope(hash types.Hash) bool {
|
2019-10-22 14:55:18 +00:00
|
|
|
for _, b := range hash {
|
|
|
|
if b != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|