Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] SIMD-170: Reserve lower default CU limits for builtin instructions #3570

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/ballet/pack/fd_compute_budget_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ static const uchar FD_COMPUTE_BUDGET_PROGRAM_ID[FD_TXN_ACCT_ADDR_SZ] = {
10^(-6) lamports, so 10^(-15) SOL. */
#define FD_COMPUTE_BUDGET_MICRO_LAMPORTS_PER_LAMPORT (1000000UL)

/* Corresponds to Agave's MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT, which
uses a distinct maximum compute unit limit for builtin instructions. */
#define FD_MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT ( 3000UL)
#define FD_COMPUTE_BUDGET_DEFAULT_INSTR_CU_LIMIT ( 200000UL)
#define FD_COMPUTE_BUDGET_MAX_CU_LIMIT (1400000UL)

Expand Down Expand Up @@ -149,16 +152,23 @@ fd_compute_budget_program_parse( uchar const * instr_data,
the per-signature fee) is stored in out_rewards. The maximum number of
compute units this transaction can consume is stored in out_compute. If the
transaction execution has not completed by this limit, it is terminated and
considered failed. */
considered failed.

instr_cnt - number of non-builtin instructions in the transaction
builtin_instr_cnt - number of builtin instructions in the transaction */
static inline void
fd_compute_budget_program_finalize( fd_compute_budget_program_state_t const * state,
ulong instr_cnt,
ulong builtin_instr_cnt,
ulong * out_rewards,
uint * out_compute ) {
ulong cu_limit = 0UL;
if( FD_LIKELY( (state->flags & FD_COMPUTE_BUDGET_PROGRAM_FLAG_SET_CU)==0U ) ) {
/* Use default compute limit */
cu_limit = (instr_cnt - state->compute_budget_instr_cnt) * FD_COMPUTE_BUDGET_DEFAULT_INSTR_CU_LIMIT;
cu_limit = instr_cnt * FD_COMPUTE_BUDGET_DEFAULT_INSTR_CU_LIMIT;

/* SIMD-170: Use a different default CU limit for builtin instructions */
cu_limit += (builtin_instr_cnt - state->compute_budget_instr_cnt ) * FD_MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT;
} else cu_limit = state->compute_units;

cu_limit = fd_ulong_min( cu_limit, FD_COMPUTE_BUDGET_MAX_CU_LIMIT );
Expand Down
2 changes: 1 addition & 1 deletion src/ballet/pack/fd_pack_cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fd_pack_compute_cost( fd_txn_t const * txn,

ulong fee[1];
uint compute[1];
fd_compute_budget_program_finalize( cbp, txn->instr_cnt, fee, compute );
fd_compute_budget_program_finalize( cbp, non_builtin_cnt, txn->instr_cnt - non_builtin_cnt, fee, compute );

non_builtin_cnt = fd_ulong_min( non_builtin_cnt, FD_COMPUTE_BUDGET_MAX_CU_LIMIT/FD_COMPUTE_BUDGET_DEFAULT_INSTR_CU_LIMIT );

Expand Down
4 changes: 3 additions & 1 deletion src/ballet/pack/test_compute_budget_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ test_txn( uchar const * payload,
fd_compute_budget_program_state_t state;
fd_compute_budget_program_init( &state );
uchar const * addresses = payload + txn->acct_addr_off;
ulong builtin_instr_cnt = 0UL;
for( ulong i=0UL; i<txn->instr_cnt; i++ ) {
if( !memcmp( addresses+FD_TXN_ACCT_ADDR_SZ*txn->instr[ i ].program_id, FD_COMPUTE_BUDGET_PROGRAM_ID, FD_TXN_ACCT_ADDR_SZ ) ) {
builtin_instr_cnt += 1UL;
FD_TEST( fd_compute_budget_program_parse( payload+txn->instr[ i ].data_off, txn->instr[ i ].data_sz, &state ) );
}
}
ulong rewards = 0UL;
uint compute = 0U;
fd_compute_budget_program_finalize( &state, txn->instr_cnt, &rewards, &compute );
fd_compute_budget_program_finalize( &state, txn->instr_cnt - builtin_instr_cnt, builtin_instr_cnt, &rewards, &compute );
FD_TEST( rewards==expected_fee_lamports );
FD_TEST( (ulong)compute==expected_max_cu );
}
Expand Down
2 changes: 1 addition & 1 deletion src/ballet/pack/test_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ schedule_validate_microblock( fd_pack_t * pack,
FD_TEST( fd_compute_budget_program_parse( txnp->payload + ix.data_off, ix.data_sz, &cbp ) );
ix = txn->instr[1];
FD_TEST( fd_compute_budget_program_parse( txnp->payload + ix.data_off, ix.data_sz, &cbp ) );
fd_compute_budget_program_finalize( &cbp, txn->instr_cnt, &rewards, &compute );
fd_compute_budget_program_finalize( &cbp, txn->instr_cnt - 2, 2, &rewards, &compute );
} /* else it's a vote */

total_rewards += rewards;
Expand Down
1 change: 1 addition & 0 deletions src/flamenco/runtime/tests/fd_pack_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ do {
ulong rewards;
uint compute_unit_limit;
fd_compute_budget_program_finalize( cbp,
0UL,
input->instr_datas_count,
&rewards,
&compute_unit_limit
Expand Down
Loading