From 915209abd8738e775bf6c4cb6e6e70a323448911 Mon Sep 17 00:00:00 2001 From: ysq Date: Tue, 19 Aug 2025 17:37:17 +0800 Subject: [PATCH] fix: use fixed-size slices for protocol data parsing - Improve borsh deserialization safety across all protocol decoders - Add MarketState support for raydium_amm_v4 - Prevent buffer overflow in data decoding operations --- Cargo.toml | 2 +- README.md | 4 +-- README_CN.md | 4 +-- .../event_parser/protocols/bonk/types.rs | 6 ++-- .../event_parser/protocols/pumpfun/types.rs | 4 +-- .../event_parser/protocols/pumpswap/types.rs | 4 +-- .../protocols/raydium_amm_v4/types.rs | 34 ++++++++++++++++++- .../protocols/raydium_clmm/types.rs | 6 ++-- .../protocols/raydium_cpmm/types.rs | 4 +-- 9 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 53b7b96..f71b1bc 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "solana-streamer-sdk" -version = "0.3.3" +version = "0.3.4" edition = "2021" authors = ["William ", "sgxiang ", "wei <1415121722@qq.com>"] repository = "https://github.com/0xfnzero/solana-streamer" diff --git a/README.md b/README.md index f1b6a13..125a6d9 100755 --- a/README.md +++ b/README.md @@ -44,14 +44,14 @@ Add the dependency to your `Cargo.toml`: ```toml # Add to your Cargo.toml -solana-streamer-sdk = { path = "./solana-streamer", version = "0.3.3" } +solana-streamer-sdk = { path = "./solana-streamer", version = "0.3.4" } ``` ### Use crates.io ```toml # Add to your Cargo.toml -solana-streamer-sdk = "0.3.3" +solana-streamer-sdk = "0.3.4" ``` ## Usage Examples diff --git a/README_CN.md b/README_CN.md index c7e9339..5299d75 100644 --- a/README_CN.md +++ b/README_CN.md @@ -44,14 +44,14 @@ git clone https://github.com/0xfnzero/solana-streamer ```toml # 添加到您的 Cargo.toml -solana-streamer-sdk = { path = "./solana-streamer", version = "0.3.3" } +solana-streamer-sdk = { path = "./solana-streamer", version = "0.3.4" } ``` ### 使用 crates.io ```toml # 添加到您的 Cargo.toml -solana-streamer-sdk = "0.3.3" +solana-streamer-sdk = "0.3.4" ``` ## 使用示例 diff --git a/src/streaming/event_parser/protocols/bonk/types.rs b/src/streaming/event_parser/protocols/bonk/types.rs index af9f576..5de6aa6 100755 --- a/src/streaming/event_parser/protocols/bonk/types.rs +++ b/src/streaming/event_parser/protocols/bonk/types.rs @@ -120,7 +120,7 @@ pub fn pool_state_decode(data: &[u8]) -> Option { if data.len() < POOL_STATE_SIZE { return None; } - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..POOL_STATE_SIZE]).ok() } pub fn pool_state_parser( @@ -168,7 +168,7 @@ pub fn global_config_decode(data: &[u8]) -> Option { if data.len() < GLOBAL_CONFIG_SIZE { return None; } - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..GLOBAL_CONFIG_SIZE]).ok() } pub fn global_config_parser( @@ -211,7 +211,7 @@ pub fn platform_config_decode(data: &[u8]) -> Option { if data.len() < PLATFORM_CONFIG_SIZE { return None; } - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..PLATFORM_CONFIG_SIZE]).ok() } pub fn platform_config_parser( diff --git a/src/streaming/event_parser/protocols/pumpfun/types.rs b/src/streaming/event_parser/protocols/pumpfun/types.rs index cdeeb7f..7dc3f69 100644 --- a/src/streaming/event_parser/protocols/pumpfun/types.rs +++ b/src/streaming/event_parser/protocols/pumpfun/types.rs @@ -28,7 +28,7 @@ pub fn bonding_curve_decode(data: &[u8]) -> Option { if data.len() < BONDING_CURVE_SIZE { return None; } - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..BONDING_CURVE_SIZE]).ok() } pub fn bonding_curve_parser( @@ -75,7 +75,7 @@ pub fn global_decode(data: &[u8]) -> Option { if data.len() < GLOBAL_SIZE { return None; } - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..GLOBAL_SIZE]).ok() } pub fn global_parser( diff --git a/src/streaming/event_parser/protocols/pumpswap/types.rs b/src/streaming/event_parser/protocols/pumpswap/types.rs index 0020dc6..83b26da 100644 --- a/src/streaming/event_parser/protocols/pumpswap/types.rs +++ b/src/streaming/event_parser/protocols/pumpswap/types.rs @@ -30,7 +30,7 @@ pub fn global_config_decode(data: &[u8]) -> Option { if data.len() < GLOBAL_CONFIG_SIZE { return None; } - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..GLOBAL_CONFIG_SIZE]).ok() } pub fn global_config_parser( @@ -72,7 +72,7 @@ pub fn pool_decode(data: &[u8]) -> Option { if data.len() < POOL_SIZE { return None; } - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..POOL_SIZE]).ok() } pub fn pool_parser( diff --git a/src/streaming/event_parser/protocols/raydium_amm_v4/types.rs b/src/streaming/event_parser/protocols/raydium_amm_v4/types.rs index f7c2117..7f87faa 100644 --- a/src/streaming/event_parser/protocols/raydium_amm_v4/types.rs +++ b/src/streaming/event_parser/protocols/raydium_amm_v4/types.rs @@ -80,7 +80,7 @@ pub struct AmmInfo { pub const AMM_INFO_SIZE: usize = 752; pub fn amm_info_decode(data: &[u8]) -> Option { - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..AMM_INFO_SIZE]).ok() } pub fn amm_info_parser( @@ -104,3 +104,35 @@ pub fn amm_info_parser( None } } + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)] +pub struct MarketState { + pub padding: [u8; 5], + pub account_flags: u64, + pub own_address: Pubkey, + pub vault_signer_nonce: u64, + pub coin_mint: Pubkey, + pub pc_mint: Pubkey, + pub serum_coin_vault_account: Pubkey, + pub coin_deposits_total: u64, + pub coin_fees_accrued: u64, + pub serum_pc_vault_account: Pubkey, + pub pc_deposits_total: u64, + pub pc_fees_accrued: u64, + pub pc_dust_threshold: u64, + pub request_queue: Pubkey, + pub serum_event_queue: Pubkey, + pub serum_bids: Pubkey, + pub serum_asks: Pubkey, + pub coin_lot_size: u64, + pub pc_lot_size: u64, + pub fee_rate_bps: u64, + pub referrer_rebate_accrued: u64, + pub padding2: [u8; 7], +} + +pub const MARKET_STATE_SIZE: usize = 388; + +pub fn market_state_decode(data: &[u8]) -> Option { + borsh::from_slice::(&data[..MARKET_STATE_SIZE]).ok() +} diff --git a/src/streaming/event_parser/protocols/raydium_clmm/types.rs b/src/streaming/event_parser/protocols/raydium_clmm/types.rs index 39834ca..7d663d4 100644 --- a/src/streaming/event_parser/protocols/raydium_clmm/types.rs +++ b/src/streaming/event_parser/protocols/raydium_clmm/types.rs @@ -31,7 +31,7 @@ pub struct AmmConfig { pub const AMM_CONFIG_SIZE: usize = 1 + 2 + 32 + 4 * 2 + 2 + 4 * 2 + 32 + 8 * 3; pub fn amm_config_decode(data: &[u8]) -> Option { - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..AMM_CONFIG_SIZE]).ok() } pub fn amm_config_parser( @@ -116,7 +116,7 @@ pub struct PoolState { pub const POOL_STATE_SIZE: usize = 1536; pub fn pool_state_decode(data: &[u8]) -> Option { - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..POOL_STATE_SIZE]).ok() } pub fn pool_state_parser( @@ -194,7 +194,7 @@ impl Default for TickArrayState { pub const TICK_ARRAY_STATE_SIZE: usize = 10232; pub fn tick_array_state_decode(data: &[u8]) -> Option { - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..TICK_ARRAY_STATE_SIZE]).ok() } pub fn tick_array_state_parser( diff --git a/src/streaming/event_parser/protocols/raydium_cpmm/types.rs b/src/streaming/event_parser/protocols/raydium_cpmm/types.rs index eae06ff..c4981e6 100644 --- a/src/streaming/event_parser/protocols/raydium_cpmm/types.rs +++ b/src/streaming/event_parser/protocols/raydium_cpmm/types.rs @@ -30,7 +30,7 @@ pub struct AmmConfig { pub const AMM_CONFIG_SIZE: usize = 228; pub fn amm_config_decode(data: &[u8]) -> Option { - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..AMM_CONFIG_SIZE]).ok() } pub fn amm_config_parser( @@ -85,7 +85,7 @@ pub struct PoolState { pub const POOL_STATE_SIZE: usize = 629; pub fn pool_state_decode(data: &[u8]) -> Option { - borsh::from_slice::(&data).ok() + borsh::from_slice::(&data[..POOL_STATE_SIZE]).ok() } pub fn pool_state_parser(