-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(as-sdk): add Tally convenience methods
To help with decoding the reveal bytes into JSON objects and merging it with the consensus results. This does not decode the actual reveal bodies as this is implementation specific and up to the developer creating the tally binary.
- Loading branch information
1 parent
76414bf
commit 54a89d2
Showing
4 changed files
with
106 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import Process from './process'; | ||
import Tally from './tally'; | ||
|
||
export { | ||
httpFetch, | ||
HttpFetchMethod, | ||
HttpFetchOptions, | ||
HttpResponse, | ||
} from './http'; | ||
|
||
export { httpFetch, HttpFetchMethod, HttpFetchOptions, HttpResponse } from './http'; | ||
export { PromiseStatus } from './promise'; | ||
export { Process }; | ||
export { Process, Tally }; | ||
export { RevealBody } from './tally'; |
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,63 @@ | ||
import { JSON } from "json-as/assembly"; | ||
import Process from "./process"; | ||
|
||
const REVEALS_ARGUMENT_POSITION = 2; | ||
const CONSENSUS_ARGUMENT_POSITION = 3; | ||
|
||
@json | ||
export class RevealBody { | ||
salt!: u8[]; | ||
exit_code!: u8; | ||
gas_used!: string; | ||
reveal!: u8[]; | ||
} | ||
|
||
@json | ||
export class RevealResult { | ||
salt!: u8[]; | ||
exit_code!: u8; | ||
gas_used!: string; | ||
reveal!: u8[]; | ||
inConsensus!: u8; | ||
} | ||
|
||
export default class Tally { | ||
static getReveals(): RevealResult[] { | ||
const encodedReveals = Process.args().at(REVEALS_ARGUMENT_POSITION); | ||
const reveals = JSON.parse<RevealBody[]>(encodedReveals); | ||
|
||
const encodedConsensus = Process.args().at(CONSENSUS_ARGUMENT_POSITION); | ||
const consensus = JSON.parse<u8[]>(encodedConsensus); | ||
|
||
const revealsAmount = reveals.length; | ||
const consensusAmount = consensus.length; | ||
if (revealsAmount !== consensusAmount) { | ||
throw new Error( | ||
`Number of reveals (${revealsAmount}) does not equal number of consensus reports (${consensusAmount}).` | ||
); | ||
} | ||
|
||
const revealResults: RevealResult[] = []; | ||
for (let index = 0; index < reveals.length; index++) { | ||
const reveal = reveals[index]; | ||
|
||
revealResults.push({ | ||
exit_code: reveal.exit_code, | ||
gas_used: reveal.gas_used, | ||
reveal: reveal.reveal, | ||
salt: reveal.salt, | ||
inConsensus: consensus.at(index), | ||
}); | ||
} | ||
|
||
return revealResults; | ||
} | ||
|
||
static getConsensusReveals(): RevealResult[] { | ||
const revealResults = Tally.getReveals(); | ||
|
||
return revealResults.filter( | ||
(revealResult) => revealResult.inConsensus === 0 | ||
); | ||
} | ||
} |
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,3 @@ | ||
export const VM_MODE_ENV_KEY = "VM_MODE"; | ||
export const VM_MODE_TALLY = "tally"; | ||
export const VM_MODE_DR = "dr"; |