All files / src/command getBlkInfo.js

100% Statements 19/19
100% Branches 2/2
100% Functions 3/3
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91      3x                                 8x                                 3x 3x 3x   1x                     3x 3x                           3x 3x 3x 3x     3x 1x   2x   2x   2x   1x   1x            
import AElf from 'aelf-sdk';
import { interopImportCJSDefault } from 'node-cjs-interop';
import asyncValidator from 'async-validator';
const Schema = interopImportCJSDefault(asyncValidator);
import BaseSubCommand from './baseSubCommand.js';
import { commonGlobalOptionValidatorDesc, blkInfoCommandParameters, blkInfoCommandUsage } from '../utils/constants.js';
import { logger } from '../utils/myLogger.js';
 
/**
 * @typedef {import('commander').Command} Command
 * @typedef {import('async-validator').Rules} Rules
 * @typedef {import('async-validator').Values} Values
 * @typedef {import('../../types/rc/index.js').default} Registry
 */
class GetBlkInfoCommand extends BaseSubCommand {
  /**
   * Constructs a new GetBlkInfoCommand instance.
   * @param {Registry} rc - The registry instance.
   */
  constructor(rc) {
    super(
      'get-blk-info',
      blkInfoCommandParameters,
      'Get a block info',
      [],
      blkInfoCommandUsage,
      rc,
      commonGlobalOptionValidatorDesc
    );
  }
  /**
   * Validates the provided parameters against the given rules.
   * @param {Rules} rule - The validation rules.
   * @param {Values} parameters - The parameters to validate.
   * @returns {Promise<void>} A promise that resolves when validation is complete.
   */
  async validateParameters(rule, parameters) {
    const validator = new Schema(rule);
    try {
      await validator.validate(parameters);
    } catch (e) {
      this.handleUniOptionsError(e);
    }
  }
  /**
   * Executes the get block info command.
   * @param {Command} commander - The commander instance.
   * @param {...any} args - Additional arguments.
   * @returns {Promise<void>} A promise that resolves when the command execution is complete.
   */
  async run(commander, ...args) {
    // @ts-ignore
    const { options, subOptions } = await super.run(commander, ...args);
    await this.validateParameters(
      {
        height: {
          type: 'string',
          required: true,
          message: 'Input a valid <height|block-hash>'
        },
        includeTxs: {
          type: 'boolean',
          message: 'Input a valid <include-txs>'
        }
      },
      subOptions
    );
    const aelf = new AElf(new AElf.providers.HttpProvider(options.endpoint));
    const { height, includeTxs } = subOptions;
    try {
      this.oraInstance.start();
      let blockInfo;
      // usually block hash is encoded with hex and has 64 characters
      if (String(height).trim().length > 50) {
        blockInfo = await aelf.chain.getBlock(height, includeTxs);
      } else {
        blockInfo = await aelf.chain.getBlockByHeight(height, includeTxs);
      }
      this.oraInstance.succeed('Succeed!');
      // @ts-ignore
      logger.info(blockInfo);
    } catch (e) {
      this.oraInstance.fail('Failed!');
      // @ts-ignore
      logger.error(e);
    }
  }
}
 
export default GetBlkInfoCommand;