Guest User

Untitled

a guest
Sep 3rd, 2021
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.20 KB | None | 0 0
  1. #pragma once
  2. /**
  3.  * @brief Solana C-based BPF program utility functions and types
  4.  */
  5.  
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9.  
  10. /**
  11.  * Pick up static_assert if C11 or greater
  12.  *
  13.  * Inlined here until <assert.h> is available
  14.  */
  15. #if (defined _ISOC11_SOURCE || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)) && !defined (__cplusplus)
  16. #undef static_assert
  17. #define static_assert _Static_assert
  18. #endif
  19.  
  20. /**
  21.  * Numeric types
  22.  */
  23. #ifndef __LP64__
  24. #error LP64 data model required
  25. #endif
  26.  
  27. typedef signed char int8_t;
  28. typedef unsigned char uint8_t;
  29. typedef signed short int16_t;
  30. typedef unsigned short uint16_t;
  31. typedef signed int int32_t;
  32. typedef unsigned int uint32_t;
  33. typedef signed long int int64_t;
  34. typedef unsigned long int uint64_t;
  35. typedef int64_t ssize_t;
  36. typedef uint64_t size_t;
  37.  
  38. #if defined (__cplusplus) || defined(static_assert)
  39. static_assert(sizeof(int8_t) == 1);
  40. static_assert(sizeof(uint8_t) == 1);
  41. static_assert(sizeof(int16_t) == 2);
  42. static_assert(sizeof(uint16_t) == 2);
  43. static_assert(sizeof(int32_t) == 4);
  44. static_assert(sizeof(uint32_t) == 4);
  45. static_assert(sizeof(int64_t) == 8);
  46. static_assert(sizeof(uint64_t) == 8);
  47. #endif
  48.  
  49. /**
  50.  * Minimum of signed integral types
  51.  */
  52. #define INT8_MIN   (-128)
  53. #define INT16_MIN  (-32767-1)
  54. #define INT32_MIN  (-2147483647-1)
  55. #define INT64_MIN  (-9223372036854775807L-1)
  56.  
  57. /**
  58.  * Maximum of signed integral types
  59.  */
  60. #define INT8_MAX   (127)
  61. #define INT16_MAX  (32767)
  62. #define INT32_MAX  (2147483647)
  63. #define INT64_MAX  (9223372036854775807L)
  64.  
  65. /**
  66.  * Maximum of unsigned integral types
  67.  */
  68. #define UINT8_MAX   (255)
  69. #define UINT16_MAX  (65535)
  70. #define UINT32_MAX  (4294967295U)
  71. #define UINT64_MAX  (18446744073709551615UL)
  72.  
  73. /**
  74.  * NULL
  75.  */
  76. #define NULL 0
  77.  
  78. /** Indicates the instruction was processed successfully */
  79. #define SUCCESS 0
  80.  
  81. /**
  82.  * Builtin program status values occupy the upper 32 bits of the program return
  83.  * value.  Programs may define their own error values but they must be confined
  84.  * to the lower 32 bits.
  85.  */
  86. #define TO_BUILTIN(error) ((uint64_t)(error) << 32)
  87.  
  88. /** Note: Not applicable to program written in C */
  89. #define ERROR_CUSTOM_ZERO TO_BUILTIN(1)
  90. /** The arguments provided to a program instruction where invalid */
  91. #define ERROR_INVALID_ARGUMENT TO_BUILTIN(2)
  92. /** An instruction's data contents was invalid */
  93. #define ERROR_INVALID_INSTRUCTION_DATA TO_BUILTIN(3)
  94. /** An account's data contents was invalid */
  95. #define ERROR_INVALID_ACCOUNT_DATA TO_BUILTIN(4)
  96. /** An account's data was too small */
  97. #define ERROR_ACCOUNT_DATA_TOO_SMALL TO_BUILTIN(5)
  98. /** An account's balance was too small to complete the instruction */
  99. #define ERROR_INSUFFICIENT_FUNDS TO_BUILTIN(6)
  100. /** The account did not have the expected program id */
  101. #define ERROR_INCORRECT_PROGRAM_ID TO_BUILTIN(7)
  102. /** A signature was required but not found */
  103. #define ERROR_MISSING_REQUIRED_SIGNATURES TO_BUILTIN(8)
  104. /** An initialize instruction was sent to an account that has already been initialized */
  105. #define ERROR_ACCOUNT_ALREADY_INITIALIZED TO_BUILTIN(9)
  106. /** An attempt to operate on an account that hasn't been initialized */
  107. #define ERROR_UNINITIALIZED_ACCOUNT TO_BUILTIN(10)
  108. /** The instruction expected additional account keys */
  109. #define ERROR_NOT_ENOUGH_ACCOUNT_KEYS TO_BUILTIN(11)
  110. /** Note: Not applicable to program written in C */
  111. #define ERROR_ACCOUNT_BORROW_FAILED TO_BUILTIN(12)
  112. /** The length of the seed is too long for address generation */
  113. #define MAX_SEED_LENGTH_EXCEEDED TO_BUILTIN(13)
  114. /** Provided seeds do not result in a valid address */
  115. #define INVALID_SEEDS TO_BUILTIN(14)
  116.  
  117. /**
  118.  * Boolean type
  119.  */
  120. #ifndef __cplusplus
  121. #include <stdbool.h>
  122. #endif
  123.  
  124. /**
  125.  * Prints a string to stdout
  126.  */
  127. void sol_log_(const char *, uint64_t);
  128. #define sol_log(message) sol_log_(message, sol_strlen(message))
  129.  
  130. /**
  131.  * Prints a 64 bit values represented in hexadecimal to stdout
  132.  */
  133. void sol_log_64_(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
  134. #define sol_log_64 sol_log_64_
  135.  
  136. /**
  137.  * Prints the current compute unit consumption to stdout
  138.  */
  139. void sol_log_compute_units_();
  140. #define sol_log_compute_units() sol_log_compute_units_()
  141.  
  142. /**
  143.  * Size of Public key in bytes
  144.  */
  145. #define SIZE_PUBKEY 32
  146.  
  147. /**
  148.  * Public key
  149.  */
  150. typedef struct {
  151.   uint8_t x[SIZE_PUBKEY];
  152. } SolPubkey;
  153.  
  154. /**
  155.  * Compares two public keys
  156.  *
  157.  * @param one First public key
  158.  * @param two Second public key
  159.  * @return true if the same
  160.  */
  161. static bool SolPubkey_same(const SolPubkey *one, const SolPubkey *two) {
  162.   for (int i = 0; i < sizeof(*one); i++) {
  163.     if (one->x[i] != two->x[i]) {
  164.       return false;
  165.     }
  166.   }
  167.   return true;
  168. }
  169.  
  170. /**
  171.  * Keyed Account
  172.  */
  173. typedef struct {
  174.   SolPubkey *key;      /** Public key of the account */
  175.   uint64_t *lamports;  /** Number of lamports owned by this account */
  176.   uint64_t data_len;   /** Length of data in bytes */
  177.   uint8_t *data;       /** On-chain data within this account */
  178.   SolPubkey *owner;    /** Program that owns this account */
  179.   uint64_t rent_epoch; /** The epoch at which this account will next owe rent */
  180.   bool is_signer;      /** Transaction was signed by this account's key? */
  181.   bool is_writable;    /** Is the account writable? */
  182.   bool executable;     /** This account's data contains a loaded program (and is now read-only) */
  183. } SolAccountInfo;
  184.  
  185. /**
  186.  * Copies memory
  187.  */
  188. static void sol_memcpy(void *dst, const void *src, int len) {
  189.   for (int i = 0; i < len; i++) {
  190.     *((uint8_t *)dst + i) = *((const uint8_t *)src + i);
  191.   }
  192. }
  193.  
  194. /**
  195.  * Compares memory
  196.  */
  197. static int sol_memcmp(const void *s1, const void *s2, int n) {
  198.   for (int i = 0; i < n; i++) {
  199.     uint8_t diff = *((uint8_t *)s1 + i) - *((const uint8_t *)s2 + i);
  200.     if (diff) {
  201.       return diff;
  202.     }
  203.   }
  204.   return 0;
  205. }
  206.  
  207. /**
  208.  * Fill a byte string with a byte value
  209.  */
  210. static void *sol_memset(void *b, int c, size_t len) {
  211.   uint8_t *a = (uint8_t *) b;
  212.   while (len > 0) {
  213.     *a = c;
  214.     a++;
  215.     len--;
  216.   }
  217. }
  218.  
  219. /**
  220.  * Find length of string
  221.  */
  222. static size_t sol_strlen(const char *s) {
  223.   size_t len = 0;
  224.   while (*s) {
  225.     len++;
  226.     s++;
  227.   }
  228.   return len;
  229. }
  230.  
  231. /**
  232.  * Computes the number of elements in an array
  233.  */
  234. #define SOL_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  235.  
  236.  
  237. /**
  238.  * Internal memory alloc/free function
  239.  */
  240. void *sol_alloc_free_(uint64_t size, void *ptr);
  241.  
  242. /**
  243.  * Alloc zero-initialized memory
  244.  */
  245. static void *sol_calloc(size_t nitems, size_t size) {
  246.   return sol_alloc_free_(nitems * size, 0);
  247. }
  248.  
  249. /**
  250.  * Deallocates the memory previously allocated by sol_calloc
  251.  */
  252. static void sol_free(void *ptr) {
  253.   (void) sol_alloc_free_(0, ptr);
  254. }
  255.  
  256. /**
  257.  * The Solana runtime provides a memory region that is available to programs at
  258.  * a fixed virtual address and length. The builtin functions `sol_calloc` and
  259.  * `sol_free` call into the Solana runtime to allocate from this memory region
  260.  * for heap operations.  Because the memory region is directly available to
  261.  * programs another option is a program can implement their own heap directly on
  262.  * top of that region.  If a program chooses to implement their own heap they
  263.  * should not call the builtin heap functions because they will conflict.
  264.  * `HEAP_START_ADDRESS` and `HEAP_LENGTH` specify the memory region's start
  265.  * virtual address and length.
  266.  */
  267. #define HEAP_START_ADDRESS (uint64_t)0x300000000
  268. #define HEAP_LENGTH (uint64_t)(32 * 1024)
  269.  
  270. /**
  271.  * Panics
  272.  *
  273.  * Prints the line number where the panic occurred and then causes
  274.  * the BPF VM to immediately halt execution. No accounts' data are updated
  275.  */
  276. void sol_panic_(const char *, uint64_t, uint64_t, uint64_t);
  277. #define sol_panic() sol_panic_(__FILE__, sizeof(__FILE__), __LINE__, 0)
  278.  
  279. /**
  280.  * Asserts
  281.  */
  282. #define sol_assert(expr)  \
  283. if (!(expr)) {          \
  284.   sol_panic(); \
  285. }
  286.  
  287. /**
  288.  * Structure that the program's entrypoint input data is deserialized into.
  289.  */
  290. typedef struct {
  291.   SolAccountInfo* ka; /** Pointer to an array of SolAccountInfo, must already
  292.                           point to an array of SolAccountInfos */
  293.   uint64_t ka_num; /** Number of SolAccountInfo entries in `ka` */
  294.   const uint8_t *data; /** pointer to the instruction data */
  295.   uint64_t data_len; /** Length in bytes of the instruction data */
  296.   const SolPubkey *program_id; /** program_id of the currently executing program */
  297. } SolParameters;
  298.  
  299. /**
  300.  * Maximum number of bytes a program may add to an account during a single realloc
  301.  */
  302. #define MAX_PERMITTED_DATA_INCREASE (1024 * 10)
  303.  
  304. /**
  305.  * De-serializes the input parameters into usable types
  306.  *
  307.  * Use this function to deserialize the buffer passed to the program entrypoint
  308.  * into usable types.  This function does not perform copy deserialization,
  309.  * instead it populates the pointers and lengths in SolAccountInfo and data so
  310.  * that any modification to lamports or account data take place on the original
  311.  * buffer.  Doing so also eliminates the need to serialize back into the buffer
  312.  * at the end of the program.
  313.  *
  314.  * @param input Source buffer containing serialized input parameters
  315.  * @param params Pointer to a SolParameters structure
  316.  * @return Boolean true if successful.
  317.  */
  318. static bool sol_deserialize(
  319.   const uint8_t *input,
  320.   SolParameters *params,
  321.   uint64_t ka_num
  322. ) {
  323.   if (NULL == input || NULL == params) {
  324.     return false;
  325.   }
  326.   params->ka_num = *(uint64_t *) input;
  327.   input += sizeof(uint64_t);
  328.  
  329.   for (int i = 0; i < params->ka_num; i++) {
  330.     uint8_t dup_info = input[0];
  331.     input += sizeof(uint8_t);
  332.  
  333.     if (i >= ka_num) {
  334.       if (dup_info == UINT8_MAX) {
  335.         input += sizeof(uint8_t);
  336.         input += sizeof(uint8_t);
  337.         input += sizeof(uint8_t);
  338.         input += 4; // padding
  339.         input += sizeof(SolPubkey);
  340.         input += sizeof(SolPubkey);
  341.         input += sizeof(uint64_t);
  342.         uint64_t data_len = *(uint64_t *) input;
  343.         input += sizeof(uint64_t);
  344.         input += data_len;
  345.         input += MAX_PERMITTED_DATA_INCREASE;
  346.         input = (uint8_t*)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
  347.         input += sizeof(uint64_t);
  348.       } else {
  349.         input += 7; // padding
  350.       }
  351.       continue;
  352.     }
  353.     if (dup_info == UINT8_MAX) {
  354.       // is signer?
  355.       params->ka[i].is_signer = *(uint8_t *) input != 0;
  356.       input += sizeof(uint8_t);
  357.  
  358.       // is writable?
  359.       params->ka[i].is_writable = *(uint8_t *) input != 0;
  360.       input += sizeof(uint8_t);
  361.  
  362.       // executable?
  363.       params->ka[i].executable = *(uint8_t *) input;
  364.       input += sizeof(uint8_t);
  365.  
  366.       input += 4; // padding
  367.  
  368.       // key
  369.       params->ka[i].key = (SolPubkey *) input;
  370.       input += sizeof(SolPubkey);
  371.  
  372.       // owner
  373.       params->ka[i].owner = (SolPubkey *) input;
  374.       input += sizeof(SolPubkey);
  375.  
  376.       // lamports
  377.       params->ka[i].lamports = (uint64_t *) input;
  378.       input += sizeof(uint64_t);
  379.  
  380.       // account data
  381.       params->ka[i].data_len = *(uint64_t *) input;
  382.       input += sizeof(uint64_t);
  383.       params->ka[i].data = (uint8_t *) input;
  384.       input += params->ka[i].data_len;
  385.       input += MAX_PERMITTED_DATA_INCREASE;
  386.       input = (uint8_t*)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
  387.  
  388.       // rent epoch
  389.       params->ka[i].rent_epoch = *(uint64_t *) input;
  390.       input += sizeof(uint64_t);
  391.     } else {
  392.       params->ka[i].is_signer = params->ka[dup_info].is_signer;
  393.       params->ka[i].is_writable = params->ka[dup_info].is_writable;
  394.       params->ka[i].executable = params->ka[dup_info].executable;
  395.       params->ka[i].key = params->ka[dup_info].key;
  396.       params->ka[i].owner = params->ka[dup_info].owner;
  397.       params->ka[i].lamports = params->ka[dup_info].lamports;
  398.       params->ka[i].data_len = params->ka[dup_info].data_len;
  399.       params->ka[i].data = params->ka[dup_info].data;
  400.       params->ka[i].rent_epoch = params->ka[dup_info].rent_epoch;
  401.       input += 7; // padding
  402.     }
  403.   }
  404.  
  405.   params->data_len = *(uint64_t *) input;
  406.   input += sizeof(uint64_t);
  407.   params->data = input;
  408.   input += params->data_len;
  409.  
  410.   params->program_id = (SolPubkey *) input;
  411.   input += sizeof(SolPubkey);
  412.  
  413.   return true;
  414. }
  415.  
  416. /**
  417.  * Byte array pointer and string
  418.  */
  419. typedef struct {
  420.   const uint8_t *addr; /** bytes */
  421.   uint64_t len; /** number of bytes*/
  422. } SolBytes;
  423.  
  424. /**
  425.  * Length of a sha256 hash result
  426.  */
  427. #define SHA256_RESULT_LENGTH 32
  428.  
  429. /**
  430.  * Sha256
  431.  *
  432.  * @param bytes Array of byte arrays
  433.  * @param bytes_len Number of byte arrays
  434.  * @param result 32 byte array to hold the result
  435.  */
  436. uint64_t sol_sha256(
  437.     const SolBytes *bytes,
  438.     int bytes_len,
  439.     uint8_t *result
  440. );
  441.  
  442. /**
  443.  * Length of a Keccak hash result
  444.  */
  445. #define KECCAK_RESULT_LENGTH 32
  446.  
  447. /**
  448.  * Keccak
  449.  *
  450.  * @param bytes Array of byte arrays
  451.  * @param bytes_len Number of byte arrays
  452.  * @param result 32 byte array to hold the result
  453.  */
  454. uint64_t sol_keccak256(
  455.     const SolBytes *bytes,
  456.     int bytes_len,
  457.     uint8_t *result
  458. );
  459.  
  460.  
  461. /** Length of a secp256k1 recover input hash */
  462. #define SECP256K1_RECOVER_HASH_LENGTH 32
  463. /** Length of a secp256k1 input signature */
  464. #define SECP256K1_RECOVER_SIGNATURE_LENGTH 64
  465. /** Length of a secp256k1 recover result */
  466. #define SECP256K1_RECOVER_RESULT_LENGTH 64
  467.  
  468. /** The hash provided to a sol_secp256k1_recover is invalid */
  469. #define SECP256K1_RECOVER_ERROR_INVALID_HASH 1
  470. /** The recovery_id provided to a sol_secp256k1_recover is invalid */
  471. #define SECP256K1_RECOVER_ERROR_INVALID_RECOVERY_ID 2
  472. /** The signature provided to a sol_secp256k1_recover is invalid */
  473. #define SECP256K1_RECOVER_ERROR_INVALID_SIGNATURE 3
  474.  
  475. /**
  476.  * Recover public key from a signed message.
  477.  *
  478.  * @param hash Hashed message
  479.  * @param recovery_id Tag used for public key recovery from signatures. Can be 0 or 1
  480.  * @param signature An ECDSA signature
  481.  * @param result 64 byte array to hold the result. A recovered public key
  482.  * @return 0 if executed successfully
  483.  */
  484. uint64_t sol_secp256k1_recover(
  485.     const uint8_t *hash,
  486.     uint64_t recovery_id,
  487.     const uint8_t *signature,
  488.     uint8_t *result
  489. );
  490.  
  491. /**
  492.  * Account Meta
  493.  */
  494. typedef struct {
  495.   SolPubkey *pubkey; /** An account's public key */
  496.   bool is_writable; /** True if the `pubkey` can be loaded as a read-write account */
  497.   bool is_signer; /** True if an Instruction requires a Transaction signature matching `pubkey` */
  498. } SolAccountMeta;
  499.  
  500. /**
  501.  * Instruction
  502.  */
  503. typedef struct {
  504.   SolPubkey *program_id; /** Pubkey of the instruction processor that executes this instruction */
  505.   SolAccountMeta *accounts; /** Metadata for what accounts should be passed to the instruction processor */
  506.   uint64_t account_len; /** Number of SolAccountMetas */
  507.   uint8_t *data; /** Opaque data passed to the instruction processor */
  508.   uint64_t data_len; /** Length of the data in bytes */
  509. } SolInstruction;
  510.  
  511. /**
  512.  * Seed used to create a program address or passed to sol_invoke_signed
  513.  */
  514. typedef struct {
  515.   const uint8_t *addr; /** Seed bytes */
  516.   uint64_t len; /** Length of the seed bytes */
  517. } SolSignerSeed;
  518.  
  519. /**
  520.  * Seeds used by a signer to create a program address or passed to
  521.  * sol_invoke_signed
  522.  */
  523. typedef struct {
  524.   const SolSignerSeed *addr; /** An arry of a signer's seeds */
  525.   uint64_t len; /** Number of seeds */
  526. } SolSignerSeeds;
  527.  
  528. /**
  529.  * Create a program address
  530.  *
  531.  * @param seeds Seed bytes used to sign program accounts
  532.  * @param seeds_len Length of the seeds array
  533.  * @param program_id Program id of the signer
  534.  * @param program_address Program address created, filled on return
  535.  */
  536. uint64_t sol_create_program_address(
  537.     const SolSignerSeed *seeds,
  538.     int seeds_len,
  539.     const SolPubkey *program_id,
  540.     SolPubkey *program_address
  541. );
  542.  
  543. /**
  544.  * Try to find a program address and return corresponding bump seed
  545.  *
  546.  * @param seeds Seed bytes used to sign program accounts
  547.  * @param seeds_len Length of the seeds array
  548.  * @param program_id Program id of the signer
  549.  * @param program_address Program address created, filled on return
  550.  * @param bump_seed Bump seed required to create a valid program address
  551.  */
  552. uint64_t sol_try_find_program_address(
  553.     const SolSignerSeed *seeds,
  554.     int seeds_len,
  555.     const SolPubkey *program_id,
  556.     SolPubkey *program_address,
  557.     uint8_t *bump_seed
  558. );
  559.  
  560. /**
  561.  * Cross-program invocation
  562.  *  * @{
  563.  */
  564.  
  565. /**
  566.  * Internal cross-program invocation function
  567.  */
  568. uint64_t sol_invoke_signed_c(
  569.   const SolInstruction *instruction,
  570.   const SolAccountInfo *account_infos,
  571.   int account_infos_len,
  572.   const SolSignerSeeds *signers_seeds,
  573.   int signers_seeds_len
  574. );
  575.  
  576. /**
  577.  * Invoke another program and sign for some of the keys
  578.  *
  579.  * @param instruction Instruction to process
  580.  * @param account_infos Accounts used by instruction
  581.  * @param account_infos_len Length of account_infos array
  582.  * @param seeds Seed bytes used to sign program accounts
  583.  * @param seeds_len Length of the seeds array
  584.  */
  585. static uint64_t sol_invoke_signed(
  586.     const SolInstruction *instruction,
  587.     const SolAccountInfo *account_infos,
  588.     int account_infos_len,
  589.     const SolSignerSeeds *signers_seeds,
  590.     int signers_seeds_len
  591. ) {
  592.   return sol_invoke_signed_c(
  593.     instruction,
  594.     account_infos,
  595.     account_infos_len,
  596.     signers_seeds,
  597.     signers_seeds_len
  598.   );
  599. }
  600. /**
  601.  * Invoke another program
  602.  *
  603.  * @param instruction Instruction to process
  604.  * @param account_infos Accounts used by instruction
  605.  * @param account_infos_len Length of account_infos array
  606. */
  607. static uint64_t sol_invoke(
  608.     const SolInstruction *instruction,
  609.     const SolAccountInfo *account_infos,
  610.     int account_infos_len
  611. ) {
  612.   const SolSignerSeeds signers_seeds[] = {{}};
  613.   return sol_invoke_signed(
  614.     instruction,
  615.     account_infos,
  616.     account_infos_len,
  617.     signers_seeds,
  618.     0
  619.   );
  620. }
  621.  
  622. /**@}*/
  623.  
  624. /**
  625.  * Debugging utilities
  626.  * @{
  627.  */
  628.  
  629. /**
  630.  * Prints the hexadecimal representation of a public key
  631.  *
  632.  * @param key The public key to print
  633.  */
  634. void sol_log_pubkey(
  635.     const SolPubkey *pubkey
  636. );
  637.  
  638. /**
  639.  * Prints the hexadecimal representation of an array
  640.  *
  641.  * @param array The array to print
  642.  */
  643. static void sol_log_array(const uint8_t *array, int len) {
  644.   for (int j = 0; j < len; j++) {
  645.     sol_log_64(0, 0, 0, j, array[j]);
  646.   }
  647. }
  648.  
  649. /**
  650.  * Prints the program's input parameters
  651.  *
  652.  * @param params Pointer to a SolParameters structure
  653.  */
  654. static void sol_log_params(const SolParameters *params) {
  655.   sol_log("- Program identifier:");
  656.   sol_log_pubkey(params->program_id);
  657.  
  658.   sol_log("- Number of KeyedAccounts");
  659.   sol_log_64(0, 0, 0, 0, params->ka_num);
  660.   for (int i = 0; i < params->ka_num; i++) {
  661.     sol_log("  - Is signer");
  662.     sol_log_64(0, 0, 0, 0, params->ka[i].is_signer);
  663.     sol_log("  - Is writable");
  664.     sol_log_64(0, 0, 0, 0, params->ka[i].is_writable);
  665.     sol_log("  - Key");
  666.     sol_log_pubkey(params->ka[i].key);
  667.     sol_log("  - Lamports");
  668.     sol_log_64(0, 0, 0, 0, *params->ka[i].lamports);
  669.     sol_log("  - data");
  670.     sol_log_array(params->ka[i].data, params->ka[i].data_len);
  671.     sol_log("  - Owner");
  672.     sol_log_pubkey(params->ka[i].owner);
  673.     sol_log("  - Executable");
  674.     sol_log_64(0, 0, 0, 0, params->ka[i].executable);
  675.     sol_log("  - Rent Epoch");
  676.     sol_log_64(0, 0, 0, 0, params->ka[i].rent_epoch);
  677.   }
  678.   sol_log("- Instruction data\0");
  679.   sol_log_array(params->data, params->data_len);
  680. }
  681.  
  682. /**@}*/
  683.  
  684. /**
  685.  * Program instruction entrypoint
  686.  *
  687.  * @param input Buffer of serialized input parameters.  Use sol_deserialize() to decode
  688.  * @return 0 if the instruction executed successfully
  689.  */
  690. uint64_t entrypoint(const uint8_t *input);
  691.  
  692. #ifdef SOL_TEST
  693. /**
  694.  * Stub functions when building tests
  695.  */
  696. #include <stdio.h>
  697. void sol_log_(const char *s, uint64_t len) {
  698.   printf("Program log: %s\n", s);
  699. }
  700. void sol_log_64(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) {
  701.   printf("Program log: %llu, %llu, %llu, %llu, %llu\n", arg1, arg2, arg3, arg4, arg5);
  702. }
  703. void sol_log_pubkey(const SolPubkey *pubkey) {
  704.   printf("Program log: ");
  705.   for (int i = 0; i < SIZE_PUBKEY; i++) {
  706.     printf("%02 ", pubkey->x[i]);
  707.   }
  708.   printf("\n");
  709. }
  710. void sol_log_compute_units_() {
  711.   printf("Program consumption: __ units remaining\n");
  712. }
  713. void sol_panic_(const char *file, uint64_t len, uint64_t line, uint64_t column) {
  714.   printf("Panic in %s at %d:%d\n", file, line, column);
  715.   abort();
  716. }
  717. #endif
  718.  
  719. #ifdef __cplusplus
  720. }
  721. #endif
  722.  
  723. /**@}*/
  724.  
Advertisement
Add Comment
Please, Sign In to add comment