Skip to content

Commit

Permalink
add new crate "namada_core" for core types, storage_api, tx_env, vp_env
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Nov 21, 2022
1 parent 8929369 commit b36c8a6
Show file tree
Hide file tree
Showing 86 changed files with 1,126 additions and 902 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"

members = [
"apps",
"core",
"proof_of_stake",
"shared",
"tests",
Expand Down
62 changes: 62 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[package]
authors = ["Heliax AG <[email protected]>"]
edition = "2021"
license = "GPL-3.0"
name = "namada_core"
resolver = "2"
version = "0.9.0"

[features]
default = []
ferveo-tpke = [
"ferveo",
"tpke",
"ark-ec",
"rand_core",
"rand",
]
# for integration tests and test utilies
testing = [
"rand",
"rand_core",
]

[dependencies]
ark-ec = {version = "0.3", optional = true}
# We switch off "blake2b" because it cannot be compiled to wasm
# branch = "bat/arse-merkle-tree"
arse-merkle-tree = {package = "sparse-merkle-tree", git = "https://github.com/heliaxdev/sparse-merkle-tree", rev = "04ad1eeb28901b57a7599bbe433b3822965dabe8", default-features = false, features = ["std", "borsh"]}
bech32 = "0.8.0"
borsh = "0.9.0"
chrono = {version = "0.4.22", default-features = false, features = ["clock", "std"]}
data-encoding = "2.3.2"
derivative = "2.2.0"
ed25519-consensus = "1.2.0"
ferveo = {optional = true, git = "https://github.com/anoma/ferveo"}
tpke = {package = "group-threshold-cryptography", optional = true, git = "https://github.com/anoma/ferveo"}
ics23 = "0.7.0"
itertools = "0.10.0"
libsecp256k1 = {git = "https://github.com/heliaxdev/libsecp256k1", rev = "bbb3bd44a49db361f21d9db80f9a087c194c0ae9", default-features = false, features = ["std", "static-context"]}
rand = {version = "0.8", optional = true}
rand_core = {version = "0.6", optional = true}
rust_decimal = "1.26.1"
serde = {version = "1.0.125", features = ["derive"]}
serde_json = "1.0.62"
sha2 = "0.9.3"
thiserror = "1.0.30"
tracing = "0.1.30"
zeroize = {version = "1.5.5", features = ["zeroize_derive"]}

[dev-dependencies]
assert_matches = "1.5.0"
libsecp256k1 = {git = "https://github.com/heliaxdev/libsecp256k1", rev = "bbb3bd44a49db361f21d9db80f9a087c194c0ae9"}
pretty_assertions = "0.7.2"
# A fork with state machine testing
proptest = {git = "https://github.com/heliaxdev/proptest", branch = "tomas/sm"}
rand = {version = "0.8"}
rand_core = {version = "0.6"}
test-log = {version = "0.2.7", default-features = false, features = ["trace"]}
tracing-subscriber = {version = "0.3.7", default-features = false, features = ["env-filter", "fmt"]}

[build-dependencies]
tonic-build = "0.6.0"
55 changes: 55 additions & 0 deletions core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::fs::read_to_string;

This comment has been minimized.

Copy link
@tzemanovic

tzemanovic Nov 25, 2022

Author Member

this should be moved from shared/build.rs

This comment has been minimized.

Copy link
@tzemanovic

tzemanovic Nov 25, 2022

Author Member

this is not moved because shared/build.rs still has ANOMA_DEV env var feature handling.

use std::process::Command;
use std::{env, str};

/// Path to the .proto source files, relative to `core` directory
const PROTO_SRC: &str = "./proto";

/// The version should match the one we use in the `Makefile`
const RUSTFMT_TOOLCHAIN_SRC: &str = "../rust-nightly-version";

fn main() {
if let Ok(val) = env::var("COMPILE_PROTO") {
if val.to_ascii_lowercase() == "false" {
// Skip compiling proto files
return;
}
}

// Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo:rerun-if-changed={}", PROTO_SRC);

let mut use_rustfmt = false;

// The version should match the one we use in the `Makefile`
if let Ok(rustfmt_toolchain) = read_to_string(RUSTFMT_TOOLCHAIN_SRC) {
// Try to find the path to rustfmt.
if let Ok(output) = Command::new("rustup")
.args(&[
"which",
"rustfmt",
"--toolchain",
rustfmt_toolchain.trim(),
])
.output()
{
if let Ok(rustfmt) = str::from_utf8(&output.stdout) {
// Set the command to be used by tonic_build below to format the
// generated files
let rustfmt = rustfmt.trim();
if !rustfmt.is_empty() {
println!("using rustfmt from path \"{}\"", rustfmt);
env::set_var("RUSTFMT", rustfmt);
use_rustfmt = true
}
}
}
}

tonic_build::configure()
.out_dir("src/proto/generated")
.format(use_rustfmt)
.protoc_arg("--experimental_allow_proto3_optional")
.compile(&[format!("{}/types.proto", PROTO_SRC)], &[PROTO_SRC])
.unwrap();
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions core/src/ledger/governance/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Governance library code
/// governance parameters
pub mod parameters;
/// governance storage
pub mod storage;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions core/src/ledger/ibc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod actions;
pub mod storage;
File renamed without changes.
11 changes: 11 additions & 0 deletions core/src/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! The ledger modules
pub mod gas;
pub mod governance;
#[cfg(feature = "ibc-rs")]
pub mod ibc;
pub mod parameters;
pub mod storage;
pub mod storage_api;
pub mod tx_env;
pub mod vp_env;
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ use arse_merkle_tree::{
use borsh::{BorshDeserialize, BorshSerialize};
use ics23::commitment_proof::Proof as Ics23Proof;
use ics23::{CommitmentProof, ExistenceProof, NonExistenceProof};
use namada_core::types::storage::{TreeKeyError, IBC_KEY_LIMIT};
use prost::Message;
use thiserror::Error;

use super::traits::{StorageHasher, SubTreeRead, SubTreeWrite};
use super::IBC_KEY_LIMIT;
use crate::bytes::ByteBuf;
use crate::ledger::storage::ics23_specs::{self, ibc_leaf_spec};
use crate::ledger::storage::types;
use crate::tendermint::merkle::proof::{Proof, ProofOp};
use crate::types::address::{Address, InternalAddress};
use crate::types::hash::Hash;
use crate::types::storage::{
DbKeySeg, Error as StorageError, Key, MembershipProof, MerkleValue,
StringKey, TreeBytes,
DbKeySeg, Error as StorageError, Key, MerkleValue, StringKey, TreeBytes,
};

#[allow(missing_docs)]
Expand All @@ -33,6 +32,8 @@ pub enum Error {
InvalidKey(StorageError),
#[error("Invalid key for merkle tree: {0}")]
InvalidMerkleKey(String),
#[error("Storage tree key error: {0}")]
StorageTreeKey(#[from] TreeKeyError),
#[error("Empty Key: {0}")]
EmptyKey(String),
#[error("Merkle Tree error: {0}")]
Expand Down Expand Up @@ -829,3 +830,15 @@ mod test {
assert!(basetree_verification_res);
}
}

/// Type of membership proof from a merkle tree
pub enum MembershipProof {
/// ICS23 compliant membership proof
ICS23(CommitmentProof),
}

impl From<CommitmentProof> for MembershipProof {
fn from(proof: CommitmentProof) -> Self {
Self::ICS23(proof)
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ pub mod ics23_specs;
mod merkle_tree;
#[cfg(any(test, feature = "testing"))]
pub mod mockdb;
pub mod traits;
pub mod types;
pub mod write_log;

use core::fmt::Debug;
use std::array;
Expand Down Expand Up @@ -336,7 +334,6 @@ where
pub fn open(
db_path: impl AsRef<std::path::Path>,
chain_id: ChainId,
native_token: Address,
cache: Option<&D::Cache>,
) -> Self {
let block = BlockStorage {
Expand All @@ -363,7 +360,6 @@ where
conversion_state: ConversionState::default(),
#[cfg(feature = "ferveo-tpke")]
tx_queue: TxQueue::default(),
native_token,
}
}

Expand Down Expand Up @@ -1158,7 +1154,6 @@ pub mod testing {
use super::mockdb::MockDB;
use super::*;
use crate::ledger::storage::traits::Sha256Hasher;
use crate::types::address;
/// Storage with a mock DB for testing
pub type TestStorage = Storage<MockDB, Sha256Hasher>;

Expand Down Expand Up @@ -1190,7 +1185,6 @@ pub mod testing {
conversion_state: ConversionState::default(),
#[cfg(feature = "ferveo-tpke")]
tx_queue: TxQueue::default(),
native_token: address::nam(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ use arse_merkle_tree::traits::{Hasher, Value};
use arse_merkle_tree::{Key as TreeKey, H256};
use ics23::commitment_proof::Proof as Ics23Proof;
use ics23::{CommitmentProof, ExistenceProof};
use namada_core::types::storage::IBC_KEY_LIMIT;
use sha2::{Digest, Sha256};

use super::merkle_tree::{Amt, Error, Smt};
use super::{ics23_specs, IBC_KEY_LIMIT};
use super::ics23_specs;
use super::merkle_tree::{Amt, Error, MembershipProof, Smt};
use crate::types::hash::Hash;
use crate::types::storage::{
Key, MembershipProof, MerkleValue, StringKey, TreeBytes,
};
use crate::types::storage::{Key, MerkleValue, StringKey, TreeBytes};

/// Trait for reading from a merkle tree that is a sub-tree
/// of the global merkle tree.
Expand Down Expand Up @@ -159,74 +158,6 @@ impl<'a, H: StorageHasher + Default> SubTreeWrite for &'a mut Amt<H> {
}
}

impl TreeKey<IBC_KEY_LIMIT> for StringKey {
type Error = Error;

fn as_slice(&self) -> &[u8] {
&self.original.as_slice()[..self.length]
}

fn try_from_bytes(bytes: &[u8]) -> Result<Self, Error> {
let mut tree_key = [0u8; IBC_KEY_LIMIT];
let mut original = [0u8; IBC_KEY_LIMIT];
let mut length = 0;
for (i, byte) in bytes.iter().enumerate() {
if i >= IBC_KEY_LIMIT {
return Err(Error::InvalidMerkleKey(
"Input IBC key is too large".into(),
));
}
original[i] = *byte;
tree_key[i] = byte.wrapping_add(1);
length += 1;
}
Ok(Self {
original,
tree_key: tree_key.into(),
length,
})
}
}

impl Value for Hash {
fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}

fn zero() -> Self {
Hash([0u8; 32])
}
}

impl From<Hash> for H256 {
fn from(hash: Hash) -> Self {
hash.0.into()
}
}

impl From<H256> for Hash {
fn from(hash: H256) -> Self {
Self(hash.into())
}
}

impl From<&H256> for Hash {
fn from(hash: &H256) -> Self {
let hash = hash.to_owned();
Self(hash.into())
}
}

impl Value for TreeBytes {
fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}

fn zero() -> Self {
TreeBytes::zero()
}
}

/// The storage hasher used for the merkle tree.
pub trait StorageHasher: Hasher + Default {
/// Hash the value to store
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! The core public types, storage_api, VpEnv and TxEnv.
#![doc(html_favicon_url = "https://dev.anoma.net/master/favicon.png")]
#![doc(html_logo_url = "https://dev.anoma.net/master/rustdoc-logo.png")]
#![warn(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(rustdoc::private_intra_doc_links)]

pub mod bytes;
pub mod ledger;
pub mod types;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit b36c8a6

Please sign in to comment.