radix_engine_interface/api/
field_api.rs

1use bitflags::bitflags;
2use radix_common::data::scrypto::{scrypto_decode, scrypto_encode, ScryptoDecode, ScryptoEncode};
3use sbor::rust::fmt::Debug;
4use sbor::rust::vec::Vec;
5use sbor::*;
6
7bitflags! {
8    #[derive(Sbor)]
9    pub struct LockFlags: u32 {
10        /// Allows the locked substate to be mutated
11        const MUTABLE = 0b00000001;
12        /// Checks that the substate locked is unmodified from the beginning of
13        /// the transaction. This is used mainly for locking fees in vaults which
14        /// requires this in order to be able to support rollbacks
15        const UNMODIFIED_BASE = 0b00000010;
16        /// Forces a write of a substate even on a transaction failure
17        /// Currently used for vault fees.
18        const FORCE_WRITE = 0b00000100;
19    }
20}
21
22impl LockFlags {
23    pub fn read_only() -> Self {
24        LockFlags::empty()
25    }
26}
27
28pub type FieldHandle = u32;
29
30pub trait FieldPayloadMarker {}
31
32impl<T: FieldPayloadMarker> FieldPayloadMarker for &T {}
33
34/// System api to read/write fields
35pub trait SystemFieldApi<E: Debug> {
36    /// Retrieve the value of a field
37    fn field_read(&mut self, handle: FieldHandle) -> Result<Vec<u8>, E>;
38
39    /// Retrieve the value of a field
40    fn field_read_typed<S: ScryptoDecode>(&mut self, handle: FieldHandle) -> Result<S, E> {
41        let buf = self.field_read(handle)?;
42        let typed_substate: S = scrypto_decode(&buf).map_err(|e| e).unwrap();
43        Ok(typed_substate)
44    }
45
46    /// Write a value to a field
47    fn field_write(&mut self, handle: FieldHandle, buffer: Vec<u8>) -> Result<(), E>;
48
49    /// Write a value to a field
50    fn field_write_typed<S: ScryptoEncode>(
51        &mut self,
52        handle: FieldHandle,
53        substate: &S,
54    ) -> Result<(), E> {
55        let buf = scrypto_encode(substate).unwrap();
56        self.field_write(handle, buf)
57    }
58
59    /// Lock a field such that it becomes immutable
60    fn field_lock(&mut self, handle: FieldHandle) -> Result<(), E>;
61
62    /// Close a field handle so that it is no longer usable
63    fn field_close(&mut self, handle: FieldHandle) -> Result<(), E>;
64}