Line data Source code
1 : #include "fd_sysvar_rent.h" 2 : #include "fd_sysvar.h" 3 : #include "../fd_acc_mgr.h" 4 : #include "../fd_system_ids.h" 5 : #include "../context/fd_exec_epoch_ctx.h" 6 : #include <assert.h> 7 : 8 : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/rent.rs#L36 */ 9 108 : #define ACCOUNT_STORAGE_OVERHEAD (128) 10 : 11 : fd_rent_t * 12 : fd_sysvar_rent_read( fd_sysvar_cache_t const * sysvar_cache, 13 : fd_acc_mgr_t * acc_mgr, 14 : fd_funk_txn_t * funk_txn, 15 0 : fd_spad_t * spad ) { 16 : 17 0 : fd_rent_t const * ret = (fd_rent_t const *)fd_sysvar_cache_rent( sysvar_cache ); 18 0 : if( FD_UNLIKELY( ret ) ) { 19 0 : return (fd_rent_t*)ret; 20 0 : } 21 : 22 0 : FD_TXN_ACCOUNT_DECL( rent_rec ); 23 : 24 0 : int err = fd_acc_mgr_view( acc_mgr, funk_txn, &fd_sysvar_rent_id, rent_rec ); 25 0 : if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) ) { 26 0 : FD_LOG_WARNING(( "failed to read rent sysvar: %d", err )); 27 0 : return NULL; 28 0 : } 29 : 30 0 : fd_bincode_decode_ctx_t decode = { 31 0 : .data = rent_rec->const_data, 32 0 : .dataend = rent_rec->const_data + rent_rec->const_meta->dlen 33 0 : }; 34 : 35 0 : ulong total_sz = 0UL; 36 0 : err = fd_rent_decode_footprint( &decode, &total_sz ); 37 0 : if( FD_UNLIKELY( err ) ) { 38 0 : FD_LOG_WARNING(( "fd_rent_decode failed" )); 39 0 : return NULL; 40 0 : } 41 : 42 0 : uchar * mem = fd_spad_alloc( spad, fd_rent_align(), total_sz ); 43 0 : if( FD_UNLIKELY( !mem ) ) { 44 0 : FD_LOG_ERR(( "failed to allocate memory for rent" )); 45 0 : } 46 : 47 0 : return fd_rent_decode( mem, &decode ); 48 0 : } 49 : 50 : static void 51 : write_rent( fd_exec_slot_ctx_t * slot_ctx, 52 0 : fd_rent_t const * rent ) { 53 : 54 0 : uchar enc[ 32 ]; 55 : 56 0 : ulong sz = fd_rent_size( rent ); 57 0 : FD_TEST( sz<=sizeof(enc) ); 58 0 : memset( enc, 0, sz ); 59 : 60 0 : fd_bincode_encode_ctx_t ctx; 61 0 : ctx.data = enc; 62 0 : ctx.dataend = enc + sz; 63 0 : if( fd_rent_encode( rent, &ctx ) ) 64 0 : FD_LOG_ERR(("fd_rent_encode failed")); 65 : 66 0 : fd_sysvar_set( slot_ctx, fd_sysvar_owner_id.key, &fd_sysvar_rent_id, enc, sz, slot_ctx->slot_bank.slot ); 67 0 : } 68 : 69 : void 70 0 : fd_sysvar_rent_init( fd_exec_slot_ctx_t * slot_ctx ) { 71 0 : fd_epoch_bank_t * epoch_bank = fd_exec_epoch_ctx_epoch_bank( slot_ctx->epoch_ctx ); 72 0 : write_rent( slot_ctx, &epoch_bank->rent ); 73 0 : } 74 : 75 : ulong 76 : fd_rent_exempt_minimum_balance( fd_rent_t const * rent, 77 108 : ulong data_len ) { 78 : /* https://github.com/anza-xyz/agave/blob/d2124a995f89e33c54f41da76bfd5b0bd5820898/sdk/program/src/rent.rs#L74 */ 79 108 : return fd_rust_cast_double_to_ulong( (double)((data_len + ACCOUNT_STORAGE_OVERHEAD) * rent->lamports_per_uint8_year) * rent->exemption_threshold ); 80 108 : }