2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2021-08-30 19:25:20 +00:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/os
|
2022-12-03 00:00:55 +00:00
|
|
|
import std/tables
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/presto
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/confutils
|
|
|
|
import pkg/confutils/defs
|
|
|
|
import pkg/nitro
|
|
|
|
import pkg/stew/io2
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/stew/shims/net as stewnet
|
2022-12-03 00:00:55 +00:00
|
|
|
import pkg/datastore
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import ./node
|
|
|
|
import ./conf
|
|
|
|
import ./rng
|
|
|
|
import ./rest/api
|
2022-03-02 16:30:42 +00:00
|
|
|
import ./stores
|
2022-01-10 15:32:56 +00:00
|
|
|
import ./blockexchange
|
|
|
|
import ./utils/fileutils
|
2022-04-06 00:34:29 +00:00
|
|
|
import ./erasure
|
2022-04-13 16:32:35 +00:00
|
|
|
import ./discovery
|
2022-04-13 12:15:22 +00:00
|
|
|
import ./contracts
|
2022-11-02 00:58:41 +00:00
|
|
|
import ./utils/addrutils
|
2022-12-03 00:00:55 +00:00
|
|
|
import ./namespaces
|
2022-11-02 00:58:41 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "codex node"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
type
|
2022-05-19 19:56:03 +00:00
|
|
|
CodexServer* = ref object
|
2022-01-10 15:32:56 +00:00
|
|
|
runHandle: Future[void]
|
2022-05-19 19:56:03 +00:00
|
|
|
config: CodexConf
|
2022-01-10 15:32:56 +00:00
|
|
|
restServer: RestServerRef
|
2022-05-19 19:56:03 +00:00
|
|
|
codexNode: CodexNodeRef
|
2022-12-03 00:00:55 +00:00
|
|
|
repoStore: RepoStore
|
2023-03-08 15:04:54 +00:00
|
|
|
maintenance: BlockMaintainer
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
CodexPrivateKey* = libp2p.PrivateKey # alias
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc start*(s: CodexServer) {.async.} =
|
2022-12-03 00:00:55 +00:00
|
|
|
notice "Starting codex node"
|
|
|
|
|
|
|
|
await s.repoStore.start()
|
2022-01-10 15:32:56 +00:00
|
|
|
s.restServer.start()
|
2022-05-19 19:56:03 +00:00
|
|
|
await s.codexNode.start()
|
2023-03-08 15:04:54 +00:00
|
|
|
s.maintenance.start()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
let
|
2022-12-03 00:00:55 +00:00
|
|
|
# TODO: Can't define these as constants, pity
|
2022-11-02 00:58:41 +00:00
|
|
|
natIpPart = MultiAddress.init("/ip4/" & $s.config.nat & "/")
|
|
|
|
.expect("Should create multiaddress")
|
|
|
|
anyAddrIp = MultiAddress.init("/ip4/0.0.0.0/")
|
|
|
|
.expect("Should create multiaddress")
|
|
|
|
loopBackAddrIp = MultiAddress.init("/ip4/127.0.0.1/")
|
|
|
|
.expect("Should create multiaddress")
|
|
|
|
|
|
|
|
# announce addresses should be set to bound addresses,
|
|
|
|
# but the IP should be mapped to the provided nat ip
|
|
|
|
announceAddrs = s.codexNode.switch.peerInfo.addrs.mapIt:
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
listenIPPart = it[multiCodec("ip4")].expect("Should get IP")
|
|
|
|
|
|
|
|
if listenIPPart == anyAddrIp or
|
|
|
|
(listenIPPart == loopBackAddrIp and natIpPart != loopBackAddrIp):
|
|
|
|
it.remapAddr(s.config.nat.some)
|
|
|
|
else:
|
|
|
|
it
|
|
|
|
|
|
|
|
s.codexNode.discovery.updateAnnounceRecord(announceAddrs)
|
|
|
|
s.codexNode.discovery.updateDhtRecord(s.config.nat, s.config.discoveryPort)
|
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
s.runHandle = newFuture[void]("codex.runHandle")
|
2022-01-10 15:32:56 +00:00
|
|
|
await s.runHandle
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc stop*(s: CodexServer) {.async.} =
|
2022-12-03 00:00:55 +00:00
|
|
|
notice "Stopping codex node"
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
await allFuturesThrowing(
|
2022-12-03 00:00:55 +00:00
|
|
|
s.restServer.stop(),
|
|
|
|
s.codexNode.stop(),
|
2023-03-08 15:04:54 +00:00
|
|
|
s.repoStore.stop(),
|
|
|
|
s.maintenance.stop())
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
s.runHandle.complete()
|
|
|
|
|
2022-08-09 04:29:06 +00:00
|
|
|
proc new(_: type ContractInteractions, config: CodexConf): ?ContractInteractions =
|
|
|
|
if not config.persistence:
|
|
|
|
if config.ethAccount.isSome:
|
|
|
|
warn "Ethereum account was set, but persistence is not enabled"
|
|
|
|
return
|
|
|
|
|
|
|
|
without account =? config.ethAccount:
|
|
|
|
error "Persistence enabled, but no Ethereum account was set"
|
|
|
|
quit QuitFailure
|
|
|
|
|
|
|
|
if deployment =? config.ethDeployment:
|
|
|
|
ContractInteractions.new(config.ethProvider, account, deployment)
|
|
|
|
else:
|
|
|
|
ContractInteractions.new(config.ethProvider, account)
|
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
proc new*(T: type CodexServer, config: CodexConf, privateKey: CodexPrivateKey): T =
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
switch = SwitchBuilder
|
|
|
|
.new()
|
2022-04-13 16:32:35 +00:00
|
|
|
.withPrivateKey(privateKey)
|
2022-10-27 13:44:56 +00:00
|
|
|
.withAddresses(config.listenAddrs)
|
2022-01-10 15:32:56 +00:00
|
|
|
.withRng(Rng.instance())
|
|
|
|
.withNoise()
|
|
|
|
.withMplex(5.minutes, 5.minutes)
|
|
|
|
.withMaxConnections(config.maxPeers)
|
|
|
|
.withAgentVersion(config.agentString)
|
2022-04-13 16:32:35 +00:00
|
|
|
.withSignedPeerRecord(true)
|
2022-01-10 15:32:56 +00:00
|
|
|
.withTcpTransport({ServerFlags.ReuseAddr})
|
|
|
|
.build()
|
|
|
|
|
2022-08-04 23:51:05 +00:00
|
|
|
var
|
2022-11-15 15:46:21 +00:00
|
|
|
cache: CacheStore = nil
|
2022-08-04 23:51:05 +00:00
|
|
|
|
|
|
|
if config.cacheSize > 0:
|
|
|
|
cache = CacheStore.new(cacheSize = config.cacheSize * MiB)
|
2023-03-08 15:04:54 +00:00
|
|
|
## Is unused?
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
let
|
2022-12-03 00:00:55 +00:00
|
|
|
discoveryDir = config.dataDir / CodexDhtNamespace
|
|
|
|
|
|
|
|
if io2.createPath(discoveryDir).isErr:
|
|
|
|
trace "Unable to create discovery directory for block store", discoveryDir = discoveryDir
|
|
|
|
raise (ref Defect)(
|
|
|
|
msg: "Unable to create discovery directory for block store: " & discoveryDir)
|
|
|
|
|
|
|
|
let
|
|
|
|
discoveryStore = Datastore(
|
|
|
|
SQLiteDatastore.new(config.dataDir / CodexDhtProvidersNamespace)
|
|
|
|
.expect("Should create discovery datastore!"))
|
2022-10-27 13:44:56 +00:00
|
|
|
|
2022-11-02 00:58:41 +00:00
|
|
|
discovery = Discovery.new(
|
2022-10-27 13:44:56 +00:00
|
|
|
switch.peerInfo.privateKey,
|
2022-11-02 00:58:41 +00:00
|
|
|
announceAddrs = config.listenAddrs,
|
|
|
|
bindIp = config.discoveryIp,
|
|
|
|
bindPort = config.discoveryPort,
|
2022-10-27 13:44:56 +00:00
|
|
|
bootstrapNodes = config.bootstrapNodes,
|
|
|
|
store = discoveryStore)
|
2022-04-13 16:32:35 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
wallet = WalletRef.new(EthPrivateKey.random())
|
|
|
|
network = BlockExcNetwork.new(switch)
|
2022-08-08 21:42:05 +00:00
|
|
|
|
2023-03-14 22:32:15 +00:00
|
|
|
repoData = case config.repoKind
|
|
|
|
of repoFS: Datastore(FSDatastore.new($config.dataDir, depth = 5)
|
|
|
|
.expect("Should create repo file data store!"))
|
|
|
|
of repoSQLite: Datastore(SQLiteDatastore.new($config.dataDir)
|
|
|
|
.expect("Should create repo SQLite data store!"))
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
repoStore = RepoStore.new(
|
2023-03-14 22:32:15 +00:00
|
|
|
repoDs = repoData,
|
2022-12-03 00:00:55 +00:00
|
|
|
metaDs = SQLiteDatastore.new(config.dataDir / CodexMetaNamespace)
|
|
|
|
.expect("Should create meta data store!"),
|
|
|
|
quotaMaxBytes = config.storageQuota.uint,
|
2023-03-08 15:04:54 +00:00
|
|
|
blockTtl = config.blockTtlSeconds.seconds)
|
|
|
|
|
|
|
|
maintenance = BlockMaintainer.new(
|
|
|
|
repoStore,
|
|
|
|
interval = config.blockMaintenanceIntervalSeconds.seconds,
|
|
|
|
numberOfBlocksPerInterval = config.blockMaintenanceNumberOfBlocks)
|
2022-08-08 21:42:05 +00:00
|
|
|
|
2022-05-19 02:29:15 +00:00
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
2022-12-03 00:00:55 +00:00
|
|
|
blockDiscovery = DiscoveryEngine.new(repoStore, peerStore, network, discovery, pendingBlocks)
|
|
|
|
engine = BlockExcEngine.new(repoStore, wallet, network, blockDiscovery, peerStore, pendingBlocks)
|
|
|
|
store = NetworkStore.new(engine, repoStore)
|
2022-04-06 00:34:29 +00:00
|
|
|
erasure = Erasure.new(store, leoEncoderProvider, leoDecoderProvider)
|
2022-08-09 04:29:06 +00:00
|
|
|
contracts = ContractInteractions.new(config)
|
2022-11-02 00:58:41 +00:00
|
|
|
codexNode = CodexNodeRef.new(switch, store, engine, erasure, discovery, contracts)
|
2022-01-10 15:32:56 +00:00
|
|
|
restServer = RestServerRef.new(
|
2022-05-19 19:56:03 +00:00
|
|
|
codexNode.initRestApi(config),
|
2023-03-08 11:45:55 +00:00
|
|
|
initTAddress(config.apiBindAddress , config.apiPort),
|
2022-01-10 15:32:56 +00:00
|
|
|
bufferSize = (1024 * 64),
|
|
|
|
maxRequestBodySize = int.high)
|
2022-11-02 00:58:41 +00:00
|
|
|
.expect("Should start rest server!")
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
switch.mount(network)
|
|
|
|
T(
|
|
|
|
config: config,
|
2022-05-19 19:56:03 +00:00
|
|
|
codexNode: codexNode,
|
2022-12-03 00:00:55 +00:00
|
|
|
restServer: restServer,
|
2023-03-08 15:04:54 +00:00
|
|
|
repoStore: repoStore,
|
|
|
|
maintenance: maintenance)
|