All files / ethers.js/src.ts/providers provider-fallback.ts

95.25% Statements 763/801
89% Branches 178/200
95.83% Functions 23/24
95.25% Lines 763/801

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 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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 8021x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 309x 309x 620x 620x 620x 620x 620x 309x 1x 290x 290x 290x 1x 580x 1x 584x 584x 3302x 59x 59x 3243x 584x 584x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x         126x 126x 126x 126x 126x 126x 126x 126x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6106x 6106x 5710x 6106x 142x 142x 5568x 6106x 104x 104x 5464x 5464x 6106x   6106x 577x 6106x 4548x 6106x 339x 339x 339x 339x 6106x         1x 444x 444x 444x 80x 80x 80x 80x 44x 44x 44x 80x 36x 36x 80x 80x 80x 364x 364x 364x 364x 1x 1x 1x 1x 1x 1x 1x 1x 1x 131x 131x 131x 248x 248x 248x 248x 131x 131x 131x 143x 107x 107x 143x 131x 131x 24x 24x 24x 1x 30x 30x 30x 30x 30x 30x 30x 30x 68x 25x 25x 25x 25x 25x 68x 43x 43x 43x 68x 30x 30x 12x 12x 5x 5x 5x 5x 18x 18x 18x 18x 18x 18x 18x 30x 13x 13x 13x 13x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x         1x 15x 15x 15x 15x 15x 90x 90x 90x 15x 15x 15x 30x 30x 30x 30x 30x 15x 15x 15x 15x 15x 45x 45x 45x 45x 45x 45x 45x 45x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 126x 124x 126x 2x 2x 42x 42x 42x 42x 42x 42x 1x 42x 41x 124x 124x 41x 41x 42x 42x 42x 42x 42x 42x 42x 42x 42x                 42x 42x 58x 58x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 290x 290x   290x 22x 290x 115x 290x 26x 290x 5x 290x 22x 22x 22x 290x 35x 290x 4x 290x 10x 290x 9x 290x   290x 3x 290x 8x 290x 12x 290x 19x 290x   290x 290x 42x 42x 42x 42x 309x 309x 309x 309x 309x 309x 309x 309x 309x 309x 309x 309x 414x 414x 414x 19x 19x 19x 42x 42x 42x 309x 309x 309x 309x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 256x 290x 34x 34x 34x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 42x 42x 42x 42x 137x 137x 42x 42x 126x 126x 126x 126x 126x 126x 42x 42x 42x 42x 42x 42x 42x 42x 42x 126x 126x 126x 42x 126x         126x 42x 42x 137x 137x 137x 42x 42x 42x 407x 407x 407x 847x 422x 422x 422x 847x 407x 407x 407x 257x 257x 150x 150x 407x 15x 15x 6x 19x 19x 19x 6x 6x 15x 15x 15x 15x 15x 15x 15x 15x 407x 407x 407x 407x 24x 407x 407x 16x 16x 16x     16x 407x 407x 407x 407x 407x 407x 407x 407x 407x 407x 95x 407x 407x   407x           42x 42x 407x 407x 407x 407x 407x 407x 407x 407x 847x 847x 847x 425x 425x 847x 847x 847x 753x 753x 753x 94x 94x 847x 37x 37x 37x 37x 37x 407x 407x 407x 407x 136x 122x 122x 271x 271x 271x 407x 37x 37x 271x 271x 271x 271x 271x 271x 271x 271x 271x 271x 270x 270x 270x 270x 270x 42x 42x 144x 144x 144x 144x 7x 7x 7x 7x 22x 22x 8x 22x 14x 14x 7x 7x 7x 7x 22x 22x 22x 26x 26x 18x 18x 18x 18x 18x 18x 18x 18x 22x 1x 1x 17x 17x 17x 22x 15x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x     137x 137x 137x 137x 137x 137x 144x 272x 272x 272x 272x 272x 137x 137x 122x 122x 122x 144x 258x 13x 13x 258x 122x 122x 122x 42x 42x 286x 858x 858x 286x 286x 42x  
/**
 *  A **FallbackProvider** provides resilience, security and performance
 *  in a way that is customizable and configurable.
 *
 *  @_section: api/providers/fallback-provider:Fallback Provider [about-fallback-provider]
 */
