Skip to content

Commit

Permalink
Merge pull request #40 from galadriel-ai/handling-failed-txs
Browse files Browse the repository at this point in the history
Separate call to mark prompts as processed
  • Loading branch information
KasparPeterson authored Mar 27, 2024
2 parents 8ef18ce + d6878e7 commit 701cd3b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/contracts_unit_tests.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 16 additions & 4 deletions contracts/contracts/ChatOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,17 @@ contract ChatOracle {
string memory errorMessage
) public onlyWhitelisted {
require(!isPromptProcessed[promptId], "Prompt already processed");
isPromptProcessed[promptId] = true;
IChatGpt(callbackAddresses[promptId]).onOracleLlmResponse(
promptCallBackId,
response,
errorMessage
);
}

function markPromptAsProcessed(uint promptId) public onlyWhitelisted {
isPromptProcessed[promptId] = true;
}

function getMessages(
uint promptId,
uint promptCallBackId
Expand Down Expand Up @@ -277,14 +280,17 @@ contract ChatOracle {
string memory errorMessage
) public onlyWhitelisted {
require(!isFunctionProcessed[functionId], "Function already processed");
isFunctionProcessed[functionId] = true;
IChatGpt(functionCallbackAddresses[functionId]).onOracleFunctionResponse(
functionCallBackId,
response,
errorMessage
);
}

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;
Expand All @@ -307,14 +313,17 @@ contract ChatOracle {
string memory errorMessage
) public onlyWhitelisted {
require(!isPromptProcessed[promptId], "Prompt already processed");
isPromptProcessed[promptId] = true;
IChatGpt(callbackAddresses[promptId]).onOracleOpenAiLlmResponse(
promptCallBackId,
response,
errorMessage
);
}

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;
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions contracts/test/ChatGpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 5 additions & 4 deletions contracts/test/ChatOracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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");
});
});
});

0 comments on commit 701cd3b

Please sign in to comment.