Skip to content

Commit fca22a5

Browse files
ali-behjatiReisen
authored andcommitted
Fix pythnet listner
1 parent ec5ad5f commit fca22a5

File tree

2 files changed

+47
-36
lines changed

2 files changed

+47
-36
lines changed

hermes/src/network/pythnet.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use {
66
crate::store::{
77
types::{
88
AccumulatorMessages,
9-
RawMessage,
109
Update,
1110
},
1211
Store,
@@ -21,18 +20,19 @@ use {
2120
RpcAccountInfoConfig,
2221
RpcProgramAccountsConfig,
2322
},
23+
rpc_filter::{
24+
Memcmp,
25+
RpcFilterType,
26+
},
2427
},
2528
solana_sdk::{
2629
account::Account,
2730
commitment_config::CommitmentConfig,
2831
pubkey::Pubkey,
2932
system_program,
3033
},
31-
std::ops::Rem,
3234
};
3335

34-
const RING_SIZE: u32 = 10_000;
35-
3636
pub async fn spawn(pythnet_ws_endpoint: String, store: Store) -> Result<()> {
3737
let client = PubsubClient::new(pythnet_ws_endpoint.as_ref()).await?;
3838

@@ -42,6 +42,10 @@ pub async fn spawn(pythnet_ws_endpoint: String, store: Store) -> Result<()> {
4242
encoding: Some(UiAccountEncoding::Base64Zstd),
4343
..Default::default()
4444
},
45+
filters: Some(vec![RpcFilterType::Memcmp(Memcmp::new_raw_bytes(
46+
0,
47+
b"PAS1".to_vec(),
48+
))]),
4549
with_context: Some(true),
4650
..Default::default()
4751
};
@@ -55,38 +59,36 @@ pub async fn spawn(pythnet_ws_endpoint: String, store: Store) -> Result<()> {
5559
log::debug!("Received Pythnet update: {:?}", update);
5660

5761
if let Some(update) = update {
58-
// Check whether this account matches the state for this slot
59-
// FIXME this is hardcoded for localnet, we need to remove it from the code
60-
let pyth = Pubkey::try_from("7th6GdMuo4u1zNLzFAyMY6psunHNsGjPjo8hXvcTgKei").unwrap();
61-
62-
let accumulator_slot = update.context.slot - 1;
63-
64-
// Apparently we get the update for the previous slot, so we need to subtract 1
65-
let ring_index = accumulator_slot.rem(RING_SIZE as u64) as u32;
66-
67-
let (candidate, _) = Pubkey::find_program_address(
68-
&[
69-
b"AccumulatorState",
70-
&pyth.to_bytes(),
71-
&ring_index.to_be_bytes(),
72-
],
73-
&system_program::id(),
74-
);
75-
76-
if candidate.to_string() != update.value.pubkey {
77-
continue;
78-
}
79-
8062
let account: Account = update.value.account.decode().unwrap();
8163
log::debug!("Received Accumulator update: {:?}", account);
82-
let accumulator_messages = AccumulatorMessages {
83-
slot: accumulator_slot,
84-
messages: Vec::<RawMessage>::try_from_slice(account.data.as_ref())?,
85-
};
8664

87-
store
88-
.store_update(Update::AccumulatorMessages(accumulator_messages))
89-
.await?;
65+
let accumulator_messages = AccumulatorMessages::try_from_slice(&account.data);
66+
match accumulator_messages {
67+
Ok(accumulator_messages) => {
68+
let (candidate, _) = Pubkey::find_program_address(
69+
&[
70+
b"AccumulatorState",
71+
&accumulator_messages.ring_index().to_be_bytes(),
72+
],
73+
&system_program::id(),
74+
);
75+
76+
if candidate.to_string() == update.value.pubkey {
77+
store
78+
.store_update(Update::AccumulatorMessages(accumulator_messages))
79+
.await?;
80+
} else {
81+
log::error!(
82+
"Failed to verify the messages public key: {:?} != {:?}",
83+
candidate,
84+
update.value.pubkey
85+
);
86+
}
87+
}
88+
Err(err) => {
89+
log::error!("Failed to parse AccumulatorMessages: {:?}", err);
90+
}
91+
};
9092
}
9193
}
9294
}

hermes/src/store/types.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use {
77
anyhow,
88
Result,
99
},
10+
borsh::BorshDeserialize,
1011
};
1112

1213
#[derive(Clone, Debug, PartialEq)]
@@ -149,10 +150,18 @@ pub enum RequestTime {
149150
FirstAfter(UnixTimestamp),
150151
}
151152

152-
#[derive(Clone, PartialEq, Debug)]
153+
#[derive(Clone, PartialEq, Debug, BorshDeserialize)]
153154
pub struct AccumulatorMessages {
154-
pub slot: Slot,
155-
pub messages: Vec<RawMessage>,
155+
pub magic: [u8; 4],
156+
pub slot: Slot,
157+
pub ring_size: u32,
158+
pub messages: Vec<RawMessage>,
159+
}
160+
161+
impl AccumulatorMessages {
162+
pub fn ring_index(&self) -> u32 {
163+
(self.slot % self.ring_size as u64) as u32
164+
}
156165
}
157166

158167
pub enum Update {

0 commit comments

Comments
 (0)