import {
    assert, assertArgument, getBigInt, getNumber, isError
} from "../utils/index.js";
 
import { AbstractProvider } from "./abstract-provider.js";
import { Network } from "./network.js"
 
import type { PerformActionRequest } from "./abstract-provider.js";
import type { Networkish } from "./network.js"
 
const BN_1 = BigInt("1");
const BN_2 = BigInt("2");
 
function shuffle<T = any>(array: Array<T>): void {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        const tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
}
 
function stall(duration: number): Promise<void> {
    return new Promise((resolve) => { setTimeout(resolve, duration); });
}
 
function getTime(): number { return (new Date()).getTime(); }
 
function stringify(value: any): string {
    return JSON.stringify(value, (key, value) => {
        if (typeof(value) === "bigint") {
            return { type: "bigint", value: value.toString() };
        }
        return value;
    });
}
 
/**
 *  A configuration entry for how to use a [[Provider]].
 */
export interface FallbackProviderConfig {
 
    /**
     *  The provider.
     */
    provider: AbstractProvider;
 
    /**
     *  The amount of time to wait before kicking off the next provider.
     *
     *  Any providers that have not responded can still respond and be
     *  counted, but this ensures new providers start.
     */
    stallTimeout?: number;
 
    /**
     *  The priority. Lower priority providers are dispatched first.
     */
    priority?: number;
 
    /**
     *  The amount of weight a provider is given against the quorum.
     */
    weight?: number;
};
 
const defaultConfig = { stallTimeout: 400, priority: 1, weight: 1 };
 
// We track a bunch of extra stuff that might help debug problems or
// optimize infrastructure later on.
/**
 *  The statistics and state maintained for a [[Provider]].
 */
export interface FallbackProviderState extends Required<FallbackProviderConfig> {
 
    /**
     *  The most recent blockNumber this provider has reported (-2 if none).
     */
    blockNumber: number;
 
    /**
     *  The number of total requests ever sent to this provider.
     */
    requests: number;
 
    /**
     *  The number of responses that errored.
     */
    errorResponses: number;
 
    /**
     *  The number of responses that occured after the result resolved.
     */
    lateResponses: number;
 
    /**
     *  How many times syncing was required to catch up the expected block.
     */
    outOfSync: number;
 
    /**
     *  The number of requests which reported unsupported operation.
     */
    unsupportedEvents: number;
 
    /**
     *  A rolling average (5% current duration) for response time.
     */
    rollingDuration: number;
 
    /**
     *  The ratio of quorum-agreed results to total.
     */
    score: number;
}
 
interface Config extends FallbackProviderState {
    _updateNumber: null | Promise<any>;
    _network: null | Network;
    _totalTime: number;
    _lastFatalError: null | Error;
    _lastFatalErrorTimestamp: number;
}
 
const defaultState = {
    blockNumber: -2, requests: 0, lateResponses: 0, errorResponses: 0,
    outOfSync: -1, unsupportedEvents: 0, rollingDuration: 0, score: 0,
    _network: null, _updateNumber: null, _totalTime: 0,
    _lastFatalError: null, _lastFatalErrorTimestamp: 0
};
 
 
async function waitForSync(config: Config, blockNumber: number): Promise<void> {
    while (config.blockNumber < 0 || config.blockNumber < blockNumber) {
        if (!config._updateNumber) {
            config._updateNumber = (async () => {
                try {
                    const blockNumber = await config.provider.getBlockNumber();
                    if (blockNumber > config.blockNumber) {
                        config.blockNumber = blockNumber;
                    }
                } catch (error: any) {
                    config.blockNumber = -2;
                    config._lastFatalError = error;
                    config._lastFatalErrorTimestamp = getTime();
                }
                config._updateNumber = null;
            })();
        }
        await config._updateNumber;
        config.outOfSync++;
        if (config._lastFatalError) { break; }
    }
}
 
