Skip to content

Commit ec5ad5f

Browse files
ali-behjatiReisen
authored andcommitted
Use slot for pending accumulations
1 parent 05bd9b4 commit ec5ad5f

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

hermes/src/store.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use {
1010
MessageType,
1111
PriceFeedsWithUpdateData,
1212
RequestTime,
13+
Slot,
1314
Update,
1415
},
1516
},
@@ -32,19 +33,14 @@ use {
3233
derive_builder::Builder,
3334
moka::future::Cache,
3435
pyth_sdk::PriceIdentifier,
35-
std::{
36-
ops::Rem,
37-
time::Duration,
38-
},
36+
std::time::Duration,
3937
wormhole_sdk::Vaa,
4038
};
4139

4240
pub mod proof;
4341
pub mod storage;
4442
pub mod types;
4543

46-
pub type RingIndex = u32;
47-
4844
#[derive(Clone, PartialEq, Debug, Builder)]
4945
#[builder(derive(Debug), pattern = "immutable")]
5046
pub struct AccumulatorState {
@@ -55,7 +51,7 @@ pub struct AccumulatorState {
5551
#[derive(Clone)]
5652
pub struct Store {
5753
pub storage: StorageInstance,
58-
pub pending_accumulations: Cache<RingIndex, AccumulatorStateBuilder>,
54+
pub pending_accumulations: Cache<Slot, AccumulatorStateBuilder>,
5955
}
6056

6157
impl Store {
@@ -73,7 +69,7 @@ impl Store {
7369

7470
/// Stores the update data in the store
7571
pub async fn store_update(&self, update: Update) -> Result<()> {
76-
let ring_index = match update {
72+
let slot = match update {
7773
Update::Vaa(vaa_bytes) => {
7874
let vaa = serde_wormhole::from_slice::<Vaa<Vec<u8>>>(&vaa_bytes)?;
7975
let payload = WormholePayload::try_from_bytes(&vaa.payload, &vaa_bytes)?;
@@ -83,41 +79,42 @@ impl Store {
8379

8480
match payload {
8581
WormholePayload::Merkle(proof) => {
86-
log::info!("Storing merkle proof for state index {:?}", proof);
82+
log::info!(
83+
"Storing merkle proof for slot {:?}: {:?}",
84+
proof.slot,
85+
proof
86+
);
8787
store_wormhole_merkle_verified_message(self, proof.clone()).await?;
88-
proof.state_index
88+
proof.slot
8989
}
9090
}
9191
}
9292
Update::AccumulatorMessages(accumulator_messages) => {
9393
// FIXME: Move this constant to a better place
94-
const RING_SIZE: u32 = 10_000;
95-
let ring_index = accumulator_messages.slot.rem(RING_SIZE as u64) as u32;
94+
95+
let slot = accumulator_messages.slot;
9696

9797
log::info!(
98-
"Storing accumulator messages for ring index {:?}: {:?}",
99-
ring_index,
98+
"Storing accumulator messages for slot {:?}: {:?}",
99+
slot,
100100
accumulator_messages
101101
);
102102

103103
let pending_acc = self
104104
.pending_accumulations
105-
.entry(ring_index)
105+
.entry(slot)
106106
.or_default()
107107
.await
108108
.into_value();
109109
self.pending_accumulations
110-
.insert(
111-
ring_index,
112-
pending_acc.accumulator_messages(accumulator_messages),
113-
)
110+
.insert(slot, pending_acc.accumulator_messages(accumulator_messages))
114111
.await;
115112

116-
ring_index
113+
slot
117114
}
118115
};
119116

120-
let pending_state = self.pending_accumulations.get(&ring_index);
117+
let pending_state = self.pending_accumulations.get(&slot);
121118
let pending_state = match pending_state {
122119
Some(pending_state) => pending_state,
123120
// Due to some race conditions this might happen when it's processed before
@@ -159,7 +156,7 @@ impl Store {
159156

160157
self.storage.store_message_states(message_states)?;
161158

162-
self.pending_accumulations.invalidate(&ring_index).await;
159+
self.pending_accumulations.invalidate(&slot).await;
163160

164161
Ok(())
165162
}

hermes/src/store/proof/wormhole_merkle.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ type Hash = [u8; 20];
3232

3333
#[derive(Clone, PartialEq, Debug)]
3434
pub struct WormholeMerkleProof {
35-
pub vaa: Vec<u8>,
36-
pub state_index: u32,
37-
pub root: Hash,
35+
pub vaa: Vec<u8>,
36+
pub slot: u64,
37+
pub ring_size: u32,
38+
pub root: Hash,
3839
}
3940

4041
#[derive(Clone, PartialEq, Debug)]
@@ -49,13 +50,13 @@ pub async fn store_wormhole_merkle_verified_message(
4950
) -> Result<()> {
5051
let pending_acc = store
5152
.pending_accumulations
52-
.entry(proof.state_index)
53+
.entry(proof.slot)
5354
.or_default()
5455
.await
5556
.into_value();
5657
store
5758
.pending_accumulations
58-
.insert(proof.state_index, pending_acc.wormhole_merkle_proof(proof))
59+
.insert(proof.slot, pending_acc.wormhole_merkle_proof(proof))
5960
.await;
6061
Ok(())
6162
}

hermes/src/store/types.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ pub enum WormholePayload {
1616

1717
impl WormholePayload {
1818
pub fn try_from_bytes(bytes: &[u8], vaa_bytes: &[u8]) -> Result<Self> {
19+
if bytes.len() != 37 {
20+
return Err(anyhow!("Invalid message length"));
21+
}
22+
23+
// TODO: Use byte string literals for this check
1924
let magic = u32::from_be_bytes(bytes[0..4].try_into()?);
2025
if magic != 0x41555756u32 {
2126
return Err(anyhow!("Invalid magic"));
@@ -27,16 +32,15 @@ impl WormholePayload {
2732
return Err(anyhow!("Invalid message type"));
2833
}
2934

30-
let state_index = u32::from_be_bytes(bytes[5..9].try_into()?);
31-
let root_digest = bytes[9..29].try_into()?;
35+
let slot = u64::from_be_bytes(bytes[5..13].try_into()?);
36+
let ring_size = u32::from_be_bytes(bytes[13..17].try_into()?);
37+
let root_digest = bytes[17..37].try_into()?;
3238

33-
if bytes.len() > 29 {
34-
return Err(anyhow!("Invalid message length"));
35-
}
3639

3740
Ok(Self::Merkle(WormholeMerkleProof {
3841
root: root_digest,
39-
state_index,
42+
slot,
43+
ring_size,
4044
vaa: vaa_bytes.to_vec(),
4145
}))
4246
}

0 commit comments

Comments
 (0)