Skip to content

Commit 7e020f7

Browse files
authored
fix(core): support Yellowstone gRPC v16 through the Agave 4.3 Geyser methods (solana-foundation#910)
1 parent d445838 commit 7e020f7

6 files changed

Lines changed: 2978 additions & 1290 deletions

File tree

‎crates/core/src/runloops/mod.rs‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use jsonrpc_ws_server::{
2929
};
3030
use libloading::{Library, Symbol};
3131
use serde::Serialize;
32+
use solana_clock::{BankId, Slot};
3233
use solana_commitment_config::CommitmentConfig;
3334
use solana_message::SimpleAddressLoader;
3435
use solana_pubkey::Pubkey;
@@ -56,6 +57,15 @@ use crate::{
5657
},
5758
};
5859

60+
/// Returns the geyser bank ID for `slot`.
61+
///
62+
/// Surfpool runs a single fork with exactly one bank per slot, so the slot
63+
/// number identifies the bank instance that every bank-scoped notification
64+
/// (account, transaction, entry, block metadata and bank status) refers to.
65+
fn bank_id_for_slot(slot: Slot) -> BankId {
66+
slot
67+
}
68+
5969
/// The accounts a hydration batch could not produce: requested, not found
6070
/// remotely, and not deliberately kept offline. Offline accounts are absent by
6171
/// configuration rather than by accident, so they are not reported.
@@ -892,7 +902,7 @@ fn start_geyser_runloop(
892902
};
893903

