Skip to content

Commit

Permalink
Merge pull request #4 from anoma/tiago/code-cleanup-mark-ii
Browse files Browse the repository at this point in the history
Code cleanup and API changes
  • Loading branch information
sug0 committed Jul 3, 2024
2 parents 2638575 + e732b40 commit ba43a57
Show file tree
Hide file tree
Showing 76 changed files with 1,478 additions and 1,242 deletions.
82 changes: 22 additions & 60 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,39 @@ resolver = "2"
members = ["chain", "shared", "orm", "webserver"]

[workspace.package]
authors = ["Gianmarco <gianmarco@heliax.dev>"]
authors = ["Heliax AG <hello@heliax.dev>"]
edition = "2021"
license = "GPL-3.0"
readme = "README.md"
version = "0.1.0"

[workspace.dependencies]
clokwerk = "0.4.0"
axum = { version = "0.6.20", features = ["tower-log"] }
tokio = { version = "1.0", features = ["full"] }
tower = { version = "0.4", features = [
"util",
"timeout",
"load-shed",
"limit",
"buffer",
] }
tower-http = { version = "0.4.4", features = [
"compression-full",
"limit",
"trace",
"cors",
] }
tower-layer = "0.3.2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
serde = { version = "1.0.138", features = ["derive"] }
serde_json = "1.0"
clap = { version = "4.4.2", features = ["derive", "env"] }
uuid = { version = "1.4.1", features = ["v4", "serde"] }
reqwest = { version = "0.11.20", features = ["json", "cookies"] }
axum-macros = "0.3.8"
chrono = { version = "0.4.30", features = ["serde"] }
async-trait = "0.1.73"
anyhow = "1.0.75"
axum = { version = "0.6.20", features = [ "tower-log" ] }
axum-macros = "0.3.8"
axum-trace-id = "0.1.0"
clap = { version = "4.4.2", features = [ "derive", "env" ] }
clap-verbosity-flag = "2.1.1"
deadpool-diesel = { version = "0.5.0", features = ["postgres"] }
diesel = { version = "2.1.0", features = [ "postgres", "uuid", "serde_json", "chrono" ] }
diesel_migrations = { version = "2.1.0", default-features = false, features = [ "postgres" ] }
futures = "0.3.30"
itertools = "0.13.0"
lazy_static = "1.4.0"
namada_core = { git = "https://github.com/anoma/namada", tag = "v0.39.0" }
namada_sdk = { git = "https://github.com/anoma/namada", tag = "v0.39.0" }
namada_tx = { git = "https://github.com/anoma/namada", tag = "v0.39.0" }
namada_governance = { git = "https://github.com/anoma/namada", tag = "v0.39.0" }
namada_ibc = { git = "https://github.com/anoma/namada", tag = "v0.39.0" }
namada_token = { git = "https://github.com/anoma/namada", tag = "v0.39.0" }
orm = { path = "orm" }
serde = { version = "1.0.138", features = [ "derive" ] }
serde_json = "1.0"
shared = { path = "shared" }
tendermint = "0.36.0"
tendermint-config = "0.36.0"
tendermint-rpc = { version = "0.36.0", features = ["http-client"] }
tendermint-proto = "0.36.0"
subtle-encoding = "0.5.1"
bimap = { version = "0.6.3", features = ["serde"] }
async-stream = "0.3.5"
futures-core = "0.3.30"
futures-util = "0.3.30"
futures = "0.3.30"
tokio-retry = "0.3"
thiserror = "1.0.56"
diesel_migrations = { version = "2.1.0", default-features = false, features = [
"postgres",
] }
deadpool-diesel = { version = "0.5.0", features = ["postgres"] }
diesel = { version = "2.1.0", features = [
"postgres",
"uuid",
"serde_json",
"chrono",
] }
diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
orm = { path = "orm" }
shared = { path = "shared" }
borsh-ext = { git = "https://github.com/heliaxdev/borsh-ext", tag = "v1.2.0" }
borsh = "=1.2.0"
lazy_static = "1.4.0"
tokio = { version = "1.0", features = [ "full" ] }
tokio-retry = "0.3"
tower = { version = "0.4", features = [ "util", "timeout", "load-shed", "limit", "buffer" ] }
tower-http = { version = "0.4.4", features = [ "compression-full", "limit", "trace", "cors" ] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = [ "env-filter" ] }
validator = { version = "0.16.0", features = ["derive"] }
axum-trace-id = "0.1.0"
derive_builder = "0.12.0"
clap-verbosity-flag = "2.1.1"
duration-str = "0.7.1"
19 changes: 19 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
nightly_version := prepend('+', `cat rust-nightly-version`)

