From d7a18777a030ed9145f00b25bcaa359f7363baed Mon Sep 17 00:00:00 2001 From: techvoyagerX Date: Wed, 11 Dec 2024 08:39:32 -0500 Subject: [PATCH] feat: improve code readability and error handling in `vote_weight_record!` macro --- spl/src/governance.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/spl/src/governance.rs b/spl/src/governance.rs index 91676f91fb..502add8034 100644 --- a/spl/src/governance.rs +++ b/spl/src/governance.rs @@ -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) => { @@ -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 { - 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 { - 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(&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 } @@ -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 } @@ -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] = &[]; } };