894904
for plugin in managed_plugins.iter().map(|p| &*p.plugin) {
895-
if let Err(e) = plugin.notify_transaction(ReplicaTransactionInfoVersions::V0_0_3(&transaction_replica), event.transaction_with_status_meta.slot) {
905+
if let Err(e) = plugin.notify_transaction_for_bank(ReplicaTransactionInfoVersions::V0_0_3(&transaction_replica), event.transaction_with_status_meta.slot, bank_id_for_slot(event.transaction_with_status_meta.slot)) {
896906
log_error(format!("Failed to notify Geyser plugin of new transaction: {:?}", e))
897907
};
898908
}
@@ -918,7 +928,7 @@ fn start_geyser_runloop(
918928
};
919929

920930
for plugin in managed_plugins.iter().map(|p| &*p.plugin) {
921-
if let Err(e) = plugin.update_account(ReplicaAccountInfoVersions::V0_0_3(&account_replica), slot, false) {
931+
if let Err(e) = plugin.update_account_for_bank(ReplicaAccountInfoVersions::V0_0_3(&account_replica), slot, bank_id_for_slot(slot)) {
922932
log_error(format!("Failed to update account in Geyser plugin: {:?}", e));
923933
}
924934
}
@@ -943,9 +953,8 @@ fn start_geyser_runloop(
943953
txn: sanitized_transaction.as_ref(),
944954
};
945955

946-
// Send startup account updates with is_startup=true
947956
for plugin in managed_plugins.iter().map(|p| &*p.plugin) {
948-
if let Err(e) = plugin.update_account(ReplicaAccountInfoVersions::V0_0_3(&account_replica), slot, true) {
957+
if let Err(e) = plugin.update_account_from_snapshot(ReplicaAccountInfoVersions::V0_0_3(&account_replica), slot) {
949958
log_error(format!("Failed to send startup account update to Geyser plugin: {:?}", e));
950959
}
951960
}
@@ -966,7 +975,7 @@ fn start_geyser_runloop(
966975
};
967976

968977
for plugin in managed_plugins.iter().map(|p| &*p.plugin) {
969-
if let Err(e) = plugin.update_slot_status(slot, parent, &slot_status) {
978+
if let Err(e) = plugin.update_bank_status(slot, parent, &slot_status, bank_id_for_slot(slot)) {
970979
simnet_events_tx.error(format!("Failed to update slot status in Geyser plugin: {:?}", e));
971980
}
972981
}
@@ -990,7 +999,7 @@ fn start_geyser_runloop(
990999
};
9911000

9921001
for plugin in managed_plugins.iter().map(|p| &*p.plugin) {
993-
if let Err(e) = plugin.notify_block_metadata(ReplicaBlockInfoVersions::V0_0_4(&block_info)) {
1002+
if let Err(e) = plugin.notify_block_metadata_for_bank(ReplicaBlockInfoVersions::V0_0_4(&block_info), bank_id_for_slot(block_info.slot)) {
9941003
simnet_events_tx.error(format!("Failed to notify block metadata to Geyser plugin: {:?}", e));
9951004
}
9961005
}
@@ -1006,7 +1015,7 @@ fn start_geyser_runloop(
10061015
};
10071016

10081017
for plugin in managed_plugins.iter().map(|p| &*p.plugin) {
1009-
if let Err(e) = plugin.notify_entry(ReplicaEntryInfoVersions::V0_0_2(&entry_replica)) {
1018+
if let Err(e) = plugin.notify_entry_for_bank(ReplicaEntryInfoVersions::V0_0_2(&entry_replica), bank_id_for_slot(entry_replica.slot)) {
10101019
simnet_events_tx.error(format!("Failed to notify entry to Geyser plugin: {:?}", e));
10111020
}
10121021
}

‎crates/core/src/surfnet/surfnet_lite_svm.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use solana_program_option::COption;
1414
use solana_pubkey::Pubkey;
1515
use solana_sdk_ids::sysvar;
1616
use solana_slot_hashes::SlotHashes;
17-
use solana_sysvar::last_restart_slot::LastRestartSlot;
1817
#[allow(deprecated)]
1918
use solana_sysvar::recent_blockhashes::RecentBlockhashes;
19+
use solana_sysvar::{last_restart_slot::LastRestartSlot, slot_history::SlotHistory};
2020
use solana_transaction::versioned::VersionedTransaction;
2121

2222
use crate::{
@@ -129,11 +129,13 @@ impl SurfnetLiteSvm {
129129
// - Clock: for time-dependent programs
130130
// - EpochSchedule: the surfnet's own (or the datasource's), not LiteSVM's default
131131
// - LastRestartSlot: the datasource's, not LiteSVM's 0
132+
// - SlotHistory: the blocks produced so far, which Geyser plugins receive with each block
132133
let recent_blockhashes = self.svm.get_sysvar::<RecentBlockhashes>();
133134
let slot_hashes = self.svm.get_sysvar::<SlotHashes>();
134135
let clock = self.svm.get_sysvar::<Clock>();
135136
let epoch_schedule = self.svm.get_sysvar::<EpochSchedule>();
136137
let last_restart_slot = self.svm.get_sysvar::<LastRestartSlot>();
138+
let slot_history = self.svm.get_sysvar::<SlotHistory>();
137139

138140
// todo: this is also resetting the log bytes limit and airdrop keypair, would be nice to avoid
139141
self.svm = Self::litesvm_settings(feature_set);
@@ -146,6 +148,7 @@ impl SurfnetLiteSvm {
146148
self.svm.set_sysvar(&clock);
147149
self.svm.set_sysvar(&epoch_schedule);
148150
self.svm.set_sysvar(&last_restart_slot);
151+
self.svm.set_sysvar(&slot_history);
149152
}
150153

151154
pub fn set_log_bytes_limit(&mut self, limit: Option<usize>) {

‎crates/core/src/surfnet/svm.rs‎

Lines changed: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use solana_sdk_ids::{bpf_loader, system_program};
5858
use solana_signature::Signature;
5959
use solana_slot_hashes::{MAX_ENTRIES as MAX_SLOT_HASHES_ENTRIES, SlotHashes};
6060
use solana_system_interface::instruction as system_instruction;
61+
use solana_sysvar::slot_history::SlotHistory;
6162
use solana_transaction::versioned::VersionedTransaction;
6263
use solana_transaction_error::TransactionError;
6364
use solana_transaction_status::{
@@ -1757,17 +1758,21 @@ impl SurfnetSvm {
17571758
/// is by now, lands on the open slot: only its time changes. If the landing can't be
17581759
/// recorded, nothing changes in memory.
17591760
pub(crate) fn apply_time_travel(&mut self, clock: Clock) -> SurfpoolResult<EpochInfo> {
1760-
let previous = std::mem::replace(&mut self.updated_at, clock.unix_timestamp as u64 * 1_000);
1761-
if let Err(e) = self.travel_to(clock.slot.max(self.get_latest_absolute_slot())) {
1762-
self.updated_at = previous;
1763-
return Err(e);
1764-
}
1761+
let slot = clock.slot.max(self.get_latest_absolute_slot());
17651762
let clock = Clock {
1766-
slot: self.latest_epoch_info.absolute_slot,
1767-
epoch: self.latest_epoch_info.epoch,
1763+
slot,
1764+
epoch: self.inner.get_sysvar::<EpochSchedule>().get_epoch(slot),
17681765
..clock
17691766
};
1767+
let previous = std::mem::replace(&mut self.updated_at, clock.unix_timestamp as u64 * 1_000);
1768+
let previous_clock = self.inner.get_sysvar::<Clock>();
1769+
// The Clock is set before the landing is announced, so Geyser streams the landing's.
17701770
self.inner.set_sysvar(&clock);
1771+
if let Err(e) = self.travel_to(slot) {
1772+
self.updated_at = previous;
1773+
self.inner.set_sysvar(&previous_clock);
1774+
return Err(e);
1775+
}
17711776
self.simnet_events_tx.system_clock_updated(clock);
17721777
Ok(self.latest_epoch_info.clone())
17731778
}
@@ -3209,6 +3214,19 @@ impl SurfnetSvm {
32093214
},
32103215
});
32113216

3217+
// Agave writes SlotHistory and RecentBlockhashes as it freezes a block. Yellowstone
3218+
// publishes a block only once it has seen them, along with the slot's Clock and SlotHashes.
3219+
let mut slot_history = self.inner.get_sysvar::<SlotHistory>();
3220+
slot_history.add(slot);
3221+
self.inner.set_sysvar(&slot_history);
3222+
self.stream_sysvars(
3223+
slot,
3224+
[
3225+
solana_sdk_ids::sysvar::slot_history::ID,
3226+
solana_sdk_ids::sysvar::recent_blockhashes::ID,
3227+
],
3228+
);
3229+
32123230
// The block's entry goes before its metadata: Yellowstone drops the metadata of an empty
32133231
// block whose entry arrives after it.
32143232
let entry_hash = solana_hash::Hash::from_str(&self.chain_tip.hash)
@@ -4411,16 +4429,39 @@ impl SurfnetSvm {
44114429

44124430
/// Announces the open slot on its parent block, as Agave does when it creates the slot's
44134431
/// bank: at startup, after each block, after a time travel and a reset. A skipped slot gets
4414-
/// none.
4432+
/// none. Geyser first receives the Clock and SlotHashes sysvars as the open slot's account
4433+
/// writes, as Agave writes them while it creates the bank.
44154434
pub fn announce_open_slot(&mut self) {
44164435
let slot = self.get_latest_absolute_slot();
44174436
let parent = self.parent_slot(slot);
44184437
let root = self.slot_for_commitment(&CommitmentConfig::finalized());
44194438
self.notify_slot_subscribers(slot, parent, root);
4439+
self.stream_sysvars(
4440+
slot,
4441+
[
4442+
solana_sdk_ids::sysvar::clock::ID,
4443+
solana_sdk_ids::sysvar::slot_hashes::ID,
4444+
],
4445+
);
44204446
let timestamp = Utc::now().timestamp_millis().max(0) as u64;
44214447
self.emit_slot_status(slot, Some(parent), GeyserSlotStatus::CreatedBank, timestamp);
44224448
}
44234449

4450+
/// Sends the current accounts of `sysvars` to Geyser plugins as account writes in `slot`.
4451+
fn stream_sysvars(&mut self, slot: Slot, sysvars: [Pubkey; 2]) {
4452+
for pubkey in sysvars {
4453+
let Some(account) = self.inner.get_account_no_db(&pubkey) else {
4454+
continue;
4455+
};
4456+
let write_version = self.increment_write_version();
4457+
self.geyser_events_tx
4458+
.send(GeyserEvent::UpdateAccount(
4459+
GeyserAccountUpdate::block_update(pubkey, account, slot, write_version),
4460+
))
4461+
.ok();
4462+
}
4463+
}
4464+
44244465
pub fn write_simulated_profile_result(
44254466
&mut self,
44264467
uuid: Uuid,
@@ -7082,6 +7123,23 @@ mod tests {
70827123
);
70837124
}
70847125

7126+
/// Garbage collection rebuilds LiteSVM as a block closes, just before the block records its
7127+
/// slot in SlotHistory, so the blocks recorded before it must survive the rebuild.
7128+
#[test]
7129+
fn slot_history_survives_garbage_collection() {
7130+
let (mut svm, _events_rx, _geyser_rx) = TestType::in_memory().initialize_svm();
7131+
let gc_slot = svm.garbage_collection_interval_slots();
7132+
svm.latest_epoch_info.absolute_slot = gc_slot - 1;
7133+
svm.latest_epoch_info.slot_index = gc_slot - 1;
7134+
svm.confirm_current_block().unwrap();
7135+
let mut expected = svm.inner.get_sysvar::<SlotHistory>();
7136+
expected.add(gc_slot);
7137+
7138+
svm.confirm_current_block().unwrap();
7139+
7140+
assert!(svm.inner.get_sysvar::<SlotHistory>() == expected);
7141+
}
7142+
70857143
#[test_case(TestType::sqlite(); "with on-disk sqlite db")]
70867144
#[test_case(TestType::in_memory(); "with in-memory sqlite db")]
70877145
#[test_case(TestType::no_db(); "with no db")]
@@ -7438,6 +7496,58 @@ mod tests {
74387496
assert_blocks_read_as_streamed(&svm, &blocks);
74397497
}
74407498

7499+
/// Yellowstone gRPC v16 publishes a block only once Geyser has written its Clock,
7500+
/// SlotHashes, SlotHistory and RecentBlockhashes in that slot; without them its slot, block
7501+
/// and confirmed streams stay empty. A time travel's landing gets its own Clock.
7502+
#[test]
7503+
fn geyser_writes_the_sysvars_of_each_block() {
7504+
use solana_sdk_ids::sysvar::{clock, recent_blockhashes, slot_hashes, slot_history};
7505+
let sysvar_writes = |events: &[GeyserEvent], slot: Slot| -> Vec<(Pubkey, Account)> {
7506+
events
7507+
.iter()
7508+
.filter_map(|event| match event {
7509+
GeyserEvent::UpdateAccount(update) if update.slot == slot => {
7510+
Some((update.pubkey, update.account.clone()))
7511+
}
7512+
_ => None,
7513+
})
7514+
.collect()
7515+
};
7516+
let (mut svm, _events_rx, geyser_rx) = SurfnetSvm::default();
7517+
svm.announce_open_slot();
7518+
let slot = svm.get_latest_absolute_slot();
7519+
svm.confirm_current_block().unwrap();
7520+
7521+
let events: Vec<_> = geyser_rx.try_iter().collect();
7522+
let written: Vec<Pubkey> = sysvar_writes(&events, slot)
7523+
.into_iter()
7524+
.map(|(pubkey, _)| pubkey)
7525+
.collect();
7526+
assert_eq!(
7527+
written,
7528+
[
7529+
clock::ID,
7530+
slot_hashes::ID,
7531+
slot_history::ID,
7532+
recent_blockhashes::ID
7533+
]
7534+
);
7535+
assert_eq!(svm.inner.get_sysvar::<SlotHistory>().next_slot, slot + 1);
7536+
7537+
let landing = svm.get_latest_absolute_slot() + 100;
7538+
let clock = svm.inner.get_sysvar::<Clock>();
7539+
svm.apply_time_travel(Clock {
7540+
slot: landing,
7541+
..clock
7542+
})
7543+
.unwrap();
7544+
let events: Vec<_> = geyser_rx.try_iter().collect();
7545+
let (pubkey, account) = &sysvar_writes(&events, landing)[0];
7546+
assert_eq!(*pubkey, clock::ID);
7547+
let streamed: Clock = bincode::deserialize(&account.data).unwrap();
7548+
assert_eq!(streamed.slot, landing);
7549+
}
7550+
74417551
/// A forward time travel skips the slots it jumps, as a leader's missed slots are: the
74427552
/// block it lands on is built on the last block, the skipped slots hold none, heights stay
74437553
/// consecutive, and every block is rooted once and in order, the ones before the jump too.

0 commit comments

Comments
 (0)