Line data Source code
1 : #include "fd_sysvar_slot_hashes.h" 2 : #include "fd_sysvar.h" 3 : #include "../fd_acc_mgr.h" 4 : #include "../fd_system_ids.h" 5 : /* FIXME These constants should be header defines */ 6 : 7 : void 8 : fd_sysvar_slot_hashes_write( fd_bank_t * bank, 9 : fd_accdb_user_t * accdb, 10 : fd_funk_txn_xid_t const * xid, 11 : fd_capture_ctx_t * capture_ctx, 12 0 : fd_slot_hashes_global_t * slot_hashes_global ) { 13 0 : uchar __attribute__((aligned(FD_SYSVAR_SLOT_HASHES_ALIGN))) enc[ FD_SYSVAR_SLOT_HASHES_BINCODE_SZ ] = {0}; 14 0 : fd_bincode_encode_ctx_t ctx = { 15 0 : .data = enc, 16 0 : .dataend = enc + FD_SYSVAR_SLOT_HASHES_BINCODE_SZ, 17 0 : }; 18 0 : if( fd_slot_hashes_encode_global( slot_hashes_global, &ctx ) ) { 19 0 : FD_LOG_ERR(("fd_slot_hashes_encode failed")); 20 0 : } 21 0 : fd_sysvar_account_update( bank, accdb, xid, capture_ctx, &fd_sysvar_slot_hashes_id, enc, FD_SYSVAR_SLOT_HASHES_BINCODE_SZ ); 22 0 : } 23 : 24 : ulong 25 0 : fd_sysvar_slot_hashes_footprint( ulong slot_hashes_cap ) { 26 0 : return sizeof(fd_slot_hashes_global_t) + 27 0 : deq_fd_slot_hash_t_footprint( slot_hashes_cap ) + deq_fd_slot_hash_t_align(); 28 0 : } 29 : 30 : void * 31 : fd_sysvar_slot_hashes_new( void * mem, 32 0 : ulong slot_hashes_cap ) { 33 0 : if( FD_UNLIKELY( !mem ) ) { 34 0 : FD_LOG_ERR(( "Unable to allocate memory for slot hashes" )); 35 0 : } 36 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, FD_SYSVAR_SLOT_HASHES_ALIGN ) ) ) { 37 0 : FD_LOG_ERR(( "Memory for slot hashes is not aligned" )); 38 0 : } 39 : 40 0 : fd_slot_hashes_global_t * slot_hashes_global = (fd_slot_hashes_global_t *)mem; 41 : 42 0 : uchar * slot_hash_mem = (uchar*)fd_ulong_align_up( (ulong)((uchar *)mem + sizeof(fd_slot_hashes_global_t)), deq_fd_slot_hash_t_align() ); 43 0 : deq_fd_slot_hash_t_new( (void*)slot_hash_mem, slot_hashes_cap ); 44 0 : slot_hashes_global->hashes_offset = (ulong)slot_hash_mem - (ulong)slot_hashes_global; 45 : 46 0 : return slot_hashes_global; 47 0 : } 48 : 49 : fd_slot_hashes_global_t * 50 : fd_sysvar_slot_hashes_join( void * shmem, 51 0 : fd_slot_hash_t ** slot_hash ) { 52 0 : fd_slot_hashes_global_t * slot_hashes_global = (fd_slot_hashes_global_t *)shmem; 53 0 : *slot_hash = deq_fd_slot_hash_t_join( (uchar*)shmem + slot_hashes_global->hashes_offset ); 54 : 55 0 : return slot_hashes_global; 56 0 : } 57 : 58 : void * 59 : fd_sysvar_slot_hashes_leave( fd_slot_hashes_global_t * slot_hashes_global, 60 0 : fd_slot_hash_t * slot_hash ) { 61 0 : deq_fd_slot_hash_t_leave( slot_hash ); 62 : 63 0 : return slot_hashes_global; 64 0 : } 65 : 66 : void * 67 0 : fd_sysvar_slot_hashes_delete( void * mem ) { 68 0 : void * slot_hash_mem = (void *)fd_ulong_align_up( (ulong)((uchar *)mem + sizeof(fd_slot_hashes_global_t)), deq_fd_slot_hash_t_align() ); 69 0 : deq_fd_slot_hash_t_delete( slot_hash_mem ); 70 : 71 0 : return mem; 72 0 : } 73 : 74 : /* https://github.com/anza-xyz/agave/blob/b11ca828cfc658b93cb86a6c5c70561875abe237/runtime/src/bank.rs#L2283-L2294 */ 75 : void 76 : fd_sysvar_slot_hashes_update( fd_bank_t * bank, 77 : fd_accdb_user_t * accdb, 78 : fd_funk_txn_xid_t const * xid, 79 0 : fd_capture_ctx_t * capture_ctx ) { 80 0 : uchar __attribute__((aligned(FD_SYSVAR_SLOT_HASHES_ALIGN))) slot_hashes_mem[FD_SYSVAR_SLOT_HASHES_FOOTPRINT]; 81 0 : fd_slot_hashes_global_t * slot_hashes_global = fd_sysvar_slot_hashes_read( accdb->funk, xid, slot_hashes_mem ); 82 0 : fd_slot_hash_t * hashes = NULL; 83 0 : if( FD_UNLIKELY( !slot_hashes_global ) ) { 84 : /* Note: Agave's implementation initializes a new slot_hashes if it doesn't already exist (refer to above URL). */ 85 0 : slot_hashes_global = fd_sysvar_slot_hashes_new( slot_hashes_mem, FD_SYSVAR_SLOT_HASHES_CAP ); 86 0 : } 87 0 : slot_hashes_global = fd_sysvar_slot_hashes_join( slot_hashes_global, &hashes ); 88 : 89 0 : uchar found = 0; 90 0 : for( deq_fd_slot_hash_t_iter_t iter = deq_fd_slot_hash_t_iter_init( hashes ); 91 0 : !deq_fd_slot_hash_t_iter_done( hashes, iter ); 92 0 : iter = deq_fd_slot_hash_t_iter_next( hashes, iter ) ) { 93 0 : fd_slot_hash_t * ele = deq_fd_slot_hash_t_iter_ele( hashes, iter ); 94 0 : if( ele->slot == fd_bank_parent_slot_get( bank ) ) { 95 0 : fd_hash_t const * bank_hash = fd_bank_bank_hash_query( bank ); 96 0 : memcpy( &ele->hash, bank_hash, sizeof(fd_hash_t) ); 97 0 : found = 1; 98 0 : } 99 0 : } 100 : 101 0 : if( !found ) { 102 : // https://github.com/firedancer-io/solana/blob/08a1ef5d785fe58af442b791df6c4e83fe2e7c74/runtime/src/bank.rs#L2371 103 0 : fd_slot_hash_t slot_hash = { 104 0 : .hash = fd_bank_bank_hash_get( bank ), // parent hash? 105 0 : .slot = fd_bank_parent_slot_get( bank ), // parent_slot 106 0 : }; 107 : 108 0 : if( deq_fd_slot_hash_t_full( hashes ) ) 109 0 : memset( deq_fd_slot_hash_t_pop_tail_nocopy( hashes ), 0, sizeof(fd_slot_hash_t) ); 110 : 111 0 : deq_fd_slot_hash_t_push_head( hashes, slot_hash ); 112 0 : } 113 : 114 0 : fd_sysvar_slot_hashes_write( bank, accdb, xid, capture_ctx, slot_hashes_global ); 115 0 : fd_sysvar_slot_hashes_leave( slot_hashes_global, hashes ); 116 0 : } 117 : 118 : fd_slot_hashes_global_t * 119 : fd_sysvar_slot_hashes_read( fd_funk_t * funk, 120 : fd_funk_txn_xid_t const * xid, 121 0 : uchar * slot_hashes_mem ) { 122 0 : fd_txn_account_t rec[1]; 123 0 : int err = fd_txn_account_init_from_funk_readonly( rec, (fd_pubkey_t const *)&fd_sysvar_slot_hashes_id, funk, xid ); 124 0 : if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) ) { 125 0 : return NULL; 126 0 : } 127 : 128 : /* This check is needed as a quirk of the fuzzer. If a sysvar account 129 : exists in the accounts database, but doesn't have any lamports, 130 : this means that the account does not exist. This wouldn't happen 131 : in a real execution environment. */ 132 0 : if( FD_UNLIKELY( fd_txn_account_get_lamports( rec )==0UL ) ) { 133 0 : return NULL; 134 0 : } 135 : 136 0 : fd_bincode_decode_ctx_t decode = { 137 0 : .data = fd_txn_account_get_data( rec ), 138 0 : .dataend = fd_txn_account_get_data( rec ) + fd_txn_account_get_data_len( rec ) 139 0 : }; 140 : 141 0 : return fd_slot_hashes_decode_global( slot_hashes_mem, &decode ); 142 0 : }