@typesol/cli 0.27.0 → 0.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +3 -1
  3. package/dist/compiler/analyze.d.ts +5 -1
  4. package/dist/compiler/analyze.js +53 -15
  5. package/dist/compiler/emit/alias.d.ts +48 -0
  6. package/dist/compiler/emit/alias.js +166 -0
  7. package/dist/compiler/emit/base.d.ts +1 -0
  8. package/dist/compiler/emit/borsh.js +6 -3
  9. package/dist/compiler/emit/calls.js +23 -4
  10. package/dist/compiler/emit/cpi-core.d.ts +70 -0
  11. package/dist/compiler/emit/cpi-core.js +356 -0
  12. package/dist/compiler/emit/cpi.d.ts +10 -175
  13. package/dist/compiler/emit/cpi.js +71 -2716
  14. package/dist/compiler/emit/create.d.ts +39 -0
  15. package/dist/compiler/emit/create.js +548 -0
  16. package/dist/compiler/emit/function.d.ts +11 -2
  17. package/dist/compiler/emit/function.js +55 -12
  18. package/dist/compiler/emit/metaplex.d.ts +78 -0
  19. package/dist/compiler/emit/metaplex.js +1345 -0
  20. package/dist/compiler/emit/program.js +24 -4
  21. package/dist/compiler/emit/shared.d.ts +5 -1
  22. package/dist/compiler/emit/shared.js +11 -2
  23. package/dist/compiler/emit/statements.d.ts +0 -9
  24. package/dist/compiler/emit/statements.js +2 -53
  25. package/dist/compiler/emit/storage.d.ts +5 -20
  26. package/dist/compiler/emit/storage.js +55 -100
  27. package/dist/compiler/emit/token.d.ts +8 -0
  28. package/dist/compiler/emit/token.js +493 -0
  29. package/dist/compiler/idl.js +1 -1
  30. package/dist/compiler/model.d.ts +12 -2
  31. package/dist/compiler/model.js +7 -1
  32. package/dist/tools/portgen.js +3 -0
  33. package/docs/architecture.md +31 -2
  34. package/docs/audit.md +4 -2
  35. package/docs/case-study-multisig.md +169 -0
  36. package/docs/errors.md +6 -4
  37. package/docs/language.md +22 -6
  38. package/docs/migrate-from-anchor.md +7 -2
  39. package/docs/security-model.md +5 -1
  40. package/lib/typesol.d.ts +51 -7
  41. package/package.json +1 -1
  42. package/runtime/typesol_rt.h +93 -3
@@ -27,6 +27,7 @@
27
27
  #define TS_ERR_CONSTRAINT_EXECUTABLE 2007 /* ConstraintExecutable */
28
28
  #define TS_ERR_ASSOCIATED 2009 /* ConstraintAssociated */
29
29
  #define TS_ERR_ADDRESS 2012 /* ConstraintAddress */
30
+ #define TS_ERR_CONSTRAINT_ZERO 2013 /* ConstraintZero */
30
31
  #define TS_ERR_TOKEN_MINT 2014 /* ConstraintTokenMint */
31
32
  #define TS_ERR_TOKEN_OWNER 2015 /* ConstraintTokenOwner */
32
33
  #define TS_ERR_MINT_AUTHORITY 2016 /* ConstraintMintMintAuthority */
