Skip to content

Commit f13391c

Browse files
runtime: removing txn accounts from executor (#7388)
1 parent dec0030 commit f13391c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+846
-965
lines changed

src/discof/bank/fd_bank_tile.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ handle_microblock( fd_bank_ctx_t * ctx,
274274
that first the non-alt accounts are laid out, then the writable
275275
alt accounts, and finally the read-only alt accounts. */
276276
fd_txn_t * txn_descriptor = TXN( txn_in->txn );
277-
fd_acct_addr_t const * writable_alt = fd_type_pun_const( txn_out->accounts.account_keys+txn_descriptor->acct_addr_cnt );
277+
fd_acct_addr_t const * writable_alt = fd_type_pun_const( txn_out->accounts.keys+txn_descriptor->acct_addr_cnt );
278278
if( FD_LIKELY( ctx->enable_rebates ) ) fd_pack_rebate_sum_add_txn( ctx->rebater, txn, &writable_alt, 1UL );
279279

280280
/* The VM will stop executing and fail an instruction immediately if
@@ -390,7 +390,7 @@ handle_bundle( fd_bank_ctx_t * ctx,
390390
continue;
391391
}
392392

393-
writable_alt[i] = fd_type_pun_const( txn_out->accounts.account_keys+TXN( txn_in->txn )->acct_addr_cnt );
393+
writable_alt[i] = fd_type_pun_const( txn_out->accounts.keys+TXN( txn_in->txn )->acct_addr_cnt );
394394
}
395395

396396
/* If all of the transactions in the bundle executed successfully, we
@@ -597,15 +597,15 @@ unprivileged_init( fd_topo_t * topo,
597597
}
598598
}
599599

600-
ctx->runtime->accdb = accdb;
601-
ctx->runtime->funk = fd_accdb_user_v1_funk( accdb );
602-
ctx->runtime->progcache = progcache;
603-
ctx->runtime->status_cache = txncache;
604-
ctx->runtime->log.log_collector = ctx->log_collector;
600+
ctx->runtime->accdb = accdb;
601+
ctx->runtime->funk = fd_accdb_user_v1_funk( accdb );
602+
ctx->runtime->progcache = progcache;
603+
ctx->runtime->status_cache = txncache;
604+
ctx->runtime->log.log_collector = ctx->log_collector;
605605
ctx->runtime->log.enable_log_collector = 0;
606-
ctx->runtime->log.capture_ctx = NULL;
607-
ctx->runtime->log.dumping_mem = NULL;
608-
ctx->runtime->log.tracing_mem = NULL;
606+
ctx->runtime->log.capture_ctx = NULL;
607+
ctx->runtime->log.dumping_mem = NULL;
608+
ctx->runtime->log.tracing_mem = NULL;
609609

610610
ulong banks_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "banks" );
611611
FD_TEST( banks_obj_id!=ULONG_MAX );

src/flamenco/fd_flamenco_base.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ struct fd_account_meta {
116116
};
117117
typedef struct fd_account_meta fd_account_meta_t;
118118

119+
FD_FN_PURE static inline uchar *
120+
fd_account_data( fd_account_meta_t const * acc ) {
121+
return (uchar *)( acc+1 );
122+
}
123+
119124
FD_PROTOTYPES_BEGIN
120125

121126
/* fd_acct_addr_cstr converts the given Solana address into a base58-

src/flamenco/log_collector/fd_log_collector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ fd_log_collector_program_invoke( fd_exec_instr_ctx_t * ctx ) {
387387
return;
388388
}
389389

390-
fd_pubkey_t const * program_id_pubkey = &ctx->txn_out->accounts.account_keys[ ctx->instr->program_id ];
390+
fd_pubkey_t const * program_id_pubkey = &ctx->txn_out->accounts.keys[ ctx->instr->program_id ];
391391
/* Cache ctx->program_id_base58 */
392392
fd_base58_encode_32( program_id_pubkey->uc, NULL, ctx->program_id_base58 );
393393
/* Max msg_sz: 22 - 4 + 44 + 10 = 72 < 127 => we can use printf */

src/flamenco/progcache/fd_prog_load.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fd_get_executable_program_content_for_v4_loader( fd_txn_account_t const * progra
1818
int err;
1919

2020
/* Get the current loader v4 state. This implicitly also checks the dlen. */
21-
fd_loader_v4_state_t const * state = fd_loader_v4_get_state( program_acc, &err );
21+
fd_loader_v4_state_t const * state = fd_loader_v4_get_state( program_acc->meta, &err );
2222
if( FD_UNLIKELY( err ) ) {
2323
return NULL;
2424
}

src/flamenco/progcache/test_progcache_common.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ create_test_account( test_env_t * env,
140140
fd_txn_account_set_data( acc, data, data_len );
141141
}
142142

143-
acc->starting_lamports = 1UL;
144-
acc->starting_dlen = data_len;
145143
fd_txn_account_set_lamports( acc, 1UL );
146144
fd_txn_account_set_executable( acc, executable );
147145
fd_txn_account_set_owner( acc, &owner );

src/flamenco/rewards/fd_rewards.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ calculate_rewards_and_distribute_vote_rewards( fd_bank_t * ba
822822
FD_LOG_ERR(( "Adding lamports to vote account would cause overflow" ));
823823
}
824824

825-
fd_hashes_update_lthash( vote_rec, prev_hash,bank, capture_ctx );
825+
fd_hashes_update_lthash( vote_rec->pubkey, vote_rec->meta, prev_hash,bank, capture_ctx );
826826
fd_txn_account_mutable_fini( vote_rec, accdb, &prepare );
827827

828828
distributed_rewards = fd_ulong_sat_add( distributed_rewards, rewards );
@@ -877,7 +877,7 @@ distribute_epoch_reward_to_stake_acc( fd_bank_t * bank,
877877
fd_txn_account_set_slot( stake_acc_rec, fd_bank_slot_get( bank ) );
878878

879879
fd_stake_state_v2_t stake_state[1] = {0};
880-
if( fd_stake_get_state( stake_acc_rec, stake_state ) != 0 ) {
880+
if( fd_stake_get_state( stake_acc_rec->meta, stake_state ) != 0 ) {
881881
FD_LOG_DEBUG(( "failed to read stake state for %s", FD_BASE58_ENC_32_ALLOCA( stake_pubkey ) ));
882882
return 1;
883883
}
@@ -927,7 +927,7 @@ distribute_epoch_reward_to_stake_acc( fd_bank_t * bank,
927927
FD_LOG_ERR(( "write_stake_state failed" ));
928928
}
929929

930-
fd_hashes_update_lthash( stake_acc_rec, prev_hash, bank, capture_ctx );
930+
fd_hashes_update_lthash( stake_acc_rec->pubkey, stake_acc_rec->meta, prev_hash, bank, capture_ctx );
931931
fd_txn_account_mutable_fini( stake_acc_rec, accdb, &prepare );
932932

933933
return 0;

src/flamenco/runtime/context/fd_exec_instr_ctx.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fd_exec_instr_ctx_find_idx_of_instr_account( fd_exec_instr_ctx_t const * ctx,
77
fd_pubkey_t const * pubkey ) {
88
for( int i=0; i<ctx->instr->acct_cnt; i++ ) {
99
ushort idx_in_txn = ctx->instr->accounts[ i ].index_in_transaction;
10-
if( memcmp( pubkey->uc, ctx->txn_out->accounts.account_keys[ idx_in_txn ].uc, sizeof(fd_pubkey_t) )==0 ) {
10+
if( memcmp( pubkey->uc, ctx->txn_out->accounts.keys[ idx_in_txn ].uc, sizeof(fd_pubkey_t) )==0 ) {
1111
return i;
1212
}
1313
}
@@ -46,11 +46,9 @@ fd_exec_instr_ctx_try_borrow_account( fd_exec_instr_ctx_t const * ctx,
4646
fd_borrowed_account_t * account ) {
4747
/* Get the account from the transaction context using idx_in_txn.
4848
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L600-L602 */
49-
fd_txn_account_t * txn_account = NULL;
5049
int err = fd_runtime_get_account_at_index( ctx->txn_in,
5150
ctx->txn_out,
5251
idx_in_txn,
53-
&txn_account,
5452
NULL );
5553
if( FD_UNLIKELY( err ) ) {
5654
/* Return a MissingAccount error if the account is not found.
@@ -59,19 +57,23 @@ fd_exec_instr_ctx_try_borrow_account( fd_exec_instr_ctx_t const * ctx,
5957
return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
6058
}
6159

60+
fd_account_meta_t * meta = ctx->txn_out->accounts.metas[idx_in_txn];
61+
6262
/* Return an AccountBorrowFailed error if the write is not acquirable.
6363
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L605 */
64-
int borrow_res = fd_txn_account_try_borrow_mut( txn_account );
65-
if( FD_UNLIKELY( !borrow_res ) ) {
64+
if( FD_UNLIKELY( ctx->runtime->accounts.refcnt[idx_in_txn]!=0UL ) ) {
6665
return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
6766
}
67+
ctx->runtime->accounts.refcnt[idx_in_txn]++;
6868

6969
/* Create a BorrowedAccount upon success.
7070
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L606 */
7171
fd_borrowed_account_init( account,
72-
txn_account,
72+
&ctx->txn_out->accounts.keys[idx_in_txn],
73+
meta,
7374
ctx,
74-
idx_in_instr );
75+
idx_in_instr,
76+
&ctx->runtime->accounts.refcnt[idx_in_txn] );
7577
return FD_EXECUTOR_INSTR_SUCCESS;
7678
}
7779

@@ -101,7 +103,7 @@ fd_exec_instr_ctx_try_borrow_instr_account_with_key( fd_exec_instr_ctx_t const *
101103
fd_borrowed_account_t * account ) {
102104
for( ushort i=0; i<ctx->instr->acct_cnt; i++ ) {
103105
ushort idx_in_txn = ctx->instr->accounts[ i ].index_in_transaction;
104-
if( memcmp( pubkey->uc, ctx->txn_out->accounts.account_keys[ idx_in_txn ].uc, sizeof(fd_pubkey_t) )==0 ) {
106+
if( memcmp( pubkey->uc, ctx->txn_out->accounts.keys[ idx_in_txn ].uc, sizeof(fd_pubkey_t) )==0 ) {
105107
return fd_exec_instr_ctx_try_borrow_instr_account( ctx, i, account );
106108
}
107109
}
@@ -148,7 +150,7 @@ fd_exec_instr_ctx_any_signed( fd_exec_instr_ctx_t const * ctx,
148150
ushort idx_in_txn = ctx->instr->accounts[ j ].index_in_transaction;
149151
is_signer |=
150152
( ( !!fd_instr_acc_is_signer_idx( ctx->instr, j, NULL ) ) &
151-
( 0==memcmp( pubkey->key, ctx->txn_out->accounts.account_keys[ idx_in_txn ].key, sizeof(fd_pubkey_t) ) ) );
153+
( 0==memcmp( pubkey->key, ctx->txn_out->accounts.keys[ idx_in_txn ].key, sizeof(fd_pubkey_t) ) ) );
152154
}
153155
return is_signer;
154156
}

src/flamenco/runtime/fd_borrowed_account.c

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ int
44
fd_borrowed_account_get_data_mut( fd_borrowed_account_t * borrowed_acct,
55
uchar * * data_out,
66
ulong * dlen_out ) {
7-
fd_txn_account_t * acct = borrowed_acct->acct;
87

98
/* https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L824 */
109
int err;
@@ -14,17 +13,16 @@ fd_borrowed_account_get_data_mut( fd_borrowed_account_t * borrowed_acct,
1413
}
1514

1615
if ( data_out != NULL )
17-
*data_out = fd_txn_account_get_data_mut( acct );
16+
*data_out = fd_account_data( borrowed_acct->meta );
1817
if ( dlen_out != NULL )
19-
*dlen_out = fd_txn_account_get_data_len( acct );
18+
*dlen_out = borrowed_acct->meta->dlen;
2019

2120
return FD_EXECUTOR_INSTR_SUCCESS;
2221
}
2322

2423
int
2524
fd_borrowed_account_set_owner( fd_borrowed_account_t * borrowed_acct,
2625
fd_pubkey_t const * owner ) {
27-
fd_txn_account_t * acct = borrowed_acct->acct;
2826

2927
/* Only the owner can assign a new owner
3028
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L741 */
@@ -46,15 +44,15 @@ fd_borrowed_account_set_owner( fd_borrowed_account_t * borrowed_acct,
4644

4745
/* Don't copy the account if the owner does not change
4846
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L757 */
49-
if( !memcmp( fd_txn_account_get_owner( acct ), owner, sizeof( fd_pubkey_t ) ) ) {
47+
if( !memcmp( borrowed_acct->meta->owner, owner, sizeof(fd_pubkey_t) ) ) {
5048
return FD_EXECUTOR_INSTR_SUCCESS;
5149
}
5250

5351
/* Agave self.touch() is a no-op */
5452

5553
/* Copy into owner
5654
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L761 */
57-
fd_txn_account_set_owner( acct, owner );
55+
fd_memcpy( borrowed_acct->meta->owner, owner, sizeof(fd_pubkey_t) );
5856
return FD_EXECUTOR_INSTR_SUCCESS;
5957
}
6058

@@ -63,12 +61,11 @@ fd_borrowed_account_set_owner( fd_borrowed_account_t * borrowed_acct,
6361
int
6462
fd_borrowed_account_set_lamports( fd_borrowed_account_t * borrowed_acct,
6563
ulong lamports ) {
66-
fd_txn_account_t * acct = borrowed_acct->acct;
6764

6865
/* An account not owned by the program cannot have its blanace decrease
6966
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L775 */
7067
if( FD_UNLIKELY( (!fd_borrowed_account_is_owned_by_current_program( borrowed_acct )) &&
71-
(lamports<fd_txn_account_get_lamports( acct )) ) ) {
68+
(lamports<borrowed_acct->meta->lamports) ) ) {
7269
return FD_EXECUTOR_INSTR_ERR_EXTERNAL_ACCOUNT_LAMPORT_SPEND;
7370
}
7471

@@ -80,21 +77,20 @@ fd_borrowed_account_set_lamports( fd_borrowed_account_t * borrowed_acct,
8077

8178
/* Don't copy the account if the lamports do not change
8279
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L787 */
83-
if( fd_txn_account_get_lamports( acct )==lamports ) {
80+
if( borrowed_acct->meta->lamports==lamports ) {
8481
return FD_EXECUTOR_INSTR_SUCCESS;
8582
}
8683

8784
/* Agave self.touch() is a no-op */
8885

89-
fd_txn_account_set_lamports( acct, lamports );
86+
borrowed_acct->meta->lamports = lamports;
9087
return FD_EXECUTOR_INSTR_SUCCESS;
9188
}
9289

9390
int
9491
fd_borrowed_account_set_data_from_slice( fd_borrowed_account_t * borrowed_acct,
9592
uchar const * data,
9693
ulong data_sz ) {
97-
fd_txn_account_t * acct = borrowed_acct->acct;
9894

9995
/* https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L865 */
10096
int err;
@@ -115,16 +111,16 @@ fd_borrowed_account_set_data_from_slice( fd_borrowed_account_t * borrowed_acct,
115111
}
116112

117113
/* AccountSharedData::set_data_from_slice() */
118-
fd_txn_account_set_data( acct, data, data_sz );
114+
borrowed_acct->meta->dlen = (uint)data_sz;
115+
fd_memcpy( fd_account_data( borrowed_acct->meta ), data, data_sz );
119116

120117
return FD_EXECUTOR_INSTR_SUCCESS;
121118
}
122119

123120
int
124121
fd_borrowed_account_set_data_length( fd_borrowed_account_t * borrowed_acct,
125122
ulong new_len ) {
126-
fd_txn_account_t * acct = borrowed_acct->acct;
127-
int err = FD_EXECUTOR_INSTR_SUCCESS;
123+
int err = FD_EXECUTOR_INSTR_SUCCESS;
128124

129125
/* https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L883 */
130126
if( FD_UNLIKELY( !fd_borrowed_account_can_data_be_resized( borrowed_acct, new_len, &err ) ) ) {
@@ -136,7 +132,7 @@ fd_borrowed_account_set_data_length( fd_borrowed_account_t * borrowed_acct,
136132
return err;
137133
}
138134

139-
ulong old_len = fd_txn_account_get_data_len( acct );
135+
ulong old_len = borrowed_acct->meta->dlen;
140136

141137
/* Don't copy the account if the length does not change
142138
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L886 */
@@ -153,19 +149,18 @@ fd_borrowed_account_set_data_length( fd_borrowed_account_t * borrowed_acct,
153149

154150
/* Resize the account
155151
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L891 */
156-
fd_txn_account_resize( acct, new_len );
152+
fd_account_meta_resize( borrowed_acct->meta, new_len );
153+
157154
return FD_EXECUTOR_INSTR_SUCCESS;
158155
}
159156

160157
int
161158
fd_borrowed_account_set_executable( fd_borrowed_account_t * borrowed_acct,
162159
int is_executable ) {
163-
fd_txn_account_t * acct = borrowed_acct->acct;
164-
165160
/* To become executable an account must be rent exempt
166161
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1003-L1006 */
167162
fd_rent_t const * rent = fd_bank_rent_query( borrowed_acct->instr_ctx->bank );
168-
if( FD_UNLIKELY( fd_txn_account_get_lamports( acct )<fd_rent_exempt_minimum_balance( rent, fd_txn_account_get_data_len( acct ) ) ) ) {
163+
if( FD_UNLIKELY( borrowed_acct->meta->lamports<fd_rent_exempt_minimum_balance( rent, borrowed_acct->meta->dlen ) ) ) {
169164
return FD_EXECUTOR_INSTR_ERR_EXECUTABLE_ACCOUNT_NOT_RENT_EXEMPT;
170165
}
171166

@@ -190,7 +185,7 @@ fd_borrowed_account_set_executable( fd_borrowed_account_t * borrowed_acct,
190185
/* Agave self.touch() is a no-op */
191186

192187
/* https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1027 */
193-
fd_txn_account_set_executable( acct, is_executable );
188+
borrowed_acct->meta->executable = !!is_executable;
194189

195190
return FD_EXECUTOR_INSTR_SUCCESS;
196191
}
@@ -200,8 +195,7 @@ fd_borrowed_account_update_accounts_resize_delta( fd_borrowed_account_t * borrow
200195
ulong new_len,
201196
int * err ) {
202197
fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx;
203-
fd_txn_account_t * acct = borrowed_acct->acct;
204-
ulong size_delta = fd_ulong_sat_sub( new_len, fd_txn_account_get_data_len( acct ) );
198+
ulong size_delta = fd_ulong_sat_sub( new_len, borrowed_acct->meta->dlen );
205199

206200
/* TODO: The size delta should never exceed the value of ULONG_MAX so this
207201
could be replaced with a normal addition. However to match execution with
@@ -215,11 +209,10 @@ int
215209
fd_borrowed_account_can_data_be_resized( fd_borrowed_account_t const * borrowed_acct,
216210
ulong new_length,
217211
int * err ) {
218-
fd_txn_account_t * acct = borrowed_acct->acct;
219212

220213
/* Only the owner can change the length of the data
221214
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1095 */
222-
if( FD_UNLIKELY( (fd_txn_account_get_data_len( acct )!=new_length) &
215+
if( FD_UNLIKELY( (borrowed_acct->meta->dlen!=new_length) &
223216
(!fd_borrowed_account_is_owned_by_current_program( borrowed_acct )) ) ) {
224217
*err = FD_EXECUTOR_INSTR_ERR_ACC_DATA_SIZE_CHANGED;
225218
return 0;
@@ -234,7 +227,7 @@ fd_borrowed_account_can_data_be_resized( fd_borrowed_account_t const * borrowed_
234227

235228
/* The resize can not exceed the per-transaction maximum
236229
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1104-L1108 */
237-
ulong length_delta = fd_ulong_sat_sub( new_length, fd_txn_account_get_data_len( acct ) );
230+
ulong length_delta = fd_ulong_sat_sub( new_length, borrowed_acct->meta->dlen );
238231
ulong new_accounts_resize_delta = fd_ulong_sat_add( borrowed_acct->instr_ctx->txn_out->details.accounts_resize_delta, length_delta );
239232
if( FD_UNLIKELY( new_accounts_resize_delta > MAX_PERMITTED_ACCOUNT_DATA_ALLOCS_PER_TXN ) ) {
240233
*err = FD_EXECUTOR_INSTR_ERR_MAX_ACCS_DATA_ALLOCS_EXCEEDED;

0 commit comments

Comments
 (0)