Skip to content

Commit

Permalink
Add gateway connectivity and observe unprocessed jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Aug 10, 2023
1 parent cf01598 commit 76cc760
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"rpcEndpoint": "https://nois-testnet-rpc.itrocket.net:443",
"rpcEndpoint2": "",
"rpcEndpoint3": "",
"contract": "nois14xef285hz5cx5q9hh32p9nztu3cct4g44sxjgx3dmftt2tj2rweqkjextk",
"drandAddress": "nois14xef285hz5cx5q9hh32p9nztu3cct4g44sxjgx3dmftt2tj2rweqkjextk",
"gatewayAddress": "",
"mnemonic": "",
"denom": "unois",
"gasPrice": "0.05unois",
Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export {
} from "npm:@cosmjs/stargate@^0.31.0";
export { sha256 } from "npm:@cosmjs/crypto@^0.31.0";
export { toUtf8 } from "npm:@cosmjs/encoding@^0.31.0";
export { Decimal } from "npm:@cosmjs/math@^0.31.0";
export { Decimal, Uint53 } from "npm:@cosmjs/math@^0.31.0";
export { DirectSecp256k1HdWallet } from "npm:@cosmjs/proto-signing@^0.31.0";
export { Tendermint34Client, Tendermint37Client } from "npm:@cosmjs/tendermint-rpc@^0.31.0";
export { assert, isDefined, sleep } from "npm:@cosmjs/utils@^0.31.0";
Expand Down
2 changes: 1 addition & 1 deletion drand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChainOptions } from "./deps.ts";

const chainHash = "dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493";
export const chainHash = "dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493";
const publicKey =
"a0b862a7527fee3a731bcb59280ab6abd62d5c0b6ea03dc4ddf6612fdfc9d01f01c31542541771903475eb1ec6615f8d0df0b8b6dce385811d6dcf8cbefb8759e5e616a3dfd054c928940766d9a5b9db91e3b697e5d70a975181e007f87fca5e";

Expand Down
57 changes: 57 additions & 0 deletions jobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { assert, CosmWasmClient, Uint53 } from "./deps.ts";
import { chainHash, timeOfRound } from "./drand.ts";

interface Job {
/// A RNG specific randomness source identifier, e.g. `drand:<network id>:<round>`
source_id: string;
// The channel the job came from and we have to send the response to
channel: string;
origin: string;
}

interface JobsResponse {
jobs: Job[];
}

function parseRound(job: Job): number {
const [sourceType, networkId, round] = job.source_id.split(":");
assert(sourceType == "drand", "Source type must be 'drand'");
assert(networkId == chainHash, "Got wrong chain hash in job");
assert(round, "Round must be set");
return Uint53.fromString(round).toNumber();
}

export class JobsObserver {
private readonly client: CosmWasmClient;
private readonly gateway: string;
private readonly abort: AbortController;
private readonly intervalId: number;

public constructor(
noisClient: CosmWasmClient,
gatewayAddress: string,
abortController: AbortController,
pollInterval = 1000,
) {
this.client = noisClient;
this.gateway = gatewayAddress;
this.abort = abortController;

this.intervalId = setInterval(() => {
const query = { jobs_desc: { offset: null, limit: 3 } };
this.client.queryContractSmart(this.gateway, query).then(
({ jobs }: JobsResponse) => {
if (jobs.length === 0) return; // Nothing to do for us

const rounds = jobs.map(parseRound);
const roundInfos = rounds.map((round) => {
const due = timeOfRound(round) - Date.now() / 1000;
return `#${round} (due ${due.toFixed(1)}s)`;
});
console.log(`Jobs pending for rounds: %c${roundInfos.join(", ")}`, "color: orange");
},
(err) => console.error(err),
);
}, pollInterval);
}
}
9 changes: 7 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
watch,
} from "./deps.ts";
import { BeaconCache } from "./cache.ts";
import { JobsObserver } from "./jobs.ts";
import { loop } from "./loop.ts";
import { queryIsAllowListed, queryIsIncentivized } from "./drand_contract.ts";
import { connectTendermint } from "./tendermint.ts";
Expand Down Expand Up @@ -53,7 +54,7 @@ if (import.meta.main) {
const { default: config } = await import("./config.json", {
assert: { type: "json" },
});
assert(config.contract, `Config field "contract" must be set.`);
assert(config.drandAddress, `Config field "drandAddress" must be set.`);
assert(config.rpcEndpoint, `Config field "rpcEndpoint" must be set.`);

const mnemonic = await (async () => {
Expand Down Expand Up @@ -106,7 +107,7 @@ if (import.meta.main) {
const fee = calculateFee(gasLimitRegister, config.gasPrice);
await client.execute(
botAddress,
config.contract,
config.drandAddress,
{ register_bot: { moniker: moniker } },
fee,
);
Expand All @@ -122,6 +123,10 @@ if (import.meta.main) {
})(),
]);

if (config.gatewayAddress) {
const _jobs = new JobsObserver(client, config.gatewayAddress, new AbortController());
}

// Initialize local sign data
await resetSignData();

Expand Down

0 comments on commit 76cc760

Please sign in to comment.