Skip to content

Commit c623910

Browse files
committed
test(message-buffer): add rust integration tests
add rust-toolchain.toml to pin rust version, add integration tests, update cpi caller auth seeds
1 parent df409c5 commit c623910

File tree

12 files changed

+7444
-693
lines changed

12 files changed

+7444
-693
lines changed

message_buffer/Cargo.lock

Lines changed: 6365 additions & 641 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

message_buffer/programs/message_buffer/src/instructions/resize_buffer.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ use {
1414
},
1515
};
1616

17+
//TODO: make sure this works regardless if the msg_buffer is initialized already or not
18+
// we could be in a sitaution where we have a new price account & new msg_buffer acount
19+
// and we know we need more than 10KB to fit all the messages. In this situation
20+
// we would call create_buffer(10240) then resize_buffer(target_size)
1721
pub fn resize_buffer<'info>(
1822
ctx: Context<'_, '_, '_, 'info, ResizeBuffer<'info>>,
1923
allowed_program_auth: Pubkey,
@@ -36,8 +40,11 @@ pub fn resize_buffer<'info>(
3640
MessageBuffer::HEADER_LEN as u32,
3741
MessageBufferError::MessageBufferTooSmall
3842
);
43+
3944
let target_size = target_size as usize;
40-
let target_size_delta = target_size.saturating_sub(message_buffer_account_info.data_len());
45+
46+
let current_account_size = message_buffer_account_info.data_len();
47+
let target_size_delta = target_size.saturating_sub(current_account_size);
4148
require_gte!(
4249
MAX_PERMITTED_DATA_INCREASE,
4350
target_size_delta,
@@ -61,10 +68,10 @@ pub fn resize_buffer<'info>(
6168
MessageBufferError::InvalidPDA
6269
);
6370

64-
// allow for delta == 0 in case Rent requirements have changed
71+
// allow for target_size == account_size in case Rent requirements have changed
6572
// and additional lamports need to be transferred.
6673
// the realloc step will be a no-op in this case.
67-
if target_size_delta >= 0 {
74+
if target_size >= current_account_size {
6875
let target_rent = Rent::get()?.minimum_balance(target_size);
6976
if message_buffer_account_info.lamports() < target_rent {
7077
system_program::transfer(

message_buffer/programs/message_buffer/src/lib.rs

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mod instructions;
1+
pub mod instructions;
22
mod macros;
33
mod state;
44

@@ -15,13 +15,62 @@ declare_id!("Vbmv1jt4vyuqBZcpYPpnVhrqVe5e6ZPb6JxDcffRHUM");
1515
pub mod message_buffer {
1616
use super::*;
1717

18+
pub fn test(ctx: Context<Test>, admin: Pubkey) -> Result<()> {
19+
use anchor_lang::{
20+
prelude::*,
21+
system_program::{
22+
self,
23+
Transfer,
24+
},
25+
};
26+
msg!("in test");
27+
let (whitelist_pda, whitelist_bump) =
28+
Pubkey::find_program_address(&[b"message", b"whitelist"], &crate::ID);
29+
msg!("crate_id: {:?}", crate::ID);
30+
let payer_pk = ctx.accounts.payer.key();
31+
msg!("payer_pk: {:?}", payer_pk);
32+
msg!("admin: {:?}", admin);
33+
msg!(
34+
"whitelist_pda: {:?}, whitelist_bump: {:?}",
35+
whitelist_pda,
36+
whitelist_bump
37+
);
38+
let whitelist_acct_pk = ctx.accounts.whitelist.key();
39+
msg!("whitelist_acct_pk: {:?}", whitelist_acct_pk);
40+
41+
// let whitelist = &mut ctx.accounts.whitelist;
42+
// whitelist.bump = w_bump;
43+
44+
// let whitelist_bump = *ctx.bumps.get("whitelist").unwrap();
45+
// msg!("whitelist_bump: {:?}", whitelist_bump);
46+
// whitelist.admin = Pubkey::default();
47+
// let init = &mut ctx.accounts.init;
48+
// init.bump = *ctx.bumps.get("init").unwrap();
49+
// msg!("[test]: initialized init");
50+
let system_prog = ctx.accounts.system_program.key();
51+
msg!("system_prog: {:?}", system_prog);
52+
system_program::transfer(
53+
CpiContext::new(
54+
ctx.accounts.system_program.to_account_info(),
55+
Transfer {
56+
from: ctx.accounts.payer.to_account_info(),
57+
to: ctx.accounts.whitelist.to_account_info(),
58+
},
59+
),
60+
Rent::get()?.minimum_balance(0),
61+
)?;
62+
msg!("transferred to payer");
63+
Ok(())
64+
}
1865

1966
/// Initializes the whitelist and sets it's admin to the provided pubkey
2067
/// Once initialized, the authority must sign all further changes to the whitelist.
2168
pub fn initialize(ctx: Context<Initialize>, admin: Pubkey) -> Result<()> {
2269
require_keys_neq!(admin, Pubkey::default());
70+
msg!("in initialize");
2371
let whitelist = &mut ctx.accounts.whitelist;
2472
whitelist.bump = *ctx.bumps.get("whitelist").unwrap();
73+
// whitelist.bump = bump;
2574
whitelist.admin = admin;
2675
Ok(())
2776
}
@@ -149,6 +198,41 @@ pub mod message_buffer {
149198
}
150199
}
151200

201+
#[derive(Accounts)]
202+
pub struct Test<'info> {
203+
#[account(mut)]
204+
pub payer: Signer<'info>,
205+
206+
// #[account(
207+
// init,
208+
// payer = payer,
209+
// seeds = [b"message".as_ref(), b"whitelist".as_ref()],
210+
// bump,
211+
// space = 8 + Whitelist::INIT_SPACE,
212+
// )]
213+
// pub whitelist: Account<'info, Whitelist>,
214+
#[account(
215+
seeds = [b"message".as_ref(), b"whitelist".as_ref()],
216+
bump,
217+
)]
218+
/// CHECK: test
219+
pub whitelist: UncheckedAccount<'info>,
220+
pub system_program: Program<'info, System>,
221+
// #[account(
222+
// init,
223+
// payer = payer,
224+
// seeds = [b"message".as_ref(), b"init".as_ref()],
225+
// bump,
226+
// space = 8,
227+
// )]
228+
// pub init: Account<'info, Init>,
229+
}
230+
231+
#[account]
232+
pub struct Init {
233+
pub bump: u8,
234+
}
235+
152236
#[derive(Accounts)]
153237
pub struct Initialize<'info> {
154238
#[account(mut)]
@@ -158,7 +242,7 @@ pub struct Initialize<'info> {
158242
payer = payer,
159243
seeds = [b"message".as_ref(), b"whitelist".as_ref()],
160244
bump,
161-
space = 8 + Whitelist::INIT_SPACE
245+
space = 8 + Whitelist::INIT_SPACE,
162246
)]
163247
pub whitelist: Account<'info, Whitelist>,
164248
pub system_program: Program<'info, System>,

