Skip to content

Commit b59bd70

Browse files
author
Stanisław Drozd
authored
p2w: Monitor and realloc() + rent adjuist on cfg account size change (#305)
* p2w: Monitor and realloc() + rent adjuist on cfg account size change * p2w/set_config: reorder data assignment and realloc()
1 parent 6d528ff commit b59bd70

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

solana/pyth2wormhole/client/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ pub fn gen_set_config_tx(
144144
AccountMeta::new(owner.pubkey(), true),
145145
// payer
146146
AccountMeta::new(payer.pubkey(), true),
147+
// system_program
148+
AccountMeta::new(system_program::id(), false),
147149
];
148150

149151
let ix_data = (
@@ -163,7 +165,6 @@ pub fn gen_set_config_tx(
163165
Ok(tx_signed)
164166
}
165167

166-
167168
pub fn gen_set_is_active_tx(
168169
payer: Keypair,
169170
p2w_addr: Pubkey,

solana/pyth2wormhole/program/src/set_config.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
use solana_program::pubkey::Pubkey;
1+
use borsh::BorshSerialize;
2+
3+
use solana_program::{
4+
program::invoke,
5+
program_error::ProgramError,
6+
pubkey::Pubkey,
7+
rent::Rent,
8+
system_instruction,
9+
sysvar::Sysvar,
10+
};
211
use solitaire::{
312
trace,
413
AccountState,
@@ -18,6 +27,8 @@ use crate::config::{
1827
Pyth2WormholeConfig,
1928
};
2029

30+
use std::cmp::Ordering;
31+
2132
#[derive(FromAccounts)]
2233
pub struct SetConfig<'b> {
2334
/// Current config used by the program
@@ -26,11 +37,13 @@ pub struct SetConfig<'b> {
2637
pub current_owner: Mut<Signer<Info<'b>>>,
2738
/// Payer account for updating the account data
2839
pub payer: Mut<Signer<Info<'b>>>,
40+
/// Used for rent adjustment transfer
41+
pub system_program: Info<'b>,
2942
}
3043

3144
/// Alters the current settings of pyth2wormhole
3245
pub fn set_config(
33-
_ctx: &ExecutionContext,
46+
ctx: &ExecutionContext,
3447
accs: &mut SetConfig,
3548
data: Pyth2WormholeConfig,
3649
) -> SoliResult<()> {
@@ -45,7 +58,32 @@ pub fn set_config(
4558
));
4659
}
4760

61+
let old_size = accs.config.info().data_len();
62+
let new_size = data.try_to_vec()?.len();
63+
64+
// Realloc if mismatched
65+
if old_size != new_size {
66+
accs.config.info().realloc(new_size, false)?;
67+
}
68+
4869
accs.config.1 = data;
4970

71+
// Adjust lamports
72+
let mut acc_lamports = accs.config.info().lamports();
73+
74+
let new_lamports = Rent::get()?.minimum_balance(new_size);
75+
76+
let diff_lamports: u64 = (acc_lamports as i64 - new_lamports as i64).abs() as u64;
77+
78+
if acc_lamports < new_lamports {
79+
// Less than enough lamports, debit the payer
80+
let transfer_ix = system_instruction::transfer(
81+
accs.payer.info().key,
82+
accs.config.info().key,
83+
diff_lamports,
84+
);
85+
invoke(&transfer_ix, ctx.accounts)?;
86+
}
87+
5088
Ok(())
5189
}

0 commit comments

Comments
 (0)