From 561872b65c208ddfb620fbd6b49742426f4cf8b8 Mon Sep 17 00:00:00 2001 From: Thomas van Dam Date: Tue, 24 Sep 2024 10:49:28 +0200 Subject: [PATCH] feat: use Oracle Program in user facing interfaces and outputs Part-of: https://github.com/sedaprotocol/seda-internal/issues/141 BREAKING CHANGE: renames a lot of methods and commands --- README.md | 10 ++-- .../src/bytes.test.ts | 44 ++++++++-------- .../src/console.test.ts | 22 +++++--- .../src/crypto.test.ts | 16 +++--- .../src/encoding.test.ts | 16 +++--- .../as-sdk-integration-tests/src/http.test.ts | 39 +++++--------- .../src/proxy-http.test.ts | 16 +++--- .../src/tallyvm.test.ts | 51 +++++++------------ libs/as-sdk/assembly/process.ts | 2 +- libs/dev-tools/README.md | 24 ++++----- libs/dev-tools/src/cli/index.ts | 4 +- .../dev-tools/src/cli/oracle-program/index.ts | 10 ++++ .../src/cli/{wasm => oracle-program}/list.ts | 10 ++-- .../src/cli/{wasm => oracle-program}/show.ts | 13 +++-- .../cli/{wasm => oracle-program}/upload.ts | 40 ++++++++------- libs/dev-tools/src/cli/wasm/index.ts | 10 ---- libs/dev-tools/src/index.ts | 7 ++- .../src/lib/services/dr/create-dr-input.ts | 18 +++---- .../oracle-program/get-oracle-program.ts | 50 ++++++++++++++++++ .../{wasm => oracle-program}/query-client.ts | 0 .../upload-oracle-program.ts} | 10 ++-- libs/dev-tools/src/lib/services/signer.ts | 2 +- .../src/lib/services/wasm/get-wasm-binary.ts | 34 ------------- .../lib/testing/execute-dr-oracle-program.ts | 0 .../src/lib/testing/execute-dr-wasm.ts | 18 ------- .../src/lib/testing/execute-tally-wasm.ts | 18 ------- .../src/lib/testing/test-oracle-program.ts | 42 +++++++++++++++ libs/vm/README.md | 8 +-- libs/vm/src/index.ts | 2 +- 29 files changed, 267 insertions(+), 269 deletions(-) create mode 100644 libs/dev-tools/src/cli/oracle-program/index.ts rename libs/dev-tools/src/cli/{wasm => oracle-program}/list.ts (69%) rename libs/dev-tools/src/cli/{wasm => oracle-program}/show.ts (70%) rename libs/dev-tools/src/cli/{wasm => oracle-program}/upload.ts (56%) delete mode 100644 libs/dev-tools/src/cli/wasm/index.ts create mode 100644 libs/dev-tools/src/lib/services/oracle-program/get-oracle-program.ts rename libs/dev-tools/src/lib/services/{wasm => oracle-program}/query-client.ts (100%) rename libs/dev-tools/src/lib/services/{wasm/upload-wasm-binary.ts => oracle-program/upload-oracle-program.ts} (85%) delete mode 100644 libs/dev-tools/src/lib/services/wasm/get-wasm-binary.ts create mode 100644 libs/dev-tools/src/lib/testing/execute-dr-oracle-program.ts delete mode 100644 libs/dev-tools/src/lib/testing/execute-dr-wasm.ts delete mode 100644 libs/dev-tools/src/lib/testing/execute-tally-wasm.ts create mode 100644 libs/dev-tools/src/lib/testing/test-oracle-program.ts diff --git a/README.md b/README.md index d359fb9..fe2be84 100644 --- a/README.md +++ b/README.md @@ -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") ); diff --git a/libs/as-sdk-integration-tests/src/bytes.test.ts b/libs/as-sdk-integration-tests/src/bytes.test.ts index 1067018..16d3d48 100644 --- a/libs/as-sdk-integration-tests/src/bytes.test.ts +++ b/libs/as-sdk-integration-tests/src/bytes.test.ts @@ -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("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!"), ); @@ -18,8 +18,8 @@ describe("bytes", () => { }); 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"), ); @@ -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"), ); @@ -41,8 +41,8 @@ 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"), ); @@ -50,8 +50,8 @@ describe("bytes", () => { }); 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"), ); @@ -71,8 +71,8 @@ describe("bytes", () => { value: "some", }, }); - const result = await executeDrWasm( - wasmBinary, + const result = await testOracleProgramExecution( + oracleProgram, Buffer.from(`testBytesJSON:${input}`), ); @@ -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"), ); @@ -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"), ); @@ -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"), ); @@ -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"), ); diff --git a/libs/as-sdk-integration-tests/src/console.test.ts b/libs/as-sdk-integration-tests/src/console.test.ts index 52ae5ac..e369613 100644 --- a/libs/as-sdk-integration-tests/src/console.test.ts +++ b/libs/as-sdk-integration-tests/src/console.test.ts @@ -1,15 +1,15 @@ 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"), ); @@ -17,8 +17,8 @@ describe("console", () => { }); it("should print a hex representation of a Uint8Array", async () => { - const result = await executeDrWasm( - wasmBinary, + const result = await testOracleProgramExecution( + oracleProgram, Buffer.from("testLogByteArray"), ); @@ -26,13 +26,19 @@ describe("console", () => { }); 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"); }); diff --git a/libs/as-sdk-integration-tests/src/crypto.test.ts b/libs/as-sdk-integration-tests/src/crypto.test.ts index 9303a6d..a62f488 100644 --- a/libs/as-sdk-integration-tests/src/crypto.test.ts +++ b/libs/as-sdk-integration-tests/src/crypto.test.ts @@ -1,15 +1,15 @@ 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"), ); @@ -17,8 +17,8 @@ describe("Crypto", () => { }); it("Test invalid Secp256k1 signature", async () => { - const result = await executeDrWasm( - wasmBinary, + const result = await testOracleProgramExecution( + oracleProgram, Buffer.from("testSecp256k1VerifyInvalid"), ); @@ -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"), ); diff --git a/libs/as-sdk-integration-tests/src/encoding.test.ts b/libs/as-sdk-integration-tests/src/encoding.test.ts index b9054b2..5d2e995 100644 --- a/libs/as-sdk-integration-tests/src/encoding.test.ts +++ b/libs/as-sdk-integration-tests/src/encoding.test.ts @@ -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", @@ -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"), ); @@ -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"), ); diff --git a/libs/as-sdk-integration-tests/src/http.test.ts b/libs/as-sdk-integration-tests/src/http.test.ts index d52da2d..17ddba7 100644 --- a/libs/as-sdk-integration-tests/src/http.test.ts +++ b/libs/as-sdk-integration-tests/src/http.test.ts @@ -1,9 +1,12 @@ 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(() => { @@ -11,12 +14,8 @@ describe("Http", () => { }); 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, ); @@ -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, @@ -40,8 +35,8 @@ describe("Http", () => { ); mockHttpFetch.mockResolvedValue(mockResponse); - const result = await executeDrWasm( - wasmBinary, + const result = await testOracleProgramExecution( + oracleProgram, Buffer.from("testHttpSuccess"), mockHttpFetch, ); @@ -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"), ); @@ -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"), ); @@ -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"), ); diff --git a/libs/as-sdk-integration-tests/src/proxy-http.test.ts b/libs/as-sdk-integration-tests/src/proxy-http.test.ts index 94ad909..190283e 100644 --- a/libs/as-sdk-integration-tests/src/proxy-http.test.ts +++ b/libs/as-sdk-integration-tests/src/proxy-http.test.ts @@ -1,11 +1,11 @@ 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 wasmBinary = await readFile( +const oracleProgram = await readFile( "dist/libs/as-sdk-integration-tests/debug.wasm", ); @@ -26,8 +26,8 @@ describe("ProxyHttp", () => { mockHttpFetch.mockResolvedValue(mockResponse); - const result = await executeDrWasm( - wasmBinary, + const result = await testOracleProgramExecution( + oracleProgram, Buffer.from("testProxyHttpFetch"), mockHttpFetch, ); @@ -49,8 +49,8 @@ describe("ProxyHttp", () => { mockHttpFetch.mockResolvedValue(mockResponse); - const result = await executeDrWasm( - wasmBinary, + const result = await testOracleProgramExecution( + oracleProgram, Buffer.from("testProxyHttpFetch"), mockHttpFetch, ); @@ -64,8 +64,8 @@ describe("ProxyHttp", () => { describe("generateProxyHttpSigningMessage", () => { it("should generate the expected message", async () => { - const result = await executeDrWasm( - wasmBinary, + const result = await testOracleProgramExecution( + oracleProgram, Buffer.from("testGenerateProxyMessage"), ); diff --git a/libs/as-sdk-integration-tests/src/tallyvm.test.ts b/libs/as-sdk-integration-tests/src/tallyvm.test.ts index f864da7..b66a633 100644 --- a/libs/as-sdk-integration-tests/src/tallyvm.test.ts +++ b/libs/as-sdk-integration-tests/src/tallyvm.test.ts @@ -3,17 +3,18 @@ import { readFile } from "node:fs/promises"; import { TallyVmAdapter, callVm } from "@seda-protocol/vm"; import { createMockTallyArgs, - executeDrWasm, - executeTallyWasm, + testOracleProgramExecution, + testOracleProgramTally, } from "@seda/dev-tools"; +const oracleProgram = await readFile( + "dist/libs/as-sdk-integration-tests/debug.wasm", +); + describe("TallyVm", () => { it("should run in dr vm mode by default", 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("testTallyVmMode"), ); @@ -22,11 +23,8 @@ describe("TallyVm", () => { }); it("should run in tally vm mode with the adapter", async () => { - const wasmBinary = await readFile( - "dist/libs/as-sdk-integration-tests/debug.wasm", - ); - const result = await executeTallyWasm( - wasmBinary, + const result = await testOracleProgramTally( + oracleProgram, Buffer.from("testTallyVmMode"), [], ); @@ -36,11 +34,8 @@ describe("TallyVm", () => { }); it("should fail to make an http call", async () => { - const wasmBinary = await readFile( - "dist/libs/as-sdk-integration-tests/debug.wasm", - ); - const result = await executeTallyWasm( - wasmBinary, + const result = await testOracleProgramTally( + oracleProgram, Buffer.from("testTallyVmHttp"), [], ); @@ -50,10 +45,6 @@ describe("TallyVm", () => { }); it("should fail when the reveals and consensus arrays are not the same length", async () => { - const wasmBinary = await readFile( - "dist/libs/as-sdk-integration-tests/debug.wasm", - ); - const args = createMockTallyArgs(Buffer.from("testTallyVmReveals"), [ { exitCode: 0, @@ -73,7 +64,7 @@ describe("TallyVm", () => { const result = await callVm( { args, - binary: wasmBinary, + binary: oracleProgram, envs: {}, }, undefined, @@ -87,10 +78,6 @@ describe("TallyVm", () => { }); it("should be able to parse the reveals and return the unfiltered reveals", async () => { - const wasmBinary = await readFile( - "dist/libs/as-sdk-integration-tests/debug.wasm", - ); - const reports = [ { exitCode: 0, @@ -112,8 +99,8 @@ describe("TallyVm", () => { }, ]; - const result = await executeTallyWasm( - wasmBinary, + const result = await testOracleProgramTally( + oracleProgram, Buffer.from("testTallyVmReveals"), reports, ); @@ -125,10 +112,6 @@ describe("TallyVm", () => { }); it("should provide a convenience method to filter out outliers.", async () => { - const wasmBinary = await readFile( - "dist/libs/as-sdk-integration-tests/debug.wasm", - ); - const reports = [ { exitCode: 1, @@ -150,8 +133,8 @@ describe("TallyVm", () => { }, ]; - const result = await executeTallyWasm( - wasmBinary, + const result = await testOracleProgramTally( + oracleProgram, Buffer.from("testTallyVmRevealsFiltered"), reports, ); diff --git a/libs/as-sdk/assembly/process.ts b/libs/as-sdk/assembly/process.ts index d5d08d1..6eb8a44 100644 --- a/libs/as-sdk/assembly/process.ts +++ b/libs/as-sdk/assembly/process.ts @@ -24,7 +24,7 @@ export default class Process { /** * Gets all the input arguments from the Data Request - * First argument (index: 0) is the binary hash + * First argument (index: 0) is the Oracle Program ID * Second argument (index: 1) is the input of the Data Request * * @returns {string[]} An array of input arguments diff --git a/libs/dev-tools/README.md b/libs/dev-tools/README.md index 3d423aa..f5304db 100644 --- a/libs/dev-tools/README.md +++ b/libs/dev-tools/README.md @@ -37,21 +37,21 @@ You can apply the following options to all CLI commands: - --rpc - The SEDA chain RPC to use -## WASM commands +## Oracle Program commands -Commands that allow you to interact with the Data Request binaries. +Commands that allow you to interact with the Oracle Programs. -### Uploading a Data Request binary +### Uploading an Oracle Program -Allows you to upload a Data Request binary to the SEDA chain. +Allows you to upload an Oracle Program to the SEDA chain. Example: ```sh -npx seda-sdk wasm upload --rpc +npx seda-sdk oracle-program upload --rpc ``` -This will return the `wasm hash` which you can use for Data Requests. +This will return the `Oracle Program ID` which you can use for Data Requests. You can apply the following options: @@ -59,22 +59,22 @@ You can apply the following options: - --gas-adjustment - Used to scale the gas estimate with gas 'auto'. (default: 1.3) - --gas-price - Price per unit of gas in aseda. (default: 10000000000) -### Data Request binary details +### Oracle Program details -Shows all the details of an uploaded Data Request binary. The Data Request binary id is the hash of the WASM binary (which you receive from the upload command). +Shows all the details of an uploaded Oracle Program. The Oracle Program id is the hash of the WASM binary (which you receive from the upload command). Example: ```sh -npx seda-sdk wasm show --rpc +npx seda-sdk oracle-program show --rpc ``` -### List Data Request binaries +### List Oracle Programs -Shows all the WASM binaries to the SEDA chain. +Shows all the Oracle Programs to the SEDA chain. Example: ```sh -npx seda-sdk wasm list --rpc +npx seda-sdk oracle-program list --rpc ``` diff --git a/libs/dev-tools/src/cli/index.ts b/libs/dev-tools/src/cli/index.ts index e73e86c..130568d 100644 --- a/libs/dev-tools/src/cli/index.ts +++ b/libs/dev-tools/src/cli/index.ts @@ -2,14 +2,14 @@ import { Command } from "commander"; import { version } from "../../package.json"; import { RPC_ENV_KEY } from "../lib/services/config"; +import { oracleProgram } from "./oracle-program"; import { spinnerError, stopSpinner } from "./utils/spinner"; -import { wasm } from "./wasm"; const program = new Command() .description("SEDA SDK Command Line Interface (CLI)") .version(version) .addHelpText("after", "\r") - .addCommand(wasm) + .addCommand(oracleProgram) .option( "-r, --rpc [string]", `SEDA CometBFT RPC Endpoint. Attempts to reads '${RPC_ENV_KEY}' env variable if not passed.`, diff --git a/libs/dev-tools/src/cli/oracle-program/index.ts b/libs/dev-tools/src/cli/oracle-program/index.ts new file mode 100644 index 0000000..27c987a --- /dev/null +++ b/libs/dev-tools/src/cli/oracle-program/index.ts @@ -0,0 +1,10 @@ +import { Command } from "commander"; +import { list } from "./list"; +import { show } from "./show"; +import { upload } from "./upload"; + +export const oracleProgram = new Command("oracle-program"); +oracleProgram.description("commands to list, show, and upload Oracle Programs"); +oracleProgram.addCommand(upload); +oracleProgram.addCommand(list); +oracleProgram.addCommand(show); diff --git a/libs/dev-tools/src/cli/wasm/list.ts b/libs/dev-tools/src/cli/oracle-program/list.ts similarity index 69% rename from libs/dev-tools/src/cli/wasm/list.ts rename to libs/dev-tools/src/cli/oracle-program/list.ts index 94ea8f2..c021601 100644 --- a/libs/dev-tools/src/cli/wasm/list.ts +++ b/libs/dev-tools/src/cli/oracle-program/list.ts @@ -5,24 +5,22 @@ import { updateSpinnerText, } from "@dev-tools/cli-utils/spinner"; import { buildQueryConfig } from "@dev-tools/services/config"; -import { createWasmQueryClient } from "@dev-tools/services/wasm/query-client"; +import { createWasmQueryClient } from "@dev-tools/services/oracle-program/query-client"; export const list = new Command("list"); -list.description("list existing Data Request WASM binaries in the SEDA chain"); +list.description("list existing Oracle Programs in the SEDA chain"); list.action(async () => { const opts = list.optsWithGlobals(); const queryConfig = buildQueryConfig(opts); const wasmQueryClient = await createWasmQueryClient(queryConfig); - updateSpinnerText( - "Querying Data Request WASM binaries from the SEDA network", - ); + updateSpinnerText("Querying Oracle Programs from the SEDA network"); const queryResult = await wasmQueryClient.DataRequestWasms({}); const result = queryResult.list.map((hashTypePair) => { const [hash, expirationHeight] = hashTypePair.split(","); - return { hash, expirationHeight }; + return { oracleProgramId: hash, expirationHeight }; }); spinnerSuccess(); diff --git a/libs/dev-tools/src/cli/wasm/show.ts b/libs/dev-tools/src/cli/oracle-program/show.ts similarity index 70% rename from libs/dev-tools/src/cli/wasm/show.ts rename to libs/dev-tools/src/cli/oracle-program/show.ts index 2893e62..939d184 100644 --- a/libs/dev-tools/src/cli/wasm/show.ts +++ b/libs/dev-tools/src/cli/oracle-program/show.ts @@ -7,18 +7,18 @@ import { updateSpinnerText, } from "@dev-tools/cli-utils/spinner"; import { buildQueryConfig } from "@dev-tools/services/config"; -import { createWasmQueryClient } from "@dev-tools/services/wasm/query-client"; +import { createWasmQueryClient } from "@dev-tools/services/oracle-program/query-client"; import { tryAsync } from "@dev-tools/utils/try-async"; export const show = new Command("show"); -show.description("show a Data Request WASM binary in the SEDA chain"); -show.argument("", "Hash of the Data Request WASM binary"); +show.description("show an Oracle Program in the SEDA chain"); +show.argument("", "ID of the Oracle Program"); show.action(async () => { const opts = show.optsWithGlobals(); const queryConfig = buildQueryConfig(opts); const wasmQueryClient = await createWasmQueryClient(queryConfig); - updateSpinnerText("Querying Data Request WASM binary from the SEDA network"); + updateSpinnerText("Querying Oracle Program from the SEDA network"); const hash = show.args[0]; const queryResult = await tryAsync(async () => { @@ -37,13 +37,12 @@ show.action(async () => { spinnerSuccess(); console.log(); console.table({ - hash, - addedAt: wasm.addedAt, + oracleProgramId: hash, expirationHeight: wasm.expirationHeight, }); }, Nothing() { - spinnerError(`Unable to find WASM for hash "${hash}"`); + spinnerError(`Unable to find Oracle Program for id "${hash}"`); }, }); }); diff --git a/libs/dev-tools/src/cli/wasm/upload.ts b/libs/dev-tools/src/cli/oracle-program/upload.ts similarity index 56% rename from libs/dev-tools/src/cli/wasm/upload.ts rename to libs/dev-tools/src/cli/oracle-program/upload.ts index 26b0b5a..198b1d9 100644 --- a/libs/dev-tools/src/cli/wasm/upload.ts +++ b/libs/dev-tools/src/cli/oracle-program/upload.ts @@ -12,17 +12,14 @@ import { updateSpinnerText, } from "@dev-tools/cli-utils/spinner"; import { buildSigningConfig } from "@dev-tools/services/config"; +import { getOracleProgram } from "@dev-tools/services/oracle-program/get-oracle-program"; +import { uploadOracleProgram } from "@dev-tools/services/oracle-program/upload-oracle-program"; import { Signer } from "@dev-tools/services/signer"; -import { getWasmBinary } from "@dev-tools/services/wasm/get-wasm-binary"; -import { uploadWasmBinary } from "@dev-tools/services/wasm/upload-wasm-binary"; import { tryAsync } from "@dev-tools/utils/try-async"; export const upload = new Command("upload"); -upload.description("upload a Data Request WASM binary to the SEDA chain."); -upload.argument( - "", - "File path of the Data Request WASM binary.", -); +upload.description("upload an Oracle Program to the SEDA chain."); +upload.argument("", "File path of the Oracle Program."); addGasOptionsToCommand(upload); upload.action(async () => { const gasOptions = getGasOptionsFromCommand(upload); @@ -35,33 +32,38 @@ upload.action(async () => { process.exit(1); } - updateSpinnerText("Uploading Data Request WASM binary to the SEDA network"); + updateSpinnerText("Uploading Oracle Program to the SEDA network"); const filePath = upload.args[0]; - const wasmBinary = await tryAsync(async () => readFile(filePath)); - if (wasmBinary.isErr) { + const oracleProgram = await tryAsync(async () => readFile(filePath)); + if (oracleProgram.isErr) { spinnerError(`Failed to read file "${filePath}"`); - console.error(wasmBinary.error); + console.error(oracleProgram.error); process.exit(1); } - const drWasmId = keccak256(wasmBinary.value); - const wasm = await getWasmBinary(signer.value, drWasmId); + const oracleProgramId = keccak256(oracleProgram.value); + const existingOracleProgram = await getOracleProgram( + signer.value, + oracleProgramId, + ); - if (wasm.isOk) { - if (wasm.value.isJust) { - // There is already a binary with the same hash on chain + if (existingOracleProgram.isOk) { + if (existingOracleProgram.value.isJust) { + // There is already an Oracle Program with the same hash on chain spinnerSuccess(); console.table({ - wasmHash: Buffer.from(wasm.value.value.hash).toString("hex"), + oracleProgramId: Buffer.from( + existingOracleProgram.value.value.oracleProgramId, + ).toString("hex"), }); process.exit(0); } } - const response = await uploadWasmBinary( + const response = await uploadOracleProgram( signer.value, - wasmBinary.value, + oracleProgram.value, gasOptions, ); diff --git a/libs/dev-tools/src/cli/wasm/index.ts b/libs/dev-tools/src/cli/wasm/index.ts deleted file mode 100644 index 42800a7..0000000 --- a/libs/dev-tools/src/cli/wasm/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Command } from "commander"; -import { list } from "./list"; -import { show } from "./show"; -import { upload } from "./upload"; - -export const wasm = new Command("wasm"); -wasm.description("commands to list, show, and upload WASM binaries"); -wasm.addCommand(upload); -wasm.addCommand(list); -wasm.addCommand(show); diff --git a/libs/dev-tools/src/index.ts b/libs/dev-tools/src/index.ts index eeec7da..304454c 100644 --- a/libs/dev-tools/src/index.ts +++ b/libs/dev-tools/src/index.ts @@ -1,8 +1,8 @@ export { buildSigningConfig } from "@dev-tools/services/config"; export { Signer, type ISigner } from "@dev-tools/services/signer"; -export { uploadWasmBinary } from "@dev-tools/services/wasm/upload-wasm-binary"; -export { getWasmBinary } from "@dev-tools/services/wasm/get-wasm-binary"; +export { uploadOracleProgram } from "@dev-tools/services/oracle-program/upload-oracle-program"; +export { getOracleProgram } from "@dev-tools/services/oracle-program/get-oracle-program"; export { postDataRequest } from "@dev-tools/services/dr/post-data-request"; export { postDataRequestBundle } from "@dev-tools/services/dr/post-data-request-bundle"; @@ -17,5 +17,4 @@ export { postAndAwaitDataRequestBundle } from "@dev-tools/services/dr/post-and-a export * from "./lib/testing/create-mock-reveal"; export * from "./lib/testing/create-mock-tally-args"; -export * from "./lib/testing/execute-dr-wasm"; -export * from "./lib/testing/execute-tally-wasm"; +export * from "./lib/testing/test-oracle-program"; diff --git a/libs/dev-tools/src/lib/services/dr/create-dr-input.ts b/libs/dev-tools/src/lib/services/dr/create-dr-input.ts index e7f3913..f90e900 100644 --- a/libs/dev-tools/src/lib/services/dr/create-dr-input.ts +++ b/libs/dev-tools/src/lib/services/dr/create-dr-input.ts @@ -13,21 +13,21 @@ export type PostDataRequestInput = { version?: string; /** - * Hash of the uploaded Data Request binary Id + * Hash of the uploaded Oracle Program */ - drBinaryId: string; + oracleProgramId: string; /** - * Inputs encoded in bytes. Input depends on the Data Request WASM binary you are calling + * Inputs encoded in bytes. Input depends on the Oracle Program you are calling */ drInputs: Uint8Array; /** - * Hash of the uploaded Tally binary Id - * Defaults to the same hash as drBinaryId + * Hash of the uploaded Tally Oracle Program + * Defaults to the value of oracleProgramId */ - tallyBinaryId?: string; + tallyOracleProgramId?: string; /** - * Inputs encoded in bytes. Input depends on the Tally WASM binary you are calling + * Inputs encoded in bytes. Input depends on the Tally Oracle Program you are calling */ tallyInputs: Uint8Array; @@ -66,10 +66,10 @@ export type PostDataRequestInput = { export function createPostedDataRequest(input: PostDataRequestInput) { const version = input.version ?? DEFAULT_VERSION; - const dr_binary_id = input.drBinaryId; + const dr_binary_id = input.oracleProgramId; const dr_inputs = base64Encode(input.drInputs); - const tally_binary_id = input.tallyBinaryId ?? input.drBinaryId; + const tally_binary_id = input.tallyOracleProgramId ?? input.oracleProgramId; const tally_inputs = base64Encode(input.tallyInputs); const replication_factor = diff --git a/libs/dev-tools/src/lib/services/oracle-program/get-oracle-program.ts b/libs/dev-tools/src/lib/services/oracle-program/get-oracle-program.ts new file mode 100644 index 0000000..ab3ad40 --- /dev/null +++ b/libs/dev-tools/src/lib/services/oracle-program/get-oracle-program.ts @@ -0,0 +1,50 @@ +import { tryAsync } from "@dev-tools/utils/try-async"; +import { Maybe, Result } from "true-myth"; +import type { ISigner } from "../signer"; +import { createWasmQueryClient } from "./query-client"; + +export async function getOracleProgram( + signer: ISigner, + id: string, +): Promise< + Result< + Maybe<{ + oracleProgramId: string; + addedAt: Date | undefined; + bytecode: Uint8Array; + expirationHeight: number; + }>, + string + > +> { + const queryClient = await tryAsync( + async () => + await createWasmQueryClient({ + rpc: signer.getEndpoint(), + }), + ); + + if (queryClient.isErr) { + return Result.err(queryClient.error); + } + + const oracleProgram = await tryAsync( + async () => + await queryClient.value.DataRequestWasm({ + hash: id, + }), + ); + + if (oracleProgram.isErr) { + return Result.err(oracleProgram.error); + } + + const result = Maybe.of(oracleProgram.value.wasm).map((wasm) => ({ + oracleProgramId: id, + addedAt: wasm.addedAt, + bytecode: wasm.bytecode, + expirationHeight: wasm.expirationHeight, + })); + + return Result.ok(result); +} diff --git a/libs/dev-tools/src/lib/services/wasm/query-client.ts b/libs/dev-tools/src/lib/services/oracle-program/query-client.ts similarity index 100% rename from libs/dev-tools/src/lib/services/wasm/query-client.ts rename to libs/dev-tools/src/lib/services/oracle-program/query-client.ts diff --git a/libs/dev-tools/src/lib/services/wasm/upload-wasm-binary.ts b/libs/dev-tools/src/lib/services/oracle-program/upload-oracle-program.ts similarity index 85% rename from libs/dev-tools/src/lib/services/wasm/upload-wasm-binary.ts rename to libs/dev-tools/src/lib/services/oracle-program/upload-oracle-program.ts index c690d71..9544637 100644 --- a/libs/dev-tools/src/lib/services/wasm/upload-wasm-binary.ts +++ b/libs/dev-tools/src/lib/services/oracle-program/upload-oracle-program.ts @@ -6,11 +6,11 @@ import { signAndSendTx } from "../sign-and-send-tx"; import type { ISigner } from "../signer"; import { createSigningClient } from "../signing-client"; -export async function uploadWasmBinary( +export async function uploadOracleProgram( signer: ISigner, - wasmBinary: Buffer, + oracleProgram: Buffer, gasOptions: GasOptions, -): Promise> { +): Promise> { const sigingClientResult = await createSigningClient(signer); if (sigingClientResult.isErr) { return Result.err(sigingClientResult.error); @@ -22,7 +22,7 @@ export async function uploadWasmBinary( typeUrl: "/sedachain.wasm_storage.v1.MsgStoreDataRequestWasm", value: sedachain.wasm_storage.v1.MsgStoreDataRequestWasm.fromPartial({ sender: address, - wasm: await gzip(wasmBinary), + wasm: await gzip(oracleProgram), }), }; @@ -48,6 +48,6 @@ export async function uploadWasmBinary( return Result.ok({ tx: response.value.transactionHash, - wasmHash: messageResponse.hash, + oracleProgramId: messageResponse.hash, }); } diff --git a/libs/dev-tools/src/lib/services/signer.ts b/libs/dev-tools/src/lib/services/signer.ts index 7576ccd..68aa4b9 100644 --- a/libs/dev-tools/src/lib/services/signer.ts +++ b/libs/dev-tools/src/lib/services/signer.ts @@ -2,7 +2,7 @@ import { DirectSecp256k1HdWallet, type OfflineSigner, } from "@cosmjs/proto-signing"; -import { createWasmQueryClient } from "@dev-tools/services/wasm/query-client"; +import { createWasmQueryClient } from "@dev-tools/services/oracle-program/query-client"; import { tryAsync } from "@dev-tools/utils/try-async"; import { AUTO_CORE_CONTRACT_VALUE, diff --git a/libs/dev-tools/src/lib/services/wasm/get-wasm-binary.ts b/libs/dev-tools/src/lib/services/wasm/get-wasm-binary.ts deleted file mode 100644 index 6624f7d..0000000 --- a/libs/dev-tools/src/lib/services/wasm/get-wasm-binary.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { tryAsync } from "@dev-tools/utils/try-async"; -import type { sedachain } from "@seda-protocol/proto-messages"; -import { Maybe, Result } from "true-myth"; -import type { ISigner } from "../signer"; -import { createWasmQueryClient } from "./query-client"; - -export async function getWasmBinary( - signer: ISigner, - id: string, -): Promise, string>> { - const queryClient = await tryAsync( - async () => - await createWasmQueryClient({ - rpc: signer.getEndpoint(), - }), - ); - - if (queryClient.isErr) { - return Result.err(queryClient.error); - } - - const binary = await tryAsync( - async () => - await queryClient.value.DataRequestWasm({ - hash: id, - }), - ); - - if (binary.isErr) { - return Result.err(binary.error); - } - - return Result.ok(Maybe.of(binary.value.wasm)); -} diff --git a/libs/dev-tools/src/lib/testing/execute-dr-oracle-program.ts b/libs/dev-tools/src/lib/testing/execute-dr-oracle-program.ts new file mode 100644 index 0000000..e69de29 diff --git a/libs/dev-tools/src/lib/testing/execute-dr-wasm.ts b/libs/dev-tools/src/lib/testing/execute-dr-wasm.ts deleted file mode 100644 index dbd9b23..0000000 --- a/libs/dev-tools/src/lib/testing/execute-dr-wasm.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { DataRequestVmAdapter, callVm } from "@seda-protocol/vm"; -import type fetch from "node-fetch"; - -export function executeDrWasm( - wasmBinary: Buffer, - inputs: Buffer, - fetchMock?: typeof fetch, -) { - return callVm( - { - args: [inputs.toString("hex")], - envs: {}, - binary: new Uint8Array(wasmBinary), - }, - undefined, - new DataRequestVmAdapter({ fetchMock }), - ); -} diff --git a/libs/dev-tools/src/lib/testing/execute-tally-wasm.ts b/libs/dev-tools/src/lib/testing/execute-tally-wasm.ts deleted file mode 100644 index 825d1ff..0000000 --- a/libs/dev-tools/src/lib/testing/execute-tally-wasm.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { TallyVmAdapter, callVm } from "@seda-protocol/vm"; -import { createMockTallyArgs } from "./create-mock-tally-args"; - -type TallyArgs = Parameters; - -export function executeTallyWasm(wasmBinary: Buffer, ...tallyArgs: TallyArgs) { - const args = createMockTallyArgs(...tallyArgs); - - return callVm( - { - args, - envs: {}, - binary: new Uint8Array(wasmBinary), - }, - undefined, - new TallyVmAdapter(), - ); -} diff --git a/libs/dev-tools/src/lib/testing/test-oracle-program.ts b/libs/dev-tools/src/lib/testing/test-oracle-program.ts new file mode 100644 index 0000000..33dab29 --- /dev/null +++ b/libs/dev-tools/src/lib/testing/test-oracle-program.ts @@ -0,0 +1,42 @@ +import { + DataRequestVmAdapter, + TallyVmAdapter, + callVm, +} from "@seda-protocol/vm"; +import type fetch from "node-fetch"; +import { createMockTallyArgs } from "./create-mock-tally-args"; + +type TallyArgs = Parameters; + +export function testOracleProgramTally( + oracleProgram: Buffer, + ...tallyArgs: TallyArgs +) { + const args = createMockTallyArgs(...tallyArgs); + + return callVm( + { + args, + envs: {}, + binary: new Uint8Array(oracleProgram), + }, + undefined, + new TallyVmAdapter(), + ); +} + +export function testOracleProgramExecution( + oracleProgram: Buffer, + inputs: Buffer, + fetchMock?: typeof fetch, +) { + return callVm( + { + args: [inputs.toString("hex")], + envs: {}, + binary: new Uint8Array(oracleProgram), + }, + undefined, + new DataRequestVmAdapter({ fetchMock }), + ); +} diff --git a/libs/vm/README.md b/libs/vm/README.md index 360d6a2..19ff491 100644 --- a/libs/vm/README.md +++ b/libs/vm/README.md @@ -1,6 +1,6 @@ # SEDA VM -Virtual Machine used for testing your Data Request using JavaScript +Virtual Machine used for testing your Oracle Program using JavaScript # Getting started @@ -20,14 +20,14 @@ const WASM_PATH = "build/debug.wasm"; describe("index.ts", () => { it("should be able to run", async () => { - const wasmBinary = await readFile(WASM_PATH); + const oracleProgram = await readFile(WASM_PATH); const vmResult = await callVm({ // Arguments passed to the VM args: [], // Environment variables passed to the VM envs: {}, - // The WASM binary in bytes - binary: new Uint8Array(wasmBinary), + // The bytes of the Oracle Program + binary: new Uint8Array(oracleProgram), }); expect(vmResult.exitCode).toBe(0); diff --git a/libs/vm/src/index.ts b/libs/vm/src/index.ts index 2d313e8..32deb15 100644 --- a/libs/vm/src/index.ts +++ b/libs/vm/src/index.ts @@ -20,7 +20,7 @@ CURRENT_FILE_PATH.base = "worker.js"; const DEFAULT_WORKER_PATH = format(CURRENT_FILE_PATH); /** - * Executes the given WASM binary as if it were a Data Request + * Executes the given WASM binary as if it were an Oracle Program * * @param callData The call data passed to the VM * @param workerUrl URL of the compiled worker.js