Переглянути джерело

p2w-client: Improve local symbol state handling, min interval=60s

With this change, we update local state only if we meet a
condition. Additionally, the publish_time change becomes a
configurable minimal delta.

commit-id:f8139cd6
Stan Drozd 3 роки тому
батько
коміт
8e9223bfb3

+ 13 - 7
solana/pyth2wormhole/client/src/attestation_cfg.rs

@@ -26,22 +26,28 @@ pub struct SymbolGroup {
     pub symbols: Vec<P2WSymbol>,
 }
 
+pub const fn DEFAULT_MIN_INTERVAL_SECS() -> u64 {
+    60
+}
+
 /// Spontaneous attestation triggers. Attestation is triggered if any
 /// of the active conditions is met. Option<> fields can be
-/// de-activated with None. All conditions are inactive by default.
+/// de-activated with None. All conditions are inactive by default,
+/// except for min_interval_secs set to 1 minute.
 #[derive(Clone, Default, Debug, Deserialize, Serialize, PartialEq)]
 pub struct AttestationConditions {
     /// Baseline, unconditional attestation interval. Attestation is triggered if the specified interval elapsed since last attestation.
-    #[serde(default)]
-    pub min_interval_secs: Option<u64>,
+    #[serde(default = "DEFAULT_MIN_INTERVAL_SECS")]
+    pub min_interval_secs: u64,
 
     /// Trigger attestation if price changes by the specified percentage.
     #[serde(default)]
     pub price_changed_pct: Option<f64>,
 
-    /// Trigger attestation if publish_time changes
+    /// Trigger attestation if publish_time advances at least the
+    /// specified amount.
     #[serde(default)]
-    pub publish_time_changed: bool,
+    pub publish_time_min_delta_secs: Option<u64>,
 }
 
 /// Config entry for a Pyth product + price pair
@@ -99,7 +105,7 @@ mod tests {
         let fastbois = SymbolGroup {
             group_name: "fast bois".to_owned(),
             conditions: AttestationConditions {
-                min_interval_secs: Some(5),
+                min_interval_secs: 5,
                 ..Default::default()
             },
             symbols: vec![
@@ -117,7 +123,7 @@ mod tests {
         let slowbois = SymbolGroup {
             group_name: "slow bois".to_owned(),
             conditions: AttestationConditions {
-                min_interval_secs: Some(200),
+                min_interval_secs: 200,
                 ..Default::default()
             },
             symbols: vec![

+ 18 - 15
solana/pyth2wormhole/client/src/batch_state.rs

@@ -94,13 +94,13 @@ impl<'a> BatchState<'a> {
         }
 
         // min interval
-        if let Some(i) = self.conditions.min_interval_secs.as_ref() {
-            if self.get_status_changed_at().elapsed() > Duration::from_secs(*i) {
-                ret = Some(format!(
-                    "minimum interval of {}s elapsed since last state change",
-                    i
-                ));
-            }
+        if self.get_status_changed_at().elapsed()
+            > Duration::from_secs(self.conditions.min_interval_secs)
+        {
+            ret = Some(format!(
+                "minimum interval of {}s elapsed since last state change",
+                self.conditions.min_interval_secs
+            ));
         }
 
         for (idx, old_new_tup) in self
@@ -109,16 +109,19 @@ impl<'a> BatchState<'a> {
             .zip(new_symbol_states.iter())
             .enumerate()
         {
-            //  Only evaluate if a triggering condition is not already met
+            //  Only evaluate this symbol if a triggering condition is not already met
             if ret.is_none() {
                 match old_new_tup {
                     (Some(old), Some(new)) => {
                         // publish_time_changed
-                        if self.conditions.publish_time_changed && new.timestamp > old.timestamp {
-                            ret = Some(format!(
-                                "publish_time advanced for {:?}",
-                                self.symbols[idx].to_string(),
-                            ))
+                        if let Some(min_delta_secs) = self.conditions.publish_time_min_delta_secs {
+                            if new.timestamp - old.timestamp > min_delta_secs as i64 {
+                                ret = Some(format!(
+                                    "publish_time advanced by at least {}s for {:?}",
+                                    min_delta_secs,
+                                    self.symbols[idx].to_string(),
+                                ))
+                            }
 
                         // price_changed_pct
                         } else if let Some(pct) = self.conditions.price_changed_pct {
@@ -148,8 +151,8 @@ impl<'a> BatchState<'a> {
                 }
             }
 
-            // Update with newer state if available, regardless of condition status
-            if old_new_tup.1.is_some() {
+            // Update with newer state if a condition was met (including this iteration, hence not in an else)
+            if ret.is_some() {
                 *old_new_tup.0 = *old_new_tup.1;
             }
         }