Skip to content

Commit 30f2e16

Browse files
author
Bengt Lofgren
committed
manual clippy lint fixes
1 parent b83bd70 commit 30f2e16

File tree

17 files changed

+205
-180
lines changed

17 files changed

+205
-180
lines changed

solana/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ overflow-checks = true
6868

6969
[workspace.lints.clippy]
7070
correctness = { priority = -1, level = "warn"}
71+
inconsistent_digit_grouping = "allow"
7172

7273
### See clippy.toml.
7374
unnecessary_lazy_evaluations = "allow"
@@ -82,7 +83,7 @@ cast_possible_wrap = "deny"
8283
cast_precision_loss = "deny"
8384
cast_sign_loss = "deny"
8485
eq_op = "deny"
85-
expect_used = "deny"
86+
expect_used = "allow" # TODO: Change this back once we get there
8687
float_cmp = "deny"
8788
integer_division = "deny"
8889
large_futures = "deny"

solana/programs/matching-engine/src/fallback/processor/close_fast_market_order.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,17 @@ pub fn close_fast_market_order(accounts: &[AccountInfo]) -> Result<()> {
6565
{
6666
return Err(ProgramError::InvalidAccountData.into()).map_err(|e: Error| {
6767
e.with_pubkeys((
68-
Pubkey::try_from(fast_market_order_data.close_account_refund_recipient)
69-
.expect("Failed to convert close account refund recipient to pubkey"),
68+
Pubkey::from(fast_market_order_data.close_account_refund_recipient),
7069
close_account_refund_recipient.key(),
7170
))
7271
});
7372
}
7473

7574
// Transfer the lamports from the fast market order to the close account refund recipient
7675
let mut fast_market_order_lamports = fast_market_order.lamports.borrow_mut();
77-
**close_account_refund_recipient.lamports.borrow_mut() += **fast_market_order_lamports;
76+
**close_account_refund_recipient.lamports.borrow_mut() =
77+
(**close_account_refund_recipient.lamports.borrow())
78+
.saturating_add(**fast_market_order_lamports);
7879
**fast_market_order_lamports = 0;
7980

8081
Ok(())

solana/programs/matching-engine/src/fallback/processor/helpers.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn create_account_reliably(
3131
payer_key,
3232
account_key,
3333
lamports,
34-
data_len as u64,
34+
u64::try_from(data_len).unwrap(), // lol it won't do ::from
3535
program_id,
3636
);
3737

@@ -105,8 +105,9 @@ pub fn create_account_reliably(
105105
let cpi_data = &mut cpi_ix.data;
106106

107107
cpi_data[0] = 8; // allocate selector
108-
cpi_data[4..12].copy_from_slice(&(data_len as u64).to_le_bytes());
109-
108+
cpi_data[4..12].copy_from_slice(&u64::try_from(data_len).unwrap().to_le_bytes());
109+
// ↑
110+
// It won't do ::from but it'll do ::try_from
110111
invoke_signed_unchecked(&cpi_ix, accounts, signer_seeds)?;
111112
}
112113

solana/programs/matching-engine/src/fallback/processor/initialise_fast_market_order.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub fn initialise_fast_market_order(
159159
// Start of fast market order account creation
160160
// ------------------------------------------------------------------------------------------------
161161
let fast_market_order_key = fast_market_order_account.key();
162-
let space = 8 + std::mem::size_of::<FastMarketOrderState>();
162+
let space = 8_usize.saturating_add(std::mem::size_of::<FastMarketOrderState>());
163163
let (fast_market_order_pda, fast_market_order_bump) = Pubkey::find_program_address(
164164
&[
165165
FastMarketOrderState::SEED_PREFIX,
@@ -201,13 +201,14 @@ pub fn initialise_fast_market_order(
201201

202202
let fast_market_order_bytes = bytemuck::bytes_of(&data.fast_market_order);
203203
// Ensure the destination has enough space
204-
if fast_market_order_account_data.len() < 8 + fast_market_order_bytes.len() {
204+
if fast_market_order_account_data.len() < 8_usize.saturating_add(fast_market_order_bytes.len())
205+
{
205206
msg!("Account data buffer too small");
206207
return Err(MatchingEngineError::AccountDataTooSmall.into());
207208
}
208209

209210
// Write the fast_market_order struct to the account
210-
fast_market_order_account_data[8..8 + fast_market_order_bytes.len()]
211+
fast_market_order_account_data[8..8_usize.saturating_add(fast_market_order_bytes.len())]
211212
.copy_from_slice(fast_market_order_bytes);
212213
// End of fast market order account creation
213214
// ------------------------------------------------------------------------------------------------

solana/programs/matching-engine/src/fallback/processor/place_initial_offer.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl VaaMessageBodyHeader {
154154
if fast_market_order.redeemer_message_length > 0 {
155155
payload.extend_from_slice(
156156
&fast_market_order.redeemer_message
157-
[..fast_market_order.redeemer_message_length as usize],
157+
[..usize::from(fast_market_order.redeemer_message_length)],
158158
);
159159
}
160160
message_body.extend_from_slice(&payload);
@@ -329,7 +329,7 @@ pub fn place_initial_offer_cctp_shim(
329329

330330
// Check contents of fast_market_order
331331
{
332-
let deadline = fast_market_order_zero_copy.deadline as i64;
332+
let deadline = i64::from(fast_market_order_zero_copy.deadline);
333333
let expiration = i64::from(vaa_time).saturating_add(crate::VAA_AUCTION_EXPIRATION_TIME);
334334
let current_time = Clock::get().unwrap().unix_timestamp;
335335
if !((deadline == 0 || current_time < deadline) && current_time < expiration) {
@@ -362,7 +362,7 @@ pub fn place_initial_offer_cctp_shim(
362362
crate::AUCTION_CUSTODY_TOKEN_SEED_PREFIX,
363363
auction_key.as_ref(),
364364
],
365-
&program_id,
365+
program_id,
366366
);
367367
if auction_custody_token_pda != auction_custody_token.key() {
368368
msg!(
@@ -407,7 +407,7 @@ pub fn place_initial_offer_cctp_shim(
407407
let auction_space = 8 + Auction::INIT_SPACE;
408408
let (pda, bump) = Pubkey::find_program_address(
409409
&[Auction::SEED_PREFIX, vaa_message_digest.as_ref()],
410-
&program_id,
410+
program_id,
411411
);
412412

413413
if pda != auction_key {
@@ -422,7 +422,7 @@ pub fn place_initial_offer_cctp_shim(
422422
auction_account.lamports(),
423423
auction_space,
424424
accounts,
425-
&program_id,
425+
program_id,
426426
auction_signer_seeds,
427427
)?;
428428
// Borrow the account data mutably
@@ -472,7 +472,7 @@ pub fn place_initial_offer_cctp_shim(
472472
let auction_bytes = auction_to_write
473473
.try_to_vec()
474474
.map_err(|_| MatchingEngineError::BorshDeserializationError)?;
475-
data[8..8 + auction_bytes.len()].copy_from_slice(&auction_bytes);
475+
data[8..8_usize.saturating_add(auction_bytes.len())].copy_from_slice(&auction_bytes);
476476
// ------------------------------------------------------------------------------------------------
477477
// End of initialisation of auction account
478478

@@ -509,30 +509,32 @@ pub fn place_initial_offer_cctp_shim(
509509

510510
#[cfg(test)]
511511
mod tests {
512+
use crate::state::FastMarketOrderParams;
513+
512514
use super::*;
513515

514516
#[test]
515517
fn test_bytemuck() {
516-
let test_fast_market_order = FastMarketOrderState::new(
517-
1000000000000000000,
518-
1000000000000000000,
519-
1000000000,
520-
1,
521-
0,
522-
[0_u8; 32],
523-
[0_u8; 32],
524-
[0_u8; 32],
525-
0,
526-
0,
527-
[0_u8; 512],
528-
[0_u8; 32],
529-
0,
530-
0,
531-
0,
532-
0,
533-
0,
534-
[0_u8; 32],
535-
);
518+
let test_fast_market_order = FastMarketOrderState::new(FastMarketOrderParams {
519+
amount_in: 1000000000000000000,
520+
min_amount_out: 1000000000000000000,
521+
deadline: 1000000000,
522+
target_chain: 1,
523+
redeemer_message_length: 0,
524+
redeemer: [0_u8; 32],
525+
sender: [0_u8; 32],
526+
refund_address: [0_u8; 32],
527+
max_fee: 0,
528+
init_auction_fee: 0,
529+
redeemer_message: [0_u8; 512],
530+
close_account_refund_recipient: [0_u8; 32],
531+
vaa_sequence: 0,
532+
vaa_timestamp: 0,
533+
vaa_nonce: 0,
534+
vaa_emitter_chain: 0,
535+
vaa_consistency_level: 0,
536+
vaa_emitter_address: [0_u8; 32],
537+
});
536538
let bytes = bytemuck::bytes_of(&test_fast_market_order);
537539
assert!(bytes.len() == std::mem::size_of::<FastMarketOrderState>());
538540
}

solana/programs/matching-engine/src/fallback/processor/process_instruction.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ pub fn process_instruction(
4343
let instruction = FallbackMatchingEngineInstruction::deserialize(instruction_data).unwrap();
4444
match instruction {
4545
FallbackMatchingEngineInstruction::InitialiseFastMarketOrder(data) => {
46-
initialise_fast_market_order(accounts, &data)
46+
initialise_fast_market_order(accounts, data)
4747
}
4848
FallbackMatchingEngineInstruction::CloseFastMarketOrder => {
4949
close_fast_market_order(accounts)
5050
}
5151
FallbackMatchingEngineInstruction::PlaceInitialOfferCctpShim(data) => {
52-
place_initial_offer_cctp_shim(accounts, &data)
52+
place_initial_offer_cctp_shim(accounts, data)
5353
} // FallbackMatchingEngineInstruction::ExecuteOrderCctpShim => {
5454
// handle_execute_order_shim(accounts)
5555
// }
@@ -68,12 +68,12 @@ impl<'ix> FallbackMatchingEngineInstruction<'ix> {
6868
match instruction_data[..8].try_into().unwrap() {
6969
FallbackMatchingEngineInstruction::PLACE_INITIAL_OFFER_CCTP_SHIM_SELECTOR => {
7070
Some(Self::PlaceInitialOfferCctpShim(
71-
&PlaceInitialOfferCctpShimData::from_bytes(&instruction_data[8..]).unwrap(),
71+
PlaceInitialOfferCctpShimData::from_bytes(&instruction_data[8..]).unwrap(),
7272
))
7373
}
7474

7575
FallbackMatchingEngineInstruction::INITIALISE_FAST_MARKET_ORDER_SELECTOR => Some(
76-
Self::InitialiseFastMarketOrder(&bytemuck::from_bytes(&instruction_data[8..])),
76+
Self::InitialiseFastMarketOrder(bytemuck::from_bytes(&instruction_data[8..])),
7777
),
7878
FallbackMatchingEngineInstruction::CLOSE_FAST_MARKET_ORDER_SELECTOR => {
7979
Some(Self::CloseFastMarketOrder)
@@ -97,7 +97,7 @@ impl FallbackMatchingEngineInstruction<'_> {
9797
Self::PlaceInitialOfferCctpShim(data) => {
9898
// Calculate the total capacity needed
9999
let data_slice = bytemuck::bytes_of(*data);
100-
let total_capacity = 8 + data_slice.len(); // 8 for the selector, plus the data length
100+
let total_capacity = 8_usize.saturating_add(data_slice.len()); // 8 for the selector, plus the data length
101101

102102
// Create a vector with the calculated capacity
103103
let mut out = Vec::with_capacity(total_capacity);
@@ -125,7 +125,7 @@ impl FallbackMatchingEngineInstruction<'_> {
125125
// }
126126
Self::InitialiseFastMarketOrder(data) => {
127127
let data_slice = bytemuck::bytes_of(*data);
128-
let total_capacity = 8 + data_slice.len(); // 8 for the selector, plus the data length
128+
let total_capacity = 8_usize.saturating_add(data_slice.len()); // 8 for the selector, plus the data length
129129

130130
let mut out = Vec::with_capacity(total_capacity);
131131

solana/programs/matching-engine/src/processor/auction/offer/place_initial/cctp_shim.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ pub struct PlaceInitialOfferCctpShim<'info> {
5050
#[account(
5151
init,
5252
payer = payer,
53-
space = 8 + std::mem::size_of::<FastMarketOrderState>(),
54-
// │ └─ FastMarketOrderState account data size
53+
space = 8_usize.saturating_add(std::mem::size_of::<FastMarketOrderState>()),
54+
// │ └─ FastMarketOrderState account data size
5555
// └─ Anchor discriminator (8 bytes)
5656
seeds = [
5757
FastMarketOrderState::SEED_PREFIX,
@@ -209,7 +209,7 @@ impl VaaMessageBody {
209209
}
210210

211211
fn to_vec(&self) -> Vec<u8> {
212-
vec![
212+
[
213213
self.vaa_time.to_be_bytes().as_ref(),
214214
self.nonce.to_be_bytes().as_ref(),
215215
self.emitter_chain.to_be_bytes().as_ref(),

solana/programs/matching-engine/src/state/fast_market_order.rs

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,46 +48,48 @@ pub struct FastMarketOrder {
4848
_padding: [u8; 5],
4949
}
5050

51+
pub struct FastMarketOrderParams {
52+
pub amount_in: u64,
53+
pub min_amount_out: u64,
54+
pub deadline: u32,
55+
pub target_chain: u16,
56+
pub redeemer_message_length: u16,
57+
pub redeemer: [u8; 32],
58+
pub sender: [u8; 32],
59+
pub refund_address: [u8; 32],
60+
pub max_fee: u64,
61+
pub init_auction_fee: u64,
62+
pub redeemer_message: [u8; 512],
63+
pub close_account_refund_recipient: [u8; 32],
64+
pub vaa_sequence: u64,
65+
pub vaa_timestamp: u32,
66+
pub vaa_nonce: u32,
67+
pub vaa_emitter_chain: u16,
68+
pub vaa_consistency_level: u8,
69+
pub vaa_emitter_address: [u8; 32],
70+
}
71+
5172
impl FastMarketOrder {
52-
pub fn new(
53-
amount_in: u64,
54-
min_amount_out: u64,
55-
deadline: u32,
56-
target_chain: u16,
57-
redeemer_message_length: u16,
58-
redeemer: [u8; 32],
59-
sender: [u8; 32],
60-
refund_address: [u8; 32],
61-
max_fee: u64,
62-
init_auction_fee: u64,
63-
redeemer_message: [u8; 512],
64-
close_account_refund_recipient: [u8; 32],
65-
vaa_sequence: u64,
66-
vaa_timestamp: u32,
67-
vaa_nonce: u32,
68-
vaa_emitter_chain: u16,
69-
vaa_consistency_level: u8,
70-
vaa_emitter_address: [u8; 32],
71-
) -> Self {
73+
pub fn new(params: FastMarketOrderParams) -> Self {
7274
Self {
73-
amount_in,
74-
min_amount_out,
75-
deadline,
76-
target_chain,
77-
redeemer_message_length,
78-
redeemer,
79-
sender,
80-
refund_address,
81-
max_fee,
82-
init_auction_fee,
83-
redeemer_message,
84-
close_account_refund_recipient,
85-
vaa_sequence,
86-
vaa_timestamp,
87-
vaa_nonce,
88-
vaa_emitter_chain,
89-
vaa_consistency_level,
90-
vaa_emitter_address,
75+
amount_in: params.amount_in,
76+
min_amount_out: params.min_amount_out,
77+
deadline: params.deadline,
78+
target_chain: params.target_chain,
79+
redeemer_message_length: params.redeemer_message_length,
80+
redeemer: params.redeemer,
81+
sender: params.sender,
82+
refund_address: params.refund_address,
83+
max_fee: params.max_fee,
84+
init_auction_fee: params.init_auction_fee,
85+
redeemer_message: params.redeemer_message,
86+
close_account_refund_recipient: params.close_account_refund_recipient,
87+
vaa_sequence: params.vaa_sequence,
88+
vaa_timestamp: params.vaa_timestamp,
89+
vaa_nonce: params.vaa_nonce,
90+
vaa_emitter_chain: params.vaa_emitter_chain,
91+
vaa_consistency_level: params.vaa_consistency_level,
92+
vaa_emitter_address: params.vaa_emitter_address,
9193
_padding: [0_u8; 5],
9294
}
9395
}
@@ -117,8 +119,10 @@ impl FastMarketOrder {
117119
payload.extend_from_slice(&self.deadline.to_be_bytes());
118120
payload.extend_from_slice(&self.redeemer_message_length.to_be_bytes());
119121
if self.redeemer_message_length > 0 {
120-
payload
121-
.extend_from_slice(&self.redeemer_message[..self.redeemer_message_length as usize]);
122+
payload.extend_from_slice(
123+
// uisize try from should never fail
124+
&self.redeemer_message[..usize::from(self.redeemer_message_length)],
125+
);
122126
}
123127
payload
124128
}

solana/programs/matching-engine/tests/integration_tests.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use anchor_lang::AccountDeserialize;
22
use anchor_spl::token::TokenAccount;
3-
use matching_engine::error::MatchingEngineError;
43
use matching_engine::ID as PROGRAM_ID;
54
use solana_program_test::tokio;
65
use solana_sdk::pubkey::Pubkey;
@@ -195,7 +194,7 @@ pub async fn test_approve_usdc() {
195194
let first_test_ft = testing_context.get_vaa_pair(0).unwrap().fast_transfer_vaa;
196195
let vaa_data = first_test_ft.vaa_data;
197196

198-
let actors = testing_context.testing_actors;
197+
let actors = &testing_context.testing_actors;
199198
let solver = actors.solvers[0].clone();
200199
let offer_price: u64 = 1__000_000;
201200
let program_id = PROGRAM_ID;
@@ -234,9 +233,6 @@ pub async fn test_approve_usdc() {
234233
println!("Solver USDC balance: {:?}", usdc_balance);
235234
let solver_token_account_address = solver.token_account_address().unwrap();
236235
let solver_token_account_info = testing_context
237-
.test_context
238-
.borrow_mut()
239-
.banks_client
240236
.get_account(solver_token_account_address)
241237
.await
242238
.expect("Failed to query banks client for solver token account info")

0 commit comments

Comments
 (0)