LCOV - code coverage report
Current view: top level - disco/bundle - fd_bundle_crank.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 177 202 87.6 %
Date: 2025-12-06 04:45:29 Functions: 4 5 80.0 %

          Line data    Source code
       1             : #include "fd_bundle_crank.h"
       2             : #include "../../flamenco/runtime/fd_pubkey_utils.h"
       3             : 
       4             : #if FD_HAS_AVX
       5             : #include "../../util/simd/fd_avx.h"
       6             : #endif
       7             : 
       8             : FD_STATIC_ASSERT( sizeof(fd_bundle_crank_tip_payment_config_t)==89UL, config_struct );
       9             : 
      10             : #define MEMO_PROGRAM_ID 0x05U,0x4aU,0x53U,0x5aU,0x99U,0x29U,0x21U,0x06U,0x4dU,0x24U,0xe8U,0x71U,0x60U,0xdaU,0x38U,0x7cU, \
      11             :                         0x7cU,0x35U,0xb5U,0xddU,0xbcU,0x92U,0xbbU,0x81U,0xe4U,0x1fU,0xa8U,0x40U,0x41U,0x05U,0x44U,0x8dU
      12             : 
      13             : static const fd_bundle_crank_3_t fd_bundle_crank_3_base[1] = {{
      14             : 
      15             :     .sig_cnt         =  1,
      16             :     ._sig_cnt        =  1,
      17             :     .ro_signed_cnt   =  0,
      18             :     .ro_unsigned_cnt =  6,
      19             :     .acct_addr_cnt   = 21,
      20             : 
      21             :     .system_program         = { SYS_PROG_ID            },
      22             :     .compute_budget_program = { COMPUTE_BUDGET_PROG_ID },
      23             :     .memo_program           = { MEMO_PROGRAM_ID        },
      24             : 
      25             :     .instr_cnt = 5,
      26             :     .compute_budget_instruction = {
      27             :         .prog_id = 15,
      28             :         .acct_cnt = 0,
      29             :         .data_sz = 5,
      30             :         .set_cu_limit = 2,
      31             :         .cus = 130000U
      32             :     },
      33             : 
      34             :     .init_tip_distribution_acct = {
      35             :         .prog_id = 19,
      36             :         .acct_cnt = 5,
      37             :         .acct_idx = { 9, 13, 17, 0, 18 },
      38             :         .data_sz = 43,
      39             :         .ix_discriminator = { FD_BUNDLE_CRANK_DISC_INIT_TIP_DISTR },
      40             :     },
      41             : 
      42             :     .change_tip_receiver = {
      43             :         .prog_id = 16,
      44             :         .acct_cnt = 13,
      45             :         .acct_idx = { 10, 11, 13, 12, 1, 2, 3, 4, 5, 6, 7, 8, 0 },
      46             :         .data_sz = 8,
      47             :         .ix_discriminator = { FD_BUNDLE_CRANK_DISC_CHANGE_TIP_RCV }
      48             :     },
      49             : 
      50             :     .change_block_builder = {
      51             :         .prog_id = 16,
      52             :         .acct_cnt = 13,
      53             :         .acct_idx = { 10, 13, 12, 14, 1, 2, 3, 4, 5, 6, 7, 8, 0},
      54             :         .data_sz = 16,
      55             :         .ix_discriminator = { FD_BUNDLE_CRANK_DISC_CHANGE_BLK_BLD },
      56             :     },
      57             : 
      58             :     .memo = {
      59             :       .prog_id = 20,
      60             :       .acct_cnt = 0,
      61             :       .data_sz  = 3
      62             :     },
      63             : 
      64             :     /* Account addresses that depend on the network: */
      65             :     .tip_payment_accounts            = {{ 0 }},
      66             :     .tip_distribution_program_config =  { 0 },
      67             :     .tip_payment_program_config      =  { 0 },
      68             :     .tip_distribution_program        =  { 0 },
      69             :     .tip_payment_program             =  { 0 },
      70             : 
      71             :     /* Fields that depend on the validator configuration: */
      72             :     .authorized_voter       = { 0 },
      73             :     .validator_vote_account = { 0 },
      74             :     .memo.memo              = { 0 },
      75             :     .init_tip_distribution_acct.merkle_root_upload_authority = { 0 },
      76             :     .init_tip_distribution_acct.commission_bps               = 0,
      77             :     .init_tip_distribution_acct.bump                         = 0,
      78             : 
      79             :     /* Fields that vary each time: */
      80             :     .old_tip_receiver  = { 0 },
      81             :     .old_block_builder = { 0 },
      82             :     .new_tip_receiver  = { 0 },
      83             :     .new_block_builder = { 0 },
      84             :     .change_block_builder.block_builder_commission_pct = 0UL
      85             : }};
      86             : 
      87             : static const fd_bundle_crank_2_t fd_bundle_crank_2_base[1] = {{
      88             : 
      89             :     .sig_cnt         =  1,
      90             :     ._sig_cnt        =  1,
      91             :     .ro_signed_cnt   =  0,
      92             :     .ro_unsigned_cnt =  3,
      93             :     .acct_addr_cnt   = 18,
      94             : 
      95             :     .compute_budget_program = { COMPUTE_BUDGET_PROG_ID },
      96             :     .memo_program           = { MEMO_PROGRAM_ID        },
      97             : 
      98             :     .instr_cnt = 4,
      99             :     .compute_budget_instruction = {
     100             :         .prog_id = 15,
     101             :         .acct_cnt = 0,
     102             :         .data_sz = 5,
     103             :         .set_cu_limit = 2,
     104             :         .cus = 83000U
     105             :     },
     106             : 
     107             :     .change_tip_receiver = {
     108             :         .prog_id = 16,
     109             :         .acct_cnt = 13,
     110             :         .acct_idx = { 10, 11, 13, 12, 1, 2, 3, 4, 5, 6, 7, 8, 0 },
     111             :         .data_sz = 8,
     112             :         .ix_discriminator = { FD_BUNDLE_CRANK_DISC_CHANGE_TIP_RCV }
     113             :     },
     114             : 
     115             :     .change_block_builder = {
     116             :         .prog_id = 16,
     117             :         .acct_cnt = 13,
     118             :         .acct_idx = { 10, 13, 12, 14, 1, 2, 3, 4, 5, 6, 7, 8, 0},
     119             :         .data_sz = 16,
     120             :         .ix_discriminator = { FD_BUNDLE_CRANK_DISC_CHANGE_BLK_BLD },
     121             :     },
     122             : 
     123             :     .memo = {
     124             :       .prog_id = 17,
     125             :       .acct_cnt = 0,
     126             :       .data_sz = 3
     127             :     },
     128             : 
     129             :     /* Account addresses that depend on the network: */
     130             :     .tip_payment_accounts            = {{ 0 }},
     131             :     .tip_distribution_program_config =  { 0 },
     132             :     .tip_payment_program_config      =  { 0 },
     133             :     .tip_payment_program             =  { 0 },
     134             : 
     135             :     /* Fields that depend on the validator configuration: */
     136             :     .authorized_voter       = { 0 },
     137             :     .memo.memo              = { 0 },
     138             : 
     139             :     /* Fields that vary each time: */
     140             :     .old_tip_receiver  = { 0 },
     141             :     .old_block_builder = { 0 },
     142             :     .new_tip_receiver  = { 0 },
     143             :     .new_block_builder = { 0 },
     144             :     .change_block_builder.block_builder_commission_pct = 0UL
     145             : }};
     146             : 
     147             : static const fd_acct_addr_t null_addr = { 0 };
     148             : 
     149             : #define MAP_NAME pidx_map
     150         306 : #define MAP_T    fd_bundle_crank_gen_pidx_t
     151        1068 : #define MAP_KEY_T   fd_acct_addr_t
     152             : #define MAP_MEMOIZE   0
     153             : #define MAP_QUERY_OPT 2 /* low hit rate */
     154        1404 : #define MAP_LG_SLOT_CNT 5 /* 18 entries, space for 32 */
     155         399 : #define MAP_KEY_NULL          null_addr
     156             : #if FD_HAS_AVX
     157        1068 : # define MAP_KEY_INVAL(k)     _mm256_testz_si256( wb_ldu( (k).b ), wb_ldu( (k).b ) )
     158             : #else
     159             : # define MAP_KEY_INVAL(k)     MAP_KEY_EQUAL(k, null_addr)
     160             : #endif
     161         660 : #define MAP_KEY_EQUAL(k0,k1)  (!memcmp((k0).b,(k1).b, FD_TXN_ACCT_ADDR_SZ))
     162             : #define MAP_KEY_EQUAL_IS_SLOW 1
     163             : #define MAP_MEMOIZE           0
     164         336 : #define MAP_KEY_HASH(key)     ((uint)fd_ulong_hash( fd_ulong_load_8( (key).b ) ))
     165             : 
     166             : #include "../../util/tmpl/fd_map.c"
     167             : 
     168             : 
     169         540 : #define EXPAND_ARR8(arr, i)  arr[(i)], arr[(i)+1], arr[(i)+2], arr[(i)+3], arr[(i)+4], arr[(i)+5], arr[(i)+6], arr[(i)+7],
     170         135 : #define EXPAND_ARR32(arr, i) EXPAND_ARR8(arr, (i)) EXPAND_ARR8(arr, (i)+8) EXPAND_ARR8(arr, (i)+16) EXPAND_ARR8(arr, (i)+24)
     171             : 
     172             : 
     173             : 
     174             : fd_bundle_crank_gen_t *
     175             : fd_bundle_crank_gen_init( void                 * mem,
     176             :                           fd_acct_addr_t const * tip_distribution_program_addr,
     177             :                           fd_acct_addr_t const * tip_payment_program_addr,
     178             :                           fd_acct_addr_t const * validator_vote_acct_addr,
     179             :                           fd_acct_addr_t const * merkle_root_authority_addr,
     180             :                           char const *           scheduler_mode,
     181           9 :                           ulong                  commission_bps ) {
     182           9 :   fd_bundle_crank_gen_t * g = (fd_bundle_crank_gen_t *)mem;
     183           9 :   memcpy( g->crank3, fd_bundle_crank_3_base, sizeof(fd_bundle_crank_3_base) );
     184           9 :   memcpy( g->crank2, fd_bundle_crank_2_base, sizeof(fd_bundle_crank_2_base) );
     185             : 
     186           9 :   g->crank3->init_tip_distribution_acct.commission_bps = (ushort)commission_bps;
     187           9 :   memcpy( g->crank3->tip_distribution_program,                                tip_distribution_program_addr, 32UL );
     188           9 :   memcpy( g->crank3->tip_payment_program,                                     tip_payment_program_addr,      32UL );
     189           9 :   memcpy( g->crank3->validator_vote_account,                                  validator_vote_acct_addr,      32UL );
     190           9 :   memcpy( g->crank3->init_tip_distribution_acct.merkle_root_upload_authority, merkle_root_authority_addr,    32UL );
     191             : 
     192             :   /* What we want here is just an strncpy, but the compiler makes it
     193             :      really hard to use strncpy to make a deliberately potentially
     194             :      unterminated string.  Rather than fight the compiler, we basically
     195             :      hand-do it. */
     196           9 :   int is_nul;
     197           9 :   is_nul = 0      || (!scheduler_mode[0]);    g->crank3->memo.memo[0] = is_nul ? '\0' : scheduler_mode[0];
     198           9 :   is_nul = is_nul || (!scheduler_mode[1]);    g->crank3->memo.memo[1] = is_nul ? '\0' : scheduler_mode[1];
     199           9 :   is_nul = is_nul || (!scheduler_mode[2]);    g->crank3->memo.memo[2] = is_nul ? '\0' : scheduler_mode[2];
     200             : 
     201           9 :   uint  cerr[1];
     202           9 :   do {
     203           9 :     char seed[13];
     204           9 :     fd_memcpy( seed, "TIP_ACCOUNT_0", 13 ); /* Not NUL terminated */
     205           9 :     uchar const * seed_ptr[1] = { (uchar const *)seed };
     206           9 :     ulong seed_len = 13;
     207          81 :     for( ulong i=0UL; i<8UL; i++ ) {
     208          72 :       seed[12] = (char)((ulong)'0' + i);
     209          72 :       uchar out_bump[1];
     210          72 :       FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_payment_program_addr,
     211          72 :                                                                   1UL, seed_ptr, &seed_len,
     212          72 :                                                                   (fd_pubkey_t *)g->crank3->tip_payment_accounts[i], out_bump, cerr ) );
     213          72 :     }
     214           9 :   } while( 0 );
     215             : 
     216           9 :   do {
     217           9 :     char seed[14];
     218           9 :     fd_memcpy( seed, "CONFIG_ACCOUNT", 14 ); /* Not NUL terminated */
     219           9 :     ulong seed_len = 14;
     220           9 :     uchar out_bump[1];
     221           9 :     uchar const * seed_ptr[1] = { (uchar const *)seed };
     222           9 :     FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_payment_program_addr,
     223           9 :                                                                 1UL, seed_ptr, &seed_len,
     224           9 :                                                                 (fd_pubkey_t *)g->crank3->tip_payment_program_config, out_bump, cerr ) );
     225             :     /* Same seed used for tip distribution config account too */
     226           9 :     FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_distribution_program_addr,
     227           9 :                                                                 1UL, seed_ptr, &seed_len,
     228           9 :                                                                 (fd_pubkey_t *)g->crank3->tip_distribution_program_config, out_bump, cerr ) );
     229           9 :   } while( 0 );
     230             : 
     231             :   /* Populate crank2 from crank3 */
     232           9 :   memcpy( g->crank2->tip_payment_accounts,            g->crank3->tip_payment_accounts,            8UL*32UL );
     233           9 :   memcpy( g->crank2->tip_distribution_program_config, g->crank3->tip_distribution_program_config,     32UL );
     234           9 :   memcpy( g->crank2->tip_payment_program_config,      g->crank3->tip_payment_program_config,          32UL );
     235           9 :   memcpy( g->crank2->tip_payment_program,             g->crank3->tip_payment_program,                 32UL );
     236           9 :   memcpy( g->crank2->memo.memo,                       g->crank3->memo.memo,                            3UL );
     237             : 
     238           9 :   FD_TEST( sizeof(g->txn3)==fd_txn_parse( (uchar const *)g->crank3, sizeof(g->crank3), g->txn3, NULL ) );
     239           9 :   FD_TEST( sizeof(g->txn2)==fd_txn_parse( (uchar const *)g->crank2, sizeof(g->crank2), g->txn2, NULL ) );
     240             : 
     241           9 :   pidx_map_new( g->map );
     242           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[0],         0 ) }} )->idx= 1UL;
     243           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[1],         0 ) }} )->idx= 2UL;
     244           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[2],         0 ) }} )->idx= 3UL;
     245           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[3],         0 ) }} )->idx= 4UL;
     246           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[4],         0 ) }} )->idx= 5UL;
     247           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[5],         0 ) }} )->idx= 6UL;
     248           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[6],         0 ) }} )->idx= 7UL;
     249           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[7],         0 ) }} )->idx= 8UL;
     250           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_distribution_program_config, 0 ) }} )->idx= 9UL;
     251           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_program_config,      0 ) }} )->idx=10UL;
     252           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->compute_budget_program,          0 ) }} )->idx=15UL;
     253           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_program,             0 ) }} )->idx=16UL;
     254           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->validator_vote_account,          0 ) }} )->idx=17UL;
     255           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_distribution_program,        0 ) }} )->idx=19UL;
     256             : 
     257           9 :   g->configured_epoch = ULONG_MAX;
     258           9 :   return g;
     259           9 : }
     260             : 
     261             : 
     262             : static inline void
     263             : fd_bundle_crank_update_epoch( fd_bundle_crank_gen_t * g,
     264           9 :                               ulong                   epoch ) {
     265           9 :   struct __attribute__((packed)) {
     266           9 :     char tip_distr_acct[24];
     267           9 :     uchar vote_pubkey  [32];
     268           9 :     ulong epoch;
     269           9 :   } seeds[1] = {{
     270           9 :     .vote_pubkey = { EXPAND_ARR32( g->crank3->validator_vote_account, 0 ) },
     271           9 :     .epoch       = epoch
     272           9 :   }};
     273           9 :   memcpy( seeds->tip_distr_acct, "TIP_DISTRIBUTION_ACCOUNT", 24 );
     274           9 :   FD_STATIC_ASSERT( sizeof(seeds)==24UL+32UL+8UL, seed_struct );
     275           9 :   ulong seed_len = sizeof(seeds);
     276           9 :   uint custom_err[1];
     277             : 
     278           9 :   uchar const * _seeds[1] = { (uchar const *)seeds };
     279             : 
     280           9 :   FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)g->crank3->tip_distribution_program,
     281           9 :                                                               1UL, _seeds, &seed_len,
     282           9 :                                                               (fd_pubkey_t *)g->crank3->new_tip_receiver,
     283           9 :                                                               &(g->crank3->init_tip_distribution_acct.bump), custom_err ) );
     284           9 :   memcpy( g->crank2->new_tip_receiver, g->crank3->new_tip_receiver, 32UL );
     285           9 :   g->configured_epoch = epoch;
     286             : 
     287           9 : }
     288             : 
     289             : void
     290             : fd_bundle_crank_get_addresses( fd_bundle_crank_gen_t * gen,
     291             :                                ulong                   epoch,
     292             :                                fd_acct_addr_t        * out_tip_payment_config,
     293           3 :                                fd_acct_addr_t        * out_tip_receiver ) {
     294           3 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     295           3 :   memcpy( out_tip_payment_config, gen->crank3->tip_payment_program_config, 32UL );
     296           3 :   memcpy( out_tip_receiver,       gen->crank3->new_tip_receiver,           32UL );
     297           3 : }
     298             : 
     299             : ulong
     300             : fd_bundle_crank_generate( fd_bundle_crank_gen_t                       * gen,
     301             :                           fd_bundle_crank_tip_payment_config_t const  * old_tip_payment_config,
     302             :                           fd_acct_addr_t                       const  * new_block_builder,
     303             :                           fd_acct_addr_t                       const  * identity,
     304             :                           fd_acct_addr_t                       const  * tip_receiver_owner,
     305             :                           ulong                                         epoch,
     306             :                           ulong                                         block_builder_commission,
     307             :                           uchar                                       * out_payload,
     308          36 :                           fd_txn_t                                    * out_txn ) {
     309             : 
     310          36 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     311             : 
     312          36 :   if( FD_UNLIKELY( old_tip_payment_config->discriminator != 0x82ccfa1ee0aa0c9bUL ) ) {
     313           0 :     FD_LOG_WARNING(( "Found unexpected tip payment config account discriminator %lx.  Refusing to crank bundles.",
     314           0 :                       old_tip_payment_config->discriminator ));
     315           0 :     return ULONG_MAX;
     316           0 :   }
     317             : 
     318          36 :   int swap3 = !fd_memeq( tip_receiver_owner, gen->crank3->tip_distribution_program, sizeof(fd_acct_addr_t) );
     319             : 
     320          36 :   if( FD_LIKELY( fd_memeq( old_tip_payment_config->tip_receiver,  gen->crank3->new_tip_receiver, 32UL ) &&
     321          36 :                  fd_memeq( old_tip_payment_config->block_builder, new_block_builder,             32UL ) &&
     322          36 :                  !swap3                                                                                 &&
     323          36 :                  old_tip_payment_config->commission_pct==block_builder_commission ) ) {
     324             :     /* Everything configured properly! */
     325           6 :     return 0UL;
     326           6 :   }
     327             : 
     328             : 
     329          30 :   if( FD_UNLIKELY( swap3 ) ) {
     330           3 :     memcpy( gen->crank3->authorized_voter,  identity,                              32UL );
     331           3 :     memcpy( gen->crank3->new_block_builder, new_block_builder,                     32UL );
     332           3 :     memcpy( gen->crank3->old_tip_receiver,  old_tip_payment_config->tip_receiver,  32UL );
     333           3 :     memcpy( gen->crank3->old_block_builder, old_tip_payment_config->block_builder, 32UL );
     334           3 :     gen->crank3->change_block_builder.block_builder_commission_pct = block_builder_commission;
     335          27 :   } else {
     336          27 :     memcpy( gen->crank2->authorized_voter,  identity,                              32UL );
     337          27 :     memcpy( gen->crank2->new_block_builder, new_block_builder,                     32UL );
     338          27 :     memcpy( gen->crank2->old_tip_receiver,  old_tip_payment_config->tip_receiver,  32UL );
     339          27 :     memcpy( gen->crank2->old_block_builder, old_tip_payment_config->block_builder, 32UL );
     340          27 :     gen->crank2->change_block_builder.block_builder_commission_pct = block_builder_commission;
     341          27 :   }
     342             : 
     343             :   /* If it weren't for the fact that the old tip payment config is
     344             :      essentially attacker-controlled, we'd be golden.  However, someone
     345             :      trying to grief us can e.g. set the old block builder to the tip
     346             :      payment program, and if we're not careful, we'll create a
     347             :      transaction with a duplicate account.  We trust identity, new
     348             :      tip_receiver, and new_block_builder well enough though.  Note that
     349             :      it's not possible for either attacker-controlled address to be the
     350             :      system program, because the account must be writable, and write
     351             :      locks to the system program get demoted. */
     352          30 :   fd_bundle_crank_gen_pidx_t * identity_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)identity );
     353          30 :   if( FD_UNLIKELY( !identity_pidx ) ) {
     354           0 :     FD_LOG_WARNING(( "Identity was already in map.  Refusing to crank bundles." ));
     355           0 :     return ULONG_MAX;
     356           0 :   }
     357          30 :   identity_pidx->idx = 0UL;
     358             : 
     359          30 :   fd_bundle_crank_gen_pidx_t * new_tr_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)gen->crank3->new_tip_receiver );
     360          30 :   if( FD_UNLIKELY( !new_tr_pidx ) ) {
     361           0 :     pidx_map_remove( gen->map, identity_pidx );
     362           0 :     FD_LOG_WARNING(( "New tip receiver was already in map.  Refusing to crank bundles." ));
     363           0 :     return ULONG_MAX;
     364           0 :   }
     365          30 :   new_tr_pidx->idx = 13UL;
     366             : 
     367          30 :   fd_bundle_crank_gen_pidx_t * new_bb_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)new_block_builder );
     368          30 :   if( FD_UNLIKELY( !new_bb_pidx ) ) {
     369           0 :     pidx_map_remove( gen->map, new_tr_pidx   );
     370           0 :     pidx_map_remove( gen->map, identity_pidx );
     371           0 :     FD_LOG_WARNING(( "New block builder was already in map.  Refusing to crank bundles." ));
     372           0 :     return ULONG_MAX;
     373           0 :   }
     374          30 :   new_bb_pidx->idx = 14UL;
     375             : 
     376          30 :   int inserted1 = 0;
     377          30 :   int inserted2 = 0;
     378          30 :   fd_bundle_crank_gen_pidx_t dummy1[1] = {{ .idx = 11UL }};
     379          30 :   fd_bundle_crank_gen_pidx_t dummy2[1] = {{ .idx = 12UL }};
     380             : 
     381          30 :   fd_bundle_crank_gen_pidx_t * old_tr_pidx = pidx_map_query( gen->map, *old_tip_payment_config->tip_receiver, NULL );
     382          30 :   if( FD_LIKELY( NULL==old_tr_pidx ) ) {
     383          15 :     old_tr_pidx = pidx_map_insert( gen->map, *old_tip_payment_config->tip_receiver );
     384          15 :     old_tr_pidx->idx = 11UL;
     385          15 :     inserted1 = 1;
     386          15 :   } else if( FD_UNLIKELY( !swap3 && old_tr_pidx->idx>16UL ) ) {
     387           0 :     old_tr_pidx = dummy1;
     388          15 :   } else {
     389             :     /* perturb the old tip receiver pubkey so that it's not a duplicate,
     390             :        then don't use it. */
     391          15 :     gen->crank3->old_tip_receiver[0]++;
     392          15 :     gen->crank2->old_tip_receiver[0]++;
     393          15 :   }
     394             : 
     395          30 :   fd_bundle_crank_gen_pidx_t * old_bb_pidx = pidx_map_query( gen->map, *old_tip_payment_config->block_builder, NULL );
     396          30 :   if( FD_UNLIKELY( NULL==old_bb_pidx ) ) {
     397           6 :     old_bb_pidx = pidx_map_insert( gen->map, *old_tip_payment_config->block_builder );
     398           6 :     old_bb_pidx->idx = 12UL;
     399           6 :     inserted2 = 1;
     400          24 :   } else if( FD_UNLIKELY( !swap3 && old_bb_pidx->idx>16UL ) ) {
     401           0 :     old_bb_pidx = dummy2;
     402          24 :   } else {
     403             :     /* perturb, but do it differently so it can't conflict with the
     404             :        old tip receiver if they're both the same. */
     405          24 :     gen->crank3->old_block_builder[0]--;
     406          24 :     gen->crank2->old_block_builder[0]--;
     407          24 :   }
     408             : 
     409          30 :   gen->crank3->change_tip_receiver.acct_idx [1] = (uchar)(old_tr_pidx->idx);
     410          30 :   gen->crank2->change_tip_receiver.acct_idx [1] = (uchar)(old_tr_pidx->idx);
     411          30 :   gen->crank3->change_tip_receiver.acct_idx [3] = (uchar)(old_bb_pidx->idx);
     412          30 :   gen->crank3->change_block_builder.acct_idx[2] = (uchar)(old_bb_pidx->idx);
     413          30 :   gen->crank2->change_tip_receiver.acct_idx [3] = (uchar)(old_bb_pidx->idx);
     414          30 :   gen->crank2->change_block_builder.acct_idx[2] = (uchar)(old_bb_pidx->idx);
     415             : 
     416          30 :   if( FD_UNLIKELY( inserted2 ) ) pidx_map_remove( gen->map, old_bb_pidx );
     417          30 :   if( FD_LIKELY  ( inserted1 ) ) pidx_map_remove( gen->map, old_tr_pidx );
     418          30 :   pidx_map_remove( gen->map, new_bb_pidx   );
     419          30 :   pidx_map_remove( gen->map, new_tr_pidx   );
     420          30 :   pidx_map_remove( gen->map, identity_pidx );
     421             : 
     422          30 :   if( FD_UNLIKELY( swap3 ) ) {
     423           3 :     fd_memcpy( out_payload, gen->crank3, sizeof(gen->crank3) );
     424           3 :     fd_memcpy( out_txn,     gen->txn3,   sizeof(gen->txn3)   );
     425           3 :     return sizeof(gen->crank3);
     426          27 :   } else {
     427          27 :     fd_memcpy( out_payload, gen->crank2, sizeof(gen->crank2) );
     428          27 :     fd_memcpy( out_txn,     gen->txn2,   sizeof(gen->txn2)   );
     429          27 :     return sizeof(gen->crank2);
     430          27 :   }
     431          30 : }
     432             : 
     433             : void
     434             : fd_bundle_crank_apply( fd_bundle_crank_gen_t                       * gen,
     435             :                        fd_bundle_crank_tip_payment_config_t        * tip_payment_config,
     436             :                        fd_acct_addr_t                       const  * new_block_builder,
     437             :                        fd_acct_addr_t                              * tip_receiver_owner,
     438             :                        ulong                                         epoch,
     439           0 :                        ulong                                         block_builder_commission ) {
     440             : 
     441           0 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     442             : 
     443           0 :   memcpy( tip_receiver_owner,                gen->crank3->tip_distribution_program, sizeof(fd_acct_addr_t) );
     444           0 :   memcpy( tip_payment_config->tip_receiver,  gen->crank3->new_tip_receiver,         sizeof(fd_acct_addr_t) );
     445           0 :   memcpy( tip_payment_config->block_builder, new_block_builder,                     sizeof(fd_acct_addr_t) );
     446             : 
     447           0 :   tip_payment_config->commission_pct = block_builder_commission;
     448           0 : }

Generated by: LCOV version 1.14