Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create local AWS setup #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@
- [API Docs](https://www.notion.so/tyvdh/Turing-Signing-Servers-661f3ff0de5143c1bd3c6fb52ae88dae) [Source of truth]
- [Get Setup Locally](https://youtu.be/StYbvqQnyuc) [Might be outdated]
- [Create and Upload a Contract](https://youtu.be/n_XQV53YkyA) [Might be outdated]

## Run locally

Prerequisites:
* node.js
* docker

```sh
npm install
docker-compose up -d
npm run dev-0
```

# Troubleshooting

There is an issue with `serverless-offline` on Node v15.5.0 where only GET requests will be processed properly (see [here](https://github.com/dherault/serverless-offline/issues/1151)). The solution for now is downgrading to another version of Node.
45 changes: 45 additions & 0 deletions database/create-dbs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { Pool } = require("/app/node_modules/pg")

const pool = new Pool({
host: process.env.PG_HOST,
port: process.env.PG_PORT,
user: process.env.PG_USER,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,

connectionTimeoutMillis: 9000,
idleTimeoutMillis: 9000,
max: 6,
})

createDatabases()

const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))

async function createDatabases() {
for (let attempt = 1; attempt <= 5; attempt++) {
try {
await pool.query(`SELECT 1`)
break
} catch {
console.log(`Waiting for database to come up…`)
await delay(500 * attempt)
}
}

console.log(`Creating databases…`)

for (let instance = 0; instance <= 4; instance++) {
try {
await pool.query(`CREATE DATABASE "turing-signing-server-${instance}"`)
} catch (error) {
if (error.message.match(/already exists/)) {
// ignore
} else {
throw error
}
}
}

console.log(`Databases created.`)
}
5 changes: 5 additions & 0 deletions database/migrations/001-v0.0.0-contracts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE contracts(
contract VARCHAR(64) PRIMARY KEY UNIQUE NOT NULL,
signer VARCHAR(56) UNIQUE NOT NULL,
pendingTxns VARCHAR(75)[] DEFAULT NULL
);
14 changes: 14 additions & 0 deletions database/seeds/demo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
INSERT INTO contracts (contract, signer)
SELECT 'GCNANNNLGDICM5NJOT7QD7MLW34M4WLJPTNOAXWEAR4CE4LO23FZ5WDR', 'SDRJWX2SNJTMFFGGGO4DQXL7X5SHVENZLYX6K7LCZQSIJ6XSEMTCXLHY'
ON CONFLICT DO NOTHING;

UPDATE contracts SET
pendingtxns = NULL
WHERE contract = 'GCNANNNLGDICM5NJOT7QD7MLW34M4WLJPTNOAXWEAR4CE4LO23FZ5WDR'
AND signer = 'SDRJWX2SNJTMFFGGGO4DQXL7X5SHVENZLYX6K7LCZQSIJ6XSEMTCXLHY';

UPDATE contracts SET
pendingtxns = (SELECT array_agg(DISTINCT e) FROM unnest(pendingtxns || '{"1179b9ddc9972bb8efd9039c2955e144241f981fe45ca792961f641031ca3212"}') e)
WHERE contract = 'GCNANNNLGDICM5NJOT7QD7MLW34M4WLJPTNOAXWEAR4CE4LO23FZ5WDR'
AND signer = 'SDRJWX2SNJTMFFGGGO4DQXL7X5SHVENZLYX6K7LCZQSIJ6XSEMTCXLHY'
AND NOT pendingtxns @> '{"1179b9ddc9972bb8efd9039c2955e144241f981fe45ca792961f641031ca3212"}';
51 changes: 51 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: "3.7"
services:
postgres:
image: postgres:13-alpine
environment:
POSTGRES_DATABASE: turing-signing-server-0
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
restart: unless-stopped

db_seeder:
image: andywer/plow:1.1.1
command: >
sh -c "
echo \"Initializing database turing-signing-server-0…\" &&
node /db/create-dbs.js &&
PGDATABASE=turing-signing-server-0 /app/plow.js migrate /db/migrations &&
PGDATABASE=turing-signing-server-0 /app/plow.js seed /db/seeds
"
depends_on:
- postgres
environment:
PGHOST: postgres
PGDATABASE: postgres
PGPASSWORD: postgres
PGUSER: postgres
restart: "no"
volumes:
- ./database:/db:ro

localstack:
image: localstack/localstack
network_mode: bridge
ports:
- "4566:4566"
- "4571:4571"
- "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
environment:
- SERVICES=serverless
- DEBUG=1
- DEFAULT_REGION=us-east-1
- AWS_REGION=us-east-1
- DATA_DIR=${DATA_DIR- }
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
- KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
- DOCKER_HOST=unix:///var/run/docker.sock
- HOST_TMP_FOLDER=${TMPDIR}
volumes:
- "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
28 changes: 28 additions & 0 deletions localstack/initAWS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const AWS = require("aws-sdk");

const s3 = new AWS.S3({
apiVersion: "2015-03-31",
accessKeyId: "foo",
secretAccessKey: "bar",
region: "us-east-1",
endpoint: "http://localhost:4566",
s3ForcePathStyle: true,
});

function createBucket(name) {
s3.createBucket({ Bucket: name }, function (err, bucket) {
if (err) {
console.log("Error", err);
} else {
console.log("Sucessfully created bucket", name);
}
});
}

const baseName = "turing-signing-server";
const instancesCount = 5;

for (let i = 0; i < instancesCount; i++) {
const bucketName = `${baseName}-${i}`;
createBucket(bucketName);
}
Loading