message_buffer/programs/message_buffer/src/state/whitelist.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use {
1111
pub struct Whitelist {
1212
pub bump: u8,
1313
pub admin: Pubkey,
14+
// This is used by the `#[derive(InitSpace)]`
15+
// to determine initial account size
1416
#[max_len(32)]
1517
pub allowed_programs: Vec<Pubkey>,
1618
}

message_buffer/programs/mock-cpi-caller/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@ no-idl = []
1414
no-log-ix-name = []
1515
cpi = ["no-entrypoint"]
1616
default = []
17+
test-bpf = []
1718

1819
[dependencies]
1920
anchor-lang = "0.27.0"
2021
message_buffer = { path = "../message_buffer", features = ["cpi"] }
2122
# needed for the new #[account(zero_copy)] in anchor 0.27.0
2223
bytemuck = { version = "1.4.0", features = ["derive", "min_const_generics"]}
24+
25+
[dev-dependencies]
26+
anchor-client = { version = "0.27.0", features = ["debug"] }
27+
solana-rpc-client-api = "1.14.16"
28+
solana-sdk = "1.14.16"
29+
solana-sdk-macro = "1.14.16"
30+
solana-program-test = "1.14.16"
31+
solana-validator = "1.14.16"
32+
assert_matches = "1.4.0"
33+
solana-logger = "1.14.16"
34+
byteorder = "1.4.3"

message_buffer/programs/mock-cpi-caller/src/instructions/add_price.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use {
33
instructions::{
44
sighash,
55
ACCUMULATOR_UPDATER_IX_NAME,
6-
CPI,
6+
UPD_PRICE_WRITE,
77
},
88
message::{
99
get_schemas,
@@ -51,13 +51,13 @@ pub fn add_price<'info>(
5151
// Note: normally pyth oracle add_price wouldn't call emit_accumulator_inputs
5252
// since add_price doesn't actually add/update any price data we would
5353
// want included in the accumulator anyways. This is just for testing
54-
AddPrice::emit_accumulator_inputs(ctx, inputs)
54+
AddPrice::emit_messages(ctx, inputs)
5555
}
5656

5757

5858
impl<'info> AddPrice<'info> {
59-
/// Invoke accumulator-updater emit-inputs ix cpi call using solana
60-
pub fn emit_accumulator_inputs(
59+
/// Invoke message_buffer::put_all ix cpi call using solana
60+
pub fn emit_messages(
6161
ctx: Context<'_, '_, '_, 'info, AddPrice<'info>>,
6262
inputs: Vec<Vec<u8>>,
6363
) -> anchor_lang::Result<()> {
@@ -89,17 +89,19 @@ impl<'info> AddPrice<'info> {
8989
// that won't be available in the oracle program
9090
let (_, bump) = Pubkey::find_program_address(
9191
&[
92+
UPD_PRICE_WRITE.as_bytes(),
9293
ctx.accounts.message_buffer_program.key().as_ref(),
93-
CPI.as_bytes(),
9494
],
9595
&crate::ID,
9696
);
9797
anchor_lang::solana_program::program::invoke_signed(
9898
&create_inputs_ix,
9999
account_infos,
100100
&[&[
101+
UPD_PRICE_WRITE.as_bytes(),
101102
ctx.accounts.message_buffer_program.key().as_ref(),
102-
CPI.as_bytes(),
103+
// ctx.accounts.message_buffer_program.key().as_ref(),
104+
// UPD_PRICE_WRITE.as_bytes(),
103105
&[bump],
104106
]],
105107
)?;
@@ -136,7 +138,7 @@ pub struct AddPrice<'info> {
136138
/// PDA representing this program's authority
137139
/// to call the accumulator program
138140
#[account(
139-
seeds = [message_buffer_program.key().as_ref(), b"cpi".as_ref()],
141+
seeds = [b"upd_price_write".as_ref(), message_buffer_program.key().as_ref()],
140142
owner = system_program::System::id(),
141143
bump,
142144
)]

message_buffer/programs/mock-cpi-caller/src/instructions/cpi_max_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ pub fn cpi_max_test<'info>(
3333
msg!("input_len: {}", input_len);
3434

3535

36-
UpdatePrice::emit_accumulator_inputs(ctx, inputs)
36+
UpdatePrice::emit_messages(ctx, inputs)
3737
}

message_buffer/programs/mock-cpi-caller/src/instructions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ pub fn sighash(namespace: &str, name: &str) -> [u8; 8] {
2424
}
2525

2626
pub const ACCUMULATOR_UPDATER_IX_NAME: &str = "put_all";
27-
pub const CPI: &str = "cpi";
27+
pub const UPD_PRICE_WRITE: &str = "upd_price_write";

message_buffer/programs/mock-cpi-caller/src/instructions/update_price.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use {
33
instructions::{
44
sighash,
55
ACCUMULATOR_UPDATER_IX_NAME,
6-
CPI,
6+
UPD_PRICE_WRITE,
77
},
88
message::{
99
price::{
@@ -45,7 +45,7 @@ pub struct UpdatePrice<'info> {
4545
/// CHECK: whitelist
4646
pub accumulator_whitelist: UncheckedAccount<'info>,
4747
#[account(
48-
seeds = [message_buffer_program.key().as_ref(), b"cpi".as_ref()],
48+
seeds = [b"upd_price_write".as_ref(), message_buffer_program.key().as_ref()],
4949
owner = system_program::System::id(),
5050
bump,
5151
)]
@@ -76,12 +76,12 @@ pub fn update_price<'info>(
7676
}
7777

7878

79-
UpdatePrice::emit_accumulator_inputs(ctx, inputs)
79+
UpdatePrice::emit_messages(ctx, inputs)
8080
}
8181

8282
impl<'info> UpdatePrice<'info> {
83-
/// Invoke accumulator-updater emit-inputs ix cpi call
84-
pub fn emit_accumulator_inputs(
83+
/// Invoke message_buffer::put_all ix cpi call
84+
pub fn emit_messages(
8585
ctx: Context<'_, '_, '_, 'info, UpdatePrice<'info>>,
8686
values: Vec<Vec<u8>>,
8787
) -> anchor_lang::Result<()> {
@@ -113,20 +113,38 @@ impl<'info> UpdatePrice<'info> {
113113
// that won't be available in the oracle program
114114
let (_, bump) = Pubkey::find_program_address(
115115
&[
116+
UPD_PRICE_WRITE.as_bytes(),
116117
ctx.accounts.message_buffer_program.key().as_ref(),
117-
CPI.as_bytes(),
118118
],
119119
&crate::ID,
120120
);
121121
anchor_lang::solana_program::program::invoke_signed(
122122
&update_inputs_ix,
123123
account_infos,
124124
&[&[
125+
UPD_PRICE_WRITE.as_bytes(),
125126
ctx.accounts.message_buffer_program.key().as_ref(),
126-
CPI.as_bytes(),
127+
// ctx.accounts.message_buffer_program.key().as_ref(),
128+
// UPD_PRICE_WRITE.as_bytes(),
127129
&[bump],
128130
]],
129131
)?;
132+
// let (_, bump) = Pubkey::find_program_address(
133+
// &[
134+
// ctx.accounts.message_buffer_program.key().as_ref(),
135+
// UPD_PRICE_WRITE.as_bytes(),
136+
// ],
137+
// &crate::ID,
138+
// );
139+
// anchor_lang::solana_program::program::invoke_signed(
140+
// &update_inputs_ix,
141+
// account_infos,
142+
// &[&[
143+
// ctx.accounts.message_buffer_program.key().as_ref(),
144+
// UPD_PRICE_WRITE.as_bytes(),
145+
// &[bump],
146+
// ]],
147+
// )?;
130148
Ok(())
131149
}
132150
}

0 commit comments

Comments
 (0)