@kyve/core-beta
Version:
🚀 The base KYVE node implementation.
187 lines (186 loc) • 8.35 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Node = void 0;
const commander_1 = require("commander");
const os_1 = __importDefault(require("os"));
const package_json_1 = require("../package.json");
const commander_2 = require("./commander");
const methods_1 = require("./methods");
const utils_1 = require("./utils");
/**
* Main class of KYVE protocol nodes representing a node.
*
* @class Node
* @constructor
*/
class Node {
/**
* Constructor for the core class. It is required to provide the
* runtime class here in order to run the
*
* @method constructor
* @param {IRuntime} runtime which implements the interface IRuntime
*/
constructor(runtime) {
// setups
this.setupLogger = methods_1.setupLogger;
this.setupCacheProvider = methods_1.setupCacheProvider;
this.setupMetrics = methods_1.setupMetrics;
this.setupSDK = methods_1.setupSDK;
this.setupValidator = methods_1.setupValidator;
// checks
this.validateRuntime = methods_1.validateRuntime;
this.validateVersion = methods_1.validateVersion;
this.validateIsNodeValidator = methods_1.validateIsNodeValidator;
this.validateIsPoolActive = methods_1.validateIsPoolActive;
// timeouts
this.waitForAuthorization = methods_1.waitForAuthorization;
this.waitForUploadInterval = methods_1.waitForUploadInterval;
this.waitForNextBundleProposal = methods_1.waitForNextBundleProposal;
this.waitForCacheContinuation = methods_1.waitForCacheContinuation;
// helpers
this.continueRound = methods_1.continueRound;
this.saveGetTransformDataItem = methods_1.saveGetTransformDataItem;
// factories
this.storageProviderFactory = methods_1.storageProviderFactory;
this.compressionFactory = methods_1.compressionFactory;
// txs
this.claimUploaderRole = methods_1.claimUploaderRole;
this.skipUploaderRole = methods_1.skipUploaderRole;
this.voteBundleProposal = methods_1.voteBundleProposal;
this.submitBundleProposal = methods_1.submitBundleProposal;
// queries
this.syncPoolState = methods_1.syncPoolState;
this.syncPoolConfig = methods_1.syncPoolConfig;
this.getBalances = methods_1.getBalances;
this.canVote = methods_1.canVote;
this.canPropose = methods_1.canPropose;
// validate
this.saveBundleDownload = methods_1.saveBundleDownload;
this.saveBundleDecompress = methods_1.saveBundleDecompress;
this.saveLoadValidationBundle = methods_1.saveLoadValidationBundle;
this.validateBundleProposal = methods_1.validateBundleProposal;
// upload
this.createBundleProposal = methods_1.createBundleProposal;
// main
this.runNode = methods_1.runNode;
this.runCache = methods_1.runCache;
// set provided runtime
this.runtime = runtime;
// set @kyve/core version
this.coreVersion = package_json_1.version;
}
/**
* Bootstrap method for protocol node. It initializes all commands including
* the main program which can be called with "start"
*
* @method bootstrap
* @return {void}
*/
bootstrap() {
// define main program
const program = new commander_1.Command();
// define version command
program
.command("version")
.description("Print runtime and core version")
.action(() => {
console.log(`${this.runtime.name} version: ${this.runtime.version}`);
console.log(`/core version: ${this.coreVersion}`);
console.log(`Node version: ${process.version}`);
console.log();
console.log(`Platform: ${os_1.default.platform()}`);
console.log(`Arch: ${os_1.default.arch()}`);
});
// define start command
program
.command("start")
.description("Run the protocol node")
.requiredOption("--pool <string>", "The ID of the pool this valaccount should participate as a validator", commander_2.parsePoolId)
.requiredOption("--valaccount <string>", "The mnemonic of the valaccount", commander_2.parseMnemonic)
.requiredOption("--storage-priv <string>", "The private key of the storage provider")
.requiredOption("--network <local|alpha|beta|korellia>", "The network of the KYVE chain", commander_2.parseNetwork)
.option("--rpc", "Custom rpc endpoint the node uses for submitting transactions to chain")
.option("--rest", "Custom rest api endpoint the node uses for querying from chain")
.option("--cache <memory|jsonfile|leveldb>", "The cache this node should use", commander_2.parseCache, "leveldb")
.option("--debug", "Run the validator node in debug mode")
.option("--verbose", "[DEPRECATED] Run the validator node in verbose logging mode")
.option("--metrics", "Start a prometheus metrics server on http://localhost:8080/metrics")
.option("--metrics-port <number>", "Specify the port of the metrics server. Only considered if '--metrics' is set [default = 8080]", "8080")
.option("--home <string>", "Specify the home directory of the node where logs and the cache should save their data. [default current directory]", "./")
.action((options) => {
this.start(options);
});
// bootstrap program
program.parse();
}
/**
* Main method of @kyve/core. By running this method the node will start and run.
* For this method to run the Runtime, Storage Provider and the Cache have to be added first.
*
* This method will run indefinetely and only exits on specific exit conditions like running
* an incorrect runtime or version.
*
* @method start
* @param {OptionValues} options contains all node options defined in bootstrap
* @return {Promise<void>}
*/
async start(options) {
// assign program options
// to node instance
this.poolId = options.pool;
this.valaccount = options.valaccount;
this.storagePriv = options.storagePriv;
this.network = options.network;
this.rpc = options.rpc;
this.rest = options.rest;
this.cache = options.cache;
this.debug = options.debug;
this.metrics = options.metrics;
this.metricsPort = options.metricsPort;
this.home = options.home;
// perform setups
this.setupLogger();
this.setupMetrics();
// perform async setups
await this.setupSDK();
await this.setupValidator();
await this.setupCacheProvider();
// start the node process. Node and cache should run at the same time.
// Thats why, although they are async they are called synchronously
try {
await this.syncPoolState();
this.runNode();
this.runCache();
}
catch (err) {
this.logger.fatal(`Unexpected runtime error. Exiting ...`);
this.logger.fatal((0, utils_1.standardizeJSON)(err));
process.exit(1);
}
}
}
exports.Node = Node;
// export commander
__exportStar(require("./commander"), exports);
// export types
__exportStar(require("./types"), exports);
// export utils
__exportStar(require("./utils"), exports);