Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 074c53d

Browse files
authored
borsh: Use to_writer API to serialize (#4228)
1 parent a15fee9 commit 074c53d

File tree

6 files changed

+49
-41
lines changed

6 files changed

+49
-41
lines changed

binary-option/program/src/processor.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
assert_initialized, assert_keys_equal, assert_keys_unequal, assert_owned_by,
1212
},
1313
};
14-
use borsh::{BorshDeserialize, BorshSerialize};
14+
use borsh::BorshDeserialize;
1515
use solana_program::{
1616
account_info::{next_account_info, AccountInfo},
1717
entrypoint::ProgramResult,
@@ -175,8 +175,10 @@ pub fn process_initialize_binary_option(
175175
binary_option.escrow_mint_account_pubkey = *escrow_mint_info.key;
176176
binary_option.escrow_account_pubkey = *escrow_account_info.key;
177177
binary_option.owner = *update_authority_info.key;
178-
binary_option.serialize(&mut *binary_option_account_info.data.borrow_mut())?;
179-
178+
borsh::to_writer(
179+
&mut binary_option_account_info.data.borrow_mut()[..],
180+
&binary_option,
181+
)?;
180182
Ok(())
181183
}
182184

@@ -576,7 +578,10 @@ pub fn process_trade(
576578
s_l,
577579
long_token_mint.decimals,
578580
)?;
579-
binary_option.serialize(&mut *binary_option_account_info.data.borrow_mut())?;
581+
borsh::to_writer(
582+
&mut binary_option_account_info.data.borrow_mut()[..],
583+
&binary_option,
584+
)?;
580585
Ok(())
581586
}
582587

@@ -607,7 +612,10 @@ pub fn process_settle(_program_id: &Pubkey, accounts: &[AccountInfo]) -> Program
607612
return Err(BinaryOptionError::InvalidWinner.into());
608613
}
609614
binary_option.settled = true;
610-
binary_option.serialize(&mut *binary_option_account_info.data.borrow_mut())?;
615+
borsh::to_writer(
616+
&mut binary_option_account_info.data.borrow_mut()[..],
617+
&binary_option,
618+
)?;
611619
Ok(())
612620
}
613621

@@ -724,6 +732,9 @@ pub fn process_collect(program_id: &Pubkey, accounts: &[AccountInfo]) -> Program
724732
)?;
725733
binary_option.decrement_supply(reward)?;
726734
}
727-
binary_option.serialize(&mut *binary_option_account_info.data.borrow_mut())?;
735+
borsh::to_writer(
736+
&mut binary_option_account_info.data.borrow_mut()[..],
737+
&binary_option,
738+
)?;
728739
Ok(())
729740
}

binary-oracle-pair/program/src/processor.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
instruction::PoolInstruction,
66
state::{Decision, Pool, POOL_VERSION},
77
};
8-
use borsh::{BorshDeserialize, BorshSerialize};
8+
use borsh::BorshDeserialize;
99
use solana_program::{
1010
account_info::next_account_info,
1111
account_info::AccountInfo,
@@ -300,8 +300,7 @@ impl Processor {
300300
pool.decide_end_slot = decide_end_slot;
301301
pool.decision = Decision::Undecided;
302302

303-
pool.serialize(&mut *pool_account_info.data.borrow_mut())
304-
.map_err(|e| e.into())
303+
borsh::to_writer(&mut pool_account_info.data.borrow_mut()[..], &pool).map_err(|e| e.into())
305304
}
306305

307306
/// Process Deposit instruction
@@ -556,8 +555,7 @@ impl Processor {
556555
Decision::Fail
557556
};
558557

559-
pool.serialize(&mut *pool_account_info.data.borrow_mut())
560-
.map_err(|e| e.into())
558+
borsh::to_writer(&mut pool_account_info.data.borrow_mut()[..], &pool).map_err(|e| e.into())
561559
}
562560

563561
/// Processes an instruction

record/program/src/processor.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use {
66
instruction::RecordInstruction,
77
state::{Data, RecordData},
88
},
9-
borsh::{BorshDeserialize, BorshSerialize},
9+
borsh::BorshDeserialize,
1010
solana_program::{
1111
account_info::{next_account_info, AccountInfo},
1212
entrypoint::ProgramResult,
@@ -53,8 +53,7 @@ pub fn process_instruction(
5353

5454
account_data.authority = *authority_info.key;
5555
account_data.version = RecordData::CURRENT_VERSION;
56-
account_data
57-
.serialize(&mut *data_info.data.borrow_mut())
56+
borsh::to_writer(&mut data_info.data.borrow_mut()[..], &account_data)
5857
.map_err(|e| e.into())
5958
}
6059

@@ -90,8 +89,7 @@ pub fn process_instruction(
9089
}
9190
check_authority(authority_info, &account_data.authority)?;
9291
account_data.authority = *new_authority_info.key;
93-
account_data
94-
.serialize(&mut *data_info.data.borrow_mut())
92+
borsh::to_writer(&mut data_info.data.borrow_mut()[..], &account_data)
9593
.map_err(|e| e.into())
9694
}
9795

