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

(API) Migrate API codebase (root -> node) #2056

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions .github/workflows/api.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches-ignore:
- "master"
paths:
- "api/**"
- "node/pkg/api/**"
workflow_dispatch:

jobs:
Expand Down Expand Up @@ -45,27 +45,23 @@ jobs:
go-version: "1.22.3"
check-latest: true
cache-dependency-path: |
./api/go.sum
./node/go.sum
- name: Install golang-migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.0/migrate.linux-amd64.tar.gz | tar xvz
sudo mv ./migrate /usr/bin
- name: Migrate up
run: |
cd ./api
migrate -database "postgresql://postgres:postgres@localhost:5432/orakl-test?search_path=public&sslmode=disable" -verbose -path ./migrations up
cd ./node
migrate -database "postgresql://postgres:postgres@localhost:5432/orakl-test?search_path=public&sslmode=disable" -verbose -path ./migrations/api up
- name: Install dependencies
run: |
cd ./api
cd ./node
go mod tidy
- name: Build
run: |
cd ./api
go build
- name: Run test
run: |
cd ./api
go test ./tests -v
cd ./node
go test ./pkg/api/tests -v
env:
DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/orakl-test?search_path=public"
ENCRYPT_PASSWORD: "abc123"
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/node.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ on:
- "!node/pkg/checker/**"
- "!node/pkg/delegator/**"
- "!node/migrations/delegator/**"
- "!node/pkg/api/**"
- "!node/migrations/api/**"
workflow_dispatch:

jobs:
Expand Down
10 changes: 5 additions & 5 deletions dockerfiles/orakl-api.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY api api
COPY node node

WORKDIR /app/api
WORKDIR /app/node

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o apibin -ldflags="-w -s" .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o apibin -ldflags="-w -s" ./cmd/api/main.go

# debian:bullseye-slim
FROM debian@sha256:4b48997afc712259da850373fdbc60315316ee72213a4e77fc5a66032d790b2a
Expand All @@ -23,9 +23,9 @@ WORKDIR /app

RUN mkdir /app/migrations

COPY --from=builder /app/api/migrations /app/migrations
COPY --from=builder /app/node/migrations/api /app/migrations

COPY --from=builder /app/api/apibin /usr/bin
COPY --from=builder /app/node/apibin /usr/bin

COPY dockerfiles/start-go.sh .

Expand Down
1 change: 1 addition & 0 deletions node/cmd/api/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
73 changes: 73 additions & 0 deletions node/cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
_ "embed"
"log"

"bisonai.com/orakl/node/pkg/api/apierr"
"bisonai.com/orakl/node/pkg/api/blocks"
"bisonai.com/orakl/node/pkg/api/chain"
"bisonai.com/orakl/node/pkg/api/listener"
"bisonai.com/orakl/node/pkg/api/proxy"
"bisonai.com/orakl/node/pkg/api/reporter"
"bisonai.com/orakl/node/pkg/api/service"
"bisonai.com/orakl/node/pkg/api/utils"
"bisonai.com/orakl/node/pkg/api/vrf"

"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
)

//go:embed .version
var version string

