casper_storage/data_access_layer/
mint.rs1use std::collections::BTreeSet;
2
3use crate::{
4 data_access_layer::BalanceIdentifier,
5 system::{
6 burn::{BurnArgs, BurnError},
7 runtime_native::{Config as NativeRuntimeConfig, TransferConfig},
8 transfer::{TransferArgs, TransferError},
9 },
10 tracking_copy::TrackingCopyCache,
11};
12use casper_types::{
13 account::AccountHash, execution::Effects, Digest, InitiatorAddr, ProtocolVersion, RuntimeArgs,
14 TransactionHash, Transfer, U512,
15};
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct BalanceIdentifierTransferArgs {
20 to: Option<AccountHash>,
21 source: BalanceIdentifier,
22 target: BalanceIdentifier,
23 amount: U512,
24 arg_id: Option<u64>,
25}
26
27impl BalanceIdentifierTransferArgs {
28 pub fn new(
30 to: Option<AccountHash>,
31 source: BalanceIdentifier,
32 target: BalanceIdentifier,
33 amount: U512,
34 arg_id: Option<u64>,
35 ) -> Self {
36 BalanceIdentifierTransferArgs {
37 to,
38 source,
39 target,
40 amount,
41 arg_id,
42 }
43 }
44
45 pub fn to(&self) -> Option<AccountHash> {
47 self.to
48 }
49
50 pub fn source(&self) -> &BalanceIdentifier {
52 &self.source
53 }
54
55 pub fn target(&self) -> &BalanceIdentifier {
57 &self.target
58 }
59
60 pub fn amount(&self) -> U512 {
62 self.amount
63 }
64
65 pub fn arg_id(&self) -> Option<u64> {
67 self.arg_id
68 }
69}
70
71#[derive(Debug, Clone, PartialEq, Eq)]
73pub enum TransferRequestArgs {
74 Raw(RuntimeArgs),
76 Explicit(TransferArgs),
78 Indirect(Box<BalanceIdentifierTransferArgs>),
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
85pub struct TransferRequest {
86 config: NativeRuntimeConfig,
88 state_hash: Digest,
90 protocol_version: ProtocolVersion,
92 transaction_hash: TransactionHash,
94 initiator: InitiatorAddr,
96 authorization_keys: BTreeSet<AccountHash>,
98 args: TransferRequestArgs,
100}
101
102impl TransferRequest {
103 #[allow(clippy::too_many_arguments)]
105 pub fn new(
106 config: NativeRuntimeConfig,
107 state_hash: Digest,
108 protocol_version: ProtocolVersion,
109 transaction_hash: TransactionHash,
110 initiator: InitiatorAddr,
111 authorization_keys: BTreeSet<AccountHash>,
112 args: TransferArgs,
113 ) -> Self {
114 let args = TransferRequestArgs::Explicit(args);
115 Self {
116 config,
117 state_hash,
118 protocol_version,
119 transaction_hash,
120 initiator,
121 authorization_keys,
122 args,
123 }
124 }
125
126 #[allow(clippy::too_many_arguments)]
128 pub fn with_runtime_args(
129 config: NativeRuntimeConfig,
130 state_hash: Digest,
131 protocol_version: ProtocolVersion,
132 transaction_hash: TransactionHash,
133 initiator: InitiatorAddr,
134 authorization_keys: BTreeSet<AccountHash>,
135 args: RuntimeArgs,
136 ) -> Self {
137 let args = TransferRequestArgs::Raw(args);
138 Self {
139 config,
140 state_hash,
141 protocol_version,
142 transaction_hash,
143 initiator,
144 authorization_keys,
145 args,
146 }
147 }
148
149 #[allow(clippy::too_many_arguments)]
151 pub fn new_indirect(
152 config: NativeRuntimeConfig,
153 state_hash: Digest,
154 protocol_version: ProtocolVersion,
155 transaction_hash: TransactionHash,
156 initiator: InitiatorAddr,
157 authorization_keys: BTreeSet<AccountHash>,
158 args: BalanceIdentifierTransferArgs,
159 ) -> Self {
160 let args = TransferRequestArgs::Indirect(Box::new(args));
161 Self {
162 config,
163 state_hash,
164 protocol_version,
165 transaction_hash,
166 initiator,
167 authorization_keys,
168 args,
169 }
170 }
171
172 pub fn config(&self) -> &NativeRuntimeConfig {
174 &self.config
175 }
176
177 pub fn transfer_config(&self) -> &TransferConfig {
179 self.config.transfer_config()
180 }
181
182 pub fn state_hash(&self) -> Digest {
184 self.state_hash
185 }
186
187 pub fn initiator(&self) -> &InitiatorAddr {
189 &self.initiator
190 }
191
192 pub fn authorization_keys(&self) -> &BTreeSet<AccountHash> {
194 &self.authorization_keys
195 }
196
197 pub fn protocol_version(&self) -> ProtocolVersion {
199 self.protocol_version
200 }
201
202 pub fn transaction_hash(&self) -> TransactionHash {
204 self.transaction_hash
205 }
206
207 pub fn args(&self) -> &TransferRequestArgs {
209 &self.args
210 }
211
212 pub fn into_args(self) -> TransferRequestArgs {
214 self.args
215 }
216
217 #[doc(hidden)]
220 pub fn set_state_hash_and_config(&mut self, state_hash: Digest, config: NativeRuntimeConfig) {
221 self.state_hash = state_hash;
222 self.config = config;
223 }
224}
225
226#[derive(Debug, Clone)]
228pub enum TransferResult {
229 RootNotFound,
231 Success {
233 transfers: Vec<Transfer>,
235 effects: Effects,
237 cache: TrackingCopyCache,
239 },
240 Failure(TransferError),
242}
243
244impl TransferResult {
245 pub fn effects(&self) -> Effects {
247 match self {
248 TransferResult::RootNotFound | TransferResult::Failure(_) => Effects::new(),
249 TransferResult::Success { effects, .. } => effects.clone(),
250 }
251 }
252
253 pub fn transfers(&self) -> Vec<Transfer> {
255 match self {
256 TransferResult::RootNotFound | TransferResult::Failure(_) => vec![],
257 TransferResult::Success { transfers, .. } => transfers.clone(),
258 }
259 }
260
261 pub fn error(&self) -> Option<TransferError> {
263 if let Self::Failure(error) = self {
264 Some(error.clone())
265 } else {
266 None
267 }
268 }
269}
270
271#[derive(Debug, Clone, PartialEq, Eq)]
273pub enum BurnRequestArgs {
274 Raw(RuntimeArgs),
276 Explicit(BurnArgs),
278}
279
280pub struct BurnRequest {
282 config: NativeRuntimeConfig,
284 state_hash: Digest,
286 protocol_version: ProtocolVersion,
288 transaction_hash: TransactionHash,
290 initiator: InitiatorAddr,
292 authorization_keys: BTreeSet<AccountHash>,
294 args: BurnRequestArgs,
296}
297
298impl BurnRequest {
299 #[allow(clippy::too_many_arguments)]
301 pub fn new(
302 config: NativeRuntimeConfig,
303 state_hash: Digest,
304 protocol_version: ProtocolVersion,
305 transaction_hash: TransactionHash,
306 initiator: InitiatorAddr,
307 authorization_keys: BTreeSet<AccountHash>,
308 args: BurnArgs,
309 ) -> Self {
310 let args = BurnRequestArgs::Explicit(args);
311 Self {
312 config,
313 state_hash,
314 protocol_version,
315 transaction_hash,
316 initiator,
317 authorization_keys,
318 args,
319 }
320 }
321
322 #[allow(clippy::too_many_arguments)]
324 pub fn with_runtime_args(
325 config: NativeRuntimeConfig,
326 state_hash: Digest,
327 protocol_version: ProtocolVersion,
328 transaction_hash: TransactionHash,
329 initiator: InitiatorAddr,
330 authorization_keys: BTreeSet<AccountHash>,
331 args: RuntimeArgs,
332 ) -> Self {
333 let args = BurnRequestArgs::Raw(args);
334 Self {
335 config,
336 state_hash,
337 protocol_version,
338 transaction_hash,
339 initiator,
340 authorization_keys,
341 args,
342 }
343 }
344
345 pub fn config(&self) -> &NativeRuntimeConfig {
347 &self.config
348 }
349
350 pub fn state_hash(&self) -> Digest {
352 self.state_hash
353 }
354
355 pub fn initiator(&self) -> &InitiatorAddr {
357 &self.initiator
358 }
359
360 pub fn authorization_keys(&self) -> &BTreeSet<AccountHash> {
362 &self.authorization_keys
363 }
364
365 pub fn protocol_version(&self) -> ProtocolVersion {
367 self.protocol_version
368 }
369
370 pub fn transaction_hash(&self) -> TransactionHash {
372 self.transaction_hash
373 }
374
375 pub fn args(&self) -> &BurnRequestArgs {
377 &self.args
378 }
379
380 pub fn into_args(self) -> BurnRequestArgs {
382 self.args
383 }
384
385 #[doc(hidden)]
388 pub fn set_state_hash_and_config(&mut self, state_hash: Digest, config: NativeRuntimeConfig) {
389 self.state_hash = state_hash;
390 self.config = config;
391 }
392}
393
394#[derive(Debug, Clone)]
396pub enum BurnResult {
397 RootNotFound,
399 Success {
401 effects: Effects,
403 cache: TrackingCopyCache,
405 },
406 Failure(BurnError),
408}
409
410impl BurnResult {
411 pub fn effects(&self) -> Effects {
413 match self {
414 BurnResult::RootNotFound | BurnResult::Failure(_) => Effects::new(),
415 BurnResult::Success { effects, .. } => effects.clone(),
416 }
417 }
418
419 pub fn error(&self) -> Option<BurnError> {
421 if let Self::Failure(error) = self {
422 Some(error.clone())
423 } else {
424 None
425 }
426 }
427}