solana_sdk.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. #pragma once
  2. /**
  3. * Numeric types
  4. */
  5. #ifndef __LP64__
  6. #error LP64 data model required
  7. #endif
  8. /** Indicates the instruction was processed successfully */
  9. #define SUCCESS 0
  10. /**
  11. * Builtin program status values occupy the upper 32 bits of the program return
  12. * value. Programs may define their own error values but they must be confined
  13. * to the lower 32 bits.
  14. */
  15. #define TO_BUILTIN(error) ((uint64_t)(error) << 32)
  16. /** Note: Not applicable to program written in C */
  17. #define ERROR_CUSTOM_ZERO TO_BUILTIN(1)
  18. /** The arguments provided to a program instruction where invalid */
  19. #define ERROR_INVALID_ARGUMENT TO_BUILTIN(2)
  20. /** An instruction's data contents was invalid */
  21. #define ERROR_INVALID_INSTRUCTION_DATA TO_BUILTIN(3)
  22. /** An account's data contents was invalid */
  23. #define ERROR_INVALID_ACCOUNT_DATA TO_BUILTIN(4)
  24. /** An account's data was too small */
  25. #define ERROR_ACCOUNT_DATA_TOO_SMALL TO_BUILTIN(5)
  26. /** An account's balance was too small to complete the instruction */
  27. #define ERROR_INSUFFICIENT_FUNDS TO_BUILTIN(6)
  28. /** The account did not have the expected program id */
  29. #define ERROR_INCORRECT_PROGRAM_ID TO_BUILTIN(7)
  30. /** A signature was required but not found */
  31. #define ERROR_MISSING_REQUIRED_SIGNATURES TO_BUILTIN(8)
  32. /** An initialize instruction was sent to an account that has already been initialized */
  33. #define ERROR_ACCOUNT_ALREADY_INITIALIZED TO_BUILTIN(9)
  34. /** An attempt to operate on an account that hasn't been initialized */
  35. #define ERROR_UNINITIALIZED_ACCOUNT TO_BUILTIN(10)
  36. /** The instruction expected additional account keys */
  37. #define ERROR_NOT_ENOUGH_ACCOUNT_KEYS TO_BUILTIN(11)
  38. /** Note: Not applicable to program written in C */
  39. #define ERROR_ACCOUNT_BORROW_FAILED TO_BUILTIN(12)
  40. /** The length of the seed is too long for address generation */
  41. #define MAX_SEED_LENGTH_EXCEEDED TO_BUILTIN(13)
  42. /** Provided seeds do not result in a valid address */
  43. #define INVALID_SEEDS TO_BUILTIN(14)
  44. /**
  45. * Boolean type
  46. */
  47. #ifndef __cplusplus
  48. #include <stdbool.h>
  49. #endif
  50. /**
  51. * Prints a string to stdout
  52. */
  53. void sol_log_(const char *, uint64_t);
  54. #define sol_log(message) sol_log_(message, sol_strlen(message))
  55. /**
  56. * Prints a 64 bit values represented in hexadecimal to stdout
  57. */
  58. void sol_log_64_(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
  59. #define sol_log_64 sol_log_64_
  60. /**
  61. * Size of Public key in bytes
  62. */
  63. #define SIZE_PUBKEY 32
  64. /**
  65. * Public key
  66. */
  67. typedef struct
  68. {
  69. uint8_t x[SIZE_PUBKEY];
  70. } SolPubkey;
  71. /**
  72. * Compares two public keys
  73. *
  74. * @param one First public key
  75. * @param two Second public key
  76. * @return true if the same
  77. */
  78. static bool SolPubkey_same(const SolPubkey *one, const SolPubkey *two)
  79. {
  80. for (int i = 0; i < sizeof(*one); i++)
  81. {
  82. if (one->x[i] != two->x[i])
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. /**
  90. * Keyed Account
  91. */
  92. typedef struct
  93. {
  94. SolPubkey *key; /** Public key of the account */
  95. uint64_t *lamports; /** Number of lamports owned by this account */
  96. uint64_t data_len; /** Length of data in bytes */
  97. uint8_t *data; /** On-chain data within this account */
  98. SolPubkey *owner; /** Program that owns this account */
  99. uint64_t rent_epoch; /** The epoch at which this account will next owe rent */
  100. bool is_signer; /** Transaction was signed by this account's key? */
  101. bool is_writable; /** Is the account writable? */
  102. bool executable; /** This account's data contains a loaded program (and is now read-only) */
  103. } SolAccountInfo;
  104. /**
  105. * Copies memory
  106. */
  107. static void sol_memcpy(void *dst, const void *src, int len)
  108. {
  109. for (int i = 0; i < len; i++)
  110. {
  111. *((uint8_t *)dst + i) = *((const uint8_t *)src + i);
  112. }
  113. }
  114. /**
  115. * Compares memory
  116. */
  117. static int sol_memcmp(const void *s1, const void *s2, int n)
  118. {
  119. for (int i = 0; i < n; i++)
  120. {
  121. uint8_t diff = *((uint8_t *)s1 + i) - *((const uint8_t *)s2 + i);
  122. if (diff)
  123. {
  124. return diff;
  125. }
  126. }
  127. return 0;
  128. }
  129. /**
  130. * Fill a byte string with a byte value
  131. */
  132. static void sol_memset(void *b, int c, size_t len)
  133. {
  134. uint8_t *a = (uint8_t *)b;
  135. while (len > 0)
  136. {
  137. *a = c;
  138. a++;
  139. len--;
  140. }
  141. }
  142. /**
  143. * Find length of string
  144. */
  145. static size_t sol_strlen(const char *s)
  146. {
  147. size_t len = 0;
  148. while (*s)
  149. {
  150. len++;
  151. s++;
  152. }
  153. return len;
  154. }
  155. /**
  156. * Computes the number of elements in an array
  157. */
  158. #define SOL_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  159. /**
  160. * Internal memory alloc/free function
  161. */
  162. void *sol_alloc_free_(uint64_t size, void *ptr);
  163. /**
  164. * Alloc zero-initialized memory
  165. */
  166. static void *sol_calloc(size_t nitems, size_t size)
  167. {
  168. return sol_alloc_free_(nitems * size, 0);
  169. }
  170. /**
  171. * Deallocates the memory previously allocated by sol_calloc
  172. */
  173. static void sol_free(void *ptr)
  174. {
  175. (void)sol_alloc_free_(0, ptr);
  176. }
  177. /**
  178. * Panics
  179. *
  180. * Prints the line number where the panic occurred and then causes
  181. * the BPF VM to immediately halt execution. No accounts' data are updated
  182. */
  183. void sol_panic_(const char *, uint64_t, uint64_t, uint64_t);
  184. #define sol_panic() sol_panic_(__FILE__, sizeof(__FILE__), __LINE__, 0)
  185. /**
  186. * Asserts
  187. */
  188. #define sol_assert(expr) \
  189. if (!(expr)) \
  190. { \
  191. sol_panic(); \
  192. }
  193. /**
  194. * Structure that the program's entrypoint input data is deserialized into.
  195. */
  196. typedef struct
  197. {
  198. SolAccountInfo ka[10]; /** Pointer to an array of SolAccountInfo, must already
  199. point to an array of SolAccountInfos */
  200. uint64_t ka_num; /** Number of SolAccountInfo entries in `ka` */
  201. uint64_t ka_cur;
  202. const SolAccountInfo *ka_last_called;
  203. const SolPubkey *account_id;
  204. const uint8_t *input; /** pointer to the instruction data */
  205. uint64_t input_len; /** Length in bytes of the instruction data */
  206. const SolPubkey *program_id; /** program_id of the currently executing program */
  207. const SolAccountInfo *ka_clock;
  208. uint32_t contract;
  209. } SolParameters;
  210. /**
  211. * Maximum number of bytes a program may add to an account during a single realloc
  212. */
  213. #define MAX_PERMITTED_DATA_INCREASE (1024 * 10)
  214. /**
  215. * De-serializes the input parameters into usable types
  216. *
  217. * Use this function to deserialize the buffer passed to the program entrypoint
  218. * into usable types. This function does not perform copy deserialization,
  219. * instead it populates the pointers and lengths in SolAccountInfo and data so
  220. * that any modification to lamports or account data take place on the original
  221. * buffer. Doing so also eliminates the need to serialize back into the buffer
  222. * at the end of the program.
  223. *
  224. * @param input Source buffer containing serialized input parameters
  225. * @param params Pointer to a SolParameters structure
  226. * @return Boolean true if successful.
  227. */
  228. static uint64_t sol_deserialize(
  229. const uint8_t *input,
  230. SolParameters *params)
  231. {
  232. if (NULL == input || NULL == params)
  233. {
  234. return ERROR_INVALID_ARGUMENT;
  235. }
  236. params->ka_num = *(uint64_t *)input;
  237. input += sizeof(uint64_t);
  238. for (int i = 0; i < params->ka_num; i++)
  239. {
  240. uint8_t dup_info = input[0];
  241. input += sizeof(uint8_t);
  242. if (i >= SOL_ARRAY_SIZE(params->ka))
  243. {
  244. if (dup_info == UINT8_MAX)
  245. {
  246. input += sizeof(uint8_t);
  247. input += sizeof(uint8_t);
  248. input += sizeof(uint8_t);
  249. input += 4; // padding
  250. input += sizeof(SolPubkey);
  251. input += sizeof(SolPubkey);
  252. input += sizeof(uint64_t);
  253. uint64_t data_len = *(uint64_t *)input;
  254. input += sizeof(uint64_t);
  255. input += data_len;
  256. input += MAX_PERMITTED_DATA_INCREASE;
  257. input = (uint8_t *)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
  258. input += sizeof(uint64_t);
  259. }
  260. continue;
  261. }
  262. if (dup_info == UINT8_MAX)
  263. {
  264. // is signer?
  265. params->ka[i].is_signer = *(uint8_t *)input != 0;
  266. input += sizeof(uint8_t);
  267. // is writable?
  268. params->ka[i].is_writable = *(uint8_t *)input != 0;
  269. input += sizeof(uint8_t);
  270. // executable?
  271. params->ka[i].executable = *(uint8_t *)input;
  272. input += sizeof(uint8_t);
  273. input += 4; // padding
  274. // key
  275. params->ka[i].key = (SolPubkey *)input;
  276. input += sizeof(SolPubkey);
  277. // owner
  278. params->ka[i].owner = (SolPubkey *)input;
  279. input += sizeof(SolPubkey);
  280. // lamports
  281. params->ka[i].lamports = (uint64_t *)input;
  282. input += sizeof(uint64_t);
  283. // account data
  284. params->ka[i].data_len = *(uint64_t *)input;
  285. input += sizeof(uint64_t);
  286. params->ka[i].data = (uint8_t *)input;
  287. input += params->ka[i].data_len;
  288. input += MAX_PERMITTED_DATA_INCREASE;
  289. input = (uint8_t *)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
  290. // rent epoch
  291. params->ka[i].rent_epoch = *(uint64_t *)input;
  292. input += sizeof(uint64_t);
  293. }
  294. else
  295. {
  296. params->ka[i].is_signer = params->ka[dup_info].is_signer;
  297. params->ka[i].is_writable = params->ka[dup_info].is_writable;
  298. params->ka[i].executable = params->ka[dup_info].executable;
  299. params->ka[i].key = params->ka[dup_info].key;
  300. params->ka[i].owner = params->ka[dup_info].owner;
  301. params->ka[i].lamports = params->ka[dup_info].lamports;
  302. params->ka[i].data_len = params->ka[dup_info].data_len;
  303. params->ka[i].data = params->ka[dup_info].data;
  304. params->ka[i].rent_epoch = params->ka[dup_info].rent_epoch;
  305. input += 7; // padding
  306. }
  307. }
  308. uint64_t data_len = *(uint64_t *)input;
  309. input += sizeof(uint64_t);
  310. if (data_len < SIZE_PUBKEY + sizeof(uint32_t))
  311. {
  312. return ERROR_INVALID_INSTRUCTION_DATA;
  313. }
  314. params->account_id = (SolPubkey *)input;
  315. input += SIZE_PUBKEY;
  316. data_len -= SIZE_PUBKEY;
  317. params->contract = *(uint32_t *)input;
  318. input += sizeof(uint32_t);
  319. data_len -= sizeof(uint32_t);
  320. params->input_len = data_len;
  321. params->input = input;
  322. input += data_len;
  323. params->program_id = (SolPubkey *)input;
  324. input += sizeof(SolPubkey);
  325. return 0;
  326. }
  327. /**
  328. * Byte array pointer and string
  329. */
  330. typedef struct
  331. {
  332. const uint8_t *addr; /** bytes */
  333. uint64_t len; /** number of bytes*/
  334. } SolBytes;
  335. /**
  336. * Length of a sha256 hash result
  337. */
  338. #define SHA256_RESULT_LENGTH 32
  339. /**
  340. * Sha256
  341. *
  342. * @param bytes Array of byte arrays
  343. * @param bytes_len Number of byte arrays
  344. * @param result 32 byte array to hold the result
  345. */
  346. static uint64_t sol_sha256(
  347. const SolBytes *bytes,
  348. int bytes_len,
  349. const uint8_t *result);
  350. /**
  351. * Account Meta
  352. */
  353. typedef struct
  354. {
  355. SolPubkey *pubkey; /** An account's public key */
  356. bool is_writable; /** True if the `pubkey` can be loaded as a read-write account */
  357. bool is_signer; /** True if an Instruction requires a Transaction signature matching `pubkey` */
  358. } SolAccountMeta;
  359. /**
  360. * Instruction
  361. */
  362. typedef struct
  363. {
  364. SolPubkey *program_id; /** Pubkey of the instruction processor that executes this instruction */
  365. SolAccountMeta *accounts; /** Metadata for what accounts should be passed to the instruction processor */
  366. uint64_t account_len; /** Number of SolAccountMetas */
  367. uint8_t *data; /** Opaque data passed to the instruction processor */
  368. uint64_t data_len; /** Length of the data in bytes */
  369. } SolInstruction;
  370. /**
  371. * Seed used to create a program address or passed to sol_invoke_signed
  372. */
  373. typedef struct
  374. {
  375. const uint8_t *addr; /** Seed bytes */
  376. uint64_t len; /** Length of the seed bytes */
  377. } SolSignerSeed;
  378. /**
  379. * Seeds used by a signer to create a program address or passed to
  380. * sol_invoke_signed
  381. */
  382. typedef struct
  383. {
  384. const SolSignerSeed *addr; /** An arry of a signer's seeds */
  385. uint64_t len; /** Number of seeds */
  386. } SolSignerSeeds;
  387. /**
  388. * Create a program address
  389. *
  390. * @param seeds Seed bytes used to sign program accounts
  391. * @param seeds_len Length of the seeds array
  392. * @param Progam id of the signer
  393. * @param Program address created, filled on return
  394. */
  395. static uint64_t sol_create_program_address(
  396. const SolSignerSeed *seeds,
  397. int seeds_len,
  398. const SolPubkey *program_id,
  399. const SolPubkey *address);
  400. /**
  401. * Cross-program invocation
  402. * * @{
  403. */
  404. /**
  405. * Invoke another program and sign for some of the keys
  406. *
  407. * @param instruction Instruction to process
  408. * @param account_infos Accounts used by instruction
  409. * @param account_infos_len Length of account_infos array
  410. * @param seeds Seed bytes used to sign program accounts
  411. * @param seeds_len Length of the seeds array
  412. */
  413. static uint64_t sol_invoke_signed(
  414. const SolInstruction *instruction,
  415. const SolAccountInfo *account_infos,
  416. int account_infos_len,
  417. const SolSignerSeeds *signers_seeds,
  418. int signers_seeds_len)
  419. {
  420. uint64_t sol_invoke_signed_c(
  421. const SolInstruction *instruction,
  422. const SolAccountInfo *account_infos,
  423. int account_infos_len,
  424. const SolSignerSeeds *signers_seeds,
  425. int signers_seeds_len);
  426. return sol_invoke_signed_c(
  427. instruction,
  428. account_infos,
  429. account_infos_len,
  430. signers_seeds,
  431. signers_seeds_len);
  432. }
  433. /**
  434. * Invoke another program
  435. *
  436. * @param instruction Instruction to process
  437. * @param account_infos Accounts used by instruction
  438. * @param account_infos_len Length of account_infos array
  439. */
  440. static uint64_t sol_invoke(
  441. const SolInstruction *instruction,
  442. const SolAccountInfo *account_infos,
  443. int account_infos_len)
  444. {
  445. const SolSignerSeeds signers_seeds[] = {{}};
  446. return sol_invoke_signed(
  447. instruction,
  448. account_infos,
  449. account_infos_len,
  450. signers_seeds,
  451. 0);
  452. }
  453. /**@}*/
  454. /**
  455. * Debugging utilities
  456. * @{
  457. */
  458. /**
  459. * Prints the hexadecimal representation of a public key
  460. *
  461. * @param key The public key to print
  462. */
  463. void sol_log_pubkey(
  464. const SolPubkey *pubkey);
  465. /**
  466. * Prints the hexadecimal representation of an array
  467. *
  468. * @param array The array to print
  469. */
  470. static void sol_log_array(const uint8_t *array, int len)
  471. {
  472. for (int j = 0; j < len; j++)
  473. {
  474. sol_log_64(0, 0, 0, j, array[j]);
  475. }
  476. }
  477. /**
  478. * Prints the program's input parameters
  479. *
  480. * @param params Pointer to a SolParameters structure
  481. */
  482. static void sol_log_params(const SolParameters *params)
  483. {
  484. sol_log("- Program identifier:");
  485. sol_log_pubkey(params->program_id);
  486. sol_log("- Number of KeyedAccounts");
  487. sol_log_64(0, 0, 0, 0, params->ka_num);
  488. for (int i = 0; i < params->ka_num; i++)
  489. {
  490. sol_log(" - Is signer");
  491. sol_log_64(0, 0, 0, 0, params->ka[i].is_signer);
  492. sol_log(" - Is writable");
  493. sol_log_64(0, 0, 0, 0, params->ka[i].is_writable);
  494. sol_log(" - Key");
  495. sol_log_pubkey(params->ka[i].key);
  496. sol_log(" - Lamports");
  497. sol_log_64(0, 0, 0, 0, *params->ka[i].lamports);
  498. sol_log(" - data");
  499. sol_log_array(params->ka[i].data, params->ka[i].data_len);
  500. sol_log(" - Owner");
  501. sol_log_pubkey(params->ka[i].owner);
  502. sol_log(" - Executable");
  503. sol_log_64(0, 0, 0, 0, params->ka[i].executable);
  504. sol_log(" - Rent Epoch");
  505. sol_log_64(0, 0, 0, 0, params->ka[i].rent_epoch);
  506. }
  507. sol_log("- Eth abi Instruction data\0");
  508. sol_log_pubkey(params->account_id);
  509. sol_log_array(params->input, params->input_len);
  510. }
  511. /**@}*/
  512. /**
  513. * Program instruction entrypoint
  514. *
  515. * @param input Buffer of serialized input parameters. Use sol_deserialize() to decode
  516. * @return 0 if the instruction executed successfully
  517. */
  518. uint64_t entrypoint(const uint8_t *input);
  519. #ifdef SOL_TEST
  520. /**
  521. * Stub log functions when building tests
  522. */
  523. #include <stdio.h>
  524. void sol_log_(const char *s, uint64_t len)
  525. {
  526. printf("sol_log: %s\n", s);
  527. }
  528. void sol_log_64(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5)
  529. {
  530. printf("sol_log_64: %llu, %llu, %llu, %llu, %llu\n", arg1, arg2, arg3, arg4, arg5);
  531. }
  532. #endif
  533. #ifdef __cplusplus
  534. }
  535. #endif
  536. /**@}*/