Skip to content

Commit 40136b0

Browse files
asdf
1 parent 23d78a5 commit 40136b0

File tree

7 files changed

+67
-63
lines changed

7 files changed

+67
-63
lines changed

src/flamenco/rewards/fd_rewards.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1179,12 +1179,12 @@ fd_rewards_recalculate_partitioned_rewards( fd_exec_slot_ctx_t * slot_ctx,
11791179
fd_spad_t * * exec_spads,
11801180
ulong exec_spad_cnt,
11811181
fd_spad_t * runtime_spad ) {
1182-
fd_sysvar_epoch_rewards_t epoch_rewards[1];
1183-
if( FD_UNLIKELY( fd_sysvar_epoch_rewards_read( epoch_rewards,
1184-
slot_ctx->sysvar_cache,
1185-
slot_ctx->funk,
1186-
slot_ctx->funk_txn,
1187-
slot_ctx->runtime_wksp )==NULL ) ) {
1182+
fd_sysvar_epoch_rewards_t const * epoch_rewards = fd_sysvar_epoch_rewards_read( slot_ctx->sysvar_cache,
1183+
slot_ctx->funk,
1184+
slot_ctx->funk_txn,
1185+
runtime_spad,
1186+
slot_ctx->runtime_wksp );
1187+
if( FD_UNLIKELY( epoch_rewards==NULL ) ) {
11881188
FD_LOG_NOTICE(( "failed to read sysvar epoch rewards - the sysvar may not have been created yet" ));
11891189
set_epoch_reward_status_inactive( slot_ctx, runtime_spad );
11901190
return;

src/flamenco/runtime/sysvar/fd_sysvar.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ fd_sysvar_set( fd_exec_slot_ctx_t * slot_ctx,
2121
if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) )
2222
return FD_ACC_MGR_ERR_READ_FAILED;
2323

24-
fd_memcpy(rec->vt->get_data_mut( rec ), data, sz);
24+
fd_memcpy( rec->vt->get_data_mut( rec ), data, sz );
2525

2626
/* https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/runtime/src/bank.rs#L1825 */
2727
fd_acc_lamports_t lamports_before = rec->vt->get_lamports( rec );
28-
fd_epoch_bank_t * epoch_bank = fd_exec_epoch_ctx_epoch_bank( slot_ctx->epoch_ctx );
28+
fd_epoch_bank_t * epoch_bank = fd_exec_epoch_ctx_epoch_bank( slot_ctx->epoch_ctx );
2929
/* https://github.com/anza-xyz/agave/blob/ae18213c19ea5335dfc75e6b6116def0f0910aff/runtime/src/bank.rs#L6184
3030
The account passed in via the updater is always the current sysvar account, so we take the max of the
3131
current account lamports and the minimum rent exempt balance needed. */
3232
fd_acc_lamports_t lamports_after = fd_ulong_max( lamports_before, fd_rent_exempt_minimum_balance( &epoch_bank->rent, sz ) );
3333
rec->vt->set_lamports( rec, lamports_after );
3434

3535
/* https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/runtime/src/bank.rs#L1826 */
36-
if ( lamports_after > lamports_before ) {
37-
slot_ctx->slot_bank.capitalization += ( lamports_after - lamports_before );
36+
if( lamports_after > lamports_before ) {
37+
slot_ctx->slot_bank.capitalization += (lamports_after - lamports_before);
3838
} else if( lamports_after < lamports_before ) {
39-
slot_ctx->slot_bank.capitalization -= ( lamports_before - lamports_after );
39+
slot_ctx->slot_bank.capitalization -= (lamports_before - lamports_after);
4040
}
4141

4242
rec->vt->set_data_len( rec, sz );

src/flamenco/runtime/sysvar/fd_sysvar_clock.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void
3535
write_clock( fd_exec_slot_ctx_t * slot_ctx,
3636
fd_sol_sysvar_clock_t * clock ) {
3737
ulong sz = fd_sol_sysvar_clock_size( clock );
38-
uchar enc[sz];
38+
uchar enc[sz];
3939
memset( enc, 0, sz );
4040
fd_bincode_encode_ctx_t ctx;
4141
ctx.data = enc;
@@ -202,7 +202,6 @@ fd_calculate_stake_weighted_timestamp( fd_exec_slot_ctx_t * slot_ctx,
202202
slot_ctx->funk_txn,
203203
runtime_spad,
204204
slot_ctx->runtime_wksp );
205-
FD_LOG_WARNING(("CLOCK %lu %lu", clock->unix_timestamp, clock->epoch_start_timestamp));
206205
// get the unique timestamps
207206
/* stake per timestamp */
208207

src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.c

+46-39
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,34 @@
88

99
static void
1010
write_epoch_rewards( fd_exec_slot_ctx_t * slot_ctx, fd_sysvar_epoch_rewards_t * epoch_rewards ) {
11-
ulong sz = fd_sysvar_epoch_rewards_size( epoch_rewards );
12-
uchar enc[sz];
13-
fd_memset( enc, 0, sz );
14-
fd_bincode_encode_ctx_t ctx;
15-
ctx.data = enc;
16-
ctx.dataend = enc + sz;
17-
if ( fd_sysvar_epoch_rewards_encode( epoch_rewards, &ctx ) ) {
18-
FD_LOG_ERR(("fd_sysvar_epoch_rewards_encode failed"));
11+
/* It's fine to not do a variable length allocation here because
12+
the size of the sysvar struct is fixed. */
13+
uchar enc[ FD_SYSVAR_EPOCH_REWARDS_FOOTPRINT ] = {0};
14+
fd_bincode_encode_ctx_t ctx = {
15+
.data = enc,
16+
.dataend = enc + FD_SYSVAR_EPOCH_REWARDS_FOOTPRINT,
17+
};
18+
if( fd_sysvar_epoch_rewards_encode( epoch_rewards, &ctx ) ) {
19+
FD_LOG_ERR(( "fd_sysvar_epoch_rewards_encode failed" ));
1920
}
2021

21-
fd_sysvar_set( slot_ctx, &fd_sysvar_owner_id, &fd_sysvar_epoch_rewards_id, enc, sz, slot_ctx->slot_bank.slot );
22+
fd_sysvar_set( slot_ctx,
23+
&fd_sysvar_owner_id,
24+
&fd_sysvar_epoch_rewards_id,
25+
enc,
26+
FD_SYSVAR_EPOCH_REWARDS_FOOTPRINT,
27+
slot_ctx->slot_bank.slot );
2228
}
2329

24-
fd_sysvar_epoch_rewards_t *
25-
fd_sysvar_epoch_rewards_read( fd_sysvar_epoch_rewards_t * result,
26-
fd_sysvar_cache_t const * sysvar_cache,
27-
fd_funk_t * funk,
28-
fd_funk_txn_t * funk_txn,
29-
fd_wksp_t * wksp ) {
30+
fd_sysvar_epoch_rewards_t const *
31+
fd_sysvar_epoch_rewards_read( fd_sysvar_cache_t const * sysvar_cache,
32+
fd_funk_t * funk,
33+
fd_funk_txn_t * funk_txn,
34+
fd_spad_t * spad,
35+
fd_wksp_t * wksp ) {
3036
fd_sysvar_epoch_rewards_t const * ret = fd_sysvar_cache_epoch_rewards( sysvar_cache, wksp );
31-
if( FD_UNLIKELY( NULL != ret ) ) {
32-
fd_memcpy(result, ret, sizeof(fd_sysvar_epoch_rewards_t));
33-
return result;
34-
} else {
35-
FD_LOG_WARNING(("DAFUQ"));
37+
if( !!ret ) {
38+
return ret;
3639
}
3740

3841
FD_TXN_ACCOUNT_DECL( acc );
@@ -41,8 +44,6 @@ fd_sysvar_epoch_rewards_read( fd_sysvar_epoch_rewards_t * result,
4144
return NULL;
4245
}
4346

44-
FD_LOG_WARNING(("DATA LENGTH %lu", acc->vt->get_data_len( acc )));
45-
4647
fd_bincode_decode_ctx_t decode = {
4748
.data = acc->vt->get_data( acc ),
4849
.dataend = acc->vt->get_data( acc ) + acc->vt->get_data_len( acc )
@@ -55,9 +56,9 @@ fd_sysvar_epoch_rewards_read( fd_sysvar_epoch_rewards_t * result,
5556
return NULL;
5657
}
5758

58-
/* We assume here that the data structure is properly allocated already.
59-
This could potentially be unsafe if not handled correctly by the caller. */
60-
fd_sysvar_epoch_rewards_decode( result, &decode );
59+
uchar * epoch_rewards_mem = fd_spad_alloc( spad, fd_sysvar_epoch_rewards_align(), total_sz );
60+
61+
fd_sysvar_epoch_rewards_t const * result = fd_sysvar_epoch_rewards_decode( epoch_rewards_mem, &decode );
6162

6263
return result;
6364
}
@@ -69,17 +70,22 @@ void
6970
fd_sysvar_epoch_rewards_distribute( fd_exec_slot_ctx_t * slot_ctx,
7071
ulong distributed,
7172
fd_spad_t * runtime_spad ) {
72-
fd_sysvar_epoch_rewards_t epoch_rewards[1];
73-
if ( FD_UNLIKELY( fd_sysvar_epoch_rewards_read( epoch_rewards,
74-
slot_ctx->sysvar_cache,
75-
slot_ctx->funk,
76-
slot_ctx->funk_txn,
77-
slot_ctx->runtime_wksp ) == NULL ) ) {
73+
fd_sysvar_epoch_rewards_t * epoch_rewards = (fd_sysvar_epoch_rewards_t *)fd_sysvar_epoch_rewards_read( slot_ctx->sysvar_cache,
74+
slot_ctx->funk,
75+
slot_ctx->funk_txn,
76+
runtime_spad,
77+
slot_ctx->runtime_wksp );
78+
if( FD_UNLIKELY( epoch_rewards == NULL ) ) {
7879
FD_LOG_ERR(( "failed to read sysvar epoch rewards" ));
7980
}
80-
FD_TEST( epoch_rewards->active );
8181

82-
FD_TEST( fd_ulong_sat_add( epoch_rewards->distributed_rewards, distributed ) <= epoch_rewards->total_rewards );
82+
if( FD_UNLIKELY( !epoch_rewards->active ) ) {
83+
FD_LOG_ERR(( "epoch rewards are not active" ));
84+
}
85+
86+
if( FD_UNLIKELY( fd_ulong_sat_add( epoch_rewards->distributed_rewards, distributed ) > epoch_rewards->total_rewards ) ) {
87+
FD_LOG_ERR(( "distributed rewards overflow" ));
88+
}
8389

8490
epoch_rewards->distributed_rewards += distributed;
8591

@@ -96,12 +102,13 @@ fd_sysvar_epoch_rewards_distribute( fd_exec_slot_ctx_t * slot_ctx,
96102
void
97103
fd_sysvar_epoch_rewards_set_inactive( fd_exec_slot_ctx_t * slot_ctx,
98104
fd_spad_t * runtime_spad ) {
99-
fd_sysvar_epoch_rewards_t epoch_rewards[1];
100-
if ( FD_UNLIKELY( fd_sysvar_epoch_rewards_read( epoch_rewards,
101-
slot_ctx->sysvar_cache,
102-
slot_ctx->funk,
103-
slot_ctx->funk_txn,
104-
slot_ctx->runtime_wksp ) == NULL ) ) {
105+
fd_sysvar_epoch_rewards_t * epoch_rewards = (fd_sysvar_epoch_rewards_t *)fd_sysvar_epoch_rewards_read( slot_ctx->sysvar_cache,
106+
slot_ctx->funk,
107+
slot_ctx->funk_txn,
108+
runtime_spad,
109+
slot_ctx->runtime_wksp );
110+
111+
if ( FD_UNLIKELY( epoch_rewards == NULL ) ) {
105112
FD_LOG_ERR(( "failed to read sysvar epoch rewards" ));
106113
}
107114

src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
FD_PROTOTYPES_BEGIN
99

1010
/* Read the current value of the EpochRewards sysvar from Funk. */
11-
fd_sysvar_epoch_rewards_t *
12-
fd_sysvar_epoch_rewards_read( fd_sysvar_epoch_rewards_t * result,
13-
fd_sysvar_cache_t const * sysvar_cache,
11+
fd_sysvar_epoch_rewards_t const *
12+
fd_sysvar_epoch_rewards_read( fd_sysvar_cache_t const * sysvar_cache,
1413
fd_funk_t * funk,
1514
fd_funk_txn_t * funk_txn,
15+
fd_spad_t * spad,
1616
fd_wksp_t * wksp );
1717

1818
/* Update EpochRewards sysvar with distributed rewards

src/flamenco/runtime/sysvar/fd_sysvar_fees.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fd_sysvar_fees_read( fd_sysvar_fees_t * result,
2525
fd_funk_txn_t * funk_txn,
2626
fd_wksp_t * wksp ) {
2727
fd_sysvar_fees_t const * ret = fd_sysvar_cache_fees( sysvar_cache, wksp );
28-
if( FD_UNLIKELY( NULL != ret ) ) {
28+
if( !!ret ) {
2929
fd_memcpy(result, ret, sizeof(fd_sysvar_fees_t));
3030
return result;
3131
}

src/flamenco/vm/syscall/fd_vm_syscall_runtime.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -641,13 +641,11 @@ fd_vm_syscall_sol_get_epoch_rewards_sysvar( /**/ void * _vm,
641641

642642
void * out = FD_VM_MEM_HADDR_ST( vm, out_vaddr, FD_SYSVAR_EPOCH_REWARDS_ALIGN, FD_SYSVAR_EPOCH_REWARDS_FOOTPRINT );
643643

644-
fd_sysvar_epoch_rewards_t var[1];
645-
fd_sysvar_epoch_rewards_new ( var );
646-
fd_sysvar_epoch_rewards_read( var,
647-
instr_ctx->txn_ctx->sysvar_cache,
648-
instr_ctx->txn_ctx->funk,
649-
instr_ctx->txn_ctx->funk_txn,
650-
instr_ctx->txn_ctx->runtime_pub_wksp );
644+
fd_sysvar_epoch_rewards_t * var = (fd_sysvar_epoch_rewards_t *)fd_sysvar_epoch_rewards_read( instr_ctx->txn_ctx->sysvar_cache,
645+
instr_ctx->txn_ctx->funk,
646+
instr_ctx->txn_ctx->funk_txn,
647+
instr_ctx->txn_ctx->spad,
648+
instr_ctx->txn_ctx->runtime_pub_wksp );
651649

652650
memcpy( out, var, FD_SYSVAR_EPOCH_REWARDS_FOOTPRINT );
653651

0 commit comments

Comments
 (0)