Skip to content

Commit

Permalink
Merge pull request #26 from noislabs/drandEndpoints
Browse files Browse the repository at this point in the history
Make drandEndpoints configurable
  • Loading branch information
webmaster128 authored Sep 4, 2023
2 parents 48576d0 + 84296c7 commit aceb54c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
7 changes: 6 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
]
}
1 change: 1 addition & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface Config {
readonly gasPrice: string;
readonly prefix: string;
readonly moniker: string;
readonly drandEndpoints?: string[] | null;
}
7 changes: 6 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
20 changes: 13 additions & 7 deletions drand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 28 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -146,7 +155,22 @@ if (import.meta.main) {

const incentivizedRounds = new Map<number, Promise<boolean>>();

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,
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { publishedSince, timeOfRound } from "./drand.ts";
import {
assertIsDeliverTxSuccess,
calculateFee,
ChainClient,
CosmWasmClient,
FastestNodeClient,
isDefined,
logs,
RandomnessBeacon,
Expand All @@ -28,7 +28,7 @@ interface Capture {
userAgent: string;
getNextSignData: () => SignerData;
incentivizedRounds: Map<number, Promise<boolean>>;
drandClient: FastestNodeClient;
drandClient: ChainClient;
}

export class Submitter {
Expand Down

0 comments on commit aceb54c

Please sign in to comment.