Skip to content

Commit 137204b

Browse files
committed
Ledger/transaction_logic: extract transaction_applied module to separate file
Extract the transaction_applied module from transaction_logic/mod.rs into its own file. This module contains types for applied transactions including SignedCommandApplied, ZkappCommandApplied, FeeTransferApplied, and CoinbaseApplied. Changes: - Extract transaction_applied module to transaction_logic/transaction_applied.rs - Use explicit imports instead of 'use super::*' - Add Magnitude trait import for Amount::zero() method - Update mod.rs to reference the new module file
1 parent 0da5eb9 commit 137204b

File tree

2 files changed

+187
-178
lines changed

2 files changed

+187
-178
lines changed

ledger/src/scan_state/transaction_logic/mod.rs

Lines changed: 1 addition & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,184 +1033,7 @@ impl From<&Transaction> for MinaTransactionTransactionStableV2 {
10331033
}
10341034
}
10351035

1036-
pub mod transaction_applied {
1037-
use crate::AccountId;
1038-
1039-
use super::*;
1040-
1041-
pub mod signed_command_applied {
1042-
use super::*;
1043-
1044-
#[derive(Debug, Clone, PartialEq)]
1045-
pub struct Common {
1046-
pub user_command: WithStatus<signed_command::SignedCommand>,
1047-
}
1048-
1049-
#[derive(Debug, Clone, PartialEq)]
1050-
pub enum Body {
1051-
Payments {
1052-
new_accounts: Vec<AccountId>,
1053-
},
1054-
StakeDelegation {
1055-
previous_delegate: Option<CompressedPubKey>,
1056-
},
1057-
Failed,
1058-
}
1059-
1060-
#[derive(Debug, Clone, PartialEq)]
1061-
pub struct SignedCommandApplied {
1062-
pub common: Common,
1063-
pub body: Body,
1064-
}
1065-
}
1066-
1067-
pub use signed_command_applied::SignedCommandApplied;
1068-
1069-
impl SignedCommandApplied {
1070-
pub fn new_accounts(&self) -> &[AccountId] {
1071-
use signed_command_applied::Body::*;
1072-
1073-
match &self.body {
1074-
Payments { new_accounts } => new_accounts.as_slice(),
1075-
StakeDelegation { .. } | Failed => &[],
1076-
}
1077-
}
1078-
}
1079-
1080-
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L65>
1081-
#[derive(Debug, Clone, PartialEq)]
1082-
pub struct ZkappCommandApplied {
1083-
pub accounts: Vec<(AccountId, Option<Box<Account>>)>,
1084-
pub command: WithStatus<zkapp_command::ZkAppCommand>,
1085-
pub new_accounts: Vec<AccountId>,
1086-
}
1087-
1088-
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L82>
1089-
#[derive(Debug, Clone, PartialEq)]
1090-
pub enum CommandApplied {
1091-
SignedCommand(Box<SignedCommandApplied>),
1092-
ZkappCommand(Box<ZkappCommandApplied>),
1093-
}
1094-
1095-
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L96>
1096-
#[derive(Debug, Clone, PartialEq)]
1097-
pub struct FeeTransferApplied {
1098-
pub fee_transfer: WithStatus<FeeTransfer>,
1099-
pub new_accounts: Vec<AccountId>,
1100-
pub burned_tokens: Amount,
1101-
}
1102-
1103-
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L112>
1104-
#[derive(Debug, Clone, PartialEq)]
1105-
pub struct CoinbaseApplied {
1106-
pub coinbase: WithStatus<Coinbase>,
1107-
pub new_accounts: Vec<AccountId>,
1108-
pub burned_tokens: Amount,
1109-
}
1110-
1111-
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L142>
1112-
#[derive(Debug, Clone, PartialEq)]
1113-
pub enum Varying {
1114-
Command(CommandApplied),
1115-
FeeTransfer(FeeTransferApplied),
1116-
Coinbase(CoinbaseApplied),
1117-
}
1118-
1119-
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L142>
1120-
#[derive(Debug, Clone, PartialEq)]
1121-
pub struct TransactionApplied {
1122-
pub previous_hash: Fp,
1123-
pub varying: Varying,
1124-
}
1125-
1126-
impl TransactionApplied {
1127-
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L639>
1128-
pub fn transaction(&self) -> WithStatus<Transaction> {
1129-
use CommandApplied::*;
1130-
use Varying::*;
1131-
1132-
match &self.varying {
1133-
Command(SignedCommand(cmd)) => cmd
1134-
.common
1135-
.user_command
1136-
.map(|c| Transaction::Command(UserCommand::SignedCommand(Box::new(c.clone())))),
1137-
Command(ZkappCommand(cmd)) => cmd
1138-
.command
1139-
.map(|c| Transaction::Command(UserCommand::ZkAppCommand(Box::new(c.clone())))),
1140-
FeeTransfer(f) => f.fee_transfer.map(|f| Transaction::FeeTransfer(f.clone())),
1141-
Coinbase(c) => c.coinbase.map(|c| Transaction::Coinbase(c.clone())),
1142-
}
1143-
}
1144-
1145-
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L662>
1146-
pub fn transaction_status(&self) -> &TransactionStatus {
1147-
use CommandApplied::*;
1148-
use Varying::*;
1149-
1150-
match &self.varying {
1151-
Command(SignedCommand(cmd)) => &cmd.common.user_command.status,
1152-
Command(ZkappCommand(cmd)) => &cmd.command.status,
1153-
FeeTransfer(f) => &f.fee_transfer.status,
1154-
Coinbase(c) => &c.coinbase.status,
1155-
}
1156-
}
1157-
1158-
pub fn burned_tokens(&self) -> Amount {
1159-
match &self.varying {
1160-
Varying::Command(_) => Amount::zero(),
1161-
Varying::FeeTransfer(f) => f.burned_tokens,
1162-
Varying::Coinbase(c) => c.burned_tokens,
1163-
}
1164-
}
1165-
1166-
pub fn new_accounts(&self) -> &[AccountId] {
1167-
use CommandApplied::*;
1168-
use Varying::*;
1169-
1170-
match &self.varying {
1171-
Command(SignedCommand(cmd)) => cmd.new_accounts(),
1172-
Command(ZkappCommand(cmd)) => cmd.new_accounts.as_slice(),
1173-
FeeTransfer(f) => f.new_accounts.as_slice(),
1174-
Coinbase(cb) => cb.new_accounts.as_slice(),
1175-
}
1176-
}
1177-
1178-
/// <https://github.com/MinaProtocol/mina/blob/e5183ca1dde1c085b4c5d37d1d9987e24c294c32/src/lib/transaction_logic/mina_transaction_logic.ml#L176>
1179-
pub fn supply_increase(
1180-
&self,
1181-
constraint_constants: &ConstraintConstants,
1182-
) -> Result<Signed<Amount>, String> {
1183-
let burned_tokens = Signed::<Amount>::of_unsigned(self.burned_tokens());
1184-
1185-
let account_creation_fees = {
1186-
let account_creation_fee_int = constraint_constants.account_creation_fee;
1187-
let num_accounts_created = self.new_accounts().len() as u64;
1188-
1189-
// int type is OK, no danger of overflow
1190-
let amount = account_creation_fee_int
1191-
.checked_mul(num_accounts_created)
1192-
.unwrap();
1193-
Signed::<Amount>::of_unsigned(Amount::from_u64(amount))
1194-
};
1195-
1196-
let expected_supply_increase = match &self.varying {
1197-
Varying::Coinbase(cb) => cb.coinbase.data.expected_supply_increase()?,
1198-
_ => Amount::zero(),
1199-
};
1200-
let expected_supply_increase = Signed::<Amount>::of_unsigned(expected_supply_increase);
1201-
1202-
// TODO: Make sure it's correct
1203-
let total = [burned_tokens, account_creation_fees]
1204-
.into_iter()
1205-
.try_fold(expected_supply_increase, |total, amt| {
1206-
total.add(&amt.negate())
1207-
});
1208-
1209-
total.ok_or_else(|| "overflow".to_string())
1210-
}
1211-
}
1212-
}
1213-
1036+
pub mod transaction_applied;
12141037
pub mod transaction_witness {
12151038
use mina_p2p_messages::v2::MinaStateProtocolStateBodyValueStableV2;
12161039

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
use mina_core::constants::ConstraintConstants;
2+
use mina_hasher::Fp;
3+
4+
use crate::{Account, AccountId};
5+
6+
use super::{
7+
signed_command, zkapp_command, Coinbase, FeeTransfer, Transaction, TransactionStatus,
8+
UserCommand, WithStatus,
9+
};
10+
use crate::scan_state::currency::{Amount, Magnitude, Signed};
11+
12+
pub mod signed_command_applied {
13+
use mina_signer::CompressedPubKey;
14+
15+
use crate::AccountId;
16+
17+
use super::{signed_command, WithStatus};
18+
19+
#[derive(Debug, Clone, PartialEq)]
20+
pub struct Common {
21+
pub user_command: WithStatus<signed_command::SignedCommand>,
22+
}
23+
24+
#[derive(Debug, Clone, PartialEq)]
25+
pub enum Body {
26+
Payments {
27+
new_accounts: Vec<AccountId>,
28+
},
29+
StakeDelegation {
30+
previous_delegate: Option<CompressedPubKey>,
31+
},
32+
Failed,
33+
}
34+
35+
#[derive(Debug, Clone, PartialEq)]
36+
pub struct SignedCommandApplied {
37+
pub common: Common,
38+
pub body: Body,
39+
}
40+
}
41+
42+
pub use signed_command_applied::SignedCommandApplied;
43+
44+
impl SignedCommandApplied {
45+
pub fn new_accounts(&self) -> &[AccountId] {
46+
use signed_command_applied::Body::*;
47+
48+
match &self.body {
49+
Payments { new_accounts } => new_accounts.as_slice(),
50+
StakeDelegation { .. } | Failed => &[],
51+
}
52+
}
53+
}
54+
55+
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L65>
56+
#[derive(Debug, Clone, PartialEq)]
57+
pub struct ZkappCommandApplied {
58+
pub accounts: Vec<(AccountId, Option<Box<Account>>)>,
59+
pub command: WithStatus<zkapp_command::ZkAppCommand>,
60+
pub new_accounts: Vec<AccountId>,
61+
}
62+
63+
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L82>
64+
#[derive(Debug, Clone, PartialEq)]
65+
pub enum CommandApplied {
66+
SignedCommand(Box<SignedCommandApplied>),
67+
ZkappCommand(Box<ZkappCommandApplied>),
68+
}
69+
70+
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L96>
71+
#[derive(Debug, Clone, PartialEq)]
72+
pub struct FeeTransferApplied {
73+
pub fee_transfer: WithStatus<FeeTransfer>,
74+
pub new_accounts: Vec<AccountId>,
75+
pub burned_tokens: Amount,
76+
}
77+
78+
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L112>
79+
#[derive(Debug, Clone, PartialEq)]
80+
pub struct CoinbaseApplied {
81+
pub coinbase: WithStatus<Coinbase>,
82+
pub new_accounts: Vec<AccountId>,
83+
pub burned_tokens: Amount,
84+
}
85+
86+
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L142>
87+
#[derive(Debug, Clone, PartialEq)]
88+
pub enum Varying {
89+
Command(CommandApplied),
90+
FeeTransfer(FeeTransferApplied),
91+
Coinbase(CoinbaseApplied),
92+
}
93+
94+
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L142>
95+
#[derive(Debug, Clone, PartialEq)]
96+
pub struct TransactionApplied {
97+
pub previous_hash: Fp,
98+
pub varying: Varying,
99+
}
100+
101+
impl TransactionApplied {
102+
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L639>
103+
pub fn transaction(&self) -> WithStatus<Transaction> {
104+
use CommandApplied::*;
105+
use Varying::*;
106+
107+
match &self.varying {
108+
Command(SignedCommand(cmd)) => cmd
109+
.common
110+
.user_command
111+
.map(|c| Transaction::Command(UserCommand::SignedCommand(Box::new(c.clone())))),
112+
Command(ZkappCommand(cmd)) => cmd
113+
.command
114+
.map(|c| Transaction::Command(UserCommand::ZkAppCommand(Box::new(c.clone())))),
115+
FeeTransfer(f) => f.fee_transfer.map(|f| Transaction::FeeTransfer(f.clone())),
116+
Coinbase(c) => c.coinbase.map(|c| Transaction::Coinbase(c.clone())),
117+
}
118+
}
119+
120+
/// <https://github.com/MinaProtocol/mina/blob/2ee6e004ba8c6a0541056076aab22ea162f7eb3a/src/lib/transaction_logic/mina_transaction_logic.ml#L662>
121+
pub fn transaction_status(&self) -> &TransactionStatus {
122+
use CommandApplied::*;
123+
use Varying::*;
124+
125+
match &self.varying {
126+
Command(SignedCommand(cmd)) => &cmd.common.user_command.status,
127+
Command(ZkappCommand(cmd)) => &cmd.command.status,
128+
FeeTransfer(f) => &f.fee_transfer.status,
129+
Coinbase(c) => &c.coinbase.status,
130+
}
131+
}
132+
133+
pub fn burned_tokens(&self) -> Amount {
134+
match &self.varying {
135+
Varying::Command(_) => Amount::zero(),
136+
Varying::FeeTransfer(f) => f.burned_tokens,
137+
Varying::Coinbase(c) => c.burned_tokens,
138+
}
139+
}
140+
141+
pub fn new_accounts(&self) -> &[AccountId] {
142+
use CommandApplied::*;
143+
use Varying::*;
144+
145+
match &self.varying {
146+
Command(SignedCommand(cmd)) => cmd.new_accounts(),
147+
Command(ZkappCommand(cmd)) => cmd.new_accounts.as_slice(),
148+
FeeTransfer(f) => f.new_accounts.as_slice(),
149+
Coinbase(cb) => cb.new_accounts.as_slice(),
150+
}
151+
}
152+
153+
/// <https://github.com/MinaProtocol/mina/blob/e5183ca1dde1c085b4c5d37d1d9987e24c294c32/src/lib/transaction_logic/mina_transaction_logic.ml#L176>
154+
pub fn supply_increase(
155+
&self,
156+
constraint_constants: &ConstraintConstants,
157+
) -> Result<Signed<Amount>, String> {
158+
let burned_tokens = Signed::<Amount>::of_unsigned(self.burned_tokens());
159+
160+
let account_creation_fees = {
161+
let account_creation_fee_int = constraint_constants.account_creation_fee;
162+
let num_accounts_created = self.new_accounts().len() as u64;
163+
164+
// int type is OK, no danger of overflow
165+
let amount = account_creation_fee_int
166+
.checked_mul(num_accounts_created)
167+
.unwrap();
168+
Signed::<Amount>::of_unsigned(Amount::from_u64(amount))
169+
};
170+
171+
let expected_supply_increase = match &self.varying {
172+
Varying::Coinbase(cb) => cb.coinbase.data.expected_supply_increase()?,
173+
_ => Amount::zero(),
174+
};
175+
let expected_supply_increase = Signed::<Amount>::of_unsigned(expected_supply_increase);
176+
177+
// TODO: Make sure it's correct
178+
let total = [burned_tokens, account_creation_fees]
179+
.into_iter()
180+
.try_fold(expected_supply_increase, |total, amt| {
181+
total.add(&amt.negate())
182+
});
183+
184+
total.ok_or_else(|| "overflow".to_string())
185+
}
186+
}

0 commit comments

Comments
 (0)