Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_tests_fd_svm_mini_h
2 : #define HEADER_fd_src_flamenco_runtime_tests_fd_svm_mini_h
3 :
4 : /* fd_svm_mini.h is an API for creating Solana runtime test
5 : environments.
6 :
7 : Structurally, the API works as follows:
8 : - svm_mini provides an environment for block/transaction execution,
9 : including a fork-aware accounts DB, program cache, etc.
10 : - svm_mini_limits configures memory limits for the above
11 : - svm_mini_params fine-tunes default state to reduce setup
12 : boilerplate (e.g. set up vote/stake accounts, builtin programs)
13 : - forks are identified by the "bank index". some care is required to
14 : handle bank indexes, as they are reused after invalidation.
15 :
16 : This API optimizes for testing, not useful for production:
17 : - smaller runtime defaults (aims for 2 GiB memory reservations)
18 : - memory lazy paged / not pinned by default
19 : - memory 4K paged by default (simplify startup)
20 : - avoids use of privileged kernel calls */
21 :
22 : #include "../../progcache/fd_progcache_user.h"
23 : #include "../../log_collector/fd_log_collector_base.h"
24 : #include "../fd_runtime.h"
25 : #include "../fd_runtime_stack.h"
26 : #include "../fd_txncache.h"
27 : #include "../../vm/fd_vm.h"
28 :
29 : /* fd_svm_mini_t holds handles to all relevant Firedancer runtime
30 : components for test environments. */
31 :
32 : struct fd_svm_mini {
33 : fd_wksp_t * wksp;
34 : fd_banks_t * banks;
35 : fd_runtime_t * runtime;
36 : fd_runtime_stack_t * runtime_stack;
37 : fd_txncache_shmem_t * txncache_shmem;
38 : fd_txncache_t * txncache;
39 : fd_vm_t * vm;
40 :
41 : fd_progcache_t progcache[1];
42 : void * bpf_frames_shmem;
43 : fd_bpf_frames_t * bpf_frames;
44 : fd_log_collector_t log_collector[1];
45 : fd_features_t features[1];
46 : fd_sha256_t sha256[1]; /* FIXME this should not be separate */
47 :
48 : /* Saved accdb init params for reset */
49 : int accdb_fd;
50 : void * accdb_shmem_mem;
51 : void * accdb_join_mem;
52 : ulong accdb_max_accounts;
53 : ulong accdb_max_live_slots;
54 : ulong accdb_joiner_cnt;
55 : };
56 :
57 : typedef struct fd_svm_mini fd_svm_mini_t;
58 :
59 : /* fd_svm_mini_limits_t specifies memory allocation limits for runtime
60 : components. */
61 :
62 : struct fd_svm_mini_limits {
63 : /* fork management */
64 : ulong max_live_slots;
65 :
66 : /* consensus */
67 : ulong max_vote_accounts;
68 : ulong max_stake_accounts;
69 : ulong max_disk_records; /* records in each stake-delegation disk tier */
70 :
71 : /* accdb */
72 : ulong max_accounts;
73 : ulong max_account_space_bytes;
74 :
75 : /* Number of accdb joiners (writer joins) the shmem must support. 0
76 : means 1 (just the mini's own runtime join). Tests that join the
77 : accdb a second time (e.g. an exec tile sharing mini's accounts DB)
78 : must set this to at least 2. */
79 : ulong accdb_joiner_cnt;
80 :
81 : /* txn executor */
82 : ulong max_txn_write_locks;
83 : ulong max_txn_per_slot;
84 :
85 : /* wksp alloc tag (0 uses default) */
86 : ulong wksp_tag;
87 :
88 : /* additional wksp partitions / data size */
89 : ulong wksp_addl_part_cnt;
90 : ulong wksp_addl_sz;
91 : };
92 :
93 : typedef struct fd_svm_mini_limits fd_svm_mini_limits_t;
94 :
95 : /* fd_svm_mini_params_t specifies defaults for initialization of an
96 : svm_mini object. */
97 :
98 : struct fd_svm_mini_params {
99 : ulong hash_seed;
100 : ulong root_slot;
101 : ulong slots_per_epoch;
102 :
103 : ulong init_sysvars : 1;
104 : ulong init_feature_accounts : 1;
105 : ulong init_builtins : 1;
106 :
107 : /* If non-zero, creates mock_validator_cnt validators with uniform
108 : stake and populates the epoch leader schedule. For each validator,
109 : creates identity, vote, and stake accounts in the accounts DB. */
110 : ulong mock_validator_cnt;
111 :
112 : /* Vote stakes fields for every mock validator. A NULL
113 : mock_block_collector keeps the node account default, and
114 : mock_block_collector_self uses the validator's own vote account. */
115 : ushort mock_block_revenue_commission_bps;
116 : fd_pubkey_t const * mock_block_collector;
117 : int mock_block_collector_self;
118 :
119 : /* Sysvar overrides */
120 : fd_sol_sysvar_clock_t const * clock;
121 : fd_epoch_schedule_t const * epoch_schedule;
122 : fd_rent_t const * rent;
123 : };
124 :
125 : typedef struct fd_svm_mini_params fd_svm_mini_params_t;
126 :
127 : FD_PROTOTYPES_BEGIN
128 :
129 : /* fd_svm_test_{boot,halt} do all-in-one setup for test executables.
130 : An important goal is rootless operation on a default Linux config for
131 : easy development.
132 :
133 : fd_svm_test_boot does the following steps:
134 : - standard command-line handling
135 : - creates an anonymous wksp / attaches to an existing wksp
136 : - creates various runtime objects
137 :
138 : Parses and strips the following arguments from pargc/pargv, or
139 : chooses sane defaults in the absence of these options.
140 :
141 : --page-sz <size> memory page size ("normal", "huge", "gigantic")
142 : if unspecified, uses lazy anonymous normal pages
143 : if specified, implies pinned/mlock() pages
144 : --page-cnt <count> number of memory pages to reserve (default derived from limits)
145 : --wksp <name> use existing wksp instead of allocating one
146 : --near-cpu <number> NUMA affinity hint for memory allocations
147 : (default: let kernel decide on first use/mlock)
148 :
149 : Terminates the process with FD_LOG_ERR (exit code 1) if svm_mini
150 : fails to boot.
151 :
152 : fd_svm_test_halt destroys the mini object and halts fd. Wksp
153 : cleanup is left to process termination. */
154 :
155 : fd_svm_mini_t *
156 : fd_svm_test_boot( int * pargc,
157 : char *** pargv,
158 : fd_svm_mini_limits_t const * limits );
159 :
160 : void
161 : fd_svm_test_halt( fd_svm_mini_t * mini );
162 :
163 : /* fd_svm_mini_limits_default populates minimal single-fork execution
164 : limits. */
165 :
166 : FD_FN_UNUSED static fd_svm_mini_limits_t *
167 0 : fd_svm_mini_limits_default( fd_svm_mini_limits_t * limits ) {
168 0 : *limits = (fd_svm_mini_limits_t) {
169 0 : .max_live_slots = 16UL,
170 0 : .max_vote_accounts = 256UL,
171 0 : .max_stake_accounts = 256UL,
172 0 : .max_disk_records = 4096UL,
173 0 : .max_accounts = 128UL,
174 0 : .max_account_space_bytes = 32UL<<20,
175 0 : .max_txn_write_locks = 0UL,
176 0 : .max_txn_per_slot = 128UL
177 0 : };
178 0 : return limits;
179 0 : }
180 :
181 : /* fd_svm_mini_wksp_data_max returns the recommended heap space in bytes
182 : for a given limits config. */
183 :
184 : ulong
185 : fd_svm_mini_wksp_data_max( fd_svm_mini_limits_t const * limits );
186 :
187 : /* fd_svm_mini_create allocates and constructs various Solana runtime
188 : environment objects and packs them into an svm_mini handle. The
189 : newly created svm_mini object is reset using default params. On
190 : failure terminates the app with FD_LOG_ERR (exit code 1). */
191 :
192 : fd_svm_mini_t *
193 : fd_svm_mini_create( fd_wksp_t * wksp,
194 : fd_svm_mini_limits_t const * limits );
195 :
196 : /* fd_svm_mini_destroy destroys all Solana runtime environment objects,
197 : accounts, blocks, etc, and frees them back to the wksp heap. */
198 :
199 : void
200 : fd_svm_mini_destroy( fd_svm_mini_t * mini );
201 :
202 : /* fd_svm_mini_params_default populates default execution state. */
203 :
204 : FD_FN_UNUSED static fd_svm_mini_params_t *
205 0 : fd_svm_mini_params_default( fd_svm_mini_params_t * params ) {
206 0 : *params = (fd_svm_mini_params_t) {
207 0 : .hash_seed = 1UL,
208 0 : .root_slot = 1UL,
209 0 : .slots_per_epoch = 16UL,
210 0 : .init_sysvars = 1,
211 0 : .init_feature_accounts = 0,
212 0 : .init_builtins = 1,
213 0 : .mock_validator_cnt = 1UL,
214 0 : .mock_block_revenue_commission_bps = FD_VOTE_DEFAULT_BLOCK_REVENUE_COMMISSION_BPS,
215 0 : .mock_block_collector = NULL,
216 0 : .mock_block_collector_self = 0,
217 0 : .clock = NULL,
218 0 : .epoch_schedule = NULL,
219 0 : .rent = NULL,
220 0 : };
221 0 : return params;
222 0 : }
223 :
224 : /* fd_svm_mini_reset destroys all existing runtime state (banks, accdb,
225 : etc), and initializes them according to params. This operation
226 : invalidates any handle previously acquired through svm_mini. Returns
227 : the initial bank index (rooted), or ULONG_MAX if mock_validator_cnt
228 : exceeds the configured vote-account capacity. */
229 :
230 : ulong
231 : fd_svm_mini_reset( fd_svm_mini_t * mini,
232 : fd_svm_mini_params_t * params );
233 :
234 : /* Fork management API */
235 :
236 : /* fd_svm_mini_attach_child creates a fork node as a descendant of the
237 : node identified by parent_bank_idx. child_slot is the slot number of
238 : this node. Terminates the app with FD_LOG_ERR on failure. */
239 :
240 : ulong
241 : fd_svm_mini_attach_child( fd_svm_mini_t * mini,
242 : ulong parent_bank_idx,
243 : ulong child_slot );
244 :
245 : /* fd_svm_mini_freeze freezes the bank identified by bank_idx. Runs
246 : slot boundary logic (registers POH hash into blockhash queue, updates
247 : sysvars, settles fees, etc). */
248 :
249 : void
250 : fd_svm_mini_freeze( fd_svm_mini_t * mini,
251 : ulong bank_idx );
252 :
253 : /* fd_svm_mini_register_blockhash makes blockhash resolvable by the
254 : status cache when executing transactions on the bank identified by
255 : bank_idx (otherwise the fd_txncache_query/insert FD_TEST(blockcache)
256 : in the executor's pre-execute checks fails). It registers blockhash
257 : on bank_idx's PARENT txncache fork, because a fork can only query
258 : blockhashes registered on a fork it descends from (a fork's
259 : descends-set holds its ancestors, not itself). bank_idx must
260 : therefore have a parent, and that parent fork must not already be
261 : finalized (each txncache fork carries at most one blockhash). */
262 :
263 : void
264 : fd_svm_mini_register_blockhash( fd_svm_mini_t * mini,
265 : ulong bank_idx,
266 : fd_hash_t const * blockhash );
267 :
268 : /* fd_svm_mini_cancel_fork cancels the subtree of the fork graph
269 : identified by bank_idx (i.e. the bank_idx node and all its children,
270 : transitively). */
271 :
272 : void
273 : fd_svm_mini_cancel_fork( fd_svm_mini_t * mini,
274 : ulong bank_idx );
275 :
276 : /* fd_svm_mini_advance_root advances the fork graph root to the node
277 : identified by bank_idx. Cancels all siblings and uncles
278 : (transitively) of the rooted nodes. */
279 :
280 : void
281 : fd_svm_mini_advance_root( fd_svm_mini_t * mini,
282 : ulong bank_idx );
283 :
284 : fd_bank_t *
285 : fd_svm_mini_bank( fd_svm_mini_t * mini,
286 : ulong bank_idx );
287 :
288 : fd_accdb_fork_id_t
289 : fd_svm_mini_fork_id( fd_svm_mini_t * mini,
290 : ulong bank_idx );
291 :
292 : /* Mock/inject API */
293 :
294 : /* fd_svm_mini_put_account_rooted injects a copy of the account at ro
295 : into the rooted state. */
296 :
297 : void
298 : fd_svm_mini_put_account_rooted( fd_svm_mini_t * mini,
299 : fd_acc_t const * ro );
300 :
301 : /* fd_svm_mini_add_lamports_rooted increases the lamport balance of a
302 : rooted accounts. */
303 :
304 : void
305 : fd_svm_mini_add_lamports_rooted( fd_svm_mini_t * mini,
306 : fd_pubkey_t const * pubkey,
307 : ulong lamports );
308 :
309 : /* fd_svm_mini_add_lamports increases the lamport balance of an account. */
310 :
311 : void
312 : fd_svm_mini_add_lamports( fd_svm_mini_t * mini,
313 : fd_accdb_fork_id_t fork_id,
314 : fd_pubkey_t const * pubkey,
315 : ulong lamports );
316 :
317 : FD_PROTOTYPES_END
318 :
319 : #endif /* HEADER_fd_src_flamenco_runtime_tests_fd_svm_mini_h */
|