LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_instructions.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 71 0.0 %
Date: 2026-07-10 05:48:38 Functions: 0 3 0.0 %

          Line data    Source code
       1             : #include "fd_sysvar_instructions.h"
       2             : #include "../fd_runtime.h"
       3             : #include "../fd_system_ids.h"
       4             : 
       5             : static ulong
       6           0 : instructions_serialized_size( fd_txn_t const * txn ) {
       7           0 :   ushort instr_cnt = txn->instr_cnt;
       8           0 :   ulong  serialized_size = sizeof(ushort)                // num_instructions
       9           0 :                            + (sizeof(ushort)*instr_cnt); // instruction offsets
      10             : 
      11           0 :   for( ushort i=0; i<instr_cnt; i++ ) {
      12           0 :     ushort data_sz  = txn->instr[i].data_sz;
      13           0 :     ushort acct_cnt = txn->instr[i].acct_cnt;
      14             : 
      15           0 :     serialized_size += sizeof(ushort); // num_accounts;
      16             : 
      17           0 :     serialized_size += acct_cnt * (
      18           0 :       sizeof(uchar)               // flags (is_signer, is_writeable)
      19           0 :       + sizeof(fd_pubkey_t)       // pubkey
      20           0 :     );
      21             : 
      22           0 :     serialized_size += sizeof(fd_pubkey_t)  // program_id pubkey
      23           0 :         + sizeof(ushort)                    // instr_data_len;
      24           0 :         + data_sz;                          // instr_data;
      25             : 
      26           0 :   }
      27             : 
      28           0 :   serialized_size += sizeof(ushort); // current_instr_idx
      29             : 
      30           0 :   return serialized_size;
      31           0 : }
      32             : 
      33             : /* https://github.com/anza-xyz/agave/blob/v2.1.1/svm/src/account_loader.rs#L547-L576 */
      34             : void
      35             : fd_sysvar_instructions_serialize_account( fd_txn_in_t const * txn_in,
      36             :                                           fd_txn_out_t *      txn_out,
      37           0 :                                           ulong               txn_idx ) {
      38           0 :   fd_txn_t const * txn           = TXN( txn_in->txn );
      39           0 :   ulong            serialized_sz = instructions_serialized_size( txn );
      40           0 :   FD_TEST( serialized_sz<=FD_SYSVAR_INSTRUCTIONS_FOOTPRINT );
      41             : 
      42           0 :   fd_acc_t * acc = txn_out->accounts.account[ txn_idx ];
      43             :   /* Agave sets up the borrowed account for the instructions sysvar to contain
      44             :      default values except for the data which is serialized into the account.
      45             :      The accdb returns data=NULL for the sysvar instructions account because
      46             :      it is constructed on the fly and never persisted; point it at the
      47             :      dedicated scratch buffer in txn_out. */
      48             : 
      49           0 :   fd_memcpy( acc->owner, &fd_sysvar_owner_id, sizeof(fd_pubkey_t) );
      50           0 :   acc->lamports   = 0UL;
      51           0 :   acc->executable = 0;
      52           0 :   acc->data       = txn_out->accounts.sysvar_instructions_data;
      53           0 :   acc->data_len   = serialized_sz;
      54             : 
      55           0 :   uchar * serialized_instructions = acc->data;
      56           0 :   ulong offset = 0;
      57             : 
      58             :   // num_instructions
      59           0 :   ushort instr_cnt = txn->instr_cnt;
      60           0 :   FD_STORE( ushort, serialized_instructions + offset, instr_cnt);
      61           0 :   offset += sizeof(ushort);
      62             : 
      63             :   // instruction offsets
      64           0 :   uchar * serialized_instruction_offsets = serialized_instructions + offset;
      65           0 :   offset += (ushort)(sizeof(ushort) * instr_cnt);
      66             : 
      67             :   // serialize instructions
      68           0 :   for( ushort i=0; i<instr_cnt; ++i ) {
      69             :     // set the instruction offset
      70           0 :     FD_STORE( ushort, serialized_instruction_offsets, (ushort)offset );
      71           0 :     serialized_instruction_offsets += sizeof(ushort);
      72             : 
      73           0 :     fd_txn_instr_t const * instr = &txn->instr[i];
      74             : 
      75             :     // num_accounts
      76           0 :     FD_STORE( ushort, serialized_instructions + offset, instr->acct_cnt );
      77           0 :     offset += sizeof(ushort);
      78             : 
      79           0 :     uchar const * instr_accts = fd_txn_get_instr_accts( instr, txn_in->txn->payload );
      80           0 :     for( ushort j=0; j<instr->acct_cnt; j++ ) {
      81           0 :       uchar idx_in_txn          = instr_accts[j];
      82           0 :       uchar is_writable         = (uchar)fd_runtime_account_is_writable_idx( txn_in, txn_out, idx_in_txn );
      83           0 :       uchar is_signer           = (uchar)fd_txn_is_signer( txn, idx_in_txn );
      84           0 :       uchar flags               = (uchar)( ((!!is_signer)*FD_INSTR_ACCT_FLAGS_IS_SIGNER) | ((!!is_writable)*FD_INSTR_ACCT_FLAGS_IS_WRITABLE) );
      85             : 
      86             :       // flags
      87           0 :       FD_STORE( uchar, serialized_instructions + offset, flags );
      88           0 :       offset += sizeof(uchar);
      89             : 
      90             :       // pubkey
      91           0 :       FD_STORE( fd_pubkey_t, serialized_instructions + offset, txn_out->accounts.keys[ idx_in_txn ] );
      92           0 :       offset += sizeof(fd_pubkey_t);
      93           0 :     }
      94             : 
      95             :     // program_id_pubkey
      96           0 :     FD_STORE( fd_pubkey_t, serialized_instructions + offset, txn_out->accounts.keys[ instr->program_id ] );
      97           0 :     offset += sizeof(fd_pubkey_t);
      98             : 
      99             :     // instr_data_len
     100           0 :     FD_STORE( ushort, serialized_instructions + offset, instr->data_sz );
     101           0 :     offset += sizeof(ushort);
     102             : 
     103             :     // instr_data
     104           0 :     uchar const * instr_data = fd_txn_get_instr_data( instr, txn_in->txn->payload );
     105           0 :     fd_memcpy( serialized_instructions + offset, instr_data, instr->data_sz );
     106           0 :     offset += instr->data_sz;
     107           0 :   }
     108             : 
     109           0 :   FD_STORE( ushort, serialized_instructions + offset, 0 );
     110           0 :   offset += sizeof(ushort);
     111           0 : }
     112             : 
     113             : /* Stores the current instruction index in the instructions sysvar account.
     114             :    https://github.com/anza-xyz/solana-sdk/blob/instructions-sysvar%40v2.2.1/instructions-sysvar/src/lib.rs#L164-L167 */
     115             : void
     116             : fd_sysvar_instructions_update_current_instr_idx( fd_acc_t * acc,
     117           0 :                                                  ushort     current_instr_idx ) {
     118             :   /* Extra safety checks */
     119           0 :   if( FD_UNLIKELY( acc->data_len<sizeof(ushort) ) ) {
     120           0 :     return;
     121           0 :   }
     122             : 
     123           0 :   uchar * serialized_current_instr_idx = acc->data + (acc->data_len - sizeof(ushort));
     124           0 :   FD_STORE( ushort, serialized_current_instr_idx, current_instr_idx );
     125           0 : }

Generated by: LCOV version 1.14