Skip to content

Commit

Permalink
fix: attempt to fix all clippy warnings related to solana
Browse files Browse the repository at this point in the history
  • Loading branch information
frolvanya committed Feb 1, 2025
1 parent 42fa960 commit 8682729
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ pub const USED_NONCES_ACCOUNT_SIZE: u32 = 8 + (USED_NONCES_PER_ACCOUNT + 7) / 8;
pub const SOLANA_OMNI_BRIDGE_CHAIN_ID: u8 = 2;

#[constant]
pub const MAX_ALLOWED_DECIMALS: u8 = 9;
pub const MAX_ALLOWED_DECIMALS: u8 = 9;
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub struct Initialize<'info> {
seeds::program = wormhole_program.key
)]
/// CHECK: Emitter's sequence account. This is not created until the first
/// message is posted, so it needs to be an [UncheckedAccount] for the
/// message is posted, so it needs to be an [`UncheckedAccount`] for the
/// [`initialize`](crate::initialize) instruction.
/// [`wormhole::post_message`] requires this account be mutable.
pub wormhole_sequence: UncheckedAccount<'info>,
Expand All @@ -88,7 +88,8 @@ pub struct Initialize<'info> {
pub program: Signer<'info>,
}

impl<'info> Initialize<'info> {
impl Initialize<'_> {
#[allow(clippy::too_many_arguments)]
pub fn process(
&mut self,
admin: Pubkey,
Expand Down Expand Up @@ -140,7 +141,7 @@ impl<'info> Initialize<'info> {
},
),
rent.minimum_balance(0) // for account creation
+ UsedNonces::rent_level(USED_NONCES_PER_ACCOUNT as u64 - 1, &rent)?,
+ UsedNonces::rent_level(u64::from(USED_NONCES_PER_ACCOUNT) - 1, &rent)?,
)?;

// If Wormhole requires a fee before posting a message, we need to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::constants::{AUTHORITY_SEED, MAX_ALLOWED_DECIMALS, METADATA_SEED, WRAPPED_MINT_SEED};
use crate::instructions::wormhole_cpi::*;
use crate::instructions::wormhole_cpi::{
WormholeCPI, WormholeCPIBumps, __client_accounts_wormhole_cpi,
__cpi_client_accounts_wormhole_cpi,
};
use crate::state::message::SignedPayload;
use crate::state::message::{
deploy_token::{DeployTokenPayload, DeployTokenResponse},
Expand Down Expand Up @@ -67,7 +70,7 @@ pub struct DeployToken<'info> {
pub token_metadata_program: Program<'info, Metaplex>,
}

