Skip to content

Commit

Permalink
feat(as-sdk): add Tally convenience methods
Browse files Browse the repository at this point in the history
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
Thomasvdam committed Jun 18, 2024
1 parent 76414bf commit 54a89d2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
12 changes: 10 additions & 2 deletions libs/as-sdk/assembly/index.ts
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';
30 changes: 30 additions & 0 deletions libs/as-sdk/assembly/process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VM_MODE_TALLY, VM_MODE_DR, VM_MODE_ENV_KEY } from './vm-modes';
import { Process as WasiProcess, CommandLine, Environ } from 'as-wasi/assembly';
import { execution_result } from './bindings/seda_v1';

Expand Down Expand Up @@ -39,6 +40,35 @@ export default class Process {
return result;
}

/**
* Gets the mode of the VM instance.
*
* @returns {string} The mode of the VM, either 'dr' or 'tally'.
*/
static getVmMode(): string {
const env = Process.envs();

return env.get(VM_MODE_ENV_KEY);
}

/**
* Returns true when the VM instance is in 'tally' mode.
*
* @returns {boolean}
*/
static isTallyVmMode(): boolean {
return Process.getVmMode() === VM_MODE_TALLY;
}

/**
* Returns true when the VM instance is in 'dr' mode.
*
* @returns {boolean}
*/
static isDrVmMode(): boolean {
return Process.getVmMode() === VM_MODE_DR;
}

/**
* Exits the process with a message
* This sets the result of the Data Request execution to the message
Expand Down
63 changes: 63 additions & 0 deletions libs/as-sdk/assembly/tally.ts
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
);
}
}
3 changes: 3 additions & 0 deletions libs/as-sdk/assembly/vm-modes.ts
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";

0 comments on commit 54a89d2

Please sign in to comment.