Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 27a4380

Browse files
mergify[bot]fanatidlijunwangs
authored
v1.17: geyser: allow custom name in config file (backport of #33550) (#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]>
1 parent 7e18276 commit 27a4380

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

geyser-plugin-manager/src/geyser_plugin_manager.rs

+50-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,48 @@ use {
44
libloading::Library,
55
log::*,
66
solana_geyser_plugin_interface::geyser_plugin_interface::GeyserPlugin,
7-
std::path::Path,
7+
std::{
8+
ops::{Deref, DerefMut},
9+
path::Path,
10+
},
811
};
912

13+
#[derive(Debug)]
14+
pub struct LoadedGeyserPlugin {
15+
name: String,
16+
plugin: Box<dyn GeyserPlugin>,
17+
}
18+
19+
impl LoadedGeyserPlugin {
20+
pub fn new(plugin: Box<dyn GeyserPlugin>, name: Option<String>) -> Self {
21+
Self {
22+
name: name.unwrap_or_else(|| plugin.name().to_owned()),
23+
plugin,
24+
}
25+
}
26+
27+
pub fn name(&self) -> &str {
28+
&self.name
29+
}
30+
}
31+
32+
impl Deref for LoadedGeyserPlugin {
33+
type Target = Box<dyn GeyserPlugin>;
34+
35+
fn deref(&self) -> &Self::Target {
36+
&self.plugin
37+
}
38+
}
39+
40+
impl DerefMut for LoadedGeyserPlugin {
41+
fn deref_mut(&mut self) -> &mut Self::Target {
42+
&mut self.plugin
43+
}
44+
}
45+
1046
#[derive(Default, Debug)]
1147
pub struct GeyserPluginManager {
12-
pub plugins: Vec<Box<dyn GeyserPlugin>>,
48+
pub plugins: Vec<LoadedGeyserPlugin>,
1349
libs: Vec<Library>,
1450
}
1551

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

362+
let plugin_name = result["name"].as_str().map(|s| s.to_owned());
363+
326364
let config_file = geyser_plugin_config_file
327365
.as_os_str()
328366
.to_str()
@@ -337,7 +375,11 @@ pub(crate) fn load_plugin_from_config(
337375
let plugin_raw = constructor();
338376
(Box::from_raw(plugin_raw), lib)
339377
};
340-
Ok((plugin, lib, config_file))
378+
Ok((
379+
LoadedGeyserPlugin::new(plugin, plugin_name),
380+
lib,
381+
config_file,
382+
))
341383
}
342384

343385
#[cfg(test)]
@@ -353,7 +395,7 @@ const TESTPLUGIN2_CONFIG: &str = "TESTPLUGIN2_CONFIG";
353395
#[cfg(test)]
354396
pub(crate) fn load_plugin_from_config(
355397
geyser_plugin_config_file: &Path,
356-
) -> Result<(Box<dyn GeyserPlugin>, Library, &str), GeyserPluginManagerError> {
398+
) -> Result<(LoadedGeyserPlugin, Library, &str), GeyserPluginManagerError> {
357399
if geyser_plugin_config_file.ends_with(TESTPLUGIN_CONFIG) {
358400
Ok(tests::dummy_plugin_and_library(
359401
tests::TestPlugin,
@@ -375,7 +417,7 @@ pub(crate) fn load_plugin_from_config(
375417
mod tests {
376418
use {
377419
crate::geyser_plugin_manager::{
378-
GeyserPluginManager, TESTPLUGIN2_CONFIG, TESTPLUGIN_CONFIG,
420+
GeyserPluginManager, LoadedGeyserPlugin, TESTPLUGIN2_CONFIG, TESTPLUGIN_CONFIG,
379421
},
380422
libloading::Library,
381423
solana_geyser_plugin_interface::geyser_plugin_interface::GeyserPlugin,
@@ -385,9 +427,9 @@ mod tests {
385427
pub(super) fn dummy_plugin_and_library<P: GeyserPlugin>(
386428
plugin: P,
387429
config_path: &'static str,
388-
) -> (Box<dyn GeyserPlugin>, Library, &'static str) {
430+
) -> (LoadedGeyserPlugin, Library, &'static str) {
389431
(
390-
Box::new(plugin),
432+
LoadedGeyserPlugin::new(Box::new(plugin), None),
391433
Library::from(libloading::os::unix::Library::this()),
392434
config_path,
393435
)

0 commit comments

Comments
 (0)