Skip to content

Commit

Permalink
feat: use Oracle Program in user facing interfaces and outputs
Browse files Browse the repository at this point in the history
Part-of: sedaprotocol/seda-internal#141
BREAKING CHANGE: renames a lot of methods and commands
  • Loading branch information
Thomasvdam committed Sep 24, 2024
1 parent ef467bc commit 561872b
Show file tree
Hide file tree
Showing 29 changed files with 267 additions and 269 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ new PlanetProgram().run();
In order to test this we can use a JS testing suite (we use Bun:test in this repository and the starter kits, but any runner should work). We use the `@seda-protocol/dev-tools` package for this, which runs the Oracle Program in a similar environment as it would on the SEDA network:

```ts
import { executeDrWasm } from "@seda-protocol/dev-tools";
import { testOracleProgramExecution } from "@seda-protocol/dev-tools";
import { readFile } from "node:fs/promises";

const WASM_PATH = "build/debug.wasm";

describe("Oracle Program: execution", () => {
it("should be able to run", async () => {
const wasmBinary = await readFile(WASM_PATH);
const oracleProgram = await readFile(WASM_PATH);

// Calls our SEDA VM
const vmResult = await executeDrWasm(
// The wasm file
wasmBinary,
const vmResult = await testOracleProgramExecution(
// The bytes of the Oracle Program
oracleProgram,
// Inputs for the Oracle Program
Buffer.from("1")
);
Expand Down
44 changes: 22 additions & 22 deletions libs/as-sdk-integration-tests/src/bytes.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { describe, expect, it } from "bun:test";
import { readFile } from "node:fs/promises";
import { executeDrWasm } from "@seda/dev-tools";
import { testOracleProgramExecution } from "@seda/dev-tools";

const wasmBinary = await readFile(
const oracleProgram = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);

describe("bytes", () => {
describe("concat", () => {
it("should concatenate 2 Bytes instances in a new one", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesConcat:world!"),
);

expect(result.resultAsString).toBe("Hello, world!");
});

it("should allow passing an array of bytes to the static method and concatenate them", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesStaticConcat:swap"),
);

Expand All @@ -29,8 +29,8 @@ describe("bytes", () => {

describe("slice", () => {
it("should return a new bytes instance when called without arguments", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesSliceNoArguments"),
);

Expand All @@ -41,17 +41,17 @@ describe("bytes", () => {
});

it("should return a new bytes instance when called with just the start", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesSliceOnlyStart"),
);

expect(result.resultAsString).toEqual("OnlyStart");
});

it("should return a new bytes instance when called with start and end", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesSliceStartEnd"),
);

Expand All @@ -71,8 +71,8 @@ describe("bytes", () => {
value: "some",
},
});
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from(`testBytesJSON:${input}`),
);

Expand All @@ -84,8 +84,8 @@ describe("bytes", () => {

describe("toNumber", () => {
it("should be able to parse a number to a type", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesToNumber"),
);

Expand All @@ -95,8 +95,8 @@ describe("bytes", () => {

describe("fromNumber", () => {
it("should convert a number to a bytes object", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testNumberToBytes"),
);

Expand All @@ -108,8 +108,8 @@ describe("bytes", () => {

describe("fromBigNumber", () => {
it("should convert a big number to Bytes", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBigNumberToBytes"),
);

Expand All @@ -121,8 +121,8 @@ describe("bytes", () => {

describe("toBigNumber", () => {
it("should be able to parse Bytes as a big number", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesToBigNumber"),
);

Expand Down
22 changes: 14 additions & 8 deletions libs/as-sdk-integration-tests/src/console.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
import { describe, expect, it } from "bun:test";
import { readFile } from "node:fs/promises";
import { executeDrWasm } from "@seda/dev-tools";
import { testOracleProgramExecution } from "@seda/dev-tools";

const wasmBinary = await readFile(
const oracleProgram = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);

describe("console", () => {
it("should print a hex representation of a raw buffer", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testLogBuffer"),
);

expect(result.stdout).toEqual("ArrayBuffer(0x627566666572)\n");
});

it("should print a hex representation of a Uint8Array", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testLogByteArray"),
);

expect(result.stdout).toEqual("TypedArray(0x54797065644172726179)\n");
});

it("should print a float value", async () => {
const result = await executeDrWasm(wasmBinary, Buffer.from("testLogFloat"));
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testLogFloat"),
);

expect(result.stdout).toEqual("0.3199999928474426\n");
});

it("should print a null value", async () => {
const result = await executeDrWasm(wasmBinary, Buffer.from("testLogNull"));
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testLogNull"),
);

expect(result.stdout).toEqual("null\n");
});
Expand Down
16 changes: 8 additions & 8 deletions libs/as-sdk-integration-tests/src/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { describe, expect, it } from "bun:test";
import { readFile } from "node:fs/promises";
import { executeDrWasm } from "@seda/dev-tools";
import { testOracleProgramExecution } from "@seda/dev-tools";

const wasmBinary = await readFile(
const oracleProgram = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);

describe("Crypto", () => {
it("Test valid Secp256k1 signature", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testSecp256k1VerifyValid"),
);

expect(result.resultAsString).toEqual("valid secp256k1 signature");
});

it("Test invalid Secp256k1 signature", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testSecp256k1VerifyInvalid"),
);

Expand All @@ -27,8 +27,8 @@ describe("Crypto", () => {

describe("keccak256", () => {
it("should hash the input bytes correctly", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testKeccak256"),
);

Expand Down
16 changes: 8 additions & 8 deletions libs/as-sdk-integration-tests/src/encoding.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { describe, expect, it } from "bun:test";
import { readFile } from "node:fs/promises";
import { executeDrWasm } from "@seda/dev-tools";
import { testOracleProgramExecution } from "@seda/dev-tools";

const wasmBinary = await readFile(
const oracleProgram = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);

describe("encoding", () => {
describe("hex", () => {
it("should encode hex to the same string that came in", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from(
"000000000000c886c362e71402e05b3b6132b2e23832bcbc42f1",
"hex",
Expand All @@ -25,8 +25,8 @@ describe("encoding", () => {

describe("bytes", () => {
it("should correctly decode/encode hex strings", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesHexEncodeDecode"),
);

Expand All @@ -36,8 +36,8 @@ describe("encoding", () => {
});

it("should ignore 0x prefixes in hex strings", async () => {
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testBytesPrefixedHexDecode"),
);

Expand Down
39 changes: 14 additions & 25 deletions libs/as-sdk-integration-tests/src/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { beforeEach, describe, expect, it, mock } from "bun:test";
import { readFile } from "node:fs/promises";
import { executeDrWasm } from "@seda/dev-tools";
import { testOracleProgramExecution } from "@seda/dev-tools";
import { Response } from "node-fetch";

const mockHttpFetch = mock();
const oracleProgram = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);

describe("Http", () => {
beforeEach(() => {
mockHttpFetch.mockReset();
});

it("Test SDK HTTP Rejection", async () => {
const wasmBinary = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);

const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testHttpRejection"),
mockHttpFetch,
);
Expand All @@ -26,10 +25,6 @@ describe("Http", () => {
});

it("Test mocked SDK HTTP Success", async () => {
const wasmBinary = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);

const mockResponse = new Response(
JSON.stringify({
userId: 200,
Expand All @@ -40,8 +35,8 @@ describe("Http", () => {
);
mockHttpFetch.mockResolvedValue(mockResponse);

const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testHttpSuccess"),
mockHttpFetch,
);
Expand All @@ -52,11 +47,8 @@ describe("Http", () => {

// Possibly flakey as it relies on internet connectivity and an external service
it("Test SDK HTTP Success", async () => {
const wasmBinary = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testHttpSuccess"),
);

Expand All @@ -66,11 +58,8 @@ describe("Http", () => {

// Possibly flakey as it relies on internet connectivity and an external service
it("Test SDK HTTP POST Success", async () => {
const wasmBinary = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);
const result = await executeDrWasm(
wasmBinary,
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testPostHttpSuccess"),
);

Expand All @@ -80,8 +69,8 @@ describe("Http", () => {
);
});

it("should exit when an invalid WASM binary is given", async () => {
const result = await executeDrWasm(
it("should exit when an invalid Oracle Program is given", async () => {
const result = await testOracleProgramExecution(
Buffer.from(new Uint8Array([0, 97, 115, 109])),
Buffer.from("testHttpSuccess"),
);
Expand Down
Loading

0 comments on commit 561872b

Please sign in to comment.