Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve code readability and error handling in vote_weight_record! macro #3425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions spl/src/governance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// A macro is exposed so that we can embed the program ID.
/// A macro to embed the program ID and provide a wrapper for the SPL governance program's VoterWeightRecord type.
#[macro_export]
macro_rules! vote_weight_record {
($id:expr) => {
Expand All @@ -7,35 +7,41 @@ macro_rules! vote_weight_record {
pub struct VoterWeightRecord(spl_governance_addin_api::voter_weight::VoterWeightRecord);

impl anchor_lang::AccountDeserialize for VoterWeightRecord {
/// Tries to deserialize the account data into a VoterWeightRecord.
fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
let mut data = buf;
let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord =
anchor_lang::AnchorDeserialize::deserialize(&mut data)
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?;
if !anchor_lang::solana_program::program_pack::IsInitialized::is_initialized(&vwr) {
let data: &[u8] = buf;
let vwr = spl_governance_addin_api::voter_weight::VoterWeightRecord::deserialize(&mut data)
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?;

if !spl_governance_addin_api::voter_weight::VoterWeightRecord::is_initialized(&vwr) {
return Err(anchor_lang::error::ErrorCode::AccountDidNotSerialize.into());
}

Ok(VoterWeightRecord(vwr))
}

/// Tries to deserialize the account data without checking initialization.
fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
let mut data = buf;
let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord =
anchor_lang::AnchorDeserialize::deserialize(&mut data)
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?;
let data: &[u8] = buf;
let vwr = spl_governance_addin_api::voter_weight::VoterWeightRecord::deserialize(&mut data)
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?;

Ok(VoterWeightRecord(vwr))
}
}

impl anchor_lang::AccountSerialize for VoterWeightRecord {
/// Serializes the VoterWeightRecord account data.
fn try_serialize<W: std::io::Write>(&self, writer: &mut W) -> anchor_lang::Result<()> {
anchor_lang::AnchorSerialize::serialize(&self.0, writer)
self.0
.serialize(writer)
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?;
Ok(())
}
}

impl anchor_lang::Owner for VoterWeightRecord {
/// Returns the program ID as the owner of the account.
fn owner() -> Pubkey {
$id
}
Expand All @@ -44,12 +50,14 @@ macro_rules! vote_weight_record {
impl std::ops::Deref for VoterWeightRecord {
type Target = spl_governance_addin_api::voter_weight::VoterWeightRecord;

/// Provides immutable access to the inner VoterWeightRecord.
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::ops::DerefMut for VoterWeightRecord {
/// Provides mutable access to the inner VoterWeightRecord.
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
Expand All @@ -60,6 +68,7 @@ macro_rules! vote_weight_record {

#[cfg(feature = "idl-build")]
impl anchor_lang::Discriminator for VoterWeightRecord {
/// Returns an empty discriminator, since none is needed here.
const DISCRIMINATOR: &'static [u8] = &[];
}
};
Expand Down