LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_stake_history.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 41 46 89.1 %
Date: 2025-01-08 12:08:44 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "fd_sysvar_stake_history.h"
       2             : #include "fd_sysvar.h"
       3             : #include "../fd_system_ids.h"
       4             : #include "../context/fd_exec_slot_ctx.h"
       5             : 
       6             : /* Ensure that the size declared by our header matches the minimum size
       7             :    of the corresponding fd_types entry. */
       8             : 
       9             : static void
      10             : write_stake_history( fd_exec_slot_ctx_t * slot_ctx,
      11       24552 :                      fd_stake_history_t * stake_history ) {
      12             :   /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/sysvar/stake_history.rs#L12 */
      13       24552 :   uchar enc[16392] = {0};
      14             : 
      15       24552 :   fd_bincode_encode_ctx_t encode =
      16       24552 :     { .data    = enc,
      17       24552 :       .dataend = enc + sizeof(enc) };
      18       24552 :   if( FD_UNLIKELY( fd_stake_history_encode( stake_history, &encode )!=FD_BINCODE_SUCCESS ) )
      19           0 :     FD_LOG_ERR(("fd_stake_history_encode failed"));
      20             : 
      21       24552 :   fd_sysvar_set( slot_ctx, fd_sysvar_owner_id.key, &fd_sysvar_stake_history_id, enc, sizeof(enc), slot_ctx->slot_bank.slot );
      22       24552 : }
      23             : 
      24             : fd_stake_history_t *
      25             : fd_sysvar_stake_history_read( fd_stake_history_t * result,
      26             :                               fd_exec_slot_ctx_t * slot_ctx,
      27       12276 :                               fd_valloc_t *        valloc ) {
      28       12276 :   FD_BORROWED_ACCOUNT_DECL(stake_rec);
      29       12276 :   int err = fd_acc_mgr_view( slot_ctx->acc_mgr, slot_ctx->funk_txn, &fd_sysvar_stake_history_id, stake_rec);
      30       12276 :   if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) )
      31           0 :     return NULL;
      32             : 
      33       12276 :   fd_bincode_decode_ctx_t ctx = {
      34       12276 :     .data = stake_rec->const_data,
      35       12276 :     .dataend = (char *) stake_rec->const_data + stake_rec->const_meta->dlen,
      36       12276 :     .valloc  = *valloc
      37       12276 :   };
      38             : 
      39       12276 :   if( FD_UNLIKELY( fd_stake_history_decode( result, &ctx )!=FD_BINCODE_SUCCESS ) )
      40           0 :     return NULL;
      41       12276 :   return result;
      42       12276 : }
      43             : 
      44             : void
      45       12276 : fd_sysvar_stake_history_init( fd_exec_slot_ctx_t * slot_ctx ) {
      46       12276 :   fd_stake_history_t stake_history;
      47       12276 :   fd_stake_history_new( &stake_history );
      48       12276 :   write_stake_history( slot_ctx, &stake_history );
      49       12276 : }
      50             : 
      51             : void
      52             : fd_sysvar_stake_history_update( fd_exec_slot_ctx_t *       slot_ctx,
      53       12276 :                                 fd_stake_history_entry_t * entry ) {
      54             :   // Need to make this maybe zero copies of map...
      55       12276 :   fd_stake_history_t stake_history;
      56       12276 :   fd_sysvar_stake_history_read( &stake_history, slot_ctx, &slot_ctx->valloc );
      57             : 
      58       12276 :   if( stake_history.fd_stake_history_offset == 0 )
      59       12276 :     stake_history.fd_stake_history_offset = stake_history.fd_stake_history_size - 1;
      60           0 :   else
      61           0 :     stake_history.fd_stake_history_offset--;
      62             : 
      63       12276 :   if( stake_history.fd_stake_history_len < stake_history.fd_stake_history_size)
      64       12276 :     stake_history.fd_stake_history_len++;
      65             : 
      66             :   // This should be done with a bit mask
      67       12276 :   ulong idx = stake_history.fd_stake_history_offset;
      68             : 
      69       12276 :   stake_history.fd_stake_history[ idx ].epoch = entry->epoch;
      70       12276 :   stake_history.fd_stake_history[ idx ].activating = entry->activating;
      71       12276 :   stake_history.fd_stake_history[ idx ].effective = entry->effective;
      72       12276 :   stake_history.fd_stake_history[ idx ].deactivating = entry->deactivating;
      73             : 
      74       12276 :   write_stake_history( slot_ctx, &stake_history);
      75       12276 :   fd_bincode_destroy_ctx_t destroy = { .valloc = slot_ctx->valloc };
      76       12276 :   fd_stake_history_destroy( &stake_history, &destroy );
      77       12276 : }

Generated by: LCOV version 1.14