diff --git a/Cargo.lock b/Cargo.lock index 3d7588b5b7..ffb75a6697 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1394,9 +1394,9 @@ dependencies = [ [[package]] name = "ed25519-consensus" -version = "1.2.1" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "758e2a0cd8a6cdf483e1d369e7d081647e00b88d8953e34d8f2cbba05ae28368" +checksum = "542217a53411d471743362251a5a1770a667cb0cc0384c9be2c0952bd70a7275" dependencies = [ "curve25519-dalek-ng", "hex", @@ -2937,6 +2937,7 @@ dependencies = [ "itertools", "libsecp256k1", "loupe", + "namada_core", "namada_proof_of_stake", "parity-wasm", "paste", @@ -3059,6 +3060,35 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "namada_core" +version = "0.9.0" +dependencies = [ + "assert_matches", + "bech32", + "borsh", + "chrono", + "data-encoding", + "derivative", + "ed25519-consensus", + "itertools", + "libsecp256k1", + "pretty_assertions", + "proptest", + "rand 0.8.5", + "rand_core 0.6.4", + "rust_decimal", + "serde 1.0.145", + "serde_json", + "sha2 0.9.9", + "sparse-merkle-tree", + "test-log", + "thiserror", + "tracing 0.1.37", + "tracing-subscriber 0.3.16", + "zeroize", +] + [[package]] name = "namada_encoding_spec" version = "0.9.0" @@ -6702,9 +6732,9 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.5.7" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f" +checksum = "94693807d016b2f2d2e14420eb3bfcca689311ff775dcf113d74ea624b7cdf07" dependencies = [ "zeroize_derive", ] diff --git a/Cargo.toml b/Cargo.toml index 5cfa43de02..feacd7bf21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ resolver = "2" members = [ "apps", + "core", "proof_of_stake", "shared", "tests", diff --git a/core/Cargo.toml b/core/Cargo.toml new file mode 100644 index 0000000000..2b43d37213 --- /dev/null +++ b/core/Cargo.toml @@ -0,0 +1,48 @@ +[package] +authors = ["Heliax AG "] +edition = "2021" +license = "GPL-3.0" +name = "namada_core" +resolver = "2" +version = "0.9.0" + +[features] +default = [] +# for integration tests and test utilies +testing = [ + "rand", + "rand_core", +] + +[dependencies] +# 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" +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"]} diff --git a/shared/src/bytes.rs b/core/src/bytes.rs similarity index 100% rename from shared/src/bytes.rs rename to core/src/bytes.rs diff --git a/core/src/ledger/mod.rs b/core/src/ledger/mod.rs new file mode 100644 index 0000000000..6b3dbf3b3e --- /dev/null +++ b/core/src/ledger/mod.rs @@ -0,0 +1,5 @@ +//! The ledger modules + +pub mod storage_api; +pub mod tx_env; +pub mod vp_env; diff --git a/shared/src/ledger/storage_api/collections/lazy_map.rs b/core/src/ledger/storage_api/collections/lazy_map.rs similarity index 100% rename from shared/src/ledger/storage_api/collections/lazy_map.rs rename to core/src/ledger/storage_api/collections/lazy_map.rs diff --git a/shared/src/ledger/storage_api/collections/lazy_vec.rs b/core/src/ledger/storage_api/collections/lazy_vec.rs similarity index 100% rename from shared/src/ledger/storage_api/collections/lazy_vec.rs rename to core/src/ledger/storage_api/collections/lazy_vec.rs diff --git a/shared/src/ledger/storage_api/collections/mod.rs b/core/src/ledger/storage_api/collections/mod.rs similarity index 100% rename from shared/src/ledger/storage_api/collections/mod.rs rename to core/src/ledger/storage_api/collections/mod.rs diff --git a/shared/src/ledger/storage_api/error.rs b/core/src/ledger/storage_api/error.rs similarity index 100% rename from shared/src/ledger/storage_api/error.rs rename to core/src/ledger/storage_api/error.rs diff --git a/shared/src/ledger/storage_api/mod.rs b/core/src/ledger/storage_api/mod.rs similarity index 100% rename from shared/src/ledger/storage_api/mod.rs rename to core/src/ledger/storage_api/mod.rs diff --git a/shared/src/ledger/storage_api/validation/mod.rs b/core/src/ledger/storage_api/validation/mod.rs similarity index 100% rename from shared/src/ledger/storage_api/validation/mod.rs rename to core/src/ledger/storage_api/validation/mod.rs diff --git a/shared/src/ledger/tx_env.rs b/core/src/ledger/tx_env.rs similarity index 100% rename from shared/src/ledger/tx_env.rs rename to core/src/ledger/tx_env.rs diff --git a/shared/src/ledger/vp_env.rs b/core/src/ledger/vp_env.rs similarity index 100% rename from shared/src/ledger/vp_env.rs rename to core/src/ledger/vp_env.rs diff --git a/core/src/lib.rs b/core/src/lib.rs new file mode 100644 index 0000000000..add1f4ba82 --- /dev/null +++ b/core/src/lib.rs @@ -0,0 +1,15 @@ +//! TODO + +#![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; + +#[cfg(test)] +#[macro_use] +extern crate assert_matches; diff --git a/shared/src/types/address.rs b/core/src/types/address.rs similarity index 100% rename from shared/src/types/address.rs rename to core/src/types/address.rs diff --git a/shared/src/types/chain.rs b/core/src/types/chain.rs similarity index 100% rename from shared/src/types/chain.rs rename to core/src/types/chain.rs diff --git a/shared/src/types/governance.rs b/core/src/types/governance.rs similarity index 100% rename from shared/src/types/governance.rs rename to core/src/types/governance.rs diff --git a/shared/src/types/hash.rs b/core/src/types/hash.rs similarity index 78% rename from shared/src/types/hash.rs rename to core/src/types/hash.rs index 229cd56063..5bd4518975 100644 --- a/shared/src/types/hash.rs +++ b/core/src/types/hash.rs @@ -3,15 +3,15 @@ use std::fmt::{self, Display}; use std::ops::Deref; -use arse_merkle_tree::traits::Value; -use arse_merkle_tree::Hash as TreeHash; +// use arse_merkle_tree::traits::Value; +// use arse_merkle_tree::Hash as TreeHash; use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use thiserror::Error; -use crate::tendermint::abci::transaction; -use crate::tendermint::Hash as TmHash; +// use crate::tendermint::abci::transaction; +// use crate::tendermint::Hash as TmHash; /// The length of the transaction hash string pub const HASH_LENGTH: usize = 32; @@ -86,11 +86,12 @@ impl TryFrom<&[u8]> for Hash { } } -impl From for transaction::Hash { - fn from(hash: Hash) -> Self { - Self::new(hash.0) - } -} +// TODO move +// impl From for transaction::Hash { +// fn from(hash: Hash) -> Self { +// Self::new(hash.0) +// } +// } impl Hash { /// Compute sha256 of some bytes @@ -99,20 +100,24 @@ impl Hash { Self(*digest.as_ref()) } + fn zero() -> Self { + Self([0u8; 32]) + } + /// Check if the hash is all zeros pub fn is_zero(&self) -> bool { self == &Self::zero() } } -impl From for TmHash { - fn from(hash: Hash) -> Self { - TmHash::Sha256(hash.0) - } -} - -impl From for TreeHash { - fn from(hash: Hash) -> Self { - Self::from(hash.0) - } -} +// impl From for TmHash { +// fn from(hash: Hash) -> Self { +// TmHash::Sha256(hash.0) +// } +// } + +// impl From for TreeHash { +// fn from(hash: Hash) -> Self { +// Self::from(hash.0) +// } +// } diff --git a/shared/src/types/key/common.rs b/core/src/types/key/common.rs similarity index 97% rename from shared/src/types/key/common.rs rename to core/src/types/key/common.rs index fc4a4732dd..7ec041d638 100644 --- a/shared/src/types/key/common.rs +++ b/core/src/types/key/common.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; use data_encoding::HEXLOWER; -use namada_proof_of_stake::types::PublicKeyTmRawHash; +// use namada_proof_of_stake::types::PublicKeyTmRawHash; #[cfg(feature = "rand")] use rand::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; @@ -325,8 +325,9 @@ impl super::SigScheme for SigScheme { } } -impl PublicKeyTmRawHash for PublicKey { - fn tm_raw_hash(&self) -> String { - tm_consensus_key_raw_hash(self) - } -} +// TODO +// impl PublicKeyTmRawHash for PublicKey { +// fn tm_raw_hash(&self) -> String { +// tm_consensus_key_raw_hash(self) +// } +// } diff --git a/shared/src/types/key/ed25519.rs b/core/src/types/key/ed25519.rs similarity index 100% rename from shared/src/types/key/ed25519.rs rename to core/src/types/key/ed25519.rs diff --git a/shared/src/types/key/mod.rs b/core/src/types/key/mod.rs similarity index 99% rename from shared/src/types/key/mod.rs rename to core/src/types/key/mod.rs index 78efcc5375..17f6fd34ba 100644 --- a/shared/src/types/key/mod.rs +++ b/core/src/types/key/mod.rs @@ -1,6 +1,4 @@ //! Cryptographic keys -/// Elliptic curve keys for the DKG -pub mod dkg_session_keys; use std::fmt::{Debug, Display}; use std::hash::Hash; diff --git a/shared/src/types/key/secp256k1.rs b/core/src/types/key/secp256k1.rs similarity index 100% rename from shared/src/types/key/secp256k1.rs rename to core/src/types/key/secp256k1.rs diff --git a/core/src/types/mod.rs b/core/src/types/mod.rs new file mode 100644 index 0000000000..db32b3d140 --- /dev/null +++ b/core/src/types/mod.rs @@ -0,0 +1,11 @@ +//! Types definitions. + +pub mod address; +pub mod chain; +pub mod governance; +pub mod hash; +pub mod key; +pub mod storage; +pub mod time; +pub mod token; +pub mod validity_predicate; diff --git a/shared/src/types/named_address.rs b/core/src/types/named_address.rs similarity index 100% rename from shared/src/types/named_address.rs rename to core/src/types/named_address.rs diff --git a/shared/src/types/storage.rs b/core/src/types/storage.rs similarity index 94% rename from shared/src/types/storage.rs rename to core/src/types/storage.rs index 57d0f66c5d..628872715f 100644 --- a/shared/src/types/storage.rs +++ b/core/src/types/storage.rs @@ -6,17 +6,17 @@ use std::num::ParseIntError; use std::ops::{Add, Deref, Div, Mul, Rem, Sub}; use std::str::FromStr; -use arse_merkle_tree::InternalKey; +// use arse_merkle_tree::InternalKey; use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; use data_encoding::BASE32HEX_NOPAD; -use ics23::CommitmentProof; +// use ics23::CommitmentProof; use serde::{Deserialize, Serialize}; use thiserror::Error; #[cfg(feature = "ferveo-tpke")] use super::transaction::WrapperTx; use crate::bytes::ByteBuf; -use crate::ledger::storage::IBC_KEY_LIMIT; +// use crate::ledger::storage::IBC_KEY_LIMIT; use crate::types::address::{self, Address}; use crate::types::hash::Hash; use crate::types::time::DateTimeUtc; @@ -267,55 +267,55 @@ where } } -/// Storage keys that are utf8 encoded strings -#[derive(Eq, PartialEq, Copy, Clone, Hash)] -pub struct StringKey { - /// The original key string, in bytes - pub original: [u8; IBC_KEY_LIMIT], - /// The utf8 bytes representation of the key to be - /// used internally in the merkle tree - pub tree_key: InternalKey, - /// The length of the input (without the padding) - pub length: usize, -} - -impl Deref for StringKey { - type Target = InternalKey; - - fn deref(&self) -> &Self::Target { - &self.tree_key - } -} - -impl BorshSerialize for StringKey { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - let to_serialize = (self.original.to_vec(), self.tree_key, self.length); - BorshSerialize::serialize(&to_serialize, writer) - } -} - -impl BorshDeserialize for StringKey { - fn deserialize(buf: &mut &[u8]) -> std::io::Result { - use std::io::ErrorKind; - let (original, tree_key, length): ( - Vec, - InternalKey, - usize, - ) = BorshDeserialize::deserialize(buf)?; - let original: [u8; IBC_KEY_LIMIT] = - original.try_into().map_err(|_| { - std::io::Error::new( - ErrorKind::InvalidData, - "Input byte vector is too large", - ) - })?; - Ok(Self { - original, - tree_key, - length, - }) - } -} +// /// Storage keys that are utf8 encoded strings +// #[derive(Eq, PartialEq, Copy, Clone, Hash)] +// pub struct StringKey { +// /// The original key string, in bytes +// pub original: [u8; IBC_KEY_LIMIT], +// /// The utf8 bytes representation of the key to be +// /// used internally in the merkle tree +// pub tree_key: InternalKey, +// /// The length of the input (without the padding) +// pub length: usize, +// } + +// impl Deref for StringKey { +// type Target = InternalKey; + +// fn deref(&self) -> &Self::Target { +// &self.tree_key +// } +// } + +// impl BorshSerialize for StringKey { +// fn serialize(&self, writer: &mut W) -> std::io::Result<()> { +// let to_serialize = (self.original.to_vec(), self.tree_key, +// self.length); BorshSerialize::serialize(&to_serialize, writer) +// } +// } + +// impl BorshDeserialize for StringKey { +// fn deserialize(buf: &mut &[u8]) -> std::io::Result { +// use std::io::ErrorKind; +// let (original, tree_key, length): ( +// Vec, +// InternalKey, +// usize, +// ) = BorshDeserialize::deserialize(buf)?; +// let original: [u8; IBC_KEY_LIMIT] = +// original.try_into().map_err(|_| { +// std::io::Error::new( +// ErrorKind::InvalidData, +// "Input byte vector is too large", +// ) +// })?; +// Ok(Self { +// original, +// tree_key, +// length, +// }) +// } +// } /// A wrapper around raw bytes to be stored as values /// in a merkle tree @@ -346,17 +346,18 @@ impl From for Vec { } } -/// Type of membership proof from a merkle tree -pub enum MembershipProof { - /// ICS23 compliant membership proof - ICS23(CommitmentProof), -} - -impl From for MembershipProof { - fn from(proof: CommitmentProof) -> Self { - Self::ICS23(proof) - } -} +// TODO not sure +// /// Type of membership proof from a merkle tree +// pub enum MembershipProof { +// /// ICS23 compliant membership proof +// ICS23(CommitmentProof), +// } + +// impl From for MembershipProof { +// fn from(proof: CommitmentProof) -> Self { +// Self::ICS23(proof) +// } +// } impl Key { /// Parses string and returns a key diff --git a/shared/src/types/time.rs b/core/src/types/time.rs similarity index 77% rename from shared/src/types/time.rs rename to core/src/types/time.rs index dfca614c82..13de2685ad 100644 --- a/shared/src/types/time.rs +++ b/core/src/types/time.rs @@ -7,10 +7,6 @@ use std::ops::{Add, Sub}; use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; pub use chrono::{DateTime, Duration, TimeZone, Utc}; -use crate::tendermint::time::Time; -use crate::tendermint::Error as TendermintError; -use crate::tendermint_proto::google::protobuf; - /// Check if the given `duration` has passed since the given `start. pub fn duration_passed( current: DateTimeUtc, @@ -179,35 +175,37 @@ impl From> for DateTimeUtc { } } -impl TryFrom for DateTimeUtc { - type Error = prost_types::TimestampOutOfSystemRangeError; +// TODO move +// impl TryFrom for DateTimeUtc { +// type Error = prost_types::TimestampOutOfSystemRangeError; - fn try_from( - timestamp: prost_types::Timestamp, - ) -> Result { - let system_time: std::time::SystemTime = timestamp.try_into()?; - Ok(Self(system_time.into())) - } -} +// fn try_from( +// timestamp: prost_types::Timestamp, +// ) -> Result { +// let system_time: std::time::SystemTime = timestamp.try_into()?; +// Ok(Self(system_time.into())) +// } +// } -impl From for prost_types::Timestamp { - fn from(dt: DateTimeUtc) -> Self { - let seconds = dt.0.timestamp(); - let nanos = dt.0.timestamp_subsec_nanos() as i32; - prost_types::Timestamp { seconds, nanos } - } -} +// impl From for prost_types::Timestamp { +// fn from(dt: DateTimeUtc) -> Self { +// let seconds = dt.0.timestamp(); +// let nanos = dt.0.timestamp_subsec_nanos() as i32; +// prost_types::Timestamp { seconds, nanos } +// } +// } -impl TryFrom for DateTimeUtc { - type Error = prost_types::TimestampOutOfSystemRangeError; +// TODO move +// impl TryFrom for DateTimeUtc { +// type Error = prost_types::TimestampOutOfSystemRangeError; - fn try_from(timestamp: protobuf::Timestamp) -> Result { - Self::try_from(prost_types::Timestamp { - seconds: timestamp.seconds, - nanos: timestamp.nanos, - }) - } -} +// fn try_from(timestamp: protobuf::Timestamp) -> Result +// { Self::try_from(prost_types::Timestamp { +// seconds: timestamp.seconds, +// nanos: timestamp.nanos, +// }) +// } +// } impl From for std::time::SystemTime { fn from(dt: DateTimeUtc) -> Self { @@ -230,18 +228,19 @@ impl From for Rfc3339String { } } -impl TryFrom for Time { - type Error = TendermintError; +// TODO move +// impl TryFrom for Time { +// type Error = TendermintError; - fn try_from(dt: DateTimeUtc) -> Result { - Self::parse_from_rfc3339(&DateTime::to_rfc3339(&dt.0)) - } -} +// fn try_from(dt: DateTimeUtc) -> Result { +// Self::parse_from_rfc3339(&DateTime::to_rfc3339(&dt.0)) +// } +// } -impl TryFrom