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

feat: DB driver and cache #15

Merged
merged 42 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9712372
feat: adds postgres driver and cache for user apps
commoddity Sep 7, 2024
6519d94
chore: add cache_refresh_interval to example yaml
commoddity Sep 7, 2024
297aa4c
fix: use whitelist type and value string types
commoddity Sep 7, 2024
76484a6
fix: update to use allowlists with enum
commoddity Sep 9, 2024
7b42346
fix: update throughput to rate limit and add capacity
commoddity Sep 9, 2024
c9e0955
fix: remove capacity limit field for now
commoddity Sep 9, 2024
41f5f08
fix: unit tests
commoddity Sep 9, 2024
2043664
chore: update README.md
commoddity Sep 9, 2024
1b821e5
fix: failing e2e test validate
commoddity Sep 9, 2024
2e2195a
fix: failing e2e test hydrate
commoddity Sep 9, 2024
9acc6b1
Merge branch 'main' into db-driver-and-cache
commoddity Sep 9, 2024
99c31a7
Apply suggestions from code review
commoddity Sep 10, 2024
26bd10d
fix: godoc comments in user app
commoddity Sep 10, 2024
a0f9af2
chore: plan type in driver.go
commoddity Sep 10, 2024
8ba5d98
fix: enforce db driver interface
commoddity Sep 10, 2024
aa968ef
fix: unit test for cache
commoddity Sep 10, 2024
b5e6dd4
fix: replace mockery with mockgen and create own mock file
commoddity Sep 10, 2024
581a18b
fix: improve comments for clarity
commoddity Sep 10, 2024
948bc82
chore: comments in docker-compose.yml
commoddity Sep 10, 2024
5ebcea6
chore: update README.md TOC
commoddity Sep 12, 2024
6b3d5b8
fix: temp remove allowlists
commoddity Sep 12, 2024
ba4f115
feat: rename user app to gateway endpoint
commoddity Sep 12, 2024
540dd26
feat: add getters to GatewayEndpoint
commoddity Sep 12, 2024
c7e6573
feat: add getters to GatewayEndpoint
commoddity Sep 12, 2024
8c8ac08
chore: add prerequisites section to readme
commoddity Sep 12, 2024
c675b73
chore: merge conflicts
commoddity Sep 12, 2024
0aa4d28
fix: package name in db/postrges
commoddity Sep 12, 2024
8051622
fix: move docker compose to own folder
commoddity Sep 12, 2024
27bd8a2
fix: address review comments
commoddity Sep 13, 2024
b4cd412
Apply suggestions from code review
commoddity Sep 13, 2024
63ff67b
chore: merge conflicts
commoddity Sep 13, 2024
95a6e7f
chore: move docker-compose.yml and add make targets
commoddity Sep 17, 2024
ff54f48
fix: update router test with mockgen and params
commoddity Sep 17, 2024
d04e9a5
chore: implement review suggestions
commoddity Sep 17, 2024
b903984
fix: remove user data from config
commoddity Sep 17, 2024
84ac602
chore: merge conflicts
commoddity Sep 18, 2024
38a7c95
chore: move helper func to postgres package
commoddity Sep 18, 2024
1bacf35
fix: address review comments
commoddity Sep 18, 2024
d5618cf
Merge branch 'main' into db-driver-and-cache
commoddity Sep 18, 2024
e64ad5a
chore: merge conflicts
commoddity Sep 19, 2024
6ebe8e0
chore: envoy folder structure
commoddity Sep 19, 2024
edfc376
fix: correct docker version in dockerfile
commoddity Sep 19, 2024
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ bin
go.work.sum

# Config
config.yaml
config.*.yaml
.config.yaml
.config.test.yaml

Expand All @@ -32,3 +30,7 @@ config.*.yaml
.env
.idea/
.mise.local.toml