/**
 *  Additional options to configure a [[FallbackProvider]].
 */
export type FallbackProviderOptions = {
    // How many providers must agree on a value before reporting
    // back the response
    quorum?: number;
 
    // How many providers must have reported the same event
    // for it to be emitted (currently unimplmented)
    eventQuorum?: number;
 
    // How many providers to dispatch each event to simultaneously.
    // Set this to 0 to use getLog polling, which implies eventQuorum
    // is equal to quorum. (currently unimplemented)
    eventWorkers?: number;
 
    cacheTimeout?: number;
 
    pollingInterval?: number;
};
 
type RunnerResult = { result: any } | { error: Error };
 
type RunnerState = {
    config: Config;
    staller: null | Promise<void>;
    didBump: boolean;
    perform: null | Promise<any>;
    result: null | RunnerResult;
}
 
function _normalize(value: any): string {
    if (value == null) { return "null"; }
 
    if (Array.isArray(value)) {
        return "[" + (value.map(_normalize)).join(",") + "]";
    }
 
    if (typeof(value) === "object" && typeof(value.toJSON) === "function") {
        return _normalize(value.toJSON());
    }
 
    switch (typeof(value)) {
        case "boolean": case "symbol":
            return value.toString();
        case "bigint": case "number":
            return BigInt(value).toString();
        case "string":
            return JSON.stringify(value);
        case "object": {
            const keys = Object.keys(value);
            keys.sort();
            return "{" + keys.map((k) => `${ JSON.stringify(k) }:${ _normalize(value[k]) }`).join(",") + "}";
        }
    }

    console.log("Could not serialize", value);
    throw new Error("Hmm...");
}
 
function normalizeResult(method: string, value: RunnerResult): { tag: string, value: any } {
 
    if ("error" in value) {
        const error = value.error;
 
        let tag: string;
        if (isError(error, "CALL_EXCEPTION")) {
            tag = _normalize(Object.assign({ }, error, {
                shortMessage: undefined, reason: undefined, info: undefined
            }));
        } else {
            tag = _normalize(error)
        }
 
        return { tag, value: error };
    }
 
    const result = value.result;
    return { tag: _normalize(result), value: result };
}
 
type TallyResult = {
    tag: string;
    value: any;
    weight: number;
};
 
// This strategy picks the highest weight result, as long as the weight is
// equal to or greater than quorum
function checkQuorum(quorum: number, results: Array<TallyResult>): any | Error {
    const tally: Map<string, { value: any, weight: number }> = new Map();
    for (const { value, tag, weight } of results) {
        const t = tally.get(tag) || { value, weight: 0 };
        t.weight += weight;
        tally.set(tag, t);
    }
 
    let best: null | { value: any, weight: number } = null;
    for (const r of tally.values()) {
        if (r.weight >= quorum && (!best || r.weight > best.weight)) {
            best = r;
        }
    }
 
    if (best) { return best.value; }
 
    return undefined;
}
 
function getMedian(quorum: number, results: Array<TallyResult>): undefined | bigint | Error {
    let resultWeight = 0;
 
    const errorMap: Map<string, { weight: number, value: Error }> = new Map();
    let bestError: null | { weight: number, value: Error } = null;
 
    const values: Array<bigint> = [ ];
    for (const { value, tag, weight } of results) {
        if (value instanceof Error) {
            const e = errorMap.get(tag) || { value, weight: 0 };
            e.weight += weight;
            errorMap.set(tag, e);
 
            if (bestError == null || e.weight > bestError.weight) { bestError = e; }
        } else {
            values.push(BigInt(value));
            resultWeight += weight;
        }
    }
 
    if (resultWeight < quorum) {
        // We have quorum for an error
        if (bestError && bestError.weight >= quorum) { return bestError.value; }
 
        // We do not have quorum for a result
        return undefined;
    }
 
    // Get the sorted values
    values.sort((a, b) => ((a < b) ? -1: (b > a) ? 1: 0));
 
    const mid = Math.floor(values.length / 2);
 
    // Odd-length; take the middle value
    if (values.length % 2) { return values[mid]; }
 
    // Even length; take the ceiling of the mean of the center two values
    return (values[mid - 1] + values[mid] + BN_1) / BN_2;
}
 
