casper_storage/data_access_layer/
mint.rs

1use 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/// Transfer arguments using balance identifiers.
18#[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    /// Ctor.
29    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    /// Get to.
46    pub fn to(&self) -> Option<AccountHash> {
47        self.to
48    }
49
50    /// Get source.
51    pub fn source(&self) -> &BalanceIdentifier {
52        &self.source
53    }
54
55    /// Get target.
56    pub fn target(&self) -> &BalanceIdentifier {
57        &self.target
58    }
59
60    /// Get amount.
61    pub fn amount(&self) -> U512 {
62        self.amount
63    }
64
65    /// Get arg_id.
66    pub fn arg_id(&self) -> Option<u64> {
67        self.arg_id
68    }
69}
70
71/// Transfer details.
72#[derive(Debug, Clone, PartialEq, Eq)]
73pub enum TransferRequestArgs {
74    /// Provides opaque arguments in runtime format.
75    Raw(RuntimeArgs),
76    /// Provides explicit structured args.
77    Explicit(TransferArgs),
78    /// Provides support for transfers using balance identifiers.
79    /// The source and target purses will get resolved on usage.
80    Indirect(Box<BalanceIdentifierTransferArgs>),
81}
82
83/// Request for motes transfer.
84#[derive(Debug, Clone, PartialEq, Eq)]
85pub struct TransferRequest {
86    /// Config.
87    config: NativeRuntimeConfig,
88    /// State root hash.
89    state_hash: Digest,
90    /// Protocol version.
91    protocol_version: ProtocolVersion,
92    /// Transaction hash.
93    transaction_hash: TransactionHash,
94    /// Base account.
95    initiator: InitiatorAddr,
96    /// List of authorizing accounts.
97    authorization_keys: BTreeSet<AccountHash>,
98    /// Args.
99    args: TransferRequestArgs,
100}
101
102impl TransferRequest {
103    /// Creates new request object.
104    #[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    /// Creates new request instance with runtime args.
127    #[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    /// Creates new request object using balance identifiers.
150    #[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    /// Returns a reference to the runtime config.
173    pub fn config(&self) -> &NativeRuntimeConfig {
174        &self.config
175    }
176
177    /// Returns a reference to the transfer config.
178    pub fn transfer_config(&self) -> &TransferConfig {
179        self.config.transfer_config()
180    }
181
182    /// Returns state root hash.
183    pub fn state_hash(&self) -> Digest {
184        self.state_hash
185    }
186
187    /// Returns initiator.
188    pub fn initiator(&self) -> &InitiatorAddr {
189        &self.initiator
190    }
191
192    /// Returns authorization keys.
193    pub fn authorization_keys(&self) -> &BTreeSet<AccountHash> {
194        &self.authorization_keys
195    }
196
197    /// Returns protocol version.
198    pub fn protocol_version(&self) -> ProtocolVersion {
199        self.protocol_version
200    }
201
202    /// Returns transaction hash.
203    pub fn transaction_hash(&self) -> TransactionHash {
204        self.transaction_hash
205    }
206
207    /// Returns transfer args.
208    pub fn args(&self) -> &TransferRequestArgs {
209        &self.args
210    }
211
212    /// Into args.
213    pub fn into_args(self) -> TransferRequestArgs {
214        self.args
215    }
216
217    /// Used by `WasmTestBuilder` to set the appropriate state root hash and transfer config before
218    /// executing the transfer.
219    #[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/// Transfer result.
227#[derive(Debug, Clone)]
228pub enum TransferResult {
229    /// Invalid state root hash.
230    RootNotFound,
231    /// Transfer succeeded
232    Success {
233        /// List of transfers that happened during execution.
234        transfers: Vec<Transfer>,
235        /// Effects of transfer.
236        effects: Effects,
237        /// Cached tracking copy operations.
238        cache: TrackingCopyCache,
239    },
240    /// Transfer failed
241    Failure(TransferError),
242}
243
244impl TransferResult {
245    /// Returns the effects, if any.
246    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    /// Returns transfers.
254    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    /// Returns transfer error, if any.
262    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/// Burn details.
272#[derive(Debug, Clone, PartialEq, Eq)]
273pub enum BurnRequestArgs {
274    /// Provides opaque arguments in runtime format.
275    Raw(RuntimeArgs),
276    /// Provides explicit structured args.
277    Explicit(BurnArgs),
278}
279
280/// Request for motes burn.
281pub struct BurnRequest {
282    /// Config.
283    config: NativeRuntimeConfig,
284    /// State root hash.
285    state_hash: Digest,
286    /// Protocol version.
287    protocol_version: ProtocolVersion,
288    /// Transaction hash.
289    transaction_hash: TransactionHash,
290    /// Base account.
291    initiator: InitiatorAddr,
292    /// List of authorizing accounts.
293    authorization_keys: BTreeSet<AccountHash>,
294    /// Args.
295    args: BurnRequestArgs,
296}
297
298impl BurnRequest {
299    /// Creates new request object.
300    #[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    /// Creates new request instance with runtime args.
323    #[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    /// Returns a reference to the runtime config.
346    pub fn config(&self) -> &NativeRuntimeConfig {
347        &self.config
348    }
349
350    /// Returns state root hash.
351    pub fn state_hash(&self) -> Digest {
352        self.state_hash
353    }
354
355    /// Returns initiator.
356    pub fn initiator(&self) -> &InitiatorAddr {
357        &self.initiator
358    }
359
360    /// Returns authorization keys.
361    pub fn authorization_keys(&self) -> &BTreeSet<AccountHash> {
362        &self.authorization_keys
363    }
364
365    /// Returns protocol version.
366    pub fn protocol_version(&self) -> ProtocolVersion {
367        self.protocol_version
368    }
369
370    /// Returns transaction hash.
371    pub fn transaction_hash(&self) -> TransactionHash {
372        self.transaction_hash
373    }
374
375    /// Returns transfer args.
376    pub fn args(&self) -> &BurnRequestArgs {
377        &self.args
378    }
379
380    /// Into args.
381    pub fn into_args(self) -> BurnRequestArgs {
382        self.args
383    }
384
385    /// Used by `WasmTestBuilder` to set the appropriate state root hash and runtime config before
386    /// executing the burn.
387    #[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/// Burn result.
395#[derive(Debug, Clone)]
396pub enum BurnResult {
397    /// Invalid state root hash.
398    RootNotFound,
399    /// Transfer succeeded
400    Success {
401        /// Effects of transfer.
402        effects: Effects,
403        /// Cached tracking copy operations.
404        cache: TrackingCopyCache,
405    },
406    /// Burn failed
407    Failure(BurnError),
408}
409
410impl BurnResult {
411    /// Returns the effects, if any.
412    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    /// Returns burn error, if any.
420    pub fn error(&self) -> Option<BurnError> {
421        if let Self::Failure(error) = self {
422            Some(error.clone())
423        } else {
424            None
425        }
426    }
427}