| 
									
										
										
										
											2018-10-29 20:10:40 -07:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief Solana C-based BPF program utility functions and types | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-29 20:12:04 -07:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 09:54:27 -08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Pick up static_assert if C11 or greater | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Inlined here until <assert.h> is available | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #if (defined _ISOC11_SOURCE || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)) && !defined (__cplusplus)
 | 
					
						
							|  |  |  | #undef static_assert
 | 
					
						
							|  |  |  | #define static_assert _Static_assert
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2018-10-29 20:10:40 -07:00
										 |  |  |  * Numeric types | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-10-31 20:28:24 -07:00
										 |  |  | #ifndef __LP64__
 | 
					
						
							|  |  |  | #error LP64 data model required
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | typedef signed char int8_t; | 
					
						
							|  |  |  | typedef unsigned char uint8_t; | 
					
						
							| 
									
										
										
										
											2018-10-31 20:28:24 -07:00
										 |  |  | typedef signed short int16_t; | 
					
						
							|  |  |  | typedef unsigned short uint16_t; | 
					
						
							|  |  |  | typedef signed int int32_t; | 
					
						
							|  |  |  | typedef unsigned int uint32_t; | 
					
						
							|  |  |  | typedef signed long int int64_t; | 
					
						
							|  |  |  | typedef unsigned long int uint64_t; | 
					
						
							| 
									
										
										
										
											2018-11-13 12:39:59 -08:00
										 |  |  | typedef int64_t ssize_t; | 
					
						
							|  |  |  | typedef uint64_t size_t; | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 09:54:27 -08:00
										 |  |  | #if defined (__cplusplus) || defined(static_assert)
 | 
					
						
							|  |  |  | static_assert(sizeof(int8_t) == 1); | 
					
						
							|  |  |  | static_assert(sizeof(uint8_t) == 1); | 
					
						
							|  |  |  | static_assert(sizeof(int16_t) == 2); | 
					
						
							|  |  |  | static_assert(sizeof(uint16_t) == 2); | 
					
						
							|  |  |  | static_assert(sizeof(int32_t) == 4); | 
					
						
							|  |  |  | static_assert(sizeof(uint32_t) == 4); | 
					
						
							|  |  |  | static_assert(sizeof(int64_t) == 8); | 
					
						
							|  |  |  | static_assert(sizeof(uint64_t) == 8); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 10:03:28 -08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Minimum of signed integral types | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-15 16:42:20 -07:00
										 |  |  | #define INT8_MIN   (-128)
 | 
					
						
							|  |  |  | #define INT16_MIN  (-32767-1)
 | 
					
						
							|  |  |  | #define INT32_MIN  (-2147483647-1)
 | 
					
						
							|  |  |  | #define INT64_MIN  (-9223372036854775807L-1)
 | 
					
						
							| 
									
										
										
										
											2020-02-11 10:03:28 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Maximum of signed integral types | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-15 16:42:20 -07:00
										 |  |  | #define INT8_MAX   (127)
 | 
					
						
							|  |  |  | #define INT16_MAX  (32767)
 | 
					
						
							|  |  |  | #define INT32_MAX  (2147483647)
 | 
					
						
							|  |  |  | #define INT64_MAX  (9223372036854775807L)
 | 
					
						
							| 
									
										
										
										
											2020-02-11 10:03:28 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Maximum of unsigned integral types | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-15 16:42:20 -07:00
										 |  |  | #define UINT8_MAX   (255)
 | 
					
						
							|  |  |  | #define UINT16_MAX  (65535)
 | 
					
						
							|  |  |  | #define UINT32_MAX  (4294967295U)
 | 
					
						
							|  |  |  | #define UINT64_MAX  (18446744073709551615UL)
 | 
					
						
							| 
									
										
										
										
											2020-02-11 10:03:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 23:11:22 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * NULL | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define NULL 0
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | /** Indicates the instruction was processed successfully */ | 
					
						
							| 
									
										
										
										
											2019-09-06 16:05:01 -07:00
										 |  |  | #define SUCCESS 0
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  |  * Builtin program status values occupy the upper 32 bits of the program return | 
					
						
							|  |  |  |  * value.  Programs may define their own error values but they must be confined | 
					
						
							|  |  |  |  * to the lower 32 bits. | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define TO_BUILTIN(error) ((uint64_t)(error) << 32)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | /** Note: Not applicable to program written in C */ | 
					
						
							|  |  |  | #define ERROR_CUSTOM_ZERO TO_BUILTIN(1)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** The arguments provided to a program instruction where invalid */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_INVALID_ARGUMENT TO_BUILTIN(2)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** An instruction's data contents was invalid */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_INVALID_INSTRUCTION_DATA TO_BUILTIN(3)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** An account's data contents was invalid */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_INVALID_ACCOUNT_DATA TO_BUILTIN(4)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** An account's data was too small */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_ACCOUNT_DATA_TOO_SMALL TO_BUILTIN(5)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** An account's balance was too small to complete the instruction */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_INSUFFICIENT_FUNDS TO_BUILTIN(6)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** The account did not have the expected program id */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_INCORRECT_PROGRAM_ID TO_BUILTIN(7)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** A signature was required but not found */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_MISSING_REQUIRED_SIGNATURES TO_BUILTIN(8)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** An initialize instruction was sent to an account that has already been initialized */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_ACCOUNT_ALREADY_INITIALIZED TO_BUILTIN(9)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** An attempt to operate on an account that hasn't been initialized */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_UNINITIALIZED_ACCOUNT TO_BUILTIN(10)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** The instruction expected additional account keys */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_NOT_ENOUGH_ACCOUNT_KEYS TO_BUILTIN(11)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | /** Note: Not applicable to program written in C */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | #define ERROR_ACCOUNT_BORROW_FAILED TO_BUILTIN(12)
 | 
					
						
							| 
									
										
										
										
											2020-06-23 11:19:27 -07:00
										 |  |  | /** The length of the seed is too long for address generation */ | 
					
						
							|  |  |  | #define MAX_SEED_LENGTH_EXCEEDED TO_BUILTIN(13)
 | 
					
						
							| 
									
										
										
										
											2020-08-05 16:35:54 -07:00
										 |  |  | /** Provided seeds do not result in a valid address */ | 
					
						
							|  |  |  | #define INVALID_SEEDS TO_BUILTIN(14)
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:47:22 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Boolean type | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-11-25 16:58:32 -08:00
										 |  |  | #ifndef __cplusplus
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							| 
									
										
										
										
											2018-11-11 09:54:27 -08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-04-30 01:43:11 -07:00
										 |  |  |  * Prints a string to stdout | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-08-23 11:03:53 -07:00
										 |  |  | void sol_log_(const char *, uint64_t); | 
					
						
							|  |  |  | #define sol_log(message) sol_log_(message, sol_strlen(message))
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-04-30 01:43:11 -07:00
										 |  |  |  * Prints a 64 bit values represented in hexadecimal to stdout | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-04-30 11:29:11 -07:00
										 |  |  | void sol_log_64_(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t); | 
					
						
							|  |  |  | #define sol_log_64 sol_log_64_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-30 07:47:17 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Prints the current compute unit consumption to stdout | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void sol_log_compute_units_(); | 
					
						
							|  |  |  | #define sol_log_compute_units() sol_log_compute_units_()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Size of Public key in bytes | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define SIZE_PUBKEY 32
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Public key | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |   uint8_t x[SIZE_PUBKEY]; | 
					
						
							|  |  |  | } SolPubkey; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Compares two public keys | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param one First public key | 
					
						
							|  |  |  |  * @param two Second public key | 
					
						
							| 
									
										
										
										
											2018-10-29 20:10:40 -07:00
										 |  |  |  * @return true if the same | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static bool SolPubkey_same(const SolPubkey *one, const SolPubkey *two) { | 
					
						
							| 
									
										
										
										
											2018-10-31 23:11:22 -07:00
										 |  |  |   for (int i = 0; i < sizeof(*one); i++) { | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |     if (one->x[i] != two->x[i]) { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2018-11-27 15:15:56 -08:00
										 |  |  |  * Keyed Account | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2020-02-11 16:30:22 -08:00
										 |  |  |   SolPubkey *key;      /** Public key of the account */ | 
					
						
							|  |  |  |   uint64_t *lamports;  /** Number of lamports owned by this account */ | 
					
						
							|  |  |  |   uint64_t data_len;   /** Length of data in bytes */ | 
					
						
							|  |  |  |   uint8_t *data;       /** On-chain data within this account */ | 
					
						
							|  |  |  |   SolPubkey *owner;    /** Program that owns this account */ | 
					
						
							| 
									
										
										
										
											2020-03-04 10:52:09 -08:00
										 |  |  |   uint64_t rent_epoch; /** The epoch at which this account will next owe rent */ | 
					
						
							|  |  |  |   bool is_signer;      /** Transaction was signed by this account's key? */ | 
					
						
							|  |  |  |   bool is_writable;    /** Is the account writable? */ | 
					
						
							|  |  |  |   bool executable;     /** This account's data contains a loaded program (and is now read-only) */ | 
					
						
							| 
									
										
										
										
											2020-03-17 19:37:16 -07:00
										 |  |  | } SolAccountInfo; | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Copies memory | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static void sol_memcpy(void *dst, const void *src, int len) { | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |   for (int i = 0; i < len; i++) { | 
					
						
							| 
									
										
										
										
											2018-10-29 19:56:24 -07:00
										 |  |  |     *((uint8_t *)dst + i) = *((const uint8_t *)src + i); | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 23:11:22 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Compares memory | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static int sol_memcmp(const void *s1, const void *s2, int n) { | 
					
						
							| 
									
										
										
										
											2018-10-31 23:11:22 -07:00
										 |  |  |   for (int i = 0; i < n; i++) { | 
					
						
							|  |  |  |     uint8_t diff = *((uint8_t *)s1 + i) - *((const uint8_t *)s2 + i); | 
					
						
							|  |  |  |     if (diff) { | 
					
						
							|  |  |  |       return diff; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 12:39:59 -08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Fill a byte string with a byte value | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static void *sol_memset(void *b, int c, size_t len) { | 
					
						
							| 
									
										
										
										
											2018-11-13 12:39:59 -08:00
										 |  |  |   uint8_t *a = (uint8_t *) b; | 
					
						
							|  |  |  |   while (len > 0) { | 
					
						
							|  |  |  |     *a = c; | 
					
						
							|  |  |  |     a++; | 
					
						
							|  |  |  |     len--; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Find length of string | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static size_t sol_strlen(const char *s) { | 
					
						
							| 
									
										
										
										
											2018-11-13 12:39:59 -08:00
										 |  |  |   size_t len = 0; | 
					
						
							|  |  |  |   while (*s) { | 
					
						
							|  |  |  |     len++; | 
					
						
							|  |  |  |     s++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return len; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-31 23:11:22 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Computes the number of elements in an array | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define SOL_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 16:42:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Internal memory alloc/free function | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void *sol_alloc_free_(uint64_t size, void *ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Alloc zero-initialized memory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void *sol_calloc(size_t nitems, size_t size) { | 
					
						
							|  |  |  |   return sol_alloc_free_(nitems * size, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Deallocates the memory previously allocated by sol_calloc | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void sol_free(void *ptr) { | 
					
						
							|  |  |  |   (void) sol_alloc_free_(0, ptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-11 06:48:02 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * The Solana runtime provides a memory region that is available to programs at | 
					
						
							|  |  |  |  * a fixed virtual address and length. The builtin functions `sol_calloc` and | 
					
						
							|  |  |  |  * `sol_free` call into the Solana runtime to allocate from this memory region | 
					
						
							|  |  |  |  * for heap operations.  Because the memory region is directly available to | 
					
						
							|  |  |  |  * programs another option is a program can implement their own heap directly on | 
					
						
							|  |  |  |  * top of that region.  If a program chooses to implement their own heap they | 
					
						
							|  |  |  |  * should not call the builtin heap functions because they will conflict. | 
					
						
							|  |  |  |  * `HEAP_START_ADDRESS` and `HEAP_LENGTH` specify the memory region's start | 
					
						
							|  |  |  |  * virtual address and length. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define HEAP_START_ADDRESS (uint64_t)0x300000000
 | 
					
						
							|  |  |  | #define HEAP_LENGTH (uint64_t)(32 * 1024)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Panics | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Prints the line number where the panic occurred and then causes | 
					
						
							| 
									
										
										
										
											2020-02-11 16:30:22 -08:00
										 |  |  |  * the BPF VM to immediately halt execution. No accounts' data are updated | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-08-23 11:03:53 -07:00
										 |  |  | void sol_panic_(const char *, uint64_t, uint64_t, uint64_t); | 
					
						
							|  |  |  | #define sol_panic() sol_panic_(__FILE__, sizeof(__FILE__), __LINE__, 0)
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Asserts | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define sol_assert(expr)  \
 | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  | if (!(expr)) {          \ | 
					
						
							| 
									
										
										
										
											2019-06-20 19:10:03 -07:00
										 |  |  |   sol_panic(); \ | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-13 19:54:41 -08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |  * Structure that the program's entrypoint input data is deserialized into. | 
					
						
							| 
									
										
										
										
											2018-11-13 19:54:41 -08:00
										 |  |  |  */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2020-03-17 19:37:16 -07:00
										 |  |  |   SolAccountInfo* ka; /** Pointer to an array of SolAccountInfo, must already
 | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  |                           point to an array of SolAccountInfos */ | 
					
						
							| 
									
										
										
										
											2020-03-17 19:37:16 -07:00
										 |  |  |   uint64_t ka_num; /** Number of SolAccountInfo entries in `ka` */ | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   const uint8_t *data; /** pointer to the instruction data */ | 
					
						
							|  |  |  |   uint64_t data_len; /** Length in bytes of the instruction data */ | 
					
						
							| 
									
										
										
										
											2018-11-17 17:02:14 -08:00
										 |  |  |   const SolPubkey *program_id; /** program_id of the currently executing program */ | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  | } SolParameters; | 
					
						
							| 
									
										
										
										
											2018-11-13 19:54:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 13:38:42 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Maximum number of bytes a program may add to an account during a single realloc | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define MAX_PERMITTED_DATA_INCREASE (1024 * 10)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * De-serializes the input parameters into usable types | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use this function to deserialize the buffer passed to the program entrypoint | 
					
						
							|  |  |  |  * into usable types.  This function does not perform copy deserialization, | 
					
						
							| 
									
										
										
										
											2020-03-17 19:37:16 -07:00
										 |  |  |  * instead it populates the pointers and lengths in SolAccountInfo and data so | 
					
						
							| 
									
										
										
										
											2019-03-05 17:28:37 -08:00
										 |  |  |  * that any modification to lamports or account data take place on the original | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  * buffer.  Doing so also eliminates the need to serialize back into the buffer | 
					
						
							| 
									
										
										
										
											2020-04-18 22:39:08 -07:00
										 |  |  |  * at the end of the program. | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @param input Source buffer containing serialized input parameters | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |  * @param params Pointer to a SolParameters structure | 
					
						
							|  |  |  |  * @return Boolean true if successful. | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static bool sol_deserialize( | 
					
						
							| 
									
										
										
										
											2018-10-29 20:02:10 -07:00
										 |  |  |   const uint8_t *input, | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   SolParameters *params, | 
					
						
							|  |  |  |   uint64_t ka_num | 
					
						
							| 
									
										
										
										
											2018-10-29 20:02:10 -07:00
										 |  |  | ) { | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   if (NULL == input || NULL == params) { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   params->ka_num = *(uint64_t *) input; | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |   input += sizeof(uint64_t); | 
					
						
							| 
									
										
										
										
											2020-03-17 12:34:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   for (int i = 0; i < params->ka_num; i++) { | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |     uint8_t dup_info = input[0]; | 
					
						
							|  |  |  |     input += sizeof(uint8_t); | 
					
						
							| 
									
										
										
										
											2020-03-17 12:34:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (i >= ka_num) { | 
					
						
							|  |  |  |       if (dup_info == UINT8_MAX) { | 
					
						
							|  |  |  |         input += sizeof(uint8_t); | 
					
						
							|  |  |  |         input += sizeof(uint8_t); | 
					
						
							| 
									
										
										
										
											2020-08-11 16:11:52 -07:00
										 |  |  |         input += sizeof(uint8_t); | 
					
						
							|  |  |  |         input += 4; // padding
 | 
					
						
							|  |  |  |         input += sizeof(SolPubkey); | 
					
						
							| 
									
										
										
										
											2020-03-17 12:34:14 -07:00
										 |  |  |         input += sizeof(SolPubkey); | 
					
						
							|  |  |  |         input += sizeof(uint64_t); | 
					
						
							| 
									
										
										
										
											2020-08-11 16:11:52 -07:00
										 |  |  |         uint64_t data_len = *(uint64_t *) input; | 
					
						
							| 
									
										
										
										
											2020-03-17 12:34:14 -07:00
										 |  |  |         input += sizeof(uint64_t); | 
					
						
							| 
									
										
										
										
											2020-08-11 16:11:52 -07:00
										 |  |  |         input += data_len; | 
					
						
							| 
									
										
										
										
											2020-08-17 13:38:42 -07:00
										 |  |  |         input += MAX_PERMITTED_DATA_INCREASE; | 
					
						
							| 
									
										
										
										
											2020-08-17 10:24:34 -07:00
										 |  |  |         input = (uint8_t*)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
 | 
					
						
							| 
									
										
										
										
											2020-03-17 12:34:14 -07:00
										 |  |  |         input += sizeof(uint64_t); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       continue; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-11 10:03:28 -08:00
										 |  |  |     if (dup_info == UINT8_MAX) { | 
					
						
							| 
									
										
										
										
											2020-02-10 21:33:29 -08:00
										 |  |  |       // is signer?
 | 
					
						
							|  |  |  |       params->ka[i].is_signer = *(uint8_t *) input != 0; | 
					
						
							|  |  |  |       input += sizeof(uint8_t); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // is writable?
 | 
					
						
							|  |  |  |       params->ka[i].is_writable = *(uint8_t *) input != 0; | 
					
						
							|  |  |  |       input += sizeof(uint8_t); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-11 16:11:52 -07:00
										 |  |  |       // executable?
 | 
					
						
							|  |  |  |       params->ka[i].executable = *(uint8_t *) input; | 
					
						
							|  |  |  |       input += sizeof(uint8_t); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       input += 4; // padding
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |       // key
 | 
					
						
							|  |  |  |       params->ka[i].key = (SolPubkey *) input; | 
					
						
							|  |  |  |       input += sizeof(SolPubkey); | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-11 16:11:52 -07:00
										 |  |  |       // owner
 | 
					
						
							|  |  |  |       params->ka[i].owner = (SolPubkey *) input; | 
					
						
							|  |  |  |       input += sizeof(SolPubkey); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |       // lamports
 | 
					
						
							|  |  |  |       params->ka[i].lamports = (uint64_t *) input; | 
					
						
							|  |  |  |       input += sizeof(uint64_t); | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 16:30:22 -08:00
										 |  |  |       // account data
 | 
					
						
							|  |  |  |       params->ka[i].data_len = *(uint64_t *) input; | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |       input += sizeof(uint64_t); | 
					
						
							| 
									
										
										
										
											2020-02-11 16:30:22 -08:00
										 |  |  |       params->ka[i].data = (uint8_t *) input; | 
					
						
							|  |  |  |       input += params->ka[i].data_len; | 
					
						
							| 
									
										
										
										
											2020-08-17 13:38:42 -07:00
										 |  |  |       input += MAX_PERMITTED_DATA_INCREASE; | 
					
						
							| 
									
										
										
										
											2020-08-17 10:24:34 -07:00
										 |  |  |       input = (uint8_t*)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
 | 
					
						
							| 
									
										
										
										
											2020-03-04 10:52:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // rent epoch
 | 
					
						
							|  |  |  |       params->ka[i].rent_epoch = *(uint64_t *) input; | 
					
						
							|  |  |  |       input += sizeof(uint64_t); | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       params->ka[i].is_signer = params->ka[dup_info].is_signer; | 
					
						
							| 
									
										
										
										
											2020-08-11 16:11:52 -07:00
										 |  |  |       params->ka[i].is_writable = params->ka[dup_info].is_writable; | 
					
						
							|  |  |  |       params->ka[i].executable = params->ka[dup_info].executable; | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |       params->ka[i].key = params->ka[dup_info].key; | 
					
						
							| 
									
										
										
										
											2020-08-11 16:11:52 -07:00
										 |  |  |       params->ka[i].owner = params->ka[dup_info].owner; | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |       params->ka[i].lamports = params->ka[dup_info].lamports; | 
					
						
							| 
									
										
										
										
											2020-02-11 16:30:22 -08:00
										 |  |  |       params->ka[i].data_len = params->ka[dup_info].data_len; | 
					
						
							|  |  |  |       params->ka[i].data = params->ka[dup_info].data; | 
					
						
							| 
									
										
										
										
											2020-03-04 10:52:09 -08:00
										 |  |  |       params->ka[i].rent_epoch = params->ka[dup_info].rent_epoch; | 
					
						
							| 
									
										
										
										
											2020-08-11 16:11:52 -07:00
										 |  |  |       input += 7; // padding
 | 
					
						
							| 
									
										
										
										
											2020-01-24 10:54:26 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-10-31 23:11:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   params->data_len = *(uint64_t *) input; | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |   input += sizeof(uint64_t); | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   params->data = input; | 
					
						
							|  |  |  |   input += params->data_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   params->program_id = (SolPubkey *) input; | 
					
						
							|  |  |  |   input += sizeof(SolPubkey); | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 23:29:20 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Byte array pointer and string | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |   const uint8_t *addr; /** bytes */ | 
					
						
							|  |  |  |   uint64_t len; /** number of bytes*/ | 
					
						
							|  |  |  | } SolBytes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Length of a sha256 hash result | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #define SHA256_RESULT_LENGTH 32
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Sha256 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param bytes Array of byte arrays | 
					
						
							|  |  |  |  * @param bytes_len Number of byte arrays | 
					
						
							|  |  |  |  * @param result 32 byte array to hold the result | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static uint64_t sol_sha256( | 
					
						
							|  |  |  |     const SolBytes *bytes, | 
					
						
							|  |  |  |     int bytes_len, | 
					
						
							|  |  |  |     const uint8_t *result | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Account Meta | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |   SolPubkey *pubkey; /** An account's public key */ | 
					
						
							|  |  |  |   bool is_writable; /** True if the `pubkey` can be loaded as a read-write account */ | 
					
						
							|  |  |  |   bool is_signer; /** True if an Instruction requires a Transaction signature matching `pubkey` */ | 
					
						
							|  |  |  | } SolAccountMeta; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Instruction | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |   SolPubkey *program_id; /** Pubkey of the instruction processor that executes this instruction */ | 
					
						
							|  |  |  |   SolAccountMeta *accounts; /** Metadata for what accounts should be passed to the instruction processor */ | 
					
						
							|  |  |  |   uint64_t account_len; /** Number of SolAccountMetas */ | 
					
						
							|  |  |  |   uint8_t *data; /** Opaque data passed to the instruction processor */ | 
					
						
							|  |  |  |   uint64_t data_len; /** Length of the data in bytes */ | 
					
						
							|  |  |  | } SolInstruction; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-09-25 09:00:06 -07:00
										 |  |  |  * Seed used to create a program address or passed to sol_invoke_signed | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2020-09-25 09:00:06 -07:00
										 |  |  |   const uint8_t *addr; /** Seed bytes */ | 
					
						
							|  |  |  |   uint64_t len; /** Length of the seed bytes */ | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  | } SolSignerSeed; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-09-25 09:00:06 -07:00
										 |  |  |  * Seeds used by a signer to create a program address or passed to | 
					
						
							|  |  |  |  * sol_invoke_signed | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |   const SolSignerSeed *addr; /** An arry of a signer's seeds */ | 
					
						
							|  |  |  |   uint64_t len; /** Number of seeds */ | 
					
						
							|  |  |  | } SolSignerSeeds; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 23:29:20 -07:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-08-05 16:35:54 -07:00
										 |  |  |  * Create a program address | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-09-25 09:00:06 -07:00
										 |  |  |  * @param seeds Seed bytes used to sign program accounts | 
					
						
							| 
									
										
										
										
											2020-08-05 16:35:54 -07:00
										 |  |  |  * @param seeds_len Length of the seeds array | 
					
						
							| 
									
										
										
										
											2021-01-05 01:41:41 +00:00
										 |  |  |  * @param program_id Program id of the signer | 
					
						
							|  |  |  |  * @param program_address Program address created, filled on return | 
					
						
							| 
									
										
										
										
											2020-08-05 16:35:54 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | static uint64_t sol_create_program_address( | 
					
						
							|  |  |  |     const SolSignerSeed *seeds, | 
					
						
							|  |  |  |     int seeds_len, | 
					
						
							|  |  |  |     const SolPubkey *program_id, | 
					
						
							| 
									
										
										
										
											2021-01-05 01:41:41 +00:00
										 |  |  |     const SolPubkey *program_address | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Try to find a program address and return corresponding bump seed | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param seeds Seed bytes used to sign program accounts | 
					
						
							|  |  |  |  * @param seeds_len Length of the seeds array | 
					
						
							|  |  |  |  * @param program_id Program id of the signer | 
					
						
							|  |  |  |  * @param program_address Program address created, filled on return | 
					
						
							|  |  |  |  * @param bump_seed Bump seed required to create a valid program address | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static uint64_t sol_try_find_program_address( | 
					
						
							|  |  |  |     const SolSignerSeed *seeds, | 
					
						
							|  |  |  |     int seeds_len, | 
					
						
							|  |  |  |     const SolPubkey *program_id, | 
					
						
							|  |  |  |     const SolPubkey *program_address, | 
					
						
							|  |  |  |     const uint8_t *bump_seed | 
					
						
							| 
									
										
										
										
											2020-08-05 16:35:54 -07:00
										 |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Cross-program invocation | 
					
						
							|  |  |  |  *  * @{ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 23:29:20 -07:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-08-05 16:35:54 -07:00
										 |  |  |  * Invoke another program and sign for some of the keys | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  |  * @param instruction Instruction to process | 
					
						
							|  |  |  |  * @param account_infos Accounts used by instruction | 
					
						
							|  |  |  |  * @param account_infos_len Length of account_infos array | 
					
						
							| 
									
										
										
										
											2020-09-25 09:00:06 -07:00
										 |  |  |  * @param seeds Seed bytes used to sign program accounts | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  |  * @param seeds_len Length of the seeds array | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static uint64_t sol_invoke_signed( | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  |     const SolInstruction *instruction, | 
					
						
							|  |  |  |     const SolAccountInfo *account_infos, | 
					
						
							|  |  |  |     int account_infos_len, | 
					
						
							|  |  |  |     const SolSignerSeeds *signers_seeds, | 
					
						
							|  |  |  |     int signers_seeds_len | 
					
						
							|  |  |  | ) { | 
					
						
							|  |  |  |   uint64_t sol_invoke_signed_c( | 
					
						
							|  |  |  |     const SolInstruction *instruction, | 
					
						
							|  |  |  |     const SolAccountInfo *account_infos, | 
					
						
							|  |  |  |     int account_infos_len, | 
					
						
							|  |  |  |     const SolSignerSeeds *signers_seeds, | 
					
						
							|  |  |  |     int signers_seeds_len | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return sol_invoke_signed_c( | 
					
						
							|  |  |  |     instruction, | 
					
						
							|  |  |  |     account_infos, | 
					
						
							|  |  |  |     account_infos_len, | 
					
						
							|  |  |  |     signers_seeds, | 
					
						
							|  |  |  |     signers_seeds_len | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-09-29 23:29:20 -07:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-08-05 16:35:54 -07:00
										 |  |  |  * Invoke another program | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param instruction Instruction to process | 
					
						
							|  |  |  |  * @param account_infos Accounts used by instruction | 
					
						
							|  |  |  |  * @param account_infos_len Length of account_infos array | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static uint64_t sol_invoke( | 
					
						
							| 
									
										
										
										
											2020-04-28 14:33:56 -07:00
										 |  |  |     const SolInstruction *instruction, | 
					
						
							|  |  |  |     const SolAccountInfo *account_infos, | 
					
						
							|  |  |  |     int account_infos_len | 
					
						
							|  |  |  | ) { | 
					
						
							|  |  |  |   const SolSignerSeeds signers_seeds[] = {{}}; | 
					
						
							|  |  |  |   return sol_invoke_signed( | 
					
						
							|  |  |  |     instruction, | 
					
						
							|  |  |  |     account_infos, | 
					
						
							|  |  |  |     account_infos_len, | 
					
						
							|  |  |  |     signers_seeds, | 
					
						
							|  |  |  |     0 | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**@}*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Debugging utilities | 
					
						
							|  |  |  |  * @{ | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Prints the hexadecimal representation of a public key | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param key The public key to print | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-15 19:40:20 +00:00
										 |  |  | void sol_log_pubkey( | 
					
						
							|  |  |  |     const SolPubkey *pubkey | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Prints the hexadecimal representation of an array | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param array The array to print | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static void sol_log_array(const uint8_t *array, int len) { | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |   for (int j = 0; j < len; j++) { | 
					
						
							| 
									
										
										
										
											2018-11-06 14:28:46 -08:00
										 |  |  |     sol_log_64(0, 0, 0, j, array[j]); | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |  * Prints the program's input parameters | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |  * @param params Pointer to a SolParameters structure | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-05-13 17:23:39 -07:00
										 |  |  | static void sol_log_params(const SolParameters *params) { | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   sol_log("- Program identifier:"); | 
					
						
							| 
									
										
										
										
											2020-10-15 19:40:20 +00:00
										 |  |  |   sol_log_pubkey(params->program_id); | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 15:12:42 -08:00
										 |  |  |   sol_log("- Number of KeyedAccounts"); | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   sol_log_64(0, 0, 0, 0, params->ka_num); | 
					
						
							|  |  |  |   for (int i = 0; i < params->ka_num; i++) { | 
					
						
							|  |  |  |     sol_log("  - Is signer"); | 
					
						
							|  |  |  |     sol_log_64(0, 0, 0, 0, params->ka[i].is_signer); | 
					
						
							| 
									
										
										
										
											2020-02-10 21:33:29 -08:00
										 |  |  |     sol_log("  - Is writable"); | 
					
						
							|  |  |  |     sol_log_64(0, 0, 0, 0, params->ka[i].is_writable); | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |     sol_log("  - Key"); | 
					
						
							| 
									
										
										
										
											2020-10-15 19:40:20 +00:00
										 |  |  |     sol_log_pubkey(params->ka[i].key); | 
					
						
							| 
									
										
										
										
											2019-03-05 17:28:37 -08:00
										 |  |  |     sol_log("  - Lamports"); | 
					
						
							|  |  |  |     sol_log_64(0, 0, 0, 0, *params->ka[i].lamports); | 
					
						
							| 
									
										
										
										
											2020-02-11 16:30:22 -08:00
										 |  |  |     sol_log("  - data"); | 
					
						
							|  |  |  |     sol_log_array(params->ka[i].data, params->ka[i].data_len); | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |     sol_log("  - Owner"); | 
					
						
							| 
									
										
										
										
											2020-10-15 19:40:20 +00:00
										 |  |  |     sol_log_pubkey(params->ka[i].owner); | 
					
						
							| 
									
										
										
										
											2020-03-04 10:52:09 -08:00
										 |  |  |     sol_log("  - Executable"); | 
					
						
							|  |  |  |     sol_log_64(0, 0, 0, 0, params->ka[i].executable); | 
					
						
							|  |  |  |     sol_log("  - Rent Epoch"); | 
					
						
							|  |  |  |     sol_log_64(0, 0, 0, 0, params->ka[i].rent_epoch); | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-01-02 15:12:42 -08:00
										 |  |  |   sol_log("- Instruction data\0"); | 
					
						
							| 
									
										
										
										
											2019-01-23 09:55:08 -08:00
										 |  |  |   sol_log_array(params->data, params->data_len); | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**@}*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2018-11-14 10:39:22 -08:00
										 |  |  |  * Program instruction entrypoint | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-11-14 10:39:22 -08:00
										 |  |  |  * @param input Buffer of serialized input parameters.  Use sol_deserialize() to decode | 
					
						
							| 
									
										
										
										
											2019-09-06 16:05:01 -07:00
										 |  |  |  * @return 0 if the instruction executed successfully | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-01-31 10:58:07 -08:00
										 |  |  | uint64_t entrypoint(const uint8_t *input); | 
					
						
							| 
									
										
										
										
											2018-11-26 12:58:14 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef SOL_TEST
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2020-11-11 06:48:02 +00:00
										 |  |  |  * Stub functions when building tests | 
					
						
							| 
									
										
										
										
											2018-11-26 12:58:14 -08:00
										 |  |  |  */ | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-23 11:03:53 -07:00
										 |  |  | void sol_log_(const char *s, uint64_t len) { | 
					
						
							| 
									
										
										
										
											2020-11-13 22:14:05 +00:00
										 |  |  |   printf("Program log: %s\n", s); | 
					
						
							| 
									
										
										
										
											2018-11-26 12:58:14 -08:00
										 |  |  | } | 
					
						
							|  |  |  | void sol_log_64(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) { | 
					
						
							| 
									
										
										
										
											2020-11-13 22:14:05 +00:00
										 |  |  |   printf("Program log: %llu, %llu, %llu, %llu, %llu\n", arg1, arg2, arg3, arg4, arg5); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void sol_log_pubkey(const SolPubkey *pubkey) { | 
					
						
							|  |  |  |   printf("Program log: "); | 
					
						
							|  |  |  |   for (int i = 0; i < SIZE_PUBKEY; i++) { | 
					
						
							|  |  |  |     printf("%02 ", pubkey->x[i]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   printf("\n"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void sol_log_compute_units_() { | 
					
						
							|  |  |  |   printf("Program consumption: __ units remaining\n"); | 
					
						
							| 
									
										
										
										
											2018-11-26 12:58:14 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-11-11 06:48:02 +00:00
										 |  |  | void sol_panic_(const char *file, uint64_t len, uint64_t line, uint64_t column) { | 
					
						
							|  |  |  |   printf("Panic in %s at %d:%d\n", file, line, column); | 
					
						
							|  |  |  |   abort(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-26 12:58:14 -08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-29 20:12:04 -07:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:38:07 -07:00
										 |  |  | /**@}*/ |