Skip to content

Commit

Permalink
parse slice instead of vec
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-gray committed Feb 24, 2024
1 parent 6931822 commit 7178b3b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
26 changes: 12 additions & 14 deletions programs/example-queries-solana-verify/src/structs/query_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ pub struct QueryRequest {
impl QueryRequest {
pub const REQUEST_VERSION: u8 = 1;

pub fn deserialize(data: &Vec<u8>) -> std::result::Result<QueryRequest, std::io::Error> {
pub fn deserialize(data: &[u8]) -> std::result::Result<QueryRequest, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<QueryRequest, std::io::Error> {
let version = rdr.read_u8()?;
if version != Self::REQUEST_VERSION {
Expand Down Expand Up @@ -58,15 +58,13 @@ pub struct PerChainQueryRequest {
}

impl PerChainQueryRequest {
pub fn deserialize(
data: &Vec<u8>,
) -> std::result::Result<PerChainQueryRequest, std::io::Error> {
pub fn deserialize(data: &[u8]) -> std::result::Result<PerChainQueryRequest, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<PerChainQueryRequest, std::io::Error> {
let chain_id = rdr.read_u16::<BigEndian>()?;
let query_type = rdr.read_u8()?;
Expand Down Expand Up @@ -118,13 +116,13 @@ pub struct EthCallData {
}

impl EthCallQueryRequest {
pub fn deserialize(data: &Vec<u8>) -> std::result::Result<EthCallQueryRequest, std::io::Error> {
pub fn deserialize(data: &[u8]) -> std::result::Result<EthCallQueryRequest, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<EthCallQueryRequest, std::io::Error> {
let block_tag_len = rdr.read_u32::<BigEndian>()?;
let mut buf = vec![0u8; block_tag_len.try_into().unwrap()];
Expand Down Expand Up @@ -157,14 +155,14 @@ pub struct EthCallByTimestampQueryRequest {

impl EthCallByTimestampQueryRequest {
pub fn deserialize(
data: &Vec<u8>,
data: &[u8],
) -> std::result::Result<EthCallByTimestampQueryRequest, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<EthCallByTimestampQueryRequest, std::io::Error> {
let target_timestamp = rdr.read_u64::<BigEndian>()?;
let target_block_hint_len = rdr.read_u32::<BigEndian>()?;
Expand Down Expand Up @@ -204,14 +202,14 @@ pub struct EthCallWithFinalityQueryRequest {

impl EthCallWithFinalityQueryRequest {
pub fn deserialize(
data: &Vec<u8>,
data: &[u8],
) -> std::result::Result<EthCallWithFinalityQueryRequest, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<EthCallWithFinalityQueryRequest, std::io::Error> {
let block_tag_len = rdr.read_u32::<BigEndian>()?;
let mut buf = vec![0u8; block_tag_len.try_into().unwrap()];
Expand Down Expand Up @@ -251,14 +249,14 @@ pub struct SolanaAccountQueryRequest {

impl SolanaAccountQueryRequest {
pub fn deserialize(
data: &Vec<u8>,
data: &[u8],
) -> std::result::Result<SolanaAccountQueryRequest, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<SolanaAccountQueryRequest, std::io::Error> {
let commitment_len = rdr.read_u32::<BigEndian>()?;
let mut buf = vec![0u8; commitment_len.try_into().unwrap()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub struct QueryResponse {
impl QueryResponse {
pub const RESPONSE_VERSION: u8 = 1;

pub fn deserialize(data: &Vec<u8>) -> std::result::Result<QueryResponse, std::io::Error> {
pub fn deserialize(data: &[u8]) -> std::result::Result<QueryResponse, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<QueryResponse, std::io::Error> {
let version = rdr.read_u8()?;
if version != Self::RESPONSE_VERSION {
Expand Down Expand Up @@ -71,15 +71,13 @@ pub struct PerChainQueryResponse {
}

impl PerChainQueryResponse {
pub fn deserialize(
data: &Vec<u8>,
) -> std::result::Result<PerChainQueryResponse, std::io::Error> {
pub fn deserialize(data: &[u8]) -> std::result::Result<PerChainQueryResponse, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<PerChainQueryResponse, std::io::Error> {
let chain_id = rdr.read_u16::<BigEndian>()?;
let query_type = rdr.read_u8()?;
Expand Down Expand Up @@ -128,15 +126,13 @@ pub struct EthCallQueryResponse {
}

impl EthCallQueryResponse {
pub fn deserialize(
data: &Vec<u8>,
) -> std::result::Result<EthCallQueryResponse, std::io::Error> {
pub fn deserialize(data: &[u8]) -> std::result::Result<EthCallQueryResponse, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<EthCallQueryResponse, std::io::Error> {
let block_number = rdr.read_u64::<BigEndian>()?;
let mut block_hash = [0u8; 32];
Expand Down Expand Up @@ -171,14 +167,14 @@ pub struct EthCallByTimestampQueryResponse {

impl EthCallByTimestampQueryResponse {
pub fn deserialize(
data: &Vec<u8>,
data: &[u8],
) -> std::result::Result<EthCallByTimestampQueryResponse, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<EthCallByTimestampQueryResponse, std::io::Error> {
let target_block_number = rdr.read_u64::<BigEndian>()?;
let mut target_block_hash = [0u8; 32];
Expand Down Expand Up @@ -217,14 +213,14 @@ pub struct EthCallWithFinalityQueryResponse {

impl EthCallWithFinalityQueryResponse {
pub fn deserialize(
data: &Vec<u8>,
data: &[u8],
) -> std::result::Result<EthCallWithFinalityQueryResponse, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<EthCallWithFinalityQueryResponse, std::io::Error> {
let EthCallQueryResponse {
block_number,
Expand Down Expand Up @@ -258,14 +254,14 @@ pub struct SolanaAccountResult {

impl SolanaAccountQueryResponse {
pub fn deserialize(
data: &Vec<u8>,
data: &[u8],
) -> std::result::Result<SolanaAccountQueryResponse, std::io::Error> {
let mut rdr = Cursor::new(data);
Self::deserialize_from_reader(&mut rdr)
}

pub fn deserialize_from_reader(
rdr: &mut Cursor<&Vec<u8>>,
rdr: &mut Cursor<&[u8]>,
) -> std::result::Result<SolanaAccountQueryResponse, std::io::Error> {
let slot_number = rdr.read_u64::<BigEndian>()?;
let block_time = rdr.read_u64::<BigEndian>()?;
Expand Down

0 comments on commit 7178b3b

Please sign in to comment.