LCOV - code coverage report
Current view: top level - choreo/tower - fd_tower_serdes.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 3 4 75.0 %
Date: 2026-03-31 06:22:16 Functions: 0 0 -

          Line data    Source code
       1             : #ifndef HEADER_fd_src_choreo_tower_fd_tower_serdes_h
       2             : #define HEADER_fd_src_choreo_tower_fd_tower_serdes_h
       3             : 
       4             : #include "../fd_choreo_base.h"
       5             : #include "../../ballet/txn/fd_txn.h"
       6             : 
       7             : #define FD_VOTE_IX_KIND_TOWER_SYNC        (14)
       8             : #define FD_VOTE_IX_KIND_TOWER_SYNC_SWITCH (15)
       9             : 
      10             : /* fd_compact_tower_sync_serde describes the serialization /
      11             :    deserialization schema of a CompactTowerSync vote instruction.  There
      12             :    are various legacy instructions for vote transactions, but current
      13             :    mainnet votes are almost exclusively this instruction. */
      14             : 
      15             : struct fd_compact_tower_sync_serde { /* CompactTowerSync */
      16             :   ulong root;                        /* u64              */
      17             :   struct {
      18             :     ushort lockouts_cnt;             /* ShortU16         */
      19             :     struct {
      20             :       ulong offset;                  /* VarInt           */
      21             :       uchar confirmation_count;      /* u8               */
      22             :     } lockouts[31];
      23             :   };
      24             :   fd_hash_t hash;                    /* [u8; 32]         */
      25             :   struct {
      26             :     uchar timestamp_option;          /* Option           */
      27             :     long  timestamp;                 /* UnixTimestamp    */
      28             :   };
      29             :   fd_hash_t block_id;                /* [u8; 32]         */
      30             : };
      31             : typedef struct fd_compact_tower_sync_serde fd_compact_tower_sync_serde_t;
      32             : 
      33             : /* fd_compact_tower_sync_ser serializes fd_compact_tower_sync_serde_t
      34             :    into a buffer.  Returns 0 on success, -1 if the lockouts_cnt is
      35             :    greater than FD_TOWER_VOTE_MAX or buf_max is too small to fit the
      36             :    serialized data.  On success, sets *buf_sz to the number of bytes
      37             :    written if buf_sz is non-NULL. */
      38             : 
      39             : int
      40             : fd_compact_tower_sync_ser( fd_compact_tower_sync_serde_t const * serde,
      41             :                            uchar *                               buf,
      42             :                            ulong                                 buf_max,
      43             :                            ulong *                               buf_sz );
      44             : 
      45             : /* fd_compact_tower_sync_de deserializes at most buf_sz of buf into
      46             :    fd_compact_tower_sync_serde_t.  Designed to deserialize untrusted
      47             :    inputs (gossip / TPU vote txns).  Assumes buf is at least of size
      48             :    buf_sz.  Returns 0 on success, -1 on deserialization failure.  Note:
      49             :    the return value only indicates whether the wire format was valid,
      50             :    not whether the resulting tower is semantically valid (e.g. slots
      51             :    and confirmations are monotonically increasing).  Callers must
      52             :    validate the deserialized tower contents separately. */
      53             : 
      54             : int
      55             : fd_compact_tower_sync_de( fd_compact_tower_sync_serde_t * serde,
      56             :                           uchar const *                   buf,
      57             :                           ulong                           buf_sz );
      58             : 
      59           0 : #define FD_VOTE_STATE_DATA_MAX 3762UL
      60             : 
      61         210 : #define FD_VOTE_ACC_V2 (1)
      62        1344 : #define FD_VOTE_ACC_V3 (2)
      63          12 : #define FD_VOTE_ACC_V4 (3)
      64             : FD_STATIC_ASSERT( FD_VOTE_ACC_V2==fd_vote_state_versioned_enum_v1_14_11, FD_VOTE_ACC_V2 );
      65             : FD_STATIC_ASSERT( FD_VOTE_ACC_V3==fd_vote_state_versioned_enum_v3,       FD_VOTE_ACC_V3 );
      66             : FD_STATIC_ASSERT( FD_VOTE_ACC_V4==fd_vote_state_versioned_enum_v4,       FD_VOTE_ACC_V4 );
      67             : 
      68             : /* fd_vote_acc describes the layout of a vote state stored in a vote
      69             :    account.  The vote_acc_{...} deserializers assume trusted input
      70             :    (account data written to by the vote program).  These structs are
      71             :    used to support zero-copy access (direct casts) of byte arrays
      72             :    containing the vote account data.
      73             : 
      74             :    fd_vote_acc is versioned, and the serialized formats differ depending
      75             :    on this.  They correspond to Agave's VoteState0_23_5,
      76             :    VoteState1_14_11 and VoteState structs.
      77             : 
      78             :    VoteStatev0_23_5 is deprecated and there are no longer vote accounts
      79             :    of that version on testnet / mainnet.  VoteState1_14_11 corresponds
      80             :    to FD_VOTE_ACC_V2 and VoteState corresponds to FD_VOTE_ACC_V3.  The
      81             :    only difference between the two is the votes in V3 contain an
      82             :    additional uchar field `latency`.
      83             : 
      84             :    The binary layout begins with metadata in the vote account, followed
      85             :    by the voter's votes (tower), and terminates with the root. */
      86             : 
      87             : struct __attribute__((packed)) fd_vote_acc_vote {
      88             :   uchar latency;
      89             :   ulong slot;
      90             :   uint  conf;
      91             : };
      92             : typedef struct fd_vote_acc_vote fd_vote_acc_vote_t;
      93             : 
      94             : struct __attribute__((packed)) fd_vote_acc {
      95             :   uint kind;
      96             :   union __attribute__((packed)) {
      97             :     struct __attribute__((packed)) {
      98             :       fd_pubkey_t node_pubkey;
      99             :       fd_pubkey_t authorized_withdrawer;
     100             :       uchar       commission;
     101             :       ulong       votes_cnt;
     102             :       struct __attribute__((packed)) {
     103             :         ulong slot;
     104             :         uint  conf;
     105             :       } votes[31]; /* variable-length */
     106             :       /* uchar root_option */
     107             :       /* ulong root */
     108             :     } v2;
     109             : 
     110             :     struct __attribute__((packed)) {
     111             :       fd_pubkey_t        node_pubkey;
     112             :       fd_pubkey_t        authorized_withdrawer;
     113             :       uchar              commission;
     114             :       ulong              votes_cnt;
     115             :       fd_vote_acc_vote_t    votes[31]; /* variable-length */
     116             :       /* uchar root_option */
     117             :       /* ulong root */
     118             :     } v3;
     119             : 
     120             :     struct __attribute__((packed)) {
     121             :       fd_pubkey_t     node_pubkey;
     122             :       fd_pubkey_t     authorized_withdrawer;
     123             :       fd_pubkey_t     inflation_rewards_collector;
     124             :       fd_pubkey_t     block_revenue_collector;
     125             :       ushort          inflation_rewards_commission_bps;
     126             :       ushort          block_revenue_commission_bps;
     127             :       ulong           pending_delegator_rewards;
     128             :       uchar           has_bls_pubkey_compressed;
     129             :       uchar           bls_pubkey_compressed[48];
     130             :       /* ulong              votes_cnt; */
     131             :       /* fd_vote_acc_vote_t votes[31]; */
     132             :       /* uchar root_option */
     133             :       /* ulong root */
     134             :     } v4;
     135             :   };
     136             : };
     137             : typedef struct fd_vote_acc fd_vote_acc_t;
     138             : 
     139             : FD_FN_PURE ulong
     140             : fd_vote_acc_vote_cnt( uchar const * buf );
     141             : 
     142             : /* fd_vote_acc_vote_slot takes a voter's vote account data and returns
     143             :    the voter's most recent vote slot in the tower.  Returns ULONG_MAX if
     144             :    they have an empty tower. */
     145             : 
     146             : FD_FN_PURE ulong
     147             : fd_vote_acc_vote_slot( uchar const * buf );
     148             : 
     149             : /* fd_vote_acc_root_slot takes a voter's vote account data and returns
     150             :    the voter's root slot.  Returns ULONG_MAX if they don't have one. */
     151             : 
     152             : FD_FN_PURE ulong
     153             : fd_vote_acc_root_slot( uchar const * buf );
     154             : 
     155             : /* fd_txn_parse_simple_vote optionally extracts the vote account pubkey,
     156             :    identity pubkey, and largest voted-for slot from a vote transaction. */
     157             : 
     158             : int
     159             : fd_txn_parse_simple_vote( fd_txn_t const * txn,
     160             :                           uchar    const * payload,
     161             :                           fd_pubkey_t *    opt_identity,
     162             :                           fd_pubkey_t *    opt_vote_acct,
     163             :                           ulong *          opt_vote_slot );
     164             : 
     165             : #endif /* HEADER_fd_src_choreo_tower_fd_tower_serdes_h */

Generated by: LCOV version 1.14