@@ -113,8 +111,7 @@ pub fn process_instruction(
113111
.checked_add(data_lamports)
114112
.ok_or(RecordError::Overflow)?;
115113
account_data.data = Data::default();
116-
account_data
117-
.serialize(&mut *data_info.data.borrow_mut())
114+
borsh::to_writer(&mut data_info.data.borrow_mut()[..], &account_data)
118115
.map_err(|e| e.into())
119116
}
120117
}

stake-pool/program/src/big_vec.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use {
55
arrayref::array_ref,
6-
borsh::{BorshDeserialize, BorshSerialize},
6+
borsh::BorshDeserialize,
77
solana_program::{
88
program_error::ProgramError, program_memory::sol_memmove, program_pack::Pack,
99
},
@@ -79,7 +79,7 @@ impl<'data> BigVec<'data> {
7979
}
8080

8181
let mut vec_len_ref = &mut self.data[0..VEC_SIZE_BYTES];
82-
vec_len.serialize(&mut vec_len_ref)?;
82+
borsh::to_writer(&mut vec_len_ref, &vec_len)?;
8383

8484
Ok(())
8585
}
@@ -116,7 +116,7 @@ impl<'data> BigVec<'data> {
116116
let end_index = start_index + T::LEN;
117117

118118
vec_len += 1;
119-
vec_len.serialize(&mut vec_len_ref)?;
119+
borsh::to_writer(&mut vec_len_ref, &vec_len)?;
120120

121121
if self.data.len() < end_index {
122122
return Err(ProgramError::AccountDataTooSmall);
@@ -252,7 +252,7 @@ mod tests {
252252
const LEN: usize = 8;
253253
fn pack_into_slice(&self, data: &mut [u8]) {
254254
let mut data = data;
255-
self.value.serialize(&mut data).unwrap();
255+
borsh::to_writer(&mut data, &self.value).unwrap();
256256
}
257257
fn unpack_from_slice(src: &[u8]) -> Result<Self, ProgramError> {
258258
Ok(TestStruct {

stake-pool/program/src/processor.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use {
1414
AUTHORITY_DEPOSIT, AUTHORITY_WITHDRAW, EPHEMERAL_STAKE_SEED_PREFIX,
1515
TRANSIENT_STAKE_SEED_PREFIX,
1616
},
17-
borsh::{BorshDeserialize, BorshSerialize},
17+
borsh::BorshDeserialize,
1818
mpl_token_metadata::{
1919
instruction::{create_metadata_accounts_v3, update_metadata_accounts_v2},
2020
pda::find_metadata_account,
@@ -901,7 +901,10 @@ impl Processor {
901901
)?;
902902
}
903903

904-
validator_list.serialize(&mut *validator_list_info.data.borrow_mut())?;
904+
borsh::to_writer(
905+
&mut validator_list_info.data.borrow_mut()[..],
906+
&validator_list,
907+
)?;
905908

906909
stake_pool.account_type = AccountType::StakePool;
907910
stake_pool.manager = *manager_info.key;
@@ -932,8 +935,7 @@ impl Processor {
932935
stake_pool.last_epoch_pool_token_supply = 0;
933936
stake_pool.last_epoch_total_lamports = 0;
934937

935-
stake_pool
936-
.serialize(&mut *stake_pool_info.data.borrow_mut())
938+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)
937939
.map_err(|e| e.into())
938940
}
939941

@@ -1216,7 +1218,7 @@ impl Processor {
12161218
if stake_pool.preferred_withdraw_validator_vote_address == Some(vote_account_address) {
12171219
stake_pool.preferred_withdraw_validator_vote_address = None;
12181220
}
1219-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
1221+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
12201222

12211223
Ok(())
12221224
}
@@ -2136,7 +2138,7 @@ impl Processor {
21362138
stake_pool.preferred_withdraw_validator_vote_address = vote_account_address
21372139
}
21382140
};
2139-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
2141+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
21402142
Ok(())
21412143
}
21422144

@@ -2549,7 +2551,7 @@ impl Processor {
25492551
let pool_mint = StateWithExtensions::<Mint>::unpack(&pool_mint_data)?;
25502552
stake_pool.pool_token_supply = pool_mint.base.supply;
25512553

2552-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
2554+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
25532555

25542556
Ok(())
25552557
}
@@ -2842,7 +2844,7 @@ impl Processor {
28422844
.total_lamports
28432845
.checked_add(total_deposit_lamports)
28442846
.ok_or(StakePoolError::CalculationFailure)?;
2845-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
2847+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
28462848

28472849
validator_stake_info.active_stake_lamports = validator_stake_account_info.lamports();
28482850

@@ -2992,7 +2994,7 @@ impl Processor {
29922994
.total_lamports
29932995
.checked_add(deposit_lamports)
29942996
.ok_or(StakePoolError::CalculationFailure)?;
2995-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
2997+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
29962998

29972999
Ok(())
29983000
}
@@ -3272,7 +3274,7 @@ impl Processor {
32723274
.total_lamports
32733275
.checked_sub(withdraw_lamports)
32743276
.ok_or(StakePoolError::CalculationFailure)?;
3275-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
3277+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
32763278

32773279
if let Some((validator_list_item, withdraw_source)) = validator_list_item_info {
32783280
match withdraw_source {
@@ -3450,7 +3452,7 @@ impl Processor {
34503452
.total_lamports
34513453
.checked_sub(withdraw_lamports)
34523454
.ok_or(StakePoolError::CalculationFailure)?;
3453-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
3455+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
34543456

34553457
Ok(())
34563458
}
@@ -3642,7 +3644,7 @@ impl Processor {
36423644

36433645
stake_pool.manager = *new_manager_info.key;
36443646
stake_pool.manager_fee_account = *new_manager_fee_info.key;
3645-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
3647+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
36463648
Ok(())
36473649
}
36483650

@@ -3671,7 +3673,7 @@ impl Processor {
36713673

36723674
fee.check_too_high()?;
36733675
stake_pool.update_fee(&fee)?;
3674-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
3676+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
36753677
Ok(())
36763678
}
36773679

@@ -3695,7 +3697,7 @@ impl Processor {
36953697
return Err(StakePoolError::SignatureMissing.into());
36963698
}
36973699
stake_pool.staker = *new_staker_info.key;
3698-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
3700+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
36993701
Ok(())
37003702
}
37013703

@@ -3729,7 +3731,7 @@ impl Processor {
37293731
FundingType::SolDeposit => stake_pool.sol_deposit_authority = new_authority,
37303732
FundingType::SolWithdraw => stake_pool.sol_withdraw_authority = new_authority,
37313733
}
3732-
stake_pool.serialize(&mut *stake_pool_info.data.borrow_mut())?;
3734+
borsh::to_writer(&mut stake_pool_info.data.borrow_mut()[..], &stake_pool)?;
37333735
Ok(())
37343736
}
37353737

stake-pool/program/src/state.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ impl Pack for ValidatorStakeInfo {
701701
let mut data = data;
702702
// Removing this unwrap would require changing from `Pack` to some other
703703
// trait or `bytemuck`, so it stays in for now
704-
self.serialize(&mut data).unwrap();
704+
borsh::to_writer(&mut data, self).unwrap();
705705
}
706706
fn unpack_from_slice(src: &[u8]) -> Result<Self, ProgramError> {
707707
let unpacked = Self::try_from_slice(src)?;
@@ -1043,7 +1043,7 @@ mod test {
10431043
let stake_list = uninitialized_validator_list();
10441044
let mut byte_vec = vec![0u8; size];
10451045
let mut bytes = byte_vec.as_mut_slice();
1046-
stake_list.serialize(&mut bytes).unwrap();
1046+
borsh::to_writer(&mut bytes, &stake_list).unwrap();
10471047
let stake_list_unpacked = try_from_slice_unchecked::<ValidatorList>(&byte_vec).unwrap();
10481048
assert_eq!(stake_list_unpacked, stake_list);
10491049

@@ -1057,15 +1057,15 @@ mod test {
10571057
};
10581058
let mut byte_vec = vec![0u8; size];
10591059
let mut bytes = byte_vec.as_mut_slice();
1060-
stake_list.serialize(&mut bytes).unwrap();
1060+
borsh::to_writer(&mut bytes, &stake_list).unwrap();
10611061
let stake_list_unpacked = try_from_slice_unchecked::<ValidatorList>(&byte_vec).unwrap();
10621062
assert_eq!(stake_list_unpacked, stake_list);
10631063

10641064
// With several accounts
10651065
let stake_list = test_validator_list(max_validators);
10661066
let mut byte_vec = vec![0u8; size];
10671067
let mut bytes = byte_vec.as_mut_slice();
1068-
stake_list.serialize(&mut bytes).unwrap();
1068+
borsh::to_writer(&mut bytes, &stake_list).unwrap();
10691069
let stake_list_unpacked = try_from_slice_unchecked::<ValidatorList>(&byte_vec).unwrap();
10701070
assert_eq!(stake_list_unpacked, stake_list);
10711071
}

0 commit comments

Comments
 (0)