refactor: restructure event handling from trait objects to enum pattern

- Convert UnifiedEvent from trait object (Box<dyn UnifiedEvent>) to concrete enum type
  - Remove match_event! macro in favor of native match expressions
  - Simplify event metadata access: event.event_type() -> event.metadata().event_type
  - Unify event callback signatures: Fn(Box<dyn UnifiedEvent>) -> Fn(UnifiedEvent)
  - Update all example code to use the new enum-based event system
  - Optimize type system to reduce runtime overhead and improve type safety

  Affected scope:
  - Core event parser and processor
  - All protocol event definitions (PumpFun, PumpSwap, Bonk, Raydium series, etc.)
  - gRPC and Shred streaming modules
  - All example code
This commit is contained in:
ysq
2025-10-10 18:19:44 +08:00
parent 77caa39f0c
commit 312a675a40
43 changed files with 1013 additions and 1839 deletions
+10 -41
View File
@@ -13,7 +13,7 @@ use futures::{SinkExt, StreamExt};
use log::error;
use solana_sdk::pubkey::Pubkey;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
use std::sync::Arc;
use tokio::sync::Mutex;
use yellowstone_grpc_proto::geyser::subscribe_update::UpdateOneof;
use yellowstone_grpc_proto::geyser::{
@@ -40,9 +40,7 @@ pub struct YellowstoneGrpc {
pub endpoint: String,
pub x_token: Option<String>,
pub config: StreamClientConfig,
pub metrics: Arc<RwLock<PerformanceMetrics>>,
pub subscription_manager: SubscriptionManager,
pub metrics_manager: MetricsManager,
pub event_processor: EventProcessor,
pub subscription_handle: Arc<Mutex<Option<SubscriptionHandle>>>,
// Dynamic subscription management fields
@@ -66,24 +64,16 @@ impl YellowstoneGrpc {
config: StreamClientConfig,
) -> AnyResult<Self> {
let _ = rustls::crypto::ring::default_provider().install_default().ok();
let metrics = Arc::new(RwLock::new(PerformanceMetrics::new()));
let subscription_manager =
SubscriptionManager::new(endpoint.clone(), x_token.clone(), config.clone());
let metrics_manager = MetricsManager::new_with_metrics(
metrics.clone(),
config.enable_metrics,
"YellowstoneGrpc".to_string(),
);
let event_processor = EventProcessor::new(metrics_manager.clone(), config.clone());
MetricsManager::init(config.enable_metrics);
let event_processor = EventProcessor::new(config.clone());
Ok(Self {
endpoint,
x_token,
config,
metrics: metrics.clone(),
subscription_manager,
metrics_manager,
event_processor,
subscription_handle: Arc::new(Mutex::new(None)),
active_subscription: Arc::new(AtomicBool::new(false)),
@@ -93,24 +83,6 @@ impl YellowstoneGrpc {
})
}
/// Creates a new YellowstoneGrpcClient with high-throughput configuration.
///
/// This is a convenience method that creates a client optimized for high-concurrency scenarios
/// where throughput is prioritized over latency. See `StreamClientConfig::high_throughput()`
/// for detailed configuration information.
pub fn new_high_throughput(endpoint: String, x_token: Option<String>) -> AnyResult<Self> {
Self::new_with_config(endpoint, x_token, StreamClientConfig::high_throughput())
}
/// Creates a new YellowstoneGrpcClient with low-latency configuration.
///
/// This is a convenience method that creates a client optimized for real-time scenarios
/// where latency is prioritized over throughput. See `StreamClientConfig::low_latency()`
/// for detailed configuration information.
pub fn new_low_latency(endpoint: String, x_token: Option<String>) -> AnyResult<Self> {
Self::new_with_config(endpoint, x_token, StreamClientConfig::low_latency())
}
/// 获取配置
pub fn get_config(&self) -> &StreamClientConfig {
&self.config
@@ -123,12 +95,12 @@ impl YellowstoneGrpc {
/// 获取性能指标
pub fn get_metrics(&self) -> PerformanceMetrics {
self.metrics_manager.get_metrics()
MetricsManager::global().get_metrics()
}
/// 打印性能指标
pub fn print_metrics(&self) {
self.metrics_manager.print_metrics();
MetricsManager::global().print_metrics();
}
/// 启用或禁用性能监控
@@ -171,7 +143,7 @@ impl YellowstoneGrpc {
callback: F,
) -> AnyResult<()>
where
F: Fn(Box<dyn UnifiedEvent>) + Send + Sync + 'static,
F: Fn(UnifiedEvent) + Send + Sync + 'static,
{
*self.event_type_filter.write().await = event_type_filter.clone();
if self
@@ -185,7 +157,7 @@ impl YellowstoneGrpc {
let mut metrics_handle = None;
// 启动自动性能监控(如果启用)
if self.config.enable_metrics {
metrics_handle = self.metrics_manager.start_auto_monitoring().await;
metrics_handle = MetricsManager::global().start_auto_monitoring().await;
}
let transactions = self
@@ -213,7 +185,6 @@ impl YellowstoneGrpc {
super::common::EventSource::Grpc,
protocols,
event_type_filter,
self.config.backpressure.clone(),
Some(Arc::new(callback)),
);
let stream_handle = tokio::spawn(async move {
@@ -228,7 +199,7 @@ impl YellowstoneGrpc {
let account_pretty = factory::create_account_pretty_pooled(account);
log::debug!("Received account: {:?}", account_pretty);
if let Err(e) = event_processor
.process_grpc_event_transaction_with_metrics(
.process_grpc_transaction(
EventPretty::Account(account_pretty),
bot_wallet,
)
@@ -241,7 +212,7 @@ impl YellowstoneGrpc {
let block_meta_pretty = factory::create_block_meta_pretty_pooled(sut, created_at);
log::debug!("Received block meta: {:?}", block_meta_pretty);
if let Err(e) = event_processor
.process_grpc_event_transaction_with_metrics(
.process_grpc_transaction(
EventPretty::BlockMeta(block_meta_pretty),
bot_wallet,
)
@@ -258,7 +229,7 @@ impl YellowstoneGrpc {
transaction_pretty.slot
);
if let Err(e) = event_processor
.process_grpc_event_transaction_with_metrics(
.process_grpc_transaction(
EventPretty::Transaction(transaction_pretty),
bot_wallet,
)
@@ -380,9 +351,7 @@ impl Clone for YellowstoneGrpc {
endpoint: self.endpoint.clone(),
x_token: self.x_token.clone(),
config: self.config.clone(),
metrics: self.metrics.clone(),
subscription_manager: self.subscription_manager.clone(),
metrics_manager: self.metrics_manager.clone(),
event_processor: self.event_processor.clone(),
subscription_handle: self.subscription_handle.clone(), // 共享同一个 Arc<Mutex<>>
active_subscription: self.active_subscription.clone(),