2023-05-09 08:16:43 +00:00
|
|
|
# nimbus_signing_node
|
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-11-30 01:20:21 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
2023-05-09 08:16:43 +00:00
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
import std/[tables, os, strutils]
|
|
|
|
import serialization, json_serialization,
|
|
|
|
json_serialization/std/[options, net],
|
|
|
|
chronos, presto, presto/secureserver, chronicles, confutils,
|
2023-04-06 13:16:21 +00:00
|
|
|
stew/[base10, results, byteutils, io2, bitops2]
|
2022-01-18 13:36:52 +00:00
|
|
|
import "."/spec/datatypes/[base, altair, phase0],
|
2021-11-30 01:20:21 +00:00
|
|
|
"."/spec/[crypto, digest, network, signatures, forks],
|
|
|
|
"."/spec/eth2_apis/[rest_types, eth2_rest_serialization],
|
|
|
|
"."/rpc/rest_constants,
|
|
|
|
"."/[conf, version, nimbus_binary_common],
|
|
|
|
"."/validators/[keystore_management, validator_pool]
|
|
|
|
|
|
|
|
const
|
|
|
|
NimbusSigningNodeIdent = "nimbus_remote_signer/" & fullVersionStr
|
|
|
|
|
|
|
|
type
|
|
|
|
SigningNodeKind* {.pure.} = enum
|
|
|
|
NonSecure, Secure
|
|
|
|
|
|
|
|
SigningNodeServer* = object
|
|
|
|
case kind: SigningNodeKind
|
|
|
|
of SigningNodeKind.Secure:
|
|
|
|
sserver: SecureRestServerRef
|
|
|
|
of SigningNodeKind.NonSecure:
|
|
|
|
nserver: RestServerRef
|
|
|
|
|
|
|
|
SigningNode* = object
|
|
|
|
config: SigningNodeConf
|
|
|
|
attachedValidators: ValidatorPool
|
|
|
|
signingServer: SigningNodeServer
|
2023-02-16 17:25:48 +00:00
|
|
|
keystoreCache: KeystoreCacheRef
|
2021-11-30 01:20:21 +00:00
|
|
|
keysList: string
|
2023-04-06 13:16:21 +00:00
|
|
|
runKeystoreCachePruningLoopFut: Future[void]
|
|
|
|
sigintHandleFut: Future[void]
|
|
|
|
sigtermHandleFut: Future[void]
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
SigningNodeRef* = ref SigningNode
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
SigningNodeError* = object of CatchableError
|
|
|
|
|
2023-07-15 16:30:52 +00:00
|
|
|
func validate(key: string, value: string): int =
|
2023-04-06 13:16:21 +00:00
|
|
|
case key
|
|
|
|
of "{validator_key}":
|
|
|
|
0
|
|
|
|
else:
|
|
|
|
1
|
|
|
|
|
|
|
|
proc getRouter*(): RestRouter =
|
|
|
|
RestRouter.init(validate)
|
|
|
|
|
|
|
|
proc router(sn: SigningNodeRef): RestRouter =
|
2021-11-30 01:20:21 +00:00
|
|
|
case sn.signingServer.kind
|
|
|
|
of SigningNodeKind.Secure:
|
|
|
|
sn.signingServer.sserver.router
|
|
|
|
of SigningNodeKind.NonSecure:
|
|
|
|
sn.signingServer.nserver.router
|
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
proc start(sn: SigningNodeRef) =
|
2021-11-30 01:20:21 +00:00
|
|
|
case sn.signingServer.kind
|
|
|
|
of SigningNodeKind.Secure:
|
|
|
|
sn.signingServer.sserver.start()
|
|
|
|
of SigningNodeKind.NonSecure:
|
|
|
|
sn.signingServer.nserver.start()
|
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
proc stop(sn: SigningNodeRef) {.async.} =
|
2021-11-30 01:20:21 +00:00
|
|
|
case sn.signingServer.kind
|
|
|
|
of SigningNodeKind.Secure:
|
|
|
|
await sn.signingServer.sserver.stop()
|
|
|
|
of SigningNodeKind.NonSecure:
|
|
|
|
await sn.signingServer.nserver.stop()
|
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
proc close(sn: SigningNodeRef) {.async.} =
|
2021-11-30 01:20:21 +00:00
|
|
|
case sn.signingServer.kind
|
|
|
|
of SigningNodeKind.Secure:
|
2023-04-06 13:16:21 +00:00
|
|
|
await sn.signingServer.sserver.closeWait()
|
2021-11-30 01:20:21 +00:00
|
|
|
of SigningNodeKind.NonSecure:
|
2023-04-06 13:16:21 +00:00
|
|
|
await sn.signingServer.nserver.closeWait()
|
2021-11-30 01:20:21 +00:00
|
|
|
|
|
|
|
proc loadTLSCert(pathName: InputFile): Result[TLSCertificate, cstring] =
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = io2.readAllChars(string(pathName))
|
|
|
|
if res.isErr():
|
|
|
|
return err("Could not read certificate file")
|
|
|
|
res.get()
|
|
|
|
let cert =
|
|
|
|
try:
|
|
|
|
TLSCertificate.init(data)
|
|
|
|
except TLSStreamProtocolError:
|
|
|
|
return err("Invalid certificate or incorrect file format")
|
|
|
|
ok(cert)
|
|
|
|
|
|
|
|
proc loadTLSKey(pathName: InputFile): Result[TLSPrivateKey, cstring] =
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = io2.readAllChars(string(pathName))
|
|
|
|
if res.isErr():
|
|
|
|
return err("Could not read private key file")
|
|
|
|
res.get()
|
|
|
|
let key =
|
|
|
|
try:
|
|
|
|
TLSPrivateKey.init(data)
|
|
|
|
except TLSStreamProtocolError:
|
|
|
|
return err("Invalid private key or incorrect file format")
|
|
|
|
ok(key)
|
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
proc new(t: typedesc[SigningNodeRef], config: SigningNodeConf): SigningNodeRef =
|
|
|
|
when declared(waitSignal):
|
|
|
|
SigningNodeRef(
|
|
|
|
config: config,
|
|
|
|
sigintHandleFut: waitSignal(SIGINT),
|
|
|
|
sigtermHandleFut: waitSignal(SIGTERM),
|
|
|
|
keystoreCache: KeystoreCacheRef.init()
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
SigningNodeRef(
|
|
|
|
config: config,
|
|
|
|
sigintHandleFut: newFuture[void]("sigint_placeholder"),
|
|
|
|
sigtermHandleFut: newFuture[void]("sigterm_placeholder"),
|
|
|
|
keystoreCache: KeystoreCacheRef.init()
|
|
|
|
)
|
|
|
|
|
|
|
|
template errorResponse(code: HttpCode, message: string): RestApiResponse =
|
|
|
|
RestApiResponse.response("{\"error\": \"" & message & "\"}", code)
|
|
|
|
|
|
|
|
template signatureResponse(code: HttpCode, signature: string): RestApiResponse =
|
|
|
|
RestApiResponse.response("{\"signature\": \"0x" & signature & "\"}",
|
|
|
|
code, "application/json")
|
|
|
|
|
|
|
|
proc loadKeystores*(node: SigningNodeRef) =
|
|
|
|
var keysList: seq[string]
|
|
|
|
for keystore in listLoadableKeystores(node.config, node.keystoreCache):
|
2022-09-17 05:30:07 +00:00
|
|
|
# Not relevant in signing node
|
|
|
|
# TODO don't print when loading validators
|
|
|
|
let feeRecipient = default(Eth1Address)
|
2021-12-22 12:37:31 +00:00
|
|
|
case keystore.kind
|
|
|
|
of KeystoreKind.Local:
|
2023-04-06 13:16:21 +00:00
|
|
|
discard node.attachedValidators.addValidator(keystore,
|
|
|
|
feeRecipient,
|
|
|
|
defaultGasLimit)
|
|
|
|
keysList.add("\"0x" & keystore.pubkey.toHex() & "\"")
|
2021-12-22 12:37:31 +00:00
|
|
|
of KeystoreKind.Remote:
|
2023-04-06 13:16:21 +00:00
|
|
|
warn "Signing node do not support remote validators",
|
|
|
|
path = node.config.validatorsDir(),
|
|
|
|
validator_pubkey = keystore.pubkey
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
node.keysList = "[" & keysList.join(", ") & "]"
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
proc installApiHandlers*(node: SigningNodeRef) =
|
2021-11-30 01:20:21 +00:00
|
|
|
var router = node.router()
|
|
|
|
|
|
|
|
router.api(MethodGet, "/api/v1/eth2/publicKeys") do () -> RestApiResponse:
|
|
|
|
return RestApiResponse.response(node.keysList, Http200,
|
|
|
|
"application/json")
|
|
|
|
|
|
|
|
router.api(MethodGet, "/upcheck") do () -> RestApiResponse:
|
|
|
|
return RestApiResponse.response("{\"status\": \"OK\"}", Http200,
|
|
|
|
"application/json")
|
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
router.api(MethodPost, "/reload") do () -> RestApiResponse:
|
|
|
|
node.attachedValidators.close()
|
|
|
|
node.loadKeystores()
|
|
|
|
return RestApiResponse.response(Http200)
|
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
router.api(MethodPost, "/api/v1/eth2/sign/{validator_key}") do (
|
|
|
|
validator_key: ValidatorPubKey,
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let request =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return errorResponse(Http400, EmptyRequestBodyError)
|
|
|
|
let res = decodeBody(Web3SignerRequest, contentBody.get())
|
|
|
|
if res.isErr():
|
|
|
|
return errorResponse(Http400, $res.error())
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
let validator =
|
|
|
|
block:
|
|
|
|
if validator_key.isErr():
|
|
|
|
return errorResponse(Http400, InvalidValidatorPublicKey)
|
|
|
|
let key = validator_key.get()
|
2023-02-20 11:28:56 +00:00
|
|
|
let validator = node.attachedValidators.getValidator(key).valueOr:
|
2021-11-30 01:20:21 +00:00
|
|
|
return errorResponse(Http404, ValidatorNotFoundError)
|
|
|
|
validator
|
|
|
|
|
|
|
|
return
|
|
|
|
case request.kind
|
|
|
|
of Web3SignerRequestKind.AggregationSlot:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_slot_signature(forkInfo.fork,
|
2022-04-08 16:22:49 +00:00
|
|
|
forkInfo.genesis_validators_root,
|
2023-04-06 13:16:21 +00:00
|
|
|
request.aggregationSlot.slot,
|
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.AggregateAndProof:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_aggregate_and_proof_signature(forkInfo.fork,
|
2022-04-08 16:22:49 +00:00
|
|
|
forkInfo.genesis_validators_root, request.aggregateAndProof,
|
2023-04-06 13:16:21 +00:00
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.Attestation:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_attestation_signature(forkInfo.fork,
|
2022-04-08 16:22:49 +00:00
|
|
|
forkInfo.genesis_validators_root, request.attestation,
|
2023-04-06 13:16:21 +00:00
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.BlockV2:
|
2023-04-06 13:16:21 +00:00
|
|
|
if node.config.expectedFeeRecipient.isNone():
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
2023-08-15 22:39:12 +00:00
|
|
|
blockRoot = hash_tree_root(request.beaconBlockHeader)
|
|
|
|
signature = get_block_signature(
|
|
|
|
forkInfo.fork, forkInfo.genesis_validators_root,
|
|
|
|
request.beaconBlockHeader.data.slot, blockRoot,
|
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2023-04-06 13:16:21 +00:00
|
|
|
return signatureResponse(Http200, signature)
|
|
|
|
|
|
|
|
let (feeRecipientIndex, blockHeader) =
|
2023-08-15 22:39:12 +00:00
|
|
|
case request.beaconBlockHeader.kind
|
2023-04-06 13:16:21 +00:00
|
|
|
of ConsensusFork.Phase0, ConsensusFork.Altair:
|
|
|
|
# `phase0` and `altair` blocks do not have `fee_recipient`, so
|
|
|
|
# we return an error.
|
|
|
|
return errorResponse(Http400, BlockIncorrectFork)
|
2023-06-29 09:36:43 +00:00
|
|
|
of ConsensusFork.Bellatrix, ConsensusFork.Capella:
|
2023-08-15 22:39:12 +00:00
|
|
|
(GeneralizedIndex(401), request.beaconBlockHeader.data)
|
2023-04-06 13:16:21 +00:00
|
|
|
of ConsensusFork.Deneb:
|
2023-08-15 22:39:12 +00:00
|
|
|
(GeneralizedIndex(801), request.beaconBlockHeader.data)
|
2023-04-06 13:16:21 +00:00
|
|
|
|
|
|
|
if request.proofs.isNone() or len(request.proofs.get()) == 0:
|
|
|
|
return errorResponse(Http400, MissingMerkleProofError)
|
|
|
|
|
|
|
|
let proof = request.proofs.get()[0]
|
|
|
|
|
|
|
|
if proof.index != feeRecipientIndex:
|
|
|
|
return errorResponse(Http400, InvalidMerkleProofIndexError)
|
|
|
|
|
|
|
|
let feeRecipientRoot = hash_tree_root(distinctBase(
|
|
|
|
node.config.expectedFeeRecipient.get()))
|
|
|
|
|
2023-05-09 08:16:43 +00:00
|
|
|
if not(is_valid_merkle_branch(feeRecipientRoot, proof.proof,
|
2023-04-06 13:16:21 +00:00
|
|
|
log2trunc(proof.index),
|
|
|
|
get_subtree_index(proof.index),
|
|
|
|
blockHeader.body_root)):
|
|
|
|
return errorResponse(Http400, InvalidMerkleProofError)
|
|
|
|
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
2023-08-15 22:39:12 +00:00
|
|
|
blockRoot = hash_tree_root(request.beaconBlockHeader)
|
2023-06-29 09:36:43 +00:00
|
|
|
signature = get_block_signature(forkInfo.fork,
|
2023-08-15 22:39:12 +00:00
|
|
|
forkInfo.genesis_validators_root,
|
|
|
|
request.beaconBlockHeader.data.slot, blockRoot,
|
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.Deposit:
|
|
|
|
let
|
|
|
|
data = DepositMessage(pubkey: request.deposit.pubkey,
|
|
|
|
withdrawal_credentials: request.deposit.withdrawalCredentials,
|
|
|
|
amount: request.deposit.amount)
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_deposit_signature(data,
|
|
|
|
request.deposit.genesisForkVersion,
|
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.RandaoReveal:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_epoch_signature(forkInfo.fork,
|
2022-04-08 16:22:49 +00:00
|
|
|
forkInfo.genesis_validators_root, request.randaoReveal.epoch,
|
2023-04-06 13:16:21 +00:00
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.VoluntaryExit:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_voluntary_exit_signature(forkInfo.fork,
|
2022-04-08 16:22:49 +00:00
|
|
|
forkInfo.genesis_validators_root, request.voluntaryExit,
|
2023-04-06 13:16:21 +00:00
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeMessage:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
|
|
|
msg = request.syncCommitteeMessage
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_sync_committee_message_signature(forkInfo.fork,
|
2022-04-08 16:22:49 +00:00
|
|
|
forkInfo.genesis_validators_root, msg.slot, msg.beaconBlockRoot,
|
2023-04-06 13:16:21 +00:00
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeSelectionProof:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
|
|
|
msg = request.syncAggregatorSelectionData
|
2023-04-06 13:16:21 +00:00
|
|
|
subcommittee =
|
|
|
|
SyncSubcommitteeIndex.init(msg.subcommittee_index).valueOr:
|
|
|
|
return errorResponse(Http400, InvalidSubCommitteeIndexValueError)
|
|
|
|
signature = get_sync_committee_selection_proof(forkInfo.fork,
|
2022-05-10 10:03:40 +00:00
|
|
|
forkInfo.genesis_validators_root, msg.slot, subcommittee,
|
2023-04-06 13:16:21 +00:00
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeContributionAndProof:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
|
|
|
msg = request.syncCommitteeContributionAndProof
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_contribution_and_proof_signature(
|
2022-04-08 16:22:49 +00:00
|
|
|
forkInfo.fork, forkInfo.genesis_validators_root, msg,
|
2023-04-06 13:16:21 +00:00
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2021-11-30 01:20:21 +00:00
|
|
|
signatureResponse(Http200, signature)
|
2022-08-01 06:41:47 +00:00
|
|
|
of Web3SignerRequestKind.ValidatorRegistration:
|
|
|
|
let
|
|
|
|
forkInfo = request.forkInfo.get()
|
2023-04-06 13:16:21 +00:00
|
|
|
signature = get_builder_signature(forkInfo.fork,
|
|
|
|
ValidatorRegistrationV1(
|
2022-08-01 06:41:47 +00:00
|
|
|
fee_recipient:
|
|
|
|
ExecutionAddress(data: distinctBase(Eth1Address.fromHex(
|
|
|
|
request.validatorRegistration.feeRecipient))),
|
|
|
|
gas_limit: request.validatorRegistration.gasLimit,
|
|
|
|
timestamp: request.validatorRegistration.timestamp,
|
|
|
|
pubkey: request.validatorRegistration.pubkey,
|
|
|
|
),
|
2023-04-06 13:16:21 +00:00
|
|
|
validator.data.privateKey).toValidatorSig().toHex()
|
2022-08-01 06:41:47 +00:00
|
|
|
signatureResponse(Http200, signature)
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
proc asyncInit(sn: SigningNodeRef) {.async.} =
|
|
|
|
notice "Launching signing node", version = fullVersionStr,
|
|
|
|
cmdParams = commandLineParams(), config = sn.config
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
info "Initializaing validators", path = sn.config.validatorsDir()
|
|
|
|
sn.loadKeystores()
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
if sn.attachedValidators.count() == 0:
|
|
|
|
fatal "Could not find/initialize local validators"
|
|
|
|
raise newException(SigningNodeError, "")
|
2021-11-30 01:20:21 +00:00
|
|
|
|
2023-04-06 13:16:21 +00:00
|
|
|
let
|
|
|
|
address = initTAddress(sn.config.bindAddress, sn.config.bindPort)
|
|
|
|
serverFlags = {HttpServerFlags.QueryCommaSeparatedArray,
|
|
|
|
HttpServerFlags.NotifyDisconnect}
|
|
|
|
timeout =
|
|
|
|
if sn.config.requestTimeout < 0:
|
|
|
|
warn "Negative value of request timeout, using default instead"
|
|
|
|
seconds(defaultSigningNodeRequestTimeout)
|
|
|
|
else:
|
|
|
|
seconds(sn.config.requestTimeout)
|
|
|
|
serverIdent =
|
|
|
|
if sn.config.serverIdent.isSome():
|
|
|
|
sn.config.serverIdent.get()
|
|
|
|
else:
|
|
|
|
NimbusSigningNodeIdent
|
|
|
|
|
|
|
|
sn.signingServer =
|
|
|
|
if sn.config.tlsEnabled:
|
|
|
|
if sn.config.tlsCertificate.isNone():
|
|
|
|
fatal "TLS certificate path is missing, please use --tls-cert option"
|
|
|
|
raise newException(SigningNodeError, "")
|
|
|
|
|
|
|
|
if sn.config.tlsPrivateKey.isNone():
|
|
|
|
fatal "TLS private key path is missing, please use --tls-key option"
|
|
|
|
raise newException(SigningNodeError, "")
|
|
|
|
|
|
|
|
let cert =
|
|
|
|
block:
|
|
|
|
let res = loadTLSCert(sn.config.tlsCertificate.get())
|
|
|
|
if res.isErr():
|
|
|
|
fatal "Could not initialize SSL certificate",
|
|
|
|
reason = $res.error()
|
|
|
|
raise newException(SigningNodeError, "")
|
|
|
|
res.get()
|
|
|
|
let key =
|
|
|
|
block:
|
|
|
|
let res = loadTLSKey(sn.config.tlsPrivateKey.get())
|
|
|
|
if res.isErr():
|
|
|
|
fatal "Could not initialize SSL private key",
|
|
|
|
reason = $res.error()
|
|
|
|
raise newException(SigningNodeError, "")
|
|
|
|
res.get()
|
|
|
|
let res = SecureRestServerRef.new(getRouter(), address, key, cert,
|
|
|
|
serverFlags = serverFlags,
|
|
|
|
httpHeadersTimeout = timeout,
|
|
|
|
serverIdent = serverIdent)
|
|
|
|
if res.isErr():
|
|
|
|
fatal "HTTPS(REST) server could not be started", address = $address,
|
|
|
|
reason = $res.error()
|
|
|
|
raise newException(SigningNodeError, "")
|
|
|
|
SigningNodeServer(kind: SigningNodeKind.Secure, sserver: res.get())
|
|
|
|
else:
|
|
|
|
let res = RestServerRef.new(getRouter(), address,
|
|
|
|
serverFlags = serverFlags,
|
|
|
|
httpHeadersTimeout = timeout,
|
|
|
|
serverIdent = serverIdent)
|
|
|
|
if res.isErr():
|
|
|
|
fatal "HTTP(REST) server could not be started", address = $address,
|
|
|
|
reason = $res.error()
|
|
|
|
raise newException(SigningNodeError, "")
|
|
|
|
SigningNodeServer(kind: SigningNodeKind.NonSecure, nserver: res.get())
|
|
|
|
|
|
|
|
proc asyncRun*(sn: SigningNodeRef) {.async.} =
|
|
|
|
sn.runKeystoreCachePruningLoopFut =
|
2023-05-10 10:20:55 +00:00
|
|
|
runKeystoreCachePruningLoop(sn.keystoreCache)
|
2022-02-27 11:02:45 +00:00
|
|
|
sn.installApiHandlers()
|
|
|
|
sn.start()
|
2023-04-06 13:16:21 +00:00
|
|
|
|
|
|
|
var future = newFuture[void]("signing-node-mainLoop")
|
2022-02-27 11:02:45 +00:00
|
|
|
try:
|
2023-04-06 13:16:21 +00:00
|
|
|
await future
|
|
|
|
except CancelledError:
|
|
|
|
debug "Main loop interrupted"
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "Main loop failed with unexpected error", err_name = $exc.name,
|
|
|
|
reason = $exc.msg
|
|
|
|
|
|
|
|
debug "Stopping main processing loop"
|
|
|
|
var pending: seq[Future[void]]
|
|
|
|
if not(sn.runKeystoreCachePruningLoopFut.finished()):
|
|
|
|
pending.add(cancelAndWait(sn.runKeystoreCachePruningLoopFut))
|
|
|
|
pending.add(sn.stop())
|
|
|
|
pending.add(sn.close())
|
2023-09-22 11:06:27 +00:00
|
|
|
await noCancel allFutures(pending)
|
2023-04-06 13:16:21 +00:00
|
|
|
|
|
|
|
template runWithSignals(sn: SigningNodeRef, body: untyped): bool =
|
|
|
|
let future = body
|
|
|
|
discard await race(future, sn.sigintHandleFut, sn.sigtermHandleFut)
|
|
|
|
if future.finished():
|
|
|
|
if future.failed() or future.cancelled():
|
2023-12-05 11:45:47 +00:00
|
|
|
discard future.readError()
|
2023-04-06 13:16:21 +00:00
|
|
|
debug "Signing node initialization failed"
|
|
|
|
var pending: seq[Future[void]]
|
|
|
|
if not(sn.sigintHandleFut.finished()):
|
|
|
|
pending.add(cancelAndWait(sn.sigintHandleFut))
|
|
|
|
if not(sn.sigtermHandleFut.finished()):
|
|
|
|
pending.add(cancelAndWait(sn.sigtermHandleFut))
|
2023-09-22 11:06:27 +00:00
|
|
|
await noCancel allFutures(pending)
|
2023-04-06 13:16:21 +00:00
|
|
|
false
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
let signal = if sn.sigintHandleFut.finished(): "SIGINT" else: "SIGTERM"
|
|
|
|
info "Got interrupt, trying to shutdown gracefully", signal = signal
|
|
|
|
var pending = @[cancelAndWait(future)]
|
|
|
|
if not(sn.sigintHandleFut.finished()):
|
|
|
|
pending.add(cancelAndWait(sn.sigintHandleFut))
|
|
|
|
if not(sn.sigtermHandleFut.finished()):
|
|
|
|
pending.add(cancelAndWait(sn.sigtermHandleFut))
|
2023-09-22 11:06:27 +00:00
|
|
|
await noCancel allFutures(pending)
|
2023-04-06 13:16:21 +00:00
|
|
|
false
|
|
|
|
|
|
|
|
proc runSigningNode(config: SigningNodeConf) {.async.} =
|
|
|
|
let sn = SigningNodeRef.new(config)
|
|
|
|
if not sn.runWithSignals(asyncInit sn):
|
|
|
|
return
|
|
|
|
if not sn.runWithSignals(asyncRun sn):
|
|
|
|
return
|
|
|
|
|
|
|
|
programMain:
|
|
|
|
let config =
|
|
|
|
makeBannerAndConfig("Nimbus signing node " & fullVersionStr,
|
|
|
|
SigningNodeConf)
|
|
|
|
setupLogging(config.logLevel, config.logStdout, config.logFile)
|
|
|
|
waitFor runSigningNode(config)
|