help:
@echo fmt, clippy, clippy-fix, check or clean

clean:
cargo clean

fmt:
cargo {{nightly_version}} fmt --all

clippy:
cargo {{nightly_version}} clippy

clippy-fix:
cargo {{nightly_version}} clippy --fix --allow-dirty --allow-staged

check:
cargo check
15 changes: 0 additions & 15 deletions Makefile

This file was deleted.

39 changes: 13 additions & 26 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,23 @@ name = "chain"
path = "src/main.rs"

[dependencies]
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
serde.workspace = true
serde_json.workspace = true
clap.workspace = true
anyhow.workspace = true
thiserror.workspace = true
namada_ibc.workspace = true
namada_sdk.workspace = true
clap-verbosity-flag.workspace = true
clap.workspace = true
deadpool-diesel.workspace = true
diesel.workspace = true
diesel_migrations.workspace = true
futures.workspace = true
namada_core.workspace = true
namada_governance.workspace = true
namada_token.workspace = true
tendermint-config.workspace = true
namada_sdk.workspace = true
orm.workspace = true
shared.workspace = true
tendermint-rpc.workspace = true
tendermint-proto.workspace = true
tendermint.workspace = true
subtle-encoding.workspace = true
shared.workspace = true
async-stream.workspace = true
futures-util.workspace = true
futures-core.workspace = true
futures.workspace = true
tokio-retry.workspace = true
diesel_migrations.workspace = true
deadpool-diesel.workspace = true
diesel.workspace = true
chrono.workspace = true
orm.workspace = true
clap-verbosity-flag.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
44 changes: 20 additions & 24 deletions chain/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
use core::fmt;
use std::fmt::Display;
use std::path::PathBuf;

use clap_verbosity_flag::{InfoLevel, Verbosity};

#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum CargoEnv {
Development,
Production,
}

