LCOV - code coverage report
Current view: top level - flamenco/runtime/program - fd_config_program.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 137 0.0 %
Date: 2025-01-08 12:08:44 Functions: 0 2 0.0 %

          Line data    Source code
       1             : #include "fd_config_program.h"
       2             : #include "../fd_account.h"
       3             : #include "../fd_acc_mgr.h"
       4             : #include "../fd_executor.h"
       5             : #include "../fd_system_ids.h"
       6             : #include "../context/fd_exec_epoch_ctx.h"
       7             : #include "../context/fd_exec_slot_ctx.h"
       8             : #include "../context/fd_exec_txn_ctx.h"
       9             : #include "../context/fd_exec_instr_ctx.h"
      10             : 
      11             : /* Useful links:
      12             : 
      13             :    https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs */
      14             : 
      15             : #define DEFAULT_COMPUTE_UNITS 450UL
      16             : 
      17             : /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L16 */
      18             : 
      19             : static int
      20           0 : _process_config_instr( fd_exec_instr_ctx_t * ctx ) {
      21             : 
      22           0 : # define ACC_IDX_CONFIG ((uchar)0)
      23             : 
      24             :   /* Deserialize the Config Program instruction data, which consists only of the ConfigKeys
      25             :      https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L21 */
      26           0 :   if( FD_UNLIKELY( ctx->instr->data==NULL ) ) {
      27           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
      28           0 :   }
      29             : 
      30           0 :   fd_bincode_decode_ctx_t decode =
      31           0 :     { .valloc  = fd_spad_virtual( ctx->txn_ctx->spad ),
      32           0 :       .data    = ctx->instr->data,
      33           0 :       .dataend = ctx->instr->data + ctx->instr->data_sz };
      34             : 
      35           0 :   fd_config_keys_t key_list = {0};
      36           0 :   int decode_result = fd_config_keys_decode( &key_list, &decode );
      37             :   /* Fail if the number of bytes consumed by deserialize exceeds 1232
      38             :      (hardcoded constant by Agave limited_deserialize) */
      39           0 :   if( FD_UNLIKELY( decode_result != FD_BINCODE_SUCCESS || 
      40           0 :                    (ulong)ctx->instr->data + 1232UL < (ulong)decode.data ) ) {
      41           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
      42           0 :   }
      43             : 
      44             :   /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L22-L26 */
      45             : 
      46           0 :   fd_config_keys_t current_data;
      47           0 :   int is_config_account_signer = 0;
      48           0 :   fd_pubkey_t const * config_account_key = NULL;
      49           0 :   FD_BORROWED_ACCOUNT_TRY_BORROW_IDX( ctx, ACC_IDX_CONFIG, config_acc_rec ) {
      50             : 
      51           0 :   config_account_key = config_acc_rec->pubkey;
      52             : 
      53             :   /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L27 */
      54             : 
      55           0 :   is_config_account_signer = fd_instr_acc_is_signer_idx( ctx->instr, ACC_IDX_CONFIG );
      56             : 
      57             :   /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L29-L31 */
      58             : 
      59           0 :   if( FD_UNLIKELY( 0!=memcmp( &config_acc_rec->const_meta->info.owner, fd_solana_config_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
      60           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER;
      61           0 :   }
      62             : 
      63             :   /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L33-L40 */
      64             : 
      65           0 :   fd_bincode_decode_ctx_t config_acc_state_decode_context = {
      66           0 :     .valloc  = fd_spad_virtual( ctx->txn_ctx->spad ),
      67           0 :     .data    = config_acc_rec->const_data,
      68           0 :     .dataend = config_acc_rec->const_data + config_acc_rec->const_meta->dlen,
      69           0 :   };
      70           0 :   decode_result = fd_config_keys_decode( &current_data, &config_acc_state_decode_context );
      71           0 :   if( FD_UNLIKELY( decode_result!=FD_BINCODE_SUCCESS ) ) {
      72             :     //TODO: full log, including err
      73           0 :     fd_log_collector_msg_literal( ctx, "Unable to deserialize config account" );
      74           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
      75           0 :   }
      76             : 
      77             :   /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L42 */
      78             : 
      79           0 :   } FD_BORROWED_ACCOUNT_DROP( config_acc_rec );
      80             : 
      81             :   /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L44-L49 */
      82             : 
      83           0 :   fd_pubkey_t * current_signer_keys    = fd_scratch_alloc( alignof(fd_pubkey_t), sizeof(fd_pubkey_t) * current_data.keys_len );
      84           0 :   ulong         current_signer_key_cnt = 0UL;
      85             : 
      86           0 :   for( ulong i=0UL; i < current_data.keys_len; i++ ) {
      87           0 :     if( current_data.keys[i].signer ) {
      88           0 :       current_signer_keys[ current_signer_key_cnt++ ] = current_data.keys[i].key;
      89           0 :     }
      90           0 :   }
      91             : 
      92             :   /* If we have no keys in the account, require the config account to have signed the transaction
      93             :      https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L50-L56 */
      94             : 
      95           0 :   if( FD_UNLIKELY( current_signer_key_cnt==0UL && !is_config_account_signer ) ) {
      96           0 :     return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
      97           0 :   }
      98             : 
      99             :   /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L58 */
     100             : 
     101           0 :   ulong counter = 0UL;
     102             :   /* Invariant: counter <= key_list.keys_len */
     103             : 
     104           0 :   for( ulong i=0UL; i<key_list.keys_len; i++ ) {
     105           0 :     if( !key_list.keys[i].signer ) continue;
     106           0 :     fd_pubkey_t const * signer = &key_list.keys[i].key;
     107             : 
     108             :     /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L60 */
     109             : 
     110           0 :     counter++;
     111             : 
     112             :     /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L61 */
     113             : 
     114           0 :     if( 0!=memcmp( signer, config_account_key, sizeof(fd_pubkey_t) ) ) {
     115             : 
     116             :       /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L62-L71 */
     117             : 
     118             :       /* Intentionally don't use the scoping macro here because Anza maps the 
     119             :          error to missing required signature if the try borrow fails */
     120           0 :       fd_borrowed_account_t * signer_account = NULL;
     121           0 :       int borrow_err = fd_instr_borrowed_account_view_idx( ctx, (uchar)counter, &signer_account );
     122           0 :       if( FD_UNLIKELY( borrow_err!=FD_ACC_MGR_SUCCESS ) ) {
     123             :         /* Max msg_sz: 33 - 2 + 45 = 76 < 127 => we can use printf */
     124           0 :         fd_log_collector_printf_dangerous_max_127( ctx,
     125           0 :           "account %s is not in account list", FD_BASE58_ENC_32_ALLOCA( signer ) );
     126           0 :         return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
     127           0 :       }
     128           0 :       if( FD_UNLIKELY( !fd_borrowed_account_acquire_read( signer_account ) ) ) {
     129             :         /* Max msg_sz: 33 - 2 + 45 = 76 < 127 => we can use printf */
     130           0 :         fd_log_collector_printf_dangerous_max_127( ctx,
     131           0 :           "account %s is not in account list", FD_BASE58_ENC_32_ALLOCA( signer ) );
     132           0 :         return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;  /* seems to be deliberately not ACC_BORROW_FAILED? */
     133           0 :       }
     134             : 
     135             :       /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L72-L79 */
     136             : 
     137           0 :       if( FD_UNLIKELY( !fd_instr_acc_is_signer_idx( ctx->instr, (uchar)counter ) ) ) {
     138             :         /* Max msg_sz: 33 - 2 + 45 = 76 < 127 => we can use printf */
     139           0 :         fd_log_collector_printf_dangerous_max_127( ctx,
     140           0 :           "account %s signer_key().is_none()", FD_BASE58_ENC_32_ALLOCA( signer ) );
     141           0 :         return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
     142           0 :       }
     143             : 
     144             :       /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L80-L87 */
     145             : 
     146           0 :       if( FD_UNLIKELY( 0!=memcmp( signer_account->pubkey, signer, sizeof(fd_pubkey_t) ) ) ) {
     147             :         /* Max msg_sz: 53 - 3 + 20 = 70 < 127 => we can use printf */
     148           0 :         fd_log_collector_printf_dangerous_max_127( ctx,
     149           0 :           "account[%lu].signer_key() does not match Config data)", counter+1 );
     150           0 :         return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
     151           0 :       }
     152             : 
     153             :       /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L89-L98 */
     154             : 
     155           0 :       if( current_data.keys_len>0UL ) {
     156             :         /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L90 */
     157           0 :         int is_signer = 0;
     158           0 :         for( ulong j=0UL; j<current_signer_key_cnt; j++ ) {
     159           0 :           if( 0==memcmp( &current_signer_keys[j], signer, sizeof(fd_pubkey_t) ) ) {
     160           0 :             is_signer = 1;
     161           0 :             break;
     162           0 :           }
     163           0 :         }
     164             :         /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L97 */
     165           0 :         if( FD_UNLIKELY( !is_signer ) ) {
     166             :           /* Max msg_sz: 39 - 2 + 45 = 82 < 127 => we can use printf */
     167           0 :           fd_log_collector_printf_dangerous_max_127( ctx,
     168           0 :             "account %s is not in stored signer list", FD_BASE58_ENC_32_ALLOCA( signer ) );
     169           0 :           return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
     170           0 :         }
     171           0 :       }
     172             : 
     173           0 :       fd_borrowed_account_release_read( signer_account );
     174             : 
     175           0 :     } else if( !is_config_account_signer ) {
     176             : 
     177             :       /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L101 */
     178           0 :       fd_log_collector_msg_literal( ctx, "account[0].signer_key().is_none()" );
     179           0 :       return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
     180             : 
     181           0 :     }
     182           0 :   }
     183             : 
     184             :   /* Disallow duplicate keys
     185             :      https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L105-L115
     186             : 
     187             :   TODO: Agave uses a O(n log n) algorithm here */
     188           0 :   for( ulong i = 0; i < key_list.keys_len; i++ ) {
     189           0 :     for( ulong j = 0; j < key_list.keys_len; j++ ) {
     190           0 :       if( i == j ) continue;
     191             : 
     192           0 :       if( FD_UNLIKELY( memcmp( &key_list.keys[i].key, &key_list.keys[j].key, sizeof(fd_pubkey_t) ) == 0 && 
     193           0 :                         key_list.keys[i].signer == key_list.keys[j].signer ) ) {
     194           0 :         fd_log_collector_msg_literal( ctx, "new config contains duplicate keys" );
     195           0 :         return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
     196           0 :       }
     197           0 :     }
     198           0 :   }
     199             : 
     200             :   /* https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L118-L126 */
     201             : 
     202           0 :   if( FD_UNLIKELY( current_signer_key_cnt>counter ) ) {
     203             :     /* Max msg_sz: 35 - 6 + 2*20 = 69 < 127 => we can use printf */
     204           0 :     fd_log_collector_printf_dangerous_max_127( ctx,
     205           0 :       "too few signers: %lu; expected: %lu", counter, current_signer_key_cnt );
     206           0 :     return FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE;
     207           0 :   }
     208             : 
     209             :   /* Upgrade to writable handle
     210             :      https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L128-L129 */
     211             : 
     212           0 :   FD_BORROWED_ACCOUNT_TRY_BORROW_IDX( ctx, ACC_IDX_CONFIG, config_acc_rec ) {
     213             : 
     214             :   /* Upgrade to writable handle
     215             :      https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L130-L133 */
     216             : 
     217           0 :   if( FD_UNLIKELY( config_acc_rec->const_meta->dlen<ctx->instr->data_sz ) ) {
     218           0 :     fd_log_collector_msg_literal( ctx, "instruction data too large" );
     219           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
     220           0 :   }
     221             : 
     222             :   /* Inlined solana_sdk::transaction_context::BorrowedAccount::get_data_mut */
     223             : 
     224           0 :   do {
     225           0 :     int err;
     226           0 :     if( FD_UNLIKELY( !fd_account_can_data_be_changed( ctx, 0, &err ) ) ) {
     227           0 :       return err;
     228           0 :     }
     229           0 :   } while(0);
     230             : 
     231           0 :   do {
     232           0 :     int err = fd_instr_borrowed_account_modify_idx( ctx, 0, config_acc_rec->const_meta->dlen, &config_acc_rec );
     233           0 :     if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "fd_instr_borrowed_account_modify_idx failed (%d-%s)", err, fd_acc_mgr_strerror( err ) ));
     234           0 :   } while(0);
     235             : 
     236             :   /* copy_from_slice */
     237             : 
     238           0 :   fd_memcpy( config_acc_rec->data, ctx->instr->data, ctx->instr->data_sz );
     239             : 
     240             :   /* Implicitly dropped in Anza */
     241             : 
     242           0 :   } FD_BORROWED_ACCOUNT_DROP( config_acc_rec );
     243             : 
     244           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     245           0 : # undef ACC_IDX_CONFIG
     246             : 
     247           0 : }
     248             : 
     249             : int
     250           0 : fd_config_program_execute( fd_exec_instr_ctx_t * ctx ) {
     251             : 
     252             :   /* https://github.com/solana-labs/solana/blob/v1.17.27/programs/config/src/config_processor.rs#L14
     253             :      See DEFAULT_COMPUTE_UNITS */
     254           0 :   FD_EXEC_CU_UPDATE( ctx, DEFAULT_COMPUTE_UNITS );
     255             : 
     256           0 :   FD_SPAD_FRAME_BEGIN( ctx->txn_ctx->spad ) {
     257           0 :   FD_SCRATCH_SCOPE_BEGIN {
     258             : 
     259           0 :   int ret = _process_config_instr( ctx );
     260           0 :   return ret;
     261             : 
     262           0 :   } FD_SCRATCH_SCOPE_END;
     263           0 :   } FD_SPAD_FRAME_END;
     264           0 : }

Generated by: LCOV version 1.14