Line data Source code
1 : #ifndef HEADER_fd_src_ballet_txn_fd_txn_h
2 : #define HEADER_fd_src_ballet_txn_fd_txn_h
3 :
4 : /* The main structure this header defines is fd_txn_t, which represents a
5 : Solana transaction. A transaction, like a SQL database transaction, is the
6 : unit of execution atomicity in Solana, i.e. intermediate state is never
7 : visible to other transactions, and a failure at any point in the transaction
8 : causes the entire transaction to be rolled back (other than charging the
9 : transaction fee).
10 :
11 : A transaction primarily consists of a list of instructions to execute in
12 : sequence. The struct fd_txn_instr_t describes one instruction. An instruction
13 : specifies the invocation of a smart contract with some specified data and
14 : accounts. The name 'instruction' was a poor choice, (since on-chain code is
15 : composed of eBPF instructions and using the same word to refer to very
16 : different concepts is confusing) but it's too late to change. Thinking of a
17 : transaction-level instruction as a 'command' might be more useful.
18 :
19 : The other major component of a transaction is a list of account addresses.
20 : The address of any account that is referenced by any instruction in the
21 : transaction must appear in the list. The address of any signer (including
22 : the fee payer) must appear in the list. An account address is sometimes
23 : called a pubkey since it has the same format as one, though it is not always
24 : a public key strictly speaking (i.e. a corresponding private key may not
25 : exist). Each account address in the list has associated permissions flags:
26 : signer/not signer and writable/readonly. All 4 combinations are possible.
27 : These flags declare the transaction's intention in accessing the account,
28 : similar to the `mode` field of fopen( ). */
29 :
30 : #include "../fd_ballet_base.h"
31 :
32 : #include "../ed25519/fd_ed25519.h"
33 :
34 : /* FD_TXN_VLEGACY: the initial, pre-V0 transaction format. */
35 79029 : #define FD_TXN_VLEGACY ((uchar)0xFF)
36 : /* FD_TXN_V0: The second transaction format. Includes a version number and
37 : potentially some address lookup tables */
38 338653 : #define FD_TXN_V0 ((uchar)0x00)
39 : /* FD_TXN_V1: The third transaction format ("Transaction V1").
40 : See https://github.com/solana-foundation/solana-improvement-documents/pull/385
41 :
42 : This format removes support for address lookup tables and is the
43 : only transaction format that supports an MTU greater than 1232
44 : bytes. */
45 26080 : #define FD_TXN_V1 ((uchar)0x01)
46 :
47 : /* FD_TXN_SIGNATURE_SZ: The size (in bytes) of an Ed25519 signature. */
48 10718 : #define FD_TXN_SIGNATURE_SZ (64UL)
49 : /* FD_TXN_PUBKEY_SZ: The size (in bytes) of an Ed25519 public key. */
50 : #define FD_TXN_PUBKEY_SZ (32UL)
51 : /* FD_TXN_ACCT_ADDR_SZ: The size (in bytes) of a Solana account address.
52 : Account addresses are sometimes Ed25519 public keys, but they can also be
53 : the output of a SHA256 hash (program derived addresses and seeded accounts),
54 : or just hardcoded values (sysvars accounts). It's important that all types
55 : of account addresses have this same size. */
56 16393 : #define FD_TXN_ACCT_ADDR_SZ (32UL)
57 : /* FD_TXN_BLOCKHASH_SZ: The size (in bytes) of a blockhash. A blockhash is a
58 : SHA256 hash, giving a size of 256 bits = 32 bytes. */
59 10718 : #define FD_TXN_BLOCKHASH_SZ (32UL)
60 :
61 :
62 : /* FD_TXN_SIG_MAX: The (inclusive) maximum number of signatures a transaction
63 : can have.
64 :
65 : For V0 transactions, which have an MTU of 1232 B, the maximum signatures
66 : that can fit in the MTU is 12.
67 :
68 : For V1 transactions, the spec explicitly limits the maximum number of
69 : signatures to 12. */
70 0 : #define FD_TXN_SIG_MAX (12UL)
71 :
72 : /* FD_TXN_ACCT_ADDR_MAX: The (inclusive) maximum number of account addresses
73 : that a transaction can have.
74 :
75 : The account lock limit currently limits this to 64. */
76 5033 : #define FD_TXN_ACCT_ADDR_MAX (64UL)
77 :
78 : /* FD_TXN_ADDR_TABLE_LOOKUP_MAX: The (inclusive) maximum number of address
79 : tables that this transaction references. This is a conservative bound:
80 : the 1232-byte V0 MTU and the requirement that each lookup reference at
81 : least one account restrict the parser to at most 31 lookups. */
82 : #define FD_TXN_ADDR_TABLE_LOOKUP_MAX (127UL)
83 :
84 : /* FD_TXN_INSTR_MAX: The (inclusive) maximum number of instructions a transaction
85 : can have. */
86 0 : #define FD_TXN_INSTR_MAX (64UL)
87 :
88 : /* FD_TXN_INSTR_ACCT_MAX: The (inclusive) maximum number of accounts a
89 : single instruction can reference.
90 : https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/transaction-context/src/lib.rs#L17 */
91 0 : #define FD_TXN_INSTR_ACCT_MAX (255UL)
92 :
93 :
94 : /* FD_TXN_MAX_SZ: The maximum amount of memory (in bytes) that a fd_txn can
95 : take up, including the instruction array and any address tables. The
96 : worst-case transaction is a V0 transaction with only two account
97 : addresses (a program and a fee payer), and tons of empty instructions (no
98 : accounts, no data) and as many address table lookups loading a single
99 : account as possible.
100 :
101 : Worst-case V0 transaction: only 2 accounts, 64 empty instructions
102 : (no accounts, no data), 24 address lookup table lookups loading
103 : a single account:
104 : sizeof(fd_txn_t) = 22
105 : 64 × sizeof(fd_txn_instr_t) = 640 +
106 : 24 × sizeof(fd_txn_acct_addr_lut_t) = 192 +
107 : = 854
108 :
109 : Worst-case V1 transaction: 64 empty instructions, no ALTs as
110 : ALTs are not allowed in V1 transactions:
111 : sizeof(fd_txn_t) = 22
112 : 64 × sizeof(fd_txn_instr_t) = 640 +
113 : = 662
114 :
115 : So the worst-case parsed transaction size is the V0 transaction
116 : case. */
117 0 : #define FD_TXN_MAX_SZ (854UL)
118 :
119 :
120 : /* FD_TXN_MTU: The maximum size (in bytes, inclusive) of a serialized
121 : transaction, across all transaction version formats. */
122 246 : #define FD_TXN_MTU (4096UL)
123 :
124 : /* FD_TXN_MTU_V0: The maximum size (in bytes, inclusive) of a serialized
125 : legacy or V0 transaction.
126 :
127 : Any transaction that has an MTU larger than this is required to
128 : use the transaction V1 format. */
129 30355 : #define FD_TXN_MTU_V0 (1232UL)
130 :
131 : /* FD_TXN_MIN_SERIALIZED_SZ: The minimum size (in bytes) of a serialized
132 : transaction, using fd_txn_parse() verification rules.
133 :
134 : Minimum legacy transaction size: 134
135 : Minimum v0 transaction size: 136
136 : Minimum v1 transaction size: 138 */
137 9114 : #define FD_TXN_MIN_SERIALIZED_SZ (134UL)
138 :
139 : /* BEGIN Agave limits */
140 :
141 : /* Maximum number of accounts that a transaction may lock. */
142 3788016 : #define MAX_TX_ACCOUNT_LOCKS (64UL)
143 :
144 : /* In the FD runtime, we've sized things assuming up to
145 : MAX_TX_ACCOUNT_LOCKS accounts per transaction. We rely on the txn
146 : parser to enforce this limit, up till the point of
147 : validate_account_locks(). If the txn parser bumps the account limit,
148 : then we might overflow in the runtime. */
149 : FD_STATIC_ASSERT( MAX_TX_ACCOUNT_LOCKS==FD_TXN_ACCT_ADDR_MAX, num_accounts_per_txn );
150 :
151 : /* END Agave limits */
152 :
153 :
154 : /* A Solana transaction instruction, i.e. one command or step to execute in a
155 : transaction.
156 :
157 : An instruction tells the runtime to execute one on-chain program (smart
158 : contract) with some arguments (think argc, argv). The arguments come in the
159 : form of binary data and/or accounts, each of which is variable-sized and
160 : optional.
161 :
162 : Note that instructions specify accounts by giving an index into the
163 : transaction-level list of account addresses. This means there are
164 : essentially two layers of indirection: a 1 B index to a 32 B address which
165 : specifies an account. */
166 : struct fd_txn_instr {
167 : /* program_id: The on-chain program that this instruction invokes,
168 : represented as the index of the program's account address in the
169 : containing transaction's list of account addresses. */
170 : uchar program_id;
171 : uchar _padding_reserved_1; /* explicitly declare what the compiler would
172 : insert anyways */
173 :
174 : /* acct_cnt: The number of accounts this instruction references.
175 : N.B. It is possible to pass > 256 accounts to an instruction, but not more
176 : than 256 unique accounts. */
177 : ushort acct_cnt;
178 :
179 : /* data_sz: The size (in bytes) of the data passed to this instruction. The
180 : data itself is included in the transaction, so is limited to the overall
181 : transaction size. */
182 : ushort data_sz;
183 :
184 : /* acct_off: The offset (relative to the start of the transaction) in bytes
185 : where the account address index array starts. This array has size
186 : acct_cnt.
187 :
188 : Specifically, if uchar const * payload is a pointer to the first byte of
189 : the transaction data in the packet, then the array (payload+acct_off)[i]
190 : for i in [0, acct_cnt) gives all of the accounts passed to this
191 : instruction. As with the program_id, these accounts are represented as
192 : indices into the transaction's list of account addresses.
193 : */
194 : ushort acct_off;
195 :
196 : /* data_off: The offset (relative to the start of the transaction) in bytes
197 : where the instruction data array starts. This array has size data_sz.
198 :
199 : Specifically, if uchar const * payload is a pointer to the first byte of
200 : the transaction data in the packet, then the array (payload+data_off)[i]
201 : for i in [0, data_sz) gives the binary data passed to this instruction. */
202 : ushort data_off;
203 : };
204 :
205 : typedef struct fd_txn_instr fd_txn_instr_t;
206 :
207 :
208 : /* fd_txn_t: A Solana transaction. As explained above, a transaction is mostly
209 : a list of instructions, but there are a few other major components:
210 : - a list of account addresses,
211 : - the hash of a recent block (used as a nonce and TTL), and
212 : - potentially (if it's a V2 transaction) some address lookup tables. */
213 : struct fd_txn {
214 : /* transaction_version: The version number of this transaction. Currently
215 : must be one of { FD_TXN_VLEGACY, FD_TXN_V0, FD_TXN_V1 }. */
216 : uchar transaction_version;
217 :
218 : /* signature_cnt: The number of signatures in this transaction. signature_cnt
219 : in [1, FD_TXN_SIG_MAX]. */
220 : uchar signature_cnt;
221 :
222 : /* signature_off: The offset (relative to the start of the transaction) in
223 : bytes where the signatures start.
224 :
225 : Specifically, if uchar const * payload is a pointer to the first byte of
226 : the transaction data in the packet, then signature i starts at
227 : (payload+signature_off)[ FD_TXN_SIGNATURE_SZ*i ] for i in
228 : [0, signature_cnt). */
229 :
230 : ushort signature_off;
231 :
232 : /* message_off: The offset (relative to the start of the transaction)
233 : in bytes where the 'message' starts.
234 :
235 : The message is the part of the transaction covered by the
236 : signatures.
237 :
238 : For legacy/V0 transactions, the signatures are at the front of the
239 : packet and the message is at the end, so the message spans from
240 : message_off to the end of the packet.
241 :
242 : For V1 transactions, the message is at the front of the packet and
243 : the signatures are at the end, so the message spans from
244 : message_off to signature_off.
245 :
246 : Use fd_txn_msg_sz( txn, payload_sz ) to determine the length of
247 : the message. */
248 : ushort message_off;
249 :
250 : /* readonly_signed_cnt: Of the signature_cnt signatures, readonly_signed_cnt
251 : of them are read only. Since there must be a fee payer,
252 : readonly_signed_cnt in [0, signature_cnt) */
253 : uchar readonly_signed_cnt;
254 :
255 : /* readonly_unsigned_cnt: Of the account addresses that don't have an
256 : accompanying signature, readonly_unsigned_cnt of them are read only.
257 : readonly_unsigned_cnt in [0, acct_addr_cnt-signature_cnt]. Excludes any
258 : accounts from address table lookups. */
259 : uchar readonly_unsigned_cnt;
260 :
261 : /* acct_addr_cnt: The number of account addresses in this transaction.
262 : acct_addr_cnt in [1, FD_TXN_ACCT_ADDR_MAX]. Excludes any accounts from
263 : address table lookups. */
264 : ushort acct_addr_cnt;
265 :
266 : /* acct_addr_off: The offset (relative to the start of the transaction) in
267 : bytes where the account addresses start.
268 :
269 : Specifically, if uchar const * payload is a pointer to the first byte of
270 : the transaction data in the packet, then the array
271 : (payload+acct_addr_off)[ FD_TXN_ACCT_ADDR_SZ*i ] for i in [0, account_cnt)
272 : gives all of the account addresses in this transaction. Since
273 : (payload+acct_addr_off) points inside the packet, it should be treated as
274 : pointing to unaligned data.
275 :
276 : The order of these addresses is important, because it determines the
277 : "permission flags" for the account in this transaction.
278 : Accounts ordered:
279 : Index Range | Signer? | Writeable?
280 : ---------------------------------------------------------------------------------|--------------|-------------
281 : [0, signature_cnt - readonly_signed_cnt) | signer | writable
282 : [signature_cnt - readonly_signed_cnt, signature_cnt) | signer | readonly
283 : [signature_cnt, acct_addr_cnt - readonly_unsigned_cnt) | not signer | writable
284 : [acct_addr_cnt - readonly_unsigned_cnt, acct_addr_cnt) | not signer | readonly
285 : */
286 : ushort acct_addr_off;
287 :
288 : /* recent_blockhash_off: The offset (relative to the start of the
289 : transaction) in bytes where the recent blockhash starts.
290 :
291 : Specifically, if uchar const * payload is a pointer to the first byte of
292 : the transaction data in the packet, then (payload+recent_blockhash_off) is
293 : a pointer to the blockhash. Since the resulting pointer points inside the
294 : packet, it should be treated as pointing to unaligned data. For legacy
295 : and V0 transactions, recent_blockhash_off is 5 or 6 (mod 32); for V1,
296 : it is 8. */
297 : ushort recent_blockhash_off;
298 :
299 : /* addr_table_lookup_cnt: The number of address lookup tables this
300 : transaction contains. Must be 0 if transaction_version==FD_TXN_VLEGACY.
301 : addr_table_lookup_cnt in [0, FD_TXN_ADDR_TABLE_LOOKUP_MAX]. */
302 : uchar addr_table_lookup_cnt;
303 :
304 : /* addr_table_adtl_writable_cnt: The total number of writable account
305 : addresses across all of the address table lookups.
306 : addr_table_adtl_writable_cnt in [0, addr_table_adtl_cnt]. */
307 : uchar addr_table_adtl_writable_cnt;
308 :
309 : /* addr_table_adtl_cnt: The total number of account addresses summed across
310 : all the address lookup tables. addr_table_adtl_cnt in
311 : [0, FD_TXN_ACCT_ADDR_MAX - acct_addr_cnt]. Since acct_addr_cnt > 0,
312 : addr_table_adtl_cnt < 64. */
313 : uchar addr_table_adtl_cnt;
314 :
315 : uchar _padding_reserved_1; /* explicit padding the compiler would have
316 : inserted anyways */
317 :
318 : /* v1_txn_config_values_off: The offset relative to the start of the
319 : transaction of the config values region. The config values region
320 : contains the fields which the V1 config mask indicates are
321 : present, packed together. Fields which are not present are not
322 : included, not set to zero.
323 :
324 : Legacy/V0 transactions have no config mask, so this field is 0
325 : for legacy/v0 transactions. */
326 : ushort v1_txn_config_values_off;
327 :
328 : /* From the address table lookups, we can add the following to the above table
329 : Index Range | Signer? | Writeable?
330 : -----------------------------------------------------------------------------------------------|--------------|-------------
331 : ...
332 : [acct_addr_cnt, acct_addr_cnt + addr_table_adtl_writable_cnt) | not signer | writable
333 : [acct_addr_cnt + addr_table_adtl_writable_cnt, acct_addr_cnt + addr_table_adtl_cnt) | not signer | readonly
334 : */
335 :
336 : /* instr_cnt: The number of instructions in this transaction.
337 : instr_cnt in [0, FD_TXN_INSTR_MAX]. */
338 : ushort instr_cnt;
339 :
340 : /* instr: The array of instructions in this transaction. It's a "flexible array
341 : member" since C does not allow the pretty typical 0-len array at the end
342 : of the struct trick.
343 : Indexed [0, instr_cnt). */
344 : fd_txn_instr_t instr[ ];
345 :
346 : /* Logically, there's another field here:
347 : address_tables: The address tables this transaction imports and which
348 : accounts from them are selected for inclusion in this transaction's
349 : overall list of accounts. Indexed [0, addr_table_lookup_cnt).
350 : fd_txn_acct_addr_lut_t address_tables[ ];
351 : To access it, call fd_txn_get_address_tables( ). */
352 :
353 : };
354 :
355 : typedef struct fd_txn fd_txn_t;
356 :
357 : /* fd_txn_acct_addr_lut: An on-chain address lookup table. Solana added this to
358 : the Transaction v2 spec in order to allow a transaction to reference more
359 : accounts. This struct specifies which account addresses from an on-chain
360 : list should be selected to include in the list of account addresses
361 : available to instructions in this transaction */
362 : struct fd_txn_acct_addr_lut {
363 : /* addr_off: The offset (relative to the start of the transaction) in bytes
364 : where the address of the account containing the list of to load is stored.
365 :
366 : Specifically, if uchar const * payload is a pointer to the first byte of
367 : the transaction data in the packet, then
368 : (fd_txn_acct_addr_t*)(payload+addr_off) is a pointer to the account
369 : address. Since (payload+acct_addr_off) points inside the packet, it
370 : should be treated as pointing to unaligned data. */
371 : ushort addr_off;
372 :
373 : /* writable_cnt: The number of account addresses this LUT selects as writable
374 : from the on-chain list. */
375 : uchar writable_cnt;
376 : /* readonly_cnt: The number of account addresses this LUT selects as read
377 : only from the on-chain list. */
378 : uchar readonly_cnt;
379 :
380 : /* writable_off: The offset (relative to the start of the transaction) in
381 : bytes where the writable account indices begins.
382 :
383 : Specifically, if uchar const * payload is a pointer to the first byte of
384 : the transaction data in the packet, then (payload+writable_off)[i] for i
385 : in [0, writable_cnt) gives the indices into the on-chain list that are
386 : selected for inclusion in this transaction's list of account addresses as
387 : writable accounts. */
388 : ushort writable_off;
389 :
390 : /* readonly_off: The offset (relative to the start of the transaction) in
391 : bytes where the read only account indices begins.
392 :
393 : Specifically, if uchar const * payload is a pointer to the first byte of
394 : the transaction data in the packet, then (payload+readonly_off)[i] for i
395 : in [0, readonly_cnt) gives the indices into the on-chain list that are
396 : selected for inclusion in this transaction's list of account addresses as
397 : read only accounts. */
398 : ushort readonly_off;
399 : };
400 :
401 : typedef struct fd_txn_acct_addr_lut fd_txn_acct_addr_lut_t;
402 :
403 0 : #define FD_TXN_PARSE_COUNTERS_RING_SZ (32UL)
404 :
405 : /* Counters for collecting some metrics about the outcome of parsing
406 : transactions */
407 : struct fd_txn_parse_counters {
408 : /* success_cnt: the number of times a transaction parsed successfully */
409 : ulong success_cnt;
410 : /* failure_cnt: the number of times a transaction was ill-formed and failed
411 : to parse for any reason */
412 : ulong failure_cnt;
413 : /* failure_ring: some information about the causes of recent transaction
414 : parsing failures. Specifically, the line of code which detected that the
415 : ith malformed transaction was malformed maps to
416 : failure_ring[ i%FD_TXN_PARSE_COUNTERS_RING_SZ ] (where i starts at 0), and the
417 : last instance mapping to each element of the array is the one that is
418 : actually present. If fewer than FD_TXN_PARSE_COUNTERS_RING_SZ failures have
419 : occurred, the contents of some entries in this array are undefined. */
420 : ulong failure_ring[ FD_TXN_PARSE_COUNTERS_RING_SZ ];
421 : };
422 : typedef struct fd_txn_parse_counters fd_txn_parse_counters_t;
423 :
424 : FD_PROTOTYPES_BEGIN
425 : /* fd_txn_get_address_tables: Returns the array of address tables in this
426 : transaction. This depends on the value of txn->instr_cnt being correct. The
427 : lifetime of the returned pointer is the same as the fd_txn_t pointer passed
428 : as an argument, so it's not necessary to free the returned pointer
429 : separately. Treat it as if this function returned a pointer to a member of
430 : the struct. Suppose x=fd_txn_get_address_tables( txn ), then x[ i ] is valid
431 : for i in [0, txn->addr_table_lookup_cnt ). */
432 : static inline fd_txn_acct_addr_lut_t *
433 10718 : fd_txn_get_address_tables( fd_txn_t * txn ) {
434 10718 : return (fd_txn_acct_addr_lut_t *)(txn->instr + txn->instr_cnt);
435 10718 : }
436 :
437 : static inline fd_txn_acct_addr_lut_t const *
438 17135 : fd_txn_get_address_tables_const( fd_txn_t const * txn ) {
439 17135 : return (fd_txn_acct_addr_lut_t const *)(txn->instr + txn->instr_cnt);
440 17135 : }
441 :
442 : /* fd_acct_addr_t: An Solana account address, which may be an Ed25519
443 : public key, a SHA256 hash from a program derived address, a hardcoded
444 : sysvar, etc. This type does not imply any alignment. */
445 : union fd_acct_addr {
446 : uchar b[FD_TXN_ACCT_ADDR_SZ];
447 : };
448 : typedef union fd_acct_addr fd_acct_addr_t;
449 :
450 : /* fd_txn_get_{signatures, acct_addrs}: Returns the array of Ed25519
451 : signatures or account addresses (commonly, yet imprecisely called
452 : pubkeys), respectively, in `payload`, the serialization of the
453 : transaction described by `txn`. The number of signatures is seen in
454 : `txn->signature_cnt` and the number of account addresses is in
455 : `txn->acct_addr_cnt`.
456 :
457 : The lifetime of the returned signature is the lifetime of `payload`.
458 : Expect the returned pointer to point to memory with no particular
459 : alignment. U.B. If `payload` and `txn` were not arguments to a valid
460 : `fd_txn_parse` call or if either was modified after the parse call.
461 : */
462 : FD_FN_PURE static inline fd_ed25519_sig_t const *
463 : fd_txn_get_signatures( fd_txn_t const * txn,
464 0 : void const * payload ) {
465 0 : return (fd_ed25519_sig_t const *)((ulong)payload + (ulong)txn->signature_off);
466 0 : }
467 :
468 : FD_FN_PURE static inline fd_acct_addr_t const *
469 : fd_txn_get_acct_addrs( fd_txn_t const * txn,
470 78577 : void const * payload ) {
471 78577 : return (fd_acct_addr_t const *)((ulong)payload + (ulong)txn->acct_addr_off);
472 78577 : }
473 :
474 : FD_FN_PURE static inline uchar const *
475 : fd_txn_get_recent_blockhash( fd_txn_t const * txn,
476 0 : void const * payload ) {
477 0 : return (uchar const *)((ulong)payload + (ulong)txn->recent_blockhash_off);
478 0 : }
479 :
480 : FD_FN_PURE static inline uchar const *
481 : fd_txn_get_instr_accts( fd_txn_instr_t const * instr,
482 5810 : void const * payload ) {
483 5810 : return (uchar const *)((ulong)payload + (ulong)instr->acct_off);
484 5810 : }
485 :
486 : FD_FN_PURE static inline uchar const *
487 : fd_txn_get_instr_data( fd_txn_instr_t const * instr,
488 41541 : void const * payload ) {
489 41541 : return (uchar const *)((ulong)payload + (ulong)instr->data_off);
490 41541 : }
491 :
492 : /* fd_txn_align returns the alignment in bytes required of a region of
493 : memory to be used as a fd_txn_t. It is the same as
494 : alignof(fd_txn_t). */
495 : static inline ulong
496 0 : fd_txn_align( void ) {
497 0 : return alignof(fd_txn_t);
498 0 : }
499 :
500 : /* fd_txn_footprint: Returns the total size of txn, including the
501 : instructions and the address tables (if any). */
502 : static inline ulong
503 : fd_txn_footprint( ulong instr_cnt,
504 10722 : ulong addr_table_lookup_cnt ) {
505 10722 : return sizeof(fd_txn_t) + instr_cnt*sizeof(fd_txn_instr_t) + addr_table_lookup_cnt*sizeof(fd_txn_acct_addr_lut_t);
506 10722 : }
507 :
508 :
509 : /* Each account address in a transaction has 3 independent binary
510 : properties:
511 : - readonly/writable: this is enforced in the runtime, but a
512 : transaction fails if it tries to modify the contents of an
513 : account it marks as readonly
514 : - signer/nonsigner: the sigverify tile ensures that the transaction
515 : has been validly signed by the key associated to each account
516 : address marked as a signer
517 : - immediate/address lookup table: account addresses can come from
518 : the transaction itself ("immediate"), which is the only option
519 : for legacy transactions, or from an address lookup table
520 :
521 : For example, the fee payer must be writable, a signer, and immediate.
522 :
523 : From these properties, we can make categories of account addresses
524 : for counting and iterating over account addresses. Since these
525 : properties can be set independently, it would seem to give us 2*2*2=8
526 : categories of accounts based on the properties, but account addresses
527 : that come from an address lookup table cannot be signers, giving 6
528 : raw categories instead of 8.
529 :
530 : The individual types of accounts are defined as bitflags so that
531 : combination categories can be created easily, e.g. all readonly
532 : accounts or all signers. */
533 :
534 : /* Signer? Writable? Source? */
535 25641 : #define FD_TXN_ACCT_CAT_WRITABLE_SIGNER 0x01 /* Yes Yes imm */
536 25641 : #define FD_TXN_ACCT_CAT_READONLY_SIGNER 0x02 /* Yes No imm */
537 25641 : #define FD_TXN_ACCT_CAT_WRITABLE_NONSIGNER_IMM 0x04 /* No Yes imm */
538 25641 : #define FD_TXN_ACCT_CAT_READONLY_NONSIGNER_IMM 0x08 /* No No imm */
539 27183 : #define FD_TXN_ACCT_CAT_WRITABLE_ALT 0x10 /* No Yes lookup */
540 25641 : #define FD_TXN_ACCT_CAT_READONLY_ALT 0x20 /* No No lookup */
541 :
542 : /* Define some groupings for convenience. In the
543 : table below, "Any" means "don't care" */
544 8683 : #define FD_TXN_ACCT_CAT_WRITABLE 0x15 /* Any Yes Any */
545 0 : #define FD_TXN_ACCT_CAT_READONLY 0x2A /* Any No Any */
546 0 : #define FD_TXN_ACCT_CAT_SIGNER 0x03 /* Yes Any Any/imm*/
547 0 : #define FD_TXN_ACCT_CAT_NONSIGNER 0x3C /* No Any Any */
548 3084 : #define FD_TXN_ACCT_CAT_IMM 0x0F /* Any Any imm */
549 1542 : #define FD_TXN_ACCT_CAT_ALT 0x30 /* No Any lookup */
550 0 : #define FD_TXN_ACCT_CAT_NONE 0x00 /* --- Empty set --- */
551 0 : #define FD_TXN_ACCT_CAT_ALL 0x3F /* Any Any Any */
552 :
553 : /* fd_txn_account_cnt: Returns the number of accounts referenced by this
554 : transaction that have the property specified by include_category.
555 : txn must be a pointer to a valid transaction. include_cat must be
556 : one of the previously defined FD_TXN_ACCT_CAT_* values. Ideally,
557 : include_cat should be a compile-time constant, in which case this
558 : function typically compiles to about 3 instructions. */
559 : static inline ulong
560 : fd_txn_account_cnt( fd_txn_t const * txn,
561 24099 : int include_cat ) {
562 24099 : ulong cnt = 0UL;
563 24099 : if( include_cat & FD_TXN_ACCT_CAT_WRITABLE_SIGNER ) cnt += (ulong)txn->signature_cnt - (ulong)txn->readonly_signed_cnt;
564 24099 : if( include_cat & FD_TXN_ACCT_CAT_READONLY_SIGNER ) cnt += (ulong)txn->readonly_signed_cnt;
565 24099 : if( include_cat & FD_TXN_ACCT_CAT_READONLY_NONSIGNER_IMM ) cnt += (ulong)txn->readonly_unsigned_cnt;
566 24099 : if( include_cat & FD_TXN_ACCT_CAT_WRITABLE_ALT ) cnt += (ulong)txn->addr_table_adtl_writable_cnt;
567 24099 : if( include_cat & FD_TXN_ACCT_CAT_WRITABLE_NONSIGNER_IMM )
568 13305 : cnt += (ulong)txn->acct_addr_cnt - (ulong)txn->signature_cnt - (ulong)txn->readonly_unsigned_cnt;
569 24099 : if( include_cat & FD_TXN_ACCT_CAT_READONLY_ALT )
570 3084 : cnt += (ulong)txn->addr_table_adtl_cnt - (ulong)txn->addr_table_adtl_writable_cnt;
571 :
572 24099 : return cnt;
573 24099 : }
574 :
575 : /* fd_txn_acct_iter_{init, next, end, idx}: These functions are used for
576 : iterating over the accounts in a transaction that have the property
577 : specified by include_cat.
578 :
579 : Example usage:
580 :
581 : fd_txn_acct_addr_t const * acct = fd_txn_get_acct_addrs( txn, payload );
582 : for( fd_txn_acct_iter_t i=fd_txn_acct_iter_init( txn, FD_TXN_ACCT_CAT_WRITABLE );
583 : i!=fd_txn_acct_iter_end(); i=fd_txn_acct_iter_next( i ) ) {
584 : // Do something with acct[ fd_txn_acct_iter_idx( i ) ]
585 : }
586 :
587 : For fd_txn_acct_iter_init, txn must be a pointer to a valid
588 : transaction and include_cat must be one of the FD_TXN_ACCT_CAT_*
589 : values defined above (or a bitwise combination of them). On
590 : completion, returns a value i such that fd_txn_acct_iter_idx( i ) is
591 : the index of the first account address meeting the specified
592 : criteria, or i==fd_txn_acct_iter_end() if there aren't any account
593 : addresses that meet the criteria.
594 :
595 : For fd_acct_iter_next, cur should be the current value of the
596 : iteration variable. Advances the iteration variable such that
597 : fd_txn_acct_iter_idx( i ) is the index of the next account meeting
598 : the initially specified criteria, or i==fd_txn_acct_iter_end() if
599 : there aren't any more account addresses meeting the criteria. It is
600 : undefined behavior to call fd_acct_iter_next with a value of cur not
601 : returned by a call to either fd_acct_iter_init or fd_acct_iter_next.
602 : It's also U.B. to call fd_acct_iter_next after fd_acct_iter_end has
603 : been returned.
604 :
605 : fd_txn_acct_iter_t should be treated as an opaque handle and not
606 : modified other than by using fd_txn_acc_iter_next. You can peek and
607 : see that it's a ulong, so it fits in a register and doesn't need to
608 : be destroyed or cleaned up. It's safe to save a fd_txn_acct_iter_t
609 : value to resume iteration later with the same transaction. */
610 :
611 : typedef ulong fd_txn_acct_iter_t;
612 :
613 : /* Account addresses are categorized into 6 categories, and all the
614 : account addresses for each category are stored contiguously. This
615 : means that for any subset of the 6 categories that the user wants to
616 : iterate over, there are at most 3 disjoint ranges.
617 :
618 : For any iteration space I, we can choose 6 integers
619 : {start,count}_{0,1,2} so that
620 : I = [start0, start0+count0) U [start1, start1+count1)
621 : U [start2, start2+count2)
622 : Any empty intervals are represented as [0, 0).
623 : We store the control word as a single ulong with start0 in the low
624 : order bits. Then the current account index can be retrieved by
625 : taking the low order byte, and the count remaining in the current
626 : interval is the second lowest byte. We can update both in one
627 : instruction by subtracting 255. */
628 :
629 : static inline fd_txn_acct_iter_t FD_FN_PURE
630 : fd_txn_acct_iter_init( fd_txn_t const * txn,
631 0 : int include_cat ) {
632 : /* Our goal is to output something that looks like [start0, count0,
633 : start1, count1, start2, count2, 0, 0] from lowest order to highest.
634 : We construct the potentially 3 (start, count) pairs and then
635 : branchlessly get rid of any empty ones. */
636 0 : ulong control[3] = { 0 }; /* High 6 bytes of each stay element not touched */
637 0 : ulong i = (ulong)(-1L); /* So that it is 0 post increment */
638 :
639 : /* Make references more convenient. Dead code elimination seems to
640 : take care of the unneeded ones. */
641 0 : ulong s = txn->signature_cnt;
642 0 : ulong q = txn->readonly_signed_cnt;
643 0 : ulong r = txn->readonly_unsigned_cnt;
644 0 : ulong a = txn->acct_addr_cnt;
645 0 : ulong t = txn->addr_table_adtl_cnt;
646 0 : ulong u = txn->addr_table_adtl_writable_cnt;
647 :
648 : /* All the branches here should be known at compile time. */
649 : /* If WRITABLE_SIGNER is included, then f>>1 is 0, so the second
650 : branch will always be true, setting control[0]=0. */
651 0 : # define INCLUDE_RANGE(f, start, cnt) \
652 0 : if( include_cat & (f) ) { \
653 0 : if( !(include_cat & ((f)>>1) ) ) control[ ++i ]=(start); \
654 0 : control[ i ] += (cnt)<<8; \
655 0 : }
656 :
657 0 : INCLUDE_RANGE( FD_TXN_ACCT_CAT_WRITABLE_SIGNER, 0, s-q );
658 0 : INCLUDE_RANGE( FD_TXN_ACCT_CAT_READONLY_SIGNER, s-q, q );
659 0 : INCLUDE_RANGE( FD_TXN_ACCT_CAT_WRITABLE_NONSIGNER_IMM, s, a-r-s );
660 0 : INCLUDE_RANGE( FD_TXN_ACCT_CAT_READONLY_NONSIGNER_IMM, a-r, r );
661 0 : INCLUDE_RANGE( FD_TXN_ACCT_CAT_WRITABLE_ALT, a, u );
662 0 : INCLUDE_RANGE( FD_TXN_ACCT_CAT_READONLY_ALT, a+u, t-u );
663 0 : # undef INCLUDE_RANGE
664 :
665 : /* We now need to delete the empty intervals (if any). */
666 0 : ulong control0 = control[0];
667 0 : ulong control1 = control[1];
668 0 : ulong control2 = control[2];
669 :
670 0 : int control2_empty = !(control2&0xFF00UL);
671 0 : control2 = fd_ulong_if( control2_empty, 0UL, control2 );
672 :
673 0 : int control1_empty = !(control1&0xFF00UL);
674 0 : control1 = fd_ulong_if( control1_empty, control2, control1 );
675 0 : control2 = fd_ulong_if( control1_empty, 0UL, control2 );
676 :
677 0 : int control0_empty = !(control0&0xFF00UL);
678 0 : control0 = fd_ulong_if( control0_empty, control1, control0 );
679 0 : control1 = fd_ulong_if( control0_empty, control2, control1 );
680 0 : control2 = fd_ulong_if( control0_empty, 0UL, control2 );
681 :
682 0 : return control0 | (control1<<16) | (control2<<32);
683 0 : }
684 :
685 : static inline fd_txn_acct_iter_t FD_FN_CONST
686 0 : fd_txn_acct_iter_next( fd_txn_acct_iter_t cur ) {
687 0 : cur = cur + 0x0001UL - 0x0100UL; /* Increment low byte, decrement count */
688 : /* Move to the next interval if we're done with this one. */
689 0 : return fd_ulong_if( cur&0xFF00UL, cur, cur>>16 );
690 0 : }
691 :
692 0 : static inline fd_txn_acct_iter_t FD_FN_CONST fd_txn_acct_iter_end( void ) { return 0UL; }
693 0 : static inline ulong FD_FN_CONST fd_txn_acct_iter_idx( fd_txn_acct_iter_t cur ) { return cur & 0xFFUL; }
694 :
695 : /* fd_txn_parse_core: Parses a transaction from the canonical encoding, i.e.
696 : the format used on the wire.
697 :
698 : Payload points to the first byte of encoded transaction, e.g. the
699 : first byte of the UDP/Quic payload if the transaction comes from the
700 : network. The encoded transaction must occupy exactly [payload,
701 : payload+payload_sz), i.e. this method will read no more than
702 : payload_sz bytes from payload, but it will reject the transaction if
703 : it contains extra padding at the end or continues past
704 : payload+payload_sz.
705 :
706 : out_buf is the memory where the parsed transaction will be stored.
707 : out_buf must be non-NULL and have room for at least FD_TXN_MAX_SZ
708 : bytes.
709 :
710 : Returns the total size of the resulting fd_txn struct on success and
711 : 0 on failure. On failure, the contents of out_buf are undefined,
712 : although nothing will be written beyond FD_TXN_MAX_SZ bytes.
713 :
714 : If counters_opt is non-NULL, some counters about the result of the
715 : parsing process will be accumulated into the struct pointed to by
716 : counters_opt. Note: The returned txn object is not self-contained
717 : since it refers to byte ranges inside the payload.
718 :
719 : payload_sz_opt, if supplied, gets filled with the total bytes this txn
720 : uses (allowing for walking of an entry/microblock). If it is not supplied, the
721 : parse will return an error if the payload_sz does not exactly match. */
722 :
723 : ulong
724 : fd_txn_parse_core( uchar const * payload,
725 : ulong payload_sz,
726 : void * out_buf,
727 : fd_txn_parse_counters_t * counters_opt,
728 : ulong * payload_sz_opt );
729 :
730 :
731 : /* fd_txn_parse: Convenient wrapper around fd_txn_parse_core that eliminates some optional arguments */
732 : static inline ulong
733 9172 : fd_txn_parse( uchar const * payload, ulong payload_sz, void * out_buf, fd_txn_parse_counters_t * counters_opt ) {
734 9172 : return fd_txn_parse_core( payload, payload_sz, out_buf, counters_opt, NULL );
735 9172 : }
736 :
737 : /* fd_txn_msg_sz returns the size in bytes of the signed message region
738 : of a transaction - not including the transaction signatures.
739 :
740 : The message bytes begin at message_off, relative to the start of
741 : the payload.
742 :
743 : Where these bytes are located in the transaction payload is
744 : dependent on the transaction format: legacy/V0 transactions have
745 : signatures at the front and then the message, whereas V1
746 : transactions have the message at the front and then the signatures.
747 :
748 : fd_txn_msg_sz and message_off should always be used together. */
749 : FD_FN_PURE static inline ulong
750 : fd_txn_msg_sz( fd_txn_t const * txn,
751 0 : ulong payload_sz ) {
752 0 : ulong msg_end = ( txn->transaction_version==FD_TXN_V1 ) ? (ulong)txn->signature_off
753 0 : : payload_sz;
754 0 : return msg_end - (ulong)txn->message_off;
755 0 : }
756 :
757 : /* fd_txn_is_writable: Is the account at the supplied index writable
758 :
759 : Accounts ordered:
760 : Index Range | Signer? | Writeable?
761 : ---------------------------------------------------------------------------------|--------------|-------------
762 : [0, signature_cnt - readonly_signed_cnt) | signer | writable
763 : [signature_cnt, acct_addr_cnt - readonly_unsigned_cnt) | not signer | writable
764 : */
765 :
766 : static inline int
767 165593 : fd_txn_is_writable( fd_txn_t const * txn, ushort idx ) {
768 165593 : if (txn->transaction_version == FD_TXN_V0 && idx >= txn->acct_addr_cnt) {
769 81409 : if (idx < (txn->acct_addr_cnt + txn->addr_table_adtl_writable_cnt)) {
770 38558 : return 1;
771 38558 : }
772 42851 : return 0;
773 81409 : }
774 :
775 84184 : if (idx < (txn->signature_cnt - txn->readonly_signed_cnt))
776 37693 : return 1;
777 46491 : if ((idx >= txn->signature_cnt) & (idx < (txn->acct_addr_cnt - txn->readonly_unsigned_cnt)))
778 10723 : return 1;
779 :
780 35768 : return 0;
781 46491 : }
782 :
783 : /* fd_txn_is_signer: Is the account at the supplied index a signer
784 :
785 : Accounts ordered:
786 : Index Range | Signer? | Writeable?
787 : ---------------------------------------------------------------------------------|--------------|-------------
788 : [0, signature_cnt - readonly_signed_cnt) | signer | writable
789 : [signature_cnt - readonly_signed_cnt, signature_cnt) | signer | readonly
790 : */
791 : static inline int
792 21768 : fd_txn_is_signer( fd_txn_t const * txn, int idx ) {
793 21768 : return idx < txn->signature_cnt;
794 21768 : }
795 :
796 : FD_PROTOTYPES_END
797 :
798 : #endif /* HEADER_fd_src_ballet_txn_fd_txn_h */
|