Skip to content

Commit 3d1689c

Browse files
committed
replace dedup with dedup_by_key
1 parent fd94b29 commit 3d1689c

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
relayer_urls = ["ws://localhost:10001/v1/transaction"]
2-
publish_keypair_path = "/tmp/keypair.json"
1+
relayer_urls = ["wss://relayer.pyth-lazer-staging.dourolabs.app/v1/transaction", "wss://relayer-1.pyth-lazer-staging.dourolabs.app/v1/transaction"]
2+
publish_keypair_path = "/path/to/solana/id.json"
33
listen_address = "0.0.0.0:8910"
44
publish_interval_duration = "25ms"
55
authorization_token="token1"

apps/pyth-lazer-agent/src/lazer_publisher.rs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use pyth_lazer_publisher_sdk::transaction::{
1313
Ed25519SignatureData, LazerTransaction, SignatureData, SignedLazerTransaction,
1414
};
1515
use solana_keypair::read_keypair_file;
16-
use std::collections::HashMap;
1716
use std::path::PathBuf;
1817
use std::sync::Arc;
1918
use std::sync::atomic::AtomicBool;
@@ -133,11 +132,10 @@ impl LazerPublisherTask {
133132
return Ok(());
134133
}
135134

136-
let updates = if self.config.enable_update_deduplication {
137-
deduplicate_feed_updates(&self.pending_updates.drain(..).collect())?
138-
} else {
139-
self.pending_updates.drain(..).collect()
140-
};
135+
let mut updates = self.pending_updates.drain(..).collect();
136+
if self.config.enable_update_deduplication {
137+
deduplicate_feed_updates(&mut updates);
138+
}
141139

142140
let publisher_update = PublisherUpdate {
143141
updates,
@@ -180,27 +178,9 @@ impl LazerPublisherTask {
180178
}
181179
}
182180

183-
fn deduplicate_feed_updates(feed_updates: &Vec<FeedUpdate>) -> Result<Vec<FeedUpdate>> {
184-
let mut deduped_feed_updates = Vec::new();
185-
let mut last_feed_update = HashMap::new();
186-
187-
// assume that feed_updates is already sorted by ts (within feed_update_id groups)
188-
for feed_update in feed_updates {
189-
let feed_id = feed_update.feed_id.context("feed_id is required")?;
190-
191-
if let Some(update) = feed_update.update.as_ref() {
192-
if let Some(last_update) = last_feed_update.get(&feed_id) {
193-
if update == last_update {
194-
continue;
195-
}
196-
}
197-
198-
deduped_feed_updates.push(feed_update.clone());
199-
last_feed_update.insert(feed_id, update.clone());
200-
}
201-
}
202-
203-
Ok(deduped_feed_updates)
181+
fn deduplicate_feed_updates(feed_updates: &mut Vec<FeedUpdate>) {
182+
// assume that feed_updates is already sorted by timestamp for each feed_update.feed_id
183+
feed_updates.dedup_by_key(|feed_update| (feed_update.feed_id, feed_update.update.clone()));
204184
}
205185

206186
#[cfg(test)]
@@ -330,7 +310,7 @@ mod tests {
330310
// - (6, 10)
331311
// we should only return (1, 10), (4, 15), (6, 10)
332312

333-
let updates = vec![
313+
let updates = &mut vec![
334314
test_feed_update(1, TimestampUs::from_millis(1).unwrap(), 10),
335315
test_feed_update(1, TimestampUs::from_millis(2).unwrap(), 10),
336316
test_feed_update(1, TimestampUs::from_millis(3).unwrap(), 10),
@@ -345,15 +325,13 @@ mod tests {
345325
test_feed_update(1, TimestampUs::from_millis(6).unwrap(), 10),
346326
];
347327

348-
assert_eq!(
349-
deduplicate_feed_updates(&updates).unwrap(),
350-
expected_updates
351-
);
328+
deduplicate_feed_updates(updates);
329+
assert_eq!(updates.to_vec(), expected_updates);
352330
}
353331

354332
#[test]
355333
fn test_deduplicate_feed_updates_multiple_feeds() {
356-
let updates = vec![
334+
let updates = &mut vec![
357335
test_feed_update(1, TimestampUs::from_millis(1).unwrap(), 10),
358336
test_feed_update(1, TimestampUs::from_millis(2).unwrap(), 10),
359337
test_feed_update(1, TimestampUs::from_millis(3).unwrap(), 10),
@@ -368,9 +346,7 @@ mod tests {
368346
test_feed_update(2, TimestampUs::from_millis(6).unwrap(), 10),
369347
];
370348

371-
assert_eq!(
372-
deduplicate_feed_updates(&updates).unwrap(),
373-
expected_updates
374-
);
349+
deduplicate_feed_updates(updates);
350+
assert_eq!(updates.to_vec(), expected_updates);
375351
}
376352
}

0 commit comments

Comments
 (0)