drive/util/test_helpers/
setup.rs

1//! Drive Setup Helpers.
2//!
3//! Defines helper functions pertinent to setting up Drive.
4//!
5
6#[cfg(feature = "full")]
7use crate::config::DriveConfig;
8use crate::drive::Drive;
9use dpp::block::block_info::BlockInfo;
10
11use crate::util::object_size_info::DocumentInfo::DocumentRefInfo;
12use crate::util::object_size_info::{DocumentAndContractInfo, OwnedDocumentInfo};
13use dpp::data_contract::document_type::DocumentTypeRef;
14use dpp::data_contract::DataContract;
15use dpp::document::Document;
16
17use dpp::version::PlatformVersion;
18use grovedb::TransactionArg;
19#[cfg(feature = "full")]
20use tempfile::TempDir;
21
22/// Struct with options regarding setting up fee pools.
23pub struct SetupFeePoolsOptions {
24    /// Bool indicating whether the fee pool structure should be applied upon setup.
25    pub apply_fee_pool_structure: bool,
26}
27
28impl Default for SetupFeePoolsOptions {
29    /// The default is true for applying the fee pool structure upon setting up fee pools.
30    fn default() -> SetupFeePoolsOptions {
31        SetupFeePoolsOptions {
32            apply_fee_pool_structure: true,
33        }
34    }
35}
36
37#[cfg(feature = "full")]
38/// Sets up Drive using a temporary directory and the optionally given Drive configuration settings.
39pub fn setup_drive(drive_config: Option<DriveConfig>) -> Drive {
40    let tmp_dir = TempDir::new().unwrap();
41
42    let (drive, _) = Drive::open(tmp_dir, drive_config).expect("should open Drive successfully");
43
44    drive
45}
46
47#[cfg(feature = "full")]
48/// Sets up Drive using a temporary directory and the default initial state structure.
49pub fn setup_drive_with_initial_state_structure(
50    specific_platform_version: Option<&PlatformVersion>,
51) -> Drive {
52    let drive = setup_drive(Some(DriveConfig {
53        batching_consistency_verification: true,
54        ..Default::default()
55    }));
56
57    let platform_version = specific_platform_version.unwrap_or(PlatformVersion::latest());
58    drive
59        .create_initial_state_structure(None, platform_version)
60        .expect("should create root tree successfully");
61
62    drive
63}
64
65/// A function to setup system data contract
66pub fn setup_system_data_contract(
67    drive: &Drive,
68    data_contract: &DataContract,
69    transaction: TransactionArg,
70) {
71    let platform_version = PlatformVersion::latest();
72    drive
73        .apply_contract(
74            data_contract,
75            BlockInfo::default(),
76            true,
77            None,
78            transaction,
79            platform_version,
80        )
81        .unwrap();
82}
83
84/// Setup document for a contract
85pub fn setup_document(
86    drive: &Drive,
87    document: &Document,
88    data_contract: &DataContract,
89    document_type: DocumentTypeRef,
90    transaction: TransactionArg,
91) {
92    let platform_version = PlatformVersion::latest();
93    drive
94        .add_document_for_contract(
95            DocumentAndContractInfo {
96                owned_document_info: OwnedDocumentInfo {
97                    document_info: DocumentRefInfo((document, None)),
98                    owner_id: None,
99                },
100                contract: data_contract,
101                document_type,
102            },
103            false,
104            BlockInfo::default(),
105            true,
106            transaction,
107            platform_version,
108            None,
109        )
110        .unwrap();
111}