Skip to content

Commit

Permalink
Fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed May 6, 2024
1 parent cda88c5 commit 80d9746
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 60 deletions.
27 changes: 18 additions & 9 deletions txstatus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::slice;

mod byte_order;
mod reader;
mod tools;
mod type_size;

use reader::Decoder;
Expand All @@ -25,7 +24,8 @@ pub extern "C" fn parse_instruction(bytes: *const u8, len: usize) -> Response {
{
// read program ID:
let program_id_bytes = decoder.read_bytes(32).unwrap();
let program_id = solana_sdk::pubkey::Pubkey::new(&program_id_bytes);
let program_id =
solana_sdk::pubkey::Pubkey::try_from(program_id_bytes).expect("invalid program id");
let mut instruction = CompiledInstruction {
program_id_index: 0,
accounts: vec![],
Expand All @@ -45,10 +45,7 @@ pub extern "C" fn parse_instruction(bytes: *const u8, len: usize) -> Response {
}
}

let mut parsed_account_keys = Combined {
parent: vec![],
child: None,
};
let parsed_account_keys: Combined;
let static_account_keys_len = decoder.read_u8().unwrap() as usize;
// println!(
// "[rust] static_account_keys_len: {:?}",
Expand All @@ -57,7 +54,8 @@ pub extern "C" fn parse_instruction(bytes: *const u8, len: usize) -> Response {
let mut static_account_keys_vec = vec![];
for _ in 0..static_account_keys_len {
let account_key_bytes = decoder.read_bytes(32).unwrap();
let account_key = solana_sdk::pubkey::Pubkey::new(&account_key_bytes);
let account_key = solana_sdk::pubkey::Pubkey::try_from(account_key_bytes)
.expect("invalid account key in static account keys");
static_account_keys_vec.push(account_key);
}

Expand All @@ -69,14 +67,16 @@ pub extern "C" fn parse_instruction(bytes: *const u8, len: usize) -> Response {
// read 32 bytes for each writable account:
for _ in 0..num_writable_accounts {
let account_key_bytes = decoder.read_bytes(32).unwrap();
let account_key = solana_sdk::pubkey::Pubkey::new(&account_key_bytes);
let account_key = solana_sdk::pubkey::Pubkey::try_from(account_key_bytes)
.expect("invalid account key in writable accounts");
loaded_addresses.writable.push(account_key);
}
let num_readonly_accounts = decoder.read_u8().unwrap() as usize;
// read 32 bytes for each readonly account:
for _ in 0..num_readonly_accounts {
let account_key_bytes = decoder.read_bytes(32).unwrap();
let account_key = solana_sdk::pubkey::Pubkey::new(&account_key_bytes);
let account_key = solana_sdk::pubkey::Pubkey::try_from(account_key_bytes)
.expect("invalid account key in readonly accounts");
loaded_addresses.readonly.push(account_key);
}

Expand Down Expand Up @@ -203,3 +203,12 @@ struct Combined {
parent: Vec<Pubkey>,
child: Option<LoadedAddresses>,
}

impl Default for Combined {
fn default() -> Self {
Combined {
parent: vec![],
child: None,
}
}
}
38 changes: 2 additions & 36 deletions txstatus/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::error::Error as StdError;
pub enum Error {
ShortBuffer { msg: String },
InvalidValue { msg: String },
GenericError { msg: String },
}

impl StdError for Error {}
Expand All @@ -16,7 +15,6 @@ impl std::fmt::Display for Error {
match self {
Error::ShortBuffer { msg } => write!(f, "short buffer: {}", msg),
Error::InvalidValue { msg } => write!(f, "invalid value: {}", msg),
Error::GenericError { msg } => write!(f, "generic error: {}", msg),
}
}
}
Expand All @@ -26,7 +24,6 @@ impl std::fmt::Debug for Error {
match self {
Error::ShortBuffer { msg } => write!(f, "short buffer: {}", msg),
Error::InvalidValue { msg } => write!(f, "invalid value: {}", msg),
Error::GenericError { msg } => write!(f, "generic error: {}", msg),
}
}
}
Expand All @@ -48,7 +45,7 @@ impl Decoder {
}

pub fn read_byte(&mut self) -> Result<u8, Error> {
if self.pos + type_size::BYTE as usize > self.data.len() {
if self.pos + type_size::BYTE > self.data.len() {
return Err(Error::ShortBuffer {
msg: format!(
"required {} bytes, but only {} bytes available",
Expand All @@ -58,7 +55,7 @@ impl Decoder {
});
}
let b = self.data[self.pos];
self.pos += type_size::BYTE as usize;
self.pos += type_size::BYTE;
Ok(b)
}

Expand Down Expand Up @@ -191,37 +188,6 @@ impl TypeIDFromBytes for TypeID {
}
}

// func DecodeCompactU16(bytes []byte) (int, int, error) {
// ln := 0
// size := 0
// for {
// if len(bytes) == 0 {
// return 0, 0, io.ErrUnexpectedEOF
// }
// elem := int(bytes[0])
// bytes = bytes[1:]
// ln |= (elem & 0x7f) << (size * 7)
// size += 1
// if (elem & 0x80) == 0 {
// break
// }
// }
// return ln, size, nil
// }

pub fn decode_compact_u16(bytes: &[u8]) -> Result<(usize, usize), Error> {
let mut ln = 0;
let mut size = 0;
for elem in bytes {
ln |= (usize::from(*elem) & 0x7F) << (size * 7);
size += 1;
if (usize::from(*elem) & 0x80) == 0 {
break;
}
}
Ok((ln, size))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
15 changes: 0 additions & 15 deletions txstatus/src/tools.rs

This file was deleted.

5 changes: 5 additions & 0 deletions txstatus/src/type_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ pub const INT8: usize = 1;
pub const INT16: usize = 2;
#[allow(dead_code)]
pub const UINT8: usize = 1;
#[allow(dead_code)]
pub const UINT16: usize = 2;
pub const UINT32: usize = 4;
#[allow(dead_code)]
pub const UINT64: usize = 8;
#[allow(dead_code)]
pub const UINT128: usize = 16;
#[allow(dead_code)]
pub const FLOAT32: usize = 4;
#[allow(dead_code)]
pub const FLOAT64: usize = 8;
#[allow(dead_code)]
pub const PUBLIC_KEY: usize = 32;
Expand Down

0 comments on commit 80d9746

Please sign in to comment.