diff --git a/.github/workflows/contracts_unit_tests.yml b/.github/workflows/contracts_unit_tests.yml new file mode 100644 index 0000000..a8e85d7 --- /dev/null +++ b/.github/workflows/contracts_unit_tests.yml @@ -0,0 +1,25 @@ +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: 20 + cache: 'npm' + cache-dependency-path: contracts/package-lock.json + - name: "Install Dependencies" + id: install + run: cd contracts && npm ci + - name: "Backend Unit tests" + run: | + cd contracts && npm run test 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; + } } 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"); }); }); });