Skip to content

Commit 5c48dac

Browse files
authored
Hack the rent exempt bug (#265)
* Hack the rent exempt bug * Fix compile bug * Fix account resize potential problem * Use invoke transfer instead of playing with lamports
1 parent 19e34f5 commit 5c48dac

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

solana/pyth2wormhole/program/src/attest.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use solana_program::{
2222
program_error::ProgramError,
2323
pubkey::Pubkey,
2424
rent::Rent,
25+
system_instruction,
26+
sysvar::Sysvar as SolanaSysvar,
2527
};
2628

2729
use p2w_sdk::{
@@ -332,5 +334,42 @@ pub fn attest(ctx: &ExecutionContext, accs: &mut Attest, data: AttestData) -> So
332334
.as_slice(),
333335
)?;
334336

337+
// NOTE: 2022-09-05
338+
//
339+
// This part is added to avoid rent exemption error that is introduced using
340+
// a wrong implementation in solitaire
341+
//
342+
// This is done after the cross-contract call to get the proper account sizes
343+
// and avoid breaking wormhole call.
344+
//
345+
// It can be removed once wormhole mitigates this problem and upgrades its contract
346+
347+
// Checking the message account balance
348+
let wh_message_balance = accs.wh_message.info().lamports();
349+
let wh_message_rent_exempt = Rent::get()?.minimum_balance(accs.wh_message.info().data_len());
350+
351+
if wh_message_balance < wh_message_rent_exempt {
352+
let required_deposit = wh_message_rent_exempt - wh_message_balance;
353+
354+
let transfer_ix = system_instruction::transfer(
355+
accs.payer.key,
356+
accs.wh_message.info().key,
357+
required_deposit,
358+
);
359+
invoke(&transfer_ix, ctx.accounts)?
360+
}
361+
362+
// Checking the sequence account balance
363+
let wh_sequence_balance = accs.wh_sequence.info().lamports();
364+
let wh_sequence_rent_exempt = Rent::get()?.minimum_balance(accs.wh_sequence.data_len());
365+
366+
if wh_sequence_balance < wh_sequence_rent_exempt {
367+
let required_deposit = wh_sequence_rent_exempt - wh_sequence_balance;
368+
369+
let transfer_ix =
370+
system_instruction::transfer(accs.payer.key, accs.wh_sequence.key, required_deposit);
371+
invoke(&transfer_ix, ctx.accounts)?
372+
}
373+
335374
Ok(())
336375
}

0 commit comments

Comments
 (0)