Skip to content

Commit dcf3dae

Browse files
committed
Ledger/transaction_logic: extract protocol_state module to separate file
Extract the protocol_state module from transaction_logic/mod.rs into its own file. This module contains protocol state types including ProtocolStateView, EpochData, EpochLedger, and GlobalState. Changes: - Extract protocol_state module to transaction_logic/protocol_state.rs - Use explicit imports instead of 'use super::*' - Update mod.rs to reference the new module file
1 parent aff5f24 commit dcf3dae

File tree

2 files changed

+162
-158
lines changed

2 files changed

+162
-158
lines changed

ledger/src/scan_state/transaction_logic/mod.rs

Lines changed: 1 addition & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,164 +1035,7 @@ impl From<&Transaction> for MinaTransactionTransactionStableV2 {
10351035

10361036
pub mod transaction_applied;
10371037
pub mod transaction_witness;
1038-
pub mod protocol_state {
1039-
use mina_p2p_messages::v2::{self, MinaStateProtocolStateValueStableV2};
1040-
1041-
use crate::proofs::field::FieldWitness;
1042-
1043-
use super::*;
1044-
1045-
#[derive(Debug, Clone)]
1046-
pub struct EpochLedger<F: FieldWitness> {
1047-
pub hash: F,
1048-
pub total_currency: Amount,
1049-
}
1050-
1051-
#[derive(Debug, Clone)]
1052-
pub struct EpochData<F: FieldWitness> {
1053-
pub ledger: EpochLedger<F>,
1054-
pub seed: F,
1055-
pub start_checkpoint: F,
1056-
pub lock_checkpoint: F,
1057-
pub epoch_length: Length,
1058-
}
1059-
1060-
#[derive(Debug, Clone)]
1061-
pub struct ProtocolStateView {
1062-
pub snarked_ledger_hash: Fp,
1063-
pub blockchain_length: Length,
1064-
pub min_window_density: Length,
1065-
pub total_currency: Amount,
1066-
pub global_slot_since_genesis: Slot,
1067-
pub staking_epoch_data: EpochData<Fp>,
1068-
pub next_epoch_data: EpochData<Fp>,
1069-
}
1070-
1071-
/// <https://github.com/MinaProtocol/mina/blob/bfd1009abdbee78979ff0343cc73a3480e862f58/src/lib/mina_state/protocol_state.ml#L180>
1072-
pub fn protocol_state_view(
1073-
state: &MinaStateProtocolStateValueStableV2,
1074-
) -> Result<ProtocolStateView, InvalidBigInt> {
1075-
let MinaStateProtocolStateValueStableV2 {
1076-
previous_state_hash: _,
1077-
body,
1078-
} = state;
1079-
1080-
protocol_state_body_view(body)
1081-
}
1082-
1083-
pub fn protocol_state_body_view(
1084-
body: &v2::MinaStateProtocolStateBodyValueStableV2,
1085-
) -> Result<ProtocolStateView, InvalidBigInt> {
1086-
let cs = &body.consensus_state;
1087-
let sed = &cs.staking_epoch_data;
1088-
let ned = &cs.next_epoch_data;
1089-
1090-
Ok(ProtocolStateView {
1091-
// <https://github.com/MinaProtocol/mina/blob/436023ba41c43a50458a551b7ef7a9ae61670b25/src/lib/mina_state/blockchain_state.ml#L58>
1092-
//
1093-
snarked_ledger_hash: body
1094-
.blockchain_state
1095-
.ledger_proof_statement
1096-
.target
1097-
.first_pass_ledger
1098-
.to_field()?,
1099-
blockchain_length: Length(cs.blockchain_length.as_u32()),
1100-
min_window_density: Length(cs.min_window_density.as_u32()),
1101-
total_currency: Amount(cs.total_currency.as_u64()),
1102-
global_slot_since_genesis: (&cs.global_slot_since_genesis).into(),
1103-
staking_epoch_data: EpochData {
1104-
ledger: EpochLedger {
1105-
hash: sed.ledger.hash.to_field()?,
1106-
total_currency: Amount(sed.ledger.total_currency.as_u64()),
1107-
},
1108-
seed: sed.seed.to_field()?,
1109-
start_checkpoint: sed.start_checkpoint.to_field()?,
1110-
lock_checkpoint: sed.lock_checkpoint.to_field()?,
1111-
epoch_length: Length(sed.epoch_length.as_u32()),
1112-
},
1113-
next_epoch_data: EpochData {
1114-
ledger: EpochLedger {
1115-
hash: ned.ledger.hash.to_field()?,
1116-
total_currency: Amount(ned.ledger.total_currency.as_u64()),
1117-
},
1118-
seed: ned.seed.to_field()?,
1119-
start_checkpoint: ned.start_checkpoint.to_field()?,
1120-
lock_checkpoint: ned.lock_checkpoint.to_field()?,
1121-
epoch_length: Length(ned.epoch_length.as_u32()),
1122-
},
1123-
})
1124-
}
1125-
1126-
pub type GlobalState<L> = GlobalStateSkeleton<L, Signed<Amount>, Slot>;
1127-
1128-
#[derive(Debug, Clone)]
1129-
pub struct GlobalStateSkeleton<L, SignedAmount, Slot> {
1130-
pub first_pass_ledger: L,
1131-
pub second_pass_ledger: L,
1132-
pub fee_excess: SignedAmount,
1133-
pub supply_increase: SignedAmount,
1134-
pub protocol_state: ProtocolStateView,
1135-
/// Slot of block when the transaction is applied.
1136-
/// NOTE: This is at least 1 slot after the protocol_state's view,
1137-
/// which is for the *previous* slot.
1138-
pub block_global_slot: Slot,
1139-
}
1140-
1141-
impl<L: LedgerIntf + Clone> GlobalState<L> {
1142-
pub fn first_pass_ledger(&self) -> L {
1143-
self.first_pass_ledger.create_masked()
1144-
}
1145-
1146-
#[must_use]
1147-
pub fn set_first_pass_ledger(&self, should_update: bool, ledger: L) -> Self {
1148-
let mut this = self.clone();
1149-
if should_update {
1150-
this.first_pass_ledger.apply_mask(ledger);
1151-
}
1152-
this
1153-
}
1154-
1155-
pub fn second_pass_ledger(&self) -> L {
1156-
self.second_pass_ledger.create_masked()
1157-
}
1158-
1159-
#[must_use]
1160-
pub fn set_second_pass_ledger(&self, should_update: bool, ledger: L) -> Self {
1161-
let mut this = self.clone();
1162-
if should_update {
1163-
this.second_pass_ledger.apply_mask(ledger);
1164-
}
1165-
this
1166-
}
1167-
1168-
pub fn fee_excess(&self) -> Signed<Amount> {
1169-
self.fee_excess
1170-
}
1171-
1172-
#[must_use]
1173-
pub fn set_fee_excess(&self, fee_excess: Signed<Amount>) -> Self {
1174-
let mut this = self.clone();
1175-
this.fee_excess = fee_excess;
1176-
this
1177-
}
1178-
1179-
pub fn supply_increase(&self) -> Signed<Amount> {
1180-
self.supply_increase
1181-
}
1182-
1183-
#[must_use]
1184-
pub fn set_supply_increase(&self, supply_increase: Signed<Amount>) -> Self {
1185-
let mut this = self.clone();
1186-
this.supply_increase = supply_increase;
1187-
this
1188-
}
1189-
1190-
pub fn block_global_slot(&self) -> Slot {
1191-
self.block_global_slot
1192-
}
1193-
}
1194-
}
1195-
1038+
pub mod protocol_state;
11961039
pub mod local_state {
11971040
use std::{cell::RefCell, rc::Rc};
11981041

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
use mina_hasher::Fp;
2+
use mina_p2p_messages::{
3+
bigint::InvalidBigInt,
4+
v2::{self, MinaStateProtocolStateValueStableV2},
5+
};
6+
7+
use crate::{
8+
proofs::field::FieldWitness,
9+
scan_state::currency::{Amount, Length, Signed, Slot},
10+
sparse_ledger::LedgerIntf,
11+
};
12+
13+
#[derive(Debug, Clone)]
14+
pub struct EpochLedger<F: FieldWitness> {
15+
pub hash: F,
16+
pub total_currency: Amount,
17+
}
18+
19+
#[derive(Debug, Clone)]
20+
pub struct EpochData<F: FieldWitness> {
21+
pub ledger: EpochLedger<F>,
22+
pub seed: F,
23+
pub start_checkpoint: F,
24+
pub lock_checkpoint: F,
25+
pub epoch_length: Length,
26+
}
27+
28+
#[derive(Debug, Clone)]
29+
pub struct ProtocolStateView {
30+
pub snarked_ledger_hash: Fp,
31+
pub blockchain_length: Length,
32+
pub min_window_density: Length,
33+
pub total_currency: Amount,
34+
pub global_slot_since_genesis: Slot,
35+
pub staking_epoch_data: EpochData<Fp>,
36+
pub next_epoch_data: EpochData<Fp>,
37+
}
38+
39+
/// <https://github.com/MinaProtocol/mina/blob/bfd1009abdbee78979ff0343cc73a3480e862f58/src/lib/mina_state/protocol_state.ml#L180>
40+
pub fn protocol_state_view(
41+
state: &MinaStateProtocolStateValueStableV2,
42+
) -> Result<ProtocolStateView, InvalidBigInt> {
43+
let MinaStateProtocolStateValueStableV2 {
44+
previous_state_hash: _,
45+
body,
46+
} = state;
47+
48+
protocol_state_body_view(body)
49+
}
50+
51+
pub fn protocol_state_body_view(
52+
body: &v2::MinaStateProtocolStateBodyValueStableV2,
53+
) -> Result<ProtocolStateView, InvalidBigInt> {
54+
let cs = &body.consensus_state;
55+
let sed = &cs.staking_epoch_data;
56+
let ned = &cs.next_epoch_data;
57+
58+
Ok(ProtocolStateView {
59+
// <https://github.com/MinaProtocol/mina/blob/436023ba41c43a50458a551b7ef7a9ae61670b25/src/lib/mina_state/blockchain_state.ml#L58>
60+
//
61+
snarked_ledger_hash: body
62+
.blockchain_state
63+
.ledger_proof_statement
64+
.target
65+
.first_pass_ledger
66+
.to_field()?,
67+
blockchain_length: Length(cs.blockchain_length.as_u32()),
68+
min_window_density: Length(cs.min_window_density.as_u32()),
69+
total_currency: Amount(cs.total_currency.as_u64()),
70+
global_slot_since_genesis: (&cs.global_slot_since_genesis).into(),
71+
staking_epoch_data: EpochData {
72+
ledger: EpochLedger {
73+
hash: sed.ledger.hash.to_field()?,
74+
total_currency: Amount(sed.ledger.total_currency.as_u64()),
75+
},
76+
seed: sed.seed.to_field()?,
77+
start_checkpoint: sed.start_checkpoint.to_field()?,
78+
lock_checkpoint: sed.lock_checkpoint.to_field()?,
79+
epoch_length: Length(sed.epoch_length.as_u32()),
80+
},
81+
next_epoch_data: EpochData {
82+
ledger: EpochLedger {
83+
hash: ned.ledger.hash.to_field()?,
84+
total_currency: Amount(ned.ledger.total_currency.as_u64()),
85+
},
86+
seed: ned.seed.to_field()?,
87+
start_checkpoint: ned.start_checkpoint.to_field()?,
88+
lock_checkpoint: ned.lock_checkpoint.to_field()?,
89+
epoch_length: Length(ned.epoch_length.as_u32()),
90+
},
91+
})
92+
}
93+
94+
pub type GlobalState<L> = GlobalStateSkeleton<L, Signed<Amount>, Slot>;
95+
96+
#[derive(Debug, Clone)]
97+
pub struct GlobalStateSkeleton<L, SignedAmount, Slot> {
98+
pub first_pass_ledger: L,
99+
pub second_pass_ledger: L,
100+
pub fee_excess: SignedAmount,
101+
pub supply_increase: SignedAmount,
102+
pub protocol_state: ProtocolStateView,
103+
/// Slot of block when the transaction is applied.
104+
/// NOTE: This is at least 1 slot after the protocol_state's view,
105+
/// which is for the *previous* slot.
106+
pub block_global_slot: Slot,
107+
}
108+
109+
impl<L: LedgerIntf + Clone> GlobalState<L> {
110+
pub fn first_pass_ledger(&self) -> L {
111+
self.first_pass_ledger.create_masked()
112+
}
113+
114+
#[must_use]
115+
pub fn set_first_pass_ledger(&self, should_update: bool, ledger: L) -> Self {
116+
let mut this = self.clone();
117+
if should_update {
118+
this.first_pass_ledger.apply_mask(ledger);
119+
}
120+
this
121+
}
122+
123+
pub fn second_pass_ledger(&self) -> L {
124+
self.second_pass_ledger.create_masked()
125+
}
126+
127+
#[must_use]
128+
pub fn set_second_pass_ledger(&self, should_update: bool, ledger: L) -> Self {
129+
let mut this = self.clone();
130+
if should_update {
131+
this.second_pass_ledger.apply_mask(ledger);
132+
}
133+
this
134+
}
135+
136+
pub fn fee_excess(&self) -> Signed<Amount> {
137+
self.fee_excess
138+
}
139+
140+
#[must_use]
141+
pub fn set_fee_excess(&self, fee_excess: Signed<Amount>) -> Self {
142+
let mut this = self.clone();
143+
this.fee_excess = fee_excess;
144+
this
145+
}
146+
147+
pub fn supply_increase(&self) -> Signed<Amount> {
148+
self.supply_increase
149+
}
150+
151+
#[must_use]
152+
pub fn set_supply_increase(&self, supply_increase: Signed<Amount>) -> Self {
153+
let mut this = self.clone();
154+
this.supply_increase = supply_increase;
155+
this
156+
}
157+
158+
pub fn block_global_slot(&self) -> Slot {
159+
self.block_global_slot
160+
}
161+
}

0 commit comments

Comments
 (0)