Skip to content

Commit 9335bcd

Browse files
committed
feat: fetch publisher buffer key
1 parent 0045bdc commit 9335bcd

File tree

5 files changed

+63
-20
lines changed

5 files changed

+63
-20
lines changed

src/agent/services/exporter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ mod exporter {
261261
config.exporter.staleness_threshold,
262262
config.exporter.unchanged_publish_threshold,
263263
).await {
264+
let publisher_buffer_key = Exporter::get_publisher_buffer_key(&*state).await;
264265
if let Err(err) = publish_batches(
265266
state.clone(),
266267
client.clone(),
@@ -270,7 +271,7 @@ mod exporter {
270271
&publish_keypair,
271272
key_store.oracle_program_key,
272273
key_store.publish_program_key,
273-
key_store.publisher_buffer_key,
274+
publisher_buffer_key,
274275
config.exporter.max_batch_size,
275276
config.exporter.staleness_threshold,
276277
config.exporter.compute_unit_limit,

src/agent/services/oracle.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ where
6262
state.clone(),
6363
key_store.mapping_key,
6464
key_store.publish_keypair,
65+
key_store.publish_program_key,
6566
config.oracle.max_lookup_batch_size,
6667
)));
6768

@@ -159,6 +160,7 @@ async fn poller<S>(
159160
state: Arc<S>,
160161
mapping_key: Pubkey,
161162
publish_keypair: Option<Keypair>,
163+
publish_program_key: Option<Pubkey>,
162164
max_lookup_batch_size: usize,
163165
) where
164166
S: Oracle,
@@ -183,6 +185,7 @@ async fn poller<S>(
183185
network,
184186
mapping_key,
185187
publish_keypair.as_ref(),
188+
publish_program_key,
186189
&client,
187190
max_lookup_batch_size,
188191
)

src/agent/solana.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ pub mod key_store {
103103
default
104104
)]
105105
pub publish_program_key: Option<Pubkey>,
106-
/// The public key of the publisher's buffer for the Publish program
107-
#[serde(
108-
serialize_with = "opt_pubkey_string_ser",
109-
deserialize_with = "opt_pubkey_string_de",
110-
default
111-
)]
112-
pub publisher_buffer_key: Option<Pubkey>,
113106
/// The public key of the root mapping account
114107
#[serde(
115108
serialize_with = "pubkey_string_ser",
@@ -129,18 +122,15 @@ pub mod key_store {
129122
/// The keypair used to publish price updates. When None,
130123
/// publishing will not start until a new keypair is supplied
131124
/// via the remote loading endpoint
132-
pub publish_keypair: Option<Keypair>,
125+
pub publish_keypair: Option<Keypair>,
133126
/// Public key of the Oracle program
134-
pub oracle_program_key: Pubkey,
127+
pub oracle_program_key: Pubkey,
135128
/// Public key of the Publish program
136-
pub publish_program_key: Option<Pubkey>,
137-
/// Public key of the publisher's buffer for the publish program
138-
pub publisher_buffer_key: Option<Pubkey>,
139-
129+
pub publish_program_key: Option<Pubkey>,
140130
/// Public key of the root mapping account
141-
pub mapping_key: Pubkey,
131+
pub mapping_key: Pubkey,
142132
/// Public key of the accumulator program (if provided)
143-
pub accumulator_key: Option<Pubkey>,
133+
pub accumulator_key: Option<Pubkey>,
144134
}
145135

146136
impl KeyStore {
@@ -161,7 +151,6 @@ pub mod key_store {
161151
publish_keypair,
162152
oracle_program_key: config.oracle_program_key,
163153
publish_program_key: config.publish_program_key,
164-
publisher_buffer_key: config.publisher_buffer_key,
165154
mapping_key: config.mapping_key,
166155
accumulator_key: config.accumulator_key,
167156
})

src/agent/state/exporter.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ pub struct ExporterState {
8181
/// Currently known permissioned prices of this publisher along with their market hours
8282
our_prices: RwLock<HashMap<Pubkey, PricePublishingMetadata>>,
8383

84+
publisher_buffer_key: RwLock<Option<Pubkey>>,
85+
86+
8487
/// Recent compute unit price in micro lamports (set if dynamic compute unit pricing is enabled)
8588
recent_compute_unit_price_micro_lamports: RwLock<Option<u64>>,
8689
}
@@ -106,6 +109,7 @@ where
106109
staleness_threshold: Duration,
107110
unchanged_publish_threshold: Duration,
108111
) -> Result<Vec<PermissionedUpdate>>;
112+
async fn get_publisher_buffer_key(&self) -> Option<Pubkey>;
109113
async fn get_recent_compute_unit_price_micro_lamports(&self) -> Option<u64>;
110114
async fn update_recent_compute_unit_price(
111115
&self,
@@ -114,11 +118,12 @@ where
114118
staleness_threshold: Duration,
115119
unchanged_publish_threshold: Duration,
116120
) -> Result<()>;
117-
async fn update_permissions(
121+
async fn update_on_chain_state(
118122
&self,
119123
network: Network,
120124
publish_keypair: Option<&Keypair>,
121125
publisher_permissions: HashMap<Pubkey, HashMap<Pubkey, PricePublishingMetadata>>,
126+
publisher_buffer_key: Option<Pubkey>,
122127
) -> Result<()>;
123128
}
124129

