2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
2020-02-03 23:05:04 -05:00
|
|
|
import { Block, BlockWithTransactions, Provider } from "@ethersproject/abstract-provider";
|
2019-08-22 16:51:35 -04:00
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
2020-05-03 21:04:23 -04:00
|
|
|
import { isHexString } from "@ethersproject/bytes";
|
|
|
|
import { Network } from "@ethersproject/networks";
|
|
|
|
import { deepCopy, defineReadOnly, shallowCopy } from "@ethersproject/properties";
|
|
|
|
import { shuffled } from "@ethersproject/random";
|
|
|
|
import { poll } from "@ethersproject/web";
|
|
|
|
|
|
|
|
import { BaseProvider } from "./base-provider";
|
2020-10-07 17:28:18 -04:00
|
|
|
import { isCommunityResource } from "./formatter";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
function now() { return (new Date()).getTime(); }
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
// Returns to network as long as all agree, or null if any is null.
|
|
|
|
// Throws an error if any two networks do not match.
|
|
|
|
function checkNetworks(networks: Array<Network>): Network {
|
|
|
|
let result = null;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
for (let i = 0; i < networks.length; i++) {
|
|
|
|
const network = networks[i];
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
// Null! We do not know our network; bail.
|
|
|
|
if (network == null) { return null; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
if (result) {
|
|
|
|
// Make sure the network matches the previous networks
|
|
|
|
if (!(result.name === network.name && result.chainId === network.chainId &&
|
|
|
|
((result.ensAddress === network.ensAddress) || (result.ensAddress == null && network.ensAddress == null)))) {
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
logger.throwArgumentError("provider mismatch", "networks", networks);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = network;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-23 08:15:46 -04:00
|
|
|
function median(values: Array<number>, maxDelta?: number): number {
|
|
|
|
values = values.slice().sort();
|
|
|
|
const middle = Math.floor(values.length / 2);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-23 08:15:46 -04:00
|
|
|
// Odd length; take the middle
|
|
|
|
if (values.length % 2) {
|
|
|
|
return values[middle];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even length; take the average of the two middle
|
|
|
|
const a = values[middle - 1], b = values[middle];
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-23 08:15:46 -04:00
|
|
|
if (maxDelta != null && Math.abs(a - b) > maxDelta) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (a + b) / 2;
|
2020-01-18 21:24:28 -05:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
function serialize(value: any): string {
|
|
|
|
if (value === null) {
|
2020-02-03 23:05:04 -05:00
|
|
|
return "null";
|
2020-01-18 21:24:28 -05:00
|
|
|
} else if (typeof(value) === "number" || typeof(value) === "boolean") {
|
|
|
|
return JSON.stringify(value);
|
|
|
|
} else if (typeof(value) === "string") {
|
|
|
|
return value;
|
|
|
|
} else if (BigNumber.isBigNumber(value)) {
|
|
|
|
return value.toString();
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
return JSON.stringify(value.map((i) => serialize(i)));
|
|
|
|
} else if (typeof(value) === "object") {
|
|
|
|
const keys = Object.keys(value);
|
2019-05-14 18:25:46 -04:00
|
|
|
keys.sort();
|
2019-08-21 01:45:51 -04:00
|
|
|
return "{" + keys.map((key) => {
|
2020-01-18 21:24:28 -05:00
|
|
|
let v = value[key];
|
|
|
|
if (typeof(v) === "function") {
|
|
|
|
v = "[function]";
|
2019-08-21 01:45:51 -04:00
|
|
|
} else {
|
2020-01-18 21:24:28 -05:00
|
|
|
v = serialize(v);
|
2019-08-21 01:45:51 -04:00
|
|
|
}
|
2020-01-18 21:24:28 -05:00
|
|
|
return JSON.stringify(key) + ":" + v;
|
2019-08-21 01:45:51 -04:00
|
|
|
}).join(",") + "}";
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
throw new Error("unknown value type: " + typeof(value));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
// Next request ID to use for emitting debug info
|
2019-05-14 18:25:46 -04:00
|
|
|
let nextRid = 1;
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
|
|
|
|
export interface FallbackProviderConfig {
|
|
|
|
// The Provider
|
|
|
|
provider: Provider;
|
|
|
|
|
2021-06-24 18:07:33 -04:00
|
|
|
// The priority to favour this Provider; lower values are used first (higher priority)
|
2020-01-18 21:24:28 -05:00
|
|
|
priority?: number;
|
|
|
|
|
|
|
|
// Timeout before also triggering the next provider; this does not stop
|
|
|
|
// this provider and if its result comes back before a quorum is reached
|
2020-05-03 16:15:19 -04:00
|
|
|
// it will be incorporated into the vote
|
2020-01-18 21:24:28 -05:00
|
|
|
// - lower values will cause more network traffic but may result in a
|
2021-10-04 11:46:24 -04:00
|
|
|
// faster result.
|
2020-01-18 21:24:28 -05:00
|
|
|
stallTimeout?: number;
|
|
|
|
|
|
|
|
// How much this provider contributes to the quorum; sometimes a specific
|
|
|
|
// provider may be more reliable or trustworthy than others, but usually
|
|
|
|
// this should be left as the default
|
|
|
|
weight?: number;
|
|
|
|
};
|
|
|
|
|
2020-05-03 16:15:19 -04:00
|
|
|
// A Staller is used to provide a delay to give a Provider a chance to response
|
|
|
|
// before asking the next Provider to try.
|
|
|
|
type Staller = {
|
|
|
|
wait: (func: () => void) => Promise<void>
|
|
|
|
getPromise: () => Promise<void>,
|
|
|
|
cancel: () => void
|
|
|
|
};
|
|
|
|
|
|
|
|
function stall(duration: number): Staller {
|
|
|
|
let cancel: () => void = null;
|
|
|
|
|
|
|
|
let timer: NodeJS.Timer = null;
|
|
|
|
let promise = <Promise<void>>(new Promise((resolve) => {
|
|
|
|
cancel = function() {
|
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
timer = setTimeout(cancel, duration);
|
|
|
|
}));
|
|
|
|
|
|
|
|
const wait = (func: () => void) => {
|
|
|
|
promise = promise.then(func);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPromise(): Promise<void> {
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { cancel, getPromise, wait };
|
2020-01-18 21:24:28 -05:00
|
|
|
}
|
|
|
|
|
2020-09-11 01:10:15 -04:00
|
|
|
const ForwardErrors = [
|
|
|
|
Logger.errors.CALL_EXCEPTION,
|
|
|
|
Logger.errors.INSUFFICIENT_FUNDS,
|
|
|
|
Logger.errors.NONCE_EXPIRED,
|
|
|
|
Logger.errors.REPLACEMENT_UNDERPRICED,
|
|
|
|
Logger.errors.UNPREDICTABLE_GAS_LIMIT
|
|
|
|
];
|
|
|
|
|
|
|
|
const ForwardProperties = [
|
|
|
|
"address",
|
|
|
|
"args",
|
|
|
|
"errorArgs",
|
|
|
|
"errorSignature",
|
|
|
|
"method",
|
|
|
|
"transaction",
|
|
|
|
];
|
|
|
|
|
|
|
|
|
2020-05-04 22:48:21 -04:00
|
|
|
// @TODO: Make this an object with staller and cancel built-in
|
2020-01-18 21:24:28 -05:00
|
|
|
interface RunningConfig extends FallbackProviderConfig {
|
|
|
|
start?: number;
|
|
|
|
done?: boolean;
|
2020-05-04 22:48:21 -04:00
|
|
|
cancelled?: boolean;
|
2020-01-18 21:24:28 -05:00
|
|
|
runner?: Promise<any>;
|
2020-05-03 16:15:19 -04:00
|
|
|
staller?: Staller;
|
2020-01-18 21:24:28 -05:00
|
|
|
result?: any;
|
|
|
|
error?: Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
function exposeDebugConfig(config: RunningConfig, now?: number): any {
|
|
|
|
const result: any = {
|
|
|
|
weight: config.weight
|
|
|
|
};
|
2020-09-11 01:10:15 -04:00
|
|
|
Object.defineProperty(result, "provider", { get: () => config.provider });
|
2020-01-18 21:24:28 -05:00
|
|
|
if (config.start) { result.start = config.start; }
|
|
|
|
if (now) { result.duration = (now - config.start); }
|
|
|
|
if (config.done) {
|
|
|
|
if (config.error) {
|
|
|
|
result.error = config.error;
|
|
|
|
} else {
|
|
|
|
result.result = config.result || null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizedTally(normalize: (value: any) => string, quorum: number): (configs: Array<RunningConfig>) => any {
|
|
|
|
return function(configs: Array<RunningConfig>): any {
|
|
|
|
|
|
|
|
// Count the votes for each result
|
|
|
|
const tally: { [ key: string]: { count: number, result: any } } = { };
|
|
|
|
configs.forEach((c) => {
|
|
|
|
const value = normalize(c.result);
|
|
|
|
if (!tally[value]) { tally[value] = { count: 0, result: c.result }; }
|
|
|
|
tally[value].count++;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check for a quorum on any given result
|
|
|
|
const keys = Object.keys(tally);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const check = tally[keys[i]];
|
|
|
|
if (check.count >= quorum) {
|
|
|
|
return check.result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No quroum
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function getProcessFunc(provider: FallbackProvider, method: string, params: { [ key: string ]: any }): (configs: Array<RunningConfig>) => any {
|
|
|
|
|
|
|
|
let normalize = serialize;
|
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case "getBlockNumber":
|
|
|
|
// Return the median value, unless there is (median + 1) is also
|
|
|
|
// present, in which case that is probably true and the median
|
|
|
|
// is going to be stale soon. In the event of a malicious node,
|
|
|
|
// the lie will be true soon enough.
|
|
|
|
return function(configs: Array<RunningConfig>): number {
|
|
|
|
const values = configs.map((c) => c.result);
|
|
|
|
|
|
|
|
// Get the median block number
|
2020-04-23 08:15:46 -04:00
|
|
|
let blockNumber = median(configs.map((c) => c.result), 2);
|
|
|
|
if (blockNumber == null) { return undefined; }
|
|
|
|
|
|
|
|
blockNumber = Math.ceil(blockNumber);
|
2020-01-18 21:24:28 -05:00
|
|
|
|
|
|
|
// If the next block height is present, its prolly safe to use
|
|
|
|
if (values.indexOf(blockNumber + 1) >= 0) { blockNumber++; }
|
|
|
|
|
|
|
|
// Don't ever roll back the blockNumber
|
|
|
|
if (blockNumber >= provider._highestBlockNumber) {
|
|
|
|
provider._highestBlockNumber = blockNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider._highestBlockNumber;
|
|
|
|
};
|
|
|
|
|
|
|
|
case "getGasPrice":
|
|
|
|
// Return the middle (round index up) value, similar to median
|
|
|
|
// but do not average even entries and choose the higher.
|
|
|
|
// Malicious actors must compromise 50% of the nodes to lie.
|
|
|
|
return function(configs: Array<RunningConfig>): BigNumber {
|
|
|
|
const values = configs.map((c) => c.result);
|
|
|
|
values.sort();
|
|
|
|
return values[Math.floor(values.length / 2)];
|
|
|
|
}
|
|
|
|
|
|
|
|
case "getEtherPrice":
|
|
|
|
// Returns the median price. Malicious actors must compromise at
|
|
|
|
// least 50% of the nodes to lie (in a meaningful way).
|
|
|
|
return function(configs: Array<RunningConfig>): number {
|
|
|
|
return median(configs.map((c) => c.result));
|
|
|
|
}
|
|
|
|
|
|
|
|
// No additional normalizing required; serialize is enough
|
|
|
|
case "getBalance":
|
|
|
|
case "getTransactionCount":
|
|
|
|
case "getCode":
|
|
|
|
case "getStorageAt":
|
|
|
|
case "call":
|
|
|
|
case "estimateGas":
|
|
|
|
case "getLogs":
|
|
|
|
break;
|
|
|
|
|
|
|
|
// We drop the confirmations from transactions as it is approximate
|
|
|
|
case "getTransaction":
|
|
|
|
case "getTransactionReceipt":
|
|
|
|
normalize = function(tx: any): string {
|
2020-02-03 23:05:04 -05:00
|
|
|
if (tx == null) { return null; }
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
tx = shallowCopy(tx);
|
|
|
|
tx.confirmations = -1;
|
|
|
|
return serialize(tx);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// We drop the confirmations from transactions as it is approximate
|
|
|
|
case "getBlock":
|
|
|
|
// We drop the confirmations from transactions as it is approximate
|
|
|
|
if (params.includeTransactions) {
|
|
|
|
normalize = function(block: BlockWithTransactions): string {
|
2020-02-03 23:05:04 -05:00
|
|
|
if (block == null) { return null; }
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
block = shallowCopy(block);
|
|
|
|
block.transactions = block.transactions.map((tx) => {
|
|
|
|
tx = shallowCopy(tx);
|
|
|
|
tx.confirmations = -1;
|
|
|
|
return tx;
|
|
|
|
});
|
|
|
|
return serialize(block);
|
|
|
|
};
|
2020-02-03 23:05:04 -05:00
|
|
|
} else {
|
|
|
|
normalize = function(block: Block): string {
|
|
|
|
if (block == null) { return null; }
|
|
|
|
return serialize(block);
|
|
|
|
}
|
2020-01-18 21:24:28 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error("unknown method: " + method);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the result if and only if the expected quorum is
|
|
|
|
// satisfied and agreed upon for the final result.
|
|
|
|
return normalizedTally(normalize, provider.quorum);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-03 21:04:23 -04:00
|
|
|
// If we are doing a blockTag query, we need to make sure the backend is
|
|
|
|
// caught up to the FallbackProvider, before sending a request to it.
|
2020-05-04 22:48:21 -04:00
|
|
|
async function waitForSync(config: RunningConfig, blockNumber: number): Promise<BaseProvider> {
|
|
|
|
const provider = <BaseProvider>(config.provider);
|
|
|
|
|
2020-05-03 21:04:23 -04:00
|
|
|
if ((provider.blockNumber != null && provider.blockNumber >= blockNumber) || blockNumber === -1) {
|
|
|
|
return provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
return poll(() => {
|
2020-05-04 22:48:21 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(function() {
|
|
|
|
|
|
|
|
// We are synced
|
2020-05-20 23:42:08 -04:00
|
|
|
if (provider.blockNumber >= blockNumber) { return resolve(provider); }
|
2020-05-04 22:48:21 -04:00
|
|
|
|
|
|
|
// We're done; just quit
|
|
|
|
if (config.cancelled) { return resolve(null); }
|
|
|
|
|
|
|
|
// Try again, next block
|
|
|
|
return resolve(undefined);
|
|
|
|
}, 0);
|
2020-05-03 21:04:23 -04:00
|
|
|
});
|
2020-05-04 22:48:21 -04:00
|
|
|
}, { oncePoll: provider });
|
2020-05-03 21:04:23 -04:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:48:21 -04:00
|
|
|
async function getRunner(config: RunningConfig, currentBlockNumber: number, method: string, params: { [ key: string]: any }): Promise<any> {
|
|
|
|
let provider = config.provider;
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
switch (method) {
|
|
|
|
case "getBlockNumber":
|
|
|
|
case "getGasPrice":
|
|
|
|
return provider[method]();
|
|
|
|
case "getEtherPrice":
|
|
|
|
if ((<any>provider).getEtherPrice) {
|
|
|
|
return (<any>provider).getEtherPrice();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "getBalance":
|
|
|
|
case "getTransactionCount":
|
|
|
|
case "getCode":
|
2020-05-03 21:04:23 -04:00
|
|
|
if (params.blockTag && isHexString(params.blockTag)) {
|
2020-05-04 22:48:21 -04:00
|
|
|
provider = await waitForSync(config, currentBlockNumber)
|
2020-05-03 21:04:23 -04:00
|
|
|
}
|
2020-01-18 21:24:28 -05:00
|
|
|
return provider[method](params.address, params.blockTag || "latest");
|
|
|
|
case "getStorageAt":
|
2020-05-03 21:04:23 -04:00
|
|
|
if (params.blockTag && isHexString(params.blockTag)) {
|
2020-05-04 22:48:21 -04:00
|
|
|
provider = await waitForSync(config, currentBlockNumber)
|
2020-05-03 21:04:23 -04:00
|
|
|
}
|
2020-01-18 21:24:28 -05:00
|
|
|
return provider.getStorageAt(params.address, params.position, params.blockTag || "latest");
|
|
|
|
case "getBlock":
|
2020-05-03 21:04:23 -04:00
|
|
|
if (params.blockTag && isHexString(params.blockTag)) {
|
2020-05-04 22:48:21 -04:00
|
|
|
provider = await waitForSync(config, currentBlockNumber)
|
2020-05-03 21:04:23 -04:00
|
|
|
}
|
2020-01-18 21:24:28 -05:00
|
|
|
return provider[(params.includeTransactions ? "getBlockWithTransactions": "getBlock")](params.blockTag || params.blockHash);
|
|
|
|
case "call":
|
|
|
|
case "estimateGas":
|
2020-05-03 21:04:23 -04:00
|
|
|
if (params.blockTag && isHexString(params.blockTag)) {
|
2020-05-04 22:48:21 -04:00
|
|
|
provider = await waitForSync(config, currentBlockNumber)
|
2020-05-03 21:04:23 -04:00
|
|
|
}
|
2022-07-13 20:58:23 -04:00
|
|
|
if (method === "call" && params.blockTag) {
|
|
|
|
return provider[method](params.transaction, params.blockTag);
|
|
|
|
}
|
2020-01-18 21:24:28 -05:00
|
|
|
return provider[method](params.transaction);
|
|
|
|
case "getTransaction":
|
|
|
|
case "getTransactionReceipt":
|
|
|
|
return provider[method](params.transactionHash);
|
2020-05-03 21:04:23 -04:00
|
|
|
case "getLogs": {
|
|
|
|
let filter = params.filter;
|
|
|
|
if ((filter.fromBlock && isHexString(filter.fromBlock)) || (filter.toBlock && isHexString(filter.toBlock))) {
|
2020-05-04 22:48:21 -04:00
|
|
|
provider = await waitForSync(config, currentBlockNumber)
|
2020-05-03 21:04:23 -04:00
|
|
|
}
|
|
|
|
return provider.getLogs(filter);
|
|
|
|
}
|
2020-01-18 21:24:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return logger.throwError("unknown method error", Logger.errors.UNKNOWN_ERROR, {
|
|
|
|
method: method,
|
|
|
|
params: params
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
export class FallbackProvider extends BaseProvider {
|
2020-02-04 08:01:26 -05:00
|
|
|
readonly providerConfigs: ReadonlyArray<FallbackProviderConfig>;
|
2019-05-14 18:25:46 -04:00
|
|
|
readonly quorum: number;
|
|
|
|
|
2020-05-03 21:04:23 -04:00
|
|
|
// Due to the highly asyncronous nature of the blockchain, we need
|
2020-01-18 21:24:28 -05:00
|
|
|
// to make sure we never unroll the blockNumber due to our random
|
|
|
|
// sample of backends
|
|
|
|
_highestBlockNumber: number;
|
|
|
|
|
|
|
|
constructor(providers: Array<Provider | FallbackProviderConfig>, quorum?: number) {
|
2019-05-14 18:25:46 -04:00
|
|
|
if (providers.length === 0) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("missing providers", "providers", providers);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
const providerConfigs: Array<FallbackProviderConfig> = providers.map((configOrProvider, index) => {
|
|
|
|
if (Provider.isProvider(configOrProvider)) {
|
2020-10-07 17:28:18 -04:00
|
|
|
const stallTimeout = isCommunityResource(configOrProvider) ? 2000: 750;
|
|
|
|
const priority = 1;
|
|
|
|
return Object.freeze({ provider: configOrProvider, weight: 1, stallTimeout, priority });
|
2020-01-18 21:24:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const config: FallbackProviderConfig = shallowCopy(configOrProvider);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
if (config.priority == null) { config.priority = 1; }
|
2020-10-07 17:28:18 -04:00
|
|
|
if (config.stallTimeout == null) {
|
|
|
|
config.stallTimeout = isCommunityResource(configOrProvider) ? 2000: 750;
|
|
|
|
}
|
2020-01-18 21:24:28 -05:00
|
|
|
if (config.weight == null) { config.weight = 1; }
|
|
|
|
|
|
|
|
const weight = config.weight;
|
|
|
|
if (weight % 1 || weight > 512 || weight < 1) {
|
|
|
|
logger.throwArgumentError("invalid weight; must be integer in [1, 512]", `providers[${ index }].weight`, weight);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.freeze(config);
|
|
|
|
});
|
|
|
|
|
|
|
|
const total = providerConfigs.reduce((accum, c) => (accum + c.weight), 0);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
if (quorum == null) {
|
|
|
|
quorum = total / 2;
|
2020-01-18 21:24:28 -05:00
|
|
|
} else if (quorum > total) {
|
|
|
|
logger.throwArgumentError("quorum will always fail; larger than total weight", "quorum", quorum);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:24:28 -04:00
|
|
|
// Are all providers' networks are known
|
|
|
|
let networkOrReady: Network | Promise<Network> = checkNetworks(providerConfigs.map((c) => (<any>(c.provider)).network));
|
|
|
|
|
|
|
|
// Not all networks are known; we must stall
|
|
|
|
if (networkOrReady == null) {
|
|
|
|
networkOrReady = new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.detectNetwork().then(resolve, reject);
|
|
|
|
}, 0);
|
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:24:28 -04:00
|
|
|
super(networkOrReady);
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// Preserve a copy, so we do not get mutated
|
2020-01-18 21:24:28 -05:00
|
|
|
defineReadOnly(this, "providerConfigs", Object.freeze(providerConfigs));
|
2019-05-14 18:25:46 -04:00
|
|
|
defineReadOnly(this, "quorum", quorum);
|
2020-01-18 21:24:28 -05:00
|
|
|
|
|
|
|
this._highestBlockNumber = -1;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:32:16 -04:00
|
|
|
async detectNetwork(): Promise<Network> {
|
|
|
|
const networks = await Promise.all(this.providerConfigs.map((c) => c.provider.getNetwork()));
|
|
|
|
return checkNetworks(networks);
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
async perform(method: string, params: { [name: string]: any }): Promise<any> {
|
|
|
|
// Sending transactions is special; always broadcast it to all backends
|
|
|
|
if (method === "sendTransaction") {
|
2020-05-03 16:15:19 -04:00
|
|
|
const results: Array<string | Error> = await Promise.all(this.providerConfigs.map((c) => {
|
2020-01-18 21:24:28 -05:00
|
|
|
return c.provider.sendTransaction(params.signedTransaction).then((result) => {
|
2019-08-22 16:51:35 -04:00
|
|
|
return result.hash;
|
2020-01-18 21:24:28 -05:00
|
|
|
}, (error) => {
|
|
|
|
return error;
|
2019-08-22 16:51:35 -04:00
|
|
|
});
|
2020-05-03 16:15:19 -04:00
|
|
|
}));
|
2020-01-18 21:24:28 -05:00
|
|
|
|
2020-05-03 16:15:19 -04:00
|
|
|
// Any success is good enough (other errors are likely "already seen" errors
|
|
|
|
for (let i = 0; i < results.length; i++) {
|
|
|
|
const result = results[i];
|
|
|
|
if (typeof(result) === "string") { return result; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// They were all an error; pick the first error
|
|
|
|
throw results[0];
|
2019-08-21 01:45:51 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 21:04:23 -04:00
|
|
|
// We need to make sure we are in sync with our backends, so we need
|
|
|
|
// to know this before we can make a lot of calls
|
|
|
|
if (this._highestBlockNumber === -1 && method !== "getBlockNumber") {
|
|
|
|
await this.getBlockNumber();
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
const processFunc = getProcessFunc(this, method, params);
|
|
|
|
|
|
|
|
// Shuffle the providers and then sort them by their priority; we
|
|
|
|
// shallowCopy them since we will store the result in them too
|
2020-05-03 16:15:19 -04:00
|
|
|
const configs: Array<RunningConfig> = shuffled(this.providerConfigs.map(shallowCopy));
|
2020-01-18 21:24:28 -05:00
|
|
|
configs.sort((a, b) => (a.priority - b.priority));
|
|
|
|
|
2020-05-03 21:04:23 -04:00
|
|
|
const currentBlockNumber = this._highestBlockNumber;
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
let i = 0;
|
2020-04-23 08:15:46 -04:00
|
|
|
let first = true;
|
2020-01-18 21:24:28 -05:00
|
|
|
while (true) {
|
|
|
|
const t0 = now();
|
|
|
|
|
|
|
|
// Compute the inflight weight (exclude anything past)
|
|
|
|
let inflightWeight = configs.filter((c) => (c.runner && ((t0 - c.start) < c.stallTimeout)))
|
|
|
|
.reduce((accum, c) => (accum + c.weight), 0);
|
|
|
|
|
|
|
|
// Start running enough to meet quorum
|
|
|
|
while (inflightWeight < this.quorum && i < configs.length) {
|
|
|
|
const config = configs[i++];
|
|
|
|
|
|
|
|
const rid = nextRid++;
|
|
|
|
|
|
|
|
config.start = now();
|
2020-05-03 16:15:19 -04:00
|
|
|
config.staller = stall(config.stallTimeout);
|
|
|
|
config.staller.wait(() => { config.staller = null; });
|
2020-01-18 21:24:28 -05:00
|
|
|
|
2020-05-04 22:48:21 -04:00
|
|
|
config.runner = getRunner(config, currentBlockNumber, method, params).then((result) => {
|
2020-01-18 21:24:28 -05:00
|
|
|
config.done = true;
|
|
|
|
config.result = result;
|
|
|
|
|
|
|
|
if (this.listenerCount("debug")) {
|
2019-08-01 16:13:35 -04:00
|
|
|
this.emit("debug", {
|
2020-01-18 21:24:28 -05:00
|
|
|
action: "request",
|
2019-08-01 16:13:35 -04:00
|
|
|
rid: rid,
|
2020-01-18 21:24:28 -05:00
|
|
|
backend: exposeDebugConfig(config, now()),
|
2019-08-01 16:13:35 -04:00
|
|
|
request: { method: method, params: deepCopy(params) },
|
2020-01-18 21:24:28 -05:00
|
|
|
provider: this
|
2019-08-01 16:13:35 -04:00
|
|
|
});
|
2020-01-18 21:24:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}, (error) => {
|
|
|
|
config.done = true;
|
|
|
|
config.error = error;
|
|
|
|
|
|
|
|
if (this.listenerCount("debug")) {
|
2019-08-01 16:13:35 -04:00
|
|
|
this.emit("debug", {
|
2020-01-18 21:24:28 -05:00
|
|
|
action: "request",
|
2019-08-01 16:13:35 -04:00
|
|
|
rid: rid,
|
2020-01-18 21:24:28 -05:00
|
|
|
backend: exposeDebugConfig(config, now()),
|
2019-08-01 16:13:35 -04:00
|
|
|
request: { method: method, params: deepCopy(params) },
|
2020-01-18 21:24:28 -05:00
|
|
|
provider: this
|
2019-08-01 16:13:35 -04:00
|
|
|
});
|
2020-01-18 21:24:28 -05:00
|
|
|
}
|
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
if (this.listenerCount("debug")) {
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "request",
|
|
|
|
rid: rid,
|
|
|
|
backend: exposeDebugConfig(config, null),
|
|
|
|
request: { method: method, params: deepCopy(params) },
|
|
|
|
provider: this
|
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
inflightWeight += config.weight;
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
// Wait for anything meaningful to finish or stall out
|
|
|
|
const waiting: Array<Promise<any>> = [ ];
|
|
|
|
configs.forEach((c) => {
|
|
|
|
if (c.done || !c.runner) { return; }
|
|
|
|
waiting.push(c.runner);
|
2020-05-03 16:15:19 -04:00
|
|
|
if (c.staller) { waiting.push(c.staller.getPromise()); }
|
2020-01-18 21:24:28 -05:00
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
if (waiting.length) { await Promise.race(waiting); }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
// Check the quorum and process the results; the process function
|
|
|
|
// may additionally decide the quorum is not met
|
|
|
|
const results = configs.filter((c) => (c.done && c.error == null));
|
|
|
|
if (results.length >= this.quorum) {
|
|
|
|
const result = processFunc(results);
|
2020-05-03 16:15:19 -04:00
|
|
|
if (result !== undefined) {
|
|
|
|
// Shut down any stallers
|
2020-05-04 22:48:21 -04:00
|
|
|
configs.forEach(c => {
|
|
|
|
if (c.staller) { c.staller.cancel(); }
|
|
|
|
c.cancelled = true;
|
|
|
|
});
|
2020-05-03 16:15:19 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (!first) { await stall(100).getPromise(); }
|
2020-04-23 08:15:46 -04:00
|
|
|
first = false;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 01:10:15 -04:00
|
|
|
// No result, check for errors that should be forwarded
|
|
|
|
const errors = configs.reduce((accum, c) => {
|
|
|
|
if (!c.done || c.error == null) { return accum; }
|
|
|
|
|
|
|
|
const code = (<any>(c.error)).code;
|
|
|
|
if (ForwardErrors.indexOf(code) >= 0) {
|
|
|
|
if (!accum[code]) { accum[code] = { error: c.error, weight: 0 }; }
|
|
|
|
accum[code].weight += c.weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return accum;
|
|
|
|
}, <{ [ code: string ]: { error: Error, weight: number } }>({ }));
|
|
|
|
|
|
|
|
Object.keys(errors).forEach((errorCode: string) => {
|
|
|
|
const tally = errors[errorCode];
|
|
|
|
if (tally.weight < this.quorum) { return; }
|
|
|
|
|
|
|
|
// Shut down any stallers
|
|
|
|
configs.forEach(c => {
|
|
|
|
if (c.staller) { c.staller.cancel(); }
|
|
|
|
c.cancelled = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const e = <any>(tally.error);
|
|
|
|
|
|
|
|
const props: { [ name: string ]: any } = { };
|
|
|
|
ForwardProperties.forEach((name) => {
|
|
|
|
if (e[name] == null) { return; }
|
|
|
|
props[name] = e[name];
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.throwError(e.reason || e.message, <any>errorCode, props);
|
|
|
|
});
|
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
// All configs have run to completion; we will never get more data
|
|
|
|
if (configs.filter((c) => !c.done).length === 0) { break; }
|
|
|
|
}
|
|
|
|
|
2020-05-03 16:15:19 -04:00
|
|
|
// Shut down any stallers; shouldn't be any
|
2020-05-04 22:48:21 -04:00
|
|
|
configs.forEach(c => {
|
|
|
|
if (c.staller) { c.staller.cancel(); }
|
|
|
|
c.cancelled = true;
|
|
|
|
});
|
2020-05-03 16:15:19 -04:00
|
|
|
|
2020-01-18 21:24:28 -05:00
|
|
|
return logger.throwError("failed to meet quorum", Logger.errors.SERVER_ERROR, {
|
|
|
|
method: method,
|
|
|
|
params: params,
|
2020-02-03 23:05:04 -05:00
|
|
|
//results: configs.map((c) => c.result),
|
2020-01-18 21:24:28 -05:00
|
|
|
//errors: configs.map((c) => c.error),
|
2020-02-03 23:05:04 -05:00
|
|
|
results: configs.map((c) => exposeDebugConfig(c)),
|
2020-01-18 21:24:28 -05:00
|
|
|
provider: this
|
2019-05-14 18:25:46 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|