solana_accountsdb_plugin_interface/accountsdb_plugin_interface.rs
1/// The interface for AccountsDb plugins. A plugin must implement
2/// the AccountsDbPlugin trait to work with the runtime.
3/// In addition, the dynamic library must export a "C" function _create_plugin which
4/// creates the implementation of the plugin.
5use {
6 solana_sdk::{clock::UnixTimestamp, signature::Signature, transaction::SanitizedTransaction},
7 solana_transaction_status::{Reward, TransactionStatusMeta},
8 std::{any::Any, error, io},
9 thiserror::Error,
10};
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13/// Information about an account being updated
14pub struct ReplicaAccountInfo<'a> {
15 /// The Pubkey for the account
16 pub pubkey: &'a [u8],
17
18 /// The lamports for the account
19 pub lamports: u64,
20
21 /// The Pubkey of the owner program account
22 pub owner: &'a [u8],
23
24 /// This account's data contains a loaded program (and is now read-only)
25 pub executable: bool,
26
27 /// The epoch at which this account will next owe rent
28 pub rent_epoch: u64,
29
30 /// The data held in this account.
31 pub data: &'a [u8],
32
33 /// A global monotonically increasing atomic number, which can be used
34 /// to tell the order of the account update. For example, when an
35 /// account is updated in the same slot multiple times, the update
36 /// with higher write_version should supersede the one with lower
37 /// write_version.
38 pub write_version: u64,
39}
40
41/// A wrapper to future-proof ReplicaAccountInfo handling.
42/// If there were a change to the structure of ReplicaAccountInfo,
43/// there would be new enum entry for the newer version, forcing
44/// plugin implementations to handle the change.
45pub enum ReplicaAccountInfoVersions<'a> {
46 V0_0_1(&'a ReplicaAccountInfo<'a>),
47}
48
49/// Information about a transaction
50#[derive(Clone, Debug)]
51pub struct ReplicaTransactionInfo<'a> {
52 /// The first signature of the transaction, used for identifying the transaction.
53 pub signature: &'a Signature,
54
55 /// Indicates if the transaction is a simple vote transaction.
56 pub is_vote: bool,
57
58 /// The sanitized transaction.
59 pub transaction: &'a SanitizedTransaction,
60
61 /// Metadata of the transaction status.
62 pub transaction_status_meta: &'a TransactionStatusMeta,
63}
64
65/// A wrapper to future-proof ReplicaTransactionInfo handling.
66/// If there were a change to the structure of ReplicaTransactionInfo,
67/// there would be new enum entry for the newer version, forcing
68/// plugin implementations to handle the change.
69pub enum ReplicaTransactionInfoVersions<'a> {
70 V0_0_1(&'a ReplicaTransactionInfo<'a>),
71}
72
73#[derive(Clone, Debug)]
74pub struct ReplicaBlockInfo<'a> {
75 pub slot: u64,
76 pub blockhash: &'a str,
77 pub rewards: &'a [Reward],
78 pub block_time: Option<UnixTimestamp>,
79 pub block_height: Option<u64>,
80}
81
82pub enum ReplicaBlockInfoVersions<'a> {
83 V0_0_1(&'a ReplicaBlockInfo<'a>),
84}
85
86/// Errors returned by plugin calls
87#[derive(Error, Debug)]
88pub enum AccountsDbPluginError {
89 /// Error opening the configuration file; for example, when the file
90 /// is not found or when the validator process has no permission to read it.
91 #[error("Error opening config file. Error detail: ({0}).")]
92 ConfigFileOpenError(#[from] io::Error),
93
94 /// Error in reading the content of the config file or the content
95 /// is not in the expected format.
96 #[error("Error reading config file. Error message: ({msg})")]
97 ConfigFileReadError { msg: String },
98
99 /// Error when updating the account.
100 #[error("Error updating account. Error message: ({msg})")]
101 AccountsUpdateError { msg: String },
102
103 /// Error when updating the slot status
104 #[error("Error updating slot status. Error message: ({msg})")]
105 SlotStatusUpdateError { msg: String },
106
107 /// Any custom error defined by the plugin.
108 #[error("Plugin-defined custom error. Error message: ({0})")]
109 Custom(Box<dyn error::Error + Send + Sync>),
110}
111
112/// The current status of a slot
113#[derive(Debug, Clone, Copy, PartialEq, Eq)]
114pub enum SlotStatus {
115 /// The highest slot of the heaviest fork processed by the node. Ledger state at this slot is
116 /// not derived from a confirmed or finalized block, but if multiple forks are present, is from
117 /// the fork the validator believes is most likely to finalize.
118 Processed,
119
120 /// The highest slot having reached max vote lockout.
121 Rooted,
122
123 /// The highest slot that has been voted on by supermajority of the cluster, ie. is confirmed.
124 Confirmed,
125}
126
127impl SlotStatus {
128 pub fn as_str(&self) -> &'static str {
129 match self {
130 SlotStatus::Confirmed => "confirmed",
131 SlotStatus::Processed => "processed",
132 SlotStatus::Rooted => "rooted",
133 }
134 }
135}
136
137pub type Result<T> = std::result::Result<T, AccountsDbPluginError>;
138
139/// Defines an AccountsDb plugin, to stream data from the runtime.
140/// AccountsDb plugins must describe desired behavior for load and unload,
141/// as well as how they will handle streamed data.
142pub trait AccountsDbPlugin: Any + Send + Sync + std::fmt::Debug {
143 fn name(&self) -> &'static str;
144
145 /// The callback called when a plugin is loaded by the system,
146 /// used for doing whatever initialization is required by the plugin.
147 /// The _config_file contains the name of the
148 /// of the config file. The config must be in JSON format and
149 /// include a field "libpath" indicating the full path
150 /// name of the shared library implementing this interface.
151 fn on_load(&mut self, _config_file: &str) -> Result<()> {
152 Ok(())
153 }
154
155 /// The callback called right before a plugin is unloaded by the system
156 /// Used for doing cleanup before unload.
157 fn on_unload(&mut self) {}
158
159 /// Called when an account is updated at a slot.
160 /// When `is_startup` is true, it indicates the account is loaded from
161 /// snapshots when the validator starts up. When `is_startup` is false,
162 /// the account is updated during transaction processing.
163 #[allow(unused_variables)]
164 fn update_account(
165 &mut self,
166 account: ReplicaAccountInfoVersions,
167 slot: u64,
168 is_startup: bool,
169 ) -> Result<()> {
170 Ok(())
171 }
172
173 /// Called when all accounts are notified of during startup.
174 fn notify_end_of_startup(&mut self) -> Result<()> {
175 Ok(())
176 }
177
178 /// Called when a slot status is updated
179 #[allow(unused_variables)]
180 fn update_slot_status(
181 &mut self,
182 slot: u64,
183 parent: Option<u64>,
184 status: SlotStatus,
185 ) -> Result<()> {
186 Ok(())
187 }
188
189 /// Called when a transaction is updated at a slot.
190 #[allow(unused_variables)]
191 fn notify_transaction(
192 &mut self,
193 transaction: ReplicaTransactionInfoVersions,
194 slot: u64,
195 ) -> Result<()> {
196 Ok(())
197 }
198
199 /// Called when block's metadata is updated.
200 #[allow(unused_variables)]
201 fn notify_block_metadata(&mut self, blockinfo: ReplicaBlockInfoVersions) -> Result<()> {
202 Ok(())
203 }
204
205 /// Check if the plugin is interested in account data
206 /// Default is true -- if the plugin is not interested in
207 /// account data, please return false.
208 fn account_data_notifications_enabled(&self) -> bool {
209 true
210 }
211
212 /// Check if the plugin is interested in transaction data
213 /// Default is false -- if the plugin is not interested in
214 /// transaction data, please return false.
215 fn transaction_notifications_enabled(&self) -> bool {
216 false
217 }
218}