function getAnyResult(quorum: number, results: Array<TallyResult>): undefined | any | Error {
    // If any value or error meets quorum, that is our preferred result
    const result = checkQuorum(quorum, results);
    if (result !== undefined) { return result; }
 
    // Otherwise, do we have any result?
    for (const r of results) {
        if (r.value) { return r.value; }
    }

    // Nope!
    return undefined;
}
 
function getFuzzyMode(quorum: number, results: Array<TallyResult>): undefined | number {
    if (quorum === 1) { return getNumber(<bigint>getMedian(quorum, results), "%internal"); }
 
    const tally: Map<number, { result: number, weight: number }> = new Map();
    const add = (result: number, weight: number) => {
        const t = tally.get(result) || { result, weight: 0 };
        t.weight += weight;
        tally.set(result, t);
    };
 
    for (const { weight, value } of results) {
        const r = getNumber(value);
        add(r - 1, weight);
        add(r, weight);
        add(r + 1, weight);
    }
 
    let bestWeight = 0;
    let bestResult: undefined | number = undefined;
 
    for (const { weight, result } of tally.values()) {
        // Use this result, if this result meets quorum and has either:
        // - a better weight
        // - or equal weight, but the result is larger
        if (weight >= quorum && (weight > bestWeight || (bestResult != null && weight === bestWeight && result > bestResult))) {
            bestWeight = weight;
            bestResult = result;
        }
    }
 
    return bestResult;
}
 
/**
 *  A **FallbackProvider** manages several [[Providers]] providing
 *  resilience by switching between slow or misbehaving nodes, security
 *  by requiring multiple backends to aggree and performance by allowing
 *  faster backends to respond earlier.
 *
 */
export class FallbackProvider extends AbstractProvider {
 
    /**
     *  The number of backends that must agree on a value before it is
     *  accpeted.
     */
    readonly quorum: number;
 
    /**
     *  @_ignore:
     */
    readonly eventQuorum: number;
 
    /**
     *  @_ignore:
     */
    readonly eventWorkers: number;
 
    readonly #configs: Array<Config>;
 
    #height: number;
    #initialSyncPromise: null | Promise<void>;
 
