radix_engine/updates/
protocol_update_batch.rs

1use crate::internal_prelude::*;
2use radix_transactions::model::*;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum ProtocolUpdateTransaction {
6    FlashTransactionV1(FlashTransactionV1),
7    SystemTransactionV1(ProtocolSystemTransactionV1),
8}
9
10impl From<FlashTransactionV1> for ProtocolUpdateTransaction {
11    fn from(value: FlashTransactionV1) -> Self {
12        Self::FlashTransactionV1(value)
13    }
14}
15
16impl From<ProtocolSystemTransactionV1> for ProtocolUpdateTransaction {
17    fn from(value: ProtocolSystemTransactionV1) -> Self {
18        Self::SystemTransactionV1(value)
19    }
20}
21
22impl ProtocolUpdateTransaction {
23    pub fn flash(name: impl Into<String>, state_updates: StateUpdates) -> Self {
24        let name = name.into();
25        if name != name.to_ascii_lowercase().as_str() {
26            panic!("Protocol transaction names should be in kebab-case for consistency");
27        }
28        Self::FlashTransactionV1(FlashTransactionV1 {
29            name: name.into(),
30            state_updates,
31        })
32    }
33
34    pub fn genesis_transaction(name: impl Into<String>, transaction: SystemTransactionV1) -> Self {
35        let name = name.into();
36        if name != name.to_ascii_lowercase().as_str() {
37            panic!("Protocol transaction names should be in kebab-case for consistency");
38        }
39        Self::SystemTransactionV1(ProtocolSystemTransactionV1 {
40            name: name.into(),
41            disable_auth: true,
42            transaction,
43        })
44    }
45
46    pub fn name(&self) -> Option<&str> {
47        match self {
48            ProtocolUpdateTransaction::FlashTransactionV1(tx) => Some(tx.name.as_str()),
49            ProtocolUpdateTransaction::SystemTransactionV1(tx) => Some(tx.name.as_str()),
50        }
51    }
52}
53
54/// At present, this isn't actually saved in the node - instead just the
55/// SystemTransactionV1 is saved.
56#[derive(Debug, Clone, PartialEq, Eq, ManifestSbor, ScryptoDescribe)]
57pub struct ProtocolSystemTransactionV1 {
58    pub name: String,
59    pub disable_auth: bool,
60    pub transaction: SystemTransactionV1,
61}
62
63/// A set of transactions which all get committed together with the same proof.
64/// To avoid memory overflows, this should be kept small enough to comfortably fit into
65/// memory (e.g. one transaction per batch).
66#[derive(Debug, Clone, PartialEq, Eq)]
67pub struct ProtocolUpdateBatch {
68    pub transactions: Vec<ProtocolUpdateTransaction>,
69}
70
71impl ProtocolUpdateBatch {
72    pub fn empty() -> Self {
73        Self {
74            transactions: vec![],
75        }
76    }
77
78    pub fn new(transactions: impl IntoIterator<Item = ProtocolUpdateTransaction>) -> Self {
79        Self {
80            transactions: transactions.into_iter().collect(),
81        }
82    }
83
84    pub fn add_flash(mut self, name: impl Into<String>, updates: StateUpdates) -> Self {
85        self.mut_add_flash(name, updates);
86        self
87    }
88
89    pub fn mut_add_flash(&mut self, name: impl Into<String>, updates: StateUpdates) {
90        self.mut_add(ProtocolUpdateTransaction::flash(name, updates))
91    }
92
93    pub fn add(mut self, transaction: ProtocolUpdateTransaction) -> Self {
94        self.mut_add(transaction);
95        self
96    }
97
98    pub fn mut_add(&mut self, transaction: ProtocolUpdateTransaction) {
99        self.transactions.push(transaction);
100    }
101
102    pub fn single(single_transaction: ProtocolUpdateTransaction) -> Self {
103        Self {
104            transactions: vec![single_transaction],
105        }
106    }
107}