@@ -58,6 +58,7 @@ use solana_sdk_ids::{bpf_loader, system_program};
5858use solana_signature:: Signature ;
5959use solana_slot_hashes:: { MAX_ENTRIES as MAX_SLOT_HASHES_ENTRIES , SlotHashes } ;
6060use solana_system_interface:: instruction as system_instruction;
61+ use solana_sysvar:: slot_history:: SlotHistory ;
6162use solana_transaction:: versioned:: VersionedTransaction ;
6263use solana_transaction_error:: TransactionError ;
6364use 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