Skip to content

Commit

Permalink
Merge branch 'main' into feat/api-docs-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy authored Nov 10, 2023
2 parents c9ea650 + 72ae62d commit 6165062
Show file tree
Hide file tree
Showing 31 changed files with 1,201 additions and 943 deletions.
92 changes: 92 additions & 0 deletions catalyst-gateway/event-db/Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# This is a Dockerfile for building a container image for the Catalyst Event Database.
# The container image is built from scratch and includes the necessary files for running
# the database and its associated software.

VERSION 0.7

# cspell: words

# Event db builder target. Prepares all necessary artifacts.
# Arguments:
# * with_historic_data: add historic data from the `historic_data` dir (default `true`).
# * with_test_data: add test data from the `test_data` dir (default `true`).
builder:
FROM github.com/input-output-hk/catalyst-ci/earthly/postgresql:v2.0.7+postgres-base

ARG with_historic_data=true
ARG with_test_data=true

WORKDIR /build

COPY refinery.toml .
COPY --dir migrations .
RUN mkdir data

IF [ $with_historic_data = true ]
COPY --dir historic_data .

RUN python3 historic_data/fund_2/mk_fund2_sql.py historic_data/fund_2/fund2_database_encrypted.sqlite3 >> historic_data/fund_2.sql
RUN python3 historic_data/fund_3/mk_fund3_sql.py historic_data/fund_3/fund3_database_encrypted.sqlite3 >> historic_data/fund_3.sql
RUN python3 historic_data/fund_4/mk_fund4_sql.py historic_data/fund_4/fund4_database_encrypted.sqlite3 >> historic_data/fund_4.sql
RUN python3 historic_data/fund_5/mk_fund5_sql.py historic_data/fund_5/fund5_database_encrypted.sqlite3 >> historic_data/fund_5.sql
RUN python3 historic_data/fund_6/mk_fund6_sql.py historic_data/fund_6/fund6_database_encrypted.sqlite3 >> historic_data/fund_6.sql
RUN python3 historic_data/fund_7/mk_fund7_sql.py historic_data/fund_7/fund7_database_encrypted.sqlite3 >> historic_data/fund_7.sql
RUN python3 historic_data/fund_8/mk_fund8_sql.py historic_data/fund_8/fund8_database_encrypted.sqlite3 >> historic_data/fund_8.sql
RUN python3 historic_data/fund_9/mk_fund9_sql.py historic_data/fund_9/fund9_database_encrypted.sqlite3 >> historic_data/fund_9.sql
RUN python3 historic_data/fund_9/mk_fund9_voteplan_sql.py historic_data/fund_9/fund9_database_encrypted.sqlite3 >> historic_data/fund_9.sql

