LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_rent.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 19 44 43.2 %
Date: 2024-11-13 11:58:15 Functions: 3 4 75.0 %

          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      926109 : #define ACCOUNT_STORAGE_OVERHEAD (128)
      10             : 
      11             : fd_rent_t *
      12             : fd_sysvar_rent_read( fd_rent_t *                result,
      13           0 :                      fd_exec_slot_ctx_t const * slot_ctx ) {
      14             : 
      15           0 :   fd_rent_t const * ret = fd_sysvar_cache_rent( slot_ctx->sysvar_cache );
      16           0 :   if( FD_UNLIKELY( NULL != ret ) ) {
      17           0 :     fd_memcpy(result, ret, sizeof(fd_rent_t));
      18           0 :     return result;
      19           0 :   }
      20             : 
      21           0 :   FD_BORROWED_ACCOUNT_DECL(rent_rec);
      22             : 
      23           0 :   int err = fd_acc_mgr_view( slot_ctx->acc_mgr, slot_ctx->funk_txn, &fd_sysvar_rent_id, rent_rec );
      24           0 :   if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) ) {
      25           0 :     FD_LOG_WARNING(( "failed to read rent sysvar: %d", err ));
      26           0 :     return NULL;
      27           0 :   }
      28             : 
      29           0 :   fd_bincode_decode_ctx_t decode = {
      30           0 :     .data    = rent_rec->const_data,
      31           0 :     .dataend = rent_rec->const_data + rent_rec->const_meta->dlen,
      32           0 :     .valloc  = slot_ctx->valloc
      33           0 :   };
      34           0 :   err = fd_rent_decode( result, &decode );
      35           0 :   if( FD_UNLIKELY( err ) ) {
      36           0 :     FD_LOG_WARNING(( "fd_rent_decode failed" ));
      37           0 :     return NULL;
      38           0 :   }
      39             : 
      40           0 :   return result;
      41           0 : }
      42             : 
      43             : static void
      44             : write_rent( fd_exec_slot_ctx_t * slot_ctx,
      45       11370 :             fd_rent_t const * rent ) {
      46             : 
      47       11370 :   uchar enc[ 32 ];
      48             : 
      49       11370 :   ulong sz = fd_rent_size( rent );
      50       11370 :   FD_TEST( sz<=sizeof(enc) );
      51       11370 :   memset( enc, 0, sz );
      52             : 
      53       11370 :   fd_bincode_encode_ctx_t ctx;
      54       11370 :   ctx.data    = enc;
      55       11370 :   ctx.dataend = enc + sz;
      56       11370 :   if( fd_rent_encode( rent, &ctx ) )
      57           0 :     FD_LOG_ERR(("fd_rent_encode failed"));
      58             : 
      59       11370 :   fd_sysvar_set( slot_ctx, fd_sysvar_owner_id.key, &fd_sysvar_rent_id, enc, sz, slot_ctx->slot_bank.slot );
      60       11370 : }
      61             : 
      62             : void
      63       11370 : fd_sysvar_rent_init( fd_exec_slot_ctx_t * slot_ctx ) {
      64       11370 :   fd_epoch_bank_t * epoch_bank = fd_exec_epoch_ctx_epoch_bank( slot_ctx->epoch_ctx );
      65       11370 :   write_rent( slot_ctx, &epoch_bank->rent );
      66       11370 : }
      67             : 
      68             : ulong
      69             : fd_rent_exempt_minimum_balance( fd_rent_t const * rent,
      70      926109 :                                 ulong             data_len ) {
      71             :   /* https://github.com/anza-xyz/agave/blob/d2124a995f89e33c54f41da76bfd5b0bd5820898/sdk/program/src/rent.rs#L74 */
      72      926109 :   return fd_rust_cast_double_to_ulong( (double)((data_len + ACCOUNT_STORAGE_OVERHEAD) * rent->lamports_per_uint8_year) * rent->exemption_threshold );
      73      926109 : }

Generated by: LCOV version 1.14