From 35a0a2164f6560fd00fc84ddb6b76168abb0ed06 Mon Sep 17 00:00:00 2001 From: Kaspar Peterson Date: Wed, 27 Mar 2024 14:14:00 +0200 Subject: [PATCH 1/3] Separate call to mark prompts as processed --- contracts/contracts/ChatOracle.sol | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/contracts/contracts/ChatOracle.sol b/contracts/contracts/ChatOracle.sol index 95d531e..4de24f9 100644 --- a/contracts/contracts/ChatOracle.sol +++ b/contracts/contracts/ChatOracle.sol @@ -228,7 +228,6 @@ contract ChatOracle { string memory errorMessage ) public onlyWhitelisted { require(!isPromptProcessed[promptId], "Prompt already processed"); - isPromptProcessed[promptId] = true; IChatGpt(callbackAddresses[promptId]).onOracleLlmResponse( promptCallBackId, response, @@ -236,6 +235,10 @@ contract ChatOracle { ); } + function markPromptAsProcessed(uint promptId) public onlyWhitelisted { + isPromptProcessed[promptId] = true; + } + function getMessages( uint promptId, uint promptCallBackId @@ -277,7 +280,6 @@ contract ChatOracle { string memory errorMessage ) public onlyWhitelisted { require(!isFunctionProcessed[functionId], "Function already processed"); - isFunctionProcessed[functionId] = true; IChatGpt(functionCallbackAddresses[functionId]).onOracleFunctionResponse( functionCallBackId, response, @@ -285,6 +287,10 @@ contract ChatOracle { ); } + function markFunctionAsProcessed(uint functionId) public onlyWhitelisted { + isFunctionProcessed[functionId] = true; + } + function createOpenAiLlmCall(uint promptCallbackId, IOracleTypes.OpenAiRequest memory config) public returns (uint i) { uint promptId = promptsCount; callbackAddresses[promptId] = msg.sender; @@ -307,7 +313,6 @@ contract ChatOracle { string memory errorMessage ) public onlyWhitelisted { require(!isPromptProcessed[promptId], "Prompt already processed"); - isPromptProcessed[promptId] = true; IChatGpt(callbackAddresses[promptId]).onOracleOpenAiLlmResponse( promptCallBackId, response, @@ -315,6 +320,10 @@ contract ChatOracle { ); } + function markOpenAiPromptAsProcessed(uint promptId) public onlyWhitelisted { + isPromptProcessed[promptId] = true; + } + function createGroqLlmCall(uint promptCallbackId, IOracleTypes.GroqRequest memory config) public returns (uint i) { uint promptId = promptsCount; callbackAddresses[promptId] = msg.sender; @@ -337,11 +346,14 @@ contract ChatOracle { string memory errorMessage ) public onlyWhitelisted { require(!isPromptProcessed[promptId], "Prompt already processed"); - isPromptProcessed[promptId] = true; IChatGpt(callbackAddresses[promptId]).onOracleGroqLlmResponse( promptCallBackId, response, errorMessage ); } + + function markGroqPromptAsProcessed(uint promptId) public onlyWhitelisted { + isPromptProcessed[promptId] = true; + } } From fd55be994a3e797878129b35c6da35b5ff97e442 Mon Sep 17 00:00:00 2001 From: Kaspar Peterson Date: Wed, 27 Mar 2024 14:29:47 +0200 Subject: [PATCH 2/3] Adds unit test CI. --- .github/workflows/unit_tests.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/unit_tests.yml diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml new file mode 100644 index 0000000..4017d71 --- /dev/null +++ b/.github/workflows/unit_tests.yml @@ -0,0 +1,24 @@ +name: Unit tests + +on: [ push ] + +jobs: + unit-tests: + runs-on: ubuntu-latest + strategy: + max-parallel: 5 + + steps: + - uses: actions/checkout@v4 + - name: "Setup Node.js" + id: setup-node + uses: actions/setup-node@v4 + with: + node-version-file: .node-version + cache: npm + - name: "Install Dependencies" + id: install + run: cd contracts && npm ci + - name: "Backend Unit tests" + run: | + cd contracts && npm run test From d6878e7008b7c4f04b224efad6bd4dba31a15987 Mon Sep 17 00:00:00 2001 From: Kaspar Peterson Date: Wed, 27 Mar 2024 14:30:33 +0200 Subject: [PATCH 3/3] Fixes unit tests --- .../{unit_tests.yml => contracts_unit_tests.yml} | 5 +++-- contracts/test/ChatGpt.ts | 1 + contracts/test/ChatOracle.ts | 9 +++++---- 3 files changed, 9 insertions(+), 6 deletions(-) rename .github/workflows/{unit_tests.yml => contracts_unit_tests.yml} (80%) diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/contracts_unit_tests.yml similarity index 80% rename from .github/workflows/unit_tests.yml rename to .github/workflows/contracts_unit_tests.yml index 4017d71..a8e85d7 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/contracts_unit_tests.yml @@ -14,8 +14,9 @@ jobs: id: setup-node uses: actions/setup-node@v4 with: - node-version-file: .node-version - cache: npm + node-version: 20 + cache: 'npm' + cache-dependency-path: contracts/package-lock.json - name: "Install Dependencies" id: install run: cd contracts && npm ci diff --git a/contracts/test/ChatGpt.ts b/contracts/test/ChatGpt.ts index 9bf799b..208d0b8 100644 --- a/contracts/test/ChatGpt.ts +++ b/contracts/test/ChatGpt.ts @@ -86,6 +86,7 @@ describe("ChatGpt", function () { await chatGpt.startChat("Hello"); await oracle.connect(oracleAccount).addResponse(0, 0, "Hi", ""); + await oracle.connect(oracleAccount).markPromptAsProcessed(0); await expect( oracle.connect(oracleAccount).addResponse(0, 0, "Hi", "") ).to.be.revertedWith("Prompt already processed"); diff --git a/contracts/test/ChatOracle.ts b/contracts/test/ChatOracle.ts index 1bb1ebe..bc079d9 100644 --- a/contracts/test/ChatOracle.ts +++ b/contracts/test/ChatOracle.ts @@ -2,7 +2,7 @@ import {loadFixture,} from "@nomicfoundation/hardhat-toolbox/network-helpers"; import {expect} from "chai"; import {ethers} from "hardhat"; -describe("ChatGpt", function () { +describe("ChatOracle", function () { // We define a fixture to reuse the same setup in every test. // We use loadFixture to run this setup once, snapshot that state, // and reset Hardhat Network to that snapshot in every test. @@ -20,7 +20,8 @@ describe("ChatGpt", function () { describe("Deployment", function () { it("Can update attestation", async () => { const {oracle, owner, allSigners} = await loadFixture(deploy); - await oracle.addAttestation(allSigners[1].address, "attestation"); + await oracle.connect(owner).updateWhitelist(allSigners[1], true); + await oracle.connect(allSigners[1]).addAttestation("attestation"); const attestationOwner = await oracle.latestAttestationOwner(); const attestation = await oracle.attestations(attestationOwner); @@ -30,8 +31,8 @@ describe("ChatGpt", function () { const {oracle, owner, allSigners} = await loadFixture(deploy); await expect( - oracle.connect(allSigners[1]).addAttestation(allSigners[1].address, "attestation") - ).to.be.rejectedWith("Caller is not owner"); + oracle.connect(allSigners[1]).addAttestation("attestation") + ).to.be.rejectedWith("Caller is not whitelisted"); }); }); });