Skip to content

Commit 02de296

Browse files
ali-behjatiReisen
authored andcommitted
Add vaa cache
1 parent 04806a5 commit 02de296

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

hermes/src/store.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ use {
2626
ProofSet,
2727
UnixTimestamp,
2828
},
29-
wormhole::parse_and_verify_vaa,
29+
wormhole::verify_vaa,
3030
},
3131
anyhow::{
3232
anyhow,
3333
Result,
3434
},
35+
moka::future::Cache,
3536
pyth_oracle::{
3637
Message,
3738
MessageType,
@@ -45,6 +46,7 @@ use {
4546
collections::HashSet,
4647
sync::Arc,
4748
time::{
49+
Duration,
4850
SystemTime,
4951
UNIX_EPOCH,
5052
},
@@ -57,6 +59,7 @@ use {
5759
Address,
5860
Chain,
5961
GuardianAddress,
62+
Vaa,
6063
},
6164
};
6265

@@ -66,15 +69,20 @@ pub mod types;
6669
pub mod wormhole;
6770

6871
pub struct Store {
69-
pub storage: StorageInstance,
70-
pub guardian_set: RwLock<Option<Vec<GuardianAddress>>>,
71-
pub update_tx: Sender<()>,
72+
pub storage: StorageInstance,
73+
pub observed_vaa_seqs: Cache<u64, bool>,
74+
pub guardian_set: RwLock<Option<Vec<GuardianAddress>>>,
75+
pub update_tx: Sender<()>,
7276
}
7377

7478
impl Store {
7579
pub fn new_with_local_cache(update_tx: Sender<()>, cache_size: u64) -> Arc<Self> {
7680
Arc::new(Self {
7781
storage: storage::local_storage::LocalStorage::new_instance(cache_size),
82+
observed_vaa_seqs: Cache::builder()
83+
.max_capacity(cache_size)
84+
.time_to_live(Duration::from_secs(60 * 5))
85+
.build(),
7886
guardian_set: RwLock::new(None),
7987
update_tx,
8088
})
@@ -84,22 +92,32 @@ impl Store {
8492
pub async fn store_update(&self, update: Update) -> Result<()> {
8593
let slot = match update {
8694
Update::Vaa(vaa_bytes) => {
87-
let body = parse_and_verify_vaa(self, &vaa_bytes).await;
88-
let body = match body {
89-
Ok(body) => body,
95+
let vaa =
96+
serde_wormhole::from_slice::<Vaa<&serde_wormhole::RawMessage>>(&vaa_bytes)?;
97+
98+
if vaa.emitter_chain != Chain::Pythnet
99+
|| vaa.emitter_address != Address(pythnet_sdk::ACCUMULATOR_EMITTER_ADDRESS)
100+
{
101+
return Ok(()); // Ignore VAA from other emitters
102+
}
103+
104+
if self.observed_vaa_seqs.get(&vaa.sequence).is_some() {
105+
return Ok(()); // Ignore VAA if we have already seen it
106+
}
107+
108+
let vaa = verify_vaa(self, vaa).await;
109+
110+
let vaa = match vaa {
111+
Ok(vaa) => vaa,
90112
Err(err) => {
91113
log::info!("Ignoring invalid VAA: {:?}", err);
92114
return Ok(());
93115
}
94116
};
95117

96-
if body.emitter_chain != Chain::Pythnet
97-
|| body.emitter_address != Address(pythnet_sdk::ACCUMULATOR_EMITTER_ADDRESS)
98-
{
99-
return Ok(()); // Ignore VAA from other emitters
100-
}
118+
self.observed_vaa_seqs.insert(vaa.sequence, true).await;
101119

102-
match WormholeMessage::try_from_bytes(body.payload)?.payload {
120+
match WormholeMessage::try_from_bytes(vaa.payload)?.payload {
103121
WormholePayload::Merkle(proof) => {
104122
log::info!("Storing merkle proof for slot {:?}", proof.slot,);
105123
store_wormhole_merkle_verified_message(self, proof.clone(), vaa_bytes)

hermes/src/store/wormhole.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ use {
2828
};
2929

3030
/// Parses and verifies a VAA to ensure it is signed by the Wormhole guardian set.
31-
pub async fn parse_and_verify_vaa<'a>(
31+
pub async fn verify_vaa<'a>(
3232
store: &Store,
33-
vaa_bytes: &'a [u8],
34-
) -> Result<Body<&'a RawMessage>> {
35-
let vaa = serde_wormhole::from_slice::<Vaa<&serde_wormhole::RawMessage>>(vaa_bytes)?;
33+
vaa: Vaa<&'a RawMessage>,
34+
) -> Result<Vaa<&'a RawMessage>> {
3635
let (header, body): (Header, Body<&RawMessage>) = vaa.into();
3736
let digest = body.digest()?;
3837

@@ -83,5 +82,5 @@ pub async fn parse_and_verify_vaa<'a>(
8382
));
8483
}
8584

86-
Ok(body)
85+
Ok((header, body).into())
8786
}

0 commit comments

Comments
 (0)