Skip to content

Commit

Permalink
Merge pull request #75 from Vovke/api-specs
Browse files Browse the repository at this point in the history
fix: server_status returns instance_id
  • Loading branch information
Slesarew authored Oct 10, 2024
2 parents 632d19b + fe0425f commit b2e1bd2
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 15 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

All notable changes to this project will be documented in this file.

## [2.1.2] - 2024-10-07
## [0.2.2] - 2024-10-10

### 🚀 Features

- Docker environment for chopsticks and compose to spawn 4 chopsticks instances in parallel looking at different RPCs

### 🐛 Bug Fixes

- Server_status API request returns instance_id instead of placeholder
- Mark_paid function will mark order correctly now

## [0.2.1] - 2024-10-07

Making the order request work according to specs in the [specs](https://alzymologist.github.io/kalatori-api/#/).
Using the tests from [kalatori-api-test-suite]() in order to validate.
Expand Down
12 changes: 11 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## Preparing development environment

It's possible to mimic to spawn chopsticks instances in parallel for development purposes.
Chopsticks Dockerfile exposes 4 ports (8000, 8500, 9000, 9500), so you can spawn 4 instances of chopsticks and each one of them will look at different RPC.
Note that the RPCs are not real, so the changes made on one chopsticks instance will not affect the others.

1. `cd chopsticks`
2. `docker compose up`, in case you want to just 2 instances edit the docker-compose.yml file
3. start the app with `KALATORI_CONFIG` environment variable pointing to `configs/chopsticks.toml`

## Version Bumping and Release Process

When you make changes that require a new version of the project, follow these steps to bump the version:
Expand All @@ -22,7 +32,7 @@ When you make changes that require a new version of the project, follow these st
git push origin <branch-name>
```

4. **Tag the version**:
4. **Tag the version at main branch**:
```bash
git tag -a v2.1.2 -m "Release version 2.1.2"
git push origin v2.1.2
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "kalatori"
authors = ["Alzymologist Oy <[email protected]>"]
version = "0.2.1"
version = "0.2.2"
edition = "2021"
description = "A gateway daemon for Kalatori."
license = "GPL-3.0-or-later"
Expand Down
14 changes: 14 additions & 0 deletions chopsticks/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Dockerfile
FROM node:16-alpine

# Set the working directory
WORKDIR /app

# Install Chopsticks
RUN npm install -g @acala-network/chopsticks

# Expose ports for both instances
EXPOSE 8000 8500 9000 9500

# Default command (this will be overridden by docker-compose)
CMD ["chopsticks", "-c", "/app/config.yml", "-p", "9000"]
46 changes: 46 additions & 0 deletions chopsticks/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: '3.8'

services:
chopsticks-polkadot:
build:
context: .
dockerfile: Dockerfile
container_name: chopsticks-polkadot
ports:
- "8000:8000"
volumes:
- ./pd.yml:/app/config.yml
command: ["chopsticks", "-c", "/app/config.yml", "-p", "8000", "--addr", "0.0.0.0"]

chopsticks-polkadot-2:
build:
context: .
dockerfile: Dockerfile
container_name: chopsticks-polkadot-2
ports:
- "8500:8500"
volumes:
- ./pd-2.yml:/app/config.yml
command: [ "chopsticks", "-c", "/app/config.yml", "-p", "8500", "--addr", "0.0.0.0" ]

chopsticks-statemint:
build:
context: .
dockerfile: Dockerfile
container_name: chopsticks-statemint
ports:
- "9000:9000"
volumes:
- ./pd-ah.yml:/app/config.yml
command: ["chopsticks", "-c", "/app/config.yml", "-p", "9000", "--addr", "0.0.0.0"]

chopsticks-statemint-2:
build:
context: .
dockerfile: Dockerfile
container_name: chopsticks-statemint-2
ports:
- "9500:9500"
volumes:
- ./pd-ah-2.yml:/app/config.yml
command: [ "chopsticks", "-c", "/app/config.yml", "-p", "9500", "--addr", "0.0.0.0" ]
11 changes: 11 additions & 0 deletions chopsticks/pd-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
endpoint: wss://1rpc.io/dot

import-storage:
System:
Account:
-
-
- 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
- providers: 1
data:
free: 1000000000000000
15 changes: 15 additions & 0 deletions chopsticks/pd-ah-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
endpoint: wss://statemint-rpc.dwellir.com

import-storage:
System:
Account:
-
-
- 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
- providers: 1
data:
free: 1000000000000000
Assets:
Account:
- [[1984, 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY], { balance: 1000000000 }]
- [[1337, 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY], { balance: 1000000000 }]
2 changes: 1 addition & 1 deletion chopsticks/pd-ah.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
endpoint: wss://statemint.api.onfinality.io/public-ws
endpoint: wss://polkadot-asset-hub-rpc.polkadot.io

import-storage:
System:
Expand Down
16 changes: 13 additions & 3 deletions configs/chopsticks.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
account-lifetime = 86400000 # 1 day.
depth = 3600000 # 1 hour.
debug = true
in-memory-db = true

[[chain]]
name = "polkadot"
native-token = "DOT"
decimals = 10
endpoints = [
"ws://localhost:8000",
"ws://localhost:8500",
]

[[chain]]
name = "statemint"
decimals = 6
endpoints = [
"ws://localhost:8000"
"ws://localhost:9000",
"ws://localhost:9500",
]

[[chain.asset]]
name = "USDC"
id = 1337

[[chain.asset]]
name = "USDT"
name = "USDt"
id = 1984
57 changes: 52 additions & 5 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! commercial offers and contracts, hence causality is a must. Care must be taken that no threads
//! are spawned here other than main database server thread that does everything in series.
use crate::definitions::api_v2::ServerInfo;
use crate::{
definitions::{
api_v2::{
Expand All @@ -15,6 +16,7 @@ use crate::{
error::{DbError, Error},
task_tracker::TaskTracker,
};
use names::{Generator, Name};
use parity_scale_codec::{Decode, Encode};
use std::time::SystemTime;
use substrate_crypto_light::common::AccountId32;
Expand Down Expand Up @@ -51,9 +53,10 @@ const HIT_LIST: &str = "hit_list";
// The database version must be stored in a separate slot to be used by the not implemented yet
// database migration logic.
const DB_VERSION_KEY: &str = "db_version";
const DAEMON_INFO: &str = "daemon_info";
const SERVER_INFO_ID: &str = "instance_id";

const ORDERS: &[u8] = b"orders";
const ORDERS_TABLE: &[u8] = b"orders";
const SERVER_INFO_TABLE: &[u8] = b"server_info";

// Slots

Expand Down Expand Up @@ -174,7 +177,7 @@ impl Database {
path_option: Option<String>,
task_tracker: TaskTracker,
account_lifetime: Timestamp,
) -> Result<Self, Error> {
) -> Result<Self, DbError> {
let (tx, mut rx) = tokio::sync::mpsc::channel(1024);
let database = if let Some(path) = path_option {
tracing::info!("Creating/Opening the database at {path:?}.");
Expand All @@ -188,7 +191,9 @@ impl Database {
);*/
sled::open("temp.db").map_err(DbError::DbStartError)?
};
let orders = database.open_tree(ORDERS).map_err(DbError::DbStartError)?;
let orders = database
.open_tree(ORDERS_TABLE)
.map_err(DbError::DbStartError)?;

task_tracker.spawn("Database server", async move {
// No process forking beyond this point!
Expand Down Expand Up @@ -230,6 +235,40 @@ impl Database {
DbRequest::MarkStuck(request) => {
let _unused = request.res.send(mark_stuck(request.order, &orders));
}
DbRequest::InitializeServerInfo(res) => {
let server_info_tree = database
.open_tree(SERVER_INFO_TABLE)
.map_err(DbError::DbStartError);
let result = server_info_tree.and_then(|tree| {
if let Some(server_info_data) =
tree.get(SERVER_INFO_ID).map_err(DbError::DbInternalError)?
{
let server_info: ServerInfo =
serde_json::from_slice(&server_info_data).map_err(|e| {
DbError::DeserializationError(e.to_string())
})?;
Ok(server_info.instance_id)
} else {
let mut generator = Generator::default();
let new_instance_id = generator
.next()
.unwrap_or_else(|| "unknown-instance".to_string());
let server_info_data = ServerInfo {
version: env!("CARGO_PKG_VERSION").to_string(),
instance_id: new_instance_id.clone(),
debug: false,
kalatori_remark: None,
};
tree.insert(
SERVER_INFO_ID,
serde_json::to_vec(&server_info_data)
.map_err(|e| DbError::SerializationError(e.to_string()))?,
)?;
Ok(new_instance_id)
}
});
let _unused = res.send(result);
}
DbRequest::Shutdown(res) => {
let _ = res.send(());
break;
Expand All @@ -245,6 +284,12 @@ impl Database {
Ok(Self { tx })
}

pub async fn initialize_server_info(&self) -> Result<String, DbError> {
let (res, rx) = oneshot::channel();
let _unused = self.tx.send(DbRequest::InitializeServerInfo(res)).await;
rx.await.map_err(|_| DbError::DbEngineDown)?
}

pub async fn order_list(&self) -> Result<Vec<(String, OrderInfo)>, DbError> {
let (res, rx) = oneshot::channel();
let _unused = self.tx.send(DbRequest::ActiveOrderList(res)).await;
Expand Down Expand Up @@ -322,6 +367,7 @@ enum DbRequest {
MarkPaid(MarkPaid),
MarkWithdrawn(ModifyOrder),
MarkStuck(ModifyOrder),
InitializeServerInfo(oneshot::Sender<Result<String, DbError>>),
Shutdown(oneshot::Sender<()>),
}

Expand Down Expand Up @@ -400,7 +446,8 @@ fn read_order(order: String, orders: &sled::Tree) -> Result<Option<OrderInfo>, D
}

fn mark_paid(order: String, orders: &sled::Tree) -> Result<OrderInfo, DbError> {
if let Some(order_info) = orders.get(order.clone())? {
let order_key = order.encode();
if let Some(order_info) = orders.get(order_key)? {
let mut order_info = OrderInfo::decode(&mut &order_info[..])?;
if order_info.payment_status == PaymentStatus::Pending {
order_info.payment_status = PaymentStatus::Paid;
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ pub enum DbError {

#[error("there was already an attempt to withdraw order {0:?}")]
WithdrawalWasAttempted(String),

#[error("wasn't able to serialize {0:?} field")]
SerializationError(String),

#[error("wasn't able to deserialize {0:?} table")]
DeserializationError(String),
}

#[derive(Debug, Error)]
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ async fn async_try_main(
Some(config.database.unwrap_or_else(|| DATABASE_DEFAULT.into()))
};

let instance_id = String::from("TODO: add unique ID and save it in db");

// Start services

tracing::info!(
Expand All @@ -126,6 +124,8 @@ async fn async_try_main(
config.account_lifetime,
)?;

let instance_id = db.initialize_server_info().await?;

let (cm_tx, cm_rx) = oneshot::channel();

let state = State::initialise(
Expand Down

0 comments on commit b2e1bd2

Please sign in to comment.