mina_tree/scan_state/transaction_logic/
valid.rs

1use super::{GenericCommand, GenericTransaction};
2use crate::{
3    scan_state::currency::{Fee, Nonce},
4    AccountId,
5};
6use mina_curves::pasta::Fp;
7use mina_p2p_messages::v2::MinaBaseUserCommandStableV2;
8use serde::{Deserialize, Serialize};
9
10#[derive(Clone, Debug, Hash, PartialEq, Eq)]
11pub struct VerificationKeyHash(pub Fp);
12
13pub type SignedCommand = super::signed_command::SignedCommand;
14
15#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
16#[serde(into = "MinaBaseUserCommandStableV2")]
17#[serde(try_from = "MinaBaseUserCommandStableV2")]
18pub enum UserCommand {
19    SignedCommand(Box<SignedCommand>),
20    ZkAppCommand(Box<super::zkapp_command::valid::ZkAppCommand>),
21}
22
23impl UserCommand {
24    /// <https://github.com/MinaProtocol/mina/blob/05c2f73d0f6e4f1341286843814ce02dcb3919e0/src/lib/mina_base/user_command.ml#L277>
25    pub fn forget_check(&self) -> super::UserCommand {
26        match self {
27            UserCommand::SignedCommand(cmd) => super::UserCommand::SignedCommand(cmd.clone()),
28            UserCommand::ZkAppCommand(cmd) => {
29                super::UserCommand::ZkAppCommand(Box::new(cmd.zkapp_command.clone()))
30            }
31        }
32    }
33
34    pub fn fee_payer(&self) -> AccountId {
35        match self {
36            UserCommand::SignedCommand(cmd) => cmd.fee_payer(),
37            UserCommand::ZkAppCommand(cmd) => cmd.zkapp_command.fee_payer(),
38        }
39    }
40
41    pub fn nonce(&self) -> Option<Nonce> {
42        match self {
43            UserCommand::SignedCommand(cmd) => Some(cmd.nonce()),
44            UserCommand::ZkAppCommand(_) => None,
45        }
46    }
47}
48
49impl GenericCommand for UserCommand {
50    fn fee(&self) -> Fee {
51        match self {
52            UserCommand::SignedCommand(cmd) => cmd.fee(),
53            UserCommand::ZkAppCommand(cmd) => cmd.zkapp_command.fee(),
54        }
55    }
56
57    fn forget(&self) -> super::UserCommand {
58        match self {
59            UserCommand::SignedCommand(cmd) => super::UserCommand::SignedCommand(cmd.clone()),
60            UserCommand::ZkAppCommand(cmd) => {
61                super::UserCommand::ZkAppCommand(Box::new(cmd.zkapp_command.clone()))
62            }
63        }
64    }
65}
66
67impl GenericTransaction for Transaction {
68    fn is_fee_transfer(&self) -> bool {
69        matches!(self, Transaction::FeeTransfer(_))
70    }
71    fn is_coinbase(&self) -> bool {
72        matches!(self, Transaction::Coinbase(_))
73    }
74    fn is_command(&self) -> bool {
75        matches!(self, Transaction::Command(_))
76    }
77}
78
79#[derive(Debug, derive_more::From)]
80pub enum Transaction {
81    Command(UserCommand),
82    FeeTransfer(super::FeeTransfer),
83    Coinbase(super::Coinbase),
84}
85
86impl Transaction {
87    /// <https://github.com/MinaProtocol/mina/blob/05c2f73d0f6e4f1341286843814ce02dcb3919e0/src/lib/transaction/transaction.ml#L61>
88    pub fn forget(&self) -> super::Transaction {
89        match self {
90            Transaction::Command(cmd) => super::Transaction::Command(cmd.forget_check()),
91            Transaction::FeeTransfer(ft) => super::Transaction::FeeTransfer(ft.clone()),
92            Transaction::Coinbase(cb) => super::Transaction::Coinbase(cb.clone()),
93        }
94    }
95}