From 093e8e0933270b7c99cd16621d1830ce09fbd93a Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Fri, 3 Nov 2023 01:16:11 +0700 Subject: [PATCH 01/24] feat!: persist execution state in platform state --- .../engine/finalize_block_proposal/v0/mod.rs | 2 +- packages/rs-drive-abci/src/execution/mod.rs | 1 + .../platform_events/block_end/mod.rs | 6 +-- .../block_end/store_ephemeral_state/mod.rs | 53 ------------------- .../block_end/store_ephemeral_state/v0/mod.rs | 51 ------------------ .../mod.rs | 8 +-- .../v0/mod.rs | 26 ++++----- .../storage/fetch_execution_state/mod.rs | 30 +++++++++++ .../storage/fetch_execution_state/v0/mod.rs | 45 ++++++++++++++++ .../src/execution/storage/mod.rs | 10 ++++ .../storage/store_execution_state/mod.rs | 31 +++++++++++ .../storage/store_execution_state/v0/mod.rs | 33 ++++++++++++ .../src/platform_types/platform/mod.rs | 9 +--- .../platform_types/platform_state/v0/mod.rs | 1 + .../platform_types/validator_set/v0/mod.rs | 1 + .../src/version/drive_abci_versions.rs | 10 +++- .../src/version/mocks/v2_test.rs | 4 +- .../src/version/mocks/v3_test.rs | 4 +- .../rs-platform-version/src/version/v1.rs | 4 +- 19 files changed, 188 insertions(+), 141 deletions(-) delete mode 100644 packages/rs-drive-abci/src/execution/platform_events/block_end/store_ephemeral_state/mod.rs delete mode 100644 packages/rs-drive-abci/src/execution/platform_events/block_end/store_ephemeral_state/v0/mod.rs rename packages/rs-drive-abci/src/execution/platform_events/block_end/{update_state_cache => update_execution_state}/mod.rs (85%) rename packages/rs-drive-abci/src/execution/platform_events/block_end/{update_state_cache => update_execution_state}/v0/mod.rs (70%) create mode 100644 packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/storage/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs diff --git a/packages/rs-drive-abci/src/execution/engine/finalize_block_proposal/v0/mod.rs b/packages/rs-drive-abci/src/execution/engine/finalize_block_proposal/v0/mod.rs index 0063cd57363..675c8dbb898 100644 --- a/packages/rs-drive-abci/src/execution/engine/finalize_block_proposal/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/engine/finalize_block_proposal/v0/mod.rs @@ -219,7 +219,7 @@ where } .into(); - self.update_state_cache(extended_block_info, transaction, platform_version)?; + self.update_execution_state(extended_block_info, transaction, platform_version)?; self.update_drive_cache(platform_version)?; diff --git a/packages/rs-drive-abci/src/execution/mod.rs b/packages/rs-drive-abci/src/execution/mod.rs index 4352caa5718..38aba2f3f42 100644 --- a/packages/rs-drive-abci/src/execution/mod.rs +++ b/packages/rs-drive-abci/src/execution/mod.rs @@ -4,6 +4,7 @@ mod check_tx; pub mod engine; /// platform execution events pub(in crate::execution) mod platform_events; +mod storage; /// Types needed in execution pub mod types; /// Validation module diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs index ee3ad883d94..fa102da37cc 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs @@ -1,7 +1,5 @@ -/// Storage of the ephemeral state -pub(in crate::execution) mod store_ephemeral_state; -/// Updating the state cache happens as the final part of block finalization -pub(in crate::execution) mod update_state_cache; +/// Updating the execution state happens as the final part of block finalization +pub(in crate::execution) mod update_execution_state; /// Validator set update pub(in crate::execution) mod validator_set_update; diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/store_ephemeral_state/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/store_ephemeral_state/mod.rs deleted file mode 100644 index bef276301da..00000000000 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/store_ephemeral_state/mod.rs +++ /dev/null @@ -1,53 +0,0 @@ -mod v0; - -use crate::error::execution::ExecutionError; -use crate::error::Error; -use crate::platform_types::platform::Platform; -use crate::platform_types::platform_state::PlatformState; -use crate::rpc::core::CoreRPCLike; - -use dpp::version::PlatformVersion; - -use drive::grovedb::Transaction; - -impl Platform -where - C: CoreRPCLike, -{ - /// Stores ephemeral state data, including the block information and quorum hash in GroveDB. - /// - /// This function is a version handler that directs to specific version implementations - /// of the store_ephemeral_state function. - /// - /// # Arguments - /// - /// * `platform_state` - A `PlatformState` reference. - /// * `transaction` - A `Transaction` reference. - /// * `platform_version` - A `PlatformVersion` reference that dictates which version of - /// the method to call. - /// - /// # Returns - /// - /// * `Result<(), Error>` - Returns an empty `Result` if the data is successfully stored, otherwise returns an `Error`. - /// - pub fn store_ephemeral_state( - &self, - platform_state: &PlatformState, - transaction: &Transaction, - platform_version: &PlatformVersion, - ) -> Result<(), Error> { - match platform_version - .drive_abci - .methods - .block_end - .store_ephemeral_state - { - 0 => self.store_ephemeral_state_v0(platform_state, transaction), - version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { - method: "store_ephemeral_state".to_string(), - known_versions: vec![0], - received: version, - })), - } - } -} diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/store_ephemeral_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/store_ephemeral_state/v0/mod.rs deleted file mode 100644 index 016b67d2072..00000000000 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/store_ephemeral_state/v0/mod.rs +++ /dev/null @@ -1,51 +0,0 @@ -use crate::error::Error; -use crate::platform_types::platform::Platform; -use crate::rpc::core::CoreRPCLike; - -use crate::platform_types::platform_state::PlatformState; -use dpp::serialization::PlatformSerializable; - -use drive::error::Error::GroveDB; -use drive::grovedb::Transaction; - -impl Platform -where - C: CoreRPCLike, -{ - /// Stores ephemeral state data, including the block information and quorum hash in GroveDB. - /// - /// This function should be removed from the current location. - /// - /// # Arguments - /// - /// * `platform_state` - A `PlatformState` reference. - /// * `transaction` - A `Transaction` reference. - /// - /// # Returns - /// - /// * `Result<(), Error>` - Returns an empty `Result` if the data is successfully stored, otherwise returns an `Error`. - /// - pub(super) fn store_ephemeral_state_v0( - &self, - platform_state: &PlatformState, - transaction: &Transaction, - ) -> Result<(), Error> { - // we need to serialize the platform state - let serialized_platform_state = platform_state.serialize_to_bytes()?; - - // next we need to store this data in grovedb - //todo:: maybe this should be in actual state - self.drive - .grove - .put_aux( - b"saved_state", - &serialized_platform_state, - None, - Some(transaction), - ) - .unwrap() - .map_err(|e| Error::Drive(GroveDB(e)))?; - - Ok(()) - } -} diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/mod.rs similarity index 85% rename from packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs rename to packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/mod.rs index a055056edc9..d357b87d1cd 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/mod.rs @@ -14,7 +14,7 @@ impl Platform where C: CoreRPCLike, { - /// Updates the state cache at the end of finalize block. This is done by overriding the current + /// Updates the execution state at the end of finalize block. This is done by overriding the current /// platform state cache with the block execution state cache. /// /// This function is a version handler that directs to specific version implementations @@ -32,7 +32,7 @@ where /// * `Result<(), Error>` - If the state cache and quorums are successfully updated, it returns `Ok(())`. /// If there is a problem with the update, it returns an `Error`. /// - pub fn update_state_cache( + pub fn update_execution_state( &self, extended_block_info: ExtendedBlockInfo, transaction: &Transaction, @@ -42,9 +42,9 @@ where .drive_abci .methods .block_end - .update_state_cache + .update_execution_state { - 0 => self.update_state_cache_v0(extended_block_info, transaction, platform_version), + 0 => self.update_execution_state_v0(extended_block_info, transaction, platform_version), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "update_state_cache".to_string(), known_versions: vec![0], diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/v0/mod.rs similarity index 70% rename from packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs rename to packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/v0/mod.rs index 5803180c654..f18240d17f7 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/v0/mod.rs @@ -12,7 +12,7 @@ impl Platform where C: CoreRPCLike, { - /// Updates the state cache at the end of finalize block. This is done by overriding the current + /// Updates the execution state at the end of finalize block. This is done by overriding the current /// platform state cache with the block execution state cache. /// /// This function takes an `ExtendedBlockInfo` and a `Transaction` as input and updates the @@ -34,7 +34,7 @@ where /// This function may return an `Error` variant if there is a problem with updating the state cache /// and quorums or storing the ephemeral data. /// - pub(super) fn update_state_cache_v0( + pub(super) fn update_execution_state_v0( &self, extended_block_info: ExtendedBlockInfo, transaction: &Transaction, @@ -46,25 +46,25 @@ where ExecutionError::CorruptedCodeExecution("there should be a block execution context"), ))?; - let mut state_cache = self.state.write().unwrap(); + let mut state = self.state.write().unwrap(); - *state_cache = block_execution_context.block_platform_state_owned(); + *state = block_execution_context.block_platform_state_owned(); - if let Some(next_validator_set_quorum_hash) = - state_cache.take_next_validator_set_quorum_hash() - { - state_cache.set_current_validator_set_quorum_hash(next_validator_set_quorum_hash); + if let Some(next_validator_set_quorum_hash) = state.take_next_validator_set_quorum_hash() { + state.set_current_validator_set_quorum_hash(next_validator_set_quorum_hash); } - state_cache.set_last_committed_block_info(Some(extended_block_info)); + state.set_last_committed_block_info(Some(extended_block_info)); - state_cache.set_genesis_block_info(None); + state.set_genesis_block_info(None); //todo: verify this with an update - PlatformVersion::set_current(PlatformVersion::get(platform_version.protocol_version)?); + let version = PlatformVersion::get(platform_version.protocol_version)?; - // Persist ephemeral data - self.store_ephemeral_state(&state_cache, transaction, platform_version)?; + PlatformVersion::set_current(version); + + // Persist execution state + self.store_execution_state(&state, transaction, platform_version)?; Ok(()) } diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs new file mode 100644 index 00000000000..0200fcc80e7 --- /dev/null +++ b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs @@ -0,0 +1,30 @@ +use crate::error::execution::ExecutionError; +use crate::error::Error; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use dpp::version::PlatformVersion; +use drive::query::TransactionArg; + +mod v0; + +impl Platform { + pub fn fetch_execution_state( + &self, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive_abci + .methods + .execution_state_storage + .fetch_execution_state + { + 0 => self.fetch_execution_state_v0(transaction, platform_version), + version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { + method: "fetch_execution_state".to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs new file mode 100644 index 00000000000..5b0b9e4c8f0 --- /dev/null +++ b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs @@ -0,0 +1,45 @@ +use crate::error::execution::ExecutionError; +use crate::error::Error; +use crate::execution::storage::{STORAGE_KEY, STORAGE_PATH}; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use dpp::serialization::PlatformDeserializable; +use dpp::version::PlatformVersion; +use drive::drive::grove_operations::QueryType; +use drive::query::{Element, TransactionArg}; + +impl Platform { + pub(super) fn fetch_execution_state_v0( + &self, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let mut ops = Vec::new(); + + let maybe_element = self + .drive + .grove_get( + STORAGE_PATH.into(), + STORAGE_KEY, + QueryType::StatefulQuery, + transaction, + &mut ops, + &platform_version.drive, + ) + .map_err(|e| Error::Drive(e))?; + + let Some(element) = maybe_element else { + return Ok(None); + }; + + let Element::Item(bytes, _) = element else { + return Err(Error::Execution(ExecutionError::CorruptedCachedState( + "execution state should be stored as an element item", + ))); + }; + + let execution_state = PlatformState::deserialize_from_bytes(&bytes)?; + + Ok(Some(execution_state)) + } +} diff --git a/packages/rs-drive-abci/src/execution/storage/mod.rs b/packages/rs-drive-abci/src/execution/storage/mod.rs new file mode 100644 index 00000000000..704dc1bb537 --- /dev/null +++ b/packages/rs-drive-abci/src/execution/storage/mod.rs @@ -0,0 +1,10 @@ +use drive::drive::RootTree; + +mod fetch_execution_state; +mod store_execution_state; + +pub use fetch_execution_state::*; +pub(in crate::execution) use store_execution_state::*; + +const STORAGE_PATH: [[u8; 1]; 1] = [Into::<[u8; 1]>::into(RootTree::Misc)]; +const STORAGE_KEY: &[u8; 1] = b"E"; diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs new file mode 100644 index 00000000000..f70eee4245b --- /dev/null +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs @@ -0,0 +1,31 @@ +mod v0; + +use crate::error::execution::ExecutionError; +use crate::error::Error; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use dpp::version::PlatformVersion; +use drive::grovedb::Transaction; + +impl Platform { + pub fn store_execution_state( + &self, + state: &PlatformState, + transaction: &Transaction, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + match platform_version + .drive_abci + .methods + .execution_state_storage + .store_execution_state + { + 0 => self.store_execution_state_v0(state, transaction, platform_version), + version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { + method: "fetch_execution_state".to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs new file mode 100644 index 00000000000..f4f1f039dbe --- /dev/null +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs @@ -0,0 +1,33 @@ +use crate::error::Error; +use crate::execution::storage::{STORAGE_KEY, STORAGE_PATH}; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use dpp::serialization::PlatformSerializable; +use dpp::version::PlatformVersion; +use drive::grovedb::Transaction; +use drive::query::Element; + +impl Platform { + pub(super) fn store_execution_state_v0( + &self, + state: &PlatformState, + transaction: &Transaction, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + let mut ops = Vec::new(); + + let element = Element::Item(state.serialize_to_bytes()?, None); + + self.drive + .grove_insert( + STORAGE_PATH.into(), + STORAGE_KEY, + element, + Some(transaction), + None, + &mut ops, + &platform_version.drive, + ) + .map_err(|e| Error::Drive(e)) + } +} diff --git a/packages/rs-drive-abci/src/platform_types/platform/mod.rs b/packages/rs-drive-abci/src/platform_types/platform/mod.rs index 791b65b0c0b..c77dc8714e3 100644 --- a/packages/rs-drive-abci/src/platform_types/platform/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform/mod.rs @@ -146,13 +146,8 @@ impl Platform { } /// Recreate the state from the backing store - pub fn recreate_state(&self, _platform_version: &PlatformVersion) -> Result { - let Some(serialized_platform_state) = self - .drive - .grove - .get_aux(b"saved_state", None) - .unwrap() - .map_err(|e| Error::Drive(GroveDB(e)))? + pub fn recreate_state(&self, platform_version: &PlatformVersion) -> Result { + let Some(serialized_platform_state) = self.fetch_execution_state(None, platform_version) else { return Ok(false); }; diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs index b15dce4743d..dee68e502cf 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs @@ -51,6 +51,7 @@ pub struct PlatformStateV0 { impl Debug for PlatformStateV0 { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("PlatformStateV0") + .field("genesis_block_info", &self.genesis_block_info) .field("last_committed_block_info", &self.last_committed_block_info) .field( "current_protocol_version_in_consensus", diff --git a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs index b8745e96fa9..b6af0b7264b 100644 --- a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs @@ -26,6 +26,7 @@ pub struct ValidatorSetV0 { /// The list of masternodes pub members: BTreeMap, /// The threshold quorum public key + #[serde(serialize_with = "serialize_bls_public_key")] pub threshold_public_key: BlsPublicKey, } diff --git a/packages/rs-platform-version/src/version/drive_abci_versions.rs b/packages/rs-platform-version/src/version/drive_abci_versions.rs index a3e9d9a4420..a9dd0da3022 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions.rs @@ -69,6 +69,7 @@ pub struct DriveAbciMethodVersions { pub epoch: DriveAbciEpochMethodVersions, pub block_start: DriveAbciBlockStartMethodVersions, pub block_end: DriveAbciBlockEndMethodVersions, + pub execution_state_storage: DriveAbciExecutionStateStorageMethodVersions, } #[derive(Clone, Debug, Default)] @@ -77,6 +78,12 @@ pub struct DriveAbciValidationVersions { pub process_state_transition: FeatureVersion, } +#[derive(Clone, Debug, Default)] +struct DriveAbciExecutionStateStorageMethodVersions { + pub fetch_execution_state: FeatureVersion, + pub store_execution_state: FeatureVersion, +} + #[derive(Clone, Debug, Default)] pub struct DriveAbciDocumentsStateTransitionValidationVersions { pub structure: FeatureVersion, @@ -222,8 +229,7 @@ pub struct DriveAbciBlockStartMethodVersions { #[derive(Clone, Debug, Default)] pub struct DriveAbciBlockEndMethodVersions { - pub store_ephemeral_state: FeatureVersion, - pub update_state_cache: FeatureVersion, + pub update_execution_state: FeatureVersion, pub update_drive_cache: FeatureVersion, pub validator_set_update: FeatureVersion, } diff --git a/packages/rs-platform-version/src/version/mocks/v2_test.rs b/packages/rs-platform-version/src/version/mocks/v2_test.rs index 481b2b0a6a3..c6808fb4272 100644 --- a/packages/rs-platform-version/src/version/mocks/v2_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v2_test.rs @@ -531,8 +531,8 @@ pub(crate) const TEST_PLATFORM_V2: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - store_ephemeral_state: 0, - update_state_cache: 0, + store_execution_state: 0, + update_execution_state: 0, update_drive_cache: 0, validator_set_update: 0, }, diff --git a/packages/rs-platform-version/src/version/mocks/v3_test.rs b/packages/rs-platform-version/src/version/mocks/v3_test.rs index 846fbf9b013..58d7bf69d92 100644 --- a/packages/rs-platform-version/src/version/mocks/v3_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v3_test.rs @@ -531,8 +531,8 @@ pub(crate) const TEST_PLATFORM_V3: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - store_ephemeral_state: 0, - update_state_cache: 0, + store_execution_state: 0, + update_execution_state: 0, update_drive_cache: 0, validator_set_update: 0, }, diff --git a/packages/rs-platform-version/src/version/v1.rs b/packages/rs-platform-version/src/version/v1.rs index f53927ca66f..d56f76a78a7 100644 --- a/packages/rs-platform-version/src/version/v1.rs +++ b/packages/rs-platform-version/src/version/v1.rs @@ -528,8 +528,8 @@ pub(super) const PLATFORM_V1: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - store_ephemeral_state: 0, - update_state_cache: 0, + store_execution_state: 0, + update_execution_state: 0, update_drive_cache: 0, validator_set_update: 0, }, From 817e5b109310503b68f43a15f81fb94c51c9e591 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Fri, 3 Nov 2023 01:19:07 +0700 Subject: [PATCH 02/24] chore: verbose debug fro G1Elements --- Cargo.lock | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 030893224e8..0a6185e5be9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -429,7 +429,7 @@ dependencies = [ [[package]] name = "bls-dash-sys" version = "1.2.5" -source = "git+https://github.com/dashpay/bls-signatures?branch=develop#795660db76636c92bea3bfccfee621f1aba371a3" +source = "git+https://github.com/dashpay/bls-signatures?branch=develop#3540b8bbed47e04ed6f02eea2141bd62dae0c411" dependencies = [ "bindgen", "cc", @@ -439,9 +439,10 @@ dependencies = [ [[package]] name = "bls-signatures" version = "1.2.5" -source = "git+https://github.com/dashpay/bls-signatures?branch=develop#795660db76636c92bea3bfccfee621f1aba371a3" +source = "git+https://github.com/dashpay/bls-signatures?branch=develop#3540b8bbed47e04ed6f02eea2141bd62dae0c411" dependencies = [ "bls-dash-sys", + "hex", "rand", "serde", ] From 8810fcac8a15ad6e8d4bbad95c814f05914f22e9 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Fri, 10 Nov 2023 20:45:44 +0700 Subject: [PATCH 03/24] feat: store protocol version separately --- packages/rs-drive-abci/src/execution/mod.rs | 3 +- .../storage/fetch_execution_state/mod.rs | 40 +++++------ .../storage/fetch_execution_state/v0/mod.rs | 62 ++++++++--------- .../src/execution/storage/mod.rs | 8 ++- .../src/execution/storage/protocol_version.rs | 48 +++++++++++++ .../storage/store_execution_state/mod.rs | 1 + .../storage/store_execution_state/v0/mod.rs | 43 ++++++++---- .../src/platform_types/platform/mod.rs | 68 +++++++++---------- .../src/platform_types/platform_state/mod.rs | 47 +++++-------- .../platform_types/platform_state/v0/mod.rs | 17 +++-- .../platform_types/validator_set/v0/mod.rs | 1 - .../tests/strategy_tests/main.rs | 6 +- .../src/version/drive_abci_versions.rs | 2 +- .../src/version/mocks/v2_test.rs | 8 ++- .../src/version/mocks/v3_test.rs | 8 ++- .../rs-platform-version/src/version/v1.rs | 8 ++- 16 files changed, 218 insertions(+), 152 deletions(-) create mode 100644 packages/rs-drive-abci/src/execution/storage/protocol_version.rs diff --git a/packages/rs-drive-abci/src/execution/mod.rs b/packages/rs-drive-abci/src/execution/mod.rs index 38aba2f3f42..0e4b73c3f00 100644 --- a/packages/rs-drive-abci/src/execution/mod.rs +++ b/packages/rs-drive-abci/src/execution/mod.rs @@ -4,7 +4,8 @@ mod check_tx; pub mod engine; /// platform execution events pub(in crate::execution) mod platform_events; -mod storage; +/// Storage implementation for the execution state +pub mod storage; /// Types needed in execution pub mod types; /// Validation module diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs index 0200fcc80e7..ba5fab244ea 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs @@ -1,30 +1,30 @@ use crate::error::execution::ExecutionError; use crate::error::Error; -use crate::platform_types::platform::Platform; +use crate::execution::storage::EXECUTION_STORAGE_PATH; use crate::platform_types::platform_state::PlatformState; use dpp::version::PlatformVersion; +use drive::drive::Drive; use drive::query::TransactionArg; mod v0; -impl Platform { - pub fn fetch_execution_state( - &self, - transaction: TransactionArg, - platform_version: &PlatformVersion, - ) -> Result, Error> { - match platform_version - .drive_abci - .methods - .execution_state_storage - .fetch_execution_state - { - 0 => self.fetch_execution_state_v0(transaction, platform_version), - version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { - method: "fetch_execution_state".to_string(), - known_versions: vec![0], - received: version, - })), - } +/// Fetches execution state from grovedb storage +pub fn fetch_execution_state( + drive: &Drive, + transaction: TransactionArg, + platform_version: &PlatformVersion, +) -> Result, Error> { + match platform_version + .drive_abci + .methods + .execution_state_storage + .fetch_execution_state + { + 0 => v0::fetch_execution_state_v0(drive, transaction, platform_version), + version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { + method: "fetch_execution_state".to_string(), + known_versions: vec![0], + received: version, + })), } } diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs index 5b0b9e4c8f0..4e80920ca9b 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs @@ -1,45 +1,43 @@ use crate::error::execution::ExecutionError; use crate::error::Error; -use crate::execution::storage::{STORAGE_KEY, STORAGE_PATH}; -use crate::platform_types::platform::Platform; +use crate::execution::storage::{EXECUTION_STORAGE_PATH, EXECUTION_STORAGE_STATE_KEY}; use crate::platform_types::platform_state::PlatformState; -use dpp::serialization::PlatformDeserializable; +use dpp::serialization::{PlatformDeserializable, PlatformDeserializableFromVersionedStructure}; use dpp::version::PlatformVersion; use drive::drive::grove_operations::QueryType; +use drive::drive::Drive; +use drive::error::drive::DriveError; use drive::query::{Element, TransactionArg}; -impl Platform { - pub(super) fn fetch_execution_state_v0( - &self, - transaction: TransactionArg, - platform_version: &PlatformVersion, - ) -> Result, Error> { - let mut ops = Vec::new(); +pub(super) fn fetch_execution_state_v0( + drive: &Drive, + transaction: TransactionArg, + platform_version: &PlatformVersion, +) -> Result, Error> { + let mut ops = Vec::new(); - let maybe_element = self - .drive - .grove_get( - STORAGE_PATH.into(), - STORAGE_KEY, - QueryType::StatefulQuery, - transaction, - &mut ops, - &platform_version.drive, - ) - .map_err(|e| Error::Drive(e))?; + let maybe_element = drive + .grove_get( + (&EXECUTION_STORAGE_PATH).into(), + EXECUTION_STORAGE_STATE_KEY, + QueryType::StatefulQuery, + transaction, + &mut ops, + &platform_version.drive, + ) + .map_err(Error::Drive)?; - let Some(element) = maybe_element else { - return Ok(None); - }; + let Some(element) = maybe_element else { + return Ok(None); + }; - let Element::Item(bytes, _) = element else { - return Err(Error::Execution(ExecutionError::CorruptedCachedState( - "execution state should be stored as an element item", - ))); - }; + let Element::Item(bytes, _) = element else { + return Err(Error::Execution(ExecutionError::CorruptedCachedState( + "execution state should be stored as an element item", + ))); + }; - let execution_state = PlatformState::deserialize_from_bytes(&bytes)?; + let execution_state = PlatformState::versioned_deserialize(&bytes, platform_version)?; - Ok(Some(execution_state)) - } + Ok(Some(execution_state)) } diff --git a/packages/rs-drive-abci/src/execution/storage/mod.rs b/packages/rs-drive-abci/src/execution/storage/mod.rs index 704dc1bb537..efc34c11c17 100644 --- a/packages/rs-drive-abci/src/execution/storage/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/mod.rs @@ -1,10 +1,12 @@ use drive::drive::RootTree; mod fetch_execution_state; +mod protocol_version; mod store_execution_state; pub use fetch_execution_state::*; -pub(in crate::execution) use store_execution_state::*; +pub use protocol_version::fetch_current_protocol_version; +pub use store_execution_state::*; -const STORAGE_PATH: [[u8; 1]; 1] = [Into::<[u8; 1]>::into(RootTree::Misc)]; -const STORAGE_KEY: &[u8; 1] = b"E"; +const EXECUTION_STORAGE_PATH: [[u8; 1]; 1] = [[RootTree::Misc as u8]]; +const EXECUTION_STORAGE_STATE_KEY: &[u8; 1] = b"S"; diff --git a/packages/rs-drive-abci/src/execution/storage/protocol_version.rs b/packages/rs-drive-abci/src/execution/storage/protocol_version.rs new file mode 100644 index 00000000000..fdc89554fb5 --- /dev/null +++ b/packages/rs-drive-abci/src/execution/storage/protocol_version.rs @@ -0,0 +1,48 @@ +use crate::error::execution::ExecutionError; +use crate::error::Error; +use crate::execution::storage::EXECUTION_STORAGE_PATH; +use dpp::version::PlatformVersion; +use drive::drive::grove_operations::QueryType; +use drive::drive::Drive; +use drive::error::drive::DriveError; +use drive::grovedb::Element; + +pub(super) const EXECUTION_STORAGE_PLATFORM_VERSION_KEY: &[u8; 1] = b"V"; + +/// Fetches the current execution protocol version from the drive +/// This method can't be versioned since there is no knowledge about versions before it called +/// but fallbacks to support all changes in this method must be implemented +pub fn fetch_current_protocol_version(drive: &Drive) -> Result, Error> { + let mut ops = Vec::new(); + + let platform_version = PlatformVersion::latest(); + + let maybe_element = drive + .grove_get( + (&EXECUTION_STORAGE_PATH).into(), + EXECUTION_STORAGE_PLATFORM_VERSION_KEY, + QueryType::StatefulQuery, + None, + &mut ops, + &platform_version.drive, + ) + .map_err(Error::Drive)?; + + let Some(element) = maybe_element else { + return Ok(None); + }; + + let Element::Item(bytes, _) = element else { + return Err(Error::Execution(ExecutionError::CorruptedCachedState( + "execution state should be stored as an element item", + ))); + }; + + let protocol_version = u32::from_be_bytes(bytes.as_slice().try_into().map_err(|_| { + drive::error::Error::Drive(DriveError::CorruptedSerialization(String::from( + "protocol version length item have an invalid length", + ))) + })?); + + Ok(Some(protocol_version)) +} diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs index f70eee4245b..cd91f0ca637 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs @@ -8,6 +8,7 @@ use dpp::version::PlatformVersion; use drive::grovedb::Transaction; impl Platform { + /// Store the execution state in grovedb storage pub fn store_execution_state( &self, state: &PlatformState, diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs index f4f1f039dbe..e223e87401f 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs @@ -1,9 +1,13 @@ use crate::error::Error; -use crate::execution::storage::{STORAGE_KEY, STORAGE_PATH}; +use crate::execution::storage::protocol_version::EXECUTION_STORAGE_PLATFORM_VERSION_KEY; +use crate::execution::storage::{EXECUTION_STORAGE_PATH, EXECUTION_STORAGE_STATE_KEY}; use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::v0::PlatformStateV0Methods; use crate::platform_types::platform_state::PlatformState; use dpp::serialization::PlatformSerializable; use dpp::version::PlatformVersion; +use drive::drive::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; +use drive::drive::batch::GroveDbOpBatch; use drive::grovedb::Transaction; use drive::query::Element; @@ -14,20 +18,33 @@ impl Platform { transaction: &Transaction, platform_version: &PlatformVersion, ) -> Result<(), Error> { - let mut ops = Vec::new(); + let mut batch = GroveDbOpBatch::new(); - let element = Element::Item(state.serialize_to_bytes()?, None); + let path: Vec> = EXECUTION_STORAGE_PATH + .iter() + .map(|byte_array| byte_array.to_vec()) + .collect(); + + let protocol_version_element = Element::Item( + state + .current_protocol_version_in_consensus() + .to_be_bytes() + .to_vec(), + None, + ); + + batch.add_insert( + path.clone(), + EXECUTION_STORAGE_PLATFORM_VERSION_KEY.to_vec(), + protocol_version_element, + ); + + let state_element = Element::Item(state.serialize_to_bytes()?, None); + + batch.add_insert(path, EXECUTION_STORAGE_STATE_KEY.to_vec(), state_element); self.drive - .grove_insert( - STORAGE_PATH.into(), - STORAGE_KEY, - element, - Some(transaction), - None, - &mut ops, - &platform_version.drive, - ) - .map_err(|e| Error::Drive(e)) + .grove_apply_batch(batch, false, Some(transaction), &platform_version.drive) + .map_err(Error::Drive) } } diff --git a/packages/rs-drive-abci/src/platform_types/platform/mod.rs b/packages/rs-drive-abci/src/platform_types/platform/mod.rs index c77dc8714e3..9f9cd4382d0 100644 --- a/packages/rs-drive-abci/src/platform_types/platform/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform/mod.rs @@ -7,7 +7,6 @@ use std::fmt::{Debug, Formatter}; #[cfg(any(feature = "mocks", test))] use crate::rpc::core::MockCoreRPCLike; -use dashcore_rpc::dashcore::hashes::hex::FromHex; use drive::drive::defaults::PROTOCOL_VERSION; use std::path::Path; use std::str::FromStr; @@ -15,13 +14,12 @@ use std::sync::RwLock; use dashcore_rpc::dashcore::BlockHash; +use crate::execution::storage::{fetch_current_protocol_version, fetch_execution_state}; use crate::execution::types::block_execution_context::BlockExecutionContext; use crate::platform_types::platform_state::v0::PlatformStateV0Methods; use crate::platform_types::platform_state::PlatformState; use dpp::block::block_info::BlockInfo; -use dpp::serialization::PlatformDeserializable; use dpp::version::{PlatformVersion, PlatformVersionCurrentVersion}; -use drive::error::Error::GroveDB; use serde_json::json; /// Platform is not versioned as it holds the main logic, we could not switch from one structure @@ -94,8 +92,8 @@ impl<'a, C> From<&PlatformRef<'a, C>> for PlatformStateRef<'a> { } } -impl std::fmt::Debug for Platform { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl Debug for Platform { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("Platform").finish() } } @@ -145,22 +143,23 @@ impl Platform { Self::open_with_client(path, config, core_rpc_mock) } - /// Recreate the state from the backing store - pub fn recreate_state(&self, platform_version: &PlatformVersion) -> Result { - let Some(serialized_platform_state) = self.fetch_execution_state(None, platform_version) + /// Fetch and reload the state from the backing store + pub fn reload_state_from_storage( + &self, + platform_version: &PlatformVersion, + ) -> Result { + let Some(persisted_state) = fetch_execution_state(&self.drive, None, platform_version)? else { return Ok(false); }; - let recreated_state = - PlatformState::deserialize_from_bytes_no_limit(&serialized_platform_state)?; - PlatformVersion::set_current(PlatformVersion::get( - recreated_state.current_protocol_version_in_consensus(), + persisted_state.current_protocol_version_in_consensus(), )?); let mut state_cache = self.state.write().unwrap(); - *state_cache = recreated_state; + *state_cache = persisted_state; + Ok(true) } } @@ -178,29 +177,31 @@ impl Platform { let config = config.unwrap_or_default(); let drive = Drive::open(path, Some(config.drive.clone())).map_err(Error::Drive)?; - // TODO: factor out key so we don't duplicate - let maybe_serialized_platform_state = drive - .grove - .get_aux(b"saved_state", None) - .unwrap() - .map_err(|e| Error::Drive(GroveDB(e)))?; + if let Some(protocol_version) = fetch_current_protocol_version(&drive)? { + let platform_version = PlatformVersion::get(protocol_version)?; - if let Some(serialized_platform_state) = maybe_serialized_platform_state { - Platform::open_with_client_saved_state::

