Skip to content

Commit

Permalink
Fortune/recording oracle (#204)
Browse files Browse the repository at this point in the history
* Developed Recording Oracle basic structure

* Removed console log

* Added get manifest method

* Added escrow contract interface plugin

* Added recording oracle and reputation oracle implementations. Added curses and uniqueness checks. Refactoring.

* Updated license

* Updated logic after local tests

* Updated request body scheme

* Changed tsconfig file. Added error handling.

* Added lint command

* Removed tests

* Added lint fix

* Added basic tests

* Added .env.test

* Added vercel config

* Added vercel config

* Added configs to tests

* Updated env.test config
  • Loading branch information
eugenvoronov authored Feb 3, 2023
1 parent 089e93e commit 13ec4d9
Show file tree
Hide file tree
Showing 44 changed files with 1,488 additions and 764 deletions.
1 change: 1 addition & 0 deletions packages/core/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const config: HardhatUserConfig = {
goerli: process.env.ETHERSCAN_API_KEY || '',
polygon: process.env.POLYGONSCAN_API_KEY || '',
polygonMumbai: process.env.POLYGONSCAN_API_KEY || '',
polygon: process.env.POLYGONSCAN_API_KEY || '',
bscTestnet: process.env.BSC_TESTNET_API_KEY || '',
moonbaseAlpha: process.env.MOONSCAN_API_KEY || '',
},
Expand Down
1 change: 0 additions & 1 deletion packages/examples/fortune/recording-oracle/.dockerignore

This file was deleted.

22 changes: 22 additions & 0 deletions packages/examples/fortune/recording-oracle/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
NODE_ENV=development
LOG_LEVEL=info
API_HOST=localhost
API_PORT=3001

ETH_PRIVATE_KEY=df57089febbacf7ba0bc227dafbffa9fc08a93fdc68e1e42411a14efcf23656e
SALT=kD09c4Elbl
REPORT_GAS=true
TS_NODE_TRANSPILE_ONLY=1

TENDERLY_FORK_ID=
ETHERSCAN_API_KEY=
POLYGONSCAN_API_KEY=

ETH_NODE_URL=

S3_HOST=localhost
S3_PORT=9000
S3_ACCESS_KEY=access-key
S3_SECRET_KEY=secret-key
S3_BUCKET_NAME=fortune-manifests
S3_BASE_URL=http://localhost
33 changes: 25 additions & 8 deletions packages/examples/fortune/recording-oracle/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
FROM node:16.13-alpine3.14
FROM node:18-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN npm install -g pnpm
RUN pnpm install --frozen-lockfile

WORKDIR /usr/src/app
FROM node:18-alpine AS builder
ARG APP_ENV
WORKDIR /app
COPY . .
COPY .env.$APP_ENV .env
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build

COPY package.json yarn.lock ./

RUN apk add python3 make gcc g++
RUN yarn

COPY ./src ./src
FROM node:18-alpine AS runner
WORKDIR /usr/app
ARG APP_ENV
COPY --from=builder /app/build ./build
COPY package.json ./
COPY .env.$APP_ENV .env
RUN npm install -g pnpm
RUN pnpm install --prod
USER node
ENV NODE_ENV="production"
CMD ["npm", "start"]
21 changes: 21 additions & 0 deletions packages/examples/fortune/recording-oracle/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Human Protocol

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 49 additions & 0 deletions packages/examples/fortune/recording-oracle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Fortune Recording Oracle

Fortune Recording Oracle

## Set Up

- Install the dependencies.

```bash
yarn install
```

or npm/yarn

- Start the server in development mode.

```bash
yarn dev
```

or npm/yarn

## Env vars

Loaded from `.env` file, with schema validation

## Backend API Development

There are a number of handy commands you can run to help with development.

|Command | Action |
|---|---|
|`yarn run dev` | Run the server in dev mode, automatically restarts on file change |
|`yarn build`| Compile TypeScript to JavaScript |
|`yarn start`| Start JavaScript from 'build' directory |
|`yarn test`| Run unit tests (run `yarn build` before) |
|`yarn test:watch`| Run backend tests in watch mode, running on changed test files |
|`yarn lint`| Run eslint |
|`yarn lint:fix`| Run eslint in fix mode |

## CI

Run tests on push/PR to 'main' branch
Check `.github/workflows/CI.yml`

## Recommended Vscode Extensions

[Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
[ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
6 changes: 0 additions & 6 deletions packages/examples/fortune/recording-oracle/babel.config.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/examples/fortune/recording-oracle/hardhat.config.js

This file was deleted.

7 changes: 0 additions & 7 deletions packages/examples/fortune/recording-oracle/jest.config.ts

This file was deleted.

68 changes: 51 additions & 17 deletions packages/examples/fortune/recording-oracle/package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,63 @@
{
"name": "recording-oracle",
"name": "@human-protocol/fortune-recording-oracle",
"description": "Fortune Recording Oracle",
"version": "1.0.0",
"description": "fortune recording oracle",
"main": "index.ts",
"author": "human-protocol",
"license": "MIT",
"private": false,
"devDependencies": {
"@babel/preset-typescript": "^7.18.6",
"@jest/globals": "^29.3.1",
"@types/body-parser": "^1.19.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.14",
"@types/node": "^18.11.9",
"typescript": "^4.9.3"
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"build": "tsc",
"start:prod": "ts-node build/src/index.js",
"start": "ts-node ./src/index.ts",
"test": "concurrently -k -s first --hide 0 \"hardhat node --port 8546\" \"sleep 5 && cross-env RPC_PORT=8546 yarn workspace @human-protocol/core deploy:local && vitest --run\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/humanprotocol/human-protocol.git"
},
"engines": {
"node": ">=16.0.0"
},
"keywords": [
"forune",
"recording",
"oracle"
],
"bugs": {
"url": "https://github.com/humanprotocol/human-protocol/issues"
},
"homepage": "https://github.com/humanprotocol/human-protocol/tree/main/packages/examples/fortune/recording-oracle#readme",
"dependencies": {
"@fastify/cors": "^8.2.0",
"@human-protocol/core": "workspace:*",
"axios": "^1.1.3",
"@sinclair/typebox": "^0.23.5",
"ajv": "^8.11.2",
"aws-sdk": "^2.1294.0",
"axios": "^1.2.2",
"dotenv": "^16.0.3",
"env-schema": "^5.1.1",
"fastify": "^4.10.2",
"fastify-plugin": "^3.0.1",
"store2": "^2.14.2",
"uuid": "^9.0.0",
"web3": "^1.8.1"
},
"scripts": {
"build": "tsc",
"start:prod": "ts-node build/src/index.js",
"start": "ts-node src/index.ts",
"test": "concurrently -k -s first --hide 0 \"hardhat node --port 8547\" \"jest\""
"devDependencies": {
"@aws-sdk/types": "^3.226.0",
"@types/node": "^18.11.15",
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"esbuild": "^0.14.54",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"pino-pretty": "^8.1.0",
"prettier": "^2.8.0",
"tsx": "^3.12.1",
"typescript": "^4.9.3",
"vitest": "^0.25.8"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum EscrowStatus {
'Launched' = 0,
'Pending' = 1,
'Partial' = 2,
'Paid' = 3,
'Complete' = 4,
'Cancelled' = 5,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IEscrowNetwork } from '../interfaces/networks';

export enum ChainId {
POLYGON = 137,
POLYGON_MUMBAI = 80001,
LOCALHOST = 1338,
}

export const ESCROW_NETWORKS: {
[chainId in ChainId]: IEscrowNetwork;
} = {
[ChainId.POLYGON]: {
chainId: ChainId.POLYGON,
title: 'Polygon',
rpcUrl: 'https://polygon-rpc.com',
factoryAddress: '0x15D55Cb5d9Df6273B296745C3585a94574d2fDd7',
hmtAddress: '0xc748B2A084F8eFc47E086ccdDD9b7e67aEb571BF',
},
[ChainId.POLYGON_MUMBAI]: {
chainId: ChainId.POLYGON_MUMBAI,
title: 'Polygon Mumbai',
rpcUrl: 'https://rpc-mumbai.maticvigil.com',
factoryAddress: '0x79aE9b3Ad106AEdc1F813AaD98f550FADd9e2254',
hmtAddress: '0xc2B8bb720e5df43e6E13b84B27dF5543B3485EA4',
},
[ChainId.LOCALHOST]: {
chainId: ChainId.LOCALHOST,
title: 'Localhost',
rpcUrl: 'http://127.0.0.1:8546',
factoryAddress: '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9',
hmtAddress: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
},
};
59 changes: 20 additions & 39 deletions packages/examples/fortune/recording-oracle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
import Web3 from 'web3';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { addFortune } from './services/fortune';
import getServer from './server';

const app = express();

const port = process.env.PORT || 3005;

const privKey =
process.env.ETH_PRIVATE_KEY ||
'59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d'; // ganaches priv key
const ethHttpServer = process.env.ETH_HTTP_SERVER || 'http://127.0.0.1:8545';
const web3 = new Web3(ethHttpServer);
const account = web3.eth.accounts.privateKeyToAccount(`0x${privKey}`);

web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;

app.use(bodyParser.json());

app.use(cors());

app.post('/job/results', async (req, res) => {
try {
const { workerAddress, escrowAddress, fortune } = req.body;
const err = await addFortune(web3, workerAddress, escrowAddress, fortune);
if (err) {
return res.status(400).send(err);
}
process.on('unhandledRejection', (err) => {
console.error(err);
process.exit(1);
});

return res.status(201).send();
} catch (err) {
return res.status(500).send(err);
const startServer = async () => {
const server = await getServer();
const port = +server.config.API_PORT;
const host = server.config.API_HOST;
await server.listen({ host, port });

for (const signal of ['SIGINT', 'SIGTERM']) {
process.on(signal, () =>
server.close().then((err) => {
console.log(`close application on ${signal}`);
process.exit(err ? 1 : 0);
})
);
}
});
};

app.listen(port, () => {
// TODO: Implement logger
// eslint-disable-next-line no-console
console.log(`Recording Oracle server listening port ${port}`);
});
startServer();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ChainId } from '../constants/networks';
import { IFortuneStorage } from './storage';

export interface IFortuneRequest {
fortune: string;
workerAddress: string;
escrowAddress: string;
chainId: ChainId;
}

export interface IFortuneResults {
escrowAddress: string;
chainId: number;
fortunes: {
[workerAddress: string]: IFortuneStorage;
};
}

export interface IRecordingOracleRequest {
[escrowAddress: string]: {
chainId: number;
fortunes: {
[workerAddress: string]: {
fortune: string;
score: boolean;
};
};
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Web3 from 'web3';

export interface IEscrowNetwork {
chainId: number;
title: string;
rpcUrl: string;
factoryAddress: string;
hmtAddress: string;
}

export interface IWeb3MultiNetwork {
[chainId: number]: Web3;
}
Loading

2 comments on commit 13ec4d9

@vercel
Copy link

@vercel vercel bot commented on 13ec4d9 Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

fortune-reputation-oracle-mumbai – ./packages/examples/fortune/reputation-oracle

fortune-reputation-oracle-mumbai.vercel.app
fortune-reputation-oracle-mumbai-portuu3-humanprotocol.vercel.app
fortune-reputation-oracle-mumbai-humanprotocol.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 13ec4d9 Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

job-launcher-client – ./packages/examples/fortune/launcher/client

Please sign in to comment.