-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from anoma/tiago/code-cleanup-mark-ii
Code cleanup and API changes
- Loading branch information
Showing
76 changed files
with
1,478 additions
and
1,242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))) | ||
} | ||
} |
Oops, something went wrong.