( - drive, - core_rpc, - config, - serialized_platform_state, - ) - } else { - Platform::open_with_client_no_saved_state::

( + let Some(execution_state) = fetch_execution_state(&drive, None, platform_version)? + else { + return Err(Error::Execution(ExecutionError::CorruptedCachedState( + "execution state should be stored as well as protocol version", + ))); + }; + + return Platform::open_with_client_saved_state::

( drive, core_rpc, config, - PROTOCOL_VERSION, - PROTOCOL_VERSION, - ) + execution_state, + ); } + + Platform::open_with_client_no_saved_state::

( + drive, + core_rpc, + config, + PROTOCOL_VERSION, + PROTOCOL_VERSION, + ) } /// Open Platform with Drive and block execution context from saved state. @@ -208,14 +209,11 @@ impl Platform { drive: Drive, core_rpc: C, config: PlatformConfig, - serialized_platform_state: Vec, + platform_state: PlatformState, ) -> Result, Error> where C: CoreRPCLike, { - let platform_state = - PlatformState::deserialize_from_bytes_no_limit(&serialized_platform_state)?; - PlatformVersion::set_current(PlatformVersion::get( platform_state.current_protocol_version_in_consensus(), )?); diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/mod.rs b/packages/rs-drive-abci/src/platform_types/platform_state/mod.rs index d4b08b9ecb8..e8a009c9fdd 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/mod.rs @@ -20,7 +20,6 @@ use dpp::util::deserializer::ProtocolVersion; use dpp::version::{PlatformVersion, TryFromPlatformVersioned, TryIntoPlatformVersioned}; use dpp::ProtocolError; -use dpp::ProtocolError::{PlatformDeserializationError, PlatformSerializationError}; use indexmap::IndexMap; use crate::error::execution::ExecutionError; @@ -59,34 +58,15 @@ impl PlatformSerializable for PlatformState { let platform_state_for_saving: PlatformStateForSaving = self.clone().try_into_platform_versioned(platform_version)?; bincode::encode_to_vec(platform_state_for_saving, config).map_err(|e| { - PlatformSerializationError(format!("unable to serialize PlatformState: {}", e)).into() + ProtocolError::PlatformSerializationError(format!( + "unable to serialize PlatformState: {}", + e + )) + .into() }) } } -// The version we should deserialize this into is determined by the actual saved state -impl PlatformDeserializable for PlatformState { - fn deserialize_from_bytes_no_limit(data: &[u8]) -> Result - where - Self: Sized, - { - let config = config::standard().with_big_endian().with_no_limit(); - let platform_state_in_save_format: PlatformStateForSaving = - bincode::decode_from_slice(data, config) - .map_err(|e| { - PlatformDeserializationError(format!( - "unable to deserialize PlatformStateForSaving: {}", - e - )) - })? - .0; - let platform_version = PlatformVersion::get( - platform_state_in_save_format.current_protocol_version_in_consensus(), - )?; - platform_state_in_save_format.try_into_platform_versioned(platform_version) - } -} - impl PlatformDeserializableFromVersionedStructure for PlatformState { fn versioned_deserialize( data: &[u8], @@ -99,13 +79,16 @@ impl PlatformDeserializableFromVersionedStructure for PlatformState { let platform_state_in_save_format: PlatformStateForSaving = bincode::decode_from_slice(data, config) .map_err(|e| { - PlatformDeserializationError(format!( + ProtocolError::PlatformDeserializationError(format!( "unable to deserialize PlatformStateForSaving: {}", e )) })? .0; - platform_state_in_save_format.try_into_platform_versioned(platform_version) + + platform_state_in_save_format + .try_into_platform_versioned(platform_version) + .map_err(|e: Error| ProtocolError::Generic(e.to_string())) } } @@ -183,7 +166,7 @@ impl TryFromPlatformVersioned for PlatformStateForSaving { } impl TryFromPlatformVersioned for PlatformState { - type Error = ProtocolError; + type Error = Error; fn try_from_platform_versioned( value: PlatformStateForSaving, @@ -193,16 +176,18 @@ impl TryFromPlatformVersioned for PlatformState { PlatformStateForSaving::V0(v0) => { match platform_version.drive_abci.structs.platform_state_structure { 0 => { - let platform_state_v0: PlatformStateV0 = v0.into(); + let platform_state_v0: PlatformStateV0 = + v0.try_into_platform_versioned(platform_version)?; + Ok(platform_state_v0.into()) } - version => Err(ProtocolError::UnknownVersionMismatch { + version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "PlatformState::try_from_platform_versioned(PlatformStateForSaving)" .to_string(), known_versions: vec![0], received: version, - }), + })), } } } diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs index dee68e502cf..2c744500309 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs @@ -17,7 +17,7 @@ use crate::platform_types::masternode::Masternode; use crate::platform_types::validator_set::ValidatorSet; use dpp::block::block_info::{BlockInfo, DEFAULT_BLOCK_INFO}; use dpp::block::extended_block_info::v0::ExtendedBlockInfoV0Getters; -use dpp::version::{PlatformVersion, TryIntoPlatformVersioned}; +use dpp::version::{PlatformVersion, TryFromPlatformVersioned, TryIntoPlatformVersioned}; use std::collections::BTreeMap; use std::fmt::{Debug, Formatter}; @@ -165,12 +165,17 @@ impl TryFrom for PlatformStateForSavingV0 { } } -impl From for PlatformStateV0 { - fn from(value: PlatformStateForSavingV0) -> Self { - PlatformStateV0 { +impl TryFromPlatformVersioned for PlatformStateV0 { + type Error = Error; + + fn try_from_platform_versioned( + value: PlatformStateForSavingV0, + platform_version: &PlatformVersion, + ) -> Result { + Ok(PlatformStateV0 { genesis_block_info: value.genesis_block_info, last_committed_block_info: value.last_committed_block_info, - current_protocol_version_in_consensus: value.current_protocol_version_in_consensus, + current_protocol_version_in_consensus: platform_version.protocol_version, next_epoch_protocol_version: value.next_epoch_protocol_version, current_validator_set_quorum_hash: QuorumHash::from_byte_array( value.current_validator_set_quorum_hash.to_buffer(), @@ -193,7 +198,7 @@ impl From for PlatformStateV0 { .into_iter() .map(|(k, v)| (ProTxHash::from_byte_array(k.to_buffer()), v.into())) .collect(), - } + }) } } diff --git a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs index b6af0b7264b..b8745e96fa9 100644 --- a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs @@ -26,7 +26,6 @@ pub struct ValidatorSetV0 { /// The list of masternodes pub members: BTreeMap, /// The threshold quorum public key - #[serde(serialize_with = "serialize_bls_public_key")] pub threshold_public_key: BlsPublicKey, } diff --git a/packages/rs-drive-abci/tests/strategy_tests/main.rs b/packages/rs-drive-abci/tests/strategy_tests/main.rs index 4c6e0d16cd8..1e10cd7237a 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/main.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/main.rs @@ -309,7 +309,7 @@ mod tests { abci_app .platform - .recreate_state(platform_version) + .reload_state_from_storage(platform_version) .expect("expected to recreate state"); let ResponseInfo { @@ -449,7 +449,7 @@ mod tests { abci_app .platform - .recreate_state(platform_version) + .reload_state_from_storage(platform_version) .expect("expected to recreate state"); let ResponseInfo { @@ -2890,7 +2890,7 @@ mod tests { abci_app .platform - .recreate_state(platform_version) + .reload_state_from_storage(platform_version) .expect("expected to recreate state"); let ResponseInfo { diff --git a/packages/rs-platform-version/src/version/drive_abci_versions.rs b/packages/rs-platform-version/src/version/drive_abci_versions.rs index a9dd0da3022..075403c8ad0 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions.rs @@ -79,7 +79,7 @@ pub struct DriveAbciValidationVersions { } #[derive(Clone, Debug, Default)] -struct DriveAbciExecutionStateStorageMethodVersions { +pub struct DriveAbciExecutionStateStorageMethodVersions { pub fetch_execution_state: FeatureVersion, pub store_execution_state: FeatureVersion, } diff --git a/packages/rs-platform-version/src/version/mocks/v2_test.rs b/packages/rs-platform-version/src/version/mocks/v2_test.rs index c6808fb4272..e37759d8013 100644 --- a/packages/rs-platform-version/src/version/mocks/v2_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v2_test.rs @@ -14,7 +14,8 @@ use crate::version::drive_abci_versions::{ DriveAbciBlockFeeProcessingMethodVersions, DriveAbciBlockStartMethodVersions, DriveAbciCoreBasedUpdatesMethodVersions, DriveAbciCoreSubsidyMethodVersions, DriveAbciDocumentsStateTransitionValidationVersions, DriveAbciEngineMethodVersions, - DriveAbciEpochMethodVersions, DriveAbciFeePoolInwardsDistributionMethodVersions, + DriveAbciEpochMethodVersions, DriveAbciExecutionStateStorageMethodVersions, + DriveAbciFeePoolInwardsDistributionMethodVersions, DriveAbciFeePoolOutwardsDistributionMethodVersions, DriveAbciIdentityCreditWithdrawalMethodVersions, DriveAbciInitializationMethodVersions, DriveAbciMasternodeIdentitiesUpdatesMethodVersions, DriveAbciMethodVersions, @@ -531,11 +532,14 @@ pub(crate) const TEST_PLATFORM_V2: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - store_execution_state: 0, update_execution_state: 0, update_drive_cache: 0, validator_set_update: 0, }, + execution_state_storage: DriveAbciExecutionStateStorageMethodVersions { + fetch_execution_state: 0, + store_execution_state: 0, + }, }, validation_and_processing: DriveAbciValidationVersions { state_transitions: DriveAbciStateTransitionValidationVersions { diff --git a/packages/rs-platform-version/src/version/mocks/v3_test.rs b/packages/rs-platform-version/src/version/mocks/v3_test.rs index 58d7bf69d92..ee166f2066a 100644 --- a/packages/rs-platform-version/src/version/mocks/v3_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v3_test.rs @@ -14,7 +14,8 @@ use crate::version::drive_abci_versions::{ DriveAbciBlockFeeProcessingMethodVersions, DriveAbciBlockStartMethodVersions, DriveAbciCoreBasedUpdatesMethodVersions, DriveAbciCoreSubsidyMethodVersions, DriveAbciDocumentsStateTransitionValidationVersions, DriveAbciEngineMethodVersions, - DriveAbciEpochMethodVersions, DriveAbciFeePoolInwardsDistributionMethodVersions, + DriveAbciEpochMethodVersions, DriveAbciExecutionStateStorageMethodVersions, + DriveAbciFeePoolInwardsDistributionMethodVersions, DriveAbciFeePoolOutwardsDistributionMethodVersions, DriveAbciIdentityCreditWithdrawalMethodVersions, DriveAbciInitializationMethodVersions, DriveAbciMasternodeIdentitiesUpdatesMethodVersions, DriveAbciMethodVersions, @@ -531,11 +532,14 @@ pub(crate) const TEST_PLATFORM_V3: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - store_execution_state: 0, update_execution_state: 0, update_drive_cache: 0, validator_set_update: 0, }, + execution_state_storage: DriveAbciExecutionStateStorageMethodVersions { + fetch_execution_state: 0, + store_execution_state: 0, + }, }, validation_and_processing: DriveAbciValidationVersions { state_transitions: DriveAbciStateTransitionValidationVersions { diff --git a/packages/rs-platform-version/src/version/v1.rs b/packages/rs-platform-version/src/version/v1.rs index d56f76a78a7..c3873ed43e5 100644 --- a/packages/rs-platform-version/src/version/v1.rs +++ b/packages/rs-platform-version/src/version/v1.rs @@ -14,7 +14,8 @@ use crate::version::drive_abci_versions::{ DriveAbciBlockFeeProcessingMethodVersions, DriveAbciBlockStartMethodVersions, DriveAbciCoreBasedUpdatesMethodVersions, DriveAbciCoreSubsidyMethodVersions, DriveAbciDocumentsStateTransitionValidationVersions, DriveAbciEngineMethodVersions, - DriveAbciEpochMethodVersions, DriveAbciFeePoolInwardsDistributionMethodVersions, + DriveAbciEpochMethodVersions, DriveAbciExecutionStateStorageMethodVersions, + DriveAbciFeePoolInwardsDistributionMethodVersions, DriveAbciFeePoolOutwardsDistributionMethodVersions, DriveAbciIdentityCreditWithdrawalMethodVersions, DriveAbciInitializationMethodVersions, DriveAbciMasternodeIdentitiesUpdatesMethodVersions, DriveAbciMethodVersions, @@ -528,11 +529,14 @@ pub(super) const PLATFORM_V1: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - store_execution_state: 0, update_execution_state: 0, update_drive_cache: 0, validator_set_update: 0, }, + execution_state_storage: DriveAbciExecutionStateStorageMethodVersions { + fetch_execution_state: 0, + store_execution_state: 0, + }, }, validation_and_processing: DriveAbciValidationVersions { state_transitions: DriveAbciStateTransitionValidationVersions { From c8808795c74278cfa0e4569d38c996aa4a390197 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 13 Nov 2023 17:07:50 +0700 Subject: [PATCH 04/24] chore: some additions --- .../src/execution/storage/protocol_version.rs | 14 ++++---------- .../src/platform_types/platform_state/v0/mod.rs | 4 ++-- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/storage/protocol_version.rs b/packages/rs-drive-abci/src/execution/storage/protocol_version.rs index fdc89554fb5..dc690b5ac93 100644 --- a/packages/rs-drive-abci/src/execution/storage/protocol_version.rs +++ b/packages/rs-drive-abci/src/execution/storage/protocol_version.rs @@ -2,7 +2,6 @@ use crate::error::execution::ExecutionError; use crate::error::Error; use crate::execution::storage::EXECUTION_STORAGE_PATH; use dpp::version::PlatformVersion; -use drive::drive::grove_operations::QueryType; use drive::drive::Drive; use drive::error::drive::DriveError; use drive::grovedb::Element; @@ -13,20 +12,15 @@ pub(super) const EXECUTION_STORAGE_PLATFORM_VERSION_KEY: &[u8; 1] = b"V"; /// This method can't be versioned since there is no knowledge about versions before it called /// but fallbacks to support all changes in this method must be implemented pub fn fetch_current_protocol_version(drive: &Drive) -> Result, Error> { - let mut ops = Vec::new(); - - let platform_version = PlatformVersion::latest(); - let maybe_element = drive - .grove_get( + .grove + .get_raw_optional( (&EXECUTION_STORAGE_PATH).into(), EXECUTION_STORAGE_PLATFORM_VERSION_KEY, - QueryType::StatefulQuery, None, - &mut ops, - &platform_version.drive, ) - .map_err(Error::Drive)?; + .unwrap() + .map_err(|e| Error::Drive(drive::error::Error::GroveDB(e)))?; let Some(element) = maybe_element else { return Ok(None); diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs index 2c744500309..2870089d94e 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs @@ -25,7 +25,7 @@ use std::fmt::{Debug, Formatter}; #[derive(Clone)] pub struct PlatformStateV0 { /// Information about the genesis block - pub genesis_block_info: Option, + pub genesis_block_info: Option, // TODO: we already have it in epoch 0 /// Information about the last block pub last_committed_block_info: Option, /// Current Version @@ -99,7 +99,7 @@ pub(super) struct PlatformStateForSavingV0 { /// Information about the last block pub last_committed_block_info: Option, /// Current Version - pub current_protocol_version_in_consensus: ProtocolVersion, + pub current_protocol_version_in_consensus: ProtocolVersion, // TODO: Remove this /// upcoming protocol version pub next_epoch_protocol_version: ProtocolVersion, /// current quorum From c4f1cc67de843ad13a8340778372b13c6418a58d Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sat, 20 Jan 2024 18:46:29 +0700 Subject: [PATCH 05/24] chore: use current protocol version to deserialize state --- .../src/execution/storage/mod.rs | 2 - .../src/execution/storage/protocol_version.rs | 42 ----------------- .../storage/store_execution_state/v0/mod.rs | 16 ------- packages/rs-drive-abci/src/main.rs | 6 +-- .../src/platform_types/platform/mod.rs | 10 ++--- .../rs-drive/src/drive/document/delete/mod.rs | 5 ++- packages/rs-drive/src/drive/open/mod.rs | 8 +--- ...d.rs => fetch_current_protocol_version.rs} | 26 +++++++++-- .../fetch_current_protocol_version/mod.rs | 44 ------------------ ...et_current_protocol_version_operations.rs} | 45 ++++++++++--------- .../v0/mod.rs | 35 --------------- packages/rs-drive/src/query/mod.rs | 5 +-- packages/rs-drive/src/tests/helpers/setup.rs | 5 +-- .../src/version/drive_versions.rs | 2 - .../src/version/mocks/v2_test.rs | 2 - .../src/version/mocks/v3_test.rs | 2 - .../rs-platform-version/src/version/v1.rs | 2 - 17 files changed, 61 insertions(+), 196 deletions(-) delete mode 100644 packages/rs-drive-abci/src/execution/storage/protocol_version.rs rename packages/rs-drive/src/drive/system/protocol_version/{fetch_current_protocol_version/v0/mod.rs => fetch_current_protocol_version.rs} (64%) delete mode 100644 packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version/mod.rs rename packages/rs-drive/src/drive/system/protocol_version/{set_current_protocol_version_operations/mod.rs => set_current_protocol_version_operations.rs} (56%) delete mode 100644 packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations/v0/mod.rs diff --git a/packages/rs-drive-abci/src/execution/storage/mod.rs b/packages/rs-drive-abci/src/execution/storage/mod.rs index efc34c11c17..ccf15930949 100644 --- a/packages/rs-drive-abci/src/execution/storage/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/mod.rs @@ -1,11 +1,9 @@ use drive::drive::RootTree; mod fetch_execution_state; -mod protocol_version; mod store_execution_state; pub use fetch_execution_state::*; -pub use protocol_version::fetch_current_protocol_version; pub use store_execution_state::*; const EXECUTION_STORAGE_PATH: [[u8; 1]; 1] = [[RootTree::Misc as u8]]; diff --git a/packages/rs-drive-abci/src/execution/storage/protocol_version.rs b/packages/rs-drive-abci/src/execution/storage/protocol_version.rs deleted file mode 100644 index dc690b5ac93..00000000000 --- a/packages/rs-drive-abci/src/execution/storage/protocol_version.rs +++ /dev/null @@ -1,42 +0,0 @@ -use crate::error::execution::ExecutionError; -use crate::error::Error; -use crate::execution::storage::EXECUTION_STORAGE_PATH; -use dpp::version::PlatformVersion; -use drive::drive::Drive; -use drive::error::drive::DriveError; -use drive::grovedb::Element; - -pub(super) const EXECUTION_STORAGE_PLATFORM_VERSION_KEY: &[u8; 1] = b"V"; - -/// Fetches the current execution protocol version from the drive -/// This method can't be versioned since there is no knowledge about versions before it called -/// but fallbacks to support all changes in this method must be implemented -pub fn fetch_current_protocol_version(drive: &Drive) -> Result, Error> { - let maybe_element = drive - .grove - .get_raw_optional( - (&EXECUTION_STORAGE_PATH).into(), - EXECUTION_STORAGE_PLATFORM_VERSION_KEY, - None, - ) - .unwrap() - .map_err(|e| Error::Drive(drive::error::Error::GroveDB(e)))?; - - let Some(element) = maybe_element else { - return Ok(None); - }; - - let Element::Item(bytes, _) = element else { - return Err(Error::Execution(ExecutionError::CorruptedCachedState( - "execution state should be stored as an element item", - ))); - }; - - let protocol_version = u32::from_be_bytes(bytes.as_slice().try_into().map_err(|_| { - drive::error::Error::Drive(DriveError::CorruptedSerialization(String::from( - "protocol version length item have an invalid length", - ))) - })?); - - Ok(Some(protocol_version)) -} diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs index e223e87401f..b913fb5e7aa 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs @@ -1,8 +1,6 @@ use crate::error::Error; -use crate::execution::storage::protocol_version::EXECUTION_STORAGE_PLATFORM_VERSION_KEY; use crate::execution::storage::{EXECUTION_STORAGE_PATH, EXECUTION_STORAGE_STATE_KEY}; use crate::platform_types::platform::Platform; -use crate::platform_types::platform_state::v0::PlatformStateV0Methods; use crate::platform_types::platform_state::PlatformState; use dpp::serialization::PlatformSerializable; use dpp::version::PlatformVersion; @@ -25,20 +23,6 @@ impl Platform { .map(|byte_array| byte_array.to_vec()) .collect(); - let protocol_version_element = Element::Item( - state - .current_protocol_version_in_consensus() - .to_be_bytes() - .to_vec(), - None, - ); - - batch.add_insert( - path.clone(), - EXECUTION_STORAGE_PLATFORM_VERSION_KEY.to_vec(), - protocol_version_element, - ); - let state_element = Element::Item(state.serialize_to_bytes()?, None); batch.add_insert(path, EXECUTION_STORAGE_STATE_KEY.to_vec(), state_element); diff --git a/packages/rs-drive-abci/src/main.rs b/packages/rs-drive-abci/src/main.rs index 53876c3f33e..375f1af2927 100644 --- a/packages/rs-drive-abci/src/main.rs +++ b/packages/rs-drive-abci/src/main.rs @@ -371,11 +371,11 @@ mod test { fn setup_db(tempdir: &Path) -> PathBuf { let path = tempdir.join("db"); fs::create_dir(&path).expect("create db dir"); - + + let drive = Drive::open(&path, None).expect("open drive"); + let platform_version = PlatformVersion::latest(); - let drive = Drive::open(&path, None, platform_version).expect("open drive"); - drive .create_initial_state_structure(None, platform_version) .expect("should create root tree successfully"); diff --git a/packages/rs-drive-abci/src/platform_types/platform/mod.rs b/packages/rs-drive-abci/src/platform_types/platform/mod.rs index 10a4f14ea11..7000d7cbca8 100644 --- a/packages/rs-drive-abci/src/platform_types/platform/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform/mod.rs @@ -14,7 +14,7 @@ use std::sync::RwLock; use dashcore_rpc::dashcore::BlockHash; -use crate::execution::storage::{fetch_current_protocol_version, fetch_execution_state}; +use crate::execution::storage::fetch_execution_state; use crate::execution::types::block_execution_context::BlockExecutionContext; use crate::platform_types::platform_state::v0::PlatformStateV0Methods; use crate::platform_types::platform_state::PlatformState; @@ -176,13 +176,9 @@ impl Platform { { let config = config.unwrap_or_default(); - // TODO: Replace with version from the disk if present or latest? - let platform_version = PlatformVersion::latest(); + let drive = Drive::open(path, Some(config.drive.clone())).map_err(Error::Drive)?; - let drive = Drive::open(path, Some(config.drive.clone()), platform_version) - .map_err(Error::Drive)?; - - if let Some(protocol_version) = fetch_current_protocol_version(&drive)? { + if let Some(protocol_version) = drive.fetch_current_protocol_version(None)? { let platform_version = PlatformVersion::get(protocol_version)?; let Some(execution_state) = fetch_execution_state(&drive, None, platform_version)? diff --git a/packages/rs-drive/src/drive/document/delete/mod.rs b/packages/rs-drive/src/drive/document/delete/mod.rs index 01a7df4435b..a43971fef05 100644 --- a/packages/rs-drive/src/drive/document/delete/mod.rs +++ b/packages/rs-drive/src/drive/document/delete/mod.rs @@ -118,9 +118,10 @@ mod tests { #[test] fn test_add_and_remove_family_one_document_no_transaction() { let tmp_dir = TempDir::new().unwrap(); + + let drive: Drive = Drive::open(tmp_dir, None).expect("expected to open Drive successfully"); + let platform_version = PlatformVersion::latest(); - let drive: Drive = Drive::open(tmp_dir, None, platform_version) - .expect("expected to open Drive successfully"); drive .create_initial_state_structure(None, platform_version) diff --git a/packages/rs-drive/src/drive/open/mod.rs b/packages/rs-drive/src/drive/open/mod.rs index 7496a651cd7..f9c61d86ce4 100644 --- a/packages/rs-drive/src/drive/open/mod.rs +++ b/packages/rs-drive/src/drive/open/mod.rs @@ -24,11 +24,7 @@ impl Drive { /// /// * `Result` - On success, returns `Ok(Self)`, where `Self` is a `Drive` instance. On error, returns an `Error`. /// - pub fn open>( - path: P, - config: Option, - platform_version: &PlatformVersion, - ) -> Result { + pub fn open>(path: P, config: Option) -> Result { match GroveDb::open(path) { Ok(grove) => { let config = config.unwrap_or_default(); @@ -40,7 +36,7 @@ impl Drive { grove, config, system_contracts: SystemContracts::load_genesis_system_contracts( - platform_version.protocol_version, + 0, // TODO: Will be fixed in #1676 )?, cache: RwLock::new(DriveCache { cached_contracts: DataContractCache::new( diff --git a/packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version/v0/mod.rs b/packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version.rs similarity index 64% rename from packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version/v0/mod.rs rename to packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version.rs index a8035d3049e..2a38de6766d 100644 --- a/packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version/v0/mod.rs +++ b/packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version.rs @@ -1,16 +1,36 @@ -use crate::drive::system::misc_path; -use crate::drive::system::misc_tree_constants::PROTOCOL_VERSION_STORAGE_KEY; use crate::drive::Drive; use crate::error::drive::DriveError; use crate::error::Error; +use crate::drive::system::misc_path; +use crate::drive::system::misc_tree_constants::PROTOCOL_VERSION_STORAGE_KEY; use dpp::util::deserializer::ProtocolVersion; use grovedb::TransactionArg; use integer_encoding::VarInt; +/// impl Drive { /// Gets the current protocol version from the backing store - pub(super) fn fetch_current_protocol_version_v0( + /// + /// !!!DON'T CHANGE!!!! + /// + /// This function should never be changed !!! since it's using + /// to get protocol version to read the state from the storage. + /// In plain English, this is the first function that we call, + /// so we don't know version yet. + /// + /// # Arguments + /// + /// * `transaction` - A `TransactionArg` object representing the transaction. + /// + /// # Returns + /// + /// * `Result, Error>` - If successful, returns an `Ok(Option)`. If an error occurs during the operation, returns an `Error`. + /// + /// # Errors + /// + /// This function will return an error if the Drive version is unknown. + pub fn fetch_current_protocol_version( &self, transaction: TransactionArg, ) -> Result, Error> { diff --git a/packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version/mod.rs b/packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version/mod.rs deleted file mode 100644 index 78e654cc382..00000000000 --- a/packages/rs-drive/src/drive/system/protocol_version/fetch_current_protocol_version/mod.rs +++ /dev/null @@ -1,44 +0,0 @@ -mod v0; - -use crate::drive::Drive; -use crate::error::drive::DriveError; -use crate::error::Error; - -use dpp::util::deserializer::ProtocolVersion; -use dpp::version::drive_versions::DriveVersion; -use grovedb::TransactionArg; - -impl Drive { - /// Gets the current protocol version from the backing store - /// - /// # Arguments - /// - /// * `transaction` - A `TransactionArg` object representing the transaction. - /// - /// # Returns - /// - /// * `Result, Error>` - If successful, returns an `Ok(Option)`. If an error occurs during the operation, returns an `Error`. - /// - /// # Errors - /// - /// This function will return an error if the Drive version is unknown. - pub fn fetch_current_protocol_version( - &self, - transaction: TransactionArg, - drive_version: &DriveVersion, - ) -> Result, Error> { - match drive_version - .methods - .platform_system - .protocol_version - .fetch_current_protocol_version - { - 0 => self.fetch_current_protocol_version_v0(transaction), - version => Err(Error::Drive(DriveError::UnknownVersionMismatch { - method: "fetch_current_protocol_version".to_string(), - known_versions: vec![0], - received: version, - })), - } - } -} diff --git a/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations/mod.rs b/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations.rs similarity index 56% rename from packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations/mod.rs rename to packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations.rs index 8e271bb055d..86913633542 100644 --- a/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations/mod.rs +++ b/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations.rs @@ -1,16 +1,23 @@ -mod v0; - +use crate::drive::grove_operations::BatchInsertApplyType; +use crate::drive::object_size_info::PathKeyElementInfo; +use crate::drive::system::misc_path; +use crate::drive::system::misc_tree_constants::PROTOCOL_VERSION_STORAGE_KEY; use crate::drive::Drive; -use crate::error::drive::DriveError; use crate::error::Error; use crate::fee::op::LowLevelDriveOperation; use dpp::util::deserializer::ProtocolVersion; use dpp::version::drive_versions::DriveVersion; -use grovedb::TransactionArg; +use grovedb::{Element, TransactionArg}; +use integer_encoding::VarInt; +///!!!DON'T CHANGE!!!! impl Drive { /// Sets the current protocol version /// + /// !!!DON'T CHANGE!!!! + /// This function should never be changed !!! since it must always be compatible + /// with fetch_current_protocol_version which is should never be changed. + /// /// # Arguments /// /// * `protocol_version` - A `ProtocolVersion` object representing the current protocol version. @@ -32,23 +39,17 @@ impl Drive { drive_operations: &mut Vec, drive_version: &DriveVersion, ) -> Result<(), Error> { - match drive_version - .methods - .platform_system - .protocol_version - .set_current_protocol_version_operations - { - 0 => self.set_current_protocol_version_operations_v0( - protocol_version, - transaction, - drive_operations, - drive_version, - ), - version => Err(Error::Drive(DriveError::UnknownVersionMismatch { - method: "set_current_protocol_version_operations".to_string(), - known_versions: vec![0], - received: version, - })), - } + self.batch_insert_if_changed_value( + PathKeyElementInfo::PathFixedSizeKeyRefElement(( + misc_path(), + PROTOCOL_VERSION_STORAGE_KEY, + Element::new_item(protocol_version.encode_var_vec()), + )), + BatchInsertApplyType::StatefulBatchInsert, + transaction, + drive_operations, + drive_version, + )?; + Ok(()) } } diff --git a/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations/v0/mod.rs b/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations/v0/mod.rs deleted file mode 100644 index c91ec847d9d..00000000000 --- a/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations/v0/mod.rs +++ /dev/null @@ -1,35 +0,0 @@ -use crate::drive::grove_operations::BatchInsertApplyType; -use crate::drive::object_size_info::PathKeyElementInfo; -use crate::drive::system::misc_path; -use crate::drive::system::misc_tree_constants::PROTOCOL_VERSION_STORAGE_KEY; -use crate::drive::Drive; -use crate::error::Error; -use crate::fee::op::LowLevelDriveOperation; -use dpp::util::deserializer::ProtocolVersion; -use dpp::version::drive_versions::DriveVersion; -use grovedb::{Element, TransactionArg}; -use integer_encoding::VarInt; - -impl Drive { - /// Sets the current protocol version - pub(super) fn set_current_protocol_version_operations_v0( - &self, - protocol_version: ProtocolVersion, - transaction: TransactionArg, - drive_operations: &mut Vec, - drive_version: &DriveVersion, - ) -> Result<(), Error> { - self.batch_insert_if_changed_value( - PathKeyElementInfo::PathFixedSizeKeyRefElement(( - misc_path(), - PROTOCOL_VERSION_STORAGE_KEY, - Element::new_item(protocol_version.encode_var_vec()), - )), - BatchInsertApplyType::StatefulBatchInsert, - transaction, - drive_operations, - drive_version, - )?; - Ok(()) - } -} diff --git a/packages/rs-drive/src/query/mod.rs b/packages/rs-drive/src/query/mod.rs index 0023c0681fb..7217e72c101 100644 --- a/packages/rs-drive/src/query/mod.rs +++ b/packages/rs-drive/src/query/mod.rs @@ -1974,10 +1974,9 @@ mod tests { fn setup_family_contract() -> (Drive, DataContract) { let tmp_dir = TempDir::new().unwrap(); - let platform_version = PlatformVersion::latest(); + let drive: Drive = Drive::open(tmp_dir, None).expect("expected to open Drive successfully"); - let drive: Drive = Drive::open(tmp_dir, None, platform_version) - .expect("expected to open Drive successfully"); + let platform_version = PlatformVersion::latest(); drive .create_initial_state_structure(None, platform_version) diff --git a/packages/rs-drive/src/tests/helpers/setup.rs b/packages/rs-drive/src/tests/helpers/setup.rs index 15095a15f2f..a62e55de431 100644 --- a/packages/rs-drive/src/tests/helpers/setup.rs +++ b/packages/rs-drive/src/tests/helpers/setup.rs @@ -64,9 +64,8 @@ impl Default for SetupFeePoolsOptions { /// Sets up Drive using a temporary directory and the optionally given Drive configuration settings. pub fn setup_drive(drive_config: Option) -> Drive { let tmp_dir = TempDir::new().unwrap(); - let platform_version = PlatformVersion::latest(); - let drive: Drive = Drive::open(tmp_dir, drive_config, platform_version) - .expect("should open Drive successfully"); + + let drive: Drive = Drive::open(tmp_dir, drive_config).expect("should open Drive successfully"); drive } diff --git a/packages/rs-platform-version/src/version/drive_versions.rs b/packages/rs-platform-version/src/version/drive_versions.rs index eb8d6f7da77..2ff79bbc87b 100644 --- a/packages/rs-platform-version/src/version/drive_versions.rs +++ b/packages/rs-platform-version/src/version/drive_versions.rs @@ -211,8 +211,6 @@ pub struct DriveBatchOperationsMethodVersion { #[derive(Clone, Debug, Default)] pub struct DriveSystemProtocolVersionMethodVersions { - pub fetch_current_protocol_version: FeatureVersion, - pub set_current_protocol_version_operations: FeatureVersion, pub fetch_next_protocol_version: FeatureVersion, pub set_next_protocol_version_operations: FeatureVersion, } diff --git a/packages/rs-platform-version/src/version/mocks/v2_test.rs b/packages/rs-platform-version/src/version/mocks/v2_test.rs index 7f471f7037a..9856e308771 100644 --- a/packages/rs-platform-version/src/version/mocks/v2_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v2_test.rs @@ -362,8 +362,6 @@ pub(crate) const TEST_PLATFORM_V2: PlatformVersion = PlatformVersion { }, platform_system: DrivePlatformSystemMethodVersions { protocol_version: DriveSystemProtocolVersionMethodVersions { - fetch_current_protocol_version: 0, - set_current_protocol_version_operations: 0, fetch_next_protocol_version: 0, set_next_protocol_version_operations: 0, }, diff --git a/packages/rs-platform-version/src/version/mocks/v3_test.rs b/packages/rs-platform-version/src/version/mocks/v3_test.rs index ade61c4a895..b1e6ff86e1a 100644 --- a/packages/rs-platform-version/src/version/mocks/v3_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v3_test.rs @@ -370,8 +370,6 @@ pub(crate) const TEST_PLATFORM_V3: PlatformVersion = PlatformVersion { }, platform_system: DrivePlatformSystemMethodVersions { protocol_version: DriveSystemProtocolVersionMethodVersions { - fetch_current_protocol_version: 0, - set_current_protocol_version_operations: 0, fetch_next_protocol_version: 0, set_next_protocol_version_operations: 0, }, diff --git a/packages/rs-platform-version/src/version/v1.rs b/packages/rs-platform-version/src/version/v1.rs index 7b95cabd231..ec7425147dc 100644 --- a/packages/rs-platform-version/src/version/v1.rs +++ b/packages/rs-platform-version/src/version/v1.rs @@ -359,8 +359,6 @@ pub(super) const PLATFORM_V1: PlatformVersion = PlatformVersion { }, platform_system: DrivePlatformSystemMethodVersions { protocol_version: DriveSystemProtocolVersionMethodVersions { - fetch_current_protocol_version: 0, - set_current_protocol_version_operations: 0, fetch_next_protocol_version: 0, set_next_protocol_version_operations: 0, }, From 3482c945ea14723c303756b74427a76c17a0991f Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sat, 20 Jan 2024 18:57:45 +0700 Subject: [PATCH 06/24] chore: keep using aux data --- packages/rs-drive-abci/src/error/mod.rs | 4 +++ .../storage/fetch_execution_state/v0/mod.rs | 34 +++++-------------- .../src/execution/storage/mod.rs | 5 +-- .../storage/store_execution_state/mod.rs | 2 +- .../storage/store_execution_state/v0/mod.rs | 29 ++++++---------- 5 files changed, 25 insertions(+), 49 deletions(-) diff --git a/packages/rs-drive-abci/src/error/mod.rs b/packages/rs-drive-abci/src/error/mod.rs index 3658a8169dc..749c4544737 100644 --- a/packages/rs-drive-abci/src/error/mod.rs +++ b/packages/rs-drive-abci/src/error/mod.rs @@ -7,6 +7,7 @@ use dpp::platform_value::Error as ValueError; use dpp::version::PlatformVersionError; use drive::dpp::ProtocolError; use drive::error::Error as DriveError; +use drive::grovedb; use tenderdash_abci::proto::abci::ResponseException; use tracing::error; @@ -48,6 +49,9 @@ pub enum Error { /// Error from metrics subsystem #[error("metrics: {0}")] Metrics(#[from] crate::metrics::Error), + /// GroveDB errors + #[error("grovedb: {0}")] + GroveDb(#[from] grovedb::Error), } impl From for Error { diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs index 4e80920ca9b..208cd74ae06 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs @@ -1,42 +1,26 @@ -use crate::error::execution::ExecutionError; use crate::error::Error; -use crate::execution::storage::{EXECUTION_STORAGE_PATH, EXECUTION_STORAGE_STATE_KEY}; +use crate::execution::storage::EXECUTION_STORAGE_STATE_KEY; use crate::platform_types::platform_state::PlatformState; -use dpp::serialization::{PlatformDeserializable, PlatformDeserializableFromVersionedStructure}; +use dpp::serialization::PlatformDeserializableFromVersionedStructure; use dpp::version::PlatformVersion; -use drive::drive::grove_operations::QueryType; use drive::drive::Drive; -use drive::error::drive::DriveError; -use drive::query::{Element, TransactionArg}; +use drive::query::TransactionArg; pub(super) fn fetch_execution_state_v0( drive: &Drive, transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result, Error> { - let mut ops = Vec::new(); + let maybe_bytes = drive + .grove + .get_aux(EXECUTION_STORAGE_STATE_KEY, transaction) + .unwrap() + .map_err(Error::GroveDb)?; - let maybe_element = drive - .grove_get( - (&EXECUTION_STORAGE_PATH).into(), - EXECUTION_STORAGE_STATE_KEY, - QueryType::StatefulQuery, - transaction, - &mut ops, - &platform_version.drive, - ) - .map_err(Error::Drive)?; - - let Some(element) = maybe_element else { + let Some(bytes) = maybe_bytes else { return Ok(None); }; - let Element::Item(bytes, _) = element else { - return Err(Error::Execution(ExecutionError::CorruptedCachedState( - "execution state should be stored as an element item", - ))); - }; - let execution_state = PlatformState::versioned_deserialize(&bytes, platform_version)?; Ok(Some(execution_state)) diff --git a/packages/rs-drive-abci/src/execution/storage/mod.rs b/packages/rs-drive-abci/src/execution/storage/mod.rs index ccf15930949..e5f33cd649f 100644 --- a/packages/rs-drive-abci/src/execution/storage/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/mod.rs @@ -1,10 +1,7 @@ -use drive::drive::RootTree; - mod fetch_execution_state; mod store_execution_state; pub use fetch_execution_state::*; pub use store_execution_state::*; -const EXECUTION_STORAGE_PATH: [[u8; 1]; 1] = [[RootTree::Misc as u8]]; -const EXECUTION_STORAGE_STATE_KEY: &[u8; 1] = b"S"; +const EXECUTION_STORAGE_STATE_KEY: &[u8; 11] = b"saved_state"; diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs index cd91f0ca637..413a4785c8e 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs @@ -21,7 +21,7 @@ impl Platform { .execution_state_storage .store_execution_state { - 0 => self.store_execution_state_v0(state, transaction, platform_version), + 0 => self.store_execution_state_v0(state, transaction), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "fetch_execution_state".to_string(), known_versions: vec![0], diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs index b913fb5e7aa..8bc7e9289e9 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs @@ -1,34 +1,25 @@ use crate::error::Error; -use crate::execution::storage::{EXECUTION_STORAGE_PATH, EXECUTION_STORAGE_STATE_KEY}; +use crate::execution::storage::EXECUTION_STORAGE_STATE_KEY; use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use dpp::serialization::PlatformSerializable; -use dpp::version::PlatformVersion; -use drive::drive::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; -use drive::drive::batch::GroveDbOpBatch; use drive::grovedb::Transaction; -use drive::query::Element; impl Platform { pub(super) fn store_execution_state_v0( &self, state: &PlatformState, transaction: &Transaction, - platform_version: &PlatformVersion, ) -> Result<(), Error> { - let mut batch = GroveDbOpBatch::new(); - - let path: Vec> = EXECUTION_STORAGE_PATH - .iter() - .map(|byte_array| byte_array.to_vec()) - .collect(); - - let state_element = Element::Item(state.serialize_to_bytes()?, None); - - batch.add_insert(path, EXECUTION_STORAGE_STATE_KEY.to_vec(), state_element); - self.drive - .grove_apply_batch(batch, false, Some(transaction), &platform_version.drive) - .map_err(Error::Drive) + .grove + .put_aux( + EXECUTION_STORAGE_STATE_KEY, + &state.serialize_to_bytes()?, + None, + Some(transaction), + ) + .unwrap() + .map_err(Error::GroveDb) } } From 168de58df897df78a6507f320025138a03b5dbd9 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sun, 21 Jan 2024 01:10:41 +0700 Subject: [PATCH 07/24] fix: invalid protocol version --- .../src/execution/storage/fetch_execution_state/mod.rs | 1 - packages/rs-drive/src/drive/open/mod.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs index ba5fab244ea..073894ad8d7 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs @@ -1,6 +1,5 @@ use crate::error::execution::ExecutionError; use crate::error::Error; -use crate::execution::storage::EXECUTION_STORAGE_PATH; use crate::platform_types::platform_state::PlatformState; use dpp::version::PlatformVersion; use drive::drive::Drive; diff --git a/packages/rs-drive/src/drive/open/mod.rs b/packages/rs-drive/src/drive/open/mod.rs index f9c61d86ce4..3c5cb661924 100644 --- a/packages/rs-drive/src/drive/open/mod.rs +++ b/packages/rs-drive/src/drive/open/mod.rs @@ -36,7 +36,7 @@ impl Drive { grove, config, system_contracts: SystemContracts::load_genesis_system_contracts( - 0, // TODO: Will be fixed in #1676 + 1, // TODO: Will be fixed in #1676 )?, cache: RwLock::new(DriveCache { cached_contracts: DataContractCache::new( From 8f11682a1f895c2be6a9d57ddbe1d61592b800f2 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sun, 21 Jan 2024 01:50:27 +0700 Subject: [PATCH 08/24] style: fix formatting --- packages/rs-drive-abci/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rs-drive-abci/src/main.rs b/packages/rs-drive-abci/src/main.rs index 375f1af2927..dec4bc0a6e5 100644 --- a/packages/rs-drive-abci/src/main.rs +++ b/packages/rs-drive-abci/src/main.rs @@ -371,9 +371,9 @@ mod test { fn setup_db(tempdir: &Path) -> PathBuf { let path = tempdir.join("db"); fs::create_dir(&path).expect("create db dir"); - + let drive = Drive::open(&path, None).expect("open drive"); - + let platform_version = PlatformVersion::latest(); drive From 68abee5c8847237d7c5cbab3a9a53843aadbcc2a Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 22 Jan 2024 18:57:24 +0700 Subject: [PATCH 09/24] refactor: don't use grovedb directly in drive abci --- .../src/abci/handler/execution_result.rs | 7 ++--- packages/rs-drive-abci/src/error/mod.rs | 3 -- .../storage/fetch_execution_state/v0/mod.rs | 7 ++--- .../storage/store_execution_state/mod.rs | 6 ++-- .../storage/store_execution_state/v0/mod.rs | 18 +++++------ .../fetch_execution_state_bytes/mod.rs | 30 ++++++++++++++++++ .../fetch_execution_state_bytes/v0/mod.rs | 16 ++++++++++ .../rs-drive/src/drive/execution_state/mod.rs | 7 +++++ .../store_execution_state_bytes/mod.rs | 31 +++++++++++++++++++ .../store_execution_state_bytes/v0/mod.rs | 18 +++++++++++ packages/rs-drive/src/drive/mod.rs | 1 + .../src/version/drive_versions.rs | 7 +++++ 12 files changed, 126 insertions(+), 25 deletions(-) create mode 100644 packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/mod.rs create mode 100644 packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/v0/mod.rs create mode 100644 packages/rs-drive/src/drive/execution_state/mod.rs create mode 100644 packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/mod.rs create mode 100644 packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/v0/mod.rs diff --git a/packages/rs-drive-abci/src/abci/handler/execution_result.rs b/packages/rs-drive-abci/src/abci/handler/execution_result.rs index e3b0c0ff2f6..4f7b1230178 100644 --- a/packages/rs-drive-abci/src/abci/handler/execution_result.rs +++ b/packages/rs-drive-abci/src/abci/handler/execution_result.rs @@ -1,14 +1,11 @@ use crate::abci::handler::error::consensus::AbciResponseInfoGetter; use crate::abci::handler::error::HandlerError; use crate::error::Error; -use crate::platform_types::state_transitions_processing_result::{ - StateTransitionExecutionResult, StateTransitionsProcessingResult, -}; +use crate::platform_types::state_transitions_processing_result::StateTransitionExecutionResult; use dpp::fee::SignedCredits; use dpp::version::PlatformVersion; use dpp::version::TryIntoPlatformVersioned; -use tenderdash_abci::proto::abci::tx_record::TxAction; -use tenderdash_abci::proto::abci::{ExecTxResult, TxRecord}; +use tenderdash_abci::proto::abci::ExecTxResult; impl TryIntoPlatformVersioned for StateTransitionExecutionResult { type Error = Error; diff --git a/packages/rs-drive-abci/src/error/mod.rs b/packages/rs-drive-abci/src/error/mod.rs index 749c4544737..3cee5d1c64e 100644 --- a/packages/rs-drive-abci/src/error/mod.rs +++ b/packages/rs-drive-abci/src/error/mod.rs @@ -49,9 +49,6 @@ pub enum Error { /// Error from metrics subsystem #[error("metrics: {0}")] Metrics(#[from] crate::metrics::Error), - /// GroveDB errors - #[error("grovedb: {0}")] - GroveDb(#[from] grovedb::Error), } impl From for Error { diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs index 208cd74ae06..cdbfaab749b 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs @@ -1,5 +1,4 @@ use crate::error::Error; -use crate::execution::storage::EXECUTION_STORAGE_STATE_KEY; use crate::platform_types::platform_state::PlatformState; use dpp::serialization::PlatformDeserializableFromVersionedStructure; use dpp::version::PlatformVersion; @@ -12,10 +11,8 @@ pub(super) fn fetch_execution_state_v0( platform_version: &PlatformVersion, ) -> Result, Error> { let maybe_bytes = drive - .grove - .get_aux(EXECUTION_STORAGE_STATE_KEY, transaction) - .unwrap() - .map_err(Error::GroveDb)?; + .fetch_execution_state_bytes(transaction, platform_version) + .map_err(Error::Drive)?; let Some(bytes) = maybe_bytes else { return Ok(None); diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs index 413a4785c8e..3dffaea1148 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs @@ -5,14 +5,14 @@ use crate::error::Error; use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use dpp::version::PlatformVersion; -use drive::grovedb::Transaction; +use drive::query::TransactionArg; impl Platform { /// Store the execution state in grovedb storage pub fn store_execution_state( &self, state: &PlatformState, - transaction: &Transaction, + transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result<(), Error> { match platform_version @@ -21,7 +21,7 @@ impl Platform { .execution_state_storage .store_execution_state { - 0 => self.store_execution_state_v0(state, transaction), + 0 => self.store_execution_state_v0(state, transaction, platform_version), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "fetch_execution_state".to_string(), known_versions: vec![0], diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs index 8bc7e9289e9..c55067721f9 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs @@ -4,22 +4,22 @@ use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use dpp::serialization::PlatformSerializable; use drive::grovedb::Transaction; +use drive::query::TransactionArg; +use platform_version::version::PlatformVersion; impl Platform { pub(super) fn store_execution_state_v0( &self, state: &PlatformState, - transaction: &Transaction, + transaction: TransactionArg, + platform_version: &PlatformVersion, ) -> Result<(), Error> { self.drive - .grove - .put_aux( - EXECUTION_STORAGE_STATE_KEY, - &state.serialize_to_bytes()?, - None, - Some(transaction), + .store_execution_state_bytes( + state.versioned_serialize()?, + transaction, + platform_version, ) - .unwrap() - .map_err(Error::GroveDb) + .map_err(Error::Drive) } } diff --git a/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/mod.rs b/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/mod.rs new file mode 100644 index 00000000000..abee8f85510 --- /dev/null +++ b/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/mod.rs @@ -0,0 +1,30 @@ +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +mod v0; + +impl Drive { + /// Fetches execution state from grovedb storage + pub fn fetch_execution_state_bytes( + &self, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result>, Error> { + match platform_version + .drive + .methods + .execution_state + .fetch_execution_state_bytes + { + 0 => self.fetch_execution_state_bytes_v0(transaction), + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "fetch_execution_state_bytes".to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/v0/mod.rs new file mode 100644 index 00000000000..002a4ef6c94 --- /dev/null +++ b/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/v0/mod.rs @@ -0,0 +1,16 @@ +use crate::drive::execution_state::EXECUTION_STORAGE_STATE_KEY; +use crate::drive::Drive; +use crate::error::Error; +use grovedb::TransactionArg; + +impl Drive { + pub(super) fn fetch_execution_state_bytes_v0( + &self, + transaction: TransactionArg, + ) -> Result>, Error> { + self.grove + .get_aux(EXECUTION_STORAGE_STATE_KEY, transaction) + .unwrap() + .map_err(Error::GroveDB) + } +} diff --git a/packages/rs-drive/src/drive/execution_state/mod.rs b/packages/rs-drive/src/drive/execution_state/mod.rs new file mode 100644 index 00000000000..9878b0d86cf --- /dev/null +++ b/packages/rs-drive/src/drive/execution_state/mod.rs @@ -0,0 +1,7 @@ +mod fetch_execution_state_bytes; +mod store_execution_state_bytes; + +pub use fetch_execution_state_bytes::*; +pub use store_execution_state_bytes::*; + +const EXECUTION_STORAGE_STATE_KEY: &[u8; 11] = b"saved_state"; diff --git a/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/mod.rs b/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/mod.rs new file mode 100644 index 00000000000..e3af9482d53 --- /dev/null +++ b/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/mod.rs @@ -0,0 +1,31 @@ +mod v0; + +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Store the execution state in grovedb storage + pub fn store_execution_state_bytes( + &self, + state_bytes: &[u8], + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + match platform_version + .drive + .methods + .execution_state + .store_execution_state_bytes + { + 0 => self.store_execution_state_bytes_v0(state_bytes, transaction), + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "store_execution_state_bytes".to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/v0/mod.rs new file mode 100644 index 00000000000..a44d5012d76 --- /dev/null +++ b/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/v0/mod.rs @@ -0,0 +1,18 @@ +use crate::drive::execution_state::EXECUTION_STORAGE_STATE_KEY; +use crate::drive::Drive; +use crate::error::Error; +use dpp::serialization::PlatformSerializable; +use grovedb::TransactionArg; + +impl Drive { + pub(super) fn store_execution_state_bytes_v0( + &self, + state_bytes: &[u8], + transaction: TransactionArg, + ) -> Result<(), Error> { + self.grove + .put_aux(EXECUTION_STORAGE_STATE_KEY, state_bytes, None, transaction) + .unwrap() + .map_err(Error::GroveDB) + } +} diff --git a/packages/rs-drive/src/drive/mod.rs b/packages/rs-drive/src/drive/mod.rs index 053a051452a..0bd4bb19fc0 100644 --- a/packages/rs-drive/src/drive/mod.rs +++ b/packages/rs-drive/src/drive/mod.rs @@ -86,6 +86,7 @@ mod test_utils; #[cfg(feature = "full")] mod asset_lock; +mod execution_state; #[cfg(feature = "full")] pub(crate) mod fee; #[cfg(feature = "full")] diff --git a/packages/rs-platform-version/src/version/drive_versions.rs b/packages/rs-platform-version/src/version/drive_versions.rs index 2ff79bbc87b..e04169b1705 100644 --- a/packages/rs-platform-version/src/version/drive_versions.rs +++ b/packages/rs-platform-version/src/version/drive_versions.rs @@ -31,6 +31,13 @@ pub struct DriveMethodVersions { pub batch_operations: DriveBatchOperationsMethodVersion, pub prove: DriveProveMethodVersions, pub state_transitions: DriveStateTransitionMethodVersions, + pub execution_state: DriveExecutionStateMethodVersions, +} + +#[derive(Clone, Debug, Default)] +struct DriveExecutionStateMethodVersions { + pub fetch_execution_state_bytes: FeatureVersion, + pub store_execution_state_bytes: FeatureVersion, } #[derive(Clone, Debug, Default)] From 4bb53821b4e401f4d30c7508eaf5b71cef881a73 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 22 Jan 2024 19:07:07 +0700 Subject: [PATCH 10/24] chore: some fixes --- packages/rs-drive-abci/src/execution/storage/mod.rs | 2 -- .../src/execution/storage/store_execution_state/v0/mod.rs | 2 -- packages/rs-drive/src/drive/mod.rs | 1 + 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/storage/mod.rs b/packages/rs-drive-abci/src/execution/storage/mod.rs index e5f33cd649f..08dd7bca642 100644 --- a/packages/rs-drive-abci/src/execution/storage/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/mod.rs @@ -3,5 +3,3 @@ mod store_execution_state; pub use fetch_execution_state::*; pub use store_execution_state::*; - -const EXECUTION_STORAGE_STATE_KEY: &[u8; 11] = b"saved_state"; diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs index c55067721f9..16369b2a879 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs @@ -1,9 +1,7 @@ use crate::error::Error; -use crate::execution::storage::EXECUTION_STORAGE_STATE_KEY; use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use dpp::serialization::PlatformSerializable; -use drive::grovedb::Transaction; use drive::query::TransactionArg; use platform_version::version::PlatformVersion; diff --git a/packages/rs-drive/src/drive/mod.rs b/packages/rs-drive/src/drive/mod.rs index 0bd4bb19fc0..89939ce8bc3 100644 --- a/packages/rs-drive/src/drive/mod.rs +++ b/packages/rs-drive/src/drive/mod.rs @@ -86,6 +86,7 @@ mod test_utils; #[cfg(feature = "full")] mod asset_lock; +#[cfg(feature = "full")] mod execution_state; #[cfg(feature = "full")] pub(crate) mod fee; From 980d3715d53dd2bf51446789e3b58f751793fb15 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 22 Jan 2024 19:09:45 +0700 Subject: [PATCH 11/24] refactor: renamings --- .../src/execution/engine/finalize_block_proposal/v0/mod.rs | 2 +- .../src/execution/platform_events/block_end/mod.rs | 2 +- .../{update_execution_state => update_state_cache}/mod.rs | 6 +++--- .../v0/mod.rs | 4 ++-- .../rs-platform-version/src/version/drive_abci_versions.rs | 2 +- packages/rs-platform-version/src/version/mocks/v2_test.rs | 2 +- packages/rs-platform-version/src/version/mocks/v3_test.rs | 2 +- packages/rs-platform-version/src/version/v1.rs | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) rename packages/rs-drive-abci/src/execution/platform_events/block_end/{update_execution_state => update_state_cache}/mod.rs (90%) rename packages/rs-drive-abci/src/execution/platform_events/block_end/{update_execution_state => update_state_cache}/v0/mod.rs (95%) diff --git a/packages/rs-drive-abci/src/execution/engine/finalize_block_proposal/v0/mod.rs b/packages/rs-drive-abci/src/execution/engine/finalize_block_proposal/v0/mod.rs index a3a6cbc88ae..7c0cf75f7a6 100644 --- a/packages/rs-drive-abci/src/execution/engine/finalize_block_proposal/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/engine/finalize_block_proposal/v0/mod.rs @@ -217,7 +217,7 @@ where } .into(); - self.update_execution_state(extended_block_info, transaction, platform_version)?; + self.update_state_cache(extended_block_info, transaction, platform_version)?; self.update_drive_cache(platform_version)?; diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs index fa102da37cc..1ab5c69cbc4 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs @@ -1,5 +1,5 @@ /// Updating the execution state happens as the final part of block finalization -pub(in crate::execution) mod update_execution_state; +pub(in crate::execution) mod update_state_cache; /// Validator set update pub(in crate::execution) mod validator_set_update; diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs similarity index 90% rename from packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/mod.rs rename to packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs index d357b87d1cd..120c25326b8 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs @@ -32,7 +32,7 @@ where /// * `Result<(), Error>` - If the state cache and quorums are successfully updated, it returns `Ok(())`. /// If there is a problem with the update, it returns an `Error`. /// - pub fn update_execution_state( + pub fn update_state_cache( &self, extended_block_info: ExtendedBlockInfo, transaction: &Transaction, @@ -42,9 +42,9 @@ where .drive_abci .methods .block_end - .update_execution_state + .update_state_cache { - 0 => self.update_execution_state_v0(extended_block_info, transaction, platform_version), + 0 => self.update_state_cache_v0(extended_block_info, transaction, platform_version), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "update_state_cache".to_string(), known_versions: vec![0], diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs similarity index 95% rename from packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/v0/mod.rs rename to packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs index f18240d17f7..c1d1751052f 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs @@ -34,7 +34,7 @@ where /// This function may return an `Error` variant if there is a problem with updating the state cache /// and quorums or storing the ephemeral data. /// - pub(super) fn update_execution_state_v0( + pub(super) fn update_state_cache_v0( &self, extended_block_info: ExtendedBlockInfo, transaction: &Transaction, @@ -64,7 +64,7 @@ where PlatformVersion::set_current(version); // Persist execution state - self.store_execution_state(&state, transaction, platform_version)?; + self.store_execution_state(&state, Some(transaction), platform_version)?; Ok(()) } diff --git a/packages/rs-platform-version/src/version/drive_abci_versions.rs b/packages/rs-platform-version/src/version/drive_abci_versions.rs index b6fa1b794f4..461b0324ac2 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions.rs @@ -230,7 +230,7 @@ pub struct DriveAbciBlockStartMethodVersions { #[derive(Clone, Debug, Default)] pub struct DriveAbciBlockEndMethodVersions { - pub update_execution_state: FeatureVersion, + pub update_state_cache: FeatureVersion, pub update_drive_cache: FeatureVersion, pub validator_set_update: FeatureVersion, } diff --git a/packages/rs-platform-version/src/version/mocks/v2_test.rs b/packages/rs-platform-version/src/version/mocks/v2_test.rs index 9856e308771..b832262022f 100644 --- a/packages/rs-platform-version/src/version/mocks/v2_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v2_test.rs @@ -530,7 +530,7 @@ pub(crate) const TEST_PLATFORM_V2: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - update_execution_state: 0, + update_state_cache: 0, update_drive_cache: 0, validator_set_update: 0, }, diff --git a/packages/rs-platform-version/src/version/mocks/v3_test.rs b/packages/rs-platform-version/src/version/mocks/v3_test.rs index b1e6ff86e1a..89fe0afaf7c 100644 --- a/packages/rs-platform-version/src/version/mocks/v3_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v3_test.rs @@ -530,7 +530,7 @@ pub(crate) const TEST_PLATFORM_V3: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - update_execution_state: 0, + update_state_cache: 0, update_drive_cache: 0, validator_set_update: 0, }, diff --git a/packages/rs-platform-version/src/version/v1.rs b/packages/rs-platform-version/src/version/v1.rs index ec7425147dc..fcb76a8c5f4 100644 --- a/packages/rs-platform-version/src/version/v1.rs +++ b/packages/rs-platform-version/src/version/v1.rs @@ -527,7 +527,7 @@ pub(super) const PLATFORM_V1: PlatformVersion = PlatformVersion { clear_drive_block_cache: 0, }, block_end: DriveAbciBlockEndMethodVersions { - update_execution_state: 0, + update_state_cache: 0, update_drive_cache: 0, validator_set_update: 0, }, From 24b742b7ab1174f0015d83cbbe520ab8762de67d Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 22 Jan 2024 19:57:59 +0700 Subject: [PATCH 12/24] refactor: use map instead of if --- .../storage/fetch_execution_state/v0/mod.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs index cdbfaab749b..831d39f25f5 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs @@ -10,15 +10,11 @@ pub(super) fn fetch_execution_state_v0( transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result, Error> { - let maybe_bytes = drive + drive .fetch_execution_state_bytes(transaction, platform_version) - .map_err(Error::Drive)?; - - let Some(bytes) = maybe_bytes else { - return Ok(None); - }; - - let execution_state = PlatformState::versioned_deserialize(&bytes, platform_version)?; - - Ok(Some(execution_state)) + .map_err(Error::Drive)? + .map(|bytes| { + PlatformState::versioned_deserialize(&bytes, platform_version).map_err(Error::Protocol) + }) + .transpose() } From 5e6e7f73f3e6bdd61ad73e515a3ff3959e5c43cd Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 22 Jan 2024 21:04:39 +0700 Subject: [PATCH 13/24] refactor: rename methods --- .../mod.rs | 4 ++-- .../v0/mod.rs | 4 ++-- packages/rs-drive-abci/src/execution/storage/mod.rs | 8 ++++---- .../mod.rs | 2 +- .../v0/mod.rs | 4 ++-- .../fetch_platform_state_bytes}/mod.rs | 6 +++--- .../fetch_platform_state_bytes}/v0/mod.rs | 2 +- .../src/drive/{execution_state => platform_state}/mod.rs | 4 ++-- .../store_platform_state_bytes}/mod.rs | 6 +++--- .../store_platform_state_bytes}/v0/mod.rs | 2 +- .../rs-platform-version/src/version/drive_versions.rs | 4 ++-- 11 files changed, 23 insertions(+), 23 deletions(-) rename packages/rs-drive-abci/src/execution/storage/{fetch_execution_state => fetch_platform_state}/mod.rs (87%) rename packages/rs-drive-abci/src/execution/storage/{fetch_execution_state => fetch_platform_state}/v0/mod.rs (84%) rename packages/rs-drive-abci/src/execution/storage/{store_execution_state => store_platform_state}/mod.rs (96%) rename packages/rs-drive-abci/src/execution/storage/{store_execution_state => store_platform_state}/v0/mod.rs (87%) rename packages/rs-drive/src/drive/{execution_state/fetch_execution_state_bytes => platform_state/fetch_platform_state_bytes}/mod.rs (83%) rename packages/rs-drive/src/drive/{execution_state/fetch_execution_state_bytes => platform_state/fetch_platform_state_bytes}/v0/mod.rs (88%) rename packages/rs-drive/src/drive/{execution_state => platform_state}/mod.rs (68%) rename packages/rs-drive/src/drive/{execution_state/store_execution_state_bytes => platform_state/store_platform_state_bytes}/mod.rs (82%) rename packages/rs-drive/src/drive/{execution_state/store_execution_state_bytes => platform_state/store_platform_state_bytes}/v0/mod.rs (90%) diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs similarity index 87% rename from packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs rename to packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs index 073894ad8d7..7734d99160b 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs @@ -8,7 +8,7 @@ use drive::query::TransactionArg; mod v0; /// Fetches execution state from grovedb storage -pub fn fetch_execution_state( +pub fn fetch_platform_state( drive: &Drive, transaction: TransactionArg, platform_version: &PlatformVersion, @@ -19,7 +19,7 @@ pub fn fetch_execution_state( .execution_state_storage .fetch_execution_state { - 0 => v0::fetch_execution_state_v0(drive, transaction, platform_version), + 0 => v0::fetch_platform_state_v0(drive, transaction, platform_version), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "fetch_execution_state".to_string(), known_versions: vec![0], diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/v0/mod.rs similarity index 84% rename from packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs rename to packages/rs-drive-abci/src/execution/storage/fetch_platform_state/v0/mod.rs index 831d39f25f5..8fe01a58a8a 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/v0/mod.rs @@ -5,13 +5,13 @@ use dpp::version::PlatformVersion; use drive::drive::Drive; use drive::query::TransactionArg; -pub(super) fn fetch_execution_state_v0( +pub(super) fn fetch_platform_state_v0( drive: &Drive, transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result, Error> { drive - .fetch_execution_state_bytes(transaction, platform_version) + .fetch_platform_state_bytes(transaction, platform_version) .map_err(Error::Drive)? .map(|bytes| { PlatformState::versioned_deserialize(&bytes, platform_version).map_err(Error::Protocol) diff --git a/packages/rs-drive-abci/src/execution/storage/mod.rs b/packages/rs-drive-abci/src/execution/storage/mod.rs index 08dd7bca642..c45853e2599 100644 --- a/packages/rs-drive-abci/src/execution/storage/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/mod.rs @@ -1,5 +1,5 @@ -mod fetch_execution_state; -mod store_execution_state; +mod fetch_platform_state; +mod store_platform_state; -pub use fetch_execution_state::*; -pub use store_execution_state::*; +pub use fetch_platform_state::*; +pub use store_platform_state::*; diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_platform_state/mod.rs similarity index 96% rename from packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs rename to packages/rs-drive-abci/src/execution/storage/store_platform_state/mod.rs index 3dffaea1148..91bf14e083e 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_platform_state/mod.rs @@ -9,7 +9,7 @@ use drive::query::TransactionArg; impl Platform { /// Store the execution state in grovedb storage - pub fn store_execution_state( + pub fn store_platform_state( &self, state: &PlatformState, transaction: TransactionArg, diff --git a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_platform_state/v0/mod.rs similarity index 87% rename from packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs rename to packages/rs-drive-abci/src/execution/storage/store_platform_state/v0/mod.rs index 16369b2a879..b41bc873ed6 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_execution_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_platform_state/v0/mod.rs @@ -6,14 +6,14 @@ use drive::query::TransactionArg; use platform_version::version::PlatformVersion; impl Platform { - pub(super) fn store_execution_state_v0( + pub(super) fn store_platform_state_v0( &self, state: &PlatformState, transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result<(), Error> { self.drive - .store_execution_state_bytes( + .store_platform_state_bytes( state.versioned_serialize()?, transaction, platform_version, diff --git a/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/mod.rs b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/mod.rs similarity index 83% rename from packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/mod.rs rename to packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/mod.rs index abee8f85510..40486769755 100644 --- a/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/mod.rs @@ -8,7 +8,7 @@ mod v0; impl Drive { /// Fetches execution state from grovedb storage - pub fn fetch_execution_state_bytes( + pub fn fetch_platform_state_bytes( &self, transaction: TransactionArg, platform_version: &PlatformVersion, @@ -17,9 +17,9 @@ impl Drive { .drive .methods .execution_state - .fetch_execution_state_bytes + .fetch_platform_state_bytes { - 0 => self.fetch_execution_state_bytes_v0(transaction), + 0 => self.fetch_platform_state_bytes_v0(transaction), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "fetch_execution_state_bytes".to_string(), known_versions: vec![0], diff --git a/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs similarity index 88% rename from packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/v0/mod.rs rename to packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs index 002a4ef6c94..ab4e4d26e28 100644 --- a/packages/rs-drive/src/drive/execution_state/fetch_execution_state_bytes/v0/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs @@ -4,7 +4,7 @@ use crate::error::Error; use grovedb::TransactionArg; impl Drive { - pub(super) fn fetch_execution_state_bytes_v0( + pub(super) fn fetch_platform_state_bytes_v0( &self, transaction: TransactionArg, ) -> Result>, Error> { diff --git a/packages/rs-drive/src/drive/execution_state/mod.rs b/packages/rs-drive/src/drive/platform_state/mod.rs similarity index 68% rename from packages/rs-drive/src/drive/execution_state/mod.rs rename to packages/rs-drive/src/drive/platform_state/mod.rs index 9878b0d86cf..c784d589bde 100644 --- a/packages/rs-drive/src/drive/execution_state/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/mod.rs @@ -1,5 +1,5 @@ -mod fetch_execution_state_bytes; -mod store_execution_state_bytes; +mod fetch_platform_state_bytes; +mod store_platform_state_bytes; pub use fetch_execution_state_bytes::*; pub use store_execution_state_bytes::*; diff --git a/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/mod.rs b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/mod.rs similarity index 82% rename from packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/mod.rs rename to packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/mod.rs index e3af9482d53..8280bbe7179 100644 --- a/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/mod.rs @@ -8,7 +8,7 @@ use grovedb::TransactionArg; impl Drive { /// Store the execution state in grovedb storage - pub fn store_execution_state_bytes( + pub fn store_platform_state_bytes( &self, state_bytes: &[u8], transaction: TransactionArg, @@ -18,9 +18,9 @@ impl Drive { .drive .methods .execution_state - .store_execution_state_bytes + .store_platform_state_bytes { - 0 => self.store_execution_state_bytes_v0(state_bytes, transaction), + 0 => self.store_platform_state_bytes_v0(state_bytes, transaction), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "store_execution_state_bytes".to_string(), known_versions: vec![0], diff --git a/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs similarity index 90% rename from packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/v0/mod.rs rename to packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs index a44d5012d76..101e454089b 100644 --- a/packages/rs-drive/src/drive/execution_state/store_execution_state_bytes/v0/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs @@ -5,7 +5,7 @@ use dpp::serialization::PlatformSerializable; use grovedb::TransactionArg; impl Drive { - pub(super) fn store_execution_state_bytes_v0( + pub(super) fn store_platform_state_bytes_v0( &self, state_bytes: &[u8], transaction: TransactionArg, diff --git a/packages/rs-platform-version/src/version/drive_versions.rs b/packages/rs-platform-version/src/version/drive_versions.rs index e04169b1705..26d2fa4a533 100644 --- a/packages/rs-platform-version/src/version/drive_versions.rs +++ b/packages/rs-platform-version/src/version/drive_versions.rs @@ -36,8 +36,8 @@ pub struct DriveMethodVersions { #[derive(Clone, Debug, Default)] struct DriveExecutionStateMethodVersions { - pub fetch_execution_state_bytes: FeatureVersion, - pub store_execution_state_bytes: FeatureVersion, + pub fetch_platform_state_bytes: FeatureVersion, + pub store_platform_state_bytes: FeatureVersion, } #[derive(Clone, Debug, Default)] From 49f2b20ac8b659b454a3f1a5f8c18b3a98366f4a Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 22 Jan 2024 21:23:31 +0700 Subject: [PATCH 14/24] fix: compilation errors --- .../block_end/update_state_cache/v0/mod.rs | 2 +- .../storage/fetch_platform_state/mod.rs | 6 ++-- .../storage/store_platform_state/mod.rs | 8 ++--- .../storage/store_platform_state/v0/mod.rs | 8 ++--- .../src/platform_types/platform/mod.rs | 6 ++-- packages/rs-drive/src/drive/mod.rs | 4 +-- .../fetch_platform_state_bytes/mod.rs | 4 +-- .../fetch_platform_state_bytes/v0/mod.rs | 2 +- .../rs-drive/src/drive/platform_state/mod.rs | 4 +-- .../store_platform_state_bytes/mod.rs | 4 +-- .../store_platform_state_bytes/v0/mod.rs | 2 +- .../src/version/drive_abci_versions.rs | 8 ++--- .../src/version/drive_versions.rs | 4 +-- .../src/version/mocks/v2_test.rs | 32 +++++++++++-------- .../src/version/mocks/v3_test.rs | 32 +++++++++++-------- .../rs-platform-version/src/version/v1.rs | 32 +++++++++++-------- 16 files changed, 83 insertions(+), 75 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs index c1d1751052f..69f12144d96 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs @@ -64,7 +64,7 @@ where PlatformVersion::set_current(version); // Persist execution state - self.store_execution_state(&state, Some(transaction), platform_version)?; + self.store_platform_state(&state, Some(transaction), platform_version)?; Ok(()) } diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs index 7734d99160b..d69e13796b7 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs @@ -16,12 +16,12 @@ pub fn fetch_platform_state( match platform_version .drive_abci .methods - .execution_state_storage - .fetch_execution_state + .platform_state_storage + .fetch_platform_state { 0 => v0::fetch_platform_state_v0(drive, transaction, platform_version), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { - method: "fetch_execution_state".to_string(), + method: "fetch_platform_state".to_string(), known_versions: vec![0], received: version, })), diff --git a/packages/rs-drive-abci/src/execution/storage/store_platform_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_platform_state/mod.rs index 91bf14e083e..203b81d2019 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_platform_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_platform_state/mod.rs @@ -18,12 +18,12 @@ impl Platform { match platform_version .drive_abci .methods - .execution_state_storage - .store_execution_state + .platform_state_storage + .store_platform_state { - 0 => self.store_execution_state_v0(state, transaction, platform_version), + 0 => self.store_platform_state_v0(state, transaction, platform_version), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { - method: "fetch_execution_state".to_string(), + method: "store_platform_state".to_string(), known_versions: vec![0], received: version, })), diff --git a/packages/rs-drive-abci/src/execution/storage/store_platform_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_platform_state/v0/mod.rs index b41bc873ed6..b41100eb477 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_platform_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_platform_state/v0/mod.rs @@ -2,8 +2,8 @@ use crate::error::Error; use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use dpp::serialization::PlatformSerializable; +use dpp::version::PlatformVersion; use drive::query::TransactionArg; -use platform_version::version::PlatformVersion; impl Platform { pub(super) fn store_platform_state_v0( @@ -13,11 +13,7 @@ impl Platform { platform_version: &PlatformVersion, ) -> Result<(), Error> { self.drive - .store_platform_state_bytes( - state.versioned_serialize()?, - transaction, - platform_version, - ) + .store_platform_state_bytes(&state.serialize_to_bytes()?, transaction, platform_version) .map_err(Error::Drive) } } diff --git a/packages/rs-drive-abci/src/platform_types/platform/mod.rs b/packages/rs-drive-abci/src/platform_types/platform/mod.rs index 7000d7cbca8..50eebed5a1e 100644 --- a/packages/rs-drive-abci/src/platform_types/platform/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform/mod.rs @@ -14,7 +14,7 @@ use std::sync::RwLock; use dashcore_rpc::dashcore::BlockHash; -use crate::execution::storage::fetch_execution_state; +use crate::execution::storage::fetch_platform_state; use crate::execution::types::block_execution_context::BlockExecutionContext; use crate::platform_types::platform_state::v0::PlatformStateV0Methods; use crate::platform_types::platform_state::PlatformState; @@ -148,7 +148,7 @@ impl Platform { &self, platform_version: &PlatformVersion, ) -> Result { - let Some(persisted_state) = fetch_execution_state(&self.drive, None, platform_version)? + let Some(persisted_state) = fetch_platform_state(&self.drive, None, platform_version)? else { return Ok(false); }; @@ -181,7 +181,7 @@ impl Platform { if let Some(protocol_version) = drive.fetch_current_protocol_version(None)? { let platform_version = PlatformVersion::get(protocol_version)?; - let Some(execution_state) = fetch_execution_state(&drive, None, platform_version)? + let Some(execution_state) = fetch_platform_state(&drive, None, platform_version)? else { return Err(Error::Execution(ExecutionError::CorruptedCachedState( "execution state should be stored as well as protocol version", diff --git a/packages/rs-drive/src/drive/mod.rs b/packages/rs-drive/src/drive/mod.rs index 89939ce8bc3..d7fbda7eee8 100644 --- a/packages/rs-drive/src/drive/mod.rs +++ b/packages/rs-drive/src/drive/mod.rs @@ -87,14 +87,14 @@ mod test_utils; #[cfg(feature = "full")] mod asset_lock; #[cfg(feature = "full")] -mod execution_state; -#[cfg(feature = "full")] pub(crate) mod fee; #[cfg(feature = "full")] mod open; #[cfg(feature = "full")] mod operations; #[cfg(feature = "full")] +mod platform_state; +#[cfg(feature = "full")] mod prove; #[cfg(feature = "full")] mod system_contracts_cache; diff --git a/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/mod.rs b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/mod.rs index 40486769755..1f341d57eab 100644 --- a/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/mod.rs @@ -16,12 +16,12 @@ impl Drive { match platform_version .drive .methods - .execution_state + .platform_state .fetch_platform_state_bytes { 0 => self.fetch_platform_state_bytes_v0(transaction), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { - method: "fetch_execution_state_bytes".to_string(), + method: "fetch_platform_state_bytes".to_string(), known_versions: vec![0], received: version, })), diff --git a/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs index ab4e4d26e28..b53ef12082a 100644 --- a/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs @@ -1,4 +1,4 @@ -use crate::drive::execution_state::EXECUTION_STORAGE_STATE_KEY; +use crate::drive::platform_state::EXECUTION_STORAGE_STATE_KEY; use crate::drive::Drive; use crate::error::Error; use grovedb::TransactionArg; diff --git a/packages/rs-drive/src/drive/platform_state/mod.rs b/packages/rs-drive/src/drive/platform_state/mod.rs index c784d589bde..2cc7cccce96 100644 --- a/packages/rs-drive/src/drive/platform_state/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/mod.rs @@ -1,7 +1,7 @@ mod fetch_platform_state_bytes; mod store_platform_state_bytes; -pub use fetch_execution_state_bytes::*; -pub use store_execution_state_bytes::*; +pub use fetch_platform_state_bytes::*; +pub use store_platform_state_bytes::*; const EXECUTION_STORAGE_STATE_KEY: &[u8; 11] = b"saved_state"; diff --git a/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/mod.rs b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/mod.rs index 8280bbe7179..4b5bb4be29c 100644 --- a/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/mod.rs @@ -17,12 +17,12 @@ impl Drive { match platform_version .drive .methods - .execution_state + .platform_state .store_platform_state_bytes { 0 => self.store_platform_state_bytes_v0(state_bytes, transaction), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { - method: "store_execution_state_bytes".to_string(), + method: "store_platform_state_bytes".to_string(), known_versions: vec![0], received: version, })), diff --git a/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs index 101e454089b..326523f590b 100644 --- a/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs @@ -1,4 +1,4 @@ -use crate::drive::execution_state::EXECUTION_STORAGE_STATE_KEY; +use crate::drive::platform_state::EXECUTION_STORAGE_STATE_KEY; use crate::drive::Drive; use crate::error::Error; use dpp::serialization::PlatformSerializable; diff --git a/packages/rs-platform-version/src/version/drive_abci_versions.rs b/packages/rs-platform-version/src/version/drive_abci_versions.rs index 461b0324ac2..e39f192cf83 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions.rs @@ -69,7 +69,7 @@ pub struct DriveAbciMethodVersions { pub epoch: DriveAbciEpochMethodVersions, pub block_start: DriveAbciBlockStartMethodVersions, pub block_end: DriveAbciBlockEndMethodVersions, - pub execution_state_storage: DriveAbciExecutionStateStorageMethodVersions, + pub platform_state_storage: DriveAbciPlatformStateStorageMethodVersions, } #[derive(Clone, Debug, Default)] @@ -80,9 +80,9 @@ pub struct DriveAbciValidationVersions { } #[derive(Clone, Debug, Default)] -pub struct DriveAbciExecutionStateStorageMethodVersions { - pub fetch_execution_state: FeatureVersion, - pub store_execution_state: FeatureVersion, +pub struct DriveAbciPlatformStateStorageMethodVersions { + pub fetch_platform_state: FeatureVersion, + pub store_platform_state: FeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/packages/rs-platform-version/src/version/drive_versions.rs b/packages/rs-platform-version/src/version/drive_versions.rs index 26d2fa4a533..7afa099da93 100644 --- a/packages/rs-platform-version/src/version/drive_versions.rs +++ b/packages/rs-platform-version/src/version/drive_versions.rs @@ -31,11 +31,11 @@ pub struct DriveMethodVersions { pub batch_operations: DriveBatchOperationsMethodVersion, pub prove: DriveProveMethodVersions, pub state_transitions: DriveStateTransitionMethodVersions, - pub execution_state: DriveExecutionStateMethodVersions, + pub platform_state: DrivePlatformStateMethodVersions, } #[derive(Clone, Debug, Default)] -struct DriveExecutionStateMethodVersions { +pub struct DrivePlatformStateMethodVersions { pub fetch_platform_state_bytes: FeatureVersion, pub store_platform_state_bytes: FeatureVersion, } diff --git a/packages/rs-platform-version/src/version/mocks/v2_test.rs b/packages/rs-platform-version/src/version/mocks/v2_test.rs index b832262022f..0982caf98c0 100644 --- a/packages/rs-platform-version/src/version/mocks/v2_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v2_test.rs @@ -14,13 +14,13 @@ use crate::version::drive_abci_versions::{ DriveAbciBlockFeeProcessingMethodVersions, DriveAbciBlockStartMethodVersions, DriveAbciCoreBasedUpdatesMethodVersions, DriveAbciCoreSubsidyMethodVersions, DriveAbciDocumentsStateTransitionValidationVersions, DriveAbciEngineMethodVersions, - DriveAbciEpochMethodVersions, DriveAbciExecutionStateStorageMethodVersions, - DriveAbciFeePoolInwardsDistributionMethodVersions, + DriveAbciEpochMethodVersions, DriveAbciFeePoolInwardsDistributionMethodVersions, DriveAbciFeePoolOutwardsDistributionMethodVersions, DriveAbciIdentityCreditWithdrawalMethodVersions, DriveAbciInitializationMethodVersions, DriveAbciMasternodeIdentitiesUpdatesMethodVersions, DriveAbciMethodVersions, - DriveAbciProtocolUpgradeMethodVersions, DriveAbciQueryDataContractVersions, - DriveAbciQueryIdentityVersions, DriveAbciQuerySystemVersions, DriveAbciQueryVersions, + DriveAbciPlatformStateStorageMethodVersions, DriveAbciProtocolUpgradeMethodVersions, + DriveAbciQueryDataContractVersions, DriveAbciQueryIdentityVersions, + DriveAbciQuerySystemVersions, DriveAbciQueryVersions, DriveAbciStateTransitionCommonValidationVersions, DriveAbciStateTransitionProcessingMethodVersions, DriveAbciStateTransitionValidationVersion, DriveAbciStateTransitionValidationVersions, DriveAbciStructureVersions, @@ -51,13 +51,13 @@ use crate::version::drive_versions::{ DriveIdentityKeysProveMethodVersions, DriveIdentityMethodVersions, DriveIdentityProveMethodVersions, DriveIdentityUpdateMethodVersions, DriveInitializationMethodVersions, DriveMethodVersions, DriveOperationsMethodVersion, - DrivePlatformSystemMethodVersions, DriveProtocolUpgradeVersions, DriveProveMethodVersions, - DriveStateTransitionMethodVersions, DriveStateTransitionOperationMethodVersions, - DriveStructureVersion, DriveSystemEstimationCostsMethodVersions, - DriveSystemProtocolVersionMethodVersions, DriveVerifyContractMethodVersions, - DriveVerifyDocumentMethodVersions, DriveVerifyIdentityMethodVersions, - DriveVerifyMethodVersions, DriveVerifySingleDocumentMethodVersions, - DriveVerifySystemMethodVersions, DriveVersion, + DrivePlatformStateMethodVersions, DrivePlatformSystemMethodVersions, + DriveProtocolUpgradeVersions, DriveProveMethodVersions, DriveStateTransitionMethodVersions, + DriveStateTransitionOperationMethodVersions, DriveStructureVersion, + DriveSystemEstimationCostsMethodVersions, DriveSystemProtocolVersionMethodVersions, + DriveVerifyContractMethodVersions, DriveVerifyDocumentMethodVersions, + DriveVerifyIdentityMethodVersions, DriveVerifyMethodVersions, + DriveVerifySingleDocumentMethodVersions, DriveVerifySystemMethodVersions, DriveVersion, }; use crate::version::mocks::TEST_BYTES; use crate::version::protocol_version::{FeatureVersionBounds, PlatformVersion}; @@ -390,6 +390,10 @@ pub(crate) const TEST_PLATFORM_V2: PlatformVersion = PlatformVersion { }, }, }, + platform_state: DrivePlatformStateMethodVersions { + fetch_platform_state_bytes: 0, + store_platform_state_bytes: 0, + }, }, grove_methods: DriveGroveMethodVersions { basic: DriveGroveBasicMethodVersions { @@ -534,9 +538,9 @@ pub(crate) const TEST_PLATFORM_V2: PlatformVersion = PlatformVersion { update_drive_cache: 0, validator_set_update: 0, }, - execution_state_storage: DriveAbciExecutionStateStorageMethodVersions { - fetch_execution_state: 0, - store_execution_state: 0, + platform_state_storage: DriveAbciPlatformStateStorageMethodVersions { + fetch_platform_state: 0, + store_platform_state: 0, }, }, validation_and_processing: DriveAbciValidationVersions { diff --git a/packages/rs-platform-version/src/version/mocks/v3_test.rs b/packages/rs-platform-version/src/version/mocks/v3_test.rs index 89fe0afaf7c..57ea010faca 100644 --- a/packages/rs-platform-version/src/version/mocks/v3_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v3_test.rs @@ -14,13 +14,13 @@ use crate::version::drive_abci_versions::{ DriveAbciBlockFeeProcessingMethodVersions, DriveAbciBlockStartMethodVersions, DriveAbciCoreBasedUpdatesMethodVersions, DriveAbciCoreSubsidyMethodVersions, DriveAbciDocumentsStateTransitionValidationVersions, DriveAbciEngineMethodVersions, - DriveAbciEpochMethodVersions, DriveAbciExecutionStateStorageMethodVersions, - DriveAbciFeePoolInwardsDistributionMethodVersions, + DriveAbciEpochMethodVersions, DriveAbciFeePoolInwardsDistributionMethodVersions, DriveAbciFeePoolOutwardsDistributionMethodVersions, DriveAbciIdentityCreditWithdrawalMethodVersions, DriveAbciInitializationMethodVersions, DriveAbciMasternodeIdentitiesUpdatesMethodVersions, DriveAbciMethodVersions, - DriveAbciProtocolUpgradeMethodVersions, DriveAbciQueryDataContractVersions, - DriveAbciQueryIdentityVersions, DriveAbciQuerySystemVersions, DriveAbciQueryVersions, + DriveAbciPlatformStateStorageMethodVersions, DriveAbciProtocolUpgradeMethodVersions, + DriveAbciQueryDataContractVersions, DriveAbciQueryIdentityVersions, + DriveAbciQuerySystemVersions, DriveAbciQueryVersions, DriveAbciStateTransitionCommonValidationVersions, DriveAbciStateTransitionProcessingMethodVersions, DriveAbciStateTransitionValidationVersion, DriveAbciStateTransitionValidationVersions, DriveAbciStructureVersions, @@ -51,13 +51,13 @@ use crate::version::drive_versions::{ DriveIdentityKeysProveMethodVersions, DriveIdentityMethodVersions, DriveIdentityProveMethodVersions, DriveIdentityUpdateMethodVersions, DriveInitializationMethodVersions, DriveMethodVersions, DriveOperationsMethodVersion, - DrivePlatformSystemMethodVersions, DriveProtocolUpgradeVersions, DriveProveMethodVersions, - DriveStateTransitionMethodVersions, DriveStateTransitionOperationMethodVersions, - DriveStructureVersion, DriveSystemEstimationCostsMethodVersions, - DriveSystemProtocolVersionMethodVersions, DriveVerifyContractMethodVersions, - DriveVerifyDocumentMethodVersions, DriveVerifyIdentityMethodVersions, - DriveVerifyMethodVersions, DriveVerifySingleDocumentMethodVersions, - DriveVerifySystemMethodVersions, DriveVersion, + DrivePlatformStateMethodVersions, DrivePlatformSystemMethodVersions, + DriveProtocolUpgradeVersions, DriveProveMethodVersions, DriveStateTransitionMethodVersions, + DriveStateTransitionOperationMethodVersions, DriveStructureVersion, + DriveSystemEstimationCostsMethodVersions, DriveSystemProtocolVersionMethodVersions, + DriveVerifyContractMethodVersions, DriveVerifyDocumentMethodVersions, + DriveVerifyIdentityMethodVersions, DriveVerifyMethodVersions, + DriveVerifySingleDocumentMethodVersions, DriveVerifySystemMethodVersions, DriveVersion, }; use crate::version::mocks::TEST_BYTES; use crate::version::protocol_version::{FeatureVersionBounds, PlatformVersion}; @@ -390,6 +390,10 @@ pub(crate) const TEST_PLATFORM_V3: PlatformVersion = PlatformVersion { convert_drive_operations_to_grove_operations: 0, apply_drive_operations: 0, }, + platform_state: DrivePlatformStateMethodVersions { + fetch_platform_state_bytes: 0, + store_platform_state_bytes: 0, + }, }, grove_methods: DriveGroveMethodVersions { basic: DriveGroveBasicMethodVersions { @@ -534,9 +538,9 @@ pub(crate) const TEST_PLATFORM_V3: PlatformVersion = PlatformVersion { update_drive_cache: 0, validator_set_update: 0, }, - execution_state_storage: DriveAbciExecutionStateStorageMethodVersions { - fetch_execution_state: 0, - store_execution_state: 0, + platform_state_storage: DriveAbciPlatformStateStorageMethodVersions { + fetch_platform_state: 0, + store_platform_state: 0, }, }, validation_and_processing: DriveAbciValidationVersions { diff --git a/packages/rs-platform-version/src/version/v1.rs b/packages/rs-platform-version/src/version/v1.rs index fcb76a8c5f4..02a0349663d 100644 --- a/packages/rs-platform-version/src/version/v1.rs +++ b/packages/rs-platform-version/src/version/v1.rs @@ -14,13 +14,13 @@ use crate::version::drive_abci_versions::{ DriveAbciBlockFeeProcessingMethodVersions, DriveAbciBlockStartMethodVersions, DriveAbciCoreBasedUpdatesMethodVersions, DriveAbciCoreSubsidyMethodVersions, DriveAbciDocumentsStateTransitionValidationVersions, DriveAbciEngineMethodVersions, - DriveAbciEpochMethodVersions, DriveAbciExecutionStateStorageMethodVersions, - DriveAbciFeePoolInwardsDistributionMethodVersions, + DriveAbciEpochMethodVersions, DriveAbciFeePoolInwardsDistributionMethodVersions, DriveAbciFeePoolOutwardsDistributionMethodVersions, DriveAbciIdentityCreditWithdrawalMethodVersions, DriveAbciInitializationMethodVersions, DriveAbciMasternodeIdentitiesUpdatesMethodVersions, DriveAbciMethodVersions, - DriveAbciProtocolUpgradeMethodVersions, DriveAbciQueryDataContractVersions, - DriveAbciQueryIdentityVersions, DriveAbciQuerySystemVersions, DriveAbciQueryVersions, + DriveAbciPlatformStateStorageMethodVersions, DriveAbciProtocolUpgradeMethodVersions, + DriveAbciQueryDataContractVersions, DriveAbciQueryIdentityVersions, + DriveAbciQuerySystemVersions, DriveAbciQueryVersions, DriveAbciStateTransitionCommonValidationVersions, DriveAbciStateTransitionProcessingMethodVersions, DriveAbciStateTransitionValidationVersion, DriveAbciStateTransitionValidationVersions, DriveAbciStructureVersions, @@ -51,13 +51,13 @@ use crate::version::drive_versions::{ DriveIdentityKeysProveMethodVersions, DriveIdentityMethodVersions, DriveIdentityProveMethodVersions, DriveIdentityUpdateMethodVersions, DriveInitializationMethodVersions, DriveMethodVersions, DriveOperationsMethodVersion, - DrivePlatformSystemMethodVersions, DriveProtocolUpgradeVersions, DriveProveMethodVersions, - DriveStateTransitionMethodVersions, DriveStateTransitionOperationMethodVersions, - DriveStructureVersion, DriveSystemEstimationCostsMethodVersions, - DriveSystemProtocolVersionMethodVersions, DriveVerifyContractMethodVersions, - DriveVerifyDocumentMethodVersions, DriveVerifyIdentityMethodVersions, - DriveVerifyMethodVersions, DriveVerifySingleDocumentMethodVersions, - DriveVerifySystemMethodVersions, DriveVersion, + DrivePlatformStateMethodVersions, DrivePlatformSystemMethodVersions, + DriveProtocolUpgradeVersions, DriveProveMethodVersions, DriveStateTransitionMethodVersions, + DriveStateTransitionOperationMethodVersions, DriveStructureVersion, + DriveSystemEstimationCostsMethodVersions, DriveSystemProtocolVersionMethodVersions, + DriveVerifyContractMethodVersions, DriveVerifyDocumentMethodVersions, + DriveVerifyIdentityMethodVersions, DriveVerifyMethodVersions, + DriveVerifySingleDocumentMethodVersions, DriveVerifySystemMethodVersions, DriveVersion, }; use crate::version::protocol_version::{FeatureVersionBounds, PlatformVersion}; use crate::version::{AbciStructureVersion, PlatformArchitectureVersion}; @@ -387,6 +387,10 @@ pub(super) const PLATFORM_V1: PlatformVersion = PlatformVersion { convert_drive_operations_to_grove_operations: 0, apply_drive_operations: 0, }, + platform_state: DrivePlatformStateMethodVersions { + fetch_platform_state_bytes: 0, + store_platform_state_bytes: 0, + }, }, grove_methods: DriveGroveMethodVersions { basic: DriveGroveBasicMethodVersions { @@ -531,9 +535,9 @@ pub(super) const PLATFORM_V1: PlatformVersion = PlatformVersion { update_drive_cache: 0, validator_set_update: 0, }, - execution_state_storage: DriveAbciExecutionStateStorageMethodVersions { - fetch_execution_state: 0, - store_execution_state: 0, + platform_state_storage: DriveAbciPlatformStateStorageMethodVersions { + fetch_platform_state: 0, + store_platform_state: 0, }, }, validation_and_processing: DriveAbciValidationVersions { From 2059f6d9bb29d2ea52d79e89f124a606d4b3ce70 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 22 Jan 2024 21:27:12 +0700 Subject: [PATCH 15/24] revert: execution -> cache --- packages/rs-drive-abci/src/error/mod.rs | 1 - .../platform_events/block_end/mod.rs | 2 +- .../block_end/update_state_cache/mod.rs | 2 +- .../block_end/update_state_cache/v0/mod.rs | 20 ++++++++++--------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/rs-drive-abci/src/error/mod.rs b/packages/rs-drive-abci/src/error/mod.rs index 3cee5d1c64e..3658a8169dc 100644 --- a/packages/rs-drive-abci/src/error/mod.rs +++ b/packages/rs-drive-abci/src/error/mod.rs @@ -7,7 +7,6 @@ use dpp::platform_value::Error as ValueError; use dpp::version::PlatformVersionError; use drive::dpp::ProtocolError; use drive::error::Error as DriveError; -use drive::grovedb; use tenderdash_abci::proto::abci::ResponseException; use tracing::error; diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs index 1ab5c69cbc4..f881cea1dd8 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/mod.rs @@ -1,4 +1,4 @@ -/// Updating the execution state happens as the final part of block finalization +/// Updating the state cache happens as the final part of block finalization pub(in crate::execution) mod update_state_cache; /// Validator set update pub(in crate::execution) mod validator_set_update; diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs index 120c25326b8..a055056edc9 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/mod.rs @@ -14,7 +14,7 @@ impl Platform where C: CoreRPCLike, { - /// Updates the execution state at the end of finalize block. This is done by overriding the current + /// Updates the state cache at the end of finalize block. This is done by overriding the current /// platform state cache with the block execution state cache. /// /// This function is a version handler that directs to specific version implementations diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs index 69f12144d96..115f360179d 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs @@ -12,7 +12,7 @@ impl Platform where C: CoreRPCLike, { - /// Updates the execution state at the end of finalize block. This is done by overriding the current + /// Updates the state cache at the end of finalize block. This is done by overriding the current /// platform state cache with the block execution state cache. /// /// This function takes an `ExtendedBlockInfo` and a `Transaction` as input and updates the @@ -46,25 +46,27 @@ where ExecutionError::CorruptedCodeExecution("there should be a block execution context"), ))?; - let mut state = self.state.write().unwrap(); + let mut state_cache = self.state.write().unwrap(); - *state = block_execution_context.block_platform_state_owned(); + *state_cache = block_execution_context.block_platform_state_owned(); - if let Some(next_validator_set_quorum_hash) = state.take_next_validator_set_quorum_hash() { - state.set_current_validator_set_quorum_hash(next_validator_set_quorum_hash); + if let Some(next_validator_set_quorum_hash) = + state_cache.take_next_validator_set_quorum_hash() + { + state_cache.set_current_validator_set_quorum_hash(next_validator_set_quorum_hash); } - state.set_last_committed_block_info(Some(extended_block_info)); + state_cache.set_last_committed_block_info(Some(extended_block_info)); - state.set_genesis_block_info(None); + state_cache.set_genesis_block_info(None); //todo: verify this with an update let version = PlatformVersion::get(platform_version.protocol_version)?; PlatformVersion::set_current(version); - // Persist execution state - self.store_platform_state(&state, Some(transaction), platform_version)?; + // Persist state cache + self.store_platform_state(&state_cache, Some(transaction), platform_version)?; Ok(()) } From 474563d63db604c80866f532e37ac24e32cdeb0c Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Tue, 23 Jan 2024 20:33:26 +0700 Subject: [PATCH 16/24] chore: set initial protocol version on init chain --- .../engine/initialization/init_chain/v0/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs b/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs index 7a585aa5622..7ab7c384f67 100644 --- a/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs @@ -79,6 +79,24 @@ where state_guard.set_genesis_block_info(Some(genesis_block_info)); + // Store initial protocol version + let mut batch_operations = vec![]; + + self.drive.set_current_protocol_version_operations( + state_guard.current_protocol_version_in_consensus(), + Some(transaction), + &mut batch_operations, + &platform_version.drive, + )?; + + self.drive.apply_batch_low_level_drive_operations( + None, + Some(transaction), + batch_operations, + &mut vec![], + &platform_version.drive, + )?; + if tracing::enabled!(tracing::Level::TRACE) { tracing::trace!( platform_state_fingerprint = hex::encode(state_guard.fingerprint()), From a920e33e9b32eb0dd8bd0c8f6c4ba5ffa6b85b35 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Wed, 24 Jan 2024 16:46:48 +0700 Subject: [PATCH 17/24] test: fix tests --- packages/rs-drive-abci/tests/strategy_tests/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rs-drive-abci/tests/strategy_tests/main.rs b/packages/rs-drive-abci/tests/strategy_tests/main.rs index bfa49e245bc..3e4070df124 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/main.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/main.rs @@ -573,7 +573,7 @@ mod tests { .expect("expected to fetch balances") .expect("expected to have an identity to get balance from"); - assert_eq!(balance, 99869074420) + assert_eq!(balance, 99869044820) } #[test] @@ -1228,7 +1228,7 @@ mod tests { .unwrap() .unwrap() ), - "7185a9b987f4fe7290f048ccdb2935d92446c240b9361be46a20f956164a9378".to_string() + "31479dd805715e196773106ae8f00e1f28f8e5d4f8e3cbf21e6dd59f94643207".to_string() ) } @@ -1838,7 +1838,7 @@ mod tests { .unwrap() .unwrap() ), - "5dfc31d164388c22154e10629030edb5557620c5fcd5c87ffeff5f4e81bdb657".to_string() + "1a1b119866b5cdff20d2fa04f3d2ff8e8d7349aa4b83e7ea05adeae0cfc9cb2c".to_string() ) } From e625f71e13c2f547b9272c075114c9db38644de0 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 25 Jan 2024 21:18:47 +0700 Subject: [PATCH 18/24] chore: introduce store_current_protocol_version --- .../initialization/init_chain/v0/mod.rs | 13 +------- .../src/drive/system/protocol_version/mod.rs | 4 +-- ...s.rs => store_current_protocol_version.rs} | 32 +++++++++++++++++-- 3 files changed, 33 insertions(+), 16 deletions(-) rename packages/rs-drive/src/drive/system/protocol_version/{set_current_protocol_version_operations.rs => store_current_protocol_version.rs} (68%) diff --git a/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs b/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs index 7ab7c384f67..40b9cf4f8ad 100644 --- a/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs @@ -80,20 +80,9 @@ where state_guard.set_genesis_block_info(Some(genesis_block_info)); // Store initial protocol version - let mut batch_operations = vec![]; - - self.drive.set_current_protocol_version_operations( + self.drive.store_current_protocol_version( state_guard.current_protocol_version_in_consensus(), Some(transaction), - &mut batch_operations, - &platform_version.drive, - )?; - - self.drive.apply_batch_low_level_drive_operations( - None, - Some(transaction), - batch_operations, - &mut vec![], &platform_version.drive, )?; diff --git a/packages/rs-drive/src/drive/system/protocol_version/mod.rs b/packages/rs-drive/src/drive/system/protocol_version/mod.rs index 2f93b44a5c0..d561094fc84 100644 --- a/packages/rs-drive/src/drive/system/protocol_version/mod.rs +++ b/packages/rs-drive/src/drive/system/protocol_version/mod.rs @@ -2,7 +2,7 @@ mod fetch_current_protocol_version; pub use fetch_current_protocol_version::*; mod fetch_next_protocol_version; pub use fetch_next_protocol_version::*; -mod set_current_protocol_version_operations; -pub use set_current_protocol_version_operations::*; +mod store_current_protocol_version; +pub use store_current_protocol_version::*; mod set_next_protocol_version_operations; pub use set_next_protocol_version_operations::*; diff --git a/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations.rs b/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs similarity index 68% rename from packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations.rs rename to packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs index 86913633542..e630cea3698 100644 --- a/packages/rs-drive/src/drive/system/protocol_version/set_current_protocol_version_operations.rs +++ b/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs @@ -10,9 +10,37 @@ use dpp::version::drive_versions::DriveVersion; use grovedb::{Element, TransactionArg}; use integer_encoding::VarInt; -///!!!DON'T CHANGE!!!! impl Drive { - /// Sets the current protocol version + /// Store the current protocol version in grovedb storage + /// + /// !!!DON'T CHANGE!!!! + /// This function should never be changed !!! since it must always be compatible + /// with fetch_current_protocol_version which is should never be changed. + pub fn store_current_protocol_version( + &self, + protocol_version: ProtocolVersion, + transaction: TransactionArg, + drive_version: &DriveVersion, + ) -> Result<(), Error> { + let mut batch_operations = vec![]; + + self.set_current_protocol_version_operations( + protocol_version, + transaction, + &mut batch_operations, + drive_version, + )?; + + self.apply_batch_low_level_drive_operations( + None, + transaction, + batch_operations, + &mut vec![], + drive_version, + ) + } + + /// Sets the current protocol version operations to batch /// /// !!!DON'T CHANGE!!!! /// This function should never be changed !!! since it must always be compatible From 9bc285ed306c374d7d04fe83f611078ebf76d645 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 25 Jan 2024 21:19:02 +0700 Subject: [PATCH 19/24] docs: remove unnecessary todo --- .../rs-drive-abci/src/platform_types/platform_state/v0/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs index 2870089d94e..b648fc8a986 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs @@ -99,7 +99,7 @@ pub(super) struct PlatformStateForSavingV0 { /// Information about the last block pub last_committed_block_info: Option, /// Current Version - pub current_protocol_version_in_consensus: ProtocolVersion, // TODO: Remove this + pub current_protocol_version_in_consensus: ProtocolVersion, /// upcoming protocol version pub next_epoch_protocol_version: ProtocolVersion, /// current quorum From 76e05c890af997d4cd4f948ac22b56e23626aca4 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 25 Jan 2024 21:25:18 +0700 Subject: [PATCH 20/24] revert: platform state versioned conversion --- .../src/platform_types/platform_state/mod.rs | 3 +-- .../src/platform_types/platform_state/v0/mod.rs | 17 ++++++----------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/mod.rs b/packages/rs-drive-abci/src/platform_types/platform_state/mod.rs index e8a009c9fdd..9e03b3f2b26 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/mod.rs @@ -176,8 +176,7 @@ impl TryFromPlatformVersioned for PlatformState { PlatformStateForSaving::V0(v0) => { match platform_version.drive_abci.structs.platform_state_structure { 0 => { - let platform_state_v0: PlatformStateV0 = - v0.try_into_platform_versioned(platform_version)?; + let platform_state_v0 = PlatformStateV0::from(v0); Ok(platform_state_v0.into()) } diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs index b648fc8a986..af69a73643b 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/v0/mod.rs @@ -17,7 +17,7 @@ use crate::platform_types::masternode::Masternode; use crate::platform_types::validator_set::ValidatorSet; use dpp::block::block_info::{BlockInfo, DEFAULT_BLOCK_INFO}; use dpp::block::extended_block_info::v0::ExtendedBlockInfoV0Getters; -use dpp::version::{PlatformVersion, TryFromPlatformVersioned, TryIntoPlatformVersioned}; +use dpp::version::{PlatformVersion, TryIntoPlatformVersioned}; use std::collections::BTreeMap; use std::fmt::{Debug, Formatter}; @@ -165,17 +165,12 @@ impl TryFrom for PlatformStateForSavingV0 { } } -impl TryFromPlatformVersioned for PlatformStateV0 { - type Error = Error; - - fn try_from_platform_versioned( - value: PlatformStateForSavingV0, - platform_version: &PlatformVersion, - ) -> Result { - Ok(PlatformStateV0 { +impl From for PlatformStateV0 { + fn from(value: PlatformStateForSavingV0) -> Self { + PlatformStateV0 { genesis_block_info: value.genesis_block_info, last_committed_block_info: value.last_committed_block_info, - current_protocol_version_in_consensus: platform_version.protocol_version, + current_protocol_version_in_consensus: value.current_protocol_version_in_consensus, next_epoch_protocol_version: value.next_epoch_protocol_version, current_validator_set_quorum_hash: QuorumHash::from_byte_array( value.current_validator_set_quorum_hash.to_buffer(), @@ -198,7 +193,7 @@ impl TryFromPlatformVersioned for PlatformStateV0 { .into_iter() .map(|(k, v)| (ProTxHash::from_byte_array(k.to_buffer()), v.into())) .collect(), - }) + } } } From ec22c04a2540603f95b21eef6f1fd81b7fcb483c Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 25 Jan 2024 21:56:56 +0700 Subject: [PATCH 21/24] refactor: move fetch_platform_state into Platform --- .../storage/fetch_platform_state/mod.rs | 39 ++++++++++--------- .../storage/fetch_platform_state/v0/mod.rs | 28 +++++++------ .../src/platform_types/platform/mod.rs | 7 ++-- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs index d69e13796b7..f2537eca34f 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/mod.rs @@ -1,5 +1,6 @@ use crate::error::execution::ExecutionError; use crate::error::Error; +use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use dpp::version::PlatformVersion; use drive::drive::Drive; @@ -7,23 +8,25 @@ use drive::query::TransactionArg; mod v0; -/// Fetches execution state from grovedb storage -pub fn fetch_platform_state( - drive: &Drive, - transaction: TransactionArg, - platform_version: &PlatformVersion, -) -> Result, Error> { - match platform_version - .drive_abci - .methods - .platform_state_storage - .fetch_platform_state - { - 0 => v0::fetch_platform_state_v0(drive, transaction, platform_version), - version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { - method: "fetch_platform_state".to_string(), - known_versions: vec![0], - received: version, - })), +impl Platform { + /// Fetches execution state from grovedb storage + pub fn fetch_platform_state( + drive: &Drive, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive_abci + .methods + .platform_state_storage + .fetch_platform_state + { + 0 => Platform::::fetch_platform_state_v0(drive, transaction, platform_version), + version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { + method: "fetch_platform_state".to_string(), + known_versions: vec![0], + received: version, + })), + } } } diff --git a/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/v0/mod.rs index 8fe01a58a8a..25960bf5b39 100644 --- a/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/fetch_platform_state/v0/mod.rs @@ -1,20 +1,24 @@ use crate::error::Error; +use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use dpp::serialization::PlatformDeserializableFromVersionedStructure; use dpp::version::PlatformVersion; use drive::drive::Drive; use drive::query::TransactionArg; -pub(super) fn fetch_platform_state_v0( - drive: &Drive, - transaction: TransactionArg, - platform_version: &PlatformVersion, -) -> Result, Error> { - drive - .fetch_platform_state_bytes(transaction, platform_version) - .map_err(Error::Drive)? - .map(|bytes| { - PlatformState::versioned_deserialize(&bytes, platform_version).map_err(Error::Protocol) - }) - .transpose() +impl Platform { + pub(super) fn fetch_platform_state_v0( + drive: &Drive, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + drive + .fetch_platform_state_bytes(transaction, platform_version) + .map_err(Error::Drive)? + .map(|bytes| { + PlatformState::versioned_deserialize(&bytes, platform_version) + .map_err(Error::Protocol) + }) + .transpose() + } } diff --git a/packages/rs-drive-abci/src/platform_types/platform/mod.rs b/packages/rs-drive-abci/src/platform_types/platform/mod.rs index 50eebed5a1e..619c02bd0ec 100644 --- a/packages/rs-drive-abci/src/platform_types/platform/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/platform/mod.rs @@ -14,7 +14,6 @@ use std::sync::RwLock; use dashcore_rpc::dashcore::BlockHash; -use crate::execution::storage::fetch_platform_state; use crate::execution::types::block_execution_context::BlockExecutionContext; use crate::platform_types::platform_state::v0::PlatformStateV0Methods; use crate::platform_types::platform_state::PlatformState; @@ -148,7 +147,8 @@ impl Platform { &self, platform_version: &PlatformVersion, ) -> Result { - let Some(persisted_state) = fetch_platform_state(&self.drive, None, platform_version)? + let Some(persisted_state) = + Platform::::fetch_platform_state(&self.drive, None, platform_version)? else { return Ok(false); }; @@ -181,7 +181,8 @@ impl Platform { if let Some(protocol_version) = drive.fetch_current_protocol_version(None)? { let platform_version = PlatformVersion::get(protocol_version)?; - let Some(execution_state) = fetch_platform_state(&drive, None, platform_version)? + let Some(execution_state) = + Platform::::fetch_platform_state(&drive, None, platform_version)? else { return Err(Error::Execution(ExecutionError::CorruptedCachedState( "execution state should be stored as well as protocol version", From f811f091e851ba82b61e10d8d11792c4d6a05a6d Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Fri, 26 Jan 2024 13:36:05 +0700 Subject: [PATCH 22/24] chore: fix review comments --- .../engine/initialization/init_chain/v0/mod.rs | 5 +++-- .../request_init_chain_cleaned_params/v0/mod.rs | 14 ++++++++++++++ packages/rs-drive-abci/src/test/fixture/abci.rs | 15 +++++++++++---- .../tests/strategy_tests/execution.rs | 2 +- .../fetch_platform_state_bytes/v0/mod.rs | 4 ++-- packages/rs-drive/src/drive/platform_state/mod.rs | 2 +- .../store_platform_state_bytes/v0/mod.rs | 4 ++-- .../v0/mod.rs | 1 - .../store_current_protocol_version.rs | 6 +----- 9 files changed, 35 insertions(+), 18 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs b/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs index 40b9cf4f8ad..2aa68d7f04d 100644 --- a/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/engine/initialization/init_chain/v0/mod.rs @@ -79,9 +79,10 @@ where state_guard.set_genesis_block_info(Some(genesis_block_info)); - // Store initial protocol version + state_guard.set_current_protocol_version_in_consensus(request.initial_protocol_version); + self.drive.store_current_protocol_version( - state_guard.current_protocol_version_in_consensus(), + request.initial_protocol_version, Some(transaction), &platform_version.drive, )?; diff --git a/packages/rs-drive-abci/src/platform_types/cleaned_abci_messages/request_init_chain_cleaned_params/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/cleaned_abci_messages/request_init_chain_cleaned_params/v0/mod.rs index 6f6c0a53396..b82e36261d5 100644 --- a/packages/rs-drive-abci/src/platform_types/cleaned_abci_messages/request_init_chain_cleaned_params/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/cleaned_abci_messages/request_init_chain_cleaned_params/v0/mod.rs @@ -34,6 +34,7 @@ //! use crate::abci::AbciError; +use dpp::util::deserializer::ProtocolVersion; use drive::dpp::identity::TimestampMillis; use serde::{Deserialize, Serialize}; use tenderdash_abci::proto::abci::RequestInitChain; @@ -51,6 +52,9 @@ pub struct RequestInitChainCleanedParams { /// Initial core chain lock height. pub initial_core_height: Option, + + /// Initial protocol version + pub initial_protocol_version: ProtocolVersion, } impl TryFrom for RequestInitChainCleanedParams { @@ -67,10 +71,20 @@ impl TryFrom for RequestInitChainCleanedParams { h => Some(h), }; + let consensus_params = request.consensus_params.ok_or(AbciError::BadRequest( + "consensus params are required in init chain".to_string(), + ))?; + + let tenderdash_abci::proto::types::VersionParams { app_version } = + consensus_params.version.ok_or(AbciError::BadRequest( + "consensus params version is required in init chain".to_string(), + ))?; + Ok(Self { genesis_time, initial_height: request.initial_height as u64, initial_core_height, + initial_protocol_version: app_version as ProtocolVersion, }) } } diff --git a/packages/rs-drive-abci/src/test/fixture/abci.rs b/packages/rs-drive-abci/src/test/fixture/abci.rs index afe4f6fb0c6..0330ac740cc 100644 --- a/packages/rs-drive-abci/src/test/fixture/abci.rs +++ b/packages/rs-drive-abci/src/test/fixture/abci.rs @@ -30,6 +30,7 @@ //! Execution Tests //! +use crate::config::PlatformConfig; use crate::platform_types::required_identity_public_key_set::v0::RequiredIdentityPublicKeysSet; use crate::platform_types::system_identity_public_keys::v0::SystemIdentityPublicKeysV0; use dpp::version::PlatformVersion; @@ -38,20 +39,26 @@ use rand::rngs::StdRng; use rand::SeedableRng; use tenderdash_abci::proto::abci::RequestInitChain; use tenderdash_abci::proto::google::protobuf::Timestamp; +use tenderdash_abci::proto::types::{ConsensusParams, VersionParams}; /// Creates static init chain request fixture -pub fn static_init_chain_request() -> RequestInitChain { +pub fn static_init_chain_request(config: &PlatformConfig) -> RequestInitChain { RequestInitChain { time: Some(Timestamp { seconds: 0, nanos: 0, }), chain_id: "strategy_tests".to_string(), - consensus_params: None, + consensus_params: Some(ConsensusParams { + version: Some(VersionParams { + app_version: config.initial_protocol_version as u64, + }), + ..Default::default() + }), validator_set: None, app_state_bytes: [0u8; 32].to_vec(), - initial_height: 0, - initial_core_height: 1, + initial_height: config.abci.genesis_height as i64, + initial_core_height: config.abci.genesis_core_height, } } diff --git a/packages/rs-drive-abci/tests/strategy_tests/execution.rs b/packages/rs-drive-abci/tests/strategy_tests/execution.rs index 313f68eb506..c51a376673f 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/execution.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/execution.rs @@ -530,7 +530,7 @@ pub(crate) fn start_chain_for_strategy( .expect("expected a quorum to be found"); // init chain - let mut init_chain_request = static_init_chain_request(); + let mut init_chain_request = static_init_chain_request(&config); init_chain_request.initial_core_height = config.abci.genesis_core_height; init_chain_request.validator_set = Some(ValidatorSetUpdate { diff --git a/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs index b53ef12082a..a1151fe8543 100644 --- a/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs @@ -1,4 +1,4 @@ -use crate::drive::platform_state::EXECUTION_STORAGE_STATE_KEY; +use crate::drive::platform_state::PLATFROM_STATE_KEY; use crate::drive::Drive; use crate::error::Error; use grovedb::TransactionArg; @@ -9,7 +9,7 @@ impl Drive { transaction: TransactionArg, ) -> Result>, Error> { self.grove - .get_aux(EXECUTION_STORAGE_STATE_KEY, transaction) + .get_aux(PLATFROM_STATE_KEY, transaction) .unwrap() .map_err(Error::GroveDB) } diff --git a/packages/rs-drive/src/drive/platform_state/mod.rs b/packages/rs-drive/src/drive/platform_state/mod.rs index 2cc7cccce96..4b56913b998 100644 --- a/packages/rs-drive/src/drive/platform_state/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/mod.rs @@ -4,4 +4,4 @@ mod store_platform_state_bytes; pub use fetch_platform_state_bytes::*; pub use store_platform_state_bytes::*; -const EXECUTION_STORAGE_STATE_KEY: &[u8; 11] = b"saved_state"; +const PLATFROM_STATE_KEY: &[u8; 11] = b"saved_state"; diff --git a/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs index 326523f590b..bebe0a24fc9 100644 --- a/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs @@ -1,4 +1,4 @@ -use crate::drive::platform_state::EXECUTION_STORAGE_STATE_KEY; +use crate::drive::platform_state::PLATFROM_STATE_KEY; use crate::drive::Drive; use crate::error::Error; use dpp::serialization::PlatformSerializable; @@ -11,7 +11,7 @@ impl Drive { transaction: TransactionArg, ) -> Result<(), Error> { self.grove - .put_aux(EXECUTION_STORAGE_STATE_KEY, state_bytes, None, transaction) + .put_aux(PLATFROM_STATE_KEY, state_bytes, None, transaction) .unwrap() .map_err(Error::GroveDB) } diff --git a/packages/rs-drive/src/drive/protocol_upgrade/change_to_new_version_and_clear_version_information/v0/mod.rs b/packages/rs-drive/src/drive/protocol_upgrade/change_to_new_version_and_clear_version_information/v0/mod.rs index 42fbb2a643b..fd37f923f70 100644 --- a/packages/rs-drive/src/drive/protocol_upgrade/change_to_new_version_and_clear_version_information/v0/mod.rs +++ b/packages/rs-drive/src/drive/protocol_upgrade/change_to_new_version_and_clear_version_information/v0/mod.rs @@ -27,7 +27,6 @@ impl Drive { self.set_current_protocol_version_operations( current_version, - transaction, &mut batch_operations, &platform_version.drive, )?; diff --git a/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs b/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs index e630cea3698..c11e27c4549 100644 --- a/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs +++ b/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs @@ -26,7 +26,6 @@ impl Drive { self.set_current_protocol_version_operations( protocol_version, - transaction, &mut batch_operations, drive_version, )?; @@ -63,18 +62,15 @@ impl Drive { pub fn set_current_protocol_version_operations( &self, protocol_version: ProtocolVersion, - transaction: TransactionArg, drive_operations: &mut Vec, drive_version: &DriveVersion, ) -> Result<(), Error> { - self.batch_insert_if_changed_value( + self.batch_insert( PathKeyElementInfo::PathFixedSizeKeyRefElement(( misc_path(), PROTOCOL_VERSION_STORAGE_KEY, Element::new_item(protocol_version.encode_var_vec()), )), - BatchInsertApplyType::StatefulBatchInsert, - transaction, drive_operations, drive_version, )?; From 7eefb1c850b05dacf97b69f7ff8f51228f1f24f5 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Fri, 26 Jan 2024 14:35:18 +0700 Subject: [PATCH 23/24] refactor: remove extra return --- .../system/protocol_version/store_current_protocol_version.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs b/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs index c11e27c4549..bb954764120 100644 --- a/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs +++ b/packages/rs-drive/src/drive/system/protocol_version/store_current_protocol_version.rs @@ -73,7 +73,6 @@ impl Drive { )), drive_operations, drive_version, - )?; - Ok(()) + ) } } From 7eabbce420630397af6fcab69e539294df812754 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Fri, 26 Jan 2024 14:35:42 +0700 Subject: [PATCH 24/24] chore: typo --- .../platform_state/fetch_platform_state_bytes/v0/mod.rs | 4 ++-- packages/rs-drive/src/drive/platform_state/mod.rs | 2 +- .../platform_state/store_platform_state_bytes/v0/mod.rs | 5 ++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs index a1151fe8543..7a0bf1f18c2 100644 --- a/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/fetch_platform_state_bytes/v0/mod.rs @@ -1,4 +1,4 @@ -use crate::drive::platform_state::PLATFROM_STATE_KEY; +use crate::drive::platform_state::PLATFORM_STATE_KEY; use crate::drive::Drive; use crate::error::Error; use grovedb::TransactionArg; @@ -9,7 +9,7 @@ impl Drive { transaction: TransactionArg, ) -> Result>, Error> { self.grove - .get_aux(PLATFROM_STATE_KEY, transaction) + .get_aux(PLATFORM_STATE_KEY, transaction) .unwrap() .map_err(Error::GroveDB) } diff --git a/packages/rs-drive/src/drive/platform_state/mod.rs b/packages/rs-drive/src/drive/platform_state/mod.rs index 4b56913b998..eb34a17d339 100644 --- a/packages/rs-drive/src/drive/platform_state/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/mod.rs @@ -4,4 +4,4 @@ mod store_platform_state_bytes; pub use fetch_platform_state_bytes::*; pub use store_platform_state_bytes::*; -const PLATFROM_STATE_KEY: &[u8; 11] = b"saved_state"; +const PLATFORM_STATE_KEY: &[u8; 11] = b"saved_state"; diff --git a/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs index bebe0a24fc9..57401f43a61 100644 --- a/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs +++ b/packages/rs-drive/src/drive/platform_state/store_platform_state_bytes/v0/mod.rs @@ -1,7 +1,6 @@ -use crate::drive::platform_state::PLATFROM_STATE_KEY; +use crate::drive::platform_state::PLATFORM_STATE_KEY; use crate::drive::Drive; use crate::error::Error; -use dpp::serialization::PlatformSerializable; use grovedb::TransactionArg; impl Drive { @@ -11,7 +10,7 @@ impl Drive { transaction: TransactionArg, ) -> Result<(), Error> { self.grove - .put_aux(PLATFROM_STATE_KEY, state_bytes, None, transaction) + .put_aux(PLATFORM_STATE_KEY, state_bytes, None, transaction) .unwrap() .map_err(Error::GroveDB) }