Skip to content

Commit 1902dba

Browse files
authored
pythnet: replace pythnet_sdk with the right files (#803)
1 parent 71bab7a commit 1902dba

File tree

13 files changed

+716
-932
lines changed

13 files changed

+716
-932
lines changed

pythnet/pythnet_sdk/Cargo.toml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
[package]
2-
name = "solana-pyth"
2+
name = "pythnet-sdk"
33
version = "1.13.6"
44
description = "Pyth Runtime for Solana"
55
authors = ["Pyth Data Association"]
66
repository = "https://github.com/pyth-network/pythnet"
77
edition = "2021"
88

9+
[lib]
10+
crate-type = ["lib"]
11+
name = "solana_pyth"
12+
913
[dependencies]
10-
borsh = "0.9.1"
1114
bincode = "1.3.1"
15+
borsh = "0.9.1"
1216
bytemuck = { version = "1.11.0", features = ["derive"] }
1317
fast-math = "0.1"
1418
hex = { version = "0.4.3", features = ["serde"] }
1519
serde = { version = "1.0.144", features = ["derive"] }
20+
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole" }
1621
sha3 = "0.10.4"
1722
slow_primes = "0.1.14"
23+
wormhole-sdk = { git = "https://github.com/wormhole-foundation/wormhole" }
1824

1925
[dev-dependencies]
26+
base64 = "0.21.0"
2027
rand = "0.7.0"
21-
22-
[lib]
23-
crate-type = ["lib"]
24-
name = "solana_pyth"
28+
serde_json = "1.0.96"
29+
solana-client = { path = "../client" }
30+
solana-sdk = { path = "../sdk" }
31+
proptest = "1.1.0"
2532

2633
[package.metadata.docs.rs]
2734
targets = ["x86_64-unknown-linux-gnu"]
2835

2936
[build-dependencies]
3037
rustc_version = "0.4"
38+
39+
[patch.crates-io]
40+
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole" }
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Use the Solana client library to pull the addresses of all relevant accounts from PythNet so we
2+
// can test locally.
3+
4+
// #![feature(proc_macro_hygiene)]
5+
6+
use {
7+
serde_json::json,
8+
solana_client::rpc_client::RpcClient,
9+
solana_pyth::PYTH_PID,
10+
solana_sdk::pubkey::Pubkey,
11+
std::str::FromStr,
12+
std::io::Write,
13+
};
14+
15+
fn main() {
16+
let client = RpcClient::new("http://pythnet.rpcpool.com/".to_string());
17+
let pythnet = Pubkey::from_str("FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH").unwrap();
18+
let wormhole = Pubkey::from_str("H3fxXJ86ADW2PNuDDmZJg6mzTtPxkYCpNuQUTgmJ7AjU").unwrap();
19+
20+
// Create a folder called `accounts` in the current directory, if it already exists that is OK
21+
// but only if the folder is empty.
22+
std::fs::create_dir_all("accounts").unwrap();
23+
24+
// Download all PythNet accounts into .json files in the current directory.
25+
{
26+
let pythnet_accounts = client.get_program_accounts(&pythnet).map_err(|e| {
27+
println!("{e}");
28+
e
29+
});
30+
31+
pythnet_accounts
32+
.unwrap()
33+
.into_iter()
34+
.for_each(|(pubkey, _account)| {
35+
// This writes the account as JSON into a file that solana-test-validator can read into
36+
// the ledger. Each account should be written into a file named `<pubkey>.json`
37+
let account = client.get_account(&pubkey).unwrap();
38+
39+
// Now write to <pubkey>.json.
40+
std::fs::write(
41+
format!("accounts/{pubkey}.json"),
42+
json!({
43+
"pubkey": pubkey.to_string(),
44+
"account": {
45+
"lamports": account.lamports,
46+
"data": [
47+
base64::encode(&account.data),
48+
"base64"
49+
],
50+
"owner": account.owner.to_string(),
51+
"executable": account.executable,
52+
"rentEpoch": account.rent_epoch,
53+
}
54+
})
55+
.to_string(),
56+
)
57+
.unwrap();
58+
});
59+
}
60+
61+
// Download the Wormhole program only into a .json file in the current directory. Instead of
62+
// getting the program accounts we just want the wormhole one itself.
63+
{
64+
let wormhole_account = client.get_account(&wormhole).unwrap();
65+
66+
// Now write to wormhole.json.
67+
std::fs::write(
68+
format!("accounts/{wormhole}.json"),
69+
json!({
70+
"pubkey": wormhole.to_string(),
71+
"account": {
72+
"lamports": wormhole_account.lamports,
73+
"data": [
74+
base64::encode(&wormhole_account.data),
75+
"base64"
76+
],
77+
"owner": wormhole_account.owner.to_string(),
78+
"executable": wormhole_account.executable,
79+
"rentEpoch": wormhole_account.rent_epoch,
80+
}
81+
})
82+
.to_string(),
83+
)
84+
.unwrap();
85+
}
86+
87+
// Same for the Pyth program.
88+
{
89+
let pyth_account = client.get_account(&pythnet).unwrap();
90+
91+
// Now write to pyth.json.
92+
std::fs::write(
93+
format!("accounts/{pythnet}.json"),
94+
json!({
95+
"pubkey": pythnet.to_string(),
96+
"account": {
97+
"lamports": pyth_account.lamports,
98+
"data": [
99+
base64::encode(&pyth_account.data),
100+
"base64"
101+
],
102+
"owner": pyth_account.owner.to_string(),
103+
"executable": pyth_account.executable,
104+
"rentEpoch": pyth_account.rent_epoch,
105+
}
106+
})
107+
.to_string(),
108+
)
109+
.unwrap();
110+
}
111+
112+
// Write names of AccumulatorState accounts to pdas.txt
113+
{
114+
let mut file = std::fs::File::create("pdas.txt").unwrap();
115+
for i in (0..10_000u32) {
116+
let (accumulator_account, _) = Pubkey::find_program_address(
117+
&[b"AccumulatorState", &PYTH_PID, &i.to_be_bytes()],
118+
&solana_sdk::system_program::id(),
119+
);
120+
file.write_all(format!("{}\n", accumulator_account).as_bytes())
121+
.unwrap();
122+
}
123+
}
124+
}
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1+
//! Accumulators
2+
//!
3+
//! This module defines the Accumulator abstraction as well as the implementation details for
4+
//! several different accumulators. This library can be used for interacting with PythNet state
5+
//! proofs for account content.
6+
17
pub mod merkle;
2-
mod mul;
8+
pub mod mul;
9+
10+
/// The Accumulator trait defines the interface for an accumulator.
11+
///
12+
/// This trait assumes an accumulator has an associated proof type that can be used to prove
13+
/// membership of a specific item. The choice to return Proof makes this the most generic
14+
/// implementation possible for any accumulator.
15+
pub trait Accumulator<'a>
16+
where
17+
Self: Sized,
18+
Self::Proof: 'a,
19+
Self::Proof: Sized,
20+
{
21+
type Proof;
322

4-
pub trait Accumulator<'a>: Sized {
5-
type Proof: 'a;
6-
fn from_set(items: impl Iterator<Item = &'a &'a [u8]>) -> Option<Self>;
23+
/// Prove an item is a member of the accumulator.
724
fn prove(&'a self, item: &[u8]) -> Option<Self::Proof>;
8-
fn verify(&'a self, proof: Self::Proof, item: &[u8]) -> bool;
25+
26+
/// Verify an item is a member of the accumulator.
27+
fn check(&'a self, proof: Self::Proof, item: &[u8]) -> bool;
28+
29+
/// Create an accumulator from a set of items.
30+
fn from_set(items: impl Iterator<Item = &'a [u8]>) -> Option<Self>;
931
}

0 commit comments

Comments
 (0)