-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gateway connectivity and observe unprocessed jobs #13
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cdd1de9
Add gateway connectivity and observe unprocessed jobs
webmaster128 2f33d08
Let main drand loop trigger JobsObserver
webmaster128 a8f6044
Let queryIsIncentivized take a single round only
webmaster128 7e20b26
Log rounds
webmaster128 1122e4a
Rename to handleBeacon
webmaster128 ab9fb96
Turn Submitter into class
webmaster128 b81fa9e
Manage submissions wisely
webmaster128 55b702d
make submitRound private
webmaster128 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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(); | ||
} | ||
|
||
function formatDuration(durationInMs: number): string { | ||
const inSeconds = durationInMs / 1000; | ||
return `${inSeconds.toFixed(1)}s`; | ||
} | ||
|
||
export class JobsObserver { | ||
private readonly noisClient: CosmWasmClient; | ||
private readonly gateway: string; | ||
|
||
public constructor( | ||
noisClient: CosmWasmClient, | ||
gatewayAddress: string, | ||
) { | ||
this.noisClient = noisClient; | ||
this.gateway = gatewayAddress; | ||
} | ||
|
||
/** | ||
* Checks gateway for pending jobs and returns the rounds of those jobs as a list | ||
*/ | ||
public async check(): Promise<number[]> { | ||
const query = { jobs_desc: { offset: null, limit: 3 } }; | ||
const { jobs }: JobsResponse = await this.noisClient.queryContractSmart(this.gateway, query); | ||
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(); | ||
return `#${round} (due ${formatDuration(due)})`; | ||
}); | ||
console.log(`Jobs pending for rounds: %c${roundInfos.join(", ")}`, "color: orange"); | ||
return rounds; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious question, Is this wasmable from nois-contracts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you means, but this is the JSON format of the jobs returned from the gateway. We want the type here, not any executable logic. There is a JSON Schema for that which the contract can create but JSON Schema -> TypeScript conversion is not that easy to set up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay makes sense. I thought wasm (apart from porting function execution ) could also help to just export some struct or enum not only functions, such that people would prevent deduplication of interfaces and APIs between two different languages. This is irrelevant to this PR anyways.