func main() {
err := godotenv.Load()
if err != nil {
log.Println("env file is not found, continuing without .env file")
}
config, err := utils.LoadEnvVars()
if err != nil {
panic(err)
}

appConfig, err := utils.Setup(version)
if err != nil {
panic(err)
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

postgres := appConfig.Postgres
app := appConfig.App

defer postgres.Close()

v1 := app.Group("/api/v1")
SetRouter(v1)

var port string
if val, ok := config["APP_PORT"].(string); ok {
port = val
} else {
port = "3000"
}

err = app.Listen(":" + port)
if err != nil {
panic(err)
}
}

func SetRouter(_router fiber.Router) {
(_router).Get("", func(c *fiber.Ctx) error {
return c.SendString("Orakl Network API")
})

apierr.Routes(_router)
chain.Routes(_router)
listener.Routes(_router)
proxy.Routes(_router)
reporter.Routes(_router)
service.Routes(_router)
vrf.Routes(_router)
blocks.Routes(_router)
}
25 changes: 25 additions & 0 deletions node/migrations/api/000001_initialize_tables.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Drop foreign key constraints first
ALTER TABLE IF EXISTS "feeds" DROP CONSTRAINT IF EXISTS "feeds_adapter_id_fkey";
ALTER TABLE IF EXISTS "aggregates" DROP CONSTRAINT IF EXISTS "aggregates_aggregator_id_fkey";
ALTER TABLE IF EXISTS "aggregators" DROP CONSTRAINT IF EXISTS "aggregators_adapter_id_fkey";
ALTER TABLE IF EXISTS "aggregators" DROP CONSTRAINT IF EXISTS "aggregators_chain_id_fkey";
ALTER TABLE IF EXISTS "data" DROP CONSTRAINT IF EXISTS "data_aggregator_id_fkey";
ALTER TABLE IF EXISTS "data" DROP CONSTRAINT IF EXISTS "data_feed_id_fkey";
ALTER TABLE IF EXISTS "listeners" DROP CONSTRAINT IF EXISTS "listeners_chain_id_fkey";
ALTER TABLE IF EXISTS "listeners" DROP CONSTRAINT IF EXISTS "listeners_service_id_fkey";
ALTER TABLE IF EXISTS "reporters" DROP CONSTRAINT IF EXISTS "reporters_chain_id_fkey";
ALTER TABLE IF EXISTS "reporters" DROP CONSTRAINT IF EXISTS "reporters_service_id_fkey";

-- Drop tables in reverse order of creation
DROP TABLE IF EXISTS "vrf_keys";
DROP TABLE IF EXISTS "services";
DROP TABLE IF EXISTS "reporters";
DROP TABLE IF EXISTS "proxies";
DROP TABLE IF EXISTS "listeners";
DROP TABLE IF EXISTS "feeds";
DROP TABLE IF EXISTS "error";
DROP TABLE IF EXISTS "data";
DROP TABLE IF EXISTS "chains";
DROP TABLE IF EXISTS "aggregators";
DROP TABLE IF EXISTS "aggregates";
DROP TABLE IF EXISTS "adapters";
124 changes: 124 additions & 0 deletions node/migrations/api/000001_initialize_tables.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
CREATE TABLE IF NOT EXISTS "adapters" (
adapter_hash TEXT NOT NULL,
adapter_id BIGSERIAL NOT NULL,
decimals INTEGER NOT NULL,
name TEXT NOT NULL,
CONSTRAINT "adapters_adapter_hash_key" UNIQUE ("adapter_hash"),
CONSTRAINT "adapters_pkey" PRIMARY KEY ("adapter_id")
);

CREATE TABLE IF NOT EXISTS "chains" (
chain_id BIGSERIAL NOT NULL,
name TEXT NOT NULL,
CONSTRAINT "chains_name_key" UNIQUE ("name"),
CONSTRAINT "chains_pkey" PRIMARY KEY ("chain_id")
);

CREATE TABLE IF NOT EXISTS "services" (
name TEXT NOT NULL,
service_id BIGSERIAL NOT NULL,
CONSTRAINT "services_name_key" UNIQUE ("name"),
CONSTRAINT "services_pkey" PRIMARY KEY ("service_id")
);

CREATE TABLE IF NOT EXISTS "aggregators" (
absolute_threshold DOUBLE PRECISION NOT NULL,
active BOOLEAN NOT NULL DEFAULT false,
adapter_id BIGINT NOT NULL,
address TEXT NOT NULL,
aggregator_hash TEXT NOT NULL,
aggregator_id BIGSERIAL NOT NULL,
chain_id BIGINT NOT NULL,
fetcher_type INTEGER NOT NULL,
heartbeat INTEGER NOT NULL,
name TEXT NOT NULL,
threshold DOUBLE PRECISION NOT NULL,
CONSTRAINT "aggregators_address_key" UNIQUE ("address"),
CONSTRAINT "aggregators_adapter_id_fkey" FOREIGN KEY ("adapter_id") REFERENCES "public"."adapters" ("adapter_id"),
CONSTRAINT "aggregators_chain_id_fkey" FOREIGN KEY ("chain_id") REFERENCES "public"."chains" ("chain_id"),
CONSTRAINT "aggregators_pkey" PRIMARY KEY ("aggregator_id")
);

CREATE TABLE IF NOT EXISTS "aggregates" (
aggregate_id BIGSERIAL NOT NULL,
aggregator_id BIGINT NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
value BIGINT NOT NULL,
CONSTRAINT "aggregates_aggregator_id_fkey" FOREIGN KEY ("aggregator_id") REFERENCES "public"."aggregators" ("aggregator_id") ON DELETE CASCADE,
CONSTRAINT "aggregates_pkey" PRIMARY KEY ("aggregate_id")
);

CREATE TABLE IF NOT EXISTS "feeds" (
adapter_id BIGINT NOT NULL,
definition JSONB NOT NULL,
feed_id BIGSERIAL NOT NULL,
name TEXT NOT NULL,
CONSTRAINT "feeds_adapter_id_fkey" FOREIGN KEY ("adapter_id") REFERENCES "public"."adapters" ("adapter_id") ON DELETE CASCADE,
CONSTRAINT "feeds_pkey" PRIMARY KEY ("feed_id")
);

CREATE TABLE IF NOT EXISTS "data" (
aggregator_id BIGINT NOT NULL,
data_id BIGSERIAL NOT NULL,
feed_id BIGINT NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
value BIGINT NOT NULL,
CONSTRAINT "data_aggregator_id_fkey" FOREIGN KEY ("aggregator_id") REFERENCES "public"."aggregators" ("aggregator_id") ON DELETE CASCADE,
CONSTRAINT "data_pkey" PRIMARY KEY ("data_id"),
CONSTRAINT "data_feed_id_fkey" FOREIGN KEY ("feed_id") REFERENCES "public"."feeds" ("feed_id")
);

CREATE TABLE IF NOT EXISTS "error" (
code TEXT NOT NULL,
error_id BIGSERIAL NOT NULL,
name TEXT NOT NULL,
request_id TEXT NOT NULL,
stack TEXT NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT "error_pkey" PRIMARY KEY ("error_id")
);
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

CREATE TABLE IF NOT EXISTS "listeners" (
address CHARACTER VARYING(42) NOT NULL,
chain_id BIGINT NOT NULL,
event_name CHARACTER VARYING(255) NOT NULL,
listener_id BIGSERIAL NOT NULL,
service_id BIGINT NOT NULL,
CONSTRAINT "listeners_chain_id_fkey" FOREIGN KEY ("chain_id") REFERENCES "public"."chains" ("chain_id"),
CONSTRAINT "listeners_service_id_fkey" FOREIGN KEY ("service_id") REFERENCES "public"."services" ("service_id"),
CONSTRAINT "listeners_pkey" PRIMARY KEY ("listener_id")
);

CREATE TABLE IF NOT EXISTS "proxies" (
host TEXT NOT NULL,
id BIGSERIAL NOT NULL,
location TEXT,
port INTEGER NOT NULL,
protocol TEXT NOT NULL,
CONSTRAINT "proxies_protocol_host_port_key" UNIQUE ("protocol", "host", "port"),
CONSTRAINT "proxies_pkey" PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "reporters" (
address CHARACTER VARYING(42) NOT NULL,
chain_id BIGINT NOT NULL,
"oracleAddress" CHARACTER VARYING(42) NOT NULL,
"privateKey" CHARACTER VARYING(164) NOT NULL,
reporter_id BIGSERIAL NOT NULL,
service_id BIGINT NOT NULL,
CONSTRAINT "reporters_chain_id_fkey" FOREIGN KEY ("chain_id") REFERENCES "public"."chains" ("chain_id"),
CONSTRAINT "reporters_service_id_fkey" FOREIGN KEY ("service_id") REFERENCES "public"."services" ("service_id"),
CONSTRAINT "reporters_pkey" PRIMARY KEY ("reporter_id")
);

CREATE TABLE IF NOT EXISTS "vrf_keys" (
chain_id BIGINT NOT NULL,
key_hash CHARACTER VARYING(66) NOT NULL,
pk CHARACTER VARYING(130) NOT NULL,
pk_x CHARACTER VARYING(78) NOT NULL,
pk_y CHARACTER VARYING(78) NOT NULL,
sk CHARACTER VARYING(64) NOT NULL,
vrf_key_id BIGSERIAL NOT NULL,
CONSTRAINT "vrf_keys_chain_id_fkey" FOREIGN KEY ("chain_id") REFERENCES "public"."chains" ("chain_id"),
CONSTRAINT "vrf_keys_pkey" PRIMARY KEY ("vrf_key_id")
);
2 changes: 2 additions & 0 deletions node/migrations/api/000002_blocks.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS "observed_blocks";
DROP TABLE IF EXISTS "unprocessed_blocks";
10 changes: 10 additions & 0 deletions node/migrations/api/000002_blocks.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS "observed_blocks" (
service TEXT NOT NULL UNIQUE,
block_number BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS "unprocessed_blocks" (
service TEXT NOT NULL,
block_number BIGINT NOT NULL,
UNIQUE (service, block_number)
);
Loading