All files / src api_client.ts

100% Statements 61/61
81.82% Branches 27/33
100% Functions 14/14
100% Lines 60/60

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                            11x     11x 11x 11x 11x 11x 11x 11x 11x 11x 11x   11x         11x         11x 1x     1x   1x       1x       1x                     11x   1x       3x             3x                                                 99x 99x 99x 99x 99x 99x 99x 99x 99x 99x       5x       3x 3x 2x     3x       7x 4x   4x     3x       103x 230x     103x 1x     102x   102x       4x             104x 3x     101x 2x         99x   99x               99x 99x 99x 99x       246x      
/*
 * Copyright © 2019 Lisk Foundation
 *
 * See the LICENSE file at the top-level directory of this distribution
 * for licensing information.
 *
 * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
 * no part of this software, including this file, may be copied, modified,
 * propagated, or distributed except according to the terms contained in the
 * LICENSE file.
 *
 * Removal or modification of this copyright notice is prohibited.
 *
 */
import * as os from 'os';
 
import { HashMap, InitOptions } from './api_types';
import * as constants from './constants';
import { AccountsResource } from './resources/accounts';
import { BlocksResource } from './resources/blocks';
import { DappsResource } from './resources/dapps';
import { DelegatesResource } from './resources/delegates';
import { NodeResource } from './resources/node';
import { PeersResource } from './resources/peers';
import { TransactionsResource } from './resources/transactions';
import { VotersResource } from './resources/voters';
import { VotesResource } from './resources/votes';
 
const defaultOptions = {
	bannedNodes: [],
	randomizeNodes: true,
};
 
const commonHeaders: HashMap = {
	Accept: 'application/json',
	'Content-Type': 'application/json',
};
 
const getClientHeaders = (clientOptions: ClientOptions): HashMap => {
	const { name = '????', version = '????', engine = '????' } = clientOptions;
 
	const liskElementsInformation =
		'LiskElements/1.0 (+https://github.com/LiskHQ/lisk-elements)';
	const locale: string | undefined =
		process.env.LC_ALL ||
		process.env.LC_MESSAGES ||
		process.env.LANG ||
		process.env.LANGUAGE;
	const systemInformation = `${os.platform()} ${os.release()}; ${os.arch()}${
		locale ? `; ${locale}` : ''
	}`;
 
	return {
		'User-Agent': `${name}/${version} (${engine}) ${liskElementsInformation} ${systemInformation}`,
	};
};
 
export interface ClientOptions {
	readonly engine?: string;
	readonly name?: string;
	readonly version?: string;
}
 
export class APIClient {
	public static get constants(): typeof constants {
		return constants;
	}
 
	public static createMainnetAPIClient(options?: InitOptions): APIClient {
		return new APIClient(constants.MAINNET_NODES, {
			genesisBlockPayloadHash: constants.MAINNET_NETHASH,
			...options,
		});
	}
 
	public static createTestnetAPIClient(options?: InitOptions): APIClient {
		return new APIClient(constants.TESTNET_NODES, {
			genesisBlockPayloadHash: constants.TESTNET_NETHASH,
			...options,
		});
	}
 
	public accounts: AccountsResource;
	public bannedNodes!: ReadonlyArray<string>;
	public blocks: BlocksResource;
	public currentNode!: string;
	public dapps: DappsResource;
	public delegates: DelegatesResource;
	public headers!: HashMap;
	public node: NodeResource;
	public nodes!: ReadonlyArray<string>;
	public peers: PeersResource;
	public randomizeNodes!: boolean;
	public transactions: TransactionsResource;
	public voters: VotersResource;
	public votes: VotesResource;
 
	public constructor(
		nodes: ReadonlyArray<string>,
		providedOptions: InitOptions = {},
	) {
		this.initialize(nodes, providedOptions);
		this.accounts = new AccountsResource(this);
		this.blocks = new BlocksResource(this);
		this.dapps = new DappsResource(this);
		this.delegates = new DelegatesResource(this);
		this.node = new NodeResource(this);
		this.peers = new PeersResource(this);
		this.transactions = new TransactionsResource(this);
		this.voters = new VotersResource(this);
		this.votes = new VotesResource(this);
	}
 
	public banActiveNode(): boolean {
		return this.banNode(this.currentNode);
	}
 
	public banActiveNodeAndSelect(): boolean {
		const banned = this.banActiveNode();
		if (banned) {
			this.currentNode = this.getNewNode();
		}
 
		return banned;
	}
 
	public banNode(node: string): boolean {
		if (!this.isBanned(node)) {
			this.bannedNodes = [...this.bannedNodes, node];
 
			return true;
		}
 
		return false;
	}
 
	public getNewNode(): string {
		const nodes = this.nodes.filter(
			(node: string): boolean => !this.isBanned(node),
		);
 
		if (nodes.length === 0) {
			throw new Error('Cannot get new node: all nodes have been banned.');
		}
 
		const randomIndex = Math.floor(Math.random() * nodes.length);
 
		return nodes[randomIndex];
	}
 
	public hasAvailableNodes(): boolean {
		return this.nodes.some((node: string): boolean => !this.isBanned(node));
	}
 
	public initialize(
		nodes: ReadonlyArray<string>,
		providedOptions: InitOptions = {},
	): void {
		if (!Array.isArray(nodes) || nodes.length <= 0) {
			throw new Error('APIClient requires nodes for initialization.');
		}
 
		if (typeof providedOptions !== 'object' || Array.isArray(providedOptions)) {
			throw new Error(
				'APIClient takes an optional object as the second parameter.',
			);
		}
 
		const options: InitOptions = { ...defaultOptions, ...providedOptions };
 
		this.headers = {
			...commonHeaders,
			...(options.genesisBlockPayloadHash
				? { nethash: options.genesisBlockPayloadHash }
				: {}),
			...(options.client ? getClientHeaders(options.client) : {}),
		};
 
		this.nodes = nodes;
		this.bannedNodes = [...(options.bannedNodes || [])];
		this.currentNode = options.node || this.getNewNode();
		this.randomizeNodes = options.randomizeNodes !== false;
	}
 
	public isBanned(node: string): boolean {
		return this.bannedNodes.includes(node);
	}
}