LCOV - code coverage report
Current view: top level - disco/bundle - fd_bundle_crank.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 175 200 87.5 %
Date: 2025-08-05 05:04:49 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] = "TIP_ACCOUNT_0"; /* Not NUL terminated */
     204           9 :     uchar * seed_ptr[1] = { (uchar *)seed };
     205           9 :     ulong seed_len = 13;
     206          81 :     for( ulong i=0UL; i<8UL; i++ ) {
     207          72 :       seed[12] = (char)((ulong)'0' + i);
     208          72 :       uchar out_bump[1];
     209          72 :       FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_payment_program_addr,
     210          72 :                                                                   1UL, seed_ptr, &seed_len,
     211          72 :                                                                   (fd_pubkey_t *)g->crank3->tip_payment_accounts[i], out_bump, cerr ) );
     212          72 :     }
     213           9 :   } while( 0 );
     214             : 
     215           9 :   do {
     216           9 :     char seed[14] = "CONFIG_ACCOUNT"; /* Not NUL terminated */
     217           9 :     ulong seed_len = 14;
     218           9 :     uchar out_bump[1];
     219           9 :     uchar * seed_ptr[1] = { (uchar *)seed };
     220           9 :     FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_payment_program_addr,
     221           9 :                                                                 1UL, seed_ptr, &seed_len,
     222           9 :                                                                 (fd_pubkey_t *)g->crank3->tip_payment_program_config, out_bump, cerr ) );
     223             :     /* Same seed used for tip distribution config account too */
     224           9 :     FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_distribution_program_addr,
     225           9 :                                                                 1UL, seed_ptr, &seed_len,
     226           9 :                                                                 (fd_pubkey_t *)g->crank3->tip_distribution_program_config, out_bump, cerr ) );
     227           9 :   } while( 0 );
     228             : 
     229             :   /* Populate crank2 from crank3 */
     230           9 :   memcpy( g->crank2->tip_payment_accounts,            g->crank3->tip_payment_accounts,            8UL*32UL );
     231           9 :   memcpy( g->crank2->tip_distribution_program_config, g->crank3->tip_distribution_program_config,     32UL );
     232           9 :   memcpy( g->crank2->tip_payment_program_config,      g->crank3->tip_payment_program_config,          32UL );
     233           9 :   memcpy( g->crank2->tip_payment_program,             g->crank3->tip_payment_program,                 32UL );
     234           9 :   memcpy( g->crank2->memo.memo,                       g->crank3->memo.memo,                            3UL );
     235             : 
     236           9 :   FD_TEST( sizeof(g->txn3)==fd_txn_parse( (uchar const *)g->crank3, sizeof(g->crank3), g->txn3, NULL ) );
     237           9 :   FD_TEST( sizeof(g->txn2)==fd_txn_parse( (uchar const *)g->crank2, sizeof(g->crank2), g->txn2, NULL ) );
     238             : 
     239           9 :   pidx_map_new( g->map );
     240           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[0],         0 ) }} )->idx= 1UL;
     241           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[1],         0 ) }} )->idx= 2UL;
     242           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[2],         0 ) }} )->idx= 3UL;
     243           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[3],         0 ) }} )->idx= 4UL;
     244           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[4],         0 ) }} )->idx= 5UL;
     245           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[5],         0 ) }} )->idx= 6UL;
     246           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[6],         0 ) }} )->idx= 7UL;
     247           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[7],         0 ) }} )->idx= 8UL;
     248           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_distribution_program_config, 0 ) }} )->idx= 9UL;
     249           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_program_config,      0 ) }} )->idx=10UL;
     250           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->compute_budget_program,          0 ) }} )->idx=15UL;
     251           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_program,             0 ) }} )->idx=16UL;
     252           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->validator_vote_account,          0 ) }} )->idx=17UL;
     253           9 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_distribution_program,        0 ) }} )->idx=19UL;
     254             : 
     255           9 :   g->configured_epoch = ULONG_MAX;
     256           9 :   return g;
     257           9 : }
     258             : 
     259             : 
     260             : static inline void
     261             : fd_bundle_crank_update_epoch( fd_bundle_crank_gen_t * g,
     262           9 :                               ulong                   epoch ) {
     263           9 :   struct __attribute__((packed)) {
     264           9 :     char tip_distr_acct[24];
     265           9 :     uchar vote_pubkey  [32];
     266           9 :     ulong epoch;
     267           9 :   } seeds[1] = {{
     268           9 :     .tip_distr_acct = "TIP_DISTRIBUTION_ACCOUNT",
     269           9 :     .vote_pubkey    = { EXPAND_ARR32( g->crank3->validator_vote_account, 0 ) },
     270           9 :     .epoch          = epoch
     271           9 :   }};
     272           9 :   FD_STATIC_ASSERT( sizeof(seeds)==24UL+32UL+8UL, seed_struct );
     273           9 :   ulong seed_len = sizeof(seeds);
     274           9 :   uint custom_err[1];
     275             : 
     276           9 :   uchar * _seeds[1] = { (uchar *)seeds };
     277             : 
     278           9 :   FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)g->crank3->tip_distribution_program,
     279           9 :                                                               1UL, _seeds, &seed_len,
     280           9 :                                                               (fd_pubkey_t *)g->crank3->new_tip_receiver,
     281           9 :                                                               &(g->crank3->init_tip_distribution_acct.bump), custom_err ) );
     282           9 :   memcpy( g->crank2->new_tip_receiver, g->crank3->new_tip_receiver, 32UL );
     283           9 :   g->configured_epoch = epoch;
     284             : 
     285           9 : }
     286             : 
     287             : void
     288             : fd_bundle_crank_get_addresses( fd_bundle_crank_gen_t * gen,
     289             :                                ulong                   epoch,
     290             :                                fd_acct_addr_t        * out_tip_payment_config,
     291           3 :                                fd_acct_addr_t        * out_tip_receiver ) {
     292           3 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     293           3 :   memcpy( out_tip_payment_config, gen->crank3->tip_payment_program_config, 32UL );
     294           3 :   memcpy( out_tip_receiver,       gen->crank3->new_tip_receiver,           32UL );
     295           3 : }
     296             : 
     297             : ulong
     298             : fd_bundle_crank_generate( fd_bundle_crank_gen_t                       * gen,
     299             :                           fd_bundle_crank_tip_payment_config_t const  * old_tip_payment_config,
     300             :                           fd_acct_addr_t                       const  * new_block_builder,
     301             :                           fd_acct_addr_t                       const  * identity,
     302             :                           fd_acct_addr_t                       const  * tip_receiver_owner,
     303             :                           ulong                                         epoch,
     304             :                           ulong                                         block_builder_commission,
     305             :                           uchar                                       * out_payload,
     306          36 :                           fd_txn_t                                    * out_txn ) {
     307             : 
     308          36 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     309             : 
     310          36 :   if( FD_UNLIKELY( old_tip_payment_config->discriminator != 0x82ccfa1ee0aa0c9bUL ) ) {
     311           0 :     FD_LOG_WARNING(( "Found unexpected tip payment config account discriminator %lx.  Refusing to crank bundles.",
     312           0 :                       old_tip_payment_config->discriminator ));
     313           0 :     return ULONG_MAX;
     314           0 :   }
     315             : 
     316          36 :   int swap3 = !fd_memeq( tip_receiver_owner, gen->crank3->tip_distribution_program, sizeof(fd_acct_addr_t) );
     317             : 
     318          36 :   if( FD_LIKELY( fd_memeq( old_tip_payment_config->tip_receiver,  gen->crank3->new_tip_receiver, 32UL ) &&
     319          36 :                  fd_memeq( old_tip_payment_config->block_builder, new_block_builder,             32UL ) &&
     320          36 :                  !swap3                                                                                 &&
     321          36 :                  old_tip_payment_config->commission_pct==block_builder_commission ) ) {
     322             :     /* Everything configured properly! */
     323           6 :     return 0UL;
     324           6 :   }
     325             : 
     326             : 
     327          30 :   if( FD_UNLIKELY( swap3 ) ) {
     328           3 :     memcpy( gen->crank3->authorized_voter,  identity,                              32UL );
     329           3 :     memcpy( gen->crank3->new_block_builder, new_block_builder,                     32UL );
     330           3 :     memcpy( gen->crank3->old_tip_receiver,  old_tip_payment_config->tip_receiver,  32UL );
     331           3 :     memcpy( gen->crank3->old_block_builder, old_tip_payment_config->block_builder, 32UL );
     332           3 :     gen->crank3->change_block_builder.block_builder_commission_pct = block_builder_commission;
     333          27 :   } else {
     334          27 :     memcpy( gen->crank2->authorized_voter,  identity,                              32UL );
     335          27 :     memcpy( gen->crank2->new_block_builder, new_block_builder,                     32UL );
     336          27 :     memcpy( gen->crank2->old_tip_receiver,  old_tip_payment_config->tip_receiver,  32UL );
     337          27 :     memcpy( gen->crank2->old_block_builder, old_tip_payment_config->block_builder, 32UL );
     338          27 :     gen->crank2->change_block_builder.block_builder_commission_pct = block_builder_commission;
     339          27 :   }
     340             : 
     341             :   /* If it weren't for the fact that the old tip payment config is
     342             :      essentially attacker-controlled, we'd be golden.  However, someone
     343             :      trying to grief us can e.g. set the old block builder to the tip
     344             :      payment program, and if we're not careful, we'll create a
     345             :      transaction with a duplicate account.  We trust identity, new
     346             :      tip_receiver, and new_block_builder well enough though.  Note that
     347             :      it's not possible for either attacker-controlled address to be the
     348             :      system program, because the account must be writable, and write
     349             :      locks to the system program get demoted. */
     350          30 :   fd_bundle_crank_gen_pidx_t * identity_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)identity );
     351          30 :   if( FD_UNLIKELY( !identity_pidx ) ) {
     352           0 :     FD_LOG_WARNING(( "Indentity was already in map.  Refusing to crank bundles." ));
     353           0 :     return ULONG_MAX;
     354           0 :   }
     355          30 :   identity_pidx->idx = 0UL;
     356             : 
     357          30 :   fd_bundle_crank_gen_pidx_t * new_tr_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)gen->crank3->new_tip_receiver );
     358          30 :   if( FD_UNLIKELY( !new_tr_pidx ) ) {
     359           0 :     pidx_map_remove( gen->map, identity_pidx );
     360           0 :     FD_LOG_WARNING(( "New block builder was already in map.  Refusing to crank bundles." ));
     361           0 :     return ULONG_MAX;
     362           0 :   }
     363          30 :   new_tr_pidx->idx = 13UL;
     364             : 
     365          30 :   fd_bundle_crank_gen_pidx_t * new_bb_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)new_block_builder );
     366          30 :   if( FD_UNLIKELY( !new_bb_pidx ) ) {
     367           0 :     pidx_map_remove( gen->map, new_tr_pidx   );
     368           0 :     pidx_map_remove( gen->map, identity_pidx );
     369           0 :     FD_LOG_WARNING(( "New block builder was already in map.  Refusing to crank bundles." ));
     370           0 :     return ULONG_MAX;
     371           0 :   }
     372          30 :   new_bb_pidx->idx = 14UL;
     373             : 
     374          30 :   int inserted1 = 0;
     375          30 :   int inserted2 = 0;
     376          30 :   fd_bundle_crank_gen_pidx_t dummy1[1] = {{ .idx = 11UL }};
     377          30 :   fd_bundle_crank_gen_pidx_t dummy2[1] = {{ .idx = 12UL }};
     378             : 
     379          30 :   fd_bundle_crank_gen_pidx_t * old_tr_pidx = pidx_map_query( gen->map, *old_tip_payment_config->tip_receiver, NULL );
     380          30 :   if( FD_LIKELY( NULL==old_tr_pidx ) ) {
     381          15 :     old_tr_pidx = pidx_map_insert( gen->map, *old_tip_payment_config->tip_receiver );
     382          15 :     old_tr_pidx->idx = 11UL;
     383          15 :     inserted1 = 1;
     384          15 :   } else if( FD_UNLIKELY( !swap3 && old_tr_pidx->idx>16UL ) ) {
     385           0 :     old_tr_pidx = dummy1;
     386          15 :   } else {
     387             :     /* perturb the old tip receiver pubkey so that it's not a duplicate,
     388             :        then don't use it. */
     389          15 :     gen->crank3->old_tip_receiver[0]++;
     390          15 :     gen->crank2->old_tip_receiver[0]++;
     391          15 :   }
     392             : 
     393          30 :   fd_bundle_crank_gen_pidx_t * old_bb_pidx = pidx_map_query( gen->map, *old_tip_payment_config->block_builder, NULL );
     394          30 :   if( FD_UNLIKELY( NULL==old_bb_pidx ) ) {
     395           6 :     old_bb_pidx = pidx_map_insert( gen->map, *old_tip_payment_config->block_builder );
     396           6 :     old_bb_pidx->idx = 12UL;
     397           6 :     inserted2 = 1;
     398          24 :   } else if( FD_UNLIKELY( !swap3 && old_bb_pidx->idx>16UL ) ) {
     399           0 :     old_bb_pidx = dummy2;
     400          24 :   } else {
     401             :     /* perturb, but do it differently so it can't conflict with the
     402             :        old tip receiver if they're both the same. */
     403          24 :     gen->crank3->old_block_builder[0]--;
     404          24 :     gen->crank2->old_block_builder[0]--;
     405          24 :   }
     406             : 
     407          30 :   gen->crank3->change_tip_receiver.acct_idx [1] = (uchar)(old_tr_pidx->idx);
     408          30 :   gen->crank2->change_tip_receiver.acct_idx [1] = (uchar)(old_tr_pidx->idx);
     409          30 :   gen->crank3->change_tip_receiver.acct_idx [3] = (uchar)(old_bb_pidx->idx);
     410          30 :   gen->crank3->change_block_builder.acct_idx[2] = (uchar)(old_bb_pidx->idx);
     411          30 :   gen->crank2->change_tip_receiver.acct_idx [3] = (uchar)(old_bb_pidx->idx);
     412          30 :   gen->crank2->change_block_builder.acct_idx[2] = (uchar)(old_bb_pidx->idx);
     413             : 
     414          30 :   if( FD_UNLIKELY( inserted2 ) ) pidx_map_remove( gen->map, old_bb_pidx );
     415          30 :   if( FD_LIKELY  ( inserted1 ) ) pidx_map_remove( gen->map, old_tr_pidx );
     416          30 :   pidx_map_remove( gen->map, new_bb_pidx   );
     417          30 :   pidx_map_remove( gen->map, new_tr_pidx   );
     418          30 :   pidx_map_remove( gen->map, identity_pidx );
     419             : 
     420          30 :   if( FD_UNLIKELY( swap3 ) ) {
     421           3 :     fd_memcpy( out_payload, gen->crank3, sizeof(gen->crank3) );
     422           3 :     fd_memcpy( out_txn,     gen->txn3,   sizeof(gen->txn3)   );
     423           3 :     return sizeof(gen->crank3);
     424          27 :   } else {
     425          27 :     fd_memcpy( out_payload, gen->crank2, sizeof(gen->crank2) );
     426          27 :     fd_memcpy( out_txn,     gen->txn2,   sizeof(gen->txn2)   );
     427          27 :     return sizeof(gen->crank2);
     428          27 :   }
     429          30 : }
     430             : 
     431             : void
     432             : fd_bundle_crank_apply( fd_bundle_crank_gen_t                       * gen,
     433             :                        fd_bundle_crank_tip_payment_config_t        * tip_payment_config,
     434             :                        fd_acct_addr_t                       const  * new_block_builder,
     435             :                        fd_acct_addr_t                              * tip_receiver_owner,
     436             :                        ulong                                         epoch,
     437           0 :                        ulong                                         block_builder_commission ) {
     438             : 
     439           0 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     440             : 
     441           0 :   memcpy( tip_receiver_owner,                gen->crank3->tip_distribution_program, sizeof(fd_acct_addr_t) );
     442           0 :   memcpy( tip_payment_config->tip_receiver,  gen->crank3->new_tip_receiver,         sizeof(fd_acct_addr_t) );
     443           0 :   memcpy( tip_payment_config->block_builder, new_block_builder,                     sizeof(fd_acct_addr_t) );
     444             : 
     445           0 :   tip_payment_config->commission_pct = block_builder_commission;
     446           0 : }

Generated by: LCOV version 1.14