Governance: Unify borsh serialisation api (#4224)

This commit is contained in:
Sebastian Bor 2023-05-09 16:11:50 +01:00 committed by GitHub
parent 718a6b6074
commit a15fee9d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 72 additions and 72 deletions

View File

@ -66,7 +66,7 @@ pub fn process_add_signatory(
)?;
proposal_data.signatories_count = proposal_data.signatories_count.checked_add(1).unwrap();
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -45,16 +45,16 @@ pub fn process_cancel_proposal(program_id: &Pubkey, accounts: &[AccountInfo]) ->
.assert_token_owner_or_delegate_is_signer(governance_authority_info)?;
proposal_owner_record_data.decrease_outstanding_proposal_count();
proposal_owner_record_data.serialize(&mut *proposal_owner_record_info.data.borrow_mut())?;
proposal_owner_record_data.serialize(&mut proposal_owner_record_info.data.borrow_mut()[..])?;
proposal_data.state = ProposalState::Cancelled;
proposal_data.closed_at = Some(clock.unix_timestamp);
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
// Update Governance active_proposal_count
governance_data.active_proposal_count = governance_data.active_proposal_count.saturating_sub(1);
governance_data.serialize(&mut *governance_info.data.borrow_mut())?;
governance_data.serialize(&mut governance_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -179,21 +179,21 @@ pub fn process_cast_vote(
} else {
proposal_owner_record_data.decrease_outstanding_proposal_count();
proposal_owner_record_data
.serialize(&mut *proposal_owner_record_info.data.borrow_mut())?;
.serialize(&mut proposal_owner_record_info.data.borrow_mut()[..])?;
};
// If the proposal is tipped decrease Governance active_proposal_count
governance_data.active_proposal_count =
governance_data.active_proposal_count.saturating_sub(1);
governance_data.serialize(&mut *governance_info.data.borrow_mut())?;
governance_data.serialize(&mut governance_info.data.borrow_mut()[..])?;
}
let governing_token_owner = voter_token_owner_record_data.governing_token_owner;
voter_token_owner_record_data
.serialize(&mut *voter_token_owner_record_info.data.borrow_mut())?;
.serialize(&mut voter_token_owner_record_info.data.borrow_mut()[..])?;
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
// Create and serialize VoteRecord
let vote_record_data = VoteRecordV2 {

View File

@ -36,6 +36,6 @@ pub fn process_complete_proposal(program_id: &Pubkey, accounts: &[AccountInfo])
proposal_data.closed_at = Some(clock.unix_timestamp);
proposal_data.state = ProposalState::Completed;
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -108,7 +108,7 @@ pub fn process_create_proposal(
.outstanding_proposal_count
.checked_add(1)
.unwrap();
proposal_owner_record_data.serialize(&mut *proposal_owner_record_info.data.borrow_mut())?;
proposal_owner_record_data.serialize(&mut proposal_owner_record_info.data.borrow_mut()[..])?;
assert_valid_proposal_options(&options, &vote_type)?;

View File

@ -132,7 +132,7 @@ pub fn process_deposit_governing_tokens(
.checked_add(amount)
.unwrap();
token_owner_record_data.serialize(&mut *token_owner_record_info.data.borrow_mut())?;
token_owner_record_data.serialize(&mut token_owner_record_info.data.borrow_mut()[..])?;
}
Ok(())

View File

@ -105,11 +105,11 @@ pub fn process_execute_transaction(program_id: &Pubkey, accounts: &[AccountInfo]
proposal_data.state = ProposalState::Completed;
}
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
proposal_transaction_data.executed_at = Some(clock.unix_timestamp);
proposal_transaction_data.execution_status = TransactionExecutionStatus::Success;
proposal_transaction_data.serialize(&mut *proposal_transaction_info.data.borrow_mut())?;
proposal_transaction_data.serialize(&mut proposal_transaction_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -76,13 +76,13 @@ pub fn process_finalize_vote(program_id: &Pubkey, accounts: &[AccountInfo]) -> P
)?;
proposal_owner_record_data.decrease_outstanding_proposal_count();
proposal_owner_record_data.serialize(&mut *proposal_owner_record_info.data.borrow_mut())?;
proposal_owner_record_data.serialize(&mut proposal_owner_record_info.data.borrow_mut()[..])?;
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
// Update Governance active_proposal_count
governance_data.active_proposal_count = governance_data.active_proposal_count.saturating_sub(1);
governance_data.serialize(&mut *governance_info.data.borrow_mut())?;
governance_data.serialize(&mut governance_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -56,10 +56,10 @@ pub fn process_flag_transaction_error(
}
proposal_data.state = ProposalState::ExecutingWithErrors;
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
proposal_transaction_data.execution_status = TransactionExecutionStatus::Error;
proposal_transaction_data.serialize(&mut *proposal_transaction_info.data.borrow_mut())?;
proposal_transaction_data.serialize(&mut proposal_transaction_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -83,7 +83,7 @@ pub fn process_insert_transaction(
}
option.transactions_count = option.transactions_count.checked_add(1).unwrap();
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
let proposal_transaction_data = ProposalTransactionV2 {
account_type: GovernanceAccountType::ProposalTransactionV2,

View File

@ -107,7 +107,7 @@ pub fn process_relinquish_vote(program_id: &Pubkey, accounts: &[AccountInfo]) ->
}
}
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
dispose_account(vote_record_info, beneficiary_info)?;
} else {
@ -119,7 +119,7 @@ pub fn process_relinquish_vote(program_id: &Pubkey, accounts: &[AccountInfo]) ->
}
vote_record_data.is_relinquished = true;
vote_record_data.serialize(&mut *vote_record_info.data.borrow_mut())?;
vote_record_data.serialize(&mut vote_record_info.data.borrow_mut()[..])?;
}
// If the Proposal has been already voted on then we only have to decrease unrelinquished_votes_count
@ -128,7 +128,7 @@ pub fn process_relinquish_vote(program_id: &Pubkey, accounts: &[AccountInfo]) ->
.checked_sub(1)
.unwrap();
token_owner_record_data.serialize(&mut *token_owner_record_info.data.borrow_mut())?;
token_owner_record_data.serialize(&mut token_owner_record_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -48,7 +48,7 @@ pub fn process_remove_signatory(
proposal_data.signatories_count = proposal_data.signatories_count.checked_sub(1).unwrap();
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
dispose_account(signatory_record_info, beneficiary_info)?;

View File

@ -45,7 +45,7 @@ pub fn process_remove_transaction(program_id: &Pubkey, accounts: &[AccountInfo])
let mut option = &mut proposal_data.options[proposal_transaction_data.option_index as usize];
option.transactions_count = option.transactions_count.checked_sub(1).unwrap();
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -75,7 +75,7 @@ pub fn process_revoke_governing_tokens(
.checked_sub(amount)
.ok_or(GovernanceError::InvalidRevokeAmount)?;
token_owner_record_data.serialize(&mut *token_owner_record_info.data.borrow_mut())?;
token_owner_record_data.serialize(&mut token_owner_record_info.data.borrow_mut()[..])?;
burn_spl_tokens_signed(
governing_token_holding_info,

View File

@ -36,7 +36,7 @@ pub fn process_set_governance_config(
governance_data.config = config;
governance_data.serialize(&mut *governance_info.data.borrow_mut())?;
governance_data.serialize(&mut governance_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -25,7 +25,7 @@ pub fn process_set_governance_delegate(
token_owner_record_data.assert_token_owner_or_delegate_is_signer(governance_authority_info)?;
token_owner_record_data.governance_delegate = *new_governance_delegate;
token_owner_record_data.serialize(&mut *token_owner_record_info.data.borrow_mut())?;
token_owner_record_data.serialize(&mut token_owner_record_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -48,7 +48,7 @@ pub fn process_set_realm_authority(
realm_data.authority = new_realm_authority;
realm_data.serialize(&mut *realm_info.data.borrow_mut())?;
realm_data.serialize(&mut realm_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -1,6 +1,5 @@
//! Program state processor
use borsh::BorshSerialize;
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
@ -112,7 +111,10 @@ pub fn process_set_realm_config(
0,
)?;
} else {
realm_config_data.serialize(&mut *realm_config_info.data.borrow_mut())?;
borsh::to_writer(
&mut realm_config_info.data.borrow_mut()[..],
&realm_config_data,
)?;
}
// Update RealmConfig (Realm.config field)
@ -125,7 +127,7 @@ pub fn process_set_realm_config(
realm_data.config.legacy1 = 0;
realm_data.config.legacy2 = 0;
realm_data.serialize(&mut *realm_info.data.borrow_mut())?;
realm_data.serialize(&mut realm_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -67,7 +67,7 @@ pub fn process_sign_off_proposal(program_id: &Pubkey, accounts: &[AccountInfo])
signatory_record_data.assert_can_sign_off(signatory_info)?;
signatory_record_data.signed_off = true;
signatory_record_data.serialize(&mut *signatory_record_info.data.borrow_mut())?;
signatory_record_data.serialize(&mut signatory_record_info.data.borrow_mut()[..])?;
if proposal_data.signatories_signed_off_count == 0 {
proposal_data.signing_off_at = Some(clock.unix_timestamp);
@ -87,7 +87,7 @@ pub fn process_sign_off_proposal(program_id: &Pubkey, accounts: &[AccountInfo])
proposal_data.state = ProposalState::Voting;
}
proposal_data.serialize(&mut *proposal_info.data.borrow_mut())?;
proposal_data.serialize(&mut proposal_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -1,6 +1,5 @@
//! Program state processor
use borsh::BorshSerialize;
use solana_program::{
account_info::{next_account_info, AccountInfo},
clock::Clock,
@ -61,7 +60,10 @@ pub fn process_update_program_metadata(
program_metadata_data.version = VERSION.to_string();
program_metadata_data.updated_at = updated_at;
program_metadata_data.serialize(&mut *program_metadata_info.data.borrow_mut())?;
borsh::to_writer(
&mut program_metadata_info.data.borrow_mut()[..],
&program_metadata_data,
)?;
}
Ok(())

View File

@ -77,7 +77,7 @@ pub fn process_withdraw_governing_tokens(
)?;
token_owner_record_data.governing_token_deposit_amount = 0;
token_owner_record_data.serialize(&mut *token_owner_record_info.data.borrow_mut())?;
token_owner_record_data.serialize(&mut token_owner_record_info.data.borrow_mut()[..])?;
Ok(())
}

View File

@ -236,9 +236,9 @@ impl GovernanceV2 {
}
/// Serializes account into the target buffer
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
pub fn serialize<W: Write>(self, writer: W) -> Result<(), ProgramError> {
if is_governance_v2_account_type(&self.account_type) {
BorshSerialize::serialize(&self, writer)?
borsh::to_writer(writer, &self)?
} else if is_governance_v1_account_type(&self.account_type) {
// V1 account can't be resized and we have to translate it back to the original format
@ -258,7 +258,7 @@ impl GovernanceV2 {
config: self.config,
};
BorshSerialize::serialize(&governance_data_v1, writer)?;
borsh::to_writer(writer, &governance_data_v1)?
}
Ok(())
@ -289,7 +289,7 @@ impl GovernanceV2 {
)?;
}
self.serialize(&mut *governance_info.data.borrow_mut())
self.serialize(&mut governance_info.data.borrow_mut()[..])
}
/// Asserts the provided voting population represented by the given governing_token_mint
@ -352,7 +352,7 @@ impl GovernanceV2 {
}
/// Returns the required deposit amount for creating Nth Proposal based on the number of active proposals
/// where N equals to active_proposal_count - deposit_exempt_proposal_count
/// where N equals to active_proposal_count - deposit_exempt_proposal_count
/// The deposit is not payed unless there are more active Proposal than the exempt amount
///
/// Note: The exact deposit payed for Nth Proposal is N*SECURITY_DEPOSIT_BASE_LAMPORTS + min_rent_for(ProposalDeposit)

View File

@ -950,9 +950,9 @@ impl ProposalV2 {
}
/// Serializes account into the target buffer
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
pub fn serialize<W: Write>(self, writer: W) -> Result<(), ProgramError> {
if self.account_type == GovernanceAccountType::ProposalV2 {
BorshSerialize::serialize(&self, writer)?
borsh::to_writer(writer, &self)?
} else if self.account_type == GovernanceAccountType::ProposalV1 {
// V1 account can't be resized and we have to translate it back to the original format
@ -1003,7 +1003,7 @@ impl ProposalV2 {
description_link: self.description_link,
};
BorshSerialize::serialize(&proposal_data_v1, writer)?;
borsh::to_writer(writer, &proposal_data_v1)?
}
Ok(())
@ -3200,7 +3200,7 @@ mod test {
let proposal_v2 = get_proposal_data(&program_id, &account_info).unwrap();
proposal_v2
.serialize(&mut &mut **account_info.data.borrow_mut())
.serialize(&mut account_info.data.borrow_mut()[..])
.unwrap();
// Assert

View File

@ -136,9 +136,9 @@ impl IsInitialized for ProposalTransactionV2 {
impl ProposalTransactionV2 {
/// Serializes account into the target buffer
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
pub fn serialize<W: Write>(self, writer: W) -> Result<(), ProgramError> {
if self.account_type == GovernanceAccountType::ProposalTransactionV2 {
BorshSerialize::serialize(&self, writer)?
borsh::to_writer(writer, &self)?
} else if self.account_type == GovernanceAccountType::ProposalInstructionV1 {
if self.instructions.len() != 1 {
panic!("Multiple instructions are not supported by ProposalInstructionV1")
@ -161,7 +161,7 @@ impl ProposalTransactionV2 {
execution_status: self.execution_status,
};
BorshSerialize::serialize(&proposal_transaction_data_v1, writer)?;
borsh::to_writer(writer, &proposal_transaction_data_v1)?
}
Ok(())
@ -394,7 +394,7 @@ mod test {
get_proposal_transaction_data(&program_id, &account_info).unwrap();
proposal_transaction_v2
.serialize(&mut &mut **account_info.data.borrow_mut())
.serialize(&mut account_info.data.borrow_mut()[..])
.unwrap();
// Assert

View File

@ -303,9 +303,9 @@ impl RealmV2 {
}
/// Serializes account into the target buffer
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
pub fn serialize<W: Write>(self, writer: W) -> Result<(), ProgramError> {
if self.account_type == GovernanceAccountType::RealmV2 {
BorshSerialize::serialize(&self, writer)?
borsh::to_writer(writer, &self)?
} else if self.account_type == GovernanceAccountType::RealmV1 {
// V1 account can't be resized and we have to translate it back to the original format
@ -324,7 +324,7 @@ impl RealmV2 {
name: self.name,
};
BorshSerialize::serialize(&realm_data_v1, writer)?;
borsh::to_writer(writer, &realm_data_v1)?
}
Ok(())
@ -512,7 +512,7 @@ mod test {
name: String,
#[allow(dead_code)]
/// Realm config args
/// Realm config args
config_args: RealmConfigArgsV1,
},

View File

@ -66,9 +66,9 @@ impl SignatoryRecordV2 {
}
/// Serializes account into the target buffer
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
pub fn serialize<W: Write>(self, writer: W) -> Result<(), ProgramError> {
if self.account_type == GovernanceAccountType::SignatoryRecordV2 {
BorshSerialize::serialize(&self, writer)?
borsh::to_writer(writer, &self)?
} else if self.account_type == GovernanceAccountType::SignatoryRecordV1 {
// V1 account can't be resized and we have to translate it back to the original format
@ -84,7 +84,7 @@ impl SignatoryRecordV2 {
signed_off: self.signed_off,
};
BorshSerialize::serialize(&signatory_record_data_v1, writer)?;
borsh::to_writer(writer, &signatory_record_data_v1)?
}
Ok(())

View File

@ -254,9 +254,9 @@ impl TokenOwnerRecordV2 {
}
/// Serializes account into the target buffer
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
pub fn serialize<W: Write>(self, writer: W) -> Result<(), ProgramError> {
if self.account_type == GovernanceAccountType::TokenOwnerRecordV2 {
BorshSerialize::serialize(&self, writer)?
borsh::to_writer(writer, &self)?
} else if self.account_type == GovernanceAccountType::TokenOwnerRecordV1 {
// V1 account can't be resized and we have to translate it back to the original format
@ -278,7 +278,7 @@ impl TokenOwnerRecordV2 {
governance_delegate: self.governance_delegate,
};
BorshSerialize::serialize(&token_owner_record_data_v1, writer)?;
borsh::to_writer(writer, &token_owner_record_data_v1)?
}
Ok(())
@ -528,9 +528,7 @@ mod test {
};
let mut legacy_data = vec![];
legacy_token_owner_record
.serialize(&mut legacy_data)
.unwrap();
borsh::to_writer(&mut legacy_data, &legacy_token_owner_record).unwrap();
let program_id = Pubkey::new_unique();

View File

@ -132,9 +132,9 @@ impl VoteRecordV2 {
}
/// Serializes account into the target buffer
pub fn serialize<W: Write>(self, writer: &mut W) -> Result<(), ProgramError> {
pub fn serialize<W: Write>(self, writer: W) -> Result<(), ProgramError> {
if self.account_type == GovernanceAccountType::VoteRecordV2 {
BorshSerialize::serialize(&self, writer)?
borsh::to_writer(writer, &self)?
} else if self.account_type == GovernanceAccountType::VoteRecordV1 {
// V1 account can't be resized and we have to translate it back to the original format
@ -159,7 +159,7 @@ impl VoteRecordV2 {
vote_weight,
};
BorshSerialize::serialize(&vote_record_data_v1, writer)?;
borsh::to_writer(writer, &vote_record_data_v1)?
}
Ok(())
@ -264,7 +264,6 @@ pub fn get_vote_record_address<'a>(
#[cfg(test)]
mod test {
use borsh::BorshSerialize;
use solana_program::clock::Epoch;
use super::*;
@ -282,7 +281,7 @@ mod test {
};
let mut account_data = vec![];
vote_record_v1_source.serialize(&mut account_data).unwrap();
borsh::to_writer(&mut account_data, &vote_record_v1_source).unwrap();
let program_id = Pubkey::new_unique();
@ -303,9 +302,8 @@ mod test {
// Act
let vote_record_v2 = get_vote_record_data(&program_id, &account_info).unwrap();
vote_record_v2
.serialize(&mut &mut **account_info.data.borrow_mut())
.serialize(&mut account_info.data.borrow_mut()[..])
.unwrap();
// Assert

View File

@ -357,7 +357,7 @@ impl ProgramTestBench {
account: &T,
) {
let mut account_data = vec![];
account.serialize(&mut account_data).unwrap();
borsh::to_writer(&mut account_data, &account).unwrap();
let data = AccountSharedData::create(
self.rent.minimum_balance(account_data.len()),

View File

@ -72,7 +72,7 @@ pub fn create_and_serialize_account<'a, T: BorshSerialize + AccountMaxSize>(
.borrow_mut()
.copy_from_slice(&serialized_data);
} else {
account_data.serialize(&mut *account_info.data.borrow_mut())?;
borsh::to_writer(&mut account_info.data.borrow_mut()[..], account_data)?;
}
Ok(())
@ -201,7 +201,7 @@ pub fn create_and_serialize_account_with_owner_signed<'a, T: BorshSerialize + Ac
.borrow_mut()
.copy_from_slice(&serialized_data);
} else if account_size > 0 {
account_data.serialize(&mut *account_info.data.borrow_mut())?;
borsh::to_writer(&mut account_info.data.borrow_mut()[..], account_data)?;
}
Ok(())