UNPKG

@rsksmart/rif-storage-pinning

Version:

Application for providing your storage space to other to use in exchange of RIF Tokens

118 lines (117 loc) 6.42 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const command_1 = require("@oclif/command"); const config_1 = __importDefault(require("config")); const path_1 = __importDefault(require("path")); const sequelize_store_1 = require("sequelize-store"); const utils_1 = __importDefault(require("../utils")); const index_1 = require("../index"); const logger_1 = require("../logger"); const definitions_1 = require("../definitions"); const logger = logger_1.loggingFactory('cli:daemon'); class DaemonCommand extends utils_1.default { constructor(argv, config) { super(argv, config, { db: { migrate: true } }); } baseConfig(flags) { var _a, _b; super.baseConfig(flags); const { userConfig, configObject } = this.configuration; if (flags.network) { const networkConfigPath = path_1.default.join(__dirname, '..', '..', 'config', `${flags.network}.json5`); config_1.default.util.extendDeep(config_1.default, config_1.default.util.parseFile(networkConfigPath)); } if (flags.strategy) { configObject.strategy = flags.strategy; } if (flags.provider) { const strategy = (_b = (_a = userConfig.strategy) !== null && _a !== void 0 ? _a : configObject.strategy) !== null && _b !== void 0 ? _b : definitions_1.Strategy.Marketplace; if (strategy === definitions_1.Strategy.Blockchain) { configObject.blockchain = { provider: flags.provider }; } else { configObject.marketplace = { provider: flags.provider }; } } if (flags.ipfs) { configObject.ipfs = { clientOptions: { url: flags.ipfs } }; } config_1.default.util.extendDeep(config_1.default, userConfig); config_1.default.util.extendDeep(config_1.default, configObject); if (!config_1.default.has('blockchain.contractAddress')) { throw new Error('You have to specify address of smart contract! Use --network flag!'); } } run() { return __awaiter(this, void 0, void 0, function* () { const offerId = this.offerId; if (config_1.default.get('strategy') === 'marketplace') { logger.warn("By default, the 'marketplace' communication strategy is used. This method should be used for testing purposes. For providing services to a real customer - use the 'blockchain' strategy and connect to a full RSK node that exposes the 'getLogs' method. You may install it locally or connect to a public one."); } // An infinite loop which you can exit only with SIGINT/SIGKILL while (true) { let stopCallback = (() => { throw new Error('No stop callback was assigned!'); }); // Promise that resolves when reset callback is called const resetPromise = new Promise((resolve, reject) => { index_1.initApp(offerId, { appResetCallback: () => resolve() }).then(value => { // Lets save the function that stops the app stopCallback = value.stop; this.log('Daemon listening for incoming events.'); }).catch(reject); }); // Let see if we have to restart the app at some point most probably because // reorgs outside of confirmation range. yield resetPromise; logger.warn('Reorg detected outside of confirmation range. Rebuilding the service\'s state!'); logger.info('Stopping service'); stopCallback(); logger.info('Removing current DB'); fs_1.default.unlinkSync(this.dbPath); this.sequelize = undefined; sequelize_store_1.reset(); // We need to reset the store object so it gets re-initted yield this.initDB(this.dbPath, { migrate: true, skipPrompt: true }); this.offerId = offerId; // Lets reset the offerId so the DB is properly configured logger.info('Restarting the app'); } }); } } exports.default = DaemonCommand; DaemonCommand.description = 'Run pinning service'; DaemonCommand.examples = [ '$ rif-pinning daemon --strategy=blockchain --provider \'ws://localhost:8546\' --ipfs \'/ip4/127.0.0.1/tcp/5001\' --network testnet', '', '$ rif-pinning daemon --strategy=marketplace --ipfs \'http://localhost:5001\' --network testnet' ]; DaemonCommand.flags = Object.assign(Object.assign({}, utils_1.default.flags), { network: command_1.flags.string({ char: 'n', description: 'specifies to which network is the provider connected', options: ['testnet', 'mainnet'], env: 'RIFS_NETWORK' }), provider: command_1.flags.string({ char: 'p', description: 'URL to blockchain node or Marketplace server', env: 'RIFS_PROVIDER' }), strategy: command_1.flags.string({ description: 'what type of provider will be used for listening on events. Default is "marketplace". For blockchain you have to have access to a node that has allowed eth_getLogs call.', options: ['marketplace', 'blockchain'], default: 'marketplace' }), ipfs: command_1.flags.string({ description: 'specifies a connection URL to IPFS node. Default is go-ipfs listening configuration.', env: 'RIFS_IPFS' }) });