1919 WormholeMerkleState ,
2020 } ,
2121 crate :: {
22- api:: types:: RpcPriceIdentifier ,
2322 network:: wormhole:: VaaBytes ,
2423 state:: {
2524 benchmarks:: Benchmarks ,
@@ -123,17 +122,29 @@ pub struct AggregateStateData {
123122 /// probes.
124123 pub latest_observed_slot : Option < Slot > ,
125124
125+ /// The duration of no aggregation after which the readiness of the state is considered stale.
126+ pub readiness_staleness_threshold : Duration ,
127+
128+ /// The maximum allowed slot lag between the latest observed slot and the latest completed slot.
129+ pub readiness_max_allowed_slot_lag : Slot ,
130+
126131 /// Aggregate Specific Metrics
127132 pub metrics : metrics:: Metrics ,
128133}
129134
130135impl AggregateStateData {
131- pub fn new ( metrics_registry : & mut Registry ) -> Self {
136+ pub fn new (
137+ readiness_staleness_threshold : Duration ,
138+ readiness_max_allowed_slot_lag : Slot ,
139+ metrics_registry : & mut Registry ,
140+ ) -> Self {
132141 Self {
133- latest_completed_slot : None ,
142+ latest_completed_slot : None ,
134143 latest_completed_update_at : None ,
135- latest_observed_slot : None ,
136- metrics : metrics:: Metrics :: new ( metrics_registry) ,
144+ latest_observed_slot : None ,
145+ metrics : metrics:: Metrics :: new ( metrics_registry) ,
146+ readiness_staleness_threshold,
147+ readiness_max_allowed_slot_lag,
137148 }
138149 }
139150}
@@ -144,9 +155,18 @@ pub struct AggregateState {
144155}
145156
146157impl AggregateState {
147- pub fn new ( update_tx : Sender < AggregationEvent > , metrics_registry : & mut Registry ) -> Self {
158+ pub fn new (
159+ update_tx : Sender < AggregationEvent > ,
160+ readiness_staleness_threshold : Duration ,
161+ readiness_max_allowed_slot_lag : Slot ,
162+ metrics_registry : & mut Registry ,
163+ ) -> Self {
148164 Self {
149- data : RwLock :: new ( AggregateStateData :: new ( metrics_registry) ) ,
165+ data : RwLock :: new ( AggregateStateData :: new (
166+ readiness_staleness_threshold,
167+ readiness_max_allowed_slot_lag,
168+ metrics_registry,
169+ ) ) ,
150170 api_update_tx : update_tx,
151171 }
152172 }
@@ -193,12 +213,6 @@ pub struct PriceFeedsWithUpdateData {
193213 pub update_data : Vec < Vec < u8 > > ,
194214}
195215
196- const READINESS_STALENESS_THRESHOLD : Duration = Duration :: from_secs ( 30 ) ;
197-
198- /// The maximum allowed slot lag between the latest observed slot and the latest completed slot.
199- /// 10 slots is almost 5 seconds.
200- const READINESS_MAX_ALLOWED_SLOT_LAG : Slot = 10 ;
201-
202216#[ async_trait:: async_trait]
203217pub trait Aggregates
204218where
@@ -388,24 +402,25 @@ where
388402 }
389403
390404 async fn is_ready ( & self ) -> bool {
391- let metadata = self . into ( ) . data . read ( ) . await ;
405+ let state_data = self . into ( ) . data . read ( ) . await ;
392406 let price_feeds_metadata = PriceFeedMeta :: retrieve_price_feeds_metadata ( self )
393407 . await
394408 . unwrap ( ) ;
395409
396- let has_completed_recently = match metadata . latest_completed_update_at . as_ref ( ) {
410+ let has_completed_recently = match state_data . latest_completed_update_at . as_ref ( ) {
397411 Some ( latest_completed_update_time) => {
398- latest_completed_update_time. elapsed ( ) < READINESS_STALENESS_THRESHOLD
412+ latest_completed_update_time. elapsed ( ) < state_data . readiness_staleness_threshold
399413 }
400414 None => false ,
401415 } ;
402416
403417 let is_not_behind = match (
404- metadata . latest_completed_slot ,
405- metadata . latest_observed_slot ,
418+ state_data . latest_completed_slot ,
419+ state_data . latest_observed_slot ,
406420 ) {
407421 ( Some ( latest_completed_slot) , Some ( latest_observed_slot) ) => {
408- latest_observed_slot - latest_completed_slot <= READINESS_MAX_ALLOWED_SLOT_LAG
422+ latest_observed_slot - latest_completed_slot
423+ <= state_data. readiness_max_allowed_slot_lag
409424 }
410425 _ => false ,
411426 } ;
@@ -512,7 +527,10 @@ mod test {
512527 use {
513528 super :: * ,
514529 crate :: {
515- api:: types:: PriceFeedMetadata ,
530+ api:: types:: {
531+ PriceFeedMetadata ,
532+ RpcPriceIdentifier ,
533+ } ,
516534 state:: test:: setup_state,
517535 } ,
518536 futures:: future:: join_all,
@@ -881,8 +899,9 @@ mod test {
881899 assert ! ( state. is_ready( ) . await ) ;
882900
883901 // Advance the clock to make the prices stale
884- MockClock :: advance_system_time ( READINESS_STALENESS_THRESHOLD ) ;
885- MockClock :: advance ( READINESS_STALENESS_THRESHOLD ) ;
902+ let staleness_threshold = Duration :: from_secs ( 30 ) ;
903+ MockClock :: advance_system_time ( staleness_threshold) ;
904+ MockClock :: advance ( staleness_threshold) ;
886905 // Check the state is not ready
887906 assert ! ( !state. is_ready( ) . await ) ;
888907 }
0 commit comments