8
8
9
9
static void
10
10
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" ));
19
20
}
20
21
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 );
22
28
}
23
29
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 ) {
30
36
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 ;
36
39
}
37
40
38
41
FD_TXN_ACCOUNT_DECL ( acc );
@@ -41,8 +44,6 @@ fd_sysvar_epoch_rewards_read( fd_sysvar_epoch_rewards_t * result,
41
44
return NULL ;
42
45
}
43
46
44
- FD_LOG_WARNING (("DATA LENGTH %lu" , acc -> vt -> get_data_len ( acc )));
45
-
46
47
fd_bincode_decode_ctx_t decode = {
47
48
.data = acc -> vt -> get_data ( acc ),
48
49
.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,
55
56
return NULL ;
56
57
}
57
58
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 );
61
62
62
63
return result ;
63
64
}
69
70
fd_sysvar_epoch_rewards_distribute ( fd_exec_slot_ctx_t * slot_ctx ,
70
71
ulong distributed ,
71
72
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 ) ) {
78
79
FD_LOG_ERR (( "failed to read sysvar epoch rewards" ));
79
80
}
80
- FD_TEST ( epoch_rewards -> active );
81
81
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
+ }
83
89
84
90
epoch_rewards -> distributed_rewards += distributed ;
85
91
@@ -96,12 +102,13 @@ fd_sysvar_epoch_rewards_distribute( fd_exec_slot_ctx_t * slot_ctx,
96
102
void
97
103
fd_sysvar_epoch_rewards_set_inactive ( fd_exec_slot_ctx_t * slot_ctx ,
98
104
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 ) ) {
105
112
FD_LOG_ERR (( "failed to read sysvar epoch rewards" ));
106
113
}
107
114
0 commit comments