diff --git a/Cargo.lock b/Cargo.lock index dd312850b..7d8bb3c0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6680,6 +6680,7 @@ dependencies = [ "serde", "serde_json", "sov-bank", + "sov-chain-state", "sov-data-generators", "sov-modules-api", "sov-modules-macros", diff --git a/module-system/module-implementations/sov-chain-state/Cargo.toml b/module-system/module-implementations/sov-chain-state/Cargo.toml index d61837ed4..60f3aa641 100644 --- a/module-system/module-implementations/sov-chain-state/Cargo.toml +++ b/module-system/module-implementations/sov-chain-state/Cargo.toml @@ -24,11 +24,13 @@ sov-state = { path = "../../sov-state", version = "0.1" } sov-rollup-interface = { path = "../../../rollup-interface", version = "0.1" } [dev-dependencies] +tempfile = { workspace = true } sov-bank = { path = "../sov-bank" } sov-value-setter = { path = "../examples/sov-value-setter" } sov-modules-stf-template = { path = "../../sov-modules-stf-template" } sov-data-generators = { path = "../../utils/sov-data-generators" } -tempfile = { workspace = true } +sov-chain-state = { path = ".", features = ["native"] } + [features] default = [] diff --git a/module-system/module-implementations/sov-chain-state/src/call.rs b/module-system/module-implementations/sov-chain-state/src/call.rs index 733e9f6bd..dd2349be7 100644 --- a/module-system/module-implementations/sov-chain-state/src/call.rs +++ b/module-system/module-implementations/sov-chain-state/src/call.rs @@ -4,13 +4,13 @@ use sov_state::WorkingSet; use crate::{ChainState, StateTransitionId, TransitionHeight}; -impl< - Ctx: sov_modules_api::Context, - Cond: ValidityCondition + BorshSerialize + BorshDeserialize, - > ChainState +impl ChainState +where + C: sov_modules_api::Context, + Cond: ValidityCondition + BorshSerialize + BorshDeserialize, { /// Increment the current slot height - pub fn increment_slot_height(&self, working_set: &mut WorkingSet) { + pub(crate) fn increment_slot_height(&self, working_set: &mut WorkingSet) { let current_height = self .slot_height .get(working_set) @@ -20,11 +20,11 @@ impl< } /// Store the previous state transition - pub fn store_state_transition( + pub(crate) fn store_state_transition( &self, height: TransitionHeight, transition: StateTransitionId, - working_set: &mut WorkingSet, + working_set: &mut WorkingSet, ) { self.historical_transitions .set(&height, &transition, working_set); diff --git a/module-system/module-implementations/sov-chain-state/src/hooks.rs b/module-system/module-implementations/sov-chain-state/src/hooks.rs index 79820f44b..06d915a23 100644 --- a/module-system/module-implementations/sov-chain-state/src/hooks.rs +++ b/module-system/module-implementations/sov-chain-state/src/hooks.rs @@ -7,8 +7,8 @@ use sov_state::{Storage, WorkingSet}; use super::ChainState; use crate::{StateTransitionId, TransitionInProgress}; -impl SlotHooks for ChainState { - type Context = Ctx; +impl SlotHooks for ChainState { + type Context = C; fn begin_slot_hook( &self, diff --git a/module-system/module-implementations/sov-chain-state/src/lib.rs b/module-system/module-implementations/sov-chain-state/src/lib.rs index 008f8f926..332149a2a 100644 --- a/module-system/module-implementations/sov-chain-state/src/lib.rs +++ b/module-system/module-implementations/sov-chain-state/src/lib.rs @@ -10,13 +10,12 @@ pub mod genesis; /// Hook implementation for the module pub mod hooks; -#[cfg(test)] -pub mod tests; - /// The query interface with the module +#[cfg(feature = "native")] pub mod query; - use borsh::{BorshDeserialize, BorshSerialize}; +#[cfg(feature = "native")] +pub use query::{ChainStateRpcImpl, ChainStateRpcServer}; use sov_modules_api::Error; use sov_modules_macros::ModuleInfo; use sov_rollup_interface::zk::{ValidityCondition, ValidityConditionChecker}; @@ -102,10 +101,10 @@ impl TransitionInProgress { /// - Must contain `[address]` field /// - Can contain any number of ` #[state]` or `[module]` fields #[derive(ModuleInfo)] -pub struct ChainState { +pub struct ChainState { /// Address of the module. #[address] - pub address: Ctx::Address, + pub address: C::Address, /// The current block height #[state] @@ -139,10 +138,42 @@ pub struct ChainStateConfig { pub initial_slot_height: TransitionHeight, } -impl sov_modules_api::Module - for ChainState +impl ChainState { + /// Returns transition height in the current slot + pub fn get_slot_height(&self, working_set: &mut WorkingSet) -> TransitionHeight { + self.slot_height + .get(working_set) + .expect("Slot height should be set at initialization") + } + + /// Return the genesis hash of the module. + pub fn get_genesis_hash(&self, working_set: &mut WorkingSet) -> Option<[u8; 32]> { + self.genesis_hash.get(working_set) + } + + /// Returns the transition in progress of the module. + pub fn get_in_progress_transition( + &self, + working_set: &mut WorkingSet, + ) -> Option> { + self.in_progress_transition.get(working_set) + } + + /// Returns the completed transition associated with the provided `transition_num`. + pub fn get_historical_transitions( + &self, + transition_num: TransitionHeight, + working_set: &mut WorkingSet, + ) -> Option> { + self.historical_transitions + .get(&transition_num, working_set) + } +} + +impl sov_modules_api::Module + for ChainState { - type Context = Ctx; + type Context = C; type Config = ChainStateConfig; @@ -151,7 +182,7 @@ impl sov_modules_api::Mo fn genesis( &self, config: &Self::Config, - working_set: &mut WorkingSet, + working_set: &mut WorkingSet, ) -> Result<(), Error> { // The initialization logic Ok(self.init_module(config, working_set)?) diff --git a/module-system/module-implementations/sov-chain-state/src/query.rs b/module-system/module-implementations/sov-chain-state/src/query.rs index 9447b295f..21b37d41f 100644 --- a/module-system/module-implementations/sov-chain-state/src/query.rs +++ b/module-system/module-implementations/sov-chain-state/src/query.rs @@ -1,44 +1,19 @@ +use jsonrpsee::core::RpcResult; +use sov_modules_api::macros::rpc_gen; use sov_rollup_interface::zk::ValidityCondition; use sov_state::WorkingSet; -use super::ChainState; -use crate::{StateTransitionId, TransitionHeight, TransitionInProgress}; - -/// Structure returned by the query methods. -pub struct Response { - /// Value returned by the queries - pub value: u64, -} +use crate::{ChainState, TransitionHeight}; +#[rpc_gen(client, server, namespace = "chainState")] impl ChainState { /// Get the height of the current slot. /// Panics if the slot height is not set - pub fn get_slot_height(&self, working_set: &mut WorkingSet) -> TransitionHeight { - self.slot_height - .get(working_set) - .expect("Slot height should be set at initialization") - } - - /// Return the genesis hash of the module. - pub fn get_genesis_hash(&self, working_set: &mut WorkingSet) -> Option<[u8; 32]> { - self.genesis_hash.get(working_set) - } - - /// Returns the transition in progress of the module. - pub fn get_in_progress_transition( - &self, - working_set: &mut WorkingSet, - ) -> Option> { - self.in_progress_transition.get(working_set) - } - - /// Returns the completed transition associated with the provided `transition_num`. - pub fn get_historical_transitions( + #[rpc_method(name = "getSlotHeight")] + pub fn get_slot_height_rpc( &self, - transition_num: TransitionHeight, working_set: &mut WorkingSet, - ) -> Option> { - self.historical_transitions - .get(&transition_num, working_set) + ) -> RpcResult { + Ok(self.get_slot_height(working_set)) } } diff --git a/module-system/module-implementations/sov-chain-state/src/tests.rs b/module-system/module-implementations/sov-chain-state/tests/all_tests.rs similarity index 95% rename from module-system/module-implementations/sov-chain-state/src/tests.rs rename to module-system/module-implementations/sov-chain-state/tests/all_tests.rs index 620e5c94e..22c86d1ae 100644 --- a/module-system/module-implementations/sov-chain-state/src/tests.rs +++ b/module-system/module-implementations/sov-chain-state/tests/all_tests.rs @@ -1,11 +1,10 @@ +use sov_chain_state::{ChainState, ChainStateConfig, StateTransitionId, TransitionInProgress}; use sov_modules_api::default_context::DefaultContext; use sov_modules_api::hooks::SlotHooks; use sov_modules_api::Genesis; use sov_rollup_interface::mocks::{MockBlock, MockBlockHeader, MockHash, MockValidityCond}; use sov_state::{ProverStorage, Storage, WorkingSet}; -use crate::{ChainState, ChainStateConfig, StateTransitionId, TransitionInProgress}; - /// This simply tests that the chain_state reacts properly with the invocation of the `begin_slot` /// hook. For more complete integration tests, feel free to have a look at the integration tests folder. #[test] @@ -60,7 +59,7 @@ fn test_simple_chain_state() { assert_eq!(stored_root, init_root_hash, "Genesis hashes don't match"); - // Check that the slot height have been updated + // Check that the slot height has been updated let new_height_storage = chain_state.get_slot_height(&mut working_set); assert_eq!( @@ -101,7 +100,7 @@ fn test_simple_chain_state() { chain_state.begin_slot_hook(&new_slot_data, &mut working_set); - // Check that the slot height have been updated correctly + // Check that the slot height has been updated correctly let new_height_storage = chain_state.get_slot_height(&mut working_set); assert_eq!( new_height_storage, diff --git a/module-system/sov-modules-macros/tests/cli_wallet_arg/derive_enum_mixed_fields.rs b/module-system/sov-modules-macros/tests/cli_wallet_arg/derive_enum_mixed_fields.rs index 031ff8dd7..6844cf511 100644 --- a/module-system/sov-modules-macros/tests/cli_wallet_arg/derive_enum_mixed_fields.rs +++ b/module-system/sov-modules-macros/tests/cli_wallet_arg/derive_enum_mixed_fields.rs @@ -15,7 +15,7 @@ fn main() { let actual_foo = ::CliStringRepr::try_parse_from(&[ "myenum", "foo", "1", "hello", ]) - .expect("parsing must succed") + .expect("parsing must succeed") .into(); assert_eq!(expected_foo, actual_foo); @@ -23,7 +23,7 @@ fn main() { let actual_bar = ::CliStringRepr::try_parse_from(&[ "myenum", "bar", "2", ]) - .expect("parsing must succed") + .expect("parsing must succeed") .into(); assert_eq!(expected_bar, actual_bar);