@@ -267,6 +272,10 @@ where
267272
.collect::<Vec<_>>())
268273
}
269274

275+
async fn get_publisher_buffer_key(&self) -> Option<Pubkey> {
276+
*self.into().publisher_buffer_key.read().await
277+
}
278+
270279
async fn get_recent_compute_unit_price_micro_lamports(&self) -> Option<u64> {
271280
*self
272281
.into()
@@ -313,11 +322,12 @@ where
313322
}
314323

315324
#[instrument(skip(self, publish_keypair, publisher_permissions))]
316-
async fn update_permissions(
325+
async fn update_on_chain_state(
317326
&self,
318327
network: Network,
319328
publish_keypair: Option<&Keypair>,
320329
publisher_permissions: HashMap<Pubkey, HashMap<Pubkey, PricePublishingMetadata>>,
330+
publisher_buffer_key: Option<Pubkey>,
321331
) -> Result<()> {
322332
let publish_keypair = get_publish_keypair(self, network, publish_keypair).await?;
323333
*self.into().our_prices.write().await = publisher_permissions
@@ -330,6 +340,7 @@ where
330340
);
331341
HashMap::new()
332342
});
343+
*self.into().publisher_buffer_key.write().await = publisher_buffer_key;
333344

334345
Ok(())
335346
}

src/agent/state/oracle.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use pyth_price_publisher::instruction::PUBLISHER_CONFIG_SEED;
2+
use solana_sdk::signer::Signer;
3+
14
#[allow(deprecated)]
25
use crate::agent::legacy_schedule::LegacySchedule;
36
use {
@@ -135,6 +138,7 @@ pub struct Data {
135138
pub price_accounts: HashMap<Pubkey, PriceEntry>,
136139
/// publisher => {their permissioned price accounts => price publishing metadata}
137140
pub publisher_permissions: HashMap<Pubkey, HashMap<Pubkey, PricePublishingMetadata>>,
141+
pub publisher_buffer_key: Option<Pubkey>,
138142
}
139143

140144
#[derive(Clone, Serialize, Deserialize, Debug)]
@@ -193,6 +197,7 @@ pub trait Oracle {
193197
network: Network,
194198
mapping_key: Pubkey,
195199
publish_keypair: Option<&Keypair>,
200+
publish_program_key: Option<Pubkey>,
196201
rpc_client: &RpcClient,
197202
max_lookup_batch_size: usize,
198203
) -> Result<()>;
@@ -267,6 +272,7 @@ where
267272
network: Network,
268273
mapping_key: Pubkey,
269274
publish_keypair: Option<&Keypair>,
275+
publish_program_key: Option<Pubkey>,
270276
rpc_client: &RpcClient,
271277
max_lookup_batch_size: usize,
272278
) -> Result<()> {
@@ -311,22 +317,38 @@ where
311317
}
312318
}
313319

320+
let mut publisher_buffer_key = None;
321+
if let (Some(publish_program_key), Some(publish_keypair)) = (publish_program_key, publish_keypair) {
322+
match fetch_publisher_buffer_key(rpc_client, publish_program_key, publish_keypair.pubkey()).await {
323+
Ok(r) => {
324+
publisher_buffer_key = Some(r);
325+
}
326+
Err(err) => {
327+
tracing::warn!(
328+
"failed to fetch publisher buffer key: {:?}", err
329+
);
330+
}
331+
}
332+
}
333+
314334
let new_data = Data {
315335
mapping_accounts,
316336
product_accounts,
317337
price_accounts,
318338
publisher_permissions,
339+
publisher_buffer_key,
319340
};
320341

321342
let mut data = self.into().data.write().await;
322343
log_data_diff(&data, &new_data);
323344
*data = new_data;
324345

325-
Exporter::update_permissions(
346+
Exporter::update_on_chain_state(
326347
self,
327348
network,
328349
publish_keypair,
329350
data.publisher_permissions.clone(),
351+
data.publisher_buffer_key,
330352
)
331353
.await?;
332354

@@ -367,6 +389,23 @@ where
367389
}
368390
}
369391

392+
async fn fetch_publisher_buffer_key(
393+
rpc_client: &RpcClient,
394+
publish_program_key: Pubkey,
395+
publisher_pubkey: Pubkey,
396+
) -> Result<Pubkey> {
397+
let (publisher_config_key, _bump) = Pubkey::find_program_address(
398+
&[
399+
PUBLISHER_CONFIG_SEED.as_bytes(),
400+
&publisher_pubkey.to_bytes(),
401+
],
402+
&publish_program_key,
403+
);
404+
let data = rpc_client.get_account_data(&publisher_config_key).await?;
405+
let config = pyth_price_publisher::accounts::publisher_config::read(&data)?;
406+
Ok(config.buffer_account.into())
407+
}
408+
370409
#[instrument(skip(rpc_client))]
371410
async fn fetch_mapping_accounts(
372411
rpc_client: &RpcClient,

0 commit comments

Comments
 (0)