Skip to content

Commit 403d3ea

Browse files
committed
Move keystore module one level higher
1 parent f510744 commit 403d3ea

File tree

3 files changed

+109
-96
lines changed

3 files changed

+109
-96
lines changed

integration-tests/agent_conf.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[primary_network]
2+
key_store.root_path = "keystore"
23
oracle.poll_interval_duration = "1s"
34
oracle.oracle_account_key = "BujGr9ChcuaCJhxeFEvGvaCFTxSV1CUCSVHL1SVFpU4i"
45
oracle.mapping_account_key = "BTJKZngp3vzeJiRmmT9PitQH4H29dhQZ1GNhxFfDi4kw"
56
oracle.subscriber.account_key = "BujGr9ChcuaCJhxeFEvGvaCFTxSV1CUCSVHL1SVFpU4i"
6-
exporter.key_store.root_path = "keystore"
77
exporter.transaction_monitor.poll_interval_duration = "1s"

src/agent/solana.rs

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub mod network {
1212
store::global,
1313
},
1414
exporter,
15+
key_store::{
16+
self,
17+
KeyStore,
18+
},
1519
oracle,
1620
},
1721
anyhow::Result,
@@ -32,10 +36,12 @@ pub mod network {
3236
/// Configuration for a network
3337
#[derive(Clone, Serialize, Deserialize, Debug, Default)]
3438
pub struct Config {
39+
/// Keystore
40+
pub key_store: key_store::Config,
3541
/// Configuration for the Oracle reading data from this network
36-
pub oracle: oracle::Config,
42+
pub oracle: oracle::Config,
3743
/// Configuration for the Exporter publishing data to this network
38-
pub exporter: exporter::Config,
44+
pub exporter: exporter::Config,
3945
}
4046

4147
pub fn spawn_network(
@@ -44,6 +50,9 @@ pub mod network {
4450
global_store_update_tx: mpsc::Sender<global::Update>,
4551
logger: Logger,
4652
) -> Result<Vec<JoinHandle<()>>> {
53+
// Create the key store
54+
let key_store = KeyStore::new(config.key_store.clone())?;
55+
4756
// Spawn the Oracle
4857
let mut jhs = oracle::spawn_oracle(
4958
config.oracle.clone(),
@@ -52,9 +61,97 @@ pub mod network {
5261
);
5362

5463
// Spawn the Exporter
55-
let exporter_jhs = exporter::spawn_exporter(config.exporter, local_store_tx, logger)?;
64+
let exporter_jhs =
65+
exporter::spawn_exporter(config.exporter, key_store, local_store_tx, logger)?;
5666
jhs.extend(exporter_jhs);
5767

5868
Ok(jhs)
5969
}
6070
}
71+
72+
/// The key_store module is responsible for parsing the pythd key store.
73+
mod key_store {
74+
use {
75+
anyhow::{
76+
anyhow,
77+
Context,
78+
Result,
79+
},
80+
serde::{
81+
Deserialize,
82+
Serialize,
83+
},
84+
solana_sdk::{
85+
pubkey::Pubkey,
86+
signature::Keypair,
87+
signer::keypair,
88+
},
89+
std::{
90+
fs,
91+
path::{
92+
Path,
93+
PathBuf,
94+
},
95+
str::FromStr,
96+
},
97+
};
98+
99+
100+
#[derive(Clone, Serialize, Deserialize, Debug)]
101+
#[serde(default)]
102+
pub struct Config {
103+
/// Root directory of the KeyStore
104+
pub root_path: PathBuf,
105+
/// Path to the keypair used to publish price updates, relative to the root
106+
pub publish_keypair_path: PathBuf,
107+
/// Path to the public key of the Oracle program, relative to the root
108+
pub program_key_path: PathBuf,
109+
/// Path to the public key of the root mapping account, relative to the root
110+
pub mapping_key_path: PathBuf,
111+
}
112+
113+
impl Default for Config {
114+
fn default() -> Self {
115+
Self {
116+
root_path: Default::default(),
117+
publish_keypair_path: "publish_key_pair.json".into(),
118+
program_key_path: "program_key.json".into(),
119+
mapping_key_path: "mapping_key.json".into(),
120+
}
121+
}
122+
}
123+
124+
pub struct KeyStore {
125+
/// The keypair used to publish price updates
126+
pub publish_keypair: Keypair,
127+
/// Public key of the Oracle program
128+
pub program_key: Pubkey,
129+
/// Public key of the root mapping account
130+
pub mapping_key: Pubkey,
131+
}
132+
133+
impl KeyStore {
134+
pub fn new(config: Config) -> Result<Self> {
135+
Ok(KeyStore {
136+
publish_keypair: keypair::read_keypair_file(
137+
config.root_path.join(config.publish_keypair_path),
138+
)
139+
.map_err(|e| anyhow!(e.to_string()))
140+
.context("reading publish keypair")?,
141+
program_key: Self::pubkey_from_path(
142+
config.root_path.join(config.program_key_path),
143+
)
144+
.context("reading program key")?,
145+
mapping_key: Self::pubkey_from_path(
146+
config.root_path.join(config.mapping_key_path),
147+
)
148+
.context("reading mapping key")?,
149+
})
150+
}
151+
152+
fn pubkey_from_path(path: impl AsRef<Path>) -> Result<Pubkey> {
153+
let contents = fs::read_to_string(path)?;
154+
Pubkey::from_str(contents.trim()).map_err(|e| e.into())
155+
}
156+
}
157+
}

src/agent/solana/exporter.rs

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use {
22
self::transaction_monitor::TransactionMonitor,
3-
super::super::store::{
4-
self,
5-
local::PriceInfo,
6-
PriceIdentifier,
3+
super::{
4+
super::store::{
5+
self,
6+
local::PriceInfo,
7+
PriceIdentifier,
8+
},
9+
key_store,
710
},
811
anyhow::{
912
anyhow,
@@ -130,6 +133,7 @@ impl Default for Config {
130133

131134
pub fn spawn_exporter(
132135
config: Config,
136+
key_store: KeyStore,
133137
local_store_tx: Sender<store::local::Message>,
134138
logger: Logger,
135139
) -> Result<Vec<JoinHandle<()>>> {
@@ -154,7 +158,6 @@ pub fn spawn_exporter(
154158
let transaction_monitor_jh = tokio::spawn(async move { transaction_monitor.run().await });
155159

156160
// Create and spawn the exporter
157-
let key_store = KeyStore::new(config.key_store.clone())?;
158161
let mut exporter = Exporter::new(
159162
config,
160163
key_store,
@@ -629,90 +632,3 @@ mod transaction_monitor {
629632
}
630633
}
631634
}
632-
633-
/// The key_store module is responsible for parsing the pythd key store.
634-
mod key_store {
635-
use {
636-
anyhow::{
637-
anyhow,
638-
Context,
639-
Result,
640-
},
641-
serde::{
642-
Deserialize,
643-
Serialize,
644-
},
645-
solana_sdk::{
646-
pubkey::Pubkey,
647-
signature::Keypair,
648-
signer::keypair,
649-
},
650-
std::{
651-
fs,
652-
path::{
653-
Path,
654-
PathBuf,
655-
},
656-
str::FromStr,
657-
},
658-
};
659-
660-
661-
#[derive(Clone, Serialize, Deserialize, Debug)]
662-
#[serde(default)]
663-
pub struct Config {
664-
/// Root directory of the KeyStore
665-
pub root_path: PathBuf,
666-
/// Path to the keypair used to publish price updates, relative to the root
667-
pub publish_keypair_path: PathBuf,
668-
/// Path to the public key of the Oracle program, relative to the root
669-
pub program_key_path: PathBuf,
670-
/// Path to the public key of the root mapping account, relative to the root
671-
pub mapping_key_path: PathBuf,
672-
}
673-
674-
impl Default for Config {
675-
fn default() -> Self {
676-
Self {
677-
root_path: Default::default(),
678-
publish_keypair_path: "publish_key_pair.json".into(),
679-
program_key_path: "program_key.json".into(),
680-
mapping_key_path: "mapping_key.json".into(),
681-
}
682-
}
683-
}
684-
685-
pub struct KeyStore {
686-
/// The keypair used to publish price updates
687-
pub publish_keypair: Keypair,
688-
/// Public key of the Oracle program
689-
pub program_key: Pubkey,
690-
/// Public key of the root mapping account
691-
pub mapping_key: Pubkey,
692-
}
693-
694-
impl KeyStore {
695-
pub fn new(config: Config) -> Result<Self> {
696-
Ok(KeyStore {
697-
publish_keypair: keypair::read_keypair_file(
698-
config.root_path.join(config.publish_keypair_path),
699-
)
700-
.map_err(|e| anyhow!(e.to_string()))
701-
.context("reading publish keypair")?,
702-
program_key: Self::pubkey_from_path(
703-
config.root_path.join(config.program_key_path),
704-
)
705-
.context("reading program key")?,
706-
mapping_key: Self::pubkey_from_path(
707-
config.root_path.join(config.mapping_key_path),
708-
)
709-
.context("reading mapping key")?,
710-
})
711-
}
712-
713-
fn pubkey_from_path(path: impl AsRef<Path>) -> Result<Pubkey> {
714-
let contents = fs::read_to_string(path)?;
715-
Pubkey::from_str(contents.trim()).map_err(|e| e.into())
716-
}
717-
}
718-
}

0 commit comments

Comments
 (0)