diff --git a/packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v0/mod.rs index 494d97af704..1ee22fbcb2e 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v0/mod.rs @@ -736,13 +736,19 @@ impl Platform { // App-connect contract: the wallet's encrypted login response gets one system // contract id on every network from this version. Fresh // chains register it at genesis (`create_genesis_state` v2). + // + // Both contracts registered here are stored without storage flags, as genesis stores + // system contracts: nobody owns their storage, and nothing ever refunds it. + // `insert_contract`, which the upgrades to 6, 9 and 13 used, would give them and every + // tree created with them the contract's flags, owned by the all-zero system owner. let app_connect_contract = load_system_data_contract(SystemDataContract::AppConnect, platform_version)?; - self.drive.insert_contract( + self.drive.apply_contract( &app_connect_contract, *block_info, true, + None, Some(transaction), platform_version, )?; @@ -754,10 +760,11 @@ impl Platform { let moderation_charters_contract = load_system_data_contract(SystemDataContract::ModerationCharters, platform_version)?; - self.drive.insert_contract( + self.drive.apply_contract( &moderation_charters_contract, *block_info, true, + None, Some(transaction), platform_version, )?; @@ -2123,6 +2130,87 @@ mod tests { ); } + /// The system contracts the upgrade to 14 registers are stored without storage flags, as a + /// chain born at 14 stores them at genesis. The contract elements, every tree created with + /// them and, for the moderation charters contract's contested index, its trees under the + /// active polls are byte-identical on the two chains. + #[test] + fn should_store_the_version_14_system_contracts_as_a_chain_born_at_14_does() { + use drive::drive::votes::paths::vote_contested_resource_active_polls_tree_path_vec; + use drive::util::grove_operations::DirectQueryType; + + let platform_version = PlatformVersion::latest(); + let born_at_14 = TestPlatformBuilder::new() + .with_initial_protocol_version(14) + .build_with_mock_rpc() + .set_genesis_state(); + let upgraded = TestPlatformBuilder::new() + .with_initial_protocol_version(13) + .build_with_mock_rpc() + .set_genesis_state(); + + let transaction = upgraded.drive.grove.start_transaction(); + let block_info = BlockInfo { + time_ms: 1_000_000, + height: 100, + core_height: 100, + epoch: Epoch::new(1).expect("expected epoch"), + }; + upgraded + .transition_to_version_14(&block_info, &transaction, platform_version) + .expect("expected version 14 transition to succeed"); + + let element = |platform: &crate::platform_types::platform::Platform, + transaction: Option<&Transaction>, + path: &[Vec], + key: &[u8]| { + platform + .drive + .grove_get_raw( + path.into(), + key, + DirectQueryType::StatefulDirectQuery, + transaction, + &mut vec![], + &platform_version.drive, + ) + .expect("expected to read the element") + .expect("expected the element to exist") + }; + + let contracts = vec![vec![RootTree::DataContractDocuments as u8]]; + let active_polls = vote_contested_resource_active_polls_tree_path_vec(); + for (parent, contract) in [ + (&contracts, SystemDataContract::AppConnect), + (&contracts, SystemDataContract::ModerationCharters), + (&active_polls, SystemDataContract::ModerationCharters), + ] { + let id = contract.id().to_buffer(); + let upgraded_element = element(&upgraded, Some(&transaction), parent, &id); + assert_eq!( + upgraded_element.get_flags(), + &None, + "{contract:?} is stored without storage flags under {parent:?}" + ); + assert_eq!( + element(&born_at_14, None, parent, &id), + upgraded_element, + "{contract:?} under {parent:?} differs between a chain born at version 14 and \ + one upgraded to it" + ); + + let mut root_path = parent.clone(); + root_path.push(id.to_vec()); + let diffs = collect_subtree_diffs(&born_at_14, &upgraded, &transaction, root_path); + assert!( + diffs.is_empty(), + "the trees of {contract:?} under {parent:?} differ between a chain born at \ + version 14 and one upgraded to it:\n{}", + diffs.join("\n"), + ); + } + } + #[test] fn test_transition_to_version_14_creates_total_credits_history_tree() { let platform_version = PlatformVersion::latest(); diff --git a/packages/rs-drive/grovedb-structure.json b/packages/rs-drive/grovedb-structure.json index 8a074d22cd9..a2845ab71a4 100644 --- a/packages/rs-drive/grovedb-structure.json +++ b/packages/rs-drive/grovedb-structure.json @@ -2537,7 +2537,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner, and the epoch the one the contract was created in. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "since": 1, "presence": "always", "source": "packages/rs-drive/src/drive/contract/paths.rs", @@ -2560,7 +2560,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner, and the epoch the one the contract was created in. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "value": "serialized DataContract", "since": 1, "presence": "always", @@ -2624,7 +2624,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner, and the epoch the one the contract was created in. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "since": 1, "presence": "always", "source": "packages/rs-drive/src/drive/contract/paths.rs", @@ -2649,7 +2649,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "since": 1, "presence": "always", "source": "packages/rs-drive/src/drive/document/paths.rs", @@ -2678,7 +2678,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "since": 1, "presence": "lazy", "source": "packages/rs-drive/src/drive/document/primary_key_tree_type.rs", @@ -2947,7 +2947,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner, and the epoch the one the contract was created in. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "since": 14, "presence": "always", "source": "packages/rs-drive/src/drive/contract/paths.rs", @@ -2969,7 +2969,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner, and the epoch the one the contract was created in. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "since": 14, "presence": "lazy", "source": "packages/rs-drive/src/drive/contract/paths.rs", @@ -2993,7 +2993,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner, and the epoch the one the contract was created in. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "since": 14, "presence": "always", "source": "packages/rs-drive/src/drive/contract/paths.rs", @@ -3062,7 +3062,7 @@ "EpochOwned", "None" ], - "flags_note": "The owner is the contract owner, and the epoch the one the contract was created in. System contracts created at genesis carry no flags.", + "flags_note": "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 and 13 registered carry them (wallet utils, token history, keyword search and document history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state transitions and later upgrades write none.", "value": "u32 big endian", "since": 14, "presence": "always", diff --git a/packages/rs-drive/src/drive/contract/structure.rs b/packages/rs-drive/src/drive/contract/structure.rs index c44b368aba9..32626ba4da1 100644 --- a/packages/rs-drive/src/drive/contract/structure.rs +++ b/packages/rs-drive/src/drive/contract/structure.rs @@ -8,9 +8,12 @@ use crate::drive::RootTree; use crate::structure::{ElementKind, FlagsKind, KeyEncoding, KeyMatcher, StructureNode}; const SOURCE: &str = "packages/rs-drive/src/drive/contract/paths.rs"; -const CONTRACT_FLAGS: &str = - "The owner is the contract owner, and the epoch the one the contract was \ - created in. System contracts created at genesis carry no flags."; +/// The flags of the elements written with a contract, shared by every area that describes one +pub(crate) const CONTRACT_FLAGS: &str = + "The contract's flags. Only the system contracts the upgrades to protocol versions 6, 9 \ + and 13 registered carry them (wallet utils, token history, keyword search and document \ + history), owned by the all-zero system owner in the epoch of the upgrade. Genesis, state \ + transitions and later upgrades write none."; const REMOVAL_FLAGS: &str = "The owner is the moderator who deleted the document. They pay for the record, \ which nothing deletes or replaces."; diff --git a/packages/rs-drive/src/drive/document/structure.rs b/packages/rs-drive/src/drive/document/structure.rs index 4aa76a28a47..75dd4d23a8a 100644 --- a/packages/rs-drive/src/drive/document/structure.rs +++ b/packages/rs-drive/src/drive/document/structure.rs @@ -1,9 +1,7 @@ +use crate::drive::contract::structure::CONTRACT_FLAGS; use crate::structure::{ElementKind, FlagsKind, KeyEncoding, KeyMatcher, StructureNode}; const SOURCE: &str = "packages/rs-drive/src/drive/document/paths.rs"; -const CONTRACT_FLAGS: &str = - "The owner is the contract owner. System contracts created at genesis carry \ - no flags."; const DOCUMENT_FLAGS: &str = "The owner is the owner of the document, who is refunded when it is deleted. \ Documents the system writes carry no flags.";