LCOV - code coverage report
Current view: top level - disco/bundle - fd_bundle_crank.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 196 0.0 %
Date: 2026-03-19 18:19:27 Functions: 0 5 0.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           0 : #define MAP_T    fd_bundle_crank_gen_pidx_t
     151           0 : #define MAP_KEY_T   fd_acct_addr_t
     152             : #define MAP_MEMOIZE   0
     153             : #define MAP_QUERY_OPT 2 /* low hit rate */
     154           0 : #define MAP_LG_SLOT_CNT 5 /* 18 entries, space for 32 */
     155           0 : #define MAP_KEY_NULL          null_addr
     156             : #if FD_HAS_AVX
     157           0 : # 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           0 : #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           0 : #define MAP_KEY_HASH(key)     ((uint)fd_ulong_hash( fd_ulong_load_8( (key).b ) ))
     164             : 
     165             : #include "../../util/tmpl/fd_map.c"
     166             : 
     167             : 
     168           0 : #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],
     169           0 : #define EXPAND_ARR32(arr, i) EXPAND_ARR8(arr, (i)) EXPAND_ARR8(arr, (i)+8) EXPAND_ARR8(arr, (i)+16) EXPAND_ARR8(arr, (i)+24)
     170             : 
     171             : 
     172             : 
     173             : fd_bundle_crank_gen_t *
     174             : fd_bundle_crank_gen_init( void                 * mem,
     175             :                           fd_acct_addr_t const * tip_distribution_program_addr,
     176             :                           fd_acct_addr_t const * tip_payment_program_addr,
     177             :                           fd_acct_addr_t const * validator_vote_acct_addr,
     178             :                           fd_acct_addr_t const * merkle_root_authority_addr,
     179             :                           char const *           scheduler_mode,
     180           0 :                           ulong                  commission_bps ) {
     181           0 :   fd_bundle_crank_gen_t * g = (fd_bundle_crank_gen_t *)mem;
     182           0 :   memcpy( g->crank3, fd_bundle_crank_3_base, sizeof(fd_bundle_crank_3_base) );
     183           0 :   memcpy( g->crank2, fd_bundle_crank_2_base, sizeof(fd_bundle_crank_2_base) );
     184             : 
     185           0 :   g->crank3->init_tip_distribution_acct.commission_bps = (ushort)commission_bps;
     186           0 :   memcpy( g->crank3->tip_distribution_program,                                tip_distribution_program_addr, 32UL );
     187           0 :   memcpy( g->crank3->tip_payment_program,                                     tip_payment_program_addr,      32UL );
     188           0 :   memcpy( g->crank3->validator_vote_account,                                  validator_vote_acct_addr,      32UL );
     189           0 :   memcpy( g->crank3->init_tip_distribution_acct.merkle_root_upload_authority, merkle_root_authority_addr,    32UL );
     190             : 
     191             :   /* What we want here is just an strncpy, but the compiler makes it
     192             :      really hard to use strncpy to make a deliberately potentially
     193             :      unterminated string.  Rather than fight the compiler, we basically
     194             :      hand-do it. */
     195           0 :   int is_nul;
     196           0 :   is_nul = 0      || (!scheduler_mode[0]);    g->crank3->memo.memo[0] = is_nul ? '\0' : scheduler_mode[0];
     197           0 :   is_nul = is_nul || (!scheduler_mode[1]);    g->crank3->memo.memo[1] = is_nul ? '\0' : scheduler_mode[1];
     198           0 :   is_nul = is_nul || (!scheduler_mode[2]);    g->crank3->memo.memo[2] = is_nul ? '\0' : scheduler_mode[2];
     199             : 
     200           0 :   uint  cerr[1];
     201           0 :   do {
     202           0 :     char seed[13];
     203           0 :     fd_memcpy( seed, "TIP_ACCOUNT_0", 13 ); /* Not NUL terminated */
     204           0 :     uchar const * seed_ptr[1] = { (uchar const *)seed };
     205           0 :     ulong seed_len = 13;
     206           0 :     for( ulong i=0UL; i<8UL; i++ ) {
     207           0 :       seed[12] = (char)((ulong)'0' + i);
     208           0 :       uchar out_bump[1];
     209           0 :       FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_payment_program_addr,
     210           0 :                                                                   1UL, seed_ptr, &seed_len,
     211           0 :                                                                   (fd_pubkey_t *)g->crank3->tip_payment_accounts[i], out_bump, cerr ) );
     212           0 :     }
     213           0 :   } while( 0 );
     214             : 
     215           0 :   do {
     216           0 :     char seed[14];
     217           0 :     fd_memcpy( seed, "CONFIG_ACCOUNT", 14 ); /* Not NUL terminated */
     218           0 :     ulong seed_len = 14;
     219           0 :     uchar out_bump[1];
     220           0 :     uchar const * seed_ptr[1] = { (uchar const *)seed };
     221           0 :     FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_payment_program_addr,
     222           0 :                                                                 1UL, seed_ptr, &seed_len,
     223           0 :                                                                 (fd_pubkey_t *)g->crank3->tip_payment_program_config, out_bump, cerr ) );
     224             :     /* Same seed used for tip distribution config account too */
     225           0 :     FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)tip_distribution_program_addr,
     226           0 :                                                                 1UL, seed_ptr, &seed_len,
     227           0 :                                                                 (fd_pubkey_t *)g->crank3->tip_distribution_program_config, out_bump, cerr ) );
     228           0 :   } while( 0 );
     229             : 
     230             :   /* Populate crank2 from crank3 */
     231           0 :   memcpy( g->crank2->tip_payment_accounts,            g->crank3->tip_payment_accounts,            8UL*32UL );
     232           0 :   memcpy( g->crank2->tip_distribution_program_config, g->crank3->tip_distribution_program_config,     32UL );
     233           0 :   memcpy( g->crank2->tip_payment_program_config,      g->crank3->tip_payment_program_config,          32UL );
     234           0 :   memcpy( g->crank2->tip_payment_program,             g->crank3->tip_payment_program,                 32UL );
     235           0 :   memcpy( g->crank2->memo.memo,                       g->crank3->memo.memo,                            3UL );
     236             : 
     237           0 :   FD_TEST( sizeof(g->txn3)==fd_txn_parse( (uchar const *)g->crank3, sizeof(g->crank3), g->txn3, NULL ) );
     238           0 :   FD_TEST( sizeof(g->txn2)==fd_txn_parse( (uchar const *)g->crank2, sizeof(g->crank2), g->txn2, NULL ) );
     239             : 
     240           0 :   pidx_map_new( g->map );
     241           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[0],         0 ) }} )->idx= 1UL;
     242           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[1],         0 ) }} )->idx= 2UL;
     243           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[2],         0 ) }} )->idx= 3UL;
     244           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[3],         0 ) }} )->idx= 4UL;
     245           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[4],         0 ) }} )->idx= 5UL;
     246           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[5],         0 ) }} )->idx= 6UL;
     247           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[6],         0 ) }} )->idx= 7UL;
     248           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_accounts[7],         0 ) }} )->idx= 8UL;
     249           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_distribution_program_config, 0 ) }} )->idx= 9UL;
     250           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_program_config,      0 ) }} )->idx=10UL;
     251           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->compute_budget_program,          0 ) }} )->idx=15UL;
     252           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_payment_program,             0 ) }} )->idx=16UL;
     253           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->validator_vote_account,          0 ) }} )->idx=17UL;
     254           0 :   pidx_map_insert( g->map, (fd_acct_addr_t){{ EXPAND_ARR32( g->crank3->tip_distribution_program,        0 ) }} )->idx=19UL;
     255             : 
     256           0 :   g->configured_epoch = ULONG_MAX;
     257           0 :   return g;
     258           0 : }
     259             : 
     260             : 
     261             : static inline void
     262             : fd_bundle_crank_update_epoch( fd_bundle_crank_gen_t * g,
     263           0 :                               ulong                   epoch ) {
     264           0 :   uchar const * const seeds[3] = {
     265           0 :     (uchar const *)"TIP_DISTRIBUTION_ACCOUNT",
     266           0 :     (uchar const *)g->crank3->validator_vote_account,
     267           0 :     (uchar const *)&epoch,
     268           0 :   };
     269           0 :   ulong seed_szs[3] = { 24, 32, 8 };
     270           0 :   uint custom_err[1];
     271           0 :   FD_TEST( FD_PUBKEY_SUCCESS==fd_pubkey_find_program_address( (fd_pubkey_t const *)g->crank3->tip_distribution_program,
     272           0 :                                                               3UL, seeds, seed_szs,
     273           0 :                                                               (fd_pubkey_t *)g->crank3->new_tip_receiver,
     274           0 :                                                               &(g->crank3->init_tip_distribution_acct.bump), custom_err ) );
     275           0 :   memcpy( g->crank2->new_tip_receiver, g->crank3->new_tip_receiver, 32UL );
     276           0 :   g->configured_epoch = epoch;
     277           0 : }
     278             : 
     279             : void
     280             : fd_bundle_crank_get_addresses( fd_bundle_crank_gen_t * gen,
     281             :                                ulong                   epoch,
     282             :                                fd_acct_addr_t        * out_tip_payment_config,
     283           0 :                                fd_acct_addr_t        * out_tip_receiver ) {
     284           0 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     285           0 :   memcpy( out_tip_payment_config, gen->crank3->tip_payment_program_config, 32UL );
     286           0 :   memcpy( out_tip_receiver,       gen->crank3->new_tip_receiver,           32UL );
     287           0 : }
     288             : 
     289             : ulong
     290             : fd_bundle_crank_generate( fd_bundle_crank_gen_t                       * gen,
     291             :                           fd_bundle_crank_tip_payment_config_t const  * old_tip_payment_config,
     292             :                           fd_acct_addr_t                       const  * new_block_builder,
     293             :                           fd_acct_addr_t                       const  * identity,
     294             :                           fd_acct_addr_t                       const  * tip_receiver_owner,
     295             :                           ulong                                         epoch,
     296             :                           ulong                                         block_builder_commission,
     297             :                           uchar                                       * out_payload,
     298           0 :                           fd_txn_t                                    * out_txn ) {
     299             : 
     300           0 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     301             : 
     302           0 :   if( FD_UNLIKELY( old_tip_payment_config->discriminator != 0x82ccfa1ee0aa0c9bUL ) ) {
     303           0 :     FD_LOG_WARNING(( "Found unexpected tip payment config account discriminator %lx.  Refusing to crank bundles.",
     304           0 :                       old_tip_payment_config->discriminator ));
     305           0 :     return ULONG_MAX;
     306           0 :   }
     307             : 
     308           0 :   int swap3 = !fd_memeq( tip_receiver_owner, gen->crank3->tip_distribution_program, sizeof(fd_acct_addr_t) );
     309             : 
     310           0 :   if( FD_LIKELY( fd_memeq( old_tip_payment_config->tip_receiver,  gen->crank3->new_tip_receiver, 32UL ) &&
     311           0 :                  fd_memeq( old_tip_payment_config->block_builder, new_block_builder,             32UL ) &&
     312           0 :                  !swap3                                                                                 &&
     313           0 :                  old_tip_payment_config->commission_pct==block_builder_commission ) ) {
     314             :     /* Everything configured properly! */
     315           0 :     return 0UL;
     316           0 :   }
     317             : 
     318             : 
     319           0 :   if( FD_UNLIKELY( swap3 ) ) {
     320           0 :     memcpy( gen->crank3->authorized_voter,  identity,                              32UL );
     321           0 :     memcpy( gen->crank3->new_block_builder, new_block_builder,                     32UL );
     322           0 :     memcpy( gen->crank3->old_tip_receiver,  old_tip_payment_config->tip_receiver,  32UL );
     323           0 :     memcpy( gen->crank3->old_block_builder, old_tip_payment_config->block_builder, 32UL );
     324           0 :     gen->crank3->change_block_builder.block_builder_commission_pct = block_builder_commission;
     325           0 :   } else {
     326           0 :     memcpy( gen->crank2->authorized_voter,  identity,                              32UL );
     327           0 :     memcpy( gen->crank2->new_block_builder, new_block_builder,                     32UL );
     328           0 :     memcpy( gen->crank2->old_tip_receiver,  old_tip_payment_config->tip_receiver,  32UL );
     329           0 :     memcpy( gen->crank2->old_block_builder, old_tip_payment_config->block_builder, 32UL );
     330           0 :     gen->crank2->change_block_builder.block_builder_commission_pct = block_builder_commission;
     331           0 :   }
     332             : 
     333             :   /* If it weren't for the fact that the old tip payment config is
     334             :      essentially attacker-controlled, we'd be golden.  However, someone
     335             :      trying to grief us can e.g. set the old block builder to the tip
     336             :      payment program, and if we're not careful, we'll create a
     337             :      transaction with a duplicate account.  We trust identity, new
     338             :      tip_receiver, and new_block_builder well enough though.  Note that
     339             :      it's not possible for either attacker-controlled address to be the
     340             :      system program, because the account must be writable, and write
     341             :      locks to the system program get demoted. */
     342           0 :   fd_bundle_crank_gen_pidx_t * identity_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)identity );
     343           0 :   if( FD_UNLIKELY( !identity_pidx ) ) {
     344           0 :     FD_LOG_WARNING(( "Identity was already in map.  Refusing to crank bundles." ));
     345           0 :     return ULONG_MAX;
     346           0 :   }
     347           0 :   identity_pidx->idx = 0UL;
     348             : 
     349           0 :   fd_bundle_crank_gen_pidx_t * new_tr_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)gen->crank3->new_tip_receiver );
     350           0 :   if( FD_UNLIKELY( !new_tr_pidx ) ) {
     351           0 :     pidx_map_remove( gen->map, identity_pidx );
     352           0 :     FD_LOG_WARNING(( "New tip receiver was already in map.  Refusing to crank bundles." ));
     353           0 :     return ULONG_MAX;
     354           0 :   }
     355           0 :   new_tr_pidx->idx = 13UL;
     356             : 
     357           0 :   fd_bundle_crank_gen_pidx_t * new_bb_pidx = pidx_map_insert( gen->map, *(fd_acct_addr_t *)new_block_builder );
     358           0 :   if( FD_UNLIKELY( !new_bb_pidx ) ) {
     359           0 :     pidx_map_remove( gen->map, new_tr_pidx   );
     360           0 :     pidx_map_remove( gen->map, identity_pidx );
     361           0 :     FD_LOG_WARNING(( "New block builder was already in map.  Refusing to crank bundles." ));
     362           0 :     return ULONG_MAX;
     363           0 :   }
     364           0 :   new_bb_pidx->idx = 14UL;
     365             : 
     366           0 :   int inserted1 = 0;
     367           0 :   int inserted2 = 0;
     368           0 :   fd_bundle_crank_gen_pidx_t dummy1[1] = {{ .idx = 11UL }};
     369           0 :   fd_bundle_crank_gen_pidx_t dummy2[1] = {{ .idx = 12UL }};
     370             : 
     371           0 :   fd_bundle_crank_gen_pidx_t * old_tr_pidx = pidx_map_query( gen->map, *old_tip_payment_config->tip_receiver, NULL );
     372           0 :   if( FD_LIKELY( NULL==old_tr_pidx ) ) {
     373           0 :     old_tr_pidx = pidx_map_insert( gen->map, *old_tip_payment_config->tip_receiver );
     374           0 :     old_tr_pidx->idx = 11UL;
     375           0 :     inserted1 = 1;
     376           0 :   } else if( FD_UNLIKELY( !swap3 && old_tr_pidx->idx>16UL ) ) {
     377           0 :     old_tr_pidx = dummy1;
     378           0 :   } else {
     379             :     /* perturb the old tip receiver pubkey so that it's not a duplicate,
     380             :        then don't use it. */
     381           0 :     gen->crank3->old_tip_receiver[0]++;
     382           0 :     gen->crank2->old_tip_receiver[0]++;
     383           0 :   }
     384             : 
     385           0 :   fd_bundle_crank_gen_pidx_t * old_bb_pidx = pidx_map_query( gen->map, *old_tip_payment_config->block_builder, NULL );
     386           0 :   if( FD_UNLIKELY( NULL==old_bb_pidx ) ) {
     387           0 :     old_bb_pidx = pidx_map_insert( gen->map, *old_tip_payment_config->block_builder );
     388           0 :     old_bb_pidx->idx = 12UL;
     389           0 :     inserted2 = 1;
     390           0 :   } else if( FD_UNLIKELY( !swap3 && old_bb_pidx->idx>16UL ) ) {
     391           0 :     old_bb_pidx = dummy2;
     392           0 :   } else {
     393             :     /* perturb, but do it differently so it can't conflict with the
     394             :        old tip receiver if they're both the same. */
     395           0 :     gen->crank3->old_block_builder[0]--;
     396           0 :     gen->crank2->old_block_builder[0]--;
     397           0 :   }
     398             : 
     399           0 :   gen->crank3->change_tip_receiver.acct_idx [1] = (uchar)(old_tr_pidx->idx);
     400           0 :   gen->crank2->change_tip_receiver.acct_idx [1] = (uchar)(old_tr_pidx->idx);
     401           0 :   gen->crank3->change_tip_receiver.acct_idx [3] = (uchar)(old_bb_pidx->idx);
     402           0 :   gen->crank3->change_block_builder.acct_idx[2] = (uchar)(old_bb_pidx->idx);
     403           0 :   gen->crank2->change_tip_receiver.acct_idx [3] = (uchar)(old_bb_pidx->idx);
     404           0 :   gen->crank2->change_block_builder.acct_idx[2] = (uchar)(old_bb_pidx->idx);
     405             : 
     406           0 :   if( FD_UNLIKELY( inserted2 ) ) pidx_map_remove( gen->map, old_bb_pidx );
     407           0 :   if( FD_LIKELY  ( inserted1 ) ) pidx_map_remove( gen->map, old_tr_pidx );
     408           0 :   pidx_map_remove( gen->map, new_bb_pidx   );
     409           0 :   pidx_map_remove( gen->map, new_tr_pidx   );
     410           0 :   pidx_map_remove( gen->map, identity_pidx );
     411             : 
     412           0 :   if( FD_UNLIKELY( swap3 ) ) {
     413           0 :     fd_memcpy( out_payload, gen->crank3, sizeof(gen->crank3) );
     414           0 :     fd_memcpy( out_txn,     gen->txn3,   sizeof(gen->txn3)   );
     415           0 :     return sizeof(gen->crank3);
     416           0 :   } else {
     417           0 :     fd_memcpy( out_payload, gen->crank2, sizeof(gen->crank2) );
     418           0 :     fd_memcpy( out_txn,     gen->txn2,   sizeof(gen->txn2)   );
     419           0 :     return sizeof(gen->crank2);
     420           0 :   }
     421           0 : }
     422             : 
     423             : void
     424             : fd_bundle_crank_apply( fd_bundle_crank_gen_t                       * gen,
     425             :                        fd_bundle_crank_tip_payment_config_t        * tip_payment_config,
     426             :                        fd_acct_addr_t                       const  * new_block_builder,
     427             :                        fd_acct_addr_t                              * tip_receiver_owner,
     428             :                        ulong                                         epoch,
     429           0 :                        ulong                                         block_builder_commission ) {
     430             : 
     431           0 :   if( FD_UNLIKELY( epoch!=gen->configured_epoch ) ) fd_bundle_crank_update_epoch( gen, epoch );
     432             : 
     433           0 :   memcpy( tip_receiver_owner,                gen->crank3->tip_distribution_program, sizeof(fd_acct_addr_t) );
     434           0 :   memcpy( tip_payment_config->tip_receiver,  gen->crank3->new_tip_receiver,         sizeof(fd_acct_addr_t) );
     435           0 :   memcpy( tip_payment_config->block_builder, new_block_builder,                     sizeof(fd_acct_addr_t) );
     436             : 
     437           0 :   tip_payment_config->commission_pct = block_builder_commission;
     438           0 : }

Generated by: LCOV version 1.14