node/transition_frontier/genesis/
transition_frontier_genesis_reducer.rs1use crate::{
2 account::AccountSecretKey, block_producer::calc_epoch_seed,
3 transition_frontier::genesis_effectful::TransitionFrontierGenesisEffectfulAction,
4};
5use ledger::{
6 dummy::dummy_blockchain_proof, scan_state::transaction_logic::local_state::LocalState,
7};
8use mina_p2p_messages::v2;
9use openmina_core::{
10 block::{genesis::genesis_and_negative_one_protocol_states, BlockWithHash},
11 constants::PROTOCOL_VERSION,
12 error,
13};
14use p2p::P2pInitializeAction;
15
16use super::{
17 empty_block_body, empty_block_body_hash, empty_pending_coinbase, empty_pending_coinbase_hash,
18 TransitionFrontierGenesisAction, TransitionFrontierGenesisActionWithMetaRef,
19 TransitionFrontierGenesisState,
20};
21
22impl TransitionFrontierGenesisState {
23 pub fn reducer(
24 mut state_context: crate::Substate<Self>,
25 action: TransitionFrontierGenesisActionWithMetaRef<'_>,
26 ) {
27 let Ok(state) = state_context.get_substate_mut() else {
28 return;
30 };
31 let (action, meta) = action.split();
32
33 match action {
34 TransitionFrontierGenesisAction::LedgerLoadInit => {
35 let (dispatcher, global_state) = state_context.into_dispatcher_and_state();
36 let config = global_state.transition_frontier.config.genesis.clone();
37
38 dispatcher.push(TransitionFrontierGenesisAction::LedgerLoadPending);
39 dispatcher
40 .push(TransitionFrontierGenesisEffectfulAction::LedgerLoadInit { config });
41 }
42 TransitionFrontierGenesisAction::LedgerLoadPending => {
43 *state = Self::LedgerLoadPending { time: meta.time() };
44 }
45 TransitionFrontierGenesisAction::LedgerLoadSuccess { data } => {
46 *state = Self::LedgerLoadSuccess {
47 time: meta.time(),
48 data: data.clone(),
49 };
50
51 let dispatcher = state_context.into_dispatcher();
53 dispatcher.push(TransitionFrontierGenesisAction::Produce);
55 }
56 TransitionFrontierGenesisAction::Produce => {
57 let Self::LedgerLoadSuccess { data, .. } = state else {
58 return;
59 };
60
61 let genesis_vrf = ::vrf::genesis_vrf(data.staking_epoch_seed.clone()).unwrap();
62 let genesis_vrf_hash = genesis_vrf.hash();
63
64 let Ok((negative_one, genesis, genesis_hash)) =
65 genesis_and_negative_one_protocol_states(
66 data.constants.clone(),
67 data.genesis_ledger_hash.clone(),
68 data.genesis_total_currency.clone(),
69 data.staking_epoch_ledger_hash.clone(),
70 data.staking_epoch_total_currency.clone(),
71 data.next_epoch_ledger_hash.clone(),
72 data.next_epoch_total_currency.clone(),
73 AccountSecretKey::genesis_producer().public_key().into(),
74 empty_pending_coinbase_hash(),
75 (&LocalState::dummy()).into(),
76 empty_block_body_hash(),
77 genesis_vrf.into(),
78 data.staking_epoch_seed.clone(),
79 data.next_epoch_seed.clone(),
80 calc_epoch_seed(&data.next_epoch_seed, genesis_vrf_hash), )
82 else {
83 error!(meta.time(); "invalid negative protocol state");
84 return;
85 };
86
87 *state = Self::Produced {
88 time: meta.time(),
89 negative_one,
90 genesis,
91 genesis_hash,
92 genesis_producer_stake_proof: data.genesis_producer_stake_proof.clone(),
93 };
94
95 let (dispatcher, global_state) = state_context.into_dispatcher_and_state();
97 if global_state.p2p.ready().is_none() {
98 let TransitionFrontierGenesisState::Produced {
99 genesis,
100 genesis_hash,
101 ..
102 } = &global_state.transition_frontier.genesis
103 else {
104 error!(meta.time(); "incorrect state: {:?}", global_state.transition_frontier.genesis);
105 return;
106 };
107 use openmina_core::{constants, ChainId};
108 let constraint_system_digests =
109 openmina_core::NetworkConfig::global().constraint_system_digests;
110 let chain_id = ChainId::compute(
111 constraint_system_digests,
112 genesis_hash,
113 &genesis.body.constants,
114 constants::PROTOCOL_TRANSACTION_VERSION,
115 constants::PROTOCOL_NETWORK_VERSION,
116 &v2::UnsignedExtendedUInt32StableV1::from(constants::TX_POOL_MAX_SIZE),
117 );
118 dispatcher.push(P2pInitializeAction::Initialize { chain_id });
119 }
120 dispatcher.push(TransitionFrontierGenesisAction::ProveInit);
121 }
122 TransitionFrontierGenesisAction::ProveInit => {
123 let TransitionFrontierGenesisState::Produced {
124 negative_one,
125 genesis,
126 genesis_hash,
127 genesis_producer_stake_proof,
128 ..
129 } = state
130 else {
131 return;
132 };
133
134 let genesis_hash = genesis_hash.clone();
135 let producer_pk = genesis.body.consensus_state.block_creator.clone();
136 let delegator_pk = genesis.body.consensus_state.block_stake_winner.clone();
137
138 let input = v2::ProverExtendBlockchainInputStableV2 {
139 chain: v2::BlockchainSnarkBlockchainStableV2 {
140 state: negative_one.clone(),
141 proof: dummy_blockchain_proof().clone(),
142 },
143 next_state: genesis.clone(),
144 block: v2::MinaStateSnarkTransitionValueStableV2 {
145 blockchain_state: genesis.body.blockchain_state.clone(),
146 consensus_transition: genesis
147 .body
148 .consensus_state
149 .curr_global_slot_since_hard_fork
150 .slot_number
151 .clone(),
152 pending_coinbase_update: v2::MinaBasePendingCoinbaseUpdateStableV1::zero(),
153 },
154 ledger_proof: None,
155 prover_state: v2::ConsensusStakeProofStableV2 {
156 delegator: v2::MinaBaseAccountIndexStableV1(0u64.into()),
157 delegator_pk,
158 coinbase_receiver_pk: genesis
159 .body
160 .consensus_state
161 .coinbase_receiver
162 .clone(),
163 producer_public_key: producer_pk,
164 producer_private_key: AccountSecretKey::genesis_producer().into(),
165 ledger: genesis_producer_stake_proof.clone(),
166 },
167 pending_coinbase: v2::MinaBasePendingCoinbaseWitnessStableV2 {
168 pending_coinbases: (&empty_pending_coinbase()).into(),
169 is_new_stack: true,
170 },
171 };
172
173 let dispatcher = state_context.into_dispatcher();
175
176 dispatcher.push(TransitionFrontierGenesisAction::ProvePending);
177 dispatcher.push(TransitionFrontierGenesisEffectfulAction::ProveInit {
178 block_hash: genesis_hash,
179 input: input.into(),
180 });
181 }
182 TransitionFrontierGenesisAction::ProvePending => {
183 let Self::Produced {
184 negative_one,
185 genesis,
186 genesis_hash,
187 genesis_producer_stake_proof,
188 ..
189 } = state
190 else {
191 return;
192 };
193
194 *state = Self::ProvePending {
195 time: meta.time(),
196 negative_one: negative_one.clone(),
197 genesis: genesis.clone(),
198 genesis_hash: genesis_hash.clone(),
199 genesis_producer_stake_proof: genesis_producer_stake_proof.clone(),
200 };
201 }
202 TransitionFrontierGenesisAction::ProveSuccess { proof } => {
203 let Self::ProvePending {
204 genesis,
205 genesis_hash,
206 ..
207 } = state
208 else {
209 return;
210 };
211
212 let block = v2::MinaBlockBlockStableV2 {
213 header: v2::MinaBlockHeaderStableV2 {
214 protocol_state: genesis.clone(),
215 protocol_state_proof: proof.clone(),
216 delta_block_chain_proof: (
217 genesis_hash.clone(),
218 std::iter::empty().collect(),
219 ),
220 current_protocol_version: PROTOCOL_VERSION.clone(),
221 proposed_protocol_version_opt: None,
222 },
223 body: v2::StagedLedgerDiffBodyStableV1 {
224 staged_ledger_diff: empty_block_body(),
225 },
226 };
227
228 let Ok(genesis) = BlockWithHash::try_new(block.into()) else {
229 error!(meta.time(); "invalid `genesis` block");
230 return;
231 };
232
233 *state = Self::ProveSuccess {
234 time: meta.time(),
235 genesis,
236 };
237 }
238 }
239 }
240}