From 84296c76686792f5636bedd0dcee1620284ea09b Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sun, 3 Sep 2023 12:59:33 +0200 Subject: [PATCH] Make drandEndpoints configurable --- config.example.json | 7 ++++++- config.ts | 1 + deps.ts | 7 ++++++- drand.ts | 20 +++++++++++++------- main.ts | 33 ++++++++++++++++++++++++++++----- submitter.ts | 4 ++-- 6 files changed, 56 insertions(+), 16 deletions(-) diff --git a/config.example.json b/config.example.json index ace901a..69e7501 100644 --- a/config.example.json +++ b/config.example.json @@ -8,5 +8,10 @@ "denom": "unois", "gasPrice": "0.05unois", "prefix": "nois", - "moniker": "" + "moniker": "", + "drandEndpoints": [ + "https://api2.drand.sh/", + "https://api3.drand.sh/", + "https://drand.cloudflare.com/" + ] } diff --git a/config.ts b/config.ts index d038b9c..dd94827 100644 --- a/config.ts +++ b/config.ts @@ -10,4 +10,5 @@ export interface Config { readonly gasPrice: string; readonly prefix: string; readonly moniker: string; + readonly drandEndpoints?: string[] | null; } diff --git a/deps.ts b/deps.ts index 29f3f6b..b516728 100644 --- a/deps.ts +++ b/deps.ts @@ -23,4 +23,9 @@ export type { TendermintClient } from "npm:@cosmjs/tendermint-rpc@^0.31.1"; // drand export type { ChainClient, ChainOptions, RandomnessBeacon } from "npm:drand-client@^1.2.0"; -export { FastestNodeClient, watch } from "npm:drand-client@^1.2.0"; +export { + FastestNodeClient, + HttpCachingChain, + HttpChainClient, + watch, +} from "npm:drand-client@^1.2.0"; diff --git a/drand.ts b/drand.ts index db2471c..af5f1d8 100644 --- a/drand.ts +++ b/drand.ts @@ -4,19 +4,25 @@ export const chainHash = "dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df const publicKey = "a0b862a7527fee3a731bcb59280ab6abd62d5c0b6ea03dc4ddf6612fdfc9d01f01c31542541771903475eb1ec6615f8d0df0b8b6dce385811d6dcf8cbefb8759e5e616a3dfd054c928940766d9a5b9db91e3b697e5d70a975181e007f87fca5e"; +// See https://drand.love/developer/ +// Note that https://api.drand.secureweb3.com:6875 does not support quicknet. +export const defaultEndpoints = [ + // `https://api.drand.sh/`, + "https://api2.drand.sh/", + "https://api3.drand.sh/", + "https://drand.cloudflare.com/", +]; + export const drandOptions: ChainOptions = { disableBeaconVerification: true, noCache: false, chainVerificationParams: { chainHash, publicKey }, }; -export const drandUrls = [ - // `https://api.drand.sh/${chainHash}`, - `https://api2.drand.sh/${chainHash}`, - `https://api3.drand.sh/${chainHash}`, - `https://drand.cloudflare.com/${chainHash}`, - // ... -]; +/** Appends the chain hash to the endpoint */ +export function drandBaseUrl(endpoint: string): string { + return endpoint.replace(/\/$/, "") + "/" + chainHash; +} // https://api3.drand.sh/dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493/info const DRAND_GENESIS = 1677685200; diff --git a/main.ts b/main.ts index 1bf8e25..efda036 100644 --- a/main.ts +++ b/main.ts @@ -1,14 +1,23 @@ -import { drandOptions, drandUrls, publishedIn, publishedSince } from "./drand.ts"; +import { + defaultEndpoints, + drandBaseUrl, + drandOptions, + publishedIn, + publishedSince, +} from "./drand.ts"; import { group } from "./group.ts"; import { assert, calculateFee, + ChainClient, Coin, CosmWasmClient, Decimal, DirectSecp256k1HdWallet, FastestNodeClient, GasPrice, + HttpCachingChain, + HttpChainClient, SignerData, SigningCosmWasmClient, sleep, @@ -146,7 +155,22 @@ if (import.meta.main) { const incentivizedRounds = new Map>(); - const fastestNodeClient = new FastestNodeClient(drandUrls, drandOptions); + const drandEndpoints = config.drandEndpoints ?? defaultEndpoints; + assert( + drandEndpoints, + "drandEndpoints must not be an empty list. Use null or omit to use the default list.", + ); + + const drandClient: ChainClient = (() => { + if (drandEndpoints.length === 1) { + const chain = new HttpCachingChain(drandBaseUrl(drandEndpoints[0]), drandOptions); + return new HttpChainClient(chain); + } else { + const fc = new FastestNodeClient(drandEndpoints.map(drandBaseUrl), drandOptions); + fc.start(); + return fc; + } + })(); const submitter = new Submitter({ client, @@ -160,12 +184,11 @@ if (import.meta.main) { drandAddress: drandAddress, userAgent, incentivizedRounds, - drandClient: fastestNodeClient, + drandClient, }); - fastestNodeClient.start(); const abortController = new AbortController(); - for await (const beacon of watch(fastestNodeClient, abortController)) { + for await (const beacon of watch(drandClient, abortController)) { const n = beacon.round; // n is the round we just received and process now const m = n + 1; // m := n+1 refers to the next round in this current loop diff --git a/submitter.ts b/submitter.ts index 0a7f91f..7531982 100644 --- a/submitter.ts +++ b/submitter.ts @@ -3,8 +3,8 @@ import { publishedSince, timeOfRound } from "./drand.ts"; import { assertIsDeliverTxSuccess, calculateFee, + ChainClient, CosmWasmClient, - FastestNodeClient, isDefined, logs, RandomnessBeacon, @@ -28,7 +28,7 @@ interface Capture { userAgent: string; getNextSignData: () => SignerData; incentivizedRounds: Map>; - drandClient: FastestNodeClient; + drandClient: ChainClient; } export class Submitter {