19
19
WormholeMerkleState ,
20
20
} ,
21
21
crate :: {
22
- api:: types:: RpcPriceIdentifier ,
23
22
network:: wormhole:: VaaBytes ,
24
23
state:: {
25
24
benchmarks:: Benchmarks ,
@@ -123,17 +122,29 @@ pub struct AggregateStateData {
123
122
/// probes.
124
123
pub latest_observed_slot : Option < Slot > ,
125
124
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
+
126
131
/// Aggregate Specific Metrics
127
132
pub metrics : metrics:: Metrics ,
128
133
}
129
134
130
135
impl 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 {
132
141
Self {
133
- latest_completed_slot : None ,
142
+ latest_completed_slot : None ,
134
143
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,
137
148
}
138
149
}
139
150
}
@@ -144,9 +155,18 @@ pub struct AggregateState {
144
155
}
145
156
146
157
impl 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 {
148
164
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
+ ) ) ,
150
170
api_update_tx : update_tx,
151
171
}
152
172
}
@@ -193,12 +213,6 @@ pub struct PriceFeedsWithUpdateData {
193
213
pub update_data : Vec < Vec < u8 > > ,
194
214
}
195
215
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
-
202
216
#[ async_trait:: async_trait]
203
217
pub trait Aggregates
204
218
where
@@ -388,24 +402,25 @@ where
388
402
}
389
403
390
404
async fn is_ready ( & self ) -> bool {
391
- let metadata = self . into ( ) . data . read ( ) . await ;
405
+ let state_data = self . into ( ) . data . read ( ) . await ;
392
406
let price_feeds_metadata = PriceFeedMeta :: retrieve_price_feeds_metadata ( self )
393
407
. await
394
408
. unwrap ( ) ;
395
409
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 ( ) {
397
411
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
399
413
}
400
414
None => false ,
401
415
} ;
402
416
403
417
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 ,
406
420
) {
407
421
( 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
409
424
}
410
425
_ => false ,
411
426
} ;
@@ -512,7 +527,10 @@ mod test {
512
527
use {
513
528
super :: * ,
514
529
crate :: {
515
- api:: types:: PriceFeedMetadata ,
530
+ api:: types:: {
531
+ PriceFeedMetadata ,
532
+ RpcPriceIdentifier ,
533
+ } ,
516
534
state:: test:: setup_state,
517
535
} ,
518
536
futures:: future:: join_all,
@@ -881,8 +899,9 @@ mod test {
881
899
assert ! ( state. is_ready( ) . await ) ;
882
900
883
901
// 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) ;
886
905
// Check the state is not ready
887
906
assert ! ( !state. is_ready( ) . await ) ;
888
907
}
0 commit comments