2019-11-27 12:22:23 +00:00
|
|
|
// +build !nimbus
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
package node
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-02 14:35:37 +00:00
|
|
|
"encoding/json"
|
2016-09-11 11:44:14 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-12-07 21:07:08 +00:00
|
|
|
"os"
|
2017-03-28 09:04:52 +00:00
|
|
|
"path/filepath"
|
2018-06-13 11:24:04 +00:00
|
|
|
"time"
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2016-12-07 21:07:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/les"
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2017-03-10 00:16:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
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/crypto"
|
2020-02-18 11:21:01 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2018-05-11 19:43:07 +00:00
|
|
|
"github.com/status-im/status-go/mailserver"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/services/ext"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/services/nodebridge"
|
2018-07-16 07:40:40 +00:00
|
|
|
"github.com/status-im/status-go/services/peer"
|
2018-04-10 10:02:54 +00:00
|
|
|
"github.com/status-im/status-go/services/personal"
|
|
|
|
"github.com/status-im/status-go/services/shhext"
|
2018-05-03 10:36:56 +00:00
|
|
|
"github.com/status-im/status-go/services/status"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/services/wakuext"
|
2018-09-13 16:31:29 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
2018-05-08 10:38:54 +00:00
|
|
|
"github.com/status-im/status-go/timesource"
|
2020-01-08 11:12:23 +00:00
|
|
|
"github.com/status-im/status-go/waku"
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
wakucommon "github.com/status-im/status-go/waku/common"
|
2019-12-09 10:36:14 +00:00
|
|
|
"github.com/status-im/status-go/whisper/v6"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// Errors related to node and services creation.
|
2016-12-07 21:07:08 +00:00
|
|
|
var (
|
2018-09-13 16:31:29 +00:00
|
|
|
ErrNodeMakeFailureFormat = "error creating p2p node: %s"
|
|
|
|
ErrWhisperServiceRegistrationFailure = errors.New("failed to register the Whisper service")
|
2020-01-08 11:12:23 +00:00
|
|
|
ErrWakuServiceRegistrationFailure = errors.New("failed to register the Waku service")
|
2018-09-13 16:31:29 +00:00
|
|
|
ErrLightEthRegistrationFailure = errors.New("failed to register the LES service")
|
|
|
|
ErrLightEthRegistrationFailureUpstreamEnabled = errors.New("failed to register the LES service, upstream is also configured")
|
|
|
|
ErrPersonalServiceRegistrationFailure = errors.New("failed to register the personal api service")
|
|
|
|
ErrStatusServiceRegistrationFailure = errors.New("failed to register the Status service")
|
|
|
|
ErrPeerServiceRegistrationFailure = errors.New("failed to register the Peer service")
|
2016-12-07 21:07:08 +00:00
|
|
|
)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
// All general log messages in this package should be routed through this logger.
|
2018-06-08 11:29:50 +00:00
|
|
|
var logger = log.New("package", "status-go/node")
|
2018-03-20 18:35:28 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
// MakeNode creates a geth node entity
|
2019-08-20 15:38:40 +00:00
|
|
|
func MakeNode(config *params.NodeConfig, accs *accounts.Manager, db *leveldb.DB) (*node.Node, error) {
|
2018-04-16 12:36:09 +00:00
|
|
|
// If DataDir is empty, it means we want to create an ephemeral node
|
|
|
|
// keeping data only in memory.
|
|
|
|
if config.DataDir != "" {
|
|
|
|
// make sure data directory exists
|
|
|
|
if err := os.MkdirAll(filepath.Clean(config.DataDir), os.ModePerm); err != nil {
|
|
|
|
return nil, fmt.Errorf("make node: make data directory: %v", err)
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// make sure keys directory exists
|
|
|
|
if err := os.MkdirAll(filepath.Clean(config.KeyStoreDir), os.ModePerm); err != nil {
|
|
|
|
return nil, fmt.Errorf("make node: make keys directory: %v", err)
|
|
|
|
}
|
2017-04-28 08:49:15 +00:00
|
|
|
}
|
2017-03-28 09:04:52 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
stackConfig, err := newGethNodeConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 00:39:20 +00:00
|
|
|
stack, err := node.New(stackConfig)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2018-07-21 15:32:53 +00:00
|
|
|
return nil, fmt.Errorf(ErrNodeMakeFailureFormat, err.Error())
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
err = activateServices(stack, config, accs, db)
|
2019-07-21 05:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stack, nil
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func activateServices(stack *node.Node, config *params.NodeConfig, accs *accounts.Manager, db *leveldb.DB) error {
|
2020-02-18 11:21:01 +00:00
|
|
|
if config.EnableNTPSync {
|
|
|
|
err := stack.Register(func(*node.ServiceContext) (node.Service, error) {
|
|
|
|
return timesource.Default(), nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to register NTP time source: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// start Ethereum service if we are not expected to use an upstream server
|
2017-08-15 10:27:12 +00:00
|
|
|
if !config.UpstreamConfig.Enabled {
|
2019-08-20 15:38:40 +00:00
|
|
|
if err := activateLightEthService(stack, accs, config); err != nil {
|
2019-07-21 05:41:30 +00:00
|
|
|
return fmt.Errorf("%v: %v", ErrLightEthRegistrationFailure, err)
|
2017-08-15 10:27:12 +00:00
|
|
|
}
|
2018-04-10 10:02:54 +00:00
|
|
|
} else {
|
2018-09-13 16:31:29 +00:00
|
|
|
if config.LightEthConfig.Enabled {
|
2019-07-21 05:41:30 +00:00
|
|
|
return ErrLightEthRegistrationFailureUpstreamEnabled
|
2018-09-13 16:31:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 12:45:43 +00:00
|
|
|
logger.Info("LES protocol is disabled")
|
|
|
|
|
2018-04-18 11:04:02 +00:00
|
|
|
// `personal_sign` and `personal_ecRecover` methods are important to
|
2018-04-10 10:02:54 +00:00
|
|
|
// keep DApps working.
|
|
|
|
// Usually, they are provided by an ETH or a LES service, but when using
|
|
|
|
// upstream, we don't start any of these, so we need to start our own
|
|
|
|
// implementation.
|
2019-08-20 15:38:40 +00:00
|
|
|
if err := activatePersonalService(stack, accs, config); err != nil {
|
2019-07-21 05:41:30 +00:00
|
|
|
return fmt.Errorf("%v: %v", ErrPersonalServiceRegistrationFailure, err)
|
2018-04-10 10:02:54 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
if err := activateNodeServices(stack, config, db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func activateNodeServices(stack *node.Node, config *params.NodeConfig, db *leveldb.DB) error {
|
2020-01-08 11:12:23 +00:00
|
|
|
// Register eth-node node bridge
|
|
|
|
err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
return &nodebridge.NodeService{Node: gethbridge.NewNodeBridge(stack)}, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-11-27 12:22:23 +00:00
|
|
|
return fmt.Errorf("failed to register NodeBridge: %v", err)
|
2020-01-08 11:12:23 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// start Whisper service.
|
2018-05-02 12:14:08 +00:00
|
|
|
if err := activateShhService(stack, config, db); err != nil {
|
2019-07-21 05:41:30 +00:00
|
|
|
return fmt.Errorf("%v: %v", ErrWhisperServiceRegistrationFailure, err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
// start Waku service
|
|
|
|
if err := activateWakuService(stack, config, db); err != nil {
|
|
|
|
return fmt.Errorf("%v: %v", ErrWakuServiceRegistrationFailure, err)
|
|
|
|
}
|
|
|
|
|
2018-05-03 10:36:56 +00:00
|
|
|
// start status service.
|
|
|
|
if err := activateStatusService(stack, config); err != nil {
|
2019-07-21 05:41:30 +00:00
|
|
|
return fmt.Errorf("%v: %v", ErrStatusServiceRegistrationFailure, err)
|
2018-05-03 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
// start peer service
|
|
|
|
if err := activatePeerService(stack); err != nil {
|
2019-07-21 05:41:30 +00:00
|
|
|
return fmt.Errorf("%v: %v", ErrPeerServiceRegistrationFailure, err)
|
2018-07-16 07:40:40 +00:00
|
|
|
}
|
2019-07-21 05:41:30 +00:00
|
|
|
return nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
// newGethNodeConfig returns default stack configuration for mobile client node
|
|
|
|
func newGethNodeConfig(config *params.NodeConfig) (*node.Config, error) {
|
2017-07-13 06:54:10 +00:00
|
|
|
nc := &node.Config{
|
2017-05-16 03:24:56 +00:00
|
|
|
DataDir: config.DataDir,
|
|
|
|
KeyStoreDir: config.KeyStoreDir,
|
|
|
|
UseLightweightKDF: true,
|
2017-05-16 12:09:52 +00:00
|
|
|
NoUSB: true,
|
2017-05-16 03:24:56 +00:00
|
|
|
Name: config.Name,
|
|
|
|
Version: config.Version,
|
|
|
|
P2P: p2p.Config{
|
2018-05-10 11:45:51 +00:00
|
|
|
NoDiscovery: true, // we always use only v5 server
|
2018-04-10 06:44:09 +00:00
|
|
|
ListenAddr: config.ListenAddr,
|
|
|
|
NAT: nat.Any(),
|
|
|
|
MaxPeers: config.MaxPeers,
|
|
|
|
MaxPendingPeers: config.MaxPendingPeers,
|
2017-05-16 03:24:56 +00:00
|
|
|
},
|
2018-10-12 12:58:32 +00:00
|
|
|
HTTPModules: config.FormatAPIModules(),
|
2017-05-16 03:24:56 +00:00
|
|
|
}
|
2017-07-13 06:54:10 +00:00
|
|
|
|
2018-10-11 10:29:59 +00:00
|
|
|
if config.IPCEnabled {
|
|
|
|
// use well-known defaults
|
|
|
|
if config.IPCFile == "" {
|
|
|
|
config.IPCFile = "geth.ipc"
|
|
|
|
}
|
|
|
|
|
|
|
|
nc.IPCPath = config.IPCFile
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:09:31 +00:00
|
|
|
if config.HTTPEnabled {
|
2017-07-13 06:54:10 +00:00
|
|
|
nc.HTTPHost = config.HTTPHost
|
|
|
|
nc.HTTPPort = config.HTTPPort
|
2018-10-12 12:58:32 +00:00
|
|
|
nc.HTTPVirtualHosts = config.HTTPVirtualHosts
|
|
|
|
nc.HTTPCors = config.HTTPCors
|
2017-07-13 06:54:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
if config.ClusterConfig.Enabled {
|
2018-04-10 06:44:09 +00:00
|
|
|
nc.P2P.BootstrapNodesV5 = parseNodesV5(config.ClusterConfig.BootNodes)
|
2018-04-20 12:23:18 +00:00
|
|
|
nc.P2P.StaticNodes = parseNodes(config.ClusterConfig.StaticNodes)
|
2017-12-07 12:07:45 +00:00
|
|
|
}
|
2018-09-13 16:31:29 +00:00
|
|
|
|
|
|
|
if config.NodeKey != "" {
|
|
|
|
sk, err := crypto.HexToECDSA(config.NodeKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// override node's private key
|
|
|
|
nc.P2P.PrivateKey = sk
|
|
|
|
}
|
|
|
|
|
|
|
|
return nc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculateGenesis retrieves genesis value for given network
|
|
|
|
func calculateGenesis(networkID uint64) (*core.Genesis, error) {
|
|
|
|
var genesis *core.Genesis
|
|
|
|
|
|
|
|
switch networkID {
|
|
|
|
case params.MainNetworkID:
|
|
|
|
genesis = core.DefaultGenesisBlock()
|
|
|
|
case params.RopstenNetworkID:
|
|
|
|
genesis = core.DefaultTestnetGenesisBlock()
|
|
|
|
case params.RinkebyNetworkID:
|
|
|
|
genesis = core.DefaultRinkebyGenesisBlock()
|
2019-03-14 08:43:32 +00:00
|
|
|
case params.GoerliNetworkID:
|
|
|
|
genesis = core.DefaultGoerliGenesisBlock()
|
2018-09-13 16:31:29 +00:00
|
|
|
case params.StatusChainNetworkID:
|
|
|
|
var err error
|
|
|
|
if genesis, err = defaultStatusChainGenesisBlock(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return genesis, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultStatusChainGenesisBlock returns the StatusChain network genesis block.
|
|
|
|
func defaultStatusChainGenesisBlock() (*core.Genesis, error) {
|
|
|
|
genesisJSON, err := static.ConfigStatusChainGenesisJsonBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("status-chain-genesis.json could not be loaded: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var genesis *core.Genesis
|
|
|
|
err = json.Unmarshal(genesisJSON, &genesis)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot unmarshal status-chain-genesis.json: %s", err)
|
|
|
|
}
|
|
|
|
return genesis, nil
|
2017-05-16 03:24:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// activateLightEthService configures and registers the eth.Ethereum service with a given node.
|
2019-08-20 15:38:40 +00:00
|
|
|
func activateLightEthService(stack *node.Node, accs *accounts.Manager, config *params.NodeConfig) error {
|
2018-09-13 16:31:29 +00:00
|
|
|
if !config.LightEthConfig.Enabled {
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Info("LES protocol is disabled")
|
2017-03-28 09:04:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
genesis, err := calculateGenesis(config.NetworkID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
ethConf := eth.DefaultConfig
|
|
|
|
ethConf.Genesis = genesis
|
|
|
|
ethConf.SyncMode = downloader.LightSync
|
2017-05-03 14:24:48 +00:00
|
|
|
ethConf.NetworkId = config.NetworkID
|
2017-05-02 14:35:37 +00:00
|
|
|
ethConf.DatabaseCache = config.LightEthConfig.DatabaseCache
|
2018-04-16 12:36:09 +00:00
|
|
|
return stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
// NOTE(dshulyak) here we set our instance of the accounts manager.
|
|
|
|
// without sharing same instance selected account won't be visible for personal_* methods.
|
|
|
|
nctx := &node.ServiceContext{}
|
|
|
|
*nctx = *ctx
|
|
|
|
nctx.AccountManager = accs
|
|
|
|
return les.New(nctx, ðConf)
|
2018-04-16 12:36:09 +00:00
|
|
|
})
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func activatePersonalService(stack *node.Node, accs *accounts.Manager, config *params.NodeConfig) error {
|
2018-04-10 10:02:54 +00:00
|
|
|
return stack.Register(func(*node.ServiceContext) (node.Service, error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
svc := personal.New(accs)
|
2018-04-10 10:02:54 +00:00
|
|
|
return svc, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-03 10:36:56 +00:00
|
|
|
func activateStatusService(stack *node.Node, config *params.NodeConfig) error {
|
2019-01-17 12:56:22 +00:00
|
|
|
if !config.EnableStatusService {
|
2018-05-03 10:36:56 +00:00
|
|
|
logger.Info("Status service api is disabled")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
2019-11-23 17:57:05 +00:00
|
|
|
var service *nodebridge.WhisperService
|
2019-10-28 13:50:33 +00:00
|
|
|
if err := ctx.Service(&service); err != nil {
|
2018-05-03 10:36:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-28 13:50:33 +00:00
|
|
|
svc := status.New(service.Whisper)
|
2018-05-03 10:36:56 +00:00
|
|
|
return svc, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
func activatePeerService(stack *node.Node) error {
|
|
|
|
return stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
svc := peer.New()
|
|
|
|
return svc, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
func registerWhisperMailServer(whisperService *whisper.Whisper, config *params.WhisperConfig) (err error) {
|
|
|
|
var mailServer mailserver.WhisperMailServer
|
|
|
|
whisperService.RegisterMailServer(&mailServer)
|
2018-07-04 09:30:57 +00:00
|
|
|
|
|
|
|
return mailServer.Init(whisperService, config)
|
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
func registerWakuMailServer(wakuService *waku.Waku, config *params.WakuConfig) (err error) {
|
|
|
|
var mailServer mailserver.WakuMailServer
|
|
|
|
wakuService.RegisterMailServer(&mailServer)
|
|
|
|
|
|
|
|
return mailServer.Init(wakuService, config)
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// activateShhService configures Whisper and adds it to the given node.
|
2018-05-02 12:14:08 +00:00
|
|
|
func activateShhService(stack *node.Node, config *params.NodeConfig, db *leveldb.DB) (err error) {
|
2018-09-13 16:31:29 +00:00
|
|
|
if !config.WhisperConfig.Enabled {
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Info("SHH protocol is disabled")
|
2017-03-28 09:04:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-04-12 16:17:10 +00:00
|
|
|
|
2018-05-08 10:38:54 +00:00
|
|
|
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
2019-10-28 13:50:33 +00:00
|
|
|
return createShhService(ctx, &config.WhisperConfig, &config.ClusterConfig)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-10-19 09:09:13 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
// Register Whisper eth-node bridge
|
|
|
|
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
var ethnode *nodebridge.NodeService
|
|
|
|
if err := ctx.Service(ðnode); err != nil {
|
2019-10-28 13:50:33 +00:00
|
|
|
return nil, err
|
2018-10-19 09:09:13 +00:00
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
w, err := ethnode.Node.GetWhisper(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &nodebridge.WhisperService{Whisper: w}, nil
|
2019-10-28 13:50:33 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(dshulyak) add a config option to enable it by default, but disable if app is started from statusd
|
|
|
|
return stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
2019-11-23 17:57:05 +00:00
|
|
|
var ethnode *nodebridge.NodeService
|
|
|
|
if err := ctx.Service(ðnode); err != nil {
|
2019-10-28 13:50:33 +00:00
|
|
|
return nil, err
|
2018-02-19 14:53:40 +00:00
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
return shhext.New(config.ShhextConfig, ethnode.Node, ctx, ext.EnvelopeSignalHandler{}, db), nil
|
2019-10-28 13:50:33 +00:00
|
|
|
})
|
|
|
|
}
|
2018-06-13 11:24:04 +00:00
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
// activateWakuService configures Waku and adds it to the given node.
|
|
|
|
func activateWakuService(stack *node.Node, config *params.NodeConfig, db *leveldb.DB) (err error) {
|
|
|
|
if !config.WakuConfig.Enabled {
|
|
|
|
logger.Info("Waku protocol is disabled")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
return createWakuService(ctx, &config.WakuConfig, &config.ClusterConfig)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-17 14:38:59 +00:00
|
|
|
// Register Whisper eth-node bridge
|
|
|
|
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
var ethnode *nodebridge.NodeService
|
|
|
|
if err := ctx.Service(ðnode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
w, err := ethnode.Node.GetWaku(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &nodebridge.WakuService{Waku: w}, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
// TODO(dshulyak) add a config option to enable it by default, but disable if app is started from statusd
|
|
|
|
return stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
var ethnode *nodebridge.NodeService
|
|
|
|
if err := ctx.Service(ðnode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
return wakuext.New(config.ShhextConfig, ethnode.Node, ctx, ext.EnvelopeSignalHandler{}, db), nil
|
2020-01-13 19:17:30 +00:00
|
|
|
})
|
2020-01-08 11:12:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 13:50:33 +00:00
|
|
|
func createShhService(ctx *node.ServiceContext, whisperConfig *params.WhisperConfig, clusterConfig *params.ClusterConfig) (*whisper.Whisper, error) {
|
|
|
|
whisperServiceConfig := &whisper.Config{
|
|
|
|
MaxMessageSize: whisper.DefaultMaxMessageSize,
|
|
|
|
MinimumAcceptedPOW: params.WhisperMinimumPoW,
|
|
|
|
}
|
2018-09-25 07:05:38 +00:00
|
|
|
|
2019-10-28 13:50:33 +00:00
|
|
|
if whisperConfig.MaxMessageSize > 0 {
|
|
|
|
whisperServiceConfig.MaxMessageSize = whisperConfig.MaxMessageSize
|
|
|
|
}
|
|
|
|
if whisperConfig.MinimumPoW > 0 {
|
|
|
|
whisperServiceConfig.MinimumAcceptedPOW = whisperConfig.MinimumPoW
|
|
|
|
}
|
2019-11-18 10:22:23 +00:00
|
|
|
|
2019-10-28 13:50:33 +00:00
|
|
|
whisperService := whisper.New(whisperServiceConfig)
|
2018-06-13 11:24:04 +00:00
|
|
|
|
2019-10-28 13:50:33 +00:00
|
|
|
if whisperConfig.EnableRateLimiter {
|
|
|
|
r := whisperRateLimiter(whisperConfig, clusterConfig)
|
|
|
|
whisperService.SetRateLimiter(r)
|
|
|
|
}
|
2017-04-12 18:26:54 +00:00
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
if timesource, err := timeSource(ctx); err == nil {
|
2019-10-28 13:50:33 +00:00
|
|
|
whisperService.SetTimeSource(timesource)
|
|
|
|
}
|
2018-03-02 09:25:30 +00:00
|
|
|
|
2019-10-28 13:50:33 +00:00
|
|
|
// enable mail service
|
|
|
|
if whisperConfig.EnableMailServer {
|
2020-01-08 11:12:23 +00:00
|
|
|
if err := registerWhisperMailServer(whisperService, whisperConfig); err != nil {
|
2019-10-28 13:50:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to register MailServer: %v", err)
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2018-04-12 16:17:10 +00:00
|
|
|
|
2019-10-28 13:50:33 +00:00
|
|
|
if whisperConfig.LightClient {
|
|
|
|
emptyBloomFilter := make([]byte, 64)
|
|
|
|
if err := whisperService.SetBloomFilter(emptyBloomFilter); err != nil {
|
2018-04-12 16:17:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-28 13:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return whisperService, nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
func createWakuService(ctx *node.ServiceContext, wakuCfg *params.WakuConfig, clusterCfg *params.ClusterConfig) (*waku.Waku, error) {
|
|
|
|
cfg := &waku.Config{
|
2020-09-09 08:10:01 +00:00
|
|
|
MaxMessageSize: wakucommon.DefaultMaxMessageSize,
|
|
|
|
BloomFilterMode: wakuCfg.BloomFilterMode,
|
|
|
|
FullNode: wakuCfg.FullNode,
|
|
|
|
SoftBlacklistedPeerIDs: wakuCfg.SoftBlacklistedPeerIDs,
|
|
|
|
MinimumAcceptedPoW: params.WakuMinimumPoW,
|
2020-01-08 11:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if wakuCfg.MaxMessageSize > 0 {
|
|
|
|
cfg.MaxMessageSize = wakuCfg.MaxMessageSize
|
|
|
|
}
|
|
|
|
if wakuCfg.MinimumPoW > 0 {
|
|
|
|
cfg.MinimumAcceptedPoW = wakuCfg.MinimumPoW
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:21:01 +00:00
|
|
|
w := waku.New(cfg, logutils.ZapLogger())
|
2020-01-08 11:12:23 +00:00
|
|
|
|
|
|
|
if wakuCfg.EnableRateLimiter {
|
|
|
|
r := wakuRateLimiter(wakuCfg, clusterCfg)
|
|
|
|
w.RegisterRateLimiter(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if timesource, err := timeSource(ctx); err == nil {
|
|
|
|
w.SetTimeSource(timesource)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable mail service
|
|
|
|
if wakuCfg.EnableMailServer {
|
|
|
|
if err := registerWakuMailServer(w, wakuCfg); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to register WakuMailServer: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wakuCfg.LightClient {
|
|
|
|
emptyBloomFilter := make([]byte, 64)
|
|
|
|
if err := w.SetBloomFilter(emptyBloomFilter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// parseNodes creates list of enode.Node out of enode strings.
|
|
|
|
func parseNodes(enodes []string) []*enode.Node {
|
|
|
|
var nodes []*enode.Node
|
|
|
|
for _, item := range enodes {
|
|
|
|
parsedPeer, err := enode.ParseV4(item)
|
2018-06-04 13:47:13 +00:00
|
|
|
if err == nil {
|
|
|
|
nodes = append(nodes, parsedPeer)
|
|
|
|
} else {
|
2018-11-14 07:03:58 +00:00
|
|
|
logger.Error("Failed to parse enode", "enode", item, "err", err)
|
2018-06-04 13:47:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 09:44:38 +00:00
|
|
|
}
|
2018-03-20 14:05:21 +00:00
|
|
|
return nodes
|
2017-02-20 09:44:38 +00:00
|
|
|
}
|
2018-04-10 06:44:09 +00:00
|
|
|
|
|
|
|
// parseNodesV5 creates list of discv5.Node out of enode strings.
|
|
|
|
func parseNodesV5(enodes []string) []*discv5.Node {
|
2018-06-04 13:47:13 +00:00
|
|
|
var nodes []*discv5.Node
|
|
|
|
for _, enode := range enodes {
|
|
|
|
parsedPeer, err := discv5.ParseNode(enode)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
nodes = append(nodes, parsedPeer)
|
|
|
|
} else {
|
|
|
|
logger.Error("Failed to parse enode", "enode", enode, "err", err)
|
|
|
|
}
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
2018-06-13 11:24:04 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func parseNodesToNodeID(enodes []string) []enode.ID {
|
|
|
|
nodeIDs := make([]enode.ID, 0, len(enodes))
|
2018-07-25 14:48:02 +00:00
|
|
|
for _, node := range parseNodes(enodes) {
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeIDs = append(nodeIDs, node.ID())
|
2018-07-25 14:48:02 +00:00
|
|
|
}
|
|
|
|
return nodeIDs
|
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
// timeSource get timeSource to be used by whisper
|
|
|
|
func timeSource(ctx *node.ServiceContext) (func() time.Time, error) {
|
2018-06-13 11:24:04 +00:00
|
|
|
var timeSource *timesource.NTPTimeSource
|
|
|
|
if err := ctx.Service(&timeSource); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return timeSource.Now, nil
|
|
|
|
}
|
2019-11-18 10:22:23 +00:00
|
|
|
|
2019-10-28 13:50:33 +00:00
|
|
|
func whisperRateLimiter(whisperConfig *params.WhisperConfig, clusterConfig *params.ClusterConfig) *whisper.PeerRateLimiter {
|
2019-11-18 10:22:23 +00:00
|
|
|
enodes := append(
|
2019-10-28 13:50:33 +00:00
|
|
|
parseNodes(clusterConfig.StaticNodes),
|
|
|
|
parseNodes(clusterConfig.TrustedMailServers)...,
|
2019-11-18 10:22:23 +00:00
|
|
|
)
|
|
|
|
var (
|
|
|
|
ips []string
|
|
|
|
peerIDs []enode.ID
|
|
|
|
)
|
|
|
|
for _, item := range enodes {
|
|
|
|
ips = append(ips, item.IP().String())
|
|
|
|
peerIDs = append(peerIDs, item.ID())
|
|
|
|
}
|
|
|
|
return whisper.NewPeerRateLimiter(
|
|
|
|
&whisper.PeerRateLimiterConfig{
|
2019-10-28 13:50:33 +00:00
|
|
|
LimitPerSecIP: whisperConfig.RateLimitIP,
|
|
|
|
LimitPerSecPeerID: whisperConfig.RateLimitPeerID,
|
2019-11-18 10:22:23 +00:00
|
|
|
WhitelistedIPs: ips,
|
|
|
|
WhitelistedPeerIDs: peerIDs,
|
|
|
|
},
|
2019-12-16 19:38:50 +00:00
|
|
|
&whisper.MetricsRateLimiterHandler{},
|
|
|
|
&whisper.DropPeerRateLimiterHandler{
|
|
|
|
Tolerance: whisperConfig.RateLimitTolerance,
|
|
|
|
},
|
2019-11-18 10:22:23 +00:00
|
|
|
)
|
|
|
|
}
|
2020-01-08 11:12:23 +00:00
|
|
|
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
func wakuRateLimiter(wakuCfg *params.WakuConfig, clusterCfg *params.ClusterConfig) *wakucommon.PeerRateLimiter {
|
2020-01-08 11:12:23 +00:00
|
|
|
enodes := append(
|
|
|
|
parseNodes(clusterCfg.StaticNodes),
|
|
|
|
parseNodes(clusterCfg.TrustedMailServers)...,
|
|
|
|
)
|
|
|
|
var (
|
|
|
|
ips []string
|
|
|
|
peerIDs []enode.ID
|
|
|
|
)
|
|
|
|
for _, item := range enodes {
|
|
|
|
ips = append(ips, item.IP().String())
|
|
|
|
peerIDs = append(peerIDs, item.ID())
|
|
|
|
}
|
Move networking code for waku under `v0` namespace
Why make the change?
As discussed previously, the way we will move across versions is to maintain completely separate
codebases and eventually remove those that are not supported anymore.
This has the drawback of some code duplication, but the advantage is that is more
explicit what each version requires, and changes in one version will not
impact the other, so we won't pile up backward compatible code.
This is the same strategy used by `whisper` in go ethereum and is influenced by
https://www.youtube.com/watch?v=oyLBGkS5ICk .
All the code that is used for the networking protocol is now under `v0/`.
Some of the common parts might still be refactored out.
The main namespace `waku` deals with `host`->`waku` interactions (through RPC),
while `v0` deals with `waku`->`remote-waku` interactions.
In order to support `v1`, the namespace `v0` will be copied over, and changed to
support `v1`. Once `v0` will be not used anymore, the whole namespace will be removed.
This PR does not actually implement `v1`, I'd rather get things looked over to
make sure the structure is what we would like before implementing the changes.
What has changed?
- Moved all code for the common parts under `waku/common/` namespace
- Moved code used for bloomfilters in `waku/common/bloomfilter.go`
- Removed all version specific code from `waku/common/const` (`ProtocolVersion`, status-codes etc)
- Added interfaces for `WakuHost` and `Peer` under `waku/common/protocol.go`
Things still to do
Some tests in `waku/` are still testing by stubbing components of a particular version (`v0`).
I started moving those tests to instead of stubbing using the actual component, which increases
the testing surface. Some other tests that can't be easily ported should be likely moved under
`v0` instead. Ideally no version specif code should be exported from a version namespace (for
example the various codes, as those might change across versions). But this will be a work-in-progress.
Some code that will be common in `v0`/`v1` could still be extract to avoid duplication, and duplicated only
when implementations diverge across versions.
2020-04-21 12:40:30 +00:00
|
|
|
return wakucommon.NewPeerRateLimiter(
|
|
|
|
&wakucommon.PeerRateLimiterConfig{
|
2020-06-08 09:43:56 +00:00
|
|
|
PacketLimitPerSecIP: wakuCfg.PacketRateLimitIP,
|
|
|
|
PacketLimitPerSecPeerID: wakuCfg.PacketRateLimitPeerID,
|
2020-06-08 11:15:19 +00:00
|
|
|
BytesLimitPerSecIP: wakuCfg.BytesRateLimitIP,
|
|
|
|
BytesLimitPerSecPeerID: wakuCfg.BytesRateLimitPeerID,
|
2020-06-08 09:43:56 +00:00
|
|
|
WhitelistedIPs: ips,
|
|
|
|
WhitelistedPeerIDs: peerIDs,
|
2020-01-08 11:12:23 +00:00
|
|
|
},
|
|
|
|
&whisper.MetricsRateLimiterHandler{},
|
|
|
|
&whisper.DropPeerRateLimiterHandler{
|
|
|
|
Tolerance: wakuCfg.RateLimitTolerance,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|