Skip to content

Commit

Permalink
v1.17: geyser: allow custom name in config file (backport of #33550) …
Browse files Browse the repository at this point in the history
…(#34669)

* geyser: allow custom name in config file (#33550)

Allow loading the plugin name from the json config file as opposed to use plugin.name which is called before config file is passed to it. Allowing different plugins using the same executable to use different names.

(cherry picked from commit c82fc6c)

# Conflicts:
#	geyser-plugin-manager/src/geyser_plugin_manager.rs

* Fixed merge conflicts

---------

Co-authored-by: Kirill Fomichev <[email protected]>
Co-authored-by: Lijun Wang <[email protected]>
  • Loading branch information
3 people authored Jan 20, 2024
1 parent 7e18276 commit 27a4380
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions geyser-plugin-manager/src/geyser_plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,48 @@ use {
libloading::Library,
log::*,
solana_geyser_plugin_interface::geyser_plugin_interface::GeyserPlugin,
std::path::Path,
std::{
ops::{Deref, DerefMut},
path::Path,
},
};

#[derive(Debug)]
pub struct LoadedGeyserPlugin {
name: String,
plugin: Box<dyn GeyserPlugin>,
}

impl LoadedGeyserPlugin {
pub fn new(plugin: Box<dyn GeyserPlugin>, name: Option<String>) -> Self {
Self {
name: name.unwrap_or_else(|| plugin.name().to_owned()),
plugin,
}
}

pub fn name(&self) -> &str {
&self.name
}
}

impl Deref for LoadedGeyserPlugin {
type Target = Box<dyn GeyserPlugin>;

fn deref(&self) -> &Self::Target {
&self.plugin
}
}

impl DerefMut for LoadedGeyserPlugin {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.plugin
}
}

#[derive(Default, Debug)]
pub struct GeyserPluginManager {
pub plugins: Vec<Box<dyn GeyserPlugin>>,
pub plugins: Vec<LoadedGeyserPlugin>,
libs: Vec<Library>,
}

Expand Down Expand Up @@ -280,7 +316,7 @@ pub enum GeyserPluginManagerError {
#[cfg(not(test))]
pub(crate) fn load_plugin_from_config(
geyser_plugin_config_file: &Path,
) -> Result<(Box<dyn GeyserPlugin>, Library, &str), GeyserPluginManagerError> {
) -> Result<(LoadedGeyserPlugin, Library, &str), GeyserPluginManagerError> {
use std::{fs::File, io::Read, path::PathBuf};
type PluginConstructor = unsafe fn() -> *mut dyn GeyserPlugin;
use libloading::Symbol;
Expand Down Expand Up @@ -323,6 +359,8 @@ pub(crate) fn load_plugin_from_config(
libpath = config_dir.join(libpath);
}

let plugin_name = result["name"].as_str().map(|s| s.to_owned());

let config_file = geyser_plugin_config_file
.as_os_str()
.to_str()
Expand All @@ -337,7 +375,11 @@ pub(crate) fn load_plugin_from_config(
let plugin_raw = constructor();
(Box::from_raw(plugin_raw), lib)
};
Ok((plugin, lib, config_file))
Ok((
LoadedGeyserPlugin::new(plugin, plugin_name),
lib,
config_file,
))
}

#[cfg(test)]
Expand All @@ -353,7 +395,7 @@ const TESTPLUGIN2_CONFIG: &str = "TESTPLUGIN2_CONFIG";
#[cfg(test)]
pub(crate) fn load_plugin_from_config(
geyser_plugin_config_file: &Path,
) -> Result<(Box<dyn GeyserPlugin>, Library, &str), GeyserPluginManagerError> {
) -> Result<(LoadedGeyserPlugin, Library, &str), GeyserPluginManagerError> {
if geyser_plugin_config_file.ends_with(TESTPLUGIN_CONFIG) {
Ok(tests::dummy_plugin_and_library(
tests::TestPlugin,
Expand All @@ -375,7 +417,7 @@ pub(crate) fn load_plugin_from_config(
mod tests {
use {
crate::geyser_plugin_manager::{
GeyserPluginManager, TESTPLUGIN2_CONFIG, TESTPLUGIN_CONFIG,
GeyserPluginManager, LoadedGeyserPlugin, TESTPLUGIN2_CONFIG, TESTPLUGIN_CONFIG,
},
libloading::Library,
solana_geyser_plugin_interface::geyser_plugin_interface::GeyserPlugin,
Expand All @@ -385,9 +427,9 @@ mod tests {
pub(super) fn dummy_plugin_and_library<P: GeyserPlugin>(
plugin: P,
config_path: &'static str,
) -> (Box<dyn GeyserPlugin>, Library, &'static str) {
) -> (LoadedGeyserPlugin, Library, &'static str) {
(
Box::new(plugin),
LoadedGeyserPlugin::new(Box::new(plugin), None),
Library::from(libloading::os::unix::Library::this()),
config_path,
)
Expand Down

0 comments on commit 27a4380

Please sign in to comment.