solana_sdk.h 16 KB

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