-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gateway connectivity and observe unprocessed jobs
- Loading branch information
1 parent
cf01598
commit 76cc760
Showing
5 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters