Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,19 @@ impl<C> Platform<C> {
// 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,
)?;
Expand All @@ -754,10 +760,11 @@ impl<C> Platform<C> {
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,
)?;
Expand Down Expand Up @@ -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<MockCoreRPCLike>,
transaction: Option<&Transaction>,
path: &[Vec<u8>],
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();
Expand Down
18 changes: 9 additions & 9 deletions packages/rs-drive/grovedb-structure.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions packages/rs-drive/src/drive/contract/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
4 changes: 1 addition & 3 deletions packages/rs-drive/src/drive/document/structure.rs
Original file line number Diff line number Diff line change
@@ -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.";
Expand Down
Loading