2022-03-14 18:18:46 -07:00
|
|
|
/// Managing the Geyser plugins
|
2021-09-30 14:26:17 -07:00
|
|
|
use {
|
|
|
|
libloading::{Library, Symbol},
|
|
|
|
log::*,
|
2022-03-14 18:18:46 -07:00
|
|
|
solana_geyser_plugin_interface::geyser_plugin_interface::GeyserPlugin,
|
2021-09-30 14:26:17 -07:00
|
|
|
std::error::Error,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
2022-03-14 18:18:46 -07:00
|
|
|
pub struct GeyserPluginManager {
|
|
|
|
pub plugins: Vec<Box<dyn GeyserPlugin>>,
|
2021-09-30 14:26:17 -07:00
|
|
|
libs: Vec<Library>,
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:18:46 -07:00
|
|
|
impl GeyserPluginManager {
|
2021-09-30 14:26:17 -07:00
|
|
|
pub fn new() -> Self {
|
2022-03-14 18:18:46 -07:00
|
|
|
GeyserPluginManager {
|
2021-09-30 14:26:17 -07:00
|
|
|
plugins: Vec::default(),
|
|
|
|
libs: Vec::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// This function loads the dynamically linked library specified in the path. The library
|
|
|
|
/// must do necessary initializations.
|
|
|
|
pub unsafe fn load_plugin(
|
|
|
|
&mut self,
|
|
|
|
libpath: &str,
|
|
|
|
config_file: &str,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2022-03-14 18:18:46 -07:00
|
|
|
type PluginConstructor = unsafe fn() -> *mut dyn GeyserPlugin;
|
2021-09-30 14:26:17 -07:00
|
|
|
let lib = Library::new(libpath)?;
|
|
|
|
let constructor: Symbol<PluginConstructor> = lib.get(b"_create_plugin")?;
|
|
|
|
let plugin_raw = constructor();
|
|
|
|
let mut plugin = Box::from_raw(plugin_raw);
|
|
|
|
plugin.on_load(config_file)?;
|
|
|
|
self.plugins.push(plugin);
|
|
|
|
self.libs.push(lib);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unload all plugins and loaded plugin libraries, making sure to fire
|
|
|
|
/// their `on_plugin_unload()` methods so they can do any necessary cleanup.
|
|
|
|
pub fn unload(&mut self) {
|
|
|
|
for mut plugin in self.plugins.drain(..) {
|
|
|
|
info!("Unloading plugin for {:?}", plugin.name());
|
|
|
|
plugin.on_unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
for lib in self.libs.drain(..) {
|
|
|
|
drop(lib);
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 17:11:38 -08:00
|
|
|
|
|
|
|
/// Check if there is any plugin interested in account data
|
2021-11-23 09:55:53 -08:00
|
|
|
pub fn account_data_notifications_enabled(&self) -> bool {
|
2021-11-17 17:11:38 -08:00
|
|
|
for plugin in &self.plugins {
|
2021-11-23 09:55:53 -08:00
|
|
|
if plugin.account_data_notifications_enabled() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if there is any plugin interested in transaction data
|
|
|
|
pub fn transaction_notifications_enabled(&self) -> bool {
|
|
|
|
for plugin in &self.plugins {
|
|
|
|
if plugin.transaction_notifications_enabled() {
|
2021-11-17 17:11:38 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2021-09-30 14:26:17 -07:00
|
|
|
}
|