    /**
     *  Creates a new **FallbackProvider** with %%providers%% connected to
     *  %%network%%.
     *
     *  If a [[Provider]] is included in %%providers%%, defaults are used
     *  for the configuration.
     */
    constructor(providers: Array<AbstractProvider | FallbackProviderConfig>, network?: Networkish, options?: FallbackProviderOptions) {
        super(network, options);
 
        this.#configs = providers.map((p) => {
            if (p instanceof AbstractProvider) {
                return Object.assign({ provider: p }, defaultConfig, defaultState );
            } else {
                return Object.assign({ }, defaultConfig, p, defaultState );
            }
        });
 
        this.#height = -2;
        this.#initialSyncPromise = null;
 
        if (options && options.quorum != null) {
            this.quorum = options.quorum;
        } else {
            this.quorum = Math.ceil(this.#configs.reduce((accum, config) => {
                accum += config.weight;
                return accum;
            }, 0) / 2);
        }
 
        this.eventQuorum = 1;
        this.eventWorkers = 1;
 
        assertArgument(this.quorum <= this.#configs.reduce((a, c) => (a + c.weight), 0),
            "quorum exceed provider weight", "quorum", this.quorum);
    }
 
    get providerConfigs(): Array<FallbackProviderState> {
        return this.#configs.map((c) => {
            const result: any = Object.assign({ }, c);
            for (const key in result) {
                if (key[0] === "_") { delete result[key]; }
            }
            return result;
        });
    }
 
    async _detectNetwork(): Promise<Network> {
        return Network.from(getBigInt(await this._perform({ method: "chainId" })));
    }
 
    // @TODO: Add support to select providers to be the event subscriber
    //_getSubscriber(sub: Subscription): Subscriber {
    //    throw new Error("@TODO");
    //}
 
    /**
     *  Transforms a %%req%% into the correct method call on %%provider%%.
     */
    async _translatePerform(provider: AbstractProvider, req: PerformActionRequest): Promise<any> {
        switch (req.method) {
            case "broadcastTransaction":
                return await provider.broadcastTransaction(req.signedTransaction);
            case "call":
                return await provider.call(Object.assign({ }, req.transaction, { blockTag: req.blockTag }));
            case "chainId":
                return (await provider.getNetwork()).chainId;
            case "estimateGas":
                return await provider.estimateGas(req.transaction);
            case "getBalance":
                return await provider.getBalance(req.address, req.blockTag);
            case "getBlock": {
                const block = ("blockHash" in req) ? req.blockHash: req.blockTag;
                return await provider.getBlock(block, req.includeTransactions);
            }
            case "getBlockNumber":
                return await provider.getBlockNumber();
            case "getCode":
                return await provider.getCode(req.address, req.blockTag);
            case "getGasPrice":
                return (await provider.getFeeData()).gasPrice;
            case "getPriorityFee":
                return (await provider.getFeeData()).maxPriorityFeePerGas;
            case "getLogs":
                return await provider.getLogs(req.filter);
            case "getStorage":
                return await provider.getStorage(req.address, req.position, req.blockTag);
            case "getTransaction":
                return await provider.getTransaction(req.hash);
            case "getTransactionCount":
                return await provider.getTransactionCount(req.address, req.blockTag);
            case "getTransactionReceipt":
                return await provider.getTransactionReceipt(req.hash);
            case "getTransactionResult":
                return await provider.getTransactionResult(req.hash);
        }
    }
 
    // Grab the next (random) config that is not already part of
    // the running set
    #getNextConfig(running: Set<RunnerState>): null | Config {
        // @TODO: Maybe do a check here to favour (heavily) providers that
        //        do not require waitForSync and disfavour providers that
        //        seem down-ish or are behaving slowly
 
        const configs = Array.from(running).map((r) => r.config)
 
        // Shuffle the states, sorted by priority
        const allConfigs = this.#configs.slice();
        shuffle(allConfigs);
        allConfigs.sort((a, b) => (a.priority - b.priority));
 
        for (const config of allConfigs) {
            if (config._lastFatalError) { continue; }
            if (configs.indexOf(config) === -1) { return config; }
        }
 
        return null;
    }
 
    // Adds a new runner (if available) to running.
    #addRunner(running: Set<RunnerState>, req: PerformActionRequest): null | RunnerState {
        const config = this.#getNextConfig(running);
 
        // No runners available
        if (config == null) { return null; }
 
        // Create a new runner
        const runner: RunnerState = {
            config, result: null, didBump: false,
            perform: null, staller: null
        };
 
        const now = getTime();
 
        // Start performing this operation
        runner.perform = (async () => {
            try {
                config.requests++;
                const result = await this._translatePerform(config.provider, req);
                runner.result = { result };
            } catch (error: any) {
                config.errorResponses++;
                runner.result = { error };
            }
 
            const dt = (getTime() - now);
            config._totalTime += dt;
 
            config.rollingDuration = 0.95 * config.rollingDuration + 0.05 * dt;
 
            runner.perform = null;
        })();
 
        // Start a staller; when this times out, it's time to force
        // kicking off another runner because we are taking too long
        runner.staller = (async () => {
            await stall(config.stallTimeout);
            runner.staller = null;
        })();
 
        running.add(runner);
        return runner;
    }
 
    // Initializes the blockNumber and network for each runner and
    // blocks until initialized
    async #initialSync(): Promise<void> {
        let initialSync = this.#initialSyncPromise;
        if (!initialSync) {
            const promises: Array<Promise<any>> = [ ];
            this.#configs.forEach((config) => {
                promises.push((async () => {
                    await waitForSync(config, 0);
                    if (!config._lastFatalError) {
                        config._network = await config.provider.getNetwork();
                    }
                })());
            });
 
            this.#initialSyncPromise = initialSync = (async () => {
                // Wait for all providers to have a block number and network
                await Promise.all(promises);
 
                // Check all the networks match
                let chainId: null | bigint = null;
                for (const config of this.#configs) {
                    if (config._lastFatalError) { continue; }
                    const network = <Network>(config._network);
                    if (chainId == null) {
                        chainId = network.chainId;
                    } else if (network.chainId !== chainId) {
                        assert(false, "cannot mix providers on different networks", "UNSUPPORTED_OPERATION", {
                            operation: "new FallbackProvider"
                        });
                    }
                }
            })();
        }
 
        await initialSync
    }
 
 
    async #checkQuorum(running: Set<RunnerState>, req: PerformActionRequest): Promise<any> {
        // Get all the result objects
        const results: Array<TallyResult> = [ ];
        for (const runner of running) {
            if (runner.result != null) {
                const { tag, value } = normalizeResult(req.method, runner.result);
                results.push({ tag, value, weight: runner.config.weight });
            }
        }
 
        // Are there enough results to event meet quorum?
        if (results.reduce((a, r) => (a + r.weight), 0) < this.quorum) {
            return undefined;
        }
 
        switch (req.method) {
            case "getBlockNumber": {
                // We need to get the bootstrap block height
                if (this.#height === -2) {
                    this.#height = Math.ceil(getNumber(<bigint>getMedian(this.quorum, this.#configs.filter((c) => (!c._lastFatalError)).map((c) => ({
                        value: c.blockNumber,
                        tag: getNumber(c.blockNumber).toString(),
                        weight: c.weight
                    })))));
                }
 
                // Find the mode across all the providers, allowing for
                // a little drift between block heights
                const mode = getFuzzyMode(this.quorum, results);
                if (mode === undefined) { return undefined; }
                if (mode > this.#height) { this.#height = mode; }
                return this.#height;
            }
 
            case "getGasPrice":
            case "getPriorityFee":
            case "estimateGas":
                return getMedian(this.quorum, results);
 
            case "getBlock":
                // Pending blocks are in the mempool and already
                // quite untrustworthy; just grab anything
                if ("blockTag" in req && req.blockTag === "pending") {
                    return getAnyResult(this.quorum, results);
                }
                return checkQuorum(this.quorum, results);
 
            case "call":
            case "chainId":
            case "getBalance":
            case "getTransactionCount":
            case "getCode":
            case "getStorage":
            case "getTransaction":
            case "getTransactionReceipt":
            case "getLogs":
                return checkQuorum(this.quorum, results);
 
            case "broadcastTransaction":
                return getAnyResult(this.quorum, results);
        }

        assert(false, "unsupported method", "UNSUPPORTED_OPERATION", {
            operation: `_perform(${ stringify((<any>req).method) })`
        });
    }
 
    async #waitForQuorum(running: Set<RunnerState>, req: PerformActionRequest): Promise<any> {
        if (running.size === 0) { throw new Error("no runners?!"); }
 
        // Any promises that are interesting to watch for; an expired stall
        // or a successful perform
        const interesting: Array<Promise<void>> = [ ];
 
        let newRunners = 0;
        for (const runner of running) {
 
            // No responses, yet; keep an eye on it
            if (runner.perform) {
                interesting.push(runner.perform);
            }
 
            // Still stalling...
            if (runner.staller) {
                interesting.push(runner.staller);
                continue;
            }
 
            // This runner has already triggered another runner
            if (runner.didBump) { continue; }
 
            // Got a response (result or error) or stalled; kick off another runner
            runner.didBump = true;
            newRunners++;
        }
 
        // Check if we have reached quorum on a result (or error)
        const value = await this.#checkQuorum(running, req);
        if (value !== undefined) {
            if (value instanceof Error) { throw value; }
            return value;
        }
 
        // Add any new runners, because a staller timed out or a result
        // or error response came in.
        for (let i = 0; i < newRunners; i++) {
            this.#addRunner(running, req);
        }
 
        // All providers have returned, and we have no result
 
        assert(interesting.length > 0, "quorum not met", "SERVER_ERROR", {
            request: "%sub-requests",
            info: { request: req, results: Array.from(running).map((r) => stringify(r.result)) }
        });
 
        // Wait for someone to either complete its perform or stall out
        await Promise.race(interesting);
 
        // This is recursive, but at worst case the depth is 2x the
        // number of providers (each has a perform and a staller)
        return await this.#waitForQuorum(running, req);
    }
 
    async _perform<T = any>(req: PerformActionRequest): Promise<T> {
        // Broadcasting a transaction is rare (ish) and already incurs
        // a cost on the user, so spamming is safe-ish. Just send it to
        // every backend.
        if (req.method === "broadcastTransaction") {
            // Once any broadcast provides a positive result, use it. No
            // need to wait for anyone else
            const results: Array<null | TallyResult> = this.#configs.map((c) => null);
            const broadcasts = this.#configs.map(async ({ provider, weight }, index) => {
                try {
                    const result = await provider._perform(req);
                    results[index] = Object.assign(normalizeResult(req.method, { result }), { weight });
                } catch (error: any) {
                    results[index] = Object.assign(normalizeResult(req.method, { error }), { weight });
                }
            });
 
            // As each promise finishes...
            while (true) {
                // Check for a valid broadcast result
                const done = <Array<any>>results.filter((r) => (r != null));
                for (const { value } of done) {
                    if (!(value instanceof Error)) { return value; }
                }
 
                // Check for a legit broadcast error (one which we cannot
                // recover from; some nodes may return the following red
                // herring events:
                // - alredy seend (UNKNOWN_ERROR)
                // - NONCE_EXPIRED
                // - REPLACEMENT_UNDERPRICED
                const result = checkQuorum(this.quorum, <Array<any>>results.filter((r) => (r != null)));
                if (isError(result, "INSUFFICIENT_FUNDS")) {
                    throw result;
                }
 
                // Kick off the next provider (if any)
                const waiting = broadcasts.filter((b, i) => (results[i] == null));
                if (waiting.length === 0) { break; }
                await Promise.race(waiting);
            }
 
            // Use standard quorum results; any result was returned above,
            // so this will find any error that met quorum if any
            const result = getAnyResult(this.quorum, <Array<any>>results);
            assert(result !== undefined, "problem multi-broadcasting", "SERVER_ERROR", {
                request: "%sub-requests",
                info: { request: req, results: results.map(stringify) }
            })
            if (result instanceof Error) { throw result; }
            return result;
        }
 
        await this.#initialSync();
 
        // Bootstrap enough runners to meet quorum
        const running: Set<RunnerState> = new Set();
        let inflightQuorum = 0;
        while (true) {
            const runner = this.#addRunner(running, req);
            if (runner == null) { break; }
            inflightQuorum += runner.config.weight;
            if (inflightQuorum >= this.quorum) { break; }
        }
 
        const result = await this.#waitForQuorum(running, req);
 
        // Track requests sent to a provider that are still
        // outstanding after quorum has been otherwise found
        for (const runner of running) {
            if (runner.perform && runner.result == null) {
                runner.config.lateResponses++;
            }
        }
 
        return result;
    }
 
    async destroy(): Promise<void> {
        for (const { provider } of this.#configs) {
            provider.destroy();
        }
        super.destroy();
    }
}