@@ -267,8 +268,14 @@ _Static_assert(__builtin_offsetof(TsAccount, key) == 8 && __builtin_offsetof(TsA
267
268
  __builtin_offsetof(TsAccount, data) == 88,
268
269
  "TsAccount layout");
269
270
 
270
- /* Accounts per instruction: the entry point refuses more (5113). */
271
+ /* Accounts per instruction: the entry point refuses more (5113). 32 unless the program declares
272
+ `const MAX_ACCOUNTS = …n` (up to 255); above 32 the CPI scratch lists live on the heap (the frame stays small). */
273
+ #ifndef TS_MAX_ACCOUNTS
271
274
  #define TS_MAX_ACCOUNTS 32
275
+ #endif
276
+ #if TS_MAX_ACCOUNTS > 32
277
+ #define TS_SCRATCH_ON_HEAP 1
278
+ #endif
272
279
 
273
280
  /* Lives in the entry point's frame; the arrays are inline so setting it up stores nothing. */
274
281
  typedef struct {
@@ -277,10 +284,43 @@ typedef struct {
277
284
  uint64_t data_len;
278
285
  const SolPubkey *program_id;
279
286
  TsAccount *ka[TS_MAX_ACCOUNTS]; /* ka_num entries */
287
+ #ifdef TS_SCRATCH_ON_HEAP
288
+ SolAccountInfo *infos; /* scratch for CPI account lists, grown on the heap (ts_infos_reserve) */
289
+ uint64_t infos_cap;
290
+ SolAccountMeta *metas; /* scratch for CPI meta lists with remaining accounts (TS_METAS) */
291
+ #else
280
292
  SolAccountInfo infos[TS_MAX_ACCOUNTS]; /* scratch for CPI account lists (ts_invoke) */
293
+ #endif
294
+ #if TS_MAX_ACCOUNTS > 64
295
+ uint64_t loaded_mut[(TS_MAX_ACCOUNTS + 63) / 64]; /* bit i: ka[i] loaded with loadAccountMut */
296
+ #else
281
297
  uint64_t loaded_mut; /* bit i: ka[i] loaded with loadAccountMut (zeroed by the entrypoint of a program that uses it) */
298
+ #endif
282
299
  } TsParams;
283
300
 
301
+ #ifdef TS_SCRATCH_ON_HEAP
302
+ static uint64_t ts_infos_reserve(TsParams *params, uint64_t n);
303
+ static uint64_t ts_metas_scratch(TsParams *params, SolAccountMeta **out);
304
+ #define TS_INFOS_RESERVE(params, n) TS_TRY(ts_infos_reserve(params, n))
305
+ #define TS_METAS_ROOM(m) \
306
+ SolAccountMeta *m; \
307
+ TS_TRY(ts_metas_scratch(params, &m))
308
+ /* A CPI's meta list with room for every account: on the heap, reused by each such CPI. */
309
+ #define TS_METAS(m, ...) \
310
+ SolAccountMeta *m; \
311
+ TS_TRY(ts_metas_scratch(params, &m)); \
312
+ { \
313
+ const SolAccountMeta m##_init[] = {__VA_ARGS__}; \
314
+ ts_memcpy(m, m##_init, sizeof(m##_init)); \
315
+ }
316
+ #else
317
+ #define TS_INFOS_RESERVE(params, n) \
318
+ do { \
319
+ } while (0)
320
+ #define TS_METAS(m, ...) SolAccountMeta m[TS_MAX_ACCOUNTS] = {__VA_ARGS__}
321
+ #define TS_METAS_ROOM(m) SolAccountMeta m[TS_MAX_ACCOUNTS]
322
+ #endif
323
+
284
324
  /*
285
325
  * Same bounds as the SDK's sol_deserialize, minus its per-account "past the array" branch: the entry point has
286
326
  * already refused counts above TS_MAX_ACCOUNTS (the size of `params->ka`), so every account fits.
@@ -330,6 +370,7 @@ static inline __attribute__((always_inline)) void ts_info(const TsAccount *a, So
330
370
  static inline __attribute__((always_inline)) uint64_t ts_invoke(TsParams *params, const SolInstruction *ix, const SolSignerSeeds *signers, int nsigners) {
331
371
  /* At most TS_MAX_ACCOUNTS metas (the compiler refuses longer lists), the size of `params->infos`. A repeated
332
372
  account gets a record per mention, which the runtime accepts. */
373
+ TS_INFOS_RESERVE(params, ix->account_len);
333
374
  SolAccountInfo *infos = params->infos;
334
375
  for (uint64_t i = 0; i < ix->account_len; i++) ts_info((const TsAccount *)((const uint8_t *)ix->accounts[i].pubkey - 8), &infos[i]);
335
376
  return sol_invoke_signed(ix, infos, ix->account_len, signers, nsigners);
@@ -454,8 +495,13 @@ static inline uint64_t ts_claim_mut(TsParams *params, const TsAccount *a) {
454
495
  for (uint64_t i = 0; i < params->ka_num; i++)
455
496
  if (params->ka[i] == a) seen++, at = i;
456
497
  if (seen > 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account passed more than once");
498
+ #if TS_MAX_ACCOUNTS > 64
499
+ if (params->loaded_mut[at >> 6] >> (at & 63) & 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account already loaded with loadAccountMut");
500
+ params->loaded_mut[at >> 6] |= (uint64_t)1 << (at & 63);
501
+ #else
457
502
  if (params->loaded_mut >> at & 1) TS_FAIL(TS_ERR_DUPLICATE_MUT, "TypeSOL: account already loaded with loadAccountMut");
458
503
  params->loaded_mut |= (uint64_t)1 << at;
504
+ #endif
459
505
  return SUCCESS;
460
506
  }
461
507
 
@@ -654,10 +700,15 @@ static inline uint64_t ts_now(int64_t *out) {
654
700
  * to this program. It must be writable, rent-exempt, big enough and still all-zero in its discriminator;
655
701
  * the discriminator is written here so the account can never be initialized twice.
656
702
  */
657
- static inline uint64_t ts_init_zeroed(TsAccount *a, const SolPubkey *program_id, const uint8_t disc[8], uint64_t size) {
703
+ /* Zeroed<T> (Anchor's `zero`), in Anchor's order: a discriminator already set (2013), then owner and size as the
704
+ * account type checks them; the rent check and the discriminator write come after the mut / signer checks. */
705
+ static inline uint64_t ts_check_zeroed(const TsAccount *a, const SolPubkey *program_id, uint64_t size) {
706
+ if (a->data_len >= 8 && ts_ld64(a->data) != 0) TS_FAIL(TS_ERR_CONSTRAINT_ZERO, "TypeSOL: account already initialized");
658
707
  if (!ts_key_eq(&a->owner, program_id)) TS_FAIL(TS_ERR_WRONG_OWNER, "TypeSOL: account not owned by this program");
659
708
  if (a->data_len < 8 + size) TS_FAIL(TS_ERR_ACCOUNT_TOO_SMALL, "TypeSOL: account data too small");
660
- if (ts_ld64(a->data) != 0) TS_FAIL(TS_ERR_ALREADY_INITIALIZED, "TypeSOL: account already initialized");
709
+ return SUCCESS;
710
+ }
711
+ static inline uint64_t ts_init_zeroed(TsAccount *a, const uint8_t disc[8]) {
661
712
  uint64_t need;
662
713
  TS_TRY(ts_minimum_balance(a->data_len, &need));
663
714
  if (a->lamports < need) TS_FAIL(TS_ERR_NOT_RENT_EXEMPT, "TypeSOL: account is not rent-exempt");
@@ -1266,6 +1317,45 @@ static inline uint64_t ts_alloc(uint64_t n, uint8_t **out) {
1266
1317
  return SUCCESS;
1267
1318
  }
1268
1319
 
1320
+ /* A heap Vec (a Vec<T> in a @layout borsh struct, held on the heap while the instruction runs): room for at least
1321
+ `need` elements of `size` bytes, doubling as Rust's Vec does. Elements move when it grows (the compiler refuses
1322
+ references into it that would outlive a push). Bounded by the heap; the account bounds it when it is packed (3004). */
1323
+ typedef struct __attribute__((packed)) {
1324
+ uint32_t len;
1325
+ uint32_t cap;
1326
+ uint8_t *items;
1327
+ } TsHeapVec;
1328
+ static uint64_t ts_hvec_reserve(void *vec, uint64_t need, uint64_t size) {
1329
+ TsHeapVec *v = (TsHeapVec *)vec;
1330
+ if (need <= v->cap) return SUCCESS;
1331
+ uint64_t cap = v->cap ? (uint64_t)v->cap * 2 : 4;
1332
+ if (cap < need) cap = need;
1333
+ if (cap > 0xffffffffULL) TS_FAIL(TS_ERR_HEAP, "TypeSOL: out of heap");
1334
+ uint8_t *items;
1335
+ TS_TRY(ts_alloc(cap * size, &items));
1336
+ if (v->len) ts_memcpy(items, v->items, (uint64_t)v->len * size);
1337
+ v->items = items;
1338
+ v->cap = (uint32_t)cap;
1339
+ return SUCCESS;
1340
+ }
1341
+
1342
+ #ifdef TS_SCRATCH_ON_HEAP
1343
+ /* Room for `n` SolAccountInfo records in the CPI scratch, doubling (the heap is never freed). */
1344
+ static uint64_t ts_infos_reserve(TsParams *params, uint64_t n) {
1345
+ if (n <= params->infos_cap) return SUCCESS;
1346
+ uint64_t cap = params->infos_cap ? params->infos_cap * 2 : 16;
1347
+ if (cap < n) cap = n;
1348
+ TS_TRY(ts_alloc(cap * sizeof(SolAccountInfo), (uint8_t **)&params->infos));
1349
+ params->infos_cap = cap;
1350
+ return SUCCESS;
1351
+ }
1352
+ static uint64_t ts_metas_scratch(TsParams *params, SolAccountMeta **out) {
1353
+ if (!params->metas) TS_TRY(ts_alloc(TS_MAX_ACCOUNTS * sizeof(SolAccountMeta), (uint8_t **)&params->metas));
1354
+ *out = params->metas;
1355
+ return SUCCESS;
1356
+ }
1357
+ #endif
1358
+
1269
1359
  /* Base58 text of a 32-byte value (a Pubkey or Hash in a template), on the heap. The value is eight big-endian u32
1270
1360
  limbs divided by 58^5 at a time, so each pass gives five digits with one 64-bit division per limb. */
1271
1361
  static uint64_t ts_base58_32(const uint8_t *in, TS_Str *out) {