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-04-13 16:32:35 +00:00
|
|
|
import std/sugar
|
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-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-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-01-10 15:32:56 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc start*(s: CodexServer) {.async.} =
|
2022-01-10 15:32:56 +00:00
|
|
|
s.restServer.start()
|
2022-05-19 19:56:03 +00:00
|
|
|
await s.codexNode.start()
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
s.runHandle = newFuture[void]()
|
|
|
|
await s.runHandle
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc stop*(s: CodexServer) {.async.} =
|
2022-01-10 15:32:56 +00:00
|
|
|
await allFuturesThrowing(
|
2022-05-19 19:56:03 +00:00
|
|
|
s.restServer.stop(), s.codexNode.stop())
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
s.runHandle.complete()
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc new*(T: type CodexServer, config: CodexConf): T =
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
const SafePermissions = {UserRead, UserWrite}
|
2022-01-10 15:32:56 +00:00
|
|
|
let
|
2022-04-13 16:32:35 +00:00
|
|
|
privateKey =
|
|
|
|
if config.netPrivKeyFile == "random":
|
|
|
|
PrivateKey.random(Rng.instance()[]).get()
|
|
|
|
else:
|
|
|
|
let path =
|
|
|
|
if config.netPrivKeyFile.isAbsolute:
|
|
|
|
config.netPrivKeyFile
|
|
|
|
else:
|
|
|
|
config.dataDir / config.netPrivKeyFile
|
|
|
|
|
|
|
|
if path.fileAccessible({AccessFlags.Find}):
|
|
|
|
info "Found a network private key"
|
|
|
|
|
|
|
|
if path.getPermissionsSet().get() != SafePermissions:
|
|
|
|
warn "The network private key file is not safe, aborting"
|
|
|
|
quit QuitFailure
|
|
|
|
|
|
|
|
PrivateKey.init(path.readAllBytes().expect("accessible private key file")).
|
|
|
|
expect("valid private key file")
|
|
|
|
else:
|
|
|
|
info "Creating a private key and saving it"
|
|
|
|
let
|
|
|
|
res = PrivateKey.random(Rng.instance()[]).get()
|
|
|
|
bytes = res.getBytes().get()
|
|
|
|
|
|
|
|
path.writeFile(bytes, SafePermissions.toInt()).expect("writing private key file")
|
|
|
|
|
|
|
|
PrivateKey.init(bytes).expect("valid key bytes")
|
|
|
|
|
|
|
|
let
|
|
|
|
addresses =
|
|
|
|
config.listenPorts.mapIt(MultiAddress.init("/ip4/" & $config.listenIp & "/tcp/" & $(it.int)).tryGet()) &
|
|
|
|
@[MultiAddress.init("/ip4/" & $config.listenIp & "/udp/" & $(config.discoveryPort.int)).tryGet()]
|
2022-01-10 15:32:56 +00:00
|
|
|
switch = SwitchBuilder
|
|
|
|
.new()
|
2022-04-13 16:32:35 +00:00
|
|
|
.withPrivateKey(privateKey)
|
|
|
|
.withAddresses(addresses)
|
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-03-02 16:30:42 +00:00
|
|
|
let cache =
|
|
|
|
if config.cacheSize > 0:
|
|
|
|
CacheStore.new(cacheSize = config.cacheSize * MiB)
|
|
|
|
else:
|
|
|
|
CacheStore.new()
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
let
|
2022-04-13 16:32:35 +00:00
|
|
|
discoveryBootstrapNodes = config.bootstrapNodes
|
2022-05-19 02:29:15 +00:00
|
|
|
blockDiscovery = Discovery.new(
|
2022-04-13 16:32:35 +00:00
|
|
|
switch.peerInfo,
|
|
|
|
discoveryPort = config.discoveryPort,
|
|
|
|
bootstrapNodes = discoveryBootstrapNodes
|
|
|
|
)
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
wallet = WalletRef.new(EthPrivateKey.random())
|
|
|
|
network = BlockExcNetwork.new(switch)
|
2022-03-02 16:30:42 +00:00
|
|
|
localStore = FSStore.new(config.dataDir / "repo", cache = cache)
|
2022-05-19 02:29:15 +00:00
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
|
|
|
discovery = DiscoveryEngine.new(localStore, peerStore, network, blockDiscovery, pendingBlocks)
|
|
|
|
engine = BlockExcEngine.new(localStore, wallet, network, discovery, peerStore, pendingBlocks)
|
2022-01-10 15:32:56 +00:00
|
|
|
store = NetworkStore.new(engine, localStore)
|
2022-04-06 00:34:29 +00:00
|
|
|
erasure = Erasure.new(store, leoEncoderProvider, leoDecoderProvider)
|
2022-04-25 13:12:37 +00:00
|
|
|
contracts = ContractInteractions.new(
|
|
|
|
config.ethProvider,
|
|
|
|
config.ethDeployment,
|
|
|
|
config.ethAccount
|
|
|
|
)
|
2022-05-19 19:56:03 +00:00
|
|
|
codexNode = CodexNodeRef.new(switch, store, engine, erasure, blockDiscovery, contracts)
|
2022-01-10 15:32:56 +00:00
|
|
|
restServer = RestServerRef.new(
|
2022-05-19 19:56:03 +00:00
|
|
|
codexNode.initRestApi(config),
|
2022-01-10 15:32:56 +00:00
|
|
|
initTAddress("127.0.0.1" , config.apiPort),
|
|
|
|
bufferSize = (1024 * 64),
|
|
|
|
maxRequestBodySize = int.high)
|
|
|
|
.tryGet()
|
|
|
|
|
|
|
|
switch.mount(network)
|
|
|
|
T(
|
|
|
|
config: config,
|
2022-05-19 19:56:03 +00:00
|
|
|
codexNode: codexNode,
|
2022-04-13 16:32:35 +00:00
|
|
|
restServer: restServer,
|
|
|
|
)
|