Line data Source code
1 : #include "fd_txn_account.h" 2 : #include "fd_acc_mgr.h" 3 : 4 : fd_txn_account_t * 5 24 : fd_txn_account_init( void * ptr ) { 6 24 : if( FD_UNLIKELY( !ptr ) ) { 7 0 : FD_LOG_WARNING(( "NULL ptr" )); 8 0 : return NULL; 9 0 : } 10 : 11 24 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)ptr, alignof(fd_txn_account_t) ) ) ) { 12 0 : FD_LOG_WARNING(( "misaligned ptr" )); 13 0 : return NULL; 14 0 : } 15 : 16 24 : memset( ptr, 0, FD_TXN_ACCOUNT_FOOTPRINT ); 17 : 18 24 : fd_txn_account_t * ret = (fd_txn_account_t *)ptr; 19 24 : ret->starting_dlen = ULONG_MAX; 20 24 : ret->starting_lamports = ULONG_MAX; 21 24 : ret->account_found = 1; 22 : 23 24 : FD_COMPILER_MFENCE(); 24 24 : ret->magic = FD_TXN_ACCOUNT_MAGIC; 25 24 : FD_COMPILER_MFENCE(); 26 : 27 24 : return ret; 28 24 : } 29 : 30 : /* Operators impl */ 31 : 32 : void 33 : fd_txn_account_resize( fd_txn_account_t * acct, 34 0 : ulong dlen ) { 35 : /* Because the memory for an account is preallocated for the transaction 36 : up to the max account size, we only need to zero out bytes (for the case 37 : where the account grew) and update the account dlen. */ 38 0 : ulong old_sz = acct->meta->dlen; 39 0 : ulong new_sz = dlen; 40 0 : ulong memset_sz = fd_ulong_sat_sub( new_sz, old_sz ); 41 0 : fd_memset( acct->data+old_sz, 0, memset_sz ); 42 : 43 0 : acct->meta->dlen = dlen; 44 0 : } 45 : 46 : /* Internal helper to initialize account data */ 47 : uchar * 48 0 : fd_txn_account_init_data( fd_txn_account_t * acct, void * buf ) { 49 : /* Assumes that buf is pointing to account data */ 50 0 : uchar * new_raw_data = (uchar *)buf; 51 0 : ulong dlen = ( acct->const_meta != NULL ) ? acct->const_meta->dlen : 0; 52 : 53 0 : if( acct->const_meta != NULL ) { 54 0 : fd_memcpy( new_raw_data, (uchar *)acct->const_meta, sizeof(fd_account_meta_t)+dlen ); 55 0 : } else { 56 : /* Account did not exist, set up metadata */ 57 0 : fd_account_meta_init( (fd_account_meta_t *)new_raw_data ); 58 0 : } 59 : 60 0 : return new_raw_data; 61 0 : } 62 : 63 : fd_txn_account_t * 64 0 : fd_txn_account_make_readonly( fd_txn_account_t * acct, void * buf ) { 65 0 : ulong dlen = ( acct->const_meta != NULL ) ? acct->const_meta->dlen : 0; 66 0 : uchar * new_raw_data = fd_txn_account_init_data( acct, buf ); 67 : 68 0 : acct->orig_meta = acct->const_meta = (fd_account_meta_t *)new_raw_data; 69 0 : acct->orig_data = acct->const_data = new_raw_data + sizeof(fd_account_meta_t); 70 0 : ((fd_account_meta_t *)new_raw_data)->dlen = dlen; 71 : 72 0 : return acct; 73 0 : } 74 : 75 : fd_txn_account_t * 76 0 : fd_txn_account_make_mutable( fd_txn_account_t * acct, void * buf ) { 77 0 : if( FD_UNLIKELY( acct->data != NULL ) ) FD_LOG_ERR(( "borrowed account is already mutable" )); 78 : 79 0 : ulong dlen = ( acct->const_meta != NULL ) ? acct->const_meta->dlen : 0; 80 0 : uchar * new_raw_data = fd_txn_account_init_data( acct, buf ); 81 : 82 0 : acct->const_meta = acct->meta = (fd_account_meta_t *)new_raw_data; 83 0 : acct->const_data = acct->data = new_raw_data + sizeof(fd_account_meta_t); 84 0 : acct->meta->dlen = dlen; 85 : 86 0 : return acct; 87 0 : } 88 : 89 : void * 90 0 : fd_txn_account_restore( fd_txn_account_t * acct ) { 91 0 : fd_account_meta_t * meta = acct->meta; 92 0 : uint is_changed = meta != acct->orig_meta; 93 : 94 0 : acct->const_meta = acct->orig_meta; 95 0 : acct->const_data = acct->orig_data; 96 0 : acct->const_rec = acct->orig_rec; 97 : 98 0 : if( is_changed ) { 99 0 : return meta; 100 0 : } 101 : 102 0 : return NULL; 103 0 : } 104 : 105 : /* Factory constructor impl */ 106 : int 107 : fd_txn_account_create_from_funk( fd_txn_account_t * acct_ptr, 108 : fd_pubkey_t const * acc_pubkey, 109 : fd_acc_mgr_t * acc_mgr, 110 0 : fd_funk_txn_t * funk_txn ) { 111 0 : fd_txn_account_init( acct_ptr ); 112 : 113 0 : return fd_acc_mgr_view( acc_mgr, funk_txn, acc_pubkey, acct_ptr ); 114 0 : }