From 9d66743752ab7f93ff62d12adbb03cec2aea907e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 18:45:23 +0200 Subject: [PATCH 01/41] add a new crate namada_vm --- Cargo.toml | 3 ++- Makefile | 1 + crates/vm/Cargo.toml | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 crates/vm/Cargo.toml diff --git a/Cargo.toml b/Cargo.toml index 0359633027..84fccaf19b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,9 +31,10 @@ members = [ "crates/tests", "crates/token", "crates/trans_token", + "crates/tx", "crates/tx_env", "crates/tx_prelude", - "crates/tx", + "crates/vm", "crates/vm_env", "crates/vote_ext", "crates/vp_env", diff --git a/Makefile b/Makefile index 31e8d0a437..6170b24465 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,7 @@ crates += namada_trans_token crates += namada_tx crates += namada_tx_env crates += namada_tx_prelude +crates += namada_vm crates += namada_vm_env crates += namada_vote_ext crates += namada_vp_env diff --git a/crates/vm/Cargo.toml b/crates/vm/Cargo.toml new file mode 100644 index 0000000000..3727cae708 --- /dev/null +++ b/crates/vm/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "namada_vm" +description = "The Namada VM" +resolver = "2" +authors.workspace = true +edition.workspace = true +documentation.workspace = true +homepage.workspace = true +keywords.workspace = true +license.workspace = true +readme.workspace = true +repository.workspace = true +version.workspace = true + +[dependencies] From 1a8d4f84544ed79ede434066e2468d65a699b476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 19:44:04 +0200 Subject: [PATCH 02/41] add a new crate namada_vp --- Cargo.toml | 1 + Makefile | 1 + crates/namada/src/ledger/lib.rs | 2 ++ crates/vp/Cargo.toml | 20 ++++++++++++++++++++ crates/vp/src/lib.rs | 2 ++ 5 files changed, 26 insertions(+) create mode 100644 crates/namada/src/ledger/lib.rs create mode 100644 crates/vp/Cargo.toml create mode 100644 crates/vp/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 84fccaf19b..9091504c4d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ members = [ "crates/vm", "crates/vm_env", "crates/vote_ext", + "crates/vp", "crates/vp_env", "crates/vp_prelude", "examples", diff --git a/Makefile b/Makefile index 6170b24465..7a11dad712 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,7 @@ crates += namada_tx_prelude crates += namada_vm crates += namada_vm_env crates += namada_vote_ext +crates += namada_vp crates += namada_vp_env crates += namada_vp_prelude diff --git a/crates/namada/src/ledger/lib.rs b/crates/namada/src/ledger/lib.rs new file mode 100644 index 0000000000..f72d4bc2e3 --- /dev/null +++ b/crates/namada/src/ledger/lib.rs @@ -0,0 +1,2 @@ +//! This crate contains the trait for native validity predicates with its +//! various context types and host functions implementation. diff --git a/crates/vp/Cargo.toml b/crates/vp/Cargo.toml new file mode 100644 index 0000000000..ca80b9852f --- /dev/null +++ b/crates/vp/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "namada_vp" +description = "The Namada validity predicates native trait and host functions" +resolver = "2" +authors.workspace = true +edition.workspace = true +documentation.workspace = true +homepage.workspace = true +keywords.workspace = true +license.workspace = true +readme.workspace = true +repository.workspace = true +version.workspace = true + +[dependencies] +namada_core = { path = "../core" } +namada_events = { path = "../events", default-features = false } +namada_gas = { path = "../gas" } +namada_storage = { path = "../storage" } +namada_tx = { path = "../tx" } diff --git a/crates/vp/src/lib.rs b/crates/vp/src/lib.rs new file mode 100644 index 0000000000..f72d4bc2e3 --- /dev/null +++ b/crates/vp/src/lib.rs @@ -0,0 +1,2 @@ +//! This crate contains the trait for native validity predicates with its +//! various context types and host functions implementation. From 5750425fcb3741e7ca4cce979f36b8b3e19555ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 18:47:28 +0200 Subject: [PATCH 03/41] mv crates/namada/src/vm/* crates/vm/src/ --- crates/{namada/src/vm => vm/src}/host_env.rs | 0 crates/{namada/src/vm/mod.rs => vm/src/lib.rs} | 0 crates/{namada/src/vm => vm/src}/memory.rs | 0 crates/{namada/src/vm => vm/src}/prefix_iter.rs | 0 crates/{namada/src/vm => vm/src}/types.rs | 0 crates/{namada/src/vm => vm/src}/wasm/compilation_cache/common.rs | 0 crates/{namada/src/vm => vm/src}/wasm/compilation_cache/mod.rs | 0 crates/{namada/src/vm => vm/src}/wasm/compilation_cache/tx.rs | 0 crates/{namada/src/vm => vm/src}/wasm/compilation_cache/vp.rs | 0 crates/{namada/src/vm => vm/src}/wasm/host_env.rs | 0 crates/{namada/src/vm => vm/src}/wasm/memory.rs | 0 crates/{namada/src/vm => vm/src}/wasm/mod.rs | 0 crates/{namada/src/vm => vm/src}/wasm/run.rs | 0 13 files changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/vm => vm/src}/host_env.rs (100%) rename crates/{namada/src/vm/mod.rs => vm/src/lib.rs} (100%) rename crates/{namada/src/vm => vm/src}/memory.rs (100%) rename crates/{namada/src/vm => vm/src}/prefix_iter.rs (100%) rename crates/{namada/src/vm => vm/src}/types.rs (100%) rename crates/{namada/src/vm => vm/src}/wasm/compilation_cache/common.rs (100%) rename crates/{namada/src/vm => vm/src}/wasm/compilation_cache/mod.rs (100%) rename crates/{namada/src/vm => vm/src}/wasm/compilation_cache/tx.rs (100%) rename crates/{namada/src/vm => vm/src}/wasm/compilation_cache/vp.rs (100%) rename crates/{namada/src/vm => vm/src}/wasm/host_env.rs (100%) rename crates/{namada/src/vm => vm/src}/wasm/memory.rs (100%) rename crates/{namada/src/vm => vm/src}/wasm/mod.rs (100%) rename crates/{namada/src/vm => vm/src}/wasm/run.rs (100%) diff --git a/crates/namada/src/vm/host_env.rs b/crates/vm/src/host_env.rs similarity index 100% rename from crates/namada/src/vm/host_env.rs rename to crates/vm/src/host_env.rs diff --git a/crates/namada/src/vm/mod.rs b/crates/vm/src/lib.rs similarity index 100% rename from crates/namada/src/vm/mod.rs rename to crates/vm/src/lib.rs diff --git a/crates/namada/src/vm/memory.rs b/crates/vm/src/memory.rs similarity index 100% rename from crates/namada/src/vm/memory.rs rename to crates/vm/src/memory.rs diff --git a/crates/namada/src/vm/prefix_iter.rs b/crates/vm/src/prefix_iter.rs similarity index 100% rename from crates/namada/src/vm/prefix_iter.rs rename to crates/vm/src/prefix_iter.rs diff --git a/crates/namada/src/vm/types.rs b/crates/vm/src/types.rs similarity index 100% rename from crates/namada/src/vm/types.rs rename to crates/vm/src/types.rs diff --git a/crates/namada/src/vm/wasm/compilation_cache/common.rs b/crates/vm/src/wasm/compilation_cache/common.rs similarity index 100% rename from crates/namada/src/vm/wasm/compilation_cache/common.rs rename to crates/vm/src/wasm/compilation_cache/common.rs diff --git a/crates/namada/src/vm/wasm/compilation_cache/mod.rs b/crates/vm/src/wasm/compilation_cache/mod.rs similarity index 100% rename from crates/namada/src/vm/wasm/compilation_cache/mod.rs rename to crates/vm/src/wasm/compilation_cache/mod.rs diff --git a/crates/namada/src/vm/wasm/compilation_cache/tx.rs b/crates/vm/src/wasm/compilation_cache/tx.rs similarity index 100% rename from crates/namada/src/vm/wasm/compilation_cache/tx.rs rename to crates/vm/src/wasm/compilation_cache/tx.rs diff --git a/crates/namada/src/vm/wasm/compilation_cache/vp.rs b/crates/vm/src/wasm/compilation_cache/vp.rs similarity index 100% rename from crates/namada/src/vm/wasm/compilation_cache/vp.rs rename to crates/vm/src/wasm/compilation_cache/vp.rs diff --git a/crates/namada/src/vm/wasm/host_env.rs b/crates/vm/src/wasm/host_env.rs similarity index 100% rename from crates/namada/src/vm/wasm/host_env.rs rename to crates/vm/src/wasm/host_env.rs diff --git a/crates/namada/src/vm/wasm/memory.rs b/crates/vm/src/wasm/memory.rs similarity index 100% rename from crates/namada/src/vm/wasm/memory.rs rename to crates/vm/src/wasm/memory.rs diff --git a/crates/namada/src/vm/wasm/mod.rs b/crates/vm/src/wasm/mod.rs similarity index 100% rename from crates/namada/src/vm/wasm/mod.rs rename to crates/vm/src/wasm/mod.rs diff --git a/crates/namada/src/vm/wasm/run.rs b/crates/vm/src/wasm/run.rs similarity index 100% rename from crates/namada/src/vm/wasm/run.rs rename to crates/vm/src/wasm/run.rs From c01ce4f060db8f2e94f1fec6fdcdfa779ebdd7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 19:14:04 +0200 Subject: [PATCH 04/41] mv crates/namada/src/ledger/vp_host_fns.rs crates/vp/src/ --- crates/{namada/src/ledger => vp/src}/vp_host_fns.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) rename crates/{namada/src/ledger => vp/src}/vp_host_fns.rs (98%) diff --git a/crates/namada/src/ledger/vp_host_fns.rs b/crates/vp/src/vp_host_fns.rs similarity index 98% rename from crates/namada/src/ledger/vp_host_fns.rs rename to crates/vp/src/vp_host_fns.rs index 37c2e3c05c..bac8f03c7a 100644 --- a/crates/namada/src/ledger/vp_host_fns.rs +++ b/crates/vp/src/vp_host_fns.rs @@ -11,15 +11,13 @@ use namada_core::storage::{ BlockHeight, Epoch, Epochs, Header, Key, TxIndex, TX_INDEX_LENGTH, }; use namada_events::{Event, EventTypeBuilder}; -use namada_gas::MEMORY_ACCESS_GAS_PER_BYTE; +use namada_gas as gas; +use namada_gas::{GasMetering, VpGasMeter, MEMORY_ACCESS_GAS_PER_BYTE}; use namada_state::write_log::WriteLog; use namada_state::{write_log, DBIter, ResultExt, StateRead, DB}; use namada_tx::{BatchedTxRef, Section}; use thiserror::Error; -use crate::ledger::gas; -use crate::ledger::gas::{GasMetering, VpGasMeter}; - /// These runtime errors will abort VP execution immediately #[allow(missing_docs)] #[derive(Error, Debug)] @@ -31,7 +29,7 @@ pub enum RuntimeError { #[error("Storage error: {0}")] StorageError(#[from] namada_state::StorageError), #[error("Storage data error: {0}")] - StorageDataError(crate::storage::Error), + StorageDataError(namada_core::storage::Error), #[error("Encoding error: {0}")] EncodingError(std::io::Error), #[error("Numeric conversion error: {0}")] From e319e203394013b372868445ab1e4fcbaec34c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 19:57:15 +0200 Subject: [PATCH 05/41] vp: post-move fixes --- Cargo.lock | 14 ++++++++++++++ crates/vp/Cargo.toml | 6 +++++- crates/vp/src/lib.rs | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 84c87d4925..b8e3115398 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5340,6 +5340,20 @@ dependencies = [ "serde", ] +[[package]] +name = "namada_vp" +version = "0.39.0" +dependencies = [ + "namada_core", + "namada_events", + "namada_gas", + "namada_state", + "namada_tx", + "smooth-operator", + "thiserror", + "tracing", +] + [[package]] name = "namada_vp_env" version = "0.41.0" diff --git a/crates/vp/Cargo.toml b/crates/vp/Cargo.toml index ca80b9852f..0523b9a50d 100644 --- a/crates/vp/Cargo.toml +++ b/crates/vp/Cargo.toml @@ -16,5 +16,9 @@ version.workspace = true namada_core = { path = "../core" } namada_events = { path = "../events", default-features = false } namada_gas = { path = "../gas" } -namada_storage = { path = "../storage" } +namada_state = { path = "../state" } namada_tx = { path = "../tx" } + +smooth-operator.workspace = true +thiserror.workspace = true +tracing.workspace = true diff --git a/crates/vp/src/lib.rs b/crates/vp/src/lib.rs index f72d4bc2e3..afc0c84ddd 100644 --- a/crates/vp/src/lib.rs +++ b/crates/vp/src/lib.rs @@ -1,2 +1,4 @@ //! This crate contains the trait for native validity predicates with its //! various context types and host functions implementation. + +pub mod vp_host_fns; From 1344ec0be4abc7c7a143013c5e66cd4d12fcce0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 19:24:49 +0200 Subject: [PATCH 06/41] vm: post-move fixes --- Cargo.lock | 34 ++++++++++++ crates/vm/Cargo.toml | 54 +++++++++++++++++++ crates/vm/src/host_env.rs | 44 +++++++-------- crates/vm/src/types.rs | 5 +- .../vm/src/wasm/compilation_cache/common.rs | 17 +++--- crates/vm/src/wasm/host_env.rs | 18 +++---- crates/vm/src/wasm/memory.rs | 6 +-- crates/vm/src/wasm/run.rs | 42 +++++++-------- 8 files changed, 152 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8e3115398..d421becfc2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5317,6 +5317,40 @@ dependencies = [ "namada_vm_env", ] +[[package]] +name = "namada_vm" +version = "0.39.0" +dependencies = [ + "assert_matches", + "borsh 1.2.1", + "byte-unit", + "clru", + "itertools 0.12.1", + "namada_core", + "namada_events", + "namada_gas", + "namada_parameters", + "namada_sdk", + "namada_state", + "namada_test_utils", + "namada_token", + "namada_tx", + "namada_vp", + "parity-wasm", + "rayon", + "smooth-operator", + "tempfile", + "test-log", + "thiserror", + "tracing", + "wasm-instrument", + "wasmer", + "wasmer-cache", + "wasmer-compiler-singlepass", + "wasmer-vm", + "wasmparser 0.107.0", +] + [[package]] name = "namada_vm_env" version = "0.41.0" diff --git a/crates/vm/Cargo.toml b/crates/vm/Cargo.toml index 3727cae708..a8e94313ab 100644 --- a/crates/vm/Cargo.toml +++ b/crates/vm/Cargo.toml @@ -12,4 +12,58 @@ readme.workspace = true repository.workspace = true version.workspace = true +[features] +default = ["wasm-runtime"] +wasm-runtime = [ + "parity-wasm", + "rayon", + "wasm-instrument", + "wasmer-cache", + "wasmer-compiler-singlepass", + "wasmer-vm", + "wasmer", +] +testing = [ + "namada_core/testing", + "tempfile", +] + [dependencies] +namada_core = { path = "../core" } +namada_events = { path = "../events", default-features = false } +namada_gas = { path = "../gas" } +namada_parameters = { path = "../parameters" } +namada_sdk = { path = "../sdk" } +namada_state = { path = "../state" } +namada_token = { path = "../token" } +namada_tx = { path = "../tx" } +namada_vp = { path = "../vp" } + +borsh.workspace = true +clru.workspace = true +parity-wasm = { version = "0.45.0", features = ["sign_ext"], optional = true } +rayon = { version = "=1.5.3", optional = true } +smooth-operator.workspace = true +tempfile = { version = "3.2.0", optional = true } +thiserror.workspace = true +tracing.workspace = true +wasm-instrument = { workspace = true, optional = true } +wasmer = { workspace = true, optional = true } +wasmer-cache = { workspace = true, optional = true } +wasmer-compiler-singlepass = { workspace = true, optional = true } +wasmer-vm = { workspace = true, optional = true } +wasmparser.workspace = true + +[dev-dependencies] +namada_core = { path = "../core", default-features = false, features = [ + "testing", +] } +namada_state = { path = "../state", features = ["testing"] } +namada_test_utils = { path = "../test_utils" } +namada_tx = { path = "../tx", features = ["testing"] } + +assert_matches.workspace = true +byte-unit.workspace = true +itertools.workspace = true +tempfile.workspace = true +test-log.workspace = true diff --git a/crates/vm/src/host_env.rs b/crates/vm/src/host_env.rs index 38dab14943..e4f235d853 100644 --- a/crates/vm/src/host_env.rs +++ b/crates/vm/src/host_env.rs @@ -6,13 +6,12 @@ use std::collections::BTreeSet; use std::fmt::Debug; use std::num::TryFromIntError; -use borsh::BorshDeserialize; -use borsh_ext::BorshSerializeExt; -use masp_primitives::transaction::Transaction; -use namada_core::address::ESTABLISHED_ADDRESS_BYTES_LEN; +use namada_core::address::{self, Address, ESTABLISHED_ADDRESS_BYTES_LEN}; use namada_core::arith::{self, checked}; -use namada_core::internal::KeyVal; -use namada_core::storage::TX_INDEX_LENGTH; +use namada_core::borsh::{BorshDeserialize, BorshSerializeExt}; +use namada_core::hash::Hash; +use namada_core::internal::{HostEnvResult, KeyVal}; +use namada_core::storage::{BlockHeight, Key, TxIndex, TX_INDEX_LENGTH}; use namada_events::{Event, EventTypeBuilder}; use namada_gas::{ self as gas, GasMetering, TxGasMeter, VpGasMeter, @@ -24,9 +23,14 @@ use namada_state::{ StorageHasher, StorageRead, StorageWrite, TxHostEnvState, VpHostEnvState, DB, }; -use namada_token::storage_key::is_any_token_parameter_key; +use namada_token::storage_key::{ + is_any_minted_balance_key, is_any_minter_key, is_any_token_balance_key, + is_any_token_parameter_key, +}; +use namada_token::transaction::Transaction; use namada_tx::data::TxSentinel; use namada_tx::{BatchedTx, BatchedTxRef, Tx, TxCommitments}; +use namada_vp::vp_host_fns; use thiserror::Error; #[cfg(feature = "wasm-runtime")] @@ -34,17 +38,9 @@ use super::wasm::TxCache; #[cfg(feature = "wasm-runtime")] use super::wasm::VpCache; use super::WasmCacheAccess; -use crate::address::{self, Address}; -use crate::hash::Hash; -use crate::internal::HostEnvResult; -use crate::ledger::vp_host_fns; -use crate::storage::{BlockHeight, Key, TxIndex}; -use crate::token::storage_key::{ - is_any_minted_balance_key, is_any_minter_key, is_any_token_balance_key, -}; -use crate::vm::memory::VmMemory; -use crate::vm::prefix_iter::{PrefixIteratorId, PrefixIterators}; -use crate::vm::{HostRef, RoAccess, RoHostRef, RwAccess, RwHostRef}; +use crate::memory::VmMemory; +use crate::prefix_iter::{PrefixIteratorId, PrefixIterators}; +use crate::{HostRef, RoAccess, RoHostRef, RwAccess, RwHostRef}; /// These runtime errors will abort tx WASM execution immediately #[allow(missing_docs)] @@ -70,7 +66,7 @@ pub enum TxRuntimeError { #[error("Storage error: {0}")] StorageError(#[from] StorageError), #[error("Storage data error: {0}")] - StorageDataError(crate::storage::Error), + StorageDataError(namada_core::storage::Error), #[error("Encoding error: {0}")] EncodingError(std::io::Error), #[error("Address error: {0}")] @@ -2086,7 +2082,7 @@ where } // Then check that VP code hash is in the allowlist. - if !crate::parameters::is_vp_allowed(&env.ctx.state(), &code_hash) + if !namada_parameters::is_vp_allowed(&env.ctx.state(), &code_hash) .map_err(TxRuntimeError::StorageError)? { return Err(TxRuntimeError::DisallowedVp); @@ -2201,7 +2197,7 @@ where let transaction = Transaction::try_from_slice(&serialized_transaction) .map_err(TxRuntimeError::EncodingError)?; - match crate::token::utils::update_note_commitment_tree( + match namada_token::utils::update_note_commitment_tree( &mut env.state(), &transaction, ) { @@ -2364,8 +2360,8 @@ pub mod testing { use std::rc::Rc; use super::*; - use crate::vm::memory::testing::NativeMemory; - use crate::vm::wasm::memory::WasmMemory; + use crate::memory::testing::NativeMemory; + use crate::wasm::memory::WasmMemory; /// Setup a transaction environment #[allow(clippy::too_many_arguments)] @@ -2432,7 +2428,7 @@ pub mod testing { { let wasm_memory = { let mut borrowed_store = store.borrow_mut(); - crate::vm::wasm::memory::prepare_tx_memory(&mut *borrowed_store) + crate::wasm::memory::prepare_tx_memory(&mut *borrowed_store) .unwrap() }; diff --git a/crates/vm/src/types.rs b/crates/vm/src/types.rs index 54367601fd..a83913e3b8 100644 --- a/crates/vm/src/types.rs +++ b/crates/vm/src/types.rs @@ -9,11 +9,10 @@ use std::collections::BTreeSet; +use namada_core::address::Address; +use namada_core::storage; use namada_tx::BatchedTxRef; -use crate::address::Address; -use crate::storage; - /// Input for validity predicate wasm module call pub struct VpInput<'a> { /// The address of the validity predicate's owning account diff --git a/crates/vm/src/wasm/compilation_cache/common.rs b/crates/vm/src/wasm/compilation_cache/common.rs index 1f305f4041..90a6201cd5 100644 --- a/crates/vm/src/wasm/compilation_cache/common.rs +++ b/crates/vm/src/wasm/compilation_cache/common.rs @@ -14,14 +14,14 @@ use std::time::Duration; use clru::{CLruCache, CLruCacheConfig, WeightScale}; use namada_core::collections::HashMap; +use namada_core::hash::Hash; +use namada_sdk::control_flow::time::{ExponentialBackoff, SleepStrategy}; use wasmer::{Module, Store}; use wasmer_cache::{FileSystemCache, Hash as CacheHash}; -use crate::control_flow::time::{ExponentialBackoff, SleepStrategy}; -use crate::core::hash::Hash; -use crate::vm::wasm::run::untrusted_wasm_store; -use crate::vm::wasm::{self, memory}; -use crate::vm::{WasmCacheAccess, WasmCacheRoAccess}; +use crate::wasm::run::untrusted_wasm_store; +use crate::wasm::{self, memory}; +use crate::{WasmCacheAccess, WasmCacheRoAccess}; /// Cache handle. Thread-safe. #[derive(Debug, Clone)] @@ -114,7 +114,7 @@ impl Cache { } /// Get a WASM module from LRU cache, from a file or compile it and cache - /// it. If the cache access is set to [`crate::vm::WasmCacheRwAccess`], it + /// it. If the cache access is set to [`crate::WasmCacheRwAccess`], it /// updates the position in the LRU cache. Otherwise, the compiled /// module will not be be cached, if it's not already. pub fn fetch( @@ -604,7 +604,7 @@ pub mod testing { use tempfile::{tempdir, TempDir}; use super::*; - use crate::vm::WasmCacheRwAccess; + use crate::WasmCacheRwAccess; /// Instantiate the default wasmer store. pub fn store() -> Store { @@ -628,13 +628,14 @@ pub mod testing { mod test { use std::cmp::max; + use assert_matches::assert_matches; use byte_unit::Byte; use namada_test_utils::TestWasms; use tempfile::{tempdir, TempDir}; use test_log::test; use super::*; - use crate::vm::WasmCacheRwAccess; + use crate::WasmCacheRwAccess; #[test] fn test_fetch_or_compile_valid_wasm() { diff --git a/crates/vm/src/wasm/host_env.rs b/crates/vm/src/wasm/host_env.rs index ae73a15366..b2d62ef103 100644 --- a/crates/vm/src/wasm/host_env.rs +++ b/crates/vm/src/wasm/host_env.rs @@ -6,9 +6,9 @@ use namada_state::{DBIter, StorageHasher, DB}; use wasmer::{Function, FunctionEnv, Imports}; -use crate::vm::host_env::{TxVmEnv, VpEvaluator, VpVmEnv}; -use crate::vm::wasm::memory::WasmMemory; -use crate::vm::{host_env, WasmCacheAccess}; +use crate::host_env::{TxVmEnv, VpEvaluator, VpVmEnv}; +use crate::wasm::memory::WasmMemory; +use crate::{host_env, WasmCacheAccess}; /// Prepare imports (memory and host functions) exposed to the vm guest running /// transaction code @@ -116,9 +116,9 @@ mod wrap_tx { use namada_state::{DBIter, StorageHasher, DB}; use wasmer::FunctionEnvMut; - use crate::vm::host_env::TxVmEnv; - use crate::vm::wasm::memory::WasmMemory; - use crate::vm::WasmCacheAccess; + use crate::host_env::TxVmEnv; + use crate::wasm::memory::WasmMemory; + use crate::WasmCacheAccess; pub(super) fn _0( f: F, @@ -297,9 +297,9 @@ mod wrap_vp { use namada_state::{DBIter, StorageHasher, DB}; use wasmer::FunctionEnvMut; - use crate::vm::host_env::{VpEvaluator, VpVmEnv}; - use crate::vm::wasm::memory::WasmMemory; - use crate::vm::WasmCacheAccess; + use crate::host_env::{VpEvaluator, VpVmEnv}; + use crate::wasm::memory::WasmMemory; + use crate::WasmCacheAccess; pub(super) fn _0( f: F, diff --git a/crates/vm/src/wasm/memory.rs b/crates/vm/src/wasm/memory.rs index 6404ff874f..373925a93d 100644 --- a/crates/vm/src/wasm/memory.rs +++ b/crates/vm/src/wasm/memory.rs @@ -6,7 +6,7 @@ use std::ptr::NonNull; use std::rc::{self, Rc}; use std::str::Utf8Error; -use borsh_ext::BorshSerializeExt; +use namada_core::borsh::BorshSerializeExt; use namada_gas::MEMORY_ACCESS_GAS_PER_BYTE; use namada_sdk::arith::{self, checked}; use namada_tx::BatchedTxRef; @@ -20,8 +20,8 @@ use wasmer_vm::{ MemoryStyle, TableStyle, VMMemoryDefinition, VMTableDefinition, }; -use crate::vm::memory::VmMemory; -use crate::vm::types::VpInput; +use crate::memory::VmMemory; +use crate::types::VpInput; #[allow(missing_docs)] #[derive(Error, Debug)] diff --git a/crates/vm/src/wasm/run.rs b/crates/vm/src/wasm/run.rs index ebf158cc1d..86ee3d50f9 100644 --- a/crates/vm/src/wasm/run.rs +++ b/crates/vm/src/wasm/run.rs @@ -9,11 +9,16 @@ use std::num::NonZeroU32; use std::rc::Rc; use borsh::BorshDeserialize; +use namada_core::address::Address; +use namada_core::hash::{Error as TxHashError, Hash}; +use namada_core::internal::HostEnvResult; +use namada_core::storage::{Key, TxIndex}; use namada_core::validity_predicate::VpError; -use namada_gas::{GasMetering, TxGasMeter, WASM_MEMORY_PAGE_GAS}; +use namada_gas::{GasMetering, TxGasMeter, VpGasMeter, WASM_MEMORY_PAGE_GAS}; use namada_state::{DBIter, State, StateRead, StorageHasher, StorageRead, DB}; use namada_tx::data::{TxSentinel, TxType}; use namada_tx::{BatchedTxRef, Commitment, Section, Tx, TxCommitments}; +use namada_vp::vp_host_fns; use parity_wasm::elements::Instruction::*; use parity_wasm::elements::{self, SignExtInstruction}; use thiserror::Error; @@ -22,18 +27,12 @@ use wasmer::{Engine, Module, NativeEngineExt, Store, Target}; use super::memory::{Limit, WasmMemory}; use super::TxCache; -use crate::address::Address; -use crate::hash::{Error as TxHashError, Hash}; -use crate::internal::HostEnvResult; -use crate::ledger::gas::VpGasMeter; -use crate::ledger::vp_host_fns; -use crate::storage::{Key, TxIndex}; -use crate::vm::host_env::{TxVmEnv, VpCtx, VpEvaluator, VpVmEnv}; -use crate::vm::prefix_iter::PrefixIterators; -use crate::vm::types::VpInput; -use crate::vm::wasm::host_env::{tx_imports, vp_imports}; -use crate::vm::wasm::{memory, Cache, CacheName, VpCache}; -use crate::vm::{ +use crate::host_env::{TxVmEnv, VpCtx, VpEvaluator, VpVmEnv}; +use crate::prefix_iter::PrefixIterators; +use crate::types::VpInput; +use crate::wasm::host_env::{tx_imports, vp_imports}; +use crate::wasm::{memory, Cache, CacheName, VpCache}; +use crate::{ validate_untrusted_wasm, HostRef, RwAccess, WasmCacheAccess, WasmValidationError, }; @@ -123,7 +122,7 @@ where .get_section(cmt.code_sechash()) .and_then(|x| Section::code_sec(&x)) { - if crate::parameters::is_tx_allowed(storage, &code_sec.code.hash()) + if namada_parameters::is_tx_allowed(storage, &code_sec.code.hash()) .map_err(|e| Error::StorageError(e.to_string()))? { return Ok(()); @@ -945,12 +944,15 @@ mod tests { use std::collections::BTreeMap; use std::error::Error as StdErrorTrait; - use borsh_ext::BorshSerializeExt; + use assert_matches::assert_matches; use itertools::Either; + use namada_core::borsh::BorshSerializeExt; use namada_sdk::arith::checked; + use namada_state::testing::TestState; use namada_state::StorageWrite; use namada_test_utils::TestWasms; use namada_token::DenominatedAmount; + use namada_tx::data::eval_vp::EvalVp; use namada_tx::data::{Fee, TxType}; use namada_tx::{Code, Data}; use test_log::test; @@ -959,10 +961,8 @@ mod tests { use super::memory::{TX_MEMORY_INIT_PAGES, VP_MEMORY_INIT_PAGES}; use super::*; - use crate::state::testing::TestState; - use crate::tx::data::eval_vp::EvalVp; - use crate::vm::host_env::TxRuntimeError; - use crate::vm::wasm; + use crate::host_env::TxRuntimeError; + use crate::wasm; const TX_GAS_LIMIT: u64 = 10_000_000_000_000; const OUT_OF_GAS_LIMIT: u64 = 10_000; @@ -1682,7 +1682,7 @@ mod tests { // tx is ok even if not allowlisted { let allowlist = vec![format!("{}-bad", read_code_hash)]; - crate::parameters::update_tx_allowlist_parameter( + namada_parameters::update_tx_allowlist_parameter( &mut state, allowlist, ) .unwrap(); @@ -1701,7 +1701,7 @@ mod tests { // `Error::DisallowedTx` { let allowlist = vec![read_code_hash.to_string()]; - crate::parameters::update_tx_allowlist_parameter( + namada_parameters::update_tx_allowlist_parameter( &mut state, allowlist, ) .unwrap(); From 02c243878542a8d74eb96ca4bed223dd44bde6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 19:29:37 +0200 Subject: [PATCH 07/41] mv crates/namada/src/ledger/protocol/mod.rs crates/node/src/protocol.rs --- .../{namada/src/ledger/protocol/mod.rs => node/src/protocol.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/protocol/mod.rs => node/src/protocol.rs} (100%) diff --git a/crates/namada/src/ledger/protocol/mod.rs b/crates/node/src/protocol.rs similarity index 100% rename from crates/namada/src/ledger/protocol/mod.rs rename to crates/node/src/protocol.rs From 9ec5a671f2cad7c3c36557b2d18d4baa7c6000dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 20:01:17 +0200 Subject: [PATCH 08/41] mv crates/namada/src/ledger/native_vp/mod.rs crates/vp/src/native_vp.rs --- .../{namada/src/ledger/native_vp/mod.rs => vp/src/native_vp.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/native_vp/mod.rs => vp/src/native_vp.rs} (100%) diff --git a/crates/namada/src/ledger/native_vp/mod.rs b/crates/vp/src/native_vp.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/mod.rs rename to crates/vp/src/native_vp.rs From 701e54d7a7b3baf26bf6d971bf232f9c06444598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 20:09:34 +0200 Subject: [PATCH 09/41] mv crates/vm/src/prefix_iter.rs crates/state/src/ --- crates/{vm => state}/src/prefix_iter.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{vm => state}/src/prefix_iter.rs (100%) diff --git a/crates/vm/src/prefix_iter.rs b/crates/state/src/prefix_iter.rs similarity index 100% rename from crates/vm/src/prefix_iter.rs rename to crates/state/src/prefix_iter.rs From cb92a41816bf5ab891dde02e88d15973e133f705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 5 Jun 2024 20:12:53 +0200 Subject: [PATCH 10/41] mv wasm cache access trait and impls into core --- crates/core/src/lib.rs | 2 ++ crates/core/src/wasm_cache.rs | 25 +++++++++++++++++++++++++ crates/vm/src/lib.rs | 26 -------------------------- 3 files changed, 27 insertions(+), 26 deletions(-) create mode 100644 crates/core/src/wasm_cache.rs diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 522db30aa5..5b44b4cb06 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -20,7 +20,9 @@ pub mod arith; pub mod bytes; pub mod hints; +mod wasm_cache; +pub use wasm_cache::{WasmCacheAccess, WasmCacheRoAccess, WasmCacheRwAccess}; // TODO(namada#3248): only re-export v037 `tendermint-rs` pub use {masp_primitives, tendermint, tendermint_proto}; /// Borsh binary encoding (re-exported) from official crate with custom ext. diff --git a/crates/core/src/wasm_cache.rs b/crates/core/src/wasm_cache.rs new file mode 100644 index 0000000000..1d47cf5680 --- /dev/null +++ b/crates/core/src/wasm_cache.rs @@ -0,0 +1,25 @@ +/// WASM Cache access level, used to limit dry-ran transactions to read-only +/// cache access. +pub trait WasmCacheAccess: Clone + std::fmt::Debug + Default { + /// Is access read/write? + fn is_read_write() -> bool; +} + +/// Regular read/write caches access +#[derive(Debug, Clone, Default)] +pub struct WasmCacheRwAccess; +impl WasmCacheAccess for WasmCacheRwAccess { + fn is_read_write() -> bool { + true + } +} + +/// Restricted read-only access for dry-ran transactions +#[derive(Debug, Clone, Default)] +pub struct WasmCacheRoAccess; + +impl WasmCacheAccess for WasmCacheRoAccess { + fn is_read_write() -> bool { + false + } +} diff --git a/crates/vm/src/lib.rs b/crates/vm/src/lib.rs index 9754e3c0e0..802b659146 100644 --- a/crates/vm/src/lib.rs +++ b/crates/vm/src/lib.rs @@ -45,32 +45,6 @@ pub enum WasmValidationError { ForbiddenWasmFeatures(wasmparser::BinaryReaderError), } -/// WASM Cache access level, used to limit dry-ran transactions to read-only -/// cache access. -pub trait WasmCacheAccess: Clone + std::fmt::Debug + Default { - /// Is access read/write? - fn is_read_write() -> bool; -} - -/// Regular read/write caches access -#[derive(Debug, Clone, Default)] -pub struct WasmCacheRwAccess; -impl WasmCacheAccess for WasmCacheRwAccess { - fn is_read_write() -> bool { - true - } -} - -/// Restricted read-only access for dry-ran transactions -#[derive(Debug, Clone, Default)] -pub struct WasmCacheRoAccess; - -impl WasmCacheAccess for WasmCacheRoAccess { - fn is_read_write() -> bool { - false - } -} - /// Read-only access to host data. #[derive(Debug)] pub enum RoAccess {} From f07fc290011a0266849eedb09314cfc8693378fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Thu, 6 Jun 2024 12:36:27 +0100 Subject: [PATCH 11/41] replace state dep on parameters with DI --- crates/core/src/parameters.rs | 26 +++++++++++++++++ crates/core/src/proof_of_stake.rs | 37 +++++++++++++++++++++++++ crates/parameters/src/lib.rs | 46 +++++++++++++++++++++++++++++-- crates/parameters/src/storage.rs | 3 +- crates/state/Cargo.toml | 2 +- crates/state/src/in_memory.rs | 2 +- crates/state/src/lib.rs | 31 +++++++++++---------- crates/state/src/prefix_iter.rs | 23 ++++++++-------- crates/state/src/wl_state.rs | 14 +++++----- crates/state/src/write_log.rs | 7 +++-- 10 files changed, 150 insertions(+), 41 deletions(-) create mode 100644 crates/core/src/proof_of_stake.rs diff --git a/crates/core/src/parameters.rs b/crates/core/src/parameters.rs index 78fb2907c3..bcac80fc1a 100644 --- a/crates/core/src/parameters.rs +++ b/crates/core/src/parameters.rs @@ -12,6 +12,32 @@ use super::hash::Hash; use super::time::DurationSecs; use super::token; use crate::borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; +use crate::storage; + +/// Abstract parameters storage keys interface +pub trait Keys { + /// Key for implicit VP + fn implicit_vp() -> storage::Key; +} + +/// Abstract parameters storage read interface +pub trait Read { + /// Storage error + type Err; + + /// Read all parameters + fn read(storage: &S) -> Result; + + /// Read MASP epoch multiplier + fn read_masp_epoch_multiplier(storage: &S) -> Result; +} + +/// Abstract parameters storage write interface +pub trait Write: Read { + /// Write all parameters + fn write(storage: &mut S, parameters: &Parameters) + -> Result<(), Self::Err>; +} /// Protocol parameters #[derive( diff --git a/crates/core/src/proof_of_stake.rs b/crates/core/src/proof_of_stake.rs new file mode 100644 index 0000000000..c174abd03b --- /dev/null +++ b/crates/core/src/proof_of_stake.rs @@ -0,0 +1,37 @@ +//! Proof-of-Stake abstract interfaces + +use crate::address::Address; +use crate::storage::Epoch; +use crate::{storage, token}; + +/// Abstract PoS storage read interface +pub trait Read { + /// Storage error + type Err; + + /// Check if the provided address is a validator address + fn is_validator(storage: &S, address: &Address) -> Result; + + /// Check if the provided address is a delegator address, optionally at a + /// particular epoch. Returns `false` if the address is a validator. + fn is_delegator( + storage: &S, + address: &Address, + epoch: Option, + ) -> Result; +} + +/// Abstract PoS storage write interface +pub trait Write: Read { + /// Self-bond tokens to a validator when `source` is `None` or equal to + /// the `validator` address, or delegate tokens from the `source` to the + /// `validator`. + fn bond_tokens( + storage: &mut S, + source: Option<&Address>, + validator: &Address, + amount: token::Amount, + current_epoch: Epoch, + offset_opt: Option, + ) -> Result<(), Self::Err>; +} diff --git a/crates/parameters/src/lib.rs b/crates/parameters/src/lib.rs index 595df8fa05..02fbfcd8b1 100644 --- a/crates/parameters/src/lib.rs +++ b/crates/parameters/src/lib.rs @@ -20,12 +20,13 @@ pub mod storage; mod wasm_allowlist; use std::collections::BTreeMap; +use std::marker::PhantomData; use namada_core::address::{Address, InternalAddress}; use namada_core::arith::checked; use namada_core::chain::ProposalBytes; pub use namada_core::parameters::*; -use namada_core::storage::{BlockHeight, Key}; +use namada_core::storage::BlockHeight; use namada_core::time::DurationSecs; use namada_core::{hints, token}; use namada_storage::{ResultExt, StorageRead, StorageWrite}; @@ -33,6 +34,47 @@ pub use storage::{get_gas_scale, get_max_block_gas}; use thiserror::Error; pub use wasm_allowlist::{is_tx_allowed, is_vp_allowed}; +/// Parameters storage keys implementation +#[derive(Debug)] +pub struct Key; + +/// Parameters storage `Read/Write` implementation +#[derive(Debug)] +pub struct Store(PhantomData); + +impl Keys for Key { + fn implicit_vp() -> namada_core::storage::Key { + storage::get_implicit_vp_key() + } +} + +impl Read for Store +where + S: StorageRead, +{ + type Err = namada_storage::Error; + + fn read(storage: &S) -> Result { + read(storage) + } + + fn read_masp_epoch_multiplier(storage: &S) -> Result { + read_masp_epoch_multiplier_parameter(storage) + } +} + +impl Write for Store +where + S: StorageRead + StorageWrite, +{ + fn write( + storage: &mut S, + parameters: &Parameters, + ) -> Result<(), Self::Err> { + init_storage(parameters, storage) + } +} + /// The internal address for storage keys representing parameters than /// can be changed via governance. pub const ADDRESS: Address = Address::Internal(InternalAddress::Parameters); @@ -409,7 +451,7 @@ where } /// Storage key for the Ethereum address of wNam. -pub fn native_erc20_key() -> Key { +pub fn native_erc20_key() -> storage::Key { storage::get_native_erc20_key_at_addr(ADDRESS) } diff --git a/crates/parameters/src/storage.rs b/crates/parameters/src/storage.rs index 31b8adfe51..f0435f7e6d 100644 --- a/crates/parameters/src/storage.rs +++ b/crates/parameters/src/storage.rs @@ -1,7 +1,8 @@ //! Parameters storage use namada_core::address::Address; -use namada_core::storage::{DbKeySeg, Key}; +use namada_core::storage::DbKeySeg; +pub use namada_core::storage::Key; use namada_macros::StorageKeys; use namada_storage::StorageRead; diff --git a/crates/state/Cargo.toml b/crates/state/Cargo.toml index 30985f248b..15254e328d 100644 --- a/crates/state/Cargo.toml +++ b/crates/state/Cargo.toml @@ -34,7 +34,6 @@ namada_gas = { path = "../gas" } namada_macros = { path = "../macros" } namada_merkle_tree = { path = "../merkle_tree" } namada_migrations = { path = "../migrations", optional = true } -namada_parameters = { path = "../parameters" } namada_replay_protection = { path = "../replay_protection" } namada_storage = { path = "../storage" } namada_tx = { path = "../tx" } @@ -52,6 +51,7 @@ proptest = { workspace = true, optional = true } [dev-dependencies] namada_core = { path = "../core", features = ["testing"] } namada_merkle_tree = { path = "../merkle_tree", features = ["testing"] } +namada_parameters = { path = "../parameters", features = ["testing"] } assert_matches.workspace = true chrono.workspace = true diff --git a/crates/state/src/in_memory.rs b/crates/state/src/in_memory.rs index e4c8d9817a..752b378f9d 100644 --- a/crates/state/src/in_memory.rs +++ b/crates/state/src/in_memory.rs @@ -5,6 +5,7 @@ use namada_core::address::{Address, EstablishedAddressGen, InternalAddress}; use namada_core::borsh::{BorshDeserialize, BorshSerialize}; use namada_core::chain::{ChainId, CHAIN_ID_LENGTH}; use namada_core::hash::Hash; +use namada_core::parameters::{EpochDuration, Parameters}; use namada_core::time::DateTimeUtc; use namada_core::{encode, ethereum_structs}; use namada_gas::MEMORY_ACCESS_GAS_PER_BYTE; @@ -12,7 +13,6 @@ use namada_macros::BorshDeserializer; use namada_merkle_tree::{MerkleRoot, MerkleTree}; #[cfg(feature = "migrations")] use namada_migrations::*; -use namada_parameters::{EpochDuration, Parameters}; use namada_storage::conversion_state::ConversionState; use namada_storage::tx_queue::ExpiredTxsQueue; use namada_storage::types::CommitOnlyData; diff --git a/crates/state/src/lib.rs b/crates/state/src/lib.rs index b5b8cdeca5..57277990c9 100644 --- a/crates/state/src/lib.rs +++ b/crates/state/src/lib.rs @@ -19,6 +19,7 @@ mod host_env; mod in_memory; +pub mod prefix_iter; mod wl_state; pub mod write_log; @@ -34,6 +35,7 @@ use namada_core::arith::{self, checked}; use namada_core::eth_bridge_pool::is_pending_transfer_key; pub use namada_core::hash::Sha256Hasher; use namada_core::hash::{Error as HashError, Hash}; +use namada_core::parameters; pub use namada_core::storage::{ BlockHash, BlockHeight, BlockResults, Epoch, Epochs, EthEventsQueue, Header, Key, KeySeg, TxIndex, BLOCK_HASH_LENGTH, BLOCK_HEIGHT_LENGTH, @@ -140,12 +142,13 @@ pub trait StateRead: StorageRead + Debug { /// Get the hash of a validity predicate for the given account address and /// the gas cost for reading it. - fn validity_predicate( + fn validity_predicate( &self, addr: &Address, + _: &ParamsKey, ) -> Result<(Option, u64)> { let key = if let Address::Implicit(_) = addr { - namada_parameters::storage::get_implicit_vp_key() + ParamsKey::implicit_vp() } else { Key::validity_predicate(addr) }; @@ -691,9 +694,9 @@ mod tests { use merkle_tree::NO_DIFF_KEY_PREFIX; use namada_core::address::InternalAddress; use namada_core::borsh::{BorshDeserialize, BorshSerializeExt}; + use namada_core::parameters::{EpochDuration, Parameters}; use namada_core::storage::DbKeySeg; use namada_core::time::{self, DateTimeUtc, Duration}; - use namada_parameters::{EpochDuration, Parameters}; use proptest::prelude::*; use proptest::test_runner::Config; // Use `RUST_LOG=info` (or another tracing level) and `--nocapture` to @@ -776,7 +779,6 @@ mod tests { minimum_gas_price: BTreeMap::default(), is_native_token_transferable: true, }; - namada_parameters::init_storage(¶meters, &mut state).unwrap(); // Initialize pred_epochs to the current height let height = state.in_mem().block.height; state @@ -789,7 +791,7 @@ mod tests { assert_eq!(epoch_before, state.in_mem().block.epoch); // Try to apply the epoch update - state.update_epoch(block_height, block_time).unwrap(); + state.update_epoch(block_height, block_time, ¶meters).unwrap(); // Test for 1. if block_height.0 - start_height.0 @@ -806,13 +808,13 @@ mod tests { let block_height = block_height + 1; let block_time = block_time + Duration::seconds(1); - state.update_epoch(block_height, block_time).unwrap(); + state.update_epoch(block_height, block_time, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before); assert_eq!(state.in_mem().update_epoch_blocks_delay, Some(1)); let block_height = block_height + 1; let block_time = block_time + Duration::seconds(1); - state.update_epoch(block_height, block_time).unwrap(); + state.update_epoch(block_height, block_time, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before.next()); assert!(state.in_mem().update_epoch_blocks_delay.is_none()); @@ -845,7 +847,6 @@ mod tests { let min_duration: i64 = parameters.epoch_duration.min_duration.0 as _; parameters.epoch_duration.min_duration = Duration::seconds(min_duration + min_duration_delta).into(); - namada_parameters::update_epoch_parameter(&mut state, ¶meters.epoch_duration).unwrap(); // Test for 2. let epoch_before = state.in_mem().block.epoch; @@ -857,31 +858,31 @@ mod tests { // No update should happen before both epoch duration conditions are // satisfied - state.update_epoch(height_before_update, time_before_update).unwrap(); + state.update_epoch(height_before_update, time_before_update, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before); assert!(state.in_mem().update_epoch_blocks_delay.is_none()); - state.update_epoch(height_of_update, time_before_update).unwrap(); + state.update_epoch(height_of_update, time_before_update, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before); assert!(state.in_mem().update_epoch_blocks_delay.is_none()); - state.update_epoch(height_before_update, time_of_update).unwrap(); + state.update_epoch(height_before_update, time_of_update, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before); assert!(state.in_mem().update_epoch_blocks_delay.is_none()); // Update should be enqueued for 2 blocks in the future starting at or after this height and time - state.update_epoch(height_of_update, time_of_update).unwrap(); + state.update_epoch(height_of_update, time_of_update, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before); assert_eq!(state.in_mem().update_epoch_blocks_delay, Some(2)); // Increment the block height and time to simulate new blocks now let height_of_update = height_of_update + 1; let time_of_update = time_of_update + Duration::seconds(1); - state.update_epoch(height_of_update, time_of_update).unwrap(); + state.update_epoch(height_of_update, time_of_update, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before); assert_eq!(state.in_mem().update_epoch_blocks_delay, Some(1)); let height_of_update = height_of_update + 1; let time_of_update = time_of_update + Duration::seconds(1); - state.update_epoch(height_of_update, time_of_update).unwrap(); + state.update_epoch(height_of_update, time_of_update, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before.next()); assert!(state.in_mem().update_epoch_blocks_delay.is_none()); // The next epoch's minimum duration should change @@ -893,7 +894,7 @@ mod tests { // Increment the block height and time once more to make sure things reset let height_of_update = height_of_update + 1; let time_of_update = time_of_update + Duration::seconds(1); - state.update_epoch(height_of_update, time_of_update).unwrap(); + state.update_epoch(height_of_update, time_of_update, ¶meters).unwrap(); assert_eq!(state.in_mem().block.epoch, epoch_before.next()); } } diff --git a/crates/state/src/prefix_iter.rs b/crates/state/src/prefix_iter.rs index a36e52272b..94668654d4 100644 --- a/crates/state/src/prefix_iter.rs +++ b/crates/state/src/prefix_iter.rs @@ -2,28 +2,29 @@ //! storage keys. use namada_core::collections::HashMap; -use namada_state::PrefixIter; + +use crate::{DBIter, PrefixIter, DB}; /// A temporary iterators storage, used during a wasm run after which it's /// dropped. Each iterator is assigned a [`PrefixIteratorId`]. #[derive(Debug)] -pub struct PrefixIterators<'iter, DB> +pub struct PrefixIterators<'iter, D> where - DB: namada_state::DB + namada_state::DBIter<'iter>, + D: DB + DBIter<'iter>, { index: PrefixIteratorId, - iterators: HashMap>, + iterators: HashMap>, } -impl<'iter, DB> PrefixIterators<'iter, DB> +impl<'iter, D> PrefixIterators<'iter, D> where - DB: namada_state::DB + namada_state::DBIter<'iter>, + D: DB + DBIter<'iter>, { /// Insert a new prefix iterator to the temporary storage. Returns `None` on /// prefix iterator ID overflow pub fn insert( &mut self, - iter: PrefixIter<'iter, DB>, + iter: PrefixIter<'iter, D>, ) -> Option { let id = self.index; self.iterators.insert(id, iter); @@ -35,7 +36,7 @@ where pub fn next( &mut self, id: PrefixIteratorId, - ) -> Option< as Iterator>::Item> { + ) -> Option< as Iterator>::Item> { self.iterators.get_mut(&id).and_then(|i| i.next()) } @@ -43,14 +44,14 @@ where pub fn get_mut( &mut self, id: PrefixIteratorId, - ) -> Option<&mut PrefixIter<'iter, DB>> { + ) -> Option<&mut PrefixIter<'iter, D>> { self.iterators.get_mut(&id) } } -impl<'iter, DB> Default for PrefixIterators<'iter, DB> +impl<'iter, D> Default for PrefixIterators<'iter, D> where - DB: namada_state::DB + namada_state::DBIter<'iter>, + D: DB + DBIter<'iter>, { fn default() -> Self { Self { diff --git a/crates/state/src/wl_state.rs b/crates/state/src/wl_state.rs index 8f6ebf1984..b301ea9b69 100644 --- a/crates/state/src/wl_state.rs +++ b/crates/state/src/wl_state.rs @@ -6,11 +6,11 @@ use namada_core::arith::checked; use namada_core::borsh::BorshSerializeExt; use namada_core::chain::ChainId; use namada_core::masp::MaspEpoch; +use namada_core::parameters::{EpochDuration, Parameters}; use namada_core::storage; use namada_core::time::DateTimeUtc; use namada_events::{EmitEvents, EventToEmit}; use namada_merkle_tree::NO_DIFF_KEY_PREFIX; -use namada_parameters::EpochDuration; use namada_replay_protection as replay_protection; use namada_storage::conversion_state::{ConversionState, WithConversionState}; use namada_storage::{ @@ -153,10 +153,8 @@ where &mut self, height: BlockHeight, time: DateTimeUtc, + parameters: &Parameters, ) -> StorageResult { - let parameters = namada_parameters::read(self) - .expect("Couldn't read protocol parameters"); - match self.in_mem.update_epoch_blocks_delay.as_mut() { None => { // Check if the new epoch minimum start height and start time @@ -207,9 +205,11 @@ where } /// Returns `true` if a new masp epoch has begun - pub fn is_masp_new_epoch(&self, is_new_epoch: bool) -> StorageResult { - let masp_epoch_multiplier = - namada_parameters::read_masp_epoch_multiplier_parameter(self)?; + pub fn is_masp_new_epoch( + &self, + is_new_epoch: bool, + masp_epoch_multiplier: u64, + ) -> StorageResult { let masp_new_epoch = is_new_epoch && matches!( self.in_mem.block.epoch.checked_rem(masp_epoch_multiplier), diff --git a/crates/state/src/write_log.rs b/crates/state/src/write_log.rs index 1d56c4699f..48ee58afa3 100644 --- a/crates/state/src/write_log.rs +++ b/crates/state/src/write_log.rs @@ -741,7 +741,7 @@ impl WriteLog { #[cfg(test)] mod tests { use assert_matches::assert_matches; - use namada_core::address; + use namada_core::{address, parameters}; use pretty_assertions::assert_eq; use proptest::prelude::*; @@ -948,8 +948,9 @@ mod tests { // commit a block state.commit_block().expect("commit failed"); - let (vp_code_hash, _gas) = - state.validity_predicate(&addr1).expect("vp read failed"); + let (vp_code_hash, _gas) = state + .validity_predicate(&addr1, &namada_parameters::Key) + .expect("vp read failed"); assert_eq!(vp_code_hash, Some(vp1)); let (value, _) = state.db_read(&key1).expect("read failed"); assert_eq!(value.expect("no read value"), val1); From 697a8c445f8da5c7b4123b6a610250000fcb75c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Thu, 6 Jun 2024 12:39:52 +0100 Subject: [PATCH 12/41] mv crates/namada/src/ledger/governance/ crates/governance/src/vp/ --- crates/{namada/src/ledger/governance => governance/src/vp}/mod.rs | 0 .../{namada/src/ledger/governance => governance/src/vp}/utils.rs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/governance => governance/src/vp}/mod.rs (100%) rename crates/{namada/src/ledger/governance => governance/src/vp}/utils.rs (100%) diff --git a/crates/namada/src/ledger/governance/mod.rs b/crates/governance/src/vp/mod.rs similarity index 100% rename from crates/namada/src/ledger/governance/mod.rs rename to crates/governance/src/vp/mod.rs diff --git a/crates/namada/src/ledger/governance/utils.rs b/crates/governance/src/vp/utils.rs similarity index 100% rename from crates/namada/src/ledger/governance/utils.rs rename to crates/governance/src/vp/utils.rs From bd7d1e744af7781b440f983763ed6d5dbd26fa50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Tue, 11 Jun 2024 21:35:05 +0100 Subject: [PATCH 13/41] governance: fix VP post move --- Cargo.lock | 14 +- crates/core/src/ledger/mod.rs | 0 crates/core/src/lib.rs | 1 + crates/core/src/parameters.rs | 23 ++ crates/core/src/proof_of_stake.rs | 18 +- crates/core/src/storage.rs | 14 + crates/core/src/token.rs | 14 + crates/governance/Cargo.toml | 20 +- crates/governance/src/lib.rs | 1 + crates/governance/src/parameters.rs | 4 +- crates/governance/src/pgf/inflation.rs | 6 +- crates/governance/src/pgf/parameters.rs | 4 +- crates/governance/src/pgf/storage/keys.rs | 2 +- crates/governance/src/pgf/storage/mod.rs | 19 +- crates/governance/src/storage/mod.rs | 38 ++- crates/governance/src/vp/mod.rs | 274 ++++++++++-------- crates/namada/src/ledger/native_vp/ibc/mod.rs | 62 +--- crates/node/src/storage/mod.rs | 19 +- crates/proof_of_stake/src/lib.rs | 69 +++++ crates/state/src/write_log.rs | 2 +- crates/tests/src/vm_host_env/ibc.rs | 10 +- crates/tests/src/vm_host_env/mod.rs | 23 +- crates/trans_token/src/lib.rs | 20 ++ crates/vm/src/host_env.rs | 59 +--- crates/vm/src/lib.rs | 1 - .../vm/src/wasm/compilation_cache/common.rs | 22 +- crates/vm/src/wasm/host_env.rs | 7 +- crates/vm/src/wasm/run.rs | 108 +++++-- crates/vp/Cargo.toml | 4 + crates/vp/src/lib.rs | 3 + crates/vp/src/native_vp.rs | 210 ++++++-------- crates/vp_env/Cargo.toml | 1 - 32 files changed, 600 insertions(+), 472 deletions(-) delete mode 100644 crates/core/src/ledger/mod.rs diff --git a/Cargo.lock b/Cargo.lock index d421becfc2..f972f7ebaf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4805,17 +4805,25 @@ dependencies = [ name = "namada_governance" version = "0.41.0" dependencies = [ + "assert_matches", "borsh", "itertools 0.12.1", "konst", "linkme", + "namada_account", "namada_core", "namada_events", + "namada_gas", "namada_macros", "namada_migrations", "namada_parameters", - "namada_storage", + "namada_proof_of_stake", + "namada_state", + "namada_token", "namada_trans_token", + "namada_tx", + "namada_vm", + "namada_vp", "proptest", "serde", "serde_json", @@ -4941,8 +4949,10 @@ dependencies = [ "namada", "namada_apps_lib", "namada_migrations", + "namada_parameters", "namada_sdk", "namada_test_utils", + "namada_vm", "num-rational", "num-traits 0.2.17", "num256", @@ -5383,6 +5393,7 @@ dependencies = [ "namada_gas", "namada_state", "namada_tx", + "namada_vp_env", "smooth-operator", "thiserror", "tracing", @@ -5396,7 +5407,6 @@ dependencies = [ "masp_primitives", "namada_core", "namada_events", - "namada_ibc", "namada_storage", "namada_tx", "smooth-operator", diff --git a/crates/core/src/ledger/mod.rs b/crates/core/src/ledger/mod.rs deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 5b44b4cb06..4e2ff1734e 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -20,6 +20,7 @@ pub mod arith; pub mod bytes; pub mod hints; +pub mod proof_of_stake; mod wasm_cache; pub use wasm_cache::{WasmCacheAccess, WasmCacheRoAccess, WasmCacheRwAccess}; diff --git a/crates/core/src/parameters.rs b/crates/core/src/parameters.rs index bcac80fc1a..5e996f3b35 100644 --- a/crates/core/src/parameters.rs +++ b/crates/core/src/parameters.rs @@ -104,3 +104,26 @@ pub struct EpochDuration { /// Minimum duration of an epoch pub min_duration: DurationSecs, } + +impl Default for Parameters { + fn default() -> Self { + Parameters { + max_tx_bytes: 1024 * 1024, + epoch_duration: EpochDuration { + min_num_of_blocks: 1, + min_duration: DurationSecs(3600), + }, + max_proposal_bytes: Default::default(), + max_block_gas: 100, + vp_allowlist: vec![], + tx_allowlist: vec![], + implicit_vp_code_hash: Default::default(), + epochs_per_year: 365, + masp_epoch_multiplier: 2, + masp_fee_payment_gas_limit: 0, + gas_scale: 100_000_000, + minimum_gas_price: Default::default(), + is_native_token_transferable: true, + } + } +} diff --git a/crates/core/src/proof_of_stake.rs b/crates/core/src/proof_of_stake.rs index c174abd03b..ca289bec3e 100644 --- a/crates/core/src/proof_of_stake.rs +++ b/crates/core/src/proof_of_stake.rs @@ -1,8 +1,7 @@ //! Proof-of-Stake abstract interfaces use crate::address::Address; -use crate::storage::Epoch; -use crate::{storage, token}; +use crate::storage; /// Abstract PoS storage read interface pub trait Read { @@ -20,18 +19,3 @@ pub trait Read { epoch: Option, ) -> Result; } - -/// Abstract PoS storage write interface -pub trait Write: Read { - /// Self-bond tokens to a validator when `source` is `None` or equal to - /// the `validator` address, or delegate tokens from the `source` to the - /// `validator`. - fn bond_tokens( - storage: &mut S, - source: Option<&Address>, - validator: &Address, - amount: token::Amount, - current_epoch: Epoch, - offset_opt: Option, - ) -> Result<(), Self::Err>; -} diff --git a/crates/core/src/storage.rs b/crates/core/src/storage.rs index d976398bee..a443cb7805 100644 --- a/crates/core/src/storage.rs +++ b/crates/core/src/storage.rs @@ -2099,4 +2099,18 @@ pub mod testing { 1 => arb_address().prop_map(DbKeySeg::AddressSeg), ] } + + /// A dummy header used for testing + pub fn get_dummy_header() -> Header { + use crate::time::{DateTimeUtc, DurationSecs}; + Header { + hash: Hash([0; 32]), + #[allow( + clippy::disallowed_methods, + clippy::arithmetic_side_effects + )] + time: DateTimeUtc::now() + DurationSecs(5), + next_validators_hash: Hash([0; 32]), + } + } } diff --git a/crates/core/src/token.rs b/crates/core/src/token.rs index 70cdeff7dc..f792825c7f 100644 --- a/crates/core/src/token.rs +++ b/crates/core/src/token.rs @@ -14,12 +14,26 @@ use namada_migrations::*; use serde::{Deserialize, Serialize}; use thiserror::Error; +use crate::address::Address; use crate::arith::{self, checked, CheckedAdd, CheckedSub}; use crate::dec::{Dec, POS_DECIMAL_PRECISION}; use crate::storage; use crate::storage::{DbKeySeg, KeySeg}; use crate::uint::{self, Uint, I256}; +/// Abstract parameters token keys interface +pub trait Keys { + /// Key for transparent token balance + fn balance(token: &Address, owner: &Address) -> storage::Key; + + /// Returns the owner address if the given storage key is a balance key for + /// the given token. + fn is_balance<'a>( + token_addr: &Address, + key: &'a storage::Key, + ) -> Option<&'a Address>; +} + /// Amount in micro units. For different granularity another representation /// might be more appropriate. #[derive( diff --git a/crates/governance/Cargo.toml b/crates/governance/Cargo.toml index c720183a21..2a8c94f592 100644 --- a/crates/governance/Cargo.toml +++ b/crates/governance/Cargo.toml @@ -20,13 +20,16 @@ migrations = [ ] [dependencies] +namada_account = { path = "../account" } namada_core = { path = "../core" } namada_events = { path = "../events", default-features = false } -namada_macros = {path = "../macros"} +namada_macros = { path = "../macros" } namada_migrations = { path= "../migrations", optional = true } -namada_parameters = {path = "../parameters"} -namada_storage = {path = "../storage"} -namada_trans_token = {path = "../trans_token"} +namada_parameters = { path = "../parameters" } +namada_state = { path = "../state" } +namada_trans_token = { path = "../trans_token" } +namada_tx = { path = "../tx" } +namada_vp = { path = "../vp" } borsh.workspace = true itertools.workspace = true @@ -41,6 +44,13 @@ tracing.workspace = true [dev-dependencies] -namada_core = {path = "../core", default-features = false, features = ["testing"]} +namada_core = { path = "../core", default-features = false, features = ["testing"] } +namada_gas = { path = "../gas" } +namada_proof_of_stake = { path = "../proof_of_stake", features = ["testing"] } +namada_state = { path = "../state", features = ["testing"] } +namada_token = { path = "../token", features = ["testing"] } +namada_tx = { path = "../tx", features = ["testing"] } +namada_vm = { path = "../vm", features = ["testing"] } +assert_matches.workspace = true proptest.workspace = true diff --git a/crates/governance/src/lib.rs b/crates/governance/src/lib.rs index da6dde1d98..febe58ee5f 100644 --- a/crates/governance/src/lib.rs +++ b/crates/governance/src/lib.rs @@ -30,6 +30,7 @@ pub mod pgf; pub mod storage; /// Governance utility functions/structs pub mod utils; +pub mod vp; pub use storage::proposal::{InitProposalData, ProposalType, VoteProposalData}; pub use storage::vote::ProposalVote; diff --git a/crates/governance/src/parameters.rs b/crates/governance/src/parameters.rs index b22b0f27cb..77a60cc8bc 100644 --- a/crates/governance/src/parameters.rs +++ b/crates/governance/src/parameters.rs @@ -3,7 +3,7 @@ use namada_core::token; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; -use namada_storage::{Result, StorageRead, StorageWrite}; +use namada_state::{StorageRead, StorageResult, StorageWrite}; use super::storage::keys as goverance_storage; @@ -54,7 +54,7 @@ impl Default for GovernanceParameters { impl GovernanceParameters { /// Initialize governance parameters into storage - pub fn init_storage(&self, storage: &mut S) -> Result<()> + pub fn init_storage(&self, storage: &mut S) -> StorageResult<()> where S: StorageRead + StorageWrite, { diff --git a/crates/governance/src/pgf/inflation.rs b/crates/governance/src/pgf/inflation.rs index e3c2e64a66..2be31ac030 100644 --- a/crates/governance/src/pgf/inflation.rs +++ b/crates/governance/src/pgf/inflation.rs @@ -2,7 +2,7 @@ use namada_core::address::Address; use namada_parameters::storage as params_storage; -use namada_storage::{Result, StorageRead, StorageWrite}; +use namada_state::{StorageRead, StorageResult, StorageWrite}; use namada_trans_token::{credit_tokens, get_effective_total_native_supply}; use crate::pgf::storage::{ @@ -14,10 +14,10 @@ use crate::storage::proposal::{PGFIbcTarget, PGFTarget}; pub fn apply_inflation( storage: &mut S, transfer_over_ibc: F, -) -> Result<()> +) -> StorageResult<()> where S: StorageWrite + StorageRead, - F: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> Result<()>, + F: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> StorageResult<()>, { let pgf_parameters = get_parameters(storage)?; let staking_token = storage.get_native_token()?; diff --git a/crates/governance/src/pgf/parameters.rs b/crates/governance/src/pgf/parameters.rs index 533497ce12..8471c781fc 100644 --- a/crates/governance/src/pgf/parameters.rs +++ b/crates/governance/src/pgf/parameters.rs @@ -6,7 +6,7 @@ use namada_core::dec::Dec; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; -use namada_storage::{Result, StorageRead, StorageWrite}; +use namada_state::{StorageRead, StorageResult, StorageWrite}; use serde::{Deserialize, Serialize}; use super::storage::keys as pgf_storage; @@ -51,7 +51,7 @@ impl Default for PgfParameters { impl PgfParameters { /// Initialize governance parameters into storage - pub fn init_storage(&self, storage: &mut S) -> Result<()> + pub fn init_storage(&self, storage: &mut S) -> StorageResult<()> where S: StorageRead + StorageWrite, { diff --git a/crates/governance/src/pgf/storage/keys.rs b/crates/governance/src/pgf/storage/keys.rs index 846b5c95ad..1b99bfa93a 100644 --- a/crates/governance/src/pgf/storage/keys.rs +++ b/crates/governance/src/pgf/storage/keys.rs @@ -1,7 +1,7 @@ use namada_core::address::Address; use namada_core::storage::{DbKeySeg, Key, KeySeg}; use namada_macros::StorageKeys; -use namada_storage::collections::{lazy_map, LazyCollection, LazyMap}; +use namada_state::collections::{lazy_map, LazyCollection, LazyMap}; use crate::pgf::storage::steward::StewardDetail; use crate::pgf::ADDRESS; diff --git a/crates/governance/src/pgf/storage/mod.rs b/crates/governance/src/pgf/storage/mod.rs index df81e6974e..576c824aa8 100644 --- a/crates/governance/src/pgf/storage/mod.rs +++ b/crates/governance/src/pgf/storage/mod.rs @@ -8,7 +8,7 @@ pub mod steward; use namada_core::address::Address; use namada_core::collections::HashMap; use namada_core::dec::Dec; -use namada_storage::{Result, StorageRead, StorageWrite}; +use namada_state::{StorageRead, StorageResult, StorageWrite}; use crate::pgf::parameters::PgfParameters; use crate::pgf::storage::keys as pgf_keys; @@ -16,7 +16,7 @@ use crate::pgf::storage::steward::StewardDetail; use crate::storage::proposal::StoragePgfFunding; /// Query the current pgf steward set -pub fn get_stewards(storage: &S) -> Result> +pub fn get_stewards(storage: &S) -> StorageResult> where S: StorageRead, { @@ -35,7 +35,7 @@ where pub fn get_steward( storage: &S, address: &Address, -) -> Result> +) -> StorageResult> where S: StorageRead, { @@ -43,7 +43,7 @@ where } /// Check if an address is a steward -pub fn is_steward(storage: &S, address: &Address) -> Result +pub fn is_steward(storage: &S, address: &Address) -> StorageResult where S: StorageRead, { @@ -51,7 +51,10 @@ where } /// Remove a steward -pub fn remove_steward(storage: &mut S, address: &Address) -> Result<()> +pub fn remove_steward( + storage: &mut S, + address: &Address, +) -> StorageResult<()> where S: StorageRead + StorageWrite, { @@ -63,7 +66,7 @@ where /// Query the current pgf continuous payments pub fn get_continuous_pgf_payments( storage: &S, -) -> Result> +) -> StorageResult> where S: StorageRead, { @@ -79,7 +82,7 @@ where } /// Query the pgf parameters -pub fn get_parameters(storage: &S) -> Result +pub fn get_parameters(storage: &S) -> StorageResult where S: StorageRead, { @@ -106,7 +109,7 @@ pub fn update_commission( storage: &mut S, address: Address, reward_distribution: HashMap, -) -> Result<()> +) -> StorageResult<()> where S: StorageRead + StorageWrite, { diff --git a/crates/governance/src/storage/mod.rs b/crates/governance/src/storage/mod.rs index 2a2182f597..797b46d4b3 100644 --- a/crates/governance/src/storage/mod.rs +++ b/crates/governance/src/storage/mod.rs @@ -13,7 +13,9 @@ use namada_core::address::Address; use namada_core::borsh::BorshDeserialize; use namada_core::collections::HashSet; use namada_core::storage::Epoch; -use namada_storage::{iter_prefix, Error, Result, StorageRead, StorageWrite}; +use namada_state::{ + iter_prefix, StorageError, StorageRead, StorageResult, StorageWrite, +}; use namada_trans_token as token; use crate::parameters::GovernanceParameters; @@ -31,7 +33,7 @@ pub fn init_proposal( data: &InitProposalData, content: Vec, code: Option>, -) -> Result +) -> StorageResult where S: StorageRead + StorageWrite, { @@ -56,7 +58,7 @@ where governance_keys::get_proposal_code_key(proposal_id); let proposal_code = - code.ok_or(Error::new_const("Missing proposal code"))?; + code.ok_or(StorageError::new_const("Missing proposal code"))?; storage.write(&proposal_code_key, proposal_code)?; } _ => storage.write(&proposal_type_key, data.r#type.clone())?, @@ -112,7 +114,7 @@ pub fn vote_proposal( storage: &mut S, data: VoteProposalData, delegation_targets: HashSet
, -) -> Result<()> +) -> StorageResult<()> where S: StorageRead + StorageWrite, { @@ -132,7 +134,7 @@ pub fn write_proposal_result( storage: &mut S, proposal_id: u64, proposal_result: ProposalResult, -) -> Result<()> +) -> StorageResult<()> where S: StorageRead + StorageWrite, { @@ -145,7 +147,7 @@ where pub fn get_proposal_by_id( storage: &S, id: u64, -) -> Result> +) -> StorageResult> where S: StorageRead, { @@ -179,7 +181,10 @@ where } /// Query all the votes for a proposal_id -pub fn get_proposal_votes(storage: &S, proposal_id: u64) -> Result> +pub fn get_proposal_votes( + storage: &S, + proposal_id: u64, +) -> StorageResult> where S: StorageRead, { @@ -214,7 +219,10 @@ where } /// Check if an accepted proposal is being executed -pub fn is_proposal_accepted(storage: &S, tx_data: &[u8]) -> Result +pub fn is_proposal_accepted( + storage: &S, + tx_data: &[u8], +) -> StorageResult where S: StorageRead, { @@ -232,7 +240,7 @@ where pub fn get_proposal_code( storage: &S, proposal_id: u64, -) -> Result>> +) -> StorageResult>> where S: StorageRead, { @@ -244,7 +252,7 @@ where pub fn get_proposal_author( storage: &S, proposal_id: u64, -) -> Result> +) -> StorageResult> where S: StorageRead, { @@ -253,7 +261,7 @@ where } /// Get governance parameters -pub fn get_parameters(storage: &S) -> Result +pub fn get_parameters(storage: &S) -> StorageResult where S: StorageRead, { @@ -295,7 +303,7 @@ where } /// Get governance "max_proposal_period" parameter -pub fn get_max_proposal_period(storage: &S) -> Result +pub fn get_max_proposal_period(storage: &S) -> StorageResult where S: StorageRead, { @@ -309,7 +317,7 @@ where pub fn get_proposal_result( storage: &S, proposal_id: u64, -) -> Result> +) -> StorageResult> where S: StorageRead, { @@ -322,14 +330,14 @@ where pub fn load_proposals( storage: &S, current_epoch: Epoch, -) -> Result> +) -> StorageResult> where S: StorageRead, { let mut ids = BTreeSet::::new(); let proposals_key = governance_keys::get_commiting_proposals_prefix(current_epoch.0); - for key_val in namada_storage::iter_prefix_bytes(storage, &proposals_key)? { + for key_val in namada_state::iter_prefix_bytes(storage, &proposals_key)? { let (key, _) = key_val?; let activation_epoch = governance_keys::get_commit_proposal_epoch(&key) .expect("this key segment should correspond to an epoch number"); diff --git a/crates/governance/src/vp/mod.rs b/crates/governance/src/vp/mod.rs index 4336c2ca86..39f6c27bff 100644 --- a/crates/governance/src/vp/mod.rs +++ b/crates/governance/src/vp/mod.rs @@ -3,30 +3,26 @@ pub mod utils; use std::collections::BTreeSet; +use std::marker::PhantomData; use borsh::BorshDeserialize; use namada_core::arith::{self, checked}; use namada_core::booleans::{BoolResultUnitExt, ResultBoolExt}; -use namada_governance::storage::proposal::{ - AddRemove, PGFAction, ProposalType, -}; -use namada_governance::storage::{is_proposal_accepted, keys as gov_storage}; -use namada_governance::utils::is_valid_validator_voting_period; -use namada_governance::ProposalVote; -use namada_proof_of_stake::is_validator; +use namada_core::storage::Epoch; +use namada_core::{proof_of_stake, storage, token}; use namada_state::{StateRead, StorageRead}; use namada_tx::action::{Action, GovAction, Read}; use namada_tx::BatchedTxRef; -use namada_vp_env::VpEnv; +use namada_vp::native_vp::{Ctx, CtxPreStorageRead, NativeVp, VpEvaluator}; +use namada_vp::{native_vp, VpEnv}; use thiserror::Error; use self::utils::ReadType; use crate::address::{Address, InternalAddress}; -use crate::ledger::native_vp::{Ctx, NativeVp}; -use crate::ledger::{native_vp, pos}; -use crate::storage::{Epoch, Key}; -use crate::token; -use crate::vm::WasmCacheAccess; +use crate::storage::proposal::{AddRemove, PGFAction, ProposalType}; +use crate::storage::{is_proposal_accepted, keys as gov_storage}; +use crate::utils::is_valid_validator_voting_period; +use crate::ProposalVote; /// for handling Governance NativeVP errors pub type Result = std::result::Result; @@ -51,26 +47,39 @@ pub enum Error { } /// Governance VP -pub struct GovernanceVp<'a, S, CA> +pub struct GovernanceVp<'a, S, CA: 'a, EVAL, PoS, TokenKeys> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + PoS: proof_of_stake::Read>, + TokenKeys: token::Keys, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA>, + pub ctx: Ctx<'a, S, CA, EVAL>, + /// Read PoS storage + pub pos: PhantomData, + /// Token keys + pub token_keys: PhantomData, } -impl<'a, S, CA> NativeVp for GovernanceVp<'a, S, CA> +impl<'a, S, CA, EVAL, PoS, TokenKeys> NativeVp<'a> + for GovernanceVp<'a, S, CA, EVAL, PoS, TokenKeys> where S: StateRead, - CA: 'static + WasmCacheAccess, + EVAL: VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, + PoS: proof_of_stake::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = namada_state::StorageError, + >, + TokenKeys: token::Keys, { type Error = Error; fn validate_tx( - &self, + &'a self, tx_data: &BatchedTxRef<'_>, - keys_changed: &BTreeSet, + keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<()> { let (is_valid_keys_set, set_count) = @@ -145,9 +154,10 @@ where } } - keys_changed.iter().try_for_each(|key| { + // keys_changed.iter().try_for_each(|key| { + for key in keys_changed.iter() { let proposal_id = gov_storage::get_proposal_id(key); - let key_type = KeyType::from_key(key, &native_token); + let key_type = KeyType::from_key::(key, &native_token); let result = match (key_type, proposal_id) { (KeyType::VOTE, Some(proposal_id)) => { @@ -201,20 +211,35 @@ where "Key {key_type:?} rejected with error: {err:#?}." ) })?; - - Ok(()) - }) + } + Ok(()) } } -impl<'a, S, CA> GovernanceVp<'a, S, CA> +impl<'a, S, CA, EVAL, PoS, TokenKeys> + GovernanceVp<'a, S, CA, EVAL, PoS, TokenKeys> where S: StateRead, - CA: 'static + WasmCacheAccess, + EVAL: VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, + PoS: proof_of_stake::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = namada_state::StorageError, + >, + TokenKeys: token::Keys, { + /// Instantiate a Governance VP + pub fn new(ctx: Ctx<'a, S, CA, EVAL>) -> Self { + Self { + ctx, + pos: PhantomData, + token_keys: PhantomData, + } + } + fn is_valid_init_proposal_key_set( &self, - keys: &BTreeSet, + keys: &BTreeSet, ) -> Result<(bool, u64)> { let counter_key = gov_storage::get_counter_key(); let pre_counter: u64 = self.force_read(&counter_key, ReadType::Pre)?; @@ -250,9 +275,9 @@ where } fn is_valid_vote_key( - &self, + &'a self, proposal_id: u64, - key: &Key, + key: &storage::Key, verifiers: &BTreeSet
, ) -> Result<()> { let counter_key = gov_storage::get_counter_key(); @@ -368,7 +393,8 @@ where /// Validate a content key pub fn is_valid_content_key(&self, proposal_id: u64) -> Result<()> { - let content_key: Key = gov_storage::get_content_key(proposal_id); + let content_key: storage::Key = + gov_storage::get_content_key(proposal_id); let max_content_length_parameter_key = gov_storage::get_max_proposal_content_key(); @@ -858,10 +884,8 @@ where native_token_address: &Address, ) -> Result<()> { let funds_key = gov_storage::get_funds_key(proposal_id); - let balance_key = token::storage_key::balance_key( - native_token_address, - self.ctx.address, - ); + let balance_key = + TokenKeys::balance(native_token_address, self.ctx.address); let min_funds_parameter_key = gov_storage::get_min_proposal_fund_key(); let min_funds_parameter: token::Amount = @@ -925,10 +949,8 @@ where /// Validate a balance key fn is_valid_balance(&self, native_token_address: &Address) -> Result<()> { - let balance_key = token::storage_key::balance_key( - native_token_address, - self.ctx.address, - ); + let balance_key = + TokenKeys::balance(native_token_address, self.ctx.address); let min_funds_parameter_key = gov_storage::get_min_proposal_fund_key(); let pre_balance: Option = @@ -1065,26 +1087,26 @@ where /// Check if a vote is from a validator pub fn is_validator( - &self, + &'a self, verifiers: &BTreeSet
, voter: &Address, validator: &Address, - ) -> Result - where - S: StateRead, - CA: 'static + WasmCacheAccess, - { + ) -> Result { if !voter.eq(validator) { return Ok(false); } - let is_validator = is_validator(&self.ctx.pre(), voter)?; + let is_validator = PoS::is_validator(&self.ctx.pre(), voter)?; Ok(is_validator && verifiers.contains(voter)) } /// Private method to read from storage data that are 100% in storage. - fn force_read(&self, key: &Key, read_type: ReadType) -> Result + fn force_read( + &self, + key: &storage::Key, + read_type: ReadType, + ) -> Result where T: BorshDeserialize, { @@ -1123,7 +1145,7 @@ where /// Check if a vote is from a delegator pub fn is_delegator( - &self, + &'a self, epoch: Epoch, verifiers: &BTreeSet
, address: &Address, @@ -1131,11 +1153,7 @@ where ) -> Result { Ok(address != delegation_address && verifiers.contains(address) - && pos::namada_proof_of_stake::is_delegator( - &self.ctx.pre(), - address, - Some(epoch), - )?) + && PoS::is_delegator(&self.ctx.pre(), address, Some(epoch))?) } } @@ -1175,7 +1193,10 @@ enum KeyType { } impl KeyType { - fn from_key(key: &Key, native_token: &Address) -> Self { + fn from_key(key: &storage::Key, native_token: &Address) -> Self + where + TokenKeys: token::Keys, + { if gov_storage::is_vote_key(key) { Self::VOTE } else if gov_storage::is_content_key(key) { @@ -1200,9 +1221,7 @@ impl KeyType { KeyType::COUNTER } else if gov_storage::is_parameter_key(key) { KeyType::PARAMETER - } else if token::storage_key::is_balance_key(native_token, key) - .is_some() - { + } else if TokenKeys::is_balance(native_token, key).is_some() { KeyType::BALANCE } else if gov_storage::is_governance_key(key) { KeyType::UNKNOWN_GOVERNANCE @@ -1218,47 +1237,61 @@ mod test { use std::cell::RefCell; use std::collections::BTreeSet; - use borsh_ext::BorshSerializeExt; - use namada_gas::{TxGasMeter, VpGasMeter}; - use namada_governance::storage::keys::{ - get_activation_epoch_key, get_author_key, get_committing_proposals_key, - get_content_key, get_counter_key, get_funds_key, get_proposal_type_key, - get_vote_proposal_key, get_voting_end_epoch_key, - get_voting_start_epoch_key, - }; - use namada_governance::{ProposalType, ProposalVote, ADDRESS}; - use namada_proof_of_stake::bond_tokens; - use namada_sdk::address::testing::{ + use assert_matches::assert_matches; + use namada_core::address::testing::{ established_address_1, established_address_3, nam, }; - use namada_sdk::key::testing::keypair_1; - use namada_sdk::key::RefTo; - use namada_sdk::time::DateTimeUtc; - use namada_sdk::token; + use namada_core::address::Address; + use namada_core::borsh::BorshSerializeExt; + use namada_core::key::testing::keypair_1; + use namada_core::key::RefTo; + use namada_core::storage::testing::get_dummy_header; + use namada_core::time::DateTimeUtc; + use namada_core::{token, WasmCacheRwAccess}; + use namada_gas::{TxGasMeter, VpGasMeter}; + use namada_parameters::Parameters; + use namada_proof_of_stake::bond_tokens; + use namada_proof_of_stake::test_utils::get_dummy_genesis_validator; use namada_state::mockdb::MockDB; use namada_state::testing::TestState; use namada_state::{ BlockHeight, Epoch, FullAccessState, Key, Sha256Hasher, State, - StorageRead, TxIndex, + StateRead, StorageRead, TxIndex, }; use namada_token::storage_key::balance_key; use namada_tx::action::{Action, GovAction, Write}; use namada_tx::data::TxType; use namada_tx::{Authorization, Code, Data, Section, Tx}; + use namada_vm::wasm; + use namada_vm::wasm::run::VpEvalWasm; + use namada_vm::wasm::VpCache; + use namada_vp::native_vp::{Ctx, CtxPreStorageRead, NativeVp}; - use crate::core::address::Address; - use crate::ledger::governance::GovernanceVp; - use crate::ledger::native_vp::ibc::{ - get_dummy_genesis_validator, get_dummy_header, + use crate::storage::keys::{ + get_activation_epoch_key, get_author_key, get_committing_proposals_key, + get_content_key, get_counter_key, get_funds_key, get_proposal_type_key, + get_vote_proposal_key, get_voting_end_epoch_key, + get_voting_start_epoch_key, }; - use crate::ledger::native_vp::{Ctx, NativeVp}; - use crate::ledger::pos; - use crate::vm::wasm; + use crate::{ProposalType, ProposalVote, ADDRESS}; + + type CA = WasmCacheRwAccess; + type Eval = VpEvalWasm<::D, ::H, CA>; + type GovernanceVp<'a, S> = super::GovernanceVp< + 'a, + S, + VpCache, + Eval, + namada_proof_of_stake::Store< + CtxPreStorageRead<'a, 'a, S, VpCache, Eval>, + >, + namada_token::Store<()>, + >; fn init_storage() -> TestState { let mut state = TestState::default(); - pos::test_utils::test_init_genesis( + namada_proof_of_stake::test_utils::test_init_genesis( &mut state, namada_proof_of_stake::OwnedPosParams::default(), vec![get_dummy_genesis_validator()].into_iter(), @@ -1284,7 +1317,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -1318,7 +1351,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -1348,6 +1381,7 @@ mod test { height: BlockHeight, ) { state.in_mem_mut().update_epoch_blocks_delay = Some(1); + let parameters = Parameters::default(); for _ in 0..total_epochs { state.in_mem_mut().update_epoch_blocks_delay = Some(1); state @@ -1360,6 +1394,7 @@ mod test { .next_second() .next_second() .next_second(), + ¶meters, ) .unwrap(); } @@ -1518,7 +1553,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -1575,7 +1610,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -1614,7 +1649,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -1671,10 +1706,9 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); let result = - governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers); - // this should fail + governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers); // this should fail assert_matches!(&result, Err(_)); if result.is_err() { @@ -1713,7 +1747,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -1770,7 +1804,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); let result = governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers); assert_matches!(&result, Ok(_)); @@ -1811,7 +1845,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -1868,7 +1902,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -1889,7 +1923,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -1946,7 +1980,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -1967,7 +2001,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2024,7 +2058,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2063,7 +2097,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2120,7 +2154,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2159,7 +2193,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2216,7 +2250,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2237,7 +2271,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2294,7 +2328,7 @@ mod test { vp_wasm_cache.clone(), ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2345,7 +2379,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2366,7 +2400,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2423,7 +2457,7 @@ mod test { vp_wasm_cache.clone(), ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2474,7 +2508,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2495,7 +2529,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2552,7 +2586,7 @@ mod test { vp_wasm_cache.clone(), ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); // this should return true because state has been stored assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2603,7 +2637,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2624,7 +2658,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2681,7 +2715,7 @@ mod test { vp_wasm_cache.clone(), ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2749,7 +2783,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2770,7 +2804,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2827,7 +2861,7 @@ mod test { vp_wasm_cache.clone(), ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2895,7 +2929,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -2916,7 +2950,7 @@ mod test { &TxGasMeter::new(u64::MAX), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let tx_index = TxIndex::default(); @@ -2973,7 +3007,7 @@ mod test { vp_wasm_cache.clone(), ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -3041,7 +3075,7 @@ mod test { vp_wasm_cache, ); - let governance_vp = GovernanceVp { ctx }; + let governance_vp = GovernanceVp::new(ctx); assert_matches!( governance_vp.validate_tx(&batched_tx, &keys_changed, &verifiers), diff --git a/crates/namada/src/ledger/native_vp/ibc/mod.rs b/crates/namada/src/ledger/native_vp/ibc/mod.rs index a1e331beda..3373f0dbf0 100644 --- a/crates/namada/src/ledger/native_vp/ibc/mod.rs +++ b/crates/namada/src/ledger/native_vp/ibc/mod.rs @@ -351,68 +351,10 @@ fn match_value( } } -/// A dummy header used for testing -#[cfg(any(test, feature = "testing", feature = "benches"))] -pub fn get_dummy_header() -> crate::storage::Header { - use namada_sdk::time::{DateTimeUtc, DurationSecs}; - crate::storage::Header { - hash: crate::hash::Hash([0; 32]), - #[allow(clippy::disallowed_methods, clippy::arithmetic_side_effects)] - time: DateTimeUtc::now() + DurationSecs(5), - next_validators_hash: crate::hash::Hash([0; 32]), - } -} - -/// A dummy validator used for testing -#[cfg(any(test, feature = "testing"))] -pub fn get_dummy_genesis_validator() --> namada_proof_of_stake::types::GenesisValidator { - use crate::core::address::testing::established_address_1; - use crate::core::dec::Dec; - use crate::core::key::testing::common_sk_from_simple_seed; - use crate::key; - - let address = established_address_1(); - let tokens = Amount::native_whole(1); - let consensus_sk = common_sk_from_simple_seed(0); - let consensus_key = consensus_sk.to_public(); - - let protocol_sk = common_sk_from_simple_seed(1); - let protocol_key = protocol_sk.to_public(); - - let commission_rate = - Dec::new(1, 1).expect("expected 0.1 to be a valid decimal"); - let max_commission_rate_change = - Dec::new(1, 1).expect("expected 0.1 to be a valid decimal"); - - let eth_hot_sk = - key::common::SecretKey::Secp256k1(key::testing::gen_keypair::< - key::secp256k1::SigScheme, - >()); - let eth_hot_key = eth_hot_sk.to_public(); - - let eth_cold_sk = - key::common::SecretKey::Secp256k1(key::testing::gen_keypair::< - key::secp256k1::SigScheme, - >()); - let eth_cold_key = eth_cold_sk.to_public(); - - namada_proof_of_stake::types::GenesisValidator { - address, - tokens, - consensus_key, - protocol_key, - eth_cold_key, - eth_hot_key, - commission_rate, - max_commission_rate_change, - metadata: Default::default(), - } -} - #[allow(clippy::arithmetic_side_effects)] #[cfg(test)] mod tests { + use std::str::FromStr; use borsh::BorshDeserialize; @@ -423,9 +365,11 @@ mod tests { use ibc_testkit::testapp::ibc::clients::mock::consensus_state::MockConsensusState; use ibc_testkit::testapp::ibc::clients::mock::header::MockHeader; use namada_core::address::InternalAddress; + use namada_core::storage::testing::get_dummy_header; use namada_gas::TxGasMeter; use namada_governance::parameters::GovernanceParameters; use namada_ibc::event::IbcEventType; + use namada_proof_of_stake::test_utils::get_dummy_genesis_validator; use namada_state::testing::TestState; use namada_state::StorageRead; use namada_tx::data::TxType; diff --git a/crates/node/src/storage/mod.rs b/crates/node/src/storage/mod.rs index 057337a3f2..72036efa1e 100644 --- a/crates/node/src/storage/mod.rs +++ b/crates/node/src/storage/mod.rs @@ -160,24 +160,7 @@ mod tests { let value: u64 = 1; let value_bytes = encode(&value); // initialize parameter storage - let params = Parameters { - max_tx_bytes: 1024 * 1024, - epoch_duration: EpochDuration { - min_num_of_blocks: 1, - min_duration: DurationSecs(3600), - }, - max_proposal_bytes: Default::default(), - max_block_gas: 100, - vp_allowlist: vec![], - tx_allowlist: vec![], - implicit_vp_code_hash: Default::default(), - epochs_per_year: 365, - masp_epoch_multiplier: 2, - masp_fee_payment_gas_limit: 0, - gas_scale: 10_000_000, - minimum_gas_price: Default::default(), - is_native_token_transferable: true, - }; + let params = Parameters::default(); parameters::init_storage(¶ms, &mut state).expect("Test failed"); // insert and commit state.db_write(&key, &value_bytes).expect("write failed"); diff --git a/crates/proof_of_stake/src/lib.rs b/crates/proof_of_stake/src/lib.rs index fb2cfc93c3..7a53aabc4c 100644 --- a/crates/proof_of_stake/src/lib.rs +++ b/crates/proof_of_stake/src/lib.rs @@ -37,6 +37,7 @@ mod tests; use core::fmt::Debug; use std::cmp; use std::collections::{BTreeMap, BTreeSet}; +use std::marker::PhantomData; use epoched::EpochOffset; pub use error::*; @@ -105,6 +106,29 @@ use crate::validator_set_update::{ update_validator_set, }; +/// PoS storage `Keys/Read/Write` implementation +#[derive(Debug)] +pub struct Store(PhantomData); + +impl namada_core::proof_of_stake::Read for Store +where + S: StorageRead, +{ + type Err = namada_storage::Error; + + fn is_validator(storage: &S, address: &Address) -> Result { + is_validator(storage, address) + } + + fn is_delegator( + storage: &S, + address: &Address, + epoch: Option, + ) -> Result { + is_delegator(storage, address, epoch) + } +} + /// Address of the PoS account implemented as a native VP pub const ADDRESS: Address = Address::Internal(InternalAddress::PoS); @@ -2733,6 +2757,51 @@ pub mod test_utils { init_genesis_helper(storage, ¶ms, validators, current_epoch)?; Ok(params) } + + /// A dummy validator used for testing + pub fn get_dummy_genesis_validator() -> types::GenesisValidator { + use namada_core::address::testing::established_address_1; + use namada_core::dec::Dec; + use namada_core::key::testing::common_sk_from_simple_seed; + use namada_core::{key, token}; + + let address = established_address_1(); + let tokens = token::Amount::native_whole(1); + let consensus_sk = common_sk_from_simple_seed(0); + let consensus_key = consensus_sk.to_public(); + + let protocol_sk = common_sk_from_simple_seed(1); + let protocol_key = protocol_sk.to_public(); + + let commission_rate = + Dec::new(1, 1).expect("expected 0.1 to be a valid decimal"); + let max_commission_rate_change = + Dec::new(1, 1).expect("expected 0.1 to be a valid decimal"); + + let eth_hot_sk = + key::common::SecretKey::Secp256k1(key::testing::gen_keypair::< + key::secp256k1::SigScheme, + >()); + let eth_hot_key = eth_hot_sk.to_public(); + + let eth_cold_sk = + key::common::SecretKey::Secp256k1(key::testing::gen_keypair::< + key::secp256k1::SigScheme, + >()); + let eth_cold_key = eth_cold_sk.to_public(); + + types::GenesisValidator { + address, + tokens, + consensus_key, + protocol_key, + eth_cold_key, + eth_hot_key, + commission_rate, + max_commission_rate_change, + metadata: Default::default(), + } + } } /// Change validator's metadata. In addition to changing any of the data from diff --git a/crates/state/src/write_log.rs b/crates/state/src/write_log.rs index 48ee58afa3..ffa76ba892 100644 --- a/crates/state/src/write_log.rs +++ b/crates/state/src/write_log.rs @@ -741,7 +741,7 @@ impl WriteLog { #[cfg(test)] mod tests { use assert_matches::assert_matches; - use namada_core::{address, parameters}; + use namada_core::address; use pretty_assertions::assert_eq; use proptest::prelude::*; diff --git a/crates/tests/src/vm_host_env/ibc.rs b/crates/tests/src/vm_host_env/ibc.rs index 23acc4f2e2..5b36c96ada 100644 --- a/crates/tests/src/vm_host_env/ibc.rs +++ b/crates/tests/src/vm_host_env/ibc.rs @@ -63,9 +63,7 @@ pub use namada::ledger::ibc::storage::{ next_sequence_send_key, port_key, receipt_key, }; pub use namada::ledger::ibc::trace::ibc_token; -use namada::ledger::native_vp::ibc::{ - get_dummy_genesis_validator, get_dummy_header as tm_dummy_header, Ibc, -}; +use namada::ledger::native_vp::ibc::Ibc; use namada::ledger::native_vp::multitoken::{ Error as MultitokenVpError, MultitokenVp, }; @@ -81,6 +79,8 @@ use namada::token::{self, Amount}; use namada::tx::BatchedTxRef; use namada::vm::{wasm, WasmCacheRwAccess}; use namada_core::collections::HashMap; +use namada_core::storage::testing::get_dummy_header; +use namada_proof_of_stake::test_utils::get_dummy_genesis_validator; use namada_sdk::state::StateRead; use namada_test_utils::TestWasms; use namada_tx_prelude::BorshSerializeExt; @@ -232,7 +232,7 @@ pub fn init_storage() -> (Address, Address) { // block header to check timeout timestamp env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); env.state.in_mem_mut().begin_block(BlockHeight(1)).unwrap(); }); @@ -277,7 +277,7 @@ pub fn init_storage() -> (Address, Address) { // block header to check timeout timestamp env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); }); diff --git a/crates/tests/src/vm_host_env/mod.rs b/crates/tests/src/vm_host_env/mod.rs index 8f7740630b..921d516652 100644 --- a/crates/tests/src/vm_host_env/mod.rs +++ b/crates/tests/src/vm_host_env/mod.rs @@ -33,13 +33,14 @@ mod tests { use namada::ibc::context::transfer_mod::testing::DummyTransferModule; use namada::ibc::primitives::ToProto; use namada::ibc::Error as IbcActionError; - use namada::ledger::ibc::{storage as ibc_storage, trace as ibc_trace}; - use namada::ledger::native_vp::ibc::{ - get_dummy_header as tm_dummy_header, Error as IbcError, + use namada::ledger::ibc::{ + storage as ibc_storage, storage as ibc_storage, trace as ibc_trace, }; + use namada::ledger::native_vp::ibc::Error as IbcError; use namada::ledger::tx_env::TxEnv; use namada::token::{self, Amount}; use namada::tx::Tx; + use namada_core::storage::testing::get_dummy_header; use namada_test_utils::TestWasms; use namada_tx_prelude::address::InternalAddress; use namada_tx_prelude::chain::ChainId; @@ -714,7 +715,7 @@ mod tests { env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); // Start a transaction to update the client @@ -790,7 +791,7 @@ mod tests { env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); tx_host_env::set(env); @@ -867,7 +868,7 @@ mod tests { env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); tx_host_env::set(env); @@ -946,7 +947,7 @@ mod tests { env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); tx_host_env::set(env); @@ -1025,7 +1026,7 @@ mod tests { env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); tx_host_env::set(env); @@ -1228,7 +1229,7 @@ mod tests { env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); tx_host_env::set(env); @@ -1789,7 +1790,7 @@ mod tests { env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); tx_host_env::set(env); @@ -1875,7 +1876,7 @@ mod tests { env.state.in_mem_mut().begin_block(BlockHeight(2)).unwrap(); env.state .in_mem_mut() - .set_header(tm_dummy_header()) + .set_header(get_dummy_header()) .unwrap(); tx_host_env::set(env); diff --git a/crates/trans_token/src/lib.rs b/crates/trans_token/src/lib.rs index b095bb547f..887e2ccfb1 100644 --- a/crates/trans_token/src/lib.rs +++ b/crates/trans_token/src/lib.rs @@ -21,5 +21,25 @@ pub mod event; mod storage; pub mod storage_key; +use std::marker::PhantomData; + +use namada_core::address::Address; pub use namada_core::token::*; pub use storage::*; + +/// Transparent token storage `Keys/Read/Write` implementation +#[derive(Debug)] +pub struct Store(PhantomData); + +impl Keys for Store { + fn balance(token: &Address, owner: &Address) -> namada_core::storage::Key { + storage_key::balance_key(token, owner) + } + + fn is_balance<'a>( + token_addr: &Address, + key: &'a namada_core::storage::Key, + ) -> Option<&'a Address> { + storage_key::is_balance_key(token_addr, key) + } +} diff --git a/crates/vm/src/host_env.rs b/crates/vm/src/host_env.rs index e4f235d853..2173962414 100644 --- a/crates/vm/src/host_env.rs +++ b/crates/vm/src/host_env.rs @@ -12,11 +12,13 @@ use namada_core::borsh::{BorshDeserialize, BorshSerializeExt}; use namada_core::hash::Hash; use namada_core::internal::{HostEnvResult, KeyVal}; use namada_core::storage::{BlockHeight, Key, TxIndex, TX_INDEX_LENGTH}; +use namada_core::WasmCacheAccess; use namada_events::{Event, EventTypeBuilder}; use namada_gas::{ self as gas, GasMetering, TxGasMeter, VpGasMeter, MEMORY_ACCESS_GAS_PER_BYTE, }; +use namada_state::prefix_iter::{PrefixIteratorId, PrefixIterators}; use namada_state::write_log::{self, WriteLog}; use namada_state::{ DBIter, InMemory, OptionExt, ResultExt, State, StateRead, StorageError, @@ -33,13 +35,8 @@ use namada_tx::{BatchedTx, BatchedTxRef, Tx, TxCommitments}; use namada_vp::vp_host_fns; use thiserror::Error; -#[cfg(feature = "wasm-runtime")] -use super::wasm::TxCache; -#[cfg(feature = "wasm-runtime")] -use super::wasm::VpCache; -use super::WasmCacheAccess; +use super::wasm::{TxCache, VpCache}; use crate::memory::VmMemory; -use crate::prefix_iter::{PrefixIteratorId, PrefixIterators}; use crate::{HostRef, RoAccess, RoHostRef, RwAccess, RwHostRef}; /// These runtime errors will abort tx WASM execution immediately @@ -134,14 +131,9 @@ where pub yielded_value: HostRef>>, /// VP WASM compilation cache (this is available in tx context, because /// we're pre-compiling VPs from [`tx_init_account`]) - #[cfg(feature = "wasm-runtime")] pub vp_wasm_cache: HostRef>, /// Tx WASM compilation cache - #[cfg(feature = "wasm-runtime")] pub tx_wasm_cache: HostRef>, - /// To avoid unused parameter without "wasm-runtime" feature - #[cfg(not(feature = "wasm-runtime"))] - pub cache_access: std::marker::PhantomData, } impl TxVmEnv @@ -172,8 +164,8 @@ where verifiers: &mut BTreeSet
, result_buffer: &mut Option>, yielded_value: &mut Option>, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, - #[cfg(feature = "wasm-runtime")] tx_wasm_cache: &mut TxCache, + vp_wasm_cache: &mut VpCache, + tx_wasm_cache: &mut TxCache, ) -> Self { let write_log = unsafe { RwHostRef::new(write_log) }; let in_mem = unsafe { RoHostRef::new(in_mem) }; @@ -187,9 +179,7 @@ where let verifiers = unsafe { RwHostRef::new(verifiers) }; let result_buffer = unsafe { RwHostRef::new(result_buffer) }; let yielded_value = unsafe { RwHostRef::new(yielded_value) }; - #[cfg(feature = "wasm-runtime")] let vp_wasm_cache = unsafe { RwHostRef::new(vp_wasm_cache) }; - #[cfg(feature = "wasm-runtime")] let tx_wasm_cache = unsafe { RwHostRef::new(tx_wasm_cache) }; let ctx = TxCtx { write_log, @@ -204,12 +194,8 @@ where verifiers, result_buffer, yielded_value, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache, - #[cfg(feature = "wasm-runtime")] tx_wasm_cache, - #[cfg(not(feature = "wasm-runtime"))] - cache_access: std::marker::PhantomData, }; Self { memory, ctx } @@ -288,12 +274,8 @@ where verifiers: self.verifiers, result_buffer: self.result_buffer, yielded_value: self.yielded_value, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache: self.vp_wasm_cache, - #[cfg(feature = "wasm-runtime")] tx_wasm_cache: self.tx_wasm_cache, - #[cfg(not(feature = "wasm-runtime"))] - cache_access: std::marker::PhantomData, } } } @@ -351,11 +333,7 @@ where /// calls to `eval`. pub verifiers: HostRef>, /// VP WASM compilation cache - #[cfg(feature = "wasm-runtime")] pub vp_wasm_cache: HostRef>, - /// To avoid unused parameter without "wasm-runtime" feature - #[cfg(not(feature = "wasm-runtime"))] - pub cache_access: std::marker::PhantomData, } /// A Validity predicate runner for calls from the [`vp_eval`] function. @@ -413,7 +391,7 @@ where yielded_value: &mut Option>, keys_changed: &BTreeSet, eval_runner: &EVAL, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, + vp_wasm_cache: &mut VpCache, ) -> Self { let ctx = VpCtx::new( address, @@ -430,7 +408,6 @@ where yielded_value, keys_changed, eval_runner, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache, ); @@ -489,7 +466,7 @@ where yielded_value: &mut Option>, keys_changed: &BTreeSet, eval_runner: &EVAL, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, + vp_wasm_cache: &mut VpCache, ) -> Self { let address = unsafe { RoHostRef::new(address) }; let write_log = unsafe { RoHostRef::new(write_log) }; @@ -505,7 +482,6 @@ where let yielded_value = unsafe { RwHostRef::new(yielded_value) }; let keys_changed = unsafe { RoHostRef::new(keys_changed) }; let eval_runner = unsafe { RoHostRef::new(eval_runner) }; - #[cfg(feature = "wasm-runtime")] let vp_wasm_cache = unsafe { RwHostRef::new(vp_wasm_cache) }; Self { address, @@ -522,10 +498,7 @@ where yielded_value, keys_changed, verifiers, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache, - #[cfg(not(feature = "wasm-runtime"))] - cache_access: std::marker::PhantomData, } } @@ -573,10 +546,7 @@ where yielded_value: self.yielded_value, keys_changed: self.keys_changed, verifiers: self.verifiers, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache: self.vp_wasm_cache, - #[cfg(not(feature = "wasm-runtime"))] - cache_access: std::marker::PhantomData, } } } @@ -2376,8 +2346,8 @@ pub mod testing { tx_index: &TxIndex, result_buffer: &mut Option>, yielded_value: &mut Option>, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, - #[cfg(feature = "wasm-runtime")] tx_wasm_cache: &mut TxCache, + vp_wasm_cache: &mut VpCache, + tx_wasm_cache: &mut TxCache, ) -> TxVmEnv::D, ::H, CA> where S: State, @@ -2398,9 +2368,7 @@ pub mod testing { verifiers, result_buffer, yielded_value, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache, - #[cfg(feature = "wasm-runtime")] tx_wasm_cache, ) } @@ -2419,8 +2387,8 @@ pub mod testing { result_buffer: &mut Option>, yielded_value: &mut Option>, store: Rc>, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, - #[cfg(feature = "wasm-runtime")] tx_wasm_cache: &mut TxCache, + vp_wasm_cache: &mut VpCache, + tx_wasm_cache: &mut TxCache, ) -> TxVmEnv::D, ::H, CA> where S: State, @@ -2447,9 +2415,7 @@ pub mod testing { verifiers, result_buffer, yielded_value, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache, - #[cfg(feature = "wasm-runtime")] tx_wasm_cache, ); @@ -2472,7 +2438,7 @@ pub mod testing { yielded_value: &mut Option>, keys_changed: &BTreeSet, eval_runner: &EVAL, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, + vp_wasm_cache: &mut VpCache, ) -> VpVmEnv::D, ::H, EVAL, CA> where S: StateRead, @@ -2495,7 +2461,6 @@ pub mod testing { yielded_value, keys_changed, eval_runner, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache, ) } diff --git a/crates/vm/src/lib.rs b/crates/vm/src/lib.rs index 802b659146..77c0d1329c 100644 --- a/crates/vm/src/lib.rs +++ b/crates/vm/src/lib.rs @@ -7,7 +7,6 @@ use wasmparser::{Validator, WasmFeatures}; pub mod host_env; pub mod memory; -pub mod prefix_iter; pub mod types; #[cfg(feature = "wasm-runtime")] pub mod wasm; diff --git a/crates/vm/src/wasm/compilation_cache/common.rs b/crates/vm/src/wasm/compilation_cache/common.rs index 90a6201cd5..aae2814b44 100644 --- a/crates/vm/src/wasm/compilation_cache/common.rs +++ b/crates/vm/src/wasm/compilation_cache/common.rs @@ -15,13 +15,13 @@ use std::time::Duration; use clru::{CLruCache, CLruCacheConfig, WeightScale}; use namada_core::collections::HashMap; use namada_core::hash::Hash; +use namada_core::{WasmCacheAccess, WasmCacheRoAccess}; use namada_sdk::control_flow::time::{ExponentialBackoff, SleepStrategy}; use wasmer::{Module, Store}; use wasmer_cache::{FileSystemCache, Hash as CacheHash}; use crate::wasm::run::untrusted_wasm_store; use crate::wasm::{self, memory}; -use crate::{WasmCacheAccess, WasmCacheRoAccess}; /// Cache handle. Thread-safe. #[derive(Debug, Clone)] @@ -601,19 +601,29 @@ mod universal { /// Testing helpers #[cfg(any(test, feature = "testing"))] pub mod testing { + use namada_core::WasmCacheRwAccess; use tempfile::{tempdir, TempDir}; use super::*; - use crate::WasmCacheRwAccess; + use crate::wasm::{TxCache, VpCache}; /// Instantiate the default wasmer store. pub fn store() -> Store { super::store() } - /// Cache with a temp dir for testing - pub fn cache() -> (Cache, TempDir) - { + /// VP Cache with a temp dir for testing + pub fn vp_cache() -> (VpCache, TempDir) { + cache::() + } + + /// Tx Cache with a temp dir for testing + pub fn tx_cache() -> (TxCache, TempDir) { + cache::() + } + + /// Generic Cache with a temp dir for testing + pub fn cache() -> (Cache, TempDir) { let dir = tempdir().unwrap(); let cache = Cache::new( dir.path(), @@ -630,12 +640,12 @@ mod test { use assert_matches::assert_matches; use byte_unit::Byte; + use namada_core::WasmCacheRwAccess; use namada_test_utils::TestWasms; use tempfile::{tempdir, TempDir}; use test_log::test; use super::*; - use crate::WasmCacheRwAccess; #[test] fn test_fetch_or_compile_valid_wasm() { diff --git a/crates/vm/src/wasm/host_env.rs b/crates/vm/src/wasm/host_env.rs index b2d62ef103..cd4aaa799c 100644 --- a/crates/vm/src/wasm/host_env.rs +++ b/crates/vm/src/wasm/host_env.rs @@ -3,12 +3,13 @@ //! Here, we expose the host functions into wasm's //! imports, so they can be called from inside the wasm. +use namada_core::WasmCacheAccess; use namada_state::{DBIter, StorageHasher, DB}; use wasmer::{Function, FunctionEnv, Imports}; +use crate::host_env; use crate::host_env::{TxVmEnv, VpEvaluator, VpVmEnv}; use crate::wasm::memory::WasmMemory; -use crate::{host_env, WasmCacheAccess}; /// Prepare imports (memory and host functions) exposed to the vm guest running /// transaction code @@ -113,12 +114,12 @@ mod wrap_tx { #![allow(missing_docs)] + use namada_core::WasmCacheAccess; use namada_state::{DBIter, StorageHasher, DB}; use wasmer::FunctionEnvMut; use crate::host_env::TxVmEnv; use crate::wasm::memory::WasmMemory; - use crate::WasmCacheAccess; pub(super) fn _0( f: F, @@ -294,12 +295,12 @@ mod wrap_vp { #![allow(missing_docs)] + use namada_core::WasmCacheAccess; use namada_state::{DBIter, StorageHasher, DB}; use wasmer::FunctionEnvMut; use crate::host_env::{VpEvaluator, VpVmEnv}; use crate::wasm::memory::WasmMemory; - use crate::WasmCacheAccess; pub(super) fn _0( f: F, diff --git a/crates/vm/src/wasm/run.rs b/crates/vm/src/wasm/run.rs index 86ee3d50f9..385467bb4b 100644 --- a/crates/vm/src/wasm/run.rs +++ b/crates/vm/src/wasm/run.rs @@ -14,7 +14,9 @@ use namada_core::hash::{Error as TxHashError, Hash}; use namada_core::internal::HostEnvResult; use namada_core::storage::{Key, TxIndex}; use namada_core::validity_predicate::VpError; +use namada_core::WasmCacheAccess; use namada_gas::{GasMetering, TxGasMeter, VpGasMeter, WASM_MEMORY_PAGE_GAS}; +use namada_state::prefix_iter::PrefixIterators; use namada_state::{DBIter, State, StateRead, StorageHasher, StorageRead, DB}; use namada_tx::data::{TxSentinel, TxType}; use namada_tx::{BatchedTxRef, Commitment, Section, Tx, TxCommitments}; @@ -28,14 +30,10 @@ use wasmer::{Engine, Module, NativeEngineExt, Store, Target}; use super::memory::{Limit, WasmMemory}; use super::TxCache; use crate::host_env::{TxVmEnv, VpCtx, VpEvaluator, VpVmEnv}; -use crate::prefix_iter::PrefixIterators; use crate::types::VpInput; use crate::wasm::host_env::{tx_imports, vp_imports}; use crate::wasm::{memory, Cache, CacheName, VpCache}; -use crate::{ - validate_untrusted_wasm, HostRef, RwAccess, WasmCacheAccess, - WasmValidationError, -}; +use crate::{validate_untrusted_wasm, HostRef, RwAccess, WasmValidationError}; const TX_ENTRYPOINT: &str = "_apply_tx"; const VP_ENTRYPOINT: &str = "_validate_tx"; @@ -524,6 +522,65 @@ where pub cache_access: PhantomData<*const CA>, } +impl<'a, S, CA> namada_vp::native_vp::VpEvaluator<'a, S, VpCache, Self> + for VpEvalWasm<::D, ::H, CA> +where + S: 'static + StateRead, + CA: WasmCacheAccess, +{ + fn eval( + ctx: &namada_vp::native_vp::Ctx<'a, S, VpCache, Self>, + vp_code_hash: Hash, + input_data: BatchedTxRef<'_>, + ) -> namada_state::StorageResult<()> { + use std::marker::PhantomData; + + use namada_state::ResultExt; + + use crate::host_env::VpCtx; + use crate::wasm::run::VpEvalWasm; + + let eval_runner = + VpEvalWasm::<::D, ::H, CA> { + db: PhantomData, + hasher: PhantomData, + cache_access: PhantomData, + }; + let mut iterators: PrefixIterators<'_, ::D> = + PrefixIterators::default(); + let mut result_buffer: Option> = None; + let mut yielded_value: Option> = None; + let mut vp_wasm_cache = ctx.vp_wasm_cache.clone(); + + let ctx = VpCtx::new( + ctx.address, + ctx.state.write_log(), + ctx.state.in_mem(), + ctx.state.db(), + ctx.gas_meter, + ctx.tx, + ctx.cmt, + ctx.tx_index, + &mut iterators, + ctx.verifiers, + &mut result_buffer, + &mut yielded_value, + ctx.keys_changed, + &eval_runner, + &mut vp_wasm_cache, + ); + eval_runner + .eval_native_result(ctx, vp_code_hash, input_data) + .inspect_err(|err| { + tracing::warn!( + "VP eval from a native VP failed with: + {err}", + ); + }) + .into_storage_result() + } +} + impl VpEvaluator for VpEvalWasm where D: DB + for<'iter> DBIter<'iter> + 'static, @@ -1120,9 +1177,9 @@ mod tests { // shouldn't fail let tx_data = 2_usize.pow(23).serialize_to_vec(); let (mut vp_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let (mut tx_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::tx_cache(); let mut outer_tx = Tx::from_type(TxType::Raw); outer_tx.set_code(Code::new(tx_code.clone(), None)); outer_tx.set_data(Data::new(tx_data)); @@ -1210,7 +1267,8 @@ mod tests { let mut outer_tx = Tx::new(state.in_mem().chain_id.clone(), None); outer_tx.add_code(vec![], None).add_data(eval_vp); - let (vp_cache, _) = wasm::compilation_cache::common::testing::cache(); + let (vp_cache, _) = + wasm::compilation_cache::common::testing::vp_cache(); // When the `eval`ed VP doesn't run out of memory, it should return // `true` assert!( @@ -1294,7 +1352,8 @@ mod tests { outer_tx.header.chain_id = state.in_mem().chain_id.clone(); outer_tx.set_data(Data::new(tx_data)); outer_tx.set_code(Code::new(vec![], None)); - let (vp_cache, _) = wasm::compilation_cache::common::testing::cache(); + let (vp_cache, _) = + wasm::compilation_cache::common::testing::vp_cache(); let result = vp( code_hash, &outer_tx.batch_ref_first_tx().unwrap(), @@ -1358,9 +1417,9 @@ mod tests { let len = 2_usize.pow(24); let tx_data: Vec = vec![6_u8; len]; let (mut vp_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let (mut tx_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::tx_cache(); let mut outer_tx = Tx::from_type(TxType::Raw); outer_tx.set_code(Code::new(tx_no_op, None)); outer_tx.set_data(Data::new(tx_data)); @@ -1426,7 +1485,8 @@ mod tests { outer_tx.header.chain_id = state.in_mem().chain_id.clone(); outer_tx.set_data(Data::new(tx_data)); outer_tx.set_code(Code::new(vec![], None)); - let (vp_cache, _) = wasm::compilation_cache::common::testing::cache(); + let (vp_cache, _) = + wasm::compilation_cache::common::testing::vp_cache(); let result = vp( code_hash, &outer_tx.batch_ref_first_tx().unwrap(), @@ -1493,9 +1553,9 @@ mod tests { state.write(&key, value).unwrap(); let tx_data = key.serialize_to_vec(); let (mut vp_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let (mut tx_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::tx_cache(); let mut outer_tx = Tx::from_type(TxType::Raw); outer_tx.set_code(Code::new(tx_read_key, None)); outer_tx.set_data(Data::new(tx_data)); @@ -1553,7 +1613,8 @@ mod tests { outer_tx.header.chain_id = state.in_mem().chain_id.clone(); outer_tx.set_data(Data::new(tx_data)); outer_tx.set_code(Code::new(vec![], None)); - let (vp_cache, _) = wasm::compilation_cache::common::testing::cache(); + let (vp_cache, _) = + wasm::compilation_cache::common::testing::vp_cache(); let error = vp( code_hash, &outer_tx.batch_ref_first_tx().unwrap(), @@ -1628,7 +1689,8 @@ mod tests { let mut outer_tx = Tx::new(state.in_mem().chain_id.clone(), None); outer_tx.add_code(vec![], None).add_data(eval_vp); - let (vp_cache, _) = wasm::compilation_cache::common::testing::cache(); + let (vp_cache, _) = + wasm::compilation_cache::common::testing::vp_cache(); assert!( vp( code_hash, @@ -1733,9 +1795,9 @@ mod tests { state.write_log_mut().write(&len_key, code_len).unwrap(); let (mut vp_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let (mut tx_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::tx_cache(); let mut outer_tx = Tx::from_type(TxType::Raw); outer_tx.set_code(Code::new(tx_code.clone(), None)); outer_tx.set_data(Data::new(vec![])); @@ -1772,9 +1834,9 @@ mod tests { state.write_log_mut().write(&len_key, code_len).unwrap(); let (mut vp_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let (mut tx_cache, _) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::tx_cache(); let mut outer_tx = Tx::from_type(TxType::Raw); outer_tx.set_code(Code::new(tx_code.clone(), None)); outer_tx.set_data(Data::new(vec![])); @@ -1815,7 +1877,8 @@ mod tests { state.write_log_mut().write(&key, tx_code.clone()).unwrap(); state.write_log_mut().write(&len_key, code_len).unwrap(); - let (vp_cache, _) = wasm::compilation_cache::common::testing::cache(); + let (vp_cache, _) = + wasm::compilation_cache::common::testing::vp_cache(); let mut outer_tx = Tx::from_type(TxType::Raw); outer_tx.set_code(Code::new(tx_code.clone(), None)); outer_tx.set_data(Data::new(vec![])); @@ -1858,7 +1921,8 @@ mod tests { state.write_log_mut().write(&key, tx_code.clone()).unwrap(); state.write_log_mut().write(&len_key, code_len).unwrap(); - let (vp_cache, _) = wasm::compilation_cache::common::testing::cache(); + let (vp_cache, _) = + wasm::compilation_cache::common::testing::vp_cache(); let mut outer_tx = Tx::from_type(TxType::Raw); outer_tx.set_code(Code::new(tx_code.clone(), None)); outer_tx.set_data(Data::new(vec![])); diff --git a/crates/vp/Cargo.toml b/crates/vp/Cargo.toml index 0523b9a50d..a87bdb9e4c 100644 --- a/crates/vp/Cargo.toml +++ b/crates/vp/Cargo.toml @@ -12,12 +12,16 @@ readme.workspace = true repository.workspace = true version.workspace = true +[features] +testing = ["namada_core/testing"] + [dependencies] namada_core = { path = "../core" } namada_events = { path = "../events", default-features = false } namada_gas = { path = "../gas" } namada_state = { path = "../state" } namada_tx = { path = "../tx" } +namada_vp_env = { path = "../vp_env" } smooth-operator.workspace = true thiserror.workspace = true diff --git a/crates/vp/src/lib.rs b/crates/vp/src/lib.rs index afc0c84ddd..0662fa432f 100644 --- a/crates/vp/src/lib.rs +++ b/crates/vp/src/lib.rs @@ -1,4 +1,7 @@ //! This crate contains the trait for native validity predicates with its //! various context types and host functions implementation. +pub mod native_vp; pub mod vp_host_fns; + +pub use namada_vp_env::VpEnv; diff --git a/crates/vp/src/native_vp.rs b/crates/vp/src/native_vp.rs index f5b27cd0cb..274913b749 100644 --- a/crates/vp/src/native_vp.rs +++ b/crates/vp/src/native_vp.rs @@ -2,34 +2,29 @@ //! Native validity predicate interface associated with internal accounts such //! as the PoS and IBC modules. -pub mod ethereum_bridge; -pub mod ibc; -pub mod masp; -pub mod multitoken; -pub mod parameters; - use std::cell::RefCell; use std::collections::BTreeSet; use std::fmt::Debug; +use std::marker::PhantomData; -use borsh::BorshDeserialize; -use namada_core::storage; +use namada_core::address::Address; +use namada_core::borsh::BorshDeserialize; +use namada_core::hash::Hash; use namada_core::storage::Epochs; +use namada_core::{borsh, storage}; use namada_events::{Event, EventType}; -use namada_gas::GasMetering; +use namada_gas::{GasMetering, VpGasMeter}; +use namada_state as state; +use namada_state::prefix_iter::PrefixIterators; +use namada_state::{ + BlockHeight, Epoch, Header, Key, ResultExt, StorageRead, StorageResult, + TxIndex, +}; use namada_tx::{BatchedTxRef, Tx, TxCommitments}; pub use namada_vp_env::VpEnv; use state::StateRead; use super::vp_host_fns; -use crate::address::Address; -use crate::hash::Hash; -use crate::ledger::gas::VpGasMeter; -use crate::state; -use crate::state::{ResultExt, StorageRead}; -use crate::storage::{BlockHeight, Epoch, Header, Key, TxIndex}; -use crate::vm::prefix_iter::PrefixIterators; -use crate::vm::WasmCacheAccess; /// Possible error in a native VP host function call /// The `state::StorageError` may wrap the `vp_host_fns::RuntimeError` @@ -37,13 +32,13 @@ use crate::vm::WasmCacheAccess; pub type Error = state::StorageError; /// A native VP module should implement its validation logic using this trait. -pub trait NativeVp { +pub trait NativeVp<'a> { /// Error type for the methods' results. type Error: std::error::Error; /// Run the validity predicate fn validate_tx( - &self, + &'a self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -56,10 +51,10 @@ pub trait NativeVp { /// wrapper types and `eval_runner` field. The references must not be changed /// when [`Ctx`] is mutable. #[derive(Debug)] -pub struct Ctx<'a, S, CA> +pub struct Ctx<'a, S, CA, EVAL> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// The address of the account that owns the VP pub address: &'a Address, @@ -82,39 +77,56 @@ where /// calls to `eval`. pub verifiers: &'a BTreeSet
, /// VP WASM compilation cache - #[cfg(feature = "wasm-runtime")] - pub vp_wasm_cache: crate::vm::wasm::VpCache, - /// To avoid unused parameter without "wasm-runtime" feature - #[cfg(not(feature = "wasm-runtime"))] - pub cache_access: std::marker::PhantomData, + pub vp_wasm_cache: CA, + /// VP evaluator type + pub eval: PhantomData, +} + +/// A Validity predicate runner for calls from the [`vp_eval`] function. +pub trait VpEvaluator<'a, S, CA, EVAL> +where + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, +{ + /// Evaluate a given validity predicate code with the given input data. + /// Currently, we can only evaluate VPs using WASM runner with WASM memory. + /// + /// Invariant: Calling `VpEvalRunner::eval` from the VP is synchronous as it + /// shares mutable access to the host context with the VP. + fn eval( + ctx: &Ctx<'a, S, CA, EVAL>, + vp_code_hash: Hash, + input_data: BatchedTxRef<'_>, + ) -> StorageResult<()>; } /// Read access to the prior storage (state before tx execution) via /// [`trait@StorageRead`]. #[derive(Debug)] -pub struct CtxPreStorageRead<'view, 'a, S, CA> +pub struct CtxPreStorageRead<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { - pub(crate) ctx: &'view Ctx<'a, S, CA>, + pub(crate) ctx: &'view Ctx<'a, S, CA, EVAL>, } /// Read access to the posterior storage (state after tx execution) via /// [`trait@StorageRead`]. #[derive(Debug)] -pub struct CtxPostStorageRead<'view, 'a, S, CA> +pub struct CtxPostStorageRead<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { - ctx: &'view Ctx<'a, S, CA>, + ctx: &'view Ctx<'a, S, CA, EVAL>, } -impl<'a, S, CA> Ctx<'a, S, CA> +impl<'a, S, CA, EVAL> Ctx<'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, { /// Initialize a new context for native VP call #[allow(clippy::too_many_arguments)] @@ -127,8 +139,7 @@ where gas_meter: &'a RefCell, keys_changed: &'a BTreeSet, verifiers: &'a BTreeSet
, - #[cfg(feature = "wasm-runtime")] - vp_wasm_cache: crate::vm::wasm::VpCache, + vp_wasm_cache: CA, ) -> Self { Self { address, @@ -140,31 +151,34 @@ where tx_index, keys_changed, verifiers, - #[cfg(feature = "wasm-runtime")] vp_wasm_cache, - #[cfg(not(feature = "wasm-runtime"))] - cache_access: std::marker::PhantomData, + eval: PhantomData, } } /// Read access to the prior storage (state before tx execution) /// via [`trait@StorageRead`]. - pub fn pre<'view>(&'view self) -> CtxPreStorageRead<'view, 'a, S, CA> { + pub fn pre<'view>( + &'view self, + ) -> CtxPreStorageRead<'view, 'a, S, CA, EVAL> { CtxPreStorageRead { ctx: self } } /// Read access to the posterior storage (state after tx execution) /// via [`trait@StorageRead`]. - pub fn post<'view>(&'view self) -> CtxPostStorageRead<'view, 'a, S, CA> { + pub fn post<'view>( + &'view self, + ) -> CtxPostStorageRead<'view, 'a, S, CA, EVAL> { CtxPostStorageRead { ctx: self } } } -impl<'view, 'a: 'view, S, CA> StorageRead - for CtxPreStorageRead<'view, 'a, S, CA> +impl<'view, 'a: 'view, S, CA, EVAL> StorageRead + for CtxPreStorageRead<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, { type PrefixIter<'iter> = state::PrefixIter<'iter,:: D> where Self: 'iter; @@ -232,16 +246,17 @@ where self.ctx.get_native_token() } - fn get_pred_epochs(&self) -> state::StorageResult { + fn get_pred_epochs(&self) -> StorageResult { self.ctx.get_pred_epochs() } } -impl<'view, 'a: 'view, S, CA> StorageRead - for CtxPostStorageRead<'view, 'a, S, CA> +impl<'view, 'a: 'view, S, CA, EVAL> StorageRead + for CtxPostStorageRead<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, { type PrefixIter<'iter> = state::PrefixIter<'iter, ::D> where Self: 'iter; @@ -309,18 +324,19 @@ where Ok(self.ctx.state.in_mem().native_token.clone()) } - fn get_pred_epochs(&self) -> state::StorageResult { + fn get_pred_epochs(&self) -> StorageResult { self.ctx.get_pred_epochs() } } -impl<'view, 'a: 'view, S, CA> VpEnv<'view> for Ctx<'a, S, CA> +impl<'view, 'a: 'view, S, CA, EVAL> VpEnv<'view> for Ctx<'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, { - type Post = CtxPostStorageRead<'view, 'a, S, CA>; - type Pre = CtxPreStorageRead<'view, 'a, S, CA>; + type Post = CtxPostStorageRead<'view, 'a, S, CA, EVAL>; + type Pre = CtxPreStorageRead<'view, 'a, S, CA, EVAL>; type PrefixIter<'iter> = state::PrefixIter<'iter, ::D> where Self: 'iter; fn pre(&'view self) -> Self::Pre { @@ -381,7 +397,7 @@ where .into_storage_result() } - fn get_pred_epochs(&self) -> state::StorageResult { + fn get_pred_epochs(&self) -> StorageResult { vp_host_fns::get_pred_epochs(self.gas_meter, self.state) .into_storage_result() } @@ -415,62 +431,8 @@ where &self, vp_code_hash: Hash, input_data: BatchedTxRef<'_>, - ) -> Result<(), state::StorageError> { - #[cfg(feature = "wasm-runtime")] - { - use std::marker::PhantomData; - - use crate::vm::host_env::VpCtx; - use crate::vm::wasm::run::VpEvalWasm; - - let eval_runner = - VpEvalWasm::<::D, ::H, CA> { - db: PhantomData, - hasher: PhantomData, - cache_access: PhantomData, - }; - let mut iterators: PrefixIterators<'_, ::D> = - PrefixIterators::default(); - let mut result_buffer: Option> = None; - let mut yielded_value: Option> = None; - let mut vp_wasm_cache = self.vp_wasm_cache.clone(); - - let ctx = VpCtx::new( - self.address, - self.state.write_log(), - self.state.in_mem(), - self.state.db(), - self.gas_meter, - self.tx, - self.cmt, - self.tx_index, - &mut iterators, - self.verifiers, - &mut result_buffer, - &mut yielded_value, - self.keys_changed, - &eval_runner, - &mut vp_wasm_cache, - ); - eval_runner - .eval_native_result(ctx, vp_code_hash, input_data) - .inspect_err(|err| { - tracing::warn!( - "VP eval from a native VP failed with: {err}", - ); - }) - .into_storage_result() - } - - #[cfg(not(feature = "wasm-runtime"))] - { - // This line is here to prevent unused var clippy warning - let _ = (vp_code_hash, input_data); - unimplemented!( - "The \"wasm-runtime\" feature must be enabled to use the \ - `eval` function." - ) - } + ) -> StorageResult<()> { + EVAL::eval(self, vp_code_hash, input_data) } fn charge_gas(&self, used_gas: u64) -> Result<(), state::StorageError> { @@ -524,10 +486,11 @@ where } } -impl namada_tx::action::Read for Ctx<'_, S, CA> +impl<'a, S, CA, EVAL> namada_tx::action::Read for Ctx<'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, { type Err = Error; @@ -586,10 +549,11 @@ pub trait StorageReader { } } -impl<'a, S, CA> StorageReader for &Ctx<'a, S, CA> +impl<'a, S, CA, EVAL> StorageReader for &Ctx<'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, { /// Helper function. After reading posterior state, /// borsh deserialize to specified type diff --git a/crates/vp_env/Cargo.toml b/crates/vp_env/Cargo.toml index e21b44ab11..7d4b650aae 100644 --- a/crates/vp_env/Cargo.toml +++ b/crates/vp_env/Cargo.toml @@ -17,7 +17,6 @@ namada_core = { path = "../core" } namada_events = { path = "../events", default-features = false } namada_storage = { path = "../storage" } namada_tx = { path = "../tx" } -namada_ibc = { path = "../ibc" } derivative.workspace = true masp_primitives.workspace = true From 21514ee2580acd2695c4fd53cebaa166afdefc15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 12 Jun 2024 09:57:01 +0100 Subject: [PATCH 14/41] mv crates/namada/src/ledger/native_vp/ethereum_bridge crates/ethereum_bridge/src/vp --- .../ethereum_bridge => ethereum_bridge/src/vp}/bridge_pool_vp.rs | 0 .../native_vp/ethereum_bridge => ethereum_bridge/src/vp}/mod.rs | 0 .../native_vp/ethereum_bridge => ethereum_bridge/src/vp}/nut.rs | 0 .../native_vp/ethereum_bridge => ethereum_bridge/src/vp}/vp.rs | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/native_vp/ethereum_bridge => ethereum_bridge/src/vp}/bridge_pool_vp.rs (100%) rename crates/{namada/src/ledger/native_vp/ethereum_bridge => ethereum_bridge/src/vp}/mod.rs (100%) rename crates/{namada/src/ledger/native_vp/ethereum_bridge => ethereum_bridge/src/vp}/nut.rs (100%) rename crates/{namada/src/ledger/native_vp/ethereum_bridge => ethereum_bridge/src/vp}/vp.rs (100%) diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs b/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs rename to crates/ethereum_bridge/src/vp/bridge_pool_vp.rs diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/mod.rs b/crates/ethereum_bridge/src/vp/mod.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/ethereum_bridge/mod.rs rename to crates/ethereum_bridge/src/vp/mod.rs diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/nut.rs b/crates/ethereum_bridge/src/vp/nut.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/ethereum_bridge/nut.rs rename to crates/ethereum_bridge/src/vp/nut.rs diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs b/crates/ethereum_bridge/src/vp/vp.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs rename to crates/ethereum_bridge/src/vp/vp.rs From 7ecf831cde958438983f1d5787965a6d73e00273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 12 Jun 2024 17:14:46 +0100 Subject: [PATCH 15/41] ethereum_bridge: fix VPs post move --- Cargo.lock | 7 + crates/core/src/token.rs | 4 + crates/core/src/uint.rs | 22 ++ crates/ethereum_bridge/Cargo.toml | 8 + crates/ethereum_bridge/src/lib.rs | 1 + .../src/storage/eth_bridge_queries.rs | 1 + .../ethereum_bridge/src/vp/bridge_pool_vp.rs | 205 +++++++++++------- .../src/vp/{vp.rs => eth_bridge_vp.rs} | 119 +++++----- crates/ethereum_bridge/src/vp/mod.rs | 10 +- .../src/vp/{nut.rs => nut_vp.rs} | 72 +++--- crates/trans_token/src/lib.rs | 6 + 11 files changed, 296 insertions(+), 159 deletions(-) rename crates/ethereum_bridge/src/vp/{vp.rs => eth_bridge_vp.rs} (82%) rename crates/ethereum_bridge/src/vp/{nut.rs => nut_vp.rs} (82%) diff --git a/Cargo.lock b/Cargo.lock index f972f7ebaf..f7cf48ee5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4735,16 +4735,23 @@ dependencies = [ "namada_account", "namada_core", "namada_events", + "namada_gas", "namada_macros", "namada_migrations", "namada_parameters", "namada_proof_of_stake", "namada_state", "namada_storage", + "namada_token", "namada_trans_token", "namada_tx", + "namada_vm", "namada_vote_ext", + "namada_vp", + "proptest", + "rand 0.8.5", "serde", + "smooth-operator", "thiserror", "toml 0.5.11", "tracing", diff --git a/crates/core/src/token.rs b/crates/core/src/token.rs index f792825c7f..cffb8c583b 100644 --- a/crates/core/src/token.rs +++ b/crates/core/src/token.rs @@ -32,6 +32,10 @@ pub trait Keys { token_addr: &Address, key: &'a storage::Key, ) -> Option<&'a Address>; + + /// Check if the given storage key is a balance key for an unspecified + /// token. If it is, return the token and owner address. + fn is_any_token_balance(key: &storage::Key) -> Option<[&Address; 2]>; } /// Amount in micro units. For different granularity another representation diff --git a/crates/core/src/uint.rs b/crates/core/src/uint.rs index a4f3f22254..d77e08369b 100644 --- a/crates/core/src/uint.rs +++ b/crates/core/src/uint.rs @@ -963,6 +963,12 @@ impl From for I320 { } } +impl From for I320 { + fn from(val: u64) -> Self { + Self::from(Uint::from(val)) + } +} + impl From for I320 { fn from(lo: token::Amount) -> Self { let mut arr = [0u64; Self::N_WORDS]; @@ -1160,6 +1166,22 @@ pub mod testing { self.checked_neg().unwrap() } } + + impl std::ops::Add for I320 { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + self.checked_add(rhs).unwrap() + } + } + + impl std::ops::Neg for I320 { + type Output = Self; + + fn neg(self) -> Self::Output { + self.checked_neg().unwrap() + } + } } #[cfg(test)] diff --git a/crates/ethereum_bridge/Cargo.toml b/crates/ethereum_bridge/Cargo.toml index 0250028605..cd2aed558b 100644 --- a/crates/ethereum_bridge/Cargo.toml +++ b/crates/ethereum_bridge/Cargo.toml @@ -38,6 +38,7 @@ namada_storage = {path = "../storage"} namada_trans_token = {path = "../trans_token"} namada_tx = {path = "../tx"} namada_vote_ext = {path = "../vote_ext"} +namada_vp = {path = "../vp"} borsh.workspace = true ethers.workspace = true @@ -46,6 +47,7 @@ itertools.workspace = true konst.workspace = true linkme = {workspace = true, optional = true} serde.workspace = true +smooth-operator.workspace = true thiserror.workspace = true tracing = "0.1.30" @@ -53,10 +55,16 @@ tracing = "0.1.30" # Added "testing" feature. namada_account = {path = "../account"} namada_core = {path = "../core", default-features = false, features = ["ethers-derive", "testing"]} +namada_gas = {path = "../gas"} namada_proof_of_stake = {path = "../proof_of_stake", default-features = false, features = ["testing"]} namada_state = { path = "../state", features = ["testing"] } +namada_token = {path = "../token", features = ["testing"]} +namada_tx = {path = "../tx", features = ["testing"]} +namada_vm = {path = "../vm", features = ["testing"]} assert_matches.workspace = true data-encoding.workspace = true ethabi.workspace = true +proptest.workspace = true +rand.workspace = true toml.workspace = true diff --git a/crates/ethereum_bridge/src/lib.rs b/crates/ethereum_bridge/src/lib.rs index 3a6586de0d..f2da36edcd 100644 --- a/crates/ethereum_bridge/src/lib.rs +++ b/crates/ethereum_bridge/src/lib.rs @@ -23,6 +23,7 @@ pub mod protocol; pub mod storage; #[cfg(any(test, feature = "testing"))] pub mod test_utils; +pub mod vp; pub use namada_core::address::ETH_BRIDGE as ADDRESS; pub use namada_trans_token as token; diff --git a/crates/ethereum_bridge/src/storage/eth_bridge_queries.rs b/crates/ethereum_bridge/src/storage/eth_bridge_queries.rs index 734b7032de..da9a2d565d 100644 --- a/crates/ethereum_bridge/src/storage/eth_bridge_queries.rs +++ b/crates/ethereum_bridge/src/storage/eth_bridge_queries.rs @@ -37,6 +37,7 @@ pub const fn is_bridge_comptime_enabled() -> bool { pub fn check_bridge_status( storage: &S, ) -> namada_storage::Result { + #[cfg(not(test))] if !is_bridge_comptime_enabled() { return Ok(EthBridgeStatus::Disabled); } diff --git a/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs b/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs index 858adf3e7b..f3b0a6c585 100644 --- a/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs +++ b/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs @@ -17,29 +17,28 @@ use std::fmt::Debug; use std::marker::PhantomData; use borsh::BorshDeserialize; +use namada_core::address::{Address, InternalAddress}; use namada_core::arith::{checked, CheckedAdd, CheckedNeg, CheckedSub}; use namada_core::booleans::BoolResultUnitExt; -use namada_core::eth_bridge_pool::erc20_token_address; -use namada_core::hints; -use namada_ethereum_bridge::storage::bridge_pool::{ - get_pending_key, is_bridge_pool_key, BRIDGE_POOL_ADDRESS, +use namada_core::eth_bridge_pool::{ + erc20_token_address, PendingTransfer, TransferToEthereumKind, }; -use namada_ethereum_bridge::storage::eth_bridge_queries::is_bridge_active_at; -use namada_ethereum_bridge::storage::parameters::read_native_erc20_address; -use namada_ethereum_bridge::storage::whitelist; -use namada_ethereum_bridge::ADDRESS as BRIDGE_ADDRESS; +use namada_core::ethereum_events::EthAddress; +use namada_core::hints; +use namada_core::storage::Key; +use namada_core::token::{self, Amount}; +use namada_core::uint::I320; use namada_state::{ResultExt, StateRead}; use namada_tx::BatchedTxRef; +use namada_vp::native_vp::{self, Ctx, NativeVp, StorageReader, VpEvaluator}; -use crate::address::{Address, InternalAddress}; -use crate::eth_bridge_pool::{PendingTransfer, TransferToEthereumKind}; -use crate::ethereum_events::EthAddress; -use crate::ledger::native_vp::{self, Ctx, NativeVp, StorageReader}; -use crate::storage::Key; -use crate::token::storage_key::balance_key; -use crate::token::Amount; -use crate::uint::I320; -use crate::vm::WasmCacheAccess; +use crate::storage::bridge_pool::{ + get_pending_key, is_bridge_pool_key, BRIDGE_POOL_ADDRESS, +}; +use crate::storage::eth_bridge_queries::is_bridge_active_at; +use crate::storage::parameters::read_native_erc20_address; +use crate::storage::whitelist; +use crate::ADDRESS as BRIDGE_ADDRESS; #[derive(thiserror::Error, Debug)] #[error("Bridge Pool VP error: {0}")] @@ -65,19 +64,23 @@ impl AmountDelta { } /// Validity predicate for the Ethereum bridge -pub struct BridgePoolVp<'ctx, S, CA> +pub struct BridgePool<'ctx, S, CA, EVAL, TokenKeys> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'ctx, S, CA>, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Token keys type + pub token_keys: PhantomData, } -impl<'a, S, CA> BridgePoolVp<'a, S, CA> +impl<'a, S, CA, EVAL, TokenKeys> BridgePool<'a, S, CA, EVAL, TokenKeys> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, + TokenKeys: token::Keys, { /// Get the change in the balance of an account /// associated with an address @@ -86,7 +89,7 @@ where token: &Address, address: &Address, ) -> Result, Error> { - let account_key = balance_key(token, address); + let account_key = TokenKeys::balance(token, address); let before: Amount = (&self.ctx) .read_pre_value(&account_key) .map_err(|error| { @@ -410,17 +413,23 @@ impl EscrowDelta<'_, KIND> { /// amount is greater than zero, then the appropriate escrow /// keys must have been written to by some wasm tx. #[inline] - fn validate(&self, changed_keys: &BTreeSet) -> bool { + fn validate( + &self, + changed_keys: &BTreeSet, + ) -> bool { if hints::unlikely(self.transferred_amount_is_nil()) { - self.check_escrow_keys_unchanged(changed_keys) + self.check_escrow_keys_unchanged::(changed_keys) } else { - self.check_escrow_keys_changed(changed_keys) + self.check_escrow_keys_changed::(changed_keys) } } /// Check if all required escrow keys in `changed_keys` were modified. #[inline] - fn check_escrow_keys_changed(&self, changed_keys: &BTreeSet) -> bool { + fn check_escrow_keys_changed( + &self, + changed_keys: &BTreeSet, + ) -> bool { let EscrowDelta { token, payer_account, @@ -428,15 +437,15 @@ impl EscrowDelta<'_, KIND> { .. } = self; - let owner_key = balance_key(token, payer_account); - let escrow_key = balance_key(token, escrow_account); + let owner_key = TokenKeys::balance(token, payer_account); + let escrow_key = TokenKeys::balance(token, escrow_account); changed_keys.contains(&owner_key) && changed_keys.contains(&escrow_key) } /// Check if no escrow keys in `changed_keys` were modified. #[inline] - fn check_escrow_keys_unchanged( + fn check_escrow_keys_unchanged( &self, changed_keys: &BTreeSet, ) -> bool { @@ -447,8 +456,8 @@ impl EscrowDelta<'_, KIND> { .. } = self; - let owner_key = balance_key(token, payer_account); - let escrow_key = balance_key(token, escrow_account); + let owner_key = TokenKeys::balance(token, payer_account); + let escrow_key = TokenKeys::balance(token, escrow_account); !changed_keys.contains(&owner_key) && !changed_keys.contains(&escrow_key) @@ -475,9 +484,12 @@ struct EscrowCheck<'a> { impl EscrowCheck<'_> { #[inline] - fn validate(&self, changed_keys: &BTreeSet) -> bool { - self.gas_check.validate(changed_keys) - && self.token_check.validate(changed_keys) + fn validate( + &self, + changed_keys: &BTreeSet, + ) -> bool { + self.gas_check.validate::(changed_keys) + && self.token_check.validate::(changed_keys) } } @@ -503,10 +515,13 @@ fn sum_gas_and_token_amounts( }) } -impl<'a, S, CA> NativeVp for BridgePoolVp<'a, S, CA> +impl<'a, S, CA, EVAL, TokenKeys> NativeVp<'a> + for BridgePool<'a, S, CA, EVAL, TokenKeys> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, + TokenKeys: token::Keys, { type Error = Error; @@ -596,7 +611,7 @@ where read_native_erc20_address(&self.ctx.pre()).map_err(Error)?; let escrow_checks = self.determine_escrow_checks(&wnam_address, &transfer)?; - if !escrow_checks.validate(keys_changed) { + if !escrow_checks.validate::(keys_changed) { let error = native_vp::Error::new_const( // TODO(namada#3247): specify which storage changes are missing // or which ones are invalid @@ -657,31 +672,41 @@ where } } -#[cfg(all(test, feature = "namada-eth-bridge"))] +#[allow(clippy::arithmetic_side_effects)] +#[cfg(test)] mod test_bridge_pool_vp { use std::cell::RefCell; use std::env::temp_dir; + use namada_core::address::testing::{nam, wnam}; use namada_core::borsh::BorshSerializeExt; - use namada_ethereum_bridge::storage::bridge_pool::get_signed_root_key; - use namada_ethereum_bridge::storage::parameters::{ - Contracts, EthereumBridgeParams, UpgradeableContract, - }; - use namada_ethereum_bridge::storage::wrapped_erc20s; - use namada_gas::TxGasMeter; + use namada_core::eth_bridge_pool::{GasFee, TransferToEthereum}; + use namada_core::hash::Hash; + use namada_core::WasmCacheRwAccess; + use namada_gas::{TxGasMeter, VpGasMeter}; use namada_state::testing::TestState; - use namada_state::StorageWrite; + use namada_state::write_log::WriteLog; + use namada_state::{StorageWrite, TxIndex}; + use namada_trans_token::storage_key::balance_key; use namada_tx::data::TxType; + use namada_tx::Tx; + use namada_vm::wasm::run::VpEvalWasm; + use namada_vm::wasm::VpCache; use super::*; - use crate::address::testing::{nam, wnam}; - use crate::eth_bridge_pool::{GasFee, TransferToEthereum}; - use crate::hash::Hash; - use crate::ledger::gas::VpGasMeter; - use crate::state::write_log::WriteLog; - use crate::storage::TxIndex; - use crate::vm::wasm::VpCache; - use crate::vm::WasmCacheRwAccess; + use crate::storage::bridge_pool::get_signed_root_key; + use crate::storage::parameters::{ + Contracts, EthereumBridgeParams, UpgradeableContract, + }; + use crate::storage::wrapped_erc20s; + + type CA = WasmCacheRwAccess; + type Eval = VpEvalWasm< + ::D, + ::H, + CA, + >; + type TokenKeys = namada_token::Store<()>; /// The amount of NAM Bertha has const ASSET: EthAddress = EthAddress([0; 20]); @@ -731,7 +756,7 @@ mod test_bridge_pool_vp { /// An implicit user address for testing & development #[allow(dead_code)] pub fn daewon_address() -> Address { - use crate::key::*; + use namada_core::key::*; pub fn daewon_keypair() -> common::SecretKey { let bytes = [ 235, 250, 15, 1, 145, 250, 172, 218, 247, 27, 63, 212, 60, 47, @@ -938,11 +963,13 @@ mod test_bridge_pool_vp { gas_meter: &'a RefCell, keys_changed: &'a BTreeSet, verifiers: &'a BTreeSet
, - ) -> Ctx<'a, TestState, WasmCacheRwAccess> { + ) -> Ctx<'a, TestState, VpCache, Eval> { + let batched_tx = tx.batch_ref_first_tx(); Ctx::new( &BRIDGE_POOL_ADDRESS, state, - tx, + batched_tx.tx, + batched_tx.cmt, &TxIndex(0), gas_meter, keys_changed, @@ -970,7 +997,8 @@ mod test_bridge_pool_vp { { // setup let mut state = setup_storage(); - let tx = Tx::from_type(TxType::Raw); + let mut tx = Tx::from_type(TxType::Raw); + tx.push_default_inner_tx(); // the transfer to be added to the pool let mut transfer = PendingTransfer { @@ -1025,13 +1053,15 @@ mod test_bridge_pool_vp { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new_from_sub_limit(u64::MAX.into()), )); - let vp = BridgePoolVp { + let vp = BridgePool { ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), + token_keys: PhantomData::, }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); + let tx = tx.batch_ref_first_tx(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); match (expect, res) { (Expect::Accepted, Ok(())) => (), @@ -1321,7 +1351,8 @@ mod test_bridge_pool_vp { fn test_adding_transfer_twice_fails() { // setup let mut state = setup_storage(); - let tx = Tx::from_type(TxType::Raw); + let mut tx = Tx::from_type(TxType::Raw); + tx.push_default_inner_tx(); // the transfer to be added to the pool let transfer = initial_pool(); @@ -1370,13 +1401,15 @@ mod test_bridge_pool_vp { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new_from_sub_limit(u64::MAX.into()), )); - let vp = BridgePoolVp { + let vp = BridgePool { ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), + token_keys: PhantomData::, }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); + let tx = tx.batch_ref_first_tx(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_err()); } @@ -1387,7 +1420,8 @@ mod test_bridge_pool_vp { fn test_zero_gas_fees_rejected() { // setup let mut state = setup_storage(); - let tx = Tx::from_type(TxType::Raw); + let mut tx = Tx::from_type(TxType::Raw); + tx.push_default_inner_tx(); // the transfer to be added to the pool let transfer = PendingTransfer { @@ -1428,13 +1462,15 @@ mod test_bridge_pool_vp { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new_from_sub_limit(u64::MAX.into()), )); - let vp = BridgePoolVp { + let vp = BridgePool { ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), + token_keys: PhantomData::, }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); + let tx = tx.batch_ref_first_tx(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_err()); } @@ -1447,7 +1483,8 @@ mod test_bridge_pool_vp { let mut state = setup_storage(); let eb_account_key = balance_key(&nam(), &Address::Internal(InternalAddress::EthBridge)); - let tx = Tx::from_type(TxType::Raw); + let mut tx = Tx::from_type(TxType::Raw); + tx.push_default_inner_tx(); // the transfer to be added to the pool let transfer = PendingTransfer { @@ -1507,13 +1544,15 @@ mod test_bridge_pool_vp { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new_from_sub_limit(u64::MAX.into()), )); - let vp = BridgePoolVp { + let vp = BridgePool { ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), + token_keys: PhantomData::, }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); + let tx = tx.batch_ref_first_tx(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_ok()); } @@ -1525,7 +1564,8 @@ mod test_bridge_pool_vp { fn test_reject_mint_wnam() { // setup let mut state = setup_storage(); - let tx = Tx::from_type(TxType::Raw); + let mut tx = Tx::from_type(TxType::Raw); + tx.push_default_inner_tx(); let eb_account_key = balance_key(&nam(), &Address::Internal(InternalAddress::EthBridge)); @@ -1581,13 +1621,15 @@ mod test_bridge_pool_vp { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new_from_sub_limit(u64::MAX.into()), )); - let vp = BridgePoolVp { + let vp = BridgePool { ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), + token_keys: PhantomData::, }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); + let tx = tx.batch_ref_first_tx(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_err()); } @@ -1612,7 +1654,8 @@ mod test_bridge_pool_vp { .write(&gas_payer_balance_key, Amount::from(BERTHA_WEALTH)) .expect("Test failed"); state.write_log_mut().commit_tx(); - let tx = Tx::from_type(TxType::Raw); + let mut tx = Tx::from_type(TxType::Raw); + tx.push_default_inner_tx(); // the transfer to be added to the pool let transfer = PendingTransfer { @@ -1672,13 +1715,15 @@ mod test_bridge_pool_vp { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new_from_sub_limit(u64::MAX.into()), )); - let vp = BridgePoolVp { + let vp = BridgePool { ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), + token_keys: PhantomData::, }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); + let tx = tx.batch_ref_first_tx(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_err()); } @@ -1687,7 +1732,8 @@ mod test_bridge_pool_vp { fn test_nut_aux(kind: TransferToEthereumKind, expect: Expect) { // setup let mut state = setup_storage(); - let tx = Tx::from_type(TxType::Raw); + let mut tx = Tx::from_type(TxType::Raw); + tx.push_default_inner_tx(); // the transfer to be added to the pool let transfer = PendingTransfer { @@ -1749,13 +1795,16 @@ mod test_bridge_pool_vp { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new_from_sub_limit(u64::MAX.into()), )); - let vp = BridgePoolVp { + let vp = BridgePool { ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), + token_keys: PhantomData::, }; let mut tx = Tx::from_type(TxType::Raw); + tx.push_default_inner_tx(); tx.add_data(transfer); + let tx = tx.batch_ref_first_tx(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); match (expect, res) { (Expect::Accepted, Ok(())) => (), @@ -1846,7 +1895,7 @@ mod test_bridge_pool_vp { // NOTE: testing no changed keys let empty_keys = BTreeSet::new(); - assert!(delta.validate(&empty_keys)); + assert!(delta.validate::(&empty_keys)); } /// Test that the Bridge pool native VP rejects transfers that @@ -1869,7 +1918,7 @@ mod test_bridge_pool_vp { // NOTE: testing changed keys let some_changed_keys = BTreeSet::from([owner_key]); - assert!(!delta.validate(&some_changed_keys)); + assert!(!delta.validate::(&some_changed_keys)); } /// Test that the Bridge pool native VP validates transfers @@ -1891,7 +1940,7 @@ mod test_bridge_pool_vp { // NOTE: testing no changed keys let empty_keys = BTreeSet::new(); - assert!(delta.validate(&empty_keys)); + assert!(delta.validate::(&empty_keys)); } /// Test that the Bridge pool native VP rejects transfers @@ -1914,6 +1963,6 @@ mod test_bridge_pool_vp { // NOTE: testing changed keys let some_changed_keys = BTreeSet::from([owner_key]); - assert!(!delta.validate(&some_changed_keys)); + assert!(!delta.validate::(&some_changed_keys)); } } diff --git a/crates/ethereum_bridge/src/vp/vp.rs b/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs similarity index 82% rename from crates/ethereum_bridge/src/vp/vp.rs rename to crates/ethereum_bridge/src/vp/eth_bridge_vp.rs index d69c29ec45..7340f6dd6d 100644 --- a/crates/ethereum_bridge/src/vp/vp.rs +++ b/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs @@ -1,20 +1,19 @@ //! Validity predicate for the Ethereum bridge use std::collections::BTreeSet; +use std::marker::PhantomData; use namada_core::address::Address; use namada_core::booleans::BoolResultUnitExt; use namada_core::collections::HashSet; use namada_core::storage::Key; -use namada_ethereum_bridge::storage; -use namada_ethereum_bridge::storage::escrow_key; +use namada_core::token::{self, Amount}; +use namada_state::StateRead; use namada_tx::BatchedTxRef; +use namada_vp::native_vp::{self, Ctx, NativeVp, StorageReader, VpEvaluator}; -use crate::ledger::native_vp::{self, Ctx, NativeVp, StorageReader}; -use crate::state::StateRead; -use crate::token::storage_key::{balance_key, is_balance_key}; -use crate::token::Amount; -use crate::vm::WasmCacheAccess; +use crate::storage; +use crate::storage::escrow_key; /// Generic error that may be returned by the validity predicate #[derive(thiserror::Error, Debug)] @@ -22,27 +21,31 @@ use crate::vm::WasmCacheAccess; pub struct Error(#[from] native_vp::Error); /// Validity predicate for the Ethereum bridge -pub struct EthBridge<'ctx, S, CA> +pub struct EthBridge<'ctx, S, CA, EVAL, TokenKeys> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'ctx, S, CA>, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Token keys type + pub token_keys: PhantomData, } -impl<'ctx, S, CA> EthBridge<'ctx, S, CA> +impl<'ctx, S, CA, EVAL, TokenKeys> EthBridge<'ctx, S, CA, EVAL, TokenKeys> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, + TokenKeys: token::Keys, { /// If the Ethereum bridge's escrow key was written to, we check /// that the NAM balance increased and that the Bridge pool VP has /// been triggered. fn check_escrow(&self, verifiers: &BTreeSet
) -> Result<(), Error> { - let escrow_key = balance_key( + let escrow_key = TokenKeys::balance( &self.ctx.state.in_mem().native_token, - &crate::ethereum_bridge::ADDRESS, + &crate::ADDRESS, ); let escrow_pre: Amount = @@ -74,10 +77,13 @@ where } } -impl<'a, S, CA> NativeVp for EthBridge<'a, S, CA> +impl<'a, S, CA, EVAL, TokenKeys> NativeVp<'a> + for EthBridge<'a, S, CA, EVAL, TokenKeys> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + TokenKeys: token::Keys, { type Error = Error; @@ -103,7 +109,7 @@ where "Ethereum Bridge VP triggered", ); - validate_changed_keys( + validate_changed_keys::( &self.ctx.state.in_mem().native_token, keys_changed, )?; @@ -121,7 +127,7 @@ where /// /// Any other keys changed under the Ethereum bridge account /// are rejected. -fn validate_changed_keys( +fn validate_changed_keys( nam_addr: &Address, keys_changed: &BTreeSet, ) -> Result<(), Error> { @@ -131,7 +137,8 @@ fn validate_changed_keys( .iter() .filter(|&key| { let changes_eth_storage = storage::has_eth_addr_segment(key); - let changes_nam_balance = is_balance_key(nam_addr, key).is_some(); + let changes_nam_balance = + TokenKeys::is_balance(nam_addr, key).is_some(); changes_nam_balance || changes_eth_storage }) .collect(); @@ -157,7 +164,7 @@ fn validate_changed_keys( } let all_keys_are_nam_balance = keys_changed .iter() - .all(|key| is_balance_key(nam_addr, key).is_some()); + .all(|key| TokenKeys::is_balance(nam_addr, key).is_some()); if !all_keys_are_nam_balance { let error = native_vp::Error::new_const( "Some modified keys were not a native token's balance key", @@ -174,28 +181,26 @@ mod tests { use std::cell::RefCell; use std::env::temp_dir; + use namada_core::address::testing::{established_address_1, nam, wnam}; use namada_core::borsh::BorshSerializeExt; - use namada_gas::TxGasMeter; + use namada_core::ethereum_events::EthAddress; + use namada_core::{ethereum_events, WasmCacheRwAccess}; + use namada_gas::{TxGasMeter, VpGasMeter}; use namada_state::testing::TestState; - use namada_state::StorageWrite; + use namada_state::{StorageWrite, TxIndex}; + use namada_trans_token::storage_key::{balance_key, minted_balance_key}; use namada_tx::data::TxType; use namada_tx::{Tx, TxCommitments}; + use namada_vm::wasm::run::VpEvalWasm; + use namada_vm::wasm::VpCache; use rand::Rng; use super::*; - use crate::address::testing::{established_address_1, nam, wnam}; - use crate::ethereum_bridge::storage::bridge_pool::BRIDGE_POOL_ADDRESS; - use crate::ethereum_bridge::storage::parameters::{ + use crate::storage::bridge_pool::BRIDGE_POOL_ADDRESS; + use crate::storage::parameters::{ Contracts, EthereumBridgeParams, UpgradeableContract, }; - use crate::ethereum_bridge::storage::wrapped_erc20s; - use crate::ethereum_events; - use crate::ethereum_events::EthAddress; - use crate::ledger::gas::VpGasMeter; - use crate::storage::TxIndex; - use crate::token::storage_key::minted_balance_key; - use crate::vm::wasm::VpCache; - use crate::vm::WasmCacheRwAccess; + use crate::storage::wrapped_erc20s; const ARBITRARY_OWNER_A_ADDRESS: &str = "tnam1qqwuj7aart6ackjfkk7486jwm2ufr4t7cq4535u4"; @@ -203,6 +208,14 @@ mod tests { const ESCROW_AMOUNT: u64 = 100; const BRIDGE_POOL_ESCROW_INITIAL_BALANCE: u64 = 0; + type CA = WasmCacheRwAccess; + type Eval = VpEvalWasm< + ::D, + ::H, + CA, + >; + type TokenKeys = namada_token::Store<()>; + /// Return some arbitrary random key belonging to this account fn arbitrary_key() -> Key { let mut rng = rand::thread_rng(); @@ -254,9 +267,9 @@ mod tests { gas_meter: &'a RefCell, keys_changed: &'a BTreeSet, verifiers: &'a BTreeSet
, - ) -> Ctx<'a, TestState, WasmCacheRwAccess> { + ) -> Ctx<'a, TestState, VpCache, Eval> { Ctx::new( - &crate::ethereum_bridge::ADDRESS, + &crate::ADDRESS, state, tx, cmt, @@ -272,10 +285,10 @@ mod tests { fn test_accepts_expected_keys_changed() { let keys_changed = BTreeSet::from([ balance_key(&nam(), &established_address_1()), - balance_key(&nam(), &crate::ethereum_bridge::ADDRESS), + balance_key(&nam(), &crate::ADDRESS), ]); - let result = validate_changed_keys(&nam(), &keys_changed); + let result = validate_changed_keys::(&nam(), &keys_changed); assert!(result.is_ok()); } @@ -284,7 +297,7 @@ mod tests { fn test_error_if_triggered_without_keys_changed() { let keys_changed = BTreeSet::new(); - let result = validate_changed_keys(&nam(), &keys_changed); + let result = validate_changed_keys::(&nam(), &keys_changed); assert!(result.is_err()); } @@ -294,7 +307,8 @@ mod tests { { let keys_changed = BTreeSet::from_iter(vec![arbitrary_key(); 3]); - let result = validate_changed_keys(&nam(), &keys_changed); + let result = + validate_changed_keys::(&nam(), &keys_changed); assert!(result.is_err()); } @@ -305,7 +319,8 @@ mod tests { arbitrary_key(), ]); - let result = validate_changed_keys(&nam(), &keys_changed); + let result = + validate_changed_keys::(&nam(), &keys_changed); assert!(result.is_err()); } @@ -317,7 +332,8 @@ mod tests { let keys_changed = BTreeSet::from_iter(vec![arbitrary_key(), arbitrary_key()]); - let result = validate_changed_keys(&nam(), &keys_changed); + let result = + validate_changed_keys::(&nam(), &keys_changed); assert!(result.is_err()); } @@ -330,7 +346,8 @@ mod tests { )), ]); - let result = validate_changed_keys(&nam(), &keys_changed); + let result = + validate_changed_keys::(&nam(), &keys_changed); assert!(result.is_err()); } @@ -347,7 +364,8 @@ mod tests { ), ]); - let result = validate_changed_keys(&nam(), &keys_changed); + let result = + validate_changed_keys::(&nam(), &keys_changed); assert!(result.is_err()); } @@ -372,7 +390,7 @@ mod tests { .expect("Test failed"); // credit the balance to the escrow - let escrow_key = balance_key(&nam(), &crate::ethereum_bridge::ADDRESS); + let escrow_key = balance_key(&nam(), &crate::ADDRESS); state .write_log_mut() .write( @@ -403,6 +421,7 @@ mod tests { &keys_changed, &verifiers, ), + token_keys: PhantomData::, }; let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers); @@ -428,7 +447,7 @@ mod tests { .expect("Test failed"); // do not credit the balance to the escrow - let escrow_key = balance_key(&nam(), &crate::ethereum_bridge::ADDRESS); + let escrow_key = balance_key(&nam(), &crate::ADDRESS); state .write_log_mut() .write( @@ -457,6 +476,7 @@ mod tests { &keys_changed, &verifiers, ), + token_keys: PhantomData::, }; let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers); @@ -483,7 +503,7 @@ mod tests { .expect("Test failed"); // credit the balance to the escrow - let escrow_key = balance_key(&nam(), &crate::ethereum_bridge::ADDRESS); + let escrow_key = balance_key(&nam(), &crate::ADDRESS); state .write_log_mut() .write( @@ -514,6 +534,7 @@ mod tests { &keys_changed, &verifiers, ), + token_keys: PhantomData::, }; let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers); diff --git a/crates/ethereum_bridge/src/vp/mod.rs b/crates/ethereum_bridge/src/vp/mod.rs index 250d51d1b5..e16646f73e 100644 --- a/crates/ethereum_bridge/src/vp/mod.rs +++ b/crates/ethereum_bridge/src/vp/mod.rs @@ -2,6 +2,10 @@ //! This includes both the bridge vp and the vp for the bridge //! pool. -pub mod bridge_pool_vp; -pub mod nut; -pub mod vp; +mod bridge_pool_vp; +mod eth_bridge_vp; +mod nut_vp; + +pub use bridge_pool_vp::BridgePool; +pub use eth_bridge_vp::EthBridge; +pub use nut_vp::NonUsableTokens; diff --git a/crates/ethereum_bridge/src/vp/nut.rs b/crates/ethereum_bridge/src/vp/nut_vp.rs similarity index 82% rename from crates/ethereum_bridge/src/vp/nut.rs rename to crates/ethereum_bridge/src/vp/nut_vp.rs index 5100bbc3f0..4af12ec0e1 100644 --- a/crates/ethereum_bridge/src/vp/nut.rs +++ b/crates/ethereum_bridge/src/vp/nut_vp.rs @@ -1,18 +1,16 @@ //! Validity predicate for Non Usable Tokens (NUTs). use std::collections::BTreeSet; +use std::marker::PhantomData; use namada_core::address::{Address, InternalAddress}; use namada_core::booleans::BoolResultUnitExt; use namada_core::storage::Key; +use namada_core::token::{self, Amount}; use namada_state::StateRead; use namada_tx::BatchedTxRef; -use namada_vp_env::VpEnv; - -use crate::ledger::native_vp::{self, Ctx, NativeVp}; -use crate::token::storage_key::is_any_token_balance_key; -use crate::token::Amount; -use crate::vm::WasmCacheAccess; +use namada_vp::native_vp::{self, Ctx, NativeVp, VpEvaluator}; +use namada_vp::VpEnv; /// Generic error that may be returned by the validity predicate #[derive(thiserror::Error, Debug)] @@ -23,19 +21,24 @@ pub struct Error(#[from] native_vp::Error); /// /// All this VP does is reject NUT transfers whose destination /// address is not the Bridge pool escrow address. -pub struct NonUsableTokens<'ctx, S, CA> +pub struct NonUsableTokens<'ctx, S, CA, EVAL, TokenKeys> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'ctx, S, CA>, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Token keys type + pub token_keys: PhantomData, } -impl<'a, S, CA> NativeVp for NonUsableTokens<'a, S, CA> +impl<'a, S, CA, EVAL, TokenKeys> NativeVp<'a> + for NonUsableTokens<'a, S, CA, EVAL, TokenKeys> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, + TokenKeys: token::Keys, { type Error = Error; @@ -61,15 +64,14 @@ where error })?; - let nut_owners = - keys_changed.iter().filter_map( - |key| match is_any_token_balance_key(key) { - Some( - [Address::Internal(InternalAddress::Nut(_)), owner], - ) => Some((key, owner)), - _ => None, - }, - ); + let nut_owners = keys_changed.iter().filter_map(|key| { + match TokenKeys::is_any_token_balance(key) { + Some([Address::Internal(InternalAddress::Nut(_)), owner]) => { + Some((key, owner)) + } + _ => None, + } + }); for (changed_key, token_owner) in nut_owners { let pre: Amount = self @@ -134,18 +136,27 @@ mod test_nuts { use namada_core::borsh::BorshSerializeExt; use namada_core::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS; use namada_core::storage::TxIndex; - use namada_ethereum_bridge::storage::wrapped_erc20s; + use namada_core::WasmCacheRwAccess; + use namada_gas::{TxGasMeter, VpGasMeter}; use namada_state::testing::TestState; use namada_state::StorageWrite; + use namada_trans_token::storage_key::balance_key; use namada_tx::data::TxType; use namada_tx::Tx; + use namada_vm::wasm::run::VpEvalWasm; + use namada_vm::wasm::VpCache; use proptest::prelude::*; use super::*; - use crate::ledger::gas::{TxGasMeter, VpGasMeter}; - use crate::token::storage_key::balance_key; - use crate::vm::wasm::VpCache; - use crate::vm::WasmCacheRwAccess; + use crate::storage::wrapped_erc20s; + + type CA = WasmCacheRwAccess; + type Eval = VpEvalWasm< + ::D, + ::H, + CA, + >; + type TokenKeys = namada_token::Store<()>; /// Run a VP check on a NUT transfer between the two provided addresses. fn check_nut_transfer(src: Address, dst: Address) -> bool { @@ -203,7 +214,7 @@ mod test_nuts { &TxGasMeter::new(u64::MAX), )); let batched_tx = tx.batch_ref_first_tx().unwrap(); - let ctx = Ctx::<_, WasmCacheRwAccess>::new( + let ctx = Ctx::<_, VpCache, Eval>::new( &Address::Internal(InternalAddress::Nut(DAI_ERC20_ETH_ADDRESS)), &state, batched_tx.tx, @@ -214,7 +225,10 @@ mod test_nuts { &verifiers, VpCache::new(temp_dir(), 100usize), ); - let vp = NonUsableTokens { ctx }; + let vp = NonUsableTokens { + ctx, + token_keys: PhantomData::, + }; // print debug info in case we run into failures for key in &keys_changed { diff --git a/crates/trans_token/src/lib.rs b/crates/trans_token/src/lib.rs index 887e2ccfb1..497ba0dadf 100644 --- a/crates/trans_token/src/lib.rs +++ b/crates/trans_token/src/lib.rs @@ -42,4 +42,10 @@ impl Keys for Store { ) -> Option<&'a Address> { storage_key::is_balance_key(token_addr, key) } + + fn is_any_token_balance( + key: &namada_core::storage::Key, + ) -> Option<[&Address; 2]> { + storage_key::is_any_token_balance_key(key) + } } From 29c0a1a7c11046a172bc32699f540d2c12564770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 12 Jun 2024 17:19:21 +0100 Subject: [PATCH 16/41] mv crates/namada/src/ledger/native_vp/ibc crates/ibc/src/vp --- crates/{namada/src/ledger/native_vp/ibc => ibc/src/vp}/context.rs | 0 crates/{namada/src/ledger/native_vp/ibc => ibc/src/vp}/mod.rs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/native_vp/ibc => ibc/src/vp}/context.rs (100%) rename crates/{namada/src/ledger/native_vp/ibc => ibc/src/vp}/mod.rs (100%) diff --git a/crates/namada/src/ledger/native_vp/ibc/context.rs b/crates/ibc/src/vp/context.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/ibc/context.rs rename to crates/ibc/src/vp/context.rs diff --git a/crates/namada/src/ledger/native_vp/ibc/mod.rs b/crates/ibc/src/vp/mod.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/ibc/mod.rs rename to crates/ibc/src/vp/mod.rs From 7dcae1c3329f89bef32054d80afe25263eb35904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 14 Jun 2024 16:46:08 +0100 Subject: [PATCH 17/41] ibc: fix VP post move --- Cargo.lock | 8 +- crates/core/src/governance.rs | 13 + crates/core/src/lib.rs | 1 + crates/core/src/parameters.rs | 9 +- crates/core/src/proof_of_stake.rs | 3 + crates/core/src/token.rs | 51 ++- .../ethereum_bridge/src/vp/bridge_pool_vp.rs | 10 +- .../ethereum_bridge/src/vp/eth_bridge_vp.rs | 6 +- crates/ethereum_bridge/src/vp/nut_vp.rs | 2 +- crates/governance/src/lib.rs | 21 + crates/governance/src/vp/mod.rs | 6 +- crates/ibc/Cargo.toml | 11 + crates/ibc/src/actions.rs | 10 + crates/ibc/src/context/client.rs | 30 +- crates/ibc/src/context/common.rs | 172 +++++--- crates/ibc/src/context/storage.rs | 11 +- crates/ibc/src/context/validation.rs | 12 +- crates/ibc/src/lib.rs | 81 +++- crates/ibc/src/vp/context.rs | 212 ++++++---- crates/ibc/src/vp/mod.rs | 386 +++++++++++------- crates/namada/src/ledger/ibc/mod.rs | 81 ---- crates/parameters/src/lib.rs | 18 +- crates/proof_of_stake/src/lib.rs | 5 + crates/proof_of_stake/src/parameters.rs | 22 +- crates/proof_of_stake/src/storage.rs | 17 +- crates/state/src/lib.rs | 6 +- crates/trans_token/src/lib.rs | 54 ++- crates/tx_prelude/src/ibc.rs | 20 +- crates/vp/src/native_vp.rs | 2 +- wasm/Cargo.lock | 27 +- 30 files changed, 864 insertions(+), 443 deletions(-) create mode 100644 crates/core/src/governance.rs delete mode 100644 crates/namada/src/ledger/ibc/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f7cf48ee5e..9cd9687dce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4843,6 +4843,7 @@ dependencies = [ name = "namada_ibc" version = "0.41.0" dependencies = [ + "assert_matches", "borsh", "data-encoding", "ibc", @@ -4854,13 +4855,18 @@ dependencies = [ "masp_primitives", "namada_core", "namada_events", + "namada_gas", "namada_governance", "namada_macros", "namada_migrations", "namada_parameters", + "namada_proof_of_stake", "namada_state", "namada_storage", "namada_token", + "namada_tx", + "namada_vm", + "namada_vp", "primitive-types", "proptest", "prost 0.12.3", @@ -4956,10 +4962,8 @@ dependencies = [ "namada", "namada_apps_lib", "namada_migrations", - "namada_parameters", "namada_sdk", "namada_test_utils", - "namada_vm", "num-rational", "num-traits 0.2.17", "num256", diff --git a/crates/core/src/governance.rs b/crates/core/src/governance.rs new file mode 100644 index 0000000000..fb2d53970a --- /dev/null +++ b/crates/core/src/governance.rs @@ -0,0 +1,13 @@ +//! Governance abstract interfaces + +/// Abstract governance storage read interface +pub trait Read { + /// Storage error + type Err; + + /// Check if an accepted proposal is being executed + fn is_proposal_accepted( + storage: &S, + tx_data: &[u8], + ) -> Result; +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 4e2ff1734e..c4e178f3b9 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -19,6 +19,7 @@ pub mod arith; pub mod bytes; +pub mod governance; pub mod hints; pub mod proof_of_stake; mod wasm_cache; diff --git a/crates/core/src/parameters.rs b/crates/core/src/parameters.rs index 5e996f3b35..ce34756c50 100644 --- a/crates/core/src/parameters.rs +++ b/crates/core/src/parameters.rs @@ -17,7 +17,7 @@ use crate::storage; /// Abstract parameters storage keys interface pub trait Keys { /// Key for implicit VP - fn implicit_vp() -> storage::Key; + fn implicit_vp_key() -> storage::Key; } /// Abstract parameters storage read interface @@ -29,7 +29,12 @@ pub trait Read { fn read(storage: &S) -> Result; /// Read MASP epoch multiplier - fn read_masp_epoch_multiplier(storage: &S) -> Result; + fn masp_epoch_multiplier(storage: &S) -> Result; + + /// Read the the epoch duration parameter from store + fn epoch_duration_parameter( + storage: &S, + ) -> Result; } /// Abstract parameters storage write interface diff --git a/crates/core/src/proof_of_stake.rs b/crates/core/src/proof_of_stake.rs index ca289bec3e..f2a73c4fda 100644 --- a/crates/core/src/proof_of_stake.rs +++ b/crates/core/src/proof_of_stake.rs @@ -18,4 +18,7 @@ pub trait Read { address: &Address, epoch: Option, ) -> Result; + + /// Read PoS pipeline length parameter + fn pipeline_len(storage: &S) -> Result; } diff --git a/crates/core/src/token.rs b/crates/core/src/token.rs index cffb8c583b..d27e44f08d 100644 --- a/crates/core/src/token.rs +++ b/crates/core/src/token.rs @@ -21,21 +21,64 @@ use crate::storage; use crate::storage::{DbKeySeg, KeySeg}; use crate::uint::{self, Uint, I256}; -/// Abstract parameters token keys interface +/// Abstract token keys interface pub trait Keys { /// Key for transparent token balance - fn balance(token: &Address, owner: &Address) -> storage::Key; + fn balance_key(token: &Address, owner: &Address) -> storage::Key; /// Returns the owner address if the given storage key is a balance key for /// the given token. - fn is_balance<'a>( + fn is_balance_key<'a>( token_addr: &Address, key: &'a storage::Key, ) -> Option<&'a Address>; /// Check if the given storage key is a balance key for an unspecified /// token. If it is, return the token and owner address. - fn is_any_token_balance(key: &storage::Key) -> Option<[&Address; 2]>; + fn is_any_token_balance_key(key: &storage::Key) -> Option<[&Address; 2]>; + + /// Obtain a storage key for the multitoken minter. + fn minter_key(token_addr: &Address) -> storage::Key; +} + +/// Abstract token storage read interface +pub trait Read { + /// Storage error + type Err; +} + +/// Abstract token storage write interface +pub trait Write: Read { + /// Transfer `token` from `src` to `dest`. Returns an `Err` if `src` has + /// insufficient balance or if the transfer the `dest` would overflow (This + /// can only happen if the total supply doesn't fit in `token::Amount`). + fn transfer( + storage: &mut S, + token: &Address, + src: &Address, + dest: &Address, + amount: Amount, + ) -> Result<(), Self::Err>; + + /// Burn a specified amount of tokens from some address. If the burn amount + /// is larger than the total balance of the given address, then the + /// remaining balance is burned. The total supply of the token is + /// properly adjusted. + fn burn_tokens( + storage: &mut S, + token: &Address, + source: &Address, + amount: Amount, + ) -> Result<(), Self::Err>; + + /// Credit tokens to an account, to be used only by protocol. In + /// transactions, this would get rejected by the default `vp_token`. + fn credit_tokens( + storage: &mut S, + token: &Address, + dest: &Address, + amount: Amount, + ) -> Result<(), Self::Err>; } /// Amount in micro units. For different granularity another representation diff --git a/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs b/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs index f3b0a6c585..a75789da03 100644 --- a/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs +++ b/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs @@ -89,7 +89,7 @@ where token: &Address, address: &Address, ) -> Result, Error> { - let account_key = TokenKeys::balance(token, address); + let account_key = TokenKeys::balance_key(token, address); let before: Amount = (&self.ctx) .read_pre_value(&account_key) .map_err(|error| { @@ -437,8 +437,8 @@ impl EscrowDelta<'_, KIND> { .. } = self; - let owner_key = TokenKeys::balance(token, payer_account); - let escrow_key = TokenKeys::balance(token, escrow_account); + let owner_key = TokenKeys::balance_key(token, payer_account); + let escrow_key = TokenKeys::balance_key(token, escrow_account); changed_keys.contains(&owner_key) && changed_keys.contains(&escrow_key) } @@ -456,8 +456,8 @@ impl EscrowDelta<'_, KIND> { .. } = self; - let owner_key = TokenKeys::balance(token, payer_account); - let escrow_key = TokenKeys::balance(token, escrow_account); + let owner_key = TokenKeys::balance_key(token, payer_account); + let escrow_key = TokenKeys::balance_key(token, escrow_account); !changed_keys.contains(&owner_key) && !changed_keys.contains(&escrow_key) diff --git a/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs b/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs index 7340f6dd6d..1cada0f280 100644 --- a/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs +++ b/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs @@ -43,7 +43,7 @@ where /// that the NAM balance increased and that the Bridge pool VP has /// been triggered. fn check_escrow(&self, verifiers: &BTreeSet
) -> Result<(), Error> { - let escrow_key = TokenKeys::balance( + let escrow_key = TokenKeys::balance_key( &self.ctx.state.in_mem().native_token, &crate::ADDRESS, ); @@ -138,7 +138,7 @@ fn validate_changed_keys( .filter(|&key| { let changes_eth_storage = storage::has_eth_addr_segment(key); let changes_nam_balance = - TokenKeys::is_balance(nam_addr, key).is_some(); + TokenKeys::is_balance_key(nam_addr, key).is_some(); changes_nam_balance || changes_eth_storage }) .collect(); @@ -164,7 +164,7 @@ fn validate_changed_keys( } let all_keys_are_nam_balance = keys_changed .iter() - .all(|key| TokenKeys::is_balance(nam_addr, key).is_some()); + .all(|key| TokenKeys::is_balance_key(nam_addr, key).is_some()); if !all_keys_are_nam_balance { let error = native_vp::Error::new_const( "Some modified keys were not a native token's balance key", diff --git a/crates/ethereum_bridge/src/vp/nut_vp.rs b/crates/ethereum_bridge/src/vp/nut_vp.rs index 4af12ec0e1..90a9fb0d34 100644 --- a/crates/ethereum_bridge/src/vp/nut_vp.rs +++ b/crates/ethereum_bridge/src/vp/nut_vp.rs @@ -65,7 +65,7 @@ where })?; let nut_owners = keys_changed.iter().filter_map(|key| { - match TokenKeys::is_any_token_balance(key) { + match TokenKeys::is_any_token_balance_key(key) { Some([Address::Internal(InternalAddress::Nut(_)), owner]) => { Some((key, owner)) } diff --git a/crates/governance/src/lib.rs b/crates/governance/src/lib.rs index febe58ee5f..e1ef6ebcbc 100644 --- a/crates/governance/src/lib.rs +++ b/crates/governance/src/lib.rs @@ -17,6 +17,8 @@ clippy::print_stderr )] +use std::marker::PhantomData; + use namada_core::address::{self, Address}; /// governance CLI structures @@ -32,9 +34,28 @@ pub mod storage; pub mod utils; pub mod vp; +use namada_state::StorageRead; pub use storage::proposal::{InitProposalData, ProposalType, VoteProposalData}; pub use storage::vote::ProposalVote; pub use storage::{init_proposal, is_proposal_accepted, vote_proposal}; /// The governance internal address pub const ADDRESS: Address = address::GOV; + +/// Governance storage `Keys/Read/Write` implementation +#[derive(Debug)] +pub struct Store(PhantomData); + +impl namada_core::governance::Read for Store +where + S: StorageRead, +{ + type Err = namada_state::StorageError; + + fn is_proposal_accepted( + storage: &S, + tx_data: &[u8], + ) -> Result { + storage::is_proposal_accepted(storage, tx_data) + } +} diff --git a/crates/governance/src/vp/mod.rs b/crates/governance/src/vp/mod.rs index 39f6c27bff..7631811a6d 100644 --- a/crates/governance/src/vp/mod.rs +++ b/crates/governance/src/vp/mod.rs @@ -885,7 +885,7 @@ where ) -> Result<()> { let funds_key = gov_storage::get_funds_key(proposal_id); let balance_key = - TokenKeys::balance(native_token_address, self.ctx.address); + TokenKeys::balance_key(native_token_address, self.ctx.address); let min_funds_parameter_key = gov_storage::get_min_proposal_fund_key(); let min_funds_parameter: token::Amount = @@ -950,7 +950,7 @@ where /// Validate a balance key fn is_valid_balance(&self, native_token_address: &Address) -> Result<()> { let balance_key = - TokenKeys::balance(native_token_address, self.ctx.address); + TokenKeys::balance_key(native_token_address, self.ctx.address); let min_funds_parameter_key = gov_storage::get_min_proposal_fund_key(); let pre_balance: Option = @@ -1221,7 +1221,7 @@ impl KeyType { KeyType::COUNTER } else if gov_storage::is_parameter_key(key) { KeyType::PARAMETER - } else if TokenKeys::is_balance(native_token, key).is_some() { + } else if TokenKeys::is_balance_key(native_token, key).is_some() { KeyType::BALANCE } else if gov_storage::is_governance_key(key) { KeyType::UNKNOWN_GOVERNANCE diff --git a/crates/ibc/Cargo.toml b/crates/ibc/Cargo.toml index e73160983f..d79d51ba9c 100644 --- a/crates/ibc/Cargo.toml +++ b/crates/ibc/Cargo.toml @@ -23,6 +23,7 @@ testing = ["namada_core/testing", "ibc-testkit", "proptest"] [dependencies] namada_core = { path = "../core" } namada_events = { path = "../events", default-features = false } +namada_gas = { path = "../gas" } namada_governance = { path = "../governance" } namada_macros = {path = "../macros"} namada_migrations = {path = "../migrations", optional = true} @@ -30,6 +31,8 @@ namada_parameters = { path = "../parameters" } namada_state = { path = "../state" } namada_storage = { path = "../storage" } namada_token = { path = "../token" } +namada_tx = { path = "../tx" } +namada_vp = { path = "../vp" } borsh.workspace = true data-encoding.workspace = true @@ -51,5 +54,13 @@ thiserror.workspace = true tracing.workspace = true [dev-dependencies] +namada_core = { path = "../core", features = ["testing"] } +namada_parameters = { path = "../parameters", features = ["testing"] } +namada_proof_of_stake = { path = "../proof_of_stake", features = ["testing"] } +namada_state = { path = "../state", features = ["testing"] } +namada_tx = { path = "../tx", features = ["testing"] } +namada_vm = { path = "../vm", features = ["testing"] } + +assert_matches.workspace = true ibc-testkit.workspace = true proptest.workspace = true diff --git a/crates/ibc/src/actions.rs b/crates/ibc/src/actions.rs index 8acc41623c..10390b8397 100644 --- a/crates/ibc/src/actions.rs +++ b/crates/ibc/src/actions.rs @@ -117,6 +117,16 @@ impl IbcStorageContext for IbcProtocolContext<'_, S> where S: State + EmitEvents, { + type Storage = Self; + + fn storage(&self) -> &Self::Storage { + self + } + + fn storage_mut(&mut self) -> &mut Self::Storage { + self + } + fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), StorageError> { // There's no gas cost for protocol, we can ignore result self.state.write_log_mut().emit_event(event); diff --git a/crates/ibc/src/context/client.rs b/crates/ibc/src/context/client.rs index ab34d71832..5d865f3d5b 100644 --- a/crates/ibc/src/context/client.rs +++ b/crates/ibc/src/context/client.rs @@ -8,9 +8,9 @@ use ibc::clients::tendermint::types::{ use ibc::core::client::types::error::ClientError; use ibc::primitives::proto::Any; use ibc_derive::{IbcClientState, IbcConsensusState}; -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] use ibc_testkit::testapp::ibc::clients::mock::client_state::MockClientState; -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] use ibc_testkit::testapp::ibc::clients::mock::consensus_state::MockConsensusState; use prost::Message; @@ -25,7 +25,7 @@ pub enum AnyClientState { /// Tendermint client state Tendermint(TmClientState), - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] /// Mock client state for testing Mock(MockClientState), } @@ -48,7 +48,7 @@ impl TryFrom for TmClientState { fn try_from(any: AnyClientState) -> Result { match any { AnyClientState::Tendermint(cs) => Ok(cs), - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] AnyClientState::Mock(_) => { Err(ClientError::UnknownConsensusStateType { consensus_state_type: "mock".to_string(), @@ -58,14 +58,14 @@ impl TryFrom for TmClientState { } } -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] impl From for AnyClientState { fn from(cs: MockClientState) -> Self { Self::Mock(cs) } } -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] impl TryFrom for MockClientState { type Error = ClientError; @@ -85,7 +85,7 @@ impl From for Any { fn from(client_state: AnyClientState) -> Self { match client_state { AnyClientState::Tendermint(cs) => cs.into(), - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] AnyClientState::Mock(cs) => cs.into(), } } @@ -95,7 +95,7 @@ impl TryFrom for AnyClientState { type Error = ClientError; fn try_from(client_state: Any) -> Result { - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] if let Ok(cs) = MockClientState::try_from(client_state.clone()) { return Ok(cs.into()); } @@ -115,7 +115,7 @@ pub enum AnyConsensusState { /// Tendermint consensus state Tendermint(TmConsensusState), - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] /// Mock consensus state for testing Mock(MockConsensusState), } @@ -138,7 +138,7 @@ impl TryFrom for TmConsensusStateType { fn try_from(any: AnyConsensusState) -> Result { match any { AnyConsensusState::Tendermint(c) => Ok(c.inner().clone()), - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] AnyConsensusState::Mock(_) => { Err(ClientError::UnknownConsensusStateType { consensus_state_type: "mock".to_string(), @@ -148,7 +148,7 @@ impl TryFrom for TmConsensusStateType { } } -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] impl From for AnyConsensusState { fn from(cs: MockConsensusState) -> Self { Self::Mock(cs) @@ -161,7 +161,7 @@ impl TryFrom for TmConsensusState { fn try_from(any: AnyConsensusState) -> Result { match any { AnyConsensusState::Tendermint(cs) => Ok(cs), - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] _ => Err(ClientError::UnknownConsensusStateType { consensus_state_type: "Only Tendermint client state type is \ supported" @@ -171,7 +171,7 @@ impl TryFrom for TmConsensusState { } } -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] impl TryFrom for MockConsensusState { type Error = ClientError; @@ -190,7 +190,7 @@ impl From for Any { fn from(consensus_state: AnyConsensusState) -> Self { match consensus_state { AnyConsensusState::Tendermint(cs) => cs.into(), - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] AnyConsensusState::Mock(cs) => cs.into(), } } @@ -200,7 +200,7 @@ impl TryFrom for AnyConsensusState { type Error = ClientError; fn try_from(consensus_state: Any) -> Result { - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] if let Ok(cs) = MockConsensusState::try_from(consensus_state.clone()) { return Ok(cs.into()); } diff --git a/crates/ibc/src/context/common.rs b/crates/ibc/src/context/common.rs index 46a6c1cbba..d2790e609e 100644 --- a/crates/ibc/src/context/common.rs +++ b/crates/ibc/src/context/common.rs @@ -22,7 +22,7 @@ use ibc::primitives::Timestamp; use namada_core::address::Address; use namada_core::storage::{BlockHeight, Key}; use namada_core::tendermint::Time as TmTime; -use namada_storage::{Error as StorageError, StorageRead}; +use namada_state::{StorageError, StorageRead, StorageWrite}; use namada_token::storage_key::balance_key; use namada_token::Amount; use prost::Message; @@ -39,7 +39,7 @@ pub trait IbcCommonContext: IbcStorageContext { /// Get the ClientState fn client_state(&self, client_id: &ClientId) -> Result { let key = storage::client_state_key(client_id); - match self.read_bytes(&key)? { + match self.storage().read_bytes(&key)? { Some(value) => Any::decode(&value[..]) .map_err(|e| ClientError::Other { description: e.to_string(), @@ -61,7 +61,9 @@ pub trait IbcCommonContext: IbcStorageContext { ) -> Result<()> { let key = storage::client_state_key(client_id); let bytes = Any::from(client_state).encode_to_vec(); - self.write_bytes(&key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(&key, bytes) + .map_err(ContextError::from) } /// Get the ConsensusState @@ -71,7 +73,7 @@ pub trait IbcCommonContext: IbcStorageContext { height: Height, ) -> Result { let key = storage::consensus_state_key(client_id, height); - match self.read_bytes(&key)? { + match self.storage().read_bytes(&key)? { Some(value) => Any::decode(&value[..]) .map_err(|e| ClientError::Other { description: e.to_string(), @@ -95,7 +97,9 @@ pub trait IbcCommonContext: IbcStorageContext { ) -> Result<()> { let key = storage::consensus_state_key(client_id, height); let bytes = Any::from(consensus_state).encode_to_vec(); - self.write_bytes(&key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(&key, bytes) + .map_err(ContextError::from) } /// Delete the ConsensusState @@ -105,7 +109,7 @@ pub trait IbcCommonContext: IbcStorageContext { height: Height, ) -> Result<()> { let key = storage::consensus_state_key(client_id, height); - self.delete(&key).map_err(ContextError::from) + self.storage_mut().delete(&key).map_err(ContextError::from) } /// Decode ConsensusState from bytes @@ -127,9 +131,9 @@ pub trait IbcCommonContext: IbcStorageContext { client_id: &ClientId, ) -> Result> { let prefix = storage::consensus_state_prefix(client_id); - let mut iter = self.iter_prefix(&prefix)?; + let mut iter = self.storage().iter_prefix(&prefix)?; let mut heights = Vec::new(); - while let Some((key, _)) = self.iter_next(&mut iter)? { + while let Some((key, _)) = self.storage().iter_next(&mut iter)? { let key = Key::parse(key).expect("the key should be parsable"); let height = storage::consensus_height(&key).map_err(|e| { ClientError::Other { @@ -148,9 +152,9 @@ pub trait IbcCommonContext: IbcStorageContext { height: &Height, ) -> Result> { let prefix = storage::consensus_state_prefix(client_id); - let mut iter = self.iter_prefix(&prefix)?; + let mut iter = self.storage().iter_prefix(&prefix)?; let mut lowest_height_value = None; - while let Some((key, value)) = self.iter_next(&mut iter)? { + while let Some((key, value)) = self.storage().iter_next(&mut iter)? { let key = Key::parse(key).expect("the key should be parsable"); let consensus_height = storage::consensus_height(&key) .expect("the key should have a height"); @@ -177,9 +181,9 @@ pub trait IbcCommonContext: IbcStorageContext { ) -> Result> { let prefix = storage::consensus_state_prefix(client_id); // for iterator - let mut iter = self.iter_prefix(&prefix)?; + let mut iter = self.storage().iter_prefix(&prefix)?; let mut highest_height_value = None; - while let Some((key, value)) = self.iter_next(&mut iter)? { + while let Some((key, value)) = self.storage().iter_next(&mut iter)? { let key = Key::parse(key).expect("the key should be parsable"); let consensus_height = storage::consensus_height(&key) .expect("the key should have the height"); @@ -204,12 +208,13 @@ pub trait IbcCommonContext: IbcStorageContext { client_id: &ClientId, ) -> Result<(Timestamp, Height)> { let key = storage::client_update_timestamp_key(client_id); - let value = - self.read_bytes(&key)?.ok_or(ClientError::ClientSpecific { + let value = self.storage().read_bytes(&key)?.ok_or( + ClientError::ClientSpecific { description: format!( "The client update time doesn't exist: ID {client_id}", ), - })?; + }, + )?; let time = TmTime::decode_vec(&value) .map_err(|_| ClientError::Other { description: format!( @@ -219,7 +224,7 @@ pub trait IbcCommonContext: IbcStorageContext { .into(); let key = storage::client_update_height_key(client_id); - let value = self.read_bytes(&key)?.ok_or({ + let value = self.storage().read_bytes(&key)?.ok_or({ ClientError::ClientSpecific { description: format!( "The client update height doesn't exist: ID {client_id}", @@ -250,27 +255,33 @@ pub trait IbcCommonContext: IbcStorageContext { "The client timestamp is invalid: ID {client_id}", ), })?; - self.write_bytes(&key, time.encode_vec()) + self.storage_mut() + .write_bytes(&key, time.encode_vec()) .map_err(ContextError::from)?; let key = storage::client_update_height_key(client_id); let bytes = host_height.encode_vec(); - self.write_bytes(&key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(&key, bytes) + .map_err(ContextError::from) } /// Delete the client update time and height fn delete_update_meta(&mut self, client_id: &ClientId) -> Result<()> { let key = storage::client_update_timestamp_key(client_id); - self.delete(&key).map_err(ContextError::from)?; + self.storage_mut() + .delete(&key) + .map_err(ContextError::from)?; let key = storage::client_update_height_key(client_id); - self.delete(&key).map_err(ContextError::from) + self.storage_mut().delete(&key).map_err(ContextError::from) } /// Get the timestamp on this chain fn host_timestamp(&self) -> Result { - let height = self.get_block_height()?; + let height = self.storage().get_block_height()?; let header = self + .storage() .get_block_header(height)? .or({ if height > BlockHeight::first() { @@ -278,7 +289,8 @@ pub trait IbcCommonContext: IbcStorageContext { // `FinalizeBlock` phase, e.g. dry-run, use the previous // header's time. It should be OK though the constraints // become a bit stricter when checking timeouts. - self.get_block_header(height.prev_height().unwrap())? + self.storage() + .get_block_header(height.prev_height().unwrap())? } else { None } @@ -302,11 +314,12 @@ pub trait IbcCommonContext: IbcStorageContext { height: &Height, ) -> Result { let height = BlockHeight(height.revision_height()); - let header = self.get_block_header(height)?.ok_or_else(|| { - ContextError::from(ClientError::Other { - description: "No host header".to_string(), - }) - })?; + let header = + self.storage().get_block_header(height)?.ok_or_else(|| { + ContextError::from(ClientError::Other { + description: "No host header".to_string(), + }) + })?; let commitment_root = header.hash.to_vec().into(); let time = header .time @@ -325,7 +338,7 @@ pub trait IbcCommonContext: IbcStorageContext { /// Get the ConnectionEnd fn connection_end(&self, conn_id: &ConnectionId) -> Result { let key = storage::connection_key(conn_id); - let value = self.read_bytes(&key)?.ok_or( + let value = self.storage().read_bytes(&key)?.ok_or( ConnectionError::ConnectionNotFound { connection_id: conn_id.clone(), }, @@ -348,7 +361,9 @@ pub trait IbcCommonContext: IbcStorageContext { ) -> Result<()> { let key = storage::connection_key(connection_id); let bytes = connection_end.encode_vec(); - self.write_bytes(&key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(&key, bytes) + .map_err(ContextError::from) } /// Append the connection ID to the connection list of the client @@ -358,11 +373,13 @@ pub trait IbcCommonContext: IbcStorageContext { conn_id: ConnectionId, ) -> Result<()> { let key = storage::client_connections_key(client_id); - let list = match self.read::(&key)? { + let list = match self.storage().read::(&key)? { Some(list) => format!("{list},{conn_id}"), None => conn_id.to_string(), }; - self.write(&key, list).map_err(ContextError::from) + self.storage_mut() + .write(&key, list) + .map_err(ContextError::from) } /// Get the ChannelEnd @@ -372,12 +389,12 @@ pub trait IbcCommonContext: IbcStorageContext { channel_id: &ChannelId, ) -> Result { let key = storage::channel_key(port_id, channel_id); - let value = - self.read_bytes(&key)? - .ok_or(ChannelError::ChannelNotFound { - port_id: port_id.clone(), - channel_id: channel_id.clone(), - })?; + let value = self.storage().read_bytes(&key)?.ok_or( + ChannelError::ChannelNotFound { + port_id: port_id.clone(), + channel_id: channel_id.clone(), + }, + )?; ChannelEnd::decode_vec(&value).map_err(|_| { ChannelError::Other { description: format!( @@ -397,7 +414,9 @@ pub trait IbcCommonContext: IbcStorageContext { ) -> Result<()> { let key = storage::channel_key(port_id, channel_id); let bytes = channel_end.encode_vec(); - self.write_bytes(&key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(&key, bytes) + .map_err(ContextError::from) } /// Get the NextSequenceSend @@ -466,7 +485,9 @@ pub trait IbcCommonContext: IbcStorageContext { /// Store the sequence fn store_sequence(&mut self, key: &Key, sequence: Sequence) -> Result<()> { let bytes = u64::from(sequence).to_be_bytes().to_vec(); - self.write_bytes(key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(key, bytes) + .map_err(ContextError::from) } /// Get the packet commitment @@ -477,7 +498,7 @@ pub trait IbcCommonContext: IbcStorageContext { sequence: Sequence, ) -> Result { let key = storage::commitment_key(port_id, channel_id, sequence); - match self.read_bytes(&key)? { + match self.storage().read_bytes(&key)? { Some(value) => Ok(value.into()), None => { Err(PacketError::PacketCommitmentNotFound { sequence }.into()) @@ -495,7 +516,9 @@ pub trait IbcCommonContext: IbcStorageContext { ) -> Result<()> { let key = storage::commitment_key(port_id, channel_id, sequence); let bytes = commitment.into_vec(); - self.write_bytes(&key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(&key, bytes) + .map_err(ContextError::from) } /// Delete the packet commitment @@ -506,7 +529,7 @@ pub trait IbcCommonContext: IbcStorageContext { sequence: Sequence, ) -> Result<()> { let key = storage::commitment_key(port_id, channel_id, sequence); - self.delete(&key).map_err(ContextError::from) + self.storage_mut().delete(&key).map_err(ContextError::from) } /// Get the packet receipt @@ -517,7 +540,7 @@ pub trait IbcCommonContext: IbcStorageContext { sequence: Sequence, ) -> Result { let key = storage::receipt_key(port_id, channel_id, sequence); - match self.read_bytes(&key)? { + match self.storage().read_bytes(&key)? { Some(_) => Ok(Receipt::Ok), None => Err(PacketError::PacketReceiptNotFound { sequence }.into()), } @@ -533,7 +556,9 @@ pub trait IbcCommonContext: IbcStorageContext { let key = storage::receipt_key(port_id, channel_id, sequence); // the value is the same as ibc-go let bytes = [1_u8].to_vec(); - self.write_bytes(&key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(&key, bytes) + .map_err(ContextError::from) } /// Get the packet acknowledgement @@ -544,7 +569,7 @@ pub trait IbcCommonContext: IbcStorageContext { sequence: Sequence, ) -> Result { let key = storage::ack_key(port_id, channel_id, sequence); - match self.read_bytes(&key)? { + match self.storage().read_bytes(&key)? { Some(value) => Ok(value.into()), None => { Err(PacketError::PacketAcknowledgementNotFound { sequence } @@ -563,7 +588,9 @@ pub trait IbcCommonContext: IbcStorageContext { ) -> Result<()> { let key = storage::ack_key(port_id, channel_id, sequence); let bytes = ack_commitment.into_vec(); - self.write_bytes(&key, bytes).map_err(ContextError::from) + self.storage_mut() + .write_bytes(&key, bytes) + .map_err(ContextError::from) } /// Delete the packet acknowledgement @@ -574,12 +601,12 @@ pub trait IbcCommonContext: IbcStorageContext { sequence: Sequence, ) -> Result<()> { let key = storage::ack_key(port_id, channel_id, sequence); - self.delete(&key).map_err(ContextError::from) + self.storage_mut().delete(&key).map_err(ContextError::from) } /// Read a counter fn read_counter(&self, key: &Key) -> Result { - match self.read::(key)? { + match self.storage().read::(key)? { Some(counter) => Ok(counter), None => unreachable!("the counter should be initialized"), } @@ -592,7 +619,9 @@ pub trait IbcCommonContext: IbcStorageContext { u64::checked_add(count, 1).ok_or_else(|| ClientError::Other { description: format!("The counter overflow: Key {key}"), })?; - self.write(key, count).map_err(ContextError::from) + self.storage_mut() + .write(key, count) + .map_err(ContextError::from) } /// Write the IBC trace. The given address could be a non-Namada token. @@ -603,11 +632,16 @@ pub trait IbcCommonContext: IbcStorageContext { trace: impl AsRef, ) -> Result<()> { let key = storage::ibc_trace_key(addr, trace_hash.as_ref()); - let has_key = self.has_key(&key).map_err(|_| ChannelError::Other { - description: format!("Reading the IBC trace failed: Key {key}"), - })?; + let has_key = + self.storage() + .has_key(&key) + .map_err(|_| ChannelError::Other { + description: format!( + "Reading the IBC trace failed: Key {key}" + ), + })?; if !has_key { - self.write(&key, trace.as_ref()).map_err(|_| { + self.storage_mut().write(&key, trace.as_ref()).map_err(|_| { ChannelError::Other { description: format!( "Writing the trace failed: Key {key}", @@ -624,13 +658,15 @@ pub trait IbcCommonContext: IbcStorageContext { class_id: &PrefixedClassId, ) -> Result> { let key = storage::nft_class_key(class_id); - self.read(&key).map_err(ContextError::from) + self.storage().read(&key).map_err(ContextError::from) } /// Store the NFT class fn store_nft_class(&mut self, class: NftClass) -> Result<()> { let key = storage::nft_class_key(&class.class_id); - self.write(&key, class).map_err(ContextError::from) + self.storage_mut() + .write(&key, class) + .map_err(ContextError::from) } /// Get the NFT metadata @@ -640,14 +676,16 @@ pub trait IbcCommonContext: IbcStorageContext { token_id: &TokenId, ) -> Result> { let key = storage::nft_metadata_key(class_id, token_id); - self.read(&key).map_err(ContextError::from) + self.storage().read(&key).map_err(ContextError::from) } /// Store the NFT metadata fn store_nft_metadata(&mut self, metadata: NftMetadata) -> Result<()> { let key = storage::nft_metadata_key(&metadata.class_id, &metadata.token_id); - self.write(&key, metadata).map_err(ContextError::from) + self.storage_mut() + .write(&key, metadata) + .map_err(ContextError::from) } /// Return true if the NFT is owned by the owner @@ -659,14 +697,14 @@ pub trait IbcCommonContext: IbcStorageContext { ) -> Result { let ibc_token = trace::ibc_token_for_nft(class_id, token_id); let balance_key = balance_key(&ibc_token, owner); - let amount = self.read::(&balance_key)?; + let amount = self.storage().read::(&balance_key)?; Ok(amount == Some(Amount::from_u64(1))) } /// Read the mint amount of the given token fn mint_amount(&self, token: &Address) -> Result { let key = storage::mint_amount_key(token); - Ok(self.read::(&key)?.unwrap_or_default()) + Ok(self.storage().read::(&key)?.unwrap_or_default()) } /// Write the mint amount of the given token @@ -676,25 +714,29 @@ pub trait IbcCommonContext: IbcStorageContext { amount: Amount, ) -> Result<()> { let key = storage::mint_amount_key(token); - self.write(&key, amount).map_err(ContextError::from) + self.storage_mut() + .write(&key, amount) + .map_err(ContextError::from) } /// Read the per-epoch deposit of the given token fn deposit(&self, token: &Address) -> Result { let key = storage::deposit_key(token); - Ok(self.read::(&key)?.unwrap_or_default()) + Ok(self.storage().read::(&key)?.unwrap_or_default()) } /// Write the per-epoch deposit of the given token fn store_deposit(&mut self, token: &Address, amount: Amount) -> Result<()> { let key = storage::deposit_key(token); - self.write(&key, amount).map_err(ContextError::from) + self.storage_mut() + .write(&key, amount) + .map_err(ContextError::from) } /// Read the per-epoch withdraw of the given token fn withdraw(&self, token: &Address) -> Result { let key = storage::withdraw_key(token); - Ok(self.read::(&key)?.unwrap_or_default()) + Ok(self.storage().read::(&key)?.unwrap_or_default()) } /// Write the per-epoch withdraw of the given token @@ -704,7 +746,9 @@ pub trait IbcCommonContext: IbcStorageContext { amount: Amount, ) -> Result<()> { let key = storage::withdraw_key(token); - self.write(&key, amount).map_err(ContextError::from) + self.storage_mut() + .write(&key, amount) + .map_err(ContextError::from) } } diff --git a/crates/ibc/src/context/storage.rs b/crates/ibc/src/context/storage.rs index a33b2621fd..deb05b0470 100644 --- a/crates/ibc/src/context/storage.rs +++ b/crates/ibc/src/context/storage.rs @@ -8,7 +8,16 @@ use namada_storage::{Error, StorageRead, StorageWrite}; use crate::event::IbcEvent; /// IBC context trait to be implemented in integration that can read and write -pub trait IbcStorageContext: StorageRead + StorageWrite { +pub trait IbcStorageContext { + /// Storage read/write type + type Storage: StorageRead + StorageWrite; + + /// Read-only storage access + fn storage(&self) -> &Self::Storage; + + /// Read/write storage access + fn storage_mut(&mut self) -> &mut Self::Storage; + /// Emit an IBC event fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), Error>; diff --git a/crates/ibc/src/context/validation.rs b/crates/ibc/src/context/validation.rs index f51ccf7cbf..a77db4d149 100644 --- a/crates/ibc/src/context/validation.rs +++ b/crates/ibc/src/context/validation.rs @@ -24,8 +24,9 @@ use ibc::core::host::types::path::{ use ibc::core::host::ValidationContext; use ibc::cosmos_host::ValidateSelfClientContext; use ibc::primitives::{Signer, Timestamp}; -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] use ibc_testkit::testapp::ibc::clients::mock::client_state::MockClientState; +use namada_state::StorageRead; use super::client::{AnyClientState, AnyConsensusState}; use super::common::IbcCommonContext; @@ -70,9 +71,9 @@ where } } -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] use ibc_testkit::testapp::ibc::clients::mock::client_state::MockClientContext; -#[cfg(feature = "testing")] +#[cfg(any(test, feature = "testing"))] impl MockClientContext for IbcContext where C: IbcCommonContext, @@ -135,7 +136,7 @@ where } fn host_height(&self) -> Result { - let height = self.inner.borrow().get_block_height()?; + let height = self.inner.borrow().storage().get_block_height()?; // the revision number is always 0 Height::new(0, height.0).map_err(ContextError::ClientError) } @@ -167,7 +168,7 @@ where &self, client_state_of_host_on_counterparty: Self::HostClientState, ) -> Result<(), ContextError> { - #[cfg(feature = "testing")] + #[cfg(any(test, feature = "testing"))] { if MockClientState::try_from( client_state_of_host_on_counterparty.clone(), @@ -308,6 +309,7 @@ where let height = self .inner .borrow() + .storage() .get_block_height() .expect("The height should exist"); Height::new(0, height.0).expect("The conversion shouldn't fail") diff --git a/crates/ibc/src/lib.rs b/crates/ibc/src/lib.rs index 72a9101792..f915d436cd 100644 --- a/crates/ibc/src/lib.rs +++ b/crates/ibc/src/lib.rs @@ -25,6 +25,7 @@ mod nft; pub mod parameters; pub mod storage; pub mod trace; +pub mod vp; use std::cell::RefCell; use std::collections::BTreeSet; @@ -77,12 +78,22 @@ use masp_primitives::transaction::Transaction as MaspTransaction; pub use msg::*; use namada_core::address::{self, Address}; use namada_core::arith::checked; -use namada_storage::{Error as StorageError, StorageRead}; +use namada_core::token::Amount; +use namada_events::EmitEvents; +use namada_state::{ + DBIter, Key, State, StorageError, StorageHasher, StorageRead, StorageWrite, + WlState, DB, +}; use namada_token::Transfer; pub use nft::*; use prost::Message; use thiserror::Error; +use crate::storage::{ + channel_counter_key, client_counter_key, connection_counter_key, + deposit_prefix, withdraw_prefix, +}; + /// The event type defined in ibc-rs for receiving a token pub const EVENT_TYPE_PACKET: &str = "fungible_token_packet"; /// The event type defined in ibc-rs for receiving an NFT @@ -438,6 +449,74 @@ pub fn received_ibc_token( .map_err(|e| Error::Trace(format!("Invalid base token: {e}"))) } +/// Initialize storage in the genesis block. +pub fn init_genesis_storage(storage: &mut S) +where + S: State, +{ + // In ibc-go, u64 like a counter is encoded with big-endian: + // https://github.com/cosmos/ibc-go/blob/89ffaafb5956a5ea606e1f1bf249c880bea802ed/modules/core/04-channel/keeper/keeper.go#L115 + + let init_value = 0_u64; + + // the client counter + let key = client_counter_key(); + storage + .write(&key, init_value) + .expect("Unable to write the initial client counter"); + + // the connection counter + let key = connection_counter_key(); + storage + .write(&key, init_value) + .expect("Unable to write the initial connection counter"); + + // the channel counter + let key = channel_counter_key(); + storage + .write(&key, init_value) + .expect("Unable to write the initial channel counter"); +} + +/// Update IBC-related data when finalizing block +pub fn finalize_block( + state: &mut WlState, + _events: &mut impl EmitEvents, + is_new_epoch: bool, +) -> Result<(), StorageError> +where + D: 'static + DB + for<'iter> DBIter<'iter> + Sync, + H: 'static + StorageHasher + Sync, +{ + if is_new_epoch { + clear_throughputs(state)?; + } + Ok(()) +} + +/// Clear the per-epoch throughputs (deposit and withdraw) +fn clear_throughputs( + state: &mut WlState, +) -> Result<(), StorageError> +where + D: 'static + DB + for<'iter> DBIter<'iter> + Sync, + H: 'static + StorageHasher + Sync, +{ + for prefix in [deposit_prefix(), withdraw_prefix()] { + let keys: Vec = state + .iter_prefix(&prefix)? + .map(|(key, _, _)| { + Key::parse(key).expect("The key should be parsable") + }) + .collect(); + for key in keys { + state.write(&key, Amount::from(0))?; + } + } + + Ok(()) +} + #[cfg(any(test, feature = "testing"))] /// Testing helpers ans strategies for IBC pub mod testing { diff --git a/crates/ibc/src/vp/context.rs b/crates/ibc/src/vp/context.rs index 0baaad84c1..4b43236a3c 100644 --- a/crates/ibc/src/vp/context.rs +++ b/crates/ibc/src/vp/context.rs @@ -1,64 +1,83 @@ //! Contexts for IBC validity predicate use std::collections::BTreeSet; +use std::marker::PhantomData; -use borsh_ext::BorshSerializeExt; +use namada_core::address::{Address, InternalAddress}; use namada_core::arith::checked; +use namada_core::borsh::BorshSerializeExt; use namada_core::collections::{HashMap, HashSet}; -use namada_core::storage::Epochs; +use namada_core::storage::{BlockHeight, Epoch, Epochs, Header, Key, TxIndex}; +use namada_core::token::{self, Amount}; +use namada_events::Event; use namada_gas::MEMORY_ACCESS_GAS_PER_BYTE; -use namada_ibc::event::IbcEvent; -use namada_ibc::{IbcCommonContext, IbcStorageContext}; -use namada_sdk::events::Event; -use namada_state::{StateRead, StorageError, StorageRead, StorageWrite}; -use namada_vp_env::VpEnv; - -use crate::address::{Address, InternalAddress}; -use crate::ledger::ibc::storage::is_ibc_key; -use crate::ledger::native_vp::CtxPreStorageRead; -use crate::state::write_log::StorageModification; -use crate::state::PrefixIter; -use crate::storage::{BlockHeight, Epoch, Header, Key, TxIndex}; -use crate::token::{ - self as token, burn_tokens, credit_tokens, transfer, Amount, +use namada_state::write_log::StorageModification; +use namada_state::{ + PrefixIter, StateRead, StorageError, StorageRead, StorageWrite, }; -use crate::vm::WasmCacheAccess; +use namada_vp::native_vp::{CtxPreStorageRead, VpEvaluator}; +use namada_vp::VpEnv; + +use crate::event::IbcEvent; +use crate::storage::is_ibc_key; +use crate::{IbcCommonContext, IbcStorageContext}; /// Result of a storage API call. pub type Result = std::result::Result; /// Pseudo execution environment context for ibc native vp #[derive(Debug)] -pub struct PseudoExecutionContext<'view, 'a, S, CA> +pub struct PseudoExecutionContext<'view, 'a, S, CA, EVAL, Token> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, +{ + /// Execution context and storage + pub storage: PseudoExecutionStorage<'view, 'a, S, CA, EVAL>, + /// Token type + pub token: PhantomData, +} + +/// Pseudo execution environment context storage for ibc native vp +#[derive(Debug)] +pub struct PseudoExecutionStorage<'view, 'a, S, CA, EVAL> +where + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Temporary store for pseudo execution store: HashMap, /// Context to read the previous value - ctx: CtxPreStorageRead<'view, 'a, S, CA>, + ctx: CtxPreStorageRead<'view, 'a, S, CA, EVAL>, /// IBC event pub event: BTreeSet, } -impl<'view, 'a, S, CA> PseudoExecutionContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL, Token> + PseudoExecutionContext<'view, 'a, S, CA, EVAL, Token> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Generate new pseudo execution context - pub fn new(ctx: CtxPreStorageRead<'view, 'a, S, CA>) -> Self { + pub fn new(ctx: CtxPreStorageRead<'view, 'a, S, CA, EVAL>) -> Self { Self { - store: HashMap::new(), - ctx, - event: BTreeSet::new(), + storage: PseudoExecutionStorage { + store: HashMap::new(), + ctx, + event: BTreeSet::new(), + }, + token: PhantomData, } } /// Get the set of changed keys pub(crate) fn get_changed_keys(&self) -> HashSet<&Key> { - self.store.keys().filter(|k| is_ibc_key(k)).collect() + self.storage + .store + .keys() + .filter(|k| is_ibc_key(k)) + .collect() } /// Get the changed value @@ -66,16 +85,19 @@ where &self, key: &Key, ) -> Option<&StorageModification> { - self.store.get(key) + self.storage.store.get(key) } } -impl<'view, 'a, S, CA> StorageRead for PseudoExecutionContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL> StorageRead + for PseudoExecutionStorage<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, { - type PrefixIter<'iter> = PrefixIter<'iter, ::D> where Self: 'iter; + type PrefixIter<'iter> = PrefixIter<'iter, ::D> where +Self: 'iter; fn read_bytes(&self, key: &Key) -> Result>> { match self.store.get(key) { @@ -155,10 +177,12 @@ where } } -impl<'view, 'a, S, CA> StorageWrite for PseudoExecutionContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL> StorageWrite + for PseudoExecutionStorage<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, { fn write_bytes( &mut self, @@ -183,14 +207,30 @@ where } } -impl<'view, 'a, S, CA> IbcStorageContext - for PseudoExecutionContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL, Token> IbcStorageContext + for PseudoExecutionContext<'view, 'a, S, CA, EVAL, Token> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + CA: 'static + Clone, + Token: token::Keys + + token::Write< + PseudoExecutionStorage<'view, 'a, S, CA, EVAL>, + Err = StorageError, + >, { + type Storage = PseudoExecutionStorage<'view, 'a, S, CA, EVAL>; + + fn storage(&self) -> &Self::Storage { + &self.storage + } + + fn storage_mut(&mut self) -> &mut Self::Storage { + &mut self.storage + } + fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<()> { - self.event.insert(event.into()); + self.storage.event.insert(event.into()); Ok(()) } @@ -201,7 +241,8 @@ where token: &Address, amount: Amount, ) -> Result<()> { - transfer(self, token, src, dest, amount) + let storage = self.storage_mut(); + Token::transfer(storage, token, src, dest, amount) } fn mint_token( @@ -210,10 +251,12 @@ where token: &Address, amount: Amount, ) -> Result<()> { - credit_tokens(self, token, target, amount)?; + let storage = self.storage_mut(); + Token::credit_tokens(storage, token, target, amount)?; - let minter_key = token::storage_key::minter_key(token); - self.write( + let minter_key = Token::minter_key(token); + StorageWrite::write( + storage, &minter_key, Address::Internal(InternalAddress::Ibc).serialize_to_vec(), ) @@ -225,7 +268,8 @@ where token: &Address, amount: Amount, ) -> Result<()> { - burn_tokens(self, token, target, amount) + let storage = self.storage_mut(); + Token::burn_tokens(storage, token, target, amount) } fn insert_verifier(&mut self, _verifier: &Address) -> Result<()> { @@ -237,40 +281,48 @@ where } } -impl<'view, 'a, S, CA> IbcCommonContext - for PseudoExecutionContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL, Token> IbcCommonContext + for PseudoExecutionContext<'view, 'a, S, CA, EVAL, Token> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + Token: token::Keys + + token::Write< + PseudoExecutionStorage<'view, 'a, S, CA, EVAL>, + Err = StorageError, + >, { } /// Ibc native vp validation context #[derive(Debug)] -pub struct VpValidationContext<'view, 'a, S, CA> +pub struct VpValidationContext<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Context to read the post value - ctx: CtxPreStorageRead<'view, 'a, S, CA>, + ctx: CtxPreStorageRead<'view, 'a, S, CA, EVAL>, } -impl<'view, 'a, S, CA> VpValidationContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL> VpValidationContext<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Generate a new ibc vp validation context - pub fn new(ctx: CtxPreStorageRead<'view, 'a, S, CA>) -> Self { + pub fn new(ctx: CtxPreStorageRead<'view, 'a, S, CA, EVAL>) -> Self { Self { ctx } } } -impl<'view, 'a, S, CA> StorageRead for VpValidationContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL> StorageRead + for VpValidationContext<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, { type PrefixIter<'iter> = PrefixIter<'iter, ::D> where Self: 'iter; @@ -325,10 +377,12 @@ where } } -impl<'view, 'a, S, CA> StorageWrite for VpValidationContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL> StorageWrite + for VpValidationContext<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { fn write_bytes( &mut self, @@ -343,12 +397,23 @@ where } } -impl<'view, 'a, S, CA> IbcStorageContext - for VpValidationContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL> IbcStorageContext + for VpValidationContext<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, { + type Storage = Self; + + fn storage(&self) -> &Self::Storage { + self + } + + fn storage_mut(&mut self) -> &mut Self::Storage { + self + } + fn emit_ibc_event(&mut self, _event: IbcEvent) -> Result<()> { unimplemented!("Validation doesn't emit an event") } @@ -391,10 +456,11 @@ where } } -impl<'view, 'a, S, CA> IbcCommonContext - for VpValidationContext<'view, 'a, S, CA> +impl<'view, 'a, S, CA, EVAL> IbcCommonContext + for VpValidationContext<'view, 'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, { } diff --git a/crates/ibc/src/vp/mod.rs b/crates/ibc/src/vp/mod.rs index 3373f0dbf0..8c022591c0 100644 --- a/crates/ibc/src/vp/mod.rs +++ b/crates/ibc/src/vp/mod.rs @@ -4,39 +4,41 @@ pub mod context; use std::cell::RefCell; use std::collections::BTreeSet; +use std::fmt::Debug; +use std::marker::PhantomData; use std::rc::Rc; use std::time::Duration; -use context::{PseudoExecutionContext, VpValidationContext}; +use context::{ + PseudoExecutionContext, PseudoExecutionStorage, VpValidationContext, +}; use namada_core::address::Address; use namada_core::arith::{self, checked}; use namada_core::collections::HashSet; use namada_core::storage::Key; +use namada_core::token::{self, Amount}; +use namada_core::{governance, parameters, proof_of_stake}; use namada_gas::{IBC_ACTION_EXECUTE_GAS, IBC_ACTION_VALIDATE_GAS}; -use namada_governance::is_proposal_accepted; -use namada_ibc::event::IbcEvent; -use namada_ibc::{ - Error as ActionError, IbcActions, NftTransferModule, TransferModule, - ValidationParams, -}; -use namada_proof_of_stake::storage::read_pos_params; use namada_state::write_log::StorageModification; -use namada_state::StateRead; +use namada_state::{StateRead, StorageError}; use namada_tx::BatchedTxRef; -use namada_vp_env::VpEnv; +use namada_vp::native_vp::{ + self, Ctx, CtxPreStorageRead, NativeVp, VpEvaluator, +}; +use namada_vp::VpEnv; use thiserror::Error; -use crate::ibc::core::host::types::identifiers::ChainId as IbcChainId; -use crate::ledger::ibc::storage::{ +use crate::core::host::types::identifiers::ChainId as IbcChainId; +use crate::event::IbcEvent; +use crate::storage::{ deposit_key, get_limits, is_ibc_key, is_ibc_trace_key, mint_amount_key, withdraw_key, }; -use crate::ledger::ibc::trace::calc_hash; -use crate::ledger::native_vp::{self, Ctx, NativeVp}; -use crate::ledger::parameters::read_epoch_duration_parameter; -use crate::token::storage_key::is_any_token_balance_key; -use crate::token::Amount; -use crate::vm::WasmCacheAccess; +use crate::trace::calc_hash; +use crate::{ + Error as ActionError, IbcActions, NftTransferModule, TransferModule, + ValidationParams, +}; #[allow(missing_docs)] #[derive(Error, Debug)] @@ -65,30 +67,58 @@ pub enum Error { pub type VpResult = std::result::Result; /// IBC VP -pub struct Ibc<'a, S, CA> +pub struct Ibc<'a, S, CA, EVAL, Params, Gov, Token, PoS> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA>, + pub ctx: Ctx<'a, S, CA, EVAL>, + /// Parameters type + pub params: PhantomData, + /// Governance type + pub gov: PhantomData, + /// Token type + pub token: PhantomData, + /// PoS type + pub pos: PhantomData, } -impl<'a, S, CA> NativeVp for Ibc<'a, S, CA> +impl<'a, S, CA, EVAL, Params, Gov, Token, PoS> NativeVp<'a> + for Ibc<'a, S, CA, EVAL, Params, Gov, Token, PoS> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL> + Debug, + CA: 'static + Clone + Debug, + Gov: governance::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, + Params: parameters::Keys + + parameters::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, + Token: token::Keys + + token::Write< + PseudoExecutionStorage<'a, 'a, S, CA, EVAL>, + Err = StorageError, + > + Debug, + PoS: proof_of_stake::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, { type Error = Error; fn validate_tx( - &self, + &'a self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, ) -> VpResult<()> { // Is VP triggered by a governance proposal? - if is_proposal_accepted( + if Gov::is_proposal_accepted( &self.ctx.pre(), batched_tx .tx @@ -120,17 +150,47 @@ where } } -impl<'a, S, CA> Ibc<'a, S, CA> +impl<'a, S, CA, EVAL, Params, Gov, Token, PoS> + Ibc<'a, S, CA, EVAL, Params, Gov, Token, PoS> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL> + Debug, + CA: 'static + Clone + Debug, + Params: parameters::Keys + + parameters::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, + Token: token::Keys + + token::Write< + PseudoExecutionStorage<'a, 'a, S, CA, EVAL>, + Err = StorageError, + > + Debug, + PoS: proof_of_stake::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, { + /// Instantiate IBC VP + pub fn new(ctx: Ctx<'a, S, CA, EVAL>) -> Self { + Self { + ctx, + params: PhantomData, + gov: PhantomData, + token: PhantomData, + pos: PhantomData, + } + } + fn validate_state( - &self, + &'a self, tx_data: &[u8], keys_changed: &BTreeSet, ) -> VpResult<()> { - let exec_ctx = PseudoExecutionContext::new(self.ctx.pre()); + let exec_ctx = + PseudoExecutionContext::<'_, '_, S, CA, EVAL, Token>::new( + self.ctx.pre(), + ); let ctx = Rc::new(RefCell::new(exec_ctx)); // Use an empty verifiers set placeholder for validation, this is only // needed in actual txs to addresses whose VPs should be triggered @@ -173,7 +233,7 @@ where .get_events_of::() .collect(); let ctx_borrow = ctx.borrow(); - let expected: BTreeSet<_> = ctx_borrow.event.iter().collect(); + let expected: BTreeSet<_> = ctx_borrow.storage.event.iter().collect(); if actual != expected { return Err(Error::IbcEvent(format!( "The IBC event is invalid: Actual {actual:?}, Expected \ @@ -184,7 +244,7 @@ where Ok(()) } - fn validate_with_msg(&self, tx_data: &[u8]) -> VpResult<()> { + fn validate_with_msg(&'a self, tx_data: &[u8]) -> VpResult<()> { let validation_ctx = VpValidationContext::new(self.ctx.pre()); let ctx = Rc::new(RefCell::new(validation_ctx)); // Use an empty verifiers set placeholder for validation, this is only @@ -206,15 +266,14 @@ where } /// Retrieve the validation params - pub fn validation_params(&self) -> VpResult { + pub fn validation_params(&'a self) -> VpResult { use std::str::FromStr; let chain_id = self.ctx.get_chain_id().map_err(Error::NativeVpError)?; let proof_specs = namada_state::ics23_specs::ibc_proof_specs::<::H>(); - let pos_params = - read_pos_params(&self.ctx.post()).map_err(Error::NativeVpError)?; - let pipeline_len = pos_params.pipeline_len; - let epoch_duration = read_epoch_duration_parameter(&self.ctx.post()) + let pipeline_len = + PoS::pipeline_len(&self.ctx.pre()).map_err(Error::NativeVpError)?; + let epoch_duration = Params::epoch_duration_parameter(&self.ctx.pre()) .map_err(Error::NativeVpError)?; let unbonding_period_secs = checked!(pipeline_len * epoch_duration.min_duration.0)?; @@ -263,7 +322,9 @@ where fn check_limits(&self, keys_changed: &BTreeSet) -> VpResult { let tokens: BTreeSet<&Address> = keys_changed .iter() - .filter_map(|k| is_any_token_balance_key(k).map(|[key, _]| key)) + .filter_map(|k| { + Token::is_any_token_balance_key(k).map(|[key, _]| key) + }) .collect(); for token in tokens { let (mint_limit, throughput_limit) = @@ -357,120 +418,150 @@ mod tests { use std::str::FromStr; - use borsh::BorshDeserialize; - use borsh_ext::BorshSerializeExt; + use assert_matches::assert_matches; use ibc_testkit::testapp::ibc::clients::mock::client_state::{ client_type, MockClientState, MOCK_CLIENT_TYPE, }; use ibc_testkit::testapp::ibc::clients::mock::consensus_state::MockConsensusState; use ibc_testkit::testapp::ibc::clients::mock::header::MockHeader; + use namada_core::address::testing::{ + established_address_1, established_address_2, nam, + }; use namada_core::address::InternalAddress; + use namada_core::borsh::{BorshDeserialize, BorshSerializeExt}; + use namada_core::key::testing::keypair_1; use namada_core::storage::testing::get_dummy_header; - use namada_gas::TxGasMeter; + use namada_core::storage::{BlockHeight, Epoch, TxIndex}; + use namada_core::tendermint::time::Time as TmTime; + use namada_core::time::DurationSecs; + use namada_core::WasmCacheRwAccess; + use namada_gas::{TxGasMeter, VpGasMeter}; use namada_governance::parameters::GovernanceParameters; - use namada_ibc::event::IbcEventType; + use namada_parameters::storage::{ + get_epoch_duration_storage_key, get_max_expected_time_per_block_key, + }; + use namada_parameters::EpochDuration; use namada_proof_of_stake::test_utils::get_dummy_genesis_validator; use namada_state::testing::TestState; use namada_state::StorageRead; + use namada_token::storage_key::balance_key; use namada_tx::data::TxType; use namada_tx::{Authorization, Code, Data, Section, Tx}; + use namada_vm::wasm; + use namada_vm::wasm::run::VpEvalWasm; + use namada_vm::wasm::VpCache; use prost::Message; use sha2::Digest; use super::*; - use crate::core::address::testing::{ - established_address_1, established_address_2, nam, - }; - use crate::core::storage::Epoch; - use crate::ibc::apps::nft_transfer::types::events::{ + use crate::apps::nft_transfer::types::events::{ RecvEvent as NftRecvEvent, TokenTraceEvent, TransferEvent as NftTransferEvent, }; - use crate::ibc::apps::nft_transfer::types::msgs::transfer::MsgTransfer as IbcMsgNftTransfer; - use crate::ibc::apps::nft_transfer::types::packet::PacketData as NftPacketData; - use crate::ibc::apps::nft_transfer::types::{ + use crate::apps::nft_transfer::types::msgs::transfer::MsgTransfer as IbcMsgNftTransfer; + use crate::apps::nft_transfer::types::packet::PacketData as NftPacketData; + use crate::apps::nft_transfer::types::{ self as nft_types, PrefixedClassId, TokenId, TokenIds, VERSION as NFT_VERSION, }; - use crate::ibc::apps::transfer::types::events::{ + use crate::apps::transfer::types::events::{ AckEvent, DenomTraceEvent, RecvEvent, TimeoutEvent, TransferEvent, }; - use crate::ibc::apps::transfer::types::msgs::transfer::MsgTransfer as IbcMsgTransfer; - use crate::ibc::apps::transfer::types::packet::PacketData; - use crate::ibc::apps::transfer::types::{ + use crate::apps::transfer::types::msgs::transfer::MsgTransfer as IbcMsgTransfer; + use crate::apps::transfer::types::packet::PacketData; + use crate::apps::transfer::types::{ ack_success_b64, PrefixedCoin, TracePrefix, VERSION, }; - use crate::ibc::core::channel::types::acknowledgement::{ + use crate::core::channel::types::acknowledgement::{ Acknowledgement, AcknowledgementStatus, }; - use crate::ibc::core::channel::types::channel::{ + use crate::core::channel::types::channel::{ ChannelEnd, Counterparty as ChanCounterparty, Order, State as ChanState, }; - use crate::ibc::core::channel::types::commitment::PacketCommitment; - use crate::ibc::core::channel::types::events::{ + use crate::core::channel::types::commitment::PacketCommitment; + use crate::core::channel::types::events::{ AcknowledgePacket, OpenAck as ChanOpenAck, OpenConfirm as ChanOpenConfirm, OpenInit as ChanOpenInit, OpenTry as ChanOpenTry, ReceivePacket, SendPacket, TimeoutPacket, WriteAcknowledgement, }; - use crate::ibc::core::channel::types::msgs::{ + use crate::core::channel::types::msgs::{ MsgAcknowledgement, MsgChannelOpenAck, MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, MsgRecvPacket, MsgTimeout, MsgTimeoutOnClose, }; - use crate::ibc::core::channel::types::packet::Packet; - use crate::ibc::core::channel::types::timeout::TimeoutHeight; - use crate::ibc::core::channel::types::Version as ChanVersion; - use crate::ibc::core::client::types::events::{CreateClient, UpdateClient}; - use crate::ibc::core::client::types::msgs::{ - MsgCreateClient, MsgUpdateClient, - }; - use crate::ibc::core::client::types::Height; - use crate::ibc::core::commitment_types::commitment::{ + use crate::core::channel::types::packet::Packet; + use crate::core::channel::types::timeout::TimeoutHeight; + use crate::core::channel::types::Version as ChanVersion; + use crate::core::client::types::events::{CreateClient, UpdateClient}; + use crate::core::client::types::msgs::{MsgCreateClient, MsgUpdateClient}; + use crate::core::client::types::Height; + use crate::core::commitment_types::commitment::{ CommitmentPrefix, CommitmentProofBytes, }; - use crate::ibc::core::connection::types::events::{ + use crate::core::connection::types::events::{ OpenAck as ConnOpenAck, OpenConfirm as ConnOpenConfirm, OpenInit as ConnOpenInit, OpenTry as ConnOpenTry, }; - use crate::ibc::core::connection::types::msgs::{ + use crate::core::connection::types::msgs::{ MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit, MsgConnectionOpenTry, }; - use crate::ibc::core::connection::types::version::Version as ConnVersion; - use crate::ibc::core::connection::types::{ + use crate::core::connection::types::version::Version as ConnVersion; + use crate::core::connection::types::{ ConnectionEnd, Counterparty as ConnCounterparty, State as ConnState, }; - use crate::ibc::core::handler::types::events::{ + use crate::core::handler::types::events::{ IbcEvent as RawIbcEvent, MessageEvent, }; - use crate::ibc::core::host::types::identifiers::{ + use crate::core::host::types::identifiers::{ ChannelId, ClientId, ConnectionId, PortId, Sequence, }; - use crate::ibc::core::router::types::event::ModuleEvent; - use crate::ibc::parameters::IbcParameters; - use crate::ibc::primitives::proto::{Any, Protobuf}; - use crate::ibc::primitives::{Timestamp, ToProto}; - use crate::ibc::storage::{ - ack_key, channel_counter_key, channel_key, client_connections_key, - client_counter_key, client_state_key, client_update_height_key, - client_update_timestamp_key, commitment_key, connection_counter_key, - connection_key, consensus_state_key, ibc_trace_key, mint_amount_key, - next_sequence_ack_key, next_sequence_recv_key, next_sequence_send_key, - nft_class_key, nft_metadata_key, receipt_key, + use crate::core::router::types::event::ModuleEvent; + use crate::event::IbcEventType; + use crate::parameters::IbcParameters; + use crate::primitives::proto::{Any, Protobuf}; + use crate::primitives::{Timestamp, ToProto}; + use crate::storage::{ + self, ack_key, channel_counter_key, channel_key, + client_connections_key, client_counter_key, client_state_key, + client_update_height_key, client_update_timestamp_key, commitment_key, + connection_counter_key, connection_key, consensus_state_key, + ibc_trace_key, mint_amount_key, next_sequence_ack_key, + next_sequence_recv_key, next_sequence_send_key, nft_class_key, + nft_metadata_key, receipt_key, + }; + use crate::trace::{calc_hash, ibc_token}; + use crate::{ + init_genesis_storage, MsgNftTransfer, MsgTransfer, NftClass, + NftMetadata, }; - use crate::ibc::trace::{calc_hash, ibc_token}; - use crate::ibc::{MsgNftTransfer, MsgTransfer, NftClass, NftMetadata}; - use crate::key::testing::keypair_1; - use crate::ledger::gas::VpGasMeter; - use crate::ledger::parameters::storage::get_epoch_duration_storage_key; - use crate::ledger::parameters::EpochDuration; - use crate::ledger::{ibc, pos}; - use crate::storage::{BlockHeight, TxIndex}; - use crate::tendermint::time::Time as TmTime; - use crate::time::DurationSecs; - use crate::token::storage_key::balance_key; - use crate::vm::wasm; + + type CA = WasmCacheRwAccess; + type Eval = VpEvalWasm< + ::D, + ::H, + CA, + >; + type Ctx<'a> = super::Ctx<'a, TestState, VpCache, Eval>; + type Ibc<'a> = super::Ibc< + 'a, + TestState, + VpCache, + Eval, + namada_parameters::Store< + CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + >, + namada_governance::Store< + CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + >, + namada_token::Store< + PseudoExecutionStorage<'a, 'a, TestState, VpCache, Eval>, + >, + namada_proof_of_stake::Store< + CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + >, + >; const ADDRESS: Address = Address::Internal(InternalAddress::Ibc); const COMMITMENT_PREFIX: &[u8] = b"ibc"; @@ -485,7 +576,7 @@ mod tests { let mut state = TestState::default(); // initialize the storage - ibc::init_genesis_storage(&mut state); + init_genesis_storage(&mut state); let gov_params = GovernanceParameters::default(); gov_params.init_storage(&mut state).unwrap(); let ibc_params = IbcParameters { @@ -493,7 +584,7 @@ mod tests { default_per_epoch_throughput_limit: Amount::native_whole(100), }; ibc_params.init_storage(&mut state).unwrap(); - pos::test_utils::test_init_genesis( + namada_proof_of_stake::test_utils::test_init_genesis( &mut state, namada_proof_of_stake::OwnedPosParams::default(), vec![get_dummy_genesis_validator()].into_iter(), @@ -576,8 +667,7 @@ mod tests { } fn get_nft_port_id() -> PortId { - PortId::from_str(crate::ibc::apps::nft_transfer::types::PORT_ID_STR) - .unwrap() + PortId::from_str(crate::apps::nft_transfer::types::PORT_ID_STR).unwrap() } fn get_channel_id() -> ChannelId { @@ -876,7 +966,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let mut outer_tx = Tx::from_type(TxType::Raw); @@ -901,7 +991,7 @@ mod tests { vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); // this should return true because state has been stored assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -916,7 +1006,7 @@ mod tests { let mut keys_changed = BTreeSet::new(); // initialize the storage - ibc::init_genesis_storage(&mut state); + init_genesis_storage(&mut state); // set a dummy header state .in_mem_mut() @@ -961,7 +1051,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -977,7 +1067,7 @@ mod tests { vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); // this should fail because no state is stored let result = ibc .validate_tx(&batched_tx, &keys_changed, &verifiers) @@ -1086,7 +1176,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -1101,7 +1191,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); // this should return true because state has been stored assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -1195,7 +1285,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = outer_tx.batch_ref_first_tx().unwrap(); @@ -1210,7 +1300,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); // this should return true because state has been stored assert!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers) @@ -1224,7 +1314,7 @@ mod tests { let mut keys_changed = BTreeSet::new(); // initialize the storage - ibc::init_genesis_storage(&mut state); + init_genesis_storage(&mut state); // set a dummy header state .in_mem_mut() @@ -1289,7 +1379,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -1304,7 +1394,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); // this should fail because no event let result = ibc .validate_tx(&batched_tx, &keys_changed, &verifiers) @@ -1409,7 +1499,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -1424,7 +1514,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); // this should return true because state has been stored assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), @@ -1519,7 +1609,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = outer_tx.batch_ref_first_tx().unwrap(); @@ -1534,7 +1624,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -1614,7 +1704,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = outer_tx.batch_ref_first_tx().unwrap(); @@ -1629,7 +1719,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -1737,7 +1827,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = outer_tx.batch_ref_first_tx().unwrap(); @@ -1752,7 +1842,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -1859,7 +1949,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = outer_tx.batch_ref_first_tx().unwrap(); @@ -1874,7 +1964,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -1966,7 +2056,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = outer_tx.batch_ref_first_tx().unwrap(); @@ -1981,7 +2071,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -2068,7 +2158,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -2083,7 +2173,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -2224,7 +2314,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -2239,7 +2329,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -2435,7 +2525,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -2450,7 +2540,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -2590,7 +2680,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -2605,7 +2695,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -2747,7 +2837,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -2762,7 +2852,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -2905,7 +2995,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -2920,7 +3010,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -2953,7 +3043,13 @@ mod tests { let class_id = get_nft_class_id(); let token_id = get_nft_id(); let sender = established_address_1(); +<<<<<<< HEAD let ibc_token = ibc::trace::ibc_token_for_nft(&class_id, &token_id); +||||||| parent of 9e9b323a5 (ibc: fix VP post move) + let ibc_token = ibc::storage::ibc_token_for_nft(&class_id, &token_id); +======= + let ibc_token = storage::ibc_token_for_nft(&class_id, &token_id); +>>>>>>> 9e9b323a5 (ibc: fix VP post move) let balance_key = balance_key(&ibc_token, &sender); let amount = Amount::from_u64(1); state @@ -2962,14 +3058,14 @@ mod tests { .expect("write failed"); // nft class let class = dummy_nft_class(); - let class_key = ibc::storage::nft_class_key(&class_id); + let class_key = storage::nft_class_key(&class_id); state .write_log_mut() .write(&class_key, class.serialize_to_vec()) .expect("write failed"); // nft metadata let metadata = dummy_nft_metadata(); - let metadata_key = ibc::storage::nft_metadata_key(&class_id, &token_id); + let metadata_key = storage::nft_metadata_key(&class_id, &token_id); state .write_log_mut() .write(&metadata_key, metadata.serialize_to_vec()) @@ -3083,7 +3179,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -3098,7 +3194,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) @@ -3317,7 +3413,7 @@ mod tests { &TxGasMeter::new(TX_GAS_LIMIT), )); let (vp_wasm_cache, _vp_cache_dir) = - wasm::compilation_cache::common::testing::cache(); + wasm::compilation_cache::common::testing::vp_cache(); let verifiers = BTreeSet::new(); let batched_tx = tx.batch_ref_first_tx().unwrap(); @@ -3332,7 +3428,7 @@ mod tests { &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = Ibc::new(ctx); assert_matches!( ibc.validate_tx(&batched_tx, &keys_changed, &verifiers), Ok(_) diff --git a/crates/namada/src/ledger/ibc/mod.rs b/crates/namada/src/ledger/ibc/mod.rs deleted file mode 100644 index be7f4f5144..0000000000 --- a/crates/namada/src/ledger/ibc/mod.rs +++ /dev/null @@ -1,81 +0,0 @@ -//! IBC integration - -use namada_core::token::Amount; -use namada_events::EmitEvents; -use namada_ibc::storage::{ - channel_counter_key, client_counter_key, connection_counter_key, - deposit_prefix, withdraw_prefix, -}; -pub use namada_ibc::{parameters, storage, trace}; -use namada_state::{ - DBIter, Key, State, StorageError, StorageHasher, StorageRead, StorageWrite, - WlState, DB, -}; - -/// Initialize storage in the genesis block. -pub fn init_genesis_storage(storage: &mut S) -where - S: State, -{ - // In ibc-go, u64 like a counter is encoded with big-endian: - // https://github.com/cosmos/ibc-go/blob/89ffaafb5956a5ea606e1f1bf249c880bea802ed/modules/core/04-channel/keeper/keeper.go#L115 - - let init_value = 0_u64; - - // the client counter - let key = client_counter_key(); - storage - .write(&key, init_value) - .expect("Unable to write the initial client counter"); - - // the connection counter - let key = connection_counter_key(); - storage - .write(&key, init_value) - .expect("Unable to write the initial connection counter"); - - // the channel counter - let key = channel_counter_key(); - storage - .write(&key, init_value) - .expect("Unable to write the initial channel counter"); -} - -/// Update IBC-related data when finalizing block -pub fn finalize_block( - state: &mut WlState, - _events: &mut impl EmitEvents, - is_new_epoch: bool, -) -> Result<(), StorageError> -where - D: 'static + DB + for<'iter> DBIter<'iter> + Sync, - H: 'static + StorageHasher + Sync, -{ - if is_new_epoch { - clear_throughputs(state)?; - } - Ok(()) -} - -/// Clear the per-epoch throughputs (deposit and withdraw) -fn clear_throughputs( - state: &mut WlState, -) -> Result<(), StorageError> -where - D: 'static + DB + for<'iter> DBIter<'iter> + Sync, - H: 'static + StorageHasher + Sync, -{ - for prefix in [deposit_prefix(), withdraw_prefix()] { - let keys: Vec = state - .iter_prefix(&prefix)? - .map(|(key, _, _)| { - Key::parse(key).expect("The key should be parsable") - }) - .collect(); - for key in keys { - state.write(&key, Amount::from(0))?; - } - } - - Ok(()) -} diff --git a/crates/parameters/src/lib.rs b/crates/parameters/src/lib.rs index 02fbfcd8b1..cb6ac30b8e 100644 --- a/crates/parameters/src/lib.rs +++ b/crates/parameters/src/lib.rs @@ -34,16 +34,12 @@ pub use storage::{get_gas_scale, get_max_block_gas}; use thiserror::Error; pub use wasm_allowlist::{is_tx_allowed, is_vp_allowed}; -/// Parameters storage keys implementation -#[derive(Debug)] -pub struct Key; - -/// Parameters storage `Read/Write` implementation +/// Parameters storage `Keys/Read/Write` implementation #[derive(Debug)] pub struct Store(PhantomData); -impl Keys for Key { - fn implicit_vp() -> namada_core::storage::Key { +impl Keys for Store { + fn implicit_vp_key() -> namada_core::storage::Key { storage::get_implicit_vp_key() } } @@ -58,9 +54,15 @@ where read(storage) } - fn read_masp_epoch_multiplier(storage: &S) -> Result { + fn masp_epoch_multiplier(storage: &S) -> Result { read_masp_epoch_multiplier_parameter(storage) } + + fn epoch_duration_parameter( + storage: &S, + ) -> Result { + read_epoch_duration_parameter(storage) + } } impl Write for Store diff --git a/crates/proof_of_stake/src/lib.rs b/crates/proof_of_stake/src/lib.rs index 7a53aabc4c..3092549249 100644 --- a/crates/proof_of_stake/src/lib.rs +++ b/crates/proof_of_stake/src/lib.rs @@ -127,6 +127,11 @@ where ) -> Result { is_delegator(storage, address, epoch) } + + fn pipeline_len(storage: &S) -> Result { + let params = storage::read_owned_pos_params(storage)?; + Ok(params.pipeline_len) + } } /// Address of the PoS account implemented as a native VP diff --git a/crates/proof_of_stake/src/parameters.rs b/crates/proof_of_stake/src/parameters.rs index 2bee07d8f8..af15a10b26 100644 --- a/crates/proof_of_stake/src/parameters.rs +++ b/crates/proof_of_stake/src/parameters.rs @@ -76,17 +76,6 @@ pub struct OwnedPosParams { pub rewards_gain_d: Dec, } -impl Default for PosParams { - fn default() -> Self { - let owned = OwnedPosParams::default(); - let gov = GovernanceParameters::default(); - Self { - owned, - max_proposal_period: gov.max_proposal_period, - } - } -} - impl Default for OwnedPosParams { fn default() -> Self { Self { @@ -117,6 +106,17 @@ impl Default for OwnedPosParams { } } +impl Default for PosParams { + fn default() -> Self { + let owned = OwnedPosParams::default(); + let gov = GovernanceParameters::default(); + Self { + owned, + max_proposal_period: gov.max_proposal_period, + } + } +} + #[allow(missing_docs)] #[derive(Error, Debug)] pub enum ValidationError { diff --git a/crates/proof_of_stake/src/storage.rs b/crates/proof_of_stake/src/storage.rs index fd99c14a44..081f6c06ac 100644 --- a/crates/proof_of_stake/src/storage.rs +++ b/crates/proof_of_stake/src/storage.rs @@ -259,15 +259,24 @@ pub fn delegation_targets_handle(delegator: &Address) -> DelegationTargets { // ---- Storage read + write ---- +/// Read owned PoS parameters +pub fn read_owned_pos_params( + storage: &S, +) -> namada_storage::Result +where + S: StorageRead, +{ + Ok(storage + .read(&storage_key::params_key())? + .expect("PosParams should always exist in storage after genesis")) +} + /// Read PoS parameters pub fn read_pos_params(storage: &S) -> namada_storage::Result where S: StorageRead, { - let params = storage - .read(&storage_key::params_key()) - .transpose() - .expect("PosParams should always exist in storage after genesis")?; + let params = read_owned_pos_params(storage)?; read_non_pos_owned_params(storage, params) } diff --git a/crates/state/src/lib.rs b/crates/state/src/lib.rs index 57277990c9..2a8e852c6e 100644 --- a/crates/state/src/lib.rs +++ b/crates/state/src/lib.rs @@ -142,13 +142,13 @@ pub trait StateRead: StorageRead + Debug { /// Get the hash of a validity predicate for the given account address and /// the gas cost for reading it. - fn validity_predicate( + fn validity_predicate( &self, addr: &Address, - _: &ParamsKey, + _: &Params, ) -> Result<(Option, u64)> { let key = if let Address::Implicit(_) = addr { - ParamsKey::implicit_vp() + Params::implicit_vp_key() } else { Key::validity_predicate(addr) }; diff --git a/crates/trans_token/src/lib.rs b/crates/trans_token/src/lib.rs index 497ba0dadf..76451529a0 100644 --- a/crates/trans_token/src/lib.rs +++ b/crates/trans_token/src/lib.rs @@ -25,6 +25,7 @@ use std::marker::PhantomData; use namada_core::address::Address; pub use namada_core::token::*; +use namada_storage::{StorageRead, StorageWrite}; pub use storage::*; /// Transparent token storage `Keys/Read/Write` implementation @@ -32,20 +33,67 @@ pub use storage::*; pub struct Store(PhantomData); impl Keys for Store { - fn balance(token: &Address, owner: &Address) -> namada_core::storage::Key { + fn balance_key( + token: &Address, + owner: &Address, + ) -> namada_core::storage::Key { storage_key::balance_key(token, owner) } - fn is_balance<'a>( + fn is_balance_key<'a>( token_addr: &Address, key: &'a namada_core::storage::Key, ) -> Option<&'a Address> { storage_key::is_balance_key(token_addr, key) } - fn is_any_token_balance( + fn is_any_token_balance_key( key: &namada_core::storage::Key, ) -> Option<[&Address; 2]> { storage_key::is_any_token_balance_key(key) } + + fn minter_key(token_addr: &Address) -> namada_core::storage::Key { + storage_key::minter_key(token_addr) + } +} + +impl namada_core::token::Read for Store +where + S: StorageRead, +{ + type Err = namada_storage::Error; +} + +impl namada_core::token::Write for Store +where + S: StorageWrite + StorageRead, +{ + fn transfer( + storage: &mut S, + token: &Address, + src: &Address, + dest: &Address, + amount: Amount, + ) -> Result<(), Self::Err> { + storage::transfer(storage, token, src, dest, amount) + } + + fn burn_tokens( + storage: &mut S, + token: &Address, + source: &Address, + amount: Amount, + ) -> Result<(), Self::Err> { + storage::burn_tokens(storage, token, source, amount) + } + + fn credit_tokens( + storage: &mut S, + token: &Address, + dest: &Address, + amount: Amount, + ) -> Result<(), Self::Err> { + storage::credit_tokens(storage, token, dest, amount) + } } diff --git a/crates/tx_prelude/src/ibc.rs b/crates/tx_prelude/src/ibc.rs index b11625656a..8396d6c392 100644 --- a/crates/tx_prelude/src/ibc.rs +++ b/crates/tx_prelude/src/ibc.rs @@ -34,7 +34,21 @@ pub fn ibc_actions(ctx: &mut Ctx) -> IbcActions<'_, Ctx> { actions } -impl IbcStorageContext for Ctx { +impl<'s> IbcStorageContext for Ctx { + type Storage = Self; + + fn storage(&self) -> &Self::Storage { + self + } + + fn storage_mut(&mut self) -> &mut Self::Storage { + self + } + + fn log_string(&self, message: String) { + super::log_string(message); + } + fn emit_ibc_event( &mut self, event: IbcEvent, @@ -73,10 +87,6 @@ impl IbcStorageContext for Ctx { fn insert_verifier(&mut self, addr: &Address) -> Result<(), Error> { TxEnv::insert_verifier(self, addr) } - - fn log_string(&self, message: String) { - super::log_string(message); - } } impl IbcCommonContext for Ctx {} diff --git a/crates/vp/src/native_vp.rs b/crates/vp/src/native_vp.rs index 274913b749..33065965cd 100644 --- a/crates/vp/src/native_vp.rs +++ b/crates/vp/src/native_vp.rs @@ -108,7 +108,7 @@ where S: 'static + StateRead, EVAL: VpEvaluator<'a, S, CA, EVAL>, { - pub(crate) ctx: &'view Ctx<'a, S, CA, EVAL>, + pub ctx: &'view Ctx<'a, S, CA, EVAL>, } /// Read access to the posterior storage (state after tx execution) via diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index 991210a691..c49bee42bf 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -3657,7 +3657,9 @@ dependencies = [ "namada_trans_token", "namada_tx", "namada_vote_ext", + "namada_vp", "serde", + "smooth-operator", "thiserror", "tracing", ] @@ -3699,13 +3701,16 @@ dependencies = [ "itertools 0.12.1", "konst", "linkme", + "namada_account", "namada_core", "namada_events", "namada_macros", "namada_migrations", "namada_parameters", - "namada_storage", + "namada_state", "namada_trans_token", + "namada_tx", + "namada_vp", "proptest", "serde", "serde_json", @@ -3728,12 +3733,15 @@ dependencies = [ "masp_primitives", "namada_core", "namada_events", + "namada_gas", "namada_governance", "namada_macros", "namada_parameters", "namada_state", "namada_storage", "namada_token", + "namada_tx", + "namada_vp", "primitive-types", "proptest", "prost", @@ -3927,7 +3935,6 @@ dependencies = [ "namada_macros", "namada_merkle_tree", "namada_migrations", - "namada_parameters", "namada_replay_protection", "namada_storage", "namada_tx", @@ -4101,6 +4108,21 @@ dependencies = [ "serde", ] +[[package]] +name = "namada_vp" +version = "0.39.0" +dependencies = [ + "namada_core", + "namada_events", + "namada_gas", + "namada_state", + "namada_tx", + "namada_vp_env", + "smooth-operator", + "thiserror", + "tracing", +] + [[package]] name = "namada_vp_env" version = "0.41.0" @@ -4109,7 +4131,6 @@ dependencies = [ "masp_primitives", "namada_core", "namada_events", - "namada_ibc", "namada_storage", "namada_tx", "smooth-operator", From 0b7ff6bd33afeb8a7355a6ae50840bfa603519c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 14 Jun 2024 17:05:45 +0100 Subject: [PATCH 18/41] mv crates/namada/src/ledger/native_vp/masp.rs crates/shielded_token/src/vp.rs --- .../src/ledger/native_vp/masp.rs => shielded_token/src/vp.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/native_vp/masp.rs => shielded_token/src/vp.rs} (100%) diff --git a/crates/namada/src/ledger/native_vp/masp.rs b/crates/shielded_token/src/vp.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/masp.rs rename to crates/shielded_token/src/vp.rs From 280900ad9d9b35a798129d20792fd273bc7bcabd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 14:34:14 +0100 Subject: [PATCH 19/41] shielded_token: fix VP post move --- Cargo.lock | 7 ++ crates/core/src/parameters.rs | 5 ++ crates/parameters/src/lib.rs | 6 ++ crates/sdk/src/masp.rs | 7 +- crates/shielded_token/Cargo.toml | 12 ++++ crates/shielded_token/src/lib.rs | 2 + crates/shielded_token/src/vp.rs | 111 ++++++++++++++++--------------- 7 files changed, 93 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9cd9687dce..8c4134c1bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5129,18 +5129,25 @@ dependencies = [ "lazy_static", "masp_primitives", "masp_proofs", + "namada_account", "namada_controller", "namada_core", "namada_gas", "namada_parameters", + "namada_state", "namada_storage", "namada_trans_token", + "namada_tx", + "namada_vp", "proptest", "rand_core 0.6.4", "rayon", + "ripemd", "serde", + "sha2 0.9.9", "smooth-operator", "test-log", + "thiserror", "tracing", ] diff --git a/crates/core/src/parameters.rs b/crates/core/src/parameters.rs index ce34756c50..a5bb9a8338 100644 --- a/crates/core/src/parameters.rs +++ b/crates/core/src/parameters.rs @@ -35,6 +35,11 @@ pub trait Read { fn epoch_duration_parameter( storage: &S, ) -> Result; + + /// Get the max signatures per transactio parameter + fn max_signatures_per_transaction( + storage: &S, + ) -> Result, Self::Err>; } /// Abstract parameters storage write interface diff --git a/crates/parameters/src/lib.rs b/crates/parameters/src/lib.rs index cb6ac30b8e..04a85bbd4e 100644 --- a/crates/parameters/src/lib.rs +++ b/crates/parameters/src/lib.rs @@ -63,6 +63,12 @@ where ) -> Result { read_epoch_duration_parameter(storage) } + + fn max_signatures_per_transaction( + storage: &S, + ) -> Result, Self::Err> { + max_signatures_per_transaction(storage) + } } impl Write for Store diff --git a/crates/sdk/src/masp.rs b/crates/sdk/src/masp.rs index ffee94a11f..af45716007 100644 --- a/crates/sdk/src/masp.rs +++ b/crates/sdk/src/masp.rs @@ -5,10 +5,8 @@ mod test_utils; pub mod utils; use std::cmp::Ordering; use std::collections::{btree_map, BTreeMap, BTreeSet}; -use std::env; use std::fmt::Debug; use std::io::{Read, Write}; -use std::path::PathBuf; use std::sync::{Arc, Mutex}; use borsh::{BorshDeserialize, BorshSerialize}; @@ -63,6 +61,7 @@ use namada_ibc::{decode_message, extract_masp_tx_from_envelope, IbcMessage}; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; +#[cfg(feature = "validation")] pub use namada_token::validation::{ partial_deauthorize, preload_verifying_keys, PVKs, CONVERT_NAME, ENV_VAR_MASP_PARAMS_DIR, OUTPUT_NAME, SPEND_NAME, @@ -3374,11 +3373,13 @@ pub mod testing { } } -#[cfg(feature = "std")] +#[cfg(all(feature = "std", feature = "validation"))] /// Implementation of MASP functionality depending on a standard filesystem pub mod fs { + use std::env; use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; + use std::path::PathBuf; use namada_token::validation::{ get_params_dir, CONVERT_NAME, ENV_VAR_MASP_PARAMS_DIR, OUTPUT_NAME, diff --git a/crates/shielded_token/Cargo.toml b/crates/shielded_token/Cargo.toml index 63a808b625..470eecf480 100644 --- a/crates/shielded_token/Cargo.toml +++ b/crates/shielded_token/Cargo.toml @@ -17,18 +17,23 @@ default = [] multicore = ["dep:rayon"] testing = [ "multicore", + "validation", "namada_core/testing", "masp_primitives/test-dependencies", ] download-params = ["masp_proofs/download-params"] [dependencies] +namada_account = { path = "../account" } namada_controller = { path = "../controller" } namada_core = { path = "../core" } namada_gas = { path = "../gas" } namada_parameters = { path = "../parameters" } +namada_state = { path = "../state" } namada_storage = { path = "../storage" } namada_trans_token = { path = "../trans_token" } +namada_tx = { path = "../tx" } +namada_vp = { path = "../vp" } borsh.workspace = true lazy_static.workspace = true @@ -36,15 +41,22 @@ masp_primitives.workspace = true masp_proofs = { workspace = true } rand_core.workspace = true rayon = { workspace = true, optional = true } +ripemd.workspace = true serde.workspace = true +sha2.workspace = true smooth-operator.workspace = true +thiserror.workspace = true tracing.workspace = true [dev-dependencies] namada_core = { path = "../core", features = ["testing"] } +namada_gas = { path = "../gas" } namada_parameters = { path = "../parameters", features = ["testing"] } namada_storage = { path = "../storage", features = ["testing"] } +lazy_static.workspace = true +masp_proofs = { workspace = true, features = ["download-params"] } proptest.workspace = true +rand_core.workspace = true rayon.workspace = true test-log.workspace = true diff --git a/crates/shielded_token/src/lib.rs b/crates/shielded_token/src/lib.rs index 50d137955c..386dfa503f 100644 --- a/crates/shielded_token/src/lib.rs +++ b/crates/shielded_token/src/lib.rs @@ -22,6 +22,8 @@ mod storage; pub mod storage_key; pub mod utils; pub mod validation; +#[cfg(any(test, feature = "validation", feature = "testing"))] +pub mod vp; use std::str::FromStr; diff --git a/crates/shielded_token/src/vp.rs b/crates/shielded_token/src/vp.rs index 4b6aa32226..1cd351331c 100644 --- a/crates/shielded_token/src/vp.rs +++ b/crates/shielded_token/src/vp.rs @@ -2,6 +2,7 @@ use std::cmp::Ordering; use std::collections::{BTreeMap, BTreeSet}; +use std::marker::PhantomData; use masp_primitives::asset_type::AssetType; use masp_primitives::merkle_tree::CommitmentTree; @@ -14,6 +15,7 @@ use masp_primitives::transaction::{Transaction, TransparentAddress}; use namada_core::address::Address; use namada_core::arith::{checked, CheckedAdd, CheckedSub}; use namada_core::booleans::BoolResultUnitExt; +use namada_core::borsh::BorshSerializeExt; use namada_core::collections::HashSet; use namada_core::ibc::apps::nft_transfer::types::msgs::transfer::MsgTransfer as IbcMsgNftTransfer; use namada_core::ibc::apps::nft_transfer::types::packet::PacketData as NftPacketData; @@ -21,52 +23,31 @@ use namada_core::ibc::apps::transfer::types::msgs::transfer::MsgTransfer as IbcM use namada_core::ibc::apps::transfer::types::packet::PacketData; use namada_core::masp::{addr_taddr, encode_asset_type, ibc_taddr, MaspEpoch}; use namada_core::storage::Key; -use namada_governance::storage::is_proposal_accepted; -use namada_ibc::core::channel::types::commitment::{ - compute_packet_commitment, PacketCommitment, +use namada_core::token::MaspDigitPos; +use namada_core::uint::I320; +use namada_core::{governance, parameters, token}; +use namada_state::{ + ConversionState, OptionExt, ResultExt, StateRead, StorageError, }; -use namada_ibc::core::channel::types::msgs::{ - MsgRecvPacket as IbcMsgRecvPacket, PacketMsg, +use namada_trans_token::read_denom; +use namada_trans_token::storage_key::{ + is_any_shielded_action_balance_key, ShieldedActionOwner, }; -use namada_ibc::core::channel::types::timeout::TimeoutHeight; -use namada_ibc::core::handler::types::msgs::MsgEnvelope; -use namada_ibc::core::host::types::identifiers::{ChannelId, PortId, Sequence}; -use namada_ibc::primitives::Timestamp; -use namada_ibc::trace::{ - convert_to_address, ibc_trace_for_nft, is_sender_chain_source, -}; -use namada_ibc::{ - extract_masp_tx_from_envelope, get_last_sequence_send, IbcMessage, -}; -use namada_sdk::masp::TAddrData; -use namada_state::{ConversionState, OptionExt, ResultExt, StateRead}; -use namada_token::read_denom; -use namada_token::validation::verify_shielded_tx; use namada_tx::action::Read; use namada_tx::BatchedTxRef; -use namada_vp_env::VpEnv; +use namada_vp::native_vp::{Ctx, CtxPreStorageRead, NativeVp, VpEvaluator}; +use namada_vp::{native_vp, VpEnv}; +use ripemd::Digest as RipemdDigest; +use sha2::Digest as Sha2Digest; use thiserror::Error; -use token::storage_key::{ - balance_key, is_any_token_balance_key, is_masp_key, is_masp_nullifier_key, - is_masp_token_map_key, is_masp_transfer_key, masp_commitment_anchor_key, - masp_commitment_tree_key, masp_convert_anchor_key, masp_nullifier_key, -}; use token::Amount; -use crate::address::{IBC, MASP}; -use crate::ledger::ibc::storage; -use crate::ledger::ibc::storage::{commitment_key, receipt_key}; -use crate::ledger::native_vp; -use crate::ledger::native_vp::{Ctx, NativeVp}; -use crate::sdk::ibc::apps::transfer::types::{ack_success_b64, PORT_ID_STR}; -use crate::sdk::ibc::core::channel::types::acknowledgement::AcknowledgementStatus; -use crate::sdk::ibc::core::channel::types::commitment::{ - compute_ack_commitment, AcknowledgementCommitment, +use crate::storage_key::{ + is_masp_key, is_masp_nullifier_key, is_masp_token_map_key, + is_masp_transfer_key, masp_commitment_anchor_key, masp_commitment_tree_key, + masp_convert_anchor_key, masp_nullifier_key, }; -use crate::token; -use crate::token::MaspDigitPos; -use crate::uint::I320; -use crate::vm::WasmCacheAccess; +use crate::validation::verify_shielded_tx; #[allow(missing_docs)] #[derive(Error, Debug)] @@ -79,13 +60,17 @@ pub enum Error { pub type Result = std::result::Result; /// MASP VP -pub struct MaspVp<'a, S, CA> +pub struct MaspVp<'a, S, CA, EVAL, Params, Gov> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA>, + pub ctx: Ctx<'a, S, CA, EVAL>, + /// Governance type + pub gov: PhantomData, + /// Parameters type + pub params: PhantomData, } // The balances changed by the transaction, split between masp and non-masp @@ -101,7 +86,6 @@ struct ChangedBalances { post: BTreeMap>, } -/// IBC transfer info struct IbcTransferInfo { src_port_id: PortId, src_channel_id: ChannelId, @@ -173,14 +157,23 @@ impl TryFrom for IbcTransferInfo { } } -impl<'a, S, CA> MaspVp<'a, S, CA> +impl<'a, S, CA, EVAL, Params, Gov> MaspVp<'a, S, CA, EVAL, Params, Gov> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + Params: parameters::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, + Gov: governance::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, { /// Return if the parameter change was done via a governance proposal pub fn is_valid_parameter_change( - &self, + &'a self, tx: &BatchedTxRef<'_>, ) -> Result<()> { tx.tx.data(tx.cmt).map_or_else( @@ -191,7 +184,7 @@ where .into()) }, |data| { - is_proposal_accepted(&self.ctx.pre(), data.as_ref()) + Gov::is_proposal_accepted(&self.ctx.pre(), data.as_ref()) .map_err(Error::NativeVpError)? .ok_or_else(|| { native_vp::Error::new_const( @@ -340,7 +333,7 @@ where // Check that the convert descriptions anchors of a transaction are valid fn valid_convert_descriptions_anchor( - &self, + &'a self, transaction: &Transaction, ) -> Result<()> { if let Some(bundle) = transaction.sapling_bundle() { @@ -747,7 +740,7 @@ where // Check that MASP Transaction and state changes are valid fn is_valid_masp_transfer( - &self, + &'a self, tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -1275,15 +1268,25 @@ fn verify_sapling_balancing_value( } } -impl<'a, S, CA> NativeVp for MaspVp<'a, S, CA> +impl<'a, S, CA, EVAL, Params, Gov> NativeVp<'a> + for MaspVp<'a, S, CA, EVAL, Params, Gov> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + Params: parameters::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, + Gov: governance::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, { type Error = Error; fn validate_tx( - &self, + &'a self, tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, From 6ec78719d0f5d6fa7fbcbe501669aaa900c9df51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 17:28:46 +0100 Subject: [PATCH 20/41] mv crates/namada/src/ledger/native_vp/multitoken.rs crates/trans_token/src/vp.rs --- .../src/ledger/native_vp/multitoken.rs => trans_token/src/vp.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/native_vp/multitoken.rs => trans_token/src/vp.rs} (100%) diff --git a/crates/namada/src/ledger/native_vp/multitoken.rs b/crates/trans_token/src/vp.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/multitoken.rs rename to crates/trans_token/src/vp.rs From b6fabdf8d8bb2469d2adc107ac64e6c458e8bbc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:07:25 +0100 Subject: [PATCH 21/41] trans_token: fix VP post move --- Cargo.lock | 11 ++ crates/core/src/parameters.rs | 4 + crates/parameters/src/lib.rs | 4 + crates/token/src/lib.rs | 11 ++ crates/trans_token/Cargo.toml | 15 +++ crates/trans_token/src/lib.rs | 1 + crates/trans_token/src/vp.rs | 211 +++++++++++++++++++++------------- 7 files changed, 179 insertions(+), 78 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c4134c1bd..d960250606 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5276,11 +5276,22 @@ dependencies = [ name = "namada_trans_token" version = "0.41.0" dependencies = [ + "assert_matches", "konst", "linkme", "namada_core", "namada_events", + "namada_gas", + "namada_governance", + "namada_ibc", + "namada_parameters", + "namada_state", "namada_storage", + "namada_tx", + "namada_vm", + "namada_vp", + "thiserror", + "tracing", ] [[package]] diff --git a/crates/core/src/parameters.rs b/crates/core/src/parameters.rs index a5bb9a8338..1ce88e8d84 100644 --- a/crates/core/src/parameters.rs +++ b/crates/core/src/parameters.rs @@ -40,6 +40,10 @@ pub trait Read { fn max_signatures_per_transaction( storage: &S, ) -> Result, Self::Err>; + + /// Helper function to retrieve the `is_native_token_transferable` protocol + /// parameter from storage + fn is_native_token_transferable(storage: &S) -> Result; } /// Abstract parameters storage write interface diff --git a/crates/parameters/src/lib.rs b/crates/parameters/src/lib.rs index 04a85bbd4e..b0e7df69df 100644 --- a/crates/parameters/src/lib.rs +++ b/crates/parameters/src/lib.rs @@ -69,6 +69,10 @@ where ) -> Result, Self::Err> { max_signatures_per_transaction(storage) } + + fn is_native_token_transferable(storage: &S) -> Result { + storage::is_native_token_transferable(storage) + } } impl Write for Store diff --git a/crates/token/src/lib.rs b/crates/token/src/lib.rs index 8a27109e41..0bffc87ea6 100644 --- a/crates/token/src/lib.rs +++ b/crates/token/src/lib.rs @@ -22,6 +22,17 @@ use namada_core::borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; use namada_macros::BorshDeserializer; pub use namada_shielded_token::*; pub use namada_trans_token::*; + +/// Validity predicates +#[cfg(any(test, feature = "validation", feature = "testing"))] +pub mod vp { + pub use namada_shielded_token::vp::{ + Error as MaspError, MaspVp, Result as MaspResult, + }; + pub use namada_trans_token::vp::{ + Error as MultitokenError, MultitokenVp, Result as MultitokenResult, + }; +} use serde::{Deserialize, Serialize}; /// Token storage keys diff --git a/crates/trans_token/Cargo.toml b/crates/trans_token/Cargo.toml index a2a6f38a98..4e187c6731 100644 --- a/crates/trans_token/Cargo.toml +++ b/crates/trans_token/Cargo.toml @@ -21,10 +21,25 @@ migrations = [ [dependencies] namada_core = { path = "../core" } namada_events = { path = "../events", default-features = false } +namada_state = { path = "../state" } namada_storage = { path = "../storage" } +namada_tx = { path = "../tx" } +namada_vp = { path = "../vp" } konst.workspace = true linkme = {workspace = true, optional = true} +thiserror.workspace = true +tracing.workspace = true [dev-dependencies] +namada_core = { path = "../core", features = ["testing"] } +namada_gas = { path = "../gas" } +namada_governance = { path = "../governance", features = ["testing"] } +namada_ibc = { path = "../ibc", features = ["testing"] } +namada_parameters = { path = "../parameters", features = ["testing"] } +namada_state = { path = "../state", features = ["testing"] } namada_storage = { path = "../storage", features = ["testing"] } +namada_tx = { path = "../tx", features = ["testing"] } +namada_vm = { path = "../vm", features = ["testing"] } + +assert_matches.workspace = true diff --git a/crates/trans_token/src/lib.rs b/crates/trans_token/src/lib.rs index 76451529a0..67115034cb 100644 --- a/crates/trans_token/src/lib.rs +++ b/crates/trans_token/src/lib.rs @@ -20,6 +20,7 @@ pub mod event; mod storage; pub mod storage_key; +pub mod vp; use std::marker::PhantomData; diff --git a/crates/trans_token/src/vp.rs b/crates/trans_token/src/vp.rs index 2f698df4ae..433a8cdd74 100644 --- a/crates/trans_token/src/vp.rs +++ b/crates/trans_token/src/vp.rs @@ -1,29 +1,30 @@ //! Native VP for multitokens use std::collections::BTreeSet; +use std::marker::PhantomData; +use namada_core::address::{Address, InternalAddress, GOV, POS}; use namada_core::booleans::BoolResultUnitExt; use namada_core::collections::HashMap; -use namada_governance::is_proposal_accepted; -use namada_parameters::storage::is_native_token_transferable; -use namada_state::StateRead; -use namada_token::storage_key::is_any_token_parameter_key; +use namada_core::storage::{Key, KeySeg}; +use namada_core::token::Amount; +use namada_core::{governance, parameters}; +use namada_state::{StateRead, StorageError}; +use namada_storage::StorageRead; use namada_tx::action::{ Action, Bond, ClaimRewards, GovAction, PosAction, Read, Withdraw, }; use namada_tx::BatchedTxRef; -use namada_vp_env::VpEnv; +use namada_vp::native_vp::{ + self, Ctx, CtxPreStorageRead, NativeVp, VpEvaluator, +}; +use namada_vp::VpEnv; use thiserror::Error; -use crate::address::{Address, InternalAddress, GOV, POS}; -use crate::ledger::native_vp::{self, Ctx, NativeVp}; -use crate::storage::{Key, KeySeg}; -use crate::token::storage_key::{ +use crate::storage_key::{ is_any_minted_balance_key, is_any_minter_key, is_any_token_balance_key, - minter_key, + is_any_token_parameter_key, minter_key, }; -use crate::token::Amount; -use crate::vm::WasmCacheAccess; /// The owner of some balance change. #[derive(Copy, Clone, Eq, PartialEq)] @@ -45,30 +46,44 @@ pub enum Error { pub type Result = std::result::Result; /// Multitoken VP -pub struct MultitokenVp<'a, S, CA> +pub struct MultitokenVp<'a, S, CA, EVAL, Params, Gov> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA>, + pub ctx: Ctx<'a, S, CA, EVAL>, + /// Parameters type + pub params: PhantomData, + /// Governance type + pub gov: PhantomData, } -impl<'a, S, CA> NativeVp for MultitokenVp<'a, S, CA> +impl<'a, S, CA, EVAL, Params, Gov> NativeVp<'a> + for MultitokenVp<'a, S, CA, EVAL, Params, Gov> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + Params: parameters::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, + Gov: governance::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, { type Error = Error; fn validate_tx( - &self, + &'a self, tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<()> { // Is VP triggered by a governance proposal? - if is_proposal_accepted( + if Gov::is_proposal_accepted( &self.ctx.pre(), tx_data.tx.data(tx_data.cmt).unwrap_or_default().as_ref(), ) @@ -77,9 +92,9 @@ where return Ok(()); } - let native_token = self.ctx.pre().ctx.get_native_token()?; + let native_token = self.ctx.pre().get_native_token()?; let is_native_token_transferable = - is_native_token_transferable(&self.ctx.pre())?; + Params::is_native_token_transferable(&self.ctx.pre())?; let actions = self.ctx.read_actions()?; // The native token can be transferred to and out of the `PoS` and `Gov` // accounts, even if `is_native_token_transferable` is false @@ -281,11 +296,29 @@ where } } -impl<'a, S, CA> MultitokenVp<'a, S, CA> +impl<'a, S, CA, EVAL, Params, Gov> MultitokenVp<'a, S, CA, EVAL, Params, Gov> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + Params: parameters::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, + Gov: governance::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, { + /// Instantiate token VP + pub fn new(ctx: Ctx<'a, S, CA, EVAL>) -> Self { + Self { + ctx, + params: PhantomData, + gov: PhantomData, + } + } + /// Return the minter if the minter is valid and the minter VP exists pub fn is_valid_minter( &self, @@ -323,7 +356,7 @@ where /// Return if the parameter change was done via a governance proposal pub fn is_valid_parameter( - &self, + &'a self, batched_tx: &BatchedTxRef<'_>, ) -> Result<()> { batched_tx.tx.data(batched_tx.cmt).map_or_else( @@ -334,7 +367,7 @@ where .into()) }, |data| { - is_proposal_accepted(&self.ctx.pre(), data.as_ref()) + Gov::is_proposal_accepted(&self.ctx.pre(), data.as_ref()) .map_err(Error::NativeVpError)? .ok_or_else(|| { native_vp::Error::new_const( @@ -403,28 +436,50 @@ mod tests { use std::cell::RefCell; use assert_matches::assert_matches; - use borsh_ext::BorshSerializeExt; - use namada_gas::TxGasMeter; + use namada_core::address::testing::{ + established_address_1, established_address_2, nam, + }; + use namada_core::borsh::BorshSerializeExt; + use namada_core::key::testing::keypair_1; + use namada_core::WasmCacheRwAccess; + use namada_gas::{TxGasMeter, VpGasMeter}; + use namada_ibc::storage::ibc_token; use namada_parameters::storage::get_native_token_transferable_key; use namada_state::testing::TestState; use namada_state::StorageWrite; + use namada_storage::TxIndex; use namada_tx::action::Write; use namada_tx::data::TxType; use namada_tx::{Authorization, BatchedTx, Code, Data, Section, Tx}; + use namada_vm::wasm::compilation_cache::common::testing::vp_cache; + use namada_vm::wasm::run::VpEvalWasm; + use namada_vm::wasm::VpCache; use super::*; - use crate::core::address::testing::{ - established_address_1, established_address_2, nam, - }; - use crate::key::testing::keypair_1; - use crate::ledger::gas::VpGasMeter; - use crate::ledger::ibc::trace::ibc_token; - use crate::storage::TxIndex; - use crate::token::storage_key::{balance_key, minted_balance_key}; - use crate::vm::wasm::compilation_cache::common::testing::cache as wasm_cache; + use crate::storage_key::{balance_key, minted_balance_key}; const ADDRESS: Address = Address::Internal(InternalAddress::Multitoken); + type CA = WasmCacheRwAccess; + type Eval = VpEvalWasm< + ::D, + ::H, + CA, + >; + type Ctx<'a> = super::Ctx<'a, TestState, VpCache, Eval>; + type MultitokenVp<'a> = super::MultitokenVp< + 'a, + TestState, + VpCache, + Eval, + namada_parameters::Store< + CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + >, + namada_governance::Store< + CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + >, + >; + fn init_state() -> TestState { let mut state = TestState::default(); namada_parameters::init_test_storage(&mut state).unwrap(); @@ -490,7 +545,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); verifiers.insert(src); let ctx = Ctx::new( @@ -502,10 +557,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers) .is_ok() @@ -532,7 +587,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let verifiers = BTreeSet::new(); let ctx = Ctx::new( &ADDRESS, @@ -543,10 +598,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers) .is_err() @@ -592,7 +647,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); // for the minter verifiers.insert(minter); @@ -607,10 +662,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers) .is_ok() @@ -654,7 +709,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); // for the minter verifiers.insert(minter); @@ -667,10 +722,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers) .is_err() @@ -709,7 +764,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let verifiers = BTreeSet::new(); let ctx = Ctx::new( &ADDRESS, @@ -720,10 +775,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers) .is_err() @@ -769,7 +824,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); // for the minter verifiers.insert(minter); @@ -782,10 +837,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers) .is_err() @@ -811,7 +866,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); // for the minter verifiers.insert(minter); @@ -824,10 +879,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers) .is_err() @@ -856,7 +911,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let verifiers = BTreeSet::new(); let ctx = Ctx::new( &ADDRESS, @@ -867,10 +922,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers) .is_err() @@ -893,7 +948,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); verifiers.insert(src); let ctx = Ctx::new( @@ -905,10 +960,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert_matches!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers), Err(_) @@ -938,7 +993,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); verifiers.insert(src); let ctx = Ctx::new( @@ -950,10 +1005,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert_matches!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers), Ok(_) @@ -981,7 +1036,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); verifiers.insert(src); let ctx = Ctx::new( @@ -993,10 +1048,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert_matches!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers), Err(_) @@ -1024,7 +1079,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); verifiers.insert(src); let ctx = Ctx::new( @@ -1036,10 +1091,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert_matches!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers), Ok(_) @@ -1072,7 +1127,7 @@ mod tests { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let (vp_wasm_cache, _vp_cache_dir) = wasm_cache(); + let (vp_vp_cache, _vp_cache_dir) = vp_cache(); let mut verifiers = BTreeSet::new(); verifiers.insert(src); let ctx = Ctx::new( @@ -1084,10 +1139,10 @@ mod tests { &gas_meter, &keys_changed, &verifiers, - vp_wasm_cache, + vp_vp_cache, ); - let vp = MultitokenVp { ctx }; + let vp = MultitokenVp::new(ctx); assert_matches!( vp.validate_tx(&tx.batch_ref_tx(&cmt), &keys_changed, &verifiers), Ok(_) From f8a6af471a96e466204052e5c505c745e48cbc1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:09:48 +0100 Subject: [PATCH 22/41] mv crates/namada/src/ledger/native_vp/parameters.rs crates/parameters/src/vp.rs --- .../src/ledger/native_vp/parameters.rs => parameters/src/vp.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/native_vp/parameters.rs => parameters/src/vp.rs} (100%) diff --git a/crates/namada/src/ledger/native_vp/parameters.rs b/crates/parameters/src/vp.rs similarity index 100% rename from crates/namada/src/ledger/native_vp/parameters.rs rename to crates/parameters/src/vp.rs From 41c2edee539f8f8bb4ab2ed9ca655ec82f511e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:15:46 +0100 Subject: [PATCH 23/41] parameters: fix VP post move --- Cargo.lock | 3 ++ crates/parameters/Cargo.toml | 3 ++ crates/parameters/src/lib.rs | 1 + crates/parameters/src/vp.rs | 61 +++++++++++++++++++++--------------- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d960250606..f76b4a1892 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5001,7 +5001,10 @@ version = "0.41.0" dependencies = [ "namada_core", "namada_macros", + "namada_state", "namada_storage", + "namada_tx", + "namada_vp", "smooth-operator", "thiserror", ] diff --git a/crates/parameters/Cargo.toml b/crates/parameters/Cargo.toml index 5b6c9f5e63..67ffbb957f 100644 --- a/crates/parameters/Cargo.toml +++ b/crates/parameters/Cargo.toml @@ -22,7 +22,10 @@ testing = [ [dependencies] namada_core = { path = "../core" } namada_macros = { path = "../macros" } +namada_state = { path = "../state" } namada_storage = { path = "../storage" } +namada_tx = { path = "../tx" } +namada_vp = { path = "../vp" } smooth-operator.workspace = true thiserror.workspace = true diff --git a/crates/parameters/src/lib.rs b/crates/parameters/src/lib.rs index b0e7df69df..ec598e3aba 100644 --- a/crates/parameters/src/lib.rs +++ b/crates/parameters/src/lib.rs @@ -18,6 +18,7 @@ )] pub mod storage; +pub mod vp; mod wasm_allowlist; use std::collections::BTreeMap; use std::marker::PhantomData; diff --git a/crates/parameters/src/vp.rs b/crates/parameters/src/vp.rs index 1529e7ca3f..4b7c76af7d 100644 --- a/crates/parameters/src/vp.rs +++ b/crates/parameters/src/vp.rs @@ -1,16 +1,20 @@ //! Native VP for protocol parameters use std::collections::BTreeSet; +use std::marker::PhantomData; use namada_core::address::Address; use namada_core::booleans::BoolResultUnitExt; +use namada_core::governance; use namada_core::storage::Key; -use namada_state::StateRead; +use namada_state::{StateRead, StorageError}; use namada_tx::BatchedTxRef; +use namada_vp::native_vp::{ + self, Ctx, CtxPreStorageRead, NativeVp, VpEvaluator, +}; use thiserror::Error; -use crate::ledger::native_vp::{self, Ctx, NativeVp}; -use crate::vm::WasmCacheAccess; +use crate::storage; #[allow(missing_docs)] #[derive(Error, Debug)] @@ -23,24 +27,31 @@ pub enum Error { pub type Result = std::result::Result; /// Parameters VP -pub struct ParametersVp<'a, S, CA> +pub struct ParametersVp<'a, S, CA, EVAL, Gov> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA>, + pub ctx: Ctx<'a, S, CA, EVAL>, + /// Governance type + pub gov: PhantomData, } -impl<'a, S, CA> NativeVp for ParametersVp<'a, S, CA> +impl<'a, S, CA, EVAL, Gov> NativeVp<'a> for ParametersVp<'a, S, CA, EVAL, Gov> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + Gov: governance::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, { type Error = Error; fn validate_tx( - &self, + &'a self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, @@ -57,19 +68,17 @@ where }; match key_type { KeyType::PARAMETER | KeyType::UNKNOWN_PARAMETER => { - namada_governance::storage::is_proposal_accepted( - &self.ctx.pre(), - &data, - ) - .map_err(Error::NativeVpError)? - .ok_or_else(|| { - native_vp::Error::new_alloc(format!( - "Attempted to change a protocol parameter from \ - outside of a governance proposal, or from a \ - non-accepted governance proposal: {key}", - )) - .into() - }) + Gov::is_proposal_accepted(&self.ctx.pre(), &data) + .map_err(Error::NativeVpError)? + .ok_or_else(|| { + native_vp::Error::new_alloc(format!( + "Attempted to change a protocol parameter \ + from outside of a governance proposal, or \ + from a non-accepted governance proposal: \ + {key}", + )) + .into() + }) } KeyType::UNKNOWN => Ok(()), } @@ -90,9 +99,9 @@ enum KeyType { impl From<&Key> for KeyType { fn from(value: &Key) -> Self { - if namada_parameters::storage::is_protocol_parameter_key(value) { + if storage::is_protocol_parameter_key(value) { KeyType::PARAMETER - } else if namada_parameters::storage::is_parameter_key(value) { + } else if storage::is_parameter_key(value) { KeyType::UNKNOWN_PARAMETER } else { KeyType::UNKNOWN From 22de51c499dcb1e25d64f2e15756860a3487e0fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:17:31 +0100 Subject: [PATCH 24/41] mv crates/namada/src/ledger/pgf/mod.rs crates/governance/src/vp/pgf.rs --- crates/{namada/src/ledger/pgf/mod.rs => governance/src/vp/pgf.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/pgf/mod.rs => governance/src/vp/pgf.rs} (100%) diff --git a/crates/namada/src/ledger/pgf/mod.rs b/crates/governance/src/vp/pgf.rs similarity index 100% rename from crates/namada/src/ledger/pgf/mod.rs rename to crates/governance/src/vp/pgf.rs From 82042a3569b939d5362c5e9a8e236d785ce268d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:32:26 +0100 Subject: [PATCH 25/41] gov/pgf: fix VP post move --- crates/governance/src/vp/mod.rs | 1 + crates/governance/src/vp/pgf.rs | 36 ++++++++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/crates/governance/src/vp/mod.rs b/crates/governance/src/vp/mod.rs index 7631811a6d..33797e0c58 100644 --- a/crates/governance/src/vp/mod.rs +++ b/crates/governance/src/vp/mod.rs @@ -1,5 +1,6 @@ //! Governance VP +pub mod pgf; pub mod utils; use std::collections::BTreeSet; diff --git a/crates/governance/src/vp/pgf.rs b/crates/governance/src/vp/pgf.rs index d7548c1cfb..9ab436c24e 100644 --- a/crates/governance/src/vp/pgf.rs +++ b/crates/governance/src/vp/pgf.rs @@ -3,18 +3,16 @@ use std::collections::BTreeSet; use namada_core::booleans::BoolResultUnitExt; -use namada_governance::pgf::storage::keys as pgf_storage; -use namada_governance::{is_proposal_accepted, pgf}; +use namada_core::storage::Key; use namada_state::StateRead; use namada_tx::action::{Action, PgfAction, Read}; use namada_tx::BatchedTxRef; +use namada_vp::native_vp::{self, Ctx, NativeVp, VpEvaluator}; use thiserror::Error; use crate::address::{Address, InternalAddress}; -use crate::ledger::native_vp; -use crate::ledger::native_vp::{Ctx, NativeVp}; -use crate::storage::Key; -use crate::vm::WasmCacheAccess; +use crate::pgf::storage::keys as pgf_storage; +use crate::{is_proposal_accepted, pgf}; /// for handling Pgf NativeVP errors pub type Result = std::result::Result; @@ -34,24 +32,25 @@ pub enum Error { } /// Pgf VP -pub struct PgfVp<'a, S, CA> +pub struct PgfVp<'a, S, CA, EVAL> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA>, + pub ctx: Ctx<'a, S, CA, EVAL>, } -impl<'a, S, CA> NativeVp for PgfVp<'a, S, CA> +impl<'a, S, CA, EVAL> NativeVp<'a> for PgfVp<'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, { type Error = Error; fn validate_tx( - &self, + &'a self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -208,14 +207,15 @@ where } } -impl<'a, S, CA> PgfVp<'a, S, CA> +impl<'a, S, CA, EVAL> PgfVp<'a, S, CA, EVAL> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, { /// Validate a governance parameter pub fn is_valid_parameter_change( - &self, + &'a self, batched_tx: &BatchedTxRef<'_>, ) -> Result<()> { batched_tx.tx.data(batched_tx.cmt).map_or_else( From 6378391ac8891caac9041cb72ce170b999b86757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:36:19 +0100 Subject: [PATCH 26/41] mv crates/namada/src/ledger/pos/vp.rs crates/proof_of_stake/src/vp.rs --- crates/{namada/src/ledger/pos => proof_of_stake/src}/vp.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/pos => proof_of_stake/src}/vp.rs (100%) diff --git a/crates/namada/src/ledger/pos/vp.rs b/crates/proof_of_stake/src/vp.rs similarity index 100% rename from crates/namada/src/ledger/pos/vp.rs rename to crates/proof_of_stake/src/vp.rs From 764ca0010d980c82fcfccf5f9b73611b9fb00f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:42:43 +0100 Subject: [PATCH 27/41] proof_of_stake: fix VP post move --- Cargo.lock | 2 + crates/proof_of_stake/Cargo.toml | 4 ++ crates/proof_of_stake/src/lib.rs | 2 +- crates/proof_of_stake/src/vp.rs | 68 +++++++++++++++++--------------- 4 files changed, 44 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f76b4a1892..88e0ce73cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5030,6 +5030,8 @@ dependencies = [ "namada_state", "namada_storage", "namada_trans_token", + "namada_tx", + "namada_vp", "once_cell", "pretty_assertions", "proptest", diff --git a/crates/proof_of_stake/Cargo.toml b/crates/proof_of_stake/Cargo.toml index 0bcb8a5c4d..72a1da3232 100644 --- a/crates/proof_of_stake/Cargo.toml +++ b/crates/proof_of_stake/Cargo.toml @@ -29,12 +29,16 @@ namada_events = { path = "../events", default-features = false } namada_governance = { path = "../governance" } namada_macros = { path = "../macros" } namada_migrations = { path = "../migrations", optional = true } +namada_state = { path = "../state" } namada_storage = { path = "../storage" } namada_parameters = { path = "../parameters" } namada_trans_token = { path = "../trans_token" } +namada_tx = { path = "../tx" } +namada_vp = { path = "../vp" } borsh.workspace = true konst.workspace = true +itertools.workspace = true linkme = {workspace = true, optional = true} once_cell.workspace = true proptest = { workspace = true, optional = true } diff --git a/crates/proof_of_stake/src/lib.rs b/crates/proof_of_stake/src/lib.rs index 3092549249..2f97947ce2 100644 --- a/crates/proof_of_stake/src/lib.rs +++ b/crates/proof_of_stake/src/lib.rs @@ -28,7 +28,7 @@ pub mod storage; pub mod storage_key; pub mod types; pub mod validator_set_update; -// pub mod validation; +pub mod vp; mod error; #[cfg(test)] diff --git a/crates/proof_of_stake/src/vp.rs b/crates/proof_of_stake/src/vp.rs index 5090636058..9bc67f27d0 100644 --- a/crates/proof_of_stake/src/vp.rs +++ b/crates/proof_of_stake/src/vp.rs @@ -1,26 +1,26 @@ //! Proof-of-Stake native validity predicate. use std::collections::{BTreeMap, BTreeSet}; +use std::marker::PhantomData; +use namada_core::address::Address; use namada_core::booleans::BoolResultUnitExt; -pub use namada_proof_of_stake; -pub use namada_proof_of_stake::parameters::PosParams; -use namada_proof_of_stake::storage::read_pos_params; -use namada_proof_of_stake::storage_key::is_params_key; -pub use namada_proof_of_stake::types; -use namada_proof_of_stake::types::BondId; -use namada_proof_of_stake::{storage_key, token}; -use namada_state::StateRead; +use namada_core::governance; +use namada_core::storage::Key; +use namada_state::{StateRead, StorageError}; use namada_tx::action::{ Action, Bond, ClaimRewards, PosAction, Read, Redelegation, Unbond, Withdraw, }; use namada_tx::BatchedTxRef; +use namada_vp::native_vp::{ + self, Ctx, CtxPreStorageRead, NativeVp, VpEvaluator, +}; use thiserror::Error; -use crate::address::Address; -use crate::ledger::native_vp::{self, Ctx, NativeVp}; -use crate::storage::Key; -use crate::vm::WasmCacheAccess; +use crate::storage::read_pos_params; +use crate::storage_key::is_params_key; +use crate::types::BondId; +use crate::{storage_key, token}; #[allow(missing_docs)] #[derive(Error, Debug)] @@ -37,24 +37,31 @@ pub enum Error { pub type Result = std::result::Result; /// Proof-of-Stake validity predicate -pub struct PosVP<'a, S, CA> +pub struct PosVP<'a, S, CA, EVAL, Gov> where - S: StateRead, - CA: WasmCacheAccess, + S: 'static + StateRead, + EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA>, + pub ctx: Ctx<'a, S, CA, EVAL>, + /// Governance type + pub gov: PhantomData, } -impl<'a, S, CA> NativeVp for PosVP<'a, S, CA> +impl<'a, S, CA, EVAL, Gov> NativeVp<'a> for PosVP<'a, S, CA, EVAL, Gov> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + Gov: governance::Read< + CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + Err = StorageError, + >, { type Error = Error; fn validate_tx( - &self, + &'a self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -65,12 +72,7 @@ where if batched_tx .tx .data(batched_tx.cmt) - .map(|tx_data| { - namada_governance::is_proposal_accepted( - &self.ctx.pre(), - &tx_data, - ) - }) + .map(|tx_data| Gov::is_proposal_accepted(&self.ctx.pre(), &tx_data)) .transpose() .map_err(Error::NativeVpError)? .unwrap_or(false) @@ -314,14 +316,18 @@ where } } -impl<'a, S, CA> PosVP<'a, S, CA> +impl<'a, S, CA, EVAL, Gov> PosVP<'a, S, CA, EVAL, Gov> where - S: StateRead, - CA: 'static + WasmCacheAccess, + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, { /// Instantiate a `PosVP`. - pub fn new(ctx: Ctx<'a, S, CA>) -> Self { - Self { ctx } + pub fn new(ctx: Ctx<'a, S, CA, EVAL>) -> Self { + Self { + ctx, + gov: PhantomData, + } } /// Return `Ok` if the changed parameters are valid From 343786f006a71e9431c34f638a2585a8c3415975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:48:44 +0100 Subject: [PATCH 28/41] namada: remove moved modules --- crates/namada/src/ledger/mod.rs | 256 ++++++++++++++------------------ 1 file changed, 112 insertions(+), 144 deletions(-) diff --git a/crates/namada/src/ledger/mod.rs b/crates/namada/src/ledger/mod.rs index 33a0754f69..0f75b0accf 100644 --- a/crates/namada/src/ledger/mod.rs +++ b/crates/namada/src/ledger/mod.rs @@ -1,160 +1,128 @@ //! The ledger modules -pub use namada_sdk::{eth_bridge, events}; -pub mod governance; -pub mod ibc; -pub mod native_vp; -pub mod pgf; -pub mod pos; -#[cfg(feature = "wasm-runtime")] -pub mod protocol; -pub use namada_sdk::queries; -pub mod storage; -pub mod vp_host_fns; - -#[cfg(feature = "wasm-runtime")] -pub use dry_run_tx::dry_run_tx; -pub use { - namada_gas as gas, namada_parameters as parameters, - namada_tx_env as tx_env, namada_vp_env as vp_env, -}; - -#[cfg(feature = "wasm-runtime")] -mod dry_run_tx { - use std::cell::RefCell; - - use namada_sdk::queries::{EncodedResponseQuery, RequestCtx, RequestQuery}; - use namada_state::{DBIter, ResultExt, StorageHasher, DB}; - use namada_tx::data::{DryRunResult, ExtendedTxResult, GasLimit, TxResult}; - - use super::protocol; - use crate::vm::wasm::{TxCache, VpCache}; - use crate::vm::WasmCacheAccess; - - /// Dry run a transaction - pub fn dry_run_tx<'a, D, H, CA>( - mut ctx: RequestCtx<'a, D, H, VpCache, TxCache>, - request: &RequestQuery, - ) -> namada_state::StorageResult - where - D: 'static + DB + for<'iter> DBIter<'iter> + Sync, - H: 'static + StorageHasher + Sync, - CA: 'static + WasmCacheAccess + Sync, - { - use borsh_ext::BorshSerializeExt; - use namada_gas::{GasMetering, TxGasMeter}; - use namada_tx::data::TxType; - use namada_tx::Tx; +use std::cell::RefCell; + +use namada_gas::Gas; +use namada_sdk::queries::{EncodedResponseQuery, RequestCtx, RequestQuery}; +use namada_state::{DBIter, ResultExt, StorageHasher, DB}; +use namada_tx::data::{DryRunResult, ExtendedTxResult, GasLimit, TxResult}; + +use super::protocol; +use crate::vm::wasm::{TxCache, VpCache}; +use crate::vm::WasmCacheAccess; + +/// Dry run a transaction +pub fn dry_run_tx<'a, D, H, CA>( + mut ctx: RequestCtx<'a, D, H, VpCache, TxCache>, + request: &RequestQuery, +) -> namada_state::StorageResult +where + D: 'static + DB + for<'iter> DBIter<'iter> + Sync, + H: 'static + StorageHasher + Sync, + CA: 'static + WasmCacheAccess + Sync, +{ + use borsh_ext::BorshSerializeExt; + use namada_gas::{GasMetering, TxGasMeter}; + use namada_tx::data::TxType; + use namada_tx::Tx; - use crate::ledger::protocol::ShellParams; - use crate::storage::TxIndex; + use crate::ledger::protocol::ShellParams; + use crate::storage::TxIndex; - let mut temp_state = ctx.state.with_temp_write_log(); - let tx = Tx::try_from(&request.data[..]).into_storage_result()?; - tx.validate_tx().into_storage_result()?; + let mut temp_state = ctx.state.with_temp_write_log(); + let tx = Tx::try_from(&request.data[..]).into_storage_result()?; + tx.validate_tx().into_storage_result()?; - let gas_scale = namada_parameters::get_gas_scale(ctx.state)?; + let gas_scale = namada_parameters::get_gas_scale(ctx.state)?; - // Wrapper dry run to allow estimating the gas cost of a transaction - let (wrapper_hash, extended_tx_result, tx_gas_meter) = - match tx.header().tx_type { - TxType::Wrapper(wrapper) => { - let gas_limit = wrapper - .gas_limit - .as_scaled_gas(gas_scale) - .into_storage_result()?; - let tx_gas_meter = RefCell::new(TxGasMeter::new(gas_limit)); - let mut shell_params = ShellParams::new( - &tx_gas_meter, - &mut temp_state, - &mut ctx.vp_wasm_cache, - &mut ctx.tx_wasm_cache, - ); - let tx_result = protocol::apply_wrapper_tx( - &tx, - &wrapper, - &request.data, - &TxIndex::default(), - &tx_gas_meter, - &mut shell_params, - None, - ) + // Wrapper dry run to allow estimating the gas cost of a transaction + let (wrapper_hash, extended_tx_result, tx_gas_meter) = + match tx.header().tx_type { + TxType::Wrapper(wrapper) => { + let gas_limit = wrapper + .gas_limit + .as_scaled_gas(gas_scale) .into_storage_result()?; - - temp_state.write_log_mut().commit_tx_to_batch(); - let available_gas = - tx_gas_meter.borrow().get_available_gas(); - ( - Some(tx.header_hash()), - tx_result, - TxGasMeter::new(available_gas), - ) - } - _ => { - // If dry run only the inner tx, use the max block gas as - // the gas limit - let max_block_gas = - namada_parameters::get_max_block_gas(ctx.state)?; - let gas_limit = GasLimit::from(max_block_gas) - .as_scaled_gas(gas_scale) - .into_storage_result()?; - ( - None, - TxResult::default().to_extended_result(None), - TxGasMeter::new(gas_limit), - ) - } - }; - - let ExtendedTxResult { - mut tx_result, - ref masp_tx_refs, - ref ibc_tx_data_refs, - } = extended_tx_result; - let tx_gas_meter = RefCell::new(tx_gas_meter); - for cmt in crate::ledger::protocol::get_batch_txs_to_execute( - &tx, - masp_tx_refs, - ibc_tx_data_refs, - ) { - let batched_tx = tx.batch_ref_tx(cmt); - let batched_tx_result = protocol::apply_wasm_tx( - &batched_tx, - &TxIndex(0), - ShellParams::new( + let tx_gas_meter = RefCell::new(TxGasMeter::new(gas_limit)); + let tx_result = protocol::apply_wrapper_tx( + &tx, + &wrapper, + &request.data, &tx_gas_meter, &mut temp_state, - &mut ctx.vp_wasm_cache, - &mut ctx.tx_wasm_cache, - ), - ); - let is_accepted = matches!(&batched_tx_result, Ok(result) if result.is_accepted()); - if is_accepted { + None, + ) + .into_storage_result()?; + temp_state.write_log_mut().commit_tx_to_batch(); - } else { - temp_state.write_log_mut().drop_tx(); + let available_gas = tx_gas_meter.borrow().get_available_gas(); + ( + Some(tx.header_hash()), + tx_result, + TxGasMeter::new(available_gas), + ) } - tx_result.insert_inner_tx_result( - wrapper_hash.as_ref(), - either::Right(cmt), - batched_tx_result, - ); + _ => { + // If dry run only the inner tx, use the max block gas as + // the gas limit + let max_block_gas = + namada_parameters::get_max_block_gas(ctx.state)?; + let gas_limit = GasLimit::from(max_block_gas) + .as_scaled_gas(gas_scale) + .into_storage_result()?; + ( + None, + TxResult::default().to_extended_result(None), + TxGasMeter::new(gas_limit), + ) + } + }; + + let ExtendedTxResult { + mut tx_result, + ref masp_tx_refs, + ref ibc_tx_data_refs, + } = extended_tx_result; + let tx_gas_meter = RefCell::new(tx_gas_meter); + for cmt in tx.commitments() { + let batched_tx = tx.batch_ref_tx(cmt); + let batched_tx_result = protocol::apply_wasm_tx( + batched_tx, + &TxIndex(0), + ShellParams::new( + &tx_gas_meter, + &mut temp_state, + &mut ctx.vp_wasm_cache, + &mut ctx.tx_wasm_cache, + ), + ); + let is_accepted = + matches!(&batched_tx_result, Ok(result) if result.is_accepted()); + if is_accepted { + temp_state.write_log_mut().commit_tx_to_batch(); + } else { + temp_state.write_log_mut().drop_tx(); } - // Account gas for both batch and wrapper - let gas_used = tx_gas_meter - .borrow() - .get_tx_consumed_gas() - .get_whole_gas_units(gas_scale); - let tx_result_string = tx_result.to_result_string(); - let dry_run_result = DryRunResult(tx_result_string, gas_used); - - Ok(EncodedResponseQuery { - data: dry_run_result.serialize_to_vec(), - proof: None, - info: Default::default(), - height: ctx.state.in_mem().get_last_block_height(), - }) + tx_result.insert_inner_tx_result( + wrapper_hash.as_ref(), + either::Right(cmt), + batched_tx_result, + ); } + // Account gas for both batch and wrapper + tx_result.gas_used = tx_gas_meter + .borrow() + .get_tx_consumed_gas() + .get_whole_gas_units(gas_scale); + let tx_result_string = tx_result.to_result_string(); + let dry_run_result = DryRunResult(tx_result_string, gas_used); + + Ok(EncodedResponseQuery { + data: dry_run_result.serialize_to_vec(), + proof: None, + info: Default::default(), + height: ctx.state.in_mem().get_last_block_height(), + }) } #[cfg(test)] From 306e25f81f127c4f33ef8ac639d6060267317117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:49:37 +0100 Subject: [PATCH 29/41] mv crates/namada/src/ledger/mod.rs crates/node/src/dry_run_tx.rs --- crates/{namada/src/ledger/mod.rs => node/src/dry_run_tx.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/{namada/src/ledger/mod.rs => node/src/dry_run_tx.rs} (100%) diff --git a/crates/namada/src/ledger/mod.rs b/crates/node/src/dry_run_tx.rs similarity index 100% rename from crates/namada/src/ledger/mod.rs rename to crates/node/src/dry_run_tx.rs From cff91fc61de6e3721d939eed6df1ee626821e8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 18:54:24 +0100 Subject: [PATCH 30/41] rm -r crates/namada --- crates/namada/.gitignore | 10 - crates/namada/Cargo.toml | 173 ------------------ .../ledger/storage/mod.txt | 7 - crates/namada/src/ledger/lib.rs | 2 - crates/namada/src/ledger/pos/mod.rs | 32 ---- crates/namada/src/ledger/storage/mod.rs | 3 - crates/namada/src/lib.rs | 54 ------ 7 files changed, 281 deletions(-) delete mode 100644 crates/namada/.gitignore delete mode 100644 crates/namada/Cargo.toml delete mode 100644 crates/namada/proptest-regressions/ledger/storage/mod.txt delete mode 100644 crates/namada/src/ledger/lib.rs delete mode 100644 crates/namada/src/ledger/pos/mod.rs delete mode 100644 crates/namada/src/ledger/storage/mod.rs delete mode 100644 crates/namada/src/lib.rs diff --git a/crates/namada/.gitignore b/crates/namada/.gitignore deleted file mode 100644 index 65d4c18e2d..0000000000 --- a/crates/namada/.gitignore +++ /dev/null @@ -1,10 +0,0 @@ -# Generated by Cargo -# will have compiled files and executables -debug/ -target/ - -# These are backup files generated by rustfmt -**/*.rs.bk - -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock \ No newline at end of file diff --git a/crates/namada/Cargo.toml b/crates/namada/Cargo.toml deleted file mode 100644 index 65ec56741c..0000000000 --- a/crates/namada/Cargo.toml +++ /dev/null @@ -1,173 +0,0 @@ -[package] -name = "namada" -description = "The main Namada library crate" -resolver = "2" -authors.workspace = true -edition.workspace = true -documentation.workspace = true -homepage.workspace = true -keywords.workspace = true -license.workspace = true -readme.workspace = true -repository.workspace = true -version.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[features] -default = ["wasm-runtime"] -mainnet = ["namada_core/mainnet"] -std = ["namada_sdk/std"] -wasm-runtime = [ - "parity-wasm", - "rayon", - "wasm-instrument", - "wasmer-cache", - "wasmer-compiler-singlepass", - "wasmer-vm", - "wasmer", -] -# Enable queries support for an async client -async-client = ["async-trait", "namada_sdk/async-client"] - -# Requires async traits to be safe to send across threads -async-send = [] - -# tendermint-rpc support -tendermint-rpc = [ - "async-client", - "dep:tendermint-rpc", - "namada_sdk/tendermint-rpc", -] -# tendermint-rpc HttpClient -http-client = ["tendermint-rpc/http-client"] - -# for integration tests and test utilities -testing = [ - "namada_core/testing", - "namada_ethereum_bridge/testing", - "namada_parameters/testing", - "namada_proof_of_stake/testing", - "namada_sdk/testing", - "namada_state/testing", - "namada_token/testing", - "async-client", - "proptest", - "tempfile", -] - -namada-sdk = [ - "tendermint-rpc", - "masp_primitives/transparent-inputs", - "namada_sdk/namada-sdk", - "namada_sdk/rand", -] - -namada-eth-bridge = [ - "namada_ethereum_bridge/namada-eth-bridge", - "namada_sdk/namada-eth-bridge", -] - -multicore = [ - "masp_proofs/multicore", - "namada_sdk/multicore", - "namada_token/multicore", -] -rand = ["namada_sdk/rand"] -migrations = [ - "namada_migrations", - "linkme", -] -benches = ["namada_core/benches", "namada_state/benches"] - -[dependencies] -namada_account = { path = "../account" } -namada_core = { path = "../core" } -namada_events = { path = "../events", default-features = false } -namada_ethereum_bridge = { path = "../ethereum_bridge", default-features = false } -namada_gas = { path = "../gas" } -namada_governance = { path = "../governance" } -namada_ibc = { path = "../ibc" } -namada_migrations = { path = "../migrations", optional = true } -namada_parameters = { path = "../parameters" } -namada_proof_of_stake = { path = "../proof_of_stake" } -namada_replay_protection = { path = "../replay_protection" } -namada_sdk = { path = "../sdk", default-features = false } -namada_state = { path = "../state" } -namada_token = { path = "../token" } -namada_tx = { path = "../tx" } -namada_tx_env = { path = "../tx_env" } -namada_vote_ext = { path = "../vote_ext" } -namada_vp_env = { path = "../vp_env" } - -async-trait = { version = "0.1.51", optional = true } -borsh.workspace = true -borsh-ext.workspace = true -clru.workspace = true -either.workspace = true -ethers.workspace = true -eyre.workspace = true -itertools.workspace = true -linkme = {workspace = true, optional = true} -masp_primitives.workspace = true -masp_proofs.workspace = true -parity-wasm = { version = "0.45.0", features = ["sign_ext"], optional = true } -proptest = { workspace = true, optional = true } -prost.workspace = true -rand.workspace = true -rayon = { version = "=1.5.3", optional = true } -ripemd.workspace = true -serde_json.workspace = true -sha2.workspace = true -smooth-operator.workspace = true -tempfile = { version = "3.2.0", optional = true } -tendermint-rpc = { workspace = true, optional = true } -thiserror.workspace = true -tiny-bip39.workspace = true -tracing.workspace = true -uint = "0.9.5" -wasm-instrument = { workspace = true, optional = true } -wasmer = { workspace = true, optional = true } -wasmer-cache = { workspace = true, optional = true } -wasmer-compiler-singlepass = { workspace = true, optional = true } -wasmer-vm = { workspace = true, optional = true } -wasmparser.workspace = true - -[target.'cfg(not(target_family = "wasm"))'.dependencies] -tokio = { workspace = true, features = ["full"] } - -[target.'cfg(target_family = "wasm")'.dependencies] -tokio = { workspace = true, default-features = false, features = ["sync"] } -wasmtimer = "0.2.0" - -[dev-dependencies] -namada_core = { path = "../core", default-features = false, features = [ - "testing", -] } -namada_parameters = { path = "../parameters", default-features = false, features = [ - "testing", -] } -namada_ethereum_bridge = { path = "../ethereum_bridge", default-features = false, features = [ - "testing", -] } -namada_proof_of_stake = { path = "../proof_of_stake", default-features = false, features = [ - "testing", -] } -namada_sdk = { path = "../sdk", features = ["std", "testing"] } -namada_state = { path = "../state", features = ["testing"] } -namada_test_utils = { path = "../test_utils" } - -assert_matches.workspace = true -async-trait.workspace = true -base58.workspace = true -byte-unit.workspace = true -ibc-testkit.workspace = true -k256.workspace = true -pretty_assertions.workspace = true -proptest.workspace = true -tempfile.workspace = true -test-log.workspace = true -tokio = { workspace = true, features = ["rt", "macros"] } -tracing-subscriber.workspace = true -wasmer-compiler = { workspace = true } -wasmer-types = { workspace = true } diff --git a/crates/namada/proptest-regressions/ledger/storage/mod.txt b/crates/namada/proptest-regressions/ledger/storage/mod.txt deleted file mode 100644 index c3826edd4b..0000000000 --- a/crates/namada/proptest-regressions/ledger/storage/mod.txt +++ /dev/null @@ -1,7 +0,0 @@ -# Seeds for failure cases proptest has generated in the past. It is -# automatically read and these particular cases re-run before any -# novel cases are generated. -# -# It is recommended to check this file in to source control so that -# everyone who runs the test benefits from these saved cases. -cc e02bfe6892bfa6d068daddc0abb2b66ae6d79b3bc4ebc345813df7e8574c78bd # shrinks to (epoch_duration, max_expected_time_per_block, start_height, start_time, block_height, block_time, min_blocks_delta, min_duration_delta, max_time_per_block_delta) = (EpochDuration { min_num_of_blocks: 1, min_duration: DurationSecs(1) }, 1, BlockHeight(0), DateTimeUtc(1970-01-01T00:00:00Z), BlockHeight(1), DateTimeUtc(1970-01-01T00:00:01Z), 0, 0, 0) diff --git a/crates/namada/src/ledger/lib.rs b/crates/namada/src/ledger/lib.rs deleted file mode 100644 index f72d4bc2e3..0000000000 --- a/crates/namada/src/ledger/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -//! This crate contains the trait for native validity predicates with its -//! various context types and host functions implementation. diff --git a/crates/namada/src/ledger/pos/mod.rs b/crates/namada/src/ledger/pos/mod.rs deleted file mode 100644 index 35015cb5c9..0000000000 --- a/crates/namada/src/ledger/pos/mod.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! Proof-of-Stake integration as a native validity predicate - -pub mod vp; - -use namada_core::address; -pub use namada_core::dec::Dec; -pub use namada_core::key::common; -pub use namada_proof_of_stake::parameters::{OwnedPosParams, PosParams}; -pub use namada_proof_of_stake::pos_queries::*; -pub use namada_proof_of_stake::storage::*; -#[cfg(any(test, feature = "testing"))] -pub use namada_proof_of_stake::test_utils; -pub use namada_proof_of_stake::types::into_tm_voting_power; -pub use namada_proof_of_stake::{staking_token_address, types}; -pub use vp::PosVP; -pub use {namada_proof_of_stake, namada_state}; - -use crate::address::{Address, InternalAddress}; -pub use crate::token; - -/// Address of the PoS account implemented as a native VP -pub const ADDRESS: Address = address::POS; - -/// Address of the PoS slash pool account -pub const SLASH_POOL_ADDRESS: Address = - Address::Internal(InternalAddress::PosSlashPool); - -/// Alias for a PoS type with the same name with concrete type parameters -pub type BondId = namada_proof_of_stake::types::BondId; - -/// Alias for a PoS type with the same name with concrete type parameters -pub type GenesisValidator = namada_proof_of_stake::types::GenesisValidator; diff --git a/crates/namada/src/ledger/storage/mod.rs b/crates/namada/src/ledger/storage/mod.rs deleted file mode 100644 index dcae9a776d..0000000000 --- a/crates/namada/src/ledger/storage/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! Ledger's state storage with key-value backed store and a merkle tree - -pub use namada_state::{write_log, PrefixIter, *}; diff --git a/crates/namada/src/lib.rs b/crates/namada/src/lib.rs deleted file mode 100644 index c7274fff70..0000000000 --- a/crates/namada/src/lib.rs +++ /dev/null @@ -1,54 +0,0 @@ -//! The shared code for the Namada ledger, gossip and wasms. - -#![doc(html_favicon_url = "https://dev.namada.net/master/favicon.png")] -#![doc(html_logo_url = "https://dev.namada.net/master/rustdoc-logo.png")] -#![deny(rustdoc::broken_intra_doc_links)] -#![deny(rustdoc::private_intra_doc_links)] -#![warn( - missing_docs, - rust_2018_idioms, - clippy::cast_sign_loss, - clippy::cast_possible_truncation, - clippy::cast_possible_wrap, - clippy::cast_lossless, - clippy::arithmetic_side_effects, - clippy::dbg_macro, - clippy::print_stdout, - clippy::print_stderr -)] - -// TODO(namada#3248): only re-export v037 `tendermint-rs` -pub use namada_core::{ - address, chain, dec, decode, encode, eth_abi, eth_bridge_pool, - ethereum_events, ethereum_structs, hash, internal, keccak, key, masp, - storage, string_encoding, tendermint, tendermint_proto, time, uint, - validity_predicate, voting_power, -}; -pub use namada_sdk::{control_flow, io}; -// TODO(namada#3248): only re-export v037 `tendermint-rs` -#[cfg(feature = "tendermint-rpc")] -pub use tendermint_rpc; -pub use { - bip39, namada_account as account, namada_core as core, - namada_ethereum_bridge as ethereum_bridge, namada_events as events, - namada_gas as gas, namada_governance as governance, namada_ibc as ibc, - namada_parameters as parameters, namada_proof_of_stake as proof_of_stake, - namada_replay_protection as replay_protection, namada_sdk as sdk, - namada_state as state, namada_token as token, namada_tx as tx, - namada_vote_ext as vote_ext, -}; - -pub mod ledger; -pub use namada_tx::proto; -pub mod vm; - -pub mod eth_bridge { - //! Namada Ethereum bridge re-exports. - pub use ethers; - pub use namada_core::ethereum_structs as structs; - pub use namada_ethereum_bridge::*; -} - -#[cfg(test)] -#[macro_use] -extern crate assert_matches; From 0e685f5580df2456727e5bb65c52e24589be3d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 19:00:23 +0100 Subject: [PATCH 31/41] vm + vp: add common crate attrs --- crates/vm/src/lib.rs | 17 +++++++++++++++++ crates/vp/src/lib.rs | 17 +++++++++++++++++ crates/vp/src/native_vp.rs | 4 +++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/vm/src/lib.rs b/crates/vm/src/lib.rs index 77c0d1329c..9489052eaa 100644 --- a/crates/vm/src/lib.rs +++ b/crates/vm/src/lib.rs @@ -1,5 +1,22 @@ //! Virtual machine modules for running transactions and validity predicates. +#![doc(html_favicon_url = "https://dev.namada.net/master/favicon.png")] +#![doc(html_logo_url = "https://dev.namada.net/master/rustdoc-logo.png")] +#![deny(rustdoc::broken_intra_doc_links)] +#![deny(rustdoc::private_intra_doc_links)] +#![warn( + missing_docs, + rust_2018_idioms, + clippy::cast_sign_loss, + clippy::cast_possible_truncation, + clippy::cast_possible_wrap, + clippy::cast_lossless, + clippy::arithmetic_side_effects, + clippy::dbg_macro, + clippy::print_stdout, + clippy::print_stderr +)] + use std::marker::PhantomData; use std::ptr::NonNull; diff --git a/crates/vp/src/lib.rs b/crates/vp/src/lib.rs index 0662fa432f..554e045082 100644 --- a/crates/vp/src/lib.rs +++ b/crates/vp/src/lib.rs @@ -1,6 +1,23 @@ //! This crate contains the trait for native validity predicates with its //! various context types and host functions implementation. +#![doc(html_favicon_url = "https://dev.namada.net/master/favicon.png")] +#![doc(html_logo_url = "https://dev.namada.net/master/rustdoc-logo.png")] +#![deny(rustdoc::broken_intra_doc_links)] +#![deny(rustdoc::private_intra_doc_links)] +#![warn( + missing_docs, + rust_2018_idioms, + clippy::cast_sign_loss, + clippy::cast_possible_truncation, + clippy::cast_possible_wrap, + clippy::cast_lossless, + clippy::arithmetic_side_effects, + clippy::dbg_macro, + clippy::print_stdout, + clippy::print_stderr +)] + pub mod native_vp; pub mod vp_host_fns; diff --git a/crates/vp/src/native_vp.rs b/crates/vp/src/native_vp.rs index 33065965cd..dd6dccb1f3 100644 --- a/crates/vp/src/native_vp.rs +++ b/crates/vp/src/native_vp.rs @@ -108,6 +108,7 @@ where S: 'static + StateRead, EVAL: VpEvaluator<'a, S, CA, EVAL>, { + /// The inner context pub ctx: &'view Ctx<'a, S, CA, EVAL>, } @@ -119,7 +120,8 @@ where S: 'static + StateRead, EVAL: VpEvaluator<'a, S, CA, EVAL>, { - ctx: &'view Ctx<'a, S, CA, EVAL>, + /// The inner context + pub ctx: &'view Ctx<'a, S, CA, EVAL>, } impl<'a, S, CA, EVAL> Ctx<'a, S, CA, EVAL> From 28b38c4eeb22daa1145dcd4368a07e04e28e4584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 19:06:36 +0100 Subject: [PATCH 32/41] remove all dependencies on namada crate --- Cargo.toml | 1 - Makefile | 3 +-- crates/apps/Cargo.toml | 4 +--- crates/apps_lib/Cargo.toml | 6 +----- crates/benches/Cargo.toml | 2 -- crates/encoding_spec/Cargo.toml | 2 -- crates/node/Cargo.toml | 6 +----- crates/tests/Cargo.toml | 10 +++------- wasm/tx_bond/Cargo.toml | 1 - wasm/tx_change_validator_commission/Cargo.toml | 1 - wasm/tx_redelegate/Cargo.toml | 1 - wasm/tx_unbond/Cargo.toml | 1 - wasm/tx_withdraw/Cargo.toml | 1 - wasm/vp_implicit/Cargo.toml | 1 - wasm/vp_user/Cargo.toml | 1 - 15 files changed, 7 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9091504c4d..e1b9095417 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ members = [ "crates/parameters", "crates/proof_of_stake", "crates/replay_protection", - "crates/namada", "crates/node", "crates/sdk", "crates/shielded_token", diff --git a/Makefile b/Makefile index 7a11dad712..280262f66b 100644 --- a/Makefile +++ b/Makefile @@ -30,8 +30,7 @@ endif audit-ignores += RUSTSEC-2021-0076 # Workspace crates -crates := namada -crates += namada_account +crates := namada_account crates += namada_apps crates += namada_apps_lib crates += namada_benchmarks diff --git a/crates/apps/Cargo.toml b/crates/apps/Cargo.toml index 80bf66df9c..32c14690da 100644 --- a/crates/apps/Cargo.toml +++ b/crates/apps/Cargo.toml @@ -47,11 +47,10 @@ path = "src/bin/namada-relayer/main.rs" default = ["migrations"] mainnet = ["namada_apps_lib/mainnet"] jemalloc = ["namada_node/jemalloc"] -migrations = ["namada/migrations", "namada_apps_lib/migrations"] +migrations = ["namada_apps_lib/migrations"] namada-eth-bridge = ["namada_apps_lib/namada-eth-bridge"] [dependencies] -namada = {path = "../namada"} namada_apps_lib = {path = "../apps_lib"} namada_node = {path = "../node"} @@ -68,7 +67,6 @@ tracing.workspace = true winapi.workspace = true [dev-dependencies] -namada = {path = "../namada", default-features = false, features = ["testing", "wasm-runtime"]} namada_test_utils = {path = "../test_utils"} assert_matches.workspace = true diff --git a/crates/apps_lib/Cargo.toml b/crates/apps_lib/Cargo.toml index 2eb8e919e8..ee138a188d 100644 --- a/crates/apps_lib/Cargo.toml +++ b/crates/apps_lib/Cargo.toml @@ -15,7 +15,7 @@ version.workspace = true [features] default = ["migrations"] mainnet = [ - "namada/mainnet", + "namada_sdk/mainnet", ] # for integration tests and test utilities testing = ["namada_test_utils", "lazy_static"] @@ -24,16 +24,13 @@ integration = [] migrations = [ "namada_migrations", "namada_sdk/migrations", - "namada/migrations", "linkme" ] namada-eth-bridge = [ - "namada/namada-eth-bridge", "namada_sdk/namada-eth-bridge", ] [dependencies] -namada = {path = "../namada", features = ["multicore", "http-client", "tendermint-rpc", "std"]} namada_macros = {path = "../macros"} namada_migrations = {path = "../migrations", optional = true} namada_sdk = {path = "../sdk", default-features = false, features = ["download-params", "std", "rand"]} @@ -83,7 +80,6 @@ tracing.workspace = true zeroize.workspace = true [dev-dependencies] -namada = {path = "../namada", default-features = false, features = ["testing", "wasm-runtime"]} namada_test_utils = {path = "../test_utils"} assert_matches.workspace = true diff --git a/crates/benches/Cargo.toml b/crates/benches/Cargo.toml index 7a4add83c8..947db3698b 100644 --- a/crates/benches/Cargo.toml +++ b/crates/benches/Cargo.toml @@ -34,7 +34,6 @@ path = "wasm_opcodes.rs" [features] namada-eth-bridge = [ - "namada/namada-eth-bridge", "namada_apps_lib/namada-eth-bridge", ] @@ -42,7 +41,6 @@ namada-eth-bridge = [ # NOTE: this crate MUST NOT import any dependency with testing features to prevent benchmarking non-production code [dev-dependencies] -namada = { path = "../namada", features = ["rand", "benches"] } namada_apps_lib = { path = "../apps_lib" } namada_node = { path = "../node", features = ["benches"] } masp_primitives.workspace = true diff --git a/crates/encoding_spec/Cargo.toml b/crates/encoding_spec/Cargo.toml index 519f0f8103..ddc59eb0e6 100644 --- a/crates/encoding_spec/Cargo.toml +++ b/crates/encoding_spec/Cargo.toml @@ -14,10 +14,8 @@ version.workspace = true [features] default = [] -namada-eth-bridge = ["namada/namada-eth-bridge"] [dependencies] -namada = { path = "../namada", features = ["rand", "tendermint-rpc"] } borsh.workspace = true itertools.workspace = true lazy_static.workspace = true diff --git a/crates/node/Cargo.toml b/crates/node/Cargo.toml index f7c0a5f6b3..d950683f03 100644 --- a/crates/node/Cargo.toml +++ b/crates/node/Cargo.toml @@ -15,7 +15,7 @@ version.workspace = true [features] default = ["migrations"] mainnet = [ - "namada/mainnet", + "namada_sdk/mainnet", ] # for integration tests and test utilities testing = [ @@ -35,16 +35,13 @@ jemalloc = ["rocksdb/jemalloc"] migrations = [ "namada_migrations", "namada_sdk/migrations", - "namada/migrations", "linkme", ] namada-eth-bridge = [ - "namada/namada-eth-bridge", "namada_sdk/namada-eth-bridge", ] [dependencies] -namada = {path = "../namada", features = ["multicore", "http-client", "tendermint-rpc", "std"]} namada_apps_lib = {path = "../apps_lib"} namada_migrations = {path = "../migrations", optional = true} namada_sdk = {path = "../sdk", default-features = false, features = ["download-params", "std", "rand"]} @@ -99,7 +96,6 @@ tracing.workspace = true warp = "0.3.2" [dev-dependencies] -namada = {path = "../namada", default-features = false, features = ["testing", "wasm-runtime"]} namada_apps_lib = {path = "../apps_lib", features = ["testing"]} namada_test_utils = {path = "../test_utils"} diff --git a/crates/tests/Cargo.toml b/crates/tests/Cargo.toml index 3534c4eaa2..3f8f60e561 100644 --- a/crates/tests/Cargo.toml +++ b/crates/tests/Cargo.toml @@ -13,25 +13,21 @@ repository.workspace = true version.workspace = true [features] -default = ["wasm-runtime", "migrations"] +default = ["migrations"] mainnet = [ - "namada/mainnet", + "namada_sdk/mainnet", ] -wasm-runtime = ["namada/wasm-runtime", "wasmer"] integration = ["namada_node/integration", "namada_apps_lib/integration"] migrations = [ - "namada/migrations", "namada_sdk/migrations", "namada_core/migrations", ] namada-eth-bridge = [ - "namada/namada-eth-bridge", "namada_sdk/namada-eth-bridge", "namada_apps_lib/namada-eth-bridge", ] [dependencies] -namada = {path = "../namada", features = ["testing"]} namada_core = {path = "../core", features = ["testing"]} namada_sdk = {path = "../sdk", default-features = false, features = ["tendermint-rpc", "download-params", "std", "rand"]} namada_test_utils = {path = "../test_utils"} @@ -52,7 +48,7 @@ sha2.workspace = true tempfile.workspace = true test-log.workspace = true tokio = {workspace = true, features = ["full"]} -wasmer = { workspace = true, optional = true } +wasmer.workspace = true [dev-dependencies] namada_apps_lib = {path = "../apps_lib", features = ["testing"]} diff --git a/wasm/tx_bond/Cargo.toml b/wasm/tx_bond/Cargo.toml index 50e0aaaa62..6d1e2c0919 100644 --- a/wasm/tx_bond/Cargo.toml +++ b/wasm/tx_bond/Cargo.toml @@ -15,7 +15,6 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada = {path = "../../crates/namada", default-features = false} namada_tests = {path = "../../crates/tests"} namada_test_utils = {path = "../../crates/test_utils"} namada_tx_prelude = { workspace = true, features = ["testing"] } diff --git a/wasm/tx_change_validator_commission/Cargo.toml b/wasm/tx_change_validator_commission/Cargo.toml index 5a6fae6f2e..e99a637027 100644 --- a/wasm/tx_change_validator_commission/Cargo.toml +++ b/wasm/tx_change_validator_commission/Cargo.toml @@ -15,7 +15,6 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada = {path = "../../crates/namada", default-features = false} namada_tests = {path = "../../crates/tests"} namada_test_utils = {path = "../../crates/test_utils"} namada_vp_prelude = {path = "../../crates/vp_prelude"} diff --git a/wasm/tx_redelegate/Cargo.toml b/wasm/tx_redelegate/Cargo.toml index 8d9a2c1d76..d59d338435 100644 --- a/wasm/tx_redelegate/Cargo.toml +++ b/wasm/tx_redelegate/Cargo.toml @@ -15,7 +15,6 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada = {path = "../../crates/namada", default-features = false} namada_tests = {path = "../../crates/tests"} namada_test_utils = {path = "../../crates/test_utils"} namada_tx_prelude = { workspace = true, features = ["testing"] } diff --git a/wasm/tx_unbond/Cargo.toml b/wasm/tx_unbond/Cargo.toml index 8184294d4b..9ede4f89fd 100644 --- a/wasm/tx_unbond/Cargo.toml +++ b/wasm/tx_unbond/Cargo.toml @@ -15,7 +15,6 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada = {path = "../../crates/namada", default-features = false} namada_tests = {path = "../../crates/tests"} namada_test_utils = {path = "../../crates/test_utils"} namada_tx_prelude = { workspace = true, features = ["testing"] } diff --git a/wasm/tx_withdraw/Cargo.toml b/wasm/tx_withdraw/Cargo.toml index ac84510764..74752fde48 100644 --- a/wasm/tx_withdraw/Cargo.toml +++ b/wasm/tx_withdraw/Cargo.toml @@ -15,7 +15,6 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada = {path = "../../crates/namada", default-features = false} namada_tests = {path = "../../crates/tests"} namada_test_utils = {path = "../../crates/test_utils"} namada_tx_prelude = { workspace = true, features = ["testing"] } diff --git a/wasm/vp_implicit/Cargo.toml b/wasm/vp_implicit/Cargo.toml index e46c65496c..099eb3c36e 100644 --- a/wasm/vp_implicit/Cargo.toml +++ b/wasm/vp_implicit/Cargo.toml @@ -16,7 +16,6 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada = {path = "../../crates/namada", default-features = false} namada_tests = {path = "../../crates/tests"} namada_test_utils = {path = "../../crates/test_utils"} namada_vp_prelude = {path = "../../crates/vp_prelude"} diff --git a/wasm/vp_user/Cargo.toml b/wasm/vp_user/Cargo.toml index 8b2a8e642f..9e131b0bc9 100644 --- a/wasm/vp_user/Cargo.toml +++ b/wasm/vp_user/Cargo.toml @@ -16,7 +16,6 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada = {path = "../../crates/namada", default-features = false} namada_tests = {path = "../../crates/tests"} namada_test_utils = {path = "../../crates/test_utils"} namada_vp_prelude = {path = "../../crates/vp_prelude"} From 50a95222435026a37ddb86a02983a1a3dcc872d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 17 Jun 2024 19:07:31 +0100 Subject: [PATCH 33/41] node: export dry_run_tx fn --- crates/node/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index a6aadd1f8e..a8bbd9f6c2 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -17,6 +17,7 @@ mod abortable; #[cfg(feature = "benches")] pub mod bench_utils; mod broadcaster; +mod dry_run_tx; pub mod ethereum_oracle; pub mod shell; pub mod shims; @@ -31,6 +32,7 @@ use std::thread; use byte_unit::Byte; use data_encoding::HEXUPPER; +pub use dry_run_tx::dry_run_tx; use futures::future::TryFutureExt; use namada::core::storage::BlockHeight; use namada::core::time::DateTimeUtc; From cc562736da6e491a771b2ec8565b8c5c77ea5df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 21 Jun 2024 15:19:09 +0100 Subject: [PATCH 34/41] mv crates/sdk/src/control_flow/ crates/core/src/ --- crates/{sdk => core}/src/control_flow/mod.rs | 0 crates/{sdk => core}/src/control_flow/time.rs | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename crates/{sdk => core}/src/control_flow/mod.rs (100%) rename crates/{sdk => core}/src/control_flow/time.rs (100%) diff --git a/crates/sdk/src/control_flow/mod.rs b/crates/core/src/control_flow/mod.rs similarity index 100% rename from crates/sdk/src/control_flow/mod.rs rename to crates/core/src/control_flow/mod.rs diff --git a/crates/sdk/src/control_flow/time.rs b/crates/core/src/control_flow/time.rs similarity index 100% rename from crates/sdk/src/control_flow/time.rs rename to crates/core/src/control_flow/time.rs From 32ed67febb0d496890feae3b728e84b98498084e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 21 Jun 2024 15:46:33 +0100 Subject: [PATCH 35/41] core: fix and feature guard control_flow after move from sdk --- crates/core/Cargo.toml | 6 ++++++ crates/core/src/control_flow/time.rs | 2 ++ crates/core/src/lib.rs | 2 ++ crates/sdk/src/lib.rs | 2 +- 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index c647dc1c90..7c06522f77 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -29,10 +29,12 @@ migrations = [ "linkme", ] benches = ["proptest"] +control_flow = ["futures", "tokio"] [dependencies] namada_macros = {path = "../macros"} namada_migrations = {path = "../migrations", optional = true} + arse-merkle-tree.workspace = true bech32.workspace = true borsh.workspace = true @@ -43,6 +45,7 @@ ed25519-consensus.workspace = true ethabi.workspace = true ethbridge-structs.workspace = true eyre.workspace = true +futures = { workspace = true, optional = true } ibc.workspace = true ics23.workspace = true impl-num-traits = "0.1.2" @@ -70,16 +73,19 @@ tendermint = {workspace = true} tendermint-proto = {workspace = true} thiserror.workspace = true tiny-keccak = {version = "2.0.2", features = ["keccak"]} +tokio = { workspace = true, optional = true, features = ["full"]} tracing.workspace = true uint = "0.9.5" zeroize.workspace = true [dev-dependencies] assert_matches.workspace = true +futures.workspace = true pretty_assertions.workspace = true proptest.workspace = true rand.workspace = true rand_core.workspace = true test-log.workspace = true +tokio = { workspace = true, features = ["full"] } toml.workspace = true tracing-subscriber.workspace = true diff --git a/crates/core/src/control_flow/time.rs b/crates/core/src/control_flow/time.rs index 95250fa137..91c7dbfbd9 100644 --- a/crates/core/src/control_flow/time.rs +++ b/crates/core/src/control_flow/time.rs @@ -1,5 +1,7 @@ //! Time related logic for futures. +#![allow(clippy::arithmetic_side_effects)] + use std::future::Future; use std::ops::ControlFlow; diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index c4e178f3b9..5a33d6c932 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -19,6 +19,8 @@ pub mod arith; pub mod bytes; +#[cfg(any(test, feature = "control_flow"))] +pub mod control_flow; pub mod governance; pub mod hints; pub mod proof_of_stake; diff --git a/crates/sdk/src/lib.rs b/crates/sdk/src/lib.rs index 79524fefc0..1b810c905d 100644 --- a/crates/sdk/src/lib.rs +++ b/crates/sdk/src/lib.rs @@ -34,7 +34,6 @@ pub mod signing; #[allow(clippy::result_large_err)] pub mod tx; -pub mod control_flow; pub mod error; pub mod events; pub(crate) mod internal_macros; @@ -55,6 +54,7 @@ use io::Io; use masp::{ShieldedContext, ShieldedUtils}; use namada_core::address::Address; use namada_core::collections::HashSet; +pub use namada_core::control_flow; use namada_core::dec::Dec; use namada_core::ethereum_events::EthAddress; use namada_core::ibc::core::host::types::identifiers::{ChannelId, PortId}; From 03c43baf1fb70eda9e130d9664f13ead814120f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 21 Jun 2024 15:47:53 +0100 Subject: [PATCH 36/41] sdk: add native VPs with filled-in concrete system types --- crates/sdk/src/lib.rs | 2 + crates/sdk/src/validation.rs | 119 +++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 crates/sdk/src/validation.rs diff --git a/crates/sdk/src/lib.rs b/crates/sdk/src/lib.rs index 1b810c905d..3cad286087 100644 --- a/crates/sdk/src/lib.rs +++ b/crates/sdk/src/lib.rs @@ -33,6 +33,8 @@ pub mod masp; pub mod signing; #[allow(clippy::result_large_err)] pub mod tx; +#[cfg(any(test, feature = "testing", feature = "validation"))] +pub mod validation; pub mod error; pub mod events; diff --git a/crates/sdk/src/validation.rs b/crates/sdk/src/validation.rs new file mode 100644 index 0000000000..2ed8ed0210 --- /dev/null +++ b/crates/sdk/src/validation.rs @@ -0,0 +1,119 @@ +//! Validity predictates dependency injection soup. In here, we're assigning +//! concrete types for generic type params of native VPs. + +use namada_vm::wasm::run::VpEvalWasm; +use namada_vm::wasm::VpCache; +use namada_vp::native_vp::{self, CtxPreStorageRead}; + +use crate::state::StateRead; +use crate::{eth_bridge, governance, ibc, parameters, proof_of_stake, token}; + +/// Native VP context +pub type NativeVpCtx<'a, S, CA> = + native_vp::Ctx<'a, S, VpCache, Eval>; + +/// VP WASM evaluator +type Eval = VpEvalWasm<::D, ::H, CA>; + +/// Native PoS VP +pub type PosVp<'a, S, CA> = proof_of_stake::vp::PosVp< + 'a, + S, + VpCache, + Eval, + GovPreStore<'a, S, CA>, +>; + +/// Native IBC VP +pub type IbcVp<'a, S, CA> = ibc::vp::Ibc< + 'a, + S, + VpCache, + Eval, + ParamsPreStore<'a, S, CA>, + GovPreStore<'a, S, CA>, + token::Store< + ibc::vp::context::PseudoExecutionStorage< + 'a, + 'a, + S, + VpCache, + Eval, + >, + >, + PosPreStore<'a, S, CA>, +>; + +/// Native parameters VP +pub type ParametersVp<'a, S, CA> = parameters::vp::ParametersVp< + 'a, + S, + VpCache, + Eval, + GovPreStore<'a, S, CA>, +>; + +/// Native governance VP +pub type GovernanceVp<'a, S, CA> = governance::vp::GovernanceVp< + 'a, + S, + VpCache, + Eval, + PosPreStore<'a, S, CA>, + TokenKeys, +>; + +/// Native PGF VP +pub type PgfVp<'a, S, CA> = + governance::vp::pgf::PgfVp<'a, S, VpCache, Eval>; + +/// Native multitoken VP +pub type MultitokenVp<'a, S, CA> = token::vp::MultitokenVp< + 'a, + S, + VpCache, + Eval, + ParamsPreStore<'a, S, CA>, + GovPreStore<'a, S, CA>, +>; + +/// Native MASP VP +pub type MaspVp<'a, S, CA> = token::vp::MaspVp< + 'a, + S, + VpCache, + Eval, + ParamsPreStore<'a, S, CA>, + GovPreStore<'a, S, CA>, +>; + +/// Native ETH bridge VP +pub type EthBridgeVp<'a, S, CA> = + eth_bridge::vp::EthBridge<'a, S, VpCache, Eval, TokenKeys>; + +/// Native ETH bridge pool VP +pub type EthBridgePoolVp<'a, S, CA> = + eth_bridge::vp::BridgePool<'a, S, VpCache, Eval, TokenKeys>; + +/// Native ETH bridge NUT VP +pub type EthBridgeNutVp<'a, S, CA> = + eth_bridge::vp::NonUsableTokens<'a, S, VpCache, Eval, TokenKeys>; + +/// Governance store implementation over the native prior context +pub type GovPreStore<'a, S, CA> = + governance::Store, Eval>>; + +/// Parameters store implementation over the native prior context +pub type ParamsPreStore<'a, S, CA> = + parameters::Store, Eval>>; + +/// PoS store implementation over the native prior context +pub type PosPreStore<'a, S, CA> = proof_of_stake::Store< + CtxPreStorageRead<'a, 'a, S, VpCache, Eval>, +>; + +/// Token storage keys implementation +pub type TokenKeys = token::Store<()>; + +/// Parameters storage keys implementation +pub type ParamKeys = parameters::Store<()>; From 32386240f46a08c98a976351e45811091c0edd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 21 Jun 2024 15:52:34 +0100 Subject: [PATCH 37/41] fix everything that was depending on rm'd namada crate --- Cargo.lock | 106 +--- Cargo.toml | 1 + Makefile | 8 +- crates/apps/src/bin/namada-node/cli.rs | 8 +- crates/apps/src/bin/namada-relayer/main.rs | 2 +- crates/apps_lib/Cargo.toml | 11 +- crates/apps_lib/src/cli.rs | 36 +- crates/apps_lib/src/cli/api.rs | 8 +- crates/apps_lib/src/cli/client.rs | 2 +- crates/apps_lib/src/cli/context.rs | 17 +- crates/apps_lib/src/cli/relayer.rs | 2 +- crates/apps_lib/src/cli/utils.rs | 8 +- crates/apps_lib/src/cli/wallet.rs | 11 +- crates/apps_lib/src/client/rpc.rs | 140 +++--- crates/apps_lib/src/client/tx.rs | 62 +-- crates/apps_lib/src/client/utils.rs | 14 +- .../src/config/ethereum_bridge/ledger.rs | 2 +- crates/apps_lib/src/config/genesis.rs | 45 +- crates/apps_lib/src/config/genesis/chain.rs | 58 ++- .../apps_lib/src/config/genesis/templates.rs | 27 +- .../src/config/genesis/transactions.rs | 42 +- crates/apps_lib/src/config/genesis/utils.rs | 6 +- crates/apps_lib/src/config/global.rs | 2 +- crates/apps_lib/src/config/mod.rs | 10 +- crates/apps_lib/src/lib.rs | 7 +- crates/apps_lib/src/tendermint_node.rs | 2 +- crates/apps_lib/src/wallet/defaults.rs | 17 +- crates/apps_lib/src/wallet/mod.rs | 6 +- crates/apps_lib/src/wallet/pre_genesis.rs | 2 +- crates/apps_lib/src/wallet/store.rs | 4 +- crates/apps_lib/src/wasm_loader/mod.rs | 2 +- crates/benches/Cargo.toml | 7 +- crates/benches/README.md | 2 +- crates/benches/host_env.rs | 25 +- crates/benches/native_vps.rs | 450 +++++++++-------- crates/benches/process_wrapper.rs | 36 +- crates/core/Cargo.toml | 11 +- .../ledger/storage/mod.txt | 2 - .../ledger/storage/wl_storage.txt | 8 - crates/core/src/lib.rs | 2 - crates/core/src/parameters.rs | 5 - crates/core/src/storage.rs | 2 +- crates/core/src/wasm_cache.rs | 25 - crates/encoding_spec/Cargo.toml | 5 + crates/encoding_spec/src/main.rs | 16 +- .../ethereum_bridge/src/vp/bridge_pool_vp.rs | 169 ++++--- .../ethereum_bridge/src/vp/eth_bridge_vp.rs | 98 ++-- crates/ethereum_bridge/src/vp/mod.rs | 6 +- crates/ethereum_bridge/src/vp/nut_vp.rs | 37 +- crates/governance/src/vp/mod.rs | 55 +-- crates/governance/src/vp/pgf.rs | 27 +- crates/ibc/src/context/common.rs | 6 +- crates/ibc/src/context/validation.rs | 3 +- crates/ibc/src/vp/mod.rs | 88 ++-- crates/node/Cargo.toml | 7 +- crates/node/src/abortable.rs | 2 +- crates/node/src/bench_utils.rs | 166 ++++--- crates/node/src/broadcaster.rs | 4 +- crates/node/src/dry_run_tx.rs | 138 +++--- crates/node/src/ethereum_oracle/control.rs | 2 +- crates/node/src/ethereum_oracle/events.rs | 16 +- crates/node/src/ethereum_oracle/mod.rs | 25 +- .../test_tools/events_endpoint.rs | 2 +- .../src/ethereum_oracle/test_tools/mod.rs | 8 +- crates/node/src/lib.rs | 23 +- crates/node/src/protocol.rs | 274 ++++++----- crates/node/src/shell/block_alloc.rs | 7 +- crates/node/src/shell/finalize_block.rs | 451 +++++++++--------- crates/node/src/shell/governance.rs | 50 +- crates/node/src/shell/init_chain.rs | 51 +- crates/node/src/shell/mod.rs | 186 ++++---- crates/node/src/shell/prepare_proposal.rs | 123 ++--- crates/node/src/shell/process_proposal.rs | 67 +-- crates/node/src/shell/queries.rs | 54 ++- crates/node/src/shell/snapshots.rs | 4 +- crates/node/src/shell/stats.rs | 2 +- crates/node/src/shell/testing/client.rs | 2 +- crates/node/src/shell/testing/node.rs | 130 ++--- crates/node/src/shell/testing/utils.rs | 2 +- crates/node/src/shell/utils.rs | 4 +- crates/node/src/shell/vote_extensions.rs | 18 +- .../shell/vote_extensions/bridge_pool_vext.rs | 47 +- .../src/shell/vote_extensions/eth_events.rs | 40 +- .../shell/vote_extensions/val_set_update.rs | 32 +- crates/node/src/shims/abcipp_shim.rs | 14 +- crates/node/src/shims/abcipp_shim_types.rs | 20 +- crates/node/src/storage/mod.rs | 53 +- crates/node/src/storage/rocksdb.rs | 46 +- crates/node/src/tendermint_node.rs | 6 +- crates/parameters/src/lib.rs | 6 - crates/parameters/src/vp.rs | 38 +- crates/proof_of_stake/src/lib.rs | 5 +- crates/proof_of_stake/src/vp.rs | 42 +- crates/sdk/Cargo.toml | 39 +- crates/sdk/src/lib.rs | 12 +- crates/sdk/src/masp.rs | 39 +- crates/sdk/src/masp/test_utils.rs | 1 - crates/sdk/src/migrations.rs | 25 +- crates/sdk/src/queries/mod.rs | 2 - crates/sdk/src/queries/router.rs | 5 +- crates/sdk/src/queries/shell.rs | 1 - crates/sdk/src/queries/vp/pos.rs | 1 - crates/sdk/src/queries/vp/token.rs | 1 - crates/sdk/src/validation.rs | 32 +- crates/shielded_token/Cargo.toml | 1 - crates/shielded_token/src/lib.rs | 1 - crates/shielded_token/src/vp.rs | 54 ++- crates/state/src/lib.rs | 1 - crates/state/src/wl_state.rs | 24 + crates/state/src/write_log.rs | 2 +- crates/tests/Cargo.toml | 12 +- crates/tests/src/e2e/eth_bridge_tests.rs | 18 +- .../tests/src/e2e/eth_bridge_tests/helpers.rs | 8 +- crates/tests/src/e2e/helpers.rs | 12 +- crates/tests/src/e2e/ibc_tests.rs | 96 ++-- crates/tests/src/e2e/ledger_tests.rs | 8 +- .../tests/src/e2e/multitoken_tests/helpers.rs | 2 +- crates/tests/src/e2e/setup.rs | 2 +- crates/tests/src/integration/ledger_tests.rs | 23 +- crates/tests/src/integration/masp.rs | 12 +- crates/tests/src/integration/setup.rs | 4 +- crates/tests/src/lib.rs | 6 +- crates/tests/src/native_vp/eth_bridge_pool.rs | 33 +- crates/tests/src/native_vp/mod.rs | 53 +- crates/tests/src/native_vp/pos.rs | 47 +- crates/tests/src/storage.rs | 2 +- .../src/storage_api/collections/lazy_map.rs | 4 +- .../src/storage_api/collections/lazy_set.rs | 4 +- .../src/storage_api/collections/lazy_vec.rs | 4 +- .../collections/nested_lazy_map.rs | 4 +- crates/tests/src/vm_host_env/ibc.rs | 148 +++--- crates/tests/src/vm_host_env/mod.rs | 34 +- crates/tests/src/vm_host_env/tx.rs | 50 +- crates/tests/src/vm_host_env/vp.rs | 59 +-- crates/token/Cargo.toml | 1 + crates/token/src/lib.rs | 1 - crates/trans_token/src/vp.rs | 54 +-- crates/tx_prelude/Cargo.toml | 1 + crates/tx_prelude/src/ibc.rs | 2 +- crates/tx_prelude/src/lib.rs | 5 +- crates/tx_prelude/src/proof_of_stake.rs | 2 +- crates/vm/Cargo.toml | 5 +- crates/vm/src/host_env.rs | 56 ++- crates/vm/src/lib.rs | 26 + .../vm/src/wasm/compilation_cache/common.rs | 8 +- crates/vm/src/wasm/host_env.rs | 7 +- crates/vm/src/wasm/memory.rs | 2 +- crates/vm/src/wasm/run.rs | 18 +- crates/vp/src/native_vp.rs | 24 +- examples/Cargo.toml | 2 +- genesis/starter/parameters.toml | 2 - wasm/Cargo.lock | 177 +++---- wasm/tx_bond/src/lib.rs | 22 +- .../tx_change_validator_commission/src/lib.rs | 14 +- wasm/tx_redelegate/src/lib.rs | 22 +- wasm/tx_unbond/src/lib.rs | 22 +- wasm/tx_withdraw/src/lib.rs | 14 +- wasm/vp_implicit/src/lib.rs | 16 +- wasm/vp_user/src/lib.rs | 14 +- wasm_for_tests/Cargo.lock | 140 +++--- wasm_for_tests/tx_fail/Cargo.toml | 4 +- .../tx_infinite_guest_gas/Cargo.toml | 4 +- .../tx_infinite_host_gas/Cargo.toml | 4 +- wasm_for_tests/tx_invalid_data/Cargo.toml | 4 +- wasm_for_tests/tx_memory_limit/Cargo.toml | 4 +- wasm_for_tests/tx_no_op/Cargo.toml | 4 +- wasm_for_tests/tx_proposal_code/Cargo.toml | 4 +- .../Cargo.toml | 4 +- .../tx_proposal_masp_reward/Cargo.toml | 4 +- wasm_for_tests/tx_read_storage_key/Cargo.toml | 4 +- wasm_for_tests/tx_write/Cargo.toml | 4 +- wasm_for_tests/vp_always_false/Cargo.toml | 4 +- wasm_for_tests/vp_always_true/Cargo.toml | 4 +- wasm_for_tests/vp_eval/Cargo.toml | 4 +- .../vp_infinite_guest_gas/Cargo.toml | 4 +- .../vp_infinite_host_gas/Cargo.toml | 4 +- wasm_for_tests/vp_memory_limit/Cargo.toml | 4 +- wasm_for_tests/vp_read_storage_key/Cargo.toml | 4 +- 178 files changed, 2750 insertions(+), 2785 deletions(-) delete mode 100644 crates/core/proptest-regressions/ledger/storage/mod.txt delete mode 100644 crates/core/proptest-regressions/ledger/storage/wl_storage.txt delete mode 100644 crates/core/src/wasm_cache.rs diff --git a/Cargo.lock b/Cargo.lock index 88e0ce73cc..84887c1eaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4455,75 +4455,6 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" -[[package]] -name = "namada" -version = "0.41.0" -dependencies = [ - "assert_matches", - "async-trait", - "base58", - "borsh", - "borsh-ext", - "byte-unit", - "clru", - "either", - "ethers", - "eyre", - "ibc-testkit", - "itertools 0.12.1", - "k256", - "linkme", - "masp_primitives", - "masp_proofs", - "namada_account", - "namada_core", - "namada_ethereum_bridge", - "namada_events", - "namada_gas", - "namada_governance", - "namada_ibc", - "namada_migrations", - "namada_parameters", - "namada_proof_of_stake", - "namada_replay_protection", - "namada_sdk", - "namada_state", - "namada_test_utils", - "namada_token", - "namada_tx", - "namada_tx_env", - "namada_vote_ext", - "namada_vp_env", - "parity-wasm", - "pretty_assertions", - "proptest", - "prost 0.12.3", - "rand 0.8.5", - "rayon", - "ripemd", - "serde_json", - "sha2 0.9.9", - "smooth-operator", - "tempfile", - "tendermint-rpc", - "test-log", - "thiserror", - "tiny-bip39", - "tokio", - "tracing", - "tracing-subscriber", - "uint", - "wasm-instrument", - "wasmer", - "wasmer-cache", - "wasmer-compiler", - "wasmer-compiler-singlepass", - "wasmer-types", - "wasmer-vm", - "wasmparser 0.107.0", - "wasmtimer", -] - [[package]] name = "namada_account" version = "0.41.0" @@ -4549,7 +4480,6 @@ dependencies = [ "color-eyre", "eyre", "git2", - "namada", "namada_apps_lib", "namada_node", "namada_test_utils", @@ -4591,11 +4521,12 @@ dependencies = [ "ledger-transport-hid", "linkme", "masp_primitives", - "namada", + "namada_core", "namada_macros", "namada_migrations", "namada_sdk", "namada_test_utils", + "namada_vm", "pretty_assertions", "proptest", "prost 0.12.3", @@ -4633,9 +4564,10 @@ dependencies = [ "lazy_static", "masp_primitives", "masp_proofs", - "namada", "namada_apps_lib", "namada_node", + "namada_vm", + "namada_vp", "prost 0.12.3", "rand 0.8.5", "rand_core 0.6.4", @@ -4669,6 +4601,7 @@ dependencies = [ "ethabi", "ethbridge-structs", "eyre", + "futures", "ibc", "ics23", "impl-num-traits", @@ -4701,10 +4634,12 @@ dependencies = [ "test-log", "thiserror", "tiny-keccak", + "tokio", "toml 0.5.11", "tracing", "tracing-subscriber", "uint", + "wasmtimer", "zeroize", ] @@ -4716,7 +4651,10 @@ dependencies = [ "itertools 0.12.1", "lazy_static", "madato", - "namada", + "namada_account", + "namada_core", + "namada_token", + "namada_tx", ] [[package]] @@ -4959,11 +4897,14 @@ dependencies = [ "linkme", "masp_primitives", "masp_proofs", - "namada", "namada_apps_lib", "namada_migrations", + "namada_replay_protection", "namada_sdk", "namada_test_utils", + "namada_vm", + "namada_vote_ext", + "namada_vp", "num-rational", "num-traits 0.2.17", "num256", @@ -5096,7 +5037,9 @@ dependencies = [ "namada_test_utils", "namada_token", "namada_tx", + "namada_vm", "namada_vote_ext", + "namada_vp", "num-traits 0.2.17", "num256", "orion", @@ -5122,7 +5065,6 @@ dependencies = [ "tokio", "toml 0.5.11", "tracing", - "wasmtimer", "zeroize", ] @@ -5234,14 +5176,16 @@ dependencies = [ "ibc-testkit", "ics23", "itertools 0.12.1", - "namada", "namada_apps_lib", "namada_core", "namada_node", "namada_sdk", "namada_test_utils", + "namada_tx_env", "namada_tx_prelude", + "namada_vm", "namada_vm_env", + "namada_vp", "namada_vp_prelude", "once_cell", "pretty_assertions", @@ -5349,6 +5293,7 @@ dependencies = [ "namada_account", "namada_core", "namada_events", + "namada_gas", "namada_governance", "namada_ibc", "namada_macros", @@ -5363,10 +5308,10 @@ dependencies = [ [[package]] name = "namada_vm" -version = "0.39.0" +version = "0.41.0" dependencies = [ "assert_matches", - "borsh 1.2.1", + "borsh", "byte-unit", "clru", "itertools 0.12.1", @@ -5374,7 +5319,6 @@ dependencies = [ "namada_events", "namada_gas", "namada_parameters", - "namada_sdk", "namada_state", "namada_test_utils", "namada_token", @@ -5390,7 +5334,9 @@ dependencies = [ "wasm-instrument", "wasmer", "wasmer-cache", + "wasmer-compiler", "wasmer-compiler-singlepass", + "wasmer-types", "wasmer-vm", "wasmparser 0.107.0", ] @@ -5420,7 +5366,7 @@ dependencies = [ [[package]] name = "namada_vp" -version = "0.39.0" +version = "0.41.0" dependencies = [ "namada_core", "namada_events", diff --git a/Cargo.toml b/Cargo.toml index e1b9095417..9ebfa971e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -198,6 +198,7 @@ wasmer-compiler = "4.3.5" wasmer-compiler-singlepass = "4.3.5" wasmer-types = "4.3.5" wasmer-vm = "4.3.5" +wasmtimer = "0.2.0" winapi = "0.3.9" yansi = "0.5.1" zeroize = { version = "1.5.5", features = ["zeroize_derive"] } diff --git a/Makefile b/Makefile index 280262f66b..2f8fa64fae 100644 --- a/Makefile +++ b/Makefile @@ -100,13 +100,13 @@ check: check-mainnet: $(cargo) check --workspace --features "mainnet" -# Check that every crate can be built with default features and that namada crate -# can be built for wasm +# Check that every crate can be built with default features and that SDK crate +# can be built for wasm and with all features enabled check-crates: cargo +$(nightly) check -Z unstable-options --tests -p namada -p namada_account -p namada_apps -p namada_apps_lib -p namada_benchmarks -p namada_core -p namada_encoding_spec -p namada_ethereum_bridge -p namada_events -p namada_gas -p namada_governance -p namada_ibc -p namada_light_sdk -p namada_macros -p namada_merkle_tree -p namada_parameters -p namada_proof_of_stake -p namada_replay_protection -p namada_node -p namada_sdk -p namada_shielded_token -p namada_state -p namada_storage -p namada_test_utils -p namada_tests -p namada_token -p namada_trans_token -p namada_tx -p namada_tx_env -p namada_tx_prelude -p namada_vm_env -p namada_vote_ext -p namada_vp_env -p namada_vp_prelude && \ make -C $(wasms) check && \ make -C $(wasms_for_tests) check && \ - cargo check --package namada --target wasm32-unknown-unknown --no-default-features --features "namada-sdk" && \ + cargo check --package namada_sdk --target wasm32-unknown-unknown --no-default-features && \ cargo check --package namada_sdk --all-features clippy-wasm = $(cargo) +$(nightly) clippy --manifest-path $(wasm)/Cargo.toml --all-targets -- -D warnings @@ -154,7 +154,6 @@ test-coverage: # Run integration tests separately because they require `integration` # feature (and without coverage) $(cargo) +$(nightly) llvm-cov --output-path lcov.info \ - --features namada/testing \ --lcov \ -- --skip e2e --skip pos_state_machine_test --skip integration \ -Z unstable-options --report-time && \ @@ -203,7 +202,6 @@ test-unit-with-eth-bridge: test-unit-with-coverage: $(cargo) +$(nightly) llvm-cov --output-path lcov.info \ - --features namada/testing \ --lcov \ -- --skip e2e --skip pos_state_machine_test --skip integration \ -Z unstable-options --report-time diff --git a/crates/apps/src/bin/namada-node/cli.rs b/crates/apps/src/bin/namada-node/cli.rs index 8e5c88e68b..d0370b892b 100644 --- a/crates/apps/src/bin/namada-node/cli.rs +++ b/crates/apps/src/bin/namada-node/cli.rs @@ -1,16 +1,16 @@ //! Namada node CLI. use eyre::{Context, Result}; -use namada::core::time::{DateTimeUtc, Utc}; -use namada::sdk::migrations::ScheduledMigration; use namada_apps_lib::cli::cmds::TestGenesis; use namada_apps_lib::cli::{self, cmds}; use namada_apps_lib::config::{ Action, ActionAtHeight, NodeLocalConfig, ValidatorLocalConfig, }; -use namada_node as node; #[cfg(not(feature = "migrations"))] -use namada_sdk::display_line; +use namada_apps_lib::display_line; +use namada_apps_lib::migrations::ScheduledMigration; +use namada_apps_lib::time::{DateTimeUtc, Utc}; +use namada_node as node; pub fn main() -> Result<()> { let (cmd, mut ctx) = cli::namada_node_cli()?; diff --git a/crates/apps/src/bin/namada-relayer/main.rs b/crates/apps/src/bin/namada-relayer/main.rs index 115b63296d..0208add846 100644 --- a/crates/apps/src/bin/namada-relayer/main.rs +++ b/crates/apps/src/bin/namada-relayer/main.rs @@ -1,6 +1,6 @@ use color_eyre::eyre::Result; -use namada::tendermint_rpc::HttpClient; use namada_apps_lib::cli::api::{CliApi, CliIo}; +use namada_apps_lib::tendermint_rpc::HttpClient; use namada_apps_lib::{cli, logging}; use tracing_subscriber::filter::LevelFilter; diff --git a/crates/apps_lib/Cargo.toml b/crates/apps_lib/Cargo.toml index ee138a188d..d443a6d682 100644 --- a/crates/apps_lib/Cargo.toml +++ b/crates/apps_lib/Cargo.toml @@ -18,8 +18,8 @@ mainnet = [ "namada_sdk/mainnet", ] # for integration tests and test utilities -testing = ["namada_test_utils", "lazy_static"] -benches = ["namada_test_utils", "lazy_static"] +testing = ["namada_test_utils", "lazy_static", "namada_sdk/testing"] +benches = ["namada_test_utils", "lazy_static", "namada_sdk/benches"] integration = [] migrations = [ "namada_migrations", @@ -31,10 +31,12 @@ namada-eth-bridge = [ ] [dependencies] +namada_core = {path = "../core"} namada_macros = {path = "../macros"} namada_migrations = {path = "../migrations", optional = true} -namada_sdk = {path = "../sdk", default-features = false, features = ["download-params", "std", "rand"]} +namada_sdk = {path = "../sdk", features = ["download-params", "multicore"]} namada_test_utils = {path = "../test_utils", optional = true} +namada_vm = {path = "../vm"} async-trait.workspace = true base64.workspace = true @@ -68,7 +70,7 @@ sha2.workspace = true tar.workspace = true tempfile.workspace = true tendermint-config.workspace = true -tendermint-rpc.workspace = true +tendermint-rpc = { workspace = true, features = ["http-client"] } textwrap-macros = "0.3.0" thiserror.workspace = true tokio = {workspace = true, features = ["full"]} @@ -80,6 +82,7 @@ tracing.workspace = true zeroize.workspace = true [dev-dependencies] +namada_sdk = {path = "../sdk", features = ["testing"]} namada_test_utils = {path = "../test_utils"} assert_matches.workspace = true diff --git a/crates/apps_lib/src/cli.rs b/crates/apps_lib/src/cli.rs index 49f59db602..f0012a9a19 100644 --- a/crates/apps_lib/src/cli.rs +++ b/crates/apps_lib/src/cli.rs @@ -15,7 +15,7 @@ pub mod wallet; use clap::{ArgGroup, ArgMatches, ColorChoice}; use color_eyre::eyre::Result; -use namada::io::StdIo; +use namada_sdk::io::StdIo; use utils::*; pub use utils::{safe_exit, Cmd}; @@ -3155,23 +3155,21 @@ pub mod args { use std::str::FromStr; use data_encoding::HEXUPPER; - use namada::core::address::{Address, EstablishedAddress}; - use namada::core::chain::{ChainId, ChainIdPrefix}; - use namada::core::collections::HashMap; - use namada::core::dec::Dec; - use namada::core::ethereum_events::EthAddress; - use namada::core::keccak::KeccakHash; - use namada::core::key::*; - use namada::core::masp::PaymentAddress; - use namada::core::storage::{self, BlockHeight, Epoch}; - use namada::core::time::DateTimeUtc; - use namada::core::token; - use namada::core::token::NATIVE_MAX_DECIMAL_PLACES; - use namada::hash::Hash; - use namada::ibc::core::host::types::identifiers::{ChannelId, PortId}; - use namada::masp::MaspEpoch; - use namada::tx::data::GasLimit; + use namada_sdk::address::{Address, EstablishedAddress}; pub use namada_sdk::args::*; + use namada_sdk::chain::{ChainId, ChainIdPrefix}; + use namada_sdk::collections::HashMap; + use namada_sdk::dec::Dec; + use namada_sdk::ethereum_events::EthAddress; + use namada_sdk::hash::Hash; + use namada_sdk::ibc::core::host::types::identifiers::{ChannelId, PortId}; + use namada_sdk::keccak::KeccakHash; + use namada_sdk::key::*; + use namada_sdk::masp::{MaspEpoch, PaymentAddress}; + use namada_sdk::storage::{self, BlockHeight, Epoch}; + use namada_sdk::time::DateTimeUtc; + use namada_sdk::token::NATIVE_MAX_DECIMAL_PLACES; + use namada_sdk::tx::data::GasLimit; pub use namada_sdk::tx::{ TX_BECOME_VALIDATOR_WASM, TX_BOND_WASM, TX_BRIDGE_POOL_WASM, TX_CHANGE_COMMISSION_WASM, TX_CHANGE_CONSENSUS_KEY_WASM, @@ -3183,7 +3181,7 @@ pub mod args { TX_UPDATE_STEWARD_COMMISSION, TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM, VP_USER_WASM, }; - use namada_sdk::DEFAULT_GAS_LIMIT; + use namada_sdk::{token, DEFAULT_GAS_LIMIT}; use super::context::*; use super::utils::*; @@ -4789,7 +4787,7 @@ pub mod args { IBC_SHIELDING_DATA_PATH.parse(matches).map(|path| { let data = std::fs::read_to_string(path) .expect("Failed to open IBC shielding data file"); - namada::ibc::decode_ibc_shielding_data(data) + namada_sdk::ibc::decode_ibc_shielding_data(data) .expect("Failed to decode IBC shielding data") }); let ibc_memo = IBC_MEMO.parse(matches); diff --git a/crates/apps_lib/src/cli/api.rs b/crates/apps_lib/src/cli/api.rs index 2dd8d9c9c8..b41520a94f 100644 --- a/crates/apps_lib/src/cli/api.rs +++ b/crates/apps_lib/src/cli/api.rs @@ -1,10 +1,10 @@ -use namada::io::Io; -use namada::tendermint_rpc::HttpClient; use namada_sdk::error::Error; +use namada_sdk::io::Io; use namada_sdk::queries::Client; use namada_sdk::rpc::wait_until_node_is_synched; -use tendermint_rpc::client::CompatMode; -use tendermint_rpc::Url as TendermintUrl; + +use crate::facade::tendermint_rpc::client::CompatMode; +use crate::facade::tendermint_rpc::{HttpClient, Url as TendermintUrl}; /// Trait for clients that can be used with the CLI. #[async_trait::async_trait(?Send)] diff --git a/crates/apps_lib/src/cli/client.rs b/crates/apps_lib/src/cli/client.rs index 00120acf97..1125378814 100644 --- a/crates/apps_lib/src/cli/client.rs +++ b/crates/apps_lib/src/cli/client.rs @@ -2,7 +2,7 @@ use std::io::Read; use color_eyre::eyre::Result; use masp_primitives::zip32::ExtendedFullViewingKey; -use namada::io::Io; +use namada_sdk::io::Io; use namada_sdk::{display_line, Namada, NamadaImpl}; use crate::cli; diff --git a/crates/apps_lib/src/cli/context.rs b/crates/apps_lib/src/cli/context.rs index 58b5862960..aa0e6c6141 100644 --- a/crates/apps_lib/src/cli/context.rs +++ b/crates/apps_lib/src/cli/context.rs @@ -6,15 +6,14 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use color_eyre::eyre::Result; -use namada::core::address::{Address, InternalAddress}; -use namada::core::chain::ChainId; -use namada::core::ethereum_events::EthAddress; -use namada::core::key::*; -use namada::core::masp::*; -use namada::ibc::trace::{ibc_token, is_ibc_denom, is_nft_trace}; -use namada::io::Io; +use namada_sdk::address::{Address, InternalAddress}; +use namada_sdk::chain::ChainId; +use namada_sdk::ethereum_events::EthAddress; +use namada_sdk::ibc::trace::{ibc_token, is_ibc_denom, is_nft_trace}; +use namada_sdk::io::Io; +use namada_sdk::key::*; use namada_sdk::masp::fs::FsShieldedUtils; -use namada_sdk::masp::ShieldedContext; +use namada_sdk::masp::{ShieldedContext, *}; use namada_sdk::wallet::Wallet; use namada_sdk::{Namada, NamadaImpl}; @@ -225,7 +224,7 @@ impl Context { /// Make an implementation of Namada from this object and parameters. pub fn to_sdk(self, client: C, io: IO) -> impl Namada where - C: namada::ledger::queries::Client + Sync, + C: namada_sdk::queries::Client + Sync, IO: Io, { let chain_ctx = self.take_chain_or_exit(); diff --git a/crates/apps_lib/src/cli/relayer.rs b/crates/apps_lib/src/cli/relayer.rs index 96995deed7..fc84673a36 100644 --- a/crates/apps_lib/src/cli/relayer.rs +++ b/crates/apps_lib/src/cli/relayer.rs @@ -1,5 +1,5 @@ use color_eyre::eyre::Result; -use namada::io::Io; +use namada_sdk::io::Io; use crate::cli; use crate::cli::api::{CliApi, CliClient}; diff --git a/crates/apps_lib/src/cli/utils.rs b/crates/apps_lib/src/cli/utils.rs index 3c5280e2aa..c33e4cf155 100644 --- a/crates/apps_lib/src/cli/utils.rs +++ b/crates/apps_lib/src/cli/utils.rs @@ -8,10 +8,10 @@ use std::sync::Arc; use clap::{ArgAction, ArgMatches}; use color_eyre::eyre::Result; use data_encoding::HEXLOWER_PERMISSIVE; -use namada::eth_bridge::ethers::core::k256::elliptic_curve::SecretKey as Secp256k1Sk; -use namada::eth_bridge::ethers::middleware::SignerMiddleware; -use namada::eth_bridge::ethers::providers::{Http, Middleware, Provider}; -use namada::eth_bridge::ethers::signers::{Signer, Wallet}; +use namada_sdk::eth_bridge::ethers::core::k256::elliptic_curve::SecretKey as Secp256k1Sk; +use namada_sdk::eth_bridge::ethers::middleware::SignerMiddleware; +use namada_sdk::eth_bridge::ethers::providers::{Http, Middleware, Provider}; +use namada_sdk::eth_bridge::ethers::signers::{Signer, Wallet}; use super::args; use super::context::Context; diff --git a/crates/apps_lib/src/cli/wallet.rs b/crates/apps_lib/src/cli/wallet.rs index 4eeab319f6..546b3a8dac 100644 --- a/crates/apps_lib/src/cli/wallet.rs +++ b/crates/apps_lib/src/cli/wallet.rs @@ -12,11 +12,12 @@ use ledger_namada_rs::{BIP44Path, NamadaApp}; use ledger_transport_hid::hidapi::HidApi; use ledger_transport_hid::TransportNativeHID; use masp_primitives::zip32::ExtendedFullViewingKey; -use namada::core::address::{Address, DecodeError}; -use namada::core::key::*; -use namada::core::masp::{ExtendedSpendingKey, MaspValue, PaymentAddress}; -use namada::io::Io; -use namada_sdk::masp::find_valid_diversifier; +use namada_sdk::address::{Address, DecodeError}; +use namada_sdk::io::Io; +use namada_sdk::key::*; +use namada_sdk::masp::{ + find_valid_diversifier, ExtendedSpendingKey, MaspValue, PaymentAddress, +}; use namada_sdk::wallet::{ DecryptionError, DerivationPath, DerivationPathError, FindKeyError, Wallet, }; diff --git a/crates/apps_lib/src/client/rpc.rs b/crates/apps_lib/src/client/rpc.rs index d078b0367a..9db5fde5e1 100644 --- a/crates/apps_lib/src/client/rpc.rs +++ b/crates/apps_lib/src/client/rpc.rs @@ -10,44 +10,42 @@ use masp_primitives::merkle_tree::MerklePath; use masp_primitives::sapling::Node; use masp_primitives::transaction::components::I128Sum; use masp_primitives::zip32::ExtendedFullViewingKey; -use namada::core::address::{Address, InternalAddress, MASP}; -use namada::core::collections::{HashMap, HashSet}; -use namada::core::hash::Hash; -use namada::core::key::*; -use namada::core::masp::BalanceOwner; -use namada::core::storage::{BlockHeight, BlockResults, Epoch}; -use namada::core::token::MaspDigitPos; -use namada::governance::parameters::GovernanceParameters; -use namada::governance::pgf::parameters::PgfParameters; -use namada::governance::pgf::storage::steward::StewardDetail; -use namada::governance::storage::keys as governance_storage; -use namada::governance::storage::proposal::{ +use namada_sdk::address::{Address, InternalAddress, MASP}; +use namada_sdk::collections::{HashMap, HashSet}; +use namada_sdk::control_flow::time::{Duration, Instant}; +use namada_sdk::events::Event; +use namada_sdk::governance::parameters::GovernanceParameters; +use namada_sdk::governance::pgf::parameters::PgfParameters; +use namada_sdk::governance::pgf::storage::steward::StewardDetail; +use namada_sdk::governance::storage::keys as governance_storage; +use namada_sdk::governance::storage::proposal::{ StoragePgfFunding, StorageProposal, }; -use namada::governance::utils::{ProposalVotes, VotePower}; -use namada::governance::ProposalVote; -use namada::io::Io; -use namada::ledger::events::Event; -use namada::ledger::parameters::{storage as param_storage, EpochDuration}; -use namada::ledger::pos::types::{CommissionPair, Slash}; -use namada::ledger::pos::PosParams; -use namada::ledger::queries::RPC; -use namada::masp::MaspEpoch; -use namada::proof_of_stake::types::{ - ValidatorState, ValidatorStateInfo, WeightedValidator, +use namada_sdk::governance::utils::{ProposalVotes, VotePower}; +use namada_sdk::governance::ProposalVote; +use namada_sdk::hash::Hash; +use namada_sdk::io::Io; +use namada_sdk::key::*; +use namada_sdk::masp::{BalanceOwner, MaspEpoch, MaspTokenRewardData}; +use namada_sdk::parameters::{storage as param_storage, EpochDuration}; +use namada_sdk::proof_of_stake::types::{ + CommissionPair, Slash, ValidatorMetaData, ValidatorState, + ValidatorStateInfo, WeightedValidator, }; -use namada::{state as storage, token}; -use namada_sdk::control_flow::time::{Duration, Instant}; -use namada_sdk::masp::MaspTokenRewardData; -use namada_sdk::proof_of_stake::types::ValidatorMetaData; -use namada_sdk::queries::Client; +use namada_sdk::proof_of_stake::PosParams; +use namada_sdk::queries::{Client, RPC}; use namada_sdk::rpc::{ self, enriched_bonds_and_unbonds, query_epoch, TxResponse, }; +use namada_sdk::storage::{BlockHeight, BlockResults, Epoch}; use namada_sdk::tendermint_rpc::endpoint::status; +use namada_sdk::token::MaspDigitPos; use namada_sdk::tx::display_batch_resp; use namada_sdk::wallet::AddressVpType; -use namada_sdk::{display, display_line, edisplay_line, error, Namada}; +use namada_sdk::{ + display, display_line, edisplay_line, error, state as storage, token, + Namada, +}; use crate::cli::{self, args}; use crate::facade::tendermint::merkle::proof::ProofOps; @@ -145,7 +143,7 @@ pub async fn query_block(context: &impl Namada) { } /// Query the results of the last committed block -pub async fn query_results( +pub async fn query_results( client: &C, _args: args::Query, ) -> Vec { @@ -338,7 +336,7 @@ pub async fn query_proposal(context: &impl Namada, args: args::QueryProposal) { } /// Query proposal by Id -pub async fn query_proposal_by_id( +pub async fn query_proposal_by_id( client: &C, proposal_id: u64, ) -> Result, error::Error> { @@ -771,7 +769,7 @@ pub async fn query_protocol_parameters( ); } -pub async fn query_bond( +pub async fn query_bond( client: &C, source: &Address, validator: &Address, @@ -783,7 +781,7 @@ pub async fn query_bond( } pub async fn query_unbond_with_slashing< - C: namada::ledger::queries::Client + Sync, + C: namada_sdk::queries::Client + Sync, >( client: &C, source: &Address, @@ -797,7 +795,7 @@ pub async fn query_unbond_with_slashing< ) } -pub async fn query_pos_parameters( +pub async fn query_pos_parameters( client: &C, ) -> PosParams { unwrap_client_response::( @@ -805,7 +803,7 @@ pub async fn query_pos_parameters( ) } -pub async fn query_consensus_keys( +pub async fn query_consensus_keys( client: &C, ) -> BTreeSet { unwrap_client_response::>( @@ -813,19 +811,19 @@ pub async fn query_consensus_keys( ) } -pub async fn query_pgf_stewards( +pub async fn query_pgf_stewards( client: &C, ) -> Vec { unwrap_client_response::(RPC.vp().pgf().stewards(client).await) } -pub async fn query_pgf_fundings( +pub async fn query_pgf_fundings( client: &C, ) -> Vec { unwrap_client_response::(RPC.vp().pgf().funding(client).await) } -pub async fn query_pgf_parameters( +pub async fn query_pgf_parameters( client: &C, ) -> PgfParameters { unwrap_client_response::(RPC.vp().pgf().parameters(client).await) @@ -873,7 +871,7 @@ pub async fn query_and_print_unbonds( } pub async fn query_withdrawable_tokens< - C: namada::ledger::queries::Client + Sync, + C: namada_sdk::queries::Client + Sync, >( client: &C, bond_source: &Address, @@ -1129,9 +1127,7 @@ pub async fn query_bonded_stake( /// Query and return validator's commission rate and max commission rate change /// per epoch -pub async fn query_commission_rate< - C: namada::ledger::queries::Client + Sync, ->( +pub async fn query_commission_rate( client: &C, validator: &Address, epoch: Option, @@ -1145,7 +1141,7 @@ pub async fn query_commission_rate< } /// Query and return validator's metadata -pub async fn query_metadata( +pub async fn query_metadata( client: &C, validator: &Address, ) -> Option { @@ -1155,9 +1151,7 @@ pub async fn query_metadata( } /// Query and return validator's state -pub async fn query_validator_state< - C: namada::ledger::queries::Client + Sync, ->( +pub async fn query_validator_state( client: &C, validator: &Address, epoch: Option, @@ -1171,7 +1165,7 @@ pub async fn query_validator_state< } /// Query and return the available reward tokens corresponding to the bond -pub async fn query_rewards( +pub async fn query_rewards( client: &C, source: &Option
, validator: &Address, @@ -1631,7 +1625,7 @@ pub async fn query_find_validator( } /// Get account's public key stored in its storage sub-space -pub async fn get_public_key( +pub async fn get_public_key( client: &C, address: &Address, index: u8, @@ -1640,7 +1634,7 @@ pub async fn get_public_key( } /// Check if the given address has any bonds. -pub async fn is_validator( +pub async fn is_validator( client: &C, address: &Address, ) -> bool { @@ -1650,7 +1644,7 @@ pub async fn is_validator( } /// Check if a given address is a known delegator -pub async fn is_delegator( +pub async fn is_delegator( client: &C, address: &Address, ) -> bool { @@ -1659,7 +1653,7 @@ pub async fn is_delegator( .unwrap() } -pub async fn is_delegator_at( +pub async fn is_delegator_at( client: &C, address: &Address, epoch: Epoch, @@ -1670,7 +1664,7 @@ pub async fn is_delegator_at( } /// Check if the given address has any bonds. -pub async fn has_bonds( +pub async fn has_bonds( client: &C, address: &Address, ) -> bool { @@ -1680,7 +1674,7 @@ pub async fn has_bonds( /// Check if the address exists on chain. Established address exists if it has a /// stored validity predicate. Implicit and internal addresses always return /// true. -pub async fn known_address( +pub async fn known_address( client: &C, address: &Address, ) -> bool { @@ -1768,7 +1762,7 @@ pub async fn query_conversions( } /// Query a conversion. -pub async fn query_conversion( +pub async fn query_conversion( client: &C, asset_type: AssetType, ) -> Option<( @@ -1818,7 +1812,7 @@ pub async fn query_wasm_code_hash( } /// Query a storage value and decode it with [`BorshDeserialize`]. -pub async fn query_storage_value( +pub async fn query_storage_value( client: &C, key: &storage::Key, ) -> Result @@ -1830,7 +1824,7 @@ where /// Query a storage value and the proof without decoding. pub async fn query_storage_value_bytes< - C: namada::ledger::queries::Client + Sync, + C: namada_sdk::queries::Client + Sync, >( client: &C, key: &storage::Key, @@ -1856,9 +1850,7 @@ where } /// Query to check if the given storage key exists. -pub async fn query_has_storage_key< - C: namada::ledger::queries::Client + Sync, ->( +pub async fn query_has_storage_key( client: &C, key: &storage::Key, ) -> bool { @@ -1869,13 +1861,11 @@ pub async fn query_has_storage_key< /// Call the corresponding `tx_event_query` RPC method, to fetch /// the current status of a transaction. -pub async fn query_tx_events( +pub async fn query_tx_events( client: &C, tx_event_query: namada_sdk::rpc::TxEventQuery<'_>, -) -> std::result::Result< - Option, - ::Error, -> { +) -> std::result::Result, ::Error> +{ namada_sdk::rpc::query_tx_events(client, tx_event_query).await } @@ -1925,7 +1915,7 @@ pub async fn epoch_sleep(context: &impl Namada, _args: args::Query) { } } -pub async fn get_bond_amount_at( +pub async fn get_bond_amount_at( client: &C, delegator: &Address, validator: &Address, @@ -1940,7 +1930,7 @@ pub async fn get_bond_amount_at( Some(total_active) } -pub async fn get_all_validators( +pub async fn get_all_validators( client: &C, epoch: Epoch, ) -> HashSet
{ @@ -1949,9 +1939,7 @@ pub async fn get_all_validators( .unwrap() } -pub async fn get_total_staked_tokens< - C: namada::ledger::queries::Client + Sync, ->( +pub async fn get_total_staked_tokens( client: &C, epoch: Epoch, ) -> token::Amount { @@ -1964,7 +1952,7 @@ pub async fn get_total_staked_tokens< /// sum of validator's self-bonds and delegations to their address. /// Returns `None` when the given address is not a validator address. For a /// validator with `0` stake, this returns `Ok(token::Amount::zero())`. -async fn get_validator_stake( +async fn get_validator_stake( client: &C, epoch: Epoch, validator: &Address, @@ -1978,7 +1966,7 @@ async fn get_validator_stake( } pub async fn get_delegation_validators< - C: namada::ledger::queries::Client + Sync, + C: namada_sdk::queries::Client + Sync, >( client: &C, address: &Address, @@ -1990,7 +1978,7 @@ pub async fn get_delegation_validators< } pub async fn get_delegations_of_delegator_at< - C: namada::ledger::queries::Client + Sync, + C: namada_sdk::queries::Client + Sync, >( client: &C, address: &Address, @@ -2002,7 +1990,7 @@ pub async fn get_delegations_of_delegator_at< } pub async fn query_governance_parameters< - C: namada::ledger::queries::Client + Sync, + C: namada_sdk::queries::Client + Sync, >( client: &C, ) -> GovernanceParameters { @@ -2010,7 +1998,7 @@ pub async fn query_governance_parameters< } /// A helper to unwrap client's response. Will shut down process on error. -fn unwrap_client_response( +fn unwrap_client_response( response: Result, ) -> T { response.unwrap_or_else(|err| { @@ -2019,9 +2007,7 @@ fn unwrap_client_response( }) } -pub async fn compute_proposal_votes< - C: namada::ledger::queries::Client + Sync, ->( +pub async fn compute_proposal_votes( client: &C, proposal_id: u64, epoch: Epoch, diff --git a/crates/apps_lib/src/client/tx.rs b/crates/apps_lib/src/client/tx.rs index 083076a28f..835ff33c93 100644 --- a/crates/apps_lib/src/client/tx.rs +++ b/crates/apps_lib/src/client/tx.rs @@ -6,19 +6,19 @@ use borsh_ext::BorshSerializeExt; use ledger_namada_rs::{BIP44Path, NamadaApp}; use ledger_transport_hid::hidapi::HidApi; use ledger_transport_hid::TransportNativeHID; -use namada::core::address::{Address, ImplicitAddress}; -use namada::core::collections::HashSet; -use namada::core::key::*; -use namada::governance::cli::onchain::{ +use namada_sdk::address::{Address, ImplicitAddress}; +use namada_sdk::args::TxBecomeValidator; +use namada_sdk::collections::HashSet; +use namada_sdk::governance::cli::onchain::{ DefaultProposal, PgfFundingProposal, PgfStewardProposal, }; -use namada::ibc::convert_masp_tx_to_ibc_memo; -use namada::io::Io; -use namada::state::EPOCH_SWITCH_BLOCKS_DELAY; -use namada::tx::data::compute_inner_tx_hash; -use namada::tx::{CompressedAuthorization, Section, Signer, Tx}; -use namada_sdk::args::TxBecomeValidator; +use namada_sdk::ibc::convert_masp_tx_to_ibc_memo; +use namada_sdk::io::Io; +use namada_sdk::key::*; use namada_sdk::rpc::{InnerTxResult, TxBroadcastData, TxResponse}; +use namada_sdk::state::EPOCH_SWITCH_BLOCKS_DELAY; +use namada_sdk::tx::data::compute_inner_tx_hash; +use namada_sdk::tx::{CompressedAuthorization, Section, Signer, Tx}; use namada_sdk::wallet::alias::{validator_address, validator_consensus_key}; use namada_sdk::wallet::{Wallet, WalletIo}; use namada_sdk::{display_line, edisplay_line, error, signing, tx, Namada}; @@ -249,7 +249,7 @@ pub async fn submit_custom( args: args::TxCustom, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { submit_reveal_aux(namada, args.tx.clone(), &args.owner).await?; @@ -271,7 +271,7 @@ pub async fn submit_update_account( args: args::TxUpdateAccount, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -291,7 +291,7 @@ pub async fn submit_init_account( args: args::TxInitAccount, ) -> Result, error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = tx::build_init_account(namada, &args).await?; @@ -370,7 +370,7 @@ pub async fn submit_change_consensus_key( // To avoid wallet deadlocks in following operations drop(wallet); - let args = namada::sdk::args::ConsensusKeyChange { + let args = args::ConsensusKeyChange { validator: validator.clone(), consensus_key: Some(new_key.clone()), ..args @@ -873,7 +873,7 @@ pub async fn submit_ibc_transfer( args: args::TxIbcTransfer, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { submit_reveal_aux( namada, @@ -901,7 +901,7 @@ pub async fn submit_init_proposal( args: args::InitProposal, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let current_epoch = rpc::query_and_print_epoch(namada).await; let governance_parameters = @@ -1001,7 +1001,7 @@ pub async fn submit_vote_proposal( args: args::VoteProposal, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx_builder, signing_data) = args.build(namada).await?; @@ -1025,7 +1025,7 @@ pub async fn sign_tx( }: args::SignTx, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let tx = if let Ok(transaction) = Tx::deserialize(tx_data.as_ref()) { transaction @@ -1102,7 +1102,7 @@ pub async fn submit_reveal_pk( args: args::RevealPk, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { submit_reveal_aux(namada, args.tx, &(&args.public_key).into()).await?; @@ -1114,7 +1114,7 @@ pub async fn submit_bond( args: args::Bond, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let default_address = args.source.clone().unwrap_or(args.validator.clone()); submit_reveal_aux(namada, args.tx.clone(), &default_address).await?; @@ -1137,7 +1137,7 @@ pub async fn submit_unbond( args: args::Unbond, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data, latest_withdrawal_pre) = args.build(namada).await?; @@ -1168,7 +1168,7 @@ pub async fn submit_withdraw( args: args::Withdraw, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1188,7 +1188,7 @@ pub async fn submit_claim_rewards( args: args::ClaimRewards, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1208,7 +1208,7 @@ pub async fn submit_redelegate( args: args::Redelegate, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1228,7 +1228,7 @@ pub async fn submit_validator_commission_change( args: args::CommissionRateChange, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1248,7 +1248,7 @@ pub async fn submit_validator_metadata_change( args: args::MetaDataChange, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1268,7 +1268,7 @@ pub async fn submit_unjail_validator( args: args::TxUnjailValidator, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1288,7 +1288,7 @@ pub async fn submit_deactivate_validator( args: args::TxDeactivateValidator, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1308,7 +1308,7 @@ pub async fn submit_reactivate_validator( args: args::TxReactivateValidator, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1328,7 +1328,7 @@ pub async fn submit_update_steward_commission( args: args::UpdateStewardCommission, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; @@ -1348,7 +1348,7 @@ pub async fn submit_resign_steward( args: args::ResignSteward, ) -> Result<(), error::Error> where - ::Error: std::fmt::Display, + ::Error: std::fmt::Display, { let (mut tx, signing_data) = args.build(namada).await?; diff --git a/crates/apps_lib/src/client/utils.rs b/crates/apps_lib/src/client/utils.rs index 04a60de000..0d88cd54d5 100644 --- a/crates/apps_lib/src/client/utils.rs +++ b/crates/apps_lib/src/client/utils.rs @@ -9,14 +9,14 @@ use flate2::read::GzDecoder; use flate2::write::GzEncoder; use flate2::Compression; use itertools::Either; -use namada::core::chain::ChainId; -use namada::core::dec::Dec; -use namada::core::key::*; -use namada::core::string_encoding::StringEncoded; -use namada::core::token; -use namada::core::uint::Uint; -use namada::vm::validate_untrusted_wasm; +use namada_sdk::chain::ChainId; +use namada_sdk::dec::Dec; +use namada_sdk::key::*; +use namada_sdk::string_encoding::StringEncoded; +use namada_sdk::token; +use namada_sdk::uint::Uint; use namada_sdk::wallet::{alias, Wallet}; +use namada_vm::validate_untrusted_wasm; use prost::bytes::Bytes; use serde_json::json; use sha2::{Digest, Sha256}; diff --git a/crates/apps_lib/src/config/ethereum_bridge/ledger.rs b/crates/apps_lib/src/config/ethereum_bridge/ledger.rs index c1117c44b5..ccde283082 100644 --- a/crates/apps_lib/src/config/ethereum_bridge/ledger.rs +++ b/crates/apps_lib/src/config/ethereum_bridge/ledger.rs @@ -1,6 +1,6 @@ //! Runtime configuration for a validator node. #[allow(unused_imports)] -use namada::core::ethereum_events::EthereumEvent; +use namada_sdk::ethereum_events::EthereumEvent; use serde::{Deserialize, Serialize}; /// Default [Ethereum JSON-RPC](https://ethereum.org/en/developers/docs/apis/json-rpc/) endpoint used by the oracle diff --git a/crates/apps_lib/src/config/genesis.rs b/crates/apps_lib/src/config/genesis.rs index c500e2fc59..5d7d4a8534 100644 --- a/crates/apps_lib/src/config/genesis.rs +++ b/crates/apps_lib/src/config/genesis.rs @@ -12,23 +12,22 @@ use std::str::FromStr; use borsh::{BorshDeserialize, BorshSerialize}; use derivative::Derivative; -use namada::core::address::{Address, EstablishedAddress}; -use namada::core::chain::ProposalBytes; -use namada::core::collections::HashMap; -use namada::core::key::*; -use namada::core::storage; -use namada::core::string_encoding::StringEncoded; -use namada::core::time::DateTimeUtc; -use namada::core::token::Denomination; -use namada::governance::parameters::GovernanceParameters; -use namada::governance::pgf::parameters::PgfParameters; -use namada::ledger::eth_bridge::EthereumBridgeParams; -use namada::ledger::parameters::EpochDuration; -use namada::ledger::pos::{Dec, GenesisValidator, OwnedPosParams}; -use namada::token; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; +use namada_sdk::address::{Address, EstablishedAddress}; +use namada_sdk::chain::ProposalBytes; +use namada_sdk::collections::HashMap; +use namada_sdk::eth_bridge::EthereumBridgeParams; +use namada_sdk::governance::parameters::GovernanceParameters; +use namada_sdk::governance::pgf::parameters::PgfParameters; +use namada_sdk::key::*; +use namada_sdk::parameters::EpochDuration; +use namada_sdk::proof_of_stake::{Dec, GenesisValidator, OwnedPosParams}; +use namada_sdk::string_encoding::StringEncoded; +use namada_sdk::time::DateTimeUtc; +use namada_sdk::token::Denomination; +use namada_sdk::{storage, token}; use serde::{Deserialize, Serialize}; #[derive( @@ -334,13 +333,13 @@ pub fn make_dev_genesis( use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::time::Duration; - use namada::core::address::testing::wnam; - use namada::core::chain::ChainIdPrefix; - use namada::core::ethereum_events::EthAddress; - use namada::core::key::*; - use namada::ledger::eth_bridge::{Contracts, UpgradeableContract}; - use namada::ledger::pos::types::ValidatorMetaData; - use namada::tx::standalone_signature; + use namada_sdk::address::testing::wnam; + use namada_sdk::chain::ChainIdPrefix; + use namada_sdk::eth_bridge::{Contracts, UpgradeableContract}; + use namada_sdk::ethereum_events::EthAddress; + use namada_sdk::key::*; + use namada_sdk::proof_of_stake::types::ValidatorMetaData; + use namada_sdk::tx::standalone_signature; use namada_sdk::wallet::alias::Alias; use crate::config::genesis::chain::{ @@ -579,8 +578,8 @@ pub fn make_dev_genesis( #[cfg(test)] pub mod tests { use borsh_ext::BorshSerializeExt; - use namada::core::address::testing::gen_established_address; - use namada::core::key::*; + use namada_sdk::address::testing::gen_established_address; + use namada_sdk::key::*; use rand::prelude::ThreadRng; use rand::thread_rng; diff --git a/crates/apps_lib/src/config/genesis/chain.rs b/crates/apps_lib/src/config/genesis/chain.rs index eb19bf0b53..fac84c20d0 100644 --- a/crates/apps_lib/src/config/genesis/chain.rs +++ b/crates/apps_lib/src/config/genesis/chain.rs @@ -4,19 +4,21 @@ use std::str::FromStr; use borsh::{BorshDeserialize, BorshSerialize}; use borsh_ext::BorshSerializeExt; -use namada::address::InternalAddress; -use namada::core::address::{ - Address, EstablishedAddress, EstablishedAddressGen, -}; -use namada::core::chain::{ChainId, ChainIdPrefix}; -use namada::core::hash::Hash; -use namada::core::key::{common, RefTo}; -use namada::core::time::{DateTimeUtc, DurationNanos, Rfc3339String}; -use namada::core::token::Amount; -use namada::ledger::parameters::EpochDuration; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; +use namada_sdk::address::{ + Address, EstablishedAddress, EstablishedAddressGen, InternalAddress, +}; +use namada_sdk::chain::{ChainId, ChainIdPrefix}; +use namada_sdk::eth_bridge::EthereumBridgeParams; +use namada_sdk::governance::pgf::parameters::PgfParameters; +use namada_sdk::hash::Hash; +use namada_sdk::ibc::parameters::IbcParameters; +use namada_sdk::key::{common, RefTo}; +use namada_sdk::parameters::EpochDuration; +use namada_sdk::time::{DateTimeUtc, DurationNanos, Rfc3339String}; +use namada_sdk::token::Amount; use namada_sdk::wallet::store::AddressVpType; use namada_sdk::wallet::{pre_genesis, Wallet}; use serde::{Deserialize, Serialize}; @@ -293,7 +295,7 @@ impl Finalized { pub fn get_chain_parameters( &self, wasm_dir: impl AsRef, - ) -> namada::ledger::parameters::Parameters { + ) -> namada_sdk::parameters::Parameters { let templates::ChainParams { min_num_of_blocks, max_proposal_bytes, @@ -329,13 +331,13 @@ impl Finalized { let min_duration: i64 = 60 * 60 * 24 * 365 / epy_i64; let epoch_duration = EpochDuration { min_num_of_blocks, - min_duration: namada::core::time::Duration::seconds(min_duration) + min_duration: namada_sdk::time::Duration::seconds(min_duration) .into(), }; let vp_allowlist = vp_allowlist.unwrap_or_default(); let tx_allowlist = tx_allowlist.unwrap_or_default(); - namada::ledger::parameters::Parameters { + namada_sdk::parameters::Parameters { max_tx_bytes, epoch_duration, vp_allowlist, @@ -362,7 +364,7 @@ impl Finalized { pub fn get_pos_params( &self, - ) -> namada::proof_of_stake::parameters::PosParams { + ) -> namada_sdk::proof_of_stake::parameters::PosParams { let templates::PosParams { max_validator_slots, pipeline_len, @@ -382,8 +384,8 @@ impl Finalized { rewards_gain_d, } = self.parameters.pos_params.clone(); - namada::proof_of_stake::parameters::PosParams { - owned: namada::proof_of_stake::parameters::OwnedPosParams { + namada_sdk::proof_of_stake::parameters::PosParams { + owned: namada_sdk::proof_of_stake::parameters::OwnedPosParams { max_validator_slots, pipeline_len, unbonding_len, @@ -407,7 +409,7 @@ impl Finalized { pub fn get_gov_params( &self, - ) -> namada::governance::parameters::GovernanceParameters { + ) -> namada_sdk::governance::parameters::GovernanceParameters { let templates::GovernanceParams { min_proposal_fund, max_proposal_code_size, @@ -417,7 +419,7 @@ impl Finalized { min_proposal_grace_epochs, max_proposal_latency, } = self.parameters.gov_params.clone(); - namada::governance::parameters::GovernanceParameters { + namada_sdk::governance::parameters::GovernanceParameters { min_proposal_fund: Amount::native_whole(min_proposal_fund), max_proposal_code_size, max_proposal_period, @@ -428,15 +430,11 @@ impl Finalized { } } - pub fn get_pgf_params( - &self, - ) -> namada::governance::pgf::parameters::PgfParameters { + pub fn get_pgf_params(&self) -> PgfParameters { self.parameters.pgf_params.clone() } - pub fn get_eth_bridge_params( - &self, - ) -> Option { + pub fn get_eth_bridge_params(&self) -> Option { if let Some(templates::EthBridgeParams { eth_start_height, min_confirmations, @@ -444,7 +442,7 @@ impl Finalized { erc20_whitelist, }) = self.parameters.eth_bridge_params.clone() { - Some(namada::ledger::eth_bridge::EthereumBridgeParams { + Some(EthereumBridgeParams { eth_start_height, min_confirmations, erc20_whitelist, @@ -455,12 +453,12 @@ impl Finalized { } } - pub fn get_ibc_params(&self) -> namada::ibc::parameters::IbcParameters { + pub fn get_ibc_params(&self) -> IbcParameters { let templates::IbcParams { default_mint_limit, default_per_epoch_throughput_limit, } = self.parameters.ibc_params.clone(); - namada::ibc::parameters::IbcParameters { + IbcParameters { default_mint_limit, default_per_epoch_throughput_limit, } @@ -733,7 +731,7 @@ pub struct FinalizedParameters { pub parameters: templates::ChainParams, pub pos_params: templates::PosParams, pub gov_params: templates::GovernanceParams, - pub pgf_params: namada::governance::pgf::parameters::PgfParameters, + pub pgf_params: PgfParameters, pub eth_bridge_params: Option, pub ibc_params: templates::IbcParams, } @@ -749,7 +747,6 @@ impl FinalizedParameters { ibc_params, }: templates::Parameters, ) -> Self { - use namada::governance::pgf::parameters::PgfParameters; let finalized_pgf_params = PgfParameters { stewards: pgf_params.stewards, pgf_inflation_rate: pgf_params.pgf_inflation_rate, @@ -846,9 +843,8 @@ pub struct Metadata { mod test { use std::path::PathBuf; - use namada::core::time::test_utils::GENESIS_TIME; - use super::*; + use crate::time::test_utils::GENESIS_TIME; /// Test that the [`finalize`] returns deterministic output with the same /// chain ID for the same input. diff --git a/crates/apps_lib/src/config/genesis/templates.rs b/crates/apps_lib/src/config/genesis/templates.rs index 253fda8268..dd4592ed62 100644 --- a/crates/apps_lib/src/config/genesis/templates.rs +++ b/crates/apps_lib/src/config/genesis/templates.rs @@ -5,20 +5,19 @@ use std::marker::PhantomData; use std::path::Path; use borsh::{BorshDeserialize, BorshSerialize}; -use namada::core::address::Address; -use namada::core::chain::ProposalBytes; -use namada::core::dec::Dec; -use namada::core::ethereum_structs; -use namada::core::token::{ - Amount, DenominatedAmount, Denomination, NATIVE_MAX_DECIMAL_PLACES, -}; -use namada::eth_bridge::storage::parameters::{ - Contracts, Erc20WhitelistEntry, MinimumConfirmations, -}; -use namada::token; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; +use namada_sdk::address::Address; +use namada_sdk::chain::ProposalBytes; +use namada_sdk::dec::Dec; +use namada_sdk::eth_bridge::storage::parameters::{ + Contracts, Erc20WhitelistEntry, MinimumConfirmations, +}; +use namada_sdk::token::{ + Amount, DenominatedAmount, Denomination, NATIVE_MAX_DECIMAL_PLACES, +}; +use namada_sdk::{ethereum_structs, token}; use serde::{Deserialize, Serialize}; use super::transactions::{self, Transactions}; @@ -990,9 +989,9 @@ mod tests { use std::fs; use std::path::PathBuf; - use namada::core::key; - use namada::core::key::RefTo; - use namada::core::string_encoding::StringEncoded; + use namada_sdk::key; + use namada_sdk::key::RefTo; + use namada_sdk::string_encoding::StringEncoded; use tempfile::tempdir; use super::*; diff --git a/crates/apps_lib/src/config/genesis/transactions.rs b/crates/apps_lib/src/config/genesis/transactions.rs index e0a641d549..f066b429a7 100644 --- a/crates/apps_lib/src/config/genesis/transactions.rs +++ b/crates/apps_lib/src/config/genesis/transactions.rs @@ -10,31 +10,29 @@ use itertools::{Either, Itertools}; use ledger_namada_rs::NamadaApp; use ledger_transport_hid::hidapi::HidApi; use ledger_transport_hid::TransportNativeHID; -use namada::account::AccountPublicKeysMap; -use namada::core::address::{Address, EstablishedAddress}; -use namada::core::chain::ChainId; -use namada::core::collections::HashSet; -use namada::core::dec::Dec; -use namada::core::key::{ - common, ed25519, RefTo, SerializeWithBorsh, SigScheme, -}; -use namada::core::string_encoding::StringEncoded; -use namada::core::time::DateTimeUtc; -use namada::core::token; -use namada::core::token::{DenominatedAmount, NATIVE_MAX_DECIMAL_PLACES}; -use namada::ledger::pos::common::PublicKey; -use namada::ledger::pos::types::ValidatorMetaData; -use namada::proof_of_stake::parameters::MAX_VALIDATOR_METADATA_LEN; -use namada::tx::data::{pos, Fee, TxType}; -use namada::tx::{ - verify_standalone_sig, Code, Commitment, Data, Section, SignatureIndex, Tx, -}; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; +use namada_sdk::account::AccountPublicKeysMap; +use namada_sdk::address::{Address, EstablishedAddress}; use namada_sdk::args::Tx as TxArgs; +use namada_sdk::chain::ChainId; +use namada_sdk::collections::HashSet; +use namada_sdk::dec::Dec; +use namada_sdk::key::common::PublicKey; +use namada_sdk::key::{common, ed25519, RefTo, SerializeWithBorsh, SigScheme}; +use namada_sdk::proof_of_stake::parameters::MAX_VALIDATOR_METADATA_LEN; +use namada_sdk::proof_of_stake::types::ValidatorMetaData; use namada_sdk::signing::{sign_tx, SigningTxData}; -use namada_sdk::tx::{TX_BECOME_VALIDATOR_WASM, TX_BOND_WASM}; +use namada_sdk::string_encoding::StringEncoded; +use namada_sdk::time::DateTimeUtc; +use namada_sdk::token; +use namada_sdk::token::{DenominatedAmount, NATIVE_MAX_DECIMAL_PLACES}; +use namada_sdk::tx::data::{pos, Fee, TxType}; +use namada_sdk::tx::{ + verify_standalone_sig, Code, Commitment, Data, Section, SignatureIndex, Tx, + TX_BECOME_VALIDATOR_WASM, TX_BOND_WASM, +}; use namada_sdk::wallet::alias::Alias; use namada_sdk::wallet::pre_genesis::ValidatorWallet; use namada_sdk::wallet::Wallet; @@ -364,7 +362,7 @@ pub async fn sign_validator_account_tx( tx_data: &T, keypair: &common::SecretKey, ) -> StringEncoded { - StringEncoded::new(namada::tx::standalone_signature::< + StringEncoded::new(namada_sdk::tx::standalone_signature::< T, SerializeWithBorsh, >(keypair, tx_data)) @@ -548,7 +546,7 @@ impl Transactions { stakes.into_values().any(|stake| { let tendermint_voting_power = - namada::ledger::pos::into_tm_voting_power( + namada_sdk::proof_of_stake::types::into_tm_voting_power( votes_per_token, stake, ); diff --git a/crates/apps_lib/src/config/genesis/utils.rs b/crates/apps_lib/src/config/genesis/utils.rs index ea1acd9cbd..ee8d6bcadd 100644 --- a/crates/apps_lib/src/config/genesis/utils.rs +++ b/crates/apps_lib/src/config/genesis/utils.rs @@ -3,9 +3,9 @@ use std::path::Path; use eyre::Context; use ledger_namada_rs::NamadaApp; use ledger_transport_hid::TransportNativeHID; -use namada::core::collections::HashSet; -use namada::core::key::common; -use namada::tx::Tx; +use namada_sdk::collections::HashSet; +use namada_sdk::key::common; +use namada_sdk::tx::Tx; use namada_sdk::wallet::Wallet; use namada_sdk::{error, signing}; use serde::de::DeserializeOwned; diff --git a/crates/apps_lib/src/config/global.rs b/crates/apps_lib/src/config/global.rs index ef1d5ecf5d..c9656c722f 100644 --- a/crates/apps_lib/src/config/global.rs +++ b/crates/apps_lib/src/config/global.rs @@ -4,7 +4,7 @@ use std::fs::{create_dir_all, File}; use std::io::Write; use std::path::{Path, PathBuf}; -use namada::core::chain::ChainId; +use namada_sdk::chain::ChainId; use serde::{Deserialize, Serialize}; use thiserror::Error; diff --git a/crates/apps_lib/src/config/mod.rs b/crates/apps_lib/src/config/mod.rs index 9a2dab326f..eb63dc4a67 100644 --- a/crates/apps_lib/src/config/mod.rs +++ b/crates/apps_lib/src/config/mod.rs @@ -11,10 +11,10 @@ use std::num::NonZeroU64; use std::path::{Path, PathBuf}; use directories::ProjectDirs; -use namada::core::chain::ChainId; -use namada::core::collections::HashMap; -use namada::core::storage::BlockHeight; -use namada::core::time::Rfc3339String; +use namada_sdk::chain::ChainId; +use namada_sdk::collections::HashMap; +use namada_sdk::storage::BlockHeight; +use namada_sdk::time::Rfc3339String; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -46,7 +46,7 @@ pub struct Config { #[derive(Debug, Serialize, Deserialize)] pub struct ValidatorLocalConfig { pub accepted_gas_tokens: - HashMap, + HashMap, } #[derive(Debug, Serialize, Deserialize)] diff --git a/crates/apps_lib/src/lib.rs b/crates/apps_lib/src/lib.rs index 58cdcbf701..29a18fd78b 100644 --- a/crates/apps_lib/src/lib.rs +++ b/crates/apps_lib/src/lib.rs @@ -21,14 +21,15 @@ pub mod logging; pub mod tendermint_node; pub mod wallet; pub mod wasm_loader; - // This is here only to include the std's docs in our docs. // Taken from . #[doc(inline)] pub use std; +pub use namada_sdk::*; + pub mod facade { // TODO(namada#3248): only re-export v037 `tendermint-rs` - pub use namada::{tendermint, tendermint_proto, tendermint_rpc}; - pub use tendermint_config; + pub use namada_core::{tendermint, tendermint_proto}; + pub use {tendermint_config, tendermint_rpc}; } diff --git a/crates/apps_lib/src/tendermint_node.rs b/crates/apps_lib/src/tendermint_node.rs index 17b6d4a9ec..1fc100e027 100644 --- a/crates/apps_lib/src/tendermint_node.rs +++ b/crates/apps_lib/src/tendermint_node.rs @@ -1,7 +1,7 @@ use std::path::{Path, PathBuf}; use borsh_ext::BorshSerializeExt; -use namada::core::key::*; +use namada_sdk::key::*; use serde_json::json; use sha2::{Digest, Sha256}; use thiserror::Error; diff --git a/crates/apps_lib/src/wallet/defaults.rs b/crates/apps_lib/src/wallet/defaults.rs index 2393b28c6b..7ea0c136d9 100644 --- a/crates/apps_lib/src/wallet/defaults.rs +++ b/crates/apps_lib/src/wallet/defaults.rs @@ -11,16 +11,17 @@ pub use dev::{ #[cfg(any(test, feature = "testing", feature = "benches"))] mod dev { use lazy_static::lazy_static; - use namada::core::address::testing::{ + use namada_sdk::address::testing::{ apfel, btc, dot, eth, kartoffel, nam, schnitzel, }; - use namada::core::address::Address; - use namada::core::collections::HashMap; - use namada::core::key::*; - use namada::ledger::{governance, pgf, pos}; + use namada_sdk::address::Address; + use namada_sdk::collections::HashMap; + use namada_sdk::governance::pgf; + use namada_sdk::key::*; use namada_sdk::wallet::alias::Alias; use namada_sdk::wallet::pre_genesis::ValidatorWallet; use namada_sdk::wallet::Wallet; + use namada_sdk::{governance, proof_of_stake}; use crate::wallet::CliWalletUtils; @@ -68,8 +69,8 @@ mod dev { /// The default addresses with their aliases. pub fn addresses() -> HashMap { let mut addresses: HashMap = vec![ - ("pos".into(), pos::ADDRESS), - ("pos_slash_pool".into(), pos::SLASH_POOL_ADDRESS), + ("pos".into(), proof_of_stake::ADDRESS), + ("pos_slash_pool".into(), proof_of_stake::SLASH_POOL_ADDRESS), ("governance".into(), governance::ADDRESS), ("governance".into(), pgf::ADDRESS), ("validator".into(), validator_address()), @@ -78,7 +79,7 @@ mod dev { ("christel".into(), christel_address()), ("daewon".into(), daewon_address()), ("ester".into(), ester_address()), - ("masp".into(), namada::core::address::MASP), + ("masp".into(), namada_sdk::address::MASP), ] .into_iter() .collect(); diff --git a/crates/apps_lib/src/wallet/mod.rs b/crates/apps_lib/src/wallet/mod.rs index 70cb3c7869..c95bdebf0d 100644 --- a/crates/apps_lib/src/wallet/mod.rs +++ b/crates/apps_lib/src/wallet/mod.rs @@ -6,8 +6,8 @@ use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::{env, fs}; -use namada::bip39::{Language, Mnemonic}; -use namada::core::key::*; +use namada_sdk::bip39::{Language, Mnemonic}; +use namada_sdk::key::*; pub use namada_sdk::wallet::alias::Alias; use namada_sdk::wallet::fs::FsWalletStorage; use namada_sdk::wallet::store::Store; @@ -286,7 +286,7 @@ pub fn read_and_confirm_encryption_password( #[cfg(test)] mod tests { - use namada::bip39::MnemonicType; + use namada_sdk::bip39::MnemonicType; use namada_sdk::wallet::WalletIo; use super::CliWalletUtils; diff --git a/crates/apps_lib/src/wallet/pre_genesis.rs b/crates/apps_lib/src/wallet/pre_genesis.rs index a39a8358f6..d383688831 100644 --- a/crates/apps_lib/src/wallet/pre_genesis.rs +++ b/crates/apps_lib/src/wallet/pre_genesis.rs @@ -3,7 +3,7 @@ use std::io::{Read, Write}; use std::path::{Path, PathBuf}; use fd_lock::RwLock; -use namada::core::key::SchemeType; +use namada_sdk::key::SchemeType; use namada_sdk::wallet::pre_genesis::{ ReadError, ValidatorStore, ValidatorWallet, }; diff --git a/crates/apps_lib/src/wallet/store.rs b/crates/apps_lib/src/wallet/store.rs index 20021765a5..15cce41b6f 100644 --- a/crates/apps_lib/src/wallet/store.rs +++ b/crates/apps_lib/src/wallet/store.rs @@ -1,6 +1,6 @@ use std::path::{Path, PathBuf}; -use namada::core::key::*; +use namada_sdk::key::*; use namada_sdk::wallet::{ gen_secret_key, LoadStoreError, Store, ValidatorKeys, }; @@ -59,7 +59,7 @@ pub fn gen_validator_keys( #[cfg(test)] mod test_wallet { - use namada::core::address::Address; + use namada_sdk::address::Address; use super::*; diff --git a/crates/apps_lib/src/wasm_loader/mod.rs b/crates/apps_lib/src/wasm_loader/mod.rs index 30cb874be7..f9f9be70f4 100644 --- a/crates/apps_lib/src/wasm_loader/mod.rs +++ b/crates/apps_lib/src/wasm_loader/mod.rs @@ -6,7 +6,7 @@ use std::path::Path; use data_encoding::HEXLOWER; use eyre::{eyre, WrapErr}; use futures::future::join_all; -use namada::core::collections::HashMap; +use namada_sdk::collections::HashMap; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use thiserror::Error; diff --git a/crates/benches/Cargo.toml b/crates/benches/Cargo.toml index 947db3698b..0cf8131cf5 100644 --- a/crates/benches/Cargo.toml +++ b/crates/benches/Cargo.toml @@ -41,10 +41,13 @@ namada-eth-bridge = [ # NOTE: this crate MUST NOT import any dependency with testing features to prevent benchmarking non-production code [dev-dependencies] -namada_apps_lib = { path = "../apps_lib" } +namada_apps_lib = { path = "../apps_lib", features = ["benches"] } namada_node = { path = "../node", features = ["benches"] } +namada_vm = { path = "../vm", features = ["wasm-runtime"] } +namada_vp = { path = "../vp" } + masp_primitives.workspace = true -masp_proofs = { workspace = true, features = ["benchmarks"] } +masp_proofs = { workspace = true, features = ["benchmarks", "multicore"] } borsh.workspace = true borsh-ext.workspace = true criterion = { version = "0.5", features = ["html_reports"] } diff --git a/crates/benches/README.md b/crates/benches/README.md index 2bdf837424..d98268b9d6 100644 --- a/crates/benches/README.md +++ b/crates/benches/README.md @@ -6,7 +6,7 @@ Measurements are taken on the elapsed wall-time. The benchmarks only focus on successful transactions and vps: in case of failure, the bench function shall panic to avoid timing incomplete execution paths. -In addition, this crate also contains benchmarks for `WrapperTx` (`namada::core::transaction::wrapper::WrapperTx`) validation and `host_env` (`namada::vm::host_env`) exposed functions that define the gas constants of `gas` (`namada::core::ledger::gas`). +In addition, this crate also contains benchmarks for `WrapperTx` (`namada_apps_lib::tx::wrapper::WrapperTx`) validation and `host_env` (`namada_vm::host_env`) exposed functions that define the gas constants of `gas` (`namada_apps_lib::gas`). For more realistic results these benchmarks should be run on all the combination of supported OS/architecture. diff --git a/crates/benches/host_env.rs b/crates/benches/host_env.rs index d24a92ace5..72e944cfc6 100644 --- a/crates/benches/host_env.rs +++ b/crates/benches/host_env.rs @@ -1,17 +1,16 @@ use criterion::{criterion_group, criterion_main, Criterion}; -use namada::core::account::AccountPublicKeysMap; -use namada::core::address; -use namada::core::collections::{HashMap, HashSet}; -use namada::ledger::storage::DB; -use namada::token::{Amount, Transfer}; -use namada::tx::Authorization; -use namada::vm::wasm::TxCache; +use namada_apps_lib::account::AccountPublicKeysMap; +use namada_apps_lib::collections::{HashMap, HashSet}; +use namada_apps_lib::storage::DB; +use namada_apps_lib::token::{Amount, Transfer}; +use namada_apps_lib::tx::Authorization; use namada_apps_lib::wallet::defaults; -use namada_apps_lib::wasm_loader; +use namada_apps_lib::{address, storage, wasm_loader}; use namada_node::bench_utils::{ BenchShell, TX_INIT_PROPOSAL_WASM, TX_REVEAL_PK_WASM, TX_TRANSFER_WASM, TX_UPDATE_ACCOUNT_WASM, VP_USER_WASM, WASM_DIR, }; +use namada_vm::wasm::TxCache; // Benchmarks the validation of a single signature on a single `Section` of a // transaction @@ -129,7 +128,7 @@ fn untrusted_wasm_validation(c: &mut Criterion) { let len = wasm_code.len() as u64; group.throughput(criterion::Throughput::Bytes(len)); group.bench_function(format!("Tx: {tx}, size: {len}"), |b| { - b.iter(|| namada::vm::validate_untrusted_wasm(&wasm_code).unwrap()) + b.iter(|| namada_vm::validate_untrusted_wasm(&wasm_code).unwrap()) }); } group.finish(); @@ -182,7 +181,7 @@ fn write_log_read(c: &mut Criterion) { let mut shell = BenchShell::default(); for (key, value_len) in generate_random_keys_sized() { - let key = namada::core::storage::Key::parse(key).unwrap(); + let key = storage::Key::parse(key).unwrap(); // Extract the throughput, together with the wall-time, so that we can // than invert it to calculate the desired metric (time/byte) // NOTE: criterion states that the throughput is measured on the @@ -213,7 +212,7 @@ fn storage_read(c: &mut Criterion) { let mut shell = BenchShell::default(); for (key, value_len) in generate_random_keys_sized() { - let key = namada::core::storage::Key::parse(key).unwrap(); + let key = storage::Key::parse(key).unwrap(); // Extract the throughput, together with the wall-time, so that we can // than invert it to calculate the desired metric (time/byte) // NOTE: criterion states that the throughput is measured on the @@ -247,7 +246,7 @@ fn write_log_write(c: &mut Criterion) { let mut shell = BenchShell::default(); for (key, value_len) in generate_random_keys_sized() { - let key = namada::core::storage::Key::parse(key).unwrap(); + let key = storage::Key::parse(key).unwrap(); // Extract the throughput, together with the wall-time, so that we can // than invert it to calculate the desired metric (time/byte) // NOTE: criterion states that the throughput is measured on the @@ -282,7 +281,7 @@ fn storage_write(c: &mut Criterion) { let mut shell = BenchShell::default(); for (key, value_len) in generate_random_keys_sized() { - let key = namada::core::storage::Key::parse(key).unwrap(); + let key = storage::Key::parse(key).unwrap(); // Extract the throughput, together with the wall-time, so that we can // than invert it to calculate the desired metric (time/byte) // NOTE: criterion states that the throughput is measured on the diff --git a/crates/benches/native_vps.rs b/crates/benches/native_vps.rs index 42856047e4..36e561c865 100644 --- a/crates/benches/native_vps.rs +++ b/crates/benches/native_vps.rs @@ -12,58 +12,53 @@ use masp_primitives::transaction::txid::TxIdDigester; use masp_primitives::transaction::TransactionData; use masp_proofs::group::GroupEncoding; use masp_proofs::sapling::BatchValidator; -use namada::core::address::{self, Address, InternalAddress}; -use namada::core::collections::HashMap; -use namada::core::eth_bridge_pool::{GasFee, PendingTransfer}; -use namada::core::masp::{TransferSource, TransferTarget}; -use namada::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; -use namada::eth_bridge::storage::whitelist; -use namada::governance::pgf::storage::steward::StewardDetail; -use namada::governance::storage::proposal::ProposalType; -use namada::governance::storage::vote::ProposalVote; -use namada::governance::{InitProposalData, VoteProposalData}; -use namada::ibc::core::channel::types::channel::Order; -use namada::ibc::core::channel::types::msgs::MsgChannelOpenInit; -use namada::ibc::core::channel::types::Version as ChannelVersion; -use namada::ibc::core::commitment_types::commitment::CommitmentPrefix; -use namada::ibc::core::connection::types::msgs::MsgConnectionOpenInit; -use namada::ibc::core::connection::types::version::Version; -use namada::ibc::core::connection::types::Counterparty; -use namada::ibc::core::host::types::identifiers::{ +use namada_apps_lib::address::{self, Address, InternalAddress}; +use namada_apps_lib::collections::HashMap; +use namada_apps_lib::eth_bridge::read_native_erc20_address; +use namada_apps_lib::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; +use namada_apps_lib::eth_bridge::storage::whitelist; +use namada_apps_lib::eth_bridge_pool::{GasFee, PendingTransfer}; +use namada_apps_lib::gas::{TxGasMeter, VpGasMeter}; +use namada_apps_lib::governance::pgf::storage::steward::StewardDetail; +use namada_apps_lib::governance::storage::proposal::ProposalType; +use namada_apps_lib::governance::storage::vote::ProposalVote; +use namada_apps_lib::governance::{InitProposalData, VoteProposalData}; +use namada_apps_lib::ibc::core::channel::types::channel::Order; +use namada_apps_lib::ibc::core::channel::types::msgs::MsgChannelOpenInit; +use namada_apps_lib::ibc::core::channel::types::Version as ChannelVersion; +use namada_apps_lib::ibc::core::commitment_types::commitment::CommitmentPrefix; +use namada_apps_lib::ibc::core::connection::types::msgs::MsgConnectionOpenInit; +use namada_apps_lib::ibc::core::connection::types::version::Version; +use namada_apps_lib::ibc::core::connection::types::Counterparty; +use namada_apps_lib::ibc::core::host::types::identifiers::{ ClientId, ConnectionId, PortId, }; -use namada::ibc::primitives::ToProto; -use namada::ibc::{IbcActions, NftTransferModule, TransferModule}; -use namada::ledger::eth_bridge::read_native_erc20_address; -use namada::ledger::gas::{TxGasMeter, VpGasMeter}; -use namada::ledger::governance::GovernanceVp; -use namada::ledger::native_vp::ethereum_bridge::bridge_pool_vp::BridgePoolVp; -use namada::ledger::native_vp::ethereum_bridge::nut::NonUsableTokens; -use namada::ledger::native_vp::ethereum_bridge::vp::EthBridge; -use namada::ledger::native_vp::ibc::context::PseudoExecutionContext; -use namada::ledger::native_vp::ibc::Ibc; -use namada::ledger::native_vp::masp::MaspVp; -use namada::ledger::native_vp::multitoken::MultitokenVp; -use namada::ledger::native_vp::parameters::ParametersVp; -use namada::ledger::native_vp::{Ctx, NativeVp}; -use namada::ledger::pgf::PgfVp; -use namada::ledger::pos::PosVP; -use namada::proof_of_stake; -use namada::proof_of_stake::KeySeg; -use namada::sdk::masp::{partial_deauthorize, preload_verifying_keys, PVKs}; -use namada::sdk::masp_primitives::merkle_tree::CommitmentTree; -use namada::sdk::masp_primitives::transaction::Transaction; -use namada::sdk::masp_proofs::sapling::SaplingVerificationContextInner; -use namada::state::{Epoch, StorageRead, StorageWrite, TxIndex}; -use namada::token::{Amount, Transfer}; -use namada::tx::{BatchedTx, Code, Section, Tx}; +use namada_apps_lib::ibc::primitives::ToProto; +use namada_apps_lib::ibc::{IbcActions, NftTransferModule, TransferModule}; +use namada_apps_lib::masp::{ + partial_deauthorize, preload_verifying_keys, PVKs, TransferSource, + TransferTarget, +}; +use namada_apps_lib::masp_primitives::merkle_tree::CommitmentTree; +use namada_apps_lib::masp_primitives::transaction::Transaction; +use namada_apps_lib::masp_proofs::sapling::SaplingVerificationContextInner; +use namada_apps_lib::proof_of_stake::KeySeg; +use namada_apps_lib::state::{Epoch, StorageRead, StorageWrite, TxIndex}; +use namada_apps_lib::token::{Amount, Transfer}; +use namada_apps_lib::tx::{BatchedTx, Code, Section, Tx}; +use namada_apps_lib::validation::{ + EthBridgeNutVp, EthBridgePoolVp, EthBridgeVp, GovernanceVp, IbcVp, + IbcVpContext, MaspVp, MultitokenVp, ParametersVp, PgfVp, PosVp, +}; use namada_apps_lib::wallet::defaults; +use namada_apps_lib::{governance, proof_of_stake, storage, token}; use namada_node::bench_utils::{ generate_foreign_key_tx, BenchShell, BenchShieldedCtx, ALBERT_PAYMENT_ADDRESS, ALBERT_SPENDING_KEY, BERTHA_PAYMENT_ADDRESS, TX_BRIDGE_POOL_WASM, TX_IBC_WASM, TX_INIT_PROPOSAL_WASM, TX_RESIGN_STEWARD, TX_TRANSFER_WASM, TX_UPDATE_STEWARD_COMMISSION, TX_VOTE_PROPOSAL_WASM, }; +use namada_vp::native_vp::{Ctx, NativeVp}; use rand_core::OsRng; fn governance(c: &mut Criterion) { @@ -144,9 +139,9 @@ fn governance(c: &mut Criterion) { } "complete_proposal" => { let max_code_size_key = - namada::governance::storage::keys::get_max_proposal_code_size_key(); + governance::storage::keys::get_max_proposal_code_size_key(); let max_proposal_content_key = - namada::governance::storage::keys::get_max_proposal_content_key(); + governance::storage::keys::get_max_proposal_content_key(); let max_code_size: u64 = shell .state .read(&max_code_size_key) @@ -211,19 +206,17 @@ fn governance(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let governance = GovernanceVp { - ctx: Ctx::new( - &Address::Internal(InternalAddress::Governance), - &shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ), - }; + let governance = GovernanceVp::new(Ctx::new( + &Address::Internal(InternalAddress::Governance), + &shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shell.vp_wasm_cache.clone(), + )); group.bench_function(bench_name, |b| { b.iter(|| { @@ -292,7 +285,7 @@ fn governance(c: &mut Criterion) { // &tx, // &TxIndex(0), // -// VpGasMeter::new_from_tx_meter(&TxGasMeter::new_from_sub_limit( +// VpGasMeter::new_from_tx_meter(&TxGasMeter::new( // u64::MAX.into(), )), // &keys_changed, // &verifiers, @@ -322,11 +315,10 @@ fn prepare_ibc_tx_and_ctx(bench_name: &str) -> (BenchShieldedCtx, BatchedTx) { match bench_name { "open_connection" => { let mut shielded_ctx = BenchShieldedCtx::default(); - let _ = shielded_ctx.shell.init_ibc_client_state( - namada::core::storage::Key::from( + let _ = + shielded_ctx.shell.init_ibc_client_state(storage::Key::from( Address::Internal(InternalAddress::Ibc).to_db_key(), - ), - ); + )); let msg = MsgConnectionOpenInit { client_id_on_a: ClientId::new("07-tendermint", 1).unwrap(), counterparty: Counterparty::new( @@ -435,19 +427,17 @@ fn ibc(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let ibc = Ibc { - ctx: Ctx::new( - &Address::Internal(InternalAddress::Ibc), - &shielded_ctx.shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shielded_ctx.shell.vp_wasm_cache.clone(), - ), - }; + let ibc = IbcVp::new(Ctx::new( + &Address::Internal(InternalAddress::Ibc), + &shielded_ctx.shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shielded_ctx.shell.vp_wasm_cache.clone(), + )); group.bench_function(bench_name, |b| { b.iter(|| { @@ -502,19 +492,17 @@ fn vp_multitoken(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let multitoken = MultitokenVp { - ctx: Ctx::new( - &Address::Internal(InternalAddress::Multitoken), - &shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ), - }; + let multitoken = MultitokenVp::new(Ctx::new( + &Address::Internal(InternalAddress::Multitoken), + &shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shell.vp_wasm_cache.clone(), + )); group.bench_function(bench_name, |b| { b.iter(|| { @@ -567,12 +555,11 @@ fn setup_storage_for_masp_verification( shielded_ctx.shell.commit_masp_tx(shield_tx.tx); // Update the anchor in storage - let tree_key = namada::token::storage_key::masp_commitment_tree_key(); + let tree_key = token::storage_key::masp_commitment_tree_key(); let updated_tree: CommitmentTree = shielded_ctx.shell.state.read(&tree_key).unwrap().unwrap(); - let anchor_key = namada::token::storage_key::masp_commitment_anchor_key( - updated_tree.root(), - ); + let anchor_key = + token::storage_key::masp_commitment_anchor_key(updated_tree.root()); shielded_ctx.shell.state.write(&anchor_key, ()).unwrap(); shielded_ctx.shell.commit_block(); @@ -615,19 +602,17 @@ fn masp(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let masp = MaspVp { - ctx: Ctx::new( - &Address::Internal(InternalAddress::Masp), - &shielded_ctx.shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shielded_ctx.shell.vp_wasm_cache.clone(), - ), - }; + let masp = MaspVp::new(Ctx::new( + &Address::Internal(InternalAddress::Masp), + &shielded_ctx.shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shielded_ctx.shell.vp_wasm_cache.clone(), + )); b.iter(|| { assert!( @@ -1179,7 +1164,7 @@ fn pgf(c: &mut Criterion) { "steward_inflation_rate", ] { let mut shell = BenchShell::default(); - namada::governance::pgf::storage::keys::stewards_handle() + namada_apps_lib::governance::pgf::storage::keys::stewards_handle() .insert( &mut shell.state, defaults::albert_address(), @@ -1199,13 +1184,14 @@ fn pgf(c: &mut Criterion) { vec![&defaults::albert_keypair()], ), "steward_inflation_rate" => { - let data = namada::tx::data::pgf::UpdateStewardCommission { - steward: defaults::albert_address(), - commission: HashMap::from([( - defaults::albert_address(), - namada::core::dec::Dec::zero(), - )]), - }; + let data = + namada_apps_lib::tx::data::pgf::UpdateStewardCommission { + steward: defaults::albert_address(), + commission: HashMap::from([( + defaults::albert_address(), + namada_apps_lib::dec::Dec::zero(), + )]), + }; shell.generate_tx( TX_UPDATE_STEWARD_COMMISSION, data, @@ -1228,19 +1214,17 @@ fn pgf(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let pgf = PgfVp { - ctx: Ctx::new( - &Address::Internal(InternalAddress::Pgf), - &shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ), - }; + let pgf = PgfVp::new(Ctx::new( + &Address::Internal(InternalAddress::Pgf), + &shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shell.vp_wasm_cache.clone(), + )); group.bench_function(bench_name, |b| { b.iter(|| { @@ -1269,11 +1253,11 @@ fn eth_bridge_nut(c: &mut Criterion) { let signed_tx = { let data = PendingTransfer { - transfer: namada::core::eth_bridge_pool::TransferToEthereum { + transfer: namada_apps_lib::eth_bridge_pool::TransferToEthereum { kind: - namada::core::eth_bridge_pool::TransferToEthereumKind::Erc20, + namada_apps_lib::eth_bridge_pool::TransferToEthereumKind::Erc20, asset: native_erc20_addres, - recipient: namada::core::ethereum_events::EthAddress([1u8; 20]), + recipient: namada_apps_lib::ethereum_events::EthAddress([1u8; 20]), sender: defaults::albert_address(), amount: Amount::from(1), }, @@ -1304,19 +1288,17 @@ fn eth_bridge_nut(c: &mut Criterion) { Address::Internal(InternalAddress::Nut(native_erc20_addres)); let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(&TxGasMeter::new(u64::MAX))); - let nut = NonUsableTokens { - ctx: Ctx::new( - &vp_address, - &shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ), - }; + let nut = EthBridgeNutVp::new(Ctx::new( + &vp_address, + &shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shell.vp_wasm_cache.clone(), + )); c.bench_function("vp_eth_bridge_nut", |b| { b.iter(|| { @@ -1342,11 +1324,11 @@ fn eth_bridge(c: &mut Criterion) { let signed_tx = { let data = PendingTransfer { - transfer: namada::core::eth_bridge_pool::TransferToEthereum { + transfer: namada_apps_lib::eth_bridge_pool::TransferToEthereum { kind: - namada::core::eth_bridge_pool::TransferToEthereumKind::Erc20, + namada_apps_lib::eth_bridge_pool::TransferToEthereumKind::Erc20, asset: native_erc20_addres, - recipient: namada::core::ethereum_events::EthAddress([1u8; 20]), + recipient: namada_apps_lib::ethereum_events::EthAddress([1u8; 20]), sender: defaults::albert_address(), amount: Amount::from(1), }, @@ -1376,19 +1358,17 @@ fn eth_bridge(c: &mut Criterion) { let vp_address = Address::Internal(InternalAddress::EthBridge); let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(&TxGasMeter::new(u64::MAX))); - let eth_bridge = EthBridge { - ctx: Ctx::new( - &vp_address, - &shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ), - }; + let eth_bridge = EthBridgeVp::new(Ctx::new( + &vp_address, + &shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shell.vp_wasm_cache.clone(), + )); c.bench_function("vp_eth_bridge", |b| { b.iter(|| { @@ -1440,11 +1420,11 @@ fn eth_bridge_pool(c: &mut Criterion) { let signed_tx = { let data = PendingTransfer { - transfer: namada::core::eth_bridge_pool::TransferToEthereum { + transfer: namada_apps_lib::eth_bridge_pool::TransferToEthereum { kind: - namada::core::eth_bridge_pool::TransferToEthereumKind::Erc20, + namada_apps_lib::eth_bridge_pool::TransferToEthereumKind::Erc20, asset: native_erc20_addres, - recipient: namada::core::ethereum_events::EthAddress([1u8; 20]), + recipient: namada_apps_lib::ethereum_events::EthAddress([1u8; 20]), sender: defaults::albert_address(), amount: Amount::from(1), }, @@ -1474,19 +1454,17 @@ fn eth_bridge_pool(c: &mut Criterion) { let vp_address = Address::Internal(InternalAddress::EthBridgePool); let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(&TxGasMeter::new(u64::MAX))); - let bridge_pool = BridgePoolVp { - ctx: Ctx::new( - &vp_address, - &shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ), - }; + let bridge_pool = EthBridgePoolVp::new(Ctx::new( + &vp_address, + &shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shell.vp_wasm_cache.clone(), + )); c.bench_function("vp_eth_bridge_pool", |b| { b.iter(|| { @@ -1519,15 +1497,18 @@ fn parameters(c: &mut Criterion) { "parameter_change" => { // Simulate governance proposal to modify a parameter let min_proposal_fund_key = - namada::governance::storage::keys::get_min_proposal_fund_key(); + namada_apps_lib::governance::storage::keys::get_min_proposal_fund_key(); shell.state.write(&min_proposal_fund_key, 1_000).unwrap(); - let proposal_key = namada::governance::storage::keys::get_proposal_execution_key(0); + let proposal_key = namada_apps_lib::governance::storage::keys::get_proposal_execution_key(0); shell.state.write(&proposal_key, 0).unwrap(); // Return a dummy tx for validation - let mut tx = Tx::from_type(namada::tx::data::TxType::Raw); - tx.set_data(namada::tx::Data::new(borsh::to_vec(&0).unwrap())); + let mut tx = + Tx::from_type(namada_apps_lib::tx::data::TxType::Raw); + tx.set_data(namada_apps_lib::tx::Data::new( + borsh::to_vec(&0).unwrap(), + )); let verifiers_from_tx = BTreeSet::default(); let cmt = tx.first_commitments().unwrap().clone(); let batched_tx = tx.batch_tx(cmt); @@ -1545,19 +1526,17 @@ fn parameters(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let parameters = ParametersVp { - ctx: Ctx::new( - &vp_address, - &shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ), - }; + let parameters = ParametersVp::new(Ctx::new( + &vp_address, + &shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shell.vp_wasm_cache.clone(), + )); group.bench_function(bench_name, |b| { b.iter(|| { @@ -1593,15 +1572,18 @@ fn pos(c: &mut Criterion) { "parameter_change" => { // Simulate governance proposal to modify a parameter let min_proposal_fund_key = - namada::governance::storage::keys::get_min_proposal_fund_key(); + namada_apps_lib::governance::storage::keys::get_min_proposal_fund_key(); shell.state.write(&min_proposal_fund_key, 1_000).unwrap(); - let proposal_key = namada::governance::storage::keys::get_proposal_execution_key(0); + let proposal_key = namada_apps_lib::governance::storage::keys::get_proposal_execution_key(0); shell.state.write(&proposal_key, 0).unwrap(); // Return a dummy tx for validation - let mut tx = Tx::from_type(namada::tx::data::TxType::Raw); - tx.set_data(namada::tx::Data::new(borsh::to_vec(&0).unwrap())); + let mut tx = + Tx::from_type(namada_apps_lib::tx::data::TxType::Raw); + tx.set_data(namada_apps_lib::tx::Data::new( + borsh::to_vec(&0).unwrap(), + )); let verifiers_from_tx = BTreeSet::default(); let cmt = tx.first_commitments().unwrap().clone(); let batched_tx = tx.batch_tx(cmt); @@ -1619,19 +1601,17 @@ fn pos(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let pos = PosVP { - ctx: Ctx::new( - &vp_address, - &shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ), - }; + let pos = PosVp::new(Ctx::new( + &vp_address, + &shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shell.vp_wasm_cache.clone(), + )); group.bench_function(bench_name, |b| { b.iter(|| { @@ -1673,24 +1653,22 @@ fn ibc_vp_validate_action(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let ibc = Ibc { - ctx: Ctx::new( - &Address::Internal(InternalAddress::Ibc), - &shielded_ctx.shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shielded_ctx.shell.vp_wasm_cache.clone(), - ), - }; + let ibc = IbcVp::new(Ctx::new( + &Address::Internal(InternalAddress::Ibc), + &shielded_ctx.shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shielded_ctx.shell.vp_wasm_cache.clone(), + )); // Use an empty verifiers set placeholder for validation, this is only // needed in actual txs to addresses whose VPs should be triggered let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - let exec_ctx = PseudoExecutionContext::new(ibc.ctx.pre()); + let exec_ctx = IbcVpContext::new(ibc.ctx.pre()); let ctx = Rc::new(RefCell::new(exec_ctx)); let mut actions = IbcActions::new(ctx.clone(), verifiers.clone()); actions.set_validation_params(ibc.validation_params().unwrap()); @@ -1731,24 +1709,22 @@ fn ibc_vp_execute_action(c: &mut Criterion) { let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( &TxGasMeter::new(u64::MAX), )); - let ibc = Ibc { - ctx: Ctx::new( - &Address::Internal(InternalAddress::Ibc), - &shielded_ctx.shell.state, - &signed_tx.tx, - &signed_tx.cmt, - &TxIndex(0), - &gas_meter, - &keys_changed, - &verifiers, - shielded_ctx.shell.vp_wasm_cache.clone(), - ), - }; + let ibc = IbcVp::new(Ctx::new( + &Address::Internal(InternalAddress::Ibc), + &shielded_ctx.shell.state, + &signed_tx.tx, + &signed_tx.cmt, + &TxIndex(0), + &gas_meter, + &keys_changed, + &verifiers, + shielded_ctx.shell.vp_wasm_cache.clone(), + )); // Use an empty verifiers set placeholder for validation, this is only // needed in actual txs to addresses whose VPs should be triggered let verifiers = Rc::new(RefCell::new(BTreeSet::
::new())); - let exec_ctx = PseudoExecutionContext::new(ibc.ctx.pre()); + let exec_ctx = IbcVpContext::new(ibc.ctx.pre()); let ctx = Rc::new(RefCell::new(exec_ctx)); let mut actions = IbcActions::new(ctx.clone(), verifiers.clone()); diff --git a/crates/benches/process_wrapper.rs b/crates/benches/process_wrapper.rs index b05b508f5f..fd608e500d 100644 --- a/crates/benches/process_wrapper.rs +++ b/crates/benches/process_wrapper.rs @@ -1,12 +1,12 @@ use criterion::{criterion_group, criterion_main, Criterion}; -use namada::core::address; -use namada::core::key::RefTo; -use namada::core::storage::BlockHeight; -use namada::core::time::DateTimeUtc; -use namada::state::TxIndex; -use namada::token::{Amount, DenominatedAmount, Transfer}; -use namada::tx::data::{Fee, WrapperTx}; -use namada::tx::Authorization; +use namada_apps_lib::address; +use namada_apps_lib::key::RefTo; +use namada_apps_lib::state::TxIndex; +use namada_apps_lib::storage::BlockHeight; +use namada_apps_lib::time::DateTimeUtc; +use namada_apps_lib::token::{Amount, DenominatedAmount, Transfer}; +use namada_apps_lib::tx::data::{Fee, WrapperTx}; +use namada_apps_lib::tx::Authorization; use namada_apps_lib::wallet::defaults; use namada_node::bench_utils::{BenchShell, TX_TRANSFER_WASM}; use namada_node::shell::process_proposal::ValidationMeta; @@ -35,7 +35,7 @@ fn process_tx(c: &mut Criterion) { batched_tx .tx - .update_header(namada::tx::data::TxType::Wrapper(Box::new( + .update_header(namada_apps_lib::tx::data::TxType::Wrapper(Box::new( WrapperTx::new( Fee { token: address::testing::nam(), @@ -47,11 +47,13 @@ fn process_tx(c: &mut Criterion) { ))); batched_tx .tx - .add_section(namada::tx::Section::Authorization(Authorization::new( - batched_tx.tx.sechashes(), - [(0, defaults::albert_keypair())].into_iter().collect(), - None, - ))); + .add_section(namada_apps_lib::tx::Section::Authorization( + Authorization::new( + batched_tx.tx.sechashes(), + [(0, defaults::albert_keypair())].into_iter().collect(), + None, + ), + )); let wrapper = batched_tx.tx.to_bytes(); #[allow(clippy::disallowed_methods)] @@ -60,9 +62,13 @@ fn process_tx(c: &mut Criterion) { c.bench_function("wrapper_tx_validation", |b| { b.iter_batched_ref( || { + // This is safe because nothing else is using `shell.state` + // concurrently. + let temp_state = + unsafe { shell.state.with_static_temp_write_log() }; ( // Prevent block out of gas and replay protection - shell.state.with_temp_write_log(), + temp_state, ValidationMeta::from(shell.state.read_only()), shell.vp_wasm_cache.clone(), shell.tx_wasm_cache.clone(), diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 7c06522f77..72352cca06 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -29,7 +29,7 @@ migrations = [ "linkme", ] benches = ["proptest"] -control_flow = ["futures", "tokio"] +control_flow = ["futures", "tokio", "wasmtimer"] [dependencies] namada_macros = {path = "../macros"} @@ -73,10 +73,16 @@ tendermint = {workspace = true} tendermint-proto = {workspace = true} thiserror.workspace = true tiny-keccak = {version = "2.0.2", features = ["keccak"]} -tokio = { workspace = true, optional = true, features = ["full"]} tracing.workspace = true uint = "0.9.5" zeroize.workspace = true +wasmtimer = { workspace = true, optional = true } + +[target.'cfg(not(target_family = "wasm"))'.dependencies] +tokio = { workspace = true, optional = true, features = ["full"] } + +[target.'cfg(target_family = "wasm")'.dependencies] +tokio = { workspace = true, optional = true, default-features = false, features = ["sync"] } [dev-dependencies] assert_matches.workspace = true @@ -89,3 +95,4 @@ test-log.workspace = true tokio = { workspace = true, features = ["full"] } toml.workspace = true tracing-subscriber.workspace = true +wasmtimer.workspace = true diff --git a/crates/core/proptest-regressions/ledger/storage/mod.txt b/crates/core/proptest-regressions/ledger/storage/mod.txt deleted file mode 100644 index 5e452257c0..0000000000 --- a/crates/core/proptest-regressions/ledger/storage/mod.txt +++ /dev/null @@ -1,2 +0,0 @@ -cc 2c5330b824e07348ee588e53138f5df5895149db6b11f98343380b663c8c344c - diff --git a/crates/core/proptest-regressions/ledger/storage/wl_storage.txt b/crates/core/proptest-regressions/ledger/storage/wl_storage.txt deleted file mode 100644 index 596dfa3497..0000000000 --- a/crates/core/proptest-regressions/ledger/storage/wl_storage.txt +++ /dev/null @@ -1,8 +0,0 @@ -# Seeds for failure cases proptest has generated in the past. It is -# automatically read and these particular cases re-run before any -# novel cases are generated. -# -# It is recommended to check this file in to source control so that -# everyone who runs the test benefits from these saved cases. -cc dda57e313536d5b8da109e808f0ea8af87981e843db9907ab75adcf58c3ad7a0 # shrinks to key_vals = [(Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [StringSeg("_"), StringSeg("0")] }, Storage(0)), (Key { segments: [StringSeg("_"), StringSeg("_")] }, Storage(0)), (Key { segments: [StringSeg("_A"), StringSeg("A")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [StringSeg("_"), StringSeg("0")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [StringSeg("_"), StringSeg("0")] }, BlockWriteLog(Write(0))), (Key { segments: [StringSeg("_"), StringSeg("0")] }, BlockWriteLog(Delete)), (Key { segments: [StringSeg("_"), StringSeg("0")] }, BlockWriteLog(Delete)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [StringSeg("_")] }, BlockWriteLog(DeletePrefix))] -cc be5b2533eaa2312359cefbe482c60fa93524e9decae0905af2b9aec936c05a56 # shrinks to key_vals = [(Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [StringSeg("a"), StringSeg("0")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [StringSeg("_"), StringSeg("_")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [StringSeg("a"), StringSeg("A")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [StringSeg("_0"), StringSeg("_")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, Storage(0)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d), StringSeg("?")] }, BlockWriteLog(Write(0))), (Key { segments: [StringSeg("_"), StringSeg("_")] }, BlockWriteLog(Delete)), (Key { segments: [StringSeg("a"), StringSeg("0")] }, BlockWriteLog(Delete)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [AddressSeg(Established: tnam1qxn7zvrfg9nvmw2mrcdmecljrexm6cl5d535w89d)] }, BlockWriteLog(DeletePrefix)), (Key { segments: [StringSeg("_")] }, TxWriteLog(DeletePrefix))] diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 5a33d6c932..200b6971cf 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -24,9 +24,7 @@ pub mod control_flow; pub mod governance; pub mod hints; pub mod proof_of_stake; -mod wasm_cache; -pub use wasm_cache::{WasmCacheAccess, WasmCacheRoAccess, WasmCacheRwAccess}; // TODO(namada#3248): only re-export v037 `tendermint-rs` pub use {masp_primitives, tendermint, tendermint_proto}; /// Borsh binary encoding (re-exported) from official crate with custom ext. diff --git a/crates/core/src/parameters.rs b/crates/core/src/parameters.rs index 1ce88e8d84..db013266d2 100644 --- a/crates/core/src/parameters.rs +++ b/crates/core/src/parameters.rs @@ -36,11 +36,6 @@ pub trait Read { storage: &S, ) -> Result; - /// Get the max signatures per transactio parameter - fn max_signatures_per_transaction( - storage: &S, - ) -> Result, Self::Err>; - /// Helper function to retrieve the `is_native_token_transferable` protocol /// parameter from storage fn is_native_token_transferable(storage: &S) -> Result; diff --git a/crates/core/src/storage.rs b/crates/core/src/storage.rs index a443cb7805..6ca5cd4e66 100644 --- a/crates/core/src/storage.rs +++ b/crates/core/src/storage.rs @@ -2102,7 +2102,7 @@ pub mod testing { /// A dummy header used for testing pub fn get_dummy_header() -> Header { - use crate::time::{DateTimeUtc, DurationSecs}; + use crate::time::DurationSecs; Header { hash: Hash([0; 32]), #[allow( diff --git a/crates/core/src/wasm_cache.rs b/crates/core/src/wasm_cache.rs deleted file mode 100644 index 1d47cf5680..0000000000 --- a/crates/core/src/wasm_cache.rs +++ /dev/null @@ -1,25 +0,0 @@ -/// WASM Cache access level, used to limit dry-ran transactions to read-only -/// cache access. -pub trait WasmCacheAccess: Clone + std::fmt::Debug + Default { - /// Is access read/write? - fn is_read_write() -> bool; -} - -/// Regular read/write caches access -#[derive(Debug, Clone, Default)] -pub struct WasmCacheRwAccess; -impl WasmCacheAccess for WasmCacheRwAccess { - fn is_read_write() -> bool { - true - } -} - -/// Restricted read-only access for dry-ran transactions -#[derive(Debug, Clone, Default)] -pub struct WasmCacheRoAccess; - -impl WasmCacheAccess for WasmCacheRoAccess { - fn is_read_write() -> bool { - false - } -} diff --git a/crates/encoding_spec/Cargo.toml b/crates/encoding_spec/Cargo.toml index ddc59eb0e6..d2999e1982 100644 --- a/crates/encoding_spec/Cargo.toml +++ b/crates/encoding_spec/Cargo.toml @@ -16,6 +16,11 @@ version.workspace = true default = [] [dependencies] +namada_account = { path = "../account" } +namada_core = { path = "../core" } +namada_token = { path = "../token" } +namada_tx = { path = "../tx" } + borsh.workspace = true itertools.workspace = true lazy_static.workspace = true diff --git a/crates/encoding_spec/src/main.rs b/crates/encoding_spec/src/main.rs index b0a3bdb858..48a087485d 100644 --- a/crates/encoding_spec/src/main.rs +++ b/crates/encoding_spec/src/main.rs @@ -23,13 +23,13 @@ use borsh::{schema, schema_container_of}; use itertools::Itertools; use lazy_static::lazy_static; use madato::types::TableRow; -use namada::core::address::Address; -use namada::core::collections::HashSet; -use namada::core::key::ed25519::{PublicKey, Signature}; -use namada::core::storage::{self, Epoch}; -use namada::ledger::parameters::Parameters; -use namada::tx::data::{pos, TxType, WrapperTx}; -use namada::{account, token}; +use namada_core::address::Address; +use namada_core::collections::HashSet; +use namada_core::key::ed25519::{PublicKey, Signature}; +use namada_core::parameters::Parameters; +use namada_core::storage::{self, Epoch}; +use namada_tx::data::{pos, TxType, WrapperTx}; +use {namada_account as account, namada_token as token}; /// This generator will write output into this `docs` file. const OUTPUT_PATH: &str = @@ -93,7 +93,7 @@ fn main() -> Result<(), Box> { // PoS // TODO add after - // TODO imported from `use namada::ledger::pos::Bonds;` + // TODO imported from `use namada_sdk::proof_of_stake::Bonds;` // let pos_bonds_schema = schema_container_of::(); // Merge type definitions diff --git a/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs b/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs index a75789da03..0687d17aec 100644 --- a/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs +++ b/crates/ethereum_bridge/src/vp/bridge_pool_vp.rs @@ -71,17 +71,25 @@ where { /// Context to interact with the host structures. pub ctx: Ctx<'ctx, S, CA, EVAL>, - /// Token keys type - pub token_keys: PhantomData, + /// Generic types for DI + pub _marker: PhantomData, } -impl<'a, S, CA, EVAL, TokenKeys> BridgePool<'a, S, CA, EVAL, TokenKeys> +impl<'ctx, S, CA, EVAL, TokenKeys> BridgePool<'ctx, S, CA, EVAL, TokenKeys> where S: 'static + StateRead, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, CA: 'static + Clone, TokenKeys: token::Keys, { + /// Instantiate bridge pool VP + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { + Self { + ctx, + _marker: PhantomData, + } + } + /// Get the change in the balance of an account /// associated with an address fn account_balance_delta( @@ -393,13 +401,13 @@ where /// Helper struct for handling the different escrow /// checking scenarios. -struct EscrowDelta<'a, KIND> { - token: Cow<'a, Address>, - payer_account: &'a Address, - escrow_account: &'a Address, +struct EscrowDelta<'ctx, KIND> { + token: Cow<'ctx, Address>, + payer_account: &'ctx Address, + escrow_account: &'ctx Address, expected_debit: Amount, expected_credit: Amount, - transferred_amount: &'a Amount, + transferred_amount: &'ctx Amount, _kind: PhantomData<*const KIND>, } @@ -477,9 +485,9 @@ impl EscrowDelta<'_, KIND> { /// /// 1. Check that gas fees were escrowed. /// 2. Check that the Nam to back wNam was escrowed. -struct EscrowCheck<'a> { - gas_check: EscrowDelta<'a, GasCheck>, - token_check: EscrowDelta<'a, TokenCheck>, +struct EscrowCheck<'ctx> { + gas_check: EscrowDelta<'ctx, GasCheck>, + token_check: EscrowDelta<'ctx, TokenCheck>, } impl EscrowCheck<'_> { @@ -515,18 +523,18 @@ fn sum_gas_and_token_amounts( }) } -impl<'a, S, CA, EVAL, TokenKeys> NativeVp<'a> - for BridgePool<'a, S, CA, EVAL, TokenKeys> +impl<'view, 'ctx: 'view, S, CA, EVAL, TokenKeys> NativeVp<'view> + for BridgePool<'ctx, S, CA, EVAL, TokenKeys> where S: 'static + StateRead, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, CA: 'static + Clone, TokenKeys: token::Keys, { type Error = Error; fn validate_tx( - &self, + &'view self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, @@ -682,7 +690,6 @@ mod test_bridge_pool_vp { use namada_core::borsh::BorshSerializeExt; use namada_core::eth_bridge_pool::{GasFee, TransferToEthereum}; use namada_core::hash::Hash; - use namada_core::WasmCacheRwAccess; use namada_gas::{TxGasMeter, VpGasMeter}; use namada_state::testing::TestState; use namada_state::write_log::WriteLog; @@ -692,6 +699,7 @@ mod test_bridge_pool_vp { use namada_tx::Tx; use namada_vm::wasm::run::VpEvalWasm; use namada_vm::wasm::VpCache; + use namada_vm::WasmCacheRwAccess; use super::*; use crate::storage::bridge_pool::get_signed_root_key; @@ -707,6 +715,8 @@ mod test_bridge_pool_vp { CA, >; type TokenKeys = namada_token::Store<()>; + type BridgePool<'a, S> = + super::BridgePool<'a, S, VpCache, Eval, TokenKeys>; /// The amount of NAM Bertha has const ASSET: EthAddress = EthAddress([0; 20]); @@ -856,7 +866,7 @@ mod test_bridge_pool_vp { // we only care about escrowing NAM I320::from(0), ); - write_log.commit_tx(); + write_log.commit_tx_to_batch(); } /// Update gas and token balances of an address and @@ -957,14 +967,14 @@ mod test_bridge_pool_vp { } /// Setup a ctx for running native vps - fn setup_ctx<'a>( - tx: &'a Tx, - state: &'a TestState, - gas_meter: &'a RefCell, - keys_changed: &'a BTreeSet, - verifiers: &'a BTreeSet
, - ) -> Ctx<'a, TestState, VpCache, Eval> { - let batched_tx = tx.batch_ref_first_tx(); + fn setup_ctx<'ctx>( + tx: &'ctx Tx, + state: &'ctx TestState, + gas_meter: &'ctx RefCell, + keys_changed: &'ctx BTreeSet, + verifiers: &'ctx BTreeSet
, + ) -> Ctx<'ctx, TestState, VpCache, Eval> { + let batched_tx = tx.batch_ref_first_tx().unwrap(); Ctx::new( &BRIDGE_POOL_ADDRESS, state, @@ -1051,17 +1061,20 @@ mod test_bridge_pool_vp { let verifiers = BTreeSet::default(); // create the data to be given to the vp let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), + &TxGasMeter::new(u64::MAX), + )); + let vp = BridgePool::new(setup_ctx( + &tx, + &state, + &gas_meter, + &keys_changed, + &verifiers, )); - let vp = BridgePool { - ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), - token_keys: PhantomData::, - }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); - let tx = tx.batch_ref_first_tx(); + let tx = tx.batch_ref_first_tx().unwrap(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); match (expect, res) { (Expect::Accepted, Ok(())) => (), @@ -1399,17 +1412,20 @@ mod test_bridge_pool_vp { // create the data to be given to the vp let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), + &TxGasMeter::new(u64::MAX), + )); + let vp = BridgePool::new(setup_ctx( + &tx, + &state, + &gas_meter, + &keys_changed, + &verifiers, )); - let vp = BridgePool { - ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), - token_keys: PhantomData::, - }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); - let tx = tx.batch_ref_first_tx(); + let tx = tx.batch_ref_first_tx().unwrap(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_err()); } @@ -1460,17 +1476,20 @@ mod test_bridge_pool_vp { let verifiers = BTreeSet::default(); // create the data to be given to the vp let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), + &TxGasMeter::new(u64::MAX), + )); + let vp = BridgePool::new(setup_ctx( + &tx, + &state, + &gas_meter, + &keys_changed, + &verifiers, )); - let vp = BridgePool { - ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), - token_keys: PhantomData::, - }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); - let tx = tx.batch_ref_first_tx(); + let tx = tx.batch_ref_first_tx().unwrap(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_err()); } @@ -1542,17 +1561,20 @@ mod test_bridge_pool_vp { let verifiers = BTreeSet::default(); // create the data to be given to the vp let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), + &TxGasMeter::new(u64::MAX), + )); + let vp = BridgePool::new(setup_ctx( + &tx, + &state, + &gas_meter, + &keys_changed, + &verifiers, )); - let vp = BridgePool { - ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), - token_keys: PhantomData::, - }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); - let tx = tx.batch_ref_first_tx(); + let tx = tx.batch_ref_first_tx().unwrap(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_ok()); } @@ -1619,17 +1641,20 @@ mod test_bridge_pool_vp { // create the data to be given to the vp let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), + &TxGasMeter::new(u64::MAX), + )); + let vp = BridgePool::new(setup_ctx( + &tx, + &state, + &gas_meter, + &keys_changed, + &verifiers, )); - let vp = BridgePool { - ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), - token_keys: PhantomData::, - }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); - let tx = tx.batch_ref_first_tx(); + let tx = tx.batch_ref_first_tx().unwrap(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_err()); } @@ -1653,7 +1678,7 @@ mod test_bridge_pool_vp { state .write(&gas_payer_balance_key, Amount::from(BERTHA_WEALTH)) .expect("Test failed"); - state.write_log_mut().commit_tx(); + state.write_log_mut().commit_tx_to_batch(); let mut tx = Tx::from_type(TxType::Raw); tx.push_default_inner_tx(); @@ -1713,17 +1738,20 @@ mod test_bridge_pool_vp { let verifiers = BTreeSet::default(); // create the data to be given to the vp let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), + &TxGasMeter::new(u64::MAX), + )); + let vp = BridgePool::new(setup_ctx( + &tx, + &state, + &gas_meter, + &keys_changed, + &verifiers, )); - let vp = BridgePool { - ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), - token_keys: PhantomData::, - }; let mut tx = Tx::new(state.in_mem().chain_id.clone(), None); tx.add_data(transfer); - let tx = tx.batch_ref_first_tx(); + let tx = tx.batch_ref_first_tx().unwrap(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); assert!(res.is_err()); } @@ -1793,18 +1821,21 @@ mod test_bridge_pool_vp { // create the data to be given to the vp let verifiers = BTreeSet::default(); let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), + &TxGasMeter::new(u64::MAX), + )); + let vp = BridgePool::new(setup_ctx( + &tx, + &state, + &gas_meter, + &keys_changed, + &verifiers, )); - let vp = BridgePool { - ctx: setup_ctx(&tx, &state, &gas_meter, &keys_changed, &verifiers), - token_keys: PhantomData::, - }; let mut tx = Tx::from_type(TxType::Raw); tx.push_default_inner_tx(); tx.add_data(transfer); - let tx = tx.batch_ref_first_tx(); + let tx = tx.batch_ref_first_tx().unwrap(); let res = vp.validate_tx(&tx, &keys_changed, &verifiers); match (expect, res) { (Expect::Accepted, Ok(())) => (), diff --git a/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs b/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs index 1cada0f280..2a10ec9399 100644 --- a/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs +++ b/crates/ethereum_bridge/src/vp/eth_bridge_vp.rs @@ -28,8 +28,8 @@ where { /// Context to interact with the host structures. pub ctx: Ctx<'ctx, S, CA, EVAL>, - /// Token keys type - pub token_keys: PhantomData, + /// Generic types for DI + pub _marker: PhantomData, } impl<'ctx, S, CA, EVAL, TokenKeys> EthBridge<'ctx, S, CA, EVAL, TokenKeys> @@ -39,6 +39,14 @@ where EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, TokenKeys: token::Keys, { + /// Instantiate eth bridge VP + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { + Self { + ctx, + _marker: PhantomData, + } + } + /// If the Ethereum bridge's escrow key was written to, we check /// that the NAM balance increased and that the Bridge pool VP has /// been triggered. @@ -77,12 +85,12 @@ where } } -impl<'a, S, CA, EVAL, TokenKeys> NativeVp<'a> - for EthBridge<'a, S, CA, EVAL, TokenKeys> +impl<'view, 'ctx: 'view, S, CA, EVAL, TokenKeys> NativeVp<'view> + for EthBridge<'ctx, S, CA, EVAL, TokenKeys> where S: 'static + StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, TokenKeys: token::Keys, { type Error = Error; @@ -98,7 +106,7 @@ where /// changes to the `eth_msgs/...` keys. For those cases, we reject here as /// no wasm transactions should be able to modify those keys. fn validate_tx( - &self, + &'view self, _: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -183,8 +191,8 @@ mod tests { use namada_core::address::testing::{established_address_1, nam, wnam}; use namada_core::borsh::BorshSerializeExt; + use namada_core::ethereum_events; use namada_core::ethereum_events::EthAddress; - use namada_core::{ethereum_events, WasmCacheRwAccess}; use namada_gas::{TxGasMeter, VpGasMeter}; use namada_state::testing::TestState; use namada_state::{StorageWrite, TxIndex}; @@ -193,6 +201,7 @@ mod tests { use namada_tx::{Tx, TxCommitments}; use namada_vm::wasm::run::VpEvalWasm; use namada_vm::wasm::VpCache; + use namada_vm::WasmCacheRwAccess; use rand::Rng; use super::*; @@ -215,6 +224,8 @@ mod tests { CA, >; type TokenKeys = namada_token::Store<()>; + type EthBridge<'a, S> = + super::EthBridge<'a, S, VpCache, Eval, TokenKeys>; /// Return some arbitrary random key belonging to this account fn arbitrary_key() -> Key { @@ -260,14 +271,14 @@ mod tests { } /// Setup a ctx for running native vps - fn setup_ctx<'a>( - tx: &'a Tx, - cmt: &'a TxCommitments, - state: &'a TestState, - gas_meter: &'a RefCell, - keys_changed: &'a BTreeSet, - verifiers: &'a BTreeSet
, - ) -> Ctx<'a, TestState, VpCache, Eval> { + fn setup_ctx<'ctx>( + tx: &'ctx Tx, + cmt: &'ctx TxCommitments, + state: &'ctx TestState, + gas_meter: &'ctx RefCell, + keys_changed: &'ctx BTreeSet, + verifiers: &'ctx BTreeSet
, + ) -> Ctx<'ctx, TestState, VpCache, Eval> { Ctx::new( &crate::ADDRESS, state, @@ -412,17 +423,14 @@ mod tests { &TxGasMeter::new(u64::MAX), )); let batched_tx = tx.batch_ref_first_tx().unwrap(); - let vp = EthBridge { - ctx: setup_ctx( - batched_tx.tx, - batched_tx.cmt, - &state, - &gas_meter, - &keys_changed, - &verifiers, - ), - token_keys: PhantomData::, - }; + let vp = EthBridge::new(setup_ctx( + batched_tx.tx, + batched_tx.cmt, + &state, + &gas_meter, + &keys_changed, + &verifiers, + )); let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers); assert!(res.is_ok()); @@ -467,17 +475,14 @@ mod tests { &TxGasMeter::new(u64::MAX), )); let batched_tx = tx.batch_ref_first_tx().unwrap(); - let vp = EthBridge { - ctx: setup_ctx( - batched_tx.tx, - batched_tx.cmt, - &state, - &gas_meter, - &keys_changed, - &verifiers, - ), - token_keys: PhantomData::, - }; + let vp = EthBridge::new(setup_ctx( + batched_tx.tx, + batched_tx.cmt, + &state, + &gas_meter, + &keys_changed, + &verifiers, + )); let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers); assert!(res.is_err()); @@ -525,17 +530,14 @@ mod tests { &TxGasMeter::new(u64::MAX), )); let batched_tx = tx.batch_ref_first_tx().unwrap(); - let vp = EthBridge { - ctx: setup_ctx( - batched_tx.tx, - batched_tx.cmt, - &state, - &gas_meter, - &keys_changed, - &verifiers, - ), - token_keys: PhantomData::, - }; + let vp = EthBridge::new(setup_ctx( + batched_tx.tx, + batched_tx.cmt, + &state, + &gas_meter, + &keys_changed, + &verifiers, + )); let res = vp.validate_tx(&batched_tx, &keys_changed, &verifiers); assert!(res.is_err()); diff --git a/crates/ethereum_bridge/src/vp/mod.rs b/crates/ethereum_bridge/src/vp/mod.rs index e16646f73e..2771996bd8 100644 --- a/crates/ethereum_bridge/src/vp/mod.rs +++ b/crates/ethereum_bridge/src/vp/mod.rs @@ -6,6 +6,6 @@ mod bridge_pool_vp; mod eth_bridge_vp; mod nut_vp; -pub use bridge_pool_vp::BridgePool; -pub use eth_bridge_vp::EthBridge; -pub use nut_vp::NonUsableTokens; +pub use bridge_pool_vp::{BridgePool, Error as BridgePoolError}; +pub use eth_bridge_vp::{Error as EthBridgeError, EthBridge}; +pub use nut_vp::{Error as NutError, NonUsableTokens}; diff --git a/crates/ethereum_bridge/src/vp/nut_vp.rs b/crates/ethereum_bridge/src/vp/nut_vp.rs index 90a9fb0d34..727f9c1c1f 100644 --- a/crates/ethereum_bridge/src/vp/nut_vp.rs +++ b/crates/ethereum_bridge/src/vp/nut_vp.rs @@ -28,22 +28,22 @@ where { /// Context to interact with the host structures. pub ctx: Ctx<'ctx, S, CA, EVAL>, - /// Token keys type - pub token_keys: PhantomData, + /// Generic types for DI + pub _marker: PhantomData, } -impl<'a, S, CA, EVAL, TokenKeys> NativeVp<'a> - for NonUsableTokens<'a, S, CA, EVAL, TokenKeys> +impl<'view, 'ctx: 'view, S, CA, EVAL, TokenKeys> NativeVp<'view> + for NonUsableTokens<'ctx, S, CA, EVAL, TokenKeys> where S: 'static + StateRead, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, CA: 'static + Clone, TokenKeys: token::Keys, { type Error = Error; fn validate_tx( - &self, + &'view self, _: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -127,6 +127,22 @@ where } } +impl<'ctx, S, CA, EVAL, TokenKeys> NonUsableTokens<'ctx, S, CA, EVAL, TokenKeys> +where + S: 'static + StateRead, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, + CA: 'static + Clone, + TokenKeys: token::Keys, +{ + /// Instantiate NUT VP + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { + Self { + ctx, + _marker: PhantomData, + } + } +} + #[cfg(test)] mod test_nuts { use std::cell::RefCell; @@ -136,7 +152,6 @@ mod test_nuts { use namada_core::borsh::BorshSerializeExt; use namada_core::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS; use namada_core::storage::TxIndex; - use namada_core::WasmCacheRwAccess; use namada_gas::{TxGasMeter, VpGasMeter}; use namada_state::testing::TestState; use namada_state::StorageWrite; @@ -145,6 +160,7 @@ mod test_nuts { use namada_tx::Tx; use namada_vm::wasm::run::VpEvalWasm; use namada_vm::wasm::VpCache; + use namada_vm::WasmCacheRwAccess; use proptest::prelude::*; use super::*; @@ -157,6 +173,8 @@ mod test_nuts { CA, >; type TokenKeys = namada_token::Store<()>; + type NonUsableTokens<'a, S> = + super::NonUsableTokens<'a, S, VpCache, Eval, TokenKeys>; /// Run a VP check on a NUT transfer between the two provided addresses. fn check_nut_transfer(src: Address, dst: Address) -> bool { @@ -225,10 +243,7 @@ mod test_nuts { &verifiers, VpCache::new(temp_dir(), 100usize), ); - let vp = NonUsableTokens { - ctx, - token_keys: PhantomData::, - }; + let vp = NonUsableTokens::new(ctx); // print debug info in case we run into failures for key in &keys_changed { diff --git a/crates/governance/src/vp/mod.rs b/crates/governance/src/vp/mod.rs index 33797e0c58..5ea32be495 100644 --- a/crates/governance/src/vp/mod.rs +++ b/crates/governance/src/vp/mod.rs @@ -48,29 +48,25 @@ pub enum Error { } /// Governance VP -pub struct GovernanceVp<'a, S, CA: 'a, EVAL, PoS, TokenKeys> +pub struct GovernanceVp<'ctx, S, CA, EVAL, PoS, TokenKeys> where - S: 'static + StateRead, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, - PoS: proof_of_stake::Read>, - TokenKeys: token::Keys, + S: StateRead, + EVAL: VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA, EVAL>, - /// Read PoS storage - pub pos: PhantomData, - /// Token keys - pub token_keys: PhantomData, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Generic types for DI + pub _marker: PhantomData<(PoS, TokenKeys)>, } -impl<'a, S, CA, EVAL, PoS, TokenKeys> NativeVp<'a> - for GovernanceVp<'a, S, CA, EVAL, PoS, TokenKeys> +impl<'view, 'ctx: 'view, S, CA, EVAL, PoS, TokenKeys> NativeVp<'view> + for GovernanceVp<'ctx, S, CA, EVAL, PoS, TokenKeys> where S: StateRead, - EVAL: VpEvaluator<'a, S, CA, EVAL>, CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, PoS: proof_of_stake::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = namada_state::StorageError, >, TokenKeys: token::Keys, @@ -78,7 +74,7 @@ where type Error = Error; fn validate_tx( - &'a self, + &'view self, tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -217,24 +213,23 @@ where } } -impl<'a, S, CA, EVAL, PoS, TokenKeys> - GovernanceVp<'a, S, CA, EVAL, PoS, TokenKeys> +impl<'view, 'ctx: 'view, S, CA, EVAL, PoS, TokenKeys> + GovernanceVp<'ctx, S, CA, EVAL, PoS, TokenKeys> where S: StateRead, - EVAL: VpEvaluator<'a, S, CA, EVAL>, CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, PoS: proof_of_stake::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = namada_state::StorageError, >, TokenKeys: token::Keys, { /// Instantiate a Governance VP - pub fn new(ctx: Ctx<'a, S, CA, EVAL>) -> Self { + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { Self { ctx, - pos: PhantomData, - token_keys: PhantomData, + _marker: PhantomData, } } @@ -276,7 +271,7 @@ where } fn is_valid_vote_key( - &'a self, + &'view self, proposal_id: u64, key: &storage::Key, verifiers: &BTreeSet
, @@ -1088,7 +1083,7 @@ where /// Check if a vote is from a validator pub fn is_validator( - &'a self, + &'view self, verifiers: &BTreeSet
, voter: &Address, validator: &Address, @@ -1146,7 +1141,7 @@ where /// Check if a vote is from a delegator pub fn is_delegator( - &'a self, + &'view self, epoch: Epoch, verifiers: &BTreeSet
, address: &Address, @@ -1248,7 +1243,7 @@ mod test { use namada_core::key::RefTo; use namada_core::storage::testing::get_dummy_header; use namada_core::time::DateTimeUtc; - use namada_core::{token, WasmCacheRwAccess}; + use namada_core::token; use namada_gas::{TxGasMeter, VpGasMeter}; use namada_parameters::Parameters; use namada_proof_of_stake::bond_tokens; @@ -1263,9 +1258,9 @@ mod test { use namada_tx::action::{Action, GovAction, Write}; use namada_tx::data::TxType; use namada_tx::{Authorization, Code, Data, Section, Tx}; - use namada_vm::wasm; use namada_vm::wasm::run::VpEvalWasm; use namada_vm::wasm::VpCache; + use namada_vm::{wasm, WasmCacheRwAccess}; use namada_vp::native_vp::{Ctx, CtxPreStorageRead, NativeVp}; use crate::storage::keys::{ @@ -1278,13 +1273,13 @@ mod test { type CA = WasmCacheRwAccess; type Eval = VpEvalWasm<::D, ::H, CA>; - type GovernanceVp<'a, S> = super::GovernanceVp< - 'a, + type GovernanceVp<'ctx, S> = super::GovernanceVp< + 'ctx, S, VpCache, Eval, namada_proof_of_stake::Store< - CtxPreStorageRead<'a, 'a, S, VpCache, Eval>, + CtxPreStorageRead<'ctx, 'ctx, S, VpCache, Eval>, >, namada_token::Store<()>, >; diff --git a/crates/governance/src/vp/pgf.rs b/crates/governance/src/vp/pgf.rs index 9ab436c24e..600a9c6905 100644 --- a/crates/governance/src/vp/pgf.rs +++ b/crates/governance/src/vp/pgf.rs @@ -32,25 +32,25 @@ pub enum Error { } /// Pgf VP -pub struct PgfVp<'a, S, CA, EVAL> +pub struct PgfVp<'ctx, S, CA, EVAL> where S: 'static + StateRead, - EVAL: VpEvaluator<'a, S, CA, EVAL>, + EVAL: VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA, EVAL>, + pub ctx: Ctx<'ctx, S, CA, EVAL>, } -impl<'a, S, CA, EVAL> NativeVp<'a> for PgfVp<'a, S, CA, EVAL> +impl<'view, 'ctx, S, CA, EVAL> NativeVp<'view> for PgfVp<'ctx, S, CA, EVAL> where S: 'static + StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, { type Error = Error; fn validate_tx( - &'a self, + &'view self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -207,15 +207,20 @@ where } } -impl<'a, S, CA, EVAL> PgfVp<'a, S, CA, EVAL> +impl<'view, 'ctx, S, CA, EVAL> PgfVp<'ctx, S, CA, EVAL> where S: 'static + StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, { + /// Instantiate PGF VP + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { + Self { ctx } + } + /// Validate a governance parameter pub fn is_valid_parameter_change( - &'a self, + &'view self, batched_tx: &BatchedTxRef<'_>, ) -> Result<()> { batched_tx.tx.data(batched_tx.cmt).map_or_else( @@ -242,8 +247,8 @@ where #[allow(clippy::upper_case_acronyms)] #[derive(Debug)] -enum KeyType<'a> { - Stewards(&'a Address), +enum KeyType<'ctx> { + Stewards(&'ctx Address), Fundings, PgfInflationRate, StewardInflationRate, diff --git a/crates/ibc/src/context/common.rs b/crates/ibc/src/context/common.rs index d2790e609e..152653a797 100644 --- a/crates/ibc/src/context/common.rs +++ b/crates/ibc/src/context/common.rs @@ -426,7 +426,7 @@ pub trait IbcCommonContext: IbcStorageContext { channel_id: &ChannelId, ) -> Result { let key = storage::next_sequence_send_key(port_id, channel_id); - read_sequence(self, &key).map_err(ContextError::from) + read_sequence(self.storage(), &key).map_err(ContextError::from) } /// Store the NextSequenceSend @@ -447,7 +447,7 @@ pub trait IbcCommonContext: IbcStorageContext { channel_id: &ChannelId, ) -> Result { let key = storage::next_sequence_recv_key(port_id, channel_id); - read_sequence(self, &key).map_err(ContextError::from) + read_sequence(self.storage(), &key).map_err(ContextError::from) } /// Store the NextSequenceRecv @@ -468,7 +468,7 @@ pub trait IbcCommonContext: IbcStorageContext { channel_id: &ChannelId, ) -> Result { let key = storage::next_sequence_ack_key(port_id, channel_id); - read_sequence(self, &key).map_err(ContextError::from) + read_sequence(self.storage(), &key).map_err(ContextError::from) } /// Store the NextSequenceAck diff --git a/crates/ibc/src/context/validation.rs b/crates/ibc/src/context/validation.rs index a77db4d149..d5cf6d8068 100644 --- a/crates/ibc/src/context/validation.rs +++ b/crates/ibc/src/context/validation.rs @@ -267,12 +267,13 @@ where let height = self .inner .borrow() + .storage() .get_block_height() .expect("The height should exist"); let estimate = namada_parameters::estimate_max_block_time_from_blocks_and_params( - &*self.inner.borrow(), + self.inner.borrow().storage(), height, // NB: estimate max height with up to 5 blocks in the past, // which will not result in too many reads diff --git a/crates/ibc/src/vp/mod.rs b/crates/ibc/src/vp/mod.rs index 8c022591c0..dddd47b1d9 100644 --- a/crates/ibc/src/vp/mod.rs +++ b/crates/ibc/src/vp/mod.rs @@ -67,52 +67,46 @@ pub enum Error { pub type VpResult = std::result::Result; /// IBC VP -pub struct Ibc<'a, S, CA, EVAL, Params, Gov, Token, PoS> +pub struct Ibc<'ctx, S, CA, EVAL, Params, Gov, Token, PoS> where S: 'static + StateRead, - EVAL: VpEvaluator<'a, S, CA, EVAL>, + EVAL: VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA, EVAL>, - /// Parameters type - pub params: PhantomData, - /// Governance type - pub gov: PhantomData, - /// Token type - pub token: PhantomData, - /// PoS type - pub pos: PhantomData, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Generic types for DI + pub _marker: PhantomData<(Params, Gov, Token, PoS)>, } -impl<'a, S, CA, EVAL, Params, Gov, Token, PoS> NativeVp<'a> - for Ibc<'a, S, CA, EVAL, Params, Gov, Token, PoS> +impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov, Token, PoS> NativeVp<'view> + for Ibc<'ctx, S, CA, EVAL, Params, Gov, Token, PoS> where S: 'static + StateRead, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL> + Debug, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL> + Debug, CA: 'static + Clone + Debug, Gov: governance::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, Params: parameters::Keys + parameters::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, Token: token::Keys + token::Write< - PseudoExecutionStorage<'a, 'a, S, CA, EVAL>, + PseudoExecutionStorage<'view, 'ctx, S, CA, EVAL>, Err = StorageError, > + Debug, PoS: proof_of_stake::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, { type Error = Error; fn validate_tx( - &'a self, + &'view self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, @@ -150,40 +144,37 @@ where } } -impl<'a, S, CA, EVAL, Params, Gov, Token, PoS> - Ibc<'a, S, CA, EVAL, Params, Gov, Token, PoS> +impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov, Token, PoS> + Ibc<'ctx, S, CA, EVAL, Params, Gov, Token, PoS> where S: 'static + StateRead, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL> + Debug, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL> + Debug, CA: 'static + Clone + Debug, Params: parameters::Keys + parameters::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, Token: token::Keys + token::Write< - PseudoExecutionStorage<'a, 'a, S, CA, EVAL>, + PseudoExecutionStorage<'view, 'ctx, S, CA, EVAL>, Err = StorageError, > + Debug, PoS: proof_of_stake::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, { /// Instantiate IBC VP - pub fn new(ctx: Ctx<'a, S, CA, EVAL>) -> Self { + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { Self { ctx, - params: PhantomData, - gov: PhantomData, - token: PhantomData, - pos: PhantomData, + _marker: PhantomData, } } fn validate_state( - &'a self, + &'view self, tx_data: &[u8], keys_changed: &BTreeSet, ) -> VpResult<()> { @@ -244,7 +235,7 @@ where Ok(()) } - fn validate_with_msg(&'a self, tx_data: &[u8]) -> VpResult<()> { + fn validate_with_msg(&'view self, tx_data: &[u8]) -> VpResult<()> { let validation_ctx = VpValidationContext::new(self.ctx.pre()); let ctx = Rc::new(RefCell::new(validation_ctx)); // Use an empty verifiers set placeholder for validation, this is only @@ -266,7 +257,7 @@ where } /// Retrieve the validation params - pub fn validation_params(&'a self) -> VpResult { + pub fn validation_params(&'view self) -> VpResult { use std::str::FromStr; let chain_id = self.ctx.get_chain_id().map_err(Error::NativeVpError)?; let proof_specs = @@ -434,12 +425,9 @@ mod tests { use namada_core::storage::{BlockHeight, Epoch, TxIndex}; use namada_core::tendermint::time::Time as TmTime; use namada_core::time::DurationSecs; - use namada_core::WasmCacheRwAccess; use namada_gas::{TxGasMeter, VpGasMeter}; use namada_governance::parameters::GovernanceParameters; - use namada_parameters::storage::{ - get_epoch_duration_storage_key, get_max_expected_time_per_block_key, - }; + use namada_parameters::storage::get_epoch_duration_storage_key; use namada_parameters::EpochDuration; use namada_proof_of_stake::test_utils::get_dummy_genesis_validator; use namada_state::testing::TestState; @@ -447,9 +435,9 @@ mod tests { use namada_token::storage_key::balance_key; use namada_tx::data::TxType; use namada_tx::{Authorization, Code, Data, Section, Tx}; - use namada_vm::wasm; use namada_vm::wasm::run::VpEvalWasm; use namada_vm::wasm::VpCache; + use namada_vm::{wasm, WasmCacheRwAccess}; use prost::Message; use sha2::Digest; @@ -526,7 +514,7 @@ mod tests { self, ack_key, channel_counter_key, channel_key, client_connections_key, client_counter_key, client_state_key, client_update_height_key, client_update_timestamp_key, commitment_key, - connection_counter_key, connection_key, consensus_state_key, + connection_counter_key, connection_key, consensus_state_key, ibc_trace_key, mint_amount_key, next_sequence_ack_key, next_sequence_recv_key, next_sequence_send_key, nft_class_key, nft_metadata_key, receipt_key, @@ -543,23 +531,23 @@ mod tests { ::H, CA, >; - type Ctx<'a> = super::Ctx<'a, TestState, VpCache, Eval>; - type Ibc<'a> = super::Ibc< - 'a, + type Ctx<'ctx> = super::Ctx<'ctx, TestState, VpCache, Eval>; + type Ibc<'ctx> = super::Ibc< + 'ctx, TestState, VpCache, Eval, namada_parameters::Store< - CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + CtxPreStorageRead<'ctx, 'ctx, TestState, VpCache, Eval>, >, namada_governance::Store< - CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + CtxPreStorageRead<'ctx, 'ctx, TestState, VpCache, Eval>, >, namada_token::Store< - PseudoExecutionStorage<'a, 'a, TestState, VpCache, Eval>, + PseudoExecutionStorage<'ctx, 'ctx, TestState, VpCache, Eval>, >, namada_proof_of_stake::Store< - CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + CtxPreStorageRead<'ctx, 'ctx, TestState, VpCache, Eval>, >, >; @@ -3043,13 +3031,7 @@ mod tests { let class_id = get_nft_class_id(); let token_id = get_nft_id(); let sender = established_address_1(); -<<<<<<< HEAD - let ibc_token = ibc::trace::ibc_token_for_nft(&class_id, &token_id); -||||||| parent of 9e9b323a5 (ibc: fix VP post move) - let ibc_token = ibc::storage::ibc_token_for_nft(&class_id, &token_id); -======= - let ibc_token = storage::ibc_token_for_nft(&class_id, &token_id); ->>>>>>> 9e9b323a5 (ibc: fix VP post move) + let ibc_token = crate::trace::ibc_token_for_nft(&class_id, &token_id); let balance_key = balance_key(&ibc_token, &sender); let amount = Amount::from_u64(1); state diff --git a/crates/node/Cargo.toml b/crates/node/Cargo.toml index d950683f03..ec0d6ffbac 100644 --- a/crates/node/Cargo.toml +++ b/crates/node/Cargo.toml @@ -44,8 +44,12 @@ namada-eth-bridge = [ [dependencies] namada_apps_lib = {path = "../apps_lib"} namada_migrations = {path = "../migrations", optional = true} -namada_sdk = {path = "../sdk", default-features = false, features = ["download-params", "std", "rand"]} +namada_replay_protection = {path = "../replay_protection"} +namada_sdk = {path = "../sdk", features = ["wasm-runtime"]} namada_test_utils = {path = "../test_utils", optional = true} +namada_vm = {path = "../vm"} +namada_vote_ext = { path = "../vote_ext" } +namada_vp = {path = "../vp"} arse-merkle-tree = { workspace = true, features = ["blake2b"] } async-trait.workspace = true @@ -98,6 +102,7 @@ warp = "0.3.2" [dev-dependencies] namada_apps_lib = {path = "../apps_lib", features = ["testing"]} namada_test_utils = {path = "../test_utils"} +namada_vm = {path = "../vm", features = ["testing"]} assert_matches.workspace = true clap.workspace = true diff --git a/crates/node/src/abortable.rs b/crates/node/src/abortable.rs index 8c8a516582..c078d5d26d 100644 --- a/crates/node/src/abortable.rs +++ b/crates/node/src/abortable.rs @@ -1,7 +1,7 @@ use std::future::Future; use std::pin::Pin; -use namada::control_flow::{install_shutdown_signal, ShutdownSignal}; +use namada_sdk::control_flow::{install_shutdown_signal, ShutdownSignal}; use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; use tokio::task::JoinHandle; diff --git a/crates/node/src/bench_utils.rs b/crates/node/src/bench_utils.rs index 6959efdeb4..d58839de4f 100644 --- a/crates/node/src/bench_utils.rs +++ b/crates/node/src/bench_utils.rs @@ -17,80 +17,77 @@ use borsh_ext::BorshSerializeExt; use masp_primitives::transaction::Transaction; use masp_primitives::zip32::ExtendedFullViewingKey; use masp_proofs::prover::LocalTxProver; -use namada::address::MASP; -use namada::core::address::{self, Address, InternalAddress}; -use namada::core::chain::ChainId; -use namada::core::key::common::SecretKey; -use namada::core::masp::{ - ExtendedViewingKey, PaymentAddress, TransferSource, TransferTarget, +use namada_apps_lib::cli; +use namada_apps_lib::cli::context::FromContext; +use namada_apps_lib::cli::Context; +use namada_apps_lib::wallet::{defaults, CliWalletUtils}; +use namada_sdk::address::{self, Address, InternalAddress, MASP}; +use namada_sdk::chain::ChainId; +use namada_sdk::events::extend::{ + ComposeEvent, MaspTxBatchRefs, MaspTxBlockIndex, }; -use namada::core::storage::{BlockHeight, Epoch, Key, KeySeg, TxIndex}; -use namada::core::time::DateTimeUtc; -use namada::events::extend::{ComposeEvent, MaspTxBatchRefs, MaspTxBlockIndex}; -use namada::events::Event; -use namada::governance::storage::proposal::ProposalType; -use namada::governance::InitProposalData; -use namada::ibc::apps::transfer::types::msgs::transfer::MsgTransfer as IbcMsgTransfer; -use namada::ibc::apps::transfer::types::packet::PacketData; -use namada::ibc::apps::transfer::types::PrefixedCoin; -use namada::ibc::clients::tendermint::client_state::ClientState; -use namada::ibc::clients::tendermint::consensus_state::ConsensusState; -use namada::ibc::clients::tendermint::types::{ +use namada_sdk::events::Event; +use namada_sdk::gas::TxGasMeter; +use namada_sdk::governance::storage::proposal::ProposalType; +use namada_sdk::governance::InitProposalData; +use namada_sdk::ibc::apps::transfer::types::msgs::transfer::MsgTransfer as IbcMsgTransfer; +use namada_sdk::ibc::apps::transfer::types::packet::PacketData; +use namada_sdk::ibc::apps::transfer::types::PrefixedCoin; +use namada_sdk::ibc::clients::tendermint::client_state::ClientState; +use namada_sdk::ibc::clients::tendermint::consensus_state::ConsensusState; +use namada_sdk::ibc::clients::tendermint::types::{ AllowUpdate, ClientState as ClientStateType, ConsensusState as ConsensusStateType, TrustThreshold, }; -use namada::ibc::core::channel::types::channel::{ +use namada_sdk::ibc::core::channel::types::channel::{ ChannelEnd, Counterparty as ChannelCounterparty, Order, State, }; -use namada::ibc::core::channel::types::timeout::TimeoutHeight; -use namada::ibc::core::channel::types::Version as ChannelVersion; -use namada::ibc::core::client::types::Height as IbcHeight; -use namada::ibc::core::commitment_types::commitment::{ +use namada_sdk::ibc::core::channel::types::timeout::TimeoutHeight; +use namada_sdk::ibc::core::channel::types::Version as ChannelVersion; +use namada_sdk::ibc::core::client::types::Height as IbcHeight; +use namada_sdk::ibc::core::commitment_types::commitment::{ CommitmentPrefix, CommitmentRoot, }; -use namada::ibc::core::commitment_types::specs::ProofSpecs; -use namada::ibc::core::connection::types::version::Version; -use namada::ibc::core::connection::types::{ +use namada_sdk::ibc::core::commitment_types::specs::ProofSpecs; +use namada_sdk::ibc::core::connection::types::version::Version; +use namada_sdk::ibc::core::connection::types::{ ConnectionEnd, Counterparty, State as ConnectionState, }; -use namada::ibc::core::host::types::identifiers::{ +use namada_sdk::ibc::core::host::types::identifiers::{ ChainId as IbcChainId, ChannelId as NamadaChannelId, ChannelId, ClientId, ConnectionId, ConnectionId as NamadaConnectionId, PortId as NamadaPortId, PortId, }; -use namada::ibc::core::host::types::path::{ +use namada_sdk::ibc::core::host::types::path::{ ClientConsensusStatePath, ClientStatePath, Path as IbcPath, }; -use namada::ibc::primitives::proto::{Any, Protobuf}; -use namada::ibc::primitives::Timestamp as IbcTimestamp; -use namada::ibc::storage::{mint_limit_key, port_key, throughput_limit_key}; -use namada::ibc::MsgTransfer; -use namada::io::StdIo; -use namada::ledger::dry_run_tx; -use namada::ledger::gas::TxGasMeter; -use namada::ledger::ibc::storage::{channel_key, connection_key}; -use namada::ledger::native_vp::ibc::get_dummy_header; -use namada::ledger::queries::{ +use namada_sdk::ibc::primitives::proto::{Any, Protobuf}; +use namada_sdk::ibc::primitives::Timestamp as IbcTimestamp; +use namada_sdk::ibc::storage::{ + channel_key, connection_key, mint_limit_key, port_key, throughput_limit_key, +}; +use namada_sdk::ibc::MsgTransfer; +use namada_sdk::io::StdIo; +use namada_sdk::key::common::SecretKey; +use namada_sdk::masp::{ + self, ContextSyncStatus, ExtendedViewingKey, MaspTransferData, MaspTxRefs, + PaymentAddress, ShieldedContext, ShieldedUtils, TransferSource, + TransferTarget, +}; +use namada_sdk::queries::{ Client, EncodedResponseQuery, RequestCtx, RequestQuery, Router, RPC, }; -use namada::masp::MaspTxRefs; -use namada::state::StorageRead; -use namada::token::{Amount, DenominatedAmount, Transfer}; -use namada::tx::data::pos::Bond; -use namada::tx::data::{BatchedTxResult, Fee, TxResult, VpsResult}; -use namada::tx::event::{new_tx_event, Batch}; -use namada::tx::{ +use namada_sdk::state::StorageRead; +use namada_sdk::storage::testing::get_dummy_header; +use namada_sdk::storage::{BlockHeight, Epoch, Key, KeySeg, TxIndex}; +use namada_sdk::time::DateTimeUtc; +use namada_sdk::token::{Amount, DenominatedAmount, Transfer}; +use namada_sdk::tx::data::pos::Bond; +use namada_sdk::tx::data::{BatchedTxResult, Fee, TxResult, VpsResult}; +use namada_sdk::tx::event::{new_tx_event, Batch}; +use namada_sdk::tx::{ Authorization, BatchedTx, BatchedTxRef, Code, Data, Section, Tx, }; -use namada::vm::wasm::run; -use namada::{proof_of_stake, tendermint}; -use namada_apps_lib::cli; -use namada_apps_lib::cli::context::FromContext; -use namada_apps_lib::cli::Context; -use namada_apps_lib::wallet::{defaults, CliWalletUtils}; -use namada_sdk::masp::{ - self, ContextSyncStatus, MaspTransferData, ShieldedContext, ShieldedUtils, -}; pub use namada_sdk::tx::{ TX_BECOME_VALIDATOR_WASM, TX_BOND_WASM, TX_BRIDGE_POOL_WASM, TX_CHANGE_COMMISSION_WASM as TX_CHANGE_VALIDATOR_COMMISSION_WASM, @@ -105,18 +102,19 @@ pub use namada_sdk::tx::{ TX_WITHDRAW_WASM, VP_USER_WASM, }; use namada_sdk::wallet::Wallet; -use namada_sdk::{Namada, NamadaImpl}; +use namada_sdk::{parameters, proof_of_stake, tendermint, Namada, NamadaImpl}; use namada_test_utils::tx_data::TxWriteData; +use namada_vm::wasm::run; use rand_core::OsRng; use tempfile::TempDir; -use crate::config; use crate::config::global::GlobalConfig; use crate::config::TendermintMode; use crate::facade::tendermint::v0_37::abci::request::InitChain; use crate::facade::tendermint_proto::google::protobuf::Timestamp; use crate::facade::tendermint_rpc; use crate::shell::Shell; +use crate::{config, dry_run_tx}; pub const WASM_DIR: &str = "../../wasm"; @@ -299,7 +297,7 @@ impl BenchShell { extra_sections: Option>, signers: Vec<&SecretKey>, ) -> BatchedTx { - let mut tx = Tx::from_type(namada::tx::data::TxType::Raw); + let mut tx = Tx::from_type(namada_sdk::tx::data::TxType::Raw); // NOTE: here we use the code hash to avoid including the cost for the // wasm validation. The wasm codes (both txs and vps) are always @@ -344,7 +342,7 @@ impl BenchShell { data: Vec, ) -> BatchedTx { // This function avoid serializaing the tx data with Borsh - let mut tx = Tx::from_type(namada::tx::data::TxType::Raw); + let mut tx = Tx::from_type(namada_sdk::tx::data::TxType::Raw); let code_hash = self .read_storage_key(&Key::wasm_hash(wasm_code_path)) .unwrap(); @@ -375,7 +373,7 @@ impl BenchShell { let timeout_height = TimeoutHeight::At(IbcHeight::new(0, 100).unwrap()); #[allow(clippy::disallowed_methods)] - let now: namada::tendermint::Time = + let now: namada_sdk::tendermint::Time = DateTimeUtc::now().try_into().unwrap(); let now: IbcTimestamp = now.into(); let timeout_timestamp = @@ -437,8 +435,15 @@ impl BenchShell { ) .unwrap(); - if self.state.is_masp_new_epoch(true).unwrap() { - namada::token::conversion::update_allowed_conversions( + let masp_epoch_multiplier = + parameters::read_masp_epoch_multiplier_parameter(&self.state) + .unwrap(); + if self + .state + .is_masp_new_epoch(true, masp_epoch_multiplier) + .unwrap() + { + namada_sdk::token::conversion::update_allowed_conversions( &mut self.state, ) .unwrap(); @@ -481,7 +486,7 @@ impl BenchShell { // Set consensus state #[allow(clippy::disallowed_methods)] - let now: namada::tendermint::Time = + let now: namada_sdk::tendermint::Time = DateTimeUtc::now().try_into().unwrap(); let consensus_key = addr_key.join(&Key::from( IbcPath::ClientConsensusState(ClientConsensusStatePath { @@ -607,7 +612,7 @@ impl BenchShell { // Commit a masp transaction and cache the tx and the changed keys for // client queries pub fn commit_masp_tx(&mut self, mut masp_tx: Tx) { - use namada::core::key::RefTo; + use namada_sdk::key::RefTo; masp_tx.add_wrapper( Fee { amount_per_gas_unit: DenominatedAmount::native(0.into()), @@ -626,7 +631,7 @@ pub fn generate_foreign_key_tx(signer: &SecretKey) -> BatchedTx { let wasm_code = std::fs::read("../../wasm_for_tests/tx_write.wasm").unwrap(); - let mut tx = Tx::from_type(namada::tx::data::TxType::Raw); + let mut tx = Tx::from_type(namada_sdk::tx::data::TxType::Raw); tx.set_code(Code::new(wasm_code, None)); tx.set_data(Data::new( TxWriteData { @@ -790,17 +795,24 @@ impl Client for BenchShell { prove, }; - let ctx = RequestCtx { - state: &self.state, - event_log: self.event_log(), - vp_wasm_cache: self.vp_wasm_cache.read_only(), - tx_wasm_cache: self.tx_wasm_cache.read_only(), - storage_read_past_height_limit: None, - }; - if request.path == RPC.shell().dry_run_tx_path() { - dry_run_tx(ctx, &request) + dry_run_tx( + // This is safe because nothing else is using `self.state` + // concurrently and the `TempWlState` will be dropped right + // after dry-run. + unsafe { self.state.read_only().with_static_temp_write_log() }, + self.vp_wasm_cache.read_only(), + self.tx_wasm_cache.read_only(), + &request, + ) } else { + let ctx = RequestCtx { + state: &self.state, + event_log: self.event_log(), + vp_wasm_cache: self.vp_wasm_cache.read_only(), + tx_wasm_cache: self.tx_wasm_cache.read_only(), + storage_read_past_height_limit: None, + }; RPC.handle(ctx, &request) } .map_err(|_| std::io::Error::from(std::io::ErrorKind::NotFound)) @@ -896,7 +908,7 @@ impl Client for BenchShell { tendermint_rpc::Error, > where - H: TryInto + Send, + H: TryInto + Send, { // NOTE: atm this is only needed to query block results at a specific // height for masp transactions @@ -950,7 +962,7 @@ impl Client for BenchShell { .unwrap(), ]))) .into(); - namada::tendermint::abci::Event::from(event) + namada_sdk::tendermint::abci::Event::from(event) }) .collect(), ) @@ -966,7 +978,7 @@ impl Client for BenchShell { end_block_events, validator_updates: vec![], consensus_param_updates: None, - app_hash: namada::tendermint::hash::AppHash::default(), + app_hash: namada_sdk::tendermint::hash::AppHash::default(), }) } } @@ -1193,7 +1205,7 @@ impl BenchShieldedCtx { let timeout_height = TimeoutHeight::At(IbcHeight::new(0, 100).unwrap()); #[allow(clippy::disallowed_methods)] - let now: namada::tendermint::Time = + let now: namada_sdk::tendermint::Time = DateTimeUtc::now().try_into().unwrap(); let now: IbcTimestamp = now.into(); let timeout_timestamp = diff --git a/crates/node/src/broadcaster.rs b/crates/node/src/broadcaster.rs index 8a081f1937..6f0fc652c5 100644 --- a/crates/node/src/broadcaster.rs +++ b/crates/node/src/broadcaster.rs @@ -1,8 +1,8 @@ use std::net::SocketAddr; use std::ops::ControlFlow; -use namada::control_flow::time; -use namada::time::{DateTimeUtc, Utc}; +use namada_sdk::control_flow::time; +use namada_sdk::time::{DateTimeUtc, Utc}; use tokio::sync::mpsc::UnboundedReceiver; use crate::facade::tendermint_rpc::client::CompatMode; diff --git a/crates/node/src/dry_run_tx.rs b/crates/node/src/dry_run_tx.rs index 0f75b0accf..20523d7b48 100644 --- a/crates/node/src/dry_run_tx.rs +++ b/crates/node/src/dry_run_tx.rs @@ -2,38 +2,39 @@ use std::cell::RefCell; -use namada_gas::Gas; -use namada_sdk::queries::{EncodedResponseQuery, RequestCtx, RequestQuery}; -use namada_state::{DBIter, ResultExt, StorageHasher, DB}; -use namada_tx::data::{DryRunResult, ExtendedTxResult, GasLimit, TxResult}; +use namada_sdk::borsh::BorshSerializeExt; +use namada_sdk::gas::{GasMetering, TxGasMeter}; +use namada_sdk::parameters; +use namada_sdk::queries::{EncodedResponseQuery, RequestQuery}; +use namada_sdk::state::{ + DBIter, ResultExt, StorageHasher, StorageResult, TxIndex, DB, +}; +use namada_sdk::tx::data::{ + DryRunResult, ExtendedTxResult, GasLimit, TxResult, TxType, +}; +use namada_sdk::tx::Tx; +use namada_vm::wasm::{TxCache, VpCache}; +use namada_vm::WasmCacheAccess; -use super::protocol; -use crate::vm::wasm::{TxCache, VpCache}; -use crate::vm::WasmCacheAccess; +use crate::protocol; +use crate::protocol::ShellParams; /// Dry run a transaction -pub fn dry_run_tx<'a, D, H, CA>( - mut ctx: RequestCtx<'a, D, H, VpCache, TxCache>, +pub fn dry_run_tx( + mut state: namada_sdk::state::TempWlState<'static, D, H>, + mut vp_wasm_cache: VpCache, + mut tx_wasm_cache: TxCache, request: &RequestQuery, -) -> namada_state::StorageResult +) -> StorageResult where D: 'static + DB + for<'iter> DBIter<'iter> + Sync, H: 'static + StorageHasher + Sync, CA: 'static + WasmCacheAccess + Sync, { - use borsh_ext::BorshSerializeExt; - use namada_gas::{GasMetering, TxGasMeter}; - use namada_tx::data::TxType; - use namada_tx::Tx; - - use crate::ledger::protocol::ShellParams; - use crate::storage::TxIndex; - - let mut temp_state = ctx.state.with_temp_write_log(); let tx = Tx::try_from(&request.data[..]).into_storage_result()?; tx.validate_tx().into_storage_result()?; - let gas_scale = namada_parameters::get_gas_scale(ctx.state)?; + let gas_scale = parameters::get_gas_scale(&state)?; // Wrapper dry run to allow estimating the gas cost of a transaction let (wrapper_hash, extended_tx_result, tx_gas_meter) = @@ -44,17 +45,24 @@ where .as_scaled_gas(gas_scale) .into_storage_result()?; let tx_gas_meter = RefCell::new(TxGasMeter::new(gas_limit)); + let mut shell_params = ShellParams::new( + &tx_gas_meter, + &mut state, + &mut vp_wasm_cache, + &mut tx_wasm_cache, + ); let tx_result = protocol::apply_wrapper_tx( &tx, &wrapper, &request.data, + &TxIndex::default(), &tx_gas_meter, - &mut temp_state, + &mut shell_params, None, ) .into_storage_result()?; - temp_state.write_log_mut().commit_tx_to_batch(); + state.write_log_mut().commit_tx_to_batch(); let available_gas = tx_gas_meter.borrow().get_available_gas(); ( Some(tx.header_hash()), @@ -65,8 +73,7 @@ where _ => { // If dry run only the inner tx, use the max block gas as // the gas limit - let max_block_gas = - namada_parameters::get_max_block_gas(ctx.state)?; + let max_block_gas = parameters::get_max_block_gas(&state)?; let gas_limit = GasLimit::from(max_block_gas) .as_scaled_gas(gas_scale) .into_storage_result()?; @@ -81,27 +88,29 @@ where let ExtendedTxResult { mut tx_result, ref masp_tx_refs, - ref ibc_tx_data_refs, + ibc_tx_data_refs, } = extended_tx_result; let tx_gas_meter = RefCell::new(tx_gas_meter); - for cmt in tx.commitments() { + for cmt in + protocol::get_batch_txs_to_execute(&tx, masp_tx_refs, &ibc_tx_data_refs) + { let batched_tx = tx.batch_ref_tx(cmt); let batched_tx_result = protocol::apply_wasm_tx( - batched_tx, + &batched_tx, &TxIndex(0), ShellParams::new( &tx_gas_meter, - &mut temp_state, - &mut ctx.vp_wasm_cache, - &mut ctx.tx_wasm_cache, + &mut state, + &mut vp_wasm_cache, + &mut tx_wasm_cache, ), ); let is_accepted = matches!(&batched_tx_result, Ok(result) if result.is_accepted()); if is_accepted { - temp_state.write_log_mut().commit_tx_to_batch(); + state.write_log_mut().commit_tx_to_batch(); } else { - temp_state.write_log_mut().drop_tx(); + state.write_log_mut().drop_tx(); } tx_result.insert_inner_tx_result( wrapper_hash.as_ref(), @@ -110,7 +119,7 @@ where ); } // Account gas for both batch and wrapper - tx_result.gas_used = tx_gas_meter + let gas_used = tx_gas_meter .borrow() .get_tx_consumed_gas() .get_whole_gas_units(gas_scale); @@ -121,7 +130,7 @@ where data: dry_run_result.serialize_to_vec(), proof: None, info: Default::default(), - height: ctx.state.in_mem().get_last_block_height(), + height: state.in_mem().get_last_block_height(), }) } @@ -129,25 +138,24 @@ where mod test { use borsh::BorshDeserialize; use borsh_ext::BorshSerializeExt; - use namada_core::address; - use namada_core::hash::Hash; - use namada_core::storage::{BlockHeight, Key}; + use namada_sdk::events::log::EventLog; + use namada_sdk::hash::Hash; use namada_sdk::queries::{ - EncodedResponseQuery, RequestCtx, RequestQuery, Router, RPC, + Client, EncodedResponseQuery, RequestCtx, RequestQuery, Router, RPC, }; + use namada_sdk::state::testing::TestState; + use namada_sdk::state::StorageWrite; + use namada_sdk::storage::{BlockHeight, Key}; use namada_sdk::tendermint_rpc::{Error as RpcError, Response}; - use namada_state::testing::TestState; - use namada_state::StorageWrite; + use namada_sdk::tx::data::TxType; + use namada_sdk::tx::{Code, Data, Tx}; + use namada_sdk::{address, token}; use namada_test_utils::TestWasms; - use namada_tx::data::TxType; - use namada_tx::{Code, Data, Tx}; + use namada_vm::wasm::{TxCache, VpCache}; + use namada_vm::{wasm, WasmCacheRoAccess}; use tempfile::TempDir; - use crate::ledger::events::log::EventLog; - use crate::ledger::queries::Client; - use crate::token; - use crate::vm::wasm::{TxCache, VpCache}; - use crate::vm::{wasm, WasmCacheRoAccess}; + use super::*; /// A test client that has direct access to the storage pub struct TestClient @@ -184,14 +192,14 @@ mod test { // Initialize mock gas limit let max_block_gas_key = - namada_parameters::storage::get_max_block_gas_key(); + namada_sdk::parameters::storage::get_max_block_gas_key(); state .db_write(&max_block_gas_key, 20_000_000_u64.serialize_to_vec()) .expect( "Max block gas parameter must be initialized in storage", ); // Initialize mock gas scale - let gas_scale_key = namada_parameters::storage::get_gas_scale_key(); + let gas_scale_key = parameters::storage::get_gas_scale_key(); state .db_write(&gas_scale_key, 100_000_000_u64.serialize_to_vec()) .expect("Gas scale parameter must be initialized in storage"); @@ -213,8 +221,7 @@ mod test { } } - #[cfg_attr(feature = "async-send", async_trait::async_trait)] - #[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] + #[async_trait::async_trait(?Send)] impl Client for TestClient where RPC: Router + Sync, @@ -238,19 +245,29 @@ mod test { height: height.try_into().unwrap(), prove, }; - let ctx = RequestCtx { - state: &self.state, - event_log: &self.event_log, - vp_wasm_cache: self.vp_wasm_cache.clone(), - tx_wasm_cache: self.tx_wasm_cache.clone(), - storage_read_past_height_limit: None, - }; // TODO(namada#3240): this is a hack to propagate errors to the // caller, we should really permit error types other // than [`std::io::Error`] if request.path == RPC.shell().dry_run_tx_path() { - super::dry_run_tx(ctx, &request) + dry_run_tx( + // This is safe because nothing else is using `self.state` + // concurrently and the `TempWlState` will be dropped right + // after dry-run. + unsafe { + self.state.read_only().with_static_temp_write_log() + }, + self.vp_wasm_cache.clone(), + self.tx_wasm_cache.clone(), + &request, + ) } else { + let ctx = RequestCtx { + state: self.state.read_only(), + event_log: &self.event_log, + vp_wasm_cache: self.vp_wasm_cache.clone(), + tx_wasm_cache: self.tx_wasm_cache.clone(), + storage_read_past_height_limit: None, + }; self.rpc.handle(ctx, &request) } .map_err(|err| { @@ -267,8 +284,7 @@ mod test { } #[tokio::test] - async fn test_shell_queries_router_with_client() - -> namada_state::StorageResult<()> { + async fn test_shell_queries_router_with_client() -> StorageResult<()> { // Initialize the `TestClient` let mut client = TestClient::new(RPC); // store the wasm code diff --git a/crates/node/src/ethereum_oracle/control.rs b/crates/node/src/ethereum_oracle/control.rs index ca9d3e6dc4..62a59692f5 100644 --- a/crates/node/src/ethereum_oracle/control.rs +++ b/crates/node/src/ethereum_oracle/control.rs @@ -1,6 +1,6 @@ //! The oracle is controlled by sending commands over a channel. -use namada::eth_bridge::oracle::config::Config; +use namada_sdk::eth_bridge::oracle::config::Config; use tokio::sync::mpsc; use tokio::sync::mpsc::error::TrySendError; diff --git a/crates/node/src/ethereum_oracle/events.rs b/crates/node/src/ethereum_oracle/events.rs index 64e94a1757..a081c106e2 100644 --- a/crates/node/src/ethereum_oracle/events.rs +++ b/crates/node/src/ethereum_oracle/events.rs @@ -8,14 +8,14 @@ pub mod eth_events { ValidatorSetUpdateFilter, }; use ethbridge_events::{DynEventCodec, Events as RawEvents}; - use namada::core::address::Address; - use namada::core::ethereum_events::{ + use namada_sdk::address::Address; + use namada_sdk::ethereum_events::{ EthAddress, EthereumEvent, TransferToEthereum, TransferToNamada, Uint, }; - use namada::core::ethereum_structs; - use namada::core::hash::Hash; - use namada::core::keccak::KeccakHash; - use namada::core::token::Amount; + use namada_sdk::ethereum_structs; + use namada_sdk::hash::Hash; + use namada_sdk::keccak::KeccakHash; + use namada_sdk::token::Amount; use num256::Uint256; use thiserror::Error; @@ -181,7 +181,7 @@ pub mod eth_events { impl Parse for ethabi::Uint { fn parse_amount(self) -> Result { let uint = { - use namada::core::uint::Uint as NamadaUint; + use namada_sdk::uint::Uint as NamadaUint; let mut num_buf = [0; 32]; self.to_little_endian(&mut num_buf); NamadaUint::from_little_endian(&num_buf) @@ -303,7 +303,7 @@ pub mod eth_events { TRANSFER_TO_CHAIN_CODEC, TRANSFER_TO_ERC_CODEC, VALIDATOR_SET_UPDATE_CODEC, }; - use namada::eth_bridge::ethers::contract::EthEvent; + use namada_sdk::eth_bridge::ethers::contract::EthEvent; use super::*; use crate::ethereum_oracle::test_tools::event_log::GetLog; diff --git a/crates/node/src/ethereum_oracle/mod.rs b/crates/node/src/ethereum_oracle/mod.rs index ed454eb002..5e6564a4ff 100644 --- a/crates/node/src/ethereum_oracle/mod.rs +++ b/crates/node/src/ethereum_oracle/mod.rs @@ -8,13 +8,12 @@ use async_trait::async_trait; use ethabi::Address; use ethbridge_events::{event_codecs, EventKind}; use itertools::Either; -use namada::control_flow::time::{Constant, Duration, Instant, Sleep}; -use namada::core::ethereum_events::EthereumEvent; -use namada::core::{ethereum_structs, hints}; -use namada::eth_bridge::ethers; -use namada::eth_bridge::ethers::providers::{Http, Middleware, Provider}; -use namada::eth_bridge::oracle::config::Config; -use namada_sdk::eth_bridge::{eth_syncing_status_timeout, SyncStatus}; +use namada_sdk::control_flow::time::{Constant, Duration, Instant, Sleep}; +use namada_sdk::eth_bridge::ethers::providers::{Http, Middleware, Provider}; +use namada_sdk::eth_bridge::oracle::config::Config; +use namada_sdk::eth_bridge::{eth_syncing_status_timeout, ethers, SyncStatus}; +use namada_sdk::ethereum_events::EthereumEvent; +use namada_sdk::{ethereum_structs, hints}; use num256::Uint256; use thiserror::Error; use tokio::sync::mpsc::error::TryRecvError; @@ -602,7 +601,7 @@ fn process_queue( pub mod last_processed_block { //! Functionality to do with publishing which blocks we have processed. - use namada::core::ethereum_structs; + use namada_sdk::ethereum_structs; use tokio::sync::watch; pub type Sender = watch::Sender>; @@ -621,11 +620,11 @@ mod test_oracle { use std::num::NonZeroU64; use ethbridge_bridge_events::{TransferToChainFilter, TransferToErcFilter}; - use namada::core::address::testing::gen_established_address; - use namada::core::ethereum_events::{EthAddress, TransferToEthereum}; - use namada::core::hash::Hash; - use namada::eth_bridge::ethers::types::H160; - use namada::eth_bridge::structs::Erc20Transfer; + use namada_sdk::address::testing::gen_established_address; + use namada_sdk::eth_bridge::ethers::types::H160; + use namada_sdk::eth_bridge::structs::Erc20Transfer; + use namada_sdk::ethereum_events::{EthAddress, TransferToEthereum}; + use namada_sdk::hash::Hash; use tokio::sync::oneshot::channel; use tokio::time::timeout; diff --git a/crates/node/src/ethereum_oracle/test_tools/events_endpoint.rs b/crates/node/src/ethereum_oracle/test_tools/events_endpoint.rs index 23f77d9301..5b6e932581 100644 --- a/crates/node/src/ethereum_oracle/test_tools/events_endpoint.rs +++ b/crates/node/src/ethereum_oracle/test_tools/events_endpoint.rs @@ -1,7 +1,7 @@ use std::net::SocketAddr; use borsh::BorshDeserialize; -use namada::core::ethereum_events::EthereumEvent; +use namada_sdk::ethereum_events::EthereumEvent; use tokio::sync::mpsc::Sender as BoundedSender; use tokio::sync::oneshot::{Receiver, Sender}; use warp::reply::WithStatus; diff --git a/crates/node/src/ethereum_oracle/test_tools/mod.rs b/crates/node/src/ethereum_oracle/test_tools/mod.rs index 46425c4362..9967897294 100644 --- a/crates/node/src/ethereum_oracle/test_tools/mod.rs +++ b/crates/node/src/ethereum_oracle/test_tools/mod.rs @@ -8,8 +8,8 @@ pub mod event_log { use ethbridge_bridge_events::{ TransferToChainFilter, TransferToErcFilter, ValidatorSetUpdateFilter, }; - use namada::eth_bridge::ethers::abi::AbiEncode; - use namada::eth_bridge::ethers::contract::EthEvent; + use namada_sdk::eth_bridge::ethers::abi::AbiEncode; + use namada_sdk::eth_bridge::ethers::contract::EthEvent; /// Get an [`ethabi::RawLog`] from a given Ethereum event. pub trait GetLog { @@ -67,8 +67,8 @@ pub mod mock_web3_client { use async_trait::async_trait; use ethabi::Address; use ethbridge_events::EventCodec; - use namada::control_flow::time::{Duration, Instant}; - use namada::core::ethereum_structs::BlockHeight; + use namada_sdk::control_flow::time::{Duration, Instant}; + use namada_sdk::ethereum_structs::BlockHeight; use num256::Uint256; use tokio::sync::mpsc::{ unbounded_channel, UnboundedReceiver, UnboundedSender, diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index a8bbd9f6c2..a7c0e73b0a 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -19,6 +19,7 @@ pub mod bench_utils; mod broadcaster; mod dry_run_tx; pub mod ethereum_oracle; +pub mod protocol; pub mod shell; pub mod shims; pub mod storage; @@ -34,20 +35,18 @@ use byte_unit::Byte; use data_encoding::HEXUPPER; pub use dry_run_tx::dry_run_tx; use futures::future::TryFutureExt; -use namada::core::storage::BlockHeight; -use namada::core::time::DateTimeUtc; -use namada::eth_bridge::ethers::providers::{Http, Provider}; -use namada::state::{ProcessProposalCachedResult, DB}; -use namada::storage::DbColFam; -use namada::tendermint::abci::request::CheckTxKind; -use namada::tendermint::abci::response::ProcessProposal; use namada_apps_lib::cli::args; use namada_apps_lib::config::utils::{ convert_tm_addr_to_socket_addr, num_of_threads, }; use namada_apps_lib::{config, wasm_loader}; +use namada_sdk::eth_bridge::ethers::providers::{Http, Provider}; use namada_sdk::migrations::ScheduledMigration; -use namada_sdk::state::StateRead; +use namada_sdk::state::{ProcessProposalCachedResult, StateRead, DB}; +use namada_sdk::storage::{BlockHeight, DbColFam}; +use namada_sdk::tendermint::abci::request::CheckTxKind; +use namada_sdk::tendermint::abci::response::ProcessProposal; +use namada_sdk::time::DateTimeUtc; use once_cell::unsync::Lazy; use sysinfo::{RefreshKind, System, SystemExt}; use tokio::sync::mpsc; @@ -354,7 +353,7 @@ pub fn dump_db( #[cfg(feature = "migrations")] pub fn query_db( config: config::Ledger, - key: &namada::core::storage::Key, + key: &namada_sdk::storage::Key, type_hash: &[u8; 32], cf: &DbColFam, ) { @@ -461,7 +460,7 @@ async fn run_aux( }; tracing::info!("Loading MASP verifying keys."); - let _ = namada::token::validation::preload_verifying_keys(); + let _ = namada_sdk::token::validation::preload_verifying_keys(); tracing::info!("Done loading MASP verifying keys."); // Start ABCI server and broadcaster (the latter only if we are a validator @@ -960,8 +959,8 @@ pub fn test_genesis_files( genesis: config::genesis::chain::Finalized, wasm_dir: PathBuf, ) { - use namada::core::hash::Sha256Hasher; - use namada::state::mockdb::MockDB; + use namada_sdk::hash::Sha256Hasher; + use namada_sdk::state::mockdb::MockDB; // Channels for validators to send protocol txs to be broadcast to the // broadcaster service diff --git a/crates/node/src/protocol.rs b/crates/node/src/protocol.rs index c84fa4e2d0..5014967b69 100644 --- a/crates/node/src/protocol.rs +++ b/crates/node/src/protocol.rs @@ -5,51 +5,47 @@ use std::fmt::Debug; use either::Either; use eyre::{eyre, WrapErr}; -use namada_core::booleans::BoolResultUnitExt; -use namada_core::hash::Hash; -use namada_core::ibc::{IbcTxDataHash, IbcTxDataRefs}; -use namada_core::masp::{MaspTxRefs, TxId}; -use namada_events::extend::{ +use namada_sdk::address::{Address, InternalAddress}; +use namada_sdk::booleans::BoolResultUnitExt; +use namada_sdk::events::extend::{ ComposeEvent, Height as HeightAttr, TxHash as TxHashAttr, UserAccount, }; -use namada_events::EventLevel; -use namada_gas::TxGasMeter; -use namada_parameters::get_gas_scale; -use namada_state::TxWrites; -use namada_token::event::{TokenEvent, TokenOperation}; -use namada_token::utils::is_masp_transfer; -use namada_tx::action::{is_ibc_shielding_transfer, Read}; -use namada_tx::data::protocol::{ProtocolTx, ProtocolTxType}; -use namada_tx::data::{ +use namada_sdk::events::EventLevel; +use namada_sdk::gas::{Gas, GasMetering, TxGasMeter, VpGasMeter}; +use namada_sdk::hash::Hash; +use namada_sdk::ibc::{IbcTxDataHash, IbcTxDataRefs}; +use namada_sdk::masp::{MaspTxRefs, TxId}; +use namada_sdk::parameters::get_gas_scale; +use namada_sdk::state::{ + DBIter, State, StorageHasher, StorageRead, TxWrites, WlState, DB, +}; +use namada_sdk::storage::TxIndex; +use namada_sdk::token::event::{TokenEvent, TokenOperation}; +use namada_sdk::token::utils::is_masp_transfer; +use namada_sdk::token::Amount; +use namada_sdk::tx::action::{self, Read}; +use namada_sdk::tx::data::protocol::{ProtocolTx, ProtocolTxType}; +use namada_sdk::tx::data::{ BatchedTxResult, ExtendedTxResult, TxResult, VpStatusFlags, VpsResult, WrapperTx, }; -use namada_tx::{BatchedTxRef, Tx, TxCommitments}; +use namada_sdk::tx::{BatchedTxRef, Tx, TxCommitments}; +use namada_sdk::validation::{ + EthBridgeNutVp, EthBridgePoolVp, EthBridgeVp, GovernanceVp, IbcVp, MaspVp, + MultitokenVp, NativeVpCtx, ParametersVp, PgfVp, PosVp, +}; +use namada_sdk::{ + eth_bridge, governance, ibc, parameters, proof_of_stake, state, storage, + token, +}; +use namada_vm::wasm::{TxCache, VpCache}; +use namada_vm::{self, wasm, WasmCacheAccess}; use namada_vote_ext::EthereumTxData; +use namada_vp::native_vp::NativeVp; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use smooth_operator::checked; use thiserror::Error; -use crate::address::{Address, InternalAddress}; -use crate::ledger::gas::{GasMetering, VpGasMeter}; -use crate::ledger::governance::GovernanceVp; -use crate::ledger::native_vp::ethereum_bridge::bridge_pool_vp::BridgePoolVp; -use crate::ledger::native_vp::ethereum_bridge::nut::NonUsableTokens; -use crate::ledger::native_vp::ethereum_bridge::vp::EthBridge; -use crate::ledger::native_vp::ibc::Ibc; -use crate::ledger::native_vp::masp::MaspVp; -use crate::ledger::native_vp::multitoken::MultitokenVp; -use crate::ledger::native_vp::parameters::{self, ParametersVp}; -use crate::ledger::native_vp::{self, NativeVp}; -use crate::ledger::pgf::PgfVp; -use crate::ledger::pos::{self, PosVP}; -use crate::state::{DBIter, State, StorageHasher, StorageRead, WlState, DB}; -use crate::storage; -use crate::storage::TxIndex; -use crate::token::Amount; -use crate::vm::wasm::{TxCache, VpCache}; -use crate::vm::{self, wasm, WasmCacheAccess}; - #[allow(missing_docs)] #[derive(Error, Debug)] pub enum Error { @@ -58,13 +54,13 @@ pub enum Error { #[error("Missing tx section: {0}")] MissingSection(String), #[error("State error: {0}")] - StateError(namada_state::Error), + StateError(state::Error), #[error("Storage error: {0}")] - StorageError(namada_state::StorageError), + StorageError(state::StorageError), #[error("Wrapper tx runner error: {0}")] WrapperRunnerError(String), #[error("Transaction runner error: {0}")] - TxRunnerError(vm::wasm::run::Error), + TxRunnerError(wasm::run::Error), #[error("{0:?}")] ProtocolTxError(#[from] eyre::Error), #[error("The atomic batch failed at inner transaction {0}")] @@ -80,31 +76,31 @@ pub enum Error { )] ReplayAttempt(Hash), #[error("Error executing VP for addresses: {0:?}")] - VpRunnerError(vm::wasm::run::Error), + VpRunnerError(wasm::run::Error), #[error("The address {0} doesn't exist")] MissingAddress(Address), #[error("IBC native VP: {0}")] - IbcNativeVpError(crate::ledger::native_vp::ibc::Error), + IbcNativeVpError(ibc::vp::Error), #[error("PoS native VP: {0}")] - PosNativeVpError(pos::vp::Error), + PosNativeVpError(proof_of_stake::vp::Error), #[error("PoS native VP panicked")] PosNativeVpRuntime, #[error("Parameters native VP: {0}")] - ParametersNativeVpError(parameters::Error), + ParametersNativeVpError(parameters::vp::Error), #[error("Multitoken native VP: {0}")] - MultitokenNativeVpError(crate::ledger::native_vp::multitoken::Error), + MultitokenNativeVpError(token::vp::MultitokenError), #[error("Governance native VP error: {0}")] - GovernanceNativeVpError(crate::ledger::governance::Error), + GovernanceNativeVpError(governance::vp::Error), #[error("Pgf native VP error: {0}")] - PgfNativeVpError(crate::ledger::pgf::Error), + PgfNativeVpError(governance::vp::pgf::Error), #[error("Ethereum bridge native VP error: {0:?}")] - EthBridgeNativeVpError(native_vp::ethereum_bridge::vp::Error), + EthBridgeNativeVpError(eth_bridge::vp::EthBridgeError), #[error("Ethereum bridge pool native VP error: {0:?}")] - BridgePoolNativeVpError(native_vp::ethereum_bridge::bridge_pool_vp::Error), + BridgePoolNativeVpError(eth_bridge::vp::BridgePoolError), #[error("Non usable tokens native VP error: {0:?}")] - NutNativeVpError(native_vp::ethereum_bridge::nut::Error), + NutNativeVpError(eth_bridge::vp::NutError), #[error("MASP native VP error: {0}")] - MaspNativeVpError(native_vp::masp::Error), + MaspNativeVpError(token::vp::MaspError), #[error("Access to an internal address {0:?} is forbidden")] AccessForbidden(InternalAddress), } @@ -409,14 +405,14 @@ where let actions = state.read_actions().map_err(Error::StateError)?; if let Some(masp_section_ref) = - namada_tx::action::get_masp_section_ref(&actions) + action::get_masp_section_ref(&actions) { extended_tx_result .masp_tx_refs .0 .push(masp_section_ref); } - if is_ibc_shielding_transfer(&*state) + if action::is_ibc_shielding_transfer(&*state) .map_err(Error::StateError)? { extended_tx_result @@ -464,7 +460,11 @@ pub(crate) fn apply_wrapper_tx( block_proposer: Option<&Address>, ) -> Result> where - S: State + Read + TxWrites + Sync, + S: 'static + + State + + Read + + TxWrites + + Sync, D: 'static + DB + for<'iter> DBIter<'iter> + Sync, H: 'static + StorageHasher + Sync, CA: 'static + WasmCacheAccess + Sync, @@ -530,10 +530,11 @@ pub fn transfer_fee( tx_index: &TxIndex, ) -> Result> where - S: State + S: 'static + + State + StorageRead + TxWrites - + Read + + Read + Sync, D: 'static + DB + for<'iter> DBIter<'iter> + Sync, H: 'static + StorageHasher + Sync, @@ -541,14 +542,14 @@ where { match wrapper.get_tx_fee() { Ok(fees) => { - let fees = crate::token::denom_to_amount( + let fees = token::denom_to_amount( fees, &wrapper.fee.token, shell_params.state, ) .map_err(Error::StorageError)?; - let balance = crate::token::read_balance( + let balance = token::read_balance( shell_params.state, &wrapper.fee.token, &wrapper.fee_payer(), @@ -573,7 +574,7 @@ where if let Ok(Some(valid_batched_tx_result)) = try_masp_fee_payment(shell_params, tx, tx_index) { - let balance = crate::token::read_balance( + let balance = token::read_balance( shell_params.state, &wrapper.fee.token, &wrapper.fee_payer(), @@ -625,7 +626,7 @@ where }; let target_post_balance = Some( - namada_token::read_balance( + token::read_balance( shell_params.state, &wrapper.fee.token, block_proposer, @@ -683,9 +684,10 @@ fn try_masp_fee_payment( tx_index: &TxIndex, ) -> Result> where - S: State + S: 'static + + State + StorageRead - + Read + + Read + Sync, D: 'static + DB + for<'iter> DBIter<'iter> + Sync, H: 'static + StorageHasher + Sync, @@ -697,19 +699,16 @@ where // actually be the lowest between the protocol parameter and the actual gas // limit of the transaction let max_gas_limit = state - .read::( - &namada_parameters::storage::get_masp_fee_payment_gas_limit_key(), - ) + .read::(¶meters::storage::get_masp_fee_payment_gas_limit_key()) .expect("Error reading the storage") .expect("Missing masp fee payment gas limit in storage") .min(tx_gas_meter.borrow().tx_gas_limit.into()); let gas_scale = get_gas_scale(&**state).map_err(Error::StorageError)?; let mut gas_meter = TxGasMeter::new( - namada_gas::Gas::from_whole_units(max_gas_limit.into(), gas_scale) - .ok_or_else(|| { - Error::GasError("Overflow in gas expansion".to_string()) - })?, + Gas::from_whole_units(max_gas_limit.into(), gas_scale).ok_or_else( + || Error::GasError("Overflow in gas expansion".to_string()), + )?, ); gas_meter .copy_consumed_gas_from(&tx_gas_meter.borrow()) @@ -755,14 +754,14 @@ where && result.is_accepted() { if let Some(masp_tx_id) = - namada_tx::action::get_masp_section_ref(&actions) + action::get_masp_section_ref(&actions) { Some(MaspTxResult { tx_result: result, masp_section_ref: Either::Left(masp_tx_id), }) } else { - is_ibc_shielding_transfer(*state) + action::is_ibc_shielding_transfer(*state) .map_err(Error::StateError)? .then_some(MaspTxResult { tx_result: result, @@ -812,18 +811,12 @@ fn fee_token_transfer( where WLS: State + StorageRead + TxWrites, { - crate::token::transfer( - &mut state.with_tx_writes(), - token, - src, - dest, - amount, - ) - .map_err(|err| { - state.write_log_mut().drop_tx(); + token::transfer(&mut state.with_tx_writes(), token, src, dest, amount) + .map_err(|err| { + state.write_log_mut().drop_tx(); - Error::StorageError(err) - }) + Error::StorageError(err) + }) } /// Check if the fee payer has enough transparent balance to pay fees @@ -833,9 +826,10 @@ pub fn check_fees( wrapper: &WrapperTx, ) -> Result> where - S: State + S: 'static + + State + StorageRead - + Read + + Read + Sync, D: 'static + DB + for<'iter> DBIter<'iter> + Sync, H: 'static + StorageHasher + Sync, @@ -843,14 +837,14 @@ where { match wrapper.get_tx_fee() { Ok(fees) => { - let fees = crate::token::denom_to_amount( + let fees = token::denom_to_amount( fees, &wrapper.fee.token, shell_params.state, ) .map_err(Error::StorageError)?; - let balance = crate::token::read_balance( + let balance = token::read_balance( shell_params.state, &wrapper.fee.token, &wrapper.fee_payer(), @@ -868,7 +862,7 @@ where &TxIndex::default(), ) { - let balance = crate::token::read_balance( + let balance = token::read_balance( shell_params.state, &wrapper.fee.token, &wrapper.fee_payer(), @@ -900,13 +894,13 @@ where /// Apply a transaction going via the wasm environment. Gas will be metered and /// validity predicates will be triggered in the normal way. -pub fn apply_wasm_tx<'a, S, D, H, CA>( +pub fn apply_wasm_tx( batched_tx: &BatchedTxRef<'_>, tx_index: &TxIndex, - shell_params: ShellParams<'a, S, D, H, CA>, + shell_params: ShellParams<'_, S, D, H, CA>, ) -> Result where - S: State + Sync, + S: 'static + State + Sync, D: 'static + DB + for<'iter> DBIter<'iter> + Sync, H: 'static + StorageHasher + Sync, CA: 'static + WasmCacheAccess + Sync, @@ -963,7 +957,7 @@ where D: 'static + DB + for<'iter> DBIter<'iter> + Sync, H: 'static + StorageHasher + Sync, { - use namada_ethereum_bridge::protocol::transactions; + use namada_sdk::eth_bridge::protocol::transactions; use namada_vote_ext::{ethereum_events, validator_set_update}; let Some(data) = data else { @@ -1078,7 +1072,7 @@ fn check_vps( }: CheckVps<'_, S, CA>, ) -> Result where - S: State + Sync, + S: 'static + State + Sync, CA: 'static + WasmCacheAccess + Sync, { let (verifiers, keys_changed) = state @@ -1115,7 +1109,7 @@ fn execute_vps( vp_wasm_cache: &mut VpCache, ) -> Result where - S: State + Sync, + S: 'static + State + Sync, CA: 'static + WasmCacheAccess + Sync, { let vps_result = verifiers @@ -1126,7 +1120,7 @@ where let tx_accepted = match &addr { Address::Implicit(_) | Address::Established(_) => { let (vp_hash, gas) = state - .validity_predicate(addr) + .validity_predicate::>(addr) .map_err(Error::StateError)?; gas_meter .borrow_mut() @@ -1156,7 +1150,7 @@ where }) } Address::Internal(internal_addr) => { - let ctx = native_vp::Ctx::new( + let ctx = NativeVpCtx::new( addr, state, batched_tx.tx, @@ -1170,7 +1164,7 @@ where match internal_addr { InternalAddress::PoS => { - let pos = PosVP { ctx }; + let pos = PosVp::new(ctx); pos.validate_tx( batched_tx, &keys_changed, @@ -1179,7 +1173,7 @@ where .map_err(Error::PosNativeVpError) } InternalAddress::Ibc => { - let ibc = Ibc { ctx }; + let ibc = IbcVp::new(ctx); ibc.validate_tx( batched_tx, &keys_changed, @@ -1188,7 +1182,7 @@ where .map_err(Error::IbcNativeVpError) } InternalAddress::Parameters => { - let parameters = ParametersVp { ctx }; + let parameters = ParametersVp::new(ctx); parameters .validate_tx( batched_tx, @@ -1201,7 +1195,7 @@ where Error::AccessForbidden((*internal_addr).clone()), ), InternalAddress::Governance => { - let governance = GovernanceVp { ctx }; + let governance = GovernanceVp::new(ctx); governance .validate_tx( batched_tx, @@ -1210,8 +1204,18 @@ where ) .map_err(Error::GovernanceNativeVpError) } + InternalAddress::Pgf => { + let pgf_vp = PgfVp::new(ctx); + pgf_vp + .validate_tx( + batched_tx, + &keys_changed, + &verifiers, + ) + .map_err(Error::PgfNativeVpError) + } InternalAddress::Multitoken => { - let multitoken = MultitokenVp { ctx }; + let multitoken = MultitokenVp::new(ctx); multitoken .validate_tx( batched_tx, @@ -1220,8 +1224,17 @@ where ) .map_err(Error::MultitokenNativeVpError) } + InternalAddress::Masp => { + let masp = MaspVp::new(ctx); + masp.validate_tx( + batched_tx, + &keys_changed, + &verifiers, + ) + .map_err(Error::MaspNativeVpError) + } InternalAddress::EthBridge => { - let bridge = EthBridge { ctx }; + let bridge = EthBridgeVp::new(ctx); bridge .validate_tx( batched_tx, @@ -1231,7 +1244,7 @@ where .map_err(Error::EthBridgeNativeVpError) } InternalAddress::EthBridgePool => { - let bridge_pool = BridgePoolVp { ctx }; + let bridge_pool = EthBridgePoolVp::new(ctx); bridge_pool .validate_tx( batched_tx, @@ -1240,18 +1253,8 @@ where ) .map_err(Error::BridgePoolNativeVpError) } - InternalAddress::Pgf => { - let pgf_vp = PgfVp { ctx }; - pgf_vp - .validate_tx( - batched_tx, - &keys_changed, - &verifiers, - ) - .map_err(Error::PgfNativeVpError) - } InternalAddress::Nut(_) => { - let non_usable_tokens = NonUsableTokens { ctx }; + let non_usable_tokens = EthBridgeNutVp::new(ctx); non_usable_tokens .validate_tx( batched_tx, @@ -1274,15 +1277,6 @@ where ) }) } - InternalAddress::Masp => { - let masp = MaspVp { ctx }; - masp.validate_tx( - batched_tx, - &keys_changed, - &verifiers, - ) - .map_err(Error::MaspNativeVpError) - } InternalAddress::TempStorage => Err( // Temp storage changes must never be committed Error::AccessForbidden((*internal_addr).clone()), @@ -1362,21 +1356,21 @@ fn merge_vp_results( #[cfg(test)] mod tests { use eyre::Result; - use namada_core::collections::HashMap; - use namada_core::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS; - use namada_core::ethereum_events::{EthereumEvent, TransferToNamada}; - use namada_core::keccak::keccak_hash; - use namada_core::storage::BlockHeight; - use namada_core::voting_power::FractionalVotingPower; - use namada_core::{address, key}; - use namada_ethereum_bridge::protocol::transactions::votes::{ + use namada_sdk::collections::HashMap; + use namada_sdk::eth_bridge::protocol::transactions::votes::{ EpochedVotingPower, Votes, }; - use namada_ethereum_bridge::storage::eth_bridge_queries::EthBridgeQueries; - use namada_ethereum_bridge::storage::proof::EthereumProof; - use namada_ethereum_bridge::storage::{vote_tallies, vp}; - use namada_ethereum_bridge::test_utils; - use namada_tx::{SignableEthMessage, Signed}; + use namada_sdk::eth_bridge::storage::eth_bridge_queries::EthBridgeQueries; + use namada_sdk::eth_bridge::storage::proof::EthereumProof; + use namada_sdk::eth_bridge::storage::{vote_tallies, vp}; + use namada_sdk::eth_bridge::test_utils; + use namada_sdk::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS; + use namada_sdk::ethereum_events::{EthereumEvent, TransferToNamada}; + use namada_sdk::keccak::keccak_hash; + use namada_sdk::storage::BlockHeight; + use namada_sdk::tx::{SignableEthMessage, Signed}; + use namada_sdk::voting_power::FractionalVotingPower; + use namada_sdk::{address, key}; use namada_vote_ext::bridge_pool_roots::BridgePoolRootVext; use namada_vote_ext::ethereum_events::EthereumEventsVext; @@ -1521,7 +1515,7 @@ mod tests { let dst_address = Address::Established([0xba; 20].into()); // supply an address with 1000 of said token - namada_token::credit_tokens( + token::credit_tokens( &mut state, &token_address, &src_address, @@ -1536,12 +1530,12 @@ mod tests { // "execute" a dummy tx, by manually performing its state changes let (dummy_tx, changed_keys, verifiers) = { - let mut tx = Tx::from_type(namada_tx::data::TxType::Raw); - tx.set_code(namada_tx::Code::new(vec![], None)); - tx.set_data(namada_tx::Data::new(vec![])); + let mut tx = Tx::from_type(namada_sdk::tx::data::TxType::Raw); + tx.set_code(namada_sdk::tx::Code::new(vec![], None)); + tx.set_data(namada_sdk::tx::Data::new(vec![])); // transfer half of the supply of src to dst - namada_token::transfer( + token::transfer( &mut state, &token_address, &src_address, @@ -1552,11 +1546,11 @@ mod tests { let changed_keys = { let mut set = BTreeSet::new(); - set.insert(namada_token::storage_key::balance_key( + set.insert(token::storage_key::balance_key( &token_address, &src_address, )); - set.insert(namada_token::storage_key::balance_key( + set.insert(token::storage_key::balance_key( &token_address, &dst_address, )); diff --git a/crates/node/src/shell/block_alloc.rs b/crates/node/src/shell/block_alloc.rs index ef723fe1f4..ee09fd3ff7 100644 --- a/crates/node/src/shell/block_alloc.rs +++ b/crates/node/src/shell/block_alloc.rs @@ -41,8 +41,9 @@ pub mod states; use std::marker::PhantomData; -use namada::proof_of_stake::pos_queries::PosQueries; -use namada::state::{self, WlState}; +use namada_sdk::parameters; +use namada_sdk::proof_of_stake::pos_queries::PosQueries; +use namada_sdk::state::{self, WlState}; #[allow(unused_imports)] use crate::facade::tendermint_proto::abci::RequestPrepareProposal; @@ -135,7 +136,7 @@ where fn from(storage: &WlState) -> Self { Self::init( storage.pos_queries().get_max_proposal_bytes().get(), - namada::parameters::get_max_block_gas(storage).unwrap(), + parameters::get_max_block_gas(storage).unwrap(), ) } } diff --git a/crates/node/src/shell/finalize_block.rs b/crates/node/src/shell/finalize_block.rs index c81f838287..11e8593fb4 100644 --- a/crates/node/src/shell/finalize_block.rs +++ b/crates/node/src/shell/finalize_block.rs @@ -3,36 +3,34 @@ use data_encoding::HEXUPPER; use masp_primitives::merkle_tree::CommitmentTree; use masp_primitives::sapling::Node; -use namada::core::storage::{BlockResults, Epoch, Header}; -use namada::events::Event; -use namada::gas::event::GasUsed; -use namada::governance::pgf::inflation as pgf_inflation; -use namada::hash::Hash; -use namada::ledger::events::extend::{ +use namada_sdk::events::extend::{ ComposeEvent, Height, IbcMaspTxBatchRefs, Info, MaspTxBatchRefs, MaspTxBlockIndex, TxHash, }; -use namada::ledger::events::EmitEvents; -use namada::ledger::gas::GasMetering; -use namada::ledger::ibc; -use namada::ledger::pos::namada_proof_of_stake; -use namada::ledger::protocol::{DispatchArgs, DispatchError}; -use namada::proof_of_stake; -use namada::proof_of_stake::storage::{ +use namada_sdk::events::{EmitEvents, Event}; +use namada_sdk::gas::event::GasUsed; +use namada_sdk::gas::GasMetering; +use namada_sdk::governance::pgf::inflation as pgf_inflation; +use namada_sdk::hash::Hash; +use namada_sdk::parameters::get_gas_scale; +use namada_sdk::proof_of_stake::storage::{ find_validator_by_raw_hash, write_last_block_proposer_address, }; -use namada::state::write_log::StorageModification; -use namada::state::{ResultExt, StorageWrite, EPOCH_SWITCH_BLOCKS_DELAY}; -use namada::tx::data::protocol::ProtocolTxType; -use namada::tx::data::VpStatusFlags; -use namada::tx::event::{Batch, Code}; -use namada::tx::new_tx_event; -use namada::vote_ext::ethereum_events::MultiSignedEthEvent; -use namada::vote_ext::ethereum_tx_data_variants; -use parameters::get_gas_scale; +use namada_sdk::state::write_log::StorageModification; +use namada_sdk::state::{ResultExt, StorageWrite, EPOCH_SWITCH_BLOCKS_DELAY}; +use namada_sdk::storage::{BlockResults, Epoch, Header}; +use namada_sdk::tx::data::protocol::ProtocolTxType; +use namada_sdk::tx::data::VpStatusFlags; +use namada_sdk::tx::event::{Batch, Code}; +use namada_sdk::tx::new_tx_event; +use namada_sdk::{ibc, proof_of_stake}; +use namada_vote_ext::ethereum_events::MultiSignedEthEvent; +use namada_vote_ext::ethereum_tx_data_variants; use super::*; use crate::facade::tendermint::abci::types::VoteInfo; +use crate::facade::tendermint_proto; +use crate::protocol::{DispatchArgs, DispatchError}; use crate::shell::stats::InternalStats; impl Shell @@ -53,7 +51,12 @@ where // Begin the new block and check if a new epoch has begun let (height, new_epoch) = self.update_state(req.header); - let is_masp_new_epoch = self.state.is_masp_new_epoch(new_epoch)?; + let masp_epoch_multiplier = + parameters::read_masp_epoch_multiplier_parameter(&self.state) + .expect("Must have parameters"); + let is_masp_new_epoch = self + .state + .is_masp_new_epoch(new_epoch, masp_epoch_multiplier)?; let (current_epoch, _gas) = self.state.in_mem().get_current_epoch(); let update_for_tendermint = matches!( @@ -223,9 +226,11 @@ where .set_header(header) .expect("Setting a header shouldn't fail"); + let parameters = + parameters::read(&self.state).expect("Must have parameters"); let new_epoch = self .state - .update_epoch(height, header_time) + .update_epoch(height, header_time, ¶meters) .expect("Must be able to update epoch"); (height, new_epoch) } @@ -240,12 +245,11 @@ where // Apply validator set update response.validator_updates = self .get_abci_validator_updates(false, |pk, power| { - let pub_key = - crate::facade::tendermint_proto::v0_37::crypto::PublicKey { - sum: Some(key_to_tendermint(&pk).unwrap()), - }; + let pub_key = tendermint_proto::v0_37::crypto::PublicKey { + sum: Some(key_to_tendermint(&pk).unwrap()), + }; let pub_key = Some(pub_key); - namada::tendermint_proto::v0_37::abci::ValidatorUpdate { + tendermint_proto::v0_37::abci::ValidatorUpdate { pub_key, power, } @@ -285,7 +289,7 @@ where ); // PoS inflation - namada_proof_of_stake::rewards::apply_inflation( + proof_of_stake::rewards::apply_inflation( &mut self.state, last_epoch, num_blocks_in_last_epoch, @@ -294,7 +298,7 @@ where // Pgf inflation pgf_inflation::apply_inflation( self.state.restrict_writes_to_write_log(), - namada::ibc::transfer_over_ibc, + ibc::transfer_over_ibc, )?; // Take events that may be emitted from PGF @@ -335,7 +339,7 @@ where &mut self, response: &mut shim::response::FinalizeBlock, extended_dispatch_result: std::result::Result< - namada::tx::data::ExtendedTxResult, + namada_sdk::tx::data::ExtendedTxResult, DispatchError, >, tx_data: TxData<'_>, @@ -439,7 +443,9 @@ where fn handle_inner_tx_results( &mut self, response: &mut shim::response::FinalizeBlock, - extended_tx_result: namada::tx::data::ExtendedTxResult, + extended_tx_result: namada_sdk::tx::data::ExtendedTxResult< + protocol::Error, + >, tx_data: TxData<'_>, tx_logs: &mut TxLogs<'_>, ) { @@ -506,7 +512,9 @@ where &mut self, response: &mut shim::response::FinalizeBlock, msg: &Error, - extended_tx_result: namada::tx::data::ExtendedTxResult, + extended_tx_result: namada_sdk::tx::data::ExtendedTxResult< + protocol::Error, + >, tx_data: TxData<'_>, tx_logs: &mut TxLogs<'_>, ) { @@ -893,7 +901,7 @@ struct WrapperCache { tx_index: usize, gas_meter: TxGasMeter, event: Event, - extended_tx_result: namada::tx::data::ExtendedTxResult, + extended_tx_result: namada_sdk::tx::data::ExtendedTxResult, } struct TxData<'tx> { @@ -966,7 +974,7 @@ impl<'finalize> TempTxLogs { fn check_inner_results( &mut self, - extended_tx_result: &namada::tx::data::ExtendedTxResult< + extended_tx_result: &namada_sdk::tx::data::ExtendedTxResult< protocol::Error, >, tx_index: usize, @@ -1072,7 +1080,7 @@ struct ReplayProtectionHashes { fn pos_votes_from_abci( storage: &impl StorageRead, votes: &[VoteInfo], -) -> Vec { +) -> Vec { votes .iter() .filter_map( @@ -1108,7 +1116,7 @@ fn pos_votes_from_abci( // Try to convert voting power to u64 let validator_vp = u64::from(*power); - Some(namada_proof_of_stake::types::VoteInfo { + Some(proof_of_stake::types::VoteInfo { validator_address, validator_vp, }) @@ -1132,63 +1140,66 @@ mod test_finalize_block { use std::num::NonZeroU64; use std::str::FromStr; - use namada::core::collections::{HashMap, HashSet}; - use namada::core::dec::{Dec, POS_DECIMAL_PRECISION}; - use namada::core::ethereum_events::{EthAddress, Uint as ethUint}; - use namada::core::hash::Hash; - use namada::core::keccak::KeccakHash; - use namada::core::key::testing::common_sk_from_simple_seed; - use namada::core::storage::KeySeg; - use namada::core::time::DurationSecs; - use namada::core::uint::Uint; - use namada::eth_bridge::storage::bridge_pool::{ + use namada_replay_protection as replay_protection; + use namada_sdk::address; + use namada_sdk::collections::{HashMap, HashSet}; + use namada_sdk::dec::{Dec, POS_DECIMAL_PRECISION}; + use namada_sdk::eth_bridge::storage::bridge_pool::{ self, get_key_from_hash, get_nonce_key, get_signed_root_key, }; - use namada::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; - use namada::eth_bridge::storage::min_confirmations_key; - use namada::ethereum_bridge::storage::wrapped_erc20s; - use namada::governance::storage::keys::get_proposal_execution_key; - use namada::governance::storage::proposal::ProposalType; - use namada::governance::{InitProposalData, VoteProposalData}; - use namada::ledger::gas::VpGasMeter; - use namada::ledger::native_vp::parameters::ParametersVp; - use namada::ledger::native_vp::NativeVp; - use namada::ledger::parameters::EpochDuration; - use namada::proof_of_stake::storage::{ + use namada_sdk::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; + use namada_sdk::eth_bridge::storage::vote_tallies::BridgePoolRoot; + use namada_sdk::eth_bridge::storage::{ + min_confirmations_key, wrapped_erc20s, + }; + use namada_sdk::eth_bridge::MinimumConfirmations; + use namada_sdk::ethereum_events::{EthAddress, Uint as ethUint}; + use namada_sdk::events::Event; + use namada_sdk::gas::VpGasMeter; + use namada_sdk::governance::storage::keys::get_proposal_execution_key; + use namada_sdk::governance::storage::proposal::ProposalType; + use namada_sdk::governance::{ + InitProposalData, ProposalVote, VoteProposalData, + }; + use namada_sdk::hash::Hash; + use namada_sdk::keccak::KeccakHash; + use namada_sdk::key::testing::common_sk_from_simple_seed; + use namada_sdk::parameters::EpochDuration; + use namada_sdk::proof_of_stake::storage::{ enqueued_slashes_handle, get_num_consensus_validators, + liveness_missed_votes_handle, liveness_sum_missed_votes_handle, + read_consensus_validator_set_addresses, read_consensus_validator_set_addresses_with_stake, read_total_stake, read_validator_stake, rewards_accumulator_handle, validator_consensus_key_handle, validator_rewards_products_handle, validator_slashes_handle, validator_state_handle, write_pos_params, }; - use namada::proof_of_stake::storage_key::{ + use namada_sdk::proof_of_stake::storage_key::{ is_validator_slashes_key, slashes_prefix, }; - use namada::proof_of_stake::types::{ + use namada_sdk::proof_of_stake::types::{ BondId, SlashType, ValidatorState, WeightedValidator, }; - use namada::proof_of_stake::{unjail_validator, ADDRESS as pos_address}; - use namada::sdk::events::Event; - use namada::tendermint::abci::types::{Misbehavior, MisbehaviorKind}; - use namada::token::{ + use namada_sdk::proof_of_stake::{ + unjail_validator, ADDRESS as pos_address, + }; + use namada_sdk::storage::KeySeg; + use namada_sdk::tendermint::abci::types::{Misbehavior, MisbehaviorKind}; + use namada_sdk::time::DurationSecs; + use namada_sdk::token::{ read_balance, update_balance, Amount, DenominatedAmount, NATIVE_MAX_DECIMAL_PLACES, }; - use namada::tx::data::Fee; - use namada::tx::event::types::APPLIED as APPLIED_TX; - use namada::tx::event::Code as CodeAttr; - use namada::tx::{Authorization, Code, Data}; - use namada::vote_ext::ethereum_events; - use namada::{address, replay_protection}; - use namada_sdk::eth_bridge::storage::vote_tallies::BridgePoolRoot; - use namada_sdk::eth_bridge::MinimumConfirmations; - use namada_sdk::governance::ProposalVote; - use namada_sdk::proof_of_stake::storage::{ - liveness_missed_votes_handle, liveness_sum_missed_votes_handle, - read_consensus_validator_set_addresses, - }; + use namada_sdk::tx::data::Fee; + use namada_sdk::tx::event::types::APPLIED as APPLIED_TX; + use namada_sdk::tx::event::Code as CodeAttr; + use namada_sdk::tx::{Authorization, Code, Data}; + use namada_sdk::uint::Uint; + use namada_sdk::validation::ParametersVp; use namada_test_utils::tx_data::TxWriteData; use namada_test_utils::TestWasms; + use namada_vote_ext::ethereum_events; + use namada_vp::native_vp::NativeVp; use test_log::test; use super::*; @@ -1572,7 +1583,7 @@ mod test_finalize_block { return; } let (mut shell, _, _, _) = setup_at_height(1u64); - namada::eth_bridge::test_utils::commit_bridge_pool_root_at_height( + namada_sdk::eth_bridge::test_utils::commit_bridge_pool_root_at_height( &mut shell.state, &KeccakHash([1; 32]), 1.into(), @@ -1660,7 +1671,7 @@ mod test_finalize_block { } // write transfer to storage let transfer = { - use namada::core::eth_bridge_pool::{ + use namada_sdk::eth_bridge_pool::{ GasFee, PendingTransfer, TransferToEthereum, TransferToEthereumKind, }; @@ -1730,7 +1741,7 @@ mod test_finalize_block { let (mut shell, _broadcaster, _, _eth_control) = setup(); let masp_epoch_multiplier = - namada::ledger::parameters::read_masp_epoch_multiplier_parameter( + namada_sdk::parameters::read_masp_epoch_multiplier_parameter( &shell.state, ) .unwrap(); @@ -1739,10 +1750,20 @@ mod test_finalize_block { for _ in 1..masp_epoch_multiplier { shell.start_new_epoch(None); - assert!(!shell.state.is_masp_new_epoch(true).unwrap()); + assert!( + !shell + .state + .is_masp_new_epoch(true, masp_epoch_multiplier) + .unwrap() + ); } shell.start_new_epoch(None); - assert!(shell.state.is_masp_new_epoch(true).unwrap()); + assert!( + shell + .state + .is_masp_new_epoch(true, masp_epoch_multiplier) + .unwrap() + ); } /// Test that the finalize block handler never commits changes directly to @@ -1756,7 +1777,7 @@ mod test_finalize_block { min_num_of_blocks: 5, min_duration: DurationSecs(0), }; - namada::ledger::parameters::update_epoch_parameter( + namada_sdk::parameters::update_epoch_parameter( &mut shell.state, &epoch_duration, ) @@ -1791,7 +1812,7 @@ mod test_finalize_block { r#type: ProposalType::Default, }; - namada::governance::init_proposal( + namada_sdk::governance::init_proposal( &mut shell.state, &proposal, vec![], @@ -1806,7 +1827,7 @@ mod test_finalize_block { }; // Vote to accept the proposal (there's only one validator, so its // vote decides) - namada::governance::vote_proposal( + namada_sdk::governance::vote_proposal( &mut shell.state, vote, HashSet::new(), @@ -1841,15 +1862,12 @@ mod test_finalize_block { // Keep applying finalize block let validator = shell.mode.get_validator_address().unwrap(); let pos_params = - namada_proof_of_stake::storage::read_pos_params(&shell.state) - .unwrap(); + proof_of_stake::storage::read_pos_params(&shell.state).unwrap(); let consensus_key = - namada_proof_of_stake::storage::validator_consensus_key_handle( - validator, - ) - .get(&shell.state, Epoch::default(), &pos_params) - .unwrap() - .unwrap(); + proof_of_stake::storage::validator_consensus_key_handle(validator) + .get(&shell.state, Epoch::default(), &pos_params) + .unwrap() + .unwrap(); let proposer_address = HEXUPPER .decode(consensus_key.tm_raw_hash().as_bytes()) .unwrap(); @@ -2282,7 +2300,7 @@ mod test_finalize_block { total_rewards += inflation; // Query the available rewards - let query_rewards = namada_proof_of_stake::query_reward_tokens( + let query_rewards = proof_of_stake::query_reward_tokens( &shell.state, None, &validator.address, @@ -2291,7 +2309,7 @@ mod test_finalize_block { .unwrap(); // Claim the rewards from the initial epoch - let reward_1 = namada_proof_of_stake::claim_reward_tokens( + let reward_1 = proof_of_stake::claim_reward_tokens( &mut shell.state, None, &validator.address, @@ -2304,7 +2322,7 @@ mod test_finalize_block { // Query the available rewards again and check that it is 0 now after // the claim - let query_rewards = namada_proof_of_stake::query_reward_tokens( + let query_rewards = proof_of_stake::query_reward_tokens( &shell.state, None, &validator.address, @@ -2320,7 +2338,7 @@ mod test_finalize_block { votes.clone(), None, ); - let att = namada_proof_of_stake::claim_reward_tokens( + let att = proof_of_stake::claim_reward_tokens( &mut shell.state, None, &validator.address, @@ -2336,7 +2354,7 @@ mod test_finalize_block { // Unbond some tokens let unbond_amount = token::Amount::native_whole(50_000); - let unbond_res = namada_proof_of_stake::unbond_tokens( + let unbond_res = proof_of_stake::unbond_tokens( &mut shell.state, None, &validator.address, @@ -2348,7 +2366,7 @@ mod test_finalize_block { assert_eq!(unbond_res.sum, unbond_amount); // Query the available rewards - let query_rewards = namada_proof_of_stake::query_reward_tokens( + let query_rewards = proof_of_stake::query_reward_tokens( &shell.state, None, &validator.address, @@ -2356,7 +2374,7 @@ mod test_finalize_block { ) .unwrap(); - let rew = namada_proof_of_stake::claim_reward_tokens( + let rew = proof_of_stake::claim_reward_tokens( &mut shell.state, None, &validator.address, @@ -2370,13 +2388,13 @@ mod test_finalize_block { // Check the bond amounts for rewards up thru the withdrawable epoch let withdraw_epoch = current_epoch + params.withdrawable_epoch_offset(); let last_claim_epoch = - namada_proof_of_stake::storage::get_last_reward_claim_epoch( + proof_of_stake::storage::get_last_reward_claim_epoch( &shell.state, &validator.address, &validator.address, ) .unwrap(); - let bond_amounts = namada_proof_of_stake::bond_amounts_for_rewards( + let bond_amounts = proof_of_stake::bond_amounts_for_rewards( &shell.state, &bond_id, last_claim_epoch.unwrap_or_default(), @@ -2416,7 +2434,7 @@ mod test_finalize_block { } // Withdraw tokens - let withdraw_amount = namada_proof_of_stake::withdraw_tokens( + let withdraw_amount = proof_of_stake::withdraw_tokens( &mut shell.state, None, &validator.address, @@ -2426,7 +2444,7 @@ mod test_finalize_block { assert_eq!(withdraw_amount, unbond_amount); // Query the available rewards - let query_rewards = namada_proof_of_stake::query_reward_tokens( + let query_rewards = proof_of_stake::query_reward_tokens( &shell.state, None, &validator.address, @@ -2435,7 +2453,7 @@ mod test_finalize_block { .unwrap(); // Claim tokens - let reward_2 = namada_proof_of_stake::claim_reward_tokens( + let reward_2 = proof_of_stake::claim_reward_tokens( &mut shell.state, None, &validator.address, @@ -2457,7 +2475,7 @@ mod test_finalize_block { assert!(token_diff < token_uncertainty); // Query the available rewards to check that they are 0 - let query_rewards = namada_proof_of_stake::query_reward_tokens( + let query_rewards = proof_of_stake::query_reward_tokens( &shell.state, None, &validator.address, @@ -2489,7 +2507,7 @@ mod test_finalize_block { let validator = validator_set.pop_first().unwrap(); let commission_rate = - namada_proof_of_stake::storage::validator_commission_rate_handle( + proof_of_stake::storage::validator_commission_rate_handle( &validator.address, ) .get(&shell.state, Epoch(0), ¶ms) @@ -2530,7 +2548,7 @@ mod test_finalize_block { let delegator = address::testing::gen_implicit_address(); let del_amount = init_stake; let staking_token = shell.state.in_mem().native_token.clone(); - namada::token::credit_tokens( + namada_sdk::token::credit_tokens( &mut shell.state, &staking_token, &delegator, @@ -2538,7 +2556,7 @@ mod test_finalize_block { ) .unwrap(); let mut current_epoch = shell.state.in_mem().block.epoch; - namada_proof_of_stake::bond_tokens( + proof_of_stake::bond_tokens( &mut shell.state, Some(&delegator), &validator.address, @@ -2561,7 +2579,7 @@ mod test_finalize_block { } // Claim the rewards for the validator for the first two epochs - let val_reward_1 = namada_proof_of_stake::claim_reward_tokens( + let val_reward_1 = proof_of_stake::claim_reward_tokens( &mut shell.state, None, &validator.address, @@ -2587,7 +2605,7 @@ mod test_finalize_block { total_rewards += inflation_3; // Claim again for the validator - let val_reward_2 = namada_proof_of_stake::claim_reward_tokens( + let val_reward_2 = proof_of_stake::claim_reward_tokens( &mut shell.state, None, &validator.address, @@ -2596,7 +2614,7 @@ mod test_finalize_block { .unwrap(); // Claim for the delegator - let del_reward_1 = namada_proof_of_stake::claim_reward_tokens( + let del_reward_1 = proof_of_stake::claim_reward_tokens( &mut shell.state, Some(&delegator), &validator.address, @@ -2655,21 +2673,21 @@ mod test_finalize_block { // Give the validators some tokens for txs let staking_token = shell.state.in_mem().native_token.clone(); - namada::token::credit_tokens( + namada_sdk::token::credit_tokens( &mut shell.state, &staking_token, &validator1.address, init_stake, ) .unwrap(); - namada::token::credit_tokens( + namada_sdk::token::credit_tokens( &mut shell.state, &staking_token, &validator2.address, init_stake, ) .unwrap(); - namada::token::credit_tokens( + namada_sdk::token::credit_tokens( &mut shell.state, &staking_token, &validator3.address, @@ -2695,7 +2713,7 @@ mod test_finalize_block { // Check that there's 3 unique consensus keys let consensus_keys = - namada_proof_of_stake::storage::get_consensus_key_set(&shell.state) + proof_of_stake::storage::get_consensus_key_set(&shell.state) .unwrap(); assert_eq!(consensus_keys.len(), 3); // let ck1 = validator_consensus_key_handle(&validator) @@ -2709,7 +2727,7 @@ mod test_finalize_block { // Validator1 bonds 1 NAM let bond_amount = token::Amount::native_whole(1); - namada_proof_of_stake::bond_tokens( + proof_of_stake::bond_tokens( &mut shell.state, None, &validator1.address, @@ -2721,7 +2739,7 @@ mod test_finalize_block { // Validator2 changes consensus key let new_ck2 = common_sk_from_simple_seed(1).ref_to(); - namada_proof_of_stake::change_consensus_key( + proof_of_stake::change_consensus_key( &mut shell.state, &validator2.address, &new_ck2, @@ -2730,7 +2748,7 @@ mod test_finalize_block { .unwrap(); // Validator3 bonds 1 NAM and changes consensus key - namada_proof_of_stake::bond_tokens( + proof_of_stake::bond_tokens( &mut shell.state, None, &validator3.address, @@ -2740,7 +2758,7 @@ mod test_finalize_block { ) .unwrap(); let new_ck3 = common_sk_from_simple_seed(2).ref_to(); - namada_proof_of_stake::change_consensus_key( + proof_of_stake::change_consensus_key( &mut shell.state, &validator3.address, &new_ck3, @@ -2750,7 +2768,7 @@ mod test_finalize_block { // Check that there's 5 unique consensus keys let consensus_keys = - namada_proof_of_stake::storage::get_consensus_key_set(&shell.state) + proof_of_stake::storage::get_consensus_key_set(&shell.state) .unwrap(); assert_eq!(consensus_keys.len(), 5); @@ -2790,7 +2808,7 @@ mod test_finalize_block { // Val 1 changes consensus key let new_ck1 = common_sk_from_simple_seed(3).ref_to(); - namada_proof_of_stake::change_consensus_key( + proof_of_stake::change_consensus_key( &mut shell.state, &validator1.address, &new_ck1, @@ -2799,7 +2817,7 @@ mod test_finalize_block { .unwrap(); // Val 2 is fully unbonded - namada_proof_of_stake::unbond_tokens( + proof_of_stake::unbond_tokens( &mut shell.state, None, &validator2.address, @@ -2810,7 +2828,7 @@ mod test_finalize_block { .unwrap(); // Val 3 is fully unbonded and changes consensus key - namada_proof_of_stake::unbond_tokens( + proof_of_stake::unbond_tokens( &mut shell.state, None, &validator3.address, @@ -2820,7 +2838,7 @@ mod test_finalize_block { ) .unwrap(); let new2_ck3 = common_sk_from_simple_seed(4).ref_to(); - namada_proof_of_stake::change_consensus_key( + proof_of_stake::change_consensus_key( &mut shell.state, &validator1.address, &new2_ck3, @@ -2830,7 +2848,7 @@ mod test_finalize_block { // Check that there's 7 unique consensus keys let consensus_keys = - namada_proof_of_stake::storage::get_consensus_key_set(&shell.state) + proof_of_stake::storage::get_consensus_key_set(&shell.state) .unwrap(); assert_eq!(consensus_keys.len(), 7); @@ -2862,7 +2880,7 @@ mod test_finalize_block { // set, along with consensus key changes // Val2 bonds 1 NAM and changes consensus key - namada_proof_of_stake::bond_tokens( + proof_of_stake::bond_tokens( &mut shell.state, None, &validator2.address, @@ -2872,7 +2890,7 @@ mod test_finalize_block { ) .unwrap(); let new2_ck2 = common_sk_from_simple_seed(5).ref_to(); - namada_proof_of_stake::change_consensus_key( + proof_of_stake::change_consensus_key( &mut shell.state, &validator2.address, &new2_ck2, @@ -2881,7 +2899,7 @@ mod test_finalize_block { .unwrap(); // Val3 bonds 1 NAM - namada_proof_of_stake::bond_tokens( + proof_of_stake::bond_tokens( &mut shell.state, None, &validator3.address, @@ -2893,7 +2911,7 @@ mod test_finalize_block { // Check that there's 8 unique consensus keys let consensus_keys = - namada_proof_of_stake::storage::get_consensus_key_set(&shell.state) + proof_of_stake::storage::get_consensus_key_set(&shell.state) .unwrap(); assert_eq!(consensus_keys.len(), 8); @@ -3021,9 +3039,10 @@ mod test_finalize_block { fn test_masp_anchors_merklized() { let (mut shell, _, _, _) = setup(); - let convert_key = namada::token::storage_key::masp_convert_anchor_key(); + let convert_key = + namada_sdk::token::storage_key::masp_convert_anchor_key(); let commitment_key = - namada::token::storage_key::masp_commitment_anchor_key(0); + namada_sdk::token::storage_key::masp_commitment_anchor_key(0); // merkle tree root before finalize_block let root_pre = shell.shell.state.in_mem().block.tree.root(); @@ -3617,13 +3636,13 @@ mod test_finalize_block { let fee_amount = wrapper.header().wrapper().unwrap().get_tx_fee().unwrap(); - let fee_amount = namada::token::denom_to_amount( + let fee_amount = namada_sdk::token::denom_to_amount( fee_amount, &wrapper.header().wrapper().unwrap().fee.token, &shell.state, ) .unwrap(); - let signer_balance = namada::token::read_balance( + let signer_balance = namada_sdk::token::read_balance( &shell.state, &shell.state.in_mem().native_token, &wrapper.header().wrapper().unwrap().fee_payer(), @@ -3658,7 +3677,7 @@ mod test_finalize_block { .unwrap(); assert!(inner_result.is_err()); - let new_signer_balance = namada::token::read_balance( + let new_signer_balance = namada_sdk::token::read_balance( &shell.state, &shell.state.in_mem().native_token, &wrapper.header().wrapper().unwrap().fee_payer(), @@ -3680,7 +3699,7 @@ mod test_finalize_block { // Credit some tokens for fee payment let initial_balance: token::Amount = 1.into(); - namada::token::credit_tokens( + namada_sdk::token::credit_tokens( &mut shell.state, &native_token, &Address::from(&keypair.to_public()), @@ -3702,7 +3721,7 @@ mod test_finalize_block { // payer let fee_amount = batch.header().wrapper().unwrap().get_tx_fee().unwrap(); - let fee_amount = namada::token::denom_to_amount( + let fee_amount = namada_sdk::token::denom_to_amount( fee_amount, &batch.header().wrapper().unwrap().fee.token, &shell.state, @@ -3748,20 +3767,17 @@ mod test_finalize_block { let validator = shell.mode.get_validator_address().unwrap().to_owned(); let pos_params = - namada_proof_of_stake::storage::read_pos_params(&shell.state) - .unwrap(); + proof_of_stake::storage::read_pos_params(&shell.state).unwrap(); let consensus_key = - namada_proof_of_stake::storage::validator_consensus_key_handle( - &validator, - ) - .get(&shell.state, Epoch::default(), &pos_params) - .unwrap() - .unwrap(); + proof_of_stake::storage::validator_consensus_key_handle(&validator) + .get(&shell.state, Epoch::default(), &pos_params) + .unwrap() + .unwrap(); let proposer_address = HEXUPPER .decode(consensus_key.tm_raw_hash().as_bytes()) .unwrap(); - let proposer_balance = namada::token::read_balance( + let proposer_balance = namada_sdk::token::read_balance( &shell.state, &shell.state.in_mem().native_token, &validator, @@ -3793,14 +3809,14 @@ mod test_finalize_block { ))); let fee_amount = wrapper.header().wrapper().unwrap().get_tx_fee().unwrap(); - let fee_amount = namada::token::denom_to_amount( + let fee_amount = namada_sdk::token::denom_to_amount( fee_amount, &wrapper.header().wrapper().unwrap().fee.token, &shell.state, ) .unwrap(); - let signer_balance = namada::token::read_balance( + let signer_balance = namada_sdk::token::read_balance( &shell.state, &shell.state.in_mem().native_token, &wrapper.header().wrapper().unwrap().fee_payer(), @@ -3828,7 +3844,7 @@ mod test_finalize_block { let code = event.read_attribute::().expect("Test failed"); assert_eq!(code, ResultCode::Ok); - let new_proposer_balance = namada::token::read_balance( + let new_proposer_balance = namada_sdk::token::read_balance( &shell.state, &shell.state.in_mem().native_token, &validator, @@ -3839,7 +3855,7 @@ mod test_finalize_block { proposer_balance.checked_add(fee_amount).unwrap() ); - let new_signer_balance = namada::token::read_balance( + let new_signer_balance = namada_sdk::token::read_balance( &shell.state, &shell.state.in_mem().native_token, &wrapper.header().wrapper().unwrap().fee_payer(), @@ -3852,7 +3868,7 @@ mod test_finalize_block { } #[test] - fn test_ledger_slashing() -> namada::state::StorageResult<()> { + fn test_ledger_slashing() -> namada_sdk::state::StorageResult<()> { let num_validators = 7_u64; let (mut shell, _recv, _, _) = setup_with_cfg(SetupCfg { last_height: 0, @@ -4058,13 +4074,15 @@ mod test_finalize_block { } } - let num_slashes = - namada::state::iter_prefix_bytes(&shell.state, &slashes_prefix())? - .filter(|kv_res| { - let (k, _v) = kv_res.as_ref().unwrap(); - is_validator_slashes_key(k).is_some() - }) - .count(); + let num_slashes = namada_sdk::state::iter_prefix_bytes( + &shell.state, + &slashes_prefix(), + )? + .filter(|kv_res| { + let (k, _v) = kv_res.as_ref().unwrap(); + is_validator_slashes_key(k).is_some() + }) + .count(); assert_eq!(num_slashes, 2); assert_eq!( @@ -4220,7 +4238,7 @@ mod test_finalize_block { /// NOTE: must call `get_default_true_votes` before every call to /// `next_block_for_inflation` #[test] - fn test_multiple_misbehaviors() -> namada::state::StorageResult<()> { + fn test_multiple_misbehaviors() -> namada_sdk::state::StorageResult<()> { for num_validators in &[4_u64, 6_u64, 9_u64] { tracing::debug!("\nNUM VALIDATORS = {}", num_validators); test_multiple_misbehaviors_by_num_vals(*num_validators)?; @@ -4240,7 +4258,7 @@ mod test_finalize_block { /// 7) Discover misbehavior in epoch 4 fn test_multiple_misbehaviors_by_num_vals( num_validators: u64, - ) -> namada::state::StorageResult<()> { + ) -> namada_sdk::state::StorageResult<()> { // Setup the network with pipeline_len = 2, unbonding_len = 4 // let num_validators = 8_u64; let (mut shell, _recv, _, _) = setup_with_cfg(SetupCfg { @@ -4258,7 +4276,7 @@ mod test_finalize_block { let slash_pool_balance_init = read_balance( &shell.state, &nam_address, - &namada_proof_of_stake::SLASH_POOL_ADDRESS, + &proof_of_stake::SLASH_POOL_ADDRESS, ) .unwrap(); debug_assert_eq!(slash_pool_balance_init, token::Amount::zero()); @@ -4299,14 +4317,14 @@ mod test_finalize_block { let delegator = address::testing::gen_implicit_address(); let del_1_amount = token::Amount::native_whole(37_231); let staking_token = shell.state.in_mem().native_token.clone(); - namada::token::credit_tokens( + namada_sdk::token::credit_tokens( &mut shell.state, &staking_token, &delegator, token::Amount::native_whole(200_000), ) .unwrap(); - namada_proof_of_stake::bond_tokens( + proof_of_stake::bond_tokens( &mut shell.state, Some(&delegator), &val1.address, @@ -4318,7 +4336,7 @@ mod test_finalize_block { // Self-unbond let self_unbond_1_amount = token::Amount::native_whole(84_654); - namada_proof_of_stake::unbond_tokens( + proof_of_stake::unbond_tokens( &mut shell.state, None, &val1.address, @@ -4328,7 +4346,7 @@ mod test_finalize_block { ) .unwrap(); - let val_stake = namada_proof_of_stake::storage::read_validator_stake( + let val_stake = proof_of_stake::storage::read_validator_stake( &shell.state, ¶ms, &val1.address, @@ -4336,7 +4354,7 @@ mod test_finalize_block { ) .unwrap(); - let total_stake = namada_proof_of_stake::storage::read_total_stake( + let total_stake = proof_of_stake::storage::read_total_stake( &shell.state, ¶ms, current_epoch + params.pipeline_len, @@ -4361,7 +4379,7 @@ mod test_finalize_block { let (current_epoch, _) = advance_epoch(&mut shell, &pkh1, &votes, None); tracing::debug!("\nUnbonding in epoch 2"); let del_unbond_1_amount = token::Amount::native_whole(18_000); - namada_proof_of_stake::unbond_tokens( + proof_of_stake::unbond_tokens( &mut shell.state, Some(&delegator), &val1.address, @@ -4371,14 +4389,14 @@ mod test_finalize_block { ) .unwrap(); - let val_stake = namada_proof_of_stake::storage::read_validator_stake( + let val_stake = proof_of_stake::storage::read_validator_stake( &shell.state, ¶ms, &val1.address, current_epoch + params.pipeline_len, ) .unwrap(); - let total_stake = namada_proof_of_stake::storage::read_total_stake( + let total_stake = proof_of_stake::storage::read_total_stake( &shell.state, ¶ms, current_epoch + params.pipeline_len, @@ -4407,7 +4425,7 @@ mod test_finalize_block { tracing::debug!("\nBonding in epoch 3"); let self_bond_1_amount = token::Amount::native_whole(9_123); - namada_proof_of_stake::bond_tokens( + proof_of_stake::bond_tokens( &mut shell.state, None, &val1.address, @@ -4427,7 +4445,7 @@ mod test_finalize_block { assert_eq!(current_epoch.0, 4_u64); let self_unbond_2_amount = token::Amount::native_whole(15_000); - namada_proof_of_stake::unbond_tokens( + proof_of_stake::unbond_tokens( &mut shell.state, None, &val1.address, @@ -4449,7 +4467,7 @@ mod test_finalize_block { // Delegate let del_2_amount = token::Amount::native_whole(8_144); - namada_proof_of_stake::bond_tokens( + proof_of_stake::bond_tokens( &mut shell.state, Some(&delegator), &val1.address, @@ -4512,18 +4530,16 @@ mod test_finalize_block { assert_eq!(enqueued_slash.r#type, SlashType::DuplicateVote); assert_eq!(enqueued_slash.rate, Dec::zero()); let last_slash = - namada_proof_of_stake::storage::read_validator_last_slash_epoch( + proof_of_stake::storage::read_validator_last_slash_epoch( &shell.state, &val1.address, ) .unwrap(); assert_eq!(last_slash, Some(misbehavior_epoch)); assert!( - namada_proof_of_stake::storage::validator_slashes_handle( - &val1.address - ) - .is_empty(&shell.state) - .unwrap() + proof_of_stake::storage::validator_slashes_handle(&val1.address) + .is_empty(&shell.state) + .unwrap() ); tracing::debug!("Advancing to epoch 7"); @@ -4583,14 +4599,14 @@ mod test_finalize_block { assert_eq!(num_enqueued_8, 2); assert_eq!(num_enqueued_9, 1); let last_slash = - namada_proof_of_stake::storage::read_validator_last_slash_epoch( + proof_of_stake::storage::read_validator_last_slash_epoch( &shell.state, &val1.address, ) .unwrap(); assert_eq!(last_slash, Some(Epoch(4))); assert!( - namada_proof_of_stake::is_validator_frozen( + proof_of_stake::is_validator_frozen( &shell.state, &val1.address, current_epoch, @@ -4599,21 +4615,18 @@ mod test_finalize_block { .unwrap() ); assert!( - namada_proof_of_stake::storage::validator_slashes_handle( - &val1.address - ) - .is_empty(&shell.state) - .unwrap() + proof_of_stake::storage::validator_slashes_handle(&val1.address) + .is_empty(&shell.state) + .unwrap() ); - let pre_stake_10 = - namada_proof_of_stake::storage::read_validator_stake( - &shell.state, - ¶ms, - &val1.address, - Epoch(10), - ) - .unwrap(); + let pre_stake_10 = proof_of_stake::storage::read_validator_stake( + &shell.state, + ¶ms, + &val1.address, + Epoch(10), + ) + .unwrap(); assert_eq!( pre_stake_10, initial_stake + del_1_amount @@ -4640,14 +4653,14 @@ mod test_finalize_block { let (current_epoch, _) = advance_epoch(&mut shell, &pkh1, &votes, None); assert_eq!(current_epoch.0, 9_u64); - let val_stake_3 = namada_proof_of_stake::storage::read_validator_stake( + let val_stake_3 = proof_of_stake::storage::read_validator_stake( &shell.state, ¶ms, &val1.address, Epoch(3), ) .unwrap(); - let val_stake_4 = namada_proof_of_stake::storage::read_validator_stake( + let val_stake_4 = proof_of_stake::storage::read_validator_stake( &shell.state, ¶ms, &val1.address, @@ -4655,13 +4668,13 @@ mod test_finalize_block { ) .unwrap(); - let tot_stake_3 = namada_proof_of_stake::storage::read_total_stake( + let tot_stake_3 = proof_of_stake::storage::read_total_stake( &shell.state, ¶ms, Epoch(3), ) .unwrap(); - let tot_stake_4 = namada_proof_of_stake::storage::read_total_stake( + let tot_stake_4 = proof_of_stake::storage::read_total_stake( &shell.state, ¶ms, Epoch(4), @@ -4687,9 +4700,7 @@ mod test_finalize_block { // There should be 2 slashes processed for the validator, each with rate // equal to the cubic slashing rate let val_slashes = - namada_proof_of_stake::storage::validator_slashes_handle( - &val1.address, - ); + proof_of_stake::storage::validator_slashes_handle(&val1.address); assert_eq!(val_slashes.len(&shell.state).unwrap(), 2u64); let is_rate_good = val_slashes .iter(&shell.state) @@ -4872,7 +4883,7 @@ mod test_finalize_block { assert_eq!(current_epoch.0, 12_u64); tracing::debug!("\nCHECK BOND AND UNBOND DETAILS"); - let details = namada_proof_of_stake::queries::bonds_and_unbonds( + let details = proof_of_stake::queries::bonds_and_unbonds( &shell.state, None, None, @@ -4991,7 +5002,7 @@ mod test_finalize_block { // let slash_pool_balance_pre_withdraw = slash_pool_balance; // Withdraw the delegation unbonds, which total to 18_000. This should // only be affected by the slashes in epoch 3 - let del_withdraw = namada_proof_of_stake::withdraw_tokens( + let del_withdraw = proof_of_stake::withdraw_tokens( &mut shell.state, Some(&delegator), &val1.address, @@ -5024,7 +5035,7 @@ mod test_finalize_block { // println!("\nWITHDRAWING SELF UNBOND"); // Withdraw the self unbonds, which total 154_654 + 15_000 - 9_123. Only // the (15_000 - 9_123) tokens are slashable. - // let self_withdraw = namada_proof_of_stake::withdraw_tokens( + // let self_withdraw = proof_of_stake::withdraw_tokens( // &mut shell.state, // None, // &val1.address, @@ -5060,8 +5071,8 @@ mod test_finalize_block { } #[test] - fn test_jail_validator_for_inactivity() -> namada::state::StorageResult<()> - { + fn test_jail_validator_for_inactivity() + -> namada_sdk::state::StorageResult<()> { let num_validators = 5_u64; let (mut shell, _recv, _, _) = setup_with_cfg(SetupCfg { last_height: 0, @@ -5093,14 +5104,13 @@ mod test_finalize_block { Epoch::default(), ); - let validator_stake = - namada_proof_of_stake::storage::read_validator_stake( - &shell.state, - ¶ms, - &val2, - Epoch::default(), - ) - .unwrap(); + let validator_stake = proof_of_stake::storage::read_validator_stake( + &shell.state, + ¶ms, + &val2, + Epoch::default(), + ) + .unwrap(); let val3 = initial_consensus_set[2].clone(); let val4 = initial_consensus_set[3].clone(); @@ -5142,7 +5152,7 @@ mod test_finalize_block { // Completely unbond one of the validator to test the pruning at the // pipeline epoch let mut current_epoch = shell.state.in_mem().block.epoch; - namada_proof_of_stake::unbond_tokens( + proof_of_stake::unbond_tokens( &mut shell.state, None, &val5, @@ -5345,7 +5355,7 @@ mod test_finalize_block { } // Validator 2 unjail itself - namada_proof_of_stake::unjail_validator( + proof_of_stake::unjail_validator( &mut shell.state, &val2, current_epoch, @@ -5412,8 +5422,7 @@ mod test_finalize_block { misbehaviors: Option>, ) -> (Epoch, token::Amount) { let current_epoch = shell.state.in_mem().block.epoch; - let staking_token = - namada_proof_of_stake::staking_token_address(&shell.state); + let staking_token = proof_of_stake::staking_token_address(&shell.state); // NOTE: assumed that the only change in pos address balance by // advancing to the next epoch is minted inflation - no change occurs @@ -5470,7 +5479,7 @@ mod test_finalize_block { let keys_changed = BTreeSet::from([min_confirmations_key()]); let verifiers = BTreeSet::default(); let batched_tx = tx.batch_ref_first_tx().unwrap(); - let ctx = namada::ledger::native_vp::Ctx::new( + let ctx = namada_vp::native_vp::Ctx::new( shell.mode.get_validator_address().expect("Test failed"), shell.state.read_only(), batched_tx.tx, @@ -5481,7 +5490,7 @@ mod test_finalize_block { &verifiers, shell.vp_wasm_cache.clone(), ); - let parameters = ParametersVp { ctx }; + let parameters = ParametersVp::new(ctx); assert!( parameters .validate_tx(&batched_tx, &keys_changed, &verifiers) @@ -5492,7 +5501,7 @@ mod test_finalize_block { let mut req = FinalizeBlock::default(); req.header.time = { #[allow(clippy::disallowed_methods)] - namada::core::time::DateTimeUtc::now() + namada_sdk::time::DateTimeUtc::now() }; let current_decision_height = shell.get_current_decision_height(); if let Some(b) = shell.state.in_mem_mut().last_block.as_mut() { diff --git a/crates/node/src/shell/governance.rs b/crates/node/src/shell/governance.rs index 5e9ed2ef24..b1444f0627 100644 --- a/crates/node/src/shell/governance.rs +++ b/crates/node/src/shell/governance.rs @@ -1,34 +1,32 @@ -use namada::core::collections::HashMap; -use namada::core::encode; -use namada::core::storage::Epoch; -use namada::governance::event::GovernanceEvent; -use namada::governance::pgf::storage::keys as pgf_storage; -use namada::governance::pgf::storage::steward::StewardDetail; -use namada::governance::pgf::{storage as pgf, ADDRESS}; -use namada::governance::storage::proposal::{ +use namada_sdk::collections::HashMap; +use namada_sdk::events::extend::{ComposeEvent, Height, UserAccount}; +use namada_sdk::events::{EmitEvents, EventLevel}; +use namada_sdk::governance::event::GovernanceEvent; +use namada_sdk::governance::pgf::storage::keys as pgf_storage; +use namada_sdk::governance::pgf::storage::steward::StewardDetail; +use namada_sdk::governance::pgf::{storage as pgf, ADDRESS}; +use namada_sdk::governance::storage::proposal::{ AddRemove, PGFAction, PGFTarget, ProposalType, StoragePgfFunding, }; -use namada::governance::storage::{keys as gov_storage, load_proposals}; -use namada::governance::utils::{ +use namada_sdk::governance::storage::{keys as gov_storage, load_proposals}; +use namada_sdk::governance::utils::{ compute_proposal_result, ProposalVotes, TallyResult, TallyType, VotePower, }; -use namada::governance::{ +use namada_sdk::governance::{ storage as gov_api, ProposalVote, ADDRESS as gov_address, }; -use namada::ibc; -use namada::ledger::events::extend::{ComposeEvent, Height, UserAccount}; -use namada::proof_of_stake::bond_amount; -use namada::proof_of_stake::parameters::PosParams; -use namada::proof_of_stake::storage::{ - read_total_active_stake, validator_state_handle, +use namada_sdk::proof_of_stake::bond_amount; +use namada_sdk::proof_of_stake::parameters::PosParams; +use namada_sdk::proof_of_stake::storage::{ + read_total_active_stake, read_validator_stake, validator_state_handle, }; -use namada::proof_of_stake::types::{BondId, ValidatorState}; -use namada::sdk::events::{EmitEvents, EventLevel}; -use namada::state::StorageWrite; -use namada::token::event::{TokenEvent, TokenOperation}; -use namada::token::read_balance; -use namada::tx::{Code, Data}; -use namada_sdk::proof_of_stake::storage::read_validator_stake; +use namada_sdk::proof_of_stake::types::{BondId, ValidatorState}; +use namada_sdk::state::StorageWrite; +use namada_sdk::storage::Epoch; +use namada_sdk::token::event::{TokenEvent, TokenOperation}; +use namada_sdk::token::read_balance; +use namada_sdk::tx::{Code, Data}; +use namada_sdk::{encode, ibc}; use super::utils::force_read; use super::*; @@ -309,7 +307,7 @@ fn compute_proposal_votes( params: &PosParams, proposal_id: u64, epoch: Epoch, -) -> namada::state::StorageResult +) -> namada_sdk::state::StorageResult where S: StorageRead, { @@ -392,7 +390,7 @@ fn execute_default_proposal( shell: &mut Shell, id: u64, proposal_code: Vec, -) -> namada::state::StorageResult +) -> namada_sdk::state::StorageResult where D: DB + for<'iter> DBIter<'iter> + Sync + 'static, H: StorageHasher + Sync + 'static, diff --git a/crates/node/src/shell/init_chain.rs b/crates/node/src/shell/init_chain.rs index 457004ff2a..6a10c9b05c 100644 --- a/crates/node/src/shell/init_chain.rs +++ b/crates/node/src/shell/init_chain.rs @@ -5,19 +5,18 @@ use std::ops::ControlFlow; use masp_primitives::merkle_tree::CommitmentTree; use masp_primitives::sapling::Node; use masp_proofs::bls12_381; -use namada::account::protocol_pk_key; -use namada::core::collections::HashMap; -use namada::core::hash::Hash as CodeHash; -use namada::core::time::{TimeZone, Utc}; -use namada::ledger::parameters::Parameters; -use namada::ledger::{ibc, pos}; -use namada::proof_of_stake::BecomeValidator; -use namada::state::StorageWrite; -use namada::token::storage_key::masp_token_map_key; -use namada::token::{credit_tokens, write_denom}; -use namada::vm::validate_untrusted_wasm; +use namada_sdk::account::protocol_pk_key; +use namada_sdk::collections::HashMap; use namada_sdk::eth_bridge::EthBridgeStatus; -use namada_sdk::proof_of_stake::PosParams; +use namada_sdk::hash::Hash as CodeHash; +use namada_sdk::parameters::Parameters; +use namada_sdk::proof_of_stake::{self, BecomeValidator, PosParams}; +use namada_sdk::state::StorageWrite; +use namada_sdk::time::{TimeZone, Utc}; +use namada_sdk::token::storage_key::masp_token_map_key; +use namada_sdk::token::{credit_tokens, write_denom}; +use namada_sdk::{eth_bridge, ibc}; +use namada_vm::validate_untrusted_wasm; use super::*; use crate::config::genesis::chain::{ @@ -184,7 +183,7 @@ where let convert_anchor_key = token::storage_key::masp_convert_anchor_key(); self.state.write( &convert_anchor_key, - namada::core::hash::Hash( + namada_sdk::hash::Hash( bls12_381::Scalar::from( self.state.in_mem().conversion_state.tree.root(), ) @@ -246,7 +245,7 @@ where } else { self.state .write( - &namada::eth_bridge::storage::active_key(), + ð_bridge::storage::active_key(), EthBridgeStatus::Disabled, ) .unwrap(); @@ -265,7 +264,7 @@ where // PoS system depends on epoch being initialized let pos_params = genesis.get_pos_params(); let (current_epoch, _gas) = self.state.in_mem().get_current_epoch(); - pos::namada_proof_of_stake::init_genesis( + proof_of_stake::init_genesis( &mut self.state, &pos_params, current_epoch, @@ -291,13 +290,13 @@ where ); self.apply_genesis_txs_bonds(&genesis); - pos::namada_proof_of_stake::compute_and_store_total_consensus_stake( + proof_of_stake::compute_and_store_total_consensus_stake( &mut self.state, current_epoch, ) .expect("Could not compute total consensus stake at genesis"); // This has to be done after `apply_genesis_txs_validator_account` - pos::namada_proof_of_stake::copy_genesis_validator_sets( + proof_of_stake::copy_genesis_validator_sets( &mut self.state, &pos_params, current_epoch, @@ -315,7 +314,7 @@ where genesis: &genesis::chain::Finalized, vp_cache: &mut HashMap>, ) -> ControlFlow<(), Vec> { - use namada::core::collections::hash_map::Entry; + use namada_sdk::collections::hash_map::Entry; let Some(vp_filename) = self .validate( genesis @@ -469,7 +468,7 @@ where } = token; // associate a token with its denomination. write_denom(&mut self.state, address, *denom).unwrap(); - namada::token::write_params( + namada_sdk::token::write_params( masp_params, &mut self.state, address, @@ -514,7 +513,7 @@ where for (owner, balance) in balances { if let genesis::GenesisAddress::PublicKey(pk) = owner { - namada::account::init_account_storage( + namada_sdk::account::init_account_storage( &mut self.state, &owner.address(), std::slice::from_ref(&pk.raw), @@ -569,7 +568,7 @@ where let public_keys: Vec<_> = public_keys.iter().map(|pk| pk.raw.clone()).collect(); - namada::account::init_account_storage( + namada_sdk::account::init_account_storage( &mut self.state, address, &public_keys, @@ -587,7 +586,7 @@ where genesis: &genesis::chain::Finalized, vp_cache: &mut HashMap>, params: &PosParams, - current_epoch: namada::core::storage::Epoch, + current_epoch: namada_sdk::storage::Epoch, ) -> ControlFlow<()> { if let Some(txs) = genesis.transactions.validator_account.as_ref() { for FinalizedValidatorAccountTx { @@ -628,7 +627,7 @@ where .write(&protocol_pk_key(address), &protocol_key.pk.raw) .expect("Unable to set genesis user protocol public key"); - if let Err(err) = pos::namada_proof_of_stake::become_validator( + if let Err(err) = proof_of_stake::become_validator( &mut self.state, BecomeValidator { params, @@ -676,7 +675,7 @@ where amount, ); - if let Err(err) = pos::namada_proof_of_stake::bond_tokens( + if let Err(err) = proof_of_stake::bond_tokens( &mut self.state, Some(&source.address()), validator, @@ -969,8 +968,8 @@ impl Policy { mod test { use std::str::FromStr; - use namada::core::string_encoding::StringEncoded; use namada_apps_lib::wallet::defaults; + use namada_sdk::string_encoding::StringEncoded; use namada_sdk::wallet::alias::Alias; use super::*; @@ -1166,7 +1165,7 @@ mod test { let pos_params = genesis.get_pos_params(); let (current_epoch, _gas) = initializer.state.in_mem().get_current_epoch(); - pos::namada_proof_of_stake::init_genesis( + proof_of_stake::init_genesis( &mut initializer.state, &pos_params, current_epoch, diff --git a/crates/node/src/shell/mod.rs b/crates/node/src/shell/mod.rs index 4d82c6f10d..5a19ba7366 100644 --- a/crates/node/src/shell/mod.rs +++ b/crates/node/src/shell/mod.rs @@ -10,11 +10,12 @@ mod finalize_block; mod governance; mod init_chain; pub use init_chain::InitChainValidation; -use namada::vm::wasm::run::check_tx_allowed; use namada_apps_lib::config::NodeLocalConfig; use namada_sdk::state::StateRead; +use namada_vm::wasm::run::check_tx_allowed; pub mod prepare_proposal; -use namada::state::State; +use namada_sdk::ibc; +use namada_sdk::state::State; pub mod process_proposal; pub(super) mod queries; mod snapshots; @@ -34,42 +35,40 @@ use std::rc::Rc; use borsh::BorshDeserialize; use borsh_ext::BorshSerializeExt; -use namada::core::address::Address; -use namada::core::chain::ChainId; -use namada::core::ethereum_events::EthereumEvent; -use namada::core::hints; -use namada::core::key::*; -use namada::core::storage::{BlockHeight, Key, TxIndex}; -use namada::core::time::DateTimeUtc; -use namada::ethereum_bridge::protocol::validation::bridge_pool_roots::validate_bp_roots_vext; -use namada::ethereum_bridge::protocol::validation::ethereum_events::validate_eth_events_vext; -use namada::ethereum_bridge::protocol::validation::validator_set_update::validate_valset_upd_vext; -use namada::ledger::events::log::EventLog; -use namada::ledger::gas::{Gas, TxGasMeter}; -use namada::ledger::pos::namada_proof_of_stake::types::{ +use namada_apps_lib::wallet::{self, ValidatorData, ValidatorKeys}; +use namada_sdk::address::Address; +use namada_sdk::chain::ChainId; +use namada_sdk::eth_bridge::protocol::validation::bridge_pool_roots::validate_bp_roots_vext; +use namada_sdk::eth_bridge::protocol::validation::ethereum_events::validate_eth_events_vext; +use namada_sdk::eth_bridge::protocol::validation::validator_set_update::validate_valset_upd_vext; +use namada_sdk::eth_bridge::{EthBridgeQueries, EthereumOracleConfig}; +use namada_sdk::ethereum_events::EthereumEvent; +use namada_sdk::events::log::EventLog; +use namada_sdk::gas::{Gas, TxGasMeter}; +use namada_sdk::key::*; +use namada_sdk::migrations::ScheduledMigration; +use namada_sdk::parameters::{get_gas_scale, validate_tx_bytes}; +use namada_sdk::proof_of_stake::storage::read_pos_params; +use namada_sdk::proof_of_stake::types::{ ConsensusValidator, ValidatorSetUpdate, }; -use namada::ledger::protocol::ShellParams; -use namada::ledger::{parameters, protocol}; -use namada::parameters::{get_gas_scale, validate_tx_bytes}; -use namada::proof_of_stake::storage::read_pos_params; -use namada::state::tx_queue::ExpiredTx; -use namada::state::{ +use namada_sdk::state::tx_queue::ExpiredTx; +use namada_sdk::state::{ DBIter, FullAccessState, Sha256Hasher, StorageHasher, StorageRead, TempWlState, WlState, DB, EPOCH_SWITCH_BLOCKS_DELAY, }; -use namada::token; -pub use namada::tx::data::ResultCode; -use namada::tx::data::{TxType, WrapperTx}; -use namada::tx::{Section, Tx}; -use namada::vm::wasm::{TxCache, VpCache}; -use namada::vm::{WasmCacheAccess, WasmCacheRwAccess}; -use namada::vote_ext::EthereumTxData; -use namada_apps_lib::wallet::{self, ValidatorData, ValidatorKeys}; -use namada_sdk::eth_bridge::{EthBridgeQueries, EthereumOracleConfig}; -use namada_sdk::migrations; -use namada_sdk::migrations::ScheduledMigration; +use namada_sdk::storage::{BlockHeight, Key, TxIndex}; use namada_sdk::tendermint::AppHash; +use namada_sdk::time::DateTimeUtc; +pub use namada_sdk::tx::data::ResultCode; +use namada_sdk::tx::data::{TxType, WrapperTx}; +use namada_sdk::tx::{Section, Tx}; +use namada_sdk::{ + eth_bridge, hints, migrations, parameters, proof_of_stake, token, +}; +use namada_vm::wasm::{TxCache, VpCache}; +use namada_vm::{WasmCacheAccess, WasmCacheRwAccess}; +use namada_vote_ext::EthereumTxData; use thiserror::Error; use tokio::sync::mpsc::{Receiver, UnboundedSender}; @@ -78,10 +77,11 @@ use crate::config::{self, genesis, TendermintMode, ValidatorLocalConfig}; use crate::facade::tendermint::v0_37::abci::{request, response}; use crate::facade::tendermint::{self, validator}; use crate::facade::tendermint_proto::v0_37::crypto::public_key; +use crate::protocol::ShellParams; use crate::shims::abcipp_shim_types::shim; use crate::shims::abcipp_shim_types::shim::response::TxResult; use crate::shims::abcipp_shim_types::shim::TakeSnapshot; -use crate::{storage, tendermint_node}; +use crate::{protocol, storage, tendermint_node}; fn key_to_tendermint( pk: &common::PublicKey, @@ -103,7 +103,7 @@ pub enum Error { #[error("chain ID mismatch: {0}")] ChainId(String), #[error("Error decoding a transaction from bytes: {0}")] - TxDecoding(namada::tx::DecodeError), + TxDecoding(namada_sdk::tx::DecodeError), #[error("Error trying to apply a transaction: {0}")] TxApply(protocol::Error), #[error("{0}")] @@ -121,7 +121,7 @@ pub enum Error { #[error("Error loading wasm: {0}")] LoadingWasm(String), #[error("Error reading from or writing to storage: {0}")] - Storage(#[from] namada::state::StorageError), + Storage(#[from] namada_sdk::state::StorageError), #[error("Transaction replay attempt: {0}")] ReplayAttempt(String), #[error("Error with snapshots: {0}")] @@ -171,7 +171,7 @@ pub fn rollback(config: config::Ledger) -> Result<()> { tracing::info!("Rollback Namada state"); db.rollback(tendermint_block_height) - .map_err(|e| Error::Storage(namada::state::StorageError::new(e))) + .map_err(|e| Error::Storage(namada_sdk::state::StorageError::new(e))) } #[derive(Debug)] @@ -383,8 +383,8 @@ pub fn is_key_diff_storable(key: &namada_sdk::storage::Key) -> bool { && *key != token::storage_key::masp_token_map_key() && *key != token::storage_key::masp_assets_hash_key() && !token::storage_key::is_masp_commitment_anchor_key(key) - || namada::ibc::storage::is_ibc_counter_key(key) - || namada::proof_of_stake::storage_key::is_delegation_targets_key(key)) + || ibc::storage::is_ibc_counter_key(key) + || proof_of_stake::storage_key::is_delegation_targets_key(key)) } /// Channels for communicating with an Ethereum oracle. @@ -443,7 +443,7 @@ where any(test, feature = "testing", feature = "benches"), not(feature = "integration"), ))] - let native_token = namada::address::testing::nam(); + let native_token = namada_sdk::address::testing::nam(); // ... Otherwise, look it up from the genesis file #[cfg(not(all( any(test, feature = "testing", feature = "benches"), @@ -851,7 +851,7 @@ where .expect("Validators should have protocol keys"); let signed_tx = EthereumTxData::EthEventsVext( - namada::vote_ext::ethereum_events::SignedVext(vote_extension), + namada_vote_ext::ethereum_events::SignedVext(vote_extension), ) .sign(protocol_key, self.chain_id.clone()) .to_bytes(); @@ -886,7 +886,7 @@ where // initialized yet. let has_key = self .state - .has_key(&namada::eth_bridge::storage::active_key()) + .has_key(ð_bridge::storage::active_key()) .expect( "We should always be able to check whether a key exists \ in storage or not", @@ -906,9 +906,7 @@ where return; }; let active = if !self.state.ethbridge_queries().is_bridge_active() { - if !changed_keys - .contains(&namada::eth_bridge::storage::active_key()) - { + if !changed_keys.contains(ð_bridge::storage::active_key()) { tracing::debug!( "Not starting oracle as the Ethereum bridge is \ disabled" @@ -935,7 +933,7 @@ where "Found Ethereum height from which the Ethereum oracle should \ be updated" ); - let config = namada::eth_bridge::oracle::config::Config { + let config = eth_bridge::oracle::config::Config { min_confirmations: config.min_confirmations.into(), bridge_contract: config.contracts.bridge.address, start_block, @@ -974,8 +972,8 @@ where tx_bytes: &[u8], r#_type: MempoolTxType, ) -> response::CheckTx { - use namada::tx::data::protocol::ProtocolTxType; - use namada::vote_ext::ethereum_tx_data_variants; + use namada_sdk::tx::data::protocol::ProtocolTxType; + use namada_vote_ext::ethereum_tx_data_variants; let mut response = response::CheckTx::default(); @@ -1182,9 +1180,7 @@ where // Max block gas let block_gas_limit: Gas = Gas::from_whole_units( - namada::parameters::get_max_block_gas(&self.state) - .unwrap() - .into(), + parameters::get_max_block_gas(&self.state).unwrap().into(), gas_scale, ) .expect("Gas limit from parameter must not overflow"); @@ -1242,11 +1238,18 @@ where } } + // This is safe as neither the inner `db` nor `in_mem` are + // actually mutable, only the `write_log` which is owned by + // the `TempWlState` struct. The `TempWlState` will be dropped + // before any other ABCI request is processed. + let mut temp_state = + unsafe { self.state.with_static_temp_write_log() }; + // Validate wrapper fees if let Err(e) = mempool_fee_check( &mut ShellParams::new( &RefCell::new(gas_meter), - &mut self.state.with_temp_write_log(), + &mut temp_state, &mut self.vp_wasm_cache.clone(), &mut self.tx_wasm_cache.clone(), ), @@ -1280,21 +1283,18 @@ where // because we're using domain types in InitChain, but FinalizeBlock is // shimmed with a different old type. The joy... mut validator_conv: F, - ) -> namada::state::StorageResult> + ) -> namada_sdk::state::StorageResult> where F: FnMut(common::PublicKey, i64) -> V, { - use namada::ledger::pos::namada_proof_of_stake; - let (current_epoch, _gas) = self.state.in_mem().get_current_epoch(); - let pos_params = - namada_proof_of_stake::storage::read_pos_params(&self.state) - .expect("Could not find the PoS parameters"); + let pos_params = proof_of_stake::storage::read_pos_params(&self.state) + .expect("Could not find the PoS parameters"); let validator_set_update_fn = if is_genesis { - namada_proof_of_stake::genesis_validator_set_tendermint + proof_of_stake::genesis_validator_set_tendermint } else { - namada_proof_of_stake::validator_set_update::validator_set_update_comet + proof_of_stake::validator_set_update::validator_set_update_comet }; validator_set_update_fn( @@ -1376,7 +1376,7 @@ where // Perform the fee check in mempool fn mempool_fee_check( - shell_params: &mut ShellParams<'_, TempWlState<'_, D, H>, D, H, CA>, + shell_params: &mut ShellParams<'_, TempWlState<'static, D, H>, D, H, CA>, tx: &Tx, wrapper: &WrapperTx, ) -> Result<()> @@ -1385,15 +1385,13 @@ where H: StorageHasher + Sync + 'static, CA: 'static + WasmCacheAccess + Sync, { - let minimum_gas_price = namada::ledger::parameters::read_gas_cost( - shell_params.state, - &wrapper.fee.token, - ) - .expect("Must be able to read gas cost parameter") - .ok_or(Error::TxApply(protocol::Error::FeeError(format!( - "The provided {} token is not allowed for fee payment", - wrapper.fee.token - ))))?; + let minimum_gas_price = + parameters::read_gas_cost(shell_params.state, &wrapper.fee.token) + .expect("Must be able to read gas cost parameter") + .ok_or(Error::TxApply(protocol::Error::FeeError(format!( + "The provided {} token is not allowed for fee payment", + wrapper.fee.token + ))))?; fee_data_check(wrapper, minimum_gas_price, shell_params)?; protocol::check_fees(shell_params, tx, wrapper) @@ -1447,17 +1445,17 @@ mod test_utils { use std::ops::{Deref, DerefMut}; use data_encoding::HEXUPPER; - use namada::core::ethereum_events::Uint; - use namada::core::hash::Hash; - use namada::core::keccak::KeccakHash; - use namada::core::key::*; - use namada::core::storage::{Epoch, Header}; - use namada::ledger::events::Event; - use namada::proof_of_stake::parameters::PosParams; - use namada::proof_of_stake::storage::validator_consensus_key_handle; - use namada::state::mockdb::MockDB; - use namada::state::{LastBlock, StorageWrite}; - use namada::tendermint::abci::types::VoteInfo; + use namada_sdk::ethereum_events::Uint; + use namada_sdk::events::Event; + use namada_sdk::hash::Hash; + use namada_sdk::keccak::KeccakHash; + use namada_sdk::key::*; + use namada_sdk::proof_of_stake::parameters::PosParams; + use namada_sdk::proof_of_stake::storage::validator_consensus_key_handle; + use namada_sdk::state::mockdb::MockDB; + use namada_sdk::state::{LastBlock, StorageWrite}; + use namada_sdk::storage::{Epoch, Header}; + use namada_sdk::tendermint::abci::types::VoteInfo; use tempfile::tempdir; use tokio::sync::mpsc::{Sender, UnboundedReceiver}; @@ -1550,7 +1548,7 @@ mod test_utils { /// Get the default bridge pool vext bytes to be signed. pub fn get_bp_bytes_to_sign() -> KeccakHash { - use namada::core::keccak::{Hasher, Keccak}; + use namada_sdk::keccak::{Hasher, Keccak}; let root = [0; 32]; let nonce = Uint::from(0).to_bytes(); @@ -1931,8 +1929,8 @@ mod test_utils { /// Set the Ethereum bridge to be inactive pub(super) fn deactivate_bridge(shell: &mut TestShell) { - use namada::eth_bridge::storage::active_key; - use namada::eth_bridge::storage::eth_bridge_queries::EthBridgeStatus; + use eth_bridge::storage::active_key; + use eth_bridge::storage::eth_bridge_queries::EthBridgeStatus; shell .state .write(&active_key(), EthBridgeStatus::Disabled) @@ -1987,17 +1985,17 @@ mod test_utils { #[cfg(test)] mod shell_tests { - use namada::core::storage::Epoch; - use namada::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; - use namada::token::read_denom; - use namada::tx::data::protocol::{ProtocolTx, ProtocolTxType}; - use namada::tx::data::Fee; - use namada::tx::{Authorization, Code, Data, Signed}; - use namada::vote_ext::{ + use eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; + use namada_sdk::address; + use namada_sdk::storage::Epoch; + use namada_sdk::token::read_denom; + use namada_sdk::tx::data::protocol::{ProtocolTx, ProtocolTxType}; + use namada_sdk::tx::data::Fee; + use namada_sdk::tx::{Authorization, Code, Data, Signed}; + use namada_vote_ext::{ bridge_pool_roots, ethereum_events, ethereum_tx_data_variants, }; - use namada::{address, replay_protection}; - use wallet; + use {namada_replay_protection as replay_protection, wallet}; use super::*; use crate::shell::token::DenominatedAmount; @@ -2102,7 +2100,7 @@ mod shell_tests { /// not validated by `CheckTx`. #[test] fn test_outdated_nonce_mempool_validate() { - use namada::core::storage::InnerEthEventsQueue; + use namada_sdk::storage::InnerEthEventsQueue; const LAST_HEIGHT: BlockHeight = BlockHeight(3); @@ -2457,7 +2455,7 @@ mod shell_tests { ))); // Write wrapper hash to storage - let mut batch = namada::state::testing::TestState::batch(); + let mut batch = namada_sdk::state::testing::TestState::batch(); let wrapper_hash = wrapper.header_hash(); let wrapper_hash_key = replay_protection::current_key(&wrapper_hash); shell diff --git a/crates/node/src/shell/prepare_proposal.rs b/crates/node/src/shell/prepare_proposal.rs index 4d77d522be..b8b80c3608 100644 --- a/crates/node/src/shell/prepare_proposal.rs +++ b/crates/node/src/shell/prepare_proposal.rs @@ -2,18 +2,17 @@ use std::cell::RefCell; -use namada::core::address::Address; -use namada::core::key::tm_raw_hash_to_string; -use namada::gas::TxGasMeter; -use namada::ledger::protocol::{self, ShellParams}; -use namada::parameters::get_gas_scale; -use namada::proof_of_stake::storage::find_validator_by_raw_hash; -use namada::state::{DBIter, StorageHasher, TempWlState, TxIndex, DB}; -use namada::token::{Amount, DenominatedAmount}; -use namada::tx::data::WrapperTx; -use namada::tx::Tx; -use namada::vm::wasm::{TxCache, VpCache}; -use namada::vm::WasmCacheAccess; +use namada_sdk::address::Address; +use namada_sdk::gas::TxGasMeter; +use namada_sdk::key::tm_raw_hash_to_string; +use namada_sdk::parameters::get_gas_scale; +use namada_sdk::proof_of_stake::storage::find_validator_by_raw_hash; +use namada_sdk::state::{DBIter, StorageHasher, TempWlState, TxIndex, DB}; +use namada_sdk::token::{Amount, DenominatedAmount}; +use namada_sdk::tx::data::WrapperTx; +use namada_sdk::tx::Tx; +use namada_vm::wasm::{TxCache, VpCache}; +use namada_vm::WasmCacheAccess; use super::super::*; use super::block_alloc::states::{ @@ -24,6 +23,7 @@ use super::block_alloc::{AllocFailure, BlockAllocator, BlockResources}; use crate::config::ValidatorLocalConfig; use crate::facade::tendermint_proto::google::protobuf::Timestamp; use crate::facade::tendermint_proto::v0_37::abci::RequestPrepareProposal; +use crate::protocol::{self, ShellParams}; use crate::shell::ShellMode; use crate::shims::abcipp_shim_types::shim::{response, TxBytes}; @@ -118,7 +118,11 @@ where // valid because of mempool check TryInto::::try_into(block_time).ok() }); - let mut temp_state = self.state.with_temp_write_log(); + // This is safe as neither the inner `db` nor `in_mem` are + // actually mutable, only the `write_log` which is owned by + // the `TempWlState` struct. The `TempWlState` will be dropped + // before any other ABCI request is processed. + let mut temp_state = unsafe { self.state.with_static_temp_write_log() }; let mut vp_wasm_cache = self.vp_wasm_cache.clone(); let mut tx_wasm_cache = self.tx_wasm_cache.clone(); @@ -126,7 +130,17 @@ where .iter() .enumerate() .filter_map(|(tx_index, tx_bytes)| { - match validate_wrapper_bytes(tx_bytes, &TxIndex::must_from_usize(tx_index),block_time, block_proposer, proposer_local_config, &mut temp_state, &mut vp_wasm_cache, &mut tx_wasm_cache, ) { + let result = validate_wrapper_bytes( + tx_bytes, + &TxIndex::must_from_usize(tx_index), + block_time, + block_proposer, + proposer_local_config, + &mut temp_state, + &mut vp_wasm_cache, + &mut tx_wasm_cache + ); + match result { Ok(gas) => { temp_state.write_log_mut().commit_batch(); Some((tx_bytes.to_owned(), gas)) @@ -266,7 +280,7 @@ fn validate_wrapper_bytes( block_time: Option, block_proposer: &Address, proposer_local_config: Option<&ValidatorLocalConfig>, - temp_state: &mut TempWlState<'_, D, H>, + temp_state: &mut TempWlState<'static, D, H>, vp_wasm_cache: &mut VpCache, tx_wasm_cache: &mut TxCache, ) -> Result @@ -324,7 +338,7 @@ fn prepare_proposal_fee_check( tx_index: &TxIndex, proposer: &Address, proposer_local_config: Option<&ValidatorLocalConfig>, - shell_params: &mut ShellParams<'_, TempWlState<'_, D, H>, D, H, CA>, + shell_params: &mut ShellParams<'_, TempWlState<'static, D, H>, D, H, CA>, ) -> Result<(), Error> where D: DB + for<'iter> DBIter<'iter> + Sync + 'static, @@ -353,7 +367,7 @@ where H: StorageHasher + Sync + 'static, { let consensus_min_gas_price = - namada::ledger::parameters::read_gas_cost(temp_state, fee_token) + namada_sdk::parameters::read_gas_cost(temp_state, fee_token) .expect("Must be able to read gas cost parameter") .ok_or_else(|| { Error::TxApply(protocol::Error::FeeError(format!( @@ -403,25 +417,23 @@ where mod test_prepare_proposal { use std::collections::BTreeSet; - use namada::core::address; - use namada::core::ethereum_events::EthereumEvent; - use namada::core::key::RefTo; - use namada::core::storage::{BlockHeight, InnerEthEventsQueue}; - use namada::ledger::pos::PosQueries; - use namada::proof_of_stake::storage::{ + use namada_apps_lib::wallet; + use namada_replay_protection as replay_protection; + use namada_sdk::ethereum_events::EthereumEvent; + use namada_sdk::key::RefTo; + use namada_sdk::proof_of_stake::storage::{ consensus_validator_set_handle, read_consensus_validator_set_addresses_with_stake, }; - use namada::proof_of_stake::types::WeightedValidator; - use namada::proof_of_stake::Epoch; - use namada::state::collections::lazy_map::{NestedSubKey, SubKey}; - use namada::token::read_denom; - use namada::tx::data::{Fee, TxType}; - use namada::tx::{Authorization, Code, Data, Section, Signed}; - use namada::vote_ext::{ethereum_events, ethereum_tx_data_variants}; - use namada::{replay_protection, token}; - use namada_apps_lib::wallet; - use namada_sdk::storage::StorageWrite; + use namada_sdk::proof_of_stake::types::WeightedValidator; + use namada_sdk::proof_of_stake::{Epoch, PosQueries}; + use namada_sdk::state::collections::lazy_map::{NestedSubKey, SubKey}; + use namada_sdk::storage::{BlockHeight, InnerEthEventsQueue, StorageWrite}; + use namada_sdk::token::read_denom; + use namada_sdk::tx::data::{Fee, TxType}; + use namada_sdk::tx::{Authorization, Code, Data, Section, Signed}; + use namada_sdk::{address, token}; + use namada_vote_ext::{ethereum_events, ethereum_tx_data_variants}; use super::*; use crate::shell::test_utils::{ @@ -592,7 +604,7 @@ mod test_prepare_proposal { /// behaves as expected, considering <= 2/3 voting power. #[test] fn test_prepare_proposal_vext_insufficient_voting_power() { - use namada::tendermint::abci::types::{Validator, VoteInfo}; + use namada_sdk::tendermint::abci::types::{Validator, VoteInfo}; const FIRST_HEIGHT: BlockHeight = BlockHeight(1); const LAST_HEIGHT: BlockHeight = BlockHeight(FIRST_HEIGHT.0 + 11); @@ -687,10 +699,11 @@ mod test_prepare_proposal { ]; let req = FinalizeBlock { proposer_address: pkh1.to_vec(), - decided_last_commit: namada::tendermint::abci::types::CommitInfo { - round: 0u8.into(), - votes, - }, + decided_last_commit: + crate::facade::tendermint::abci::types::CommitInfo { + round: 0u8.into(), + votes, + }, ..Default::default() }; shell.start_new_epoch(Some(req)); @@ -941,7 +954,7 @@ mod test_prepare_proposal { #[allow(clippy::disallowed_methods)] let time = DateTimeUtc::now(); let block_time = - namada::core::tendermint_proto::google::protobuf::Timestamp { + namada_sdk::tendermint_proto::google::protobuf::Timestamp { seconds: time.0.timestamp(), nanos: time.0.timestamp_subsec_nanos() as i32, }; @@ -963,7 +976,7 @@ mod test_prepare_proposal { let (shell, _recv, _, _) = test_utils::setup(); let block_gas_limit = - namada::parameters::get_max_block_gas(&shell.state).unwrap(); + namada_sdk::parameters::get_max_block_gas(&shell.state).unwrap(); let keypair = gen_keypair(); let wrapper = WrapperTx::new( @@ -1047,9 +1060,9 @@ mod test_prepare_proposal { { // Remove the allowed btc *validator_local_config = Some(ValidatorLocalConfig { - accepted_gas_tokens: namada::core::collections::HashMap::from( - [(namada::core::address::testing::nam(), Amount::from(1))], - ), + accepted_gas_tokens: namada_sdk::collections::HashMap::from([ + (namada_sdk::address::testing::nam(), Amount::from(1)), + ]), }); } @@ -1152,12 +1165,9 @@ mod test_prepare_proposal { { // Remove btc and increase minimum for nam *validator_local_config = Some(ValidatorLocalConfig { - accepted_gas_tokens: namada::core::collections::HashMap::from( - [( - namada::core::address::testing::nam(), - Amount::from(100), - )], - ), + accepted_gas_tokens: namada_sdk::collections::HashMap::from([ + (namada_sdk::address::testing::nam(), Amount::from(100)), + ]), }); } @@ -1429,19 +1439,18 @@ mod test_prepare_proposal { let temp_state = shell.state.with_temp_write_log(); let validator_min_gas_price = Amount::zero(); - let consensus_min_gas_price = - namada::ledger::parameters::read_gas_cost( - &temp_state, - &shell.state.in_mem().native_token, - ) - .expect("Must be able to read gas cost parameter") - .expect("NAM should be an allowed gas token"); + let consensus_min_gas_price = namada_sdk::parameters::read_gas_cost( + &temp_state, + &shell.state.in_mem().native_token, + ) + .expect("Must be able to read gas cost parameter") + .expect("NAM should be an allowed gas token"); assert!(validator_min_gas_price < consensus_min_gas_price); let config = ValidatorLocalConfig { accepted_gas_tokens: { - let mut m = namada::core::collections::HashMap::new(); + let mut m = namada_sdk::collections::HashMap::new(); m.insert( shell.state.in_mem().native_token.clone(), validator_min_gas_price, diff --git a/crates/node/src/shell/process_proposal.rs b/crates/node/src/shell/process_proposal.rs index 4adfe05a3e..de060e2f11 100644 --- a/crates/node/src/shell/process_proposal.rs +++ b/crates/node/src/shell/process_proposal.rs @@ -2,10 +2,11 @@ //! and [`RevertProposal`] ABCI++ methods for the Shell use data_encoding::HEXUPPER; -use namada::ledger::pos::PosQueries; -use namada::proof_of_stake::storage::find_validator_by_raw_hash; -use namada::tx::data::protocol::ProtocolTxType; -use namada::vote_ext::ethereum_tx_data_variants; +use namada_sdk::parameters; +use namada_sdk::proof_of_stake::storage::find_validator_by_raw_hash; +use namada_sdk::proof_of_stake::PosQueries; +use namada_sdk::tx::data::protocol::ProtocolTxType; +use namada_vote_ext::ethereum_tx_data_variants; use super::block_alloc::{BlockGas, BlockSpace}; use super::*; @@ -32,8 +33,7 @@ where fn from(state: &WlState) -> Self { let max_proposal_bytes = state.pos_queries().get_max_proposal_bytes().get(); - let max_block_gas = - namada::parameters::get_max_block_gas(state).unwrap(); + let max_block_gas = parameters::get_max_block_gas(state).unwrap(); let user_gas = TxBin::init(max_block_gas); let txs_bin = TxBin::init(max_proposal_bytes); @@ -129,7 +129,11 @@ where block_time: DateTimeUtc, block_proposer: &Address, ) -> Vec { - let mut temp_state = self.state.with_temp_write_log(); + // This is safe as neither the inner `db` nor `in_mem` are + // actually mutable, only the `write_log` which is owned by + // the `TempWlState` struct. The `TempWlState` will be dropped + // before any other ABCI request is processed. + let mut temp_state = unsafe { self.state.with_static_temp_write_log() }; let mut metadata = ValidationMeta::from(self.state.read_only()); let mut vp_wasm_cache = self.vp_wasm_cache.clone(); let mut tx_wasm_cache = self.tx_wasm_cache.clone(); @@ -196,7 +200,7 @@ where tx_bytes: &[u8], tx_index: &TxIndex, metadata: &mut ValidationMeta, - temp_state: &mut TempWlState<'_, D, H>, + temp_state: &mut TempWlState<'static, D, H>, block_time: DateTimeUtc, vp_wasm_cache: &mut VpCache, tx_wasm_cache: &mut TxCache, @@ -532,22 +536,20 @@ fn process_proposal_fee_check( tx: &Tx, tx_index: &TxIndex, proposer: &Address, - shell_params: &mut ShellParams<'_, TempWlState<'_, D, H>, D, H, CA>, + shell_params: &mut ShellParams<'_, TempWlState<'static, D, H>, D, H, CA>, ) -> Result<()> where D: DB + for<'iter> DBIter<'iter> + Sync + 'static, H: StorageHasher + Sync + 'static, CA: 'static + WasmCacheAccess + Sync, { - let minimum_gas_price = namada::ledger::parameters::read_gas_cost( - shell_params.state, - &wrapper.fee.token, - ) - .expect("Must be able to read gas cost parameter") - .ok_or(Error::TxApply(protocol::Error::FeeError(format!( - "The provided {} token is not allowed for fee payment", - wrapper.fee.token - ))))?; + let minimum_gas_price = + parameters::read_gas_cost(shell_params.state, &wrapper.fee.token) + .expect("Must be able to read gas cost parameter") + .ok_or(Error::TxApply(protocol::Error::FeeError(format!( + "The provided {} token is not allowed for fee payment", + wrapper.fee.token + ))))?; fee_data_check(wrapper, minimum_gas_price, shell_params)?; @@ -561,19 +563,20 @@ where // process proposals #[cfg(test)] mod test_process_proposal { - use namada::core::key::*; - use namada::eth_bridge::storage::eth_bridge_queries::{ + use namada_apps_lib::wallet; + use namada_replay_protection as replay_protection; + use namada_sdk::address; + use namada_sdk::eth_bridge::storage::eth_bridge_queries::{ is_bridge_comptime_enabled, EthBridgeQueries, }; - use namada::state::StorageWrite; - use namada::token::{read_denom, Amount, DenominatedAmount}; - use namada::tx::data::Fee; - use namada::tx::{Authorization, Code, Data, Signed}; - use namada::vote_ext::{ + use namada_sdk::key::*; + use namada_sdk::state::StorageWrite; + use namada_sdk::token::{read_denom, Amount, DenominatedAmount}; + use namada_sdk::tx::data::Fee; + use namada_sdk::tx::{Authorization, Code, Data, Signed}; + use namada_vote_ext::{ bridge_pool_roots, ethereum_events, validator_set_update, }; - use namada::{address, replay_protection}; - use namada_apps_lib::wallet; use super::*; use crate::shell::test_utils::{ @@ -1167,7 +1170,7 @@ mod test_process_proposal { ))); // Write wrapper hash to storage - let mut batch = namada::state::testing::TestState::batch(); + let mut batch = namada_sdk::state::testing::TestState::batch(); let wrapper_unsigned_hash = wrapper.header_hash(); let hash_key = replay_protection::current_key(&wrapper_unsigned_hash); shell @@ -1287,7 +1290,7 @@ mod test_process_proposal { ))); // Write inner hash to storage - let mut batch = namada::state::testing::TestState::batch(); + let mut batch = namada_sdk::state::testing::TestState::batch(); let hash_key = replay_protection::current_key(&wrapper.raw_header_hash()); shell @@ -1480,7 +1483,7 @@ mod test_process_proposal { let (shell, _recv, _, _) = test_utils::setup(); let block_gas_limit = - namada::parameters::get_max_block_gas(&shell.state).unwrap(); + parameters::get_max_block_gas(&shell.state).unwrap(); let keypair = super::test_utils::gen_keypair(); let mut wrapper = @@ -1734,7 +1737,7 @@ mod test_process_proposal { /// Test max tx bytes parameter in ProcessProposal #[test] fn test_max_tx_bytes_process_proposal() { - use namada::ledger::parameters::storage::get_max_tx_bytes_key; + use parameters::storage::get_max_tx_bytes_key; let (shell, _recv, _, _) = test_utils::setup_at_height(3u64); let max_tx_bytes: u32 = { @@ -1801,7 +1804,7 @@ mod test_process_proposal { /// not validated by `ProcessProposal`. #[test] fn test_outdated_nonce_process_proposal() { - use namada::core::storage::InnerEthEventsQueue; + use namada_sdk::storage::InnerEthEventsQueue; const LAST_HEIGHT: BlockHeight = BlockHeight(3); diff --git a/crates/node/src/shell/queries.rs b/crates/node/src/shell/queries.rs index 0269f36602..bad7126e86 100644 --- a/crates/node/src/shell/queries.rs +++ b/crates/node/src/shell/queries.rs @@ -1,9 +1,9 @@ //! Shell methods for querying state -use namada::ledger::dry_run_tx; -use namada::ledger::queries::{RequestCtx, ResponseQuery, RPC}; +use namada_sdk::queries::{RequestCtx, ResponseQuery, RPC}; use super::*; +use crate::dry_run_tx; impl Shell where @@ -15,19 +15,29 @@ where /// the default if `path` is not a supported string. /// INVARIANT: This method must be stateless. pub fn query(&self, query: request::Query) -> response::Query { - let ctx = RequestCtx { - state: self.state.read_only(), - event_log: self.event_log(), - vp_wasm_cache: self.vp_wasm_cache.read_only(), - tx_wasm_cache: self.tx_wasm_cache.read_only(), - storage_read_past_height_limit: self.storage_read_past_height_limit, - }; - // Invoke the root RPC handler - returns borsh-encoded data on success let result = if query.path == RPC.shell().dry_run_tx_path() { - dry_run_tx(ctx, &query) + dry_run_tx( + // This is safe as neither the inner `db` nor `in_mem` are + // actually mutable, only the `write_log` which is owned by + // the `TempWlState` struct. The `TempWlState` will be dropped + // right after dry-run and before any other ABCI request is + // processed. + unsafe { self.state.read_only().with_static_temp_write_log() }, + self.vp_wasm_cache.read_only(), + self.tx_wasm_cache.read_only(), + &query, + ) } else { - namada::ledger::queries::handle_path(ctx, &query) + let ctx = RequestCtx { + state: self.state.read_only(), + event_log: self.event_log(), + vp_wasm_cache: self.vp_wasm_cache.read_only(), + tx_wasm_cache: self.tx_wasm_cache.read_only(), + storage_read_past_height_limit: self + .storage_read_past_height_limit, + }; + namada_sdk::queries::handle_path(ctx, &query) }; match result { Ok(ResponseQuery { @@ -64,19 +74,19 @@ where } } -// NOTE: we are testing `namada::ledger::queries_ext`, +// NOTE: we are testing `namada_sdk::queries_ext`, // which is not possible from `namada` since we do not have // access to the `Shell` there #[allow(clippy::cast_possible_truncation)] #[cfg(test)] mod test_queries { - use namada::core::storage::Epoch; - use namada::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; - use namada::ledger::pos::PosQueries; - use namada::proof_of_stake::storage::read_consensus_validator_set_addresses_with_stake; - use namada::proof_of_stake::types::WeightedValidator; - use namada::tendermint::abci::types::VoteInfo; + use namada_sdk::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; use namada_sdk::eth_bridge::SendValsetUpd; + use namada_sdk::proof_of_stake::storage::read_consensus_validator_set_addresses_with_stake; + use namada_sdk::proof_of_stake::types::WeightedValidator; + use namada_sdk::proof_of_stake::PosQueries; + use namada_sdk::storage::Epoch; + use namada_sdk::tendermint::abci::types::VoteInfo; use super::*; use crate::shell::test_utils::get_pkh_from_address; @@ -155,15 +165,15 @@ mod test_queries { Epoch::default(), ); let votes = vec![VoteInfo { - validator: namada::tendermint::abci::types::Validator { + validator: namada_sdk::tendermint::abci::types::Validator { address: pkh1.clone().into(), power: (u128::try_from(val1.bonded_stake).expect("Test failed") as u64).try_into().unwrap(), }, - sig_info: namada::tendermint::abci::types::BlockSignatureInfo::LegacySigned, + sig_info: namada_sdk::tendermint::abci::types::BlockSignatureInfo::LegacySigned, }]; let req = FinalizeBlock { proposer_address: pkh1.to_vec(), - decided_last_commit: namada::tendermint::abci::types::CommitInfo{ + decided_last_commit: namada_sdk::tendermint::abci::types::CommitInfo{ round: 0u8.into(), votes }, diff --git a/crates/node/src/shell/snapshots.rs b/crates/node/src/shell/snapshots.rs index 2e0ea0f36e..238eb8f6b2 100644 --- a/crates/node/src/shell/snapshots.rs +++ b/crates/node/src/shell/snapshots.rs @@ -1,6 +1,6 @@ use borsh_ext::BorshSerializeExt; -use namada::hash::{Hash, Sha256Hasher}; -use namada::state::BlockHeight; +use namada_sdk::hash::{Hash, Sha256Hasher}; +use namada_sdk::state::BlockHeight; use super::{Error, Result}; use crate::facade::tendermint::abci::types::Snapshot; diff --git a/crates/node/src/shell/stats.rs b/crates/node/src/shell/stats.rs index 7bd198c6d6..4b088ee68b 100644 --- a/crates/node/src/shell/stats.rs +++ b/crates/node/src/shell/stats.rs @@ -2,7 +2,7 @@ use std::fmt::Display; -use namada::core::collections::HashMap; +use namada_sdk::collections::HashMap; #[derive(Debug, Default, Clone)] pub struct InternalStats { diff --git a/crates/node/src/shell/testing/client.rs b/crates/node/src/shell/testing/client.rs index 037c390bd4..f9921211ad 100644 --- a/crates/node/src/shell/testing/client.rs +++ b/crates/node/src/shell/testing/client.rs @@ -1,12 +1,12 @@ use clap::Command as App; use eyre::Report; -use namada::io::Io; use namada_apps_lib::cli::api::{CliApi, CliClient}; use namada_apps_lib::cli::args::Global; use namada_apps_lib::cli::{ args, cmds, Cmd, Context, NamadaClient, NamadaRelayer, }; use namada_sdk::error::Error as SdkError; +use namada_sdk::io::Io; use super::node::MockNode; use crate::shell::testing::utils::{Bin, TestingIo}; diff --git a/crates/node/src/shell/testing/node.rs b/crates/node/src/shell/testing/node.rs index 61db218cae..580a69266d 100644 --- a/crates/node/src/shell/testing/node.rs +++ b/crates/node/src/shell/testing/node.rs @@ -9,38 +9,36 @@ use color_eyre::eyre::{Report, Result}; use data_encoding::HEXUPPER; use itertools::Either; use lazy_static::lazy_static; -use namada::address::Address; -use namada::control_flow::time::Duration; -use namada::core::collections::HashMap; -use namada::core::ethereum_events::EthereumEvent; -use namada::core::ethereum_structs; -use namada::core::hash::Hash; -use namada::core::key::tm_consensus_key_raw_hash; -use namada::core::storage::{BlockHeight, Epoch, Header}; -use namada::core::time::DateTimeUtc; -use namada::eth_bridge::oracle::config::Config as OracleConfig; -use namada::ledger::dry_run_tx; -use namada::ledger::events::log::dumb_queries; -use namada::ledger::queries::{ - EncodedResponseQuery, RequestCtx, RequestQuery, Router, RPC, -}; -use namada::proof_of_stake::pos_queries::PosQueries; -use namada::proof_of_stake::storage::{ +use namada_sdk::address::Address; +use namada_sdk::collections::HashMap; +use namada_sdk::control_flow::time::Duration; +use namada_sdk::eth_bridge::oracle::config::Config as OracleConfig; +use namada_sdk::ethereum_events::EthereumEvent; +use namada_sdk::ethereum_structs; +use namada_sdk::events::extend::Height as HeightAttr; +use namada_sdk::events::log::dumb_queries; +use namada_sdk::events::Event; +use namada_sdk::hash::Hash; +use namada_sdk::key::tm_consensus_key_raw_hash; +use namada_sdk::proof_of_stake::pos_queries::PosQueries; +use namada_sdk::proof_of_stake::storage::{ read_consensus_validator_set_addresses_with_stake, validator_consensus_key_handle, }; -use namada::proof_of_stake::types::WeightedValidator; -use namada::sdk::events::extend::Height as HeightAttr; -use namada::sdk::events::Event; -use namada::state::{ +use namada_sdk::proof_of_stake::types::WeightedValidator; +use namada_sdk::queries::{ + Client, EncodedResponseQuery, RequestCtx, RequestQuery, Router, RPC, +}; +use namada_sdk::state::{ LastBlock, Sha256Hasher, StorageRead, EPOCH_SWITCH_BLOCKS_DELAY, }; -use namada::tendermint::abci::response::Info; -use namada::tendermint::abci::types::VoteInfo; -use namada::tx::event::Code as CodeAttr; -use namada_sdk::queries::Client; +use namada_sdk::storage::{BlockHeight, Epoch, Header}; +use namada_sdk::tendermint::abci::response::Info; +use namada_sdk::tendermint::abci::types::VoteInfo; use namada_sdk::tendermint_proto::google::protobuf::Timestamp; +use namada_sdk::time::DateTimeUtc; use namada_sdk::tx::data::ResultCode; +use namada_sdk::tx::event::Code as CodeAttr; use regex::Regex; use tokio::sync::mpsc; @@ -63,7 +61,7 @@ use crate::shims::abcipp_shim_types::shim::request::{ FinalizeBlock, ProcessedTx, }; use crate::shims::abcipp_shim_types::shim::response::TxResult; -use crate::storage; +use crate::{dry_run_tx, storage}; /// Mock Ethereum oracle used for testing purposes. struct MockEthOracle { @@ -382,7 +380,7 @@ impl MockNode { pub fn next_masp_epoch(&mut self) -> Epoch { let masp_epoch_multiplier = - namada::parameters::read_masp_epoch_multiplier_parameter( + namada_sdk::parameters::read_masp_epoch_multiplier_parameter( &self.shell.lock().unwrap().state, ) .unwrap(); @@ -744,16 +742,26 @@ impl<'a> Client for &'a MockNode { prove, }; let borrowed = self.shell.lock().unwrap(); - let ctx = RequestCtx { - state: &borrowed.state, - event_log: borrowed.event_log(), - vp_wasm_cache: borrowed.vp_wasm_cache.read_only(), - tx_wasm_cache: borrowed.tx_wasm_cache.read_only(), - storage_read_past_height_limit: None, - }; if request.path == RPC.shell().dry_run_tx_path() { - dry_run_tx(ctx, &request) + dry_run_tx( + // This is safe because nothing else is using `self.state` + // concurrently and the `TempWlState` will be dropped right + // after dry-run. + unsafe { + borrowed.state.read_only().with_static_temp_write_log() + }, + borrowed.vp_wasm_cache.read_only(), + borrowed.tx_wasm_cache.read_only(), + &request, + ) } else { + let ctx = RequestCtx { + state: &borrowed.state, + event_log: borrowed.event_log(), + vp_wasm_cache: borrowed.vp_wasm_cache.read_only(), + tx_wasm_cache: borrowed.tx_wasm_cache.read_only(), + storage_read_past_height_limit: None, + }; rpc.handle(ctx, &request) } .map_err(Report::new) @@ -821,10 +829,10 @@ impl<'a> Client for &'a MockNode { /// `/block_search`: search for blocks by BeginBlock and EndBlock events. async fn block_search( &self, - query: namada::tendermint_rpc::query::Query, + query: namada_sdk::tendermint_rpc::query::Query, _page: u32, _per_page: u8, - _order: namada::tendermint_rpc::Order, + _order: namada_sdk::tendermint_rpc::Order, ) -> Result { self.drive_mock_services_bg().await; @@ -844,10 +852,12 @@ impl<'a> Client for &'a MockNode { .map(block_search_response) .collect::>(); - Ok(namada::tendermint_rpc::endpoint::block_search::Response { - total_count: blocks.len() as u32, - blocks, - }) + Ok( + namada_sdk::tendermint_rpc::endpoint::block_search::Response { + total_count: blocks.len() as u32, + blocks, + }, + ) } /// `/block_results`: get ABCI results for a block at a particular height. @@ -856,7 +866,7 @@ impl<'a> Client for &'a MockNode { height: H, ) -> Result where - H: TryInto + Send, + H: TryInto + Send, { self.drive_mock_services_bg().await; let height = height.try_into().map_err(|_| { @@ -882,7 +892,9 @@ impl<'a> Client for &'a MockNode { None } }) - .map(|event| namada::tendermint::abci::Event::from(event.clone())) + .map(|event| { + namada_sdk::tendermint::abci::Event::from(event.clone()) + }) .collect(); let has_events = !events.is_empty(); Ok(tendermint_rpc::endpoint::block_results::Response { @@ -893,7 +905,7 @@ impl<'a> Client for &'a MockNode { end_block_events: has_events.then_some(events), validator_updates: vec![], consensus_param_updates: None, - app_hash: namada::tendermint::hash::AppHash::default(), + app_hash: namada_sdk::tendermint::hash::AppHash::default(), }) } @@ -930,11 +942,11 @@ impl<'a> Client for &'a MockNode { /// `/tx_search`: search for transactions with their results. async fn tx_search( &self, - _query: namada::tendermint_rpc::query::Query, + _query: namada_sdk::tendermint_rpc::query::Query, _prove: bool, _page: u32, _per_page: u8, - _order: namada::tendermint_rpc::Order, + _order: namada_sdk::tendermint_rpc::Order, ) -> Result { // In the past, some cli commands for masp called this. However, these // commands are not currently supported, so we do not need to fill @@ -954,7 +966,7 @@ impl<'a> Client for &'a MockNode { /// Parse a Tendermint query. fn parse_tm_query( - query: namada::tendermint_rpc::query::Query, + query: namada_sdk::tendermint_rpc::query::Query, ) -> dumb_queries::QueryMatcher { const QUERY_PARSING_REGEX_STR: &str = r"^tm\.event='NewBlock' AND applied\.hash='([^']+)'$"; @@ -1013,15 +1025,17 @@ where #[inline] fn block_search_response( encoded_event: EncodedEvent, -) -> namada::tendermint_rpc::endpoint::block::Response { - namada::tendermint_rpc::endpoint::block::Response { +) -> namada_sdk::tendermint_rpc::endpoint::block::Response { + namada_sdk::tendermint_rpc::endpoint::block::Response { block_id: Default::default(), - block: namada::tendermint_proto::types::Block { - header: Some(namada::tendermint_proto::types::Header { - version: Some(namada::tendermint_proto::version::Consensus { - block: 0, - app: 0, - }), + block: namada_sdk::tendermint_proto::types::Block { + header: Some(namada_sdk::tendermint_proto::types::Header { + version: Some( + namada_sdk::tendermint_proto::version::Consensus { + block: 0, + app: 0, + }, + ), chain_id: String::new(), // NB: this is the only field that matters to us, // everything else is junk @@ -1040,13 +1054,13 @@ fn block_search_response( }), data: Default::default(), evidence: Default::default(), - last_commit: Some(namada::tendermint_proto::types::Commit { + last_commit: Some(namada_sdk::tendermint_proto::types::Commit { height: encoded_event.0 as i64, round: 0, - block_id: Some(namada::tendermint_proto::types::BlockId { + block_id: Some(namada_sdk::tendermint_proto::types::BlockId { hash: vec![0u8; 32], part_set_header: Some( - namada::tendermint_proto::types::PartSetHeader { + namada_sdk::tendermint_proto::types::PartSetHeader { total: 1, hash: vec![1; 32], }, diff --git a/crates/node/src/shell/testing/utils.rs b/crates/node/src/shell/testing/utils.rs index 52485b9d24..e64114c59a 100644 --- a/crates/node/src/shell/testing/utils.rs +++ b/crates/node/src/shell/testing/utils.rs @@ -5,7 +5,7 @@ use std::pin::Pin; use std::task::{Context, Poll}; use lazy_static::lazy_static; -use namada::io::{prompt_aux, read_aux, Io}; +use namada_sdk::io::{prompt_aux, read_aux, Io}; use tempfile::tempdir; use tokio::io::{AsyncRead, ReadBuf}; diff --git a/crates/node/src/shell/utils.rs b/crates/node/src/shell/utils.rs index e34009956d..f34773efa3 100644 --- a/crates/node/src/shell/utils.rs +++ b/crates/node/src/shell/utils.rs @@ -1,6 +1,6 @@ use borsh::BorshDeserialize; -use namada::core::storage::Key; -use namada::state::{self, StorageRead}; +use namada_sdk::state::{self, StorageRead}; +use namada_sdk::storage::Key; pub(super) fn force_read( storage: &S, diff --git a/crates/node/src/shell/vote_extensions.rs b/crates/node/src/shell/vote_extensions.rs index 705c6ade0a..6114ba9b7a 100644 --- a/crates/node/src/shell/vote_extensions.rs +++ b/crates/node/src/shell/vote_extensions.rs @@ -5,12 +5,12 @@ pub mod eth_events; pub mod val_set_update; use drain_filter_polyfill::DrainFilter; -use namada::ethereum_bridge::protocol::transactions::bridge_pool_roots::sign_bridge_pool_root; -use namada::ethereum_bridge::protocol::transactions::ethereum_events::sign_ethereum_events; -use namada::ethereum_bridge::protocol::transactions::validator_set_update::sign_validator_set_update; -pub use namada::ethereum_bridge::protocol::validation::VoteExtensionError; -use namada::tx::Signed; -use namada::vote_ext::{ +use namada_sdk::eth_bridge::protocol::transactions::bridge_pool_roots::sign_bridge_pool_root; +use namada_sdk::eth_bridge::protocol::transactions::ethereum_events::sign_ethereum_events; +use namada_sdk::eth_bridge::protocol::transactions::validator_set_update::sign_validator_set_update; +pub use namada_sdk::eth_bridge::protocol::validation::VoteExtensionError; +use namada_sdk::tx::Signed; +use namada_vote_ext::{ bridge_pool_roots, ethereum_events, validator_set_update, VoteExtension, }; @@ -34,7 +34,7 @@ where ethereum_events: self.extend_vote_with_ethereum_events(), bridge_pool_root: self .extend_vote_with_bp_roots() - .map(namada::vote_ext::bridge_pool_roots::SignedVext), + .map(bridge_pool_roots::SignedVext), validator_set_update: self.extend_vote_with_valset_update(), } } @@ -176,9 +176,7 @@ pub fn iter_protocol_txs( } = ext; [ ethereum_events.map(|e| { - EthereumTxData::EthEventsVext( - namada::vote_ext::ethereum_events::SignedVext(e), - ) + EthereumTxData::EthEventsVext(ethereum_events::SignedVext(e)) }), bridge_pool_root.map(EthereumTxData::BridgePoolVext), validator_set_update.map(EthereumTxData::ValSetUpdateVext), diff --git a/crates/node/src/shell/vote_extensions/bridge_pool_vext.rs b/crates/node/src/shell/vote_extensions/bridge_pool_vext.rs index f76d859886..0249e4a6bd 100644 --- a/crates/node/src/shell/vote_extensions/bridge_pool_vext.rs +++ b/crates/node/src/shell/vote_extensions/bridge_pool_vext.rs @@ -54,29 +54,31 @@ where #[allow(clippy::cast_possible_truncation)] #[cfg(test)] mod test_bp_vote_extensions { - use namada::core::ethereum_events::Uint; - use namada::core::keccak::{keccak_hash, KeccakHash}; - use namada::core::key::*; - use namada::core::storage::BlockHeight; - use namada::core::token; - use namada::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; - use namada::ethereum_bridge::protocol::validation::bridge_pool_roots::validate_bp_roots_vext; - use namada::ethereum_bridge::storage::bridge_pool::get_key_from_hash; - use namada::ethereum_bridge::storage::eth_bridge_queries::EthBridgeQueries; - use namada::ledger::pos::PosQueries; - use namada::proof_of_stake::storage::{ + use namada_apps_lib::wallet::defaults::{bertha_address, bertha_keypair}; + use namada_sdk::eth_bridge::protocol::validation::bridge_pool_roots::validate_bp_roots_vext; + use namada_sdk::eth_bridge::storage::bridge_pool::get_key_from_hash; + use namada_sdk::eth_bridge::storage::eth_bridge_queries::{ + is_bridge_comptime_enabled, EthBridgeQueries, + }; + use namada_sdk::ethereum_events::Uint; + use namada_sdk::keccak::{keccak_hash, KeccakHash}; + use namada_sdk::key::*; + use namada_sdk::proof_of_stake::storage::{ consensus_validator_set_handle, read_consensus_validator_set_addresses_with_stake, }; - use namada::proof_of_stake::types::{ + use namada_sdk::proof_of_stake::types::{ Position as ValidatorPosition, WeightedValidator, }; - use namada::proof_of_stake::{become_validator, BecomeValidator, Epoch}; - use namada::state::StorageWrite; - use namada::tendermint::abci::types::VoteInfo; - use namada::tx::Signed; - use namada::vote_ext::bridge_pool_roots; - use namada_apps_lib::wallet::defaults::{bertha_address, bertha_keypair}; + use namada_sdk::proof_of_stake::{ + become_validator, BecomeValidator, Epoch, PosQueries, + }; + use namada_sdk::state::StorageWrite; + use namada_sdk::storage::BlockHeight; + use namada_sdk::tendermint::abci::types::VoteInfo; + use namada_sdk::token; + use namada_sdk::tx::Signed; + use namada_vote_ext::bridge_pool_roots; use crate::shell::test_utils::*; use crate::shims::abcipp_shim_types::shim::request::FinalizeBlock; @@ -145,10 +147,11 @@ mod test_bp_vote_extensions { }]; let req = FinalizeBlock { proposer_address: pkh1.to_vec(), - decided_last_commit: namada::tendermint::abci::types::CommitInfo { - round: 0u8.into(), - votes, - }, + decided_last_commit: + crate::facade::tendermint::abci::types::CommitInfo { + round: 0u8.into(), + votes, + }, ..Default::default() }; assert_eq!(shell.start_new_epoch(Some(req)).0, 1); diff --git a/crates/node/src/shell/vote_extensions/eth_events.rs b/crates/node/src/shell/vote_extensions/eth_events.rs index bab313dca3..236ebd3699 100644 --- a/crates/node/src/shell/vote_extensions/eth_events.rs +++ b/crates/node/src/shell/vote_extensions/eth_events.rs @@ -2,8 +2,8 @@ use std::collections::BTreeMap; -use namada::vote_ext::ethereum_events::MultiSignedEthEvent; use namada_sdk::collections::HashMap; +use namada_vote_ext::ethereum_events::MultiSignedEthEvent; use super::*; @@ -137,26 +137,25 @@ where #[allow(clippy::cast_possible_truncation)] #[cfg(test)] mod test_vote_extensions { - use namada::core::address::testing::gen_established_address; - use namada::core::ethereum_events::{ + use namada_sdk::address::testing::gen_established_address; + use namada_sdk::eth_bridge::storage::bridge_pool; + use namada_sdk::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; + use namada_sdk::eth_bridge::EthBridgeQueries; + use namada_sdk::ethereum_events::{ EthAddress, EthereumEvent, TransferToEthereum, Uint, }; - use namada::core::hash::Hash; - use namada::core::key::*; - use namada::core::storage::{Epoch, InnerEthEventsQueue}; - use namada::eth_bridge::storage::bridge_pool; - use namada::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; - use namada::ledger::eth_bridge::EthBridgeQueries; - use namada::ledger::pos::PosQueries; - use namada::proof_of_stake::storage::{ + use namada_sdk::hash::Hash; + use namada_sdk::key::*; + use namada_sdk::proof_of_stake::storage::{ consensus_validator_set_handle, read_consensus_validator_set_addresses_with_stake, }; - use namada::proof_of_stake::types::WeightedValidator; - use namada::state::collections::lazy_map::{NestedSubKey, SubKey}; - use namada::tendermint::abci::types::VoteInfo; - use namada::vote_ext::ethereum_events; - use namada_sdk::storage::StorageWrite; + use namada_sdk::proof_of_stake::types::WeightedValidator; + use namada_sdk::proof_of_stake::PosQueries; + use namada_sdk::state::collections::lazy_map::{NestedSubKey, SubKey}; + use namada_sdk::storage::{Epoch, InnerEthEventsQueue, StorageWrite}; + use namada_sdk::tendermint::abci::types::VoteInfo; + use namada_vote_ext::ethereum_events; use super::validate_eth_events_vext; use crate::shell::test_utils::*; @@ -471,10 +470,11 @@ mod test_vote_extensions { }]; let req = FinalizeBlock { proposer_address: pkh1.to_vec(), - decided_last_commit: namada::tendermint::abci::types::CommitInfo { - round: 0u8.into(), - votes, - }, + decided_last_commit: + crate::facade::tendermint::abci::types::CommitInfo { + round: 0u8.into(), + votes, + }, ..Default::default() }; assert_eq!(shell.start_new_epoch(Some(req)).0, 1); diff --git a/crates/node/src/shell/vote_extensions/val_set_update.rs b/crates/node/src/shell/vote_extensions/val_set_update.rs index 54fc8d073e..9ac1e3322b 100644 --- a/crates/node/src/shell/vote_extensions/val_set_update.rs +++ b/crates/node/src/shell/vote_extensions/val_set_update.rs @@ -1,7 +1,7 @@ //! Extend Tendermint votes with validator set updates, to be relayed to //! Namada's Ethereum bridge smart contracts. -use namada::core::collections::HashMap; +use namada_sdk::collections::HashMap; use super::*; @@ -110,20 +110,19 @@ where #[allow(clippy::cast_possible_truncation)] #[cfg(test)] mod test_vote_extensions { - use namada::core::key::RefTo; - use namada::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; - use namada::ledger::pos::PosQueries; - use namada::proof_of_stake::storage::{ + use namada_apps_lib::wallet; + use namada_sdk::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; + use namada_sdk::eth_bridge::EthBridgeQueries; + use namada_sdk::key::RefTo; + use namada_sdk::proof_of_stake::storage::{ consensus_validator_set_handle, read_consensus_validator_set_addresses_with_stake, }; - use namada::proof_of_stake::types::WeightedValidator; - use namada::proof_of_stake::Epoch; - use namada::state::collections::lazy_map::{NestedSubKey, SubKey}; - use namada::tendermint::abci::types::VoteInfo; - use namada::vote_ext::validator_set_update; - use namada_apps_lib::wallet; - use namada_sdk::eth_bridge::EthBridgeQueries; + use namada_sdk::proof_of_stake::types::WeightedValidator; + use namada_sdk::proof_of_stake::{Epoch, PosQueries}; + use namada_sdk::state::collections::lazy_map::{NestedSubKey, SubKey}; + use namada_sdk::tendermint::abci::types::VoteInfo; + use namada_vote_ext::validator_set_update; use super::validate_valset_upd_vext; use crate::shell::test_utils::{self, get_pkh_from_address}; @@ -323,10 +322,11 @@ mod test_vote_extensions { }]; let req = FinalizeBlock { proposer_address: pkh1.to_vec(), - decided_last_commit: namada::tendermint::abci::types::CommitInfo { - round: 0u8.into(), - votes, - }, + decided_last_commit: + crate::facade::tendermint::abci::types::CommitInfo { + round: 0u8.into(), + votes, + }, ..Default::default() }; assert_eq!(shell.start_new_epoch(Some(req)).0, 1); diff --git a/crates/node/src/shims/abcipp_shim.rs b/crates/node/src/shims/abcipp_shim.rs index 6ad77f364a..00b1c6c01b 100644 --- a/crates/node/src/shims/abcipp_shim.rs +++ b/crates/node/src/shims/abcipp_shim.rs @@ -4,13 +4,13 @@ use std::pin::Pin; use std::task::{Context, Poll}; use futures::future::FutureExt; -use namada::core::hash::Hash; -use namada::core::storage::BlockHeight; -use namada::state::{ProcessProposalCachedResult, DB}; -use namada::tendermint::abci::response::ProcessProposal; -use namada::time::{DateTimeUtc, Utc}; -use namada::tx::data::hash_tx; +use namada_sdk::hash::Hash; use namada_sdk::migrations::ScheduledMigration; +use namada_sdk::state::{ProcessProposalCachedResult, DB}; +use namada_sdk::storage::BlockHeight; +use namada_sdk::tendermint::abci::response::ProcessProposal; +use namada_sdk::time::{DateTimeUtc, Utc}; +use namada_sdk::tx::data::hash_tx; use tokio::sync::broadcast; use tokio::sync::mpsc::UnboundedSender; use tower::Service; @@ -247,7 +247,7 @@ impl AbcippShim { &mut self, begin_block_request: request::BeginBlock, ) -> ProcessProposalCachedResult { - match namada::core::hash::Hash::try_from(begin_block_request.hash) { + match namada_sdk::hash::Hash::try_from(begin_block_request.hash) { Ok(block_hash) => { match self .service diff --git a/crates/node/src/shims/abcipp_shim_types.rs b/crates/node/src/shims/abcipp_shim_types.rs index be671f6908..e6b847aa13 100644 --- a/crates/node/src/shims/abcipp_shim_types.rs +++ b/crates/node/src/shims/abcipp_shim_types.rs @@ -169,13 +169,13 @@ pub mod shim { pub mod request { use bytes::Bytes; - use namada::core::hash::Hash; - use namada::core::storage::Header; - use namada::core::time::DateTimeUtc; - use namada::tendermint::abci::types::CommitInfo; - use namada::tendermint::account::Id; - use namada::tendermint::block::Height; - use namada::tendermint::time::Time; + use namada_sdk::hash::Hash; + use namada_sdk::storage::Header; + use namada_sdk::tendermint::abci::types::CommitInfo; + use namada_sdk::tendermint::account::Id; + use namada_sdk::tendermint::block::Height; + use namada_sdk::tendermint::time::Time; + use namada_sdk::time::DateTimeUtc; use crate::facade::tendermint::abci::types::Misbehavior; use crate::facade::tendermint::v0_37::abci::request as tm_request; @@ -206,10 +206,10 @@ pub mod shim { pub(crate) struct CheckProcessProposal { proposed_last_commit: Option, misbehavior: Vec, - hash: namada::tendermint::Hash, + hash: namada_sdk::tendermint::Hash, height: Height, time: Time, - next_validators_hash: namada::tendermint::Hash, + next_validators_hash: namada_sdk::tendermint::Hash, proposer_address: Id, } @@ -311,7 +311,7 @@ pub mod shim { /// Custom types for response payloads pub mod response { - use namada::ledger::events::Event; + use namada_sdk::events::Event; pub use crate::facade::tendermint::v0_37::abci::response::{ PrepareProposal, ProcessProposal, diff --git a/crates/node/src/storage/mod.rs b/crates/node/src/storage/mod.rs index 72036efa1e..4d6e5ea552 100644 --- a/crates/node/src/storage/mod.rs +++ b/crates/node/src/storage/mod.rs @@ -9,8 +9,7 @@ use arse_merkle_tree::blake2b::Blake2bHasher; use arse_merkle_tree::traits::Hasher; use arse_merkle_tree::H256; use blake2b_rs::{Blake2b, Blake2bBuilder}; -use namada::state::StorageHasher; -use namada_sdk::state::FullAccessState; +use namada_sdk::state::{FullAccessState, StorageHasher}; pub use rocksdb::{open, DbSnapshot, RocksDBUpdateVisitor, SnapshotMetadata}; #[derive(Default)] @@ -56,25 +55,25 @@ fn new_blake2b() -> Blake2b { mod tests { use borsh::BorshDeserialize; use itertools::Itertools; - use namada::core::chain::ChainId; - use namada::core::collections::HashMap; - use namada::core::ethereum_events::Uint; - use namada::core::hash::Hash; - use namada::core::keccak::KeccakHash; - use namada::core::storage::{BlockHeight, Key, KeySeg}; - use namada::core::time::DurationSecs; - use namada::core::{address, storage}; - use namada::eth_bridge::storage::proof::BridgePoolRootProof; - use namada::ibc::storage::is_ibc_key; - use namada::ledger::eth_bridge::storage::bridge_pool; - use namada::ledger::gas::STORAGE_ACCESS_GAS_PER_BYTE; - use namada::ledger::ibc::storage::{client_counter_key, ibc_key}; - use namada::ledger::parameters::{EpochDuration, Parameters}; - use namada::state::{self, StorageRead, StorageWrite, StoreType, DB}; - use namada::token::conversion::update_allowed_conversions; - use namada::{decode, encode, parameters}; + use namada_sdk::chain::ChainId; + use namada_sdk::collections::HashMap; + use namada_sdk::eth_bridge::storage::bridge_pool; + use namada_sdk::eth_bridge::storage::proof::BridgePoolRootProof; + use namada_sdk::ethereum_events::Uint; + use namada_sdk::gas::STORAGE_ACCESS_GAS_PER_BYTE; + use namada_sdk::hash::Hash; + use namada_sdk::ibc::storage::{client_counter_key, ibc_key, is_ibc_key}; + use namada_sdk::keccak::KeccakHash; + use namada_sdk::parameters::Parameters; use namada_sdk::state::merkle_tree::NO_DIFF_KEY_PREFIX; - use namada_sdk::state::StateRead; + use namada_sdk::state::{ + self, StateRead, StorageRead, StorageWrite, StoreType, DB, + }; + use namada_sdk::storage::{BlockHeight, Key, KeySeg}; + use namada_sdk::token::conversion::update_allowed_conversions; + use namada_sdk::{ + address, decode, encode, parameters, storage, validation, + }; use proptest::collection::vec; use proptest::prelude::*; use proptest::test_runner::Config; @@ -269,8 +268,9 @@ mod tests { let key = Key::validity_predicate(&addr); // not exist - let (vp, gas) = - state.validity_predicate(&addr).expect("VP load failed"); + let (vp, gas) = state + .validity_predicate::(&addr) + .expect("VP load failed"); assert_eq!(vp, None); assert_eq!(gas, (key.len() as u64) * STORAGE_ACCESS_GAS_PER_BYTE); @@ -279,8 +279,9 @@ mod tests { state.db_write(&key, vp1).expect("write failed"); // check - let (vp_code_hash, gas) = - state.validity_predicate(&addr).expect("VP load failed"); + let (vp_code_hash, gas) = state + .validity_predicate::(&addr) + .expect("VP load failed"); assert_eq!(vp_code_hash.expect("no VP"), vp1); assert_eq!( gas, @@ -318,7 +319,7 @@ mod tests { /// value, if any. fn test_read_with_height_aux( blocks_write_value: Vec, - ) -> namada::state::Result<()> { + ) -> namada_sdk::state::Result<()> { let db_path = TempDir::new().expect("Unable to create a temporary DB directory"); let mut state = PersistentState::open( @@ -412,7 +413,7 @@ mod tests { /// Test the restore of the merkle tree fn test_get_merkle_tree_aux( blocks_write_type: Vec, - ) -> namada::state::Result<()> { + ) -> namada_sdk::state::Result<()> { let db_path = TempDir::new().expect("Unable to create a temporary DB directory"); let mut state = PersistentState::open( diff --git a/crates/node/src/storage/rocksdb.rs b/crates/node/src/storage/rocksdb.rs index 55829afc6b..facb1f79f7 100644 --- a/crates/node/src/storage/rocksdb.rs +++ b/crates/node/src/storage/rocksdb.rs @@ -55,27 +55,26 @@ use borsh::{BorshDeserialize, BorshSerialize}; use borsh_ext::BorshSerializeExt; use data_encoding::HEXLOWER; use itertools::Either; -use namada::core::collections::{HashMap, HashSet}; -use namada::core::storage::{BlockHeight, Epoch, Header, Key, KeySeg}; -use namada::core::{decode, encode, ethereum_events}; -use namada::eth_bridge::storage::proof::BridgePoolRootProof; -use namada::hash::Hash; -use namada::ledger::eth_bridge::storage::bridge_pool; -use namada::replay_protection; -use namada::state::merkle_tree::{ +use namada_replay_protection as replay_protection; +use namada_sdk::arith::checked; +use namada_sdk::collections::{HashMap, HashSet}; +use namada_sdk::eth_bridge::storage::bridge_pool; +use namada_sdk::eth_bridge::storage::proof::BridgePoolRootProof; +use namada_sdk::hash::Hash; +use namada_sdk::migrations::{DBUpdateVisitor, DbUpdateType}; +use namada_sdk::state::merkle_tree::{ tree_key_prefix_with_epoch, tree_key_prefix_with_height, }; -use namada::state::{ +use namada_sdk::state::{ BlockStateRead, BlockStateWrite, DBIter, DBWriteBatch, DbError as Error, DbResult as Result, MerkleTreeStoresRead, PatternIterator, PrefixIterator, StoreType, DB, }; -use namada::storage::{ - DbColFam, BLOCK_CF, DIFFS_CF, REPLAY_PROTECTION_CF, ROLLBACK_CF, STATE_CF, - SUBSPACE_CF, +use namada_sdk::storage::{ + BlockHeight, DbColFam, Epoch, Header, Key, KeySeg, BLOCK_CF, DIFFS_CF, + REPLAY_PROTECTION_CF, ROLLBACK_CF, STATE_CF, SUBSPACE_CF, }; -use namada_sdk::arith::checked; -use namada_sdk::migrations::{DBUpdateVisitor, DbUpdateType}; +use namada_sdk::{decode, encode, ethereum_events}; use rayon::prelude::*; use regex::Regex; use rocksdb::{ @@ -1312,7 +1311,7 @@ impl DB for RocksDB { fn has_replay_protection_entry( &self, - hash: &namada::core::hash::Hash, + hash: &namada_sdk::hash::Hash, ) -> Result { let replay_protection_cf = self.get_column_family(REPLAY_PROTECTION_CF)?; @@ -1608,7 +1607,7 @@ impl DB for RocksDB { stripped_prefix.as_ref(), None, ) { - let hash = namada::core::hash::Hash::from_str(hash_str) + let hash = namada_sdk::hash::Hash::from_str(hash_str) .expect("Failed hash conversion"); let current_key = replay_protection::current_key(&hash); let key = replay_protection::key(&hash); @@ -2128,15 +2127,14 @@ mod imp { #[allow(clippy::arithmetic_side_effects)] #[cfg(test)] mod test { - use namada::address::EstablishedAddressGen; - use namada::core::collections::HashMap; - use namada::core::hash::Hash; - use namada::core::storage::Epochs; - use namada::ledger::storage::ConversionState; - use namada::state::{MerkleTree, Sha256Hasher}; - use namada::storage::{BlockResults, EthEventsQueue}; - use namada::time::DateTimeUtc; + use namada_sdk::address::EstablishedAddressGen; + use namada_sdk::collections::HashMap; + use namada_sdk::hash::Hash; + use namada_sdk::state::{MerkleTree, Sha256Hasher}; + use namada_sdk::storage::conversion_state::ConversionState; use namada_sdk::storage::types::CommitOnlyData; + use namada_sdk::storage::{BlockResults, Epochs, EthEventsQueue}; + use namada_sdk::time::DateTimeUtc; use tempfile::tempdir; use test_log::test; diff --git a/crates/node/src/tendermint_node.rs b/crates/node/src/tendermint_node.rs index b0a4164ba8..04c6e420bf 100644 --- a/crates/node/src/tendermint_node.rs +++ b/crates/node/src/tendermint_node.rs @@ -3,12 +3,12 @@ use std::path::{Path, PathBuf}; use std::process::Stdio; use std::str::FromStr; -use namada::core::chain::{ChainId, ProposalBytes}; -use namada::core::storage::BlockHeight; -use namada::core::time::DateTimeUtc; use namada_apps_lib::cli::namada_version; use namada_apps_lib::config; pub use namada_apps_lib::tendermint_node::*; +use namada_sdk::chain::{ChainId, ProposalBytes}; +use namada_sdk::storage::BlockHeight; +use namada_sdk::time::DateTimeUtc; use thiserror::Error; use tokio::fs::{File, OpenOptions}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; diff --git a/crates/parameters/src/lib.rs b/crates/parameters/src/lib.rs index ec598e3aba..8ffe5bb8f9 100644 --- a/crates/parameters/src/lib.rs +++ b/crates/parameters/src/lib.rs @@ -65,12 +65,6 @@ where read_epoch_duration_parameter(storage) } - fn max_signatures_per_transaction( - storage: &S, - ) -> Result, Self::Err> { - max_signatures_per_transaction(storage) - } - fn is_native_token_transferable(storage: &S) -> Result { storage::is_native_token_transferable(storage) } diff --git a/crates/parameters/src/vp.rs b/crates/parameters/src/vp.rs index 4b7c76af7d..2e3cdaca46 100644 --- a/crates/parameters/src/vp.rs +++ b/crates/parameters/src/vp.rs @@ -27,31 +27,32 @@ pub enum Error { pub type Result = std::result::Result; /// Parameters VP -pub struct ParametersVp<'a, S, CA, EVAL, Gov> +pub struct ParametersVp<'ctx, S, CA, EVAL, Gov> where S: 'static + StateRead, - EVAL: VpEvaluator<'a, S, CA, EVAL>, + EVAL: VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA, EVAL>, - /// Governance type - pub gov: PhantomData, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Generic types for DI + pub _marker: PhantomData, } -impl<'a, S, CA, EVAL, Gov> NativeVp<'a> for ParametersVp<'a, S, CA, EVAL, Gov> +impl<'view, 'ctx: 'view, S, CA, EVAL, Gov> NativeVp<'view> + for ParametersVp<'ctx, S, CA, EVAL, Gov> where S: 'static + StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, Gov: governance::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, { type Error = Error; fn validate_tx( - &'a self, + &'view self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, _verifiers: &BTreeSet
, @@ -86,6 +87,25 @@ where } } +impl<'ctx, S, CA, EVAL, Gov> ParametersVp<'ctx, S, CA, EVAL, Gov> +where + S: 'static + StateRead, + CA: 'static + Clone, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, + Gov: governance::Read< + CtxPreStorageRead<'ctx, 'ctx, S, CA, EVAL>, + Err = StorageError, + >, +{ + /// Instantiate parameters VP + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { + Self { + ctx, + _marker: PhantomData, + } + } +} + #[allow(clippy::upper_case_acronyms)] enum KeyType { #[allow(clippy::upper_case_acronyms)] diff --git a/crates/proof_of_stake/src/lib.rs b/crates/proof_of_stake/src/lib.rs index 2f97947ce2..0717490396 100644 --- a/crates/proof_of_stake/src/lib.rs +++ b/crates/proof_of_stake/src/lib.rs @@ -44,7 +44,7 @@ pub use error::*; use namada_core::address::{Address, InternalAddress}; use namada_core::arith::checked; use namada_core::collections::HashSet; -use namada_core::dec::Dec; +pub use namada_core::dec::Dec; use namada_core::key::common; use namada_core::storage::BlockHeight; pub use namada_core::storage::{Epoch, Key, KeySeg}; @@ -54,7 +54,9 @@ use namada_storage::collections::lazy_map::{self, Collectable, LazyMap}; use namada_storage::{OptionExt, StorageRead, StorageWrite}; pub use namada_trans_token as token; pub use parameters::{OwnedPosParams, PosParams}; +pub use pos_queries::PosQueries; use storage::write_validator_name; +pub use types::GenesisValidator; use types::{into_tm_voting_power, DelegationEpochs}; use crate::queries::{find_bonds, has_bonds}; @@ -2766,7 +2768,6 @@ pub mod test_utils { /// A dummy validator used for testing pub fn get_dummy_genesis_validator() -> types::GenesisValidator { use namada_core::address::testing::established_address_1; - use namada_core::dec::Dec; use namada_core::key::testing::common_sk_from_simple_seed; use namada_core::{key, token}; diff --git a/crates/proof_of_stake/src/vp.rs b/crates/proof_of_stake/src/vp.rs index 9bc67f27d0..f86d613803 100644 --- a/crates/proof_of_stake/src/vp.rs +++ b/crates/proof_of_stake/src/vp.rs @@ -37,31 +37,32 @@ pub enum Error { pub type Result = std::result::Result; /// Proof-of-Stake validity predicate -pub struct PosVP<'a, S, CA, EVAL, Gov> +pub struct PosVp<'ctx, S, CA, EVAL, Gov> where - S: 'static + StateRead, - EVAL: VpEvaluator<'a, S, CA, EVAL>, + S: StateRead, + EVAL: VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA, EVAL>, - /// Governance type - pub gov: PhantomData, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Generic types for DI + pub _marker: PhantomData, } -impl<'a, S, CA, EVAL, Gov> NativeVp<'a> for PosVP<'a, S, CA, EVAL, Gov> +impl<'view, 'ctx: 'view, S, CA, EVAL, Gov> NativeVp<'view> + for PosVp<'ctx, S, CA, EVAL, Gov> where - S: 'static + StateRead, + S: StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, Gov: governance::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, { type Error = Error; fn validate_tx( - &'a self, + &'view self, batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -316,26 +317,27 @@ where } } -impl<'a, S, CA, EVAL, Gov> PosVP<'a, S, CA, EVAL, Gov> +impl<'ctx, S, CA, EVAL, Gov> PosVp<'ctx, S, CA, EVAL, Gov> where - S: 'static + StateRead, + S: StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, { /// Instantiate a `PosVP`. - pub fn new(ctx: Ctx<'a, S, CA, EVAL>) -> Self { + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { Self { ctx, - gov: PhantomData, + _marker: PhantomData, } } /// Return `Ok` if the changed parameters are valid fn is_valid_parameter_change(&self) -> Result<()> { - let validation_errors = read_pos_params(&self.ctx.post()) - .map_err(Error::NativeVpError)? - .owned - .validate(); + let validation_errors: Vec = + read_pos_params(&self.ctx.post()) + .map_err(Error::NativeVpError)? + .owned + .validate(); validation_errors.is_empty().ok_or_else(|| { let validation_errors_str = itertools::join(validation_errors, ", "); diff --git a/crates/sdk/Cargo.toml b/crates/sdk/Cargo.toml index 413f4c8ecb..572ee3f8dc 100644 --- a/crates/sdk/Cargo.toml +++ b/crates/sdk/Cargo.toml @@ -15,26 +15,14 @@ version.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["tendermint-rpc", "std", "rand", "migrations"] - +default = ["std"] mainnet = ["namada_core/mainnet", "namada_events/mainnet"] - -multicore = ["masp_proofs/multicore"] - -namada-sdk = ["tendermint-rpc", "masp_primitives/transparent-inputs"] - +multicore = ["masp_proofs/multicore", "namada_token/multicore"] std = ["fd-lock", "download-params"] -rand = ["dep:rand", "rand_core", "namada_core/rand"] - -# tendermint-rpc support -tendermint-rpc = ["async-client", "dep:tendermint-rpc"] - -# Enable queries support for an async client -async-client = ["async-trait"] - async-send = [] - namada-eth-bridge = ["namada_ethereum_bridge/namada-eth-bridge"] +benches = ["namada_core/benches", "namada_core/testing", "namada_state/benches"] +wasm-runtime = ["namada_vm/wasm-runtime"] # for integration tests and test utilities testing = [ @@ -44,11 +32,11 @@ testing = [ "namada_ethereum_bridge/testing", "namada_governance/testing", "namada_ibc/testing", + "namada_parameters/testing", "namada_proof_of_stake/testing", "namada_storage/testing", "namada_token/testing", "namada_tx/testing", - "async-client", "proptest", "jubjub", ] @@ -72,7 +60,7 @@ migrations = [ [dependencies] namada_account = { path = "../account" } -namada_core = { path = "../core" } +namada_core = { path = "../core", features = ["control_flow", "rand"] } namada_ethereum_bridge = { path = "../ethereum_bridge", default-features = false } namada_events = { path = "../events" } namada_gas = { path = "../gas" } @@ -86,9 +74,11 @@ namada_state = { path = "../state" } namada_storage = { path = "../storage" } namada_token = { path = "../token" } namada_tx = { path = "../tx" } +namada_vm = { path = "../vm", default-features = false } namada_vote_ext = { path = "../vote_ext" } +namada_vp = { path = "../vp" } -async-trait = { workspace = true, optional = true } +async-trait.workspace = true bimap.workspace = true borsh.workspace = true borsh-ext.workspace = true @@ -118,15 +108,15 @@ paste.workspace = true patricia_tree.workspace = true proptest = { workspace = true, optional = true } prost.workspace = true -rand = { workspace = true, optional = true } -rand_core = { workspace = true, optional = true } +rand.workspace = true +rand_core.workspace = true regex.workspace = true serde.workspace = true serde_json.workspace = true sha2.workspace = true slip10_ed25519.workspace = true smooth-operator.workspace = true -tendermint-rpc = { workspace = true, optional = true } +tendermint-rpc.workspace = true thiserror.workspace = true tiny-bip39.workspace = true tiny-hderive.workspace = true @@ -140,7 +130,6 @@ tokio = { workspace = true, features = ["full"] } [target.'cfg(target_family = "wasm")'.dependencies] tokio = { workspace = true, default-features = false, features = ["sync"] } -wasmtimer = "0.2.0" [dev-dependencies] namada_account = { path = "../account", features = ["testing"]} @@ -162,7 +151,9 @@ namada_storage = { path = "../storage", features = ["testing"] } namada_test_utils = { path = "../test_utils" } namada_token = { path = "../token", features = ["testing"] } namada_tx = { path = "../tx", features = ["testing"]} -namada_vote_ext = {path = "../vote_ext"} +namada_vm = { path = "../vm" } +namada_vote_ext = { path = "../vote_ext" } +namada_vp = { path = "../vp" } assert_matches.workspace = true base58.workspace = true diff --git a/crates/sdk/src/lib.rs b/crates/sdk/src/lib.rs index 3cad286087..5115746747 100644 --- a/crates/sdk/src/lib.rs +++ b/crates/sdk/src/lib.rs @@ -15,13 +15,12 @@ extern crate alloc; pub use namada_core::*; -#[cfg(feature = "tendermint-rpc")] -pub use tendermint_rpc; pub use { bip39, masp_primitives, masp_proofs, namada_account as account, - namada_gas as gas, namada_governance as governance, - namada_proof_of_stake as proof_of_stake, namada_state as state, - namada_storage as storage, namada_token as token, zeroize, + namada_gas as gas, namada_governance as governance, namada_ibc as ibc, + namada_parameters as parameters, namada_proof_of_stake as proof_of_stake, + namada_state as state, namada_storage as storage, namada_token as token, + tendermint_rpc, zeroize, }; pub mod eth_bridge; @@ -33,13 +32,14 @@ pub mod masp; pub mod signing; #[allow(clippy::result_large_err)] pub mod tx; -#[cfg(any(test, feature = "testing", feature = "validation"))] +#[cfg(feature = "wasm-runtime")] pub mod validation; pub mod error; pub mod events; pub(crate) mod internal_macros; pub mod io; +#[cfg(feature = "migrations")] pub mod migrations; pub mod queries; pub mod wallet; diff --git a/crates/sdk/src/masp.rs b/crates/sdk/src/masp.rs index af45716007..b5847d6429 100644 --- a/crates/sdk/src/masp.rs +++ b/crates/sdk/src/masp.rs @@ -37,18 +37,16 @@ use masp_primitives::transaction::components::{ }; use masp_primitives::transaction::fees::fixed::FeeRule; use masp_primitives::transaction::{Authorization, Authorized, Transaction}; -use masp_primitives::zip32::{ExtendedFullViewingKey, ExtendedSpendingKey}; +use masp_primitives::zip32::{ + ExtendedFullViewingKey, ExtendedSpendingKey as MaspExtendedSpendingKey, +}; use masp_proofs::prover::LocalTxProver; use namada_core::address::Address; use namada_core::arith::CheckedAdd; use namada_core::collections::{HashMap, HashSet}; use namada_core::dec::Dec; use namada_core::ibc::IbcTxDataRefs; -pub use namada_core::masp::{ - encode_asset_type, AssetData, BalanceOwner, ExtendedViewingKey, - PaymentAddress, TAddrData, TransferSource, TransferTarget, TxId, -}; -use namada_core::masp::{MaspEpoch, MaspTxRefs}; +pub use namada_core::masp::*; use namada_core::storage::{BlockHeight, TxIndex}; use namada_core::time::DateTimeUtc; use namada_core::uint::Uint; @@ -61,7 +59,6 @@ use namada_ibc::{decode_message, extract_masp_tx_from_envelope, IbcMessage}; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; -#[cfg(feature = "validation")] pub use namada_token::validation::{ partial_deauthorize, preload_verifying_keys, PVKs, CONVERT_NAME, ENV_VAR_MASP_PARAMS_DIR, OUTPUT_NAME, SPEND_NAME, @@ -203,20 +200,20 @@ struct WalletMap; impl masp_primitives::transaction::components::sapling::builder::MapBuilder< P1, - ExtendedSpendingKey, + MaspExtendedSpendingKey, (), ExtendedFullViewingKey, > for WalletMap { fn map_params(&self, _s: P1) {} - fn map_key(&self, s: ExtendedSpendingKey) -> ExtendedFullViewingKey { + fn map_key(&self, s: MaspExtendedSpendingKey) -> ExtendedFullViewingKey { (&s).into() } } impl - MapBuilder + MapBuilder for WalletMap { fn map_notifier(&self, _s: N1) {} @@ -247,7 +244,7 @@ pub trait ShieldedUtils: } /// Make a ViewingKey that can view notes encrypted by given ExtendedSpendingKey -pub fn to_viewing_key(esk: &ExtendedSpendingKey) -> FullViewingKey { +pub fn to_viewing_key(esk: &MaspExtendedSpendingKey) -> FullViewingKey { ExtendedFullViewingKey::from(esk).fvk } @@ -605,7 +602,7 @@ impl ShieldedContext { start_query_height: Option, last_query_height: Option, retry: RetryStrategy, - sks: &[ExtendedSpendingKey], + sks: &[MaspExtendedSpendingKey], fvks: &[ViewingKey], ) -> Result<(), Error> where @@ -701,7 +698,7 @@ impl ShieldedContext { start_query_height: Option, last_query_height: Option, retry: RetryStrategy, - sks: &[ExtendedSpendingKey], + sks: &[MaspExtendedSpendingKey], fvks: &[ViewingKey], mut shutdown_signal: ShutdownSignal, ) -> Result<(), Error> @@ -1570,7 +1567,7 @@ impl ShieldedContext { #[allow(unused_mut)] let mut rng = StdRng::from_rng(OsRng).unwrap(); #[cfg(feature = "testing")] - let mut rng = if let Ok(seed) = env::var(ENV_VAR_MASP_TEST_SEED) + let mut rng = if let Ok(seed) = std::env::var(ENV_VAR_MASP_TEST_SEED) .map_err(|e| Error::Other(e.to_string())) .and_then(|seed| { let exp_str = @@ -2016,7 +2013,7 @@ impl ShieldedContext { // viewing key in the following computations. let ovk_opt = source .spending_key() - .map(|x| ExtendedSpendingKey::from(x).expsk.ovk); + .map(|x| MaspExtendedSpendingKey::from(x).expsk.ovk); // Make transaction output tied to the current token, // denomination, and epoch. if let Some(pa) = payment_address { @@ -2351,7 +2348,7 @@ impl ShieldedContext { for (sp, changes) in changes.into_iter() { for (asset_type, amt) in changes.components() { if let Ordering::Greater = amt.cmp(&0) { - let sk = ExtendedSpendingKey::from(sp.to_owned()); + let sk = MaspExtendedSpendingKey::from(sp.to_owned()); // Send the change in this asset type back to the sender builder .add_sapling_output( @@ -3048,7 +3045,7 @@ pub mod testing { ) -> (Option, masp_primitives::sapling::PaymentAddress, AssetType, u64, MemoBytes) { let mut spending_key_seed = [0; 32]; rng.fill_bytes(&mut spending_key_seed); - let spending_key = masp_primitives::zip32::ExtendedSpendingKey::master(spending_key_seed.as_ref()); + let spending_key = MaspExtendedSpendingKey::master(spending_key_seed.as_ref()); let viewing_key = ExtendedFullViewingKey::from(&spending_key).fvk.vk; let (div, _g_d) = find_valid_diversifier(&mut rng); @@ -3071,10 +3068,10 @@ pub mod testing { mut rng in arb_rng().prop_map(TestCsprng), bparams_rng in arb_rng().prop_map(TestCsprng), prover_rng in arb_rng().prop_map(TestCsprng), - ) -> (ExtendedSpendingKey, Diversifier, Note, Node) { + ) -> (MaspExtendedSpendingKey, Diversifier, Note, Node) { let mut spending_key_seed = [0; 32]; rng.fill_bytes(&mut spending_key_seed); - let spending_key = masp_primitives::zip32::ExtendedSpendingKey::master(spending_key_seed.as_ref()); + let spending_key = MaspExtendedSpendingKey::master(spending_key_seed.as_ref()); let viewing_key = ExtendedFullViewingKey::from(&spending_key).fvk.vk; let (div, _g_d) = find_valid_diversifier(&mut rng); @@ -3166,7 +3163,7 @@ pub mod testing { ).unwrap(), *value, )).collect::>() - ) -> Vec<(ExtendedSpendingKey, Diversifier, Note, Node)> { + ) -> Vec<(MaspExtendedSpendingKey, Diversifier, Note, Node)> { spend_description } } @@ -3373,7 +3370,7 @@ pub mod testing { } } -#[cfg(all(feature = "std", feature = "validation"))] +#[cfg(feature = "std")] /// Implementation of MASP functionality depending on a standard filesystem pub mod fs { use std::env; diff --git a/crates/sdk/src/masp/test_utils.rs b/crates/sdk/src/masp/test_utils.rs index df8acbdd47..dc5ea58886 100644 --- a/crates/sdk/src/masp/test_utils.rs +++ b/crates/sdk/src/masp/test_utils.rs @@ -45,7 +45,6 @@ impl DerefMut for TestingClient { } } -#[cfg(any(test, feature = "async-client"))] #[cfg_attr(feature = "async-send", async_trait::async_trait)] #[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] impl Client for TestingClient { diff --git a/crates/sdk/src/migrations.rs b/crates/sdk/src/migrations.rs index df28424238..b66a111f4e 100644 --- a/crates/sdk/src/migrations.rs +++ b/crates/sdk/src/migrations.rs @@ -1,10 +1,6 @@ #![allow(missing_docs)] -#[cfg(not(feature = "migrations"))] -use core::fmt::Formatter; -#[cfg(feature = "migrations")] use core::fmt::{Display, Formatter}; -#[cfg(feature = "migrations")] use core::str::FromStr; use std::marker::PhantomData; use std::path::{Path, PathBuf}; @@ -15,20 +11,13 @@ use data_encoding::HEXUPPER; use eyre::eyre; use namada_core::hash::Hash; use namada_core::storage::{BlockHeight, Key}; -#[cfg(feature = "migrations")] -use namada_macros::derive_borshdeserializer; -#[cfg(feature = "migrations")] -use namada_macros::typehash; -#[cfg(feature = "migrations")] -use namada_migrations::TypeHash; -#[cfg(feature = "migrations")] -use namada_migrations::*; +use namada_macros::{derive_borshdeserializer, typehash}; +use namada_migrations::{TypeHash, *}; use namada_storage::{DbColFam, DbMigration, DB}; use regex::Regex; use serde::de::{DeserializeOwned, Error, Visitor}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[cfg(feature = "migrations")] /// The maximum number of character printed per value. const PRINTLN_CUTOFF: usize = 300; @@ -58,7 +47,6 @@ pub struct UpdateValue { bytes: UpdateBytes, } -#[cfg(feature = "migrations")] impl UpdateValue { /// Using a type that is a thin wrapper around bytes but with a custom /// serialization when we don't want to use Borsh necessarily @@ -119,7 +107,6 @@ impl UpdateValue { } } -#[cfg(feature = "migrations")] impl From for UpdateValue { fn from(value: T) -> Self { Self { @@ -196,7 +183,6 @@ pub enum DbUpdateType { impl DbMigration for DbUpdateType {} -#[cfg(feature = "migrations")] impl DbUpdateType { /// Get the key or pattern being modified as string pub fn pattern(&self) -> String { @@ -432,7 +418,6 @@ impl IntoIterator for DbChanges { self.changes.into_iter() } } -#[cfg(feature = "migrations")] impl Display for DbUpdateType { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match self { @@ -484,7 +469,6 @@ pub enum UpdateStatus { Add(Vec<(String, String)>), } -#[cfg(feature = "migrations")] impl Display for UpdateStatus { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { match self { @@ -543,18 +527,14 @@ pub fn commit( } } -#[cfg(feature = "migrations")] derive_borshdeserializer!(Vec::); -#[cfg(feature = "migrations")] derive_borshdeserializer!(Vec::); -#[cfg(feature = "migrations")] derive_borshdeserializer!(u64); #[derive(BorshSerialize, BorshDeserialize)] #[repr(transparent)] pub struct SerializeWrapper(T); -#[cfg(feature = "migrations")] impl TypeHash for SerializeWrapper< std::collections::BTreeMap, @@ -564,7 +544,6 @@ impl TypeHash typehash!(SerializeWrapper>); } -#[cfg(feature = "migrations")] #[distributed_slice(REGISTER_DESERIALIZERS)] static BTREEMAP_STRING_ADDRESS: fn() = || { use std::collections::BTreeMap; diff --git a/crates/sdk/src/queries/mod.rs b/crates/sdk/src/queries/mod.rs index ce338c89fa..d91ffd72dc 100644 --- a/crates/sdk/src/queries/mod.rs +++ b/crates/sdk/src/queries/mod.rs @@ -24,7 +24,6 @@ mod shell; mod types; pub mod vp; -#[cfg(any(test, feature = "async-client"))] const HEIGHT_CAST_ERR: &str = "Failed to cast block height"; // Most commonly expected patterns should be declared first @@ -212,7 +211,6 @@ use crate::tendermint::block::Height; /// A client with async request dispatcher method, which can be used to invoke /// type-safe methods from a root [`Router`], generated /// via `router!` macro. -#[cfg(any(test, feature = "async-client"))] #[cfg_attr(feature = "async-send", async_trait::async_trait)] #[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] pub trait Client { diff --git a/crates/sdk/src/queries/router.rs b/crates/sdk/src/queries/router.rs index ec2e5fe3d8..43e109849b 100644 --- a/crates/sdk/src/queries/router.rs +++ b/crates/sdk/src/queries/router.rs @@ -398,7 +398,6 @@ macro_rules! pattern_and_handler_to_method { #[allow(dead_code)] #[allow(clippy::too_many_arguments)] - #[cfg(any(test, feature = "async-client"))] #[doc = "Request value with optional data (used for e.g. \ `dry_run_tx`), optionally specified height (supported for \ `storage_value`) and optional proof (supported for \ @@ -451,7 +450,6 @@ macro_rules! pattern_and_handler_to_method { #[allow(dead_code)] #[allow(clippy::too_many_arguments)] - #[cfg(any(test, feature = "async-client"))] #[doc = "Request value with optional data (used for e.g. \ `dry_run_tx`), optionally specified height (supported for \ `storage_value`) and optional proof (supported for \ @@ -507,7 +505,6 @@ macro_rules! pattern_and_handler_to_method { #[allow(dead_code)] #[allow(clippy::too_many_arguments)] - #[cfg(any(test, feature = "async-client"))] #[doc = "Request a simple borsh-encoded value from `" $handle "`, \ without any additional request data, specified block height or \ proof."] @@ -729,7 +726,7 @@ macro_rules! router_type { /// Compile time tree patterns router with type-safe dynamic parameter parsing, /// automatic routing, type-safe path constructors and optional client query -/// methods (enabled with `feature = "async-client"`). +/// methods. /// /// The `router!` macro implements greedy matching algorithm. /// diff --git a/crates/sdk/src/queries/shell.rs b/crates/sdk/src/queries/shell.rs index 403cc80851..5a74ca98a5 100644 --- a/crates/sdk/src/queries/shell.rs +++ b/crates/sdk/src/queries/shell.rs @@ -24,7 +24,6 @@ use namada_ibc::event::IbcEventType; use namada_state::{DBIter, LastBlock, StateRead, StorageHasher, DB}; use namada_storage::{ResultExt, StorageRead}; use namada_token::storage_key::masp_token_map_key; -#[cfg(any(test, feature = "async-client"))] use namada_tx::data::DryRunResult; use self::eth_bridge::{EthBridge, ETH_BRIDGE}; diff --git a/crates/sdk/src/queries/vp/pos.rs b/crates/sdk/src/queries/vp/pos.rs index 9bab8a7156..0072a84c8b 100644 --- a/crates/sdk/src/queries/vp/pos.rs +++ b/crates/sdk/src/queries/vp/pos.rs @@ -698,7 +698,6 @@ where } /// Client-only methods for the router type are composed from router functions. -#[cfg(any(test, feature = "async-client"))] pub mod client_only_methods { use super::*; use crate::queries::{Client, RPC}; diff --git a/crates/sdk/src/queries/vp/token.rs b/crates/sdk/src/queries/vp/token.rs index 89d8a15e55..7bae2619a0 100644 --- a/crates/sdk/src/queries/vp/token.rs +++ b/crates/sdk/src/queries/vp/token.rs @@ -44,7 +44,6 @@ where } } -#[cfg(any(test, feature = "async-client"))] pub mod client_only_methods { use borsh::BorshDeserialize; use namada_core::address::Address; diff --git a/crates/sdk/src/validation.rs b/crates/sdk/src/validation.rs index 2ed8ed0210..193cf16a91 100644 --- a/crates/sdk/src/validation.rs +++ b/crates/sdk/src/validation.rs @@ -32,18 +32,21 @@ pub type IbcVp<'a, S, CA> = ibc::vp::Ibc< Eval, ParamsPreStore<'a, S, CA>, GovPreStore<'a, S, CA>, - token::Store< - ibc::vp::context::PseudoExecutionStorage< - 'a, - 'a, - S, - VpCache, - Eval, - >, - >, + TokenStoreForIbcExec<'a, S, CA>, PosPreStore<'a, S, CA>, >; +/// IBC VP pseudo-execution context +pub type IbcVpContext<'view, 'a, S, CA, EVAL> = + ibc::vp::context::PseudoExecutionContext< + 'view, + 'a, + S, + VpCache, + EVAL, + TokenStoreForIbcExec<'a, S, CA>, + >; + /// Native parameters VP pub type ParametersVp<'a, S, CA> = parameters::vp::ParametersVp< 'a, @@ -112,6 +115,17 @@ pub type PosPreStore<'a, S, CA> = proof_of_stake::Store< CtxPreStorageRead<'a, 'a, S, VpCache, Eval>, >; +/// Token store impl over IBC pseudo-execution storage +pub type TokenStoreForIbcExec<'a, S, CA> = token::Store< + ibc::vp::context::PseudoExecutionStorage< + 'a, + 'a, + S, + VpCache, + Eval, + >, +>; + /// Token storage keys implementation pub type TokenKeys = token::Store<()>; diff --git a/crates/shielded_token/Cargo.toml b/crates/shielded_token/Cargo.toml index 470eecf480..a361bfaa9c 100644 --- a/crates/shielded_token/Cargo.toml +++ b/crates/shielded_token/Cargo.toml @@ -17,7 +17,6 @@ default = [] multicore = ["dep:rayon"] testing = [ "multicore", - "validation", "namada_core/testing", "masp_primitives/test-dependencies", ] diff --git a/crates/shielded_token/src/lib.rs b/crates/shielded_token/src/lib.rs index 386dfa503f..3a1450d9a6 100644 --- a/crates/shielded_token/src/lib.rs +++ b/crates/shielded_token/src/lib.rs @@ -22,7 +22,6 @@ mod storage; pub mod storage_key; pub mod utils; pub mod validation; -#[cfg(any(test, feature = "validation", feature = "testing"))] pub mod vp; use std::str::FromStr; diff --git a/crates/shielded_token/src/vp.rs b/crates/shielded_token/src/vp.rs index 1cd351331c..5826b64e1e 100644 --- a/crates/shielded_token/src/vp.rs +++ b/crates/shielded_token/src/vp.rs @@ -30,9 +30,6 @@ use namada_state::{ ConversionState, OptionExt, ResultExt, StateRead, StorageError, }; use namada_trans_token::read_denom; -use namada_trans_token::storage_key::{ - is_any_shielded_action_balance_key, ShieldedActionOwner, -}; use namada_tx::action::Read; use namada_tx::BatchedTxRef; use namada_vp::native_vp::{Ctx, CtxPreStorageRead, NativeVp, VpEvaluator}; @@ -60,17 +57,15 @@ pub enum Error { pub type Result = std::result::Result; /// MASP VP -pub struct MaspVp<'a, S, CA, EVAL, Params, Gov> +pub struct MaspVp<'ctx, S, CA, EVAL, Params, Gov> where S: 'static + StateRead, - EVAL: VpEvaluator<'a, S, CA, EVAL>, + EVAL: VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA, EVAL>, - /// Governance type - pub gov: PhantomData, - /// Parameters type - pub params: PhantomData, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Generic types for DI + pub _marker: PhantomData<(Params, Gov)>, } // The balances changed by the transaction, split between masp and non-masp @@ -157,23 +152,32 @@ impl TryFrom for IbcTransferInfo { } } -impl<'a, S, CA, EVAL, Params, Gov> MaspVp<'a, S, CA, EVAL, Params, Gov> +impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov> + MaspVp<'ctx, S, CA, EVAL, Params, Gov> where S: 'static + StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, Params: parameters::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, Gov: governance::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, { + /// Instantiate MASP VP + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { + Self { + ctx, + _marker: PhantomData, + } + } + /// Return if the parameter change was done via a governance proposal pub fn is_valid_parameter_change( - &'a self, + &'view self, tx: &BatchedTxRef<'_>, ) -> Result<()> { tx.tx.data(tx.cmt).map_or_else( @@ -333,7 +337,7 @@ where // Check that the convert descriptions anchors of a transaction are valid fn valid_convert_descriptions_anchor( - &'a self, + &'view self, transaction: &Transaction, ) -> Result<()> { if let Some(bundle) = transaction.sapling_bundle() { @@ -740,15 +744,13 @@ where // Check that MASP Transaction and state changes are valid fn is_valid_masp_transfer( - &'a self, + &'view self, tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<()> { let masp_epoch_multiplier = - namada_parameters::read_masp_epoch_multiplier_parameter( - self.ctx.state, - )?; + Params::masp_epoch_multiplier(&self.ctx.pre())?; let masp_epoch = MaspEpoch::try_from_epoch( self.ctx.get_block_epoch()?, masp_epoch_multiplier, @@ -1268,25 +1270,25 @@ fn verify_sapling_balancing_value( } } -impl<'a, S, CA, EVAL, Params, Gov> NativeVp<'a> - for MaspVp<'a, S, CA, EVAL, Params, Gov> +impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov> NativeVp<'view> + for MaspVp<'ctx, S, CA, EVAL, Params, Gov> where S: 'static + StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, Params: parameters::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, Gov: governance::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, { type Error = Error; fn validate_tx( - &'a self, + &'view self, tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, diff --git a/crates/state/src/lib.rs b/crates/state/src/lib.rs index 2a8e852c6e..9d4aae39ba 100644 --- a/crates/state/src/lib.rs +++ b/crates/state/src/lib.rs @@ -145,7 +145,6 @@ pub trait StateRead: StorageRead + Debug { fn validity_predicate( &self, addr: &Address, - _: &Params, ) -> Result<(Option, u64)> { let key = if let Address::Implicit(_) = addr { Params::implicit_vp_key() diff --git a/crates/state/src/wl_state.rs b/crates/state/src/wl_state.rs index b301ea9b69..01c0bf3169 100644 --- a/crates/state/src/wl_state.rs +++ b/crates/state/src/wl_state.rs @@ -667,6 +667,30 @@ where } } + /// Borrow in-memory state and DB handle with a mutable temporary write-log. + /// + /// The lifetime of borrows is unsafely extended to `'static` to allow usage + /// in node's `dry_run_tx`, which needs a static lifetime to be able to call + /// protocol's API that is generic over the state with a bound `S: 'static + + /// State` with a mutable reference to this struct. + /// Because the lifetime of `S` is invariant w.r.t. `&mut S` + /// () we are faking a + /// static lifetime of `S` for `TempWlState`. + /// + /// # Safety + /// + /// The caller must guarantee that the source `WlState` is not being + /// accessed mutably before `TempWlState` gets dropped. + pub unsafe fn with_static_temp_write_log( + &self, + ) -> TempWlState<'static, D, H> { + TempWlState { + write_log: WriteLog::default(), + db: &*(&self.db as *const _), + in_mem: &*(&self.in_mem as *const _), + } + } + /// Commit the current transaction's write log and the entire batch to the /// block. Starts a new transaction and batch write log. pub fn commit_tx_batch(&mut self) { diff --git a/crates/state/src/write_log.rs b/crates/state/src/write_log.rs index ffa76ba892..1f1bb1bc06 100644 --- a/crates/state/src/write_log.rs +++ b/crates/state/src/write_log.rs @@ -949,7 +949,7 @@ mod tests { state.commit_block().expect("commit failed"); let (vp_code_hash, _gas) = state - .validity_predicate(&addr1, &namada_parameters::Key) + .validity_predicate::>(&addr1) .expect("vp read failed"); assert_eq!(vp_code_hash, Some(vp1)); let (value, _) = state.db_read(&key1).expect("read failed"); diff --git a/crates/tests/Cargo.toml b/crates/tests/Cargo.toml index 3f8f60e561..a58ee5aa81 100644 --- a/crates/tests/Cargo.toml +++ b/crates/tests/Cargo.toml @@ -13,7 +13,7 @@ repository.workspace = true version.workspace = true [features] -default = ["migrations"] +default = [] mainnet = [ "namada_sdk/mainnet", ] @@ -29,10 +29,14 @@ namada-eth-bridge = [ [dependencies] namada_core = {path = "../core", features = ["testing"]} -namada_sdk = {path = "../sdk", default-features = false, features = ["tendermint-rpc", "download-params", "std", "rand"]} +namada_sdk = {path = "../sdk", features = ["download-params", "testing", "wasm-runtime"]} namada_test_utils = {path = "../test_utils"} +namada_tx_env = {path = "../tx_env"} namada_tx_prelude = {path = "../tx_prelude"} +namada_vp = {path = "../vp"} namada_vp_prelude = {path = "../vp_prelude"} +namada_vm = {path = "../vm", features = ["testing"]} + concat-idents.workspace = true derivative.workspace = true hyper = {version = "0.14.20", features = ["full"]} @@ -53,7 +57,11 @@ wasmer.workspace = true [dev-dependencies] namada_apps_lib = {path = "../apps_lib", features = ["testing"]} namada_node = { path = "../node", features = ["testing"] } +namada_sdk = {path = "../sdk", default-features = false, features = [ + "download-params", "testing", "migrations" +]} namada_vm_env = {path = "../vm_env"} + assert_cmd.workspace = true assert_matches.workspace = true borsh.workspace = true diff --git a/crates/tests/src/e2e/eth_bridge_tests.rs b/crates/tests/src/e2e/eth_bridge_tests.rs index 582a2ca094..c8d9293727 100644 --- a/crates/tests/src/e2e/eth_bridge_tests.rs +++ b/crates/tests/src/e2e/eth_bridge_tests.rs @@ -7,15 +7,10 @@ use std::str::FromStr; use borsh::{BorshDeserialize, BorshSerialize}; use color_eyre::eyre::{eyre, Result}; use expectrl::ControlCode; -use namada::control_flow::time::{Constant, Sleep}; -use namada::core::address::wnam; -use namada::core::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS; -use namada::core::ethereum_events::EthAddress; -use namada::core::storage::{self, Epoch}; -use namada::core::{address, token}; -use namada::eth_bridge::oracle; -use namada::eth_bridge::storage::vote_tallies; -use namada::ledger::eth_bridge::{ +use namada_sdk::control_flow::time::{Constant, Sleep}; +use namada_sdk::eth_bridge::oracle; +use namada_sdk::eth_bridge::storage::vote_tallies; +use namada_sdk::eth_bridge::{ ContractVersion, Contracts, EthereumBridgeParams, MinimumConfirmations, UpgradeableContract, }; @@ -25,6 +20,11 @@ use namada_core::ethereum_events::{ EthereumEvent, TransferToEthereum, TransferToNamada, }; use namada_core::token::Amount; +use namada_sdk::address::wnam; +use namada_sdk::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS; +use namada_sdk::ethereum_events::EthAddress; +use namada_sdk::storage::{self, Epoch}; +use namada_sdk::{address, token}; use namada_test_utils::tx_data::TxWriteData; use namada_test_utils::TestWasms; use tokio::time::{Duration, Instant}; diff --git a/crates/tests/src/e2e/eth_bridge_tests/helpers.rs b/crates/tests/src/e2e/eth_bridge_tests/helpers.rs index 40f9b68bf9..b77944cb15 100644 --- a/crates/tests/src/e2e/eth_bridge_tests/helpers.rs +++ b/crates/tests/src/e2e/eth_bridge_tests/helpers.rs @@ -7,15 +7,15 @@ use data_encoding::HEXLOWER; use eyre::{eyre, Context, Result}; use hyper::client::HttpConnector; use hyper::{Body, Client, Method, Request, StatusCode}; -use namada::core::address::{wnam, Address}; -use namada::core::ethereum_events::{ +use namada_sdk::address::{wnam, Address}; +use namada_sdk::ethereum_events::{ EthAddress, EthereumEvent, TransferToNamada, Uint, }; -use namada::ledger::eth_bridge::{ +use namada_sdk::eth_bridge::{ wrapped_erc20s, ContractVersion, Contracts, EthereumBridgeParams, MinimumConfirmations, UpgradeableContract, }; -use namada::token; +use namada_sdk::token; use namada_apps_lib::config::ethereum_bridge; use crate::e2e::helpers::{ diff --git a/crates/tests/src/e2e/helpers.rs b/crates/tests/src/e2e/helpers.rs index 9bf317bb9d..b495c7a4c0 100644 --- a/crates/tests/src/e2e/helpers.rs +++ b/crates/tests/src/e2e/helpers.rs @@ -15,18 +15,18 @@ use color_eyre::owo_colors::OwoColorize; use data_encoding::HEXLOWER; use escargot::CargoBuild; use eyre::eyre; -use namada::core::address::Address; -use namada::core::key::*; -use namada::core::storage::Epoch; -use namada::ledger::queries::{Rpc, RPC}; -use namada::tendermint_rpc::HttpClient; -use namada::token; use namada_apps_lib::cli::context::ENV_VAR_CHAIN_ID; use namada_apps_lib::config::genesis::chain::DeriveEstablishedAddress; use namada_apps_lib::config::genesis::templates; use namada_apps_lib::config::utils::convert_tm_addr_to_socket_addr; use namada_apps_lib::config::{Config, TendermintMode}; use namada_core::token::NATIVE_MAX_DECIMAL_PLACES; +use namada_sdk::address::Address; +use namada_sdk::key::*; +use namada_sdk::queries::{Rpc, RPC}; +use namada_sdk::storage::Epoch; +use namada_sdk::tendermint_rpc::HttpClient; +use namada_sdk::token; use namada_sdk::wallet::fs::FsWalletUtils; use namada_sdk::wallet::Wallet; use toml::Value; diff --git a/crates/tests/src/e2e/ibc_tests.rs b/crates/tests/src/e2e/ibc_tests.rs index 5161d2aebf..60bdd4970e 100644 --- a/crates/tests/src/e2e/ibc_tests.rs +++ b/crates/tests/src/e2e/ibc_tests.rs @@ -16,71 +16,71 @@ use std::path::{Path, PathBuf}; use color_eyre::eyre::Result; use eyre::eyre; -use namada::core::address::{Address, InternalAddress}; -use namada::core::key::PublicKey; -use namada::core::storage::{BlockHeight, Epoch, Key}; -use namada::core::token::Amount; -use namada::governance::cli::onchain::PgfFunding; -use namada::governance::storage::proposal::{PGFIbcTarget, PGFTarget}; -use namada::ibc::apps::transfer::types::VERSION as ICS20_VERSION; -use namada::ibc::clients::tendermint::client_state::ClientState as TmClientState; -use namada::ibc::clients::tendermint::consensus_state::ConsensusState as TmConsensusState; -use namada::ibc::clients::tendermint::types::{ +use namada_apps_lib::cli::context::ENV_VAR_CHAIN_ID; +use namada_apps_lib::client::rpc::{ + query_pos_parameters, query_storage_value, query_storage_value_bytes, +}; +use namada_apps_lib::client::utils::id_from_pk; +use namada_apps_lib::config::genesis::{chain, templates}; +use namada_apps_lib::config::utils::set_port; +use namada_apps_lib::config::{ethereum_bridge, TendermintMode}; +use namada_apps_lib::facade::tendermint::block::Header as TmHeader; +use namada_apps_lib::facade::tendermint::merkle::proof::ProofOps as TmProof; +use namada_apps_lib::facade::tendermint_rpc::{Client, HttpClient, Url}; +use namada_core::string_encoding::StringEncoded; +use namada_sdk::address::{Address, InternalAddress}; +use namada_sdk::events::extend::ReadFromEventAttributes; +use namada_sdk::governance::cli::onchain::PgfFunding; +use namada_sdk::governance::pgf::ADDRESS as PGF_ADDRESS; +use namada_sdk::governance::storage::proposal::{PGFIbcTarget, PGFTarget}; +use namada_sdk::ibc::apps::transfer::types::VERSION as ICS20_VERSION; +use namada_sdk::ibc::clients::tendermint::client_state::ClientState as TmClientState; +use namada_sdk::ibc::clients::tendermint::consensus_state::ConsensusState as TmConsensusState; +use namada_sdk::ibc::clients::tendermint::types::{ AllowUpdate, ClientState as TmClientStateType, Header as IbcTmHeader, TrustThreshold, }; -use namada::ibc::core::channel::types::channel::Order as ChanOrder; -use namada::ibc::core::channel::types::msgs::{ +use namada_sdk::ibc::core::channel::types::channel::Order as ChanOrder; +use namada_sdk::ibc::core::channel::types::msgs::{ MsgAcknowledgement, MsgChannelOpenAck, MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, MsgRecvPacket as IbcMsgRecvPacket, MsgTimeout as IbcMsgTimeout, }; -use namada::ibc::core::channel::types::packet::Packet; -use namada::ibc::core::channel::types::Version as ChanVersion; -use namada::ibc::core::client::context::client_state::ClientStateCommon; -use namada::ibc::core::client::types::msgs::{ +use namada_sdk::ibc::core::channel::types::packet::Packet; +use namada_sdk::ibc::core::channel::types::Version as ChanVersion; +use namada_sdk::ibc::core::client::context::client_state::ClientStateCommon; +use namada_sdk::ibc::core::client::types::msgs::{ MsgCreateClient, MsgUpdateClient, }; -use namada::ibc::core::client::types::Height; -use namada::ibc::core::commitment_types::commitment::{ +use namada_sdk::ibc::core::client::types::Height; +use namada_sdk::ibc::core::commitment_types::commitment::{ CommitmentPrefix, CommitmentProofBytes, }; -use namada::ibc::core::commitment_types::merkle::MerkleProof; -use namada::ibc::core::connection::types::msgs::{ +use namada_sdk::ibc::core::commitment_types::merkle::MerkleProof; +use namada_sdk::ibc::core::connection::types::msgs::{ MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit, MsgConnectionOpenTry, }; -use namada::ibc::core::connection::types::version::Version as ConnVersion; -use namada::ibc::core::connection::types::Counterparty as ConnCounterparty; -use namada::ibc::core::host::types::identifiers::{ +use namada_sdk::ibc::core::connection::types::version::Version as ConnVersion; +use namada_sdk::ibc::core::connection::types::Counterparty as ConnCounterparty; +use namada_sdk::ibc::core::host::types::identifiers::{ ChainId, ChannelId, ClientId, ConnectionId, PortId, }; -use namada::ibc::event as ibc_events; -use namada::ibc::event::IbcEventType; -use namada::ibc::primitives::proto::Any; -use namada::ibc::primitives::{Signer, ToProto}; -use namada::ledger::ibc::storage::*; -use namada::ledger::parameters::{storage as param_storage, EpochDuration}; -use namada::ledger::pgf::ADDRESS as PGF_ADDRESS; -use namada::ledger::queries::RPC; -use namada::ledger::storage::ics23_specs::ibc_proof_specs; -use namada::sdk::events::extend::ReadFromEventAttributes; -use namada::state::Sha256Hasher; -use namada::tendermint::abci::Event as AbciEvent; -use namada::tendermint::block::Height as TmHeight; -use namada_apps_lib::cli::context::ENV_VAR_CHAIN_ID; -use namada_apps_lib::client::rpc::{ - query_pos_parameters, query_storage_value, query_storage_value_bytes, -}; -use namada_apps_lib::client::utils::id_from_pk; -use namada_apps_lib::config::genesis::{chain, templates}; -use namada_apps_lib::config::utils::set_port; -use namada_apps_lib::config::{ethereum_bridge, TendermintMode}; -use namada_apps_lib::facade::tendermint::block::Header as TmHeader; -use namada_apps_lib::facade::tendermint::merkle::proof::ProofOps as TmProof; -use namada_apps_lib::facade::tendermint_rpc::{Client, HttpClient, Url}; -use namada_core::string_encoding::StringEncoded; +use namada_sdk::ibc::event as ibc_events; +use namada_sdk::ibc::event::IbcEventType; +use namada_sdk::ibc::primitives::proto::Any; +use namada_sdk::ibc::primitives::{Signer, ToProto}; +use namada_sdk::ibc::storage::*; +use namada_sdk::key::PublicKey; use namada_sdk::masp::fs::FsShieldedUtils; +use namada_sdk::parameters::{storage as param_storage, EpochDuration}; +use namada_sdk::queries::RPC; +use namada_sdk::state::ics23_specs::ibc_proof_specs; +use namada_sdk::state::Sha256Hasher; +use namada_sdk::storage::{BlockHeight, Epoch, Key}; +use namada_sdk::tendermint::abci::Event as AbciEvent; +use namada_sdk::tendermint::block::Height as TmHeight; +use namada_sdk::token::Amount; use namada_test_utils::TestWasms; use prost::Message; use setup::constants::*; diff --git a/crates/tests/src/e2e/ledger_tests.rs b/crates/tests/src/e2e/ledger_tests.rs index e4e965b782..97413bb057 100644 --- a/crates/tests/src/e2e/ledger_tests.rs +++ b/crates/tests/src/e2e/ledger_tests.rs @@ -19,15 +19,15 @@ use std::time::{Duration, Instant}; use color_eyre::eyre::Result; use color_eyre::owo_colors::OwoColorize; -use namada::core::address::Address; -use namada::core::storage::Epoch; -use namada::token; use namada_apps_lib::cli::context::ENV_VAR_CHAIN_ID; use namada_apps_lib::config::ethereum_bridge; use namada_apps_lib::config::utils::convert_tm_addr_to_socket_addr; use namada_apps_lib::facade::tendermint_config::net::Address as TendermintAddress; use namada_core::chain::ChainId; use namada_core::token::NATIVE_MAX_DECIMAL_PLACES; +use namada_sdk::address::Address; +use namada_sdk::storage::Epoch; +use namada_sdk::token; use namada_test_utils::TestWasms; use serde::Serialize; use serde_json::json; @@ -1068,9 +1068,9 @@ fn double_signing_gets_slashed() -> Result<()> { use std::net::SocketAddr; use std::str::FromStr; - use namada::core::key::{self, ed25519, SigScheme}; use namada_apps_lib::client; use namada_apps_lib::config::Config; + use namada_sdk::key::{self, ed25519, SigScheme}; let mut pipeline_len = 0; let mut unbonding_len = 0; diff --git a/crates/tests/src/e2e/multitoken_tests/helpers.rs b/crates/tests/src/e2e/multitoken_tests/helpers.rs index 948fc750da..ba6ecbf0ef 100644 --- a/crates/tests/src/e2e/multitoken_tests/helpers.rs +++ b/crates/tests/src/e2e/multitoken_tests/helpers.rs @@ -41,7 +41,7 @@ pub fn init_multitoken_vp(test: &Test, rpc_addr: &str) -> Result { ARBITRARY_SIGNER, "--public-key", // Value obtained from - // `namada::core::key::ed25519::tests::gen_keypair` + // `namada_sdk::key::ed25519::tests::gen_keypair` "001be519a321e29020fa3cbfbfd01bd5e92db134305609270b71dace25b5a21168", "--code-path", &multitoken_vp_wasm_path, diff --git a/crates/tests/src/e2e/setup.rs b/crates/tests/src/e2e/setup.rs index 311fb8f966..ba61380b9b 100644 --- a/crates/tests/src/e2e/setup.rs +++ b/crates/tests/src/e2e/setup.rs @@ -18,7 +18,6 @@ use expectrl::stream::log::LogStream; use expectrl::{ControlCode, Eof, WaitStatus}; use eyre::eyre; use itertools::{Either, Itertools}; -use namada::core::chain::ChainId; use namada_apps_lib::cli::context::ENV_VAR_CHAIN_ID; use namada_apps_lib::client::utils::{ self, validator_pre_genesis_dir, validator_pre_genesis_txs_file, @@ -34,6 +33,7 @@ use namada_core::collections::HashMap; use namada_core::key::{RefTo, SchemeType}; use namada_core::string_encoding::StringEncoded; use namada_core::token::NATIVE_MAX_DECIMAL_PLACES; +use namada_sdk::chain::ChainId; use namada_sdk::wallet::alias::Alias; use namada_tx_prelude::token; use once_cell::sync::Lazy; diff --git a/crates/tests/src/integration/ledger_tests.rs b/crates/tests/src/integration/ledger_tests.rs index fef77e521b..90f17d7359 100644 --- a/crates/tests/src/integration/ledger_tests.rs +++ b/crates/tests/src/integration/ledger_tests.rs @@ -6,9 +6,6 @@ use borsh::BorshDeserialize; use borsh_ext::BorshSerializeExt; use color_eyre::eyre::Result; use data_encoding::HEXLOWER; -use namada::account::AccountPublicKeysMap; -use namada::core::collections::HashMap; -use namada::token::{self, Amount, DenominatedAmount}; use namada_apps_lib::wallet::defaults::{self, albert_keypair}; use namada_core::dec::Dec; use namada_core::hash::Hash; @@ -17,8 +14,11 @@ use namada_core::token::NATIVE_MAX_DECIMAL_PLACES; use namada_node::shell::testing::client::run; use namada_node::shell::testing::node::NodeResults; use namada_node::shell::testing::utils::{Bin, CapturedOutput}; +use namada_sdk::account::AccountPublicKeysMap; +use namada_sdk::collections::HashMap; use namada_sdk::migrations; use namada_sdk::queries::RPC; +use namada_sdk::token::{self, DenominatedAmount}; use namada_sdk::tx::{TX_TRANSFER_WASM, VP_USER_WASM}; use namada_test_utils::TestWasms; use test_log::test; @@ -156,7 +156,7 @@ fn ledger_txs_and_queries() -> Result<()> { vec![ "init-account", "--public-keys", - // Value obtained from `namada::core::key::ed25519::tests::gen_keypair` + // Value obtained from `namada_sdk::key::ed25519::tests::gen_keypair` "tpknam1qpqfzxu3gt05jx2mvg82f4anf90psqerkwqhjey4zlqv0qfgwuvkzt5jhkp", "--threshold", "1", @@ -952,7 +952,8 @@ fn proposal_submission() -> Result<()> { let init_account = vec![ "init-account", "--public-keys", - // Value obtained from `namada::core::key::ed25519::tests::gen_keypair` + // Value obtained from + // `namada_sdk::key::ed25519::tests::gen_keypair` "tpknam1qpqfzxu3gt05jx2mvg82f4anf90psqerkwqhjey4zlqv0qfgwuvkzt5jhkp", "--threshold", "1", @@ -1693,11 +1694,11 @@ fn enforce_fee_payment() -> Result<()> { let mut txs = vec![]; for bytes in txs_bytes { - let mut tx = namada::tx::Tx::deserialize(&bytes).unwrap(); + let mut tx = namada_sdk::tx::Tx::deserialize(&bytes).unwrap(); tx.add_wrapper( - namada::tx::data::wrapper::Fee { + namada_sdk::tx::data::wrapper::Fee { amount_per_gas_unit: DenominatedAmount::native( - Amount::native_whole(1), + token::Amount::native_whole(1), ), token: native_token.clone(), }, @@ -1842,8 +1843,8 @@ fn scheduled_migration() -> Result<()> { )) .expect("Test failed") .data; - let amount = Amount::try_from_slice(&bytes).expect("Test failed"); - assert_eq!(amount, Amount::native_whole(1337)); + let amount = token::Amount::try_from_slice(&bytes).expect("Test failed"); + assert_eq!(amount, token::Amount::native_whole(1337)); // check that no migration is scheduled { @@ -1858,7 +1859,7 @@ fn make_migration_json() -> (Hash, tempfile::NamedTempFile) { let updates = [migrations::DbUpdateType::Add { key: Key::parse("bing/fucking/bong").expect("Test failed"), cf: DbColFam::SUBSPACE, - value: Amount::native_whole(1337).into(), + value: token::Amount::native_whole(1337).into(), force: false, }]; let changes = migrations::DbChanges { diff --git a/crates/tests/src/integration/masp.rs b/crates/tests/src/integration/masp.rs index 336a4fe711..e719c58c1c 100644 --- a/crates/tests/src/integration/masp.rs +++ b/crates/tests/src/integration/masp.rs @@ -3,9 +3,6 @@ use std::str::FromStr; use color_eyre::eyre::Result; use color_eyre::owo_colors::OwoColorize; -use namada::state::{StorageRead, StorageWrite}; -use namada::token::storage_key::masp_token_map_key; -use namada::token::{self, DenominatedAmount}; use namada_apps_lib::wallet::defaults::christel_keypair; use namada_core::dec::Dec; use namada_core::masp::TokenMap; @@ -13,6 +10,9 @@ use namada_node::shell::testing::client::run; use namada_node::shell::testing::node::NodeResults; use namada_node::shell::testing::utils::{Bin, CapturedOutput}; use namada_sdk::masp::fs::FsShieldedUtils; +use namada_sdk::state::{StorageRead, StorageWrite}; +use namada_sdk::token::storage_key::masp_token_map_key; +use namada_sdk::token::{self, DenominatedAmount}; use namada_sdk::DEFAULT_GAS_LIMIT; use test_log::test; @@ -1498,9 +1498,9 @@ fn multiple_unfetched_txs_same_block() -> Result<()> { .clone(); let mut txs = vec![]; for bytes in txs_bytes { - let mut tx = namada::tx::Tx::deserialize(&bytes).unwrap(); + let mut tx = namada_sdk::tx::Tx::deserialize(&bytes).unwrap(); tx.add_wrapper( - namada::tx::data::wrapper::Fee { + namada_sdk::tx::data::wrapper::Fee { amount_per_gas_unit: DenominatedAmount::native(1.into()), token: native_token.clone(), }, @@ -2972,7 +2972,7 @@ fn masp_fee_payment_with_different_token() -> Result<()> { // Whitelist BTC for gas payment genesis.parameters.parameters.minimum_gas_price.insert( "btc".into(), - DenominatedAmount::new(1.into(), namada::token::Denomination(6)), + DenominatedAmount::new(1.into(), token::Denomination(6)), ); genesis })?; diff --git a/crates/tests/src/integration/setup.rs b/crates/tests/src/integration/setup.rs index 7bbc54bbb3..49561b517b 100644 --- a/crates/tests/src/integration/setup.rs +++ b/crates/tests/src/integration/setup.rs @@ -5,8 +5,6 @@ use std::str::FromStr; use std::sync::{Arc, Mutex}; use color_eyre::eyre::{eyre, Result}; -use namada::core::dec::Dec; -use namada::token; use namada_apps_lib::cli::args; use namada_apps_lib::client::utils::PRE_GENESIS_DIR; use namada_apps_lib::config; @@ -25,6 +23,8 @@ use namada_node::shell::testing::node::{ }; use namada_node::shell::testing::utils::TestDir; use namada_node::shell::Shell; +use namada_sdk::dec::Dec; +use namada_sdk::token; use namada_sdk::wallet::alias::Alias; use crate::e2e::setup::{copy_wasm_to_chain_dir, SINGLE_NODE_NET_GENESIS}; diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index ca55eba207..e4717b8c12 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -5,10 +5,8 @@ #![deny(rustdoc::broken_intra_doc_links)] #![deny(rustdoc::private_intra_doc_links)] -pub use namada; - mod vm_host_env; -pub use vm_host_env::{ibc, tx, vp}; +pub use vm_host_env::{tx, vp}; #[cfg(test)] mod e2e; #[cfg(test)] @@ -26,3 +24,5 @@ pub mod strings; pub mod log { pub use test_log::test; } + +pub use namada_sdk::*; diff --git a/crates/tests/src/native_vp/eth_bridge_pool.rs b/crates/tests/src/native_vp/eth_bridge_pool.rs index aefc436f16..686fadc0c6 100644 --- a/crates/tests/src/native_vp/eth_bridge_pool.rs +++ b/crates/tests/src/native_vp/eth_bridge_pool.rs @@ -5,25 +5,24 @@ mod test_bridge_pool_vp { use borsh::BorshDeserialize; use borsh_ext::BorshSerializeExt; - use namada::core::address::testing::{nam, wnam}; - use namada::core::chain::ChainId; - use namada::core::eth_bridge_pool::{ - GasFee, PendingTransfer, TransferToEthereum, TransferToEthereumKind, - }; - use namada::core::ethereum_events::EthAddress; - use namada::core::key::{common, ed25519, SecretKey}; - use namada::core::token::Amount; - use namada::eth_bridge::storage::bridge_pool::BRIDGE_POOL_ADDRESS; - use namada::gas::VpGasMeter; - use namada::ledger::native_vp::ethereum_bridge::bridge_pool_vp::BridgePoolVp; - use namada::tx::Tx; use namada_apps_lib::wallet::defaults::{albert_address, bertha_address}; use namada_apps_lib::wasm_loader; + use namada_sdk::address::testing::{nam, wnam}; + use namada_sdk::chain::ChainId; + use namada_sdk::eth_bridge::storage::bridge_pool::BRIDGE_POOL_ADDRESS; use namada_sdk::eth_bridge::{ wrapped_erc20s, Contracts, Erc20WhitelistEntry, EthereumBridgeParams, UpgradeableContract, }; - use namada_sdk::tx::TX_BRIDGE_POOL_WASM as ADD_TRANSFER_WASM; + use namada_sdk::eth_bridge_pool::{ + GasFee, PendingTransfer, TransferToEthereum, TransferToEthereumKind, + }; + use namada_sdk::ethereum_events::EthAddress; + use namada_sdk::gas::VpGasMeter; + use namada_sdk::key::{common, ed25519, SecretKey}; + use namada_sdk::token::Amount; + use namada_sdk::tx::{Tx, TX_BRIDGE_POOL_WASM as ADD_TRANSFER_WASM}; + use namada_sdk::validation::EthBridgePoolVp; use namada_tx_prelude::BatchedTx; use crate::native_vp::TestNativeVpEnv; @@ -38,7 +37,7 @@ mod test_bridge_pool_vp { /// A signing keypair for good old Bertha. fn bertha_keypair() -> common::SecretKey { // generated from - // [`namada::core::key::ed25519::gen_keypair`] + // [`namada_sdk::key::ed25519::gen_keypair`] let bytes = [ 240, 3, 224, 69, 201, 148, 60, 53, 112, 79, 80, 107, 101, 127, 186, 6, 176, 162, 113, 224, 62, 8, 183, 187, 124, 234, 244, 251, 92, 36, @@ -118,9 +117,9 @@ mod test_bridge_pool_vp { &tx_env.gas_meter.borrow(), )); let vp_env = TestNativeVpEnv::from_tx_env(tx_env, BRIDGE_POOL_ADDRESS); - vp_env - .validate_tx(&gas_meter, |ctx| BridgePoolVp { ctx }) - .is_ok() + + let vp = vp_env.init_vp(&gas_meter, EthBridgePoolVp::new); + vp_env.validate_tx(&vp).is_ok() } fn validate_tx(tx: BatchedTx) { diff --git a/crates/tests/src/native_vp/mod.rs b/crates/tests/src/native_vp/mod.rs index d5867e7d03..f3c659f2ce 100644 --- a/crates/tests/src/native_vp/mod.rs +++ b/crates/tests/src/native_vp/mod.rs @@ -4,16 +4,28 @@ pub mod pos; use std::cell::RefCell; use std::collections::BTreeSet; -use namada::core::address::Address; -use namada::core::storage; -use namada::ledger::gas::VpGasMeter; -use namada::ledger::native_vp::{Ctx, NativeVp}; -use namada::state::testing::TestState; -use namada::vm::WasmCacheRwAccess; +use namada_sdk::address::Address; +use namada_sdk::gas::VpGasMeter; +use namada_sdk::state::testing::TestState; +use namada_sdk::state::StateRead; +use namada_sdk::storage; +use namada_vm::wasm::run::VpEvalWasm; +use namada_vm::wasm::VpCache; +use namada_vm::WasmCacheRwAccess; +use namada_vp::native_vp::{Ctx, NativeVp}; use crate::tx::TestTxEnv; -type NativeVpCtx<'a> = Ctx<'a, TestState, WasmCacheRwAccess>; +type NativeVpCtx<'a> = Ctx< + 'a, + TestState, + VpCache, + VpEvalWasm< + ::D, + ::H, + WasmCacheRwAccess, + >, +>; #[derive(Debug)] pub struct TestNativeVpEnv { @@ -41,15 +53,15 @@ impl TestNativeVpEnv { impl TestNativeVpEnv { /// Run some transaction code `apply_tx` and validate it with a native VP - pub fn validate_tx<'a, T>( - &'a self, - gas_meter: &'a RefCell, - init_native_vp: impl Fn(NativeVpCtx<'a>) -> T, - ) -> Result<(), ::Error> + pub fn init_vp<'view, 'ctx: 'view, T>( + &'ctx self, + gas_meter: &'ctx RefCell, + init_native_vp: impl Fn(NativeVpCtx<'ctx>) -> T, + ) -> T where - T: NativeVp, + T: NativeVp<'view>, { - let ctx = Ctx::new( + let ctx = NativeVpCtx::new( &self.address, &self.tx_env.state, &self.tx_env.batched_tx.tx, @@ -60,9 +72,18 @@ impl TestNativeVpEnv { &self.verifiers, self.tx_env.vp_wasm_cache.clone(), ); - let native_vp = init_native_vp(ctx); + init_native_vp(ctx) + } - native_vp.validate_tx( + /// Run some transaction code `apply_tx` and validate it with a native VP + pub fn validate_tx<'view, 'ctx: 'view, T>( + &'ctx self, + vp: &'view T, + ) -> Result<(), >::Error> + where + T: 'view + NativeVp<'view>, + { + vp.validate_tx( &self.tx_env.batched_tx.to_ref(), &self.keys_changed, &self.verifiers, diff --git a/crates/tests/src/native_vp/pos.rs b/crates/tests/src/native_vp/pos.rs index 1677127b62..b0ae83a158 100644 --- a/crates/tests/src/native_vp/pos.rs +++ b/crates/tests/src/native_vp/pos.rs @@ -95,10 +95,10 @@ //! - add slashes //! - add rewards -use namada::core::storage::Epoch; -use namada::proof_of_stake::parameters::{OwnedPosParams, PosParams}; -use namada::proof_of_stake::test_utils::test_init_genesis as init_genesis; -use namada::proof_of_stake::types::GenesisValidator; +use namada_sdk::proof_of_stake::parameters::{OwnedPosParams, PosParams}; +use namada_sdk::proof_of_stake::test_utils::test_init_genesis as init_genesis; +use namada_sdk::proof_of_stake::types::GenesisValidator; +use namada_sdk::storage::Epoch; use crate::tx::tx_host_env; @@ -149,11 +149,10 @@ mod tests { use std::cell::RefCell; - use namada::core::address; - use namada::core::key::common::PublicKey; - use namada::gas::VpGasMeter; - use namada::ledger::pos::PosVP; - use namada::token; + use namada_sdk::gas::VpGasMeter; + use namada_sdk::key::common::PublicKey; + use namada_sdk::validation::PosVp; + use namada_sdk::{address, token}; use namada_tx_prelude::proof_of_stake::parameters::testing::arb_pos_params; use namada_tx_prelude::Address; use proptest::prelude::*; @@ -440,7 +439,8 @@ mod tests { &tx_env.gas_meter.borrow(), )); let vp_env = TestNativeVpEnv::from_tx_env(tx_env, address::POS); - let result = vp_env.validate_tx(&gas_meter, PosVP::new); + let vp = vp_env.init_vp(&gas_meter, PosVp::new); + let result = vp_env.validate_tx(&vp); // Put the tx_env back before checking the result tx_host_env::set(vp_env.tx_env); @@ -580,22 +580,21 @@ pub mod testing { use derivative::Derivative; use itertools::Either; - use namada::core::dec::Dec; - use namada::core::key::common::PublicKey; - use namada::core::key::RefTo; - use namada::core::storage::Epoch; - use namada::core::{address, key}; - use namada::ledger::gas::TxGasMeter; - use namada::proof_of_stake::epoched::DynEpochOffset; - use namada::proof_of_stake::parameters::testing::arb_rate; - use namada::proof_of_stake::parameters::PosParams; - use namada::proof_of_stake::storage::{ + use namada_sdk::dec::Dec; + use namada_sdk::gas::TxGasMeter; + use namada_sdk::key::common::PublicKey; + use namada_sdk::key::RefTo; + use namada_sdk::proof_of_stake::epoched::DynEpochOffset; + use namada_sdk::proof_of_stake::parameters::testing::arb_rate; + use namada_sdk::proof_of_stake::parameters::PosParams; + use namada_sdk::proof_of_stake::storage::{ get_num_consensus_validators, read_pos_params, unbond_handle, }; - use namada::proof_of_stake::types::{BondId, ValidatorState}; - use namada::proof_of_stake::ADDRESS as POS_ADDRESS; - use namada::token; - use namada::token::{Amount, Change}; + use namada_sdk::proof_of_stake::types::{BondId, ValidatorState}; + use namada_sdk::proof_of_stake::ADDRESS as POS_ADDRESS; + use namada_sdk::storage::Epoch; + use namada_sdk::token::{Amount, Change}; + use namada_sdk::{address, key, token}; use namada_tx_prelude::{Address, StorageRead, StorageWrite}; use proptest::prelude::*; diff --git a/crates/tests/src/storage.rs b/crates/tests/src/storage.rs index 3a5c9a5118..3256105036 100644 --- a/crates/tests/src/storage.rs +++ b/crates/tests/src/storage.rs @@ -3,7 +3,7 @@ use std::rc::Rc; use derivative::Derivative; -use namada::core::storage; +use namada_sdk::storage; /// A list of changes, which must be applied in the same order to get to the /// current state. diff --git a/crates/tests/src/storage_api/collections/lazy_map.rs b/crates/tests/src/storage_api/collections/lazy_map.rs index 2932be34dd..a9b1941177 100644 --- a/crates/tests/src/storage_api/collections/lazy_map.rs +++ b/crates/tests/src/storage_api/collections/lazy_map.rs @@ -3,8 +3,8 @@ mod tests { use std::collections::BTreeMap; use borsh::{BorshDeserialize, BorshSerialize}; - use namada::core::address::{self, Address}; - use namada::core::storage; + use namada_sdk::address::{self, Address}; + use namada_sdk::storage; use namada_tx_prelude::collections::{LazyCollection, LazyMap}; use namada_tx_prelude::storage::KeySeg; use namada_vp_prelude::collection_validation::{self, LazyCollectionExt}; diff --git a/crates/tests/src/storage_api/collections/lazy_set.rs b/crates/tests/src/storage_api/collections/lazy_set.rs index 386bb13a91..b00a273fdb 100644 --- a/crates/tests/src/storage_api/collections/lazy_set.rs +++ b/crates/tests/src/storage_api/collections/lazy_set.rs @@ -2,8 +2,8 @@ mod tests { use std::collections::BTreeSet; - use namada::core::address::{self, Address}; - use namada::core::storage; + use namada_sdk::address::{self, Address}; + use namada_sdk::storage; use namada_tx_prelude::collections::{LazyCollection, LazySet}; use namada_tx_prelude::storage::KeySeg; use namada_vp_prelude::collection_validation::{self, LazyCollectionExt}; diff --git a/crates/tests/src/storage_api/collections/lazy_vec.rs b/crates/tests/src/storage_api/collections/lazy_vec.rs index af19ab8afb..8bbf51ca9c 100644 --- a/crates/tests/src/storage_api/collections/lazy_vec.rs +++ b/crates/tests/src/storage_api/collections/lazy_vec.rs @@ -2,8 +2,8 @@ mod tests { use borsh::{BorshDeserialize, BorshSerialize}; - use namada::core::address::{self, Address}; - use namada::core::storage; + use namada_sdk::address::{self, Address}; + use namada_sdk::storage; use namada_tx_prelude::collections::{lazy_vec, LazyCollection, LazyVec}; use namada_tx_prelude::storage::KeySeg; use namada_vp_prelude::collection_validation::{self, LazyCollectionExt}; diff --git a/crates/tests/src/storage_api/collections/nested_lazy_map.rs b/crates/tests/src/storage_api/collections/nested_lazy_map.rs index f7b0eef995..82159ce23f 100644 --- a/crates/tests/src/storage_api/collections/nested_lazy_map.rs +++ b/crates/tests/src/storage_api/collections/nested_lazy_map.rs @@ -3,8 +3,8 @@ mod tests { use std::collections::BTreeMap; use borsh::{BorshDeserialize, BorshSerialize}; - use namada::core::address::{self, Address}; - use namada::core::storage; + use namada_sdk::address::{self, Address}; + use namada_sdk::storage; use namada_tx_prelude::collections::lazy_map::{ NestedMap, NestedSubKey, SubKey, }; diff --git a/crates/tests/src/vm_host_env/ibc.rs b/crates/tests/src/vm_host_env/ibc.rs index 5b36c96ada..b6cc717dfc 100644 --- a/crates/tests/src/vm_host_env/ibc.rs +++ b/crates/tests/src/vm_host_env/ibc.rs @@ -6,84 +6,78 @@ use ibc_testkit::testapp::ibc::clients::mock::client_state::{ }; use ibc_testkit::testapp::ibc::clients::mock::consensus_state::MockConsensusState; use ibc_testkit::testapp::ibc::clients::mock::header::MockHeader; -use namada::core::address::{self, Address, InternalAddress}; -use namada::core::hash::Hash; -use namada::core::storage::{self, BlockHeight, Epoch, Key, TxIndex}; -use namada::core::time::DurationSecs; -use namada::gas::TxGasMeter; -use namada::governance::parameters::GovernanceParameters; -use namada::ibc::apps::transfer::types::error::TokenTransferError; -use namada::ibc::apps::transfer::types::msgs::transfer::MsgTransfer as IbcMsgTransfer; -use namada::ibc::apps::transfer::types::packet::PacketData; -use namada::ibc::apps::transfer::types::{ +use namada_core::collections::HashMap; +use namada_core::storage::testing::get_dummy_header; +use namada_sdk::address::{self, Address, InternalAddress}; +use namada_sdk::gas::{TxGasMeter, VpGasMeter}; +use namada_sdk::governance::parameters::GovernanceParameters; +use namada_sdk::hash::Hash; +use namada_sdk::ibc::apps::transfer::types::error::TokenTransferError; +use namada_sdk::ibc::apps::transfer::types::msgs::transfer::MsgTransfer as IbcMsgTransfer; +use namada_sdk::ibc::apps::transfer::types::packet::PacketData; +use namada_sdk::ibc::apps::transfer::types::{ ack_success_b64, PrefixedCoin, VERSION, }; -use namada::ibc::core::channel::types::acknowledgement::{ +use namada_sdk::ibc::core::channel::types::acknowledgement::{ AcknowledgementStatus, StatusValue, }; -use namada::ibc::core::channel::types::channel::{ +use namada_sdk::ibc::core::channel::types::channel::{ ChannelEnd, Counterparty as ChanCounterparty, Order, State as ChanState, }; -use namada::ibc::core::channel::types::msgs::{ +use namada_sdk::ibc::core::channel::types::msgs::{ MsgAcknowledgement, MsgChannelCloseConfirm, MsgChannelCloseInit, MsgChannelOpenAck, MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, MsgRecvPacket, MsgTimeout, MsgTimeoutOnClose, }; -pub use namada::ibc::core::channel::types::packet::Packet; -use namada::ibc::core::channel::types::timeout::TimeoutHeight; -use namada::ibc::core::channel::types::Version as ChanVersion; -use namada::ibc::core::client::types::msgs::{ - MsgCreateClient, MsgUpdateClient, MsgUpgradeClient, +pub use namada_sdk::ibc::core::channel::types::packet::Packet; +use namada_sdk::ibc::core::channel::types::timeout::TimeoutHeight; +use namada_sdk::ibc::core::channel::types::Version as ChanVersion; +use namada_sdk::ibc::core::client::types::msgs::{ + MsgCreateClient, MsgUpdateClient, }; -use namada::ibc::core::client::types::Height; -use namada::ibc::core::commitment_types::commitment::{ +use namada_sdk::ibc::core::client::types::Height; +use namada_sdk::ibc::core::commitment_types::commitment::{ CommitmentPrefix, CommitmentProofBytes, }; -use namada::ibc::core::connection::types::msgs::{ +use namada_sdk::ibc::core::connection::types::msgs::{ MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit, MsgConnectionOpenTry, }; -use namada::ibc::core::connection::types::version::Version as ConnVersion; -use namada::ibc::core::connection::types::{ +use namada_sdk::ibc::core::connection::types::version::Version as ConnVersion; +use namada_sdk::ibc::core::connection::types::{ ConnectionEnd, Counterparty as ConnCounterparty, State as ConnState, }; -pub use namada::ibc::core::host::types::identifiers::{ +pub use namada_sdk::ibc::core::host::types::identifiers::{ ChannelId, ClientId, ConnectionId, PortId, Sequence, }; -use namada::ibc::primitives::proto::{Any, Protobuf}; -use namada::ibc::primitives::Timestamp; -use namada::ibc::MsgTransfer; -use namada::ledger::gas::VpGasMeter; -use namada::ledger::ibc::parameters::IbcParameters; -pub use namada::ledger::ibc::storage::{ - ack_key, channel_counter_key, channel_key, client_counter_key, - client_state_key, client_update_height_key, client_update_timestamp_key, - commitment_key, connection_counter_key, connection_key, - consensus_state_key, next_sequence_ack_key, next_sequence_recv_key, - next_sequence_send_key, port_key, receipt_key, +use namada_sdk::ibc::parameters::IbcParameters; +use namada_sdk::ibc::primitives::proto::{Any, Protobuf}; +use namada_sdk::ibc::primitives::Timestamp; +pub use namada_sdk::ibc::storage::{ + channel_key, client_counter_key, client_state_key, + client_update_height_key, client_update_timestamp_key, + connection_counter_key, connection_key, consensus_state_key, port_key, }; -pub use namada::ledger::ibc::trace::ibc_token; -use namada::ledger::native_vp::ibc::Ibc; -use namada::ledger::native_vp::multitoken::{ - Error as MultitokenVpError, MultitokenVp, -}; -use namada::ledger::native_vp::{Ctx, NativeVp}; -use namada::ledger::parameters::storage::get_epoch_duration_storage_key; -use namada::ledger::parameters::EpochDuration; -use namada::ledger::tx_env::TxEnv; -use namada::ledger::{ibc, pos}; -use namada::proof_of_stake::OwnedPosParams; -use namada::state::testing::TestState; -use namada::tendermint::time::Time as TmTime; -use namada::token::{self, Amount}; -use namada::tx::BatchedTxRef; -use namada::vm::{wasm, WasmCacheRwAccess}; -use namada_core::collections::HashMap; -use namada_core::storage::testing::get_dummy_header; -use namada_proof_of_stake::test_utils::get_dummy_genesis_validator; +pub use namada_sdk::ibc::trace::ibc_token; +use namada_sdk::ibc::MsgTransfer; +use namada_sdk::parameters::storage::get_epoch_duration_storage_key; +use namada_sdk::parameters::EpochDuration; +use namada_sdk::proof_of_stake::test_utils::get_dummy_genesis_validator; +use namada_sdk::proof_of_stake::OwnedPosParams; +use namada_sdk::state::testing::TestState; use namada_sdk::state::StateRead; +use namada_sdk::storage::{self, BlockHeight, Epoch, Key, TxIndex}; +use namada_sdk::tendermint::time::Time as TmTime; +use namada_sdk::time::DurationSecs; +use namada_sdk::token::vp::MultitokenError; +use namada_sdk::tx::BatchedTxRef; +use namada_sdk::validation::{IbcVp, MultitokenVp}; +use namada_sdk::{ibc, proof_of_stake, token}; use namada_test_utils::TestWasms; +use namada_tx_env::TxEnv; use namada_tx_prelude::BorshSerializeExt; +use namada_vm::{wasm, WasmCacheRwAccess}; +use namada_vp::native_vp::{Ctx, NativeVp}; use crate::tx::*; @@ -92,14 +86,14 @@ pub const ANY_DENOMINATION: u8 = token::NATIVE_MAX_DECIMAL_PLACES; const COMMITMENT_PREFIX: &[u8] = b"ibc"; pub struct TestIbcVp<'a> { - pub ibc: Ibc<'a, TestState, WasmCacheRwAccess>, + pub ibc: IbcVp<'a, TestState, WasmCacheRwAccess>, } impl<'a> TestIbcVp<'a> { pub fn validate( &self, batched_tx: &BatchedTxRef, - ) -> std::result::Result<(), namada::ledger::native_vp::ibc::Error> { + ) -> std::result::Result<(), namada_sdk::ibc::vp::Error> { self.ibc.validate_tx( batched_tx, self.ibc.ctx.keys_changed, @@ -116,7 +110,7 @@ impl<'a> TestMultitokenVp<'a> { pub fn validate( &self, batched_tx: &BatchedTxRef, - ) -> std::result::Result<(), MultitokenVpError> { + ) -> std::result::Result<(), MultitokenError> { self.multitoken_vp.validate_tx( batched_tx, self.multitoken_vp.ctx.keys_changed, @@ -129,7 +123,7 @@ impl<'a> TestMultitokenVp<'a> { pub fn validate_ibc_vp_from_tx<'a>( tx_env: &'a TestTxEnv, batched_tx: &'a BatchedTxRef, -) -> std::result::Result<(), namada::ledger::native_vp::ibc::Error> { +) -> std::result::Result<(), namada_sdk::ibc::vp::Error> { let (verifiers, keys_changed) = tx_env .state .write_log() @@ -158,7 +152,7 @@ pub fn validate_ibc_vp_from_tx<'a>( &verifiers, vp_wasm_cache, ); - let ibc = Ibc { ctx }; + let ibc = IbcVp::new(ctx); TestIbcVp { ibc }.validate(batched_tx) } @@ -168,7 +162,7 @@ pub fn validate_multitoken_vp_from_tx<'a>( tx_env: &'a TestTxEnv, batched_tx: &'a BatchedTxRef, target: &Key, -) -> std::result::Result<(), MultitokenVpError> { +) -> std::result::Result<(), MultitokenError> { let (verifiers, keys_changed) = tx_env .state .write_log() @@ -197,7 +191,7 @@ pub fn validate_multitoken_vp_from_tx<'a>( &verifiers, vp_wasm_cache, ); - let multitoken_vp = MultitokenVp { ctx }; + let multitoken_vp = MultitokenVp::new(ctx); TestMultitokenVp { multitoken_vp }.validate(batched_tx) } @@ -209,16 +203,18 @@ pub fn init_storage() -> (Address, Address) { let code_hash = Hash::sha256(&code); tx_host_env::with(|env| { - namada::parameters::init_test_storage(&mut env.state).unwrap(); + namada_sdk::parameters::init_test_storage(&mut env.state).unwrap(); ibc::init_genesis_storage(&mut env.state); let gov_params = GovernanceParameters::default(); gov_params.init_storage(&mut env.state).unwrap(); let ibc_params = IbcParameters { - default_mint_limit: Amount::native_whole(100), - default_per_epoch_throughput_limit: Amount::native_whole(100), + default_mint_limit: token::Amount::native_whole(100), + default_per_epoch_throughput_limit: token::Amount::native_whole( + 100, + ), }; ibc_params.init_storage(&mut env.state).unwrap(); - pos::test_utils::test_init_genesis( + proof_of_stake::test_utils::test_init_genesis( &mut env.state, OwnedPosParams::default(), vec![get_dummy_genesis_validator()].into_iter(), @@ -248,7 +244,7 @@ pub fn init_storage() -> (Address, Address) { .init_account(code_hash, &None, &[]) .unwrap(); let key = token::storage_key::balance_key(&token, &account); - let init_bal = Amount::from_uint(100, token_denom).unwrap(); + let init_bal = token::Amount::from_uint(100, token_denom).unwrap(); tx_host_env::with(|env| { env.state .db_write(&denom_key, &token_denom.serialize_to_vec()) @@ -422,18 +418,6 @@ pub fn msg_update_client(client_id: ClientId) -> MsgUpdateClient { } } -pub fn msg_upgrade_client(client_id: ClientId) -> MsgUpgradeClient { - let (client_state, consensus_state) = dummy_client(); - MsgUpgradeClient { - client_id, - upgraded_client_state: client_state.into(), - upgraded_consensus_state: consensus_state.into(), - proof_upgrade_client: dummy_proof(), - proof_upgrade_consensus_state: dummy_proof(), - signer: "test".to_string().into(), - } -} - pub fn msg_connection_open_init(client_id: ClientId) -> MsgConnectionOpenInit { let counterparty_client_id = ClientId::new(&client_type().to_string(), 42).unwrap(); @@ -617,10 +601,6 @@ pub fn dummy_channel_counterparty() -> ChanCounterparty { ChanCounterparty::new(port_id, Some(channel_id)) } -pub fn unorder_channel(channel: &mut ChannelEnd) { - channel.ordering = Order::Unordered; -} - pub fn msg_transfer( port_id: PortId, channel_id: ChannelId, @@ -634,7 +614,7 @@ pub fn msg_transfer( packet_data: PacketData { token: PrefixedCoin { denom: denom.parse().expect("invalid denom"), - amount: Amount::native_whole(100).into(), + amount: token::Amount::native_whole(100).into(), }, sender: sender.to_string().into(), receiver: address::testing::gen_established_address() @@ -687,7 +667,7 @@ pub fn received_packet( let timestamp = (Timestamp::now() + Duration::from_secs(100)).unwrap(); let coin = PrefixedCoin { denom: token.parse().expect("invalid denom"), - amount: Amount::native_whole(100).into(), + amount: token::Amount::native_whole(100).into(), }; let sender = address::testing::gen_established_address(); let data = PacketData { diff --git a/crates/tests/src/vm_host_env/mod.rs b/crates/tests/src/vm_host_env/mod.rs index 921d516652..290b4a65af 100644 --- a/crates/tests/src/vm_host_env/mod.rs +++ b/crates/tests/src/vm_host_env/mod.rs @@ -11,6 +11,7 @@ //! //! `RUST_LOG=debug cargo test test_tx_read_write -- --nocapture` +#[cfg(test)] pub mod ibc; pub mod tx; pub mod vp; @@ -23,25 +24,24 @@ mod tests { use borsh_ext::BorshSerializeExt; use itertools::Itertools; - use namada::account::pks_handle; - use namada::core::hash::Hash; - use namada::core::key::*; - use namada::core::storage::{self, BlockHeight, Key, KeySeg}; - use namada::core::time::DateTimeUtc; - use namada::core::{address, key}; - use namada::ibc::context::nft_transfer_mod::testing::DummyNftTransferModule; - use namada::ibc::context::transfer_mod::testing::DummyTransferModule; - use namada::ibc::primitives::ToProto; - use namada::ibc::Error as IbcActionError; - use namada::ledger::ibc::{ - storage as ibc_storage, storage as ibc_storage, trace as ibc_trace, - }; - use namada::ledger::native_vp::ibc::Error as IbcError; - use namada::ledger::tx_env::TxEnv; - use namada::token::{self, Amount}; - use namada::tx::Tx; use namada_core::storage::testing::get_dummy_header; + use namada_sdk::account::pks_handle; + use namada_sdk::hash::Hash; + use namada_sdk::ibc::context::nft_transfer_mod::testing::DummyNftTransferModule; + use namada_sdk::ibc::context::transfer_mod::testing::DummyTransferModule; + use namada_sdk::ibc::primitives::ToProto; + use namada_sdk::ibc::vp::Error as IbcError; + use namada_sdk::ibc::{ + storage as ibc_storage, trace as ibc_trace, Error as IbcActionError, + }; + use namada_sdk::key::*; + use namada_sdk::storage::{self, BlockHeight, Key, KeySeg}; + use namada_sdk::time::DateTimeUtc; + use namada_sdk::token::{self, Amount}; + use namada_sdk::tx::Tx; + use namada_sdk::{address, key}; use namada_test_utils::TestWasms; + use namada_tx_env::TxEnv; use namada_tx_prelude::address::InternalAddress; use namada_tx_prelude::chain::ChainId; use namada_tx_prelude::{Address, BatchedTx, StorageRead, StorageWrite}; diff --git a/crates/tests/src/vm_host_env/tx.rs b/crates/tests/src/vm_host_env/tx.rs index e6870f0c49..776c53f0cf 100644 --- a/crates/tests/src/vm_host_env/tx.rs +++ b/crates/tests/src/vm_host_env/tx.rs @@ -3,23 +3,23 @@ use std::cell::RefCell; use std::collections::BTreeSet; use std::rc::Rc; -use namada::core::address::Address; -use namada::core::hash::Hash; -use namada::core::storage::{Key, TxIndex}; -use namada::core::time::DurationSecs; -use namada::ledger::gas::TxGasMeter; -use namada::ledger::parameters::{self, EpochDuration}; -use namada::ledger::storage::mockdb::MockDB; -use namada::ledger::storage::testing::TestState; -pub use namada::tx::data::TxType; -use namada::tx::Tx; -use namada::vm::prefix_iter::PrefixIterators; -use namada::vm::wasm::run::Error; -use namada::vm::wasm::{self, TxCache, VpCache}; -use namada::vm::{self, WasmCacheRwAccess}; -use namada::{account, token}; +use namada_sdk::address::Address; +use namada_sdk::gas::TxGasMeter; +use namada_sdk::hash::Hash; +use namada_sdk::parameters::{self, EpochDuration}; +use namada_sdk::state::prefix_iter::PrefixIterators; +use namada_sdk::state::testing::TestState; +use namada_sdk::storage::mockdb::MockDB; +use namada_sdk::storage::{Key, TxIndex}; +use namada_sdk::time::DurationSecs; +pub use namada_sdk::tx::data::TxType; +pub use namada_sdk::tx::*; +use namada_sdk::{account, token}; use namada_tx_prelude::transaction::TxSentinel; -use namada_tx_prelude::{BatchedTx, BorshSerializeExt, Ctx}; +use namada_tx_prelude::{BorshSerializeExt, Ctx}; +use namada_vm::wasm::run::Error; +use namada_vm::wasm::{self, TxCache, VpCache}; +use namada_vm::WasmCacheRwAccess; use namada_vp_prelude::key::common; use tempfile::TempDir; @@ -246,7 +246,7 @@ mod native_tx_host_env { // TODO replace with `std::concat_idents` once stabilized (https://github.com/rust-lang/rust/issues/29599) use concat_idents::concat_idents; - use namada::vm::host_env::*; + use namada_vm::host_env::*; use super::*; @@ -355,7 +355,7 @@ mod native_tx_host_env { wasmer_store: _, }: &mut TestTxEnv| { - let mut tx_env = vm::host_env::testing::tx_env( + let mut tx_env = namada_vm::host_env::testing::tx_env( state, iterators, verifiers, @@ -400,7 +400,7 @@ mod native_tx_host_env { wasmer_store: _, }: &mut TestTxEnv| { - let mut tx_env = vm::host_env::testing::tx_env( + let mut tx_env = namada_vm::host_env::testing::tx_env( state, iterators, verifiers, @@ -445,7 +445,7 @@ mod native_tx_host_env { wasmer_store: _, }: &mut TestTxEnv| { - let mut tx_env = vm::host_env::testing::tx_env( + let mut tx_env = namada_vm::host_env::testing::tx_env( state, iterators, verifiers, @@ -534,11 +534,11 @@ mod native_tx_host_env { #[cfg(test)] mod tests { - use namada::core::storage; - use namada::vm::host_env::{self, TxVmEnv}; - use namada::vm::memory::VmMemory; use namada_core::hash::Sha256Hasher; + use namada_sdk::storage; use namada_tx_prelude::StorageWrite; + use namada_vm::host_env::{self, TxVmEnv}; + use namada_vm::memory::VmMemory; use proptest::prelude::*; use test_log::test; @@ -761,7 +761,7 @@ mod tests { wasmer_store, } = test_env; - let mut tx_env = vm::host_env::testing::tx_env_with_wasm_memory( + let mut tx_env = host_env::testing::tx_env_with_wasm_memory( state, iterators, verifiers, @@ -793,7 +793,7 @@ mod tests { any::(), any::(), any::(), - namada::core::storage::testing::arb_key(), + namada_sdk::storage::testing::arb_key(), arb_u64(), arb_u64(), any::>(), diff --git a/crates/tests/src/vm_host_env/vp.rs b/crates/tests/src/vm_host_env/vp.rs index 32fe5b7349..4c7843e29e 100644 --- a/crates/tests/src/vm_host_env/vp.rs +++ b/crates/tests/src/vm_host_env/vp.rs @@ -1,18 +1,17 @@ use std::cell::RefCell; use std::collections::BTreeSet; -use namada::core::address::{self, Address}; -use namada::core::storage::{self, Key, TxIndex}; -use namada::gas::TxGasMeter; -use namada::ledger::gas::VpGasMeter; -use namada::ledger::storage::mockdb::MockDB; -use namada::ledger::storage::testing::TestState; -use namada::tx::data::TxType; -use namada::tx::Tx; -use namada::vm::prefix_iter::PrefixIterators; -use namada::vm::wasm::{self, VpCache}; -use namada::vm::{self, WasmCacheRwAccess}; +use namada_sdk::address::{self, Address}; +use namada_sdk::gas::{TxGasMeter, VpGasMeter}; +use namada_sdk::state::mockdb::MockDB; +use namada_sdk::state::prefix_iter::PrefixIterators; +use namada_sdk::state::testing::TestState; +use namada_sdk::storage::{self, Key, TxIndex}; +use namada_sdk::tx::data::TxType; +use namada_sdk::tx::Tx; use namada_tx_prelude::BatchedTx; +use namada_vm::wasm::{self, VpCache}; +use namada_vm::WasmCacheRwAccess; use namada_vp_prelude::Ctx; use tempfile::TempDir; @@ -57,10 +56,7 @@ pub struct TestVpEnv { impl Default for TestVpEnv { fn default() -> Self { - #[cfg(feature = "wasm-runtime")] - let eval_runner = namada::vm::wasm::run::VpEvalWasm::default(); - #[cfg(not(feature = "wasm-runtime"))] - let eval_runner = native_vp_host_env::VpEval; + let eval_runner = wasm::run::VpEvalWasm::default(); let (vp_wasm_cache, vp_cache_dir) = wasm::compilation_cache::common::testing::cache(); @@ -112,19 +108,16 @@ mod native_vp_host_env { // TODO replace with `std::concat_idents` once stabilized (https://github.com/rust-lang/rust/issues/29599) use concat_idents::concat_idents; - use namada::state::StateRead; - use namada::vm::host_env::*; + use namada_sdk::state::StateRead; + use namada_vm::host_env::*; use super::*; - #[cfg(feature = "wasm-runtime")] - pub type VpEval = namada::vm::wasm::run::VpEvalWasm< + pub type VpEval = namada_vm::wasm::run::VpEvalWasm< ::D, ::H, WasmCacheRwAccess, >; - #[cfg(not(feature = "wasm-runtime"))] - pub struct VpEval; thread_local! { /// A [`TestVpEnv`] that can be used for VP host env functions calls @@ -222,26 +215,6 @@ mod native_vp_host_env { set(vp_env); } - #[cfg(not(feature = "wasm-runtime"))] - impl VpEvaluator for VpEval { - type CA = WasmCacheRwAccess; - type Db = MockDB; - type Eval = VpEval; - type H = Sha256Hasher; - - fn eval( - &self, - _ctx: VpCtx<'static, Self::Db, Self::H, Self::Eval, Self::CA>, - _vp_code_hash: Vec, - _input_data: Vec, - ) -> namada::core::internal::HostEnvResult { - unimplemented!( - "The \"wasm-runtime\" feature must be enabled to test with \ - the `eval` function." - ) - } - } - /// A helper macro to create implementations of the host environment /// functions exported to wasm, which uses the environment from the /// `ENV` variable. @@ -267,7 +240,7 @@ mod native_vp_host_env { vp_cache_dir: _, }: &mut TestVpEnv| { - let mut env = vm::host_env::testing::vp_env( + let mut env = namada_vm::host_env::testing::vp_env( addr, state, iterators, @@ -312,7 +285,7 @@ mod native_vp_host_env { vp_cache_dir: _, }: &mut TestVpEnv| { - let mut env = vm::host_env::testing::vp_env( + let mut env = namada_vm::host_env::testing::vp_env( addr, state, iterators, diff --git a/crates/token/Cargo.toml b/crates/token/Cargo.toml index 175f73b54a..97ac5f7103 100644 --- a/crates/token/Cargo.toml +++ b/crates/token/Cargo.toml @@ -32,5 +32,6 @@ serde.workspace = true [dev-dependencies] namada_core = { path = "../core", features = ["testing"] } +namada_shielded_token = { path = "../shielded_token", features = ["testing"] } proptest.workspace = true diff --git a/crates/token/src/lib.rs b/crates/token/src/lib.rs index 0bffc87ea6..4faf13d99c 100644 --- a/crates/token/src/lib.rs +++ b/crates/token/src/lib.rs @@ -24,7 +24,6 @@ pub use namada_shielded_token::*; pub use namada_trans_token::*; /// Validity predicates -#[cfg(any(test, feature = "validation", feature = "testing"))] pub mod vp { pub use namada_shielded_token::vp::{ Error as MaspError, MaspVp, Result as MaspResult, diff --git a/crates/trans_token/src/vp.rs b/crates/trans_token/src/vp.rs index 433a8cdd74..fc66a64092 100644 --- a/crates/trans_token/src/vp.rs +++ b/crates/trans_token/src/vp.rs @@ -46,38 +46,36 @@ pub enum Error { pub type Result = std::result::Result; /// Multitoken VP -pub struct MultitokenVp<'a, S, CA, EVAL, Params, Gov> +pub struct MultitokenVp<'ctx, S, CA, EVAL, Params, Gov> where S: 'static + StateRead, - EVAL: VpEvaluator<'a, S, CA, EVAL>, + EVAL: VpEvaluator<'ctx, S, CA, EVAL>, { /// Context to interact with the host structures. - pub ctx: Ctx<'a, S, CA, EVAL>, - /// Parameters type - pub params: PhantomData, - /// Governance type - pub gov: PhantomData, + pub ctx: Ctx<'ctx, S, CA, EVAL>, + /// Generic types for DI + pub _marker: PhantomData<(Params, Gov)>, } -impl<'a, S, CA, EVAL, Params, Gov> NativeVp<'a> - for MultitokenVp<'a, S, CA, EVAL, Params, Gov> +impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov> NativeVp<'view> + for MultitokenVp<'ctx, S, CA, EVAL, Params, Gov> where S: 'static + StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, Params: parameters::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, Gov: governance::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, { type Error = Error; fn validate_tx( - &'a self, + &'view self, tx_data: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, @@ -296,26 +294,26 @@ where } } -impl<'a, S, CA, EVAL, Params, Gov> MultitokenVp<'a, S, CA, EVAL, Params, Gov> +impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov> + MultitokenVp<'ctx, S, CA, EVAL, Params, Gov> where S: 'static + StateRead, CA: 'static + Clone, - EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, + EVAL: 'static + VpEvaluator<'ctx, S, CA, EVAL>, Params: parameters::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, Gov: governance::Read< - CtxPreStorageRead<'a, 'a, S, CA, EVAL>, + CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, { /// Instantiate token VP - pub fn new(ctx: Ctx<'a, S, CA, EVAL>) -> Self { + pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { Self { ctx, - params: PhantomData, - gov: PhantomData, + _marker: PhantomData, } } @@ -356,7 +354,7 @@ where /// Return if the parameter change was done via a governance proposal pub fn is_valid_parameter( - &'a self, + &'view self, batched_tx: &BatchedTxRef<'_>, ) -> Result<()> { batched_tx.tx.data(batched_tx.cmt).map_or_else( @@ -441,9 +439,8 @@ mod tests { }; use namada_core::borsh::BorshSerializeExt; use namada_core::key::testing::keypair_1; - use namada_core::WasmCacheRwAccess; use namada_gas::{TxGasMeter, VpGasMeter}; - use namada_ibc::storage::ibc_token; + use namada_ibc::trace::ibc_token; use namada_parameters::storage::get_native_token_transferable_key; use namada_state::testing::TestState; use namada_state::StorageWrite; @@ -454,6 +451,7 @@ mod tests { use namada_vm::wasm::compilation_cache::common::testing::vp_cache; use namada_vm::wasm::run::VpEvalWasm; use namada_vm::wasm::VpCache; + use namada_vm::WasmCacheRwAccess; use super::*; use crate::storage_key::{balance_key, minted_balance_key}; @@ -466,17 +464,17 @@ mod tests { ::H, CA, >; - type Ctx<'a> = super::Ctx<'a, TestState, VpCache, Eval>; - type MultitokenVp<'a> = super::MultitokenVp< - 'a, + type Ctx<'ctx> = super::Ctx<'ctx, TestState, VpCache, Eval>; + type MultitokenVp<'ctx> = super::MultitokenVp< + 'ctx, TestState, VpCache, Eval, namada_parameters::Store< - CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + CtxPreStorageRead<'ctx, 'ctx, TestState, VpCache, Eval>, >, namada_governance::Store< - CtxPreStorageRead<'a, 'a, TestState, VpCache, Eval>, + CtxPreStorageRead<'ctx, 'ctx, TestState, VpCache, Eval>, >, >; diff --git a/crates/tx_prelude/Cargo.toml b/crates/tx_prelude/Cargo.toml index 46bcb953ca..a15aac9bfd 100644 --- a/crates/tx_prelude/Cargo.toml +++ b/crates/tx_prelude/Cargo.toml @@ -20,6 +20,7 @@ testing = ["namada_core/testing", "namada_token/testing"] namada_account = { path = "../account" } namada_core = { path = "../core" } namada_events = { path = "../events", default-features = false } +namada_gas = { path = "../gas" } namada_governance = { path = "../governance" } namada_ibc = { path = "../ibc" } namada_macros = { path = "../macros" } diff --git a/crates/tx_prelude/src/ibc.rs b/crates/tx_prelude/src/ibc.rs index 8396d6c392..8e83088ab9 100644 --- a/crates/tx_prelude/src/ibc.rs +++ b/crates/tx_prelude/src/ibc.rs @@ -34,7 +34,7 @@ pub fn ibc_actions(ctx: &mut Ctx) -> IbcActions<'_, Ctx> { actions } -impl<'s> IbcStorageContext for Ctx { +impl IbcStorageContext for Ctx { type Storage = Self; fn storage(&self) -> &Self::Storage { diff --git a/crates/tx_prelude/src/lib.rs b/crates/tx_prelude/src/lib.rs index 294815942e..8dae255f69 100644 --- a/crates/tx_prelude/src/lib.rs +++ b/crates/tx_prelude/src/lib.rs @@ -50,7 +50,10 @@ pub use namada_tx::{action, data as transaction, BatchedTx, Section, Tx}; pub use namada_tx_env::TxEnv; use namada_vm_env::tx::*; use namada_vm_env::{read_from_buffer, read_key_val_bytes_from_buffer}; -pub use {namada_governance as governance, namada_parameters as parameters}; +pub use { + namada_gas as gas, namada_governance as governance, + namada_parameters as parameters, +}; /// Log a string. The message will be printed at the `tracing::Level::Info`. pub fn log_string>(msg: T) { diff --git a/crates/tx_prelude/src/proof_of_stake.rs b/crates/tx_prelude/src/proof_of_stake.rs index 97ed43735c..72b376a184 100644 --- a/crates/tx_prelude/src/proof_of_stake.rs +++ b/crates/tx_prelude/src/proof_of_stake.rs @@ -12,7 +12,7 @@ use namada_proof_of_stake::{ claim_reward_tokens, deactivate_validator, reactivate_validator, redelegate_tokens, unbond_tokens, unjail_validator, withdraw_tokens, }; -pub use namada_proof_of_stake::{parameters, types}; +pub use namada_proof_of_stake::{parameters, storage, storage_key, types}; use namada_tx::action::{ Action, ClaimRewards, PosAction, Redelegation, Unbond, Withdraw, Write, }; diff --git a/crates/vm/Cargo.toml b/crates/vm/Cargo.toml index a8e94313ab..779433090a 100644 --- a/crates/vm/Cargo.toml +++ b/crates/vm/Cargo.toml @@ -29,11 +29,10 @@ testing = [ ] [dependencies] -namada_core = { path = "../core" } +namada_core = { path = "../core", features = ["control_flow"] } namada_events = { path = "../events", default-features = false } namada_gas = { path = "../gas" } namada_parameters = { path = "../parameters" } -namada_sdk = { path = "../sdk" } namada_state = { path = "../state" } namada_token = { path = "../token" } namada_tx = { path = "../tx" } @@ -67,3 +66,5 @@ byte-unit.workspace = true itertools.workspace = true tempfile.workspace = true test-log.workspace = true +wasmer-compiler = { workspace = true } +wasmer-types = { workspace = true } diff --git a/crates/vm/src/host_env.rs b/crates/vm/src/host_env.rs index 2173962414..e928de6afe 100644 --- a/crates/vm/src/host_env.rs +++ b/crates/vm/src/host_env.rs @@ -12,7 +12,6 @@ use namada_core::borsh::{BorshDeserialize, BorshSerializeExt}; use namada_core::hash::Hash; use namada_core::internal::{HostEnvResult, KeyVal}; use namada_core::storage::{BlockHeight, Key, TxIndex, TX_INDEX_LENGTH}; -use namada_core::WasmCacheAccess; use namada_events::{Event, EventTypeBuilder}; use namada_gas::{ self as gas, GasMetering, TxGasMeter, VpGasMeter, @@ -35,9 +34,12 @@ use namada_tx::{BatchedTx, BatchedTxRef, Tx, TxCommitments}; use namada_vp::vp_host_fns; use thiserror::Error; +#[cfg(feature = "wasm-runtime")] use super::wasm::{TxCache, VpCache}; use crate::memory::VmMemory; -use crate::{HostRef, RoAccess, RoHostRef, RwAccess, RwHostRef}; +use crate::{ + HostRef, RoAccess, RoHostRef, RwAccess, RwHostRef, WasmCacheAccess, +}; /// These runtime errors will abort tx WASM execution immediately #[allow(missing_docs)] @@ -131,9 +133,14 @@ where pub yielded_value: HostRef>>, /// VP WASM compilation cache (this is available in tx context, because /// we're pre-compiling VPs from [`tx_init_account`]) + #[cfg(feature = "wasm-runtime")] pub vp_wasm_cache: HostRef>, /// Tx WASM compilation cache + #[cfg(feature = "wasm-runtime")] pub tx_wasm_cache: HostRef>, + /// To avoid unused parameter without "wasm-runtime" feature + #[cfg(not(feature = "wasm-runtime"))] + pub cache_access: std::marker::PhantomData, } impl TxVmEnv @@ -164,8 +171,8 @@ where verifiers: &mut BTreeSet
, result_buffer: &mut Option>, yielded_value: &mut Option>, - vp_wasm_cache: &mut VpCache, - tx_wasm_cache: &mut TxCache, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, + #[cfg(feature = "wasm-runtime")] tx_wasm_cache: &mut TxCache, ) -> Self { let write_log = unsafe { RwHostRef::new(write_log) }; let in_mem = unsafe { RoHostRef::new(in_mem) }; @@ -179,7 +186,9 @@ where let verifiers = unsafe { RwHostRef::new(verifiers) }; let result_buffer = unsafe { RwHostRef::new(result_buffer) }; let yielded_value = unsafe { RwHostRef::new(yielded_value) }; + #[cfg(feature = "wasm-runtime")] let vp_wasm_cache = unsafe { RwHostRef::new(vp_wasm_cache) }; + #[cfg(feature = "wasm-runtime")] let tx_wasm_cache = unsafe { RwHostRef::new(tx_wasm_cache) }; let ctx = TxCtx { write_log, @@ -194,8 +203,12 @@ where verifiers, result_buffer, yielded_value, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache, + #[cfg(feature = "wasm-runtime")] tx_wasm_cache, + #[cfg(not(feature = "wasm-runtime"))] + cache_access: std::marker::PhantomData, }; Self { memory, ctx } @@ -274,8 +287,12 @@ where verifiers: self.verifiers, result_buffer: self.result_buffer, yielded_value: self.yielded_value, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache: self.vp_wasm_cache, + #[cfg(feature = "wasm-runtime")] tx_wasm_cache: self.tx_wasm_cache, + #[cfg(not(feature = "wasm-runtime"))] + cache_access: std::marker::PhantomData, } } } @@ -333,7 +350,11 @@ where /// calls to `eval`. pub verifiers: HostRef>, /// VP WASM compilation cache + #[cfg(feature = "wasm-runtime")] pub vp_wasm_cache: HostRef>, + /// To avoid unused parameter without "wasm-runtime" feature + #[cfg(not(feature = "wasm-runtime"))] + pub cache_access: std::marker::PhantomData, } /// A Validity predicate runner for calls from the [`vp_eval`] function. @@ -391,7 +412,7 @@ where yielded_value: &mut Option>, keys_changed: &BTreeSet, eval_runner: &EVAL, - vp_wasm_cache: &mut VpCache, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, ) -> Self { let ctx = VpCtx::new( address, @@ -408,6 +429,7 @@ where yielded_value, keys_changed, eval_runner, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache, ); @@ -466,7 +488,7 @@ where yielded_value: &mut Option>, keys_changed: &BTreeSet, eval_runner: &EVAL, - vp_wasm_cache: &mut VpCache, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, ) -> Self { let address = unsafe { RoHostRef::new(address) }; let write_log = unsafe { RoHostRef::new(write_log) }; @@ -482,6 +504,7 @@ where let yielded_value = unsafe { RwHostRef::new(yielded_value) }; let keys_changed = unsafe { RoHostRef::new(keys_changed) }; let eval_runner = unsafe { RoHostRef::new(eval_runner) }; + #[cfg(feature = "wasm-runtime")] let vp_wasm_cache = unsafe { RwHostRef::new(vp_wasm_cache) }; Self { address, @@ -498,7 +521,10 @@ where yielded_value, keys_changed, verifiers, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache, + #[cfg(not(feature = "wasm-runtime"))] + cache_access: std::marker::PhantomData, } } @@ -546,7 +572,10 @@ where yielded_value: self.yielded_value, keys_changed: self.keys_changed, verifiers: self.verifiers, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache: self.vp_wasm_cache, + #[cfg(not(feature = "wasm-runtime"))] + cache_access: std::marker::PhantomData, } } } @@ -2346,8 +2375,8 @@ pub mod testing { tx_index: &TxIndex, result_buffer: &mut Option>, yielded_value: &mut Option>, - vp_wasm_cache: &mut VpCache, - tx_wasm_cache: &mut TxCache, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, + #[cfg(feature = "wasm-runtime")] tx_wasm_cache: &mut TxCache, ) -> TxVmEnv::D, ::H, CA> where S: State, @@ -2368,7 +2397,9 @@ pub mod testing { verifiers, result_buffer, yielded_value, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache, + #[cfg(feature = "wasm-runtime")] tx_wasm_cache, ) } @@ -2387,8 +2418,8 @@ pub mod testing { result_buffer: &mut Option>, yielded_value: &mut Option>, store: Rc>, - vp_wasm_cache: &mut VpCache, - tx_wasm_cache: &mut TxCache, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, + #[cfg(feature = "wasm-runtime")] tx_wasm_cache: &mut TxCache, ) -> TxVmEnv::D, ::H, CA> where S: State, @@ -2415,7 +2446,9 @@ pub mod testing { verifiers, result_buffer, yielded_value, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache, + #[cfg(feature = "wasm-runtime")] tx_wasm_cache, ); @@ -2438,7 +2471,7 @@ pub mod testing { yielded_value: &mut Option>, keys_changed: &BTreeSet, eval_runner: &EVAL, - vp_wasm_cache: &mut VpCache, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache: &mut VpCache, ) -> VpVmEnv::D, ::H, EVAL, CA> where S: StateRead, @@ -2461,6 +2494,7 @@ pub mod testing { yielded_value, keys_changed, eval_runner, + #[cfg(feature = "wasm-runtime")] vp_wasm_cache, ) } diff --git a/crates/vm/src/lib.rs b/crates/vm/src/lib.rs index 9489052eaa..d2866119f2 100644 --- a/crates/vm/src/lib.rs +++ b/crates/vm/src/lib.rs @@ -61,6 +61,32 @@ pub enum WasmValidationError { ForbiddenWasmFeatures(wasmparser::BinaryReaderError), } +/// WASM Cache access level, used to limit dry-ran transactions to read-only +/// cache access. +pub trait WasmCacheAccess: Clone + std::fmt::Debug + Default { + /// Is access read/write? + fn is_read_write() -> bool; +} + +/// Regular read/write caches access +#[derive(Debug, Clone, Default)] +pub struct WasmCacheRwAccess; +impl WasmCacheAccess for WasmCacheRwAccess { + fn is_read_write() -> bool { + true + } +} + +/// Restricted read-only access for dry-ran transactions +#[derive(Debug, Clone, Default)] +pub struct WasmCacheRoAccess; + +impl WasmCacheAccess for WasmCacheRoAccess { + fn is_read_write() -> bool { + false + } +} + /// Read-only access to host data. #[derive(Debug)] pub enum RoAccess {} diff --git a/crates/vm/src/wasm/compilation_cache/common.rs b/crates/vm/src/wasm/compilation_cache/common.rs index aae2814b44..75708a1a2f 100644 --- a/crates/vm/src/wasm/compilation_cache/common.rs +++ b/crates/vm/src/wasm/compilation_cache/common.rs @@ -14,14 +14,14 @@ use std::time::Duration; use clru::{CLruCache, CLruCacheConfig, WeightScale}; use namada_core::collections::HashMap; +use namada_core::control_flow::time::{ExponentialBackoff, SleepStrategy}; use namada_core::hash::Hash; -use namada_core::{WasmCacheAccess, WasmCacheRoAccess}; -use namada_sdk::control_flow::time::{ExponentialBackoff, SleepStrategy}; use wasmer::{Module, Store}; use wasmer_cache::{FileSystemCache, Hash as CacheHash}; use crate::wasm::run::untrusted_wasm_store; use crate::wasm::{self, memory}; +use crate::{WasmCacheAccess, WasmCacheRoAccess}; /// Cache handle. Thread-safe. #[derive(Debug, Clone)] @@ -601,11 +601,11 @@ mod universal { /// Testing helpers #[cfg(any(test, feature = "testing"))] pub mod testing { - use namada_core::WasmCacheRwAccess; use tempfile::{tempdir, TempDir}; use super::*; use crate::wasm::{TxCache, VpCache}; + use crate::WasmCacheRwAccess; /// Instantiate the default wasmer store. pub fn store() -> Store { @@ -640,12 +640,12 @@ mod test { use assert_matches::assert_matches; use byte_unit::Byte; - use namada_core::WasmCacheRwAccess; use namada_test_utils::TestWasms; use tempfile::{tempdir, TempDir}; use test_log::test; use super::*; + use crate::WasmCacheRwAccess; #[test] fn test_fetch_or_compile_valid_wasm() { diff --git a/crates/vm/src/wasm/host_env.rs b/crates/vm/src/wasm/host_env.rs index cd4aaa799c..b2d62ef103 100644 --- a/crates/vm/src/wasm/host_env.rs +++ b/crates/vm/src/wasm/host_env.rs @@ -3,13 +3,12 @@ //! Here, we expose the host functions into wasm's //! imports, so they can be called from inside the wasm. -use namada_core::WasmCacheAccess; use namada_state::{DBIter, StorageHasher, DB}; use wasmer::{Function, FunctionEnv, Imports}; -use crate::host_env; use crate::host_env::{TxVmEnv, VpEvaluator, VpVmEnv}; use crate::wasm::memory::WasmMemory; +use crate::{host_env, WasmCacheAccess}; /// Prepare imports (memory and host functions) exposed to the vm guest running /// transaction code @@ -114,12 +113,12 @@ mod wrap_tx { #![allow(missing_docs)] - use namada_core::WasmCacheAccess; use namada_state::{DBIter, StorageHasher, DB}; use wasmer::FunctionEnvMut; use crate::host_env::TxVmEnv; use crate::wasm::memory::WasmMemory; + use crate::WasmCacheAccess; pub(super) fn _0( f: F, @@ -295,12 +294,12 @@ mod wrap_vp { #![allow(missing_docs)] - use namada_core::WasmCacheAccess; use namada_state::{DBIter, StorageHasher, DB}; use wasmer::FunctionEnvMut; use crate::host_env::{VpEvaluator, VpVmEnv}; use crate::wasm::memory::WasmMemory; + use crate::WasmCacheAccess; pub(super) fn _0( f: F, diff --git a/crates/vm/src/wasm/memory.rs b/crates/vm/src/wasm/memory.rs index 373925a93d..3b20b7b532 100644 --- a/crates/vm/src/wasm/memory.rs +++ b/crates/vm/src/wasm/memory.rs @@ -6,9 +6,9 @@ use std::ptr::NonNull; use std::rc::{self, Rc}; use std::str::Utf8Error; +use namada_core::arith::{self, checked}; use namada_core::borsh::BorshSerializeExt; use namada_gas::MEMORY_ACCESS_GAS_PER_BYTE; -use namada_sdk::arith::{self, checked}; use namada_tx::BatchedTxRef; use thiserror::Error; use wasmer::sys::BaseTunables; diff --git a/crates/vm/src/wasm/run.rs b/crates/vm/src/wasm/run.rs index 385467bb4b..dd58a51548 100644 --- a/crates/vm/src/wasm/run.rs +++ b/crates/vm/src/wasm/run.rs @@ -14,7 +14,6 @@ use namada_core::hash::{Error as TxHashError, Hash}; use namada_core::internal::HostEnvResult; use namada_core::storage::{Key, TxIndex}; use namada_core::validity_predicate::VpError; -use namada_core::WasmCacheAccess; use namada_gas::{GasMetering, TxGasMeter, VpGasMeter, WASM_MEMORY_PAGE_GAS}; use namada_state::prefix_iter::PrefixIterators; use namada_state::{DBIter, State, StateRead, StorageHasher, StorageRead, DB}; @@ -33,7 +32,10 @@ use crate::host_env::{TxVmEnv, VpCtx, VpEvaluator, VpVmEnv}; use crate::types::VpInput; use crate::wasm::host_env::{tx_imports, vp_imports}; use crate::wasm::{memory, Cache, CacheName, VpCache}; -use crate::{validate_untrusted_wasm, HostRef, RwAccess, WasmValidationError}; +use crate::{ + validate_untrusted_wasm, HostRef, RwAccess, WasmCacheAccess, + WasmValidationError, +}; const TX_ENTRYPOINT: &str = "_apply_tx"; const VP_ENTRYPOINT: &str = "_validate_tx"; @@ -533,13 +535,8 @@ where vp_code_hash: Hash, input_data: BatchedTxRef<'_>, ) -> namada_state::StorageResult<()> { - use std::marker::PhantomData; - use namada_state::ResultExt; - use crate::host_env::VpCtx; - use crate::wasm::run::VpEvalWasm; - let eval_runner = VpEvalWasm::<::D, ::H, CA> { db: PhantomData, @@ -572,10 +569,7 @@ where eval_runner .eval_native_result(ctx, vp_code_hash, input_data) .inspect_err(|err| { - tracing::warn!( - "VP eval from a native VP failed with: - {err}", - ); + tracing::warn!("VP eval from a native VP failed with: {err}"); }) .into_storage_result() } @@ -1003,8 +997,8 @@ mod tests { use assert_matches::assert_matches; use itertools::Either; + use namada_core::arith::checked; use namada_core::borsh::BorshSerializeExt; - use namada_sdk::arith::checked; use namada_state::testing::TestState; use namada_state::StorageWrite; use namada_test_utils::TestWasms; diff --git a/crates/vp/src/native_vp.rs b/crates/vp/src/native_vp.rs index dd6dccb1f3..dde8eafb58 100644 --- a/crates/vp/src/native_vp.rs +++ b/crates/vp/src/native_vp.rs @@ -47,13 +47,13 @@ pub trait NativeVp<'a> { /// A validity predicate's host context. /// -/// This is similar to [`crate::vm::host_env::VpCtx`], but without the VM +/// This is similar to `namada_vm::host_env::VpCtx`, but without the VM /// wrapper types and `eval_runner` field. The references must not be changed /// when [`Ctx`] is mutable. #[derive(Debug)] pub struct Ctx<'a, S, CA, EVAL> where - S: 'static + StateRead, + S: StateRead, EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// The address of the account that owns the VP @@ -82,10 +82,10 @@ where pub eval: PhantomData, } -/// A Validity predicate runner for calls from the [`vp_eval`] function. +/// A Validity predicate runner for calls from the host env `vp_eval` function. pub trait VpEvaluator<'a, S, CA, EVAL> where - S: 'static + StateRead, + S: 'a + StateRead, EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// Evaluate a given validity predicate code with the given input data. @@ -105,7 +105,7 @@ where #[derive(Debug)] pub struct CtxPreStorageRead<'view, 'a, S, CA, EVAL> where - S: 'static + StateRead, + S: StateRead, EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// The inner context @@ -117,7 +117,7 @@ where #[derive(Debug)] pub struct CtxPostStorageRead<'view, 'a, S, CA, EVAL> where - S: 'static + StateRead, + S: StateRead, EVAL: VpEvaluator<'a, S, CA, EVAL>, { /// The inner context @@ -126,7 +126,7 @@ where impl<'a, S, CA, EVAL> Ctx<'a, S, CA, EVAL> where - S: 'static + StateRead, + S: StateRead, EVAL: VpEvaluator<'a, S, CA, EVAL>, CA: 'static + Clone, { @@ -178,7 +178,7 @@ where impl<'view, 'a: 'view, S, CA, EVAL> StorageRead for CtxPreStorageRead<'view, 'a, S, CA, EVAL> where - S: 'static + StateRead, + S: StateRead, EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, CA: 'static + Clone, { @@ -256,7 +256,7 @@ where impl<'view, 'a: 'view, S, CA, EVAL> StorageRead for CtxPostStorageRead<'view, 'a, S, CA, EVAL> where - S: 'static + StateRead, + S: StateRead, EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, CA: 'static + Clone, { @@ -333,7 +333,7 @@ where impl<'view, 'a: 'view, S, CA, EVAL> VpEnv<'view> for Ctx<'a, S, CA, EVAL> where - S: 'static + StateRead, + S: 'a + StateRead, EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, CA: 'static + Clone, { @@ -490,7 +490,7 @@ where impl<'a, S, CA, EVAL> namada_tx::action::Read for Ctx<'a, S, CA, EVAL> where - S: 'static + StateRead, + S: StateRead, EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, CA: 'static + Clone, { @@ -553,7 +553,7 @@ pub trait StorageReader { impl<'a, S, CA, EVAL> StorageReader for &Ctx<'a, S, CA, EVAL> where - S: 'static + StateRead, + S: StateRead, EVAL: 'static + VpEvaluator<'a, S, CA, EVAL>, CA: 'static + Clone, { diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 1720015a94..9f9c30f10b 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -35,7 +35,7 @@ namada_macros = {path = "../crates/macros"} namada_migrations = {path = "../crates/migrations"} namada_parameters = { path = "../crates/parameters"} namada_trans_token = {path = "../crates/trans_token", features = ["migrations"]} -namada_sdk = { path = "../crates/sdk", default-features = false, features = ["namada-sdk", "std", "testing", "migrations"] } +namada_sdk = { path = "../crates/sdk", default-features = false, features = ["std", "testing", "migrations"] } namada_shielded_token = { path = "../crates/shielded_token" } borsh.workspace = true diff --git a/genesis/starter/parameters.toml b/genesis/starter/parameters.toml index 4942cf5ee5..2b4f5ea052 100644 --- a/genesis/starter/parameters.toml +++ b/genesis/starter/parameters.toml @@ -18,8 +18,6 @@ implicit_vp = "vp_implicit" epochs_per_year = 31_536_000 # The multiplier for masp epochs masp_epoch_multiplier = 2 -# Maximum number of signature per transaction -max_signatures_per_transaction = 15 # Max gas for block max_block_gas = 20000000 # Masp fee payment gas limit diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index c49bee42bf..b67ad5e32f 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -3273,26 +3273,6 @@ dependencies = [ "redox_syscall", ] -[[package]] -name = "linkme" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2cfee0de9bd869589fb9a015e155946d1be5ff415cb844c2caccc6cc4b5db9" -dependencies = [ - "linkme-impl", -] - -[[package]] -name = "linkme-impl" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adf157a4dc5a29b7b464aa8fe7edeff30076e07e13646a1c3874f58477dc99f8" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "linux-raw-sys" version = "0.4.12" @@ -3507,72 +3487,13 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" -[[package]] -name = "namada" -version = "0.41.0" -dependencies = [ - "async-trait", - "borsh", - "borsh-ext", - "clru", - "either", - "ethers", - "eyre", - "itertools 0.12.1", - "linkme", - "masp_primitives", - "masp_proofs", - "namada_account", - "namada_core", - "namada_ethereum_bridge", - "namada_events", - "namada_gas", - "namada_governance", - "namada_ibc", - "namada_migrations", - "namada_parameters", - "namada_proof_of_stake", - "namada_replay_protection", - "namada_sdk", - "namada_state", - "namada_token", - "namada_tx", - "namada_tx_env", - "namada_vote_ext", - "namada_vp_env", - "parity-wasm", - "proptest", - "prost", - "rand 0.8.5", - "rayon", - "ripemd", - "serde_json", - "sha2 0.9.9", - "smooth-operator", - "tempfile", - "thiserror", - "tiny-bip39", - "tokio", - "tracing", - "uint", - "wasm-instrument", - "wasmer", - "wasmer-cache", - "wasmer-compiler-singlepass", - "wasmer-vm", - "wasmparser 0.107.0", - "wasmtimer", -] - [[package]] name = "namada_account" version = "0.41.0" dependencies = [ "borsh", - "linkme", "namada_core", "namada_macros", - "namada_migrations", "namada_storage", "proptest", "serde", @@ -3600,16 +3521,15 @@ dependencies = [ "ethabi", "ethbridge-structs", "eyre", + "futures", "ibc", "ics23", "impl-num-traits", "index-set", "indexmap 2.2.4", "k256", - "linkme", "masp_primitives", "namada_macros", - "namada_migrations", "num-integer", "num-rational", "num-traits 0.2.17", @@ -3630,8 +3550,10 @@ dependencies = [ "tendermint-proto 0.37.0", "thiserror", "tiny-keccak", + "tokio", "tracing", "uint", + "wasmtimer", "zeroize", ] @@ -3644,12 +3566,10 @@ dependencies = [ "eyre", "itertools 0.12.1", "konst", - "linkme", "namada_account", "namada_core", "namada_events", "namada_macros", - "namada_migrations", "namada_parameters", "namada_proof_of_stake", "namada_state", @@ -3669,10 +3589,8 @@ name = "namada_events" version = "0.41.0" dependencies = [ "borsh", - "linkme", "namada_core", "namada_macros", - "namada_migrations", "serde", "serde_json", "thiserror", @@ -3684,11 +3602,9 @@ name = "namada_gas" version = "0.41.0" dependencies = [ "borsh", - "linkme", "namada_core", "namada_events", "namada_macros", - "namada_migrations", "serde", "thiserror", ] @@ -3700,12 +3616,10 @@ dependencies = [ "borsh", "itertools 0.12.1", "konst", - "linkme", "namada_account", "namada_core", "namada_events", "namada_macros", - "namada_migrations", "namada_parameters", "namada_state", "namada_trans_token", @@ -3778,22 +3692,16 @@ dependencies = [ "thiserror", ] -[[package]] -name = "namada_migrations" -version = "0.41.0" -dependencies = [ - "lazy_static", - "linkme", - "namada_macros", -] - [[package]] name = "namada_parameters" version = "0.41.0" dependencies = [ "namada_core", "namada_macros", + "namada_state", "namada_storage", + "namada_tx", + "namada_vp", "smooth-operator", "thiserror", ] @@ -3803,18 +3711,20 @@ name = "namada_proof_of_stake" version = "0.41.0" dependencies = [ "borsh", + "itertools 0.12.1", "konst", - "linkme", "namada_account", "namada_controller", "namada_core", "namada_events", "namada_governance", "namada_macros", - "namada_migrations", "namada_parameters", + "namada_state", "namada_storage", "namada_trans_token", + "namada_tx", + "namada_vp", "once_cell", "proptest", "serde", @@ -3853,7 +3763,6 @@ dependencies = [ "itertools 0.12.1", "jubjub", "lazy_static", - "linkme", "masp_primitives", "masp_proofs", "namada_account", @@ -3864,14 +3773,15 @@ dependencies = [ "namada_governance", "namada_ibc", "namada_macros", - "namada_migrations", "namada_parameters", "namada_proof_of_stake", "namada_state", "namada_storage", "namada_token", "namada_tx", + "namada_vm", "namada_vote_ext", + "namada_vp", "num-traits 0.2.17", "num256", "orion", @@ -3896,7 +3806,6 @@ dependencies = [ "tokio", "toml 0.5.11", "tracing", - "wasmtimer", "zeroize", ] @@ -3908,16 +3817,23 @@ dependencies = [ "lazy_static", "masp_primitives", "masp_proofs", + "namada_account", "namada_controller", "namada_core", "namada_gas", "namada_parameters", + "namada_state", "namada_storage", "namada_trans_token", + "namada_tx", + "namada_vp", "rand_core 0.6.4", "rayon", + "ripemd", "serde", + "sha2 0.9.9", "smooth-operator", + "thiserror", "tracing", ] @@ -3928,13 +3844,11 @@ dependencies = [ "borsh", "clru", "itertools 0.12.1", - "linkme", "namada_core", "namada_events", "namada_gas", "namada_macros", "namada_merkle_tree", - "namada_migrations", "namada_replay_protection", "namada_storage", "namada_tx", @@ -3951,11 +3865,9 @@ version = "0.41.0" dependencies = [ "borsh", "itertools 0.12.1", - "linkme", "namada_core", "namada_macros", "namada_merkle_tree", - "namada_migrations", "namada_replay_protection", "regex", "serde", @@ -3983,11 +3895,13 @@ dependencies = [ "ibc-testkit", "ics23", "itertools 0.12.1", - "namada", "namada_core", "namada_sdk", "namada_test_utils", + "namada_tx_env", "namada_tx_prelude", + "namada_vm", + "namada_vp", "namada_vp_prelude", "proptest", "prost", @@ -4023,7 +3937,12 @@ dependencies = [ "konst", "namada_core", "namada_events", + "namada_state", "namada_storage", + "namada_tx", + "namada_vp", + "thiserror", + "tracing", ] [[package]] @@ -4036,13 +3955,11 @@ dependencies = [ "data-encoding", "either", "konst", - "linkme", "masp_primitives", "namada_core", "namada_events", "namada_gas", "namada_macros", - "namada_migrations", "num-derive 0.4.2", "num-traits 0.2.17", "proptest", @@ -4074,6 +3991,7 @@ dependencies = [ "namada_account", "namada_core", "namada_events", + "namada_gas", "namada_governance", "namada_ibc", "namada_macros", @@ -4086,6 +4004,34 @@ dependencies = [ "namada_vm_env", ] +[[package]] +name = "namada_vm" +version = "0.41.0" +dependencies = [ + "borsh", + "clru", + "namada_core", + "namada_events", + "namada_gas", + "namada_parameters", + "namada_state", + "namada_token", + "namada_tx", + "namada_vp", + "parity-wasm", + "rayon", + "smooth-operator", + "tempfile", + "thiserror", + "tracing", + "wasm-instrument", + "wasmer", + "wasmer-cache", + "wasmer-compiler-singlepass", + "wasmer-vm", + "wasmparser 0.107.0", +] + [[package]] name = "namada_vm_env" version = "0.41.0" @@ -4100,17 +4046,15 @@ name = "namada_vote_ext" version = "0.41.0" dependencies = [ "borsh", - "linkme", "namada_core", "namada_macros", - "namada_migrations", "namada_tx", "serde", ] [[package]] name = "namada_vp" -version = "0.39.0" +version = "0.41.0" dependencies = [ "namada_core", "namada_events", @@ -6722,7 +6666,6 @@ name = "tx_bond" version = "0.41.0" dependencies = [ "getrandom 0.2.15", - "namada", "namada_test_utils", "namada_tests", "namada_tx_prelude", @@ -6757,7 +6700,6 @@ name = "tx_change_validator_commission" version = "0.41.0" dependencies = [ "getrandom 0.2.15", - "namada", "namada_test_utils", "namada_tests", "namada_tx_prelude", @@ -6837,7 +6779,6 @@ name = "tx_redelegate" version = "0.41.0" dependencies = [ "getrandom 0.2.15", - "namada", "namada_test_utils", "namada_tests", "namada_tx_prelude", @@ -6881,7 +6822,6 @@ name = "tx_unbond" version = "0.41.0" dependencies = [ "getrandom 0.2.15", - "namada", "namada_test_utils", "namada_tests", "namada_tx_prelude", @@ -6934,7 +6874,6 @@ name = "tx_withdraw" version = "0.41.0" dependencies = [ "getrandom 0.2.15", - "namada", "namada_test_utils", "namada_tests", "namada_tx_prelude", @@ -7117,7 +7056,6 @@ name = "vp_implicit" version = "0.41.0" dependencies = [ "getrandom 0.2.15", - "namada", "namada_test_utils", "namada_tests", "namada_tx_prelude", @@ -7134,7 +7072,6 @@ name = "vp_user" version = "0.41.0" dependencies = [ "getrandom 0.2.15", - "namada", "namada_test_utils", "namada_tests", "namada_tx_prelude", diff --git a/wasm/tx_bond/src/lib.rs b/wasm/tx_bond/src/lib.rs index f09e127b50..b8a1f60536 100644 --- a/wasm/tx_bond/src/lib.rs +++ b/wasm/tx_bond/src/lib.rs @@ -16,26 +16,29 @@ mod tests { use std::cell::RefCell; use std::collections::BTreeSet; - use namada::core::dec::Dec; - use namada::ledger::gas::VpGasMeter; - use namada::ledger::pos::{OwnedPosParams, PosVP}; - use namada::proof_of_stake::storage::{ - bond_handle, read_consensus_validator_set_addresses_with_stake, - read_total_stake, read_validator_stake, - }; - use namada::proof_of_stake::types::{GenesisValidator, WeightedValidator}; use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; use namada_tests::native_vp::TestNativeVpEnv; use namada_tests::tx::*; + use namada_tests::validation::PosVp; use namada_tx_prelude::address::testing::{ arb_established_address, arb_non_internal_address, }; use namada_tx_prelude::address::InternalAddress; use namada_tx_prelude::chain::ChainId; + use namada_tx_prelude::dec::Dec; + use namada_tx_prelude::gas::VpGasMeter; use namada_tx_prelude::key::testing::arb_common_keypair; use namada_tx_prelude::key::RefTo; use namada_tx_prelude::proof_of_stake::parameters::testing::arb_pos_params; + use namada_tx_prelude::proof_of_stake::parameters::OwnedPosParams; + use namada_tx_prelude::proof_of_stake::storage::{ + bond_handle, read_consensus_validator_set_addresses_with_stake, + read_total_stake, read_validator_stake, + }; + use namada_tx_prelude::proof_of_stake::types::{ + GenesisValidator, WeightedValidator, + }; use proptest::prelude::*; use super::*; @@ -326,7 +329,8 @@ mod tests { &tx_env.gas_meter.borrow(), )); let vp_env = TestNativeVpEnv::from_tx_env(tx_env, address::POS); - let result = vp_env.validate_tx(&gas_meter, PosVP::new); + let vp = vp_env.init_vp(&gas_meter, PosVp::new); + let result = vp_env.validate_tx(&vp); assert!( result.is_ok(), "PoS Validity predicate must accept this transaction" diff --git a/wasm/tx_change_validator_commission/src/lib.rs b/wasm/tx_change_validator_commission/src/lib.rs index 8b6caa8011..2218f5db61 100644 --- a/wasm/tx_change_validator_commission/src/lib.rs +++ b/wasm/tx_change_validator_commission/src/lib.rs @@ -20,20 +20,21 @@ mod tests { use std::cell::RefCell; use std::cmp; - use namada::core::dec::{Dec, POS_DECIMAL_PRECISION}; - use namada::ledger::gas::VpGasMeter; - use namada::ledger::pos::{OwnedPosParams, PosVP}; - use namada::proof_of_stake::storage::validator_commission_rate_handle; - use namada::proof_of_stake::types::GenesisValidator; use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; use namada_tests::native_vp::TestNativeVpEnv; use namada_tests::tx::*; + use namada_tests::validation::PosVp; use namada_tx_prelude::address::testing::arb_established_address; use namada_tx_prelude::chain::ChainId; + use namada_tx_prelude::dec::{Dec, POS_DECIMAL_PRECISION}; + use namada_tx_prelude::gas::VpGasMeter; use namada_tx_prelude::key::testing::arb_common_keypair; use namada_tx_prelude::key::RefTo; use namada_tx_prelude::proof_of_stake::parameters::testing::arb_pos_params; + use namada_tx_prelude::proof_of_stake::parameters::OwnedPosParams; + use namada_tx_prelude::proof_of_stake::storage::validator_commission_rate_handle; + use namada_tx_prelude::proof_of_stake::types::GenesisValidator; use proptest::prelude::*; use super::*; @@ -153,7 +154,8 @@ mod tests { &tx_env.gas_meter.borrow(), )); let vp_env = TestNativeVpEnv::from_tx_env(tx_env, address::POS); - let result = vp_env.validate_tx(&gas_meter, PosVP::new); + let vp = vp_env.init_vp(&gas_meter, PosVp::new); + let result = vp_env.validate_tx(&vp); assert!( result.is_ok(), "PoS Validity predicate must accept this transaction" diff --git a/wasm/tx_redelegate/src/lib.rs b/wasm/tx_redelegate/src/lib.rs index efc7aab65e..3ccbd7520e 100644 --- a/wasm/tx_redelegate/src/lib.rs +++ b/wasm/tx_redelegate/src/lib.rs @@ -22,23 +22,26 @@ mod tests { use std::cell::RefCell; use std::collections::BTreeSet; - use namada::core::dec::Dec; - use namada::ledger::gas::VpGasMeter; - use namada::ledger::pos::{OwnedPosParams, PosVP}; - use namada::proof_of_stake::storage::{ - bond_handle, read_consensus_validator_set_addresses_with_stake, - read_total_stake, read_validator_stake, unbond_handle, - }; - use namada::proof_of_stake::types::{GenesisValidator, WeightedValidator}; use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; use namada_tests::native_vp::TestNativeVpEnv; use namada_tests::tx::*; + use namada_tests::validation::PosVp; use namada_tx_prelude::address::InternalAddress; use namada_tx_prelude::chain::ChainId; + use namada_tx_prelude::dec::Dec; + use namada_tx_prelude::gas::VpGasMeter; use namada_tx_prelude::key::testing::arb_common_keypair; use namada_tx_prelude::key::RefTo; use namada_tx_prelude::proof_of_stake::parameters::testing::arb_pos_params; + use namada_tx_prelude::proof_of_stake::parameters::OwnedPosParams; + use namada_tx_prelude::proof_of_stake::storage::{ + bond_handle, read_consensus_validator_set_addresses_with_stake, + read_total_stake, read_validator_stake, unbond_handle, + }; + use namada_tx_prelude::proof_of_stake::types::{ + GenesisValidator, WeightedValidator, + }; use proptest::prelude::*; use super::*; @@ -362,7 +365,8 @@ mod tests { &tx_env.gas_meter.borrow(), )); let vp_env = TestNativeVpEnv::from_tx_env(tx_env, address::POS); - let result = vp_env.validate_tx(&gas_meter, PosVP::new); + let vp = vp_env.init_vp(&gas_meter, PosVp::new); + let result = vp_env.validate_tx(&vp); assert!( result.is_ok(), "PoS Validity predicate must accept this transaction" diff --git a/wasm/tx_unbond/src/lib.rs b/wasm/tx_unbond/src/lib.rs index e22a3ed7a8..3188c16673 100644 --- a/wasm/tx_unbond/src/lib.rs +++ b/wasm/tx_unbond/src/lib.rs @@ -22,23 +22,26 @@ mod tests { use std::cell::RefCell; use std::collections::BTreeSet; - use namada::core::dec::Dec; - use namada::ledger::gas::VpGasMeter; - use namada::ledger::pos::{OwnedPosParams, PosVP}; - use namada::proof_of_stake::storage::{ - bond_handle, read_consensus_validator_set_addresses_with_stake, - read_total_stake, read_validator_stake, unbond_handle, - }; - use namada::proof_of_stake::types::{GenesisValidator, WeightedValidator}; use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; use namada_tests::native_vp::TestNativeVpEnv; use namada_tests::tx::*; + use namada_tests::validation::PosVp; use namada_tx_prelude::address::InternalAddress; use namada_tx_prelude::chain::ChainId; + use namada_tx_prelude::dec::Dec; + use namada_tx_prelude::gas::VpGasMeter; use namada_tx_prelude::key::testing::arb_common_keypair; use namada_tx_prelude::key::RefTo; use namada_tx_prelude::proof_of_stake::parameters::testing::arb_pos_params; + use namada_tx_prelude::proof_of_stake::parameters::OwnedPosParams; + use namada_tx_prelude::proof_of_stake::storage::{ + bond_handle, read_consensus_validator_set_addresses_with_stake, + read_total_stake, read_validator_stake, unbond_handle, + }; + use namada_tx_prelude::proof_of_stake::types::{ + GenesisValidator, WeightedValidator, + }; use proptest::prelude::*; use super::*; @@ -338,7 +341,8 @@ mod tests { &tx_env.gas_meter.borrow(), )); let vp_env = TestNativeVpEnv::from_tx_env(tx_env, address::POS); - let result = vp_env.validate_tx(&gas_meter, PosVP::new); + let vp = vp_env.init_vp(&gas_meter, PosVp::new); + let result = vp_env.validate_tx(&vp); assert!( result.is_ok(), "PoS Validity predicate must accept this transaction" diff --git a/wasm/tx_withdraw/src/lib.rs b/wasm/tx_withdraw/src/lib.rs index a11cc0a63a..7a0dc5591d 100644 --- a/wasm/tx_withdraw/src/lib.rs +++ b/wasm/tx_withdraw/src/lib.rs @@ -22,23 +22,24 @@ fn apply_tx(ctx: &mut Ctx, tx_data: BatchedTx) -> TxResult { mod tests { use std::cell::RefCell; - use namada::core::dec::Dec; - use namada::ledger::gas::VpGasMeter; - use namada::ledger::pos::{OwnedPosParams, PosVP}; - use namada::proof_of_stake::storage::unbond_handle; - use namada::proof_of_stake::types::GenesisValidator; use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; use namada_tests::native_vp::TestNativeVpEnv; use namada_tests::tx::*; + use namada_tests::validation::PosVp; use namada_tx_prelude::address::testing::{ arb_established_address, arb_non_internal_address, }; use namada_tx_prelude::address::InternalAddress; use namada_tx_prelude::chain::ChainId; + use namada_tx_prelude::dec::Dec; + use namada_tx_prelude::gas::VpGasMeter; use namada_tx_prelude::key::testing::arb_common_keypair; use namada_tx_prelude::key::RefTo; use namada_tx_prelude::proof_of_stake::parameters::testing::arb_pos_params; + use namada_tx_prelude::proof_of_stake::parameters::OwnedPosParams; + use namada_tx_prelude::proof_of_stake::storage::unbond_handle; + use namada_tx_prelude::proof_of_stake::types::GenesisValidator; use proptest::prelude::*; use super::*; @@ -226,7 +227,8 @@ mod tests { &tx_env.gas_meter.borrow(), )); let vp_env = TestNativeVpEnv::from_tx_env(tx_env, address::POS); - let result = vp_env.validate_tx(&gas_meter, PosVP::new); + let vp = vp_env.init_vp(&gas_meter, PosVp::new); + let result = vp_env.validate_tx(&vp); assert!( result.is_ok(), "PoS Validity predicate must accept this transaction" diff --git a/wasm/vp_implicit/src/lib.rs b/wasm/vp_implicit/src/lib.rs index 884c03a228..de6a44b5f0 100644 --- a/wasm/vp_implicit/src/lib.rs +++ b/wasm/vp_implicit/src/lib.rs @@ -248,18 +248,20 @@ impl<'a> From<&'a storage::Key> for KeyType<'a> { mod tests { use std::panic; - // Use this as `#[test]` annotation to enable logging - use namada::core::dec::Dec; - use namada::core::storage::Epoch; - use namada::ledger::pos::{GenesisValidator, PosParams}; - use namada::tx::data::TxType; - use namada::tx::{Authorization, Code, Data}; use namada_test_utils::TestWasms; use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; - use namada_tests::tx::{self, tx_host_env, TestTxEnv}; + use namada_tests::tx::data::TxType; + use namada_tests::tx::{ + self, tx_host_env, Authorization, Code, Data, TestTxEnv, + }; use namada_tests::vp::vp_host_env::storage::Key; use namada_tests::vp::*; + // Use this as `#[test]` annotation to enable logging + use namada_tx_prelude::dec::Dec; + use namada_tx_prelude::proof_of_stake::parameters::PosParams; + use namada_tx_prelude::proof_of_stake::types::GenesisValidator; + use namada_tx_prelude::storage::Epoch; use namada_tx_prelude::{StorageWrite, TxEnv}; use namada_vp_prelude::account::AccountPublicKeysMap; use namada_vp_prelude::key::RefTo; diff --git a/wasm/vp_user/src/lib.rs b/wasm/vp_user/src/lib.rs index a046c19a95..395e213cdf 100644 --- a/wasm/vp_user/src/lib.rs +++ b/wasm/vp_user/src/lib.rs @@ -217,18 +217,20 @@ mod tests { use std::panic; use address::testing::arb_non_internal_address; - use namada::core::dec::Dec; - use namada::core::storage::Epoch; - use namada::ledger::pos::{GenesisValidator, PosParams}; - use namada::tx::data::{self, TxType}; - use namada::tx::{Authorization, Code, Data}; use namada_test_utils::TestWasms; // Use this as `#[test]` annotation to enable logging use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; - use namada_tests::tx::{self, tx_host_env, TestTxEnv}; + use namada_tests::tx::data::{self, TxType}; + use namada_tests::tx::{ + self, tx_host_env, Authorization, Code, Data, TestTxEnv, + }; use namada_tests::vp::vp_host_env::storage::Key; use namada_tests::vp::*; + use namada_tx_prelude::dec::Dec; + use namada_tx_prelude::proof_of_stake::parameters::PosParams; + use namada_tx_prelude::proof_of_stake::types::GenesisValidator; + use namada_tx_prelude::storage::Epoch; use namada_tx_prelude::{StorageWrite, TxEnv}; use namada_vp_prelude::account::AccountPublicKeysMap; use namada_vp_prelude::key::RefTo; diff --git a/wasm_for_tests/Cargo.lock b/wasm_for_tests/Cargo.lock index ab7a4b7839..4b03d66267 100644 --- a/wasm_for_tests/Cargo.lock +++ b/wasm_for_tests/Cargo.lock @@ -3487,61 +3487,6 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" -[[package]] -name = "namada" -version = "0.41.0" -dependencies = [ - "async-trait", - "borsh", - "borsh-ext", - "clru", - "either", - "ethers", - "eyre", - "itertools 0.12.1", - "masp_primitives", - "masp_proofs", - "namada_account", - "namada_core", - "namada_ethereum_bridge", - "namada_events", - "namada_gas", - "namada_governance", - "namada_ibc", - "namada_parameters", - "namada_proof_of_stake", - "namada_replay_protection", - "namada_sdk", - "namada_state", - "namada_token", - "namada_tx", - "namada_tx_env", - "namada_vote_ext", - "namada_vp_env", - "parity-wasm", - "proptest", - "prost", - "rand 0.8.5", - "rayon", - "ripemd", - "serde_json", - "sha2 0.9.9", - "smooth-operator", - "tempfile", - "thiserror", - "tiny-bip39", - "tokio", - "tracing", - "uint", - "wasm-instrument", - "wasmer", - "wasmer-cache", - "wasmer-compiler-singlepass", - "wasmer-vm", - "wasmparser 0.107.0", - "wasmtimer", -] - [[package]] name = "namada_account" version = "0.41.0" @@ -3576,6 +3521,7 @@ dependencies = [ "ethabi", "ethbridge-structs", "eyre", + "futures", "ibc", "ics23", "impl-num-traits", @@ -3604,8 +3550,10 @@ dependencies = [ "tendermint-proto 0.37.0", "thiserror", "tiny-keccak", + "tokio", "tracing", "uint", + "wasmtimer", "zeroize", ] @@ -3629,7 +3577,9 @@ dependencies = [ "namada_trans_token", "namada_tx", "namada_vote_ext", + "namada_vp", "serde", + "smooth-operator", "thiserror", "tracing", ] @@ -3666,12 +3616,15 @@ dependencies = [ "borsh", "itertools 0.12.1", "konst", + "namada_account", "namada_core", "namada_events", "namada_macros", "namada_parameters", - "namada_storage", + "namada_state", "namada_trans_token", + "namada_tx", + "namada_vp", "proptest", "serde", "serde_json", @@ -3694,12 +3647,15 @@ dependencies = [ "masp_primitives", "namada_core", "namada_events", + "namada_gas", "namada_governance", "namada_macros", "namada_parameters", "namada_state", "namada_storage", "namada_token", + "namada_tx", + "namada_vp", "primitive-types", "proptest", "prost", @@ -3742,7 +3698,10 @@ version = "0.41.0" dependencies = [ "namada_core", "namada_macros", + "namada_state", "namada_storage", + "namada_tx", + "namada_vp", "smooth-operator", "thiserror", ] @@ -3752,6 +3711,7 @@ name = "namada_proof_of_stake" version = "0.41.0" dependencies = [ "borsh", + "itertools 0.12.1", "konst", "namada_account", "namada_controller", @@ -3760,8 +3720,11 @@ dependencies = [ "namada_governance", "namada_macros", "namada_parameters", + "namada_state", "namada_storage", "namada_trans_token", + "namada_tx", + "namada_vp", "once_cell", "proptest", "serde", @@ -3816,7 +3779,9 @@ dependencies = [ "namada_storage", "namada_token", "namada_tx", + "namada_vm", "namada_vote_ext", + "namada_vp", "num-traits 0.2.17", "num256", "orion", @@ -3841,7 +3806,6 @@ dependencies = [ "tokio", "toml 0.5.11", "tracing", - "wasmtimer", "zeroize", ] @@ -3853,16 +3817,23 @@ dependencies = [ "lazy_static", "masp_primitives", "masp_proofs", + "namada_account", "namada_controller", "namada_core", "namada_gas", "namada_parameters", + "namada_state", "namada_storage", "namada_trans_token", + "namada_tx", + "namada_vp", "rand_core 0.6.4", "rayon", + "ripemd", "serde", + "sha2 0.9.9", "smooth-operator", + "thiserror", "tracing", ] @@ -3878,7 +3849,6 @@ dependencies = [ "namada_gas", "namada_macros", "namada_merkle_tree", - "namada_parameters", "namada_replay_protection", "namada_storage", "namada_tx", @@ -3925,11 +3895,13 @@ dependencies = [ "ibc-testkit", "ics23", "itertools 0.12.1", - "namada", "namada_core", "namada_sdk", "namada_test_utils", + "namada_tx_env", "namada_tx_prelude", + "namada_vm", + "namada_vp", "namada_vp_prelude", "proptest", "prost", @@ -3965,7 +3937,12 @@ dependencies = [ "konst", "namada_core", "namada_events", + "namada_state", "namada_storage", + "namada_tx", + "namada_vp", + "thiserror", + "tracing", ] [[package]] @@ -4014,6 +3991,7 @@ dependencies = [ "namada_account", "namada_core", "namada_events", + "namada_gas", "namada_governance", "namada_ibc", "namada_macros", @@ -4026,6 +4004,34 @@ dependencies = [ "namada_vm_env", ] +[[package]] +name = "namada_vm" +version = "0.41.0" +dependencies = [ + "borsh", + "clru", + "namada_core", + "namada_events", + "namada_gas", + "namada_parameters", + "namada_state", + "namada_token", + "namada_tx", + "namada_vp", + "parity-wasm", + "rayon", + "smooth-operator", + "tempfile", + "thiserror", + "tracing", + "wasm-instrument", + "wasmer", + "wasmer-cache", + "wasmer-compiler-singlepass", + "wasmer-vm", + "wasmparser 0.107.0", +] + [[package]] name = "namada_vm_env" version = "0.41.0" @@ -4046,6 +4052,21 @@ dependencies = [ "serde", ] +[[package]] +name = "namada_vp" +version = "0.41.0" +dependencies = [ + "namada_core", + "namada_events", + "namada_gas", + "namada_state", + "namada_tx", + "namada_vp_env", + "smooth-operator", + "thiserror", + "tracing", +] + [[package]] name = "namada_vp_env" version = "0.41.0" @@ -4054,7 +4075,6 @@ dependencies = [ "masp_primitives", "namada_core", "namada_events", - "namada_ibc", "namada_storage", "namada_tx", "smooth-operator", diff --git a/wasm_for_tests/tx_fail/Cargo.toml b/wasm_for_tests/tx_fail/Cargo.toml index 7ad61b1390..474241508d 100644 --- a/wasm_for_tests/tx_fail/Cargo.toml +++ b/wasm_for_tests/tx_fail/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_infinite_guest_gas/Cargo.toml b/wasm_for_tests/tx_infinite_guest_gas/Cargo.toml index 4ae786610a..bb96412cd1 100644 --- a/wasm_for_tests/tx_infinite_guest_gas/Cargo.toml +++ b/wasm_for_tests/tx_infinite_guest_gas/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_infinite_host_gas/Cargo.toml b/wasm_for_tests/tx_infinite_host_gas/Cargo.toml index f6173d018d..7839da4731 100644 --- a/wasm_for_tests/tx_infinite_host_gas/Cargo.toml +++ b/wasm_for_tests/tx_infinite_host_gas/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_invalid_data/Cargo.toml b/wasm_for_tests/tx_invalid_data/Cargo.toml index 08ede9f085..dd0d5d75b5 100644 --- a/wasm_for_tests/tx_invalid_data/Cargo.toml +++ b/wasm_for_tests/tx_invalid_data/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_memory_limit/Cargo.toml b/wasm_for_tests/tx_memory_limit/Cargo.toml index c7d9599462..838d9784de 100644 --- a/wasm_for_tests/tx_memory_limit/Cargo.toml +++ b/wasm_for_tests/tx_memory_limit/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_no_op/Cargo.toml b/wasm_for_tests/tx_no_op/Cargo.toml index 6a7d841196..13c1d1585e 100644 --- a/wasm_for_tests/tx_no_op/Cargo.toml +++ b/wasm_for_tests/tx_no_op/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_proposal_code/Cargo.toml b/wasm_for_tests/tx_proposal_code/Cargo.toml index 871734a09d..0fbf2f4715 100644 --- a/wasm_for_tests/tx_proposal_code/Cargo.toml +++ b/wasm_for_tests/tx_proposal_code/Cargo.toml @@ -15,9 +15,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_proposal_ibc_token_inflation/Cargo.toml b/wasm_for_tests/tx_proposal_ibc_token_inflation/Cargo.toml index e5caefcfb0..4dc32d08cf 100644 --- a/wasm_for_tests/tx_proposal_ibc_token_inflation/Cargo.toml +++ b/wasm_for_tests/tx_proposal_ibc_token_inflation/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_proposal_masp_reward/Cargo.toml b/wasm_for_tests/tx_proposal_masp_reward/Cargo.toml index 361d8eaeef..cbe83d4cbe 100644 --- a/wasm_for_tests/tx_proposal_masp_reward/Cargo.toml +++ b/wasm_for_tests/tx_proposal_masp_reward/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_read_storage_key/Cargo.toml b/wasm_for_tests/tx_read_storage_key/Cargo.toml index 4a4f1aef2a..2e2ca9b334 100644 --- a/wasm_for_tests/tx_read_storage_key/Cargo.toml +++ b/wasm_for_tests/tx_read_storage_key/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/tx_write/Cargo.toml b/wasm_for_tests/tx_write/Cargo.toml index 52ef2c7b91..80f03b180c 100644 --- a/wasm_for_tests/tx_write/Cargo.toml +++ b/wasm_for_tests/tx_write/Cargo.toml @@ -15,9 +15,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/vp_always_false/Cargo.toml b/wasm_for_tests/vp_always_false/Cargo.toml index dfb265329d..4483918916 100644 --- a/wasm_for_tests/vp_always_false/Cargo.toml +++ b/wasm_for_tests/vp_always_false/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/vp_always_true/Cargo.toml b/wasm_for_tests/vp_always_true/Cargo.toml index 9ad6a8621c..3bc58827e9 100644 --- a/wasm_for_tests/vp_always_true/Cargo.toml +++ b/wasm_for_tests/vp_always_true/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/vp_eval/Cargo.toml b/wasm_for_tests/vp_eval/Cargo.toml index d6c7d13215..c8477dd155 100644 --- a/wasm_for_tests/vp_eval/Cargo.toml +++ b/wasm_for_tests/vp_eval/Cargo.toml @@ -15,9 +15,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/vp_infinite_guest_gas/Cargo.toml b/wasm_for_tests/vp_infinite_guest_gas/Cargo.toml index 3243cae45a..50ac66ce80 100644 --- a/wasm_for_tests/vp_infinite_guest_gas/Cargo.toml +++ b/wasm_for_tests/vp_infinite_guest_gas/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/vp_infinite_host_gas/Cargo.toml b/wasm_for_tests/vp_infinite_host_gas/Cargo.toml index 5f2ee8d60b..d13e70a821 100644 --- a/wasm_for_tests/vp_infinite_host_gas/Cargo.toml +++ b/wasm_for_tests/vp_infinite_host_gas/Cargo.toml @@ -15,9 +15,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/vp_memory_limit/Cargo.toml b/wasm_for_tests/vp_memory_limit/Cargo.toml index 67d8d97e6a..5715cf1d07 100644 --- a/wasm_for_tests/vp_memory_limit/Cargo.toml +++ b/wasm_for_tests/vp_memory_limit/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} diff --git a/wasm_for_tests/vp_read_storage_key/Cargo.toml b/wasm_for_tests/vp_read_storage_key/Cargo.toml index 094c40c7b4..b7bd36790f 100644 --- a/wasm_for_tests/vp_read_storage_key/Cargo.toml +++ b/wasm_for_tests/vp_read_storage_key/Cargo.toml @@ -14,9 +14,7 @@ rlsf.workspace = true getrandom.workspace = true [dev-dependencies] -namada_tests = { path = "../../crates/tests", default-features = false, features = [ - "wasm-runtime", -] } +namada_tests = { path = "../../crates/tests" } proptest = "1.4.0" test-log = {version = "0.2.14", default-features = false, features = ["trace"]} From 28956682998f93a6ea6bef2ada37208938cc99a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 5 Jul 2024 16:50:24 +0100 Subject: [PATCH 38/41] wasm/Make: run clippy with `--all-targets` --- wasm/Makefile | 2 +- wasm_for_tests/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/wasm/Makefile b/wasm/Makefile index 1bc09fcc1c..6c38626701 100644 --- a/wasm/Makefile +++ b/wasm/Makefile @@ -16,7 +16,7 @@ check: $(cargo) +$(nightly) check --workspace --target wasm32-unknown-unknown clippy: - $(cargo) +$(nightly) clippy --workspace -- -D warnings + $(cargo) +$(nightly) clippy --all-targets --workspace -- -D warnings clippy-fix: $(cargo) +$(nightly) clippy --fix -Z unstable-options --workspace --allow-dirty --allow-staged diff --git a/wasm_for_tests/Makefile b/wasm_for_tests/Makefile index fc63e4bfea..d0617bb03b 100644 --- a/wasm_for_tests/Makefile +++ b/wasm_for_tests/Makefile @@ -38,7 +38,7 @@ check: $(cargo) +$(nightly) check --workspace --target wasm32-unknown-unknown clippy: - $(cargo) +$(nightly) clippy --workspace -- -D warnings + $(cargo) +$(nightly) clippy --all-targets --workspace -- -D warnings clippy-fix: $(cargo) +$(nightly) clippy --fix -Z unstable-options --workspace --allow-dirty --allow-staged From 7bdd949b516166b7bf0d282ca134d22248342e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 8 Jul 2024 11:34:04 +0100 Subject: [PATCH 39/41] shielded_token/vp: replace IBC dep with DI --- crates/core/src/ibc.rs | 36 +++ crates/ibc/src/lib.rs | 421 +++++++++++++++++++++++++++- crates/sdk/src/validation.rs | 8 +- crates/shielded_token/src/vp.rs | 468 +++++--------------------------- crates/vp_env/src/lib.rs | 15 +- 5 files changed, 526 insertions(+), 422 deletions(-) diff --git a/crates/core/src/ibc.rs b/crates/core/src/ibc.rs index 4f88c57c89..e6ab24da1d 100644 --- a/crates/core/src/ibc.rs +++ b/crates/core/src/ibc.rs @@ -1,18 +1,54 @@ //! IBC-related data types +use std::collections::{BTreeMap, BTreeSet}; use std::fmt::Display; use std::str::FromStr; use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; use data_encoding::{DecodePartial, HEXLOWER, HEXLOWER_PERMISSIVE}; pub use ibc::*; +use masp_primitives::transaction::components::ValueSum; +use masp_primitives::transaction::TransparentAddress; use namada_macros::BorshDeserializer; #[cfg(feature = "migrations")] use namada_migrations::*; use serde::{Deserialize, Serialize}; use super::address::HASH_LEN; +use crate::address::Address; use crate::hash::Hash; +use crate::masp::TAddrData; +use crate::{storage, token}; + +/// Abstract IBC storage read interface +pub trait Read { + /// Storage error + type Err; + + /// Extract MASP transaction from IBC envelope + fn try_extract_masp_tx_from_envelope( + tx_data: &[u8], + ) -> Result, Self::Err>; + + /// Apply relevant IBC packets to the changed balances structure + fn apply_ibc_packet( + storage: &S, + tx_data: &[u8], + acc: ChangedBalances, + keys_changed: &BTreeSet, + ) -> Result; +} + +/// Balances changed by a transaction +#[derive(Default, Debug, Clone)] +pub struct ChangedBalances { + /// Map between MASP transparent address and namada types + pub decoder: BTreeMap, + /// Balances before the tx + pub pre: BTreeMap>, + /// Balances after the tx + pub post: BTreeMap>, +} /// IBC token hash derived from a denomination. #[derive( diff --git a/crates/ibc/src/lib.rs b/crates/ibc/src/lib.rs index f915d436cd..242a5d1c2d 100644 --- a/crates/ibc/src/lib.rs +++ b/crates/ibc/src/lib.rs @@ -30,9 +30,12 @@ pub mod vp; use std::cell::RefCell; use std::collections::BTreeSet; use std::fmt::Debug; +use std::marker::PhantomData; use std::rc::Rc; pub use actions::transfer_over_ibc; +use apps::transfer::types::packet::PacketData; +use apps::transfer::types::PORT_ID_STR; use borsh::BorshDeserialize; pub use context::common::IbcCommonContext; pub use context::nft_transfer::NftTransferContext; @@ -65,6 +68,7 @@ use ibc::core::channel::types::commitment::compute_ack_commitment; use ibc::core::channel::types::msgs::{ MsgRecvPacket as IbcMsgRecvPacket, PacketMsg, }; +use ibc::core::channel::types::timeout::TimeoutHeight; use ibc::core::entrypoint::{execute, validate}; use ibc::core::handler::types::error::ContextError; use ibc::core::handler::types::events::Error as RawIbcEventError; @@ -77,17 +81,26 @@ pub use ibc::*; use masp_primitives::transaction::Transaction as MaspTransaction; pub use msg::*; use namada_core::address::{self, Address}; -use namada_core::arith::checked; +use namada_core::arith::{checked, CheckedAdd, CheckedSub}; +use namada_core::ibc::apps::nft_transfer::types::packet::PacketData as NftPacketData; +use namada_core::ibc::core::channel::types::commitment::{ + compute_packet_commitment, AcknowledgementCommitment, PacketCommitment, +}; +pub use namada_core::ibc::*; +use namada_core::masp::{addr_taddr, ibc_taddr, TAddrData}; use namada_core::token::Amount; use namada_events::EmitEvents; use namada_state::{ - DBIter, Key, State, StorageError, StorageHasher, StorageRead, StorageWrite, - WlState, DB, + DBIter, Key, ResultExt, State, StorageError, StorageHasher, StorageRead, + StorageWrite, WlState, DB, }; +use namada_token::transaction::components::ValueSum; use namada_token::Transfer; pub use nft::*; +use primitives::Timestamp; use prost::Message; use thiserror::Error; +use trace::{convert_to_address, ibc_trace_for_nft, is_sender_chain_source}; use crate::storage::{ channel_counter_key, client_counter_key, connection_counter_key, @@ -124,6 +137,408 @@ pub enum Error { Verifier(namada_storage::Error), } +struct IbcTransferInfo { + src_port_id: PortId, + src_channel_id: ChannelId, + timeout_height: TimeoutHeight, + timeout_timestamp: Timestamp, + packet_data: Vec, + ibc_traces: Vec, + amount: Amount, + receiver: String, +} + +impl TryFrom for IbcTransferInfo { + type Error = namada_storage::Error; + + fn try_from( + message: IbcMsgTransfer, + ) -> std::result::Result { + let packet_data = serde_json::to_vec(&message.packet_data) + .map_err(namada_storage::Error::new)?; + let ibc_traces = vec![message.packet_data.token.denom.to_string()]; + let amount = message + .packet_data + .token + .amount + .try_into() + .into_storage_result()?; + let receiver = message.packet_data.receiver.to_string(); + Ok(Self { + src_port_id: message.port_id_on_a, + src_channel_id: message.chan_id_on_a, + timeout_height: message.timeout_height_on_b, + timeout_timestamp: message.timeout_timestamp_on_b, + packet_data, + ibc_traces, + amount, + receiver, + }) + } +} + +impl TryFrom for IbcTransferInfo { + type Error = namada_storage::Error; + + fn try_from( + message: IbcMsgNftTransfer, + ) -> std::result::Result { + let packet_data = serde_json::to_vec(&message.packet_data) + .map_err(namada_storage::Error::new)?; + let ibc_traces = message + .packet_data + .token_ids + .0 + .iter() + .map(|token_id| { + ibc_trace_for_nft(&message.packet_data.class_id, token_id) + }) + .collect(); + let receiver = message.packet_data.receiver.to_string(); + Ok(Self { + src_port_id: message.port_id_on_a, + src_channel_id: message.chan_id_on_a, + timeout_height: message.timeout_height_on_b, + timeout_timestamp: message.timeout_timestamp_on_b, + packet_data, + ibc_traces, + amount: Amount::from_u64(1), + receiver, + }) + } +} + +/// IBC storage `Keys/Read/Write` implementation +#[derive(Debug)] +pub struct Store(PhantomData); + +impl namada_core::ibc::Read for Store +where + S: StorageRead, +{ + type Err = namada_storage::Error; + + fn try_extract_masp_tx_from_envelope( + tx_data: &[u8], + ) -> Result, Self::Err> + { + let msg = decode_message(tx_data).into_storage_result().ok(); + let tx = if let Some(IbcMessage::Envelope(ref envelope)) = msg { + Some(extract_masp_tx_from_envelope(envelope).ok_or_else(|| { + namada_storage::Error::new_const( + "Missing MASP transaction in IBC message", + ) + })?) + } else { + None + }; + Ok(tx) + } + + fn apply_ibc_packet( + storage: &S, + tx_data: &[u8], + mut accum: ChangedBalances, + keys_changed: &BTreeSet, + ) -> Result { + let msg = decode_message(tx_data).into_storage_result().ok(); + match msg { + None => {} + // This event is emitted on the sender + Some(IbcMessage::Transfer(msg)) => { + // Get the packet commitment from post-storage that corresponds + // to this event + let ibc_transfer = IbcTransferInfo::try_from(msg.message)?; + let receiver = ibc_transfer.receiver.clone(); + let addr = TAddrData::Ibc(receiver.clone()); + accum.decoder.insert(ibc_taddr(receiver), addr); + accum = apply_transfer_msg( + storage, + accum, + &ibc_transfer, + keys_changed, + )?; + } + Some(IbcMessage::NftTransfer(msg)) => { + let ibc_transfer = IbcTransferInfo::try_from(msg.message)?; + let receiver = ibc_transfer.receiver.clone(); + let addr = TAddrData::Ibc(receiver.clone()); + accum.decoder.insert(ibc_taddr(receiver), addr); + accum = apply_transfer_msg( + storage, + accum, + &ibc_transfer, + keys_changed, + )?; + } + // This event is emitted on the receiver + Some(IbcMessage::Envelope(envelope)) => { + if let MsgEnvelope::Packet(PacketMsg::Recv(msg)) = *envelope { + if msg.packet.port_id_on_b.as_str() == PORT_ID_STR { + let packet_data = serde_json::from_slice::( + &msg.packet.data, + ) + .map_err(namada_storage::Error::new)?; + let receiver = packet_data.receiver.to_string(); + let addr = TAddrData::Ibc(receiver.clone()); + accum.decoder.insert(ibc_taddr(receiver), addr); + let ibc_denom = packet_data.token.denom.to_string(); + let amount = packet_data + .token + .amount + .try_into() + .into_storage_result()?; + accum = apply_recv_msg( + storage, + accum, + &msg, + vec![ibc_denom], + amount, + keys_changed, + )?; + } else { + let packet_data = + serde_json::from_slice::( + &msg.packet.data, + ) + .map_err(namada_storage::Error::new)?; + let receiver = packet_data.receiver.to_string(); + let addr = TAddrData::Ibc(receiver.clone()); + accum.decoder.insert(ibc_taddr(receiver), addr); + let ibc_traces = packet_data + .token_ids + .0 + .iter() + .map(|token_id| { + ibc_trace_for_nft( + &packet_data.class_id, + token_id, + ) + }) + .collect(); + accum = apply_recv_msg( + storage, + accum, + &msg, + ibc_traces, + Amount::from_u64(1), + keys_changed, + )?; + } + } + } + } + Ok(accum) + } +} + +fn check_ibc_transfer( + storage: &S, + ibc_transfer: &IbcTransferInfo, + keys_changed: &BTreeSet, +) -> namada_storage::Result<()> +where + S: StorageRead, +{ + let IbcTransferInfo { + src_port_id, + src_channel_id, + timeout_height, + timeout_timestamp, + packet_data, + .. + } = ibc_transfer; + let sequence = + get_last_sequence_send(storage, src_port_id, src_channel_id)?; + let commitment_key = + storage::commitment_key(src_port_id, src_channel_id, sequence); + + if !keys_changed.contains(&commitment_key) { + return Err(namada_storage::Error::new_alloc(format!( + "Expected IBC transfer didn't happen: Port ID {src_port_id}, \ + Channel ID {src_channel_id}, Sequence {sequence}" + ))); + } + + // The commitment is also validated in IBC VP. Make sure that for when + // IBC VP isn't triggered. + let actual: PacketCommitment = storage + .read_bytes(&commitment_key)? + .ok_or(namada_storage::Error::new_alloc(format!( + "Packet commitment doesn't exist: Port ID {src_port_id}, Channel \ + ID {src_channel_id}, Sequence {sequence}" + )))? + .into(); + let expected = compute_packet_commitment( + packet_data, + timeout_height, + timeout_timestamp, + ); + if actual != expected { + return Err(namada_storage::Error::new_alloc(format!( + "Packet commitment mismatched: Port ID {src_port_id}, Channel ID \ + {src_channel_id}, Sequence {sequence}" + ))); + } + + Ok(()) +} + +fn check_packet_receiving( + msg: &IbcMsgRecvPacket, + keys_changed: &BTreeSet, +) -> namada_storage::Result<()> { + let receipt_key = storage::receipt_key( + &msg.packet.port_id_on_b, + &msg.packet.chan_id_on_b, + msg.packet.seq_on_a, + ); + if !keys_changed.contains(&receipt_key) { + return Err(namada_storage::Error::new_alloc(format!( + "The packet has not been received: Port ID {}, Channel ID {}, \ + Sequence {}", + msg.packet.port_id_on_b, + msg.packet.chan_id_on_b, + msg.packet.seq_on_a, + ))); + } + Ok(()) +} + +// Apply the given transfer message to the changed balances structure +fn apply_transfer_msg( + storage: &S, + mut accum: ChangedBalances, + ibc_transfer: &IbcTransferInfo, + keys_changed: &BTreeSet, +) -> namada_storage::Result +where + S: StorageRead, +{ + check_ibc_transfer(storage, ibc_transfer, keys_changed)?; + + let IbcTransferInfo { + ibc_traces, + src_port_id, + src_channel_id, + amount, + receiver, + .. + } = ibc_transfer; + + let receiver = ibc_taddr(receiver.clone()); + for ibc_trace in ibc_traces { + let token = convert_to_address(ibc_trace).into_storage_result()?; + let delta = ValueSum::from_pair(token, *amount); + // If there is a transfer to the IBC account, then deduplicate the + // balance increase since we already accounted for it above + if is_sender_chain_source(ibc_trace, src_port_id, src_channel_id) { + let ibc_taddr = addr_taddr(address::IBC); + let post_entry = accum + .post + .get(&ibc_taddr) + .cloned() + .unwrap_or(ValueSum::zero()); + accum.post.insert( + ibc_taddr, + checked!(post_entry - &delta) + .map_err(namada_storage::Error::new)?, + ); + } + // Record an increase to the balance of a specific IBC receiver + let post_entry = accum + .post + .get(&receiver) + .cloned() + .unwrap_or(ValueSum::zero()); + accum.post.insert( + receiver, + checked!(post_entry + &delta) + .map_err(namada_storage::Error::new)?, + ); + } + + Ok(accum) +} + +// Check if IBC message was received successfully in this state transition +fn is_receiving_success( + storage: &S, + dst_port_id: &PortId, + dst_channel_id: &ChannelId, + sequence: Sequence, +) -> namada_storage::Result +where + S: StorageRead, +{ + // Ensure that the event corresponds to the current changes to storage + let ack_key = storage::ack_key(dst_port_id, dst_channel_id, sequence); // If the receive is a success, then the commitment is unique + let succ_ack_commitment = compute_ack_commitment( + &AcknowledgementStatus::success(ack_success_b64()).into(), + ); + Ok(match storage.read_bytes(&ack_key)? { + // Success happens only if commitment equals the above + Some(value) => { + AcknowledgementCommitment::from(value) == succ_ack_commitment + } + // Acknowledgement key non-existence is failure + None => false, + }) +} + +// Apply the given write acknowledge to the changed balances structure +fn apply_recv_msg( + storage: &S, + mut accum: ChangedBalances, + msg: &IbcMsgRecvPacket, + ibc_traces: Vec, + amount: Amount, + keys_changed: &BTreeSet, +) -> namada_storage::Result +where + S: StorageRead, +{ + check_packet_receiving(msg, keys_changed)?; + + // If the transfer was a failure, then enable funds to + // be withdrawn from the IBC internal address + if is_receiving_success( + storage, + &msg.packet.port_id_on_b, + &msg.packet.chan_id_on_b, + msg.packet.seq_on_a, + )? { + for ibc_trace in ibc_traces { + // Get the received token + let token = received_ibc_token( + ibc_trace, + &msg.packet.port_id_on_a, + &msg.packet.chan_id_on_a, + &msg.packet.port_id_on_b, + &msg.packet.chan_id_on_b, + ) + .into_storage_result()?; + let delta = ValueSum::from_pair(token.clone(), amount); + // Enable funds to be taken from the IBC internal + // address and be deposited elsewhere + // Required for the IBC internal Address to release + // funds + let ibc_taddr = addr_taddr(address::IBC); + let pre_entry = accum + .pre + .get(&ibc_taddr) + .cloned() + .unwrap_or(ValueSum::zero()); + accum.pre.insert( + ibc_taddr, + checked!(pre_entry + &delta) + .map_err(namada_storage::Error::new)?, + ); + } + } + Ok(accum) +} + /// IBC actions to handle IBC operations #[derive(Debug)] pub struct IbcActions<'a, C> diff --git a/crates/sdk/src/validation.rs b/crates/sdk/src/validation.rs index 193cf16a91..e5de512c5c 100644 --- a/crates/sdk/src/validation.rs +++ b/crates/sdk/src/validation.rs @@ -3,7 +3,7 @@ use namada_vm::wasm::run::VpEvalWasm; use namada_vm::wasm::VpCache; -use namada_vp::native_vp::{self, CtxPreStorageRead}; +use namada_vp::native_vp::{self, CtxPostStorageRead, CtxPreStorageRead}; use crate::state::StateRead; use crate::{eth_bridge, governance, ibc, parameters, proof_of_stake, token}; @@ -88,6 +88,8 @@ pub type MaspVp<'a, S, CA> = token::vp::MaspVp< Eval, ParamsPreStore<'a, S, CA>, GovPreStore<'a, S, CA>, + IbcPostStore<'a, S, CA>, + TokenKeys, >; /// Native ETH bridge VP @@ -115,6 +117,10 @@ pub type PosPreStore<'a, S, CA> = proof_of_stake::Store< CtxPreStorageRead<'a, 'a, S, VpCache, Eval>, >; +/// Ibc store implementation over the native posterior context +pub type IbcPostStore<'a, S, CA> = + ibc::Store, Eval>>; + /// Token store impl over IBC pseudo-execution storage pub type TokenStoreForIbcExec<'a, S, CA> = token::Store< ibc::vp::context::PseudoExecutionStorage< diff --git a/crates/shielded_token/src/vp.rs b/crates/shielded_token/src/vp.rs index 5826b64e1e..39827d1289 100644 --- a/crates/shielded_token/src/vp.rs +++ b/crates/shielded_token/src/vp.rs @@ -12,30 +12,25 @@ use masp_primitives::transaction::components::{ I128Sum, TxIn, TxOut, ValueSum, }; use masp_primitives::transaction::{Transaction, TransparentAddress}; -use namada_core::address::Address; +use namada_core::address::{self, Address}; use namada_core::arith::{checked, CheckedAdd, CheckedSub}; use namada_core::booleans::BoolResultUnitExt; -use namada_core::borsh::BorshSerializeExt; use namada_core::collections::HashSet; -use namada_core::ibc::apps::nft_transfer::types::msgs::transfer::MsgTransfer as IbcMsgNftTransfer; -use namada_core::ibc::apps::nft_transfer::types::packet::PacketData as NftPacketData; -use namada_core::ibc::apps::transfer::types::msgs::transfer::MsgTransfer as IbcMsgTransfer; -use namada_core::ibc::apps::transfer::types::packet::PacketData; -use namada_core::masp::{addr_taddr, encode_asset_type, ibc_taddr, MaspEpoch}; +use namada_core::masp::{addr_taddr, encode_asset_type, MaspEpoch, TAddrData}; use namada_core::storage::Key; use namada_core::token::MaspDigitPos; use namada_core::uint::I320; -use namada_core::{governance, parameters, token}; +use namada_core::{governance, ibc, parameters, token}; use namada_state::{ ConversionState, OptionExt, ResultExt, StateRead, StorageError, }; use namada_trans_token::read_denom; use namada_tx::action::Read; use namada_tx::BatchedTxRef; -use namada_vp::native_vp::{Ctx, CtxPreStorageRead, NativeVp, VpEvaluator}; +use namada_vp::native_vp::{ + Ctx, CtxPostStorageRead, CtxPreStorageRead, NativeVp, VpEvaluator, +}; use namada_vp::{native_vp, VpEnv}; -use ripemd::Digest as RipemdDigest; -use sha2::Digest as Sha2Digest; use thiserror::Error; use token::Amount; @@ -57,7 +52,7 @@ pub enum Error { pub type Result = std::result::Result; /// MASP VP -pub struct MaspVp<'ctx, S, CA, EVAL, Params, Gov> +pub struct MaspVp<'ctx, S, CA, EVAL, Params, Gov, Ibc, TransToken> where S: 'static + StateRead, EVAL: VpEvaluator<'ctx, S, CA, EVAL>, @@ -65,7 +60,7 @@ where /// Context to interact with the host structures. pub ctx: Ctx<'ctx, S, CA, EVAL>, /// Generic types for DI - pub _marker: PhantomData<(Params, Gov)>, + pub _marker: PhantomData<(Params, Gov, Ibc, TransToken)>, } // The balances changed by the transaction, split between masp and non-masp @@ -81,79 +76,8 @@ struct ChangedBalances { post: BTreeMap>, } -struct IbcTransferInfo { - src_port_id: PortId, - src_channel_id: ChannelId, - timeout_height: TimeoutHeight, - timeout_timestamp: Timestamp, - packet_data: Vec, - ibc_traces: Vec, - amount: Amount, - receiver: String, -} - -impl TryFrom for IbcTransferInfo { - type Error = Error; - - fn try_from( - message: IbcMsgTransfer, - ) -> std::result::Result { - let packet_data = serde_json::to_vec(&message.packet_data) - .map_err(native_vp::Error::new)?; - let ibc_traces = vec![message.packet_data.token.denom.to_string()]; - let amount = message - .packet_data - .token - .amount - .try_into() - .into_storage_result()?; - let receiver = message.packet_data.receiver.to_string(); - Ok(Self { - src_port_id: message.port_id_on_a, - src_channel_id: message.chan_id_on_a, - timeout_height: message.timeout_height_on_b, - timeout_timestamp: message.timeout_timestamp_on_b, - packet_data, - ibc_traces, - amount, - receiver, - }) - } -} - -impl TryFrom for IbcTransferInfo { - type Error = Error; - - fn try_from( - message: IbcMsgNftTransfer, - ) -> std::result::Result { - let packet_data = serde_json::to_vec(&message.packet_data) - .map_err(native_vp::Error::new)?; - let ibc_traces = message - .packet_data - .token_ids - .0 - .iter() - .map(|token_id| { - ibc_trace_for_nft(&message.packet_data.class_id, token_id) - }) - .collect(); - let receiver = message.packet_data.receiver.to_string(); - Ok(Self { - src_port_id: message.port_id_on_a, - src_channel_id: message.chan_id_on_a, - timeout_height: message.timeout_height_on_b, - timeout_timestamp: message.timeout_timestamp_on_b, - packet_data, - ibc_traces, - amount: Amount::from_u64(1), - receiver, - }) - } -} - -impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov> - MaspVp<'ctx, S, CA, EVAL, Params, Gov> +impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov, Ibc, TransToken> + MaspVp<'ctx, S, CA, EVAL, Params, Gov, Ibc, TransToken> where S: 'static + StateRead, CA: 'static + Clone, @@ -166,6 +90,11 @@ where CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, + Ibc: ibc::Read< + CtxPostStorageRead<'view, 'ctx, S, CA, EVAL>, + Err = StorageError, + >, + TransToken: token::Keys, { /// Instantiate MASP VP pub fn new(ctx: Ctx<'ctx, S, CA, EVAL>) -> Self { @@ -372,297 +301,6 @@ where Ok(()) } - fn check_ibc_transfer( - &self, - ibc_transfer: &IbcTransferInfo, - keys_changed: &BTreeSet, - ) -> Result<()> { - let IbcTransferInfo { - src_port_id, - src_channel_id, - timeout_height, - timeout_timestamp, - packet_data, - .. - } = ibc_transfer; - let sequence = get_last_sequence_send( - &self.ctx.post(), - src_port_id, - src_channel_id, - )?; - let commitment_key = - commitment_key(src_port_id, src_channel_id, sequence); - - if !keys_changed.contains(&commitment_key) { - return Err(Error::NativeVpError(native_vp::Error::AllocMessage( - format!( - "Expected IBC transfer didn't happen: Port ID \ - {src_port_id}, Channel ID {src_channel_id}, Sequence \ - {sequence}" - ), - ))); - } - - // The commitment is also validated in IBC VP. Make sure that for when - // IBC VP isn't triggered. - let actual: PacketCommitment = self - .ctx - .read_bytes_post(&commitment_key)? - .ok_or(Error::NativeVpError(native_vp::Error::AllocMessage( - format!( - "Packet commitment doesn't exist: Port ID {src_port_id}, \ - Channel ID {src_channel_id}, Sequence {sequence}" - ), - )))? - .into(); - let expected = compute_packet_commitment( - packet_data, - timeout_height, - timeout_timestamp, - ); - if actual != expected { - return Err(Error::NativeVpError(native_vp::Error::AllocMessage( - format!( - "Packet commitment mismatched: Port ID {src_port_id}, \ - Channel ID {src_channel_id}, Sequence {sequence}" - ), - ))); - } - - Ok(()) - } - - fn check_packet_receiving( - &self, - msg: &IbcMsgRecvPacket, - keys_changed: &BTreeSet, - ) -> Result<()> { - let receipt_key = receipt_key( - &msg.packet.port_id_on_b, - &msg.packet.chan_id_on_b, - msg.packet.seq_on_a, - ); - if !keys_changed.contains(&receipt_key) { - return Err(Error::NativeVpError(native_vp::Error::AllocMessage( - format!( - "The packet has not been received: Port ID {}, Channel \ - ID {}, Sequence {}", - msg.packet.port_id_on_b, - msg.packet.chan_id_on_b, - msg.packet.seq_on_a, - ), - ))); - } - Ok(()) - } - - // Apply the given transfer message to the changed balances structure - fn apply_transfer_msg( - &self, - mut acc: ChangedBalances, - ibc_transfer: &IbcTransferInfo, - keys_changed: &BTreeSet, - ) -> Result { - self.check_ibc_transfer(ibc_transfer, keys_changed)?; - - let IbcTransferInfo { - ibc_traces, - src_port_id, - src_channel_id, - amount, - receiver, - .. - } = ibc_transfer; - - let receiver = ibc_taddr(receiver.clone()); - for ibc_trace in ibc_traces { - let token = convert_to_address(ibc_trace).into_storage_result()?; - let delta = ValueSum::from_pair(token, *amount); - // If there is a transfer to the IBC account, then deduplicate the - // balance increase since we already accounted for it above - if is_sender_chain_source(ibc_trace, src_port_id, src_channel_id) { - let ibc_taddr = addr_taddr(IBC); - let post_entry = acc - .post - .get(&ibc_taddr) - .cloned() - .unwrap_or(ValueSum::zero()); - acc.post.insert( - ibc_taddr, - checked!(post_entry - &delta) - .map_err(native_vp::Error::new)?, - ); - } - // Record an increase to the balance of a specific IBC receiver - let post_entry = - acc.post.get(&receiver).cloned().unwrap_or(ValueSum::zero()); - acc.post.insert( - receiver, - checked!(post_entry + &delta).map_err(native_vp::Error::new)?, - ); - } - - Ok(acc) - } - - // Check if IBC message was received successfully in this state transition - fn is_receiving_success( - &self, - dst_port_id: &PortId, - dst_channel_id: &ChannelId, - sequence: Sequence, - ) -> Result { - // Ensure that the event corresponds to the current changes to storage - let ack_key = storage::ack_key(dst_port_id, dst_channel_id, sequence); - // If the receive is a success, then the commitment is unique - let succ_ack_commitment = compute_ack_commitment( - &AcknowledgementStatus::success(ack_success_b64()).into(), - ); - Ok(match self.ctx.read_bytes_post(&ack_key)? { - // Success happens only if commitment equals the above - Some(value) => { - AcknowledgementCommitment::from(value) == succ_ack_commitment - } - // Acknowledgement key non-existence is failure - None => false, - }) - } - - // Apply the given write acknowledge to the changed balances structure - fn apply_recv_msg( - &self, - mut acc: ChangedBalances, - msg: &IbcMsgRecvPacket, - ibc_traces: Vec, - amount: Amount, - keys_changed: &BTreeSet, - ) -> Result { - self.check_packet_receiving(msg, keys_changed)?; - - // If the transfer was a failure, then enable funds to - // be withdrawn from the IBC internal address - if self.is_receiving_success( - &msg.packet.port_id_on_b, - &msg.packet.chan_id_on_b, - msg.packet.seq_on_a, - )? { - for ibc_trace in ibc_traces { - // Get the received token - let token = namada_ibc::received_ibc_token( - ibc_trace, - &msg.packet.port_id_on_a, - &msg.packet.chan_id_on_a, - &msg.packet.port_id_on_b, - &msg.packet.chan_id_on_b, - ) - .into_storage_result() - .map_err(Error::NativeVpError)?; - let delta = ValueSum::from_pair(token.clone(), amount); - // Enable funds to be taken from the IBC internal - // address and be deposited elsewhere - // Required for the IBC internal Address to release - // funds - let ibc_taddr = addr_taddr(IBC); - let pre_entry = acc - .pre - .get(&ibc_taddr) - .cloned() - .unwrap_or(ValueSum::zero()); - acc.pre.insert( - ibc_taddr, - checked!(pre_entry + &delta) - .map_err(native_vp::Error::new)?, - ); - } - } - Ok(acc) - } - - // Apply relevant IBC packets to the changed balances structure - fn apply_ibc_packet( - &self, - mut acc: ChangedBalances, - ibc_msg: IbcMessage, - keys_changed: &BTreeSet, - ) -> Result { - match ibc_msg { - // This event is emitted on the sender - IbcMessage::Transfer(msg) => { - // Get the packet commitment from post-storage that corresponds - // to this event - let ibc_transfer = IbcTransferInfo::try_from(msg.message)?; - let receiver = ibc_transfer.receiver.clone(); - let addr = TAddrData::Ibc(receiver.clone()); - acc.decoder.insert(ibc_taddr(receiver), addr); - acc = - self.apply_transfer_msg(acc, &ibc_transfer, keys_changed)?; - } - IbcMessage::NftTransfer(msg) => { - let ibc_transfer = IbcTransferInfo::try_from(msg.message)?; - let receiver = ibc_transfer.receiver.clone(); - let addr = TAddrData::Ibc(receiver.clone()); - acc.decoder.insert(ibc_taddr(receiver), addr); - acc = - self.apply_transfer_msg(acc, &ibc_transfer, keys_changed)?; - } - // This event is emitted on the receiver - IbcMessage::Envelope(envelope) => { - if let MsgEnvelope::Packet(PacketMsg::Recv(msg)) = *envelope { - if msg.packet.port_id_on_b.as_str() == PORT_ID_STR { - let packet_data = serde_json::from_slice::( - &msg.packet.data, - ) - .map_err(native_vp::Error::new)?; - let receiver = packet_data.receiver.to_string(); - let addr = TAddrData::Ibc(receiver.clone()); - acc.decoder.insert(ibc_taddr(receiver), addr); - let ibc_denom = packet_data.token.denom.to_string(); - let amount = packet_data - .token - .amount - .try_into() - .into_storage_result()?; - acc = self.apply_recv_msg( - acc, - &msg, - vec![ibc_denom], - amount, - keys_changed, - )?; - } else { - let packet_data = - serde_json::from_slice::( - &msg.packet.data, - ) - .map_err(native_vp::Error::new)?; - let receiver = packet_data.receiver.to_string(); - let addr = TAddrData::Ibc(receiver.clone()); - acc.decoder.insert(ibc_taddr(receiver), addr); - let ibc_traces = packet_data - .token_ids - .0 - .iter() - .map(|token_id| { - ibc_trace_for_nft( - &packet_data.class_id, - token_id, - ) - }) - .collect(); - acc = self.apply_recv_msg( - acc, - &msg, - ibc_traces, - Amount::from_u64(1), - keys_changed, - )?; - } - } - } - } - Result::<_>::Ok(acc) - } - // Apply the balance change to the changed balances structure fn apply_balance_change( &self, @@ -674,7 +312,8 @@ where )?; // Record the token without an epoch to facilitate later decoding unepoched_tokens(token, denom, &mut result.tokens)?; - let counterpart_balance_key = balance_key(token, counterpart); + let counterpart_balance_key = + TransToken::balance_key(token, counterpart); let pre_balance: Amount = self .ctx .read_pre(&counterpart_balance_key)? @@ -715,13 +354,14 @@ where // Check that transfer is pinned correctly and record the balance changes fn validate_state_and_get_transfer_data( - &self, + &'view self, keys_changed: &BTreeSet, - ibc_msg: Option, + tx_data: &[u8], ) -> Result { // Get the changed balance keys - let mut counterparts_balances = - keys_changed.iter().filter_map(is_any_token_balance_key); + let mut counterparts_balances = keys_changed + .iter() + .filter_map(TransToken::is_any_token_balance_key); // Apply the balance changes to the changed balances structure let mut changed_balances = counterparts_balances @@ -729,23 +369,38 @@ where self.apply_balance_change(acc, account) })?; - let ibc_addr = TAddrData::Addr(IBC); + let ibc_addr = TAddrData::Addr(address::IBC); // Enable decoding the IBC address hash - changed_balances.decoder.insert(addr_taddr(IBC), ibc_addr); + changed_balances + .decoder + .insert(addr_taddr(address::IBC), ibc_addr); // Note the balance changes they imply - match ibc_msg { - Some(ibc_msg) => { - self.apply_ibc_packet(changed_balances, ibc_msg, keys_changed) - } - None => Ok(changed_balances), - } + let ChangedBalances { + tokens, + decoder, + pre, + post, + } = changed_balances; + let ibc::ChangedBalances { decoder, pre, post } = + Ibc::apply_ibc_packet( + &self.ctx.post(), + tx_data, + ibc::ChangedBalances { decoder, pre, post }, + keys_changed, + )?; + Ok(ChangedBalances { + tokens, + decoder, + pre, + post, + }) } // Check that MASP Transaction and state changes are valid fn is_valid_masp_transfer( &'view self, - tx_data: &BatchedTxRef<'_>, + batched_tx: &BatchedTxRef<'_>, keys_changed: &BTreeSet, verifiers: &BTreeSet
, ) -> Result<()> { @@ -759,15 +414,15 @@ where Error::NativeVpError(native_vp::Error::new_const(msg)) })?; let conversion_state = self.ctx.state.in_mem().get_conversion_state(); - let ibc_msg = self.ctx.get_ibc_message(tx_data).ok(); + let tx_data = batched_tx + .tx + .data(batched_tx.cmt) + .ok_or_err_msg("No transaction data")?; let actions = self.ctx.read_actions()?; let shielded_tx = - if let Some(IbcMessage::Envelope(ref envelope)) = ibc_msg { - extract_masp_tx_from_envelope(envelope).ok_or_else(|| { - native_vp::Error::new_const( - "Missing MASP transaction in IBC message", - ) - })? + if let Some(tx) = Ibc::try_extract_masp_tx_from_envelope(&tx_data)? + { + tx } else { // Get the Transaction object from the actions let masp_section_ref = @@ -777,7 +432,7 @@ where "Missing MASP section reference in action", ) })?; - tx_data + batched_tx .tx .get_masp_section(&masp_section_ref) .cloned() @@ -800,11 +455,11 @@ where // Check the validity of the keys and get the transfer data let changed_balances = - self.validate_state_and_get_transfer_data(keys_changed, ibc_msg)?; + self.validate_state_and_get_transfer_data(keys_changed, &tx_data)?; // Some constants that will be used repeatedly let zero = ValueSum::zero(); - let masp_address_hash = addr_taddr(MASP); + let masp_address_hash = addr_taddr(address::MASP); verify_sapling_balancing_value( changed_balances .pre @@ -885,7 +540,7 @@ where .collect(); // Ensure that this transaction is authorized by all involved parties for authorizer in authorizers { - if let Some(TAddrData::Addr(IBC)) = + if let Some(TAddrData::Addr(address::IBC)) = changed_bals_minus_txn.decoder.get(&authorizer) { // If the IBC address is a signatory, then it means that either @@ -1270,8 +925,8 @@ fn verify_sapling_balancing_value( } } -impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov> NativeVp<'view> - for MaspVp<'ctx, S, CA, EVAL, Params, Gov> +impl<'view, 'ctx: 'view, S, CA, EVAL, Params, Gov, Ibc, TransToken> + NativeVp<'view> for MaspVp<'ctx, S, CA, EVAL, Params, Gov, Ibc, TransToken> where S: 'static + StateRead, CA: 'static + Clone, @@ -1284,6 +939,11 @@ where CtxPreStorageRead<'view, 'ctx, S, CA, EVAL>, Err = StorageError, >, + Ibc: ibc::Read< + CtxPostStorageRead<'view, 'ctx, S, CA, EVAL>, + Err = StorageError, + >, + TransToken: token::Keys, { type Error = Error; diff --git a/crates/vp_env/src/lib.rs b/crates/vp_env/src/lib.rs index c65075c8a2..7751632299 100644 --- a/crates/vp_env/src/lib.rs +++ b/crates/vp_env/src/lib.rs @@ -25,8 +25,7 @@ use namada_core::borsh::BorshDeserialize; use namada_core::hash::Hash; use namada_core::storage::{BlockHeight, Epoch, Epochs, Header, Key, TxIndex}; use namada_events::{Event, EventType}; -use namada_ibc::{decode_message, IbcMessage}; -use namada_storage::{OptionExt, ResultExt, StorageRead}; +use namada_storage::StorageRead; use namada_tx::BatchedTxRef; /// Validity predicate's environment is available for native VPs and WASM VPs @@ -120,18 +119,6 @@ where /// Get a tx hash fn get_tx_code_hash(&self) -> Result, namada_storage::Error>; - /// Get the IBC message from the data section - fn get_ibc_message( - &self, - batched_tx: &BatchedTxRef<'_>, - ) -> Result { - let data = batched_tx - .tx - .data(batched_tx.cmt) - .ok_or_err_msg("No transaction data")?; - decode_message(&data).into_storage_result() - } - /// Charge the provided gas for the current vp fn charge_gas(&self, used_gas: u64) -> Result<(), namada_storage::Error>; From 05992dec359ac85dd4ba23bf77e62ad97f3b5eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 26 Jul 2024 20:17:27 +0100 Subject: [PATCH 40/41] make check-crates: replace hard-coded args with the global list --- Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2f8fa64fae..7ba72ab8f1 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,10 @@ crates += namada_vp crates += namada_vp_env crates += namada_vp_prelude +# All crates format as `cargo check --package` arguments +all-crates := $(foreach crate,$(crates), -p $(crate)) + + build: $(cargo) build $(jobs) --workspace --exclude namada_benchmarks @@ -103,7 +107,7 @@ check-mainnet: # Check that every crate can be built with default features and that SDK crate # can be built for wasm and with all features enabled check-crates: - cargo +$(nightly) check -Z unstable-options --tests -p namada -p namada_account -p namada_apps -p namada_apps_lib -p namada_benchmarks -p namada_core -p namada_encoding_spec -p namada_ethereum_bridge -p namada_events -p namada_gas -p namada_governance -p namada_ibc -p namada_light_sdk -p namada_macros -p namada_merkle_tree -p namada_parameters -p namada_proof_of_stake -p namada_replay_protection -p namada_node -p namada_sdk -p namada_shielded_token -p namada_state -p namada_storage -p namada_test_utils -p namada_tests -p namada_token -p namada_trans_token -p namada_tx -p namada_tx_env -p namada_tx_prelude -p namada_vm_env -p namada_vote_ext -p namada_vp_env -p namada_vp_prelude && \ + cargo +$(nightly) check -Z unstable-options --tests $(all-crates) && \ make -C $(wasms) check && \ make -C $(wasms_for_tests) check && \ cargo check --package namada_sdk --target wasm32-unknown-unknown --no-default-features && \ From 11540233019047dbfd0f5c65ecb94e1ef6ba72e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Thu, 27 Jun 2024 16:42:07 +0100 Subject: [PATCH 41/41] changelog: add #3402 --- .../unreleased/improvements/3402-refactor-out-namada-crate.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .changelog/unreleased/improvements/3402-refactor-out-namada-crate.md diff --git a/.changelog/unreleased/improvements/3402-refactor-out-namada-crate.md b/.changelog/unreleased/improvements/3402-refactor-out-namada-crate.md new file mode 100644 index 0000000000..3d76329a2a --- /dev/null +++ b/.changelog/unreleased/improvements/3402-refactor-out-namada-crate.md @@ -0,0 +1,4 @@ +- Added two new crates, namada_vm and namada_vp and removed namada crate that + contained various loosely related code. Moved the native VP implementations + to the relevant crates and replaced their cross-dependencies with dependency- + injection. ([\#3402](https://github.com/anoma/namada/pull/3402)) \ No newline at end of file