LCOV - code coverage report
Current view: top level - flamenco/runtime/program/vote - fd_vote_common.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 28 43 65.1 %
Date: 2026-03-19 18:19:27 Functions: 4 5 80.0 %

          Line data    Source code
       1             : #include "fd_vote_common.h"
       2             : #include "../fd_vote_program.h"
       3             : #include "../fd_program_util.h"
       4             : 
       5             : int
       6             : fd_vote_verify_authorized_signer( fd_pubkey_t const * authorized,
       7             :                                   fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
       8         502 :                                   ulong               signers_cnt ) {
       9             :   // https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_state/mod.rs#L989
      10         502 :   return fd_signers_contains( signers, signers_cnt, authorized ) ?
      11         389 :     FD_EXECUTOR_INSTR_SUCCESS :
      12         502 :     FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
      13         502 : }
      14             : 
      15             : int
      16             : fd_vote_signature_verify( fd_pubkey_t *       epoch_authorized_voter,
      17             :                           int                 authorized_withdrawer_signer,
      18             :                           fd_pubkey_t const * signers[static FD_TXN_SIG_MAX],
      19          29 :                           ulong               signers_cnt ) {
      20          29 :   return authorized_withdrawer_signer ? 0 : fd_vote_verify_authorized_signer( epoch_authorized_voter, signers, signers_cnt );
      21          29 : }
      22             : 
      23             : uchar
      24         366 : fd_vote_compute_vote_latency( ulong voted_for_slot, ulong current_slot ) {
      25         366 :   return (uchar)fd_ulong_min( fd_ulong_sat_sub( current_slot, voted_for_slot ), UCHAR_MAX );
      26         366 : }
      27             : 
      28             : ulong
      29             : fd_vote_credits_for_vote_at_index( fd_landed_vote_t const * votes,
      30         357 :                                    ulong                    index ) {
      31             :   // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L679
      32         357 :   fd_landed_vote_t const * landed_vote = deq_fd_landed_vote_t_peek_index_const( votes, index );
      33         357 :   ulong                    latency     = landed_vote == NULL ? 0 : landed_vote->latency;
      34             :   // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L683
      35         357 :   ulong                    max_credits =  VOTE_CREDITS_MAXIMUM_PER_SLOT;
      36             : 
      37             :   /* If latency is 0, this means that the Lockout was created and stored
      38             :      from a software version that did not store vote latencies; in this
      39             :      case, 1 credit is awarded.
      40             :      https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L691 */
      41         357 :   if( FD_UNLIKELY( latency==0UL ) ) {
      42           0 :     return 1;
      43           0 :   }
      44             : 
      45         357 :   ulong diff = 0;
      46         357 :   int   cf   = fd_ulong_checked_sub( latency, VOTE_CREDITS_GRACE_SLOTS, &diff );
      47         357 :   if( cf || diff==0UL ) {
      48             :     // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L697
      49         300 :     return max_credits;
      50         300 :   }
      51             : 
      52          57 :   ulong credits = 0;
      53          57 :   cf = fd_ulong_checked_sub( max_credits, diff, &credits );
      54          57 :   if( cf != 0 || credits == 0 ) {
      55             :     // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L705
      56          44 :     return 1;
      57          44 :   }
      58             :   // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L707
      59          13 :   return credits;
      60          57 : }
      61             : 
      62             : int
      63             : fd_vote_contains_slot( fd_landed_vote_t const * votes,
      64           0 :                        ulong                    slot ) {
      65             :   /* Logic is copied from slice::binary_search_by() in Rust. While not fully optimized,
      66             :      it aims to achieve fuzzing conformance for both sorted and unsorted inputs. */
      67           0 :   ulong size = deq_fd_landed_vote_t_cnt( votes );
      68           0 :   if( FD_UNLIKELY( size==0UL ) ) return 0;
      69             : 
      70           0 :   ulong base = 0UL;
      71           0 :   while( size>1UL ) {
      72           0 :     ulong half = size / 2UL;
      73           0 :     ulong mid = base + half;
      74           0 :     ulong mid_slot = deq_fd_landed_vote_t_peek_index_const( votes, mid )->lockout.slot;
      75           0 :     base = (slot<mid_slot) ? base : mid;
      76           0 :     size -= half;
      77           0 :   }
      78             : 
      79           0 :   return deq_fd_landed_vote_t_peek_index_const( votes, base )->lockout.slot==slot;
      80           0 : }

Generated by: LCOV version 1.14