Skip to content

Commit 3045d62

Browse files
authored
Added filtering of transactions (#81)
Signed-off-by: Anthony Milbourne <[email protected]>
1 parent 23f73c3 commit 3045d62

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/filter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ impl Filter {
3333
}
3434
}
3535

36-
pub fn wants_program(&self, program: &[u8]) -> bool {
37-
let key = match <&[u8; 32]>::try_from(program) {
36+
pub fn wants_account_key(&self, account_key: &[u8]) -> bool {
37+
let key = match <&[u8; 32]>::try_from(account_key) {
3838
Ok(key) => key,
3939
_ => return true,
4040
};
@@ -59,12 +59,12 @@ mod tests {
5959
let filter = Filter::new(&config);
6060
assert_eq!(filter.program_ignores.len(), 2);
6161

62-
assert!(filter.wants_program(
62+
assert!(filter.wants_account_key(
6363
&Pubkey::from_str("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin")
6464
.unwrap()
6565
.to_bytes()
6666
));
67-
assert!(!filter.wants_program(
67+
assert!(!filter.wants_account_key(
6868
&Pubkey::from_str("Vote111111111111111111111111111111111111111")
6969
.unwrap()
7070
.to_bytes()

src/plugin.rs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414

1515
use {
1616
crate::*,
17-
log::info,
17+
log::{debug, info, log_enabled},
1818
rdkafka::util::get_rdkafka_version,
1919
simple_error::simple_error,
2020
solana_geyser_plugin_interface::geyser_plugin_interface::{
2121
GeyserPlugin, GeyserPluginError as PluginError, ReplicaAccountInfo,
22-
ReplicaAccountInfoVersions, ReplicaTransactionInfoVersions, Result as PluginResult,
23-
SlotStatus as PluginSlotStatus,
22+
ReplicaAccountInfoVersions, ReplicaTransactionInfo, ReplicaTransactionInfoVersions,
23+
Result as PluginResult, SlotStatus as PluginSlotStatus,
2424
},
25+
solana_program::pubkey::Pubkey,
2526
std::fmt::{Debug, Formatter},
2627
};
2728

@@ -90,7 +91,8 @@ impl GeyserPlugin for KafkaPlugin {
9091
}
9192

9293
let info = Self::unwrap_update_account(account);
93-
if !self.unwrap_filter().wants_program(info.owner) {
94+
if !self.unwrap_filter().wants_account_key(info.owner) {
95+
Self::log_ignore_account_update(info);
9496
return Ok(());
9597
}
9698

@@ -143,7 +145,23 @@ impl GeyserPlugin for KafkaPlugin {
143145
return Ok(());
144146
}
145147

146-
let event = Self::build_transaction_event(slot, transaction);
148+
let info = Self::unwrap_transaction(transaction);
149+
let maybe_ignored = info
150+
.transaction
151+
.message()
152+
.account_keys()
153+
.iter()
154+
.find(|key| !self.unwrap_filter().wants_account_key(&key.to_bytes()));
155+
if maybe_ignored.is_some() {
156+
debug!(
157+
"Ignoring transaction {:?} due to account key: {:?}",
158+
info.signature,
159+
&maybe_ignored.unwrap()
160+
);
161+
return Ok(());
162+
}
163+
164+
let event = Self::build_transaction_event(slot, info);
147165

148166
publisher
149167
.update_transaction(event)
@@ -178,6 +196,12 @@ impl KafkaPlugin {
178196
}
179197
}
180198

199+
fn unwrap_transaction(transaction: ReplicaTransactionInfoVersions) -> &ReplicaTransactionInfo {
200+
match transaction {
201+
ReplicaTransactionInfoVersions::V0_0_1(info) => info,
202+
}
203+
}
204+
181205
fn build_compiled_instruction(
182206
ix: &solana_program::instruction::CompiledInstruction,
183207
) -> CompiledInstruction {
@@ -216,9 +240,8 @@ impl KafkaPlugin {
216240

217241
fn build_transaction_event(
218242
slot: u64,
219-
transaction: ReplicaTransactionInfoVersions,
243+
transaction: &ReplicaTransactionInfo,
220244
) -> TransactionEvent {
221-
let ReplicaTransactionInfoVersions::V0_0_1(transaction) = transaction;
222245
let transaction_status_meta = transaction.transaction_status_meta;
223246
let signature = transaction.signature;
224247
let is_vote = transaction.is_vote;
@@ -380,4 +403,17 @@ impl KafkaPlugin {
380403
}),
381404
}
382405
}
406+
407+
fn log_ignore_account_update(info: &ReplicaAccountInfo) {
408+
if log_enabled!(::log::Level::Debug) {
409+
match <&[u8; 32]>::try_from(info.owner) {
410+
Ok(key) => debug!(
411+
"Ignoring update for account key: {:?}",
412+
Pubkey::new_from_array(*key)
413+
),
414+
// Err should never happen because wants_account_key only returns false if the input is &[u8; 32]
415+
Err(_err) => debug!("Ignoring update for account key: {:?}", info.owner),
416+
};
417+
}
418+
}
383419
}

0 commit comments

Comments
 (0)