Skip to content

Commit

Permalink
remove mysql and redis (#119)
Browse files Browse the repository at this point in the history
* refactor sequencer, accepting commands from sidecar directly

* extract scanner/committer from connector

* replace mysql with rocksdb

* update README.md

---------

Co-authored-by: Cyberaurora <[email protected]>
  • Loading branch information
kb1ns and kb1ns authored Nov 8, 2023
1 parent 1b74db0 commit c1dd0cf
Show file tree
Hide file tree
Showing 33 changed files with 2,601 additions and 3,677 deletions.
839 changes: 185 additions & 654 deletions Cargo.lock

Large diffs are not rendered by default.

54 changes: 26 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
[![SAY NO TO THE TRADEMARK POLICY ](https://gist.githubusercontent.com/blyxyas/8f17fbe1cafdeff65bbe6b332d4f4723/raw/715a24df3ad74b838c6b0ff8079d3f7f9172b0db/banner.svg)](https://github.com/blyxyas/no-rust-policy-change)
# Galois

[![License](https://img.shields.io/badge/License-Apache%202.0-orange.svg)](#LICENSE)
[![GitHub Workflow Status (branch)](https://github.com/uinb/galois/actions/workflows/build.yml/badge.svg)](https://github.com/uinb/galois/actions?query=branch%3Amaster)


## Introduction

Galois is an extremely high performance matching engine written in Rust which uses event-sourcing pattern to handle tens of thousands of orders per second or even better, depending on the performance of persistence.
Basic architecture is shown below.

```
core dump(disk)
^
^
+----------+
events(mysql) >> | galois | >> match results(mysql)/best n price(redis)
will be replaced +----------+ will be replaced
^
^
query requests(TCP)
```

Galois works as the prover(a.k.a Proof of Matches) component of [Fusotao](https://github.com/uinb/fusotao). From v0.4.0, we don't support running Galois in standalone mode anymore.
According to the Roadmap 2023, the UINB team is working on implementing the Proof of Order Relay which enables users to run [Fusotao Node](https://github.com/uinb/fusotao) as an order relayer(a.k.a broker) rather than supporting multiple prover instances in the network. In the near future, galois will be recoverable from anywhere by pulling the core data and sequences from [Arweave](https://arweave.org/) and under management of the FusoDAO.

## Getting Started
sidecar chain <-+
| | \
| | \
v v \
+-----> server scanner +
| \ / |
|\ \ / |
| \ \ / |
| +-<- sequencer |
+ | |
|\ | |
| \ v |
| +-<- executor |
+ / \ +
\ / \ /
\ / \ /
+-<- output committer -+
[Fusotao Docs](https://docs.fusotao.org/)
```

### Dependencies
Galois works as the prover of [Fusotao](https://github.com/uinb/fusotao)(a.k.a Proof of Matches). From v0.4, we don't support running Galois in standalone mode anymore.

- MySQL(will be replaced with RocksDB): persist the events and output the match result
- Redis(will be removed): output the best n price of the orderbook
NOTICE: The v0.7 is still under heavy development.

### Quick Start
## Documents

TODO, or refer to the old version.
See [Fusotao Docs](https://docs.fusotao.org/).

### Instructions
## How it works

TODO, or refer to the old version.
See [Fusotao Greebook](https://www.fusotao.org/fusotao-greenbook.pdf).

## License
Galois is licensed under [Apache 2.0](LICENSE).

[Apache 2.0](LICENSE).
6 changes: 3 additions & 3 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "galois-bin"
version = "0.5.0-dev"
version = "0.7.0-dev"
authors = ["UINB Technologies"]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -15,8 +15,8 @@ name = "sidecar"
path = "src/sidecar.rs"

[dependencies]
galois-engine = { path = "../engine" }
galois-sidecar = { path = "../sidecar" }
engine = { path = "../engine", package = "galois-engine" }
sidecar = { path = "../sidecar", package = "galois-sidecar" }
jsonrpsee = { version = "0.16.2", features = ["full"] }
tokio = { version = "1.16", features = ["full"] }
tower = "0.4.13"
Expand Down
72 changes: 57 additions & 15 deletions bin/src/galois.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,74 @@
// limitations under the License.

use clap::Parser;
use galois_engine::{
config, executor, fusotao, output, sequence, server, shared::Shared, snapshot,
};
use std::sync::{atomic, mpsc, Arc};
use engine::*;

fn print_banner() {
println!(
r#"
** **
******* ****** ** ** ******
*** ** ** ***** ** ** *
** ***** ** *** *** **
** ******* ** ** ** ** **
** ***** ** ** ** * * ** *****
** *** ** ** ** ** ** ** **
********* ** **** ** ******* ** ** **
* * **** * ** *** ** ****"#
);
}

/// Overview:
///
/// sidecar chain <-+
/// | | \
/// | | \
/// v v \
/// +----> server scanner +
/// | \ / |
/// |\ \ / |
/// | \ \ / |
/// | +-- sequencer |
/// + | |
/// |\ | |
/// | \ v |
/// | +-- executor |
/// | | |
/// | | |
/// | v |
/// + storage |
/// \ / \ +
/// \ / \ /
/// \ / \ /
/// +-- replyer committer -+
///
fn start() {
let (id, coredump) = snapshot::load().unwrap();
let (output_tx, output_rx) = mpsc::channel();
let (event_tx, event_rx) = mpsc::channel();
let (proof_tx, proof_rx) = mpsc::channel();
let (msg_tx, msg_rx) = mpsc::channel();
let (connector, state) = fusotao::sync().unwrap();
let shared = Shared::new(state.clone(), C.fusotao.get_x25519());
let (output_tx, output_rx) = std::sync::mpsc::channel();
let (event_tx, event_rx) = std::sync::mpsc::channel();
let (input_tx, input_rx) = std::sync::mpsc::channel();
let (reply_tx, reply_rx) = std::sync::mpsc::channel();
output::init(output_rx);
let fuso = fusotao::init(proof_rx);
let shared = Shared::new(fuso.state, config::C.fusotao.get_x25519_prikey().unwrap());
executor::init(event_rx, output_tx, proof_tx, msg_tx, coredump);
let ready = Arc::new(atomic::AtomicBool::new(false));
sequence::init(event_tx.clone(), id, ready.clone());
server::init(event_tx, msg_rx, shared, ready);
committer::init(connector.clone(), state.clone());
executor::init(event_rx, output_tx, reply_tx.clone(), coredump);
sequencer::init(input_rx, event_tx, reply_tx, id);
scanner::init(input_tx.clone(), connector, state);
server::init(reply_rx, input_tx, shared);
}

fn main() {
env_logger::init();
let opts = config::GaloisCli::parse();
match opts.sub {
Some(config::SubCmd::EncryptConfig) => config::print_config(&opts.file).unwrap(),
None => {
lazy_static::initialize(&config::C);
print_banner();
lazy_static::initialize(&C);
if C.dry_run.is_some() {
log::info!("running in dry-run mode");
}
start();
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use galois_sidecar::*;
use sidecar::*;

#[tokio::main]
async fn main() {
Expand Down
11 changes: 5 additions & 6 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "galois-engine"
version = "0.5.0-dev"
version = "0.7.0-dev"
authors = ["UINB Technologies"]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -14,13 +14,14 @@ serde = { version = "1.0", features = ["derive"] }
hashbrown = "0.13.2"
async-trait = "0.1.63"
serde_json = "1.0"
rocksdb = "0.21"
flate2 = { version = "1.0", features = ["zlib"], default-features = false }
mysql = "23.0"
redis = { version = "0.17", features = ["tls", "tokio-rt-core", "tokio-tls-comp", "native-tls","async-native-tls", "async-std-tls-comp"] }
# mysql = "23.0"
# redis = { version = "0.17", features = ["tls", "tokio-rt-core", "tokio-tls-comp", "native-tls","async-native-tls", "async-std-tls-comp"] }
toml = "0.5"
lazy_static = "1.4"
linked-hash-map = { version = "0.5.3", features = ["serde_impl"] }
async-std = { version = "1.12", default-features = false, features = ["std", "attributes", "tokio1"] }
async-std = { version = "1.12", default-features = false, features = ["std", "attributes", "tokio1", "default"] }
futures = "0.3"
chashmap = "2.2"
syn = "1.0.107"
Expand All @@ -44,8 +45,6 @@ rand = "0.8.5"
smt = { git = "https://github.com/uinb/sparse-merkle-tree", tag = "v0.1.8", package = "sparse-merkle-tree", features = ["serde-rs", "blake2b"] }
sub-api = { package = "substrate-api-client", git = "https://github.com/uinb/fusotao-rust-client.git", branch = "master" }
node-api = { package = "ac-node-api", git = "https://github.com/uinb/fusotao-rust-client.git", branch = "master" }
#sub-api = { path = "../fusotao-rust-client", package = "substrate-api-client" }
#node-api = { path = "../fusotao-rust-client/node-api", package = "ac-node-api" }
parity-scale-codec = { version = "3", features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", package = "sp-core" }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", package = "sp-runtime" }
Expand Down
92 changes: 21 additions & 71 deletions engine/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,10 @@
// limitations under the License.

use clap::Parser;
use log4rs::config::{Logger, RawConfig as LogConfig};
use serde::{Deserialize, Serialize};

#[derive(Debug, Parser)]
#[command(author, version, about = r#"
** **
******* ****** ** ** ******
*** ** ** ***** ** ** *
** ***** ** *** *** **
** ******* ** ** ** ** **
** ***** ** ** ** * * ** *****
** *** ** ** ** ** ** ** **
********* ** **** ** ******* ** ** **
* * **** * ** *** ** ****"#,
long_about = None)]
#[command(author, version)]
pub struct GaloisCli {
#[arg(short('c'), long("config"), required = true, value_name = "FILE")]
pub file: std::path::PathBuf,
Expand Down Expand Up @@ -60,12 +49,8 @@ pub struct RunCmd {
pub struct Config {
pub server: ServerConfig,
pub sequence: SequenceConfig,
pub mysql: MysqlConfig,
pub redis: RedisConfig,
pub fusotao: FusotaoConfig,
#[serde(skip_serializing)]
pub log: LogConfig,
#[serde(skip_serializing)]
pub dry_run: Option<u64>,
}

Expand All @@ -77,15 +62,22 @@ pub trait EncryptedConfig {
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ServerConfig {
pub bind_addr: String,
pub data_home: String,
}

impl ServerConfig {
pub fn get_coredump_path(&self) -> String {
format!("{}/coredump/", self.data_home)
}

pub fn get_storage_path(&self) -> String {
format!("{}/storage/", self.data_home)
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SequenceConfig {
pub coredump_dir: String,
pub checkpoint: usize,
pub batch_size: usize,
pub dump_mode: String,
pub fetch_intervel_ms: u64,
pub checkpoint: u64,
pub enable_from_genesis: bool,
}

Expand Down Expand Up @@ -118,13 +110,12 @@ pub struct FusotaoConfig {
pub key_seed: String,
pub claim_block: u32,
pub proof_batch_limit: usize,
pub compress_proofs: bool,
pub x25519_priv: String,
}

impl FusotaoConfig {
pub fn get_x25519_prikey(&self) -> anyhow::Result<String> {
Ok(self.x25519_priv.clone())
pub fn get_x25519(&self) -> String {
self.x25519_priv.clone()
}
}

Expand Down Expand Up @@ -202,8 +193,6 @@ pub fn print_config(f: &std::path::PathBuf) -> anyhow::Result<()> {
.ok_or(anyhow::anyhow!("env MAGIC_KEY not set"))?;
let toml = std::fs::read_to_string(f)?;
let mut cfg: Config = toml::from_str(&toml)?;
cfg.mysql.encrypt(&key)?;
cfg.redis.encrypt(&key)?;
cfg.fusotao.encrypt(&key)?;
println!("{}", toml::to_string(&cfg)?);
Ok(())
Expand All @@ -212,68 +201,29 @@ pub fn print_config(f: &std::path::PathBuf) -> anyhow::Result<()> {
fn init_config(toml: &str, key: Option<String>) -> anyhow::Result<Config> {
let mut cfg: Config = toml::from_str(toml)?;
if let Some(key) = key {
cfg.mysql.decrypt(&key)?;
cfg.redis.decrypt(&key)?;
cfg.fusotao.decrypt(&key)?;
}
// TODO replace with env_log
let mut loggers = cfg
.log
.loggers()
.iter()
.map(|l| (l.name().to_string(), l.clone()))
.collect::<std::collections::HashMap<String, _>>();
loggers
.entry("ws".to_string())
.or_insert_with(|| Logger::builder().build("ws".to_string(), log::LevelFilter::Error));
loggers.entry("ac_node_api".to_string()).or_insert_with(|| {
Logger::builder().build("ac_node_api".to_string(), log::LevelFilter::Error)
});
loggers
.entry("fusotao_rust_client".to_string())
.or_insert_with(|| {
Logger::builder().build("fusotao_rust_client".to_string(), log::LevelFilter::Error)
});
let log = log4rs::Config::builder()
.loggers::<Vec<_>>(loggers.into_values().collect())
.appenders(cfg.log.appenders_lossy(&Default::default()).0)
.build(cfg.log.root())?;
log4rs::init_config(log)?;
Ok(cfg)
}

#[test]
pub fn test_default() {
pub fn test_config() {
let toml = r#"
[server]
bind_addr = "127.0.0.1:8097"
[mysql]
url = "mysql://username:password@localhost:3306/galois"
[redis]
url = "redis://localhost:6379/0"
data_home = "/tmp/galois"
[sequence]
checkpoint = 100000
coredump_dir = "/tmp/snapshot"
batch_size = 1000
dump_mode = "disk"
fetch_intervel_ms = 5
enable_from_genesis = true
[log]
[log.appenders.console]
kind = "console"
[log.root]
level = "info"
appenders = ["console"]
[fusotao]
node_url = "ws://localhost:9944"
key_seed = "//Alice"
x25519_priv = "0xedcff0c69e4c0fa7e9a36e2e6d07f2cc355c8d25907a0ad2ab7e03b24f8e90f3"
proof_batch_limit = 20
claim_block = 1
compress_proofs = true
fee_adjust_threshold = 1000
"#;
let config = init_config(&toml, None).unwrap();
let mysql_opts = mysql::Opts::from_url(&config.mysql.url).unwrap();
assert_eq!("password", mysql_opts.get_pass().unwrap());
let config = init_config(&toml, None);
assert!(config.is_ok());
}
Loading

0 comments on commit c1dd0cf

Please sign in to comment.