-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
fallback-provider.ts
654 lines (542 loc) · 23.5 KB
/
fallback-provider.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
"use strict";
import { Block, BlockWithTransactions, Provider } from "@ethersproject/abstract-provider";
import { BigNumber } from "@ethersproject/bignumber";
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";
import { isCommunityResource } from "./formatter";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
function now() { return (new Date()).getTime(); }
// 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;
for (let i = 0; i < networks.length; i++) {
const network = networks[i];
// Null! We do not know our network; bail.
if (network == null) { return null; }
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)))) {
logger.throwArgumentError("provider mismatch", "networks", networks);
}
} else {
result = network;
}
}
return result;
}
function median(values: Array<number>, maxDelta?: number): number {
values = values.slice().sort();
const middle = Math.floor(values.length / 2);
// 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];
if (maxDelta != null && Math.abs(a - b) > maxDelta) {
return null;
}
return (a + b) / 2;
}
function serialize(value: any): string {
if (value === null) {
return "null";
} 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);
keys.sort();
return "{" + keys.map((key) => {
let v = value[key];
if (typeof(v) === "function") {
v = "[function]";
} else {
v = serialize(v);
}
return JSON.stringify(key) + ":" + v;
}).join(",") + "}";
}
throw new Error("unknown value type: " + typeof(value));
}
// Next request ID to use for emitting debug info
let nextRid = 1;
export interface FallbackProviderConfig {
// The Provider
provider: Provider;
// The priority to favour this Provider; lower values are used first (higher priority)
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
// it will be incorporated into the vote
// - lower values will cause more network traffic but may result in a
// faster result.
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;
};
// 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 };
}
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",
];
// @TODO: Make this an object with staller and cancel built-in
interface RunningConfig extends FallbackProviderConfig {
start?: number;
done?: boolean;
cancelled?: boolean;
runner?: Promise<any>;
staller?: Staller;
result?: any;
error?: Error;
};
function exposeDebugConfig(config: RunningConfig, now?: number): any {
const result: any = {
weight: config.weight
};
Object.defineProperty(result, "provider", { get: () => config.provider });
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
let blockNumber = median(configs.map((c) => c.result), 2);
if (blockNumber == null) { return undefined; }
blockNumber = Math.ceil(blockNumber);
// 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 {
if (tx == null) { return null; }
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 {
if (block == null) { return null; }
block = shallowCopy(block);
block.transactions = block.transactions.map((tx) => {
tx = shallowCopy(tx);
tx.confirmations = -1;
return tx;
});
return serialize(block);
};
} else {
normalize = function(block: Block): string {
if (block == null) { return null; }
return serialize(block);
}
}
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);
}
// 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.
async function waitForSync(config: RunningConfig, blockNumber: number): Promise<BaseProvider> {
const provider = <BaseProvider>(config.provider);
if ((provider.blockNumber != null && provider.blockNumber >= blockNumber) || blockNumber === -1) {
return provider;
}
return poll(() => {
return new Promise((resolve, reject) => {
setTimeout(function() {
// We are synced
if (provider.blockNumber >= blockNumber) { return resolve(provider); }
// We're done; just quit
if (config.cancelled) { return resolve(null); }
// Try again, next block
return resolve(undefined);
}, 0);
});
}, { oncePoll: provider });
}
async function getRunner(config: RunningConfig, currentBlockNumber: number, method: string, params: { [ key: string]: any }): Promise<any> {
let provider = config.provider;
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":
if (params.blockTag && isHexString(params.blockTag)) {
provider = await waitForSync(config, currentBlockNumber)
}
return provider[method](params.address, params.blockTag || "latest");
case "getStorageAt":
if (params.blockTag && isHexString(params.blockTag)) {
provider = await waitForSync(config, currentBlockNumber)
}
return provider.getStorageAt(params.address, params.position, params.blockTag || "latest");
case "getBlock":
if (params.blockTag && isHexString(params.blockTag)) {
provider = await waitForSync(config, currentBlockNumber)
}
return provider[(params.includeTransactions ? "getBlockWithTransactions": "getBlock")](params.blockTag || params.blockHash);
case "call":
case "estimateGas":
if (params.blockTag && isHexString(params.blockTag)) {
provider = await waitForSync(config, currentBlockNumber)
}
if (method === "call" && params.blockTag) {
return provider[method](params.transaction, params.blockTag);
}
return provider[method](params.transaction);
case "getTransaction":
case "getTransactionReceipt":
return provider[method](params.transactionHash);
case "getLogs": {
let filter = params.filter;
if ((filter.fromBlock && isHexString(filter.fromBlock)) || (filter.toBlock && isHexString(filter.toBlock))) {
provider = await waitForSync(config, currentBlockNumber)
}
return provider.getLogs(filter);
}
}
return logger.throwError("unknown method error", Logger.errors.UNKNOWN_ERROR, {
method: method,
params: params
});
}
export class FallbackProvider extends BaseProvider {
readonly providerConfigs: ReadonlyArray<FallbackProviderConfig>;
readonly quorum: number;
// Due to the highly asyncronous nature of the blockchain, we need
// to make sure we never unroll the blockNumber due to our random
// sample of backends
_highestBlockNumber: number;
constructor(providers: Array<Provider | FallbackProviderConfig>, quorum?: number) {
if (providers.length === 0) {
logger.throwArgumentError("missing providers", "providers", providers);
}
const providerConfigs: Array<FallbackProviderConfig> = providers.map((configOrProvider, index) => {
if (Provider.isProvider(configOrProvider)) {
const stallTimeout = isCommunityResource(configOrProvider) ? 2000: 750;
const priority = 1;
return Object.freeze({ provider: configOrProvider, weight: 1, stallTimeout, priority });
}
const config: FallbackProviderConfig = shallowCopy(configOrProvider);
if (config.priority == null) { config.priority = 1; }
if (config.stallTimeout == null) {
config.stallTimeout = isCommunityResource(configOrProvider) ? 2000: 750;
}
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);
if (quorum == null) {
quorum = total / 2;
} else if (quorum > total) {
logger.throwArgumentError("quorum will always fail; larger than total weight", "quorum", quorum);
}
// 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);
});
}
super(networkOrReady);
// Preserve a copy, so we do not get mutated
defineReadOnly(this, "providerConfigs", Object.freeze(providerConfigs));
defineReadOnly(this, "quorum", quorum);
this._highestBlockNumber = -1;
}
async detectNetwork(): Promise<Network> {
const networks = await Promise.all(this.providerConfigs.map((c) => c.provider.getNetwork()));
return checkNetworks(networks);
}
async perform(method: string, params: { [name: string]: any }): Promise<any> {
// Sending transactions is special; always broadcast it to all backends
if (method === "sendTransaction") {
const results: Array<string | Error> = await Promise.all(this.providerConfigs.map((c) => {
return c.provider.sendTransaction(params.signedTransaction).then((result) => {
return result.hash;
}, (error) => {
return error;
});
}));
// 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];
}
// 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();
}
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
const configs: Array<RunningConfig> = shuffled(this.providerConfigs.map(shallowCopy));
configs.sort((a, b) => (a.priority - b.priority));
const currentBlockNumber = this._highestBlockNumber;
let i = 0;
let first = true;
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();
config.staller = stall(config.stallTimeout);
config.staller.wait(() => { config.staller = null; });
config.runner = getRunner(config, currentBlockNumber, method, params).then((result) => {
config.done = true;
config.result = result;
if (this.listenerCount("debug")) {
this.emit("debug", {
action: "request",
rid: rid,
backend: exposeDebugConfig(config, now()),
request: { method: method, params: deepCopy(params) },
provider: this
});
}
}, (error) => {
config.done = true;
config.error = error;
if (this.listenerCount("debug")) {
this.emit("debug", {
action: "request",
rid: rid,
backend: exposeDebugConfig(config, now()),
request: { method: method, params: deepCopy(params) },
provider: this
});
}
});
if (this.listenerCount("debug")) {
this.emit("debug", {
action: "request",
rid: rid,
backend: exposeDebugConfig(config, null),
request: { method: method, params: deepCopy(params) },
provider: this
});
}
inflightWeight += config.weight;
}
// 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);
if (c.staller) { waiting.push(c.staller.getPromise()); }
});
if (waiting.length) { await Promise.race(waiting); }
// 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);
if (result !== undefined) {
// Shut down any stallers
configs.forEach(c => {
if (c.staller) { c.staller.cancel(); }
c.cancelled = true;
});
return result;
}
if (!first) { await stall(100).getPromise(); }
first = false;
}
// 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);
});
// All configs have run to completion; we will never get more data
if (configs.filter((c) => !c.done).length === 0) { break; }
}
// Shut down any stallers; shouldn't be any
configs.forEach(c => {
if (c.staller) { c.staller.cancel(); }
c.cancelled = true;
});
return logger.throwError("failed to meet quorum", Logger.errors.SERVER_ERROR, {
method: method,
params: params,
//results: configs.map((c) => c.result),
//errors: configs.map((c) => c.error),
results: configs.map((c) => exposeDebugConfig(c)),
provider: this
});
}
}