impl<'info> DeployToken<'info> {
impl DeployToken<'_> {
pub fn initialize_token_metadata(&self, mut metadata: DeployTokenPayload) -> Result<()> {
let bump = &[self.common.config.bumps.authority];
let signer_seeds = &[&[AUTHORITY_SEED, bump][..]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use anchor_spl::{

use crate::{
constants::{
AUTHORITY_SEED, USED_NONCES_ACCOUNT_SIZE, USED_NONCES_PER_ACCOUNT,
USED_NONCES_SEED, VAULT_SEED,
AUTHORITY_SEED, USED_NONCES_ACCOUNT_SIZE, USED_NONCES_PER_ACCOUNT, USED_NONCES_SEED,
VAULT_SEED,
},
error::ErrorCode,
instructions::wormhole_cpi::*,
instructions::wormhole_cpi::{
WormholeCPI, WormholeCPIBumps, __client_accounts_wormhole_cpi,
__cpi_client_accounts_wormhole_cpi,
},
state::{
message::{
finalize_transfer::{FinalizeTransferPayload, FinalizeTransferResponse},
Expand All @@ -30,7 +33,7 @@ pub struct FinalizeTransfer<'info> {
payer = common.payer,
seeds = [
USED_NONCES_SEED,
&(data.payload.destination_nonce / USED_NONCES_PER_ACCOUNT as u64).to_le_bytes(),
&(data.payload.destination_nonce / u64::from(USED_NONCES_PER_ACCOUNT)).to_le_bytes(),
],
bump,
)]
Expand Down Expand Up @@ -80,7 +83,7 @@ pub struct FinalizeTransfer<'info> {
pub token_program: Interface<'info, TokenInterface>,
}

impl<'info> FinalizeTransfer<'info> {
impl FinalizeTransfer<'_> {
pub fn process(&mut self, data: FinalizeTransferPayload) -> Result<()> {
UsedNonces::use_nonce(
data.destination_nonce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::{
AUTHORITY_SEED, CONFIG_SEED, SOL_VAULT_SEED, USED_NONCES_ACCOUNT_SIZE,
USED_NONCES_PER_ACCOUNT, USED_NONCES_SEED,
},
instructions::wormhole_cpi::*,
instructions::wormhole_cpi::{
WormholeCPI, WormholeCPIBumps, __client_accounts_wormhole_cpi,
__cpi_client_accounts_wormhole_cpi,
},
state::{
config::Config,
message::{
Expand Down Expand Up @@ -33,7 +36,7 @@ pub struct FinalizeTransferSol<'info> {
payer = common.payer,
seeds = [
USED_NONCES_SEED,
&(data.payload.destination_nonce / USED_NONCES_PER_ACCOUNT as u64).to_le_bytes(),
&(data.payload.destination_nonce / u64::from(USED_NONCES_PER_ACCOUNT)).to_le_bytes(),
],
bump,
)]
Expand All @@ -59,7 +62,7 @@ pub struct FinalizeTransferSol<'info> {
pub system_program: Program<'info, System>,
}

impl<'info> FinalizeTransferSol<'info> {
impl FinalizeTransferSol<'_> {
pub fn process(&mut self, data: FinalizeTransferPayload) -> Result<()> {
UsedNonces::use_nonce(
data.destination_nonce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use anchor_spl::{
use crate::{
constants::{AUTHORITY_SEED, SOL_VAULT_SEED, VAULT_SEED},
error::ErrorCode,
instructions::wormhole_cpi::*,
instructions::wormhole_cpi::{
WormholeCPI, WormholeCPIBumps, __client_accounts_wormhole_cpi,
__cpi_client_accounts_wormhole_cpi,
},
state::message::{init_transfer::InitTransferPayload, Payload},
};

Expand Down Expand Up @@ -65,8 +68,8 @@ pub struct InitTransfer<'info> {
pub token_program: Interface<'info, TokenInterface>,
}

impl<'info> InitTransfer<'info> {
pub fn process(&self, payload: InitTransferPayload) -> Result<()> {
impl InitTransfer<'_> {
pub fn process(&self, payload: &InitTransferPayload) -> Result<()> {
if payload.native_fee > 0 {
transfer(
CpiContext::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use anchor_lang::{
use crate::{
constants::SOL_VAULT_SEED,
error::ErrorCode,
instructions::wormhole_cpi::*,
instructions::wormhole_cpi::{
WormholeCPI, WormholeCPIBumps, __client_accounts_wormhole_cpi,
__cpi_client_accounts_wormhole_cpi,
},
state::message::{init_transfer::InitTransferPayload, Payload},
};

Expand All @@ -28,8 +31,8 @@ pub struct InitTransferSol<'info> {
pub common: WormholeCPI<'info>,
}

impl<'info> InitTransferSol<'info> {
pub fn process(&self, payload: InitTransferPayload) -> Result<()> {
impl InitTransferSol<'_> {
pub fn process(&self, payload: &InitTransferPayload) -> Result<()> {
require!(payload.fee == 0, ErrorCode::InvalidFee);

transfer(
Expand All @@ -40,7 +43,9 @@ impl<'info> InitTransferSol<'info> {
to: self.sol_vault.to_account_info(),
},
),
payload.native_fee.checked_add(payload.amount.try_into().unwrap())
payload
.native_fee
.checked_add(payload.amount.try_into().unwrap())
.unwrap(),
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ use anchor_spl::{
},
};

use crate::{constants::METADATA_SEED, instructions::wormhole_cpi::*};
use crate::{
constants::METADATA_SEED,
instructions::wormhole_cpi::{
WormholeCPI, WormholeCPIBumps, __client_accounts_wormhole_cpi,
__cpi_client_accounts_wormhole_cpi,
},
};
use crate::{
constants::{AUTHORITY_SEED, VAULT_SEED},
state::message::Payload,
Expand Down Expand Up @@ -62,7 +68,7 @@ pub struct LogMetadata<'info> {
pub associated_token_program: Program<'info, AssociatedToken>,
}

impl<'info> LogMetadata<'info> {
impl LogMetadata<'_> {
fn parse_metadata_account(&self, address: Pubkey) -> Result<(String, String)> {
let metadata = self
.metadata
Expand All @@ -89,9 +95,7 @@ impl<'info> LogMetadata<'info> {
let mint_with_extension =
StateWithExtensions::<spl_token_2022::state::Mint>::unpack(&mint_data)?;

if let Some(metadata_pointer) =
mint_with_extension.get_extension::<MetadataPointer>().ok()
{
if let Ok(metadata_pointer) = mint_with_extension.get_extension::<MetadataPointer>() {
if metadata_pointer.metadata_address.0 == self.mint.key() {
// Embedded metadata
let metadata =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct WormholeCPI<'info> {
seeds::program = wormhole_program.key
)]
/// CHECK: Emitter's sequence account. This is not created until the first
/// message is posted, so it needs to be an [UncheckedAccount] for the
/// message is posted, so it needs to be an [`UncheckedAccount`] for the
/// [`initialize`](crate::initialize) instruction.
/// [`wormhole::post_message`] requires this account be mutable.
pub sequence: Account<'info, wormhole::SequenceTracker>,
Expand All @@ -67,7 +67,7 @@ pub struct WormholeCPI<'info> {
pub system_program: Program<'info, System>,
}

impl<'info> WormholeCPI<'info> {
impl WormholeCPI<'_> {
pub fn post_message(&self, data: Vec<u8>) -> Result<()> {
// If Wormhole requires a fee before posting a message, we need to
// transfer lamports to the fee collector. Otherwise
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use anchor_lang::prelude::*;
use instructions::*;
use instructions::{
DeployToken, FinalizeTransfer, FinalizeTransferSol, InitTransfer, InitTransferSol, Initialize,
LogMetadata, __client_accounts_deploy_token, __client_accounts_finalize_transfer,
__client_accounts_finalize_transfer_sol, __client_accounts_init_transfer,
__client_accounts_init_transfer_sol, __client_accounts_initialize,
__client_accounts_log_metadata,
};
use state::message::{
deploy_token::DeployTokenPayload, finalize_transfer::FinalizeTransferPayload,
init_transfer::InitTransferPayload, SignedPayload,
Expand Down Expand Up @@ -90,7 +96,7 @@ pub mod bridge_token_factory {
pub fn init_transfer(ctx: Context<InitTransfer>, payload: InitTransferPayload) -> Result<()> {
msg!("Initializing transfer");

ctx.accounts.process(payload)?;
ctx.accounts.process(&payload)?;

Ok(())
}
Expand All @@ -101,7 +107,7 @@ pub mod bridge_token_factory {
) -> Result<()> {
msg!("Initializing transfer");

ctx.accounts.process(payload)?;
ctx.accounts.process(&payload)?;

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ impl Payload for FinalizeTransferPayload {
// 1. destination_nonce
self.destination_nonce.serialize(&mut writer)?;
// 2. transfer_id
writer.write(&[self.transfer_id.origin_chain])?;
writer.write_all(&[self.transfer_id.origin_chain])?;
self.transfer_id.origin_nonce.serialize(&mut writer)?;
// 3. token
writer.write(&[SOLANA_OMNI_BRIDGE_CHAIN_ID])?;
writer.write_all(&[SOLANA_OMNI_BRIDGE_CHAIN_ID])?;
params.0.serialize(&mut writer)?;
// 4. amount
self.amount.serialize(&mut writer)?;
// 5. recipient
writer.write(&[SOLANA_OMNI_BRIDGE_CHAIN_ID])?;
writer.write_all(&[SOLANA_OMNI_BRIDGE_CHAIN_ID])?;
params.1.serialize(&mut writer)?;
// 6. fee_recipient
self.fee_recipient.serialize(&mut writer)?;
Expand All @@ -58,10 +58,10 @@ impl Payload for FinalizeTransferResponse {
// 0. OutgoingMessageType::FinTransfer
OutgoingMessageType::FinTransfer.serialize(&mut writer)?;
// 1. transfer_id
writer.write(&[self.transfer_id.origin_chain])?;
writer.write_all(&[self.transfer_id.origin_chain])?;
self.transfer_id.origin_nonce.serialize(&mut writer)?;
// 2. token
writer.write(&[SOLANA_OMNI_BRIDGE_CHAIN_ID])?;
writer.write_all(&[SOLANA_OMNI_BRIDGE_CHAIN_ID])?;
self.token.serialize(&mut writer)?;
// 3. amount
self.amount.serialize(&mut writer)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Payload for InitTransferPayload {
// 5. fee
self.fee.serialize(&mut writer)?;
// 6. native_fee
(self.native_fee as u128).serialize(&mut writer)?;
u128::from(self.native_fee).serialize(&mut writer)?;
// 7. recipient
self.recipient.serialize(&mut writer)?;
// 8. message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ impl Payload for LogMetadataPayload {
// 0. Message type
OutgoingMessageType::LogMetadata.serialize(&mut writer)?;
// 1. token
writer.write(&[SOLANA_OMNI_BRIDGE_CHAIN_ID])?;
writer.write_all(&[SOLANA_OMNI_BRIDGE_CHAIN_ID])?;
self.token.serialize(&mut writer)?;
// 2. name
self.name.serialize(&mut writer)?;
// 3. symbol
self.symbol.serialize(&mut writer)?;
// 4. decimals
writer.write(&[self.decimals])?;
writer.write_all(&[self.decimals])?;

writer
.into_inner()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ impl<P: Payload> SignedPayload<P> {

let signature = libsecp256k1::Signature::parse_standard_slice(signature_bytes)
.map_err(|_| ProgramError::InvalidArgument)?;
require!(
!signature.s.is_high(),
ErrorCode::MalleableSignature
);
require!(!signature.s.is_high(), ErrorCode::MalleableSignature);

let signer =
secp256k1_recover(&hash.to_bytes(), self.signature[64], signature_bytes)
.map_err(|_| error!(ErrorCode::SignatureVerificationFailed))?;
let signer = secp256k1_recover(&hash.to_bytes(), self.signature[64], signature_bytes)
.map_err(|_| error!(ErrorCode::SignatureVerificationFailed))?;

require!(
signer.0 == *derived_near_bridge_address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl UsedNonces {

pub fn rent_level(nonce: u64, rent: &Rent) -> Result<u64> {
let full = Self::full_rent(rent);
Ok((nonce % USED_NONCES_PER_ACCOUNT as u64 + 1) * full as u64
/ USED_NONCES_PER_ACCOUNT as u64)
Ok((nonce % u64::from(USED_NONCES_PER_ACCOUNT) + 1) * full
/ u64::from(USED_NONCES_PER_ACCOUNT))
}

pub fn use_nonce<'info>(
Expand Down Expand Up @@ -97,7 +97,7 @@ impl UsedNonces {
let mut nonce_slot = unsafe {
used_nonces
.used
.get_unchecked_mut((nonce % USED_NONCES_PER_ACCOUNT as u64) as usize)
.get_unchecked_mut(usize::try_from(nonce % u64::from(USED_NONCES_PER_ACCOUNT))?)
};
require!(!nonce_slot.replace(true), ErrorCode::NonceAlreadyUsed);
}
Expand Down

0 comments on commit 8682729

Please sign in to comment.