feat(waku2)_: allow using an env variable to set the node's key (#5152)

This commit is contained in:
richΛrd 2024-05-15 19:15:22 -04:00 committed by GitHub
parent 9e0fb30f8d
commit 82ae41f6d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 47 additions and 25 deletions

View File

@ -54,7 +54,7 @@ func (s *MessengerRawMessageResendTest) SetupTest() {
UseShardAsDefaultTopic: true, UseShardAsDefaultTopic: true,
DefaultShardPubsubTopic: shard.DefaultShardPubsubTopic(), DefaultShardPubsubTopic: shard.DefaultShardPubsubTopic(),
} }
s.exchangeBootNode, err = wakuv2.New("", "", exchangeNodeConfig, logger.Named("pxServerNode"), nil, nil, nil, nil) s.exchangeBootNode, err = wakuv2.New(nil, "", exchangeNodeConfig, logger.Named("pxServerNode"), nil, nil, nil, nil)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NoError(s.exchangeBootNode.Start()) s.Require().NoError(s.exchangeBootNode.Start())

View File

@ -1,10 +1,12 @@
package node package node
import ( import (
"crypto/ecdsa"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"os"
"reflect" "reflect"
"time" "time"
@ -13,6 +15,7 @@ import (
"github.com/status-im/status-go/signal" "github.com/status-im/status-go/signal"
"github.com/status-im/status-go/transactions" "github.com/status-im/status-go/transactions"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
gethrpc "github.com/ethereum/go-ethereum/rpc" gethrpc "github.com/ethereum/go-ethereum/rpc"
@ -20,6 +23,7 @@ import (
"github.com/status-im/status-go/appmetrics" "github.com/status-im/status-go/appmetrics"
"github.com/status-im/status-go/common" "github.com/status-im/status-go/common"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth" gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/logutils" "github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/mailserver" "github.com/status-im/status-go/mailserver"
@ -347,7 +351,29 @@ func (b *StatusNode) wakuV2Service(nodeConfig *params.NodeConfig, telemetryServe
cfg.MaxMessageSize = nodeConfig.WakuV2Config.MaxMessageSize cfg.MaxMessageSize = nodeConfig.WakuV2Config.MaxMessageSize
} }
w, err := wakuv2.New(nodeConfig.NodeKey, nodeConfig.ClusterConfig.Fleet, cfg, logutils.ZapLogger(), b.appDB, b.timeSource(), signal.SendHistoricMessagesRequestFailed, signal.SendPeerStats) var nodeKey *ecdsa.PrivateKey
var err error
if nodeConfig.NodeKey != "" {
nodeKey, err = crypto.HexToECDSA(nodeConfig.NodeKey)
if err != nil {
return nil, fmt.Errorf("could not convert nodekey into a valid private key: %v", err)
}
} else {
nodeKeyStr := os.Getenv("WAKUV2_NODE_KEY")
if nodeKeyStr != "" {
nodeKeyBytes, err := hexutil.Decode(nodeKeyStr)
if err != nil {
return nil, fmt.Errorf("failed to decode the go-waku private key: %v", err)
}
nodeKey, err = crypto.ToECDSA(nodeKeyBytes)
if err != nil {
return nil, fmt.Errorf("could not convert nodekey into a valid private key: %v", err)
}
}
}
w, err := wakuv2.New(nodeKey, nodeConfig.ClusterConfig.Fleet, cfg, logutils.ZapLogger(), b.appDB, b.timeSource(), signal.SendHistoricMessagesRequestFailed, signal.SendPeerStats)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -44,7 +44,7 @@ func NewTestWakuV2(s *suite.Suite, cfg testWakuV2Config) *waku2.Waku {
} }
wakuNode, err := waku2.New( wakuNode, err := waku2.New(
"", nil,
"", "",
wakuConfig, wakuConfig,
cfg.logger, cfg.logger,

View File

@ -30,7 +30,7 @@ import (
) )
func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) { func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
w, err := New("", "", nil, nil, nil, nil, nil, nil) w, err := New(nil, "", nil, nil, nil, nil, nil, nil)
if err != nil { if err != nil {
t.Fatalf("Error creating WakuV2 client: %v", err) t.Fatalf("Error creating WakuV2 client: %v", err)
} }

View File

@ -166,7 +166,7 @@ func newTTLCache() *ttlcache.Cache[gethcommon.Hash, *common.ReceivedMessage] {
} }
// New creates a WakuV2 client ready to communicate through the LibP2P network. // New creates a WakuV2 client ready to communicate through the LibP2P network.
func New(nodeKey string, fleet string, cfg *Config, logger *zap.Logger, appDB *sql.DB, ts *timesource.NTPTimeSource, onHistoricMessagesRequestFailed func([]byte, peer.ID, error), onPeerStats func(types.ConnStatus)) (*Waku, error) { func New(nodeKey *ecdsa.PrivateKey, fleet string, cfg *Config, logger *zap.Logger, appDB *sql.DB, ts *timesource.NTPTimeSource, onHistoricMessagesRequestFailed func([]byte, peer.ID, error), onPeerStats func(types.ConnStatus)) (*Waku, error) {
var err error var err error
if logger == nil { if logger == nil {
logger, err = zap.NewDevelopment() logger, err = zap.NewDevelopment()
@ -216,16 +216,12 @@ func New(nodeKey string, fleet string, cfg *Config, logger *zap.Logger, appDB *s
waku.filters = common.NewFilters(waku.cfg.DefaultShardPubsubTopic, waku.logger) waku.filters = common.NewFilters(waku.cfg.DefaultShardPubsubTopic, waku.logger)
waku.bandwidthCounter = metrics.NewBandwidthCounter() waku.bandwidthCounter = metrics.NewBandwidthCounter()
var privateKey *ecdsa.PrivateKey if nodeKey == nil {
if nodeKey != "" { // No nodekey is provided, create an ephemeral key
privateKey, err = crypto.HexToECDSA(nodeKey) nodeKey, err = crypto.GenerateKey()
} else { if err != nil {
// If no nodekey is provided, create an ephemeral key return nil, fmt.Errorf("failed to generate a random go-waku private key: %v", err)
privateKey, err = crypto.GenerateKey() }
}
if err != nil {
return nil, fmt.Errorf("failed to setup the go-waku private key: %v", err)
} }
hostAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprint(cfg.Host, ":", cfg.Port)) hostAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprint(cfg.Host, ":", cfg.Port))
@ -243,7 +239,7 @@ func New(nodeKey string, fleet string, cfg *Config, logger *zap.Logger, appDB *s
opts := []node.WakuNodeOption{ opts := []node.WakuNodeOption{
node.WithLibP2POptions(libp2pOpts...), node.WithLibP2POptions(libp2pOpts...),
node.WithPrivateKey(privateKey), node.WithPrivateKey(nodeKey),
node.WithHostAddress(hostAddr), node.WithHostAddress(hostAddr),
node.WithTopicHealthStatusChannel(waku.topicHealthStatusChan), node.WithTopicHealthStatusChannel(waku.topicHealthStatusChan),
node.WithKeepAlive(time.Duration(cfg.KeepAliveInterval) * time.Second), node.WithKeepAlive(time.Duration(cfg.KeepAliveInterval) * time.Second),

View File

@ -42,7 +42,7 @@ func TestDiscoveryV5(t *testing.T) {
config.EnableDiscV5 = true config.EnableDiscV5 = true
config.DiscV5BootstrapNodes = []string{testENRBootstrap} config.DiscV5BootstrapNodes = []string{testENRBootstrap}
config.DiscoveryLimit = 20 config.DiscoveryLimit = 20
w, err := New("", "", config, nil, nil, nil, nil, nil) w, err := New(nil, "", config, nil, nil, nil, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, w.Start()) require.NoError(t, w.Start())
@ -67,7 +67,7 @@ func TestRestartDiscoveryV5(t *testing.T) {
config.DiscV5BootstrapNodes = []string{"enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@1.1.1.2"} config.DiscV5BootstrapNodes = []string{"enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@1.1.1.2"}
config.DiscoveryLimit = 20 config.DiscoveryLimit = 20
config.UDPPort = 9002 config.UDPPort = 9002
w, err := New("", "", config, nil, nil, nil, nil, nil) w, err := New(nil, "", config, nil, nil, nil, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, w.Start()) require.NoError(t, w.Start())
@ -120,7 +120,7 @@ func TestBasicWakuV2(t *testing.T) {
config.DiscV5BootstrapNodes = []string{enrTreeAddress} config.DiscV5BootstrapNodes = []string{enrTreeAddress}
config.DiscoveryLimit = 20 config.DiscoveryLimit = 20
config.WakuNodes = []string{enrTreeAddress} config.WakuNodes = []string{enrTreeAddress}
w, err := New("", "", config, nil, nil, nil, nil, nil) w, err := New(nil, "", config, nil, nil, nil, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, w.Start()) require.NoError(t, w.Start())
@ -229,7 +229,7 @@ func TestPeerExchange(t *testing.T) {
config.EnableDiscV5 = true config.EnableDiscV5 = true
config.EnablePeerExchangeServer = true config.EnablePeerExchangeServer = true
config.EnablePeerExchangeClient = false config.EnablePeerExchangeClient = false
pxServerNode, err := New("", "", config, logger.Named("pxServerNode"), nil, nil, nil, nil) pxServerNode, err := New(nil, "", config, logger.Named("pxServerNode"), nil, nil, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, pxServerNode.Start()) require.NoError(t, pxServerNode.Start())
@ -241,7 +241,7 @@ func TestPeerExchange(t *testing.T) {
config.EnablePeerExchangeServer = false config.EnablePeerExchangeServer = false
config.EnablePeerExchangeClient = false config.EnablePeerExchangeClient = false
config.DiscV5BootstrapNodes = []string{pxServerNode.node.ENR().String()} config.DiscV5BootstrapNodes = []string{pxServerNode.node.ENR().String()}
discV5Node, err := New("", "", config, logger.Named("discV5Node"), nil, nil, nil, nil) discV5Node, err := New(nil, "", config, logger.Named("discV5Node"), nil, nil, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, discV5Node.Start()) require.NoError(t, discV5Node.Start())
@ -259,7 +259,7 @@ func TestPeerExchange(t *testing.T) {
config.Resolver = resolver config.Resolver = resolver
config.WakuNodes = []string{url} config.WakuNodes = []string{url}
lightNode, err := New("", "", config, logger.Named("lightNode"), nil, nil, nil, nil) lightNode, err := New(nil, "", config, logger.Named("lightNode"), nil, nil, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, lightNode.Start()) require.NoError(t, lightNode.Start())
@ -301,7 +301,7 @@ func TestWakuV2Filter(t *testing.T) {
config.DiscoveryLimit = 20 config.DiscoveryLimit = 20
config.WakuNodes = []string{enrTreeAddress} config.WakuNodes = []string{enrTreeAddress}
fleet := "status.test" // Need a name fleet so that LightClient is not set to false fleet := "status.test" // Need a name fleet so that LightClient is not set to false
w, err := New("", fleet, config, nil, nil, nil, nil, nil) w, err := New(nil, fleet, config, nil, nil, nil, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, w.Start()) require.NoError(t, w.Start())
@ -390,7 +390,7 @@ func TestWakuV2Store(t *testing.T) {
w1PeersCh := make(chan []string, 100) // buffered not to block on the send side w1PeersCh := make(chan []string, 100) // buffered not to block on the send side
// Start the first Waku node // Start the first Waku node
w1, err := New("", "", config1, nil, nil, nil, nil, func(cs types.ConnStatus) { w1, err := New(nil, "", config1, nil, nil, nil, nil, func(cs types.ConnStatus) {
w1PeersCh <- maps.Keys(cs.Peers) w1PeersCh <- maps.Keys(cs.Peers)
}) })
require.NoError(t, err) require.NoError(t, err)
@ -414,7 +414,7 @@ func TestWakuV2Store(t *testing.T) {
} }
// Start the second Waku node // Start the second Waku node
w2, err := New("", "", config2, nil, sql2, nil, nil, nil) w2, err := New(nil, "", config2, nil, sql2, nil, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, w2.Start()) require.NoError(t, w2.Start())
w2EnvelopeCh := make(chan common.EnvelopeEvent, 100) w2EnvelopeCh := make(chan common.EnvelopeEvent, 100)