22//! Manages stake and reward accounts state
33
44use acropolis_common:: {
5- messages:: { CardanoMessage , Message , StateQuery , StateQueryResponse } ,
5+ caryatid:: SubscriptionExt ,
6+ messages:: { CardanoMessage , Message , StateQuery , StateQueryResponse , StateTransitionMessage } ,
67 queries:: accounts:: { DrepDelegators , PoolDelegators , DEFAULT_ACCOUNTS_QUERY_TOPIC } ,
78 state_history:: { StateHistory , StateHistoryStore } ,
89 BlockInfo , BlockStatus ,
@@ -120,17 +121,13 @@ impl AccountsState {
120121 // Get a mutable state
121122 let mut state = history. lock ( ) . await . get_current_state ( ) ;
122123
123- // Read per-block topics in parallel
124- let certs_message_f = certs_subscription. read ( ) ;
125- let stake_message_f = stake_subscription. read ( ) ;
126- let withdrawals_message_f = withdrawals_subscription. read ( ) ;
127124 let mut current_block: Option < BlockInfo > = None ;
128125
129126 // Use certs_message as the synchroniser, but we have to handle it after the
130127 // epoch things, because they apply to the new epoch, not the last
131- let ( _, certs_message) = certs_message_f . await ?;
128+ let ( _, certs_message) = certs_subscription . read ( ) . await ?;
132129 let new_epoch = match certs_message. as_ref ( ) {
133- Message :: Cardano ( ( block_info, _ ) ) => {
130+ Message :: Cardano ( ( block_info, CardanoMessage :: TxCertificates ( _ ) ) ) => {
134131 // Handle rollbacks on this topic only
135132 if block_info. status == BlockStatus :: RolledBack {
136133 state = history. lock ( ) . await . get_rolled_back_state ( block_info. number ) ;
@@ -139,6 +136,16 @@ impl AccountsState {
139136 current_block = Some ( block_info. clone ( ) ) ;
140137 block_info. new_epoch && block_info. epoch > 0
141138 }
139+ Message :: Cardano ( (
140+ _,
141+ CardanoMessage :: StateTransition ( StateTransitionMessage :: Rollback ( _) ) ,
142+ ) ) => {
143+ drep_publisher. publish_rollback ( certs_message. clone ( ) ) . await ?;
144+ spo_publisher. publish_rollback ( certs_message. clone ( ) ) . await ?;
145+ spo_rewards_publisher. publish_rollback ( certs_message. clone ( ) ) . await ?;
146+ stake_reward_deltas_publisher. publish_rollback ( certs_message. clone ( ) ) . await ?;
147+ false
148+ }
142149 _ => false ,
143150 } ;
144151
@@ -149,18 +156,13 @@ impl AccountsState {
149156
150157 // Read from epoch-boundary messages only when it's a new epoch
151158 if new_epoch {
152- let dreps_message_f = drep_state_subscription. read ( ) ;
153- let spos_message_f = spos_subscription. read ( ) ;
154- let ea_message_f = ea_subscription. read ( ) ;
155- let params_message_f = parameters_subscription. read ( ) ;
156-
157159 let spdd_store_guard = match spdd_store. as_ref ( ) {
158160 Some ( s) => Some ( s. lock ( ) . await ) ,
159161 None => None ,
160162 } ;
161163
162164 // Handle DRep
163- let ( _, message) = dreps_message_f . await ?;
165+ let ( _, message) = drep_state_subscription . read_ignoring_rollbacks ( ) . await ?;
164166 match message. as_ref ( ) {
165167 Message :: Cardano ( ( block_info, CardanoMessage :: DRepState ( dreps_msg) ) ) => {
166168 let span = info_span ! (
@@ -184,7 +186,7 @@ impl AccountsState {
184186 }
185187
186188 // Handle SPOs
187- let ( _, message) = spos_message_f . await ?;
189+ let ( _, message) = spos_subscription . read_ignoring_rollbacks ( ) . await ?;
188190 match message. as_ref ( ) {
189191 Message :: Cardano ( ( block_info, CardanoMessage :: SPOState ( spo_msg) ) ) => {
190192 let span =
@@ -219,7 +221,7 @@ impl AccountsState {
219221 _ => error ! ( "Unexpected message type: {message:?}" ) ,
220222 }
221223
222- let ( _, message) = params_message_f . await ?;
224+ let ( _, message) = parameters_subscription . read_ignoring_rollbacks ( ) . await ?;
223225 match message. as_ref ( ) {
224226 Message :: Cardano ( ( block_info, CardanoMessage :: ProtocolParams ( params_msg) ) ) => {
225227 let span = info_span ! (
@@ -241,7 +243,7 @@ impl AccountsState {
241243 }
242244
243245 // Handle epoch activity
244- let ( _, message) = ea_message_f . await ?;
246+ let ( _, message) = ea_subscription . read_ignoring_rollbacks ( ) . await ?;
245247 match message. as_ref ( ) {
246248 Message :: Cardano ( ( block_info, CardanoMessage :: EpochActivity ( ea_msg) ) ) => {
247249 let span = info_span ! (
@@ -297,11 +299,18 @@ impl AccountsState {
297299 . await ;
298300 }
299301
302+ Message :: Cardano ( (
303+ _,
304+ CardanoMessage :: StateTransition ( StateTransitionMessage :: Rollback ( _) ) ,
305+ ) ) => {
306+ // Ignore this, we already handled rollbacks
307+ }
308+
300309 _ => error ! ( "Unexpected message type: {certs_message:?}" ) ,
301310 }
302311
303312 // Handle withdrawals
304- let ( _, message) = withdrawals_message_f . await ?;
313+ let ( _, message) = withdrawals_subscription . read_ignoring_rollbacks ( ) . await ?;
305314 match message. as_ref ( ) {
306315 Message :: Cardano ( ( block_info, CardanoMessage :: Withdrawals ( withdrawals_msg) ) ) => {
307316 let span = info_span ! (
@@ -323,7 +332,7 @@ impl AccountsState {
323332 }
324333
325334 // Handle stake address deltas
326- let ( _, message) = stake_message_f . await ?;
335+ let ( _, message) = stake_subscription . read_ignoring_rollbacks ( ) . await ?;
327336 match message. as_ref ( ) {
328337 Message :: Cardano ( ( block_info, CardanoMessage :: StakeAddressDeltas ( deltas_msg) ) ) => {
329338 let span = info_span ! (
0 commit comments