impl Display for CargoEnv {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
use clap_verbosity_flag::{InfoLevel, LevelFilter, Verbosity};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[derive(clap::Parser)]
pub struct AppConfig {
#[clap(long, env)]
pub tendermint_url: String,
pub cometbft_url: String,

#[clap(long, env)]
pub database_url: String,

#[clap(long, env)]
pub chain_id: String,

#[clap(long, env)]
pub checksums_filepath: PathBuf,

#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
}

pub fn install_tracing_subscriber(verbosity: Verbosity<InfoLevel>) {
let log_level = match verbosity.log_level_filter() {
LevelFilter::Off => None,
LevelFilter::Error => Some(Level::ERROR),
LevelFilter::Warn => Some(Level::WARN),
LevelFilter::Info => Some(Level::INFO),
LevelFilter::Debug => Some(Level::DEBUG),
LevelFilter::Trace => Some(Level::TRACE),
};
if let Some(log_level) = log_level {
let subscriber =
FmtSubscriber::builder().with_max_level(log_level).finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
}
}
1 change: 1 addition & 0 deletions chain/src/entity/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl ChainState {

pub fn into_db(&self) -> ChainStateteInsertDb {
ChainStateteInsertDb {
id: 0, // NB: overwrite old row
block_height: self.block_height.0 as i32,
}
}
Expand Down
86 changes: 67 additions & 19 deletions chain/src/entity/commitment_tree.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,100 @@
use std::sync::{Arc, Mutex};

use anyhow::Context;
use namada_sdk::borsh::{BorshDeserialize, BorshSerializeExt};
use namada_sdk::masp_primitives::merkle_tree::CommitmentTree as MaspCommitmentTree;
use namada_sdk::masp_primitives::sapling::Node;
use orm::tree::{TreeDb, TreeInsertDb};
use shared::height::BlockHeight;
use shared::transactional::Transactional;

#[derive(Clone, Debug)]
pub struct CommitmentTree(Arc<Mutex<MaspCommitmentTree<Node>>>);
#[derive(Debug)]
struct InnerCommitmentTree {
transactional: Transactional<MaspCommitmentTree<Node>>,
}

impl Default for InnerCommitmentTree {
fn default() -> Self {
Self {
transactional: Transactional::new(MaspCommitmentTree::empty()),
}
}
}

impl InnerCommitmentTree {
const fn new(tree: MaspCommitmentTree<Node>) -> Self {
Self {
transactional: Transactional::new(tree),
}
}

fn rollback(&mut self) {
self.transactional.rollback();
}

fn append(&mut self, node: Node) -> bool {
self.transactional.as_mut().append(node).is_ok()
}

fn size(&self) -> usize {
self.transactional.as_ref().size()
}

fn get_tree(&self) -> MaspCommitmentTree<Node> {
self.transactional.as_ref().clone()
}

#[allow(clippy::wrong_self_convention)]
fn into_db(&mut self, block_height: BlockHeight) -> Option<TreeInsertDb> {
if !self.transactional.commit() {
return None;
}
Some(TreeInsertDb {
tree: self.transactional.as_ref().serialize_to_vec(),
block_height: block_height.0 as i32,
})
}
}

#[derive(Default, Clone, Debug)]
pub struct CommitmentTree(Arc<Mutex<InnerCommitmentTree>>);

impl CommitmentTree {
pub fn new(tree: MaspCommitmentTree<Node>) -> Self {
Self(Arc::new(Mutex::new(tree)))
Self(Arc::new(Mutex::new(InnerCommitmentTree::new(tree))))
}

pub fn rollback(&self) {
self.0.lock().unwrap().rollback()
}

pub fn size(&self) -> usize {
self.0.lock().unwrap().size()
}

pub fn append(&self, node: Node) -> Result<(), ()> {
pub fn append(&self, node: Node) -> bool {
self.0.lock().unwrap().append(node)
}

pub fn get_tree(&self) -> MaspCommitmentTree<Node> {
self.0.lock().unwrap().clone()
self.0.lock().unwrap().get_tree()
}

pub fn into_db(&self, block_height: BlockHeight) -> TreeInsertDb {
TreeInsertDb {
tree: self.0.lock().unwrap().serialize_to_vec(),
block_height: block_height.0 as i32,
}
#[allow(clippy::wrong_self_convention)]
pub fn into_db(&self, block_height: BlockHeight) -> Option<TreeInsertDb> {
self.0.lock().unwrap().into_db(block_height)
}
}

impl TryFrom<TreeDb> for CommitmentTree {
type Error = String;
type Error = anyhow::Error;

fn try_from(value: TreeDb) -> Result<Self, Self::Error> {
let commitment_tree =
MaspCommitmentTree::<Node>::try_from_slice(&value.tree)
.map_err(|e| e.to_string())?;
MaspCommitmentTree::<Node>::try_from_slice(&value.tree).context(
"Failed to deserialize commitment tree from db borsh encoded \
bytes",
)?;
Ok(Self::new(commitment_tree))
}
}

impl Default for CommitmentTree {
fn default() -> Self {
Self(Arc::new(Mutex::new(MaspCommitmentTree::empty())))
}
}
Loading

0 comments on commit ba43a57

Please sign in to comment.