2024-03-03 02:59:53 +02:00
import
std / [ options , sequtils ] ,
chronicles ,
chronos ,
libp2p / peerid ,
libp2p / protocols / pubsub / gossipsub ,
2024-10-28 09:17:46 +01:00
libp2p / protocols / connectivity / relay / relay ,
2024-03-03 02:59:53 +02:00
libp2p / nameresolving / dnsresolver ,
libp2p / crypto / crypto
import
. / internal_config ,
. / external_config ,
. / builder ,
. / validator_signed ,
.. / waku_enr / sharding ,
.. / waku_node ,
.. / waku_core ,
.. / waku_rln_relay ,
2024-04-17 21:48:20 +02:00
.. / discovery / waku_dnsdisc ,
2024-07-12 12:19:12 -04:00
.. / waku_archive / retention_policy as policy ,
.. / waku_archive / retention_policy / builder as policy_builder ,
.. / waku_archive / driver as driver ,
.. / waku_archive / driver / builder as driver_builder ,
.. / waku_archive_legacy / driver as legacy_driver ,
.. / waku_archive_legacy / driver / builder as legacy_driver_builder ,
2024-03-03 02:59:53 +02:00
.. / waku_store ,
2024-04-25 09:09:52 -04:00
.. / waku_store / common as store_common ,
.. / waku_store_legacy ,
.. / waku_store_legacy / common as legacy_common ,
2024-03-03 02:59:53 +02:00
.. / waku_filter_v2 ,
.. / waku_peer_exchange ,
.. / node / peer_manager ,
.. / node / peer_manager / peer_store / waku_peer_storage ,
.. / node / peer_manager / peer_store / migrations as peer_store_sqlite_migrations ,
.. / waku_lightpush / common ,
2024-04-15 15:28:35 +02:00
.. / common / utils / parse_size_units ,
2025-01-20 16:13:20 +01:00
.. / common / rate_limit / setting ,
.. / common / databases / dburl
2024-03-03 02:59:53 +02:00
## Peer persistence
const PeerPersistenceDbUrl = " peers.db "
2024-03-12 07:44:54 -06:00
proc setupPeerStorage ( ) : Result [ Option [ WakuPeerStorage ] , string ] =
2024-03-16 00:08:47 +01:00
let db = ? SqliteDatabase . new ( PeerPersistenceDbUrl )
2024-03-03 02:59:53 +02:00
2024-03-16 00:08:47 +01:00
? peer_store_sqlite_migrations . migrate ( db )
2024-03-03 02:59:53 +02:00
let res = WakuPeerStorage . new ( db )
if res . isErr ( ) :
return err ( " failed to init peer store " & res . error )
ok ( some ( res . value ) )
## Init waku node instance
2024-03-16 00:08:47 +01:00
proc initNode (
conf : WakuNodeConf ,
netConfig : NetConfig ,
rng : ref HmacDrbgContext ,
nodeKey : crypto . PrivateKey ,
record : enr . Record ,
peerStore : Option [ WakuPeerStorage ] ,
2024-10-28 09:17:46 +01:00
relay : Relay ,
2024-03-16 00:08:47 +01:00
dynamicBootstrapNodes : openArray [ RemotePeerInfo ] = @ [ ] ,
) : Result [ WakuNode , string ] =
2024-03-03 02:59:53 +02:00
## Setup a basic Waku v2 node based on a supplied configuration
## file. Optionally include persistent peer storage.
## No protocols are mounted yet.
var dnsResolver : DnsResolver
if conf . dnsAddrs :
# Support for DNS multiaddrs
var nameServers : seq [ TransportAddress ]
for ip in conf . dnsAddrsNameServers :
nameServers . add ( initTAddress ( ip , Port ( 53 ) ) ) # Assume all servers use port 53
dnsResolver = DnsResolver . new ( nameServers )
var node : WakuNode
2024-03-16 00:08:47 +01:00
let pStorage =
if peerStore . isNone ( ) :
nil
else :
peerStore . get ( )
2024-03-03 02:59:53 +02:00
# Build waku node instance
var builder = WakuNodeBuilder . init ( )
builder . withRng ( rng )
builder . withNodeKey ( nodekey )
builder . withRecord ( record )
builder . withNetworkConfiguration ( netConfig )
builder . withPeerStorage ( pStorage , capacity = conf . peerStoreCapacity )
builder . withSwitchConfiguration (
2024-03-16 00:08:47 +01:00
maxConnections = some ( conf . maxConnections . int ) ,
secureKey = some ( conf . websocketSecureKeyPath ) ,
secureCert = some ( conf . websocketSecureCertPath ) ,
nameResolver = dnsResolver ,
sendSignedPeerRecord = conf . relayPeerExchange ,
# We send our own signed peer record when peer exchange enabled
agentString = some ( conf . agentString ) ,
2024-03-03 02:59:53 +02:00
)
builder . withColocationLimit ( conf . colocationLimit )
2025-01-21 11:29:52 +05:30
if conf . maxRelayPeers . isSome ( ) :
let
maxRelayPeers = conf . maxRelayPeers . get ( )
maxConnections = conf . maxConnections
# Calculate the ratio as percentages
relayRatio = ( maxRelayPeers . float / maxConnections . float ) * 100
serviceRatio = 100 - relayRatio
builder . withPeerManagerConfig (
maxConnections = conf . maxConnections ,
relayServiceRatio = $ relayRatio & " : " & $ serviceRatio ,
shardAware = conf . relayShardedPeerManagement ,
)
error " maxRelayPeers is deprecated. It is recommended to use relayServiceRatio instead. If relayServiceRatio is not set, it will be automatically calculated based on maxConnections and maxRelayPeers. "
else :
builder . withPeerManagerConfig (
maxConnections = conf . maxConnections ,
relayServiceRatio = conf . relayServiceRatio ,
shardAware = conf . relayShardedPeerManagement ,
)
2024-09-18 15:58:07 +02:00
builder . withRateLimit ( conf . rateLimits )
2024-10-28 09:17:46 +01:00
builder . withCircuitRelay ( relay )
2024-03-03 02:59:53 +02:00
2024-03-16 00:08:47 +01:00
node =
? builder . build ( ) . mapErr (
proc ( err : string ) : string =
" failed to create waku node instance: " & err
)
2024-03-03 02:59:53 +02:00
ok ( node )
## Mount protocols
2024-09-10 15:07:12 -06:00
proc getNumShardsInNetwork * ( conf : WakuNodeConf ) : uint32 =
if conf . numShardsInNetwork ! = 0 :
return conf . numShardsInNetwork
# If conf.numShardsInNetwork is not set, use 1024 - the maximum possible as per the static sharding spec
# https://github.com/waku-org/specs/blob/master/standards/core/relay-sharding.md#static-sharding
return uint32 ( MaxShardIndex + 1 )
2024-12-13 17:38:16 +01:00
proc getAutoshards * (
node : WakuNode , contentTopics : seq [ string ]
) : Result [ seq [ RelayShard ] , string ] =
var autoShards : seq [ RelayShard ]
for contentTopic in contentTopics :
let shard = node . wakuSharding . getShard ( contentTopic ) . valueOr :
return err ( " Could not parse content topic: " & error )
autoShards . add ( shard )
return ok ( autoshards )
2024-03-16 00:08:47 +01:00
proc setupProtocols (
node : WakuNode , conf : WakuNodeConf , nodeKey : crypto . PrivateKey
) : Future [ Result [ void , string ] ] {. async . } =
2024-03-03 02:59:53 +02:00
## Setup configured protocols on an existing Waku v2 node.
## Optionally include persistent message storage.
## No protocols are started yet.
2024-08-01 23:28:00 +03:00
if conf . discv5Only :
notice " Running node only with Discv5, not mounting additional protocols "
return ok ( )
2024-03-03 02:59:53 +02:00
node . mountMetadata ( conf . clusterId ) . isOkOr :
return err ( " failed to mount waku metadata protocol: " & error )
2024-09-10 15:07:12 -06:00
# If conf.numShardsInNetwork is not set, use the number of shards configured as numShardsInNetwork
let numShardsInNetwork = getNumShardsInNetwork ( conf )
if conf . numShardsInNetwork = = 0 :
warn " Number of shards in network not configured, setting it to " ,
numShardsInNetwork = $ numShardsInNetwork
node . mountSharding ( conf . clusterId , numShardsInNetwork ) . isOkOr :
2024-03-13 10:58:13 +01:00
return err ( " failed to mount waku sharding: " & error )
2024-03-03 02:59:53 +02:00
# Mount relay on all nodes
var peerExchangeHandler = none ( RoutingRecordsHandler )
if conf . relayPeerExchange :
2024-03-16 00:08:47 +01:00
proc handlePeerExchange (
peer : PeerId , topic : string , peers : seq [ RoutingRecordsPair ]
) {. gcsafe . } =
2024-03-03 02:59:53 +02:00
## Handle peers received via gossipsub peer exchange
# TODO: Only consider peers on pubsub topics we subscribe to
2024-03-16 00:08:47 +01:00
let exchangedPeers = peers . filterIt ( it . record . isSome ( ) )
# only peers with populated records
. mapIt ( toRemotePeerInfo ( it . record . get ( ) ) )
2024-03-03 02:59:53 +02:00
2024-06-20 10:46:40 +02:00
debug " adding exchanged peers " ,
2024-03-16 00:08:47 +01:00
src = peer , topic = topic , numPeers = exchangedPeers . len
2024-03-03 02:59:53 +02:00
2024-06-20 10:46:40 +02:00
for peer in exchangedPeers :
# Peers added are filtered by the peer manager
node . peerManager . addPeer ( peer , PeerOrigin . PeerExchange )
2024-03-03 02:59:53 +02:00
peerExchangeHandler = some ( handlePeerExchange )
2024-12-13 17:38:16 +01:00
let autoShards = node . getAutoshards ( conf . contentTopics ) . valueOr :
return err ( " Could not get autoshards: " & error )
2024-09-10 15:07:12 -06:00
2024-06-21 17:47:44 +05:30
debug " Shards created from content topics " ,
2024-09-10 15:07:12 -06:00
contentTopics = conf . contentTopics , shards = autoShards
2024-06-21 17:47:44 +05:30
2024-09-10 15:07:12 -06:00
let confShards =
conf . shards . mapIt ( RelayShard ( clusterId : conf . clusterId , shardId : uint16 ( it ) ) )
let shards = confShards & autoShards
2024-03-03 02:59:53 +02:00
2024-09-10 15:07:12 -06:00
if conf . relay :
2024-03-03 02:59:53 +02:00
let parsedMaxMsgSize = parseMsgSize ( conf . maxMessageSize ) . valueOr :
return err ( " failed to parse ' max-num-bytes-msg-size ' param: " & $ error )
2024-03-16 00:08:47 +01:00
debug " Setting max message size " , num_bytes = parsedMaxMsgSize
2024-03-03 02:59:53 +02:00
try :
2024-03-16 00:08:47 +01:00
await mountRelay (
2024-09-10 15:07:12 -06:00
node , shards , peerExchangeHandler = peerExchangeHandler , int ( parsedMaxMsgSize )
2024-03-16 00:08:47 +01:00
)
2024-03-03 02:59:53 +02:00
except CatchableError :
return err ( " failed to mount waku relay protocol: " & getCurrentExceptionMsg ( ) )
# Add validation keys to protected topics
2024-08-19 12:56:22 +02:00
var subscribedProtectedShards : seq [ ProtectedShard ]
for shardKey in conf . protectedShards :
if shardKey . shard notin conf . shards :
warn " protected shard not in subscribed shards, skipping adding validator " ,
protectedShard = shardKey . shard , subscribedShards = shards
2024-03-03 02:59:53 +02:00
continue
2024-08-19 12:56:22 +02:00
subscribedProtectedShards . add ( shardKey )
2024-03-16 00:08:47 +01:00
notice " routing only signed traffic " ,
2024-08-19 12:56:22 +02:00
protectedShard = shardKey . shard , publicKey = shardKey . key
node . wakuRelay . addSignedShardsValidator ( subscribedProtectedShards , conf . clusterId )
2024-03-03 02:59:53 +02:00
2024-12-09 15:22:36 -05:00
# Only relay nodes should be rendezvous points.
if conf . rendezvous :
await node . mountRendezvous ( )
2024-03-03 02:59:53 +02:00
# Keepalive mounted on all nodes
try :
await mountLibp2pPing ( node )
except CatchableError :
return err ( " failed to mount libp2p ping protocol: " & getCurrentExceptionMsg ( ) )
var onFatalErrorAction = proc ( msg : string ) {. gcsafe , closure . } =
## Action to be taken when an internal error occurs during the node run.
## e.g. the connection with the database is lost and not recovered.
error " Unrecoverable error occurred " , error = msg
quit ( QuitFailure )
if conf . rlnRelay :
2024-06-20 15:05:21 +05:30
let rlnConf = WakuRlnConfig (
rlnRelayDynamic : conf . rlnRelayDynamic ,
rlnRelayCredIndex : conf . rlnRelayCredIndex ,
rlnRelayEthContractAddress : conf . rlnRelayEthContractAddress ,
2024-06-28 14:49:16 +05:30
rlnRelayChainId : conf . rlnRelayChainId ,
2024-06-20 15:05:21 +05:30
rlnRelayEthClientAddress : string ( conf . rlnRelayethClientAddress ) ,
rlnRelayCredPath : conf . rlnRelayCredPath ,
rlnRelayCredPassword : conf . rlnRelayCredPassword ,
rlnRelayTreePath : conf . rlnRelayTreePath ,
rlnRelayUserMessageLimit : conf . rlnRelayUserMessageLimit ,
rlnEpochSizeSec : conf . rlnEpochSizeSec ,
onFatalErrorAction : onFatalErrorAction ,
)
2024-03-03 02:59:53 +02:00
try :
waitFor node . mountRlnRelay ( rlnConf )
except CatchableError :
return err ( " failed to mount waku RLN relay protocol: " & getCurrentExceptionMsg ( ) )
if conf . store :
2024-07-12 12:19:12 -04:00
if conf . legacyStore :
let archiveDriverRes = waitFor legacy_driver . ArchiveDriver . new (
conf . storeMessageDbUrl , conf . storeMessageDbVacuum , conf . storeMessageDbMigration ,
conf . storeMaxNumDbConnections , onFatalErrorAction ,
)
if archiveDriverRes . isErr ( ) :
return err ( " failed to setup legacy archive driver: " & archiveDriverRes . error )
2024-07-30 14:05:23 +02:00
let mountArcRes = node . mountLegacyArchive ( archiveDriverRes . get ( ) )
2024-07-12 12:19:12 -04:00
if mountArcRes . isErr ( ) :
return err ( " failed to mount waku legacy archive protocol: " & mountArcRes . error )
## For now we always mount the future archive driver but if the legacy one is mounted,
## then the legacy will be in charge of performing the archiving.
## Regarding storage, the only diff between the current/future archive driver and the legacy
## one, is that the legacy stores an extra field: the id (message digest.)
2025-01-20 16:13:20 +01:00
## TODO: remove this "migrate" variable once legacy store is removed
## It is now necessary because sqlite's legacy store has an extra field: storedAt
## This breaks compatibility between store's and legacy store's schemas in sqlite
## So for now, we need to make sure that when legacy store is enabled and we use sqlite
## that we migrate our db according to legacy store's schema to have the extra field
let engineRes = dburl . getDbEngine ( conf . storeMessageDbUrl )
if engineRes . isErr ( ) :
return err ( " error getting db engine in setupProtocols: " & engineRes . error )
let engine = engineRes . get ( )
let migrate =
if engine = = " sqlite " and conf . legacyStore :
false
else :
conf . storeMessageDbMigration
2024-07-12 12:19:12 -04:00
let archiveDriverRes = waitFor driver . ArchiveDriver . new (
2025-01-20 16:13:20 +01:00
conf . storeMessageDbUrl , conf . storeMessageDbVacuum , migrate ,
2024-03-16 00:08:47 +01:00
conf . storeMaxNumDbConnections , onFatalErrorAction ,
)
2024-03-03 02:59:53 +02:00
if archiveDriverRes . isErr ( ) :
return err ( " failed to setup archive driver: " & archiveDriverRes . error )
2024-07-12 12:19:12 -04:00
let retPolicyRes = policy . RetentionPolicy . new ( conf . storeMessageRetentionPolicy )
2024-03-03 02:59:53 +02:00
if retPolicyRes . isErr ( ) :
return err ( " failed to create retention policy: " & retPolicyRes . error )
2024-03-16 00:08:47 +01:00
let mountArcRes = node . mountArchive ( archiveDriverRes . get ( ) , retPolicyRes . get ( ) )
2024-03-03 02:59:53 +02:00
if mountArcRes . isErr ( ) :
return err ( " failed to mount waku archive protocol: " & mountArcRes . error )
2024-07-12 12:19:12 -04:00
if conf . legacyStore :
# Store legacy setup
try :
2024-09-18 15:58:07 +02:00
await mountLegacyStore ( node , node . rateLimitSettings . getSetting ( STOREV2 ) )
2024-07-12 12:19:12 -04:00
except CatchableError :
return
err ( " failed to mount waku legacy store protocol: " & getCurrentExceptionMsg ( ) )
# Store setup
2024-03-03 02:59:53 +02:00
try :
2024-09-18 15:58:07 +02:00
await mountStore ( node , node . rateLimitSettings . getSetting ( STOREV3 ) )
2024-03-03 02:59:53 +02:00
except CatchableError :
return err ( " failed to mount waku store protocol: " & getCurrentExceptionMsg ( ) )
mountStoreClient ( node )
if conf . storenode ! = " " :
let storeNode = parsePeerInfo ( conf . storenode )
if storeNode . isOk ( ) :
2024-04-25 09:09:52 -04:00
node . peerManager . addServicePeer ( storeNode . value , store_common . WakuStoreCodec )
2024-03-03 02:59:53 +02:00
else :
return err ( " failed to set node waku store peer: " & storeNode . error )
2024-04-25 09:09:52 -04:00
mountLegacyStoreClient ( node )
if conf . storenode ! = " " :
let storeNode = parsePeerInfo ( conf . storenode )
if storeNode . isOk ( ) :
2024-05-09 20:07:49 +02:00
node . peerManager . addServicePeer (
storeNode . value , legacy_common . WakuLegacyStoreCodec
)
2024-04-25 09:09:52 -04:00
else :
return err ( " failed to set node waku legacy store peer: " & storeNode . error )
2024-07-30 07:23:39 -04:00
if conf . store and conf . storeResume :
node . setupStoreResume ( )
2024-03-03 02:59:53 +02:00
# NOTE Must be mounted after relay
if conf . lightpush :
try :
2024-09-18 15:58:07 +02:00
await mountLightPush ( node , node . rateLimitSettings . getSetting ( LIGHTPUSH ) )
2024-03-03 02:59:53 +02:00
except CatchableError :
return err ( " failed to mount waku lightpush protocol: " & getCurrentExceptionMsg ( ) )
2024-11-29 15:31:08 +01:00
mountLightPushClient ( node )
2024-03-03 02:59:53 +02:00
if conf . lightpushnode ! = " " :
let lightPushNode = parsePeerInfo ( conf . lightpushnode )
if lightPushNode . isOk ( ) :
node . peerManager . addServicePeer ( lightPushNode . value , WakuLightPushCodec )
else :
return err ( " failed to set node waku lightpush peer: " & lightPushNode . error )
# Filter setup. NOTE Must be mounted after relay
if conf . filter :
try :
2024-03-16 00:08:47 +01:00
await mountFilter (
node ,
subscriptionTimeout = chronos . seconds ( conf . filterSubscriptionTimeout ) ,
maxFilterPeers = conf . filterMaxPeersToServe ,
maxFilterCriteriaPerPeer = conf . filterMaxCriteria ,
2024-09-18 15:58:07 +02:00
rateLimitSetting = node . rateLimitSettings . getSetting ( FILTER ) ,
2024-03-16 00:08:47 +01:00
)
2024-03-03 02:59:53 +02:00
except CatchableError :
return err ( " failed to mount waku filter protocol: " & getCurrentExceptionMsg ( ) )
2024-11-29 15:31:08 +01:00
await node . mountFilterClient ( )
2024-03-03 02:59:53 +02:00
if conf . filternode ! = " " :
let filterNode = parsePeerInfo ( conf . filternode )
if filterNode . isOk ( ) :
try :
node . peerManager . addServicePeer ( filterNode . value , WakuFilterSubscribeCodec )
except CatchableError :
2024-03-16 00:08:47 +01:00
return err (
" failed to mount waku filter client protocol: " & getCurrentExceptionMsg ( )
)
2024-03-03 02:59:53 +02:00
else :
return err ( " failed to set node waku filter peer: " & filterNode . error )
# waku peer exchange setup
2024-08-06 13:27:25 +05:30
if conf . peerExchange :
2024-03-03 02:59:53 +02:00
try :
2024-09-18 15:58:07 +02:00
await mountPeerExchange (
node , some ( conf . clusterId ) , node . rateLimitSettings . getSetting ( PEEREXCHG )
)
2024-03-03 02:59:53 +02:00
except CatchableError :
2024-03-16 00:08:47 +01:00
return
err ( " failed to mount waku peer-exchange protocol: " & getCurrentExceptionMsg ( ) )
2024-03-03 02:59:53 +02:00
2024-08-06 13:27:25 +05:30
if conf . peerExchangeNode ! = " " :
let peerExchangeNode = parsePeerInfo ( conf . peerExchangeNode )
if peerExchangeNode . isOk ( ) :
node . peerManager . addServicePeer ( peerExchangeNode . value , WakuPeerExchangeCodec )
else :
return
err ( " failed to set node waku peer-exchange peer: " & peerExchangeNode . error )
2024-03-03 02:59:53 +02:00
return ok ( )
## Start node
2024-03-16 00:08:47 +01:00
proc startNode * (
node : WakuNode , conf : WakuNodeConf , dynamicBootstrapNodes : seq [ RemotePeerInfo ] = @ [ ]
2024-05-03 14:07:15 +02:00
) : Future [ Result [ void , string ] ] {. async : ( raises : [ ] ) . } =
2024-03-03 02:59:53 +02:00
## Start a configured node and all mounted protocols.
## Connect to static nodes and start
## keep-alive, if configured.
# Start Waku v2 node
try :
await node . start ( )
except CatchableError :
return err ( " failed to start waku node: " & getCurrentExceptionMsg ( ) )
# Connect to configured static nodes
if conf . staticnodes . len > 0 :
try :
await connectToNodes ( node , conf . staticnodes , " static " )
except CatchableError :
return err ( " failed to connect to static nodes: " & getCurrentExceptionMsg ( ) )
if dynamicBootstrapNodes . len > 0 :
info " Connecting to dynamic bootstrap peers "
try :
await connectToNodes ( node , dynamicBootstrapNodes , " dynamic bootstrap " )
except CatchableError :
2024-03-16 00:08:47 +01:00
return
err ( " failed to connect to dynamic bootstrap nodes: " & getCurrentExceptionMsg ( ) )
2024-03-03 02:59:53 +02:00
# retrieve px peers and add the to the peer store
if conf . peerExchangeNode ! = " " :
2024-10-16 17:04:27 +03:00
var desiredOutDegree = DefaultPXNumPeersReq
if not node . wakuRelay . isNil ( ) and node . wakuRelay . parameters . d . uint64 ( ) > 0 :
desiredOutDegree = node . wakuRelay . parameters . d . uint64 ( )
2024-03-14 17:48:09 +01:00
( await node . fetchPeerExchangePeers ( desiredOutDegree ) ) . isOkOr :
2024-03-16 00:08:47 +01:00
error " error while fetching peers from peer exchange " , error = error
2024-10-30 12:51:04 +02:00
# Use px to periodically get peers if discv5 is disabled, as discv5 nodes have their own
# periodic loop to find peers and px returned peers actually come from discv5
if conf . peerExchange and not conf . discv5Discovery :
node . startPeerExchangeLoop ( )
2024-03-03 02:59:53 +02:00
# Start keepalive, if enabled
if conf . keepAlive :
node . startKeepalive ( )
# Maintain relay connections
if conf . relay :
node . peerManager . start ( )
2024-03-08 16:46:42 -06:00
return ok ( )
2024-03-16 00:08:47 +01:00
proc setupNode * (
2024-10-28 09:17:46 +01:00
conf : WakuNodeConf , rng : ref HmacDrbgContext = crypto . newRng ( ) , relay : Relay
2024-03-16 00:08:47 +01:00
) : Result [ WakuNode , string ] =
# Use provided key only if corresponding rng is also provided
let key =
2024-10-28 09:17:46 +01:00
if conf . nodeKey . isSome ( ) :
2024-03-16 00:08:47 +01:00
conf . nodeKey . get ( )
else :
2024-10-28 09:17:46 +01:00
warn " missing key, generating new "
crypto . PrivateKey . random ( Secp256k1 , rng [ ] ) . valueOr :
2024-03-16 00:08:47 +01:00
error " Failed to generate key " , error = error
return err ( " Failed to generate key: " & $ error )
let netConfig = networkConfiguration ( conf , clientId ) . valueOr :
error " failed to create internal config " , error = error
return err ( " failed to create internal config: " & error )
let record = enrConfiguration ( conf , netConfig , key ) . valueOr :
error " failed to create record " , error = error
return err ( " failed to create record: " & error )
if isClusterMismatched ( record , conf . clusterId ) :
error " cluster id mismatch configured shards "
return err ( " cluster id mismatch configured shards " )
debug " Setting up storage "
## Peer persistence
var peerStore : Option [ WakuPeerStorage ]
if conf . peerPersistence :
peerStore = setupPeerStorage ( ) . valueOr :
error " Setting up storage failed " , error = " failed to setup peer store " & error
return err ( " Setting up storage failed: " & error )
debug " Initializing node "
2024-10-28 09:17:46 +01:00
let node = initNode ( conf , netConfig , rng , key , record , peerStore , relay ) . valueOr :
2024-03-16 00:08:47 +01:00
error " Initializing node failed " , error = error
return err ( " Initializing node failed: " & error )
debug " Mounting protocols "
try :
( waitFor node . setupProtocols ( conf , key ) ) . isOkOr :
error " Mounting protocols failed " , error = error
return err ( " Mounting protocols failed: " & error )
except CatchableError :
return err ( " Exception setting up protocols: " & getCurrentExceptionMsg ( ) )
2024-03-08 16:46:42 -06:00
2024-03-16 00:08:47 +01:00
return ok ( node )