RUN mv ./historic_data/*.sql ./data
END

IF [ $with_test_data = true ]
COPY --dir test_data .

RUN mv ./test_data/*.sql ./data
END

DO github.com/input-output-hk/catalyst-ci/earthly/postgresql:v2.0.7+BUILDER

check:
FROM +builder

DO github.com/input-output-hk/catalyst-ci/earthly/postgresql:v2.0.7+CHECK

format:
LOCALLY

DO github.com/input-output-hk/catalyst-ci/earthly/postgresql:v2.0.7+FORMAT --src=$(echo ${PWD})

# Build event db docker image.
# Arguments:
# * tag: docker image `tag`.
# * registry: docker image `registry`.
# * with_historic_data: add historic data from the `historic_data` dir (default `true`).
# * with_test_data: add test data from the `test_data` dir (default `true`).
build:
ARG with_historic_data=true
ARG with_test_data=true
ARG tag="latest"
ARG registry

FROM +builder --with_historic_data=$with_historic_data --with_test_data=$with_test_data

DO github.com/input-output-hk/catalyst-ci/earthly/postgresql:v2.0.7+BUILD --tag=$tag --registry=$registry --image_name=event-db

test:
FROM github.com/input-output-hk/catalyst-ci/earthly/postgresql:v2.0.7+postgres-base

COPY github.com/input-output-hk/catalyst-ci/earthly/utils:v2.0.7+shell-assert/assert.sh .

COPY ./docker-compose.yml .
WITH DOCKER \
--compose docker-compose.yml \
--load event-db:latest=(+build --with_historic_data=false) \
--service event-db \
--allow-privileged
RUN sleep 5;\
res=$(psql postgresql://catalyst-event-dev:CHANGE_ME@0.0.0.0:5432/CatalystEventDev -c "SELECT COUNT(*) FROM event");\

source assert.sh;\
expected=$(printf " count \n-------\n 5\n(1 row)");\
assert_eq "$expected" "$res"
END
62 changes: 7 additions & 55 deletions catalyst-gateway/event-db/Readme.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,27 @@
# Catalyst Event Database

This crate defines the structure and RUST access methods for the Catalyst Event Database.
This crate defines the necessary migrations, seed data and docker image builder for the Catalyst Event Database.

## Starting a Local Test DB with Docker and Earthly

Firstly you will need to prepare a docker images with all migrations and data.

Prepare a event-db docker image with the historic data
(from the root directory)
Prepare a event-db docker image with the historic and test data

```sh
earthly ./containers/event-db-migrations+docker
```

Prepare a event-db docker image with the test data
(from the root directory)

```sh
earthly ./containers/event-db-migrations+docker --data=test
earthly ./catalyst-gateway/event-db+build --tag "latest" --registry "" --with_historic_data true --with_test_data true
```

Run a event db docker container
(from the root directory)

```sh
docker-compose -f src/event-db/docker-compose.yml up migrations
docker-compose -f catalyst-gateway/event-db/docker-compose.yml up event-db
```

This will run postgres on port `5432`

## GraphQL
This will run postgres on port `5432`.

GraphQL is ONLY used for the admin interface.
It is configured with the `setup/graphql-setup.sql`.

For local testing, make sure the local `event-db` is setup and running, as described above.
Then:
To test that docker image builds fine and migrations correctly applies run

```sh
setup/start-graphql.sh
earthly -P ./catalyst-gateway/event-db+test
```

See <https://www.graphile.org/postgraphile/> for documentation on the GraphQL server.

### GraphQL Users

There are two GraphQL Users:

* `cat_admin`: Full admin access to the database.
* `cat_anon`: Unauthenticated read-only access to the database.

To authenticate, as the `cat_admin` user, execute the `authenticate` mutation.
This will return a Signed JWT Token for the user.
The JWT Token needs to be included as a `bearer` token `Authorization` header in subsequent requests.
Authentication only last 1 Hour, after which a new token must be requested.

When authenticated as the `cat_admin` user, new `cat_admin` users can be registered with the `registerAdmin` mutation.

Further Security Roles or Admin management functions can be added as required.

#### Authentication API

The GraphQL server exposes 2 **Mutations** that are used for security.

If the user is NOT authenticated, they can not update any data in the database, only read data.

To authenticate run the `authenticate` mutation with the email address and password of the user to be authenticated.
If successful, this mutation will return a JWT which will have 1 hr of Life.
Place the returned JWT in an `Authorization: Bearer <JWT>` header for all subsequent calls.

`currentAcct` query will return the authenticated user's current account details and role.

To register a new user, the `registerAdmin` mutation can be used.
It will only work if the user is properly authenticated with a `cat_admin` role.
37 changes: 12 additions & 25 deletions catalyst-gateway/event-db/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
version: "3"

services:
postgres:
image: postgres:14
restart: unless-stopped
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: CatalystEventDev
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 2s
timeout: 5s
retries: 10
ports:
- 5432:5432
# cspell: words

migrations:
image: migrations:latest
services:
event-db:
image: event-db:latest
environment:
# Required environment variables for migrations
- DB_HOST=postgres
- DB_HOST=localhost
- DB_PORT=5432
- DB_NAME=CatalystEventDev
- DB_DESCRIPTION="Catalyst Event DB"
- DB_SUPERUSER=postgres
- DB_SUPERUSER_PASSWORD=postgres
- DB_USER=catalyst-event-dev
- DB_USER_PASSWORD=CHANGE_ME
- ADMIN_ROLE_PASSWORD=CHANGE_ME
- ADMIN_USER_PASSWORD=CHANGE_ME
- ANON_ROLE_PASSWORD=CHANGE_ME
- STAGE=dev
depends_on:
postgres:
condition: service_healthy

- INIT_AND_DROP_DB=true
- WITH_MIGRATIONS=true
- WITH_SEED_DATA=true
ports:
- 5432:5432
91 changes: 49 additions & 42 deletions catalyst-gateway/event-db/historic_data/fund_0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,54 @@ DELETE FROM event WHERE row_id = 0;
-- Create the Event record for Fund 0

INSERT INTO event
(row_id, name, description,
start_time,
end_time,
registration_snapshot_time,
snapshot_start,
voting_power_threshold,
max_voting_power_pct,
insight_sharing_start,
proposal_submission_start,
refine_proposals_start,
finalize_proposals_start,
proposal_assessment_start,
assessment_qa_start,
voting_start,
voting_end,
tallying_end,
block0,
block0_hash,
committee_size,
committee_threshold)
(
row_id, name, description,
start_time,
end_time,
registration_snapshot_time,
snapshot_start,
voting_power_threshold,
max_voting_power_pct,
insight_sharing_start,
proposal_submission_start,
refine_proposals_start,
finalize_proposals_start,
proposal_assessment_start,
assessment_qa_start,
voting_start,
voting_end,
tallying_end,
block0,
block0_hash,
committee_size,
committee_threshold
)
VALUES

(0, 'Catalyst Fund 0', 'Catalyst Internal Test Fund - Voting by Focus Group',
'2020-05-22 00:00:00', -- Start Time - Date accurate, time not known.
'2020-06-24 00:00:00', -- End Time - Date accurate, time not known.
'2020-06-08 00:00:00', -- Registration Snapshot Time - Date accurate, time not known.
'2020-06-08 00:00:00', -- Snapshot Start - Date Assumed, time not known.
1, -- Voting Power Threshold - Unknown, assume 1
100, -- Max Voting Power PCT - No max% threshold used in this fund.
NULL, -- Insight Sharing Start - None
'2020-05-22 00:00:00', -- Proposal Submission Start - Date accurate, time not known.
NULL, -- Refine Proposals Start - Date accurate, time not known.
'2020-05-29 00:00:00', -- Finalize Proposals Start - Date accurate, time not known.
NULL, -- Proposal Assessment Start - None
NULL, -- Assessment QA Start - None
'2020-06-15 00:00:00', -- Voting Starts - Date Accurate, time not known.
'2020-06-19 00:00:00', -- Voting Ends - Date Accurate, time not known.
'2020-06-19 12:00:00', -- Tallying Ends - Date Accurate, time not known.
NULL, -- Block 0 Data - Not Known
NULL, -- Block 0 Hash - Not Known
0, -- Committee Size - Not Known
0 -- Committee Threshold - Not Known
);

(
0, 'Catalyst Fund 0', 'Catalyst Internal Test Fund - Voting by Focus Group',
'2020-05-22 00:00:00', -- Start Time - Date accurate, time not known.
'2020-06-24 00:00:00', -- End Time - Date accurate, time not known.
-- Registration Snapshot Time - Date accurate, time not known.
'2020-06-08 00:00:00',
'2020-06-08 00:00:00', -- Snapshot Start - Date Assumed, time not known.
1, -- Voting Power Threshold - Unknown, assume 1
-- Max Voting Power PCT - No max% threshold used in this fund.
100,
NULL, -- Insight Sharing Start - None
-- Proposal Submission Start - Date accurate, time not known.
'2020-05-22 00:00:00',
-- Refine Proposals Start - Date accurate, time not known.
NULL,
-- Finalize Proposals Start - Date accurate, time not known.
'2020-05-29 00:00:00',
NULL, -- Proposal Assessment Start - None
NULL, -- Assessment QA Start - None
'2020-06-15 00:00:00', -- Voting Starts - Date Accurate, time not known.
'2020-06-19 00:00:00', -- Voting Ends - Date Accurate, time not known.
'2020-06-19 12:00:00', -- Tallying Ends - Date Accurate, time not known.
NULL, -- Block 0 Data - Not Known
NULL, -- Block 0 Hash - Not Known
0, -- Committee Size - Not Known
0 -- Committee Threshold - Not Known
);
Loading

0 comments on commit 6165062

Please sign in to comment.