Add support for BPF program custom errors (#5743)

* Add support for BPF program custom errors

* Rename SOL_SUCCESS -> SUCCESS
This commit is contained in:
Justin Starry
2019-09-06 16:05:01 -07:00
committed by GitHub
parent d3052d094c
commit 81c36699c4
26 changed files with 108 additions and 67 deletions

View File

@ -6,7 +6,7 @@
#include <solana_sdk.h>
extern bool entrypoint(const uint8_t *input) {
extern uint32_t entrypoint(const uint8_t *input) {
uint64_t x = *(uint64_t *) input;
uint64_t *result = (uint64_t *) input + 1;
uint64_t count = 0;
@ -26,5 +26,5 @@ extern bool entrypoint(const uint8_t *input) {
// sol_log_64(x, count, 0, 0, 0);
*result = count;
return true;
return SUCCESS;
}

View File

@ -4,7 +4,7 @@
Test(bench_alu, sanity) {
uint64_t input[] = {500, 0};
cr_assert(entrypoint((uint8_t *) input));
cr_assert_eq(entrypoint((uint8_t *) input), 0);
cr_assert_eq(input[0], 500);
cr_assert_eq(input[1], 5);

View File

@ -6,9 +6,9 @@
#include "helper.h"
extern bool entrypoint(const uint8_t *input) {
extern uint32_t entrypoint(const uint8_t *input) {
sol_log(__FILE__);
helper_function();
sol_log(__FILE__);
return true;
return SUCCESS;
}

View File

@ -11,17 +11,27 @@
*/
#define NUM_KA 3
extern bool entrypoint(const uint8_t *input) {
/**
* Custom error for when input serialization fails
*/
#define INVALID_INPUT 1
/**
* Custom error for when transaction is not signed properly
*/
#define NOT_SIGNED 2
extern uint32_t entrypoint(const uint8_t *input) {
SolKeyedAccount ka[NUM_KA];
SolParameters params = (SolParameters) { .ka = ka };
if (!sol_deserialize(input, &params, SOL_ARRAY_SIZE(ka))) {
return false;
return INVALID_INPUT;
}
if (!params.ka[0].is_signer) {
sol_log("Transaction not signed by key 0");
return false;
return NOT_SIGNED;
}
int64_t lamports = *(int64_t *)params.data;
@ -32,5 +42,5 @@ extern bool entrypoint(const uint8_t *input) {
} else {
// sol_log_64(0, 0, 0xFF, *ka[0].lamports, lamports);
}
return true;
return SUCCESS;
}

View File

@ -3,8 +3,8 @@
static const char msg[] = "This is a message";
static const char msg2[] = "This is a different message";
extern bool entrypoint(const uint8_t *input) {
extern uint32_t entrypoint(const uint8_t *input) {
sol_log((char*)msg);
sol_log((char*)msg2);
return true;
return SUCCESS;
}

View File

@ -4,19 +4,24 @@
*/
#include <solana_sdk.h>
extern bool entrypoint(const uint8_t *input) {
/**
* Custom error for when input serialization fails
*/
#define INVALID_INPUT 1
extern uint32_t entrypoint(const uint8_t *input) {
SolKeyedAccount ka[1];
SolParameters params = (SolParameters) { .ka = ka };
sol_log(__FILE__);
if (!sol_deserialize(input, &params, SOL_ARRAY_SIZE(ka))) {
return false;
return INVALID_INPUT;
}
// Log the provided input parameters. In the case of the no-op
// program, no account keys or input data are expected but real
// programs will have specific requirements so they can do their work.
sol_log_params(&params);
return true;
return SUCCESS;
}

View File

@ -4,20 +4,24 @@
*/
#include <solana_sdk.h>
extern bool entrypoint(const uint8_t *input) {
/**
* Custom error for when input serialization fails
*/
#define INVALID_INPUT 1
extern uint32_t entrypoint(const uint8_t *input) {
SolKeyedAccount ka[1];
SolParameters params = (SolParameters) { .ka = ka };
sol_log(__FILE__);
if (!sol_deserialize(input, &params, SOL_ARRAY_SIZE(ka))) {
return false;
return INVALID_INPUT;
}
// Log the provided input parameters. In the case of the no-op
// program, no account keys or input data are expected but real
// programs will have specific requirements so they can do their work.
sol_log_params(&params);
return true;
return SUCCESS;
}

View File

@ -4,8 +4,7 @@
*/
#include <solana_sdk.h>
extern bool entrypoint(const uint8_t *input) {
extern uint32_t entrypoint(const uint8_t *input) {
sol_panic();
return true;
return SUCCESS;
}

View File

@ -8,9 +8,8 @@ void __attribute__ ((noinline)) helper() {
sol_log(__func__);
}
extern bool entrypoint(const uint8_t *input) {
extern uint32_t entrypoint(const uint8_t *input) {
sol_log(__func__);
helper();
return true;
return SUCCESS;
}

View File

@ -3,15 +3,14 @@
struct foo {const uint8_t *input;};
void foo(const uint8_t *input, struct foo foo) ;
extern bool entrypoint(const uint8_t *input) {
extern uint32_t entrypoint(const uint8_t *input) {
struct foo f;
f.input = input;
foo(input, f);
return true;
return SUCCESS;
}
void foo(const uint8_t *input, struct foo foo) {
sol_log_64(0, 0, 0, (uint64_t)input, (uint64_t)foo.input);
sol_assert(input == foo.input);
}

View File

@ -1,5 +1,10 @@
#include <solana_sdk.h>
/**
* Custom error for when struct doesn't add to 12
*/
#define INCORRECT_SUM 1
struct test_struct { uint64_t x; uint64_t y; uint64_t z;};
static struct test_struct __attribute__ ((noinline)) test_function(void) {
@ -10,12 +15,11 @@ static struct test_struct __attribute__ ((noinline)) test_function(void) {
return s;
}
extern bool entrypoint(const uint8_t* input) {
extern uint32_t entrypoint(const uint8_t* input) {
struct test_struct s = test_function();
sol_log("foobar");
if (s.x + s.y + s.z == 12 ) {
return true;
return SUCCESS;
}
return false;
return INCORRECT_SUM;
}