Line data Source code
1 : #include "fd_sysvar_cache.h"
2 : #include "fd_sysvar_cache_private.h"
3 : #include "fd_sysvar_recent_hashes.h"
4 : #include "fd_sysvar_slot_hashes.h"
5 : #include "fd_sysvar_stake_history.h"
6 :
7 : #include <errno.h>
8 :
9 : void *
10 64481 : fd_sysvar_cache_new( void * mem ) {
11 :
12 64481 : if( FD_UNLIKELY( !mem ) ) {
13 0 : FD_LOG_WARNING(( "NULL mem" ));
14 0 : return NULL;
15 0 : }
16 64481 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, alignof(fd_sysvar_cache_t) ) ) ) {
17 0 : FD_LOG_WARNING(( "misaligned mem" ));
18 0 : return NULL;
19 0 : }
20 :
21 64481 : fd_sysvar_cache_t * sysvar_cache = mem;
22 64481 : sysvar_cache->magic = 0UL;
23 64481 : memset( sysvar_cache->desc, 0, FD_SYSVAR_CACHE_ENTRY_CNT*sizeof(fd_sysvar_desc_t) );
24 :
25 64481 : FD_COMPILER_MFENCE();
26 64481 : sysvar_cache->magic = FD_SYSVAR_CACHE_MAGIC;
27 64481 : FD_COMPILER_MFENCE();
28 :
29 64481 : return sysvar_cache;
30 64481 : }
31 :
32 : fd_sysvar_cache_t *
33 64463 : fd_sysvar_cache_join( void * mem ) {
34 : /* FIXME This is a good place to ref-count writable joins */
35 64463 : return (fd_sysvar_cache_t *)fd_sysvar_cache_join_const( mem );
36 64463 : }
37 :
38 : fd_sysvar_cache_t const *
39 64469 : fd_sysvar_cache_join_const( void const * mem ) {
40 :
41 64469 : if( FD_UNLIKELY( !mem ) ) {
42 0 : FD_LOG_WARNING(( "NULL mem" ));
43 0 : return NULL;
44 0 : }
45 64469 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, alignof(fd_sysvar_cache_t) ) ) ) {
46 0 : FD_LOG_WARNING(( "misaligned mem" ));
47 0 : return NULL;
48 0 : }
49 64469 : fd_sysvar_cache_t const * sysvar_cache = mem;
50 64469 : if( FD_UNLIKELY( sysvar_cache->magic != FD_SYSVAR_CACHE_MAGIC ) ) {
51 0 : FD_LOG_WARNING(( "bad magic" ));
52 0 : return NULL;
53 0 : }
54 :
55 64469 : return sysvar_cache;
56 64469 : }
57 :
58 : void *
59 8736 : fd_sysvar_cache_leave( fd_sysvar_cache_t * sysvar_cache ) {
60 8736 : return sysvar_cache;
61 8736 : }
62 :
63 : void const *
64 0 : fd_sysvar_cache_leave_const( fd_sysvar_cache_t const * sysvar_cache ) {
65 0 : return sysvar_cache;
66 0 : }
67 :
68 : void *
69 0 : fd_sysvar_cache_delete( void * mem ) {
70 :
71 0 : if( FD_UNLIKELY( !mem ) ) {
72 0 : FD_LOG_WARNING(( "NULL mem" ));
73 0 : return NULL;
74 0 : }
75 0 : fd_sysvar_cache_t * sysvar_cache = mem;
76 0 : if( FD_UNLIKELY( sysvar_cache->magic != FD_SYSVAR_CACHE_MAGIC ) ) {
77 0 : FD_LOG_WARNING(( "bad magic" ));
78 0 : return NULL;
79 0 : }
80 :
81 0 : memset( sysvar_cache, 0, sizeof(fd_sysvar_cache_t) );
82 :
83 0 : return mem;
84 0 : }
85 :
86 : uchar const *
87 : fd_sysvar_cache_data_query(
88 : fd_sysvar_cache_t const * sysvar_cache,
89 : void const * address, /* 32 bytes */
90 : ulong * psz
91 73 : ) {
92 73 : *psz = 0UL;
93 73 : fd_pubkey_t const pubkey = FD_LOAD( fd_pubkey_t, address );
94 73 : sysvar_tbl_t const * entry = sysvar_map_query( &pubkey, NULL );
95 73 : if( FD_UNLIKELY( !entry ) ) return NULL; /* address is not a sysvar */
96 73 : fd_sysvar_desc_t const * desc = &sysvar_cache->desc[ entry->desc_idx ];
97 73 : fd_sysvar_pos_t const * pos = &fd_sysvar_pos_tbl [ entry->desc_idx ];
98 73 : if( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) return NULL; /* sysvar data invalid */
99 70 : *psz = desc->data_sz;
100 70 : return (uchar const *)sysvar_cache + pos->data_off;
101 73 : }
102 :
103 : /* Generate accessors for sysvars that are backed by POD structs. */
104 :
105 : #define SIMPLE_SYSVAR_READ( name, name2, typet ) \
106 : typet * \
107 : fd_sysvar_cache_##name##_read( fd_sysvar_cache_t const * cache, \
108 191437 : typet * out ) { \
109 191437 : ulong const idx = FD_SYSVAR_##name##_IDX; \
110 191437 : fd_sysvar_desc_t const * desc = &cache->desc[ idx ]; \
111 191437 : fd_sysvar_pos_t const * pos = &fd_sysvar_pos_tbl[ idx ]; \
112 191437 : if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return NULL; \
113 191437 : memcpy( out, (uchar *)cache+pos->data_off, pos->data_max ); \
114 191429 : return out; \
115 191437 : }
116 :
117 : #define SIMPLE_SYSVAR( name, name2, type ) \
118 : SIMPLE_SYSVAR_READ( name, name2, fd_##type##_t )
119 : FD_SYSVAR_SIMPLE_ITER( SIMPLE_SYSVAR )
120 : #undef SIMPLE_SYSVAR
121 : #undef SIMPLE_SYSVAR_READ
122 :
123 : ulong const *
124 86 : fd_sysvar_cache_last_restart_slot_read( fd_sysvar_cache_t const * cache ) {
125 86 : ulong const idx = FD_SYSVAR_last_restart_slot_IDX;
126 86 : fd_sysvar_desc_t const * desc = &cache->desc[ idx ];
127 86 : fd_sysvar_pos_t const * pos = &fd_sysvar_pos_tbl[ idx ];
128 86 : if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return NULL;
129 65 : return fd_type_pun_const( (uchar const *)cache + pos->data_off );
130 86 : }
131 :
132 : int
133 56799 : fd_sysvar_cache_recent_hashes_is_empty( fd_sysvar_cache_t const * sysvar_cache ) {
134 56799 : fd_sysvar_desc_t const * desc = &sysvar_cache->desc[ FD_SYSVAR_recent_hashes_IDX ];
135 56799 : if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return 1;
136 56799 : FD_TEST( desc->data_sz >= sizeof(ulong) );
137 56799 : ulong len = FD_LOAD( ulong, sysvar_cache->bin_recent_hashes );
138 56799 : return len == 0UL;
139 56799 : }
140 :
141 : int
142 : fd_sysvar_obj_restore( fd_sysvar_cache_t * cache,
143 : fd_sysvar_desc_t * desc,
144 325205 : fd_sysvar_pos_t const * pos ) {
145 325205 : desc->flags &= ~FD_SYSVAR_FLAG_VALID;
146 :
147 325205 : uchar const * data = (uchar const *)cache + pos->data_off;
148 325205 : ulong const data_sz = desc->data_sz;
149 :
150 325205 : if( FD_UNLIKELY( !pos->validate( data, data_sz ) ) ) {
151 133 : FD_LOG_DEBUG(( "Failed to validate sysvar %s with data_sz=%lu",
152 133 : pos->name, data_sz ));
153 133 : return EINVAL;
154 133 : }
155 325072 : desc->flags |= FD_SYSVAR_FLAG_VALID;
156 325072 : return 0;
157 325205 : }
158 :
159 : static int
160 65031 : fd_sysvar_validate_clock( uchar const * data, ulong data_sz ) {
161 65031 : (void)data;
162 65031 : return data_sz >= sizeof(fd_sol_sysvar_clock_t );
163 65031 : }
164 :
165 : static int
166 64993 : fd_sysvar_validate_rent( uchar const * data, ulong data_sz ) {
167 64993 : (void)data;
168 64993 : return data_sz >= sizeof(fd_rent_t );
169 64993 : }
170 :
171 : static int
172 26869 : fd_sysvar_validate_last_restart_slot( uchar const * data, ulong data_sz ) {
173 26869 : (void)data;
174 26869 : return data_sz >= sizeof(ulong);
175 26869 : }
176 :
177 : static int
178 7780 : fd_sysvar_validate_epoch_rewards( uchar const * data, ulong data_sz ) {
179 7780 : if( FD_UNLIKELY( data_sz < sizeof(fd_sysvar_epoch_rewards_t) ) ) return 0;
180 7695 : fd_sysvar_epoch_rewards_t ew = FD_LOAD( fd_sysvar_epoch_rewards_t, data );
181 7695 : uchar active = ew.active;
182 7695 : if( FD_UNLIKELY( active!=0 && active!=1 ) ) return 0;
183 7692 : return 1;
184 7695 : }
185 :
186 : static int
187 64985 : fd_sysvar_validate_epoch_schedule( uchar const * data, ulong data_sz ) {
188 64985 : if( FD_UNLIKELY( data_sz < sizeof(fd_epoch_schedule_t) ) ) return 0;
189 64985 : fd_epoch_schedule_t es = FD_LOAD( fd_epoch_schedule_t, data );
190 64985 : uchar warmup = es.warmup;
191 64985 : if( FD_UNLIKELY( warmup!=0 && warmup!=1 ) ) return 0;
192 64985 : return 1;
193 64985 : }
194 :
195 : fd_sysvar_pos_t const fd_sysvar_pos_tbl[ FD_SYSVAR_CACHE_ENTRY_CNT ] = {
196 : [FD_SYSVAR_clock_IDX] =
197 : { .name="clock",
198 : .data_off=offsetof(fd_sysvar_cache_t, bin_clock ), .data_max=FD_SYSVAR_CLOCK_BINCODE_SZ,
199 : .validate=fd_sysvar_validate_clock },
200 : [FD_SYSVAR_epoch_rewards_IDX] =
201 : { .name="epoch rewards",
202 : .data_off=offsetof(fd_sysvar_cache_t, bin_epoch_rewards ), .data_max=FD_SYSVAR_EPOCH_REWARDS_BINCODE_SZ,
203 : .validate=fd_sysvar_validate_epoch_rewards },
204 : [FD_SYSVAR_epoch_schedule_IDX] =
205 : { .name="epoch schedule",
206 : .data_off=offsetof(fd_sysvar_cache_t, bin_epoch_schedule ), .data_max=FD_SYSVAR_EPOCH_SCHEDULE_BINCODE_SZ,
207 : .validate=fd_sysvar_validate_epoch_schedule },
208 : [FD_SYSVAR_last_restart_slot_IDX] =
209 : { .name="last restart slot",
210 : .data_off=offsetof(fd_sysvar_cache_t, bin_last_restart_slot), .data_max=FD_SYSVAR_LAST_RESTART_SLOT_BINCODE_SZ,
211 : .validate=fd_sysvar_validate_last_restart_slot },
212 : [FD_SYSVAR_recent_hashes_IDX] =
213 : { .name="recent blockhashes",
214 : .data_off=offsetof(fd_sysvar_cache_t, bin_recent_hashes ), .data_max=FD_SYSVAR_RECENT_HASHES_BINCODE_SZ,
215 : .validate=fd_sysvar_recent_hashes_validate },
216 : [FD_SYSVAR_rent_IDX] =
217 : { .name="rent",
218 : .data_off=offsetof(fd_sysvar_cache_t, bin_rent ), .data_max=FD_SYSVAR_RENT_BINCODE_SZ,
219 : .validate=fd_sysvar_validate_rent },
220 : [FD_SYSVAR_slot_hashes_IDX] =
221 : { .name="slot hashes",
222 : .data_off=offsetof(fd_sysvar_cache_t, bin_slot_hashes ), .data_max=FD_SYSVAR_SLOT_HASHES_BINCODE_SZ,
223 : .validate=fd_sysvar_slot_hashes_validate },
224 : [FD_SYSVAR_stake_history_IDX] =
225 : { .name="stake history",
226 : .data_off=offsetof(fd_sysvar_cache_t, bin_stake_history ), .data_max=FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ,
227 : .validate=fd_sysvar_stake_history_validate },
228 : };
229 :
230 : fd_slot_hashes_t *
231 : fd_sysvar_cache_slot_hashes_view( fd_sysvar_cache_t const * cache,
232 9087 : fd_slot_hashes_t * view ) {
233 9087 : fd_sysvar_desc_t const * desc = &cache->desc[ FD_SYSVAR_slot_hashes_IDX ];
234 9087 : if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return NULL;
235 9087 : return fd_sysvar_slot_hashes_view( view, cache->bin_slot_hashes, desc->data_sz );
236 9087 : }
237 :
238 : fd_stake_history_t *
239 : fd_sysvar_cache_stake_history_view( fd_sysvar_cache_t const * cache,
240 0 : fd_stake_history_t * view ) {
241 0 : fd_sysvar_desc_t const * desc = &cache->desc[ FD_SYSVAR_stake_history_IDX ];
242 0 : if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return NULL;
243 0 : return fd_sysvar_stake_history_view( view, cache->bin_stake_history, desc->data_sz );
244 0 : }
|