# tmp dir
tmp
tmp/*
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ help: ## Prints all the targets in all the Makefiles

.PHONY: path_up
commoddity marked this conversation as resolved.
Show resolved Hide resolved
path_up: ## Run docker compose up
docker compose up -d
docker compose -f ./docker/docker-compose.yml up -d

.PHONY: path_up_build
path_up_build: ## Run docker compose up with build
docker compose up -d --build
docker compose -f ./docker/docker-compose.yml up -d --build

.PHONY: path_down
path_down: ## Run docker compose down
docker compose down
docker compose -f ./docker/docker-compose.yml down

#########################
### Test Make Targets ###
Expand Down Expand Up @@ -59,3 +59,12 @@ copy_test_config: ## copies the example test configuration yaml file to .gitigno
else \
echo ".config.test.yaml already exists, not overwriting."; \
fi

commoddity marked this conversation as resolved.
Show resolved Hide resolved
#######################
### SQLC Generation ###
#######################

.PHONY: sqlc_generate
sqlc_generate: ## Generate SQLC code from db/driver/sqlc/*.sql files
sqlc generate -f ./db/driver/sqlc/sqlc.yaml
Olshansk marked this conversation as resolved.
Show resolved Hide resolved

129 changes: 106 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,38 @@
</div>
<br/>

# Table of Contents <!-- omit in toc -->

- [Introduction](#introduction)
- [Quickstart (Shannon)](#quickstart-shannon)
- [Configuration](#configuration)
- [Configuration File](#configuration-file)
- [Example Configuration Format](#example-configuration-format)
- [Running PATH](#running-path)
- [Setup Config YAML](#setup-config-yaml)
- [Start the Container](#start-the-container)
- [E2E Tests](#e2e-tests)
- [Running Tests](#running-tests)

## Introduction
- [1. Introduction](#1-introduction)
- [1.1 Prerequisites](#11-prerequisites)
- [2. Quickstart (Shannon)](#2-quickstart-shannon)
- [3. Configuration](#3-configuration)
- [3.1 Configuration File](#31-configuration-file)
- [3.2 Example Configuration Format](#32-example-configuration-format)
- [4. Running PATH](#4-running-path)
- [4.1 Setup Config YAML](#41-setup-config-yaml)
- [4.2 Start the Container](#42-start-the-container)
- [5. E2E Tests](#5-e2e-tests)
- [5.1 Running Tests](#51-running-tests)
- [6. User Data](#6-user-data)
- [6.1 Updated Endpoint](#61-updated-endpoint)
- [6.2 Database Configuration](#62-database-configuration)
- [6.3 Database Schema](#63-database-schema)

## 1. Introduction
Olshansk marked this conversation as resolved.
Show resolved Hide resolved

**PATH** (Path API & Toolkit Harness) is an open source framework for enabling access to a decentralized supply network.

It provides various tools and libraries to streamline the integration and interaction with decentralized protocols.

## Quickstart (Shannon)
### 1.1 Prerequisites

- [Docker](https://docs.docker.com/get-docker/)

**Required For Development:**

- [SQLC](https://docs.sqlc.dev/)
- [Mockgen](https://github.com/uber-go/mock)

## 2. Quickstart (Shannon)

1. Stake Apps and Gateway

Expand Down Expand Up @@ -54,9 +66,9 @@ It provides various tools and libraries to streamline the integration and intera

_For detailed instructions on running PATH, see the [Running PATH](#running-path) section._

## Configuration
## 3. Configuration

### Configuration File
### 3.1 Configuration File

The configuration for PATH is defined in a YAML file, which should be named `.config.yaml`.

Expand Down Expand Up @@ -91,7 +103,7 @@ The configuration is divided into several sections:
- _Required only if the gateway operator wishes to associate user data with requests._
- Configures the PostgreSQL database connection string.

#### Example Configuration Format
### 3.2 Example Configuration Format

```yaml
shannon_config:
Expand All @@ -115,9 +127,9 @@ services:
- [Shannon](./cmd/config/testdata/shannon.example.yaml)
- [Config YAML Schema](./config/config.schema.yaml)

## Running PATH
## 4. Running PATH

#### Setup Config YAML
### 4.1 Setup Config YAML

- The PATH service requires the config YAML file to be populated.

Expand All @@ -133,7 +145,7 @@ services:

**⚠️ IMPORTANT: The data required to populate the `.config.yaml` file is sensitive and the contents of this file must never be shared outside of your organization. ⚠️**

#### Start the Container
### 4.2 Start the Container

1. Once the `.config.yaml` file is populated, to start the PATH service for a specific protocol, use the `make` target:

Expand All @@ -153,7 +165,7 @@ services:
make path_down
```

## E2E Tests
## 5. E2E Tests

This repository contains end-to-end (E2E) tests for the Shannon relay protocol. The tests ensure that the protocol behaves as expected under various conditions.

Expand All @@ -171,7 +183,7 @@ Currently, the E2E tests are configured to run against the Shannon testnet.

Future work will include adding support for other protocols.

#### Running Tests
### 5.1 Running Tests

To run the tests, use the following `make` targets:

Expand All @@ -185,3 +197,74 @@ make test_unit
# Shannon E2E test only
make test_e2e_shannon_relay
```

## 6. User Data

By default, PATH does not associate user data with service requests (i.e. relays).

You may opt to enable user data config to unlock the ability to associate a specific user with a particular service request.

This is required for:

- User-specified app settings (e.g. api auth keys, etc.)
- Metering and billing of service requests (e.g. charging users $2 per 1 million requests)
- Rate limiting of service requests by throughput (e.g. 30 req / second) and/or capacity (e.g. 1M req / month)

### 6.1 Updated Endpoint

Enabling user data will modify the endpoint for service requests to require a gateway endpoint ID at the end of the URL path.

For example:

```bash
http://eth-mainnet.localhost:3000/v1/{gateway_endpoint_id}
```

The default endpoint of `/v1` will no longer function without a gateway endpoint ID.

### 6.2 Database Configuration

To enable user data, you must set up a Postgres database and populate the `.config.yaml` file's `user_data_config` field with the connection string.

```yaml
user_data_config:
postgres_connection_string: "postgres://user:password@localhost:5432/database"
```

An example Postgres Docker configuration is included in the [docker-compose.yml](./docker/docker-compose.yml) file at the root of this repository. **However, this configuration is not recommended for production use.**

### 6.3 Database Schema

[Base Schema SQL File](./db/driver/sqlc/schema.sql)

```mermaid
Olshansk marked this conversation as resolved.
Show resolved Hide resolved
erDiagram
PLANS {
int id
varchar type
int rate_limit_throughput
int rate_limit_capacity
enum rate_limit_capacity_period
}

USER_ACCOUNTS {
varchar id
varchar plan_type
}

GATEWAY_ENDPOINTS {
varchar id
varchar account_id
varchar api_key
boolean api_key_required
}

PLANS ||--o{ USER_ACCOUNTS : "plan_type"
USER_ACCOUNTS ||--o{ GATEWAY_ENDPOINTS : "account_id"
```

A base schema is provided with the minimal tables and columns required to enable user data handling in PATH.

These tables should not be modified; instead, any additional functionality required by the gateway operator for managing user data should be added by extending the base tables and columns provided in this schema.

For example, it is up to the gateway operator to decide how they wish to manage their gateway's user data, user accounts, subscription plans, authentication, etc.
23 changes: 21 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ type (

Services map[relayer.ServiceID]ServiceConfig `yaml:"services"`
Router RouterConfig `yaml:"router_config"`
UserData UserDataConfig `yaml:"user_data_config"`
// UserDataConfig is optional and only used if user data handling is enabled
// for the gateway by setting the 'user_data_config' field in the config YAML file.
UserData *UserDataConfig `yaml:"user_data_config"`

// A map from human readable aliases (e.g. eth-mainnet) to service ID (e.g. 0021)
serviceAliases map[string]relayer.ServiceID
Expand All @@ -55,6 +57,9 @@ func LoadGatewayConfigFromYAML(path string) (GatewayConfig, error) {
// hydrate required fields and set defaults for optional fields
config.hydrateServiceAliases()
config.hydrateRouterConfig()
if config.IsUserDataEnabled() {
config.hydrateUserDataConfig()
}

return config, config.validate()
}
Expand All @@ -73,7 +78,12 @@ func (c GatewayConfig) GetRouterConfig() RouterConfig {
return c.Router
}

func (c GatewayConfig) GetUserDataConfig() UserDataConfig {
// UserDataEnabled returns true if user data handling is enabled for the Gateway.
func (c GatewayConfig) IsUserDataEnabled() bool {
return c.UserData != nil
}

func (c GatewayConfig) GetUserDataConfig() *UserDataConfig {
return c.UserData
}

Expand Down Expand Up @@ -117,6 +127,10 @@ func (c *GatewayConfig) hydrateRouterConfig() {
c.Router.hydrateRouterDefaults()
}

func (c *GatewayConfig) hydrateUserDataConfig() {
c.UserData.hydrateDefaults()
}

/* --------------------------------- Gateway Config Validation Helpers -------------------------------- */

func (c GatewayConfig) validate() error {
Expand All @@ -126,6 +140,11 @@ func (c GatewayConfig) validate() error {
if err := c.validateServiceConfig(); err != nil {
return err
}
if c.IsUserDataEnabled() {
if err := c.UserData.validate(); err != nil {
return err
}
}

return nil
}
Expand Down
Loading