Skip to content

Commit

Permalink
Add some tests + default for slot entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ebin-mathews committed Aug 28, 2024
1 parent da71eda commit 3e3ddcc
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 2 deletions.
102 changes: 100 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rand = "0.8"
serde = "1.0.160"
serde_derive = "1.0.160"
serde_json = "1.0.96"
serde_with = "3.9.0"
solana-account-decoder = "=1.18.22"
solana-geyser-plugin-interface = "=1.18.22"
solana-logger = "=1.18.22"
Expand Down
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ serde = { workspace = true }
serde_derive = { workspace = true }
serde_json = { workspace = true }
solana-geyser-plugin-interface = { workspace = true }
serde_with = { workspace = true }
solana-logger = { workspace = true }
solana-metrics = { workspace = true }
solana-program = { workspace = true }
Expand Down
124 changes: 124 additions & 0 deletions server/src/geyser_grpc_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use jito_geyser_protos::solana::{
use log::*;
use serde_derive::Deserialize;
use serde_json;
use serde_with::{serde_as, DefaultOnError};
use solana_geyser_plugin_interface::geyser_plugin_interface::{
GeyserPlugin, GeyserPluginError, ReplicaAccountInfoVersions, ReplicaBlockInfoVersions,
ReplicaEntryInfoVersions, ReplicaTransactionInfoVersions, Result as PluginResult, SlotStatus,
Expand Down Expand Up @@ -67,18 +68,40 @@ impl std::fmt::Debug for GeyserGrpcPlugin {
}
}

macro_rules! generate_default_fns {
($($name:ident: $type:ty = $value:expr),* $(,)?) => {
$(
fn $name() -> $type {
$value
}
)*
};
}

#[serde_as]
#[derive(Clone, Debug, Deserialize)]
pub struct PluginConfig {
pub geyser_service_config: GeyserServiceConfig,
pub bind_address: String,
pub account_update_buffer_size: usize,
pub slot_update_buffer_size: usize,
#[serde_as(deserialize_as = "DefaultOnError")]
#[serde(default = "default_slot_entry_update_buffer_size")]
pub slot_entry_update_buffer_size: usize,
pub block_update_buffer_size: usize,
pub transaction_update_buffer_size: usize,
pub skip_startup_stream: Option<bool>,
}

impl PluginConfig {
const DEFAULT_SLOT_ENTRY_UPDATE_BUFFER_SIZE: usize = 1000000;
}

// Can add default values for other fields here
generate_default_fns! {
default_slot_entry_update_buffer_size: usize = PluginConfig::DEFAULT_SLOT_ENTRY_UPDATE_BUFFER_SIZE,
}

impl GeyserPlugin for GeyserGrpcPlugin {
fn name(&self) -> &'static str {
"geyser-grpc-plugin"
Expand Down Expand Up @@ -566,3 +589,104 @@ impl Interceptor for AccessTokenChecker {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_plugin_config_deserialization() {
let config_json = r#"
{
"libpath": "/path/to/container-output/libgeyser_grpc_plugin_server.so",
"bind_address": "0.0.0.0:10000",
"account_update_buffer_size": 100000,
"slot_update_buffer_size": 100000,
"slot_entry_update_buffer_size": 1000000,
"block_update_buffer_size": 100000,
"transaction_update_buffer_size": 100000,
"geyser_service_config": {
"heartbeat_interval_ms": 1000,
"subscriber_buffer_size": 1000000
}
}
"#;

let config: PluginConfig = serde_json::from_str(config_json).unwrap();

assert_eq!(config.bind_address, "0.0.0.0:10000");
assert_eq!(config.account_update_buffer_size, 100000);
assert_eq!(config.slot_update_buffer_size, 100000);
assert_eq!(config.slot_entry_update_buffer_size, 1000000);
assert_eq!(config.block_update_buffer_size, 100000);
assert_eq!(config.transaction_update_buffer_size, 100000);
}

// Please update the test when the default values are added
#[test]
fn test_plugin_config_missing_fields_error() {
let config_json = r#"
{
"bind_address": "0.0.0.0:10000",
"account_update_buffer_size": 100000,
"geyser_service_config": {
"heartbeat_interval_ms": 1000
}
}
"#;

let result: Result<PluginConfig, _> = serde_json::from_str(config_json);
assert!(result.is_err());
}

#[test]
fn test_plugin_config_invalid_types() {
let config_json = r#"
{
"bind_address": "0.0.0.0:10000",
"account_update_buffer_size": "not a number",
"slot_update_buffer_size": 100000,
"block_update_buffer_size": 100000,
"transaction_update_buffer_size": 100000,
"geyser_service_config": {
"heartbeat_interval_ms": 1000,
"subscriber_buffer_size": 1000000
}
}
"#;

let result: Result<PluginConfig, _> = serde_json::from_str(config_json);
assert!(result.is_err());
}

// We currently have default value for slot_entry_update_buffer_size, so this test will always pass
#[test]
fn test_plugin_config_no_slot_entry_update_buffer_size() {
let config_json = r#"
{
"libpath": "/path/to/container-output/libgeyser_grpc_plugin_server.so",
"bind_address": "0.0.0.0:10000",
"account_update_buffer_size": 100000,
"slot_update_buffer_size": 100000,
"block_update_buffer_size": 100000,
"transaction_update_buffer_size": 100000,
"geyser_service_config": {
"heartbeat_interval_ms": 1000,
"subscriber_buffer_size": 1000000
}
}
"#;

let config: PluginConfig = serde_json::from_str(config_json).unwrap();

assert_eq!(config.bind_address, "0.0.0.0:10000");
assert_eq!(config.account_update_buffer_size, 100000);
assert_eq!(config.slot_update_buffer_size, 100000);
assert_eq!(
config.slot_entry_update_buffer_size,
PluginConfig::DEFAULT_SLOT_ENTRY_UPDATE_BUFFER_SIZE
);
assert_eq!(config.block_update_buffer_size, 100000);
assert_eq!(config.transaction_update_buffer_size, 100000);
}
}

0 comments on commit 3e3ddcc

Please sign in to comment.