sol_bpf.h improvements

- Define NULL
- Add sol_memcmp()
- Use sizeof() more
- Add SOL_ARRAY_SIZE
- Make sol_deserialize() more flexible
This commit is contained in:
Michael Vines 2018-10-31 23:11:22 -07:00
parent dffa2eb04f
commit 776b1c2294
3 changed files with 60 additions and 24 deletions

View File

@ -23,6 +23,11 @@ typedef unsigned int uint32_t;
typedef signed long int int64_t; typedef signed long int int64_t;
typedef unsigned long int uint64_t; typedef unsigned long int uint64_t;
/**
* NULL
*/
#define NULL 0
/** /**
* Boolean type * Boolean type
*/ */
@ -88,7 +93,7 @@ typedef struct {
* @return true if the same * @return true if the same
*/ */
SOL_FN_PREFIX bool SolPubkey_same(const SolPubkey *one, const SolPubkey *two) { SOL_FN_PREFIX bool SolPubkey_same(const SolPubkey *one, const SolPubkey *two) {
for (int i = 0; i < SIZE_PUBKEY; i++) { for (int i = 0; i < sizeof(*one); i++) {
if (one->x[i] != two->x[i]) { if (one->x[i] != two->x[i]) {
return false; return false;
} }
@ -116,6 +121,24 @@ SOL_FN_PREFIX void sol_memcpy(void *dst, const void *src, int len) {
} }
} }
/**
* Compares memory
*/
SOL_FN_PREFIX int sol_memcmp(const void *s1, const void *s2, int n) {
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;
}
/**
* Computes the number of elements in an array
*/
#define SOL_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
/** /**
* Panics * Panics
* *
@ -148,45 +171,60 @@ SOL_FN_PREFIX void _sol_panic(uint64_t line) {
* at program end. * at program end.
* *
* @param input Source buffer containing serialized input parameters * @param input Source buffer containing serialized input parameters
* @param num_ka Numer of SolKeyedAccounts to fill
* @param ka Pointer to an array of SolKeyedAccounts to deserialize into * @param ka Pointer to an array of SolKeyedAccounts to deserialize into
* @param ka_len Number of SolKeyedAccounts entries in `ka`
* @param ka_len_out If NULL, fill exactly `ka_len` accounts or fail.
* If not NULL, fill up to `ka_len` accounts and return the
* number of filled accounts in `ka_len_out`.
* @param data On return, a pointer to the instruction data * @param data On return, a pointer to the instruction data
* @param data_len On return, the length in bytes of the instruction data * @param data_len On return, the length in bytes of the instruction data
* @return Boolean true if successful * @return Boolean true if successful
*/ */
SOL_FN_PREFIX bool sol_deserialize( SOL_FN_PREFIX bool sol_deserialize(
const uint8_t *input, const uint8_t *input,
uint64_t num_ka,
SolKeyedAccounts *ka, SolKeyedAccounts *ka,
uint64_t ka_len,
uint64_t *ka_len_out,
const uint8_t **data, const uint8_t **data,
uint64_t *data_len uint64_t *data_len
) { ) {
if (num_ka != *(uint64_t *)input) {
return false;
}
input += sizeof(uint64_t);
for (int i = 0; i < num_ka; i++) {
if (ka_len_out == NULL) {
if (ka_len != *(uint64_t *) input) {
return false;
}
ka_len = *(uint64_t *) input;
} else {
if (ka_len > *(uint64_t *) input) {
ka_len = *(uint64_t *) input;
}
*ka_len_out = ka_len;
}
input += sizeof(uint64_t);
for (int i = 0; i < ka_len; i++) {
// key // key
ka[i].key = (SolPubkey *)input; ka[i].key = (SolPubkey *) input;
input += SIZE_PUBKEY; input += sizeof(SolPubkey);
// tokens // tokens
ka[i].tokens = (int64_t *)input; ka[i].tokens = (int64_t *) input;
input += sizeof(int64_t); input += sizeof(int64_t);
// account userdata // account userdata
ka[i].userdata_len = *(uint64_t *)input; ka[i].userdata_len = *(uint64_t *) input;
input += sizeof(uint64_t); input += sizeof(uint64_t);
ka[i].userdata = (uint8_t*)input; ka[i].userdata = input;
input += ka[i].userdata_len; input += ka[i].userdata_len;
// program_id // program_id
ka[i].program_id = (SolPubkey *)input; ka[i].program_id = (SolPubkey *) input;
input += SIZE_PUBKEY; input += sizeof(SolPubkey);
} }
// instruction data
*data_len = *(uint64_t *)input; // input data
*data_len = *(uint64_t *) input;
input += sizeof(uint64_t); input += sizeof(uint64_t);
*data = input; *data = input;
@ -204,7 +242,7 @@ SOL_FN_PREFIX bool sol_deserialize(
* @param key The public key to print * @param key The public key to print
*/ */
SOL_FN_PREFIX void sol_print_key(const SolPubkey *key) { SOL_FN_PREFIX void sol_print_key(const SolPubkey *key) {
for (int j = 0; j < SIZE_PUBKEY; j++) { for (int j = 0; j < sizeof(*key); j++) {
sol_print(0, 0, 0, j, key->x[j]); sol_print(0, 0, 0, j, key->x[j]);
} }
} }
@ -253,14 +291,12 @@ SOL_FN_PREFIX void sol_print_params(
* The following is an example of a simple program that prints the input * The following is an example of a simple program that prints the input
* parameters it received: * parameters it received:
* *
* #define NUM_KA 1
*
* bool entrypoint(const uint8_t *input) { * bool entrypoint(const uint8_t *input) {
* SolKeyedAccounts ka[NUM_KA]; * SolKeyedAccounts ka[1];
* uint8_t *data; * uint8_t *data;
* uint64_t data_len; * uint64_t data_len;
* *
* if (1 != sol_deserialize(buf, NUM_KA, ka, &data, &data_len)) { * if (!sol_deserialize(buf, ka, SOL_ARRAY_SIZE(ka), NULL, &data, &data_len)) {
* return false; * return false;
* } * }
* print_params(1, ka, data, data_len); * print_params(1, ka, data, data_len);

View File

@ -16,7 +16,7 @@ extern bool entrypoint(const uint8_t *input) {
const uint8_t *data; const uint8_t *data;
uint64_t data_len; uint64_t data_len;
if (!sol_deserialize(input, NUM_KA, ka, &data, &data_len)) { if (!sol_deserialize(input, ka, NUM_KA, NULL, &data, &data_len)) {
return false; return false;
} }

View File

@ -16,7 +16,7 @@ extern bool entrypoint(const uint8_t *input) {
const uint8_t *data; const uint8_t *data;
uint64_t data_len; uint64_t data_len;
if (!sol_deserialize(input, NUM_KA, ka, &data, &data_len)) { if (!sol_deserialize(input, ka, NUM_KA, NULL, &data, &data_len)) {
return false; return false;
} }
sol_print_params(NUM_KA, ka, data, data_len); sol_print_params(NUM_KA, ka, data, data_len);