drive/cache/
system_contracts.rs1use crate::error::Error;
2use arc_swap::{ArcSwap, Guard};
3use dpp::data_contract::DataContract;
4use dpp::system_data_contracts::{load_system_data_contract, SystemDataContract};
5use platform_version::version::{PlatformVersion, ProtocolVersion};
6use std::sync::Arc;
7
8pub struct ActiveSystemDataContract {
14 pub contract: ArcSwap<DataContract>,
16
17 #[allow(unused)]
19 pub active_since_protocol_version: ProtocolVersion,
20}
21
22impl ActiveSystemDataContract {
23 pub fn store(&self, contract: DataContract) {
29 self.contract.store(Arc::new(contract));
30 }
31
32 pub fn load(&self) -> Guard<Arc<DataContract>> {
37 self.contract.load()
38 }
39
40 pub fn new(contract: DataContract, active_since_protocol_version: ProtocolVersion) -> Self {
47 ActiveSystemDataContract {
48 contract: ArcSwap::from_pointee(contract),
49 active_since_protocol_version,
50 }
51 }
52}
53
54pub struct SystemDataContracts {
56 withdrawals: ActiveSystemDataContract,
58 dpns: ActiveSystemDataContract,
60 dashpay: ActiveSystemDataContract,
62 masternode_reward_shares: ActiveSystemDataContract,
64 token_history: ActiveSystemDataContract,
66 keyword_search: ActiveSystemDataContract,
68}
69
70impl SystemDataContracts {
71 pub fn reload_system_contracts(&self, platform_version: &PlatformVersion) -> Result<(), Error> {
80 use SystemDataContract::*;
81
82 let withdrawals = load_system_data_contract(Withdrawals, platform_version)?;
84 let dpns = load_system_data_contract(DPNS, platform_version)?;
85 let dashpay = load_system_data_contract(Dashpay, platform_version)?;
86 let masternode_reward_shares =
87 load_system_data_contract(MasternodeRewards, platform_version)?;
88 let token_history = load_system_data_contract(TokenHistory, platform_version)?;
89 let keyword_search = load_system_data_contract(KeywordSearch, platform_version)?;
90
91 self.withdrawals.store(withdrawals);
93 self.dpns.store(dpns);
94 self.dashpay.store(dashpay);
95 self.masternode_reward_shares
96 .store(masternode_reward_shares);
97 self.token_history.store(token_history);
98 self.keyword_search.store(keyword_search);
99
100 Ok(())
101 }
102
103 pub fn load_genesis_system_contracts() -> Result<Self, Error> {
105 Ok(Self {
107 withdrawals: ActiveSystemDataContract::new(
108 load_system_data_contract(
109 SystemDataContract::Withdrawals,
110 PlatformVersion::first(),
111 )?,
112 1,
113 ),
114 dpns: ActiveSystemDataContract::new(
115 load_system_data_contract(SystemDataContract::DPNS, PlatformVersion::first())?,
116 1,
117 ),
118 dashpay: ActiveSystemDataContract::new(
119 load_system_data_contract(SystemDataContract::Dashpay, PlatformVersion::first())?,
120 1,
121 ),
122 masternode_reward_shares: ActiveSystemDataContract::new(
123 load_system_data_contract(
124 SystemDataContract::MasternodeRewards,
125 PlatformVersion::first(),
126 )?,
127 1,
128 ),
129 token_history: ActiveSystemDataContract::new(
130 load_system_data_contract(
131 SystemDataContract::TokenHistory,
132 PlatformVersion::first(),
133 )?,
134 9,
135 ),
136 keyword_search: ActiveSystemDataContract::new(
137 load_system_data_contract(
138 SystemDataContract::KeywordSearch,
139 PlatformVersion::first(),
140 )?,
141 9,
142 ),
143 })
144 }
145
146 pub fn load_withdrawals(&self) -> Guard<Arc<DataContract>> {
148 self.withdrawals.load()
149 }
150
151 pub fn load_token_history(&self) -> Guard<Arc<DataContract>> {
153 self.token_history.load()
154 }
155
156 pub fn load_dpns(&self) -> Guard<Arc<DataContract>> {
158 self.dpns.load()
159 }
160
161 pub fn load_dashpay(&self) -> Guard<Arc<DataContract>> {
163 self.dashpay.load()
164 }
165
166 pub fn load_masternode_reward_shares(&self) -> Guard<Arc<DataContract>> {
168 self.masternode_reward_shares.load()
169 }
170
171 pub fn load_keyword_search(&self) -> Guard<Arc<DataContract>> {
173 self.keyword_search.load()
174 }
175}