Skip to content

Commit e08410b

Browse files
authored
doc(target_chains/solana): add more comments and configure publish (#1883)
1 parent 498edea commit e08410b

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Publish Pyth Price Publisher to crates.io
2+
3+
on:
4+
push:
5+
tags:
6+
- pyth-price-publisher-v*
7+
jobs:
8+
publish-pyth-price-publisher:
9+
name: Publish Pyth Price Publisher
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout sources
13+
uses: actions/checkout@v2
14+
- name: Install Rust
15+
uses: actions-rs/toolchain@v1
16+
with:
17+
toolchain: stable
18+
default: true
19+
profile: minimal
20+
- name: Publish
21+
run: cargo publish --token ${CARGO_REGISTRY_TOKEN}
22+
env:
23+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
24+
working-directory: "target_chains/solana/programs/pyth-price-publisher"

target_chains/solana/programs/pyth-price-publisher/src/accounts/buffer.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,13 @@ impl BufferedPrice {
8585
}
8686
}
8787

88+
/// Verifies the account magic.
8889
pub fn format_matches(data: &[u8]) -> bool {
8990
super::format(data).map_or(false, |f| f == FORMAT)
9091
}
9192

93+
/// Verifies the account size and header. Returns the header and the currently stored prices
94+
/// (as indicated by the `num_prices` field in the header).
9295
pub fn read(data: &[u8]) -> Result<(&BufferHeader, &[BufferedPrice]), ReadAccountError> {
9396
if data.len() < size_of::<BufferHeader>() {
9497
return Err(ReadAccountError::DataTooShort);
@@ -109,10 +112,13 @@ pub fn read(data: &[u8]) -> Result<(&BufferHeader, &[BufferedPrice]), ReadAccoun
109112
Ok((header, prices))
110113
}
111114

115+
/// Returns the buffer size required to hold the specified number of prices.
112116
pub fn size(max_prices: usize) -> usize {
113117
size_of::<BufferHeader>() + max_prices * size_of::<BufferedPrice>()
114118
}
115119

120+
/// Verifies the account size and header. Returns the header and the remaining buffer space.
121+
/// The remaining space may contain some prices, as indicated by the `num_prices` field in the header.
116122
pub fn read_mut(data: &mut [u8]) -> Result<(&mut BufferHeader, &mut [u8]), ReadAccountError> {
117123
if data.len() < size_of::<BufferHeader>() {
118124
return Err(ReadAccountError::DataTooShort);
@@ -125,6 +131,7 @@ pub fn read_mut(data: &mut [u8]) -> Result<(&mut BufferHeader, &mut [u8]), ReadA
125131
Ok((header, prices))
126132
}
127133

134+
/// Initializes the buffer. Returns the header and the remaining buffer space.
128135
pub fn create(
129136
data: &mut [u8],
130137
publisher: [u8; 32],
@@ -141,7 +148,8 @@ pub fn create(
141148
Ok((header, prices))
142149
}
143150

144-
pub fn extend(
151+
/// Removes prices for the other slot from the buffer (if any) and adds new prices.
152+
pub fn update(
145153
header: &mut BufferHeader,
146154
prices: &mut [u8],
147155
current_slot: u64,
@@ -193,17 +201,17 @@ fn test_extend_clears_old_prices() {
193201
assert!(read(&buf).unwrap().1.is_empty());
194202
{
195203
let (header, prices) = read_mut(&mut buf).unwrap();
196-
extend(header, prices, 1, &vec![1; 40]).unwrap();
204+
update(header, prices, 1, &vec![1; 40]).unwrap();
197205
}
198206
assert_eq!(read(&buf).unwrap().1.len(), 2);
199207
{
200208
let (header, prices) = read_mut(&mut buf).unwrap();
201-
extend(header, prices, 1, &vec![1; 60]).unwrap();
209+
update(header, prices, 1, &vec![1; 60]).unwrap();
202210
}
203211
assert_eq!(read(&buf).unwrap().1.len(), 5);
204212
{
205213
let (header, prices) = read_mut(&mut buf).unwrap();
206-
extend(header, prices, 2, &vec![1; 60]).unwrap();
214+
update(header, prices, 2, &vec![1; 60]).unwrap();
207215
}
208216
assert_eq!(read(&buf).unwrap().1.len(), 3);
209217
}

target_chains/solana/programs/pyth-price-publisher/src/processor/initialize.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ use {
2222
},
2323
};
2424

25-
// Creates a config account that stores the authority pubkey.
26-
// The authority is allowed to modify publisher configs.
25+
/// Creates a config account that stores the authority pubkey.
26+
/// The authority is the account that will be allowed to modify publisher configs.
27+
/// See `Instruction` for the list of required accounts.
28+
/// The config account must be a non-existing PDA account with an expected seed.
2729
pub fn initialize(
2830
program_id: &Pubkey,
2931
accounts: &[AccountInfo],

target_chains/solana/programs/pyth-price-publisher/src/processor/initialize_publisher.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ use {
2929
},
3030
};
3131

32-
// Creates a publisher config account and stores the buffer account pubkey in it.
33-
// Verifies and initializes the buffer account.
32+
/// Creates a publisher config account and stores the buffer account pubkey in it.
33+
/// Verifies and initializes the buffer account.
34+
/// See `Instruction` for the list of required accounts.
35+
/// The config account must be an initialized PDA account with an expected seed.
36+
/// The authority account that signed the instruction
37+
/// must match the authority key stored in the config account.
38+
/// The publisher config account must be a non-existing PDA account with an expected seed.
39+
/// The buffer config must be an existing, zero-filled account owned by the program.
40+
/// Note: we aren't using a PDA for the buffer because the program can't create
41+
/// a PDA larger than 10240 bytes in a single transaction.
42+
/// Note: currently, the publisher config can only be set once and can only contain
43+
/// a single buffer key. If we need to modify the buffer key or create multiple buffer keys
44+
/// per publisher, we'll need to upgrade the program.
3445
pub fn initialize_publisher(
3546
program_id: &Pubkey,
3647
accounts: &[AccountInfo],

target_chains/solana/programs/pyth-price-publisher/src/processor/submit_prices.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ use {
2727
/// its buffer account. The buffer account will be read and applied by the validator
2828
/// to read at the end of the slot.
2929
/// If there are old prices in the account, they will be removed before adding new data.
30+
/// See `Instruction` for the list of required accounts.
31+
/// The publisher config account must be an initialized PDA account with an expected seed
32+
/// (depending on the publisher account that signed the instruction).
33+
/// The buffer account must match the buffer key stored in the publisher config account.
3034
pub fn submit_prices(
3135
program_id: &Pubkey,
3236
accounts: &[AccountInfo],
@@ -54,7 +58,7 @@ pub fn submit_prices(
5458
// Access and update PublisherPrices account with new data.
5559
let mut buffer_data = buffer.data.borrow_mut();
5660
let (header, prices) = buffer::read_mut(*buffer_data)?;
57-
buffer::extend(header, prices, Clock::get()?.slot, prices_data)?;
61+
buffer::update(header, prices, Clock::get()?.slot, prices_data)?;
5862

5963
Ok(())
6064
}

0 commit comments

Comments
 (0)