diff --git a/build.rs b/build.rs index 4dcd3e7db8..2cf08b41ed 100644 --- a/build.rs +++ b/build.rs @@ -50,6 +50,17 @@ fn main() { .status() .expect("Failed to call tictactoe_c build script"); assert!(status.success()); + + println!("cargo:rerun-if-changed=programs/bpf/tictactoe_dashboard_c/build.sh"); + println!( + "cargo:rerun-if-changed=programs/bpf/tictactoe_dashboard_c/src/tictactoe_dashboard.c" + ); + println!("cargo:warning=(not a warning) Compiling tictactoe_dashboard_c"); + let status = Command::new("programs/bpf/tictactoe_dashboard_c/build.sh") + .arg(&out_dir) + .status() + .expect("Failed to call tictactoe_dashboard_c build script"); + assert!(status.success()); } if chacha || cuda || erasure { println!("cargo:rustc-link-search=native=target/perf-libs"); diff --git a/programs/bpf/tictactoe_c/src/tictactoe.c b/programs/bpf/tictactoe_c/src/tictactoe.c index 0ea42dd233..25b3a7f9af 100644 --- a/programs/bpf/tictactoe_c/src/tictactoe.c +++ b/programs/bpf/tictactoe_c/src/tictactoe.c @@ -1,4 +1,3 @@ - //#include //#include @@ -16,117 +15,108 @@ typedef unsigned char uint8_t; typedef enum { false = 0, true } bool; -#define SIZE_PUBKEY 32 -typedef struct { - uint8_t x[SIZE_PUBKEY]; -} SolPubkey; - -typedef struct { - SolPubkey *key; - int64_t tokens; - uint64_t userdata_len; - uint8_t *userdata; - SolPubkey *program_id; -} SolKeyedAccounts; - // TODO support BPF function calls rather then forcing everything to be inlined #define SOL_FN_PREFIX __attribute__((always_inline)) static // TODO move this to a registered helper SOL_FN_PREFIX void sol_memcpy(void *dst, void *src, int len) { - for (int i = 0; i < len; i++) { - *((uint8_t *)dst + i) = *((uint8_t *)src + i); - } + for (int i = 0; i < len; i++) { + *((uint8_t *)dst + i) = *((uint8_t *)src + i); + } } +#define sol_trace() sol_print(0, 0, 0xFF, 0xFF, (__LINE__)); #define sol_panic() _sol_panic(__LINE__) SOL_FN_PREFIX void _sol_panic(uint64_t line) { - sol_print(0, 0, 0xFF, 0xFF, line); - char *pv = (char *)1; - *pv = 1; + sol_print(0, 0, 0xFF, 0xFF, line); + char *pv = (char *)1; + *pv = 1; } -SOL_FN_PREFIX int sol_deserialize(uint8_t *src, uint64_t num_ka, SolKeyedAccounts *ka, - uint8_t **userdata, uint64_t *userdata_len) { - if (num_ka != *(uint64_t *)src) { - return 0; - } +#define SIZE_PUBKEY 32 +typedef struct { + uint8_t x[SIZE_PUBKEY]; +} SolPubkey; + +typedef struct { + SolPubkey *key; + int64_t tokens; + uint64_t userdata_len; + uint8_t *userdata; + SolPubkey *program_id; +} SolKeyedAccounts; + +SOL_FN_PREFIX int sol_deserialize(uint8_t *src, uint64_t num_ka, + SolKeyedAccounts *ka, uint8_t **tx_data, + uint64_t *tx_data_len) { + if (num_ka != *(uint64_t *)src) { + return 0; + } + src += sizeof(uint64_t); + + // TODO fixed iteration loops ok? unrolled? + for (int i = 0; i < num_ka; + i++) { // TODO this should end up unrolled, confirm + // key + ka[i].key = (SolPubkey *)src; + src += SIZE_PUBKEY; + + // tokens + ka[i].tokens = *(uint64_t *)src; src += sizeof(uint64_t); - // TODO fixed iteration loops ok? unrolled? - for (int i = 0; i < num_ka; i++) { // TODO this should end up unrolled, confirm - // key - ka[i].key = (SolPubkey *)src; - src += SIZE_PUBKEY; - - // tokens - ka[i].tokens = *(uint64_t *)src; - src += sizeof(uint64_t); - - // account userdata - ka[i].userdata_len = *(uint64_t *)src; - src += sizeof(uint64_t); - ka[i].userdata = src; - src += ka[i].userdata_len; - - // program_id - ka[i].program_id = (SolPubkey *)src; - src += SIZE_PUBKEY; - } - // tx userdata - *userdata_len = *(uint64_t *)src; + // account userdata + ka[i].userdata_len = *(uint64_t *)src; src += sizeof(uint64_t); - *userdata = src; + ka[i].userdata = src; + src += ka[i].userdata_len; - return 1; + // program_id + ka[i].program_id = (SolPubkey *)src; + src += SIZE_PUBKEY; + } + // tx userdata + *tx_data_len = *(uint64_t *)src; + src += sizeof(uint64_t); + *tx_data = src; + + return 1; } // // -- Debug -- SOL_FN_PREFIX void print_key(SolPubkey *key) { - for (int j = 0; j < SIZE_PUBKEY; j++) { - sol_print(0, 0, 0, j, key->x[j]); - } + for (int j = 0; j < SIZE_PUBKEY; j++) { + sol_print(0, 0, 0, j, key->x[j]); + } } -SOL_FN_PREFIX void print_userdata(uint8_t *data, int len) { - for (int j = 0; j < len; j++) { - sol_print(0, 0, 0, j, data[j]); - } +SOL_FN_PREFIX void print_data(uint8_t *data, int len) { + for (int j = 0; j < len; j++) { + sol_print(0, 0, 0, j, data[j]); + } } SOL_FN_PREFIX void print_params(uint64_t num_ka, SolKeyedAccounts *ka, - uint8_t *userdata, uint64_t userdata_len) { - sol_print(0, 0, 0, 0, num_ka); - for (int i = 0; i < num_ka; i++) { - // key - print_key(ka[i].key); + uint8_t *tx_data, uint64_t tx_data_len) { + sol_print(0, 0, 0, 0, num_ka); + for (int i = 0; i < num_ka; i++) { + // key + print_key(ka[i].key); - // tokens - sol_print(0, 0, 0, 0, ka[i].tokens); + // tokens + sol_print(0, 0, 0, 0, ka[i].tokens); - // account userdata - print_userdata(ka[i].userdata, ka[i].userdata_len); + // account userdata + print_data(ka[i].userdata, ka[i].userdata_len); - // program_id - print_key(ka[i].program_id); - } - // tx userdata - print_userdata(userdata, userdata_len); + // program_id + print_key(ka[i].program_id); + } + // tx userdata + print_data(tx_data, tx_data_len); } -// void entrypoint(char *buf) { -// SolKeyedAccounts ka[3]; -// uint64_t userdata_len; -// uint8_t *userdata; - -// if (0 != sol_deserialize((uint8_t *)buf, 3, ka, &userdata, &userdata_len)) { -// return; -// } - -// print_params(3, ka, userdata, userdata_len); -// } - // -- TicTacToe -- // Board Coodinates @@ -135,238 +125,249 @@ SOL_FN_PREFIX void print_params(uint64_t num_ka, SolKeyedAccounts *ka, // | 0,2 | 1,2 | 2,2 | typedef enum { - Result_Ok, - Result_Panic, - Result_GameInProgress, - Result_InvalidArguments, - Result_InvalidMove, - Result_InvalidUserdata, - Result_InvalidTimestamp, - Result_NoGame, - Result_NotYourTurn, - Result_PlayerNotFound, - Result_UserdataTooSmall, + Result_Ok, + Result_Panic, + Result_GameInProgress, + Result_InvalidArguments, + Result_InvalidMove, + Result_InvalidUserdata, + Result_InvalidTimestamp, + Result_NoGame, + Result_NotYourTurn, + Result_PlayerNotFound, + Result_UserdataTooSmall, } Result; typedef enum { BoardItem_F, BoardItem_X, BoardItem_O } BoardItem; typedef enum { - State_Waiting, - State_XMove, - State_OMove, - State_XWon, - State_OWon, - State_Draw, + State_Waiting, + State_XMove, + State_OMove, + State_XWon, + State_OWon, + State_Draw, } State; typedef struct { - SolPubkey player_x; - SolPubkey player_o; - State state; - BoardItem board[9]; - int64_t keep_alive[2]; + // Player who initialized the game + SolPubkey player_x; + // Player who joined the game + SolPubkey player_o; + // Current state of the game + State state; + // Tracks the player moves + BoardItem board[9]; + // Keep Alive for each player + int64_t keep_alive[2]; } Game; typedef enum { - Command_Init = 0, - Command_Join, - Command_KeepAlive, - Command_Move, + Command_Init = 0, + Command_Join, + Command_KeepAlive, + Command_Move, } Command; SOL_FN_PREFIX void game_dump_board(Game *self) { - sol_print(0, 0, 0x9, 0x9, 0x9); - sol_print(0, 0, self->board[0], self->board[1], self->board[2]); - sol_print(0, 0, self->board[3], self->board[4], self->board[5]); - sol_print(0, 0, self->board[6], self->board[7], self->board[8]); - sol_print(0, 0, 0x9, 0x9, 0x9); + sol_print(0, 0, 0x9, 0x9, 0x9); + sol_print(0, 0, self->board[0], self->board[1], self->board[2]); + sol_print(0, 0, self->board[3], self->board[4], self->board[5]); + sol_print(0, 0, self->board[6], self->board[7], self->board[8]); + sol_print(0, 0, 0x9, 0x9, 0x9); } SOL_FN_PREFIX void game_create(Game *self, SolPubkey *player_x) { - sol_memcpy(self->player_x.x, player_x, SIZE_PUBKEY); - // TODO self->player_o = 0; - self->state = State_Waiting; - self->keep_alive[0] = 0; - self->keep_alive[1] = 0; + sol_memcpy(self->player_x.x, player_x, SIZE_PUBKEY); + // TODO self->player_o = 0; + self->state = State_Waiting; + self->keep_alive[0] = 0; + self->keep_alive[1] = 0; - // TODO fixed iteration loops ok? unrolled? - for (int i = 0; i < 9; i++) { - self->board[i] = BoardItem_F; - } + // TODO fixed iteration loops ok? unrolled? + for (int i = 0; i < 9; i++) { + self->board[i] = BoardItem_F; + } } SOL_FN_PREFIX Result game_join(Game *self, SolPubkey *player_o, int64_t timestamp) { - if (self->state == State_Waiting) { - sol_memcpy(self->player_o.x, player_o, SIZE_PUBKEY); - self->state = State_XMove; + if (self->state == State_Waiting) { + sol_memcpy(self->player_o.x, player_o, SIZE_PUBKEY); + self->state = State_XMove; - if (timestamp <= self->keep_alive[1]) { - return Result_InvalidTimestamp; - } else { - self->keep_alive[1] = timestamp; - return Result_Ok; - } + if (timestamp <= self->keep_alive[1]) { + return Result_InvalidTimestamp; + } else { + self->keep_alive[1] = timestamp; + return Result_Ok; } - return Result_GameInProgress; + } + return Result_GameInProgress; } SOL_FN_PREFIX bool game_same(BoardItem x_or_o, BoardItem one, BoardItem two, BoardItem three) { - if (x_or_o == one && x_or_o == two && x_or_o == three) { - return true; - } - return false; + if (x_or_o == one && x_or_o == two && x_or_o == three) { + return true; + } + return false; } SOL_FN_PREFIX bool game_same_player(SolPubkey *one, SolPubkey *two) { - // TODO fixed iteration loops ok? unrolled? - for (int i = 0; i < SIZE_PUBKEY; i++) { - if (one->x[i] != two->x[i]) { - return false; - } + // TODO fixed iteration loops ok? unrolled? + for (int i = 0; i < SIZE_PUBKEY; i++) { + if (one->x[i] != two->x[i]) { + return false; } - return true; + } + return true; } -SOL_FN_PREFIX Result game_next_move(Game *self, SolPubkey *player, int x, int y) { - int board_index = y * 3 + x; - if (board_index >= 9 || self->board[board_index] != BoardItem_F) { - return Result_InvalidMove; +SOL_FN_PREFIX Result game_next_move(Game *self, SolPubkey *player, int x, + int y) { + int board_index = y * 3 + x; + if (board_index >= 9 || self->board[board_index] != BoardItem_F) { + return Result_InvalidMove; + } + + BoardItem x_or_o; + State won_state; + + switch (self->state) { + case State_XMove: + if (!game_same_player(player, &self->player_x)) { + return Result_PlayerNotFound; + } + self->state = State_OMove; + x_or_o = BoardItem_X; + won_state = State_XWon; + break; + + case State_OMove: + if (!game_same_player(player, &self->player_o)) { + return Result_PlayerNotFound; + } + self->state = State_XMove; + x_or_o = BoardItem_O; + won_state = State_OWon; + break; + + default: + return Result_NotYourTurn; + } + + self->board[board_index] = x_or_o; + + // game_dump_board(self); + + bool winner = + // Check rows + game_same(x_or_o, self->board[0], self->board[1], self->board[2]) || + game_same(x_or_o, self->board[3], self->board[4], self->board[5]) || + game_same(x_or_o, self->board[6], self->board[7], self->board[8]) || + // Check columns + game_same(x_or_o, self->board[0], self->board[3], self->board[6]) || + game_same(x_or_o, self->board[1], self->board[4], self->board[7]) || + game_same(x_or_o, self->board[2], self->board[5], self->board[8]) || + // Check both diagonals + game_same(x_or_o, self->board[0], self->board[4], self->board[8]) || + game_same(x_or_o, self->board[2], self->board[4], self->board[6]); + + if (winner) { + self->state = won_state; + } + + { + int draw = true; + // TODO fixed iteration loops ok? unrolled? + for (int i = 0; i < 9; i++) { + if (BoardItem_F == self->board[i]) { + draw = false; + break; + } } - - BoardItem x_or_o; - State won_state; - - switch (self->state) { - case State_XMove: - if (!game_same_player(player, &self->player_x)) { - return Result_PlayerNotFound; - } - self->state = State_OMove; - x_or_o = BoardItem_X; - won_state = State_XWon; - break; - - case State_OMove: - if (!game_same_player(player, &self->player_o)) { - return Result_PlayerNotFound; - } - self->state = State_XMove; - x_or_o = BoardItem_O; - won_state = State_OWon; - break; - - default: - return Result_NotYourTurn; + if (draw) { + self->state = State_Draw; } - - self->board[board_index] = x_or_o; - - // game_dump_board(self); - - bool winner = - // Check rows - game_same(x_or_o, self->board[0], self->board[1], self->board[2]) || - game_same(x_or_o, self->board[3], self->board[4], self->board[5]) || - game_same(x_or_o, self->board[6], self->board[7], self->board[8]) || - // Check columns - game_same(x_or_o, self->board[0], self->board[3], self->board[6]) || - game_same(x_or_o, self->board[1], self->board[4], self->board[7]) || - game_same(x_or_o, self->board[2], self->board[5], self->board[8]) || - // Check both diagonals - game_same(x_or_o, self->board[0], self->board[4], self->board[8]) || - game_same(x_or_o, self->board[2], self->board[4], self->board[6]); - - if (winner) { - self->state = won_state; - } - - { - int draw = true; - // TODO fixed iteration loops ok? unrolled? - for (int i = 0; i < 9; i++) { - if (BoardItem_F == self->board[i]) { - draw = false; - break; - } - } - if (draw) { - self->state = State_Draw; - } - } - return Result_Ok; + } + return Result_Ok; } SOL_FN_PREFIX Result game_keep_alive(Game *self, SolPubkey *player, int64_t timestamp) { - switch (self->state) { - case State_Waiting: - case State_XMove: - case State_OMove: - if (game_same_player(player, &self->player_x)) { - if (timestamp <= self->keep_alive[0]) { - return Result_InvalidTimestamp; - } - self->keep_alive[0] = timestamp; - } else if (game_same_player(player, &self->player_o)) { - if (timestamp <= self->keep_alive[1]) { - return Result_InvalidTimestamp; - } - self->keep_alive[1] = timestamp; - } else { - return Result_PlayerNotFound; - } - break; + switch (self->state) { + case State_Waiting: + case State_XMove: + case State_OMove: + if (game_same_player(player, &self->player_x)) { + if (timestamp <= self->keep_alive[0]) { + return Result_InvalidTimestamp; + } + self->keep_alive[0] = timestamp; + } else if (game_same_player(player, &self->player_o)) { + if (timestamp <= self->keep_alive[1]) { + return Result_InvalidTimestamp; + } + self->keep_alive[1] = timestamp; + } else { + return Result_PlayerNotFound; + } + break; - default: - break; - } - return Result_Ok; + default: + break; + } + return Result_Ok; } +// accounts[0] On Init must be player X, after that doesn't matter, +// anybody can cause a dashboard update +// accounts[1] must be a TicTacToe state account +// accounts[2] must be account of current player, only Pubkey is used uint64_t entrypoint(uint8_t *buf) { - SolKeyedAccounts ka[4]; - uint64_t userdata_len; - uint8_t *userdata; - int err = 0; + SolKeyedAccounts ka[3]; + uint64_t tx_data_len; + uint8_t *tx_data; + int err = 0; - if (1 != sol_deserialize(buf, 4, ka, &userdata, &userdata_len)) { - return 0; - } + if (1 != sol_deserialize(buf, 3, ka, &tx_data, &tx_data_len)) { + return false; + } - if (sizeof(Game) > ka[2].userdata_len) { - sol_print(0, 0, 0xFF, sizeof(Game), ka[2].userdata_len); - return 0; - } - Game game; - sol_memcpy(&game, ka[2].userdata, ka[2].userdata_len); + if (sizeof(Game) > ka[1].userdata_len) { + sol_print(0, 0, 0xFF, sizeof(Game), ka[2].userdata_len); + return false; + } + Game game; + sol_memcpy(&game, ka[1].userdata, sizeof(game)); - Command command = *userdata; - //sol_print(0, 0, 0, 0, command); - switch (command) { - case Command_Init: - game_create(&game, ka[3].key); - break; + Command command = *tx_data; + switch (command) { + case Command_Init: + game_create(&game, ka[2].key); + break; - case Command_Join: - err = game_join(&game, ka[3].key, userdata[8]); - break; + case Command_Join: + err = game_join(&game, ka[2].key, *((int64_t *)(tx_data + 4))); + break; - case Command_KeepAlive: - err = game_keep_alive(&game, ka[3].key, /*TODO*/ 0); - break; + case Command_KeepAlive: + err = game_keep_alive(&game, ka[2].key, /*TODO*/ 0); + break; - case Command_Move: - err = game_next_move(&game, ka[3].key, userdata[8], userdata[9]); - break; + case Command_Move: + err = game_next_move(&game, ka[2].key, tx_data[4], tx_data[5]); + break; - default: - return 0; - } + default: + return false; + } - sol_memcpy(ka[2].userdata, &game, ka[2].userdata_len); - sol_print(0, 0, 0, err, game.state); - return 1; + sol_memcpy(ka[1].userdata, &game, sizeof(game)); + sol_print(0, 0, 0, err, game.state); + if (Result_Ok != err) { + return false; + } + return true; } - diff --git a/programs/bpf/tictactoe_dashboard_c/build.sh b/programs/bpf/tictactoe_dashboard_c/build.sh new file mode 100755 index 0000000000..8920049e15 --- /dev/null +++ b/programs/bpf/tictactoe_dashboard_c/build.sh @@ -0,0 +1,9 @@ +#!/bin/bash -ex + +OUTDIR="${1:-../../../target/release/}" +THISDIR=$(dirname "$0") +mkdir -p "$OUTDIR" +/usr/local/opt/llvm/bin/clang -Werror -target bpf -O2 -emit-llvm -fno-builtin -o "$OUTDIR"/tictactoe_dashboard_c.bc -c "$THISDIR"/src/tictactoe_dashboard.c +/usr/local/opt/llvm/bin/llc -march=bpf -filetype=obj -function-sections -o "$OUTDIR"/tictactoe_dashboard_c.o "$OUTDIR"/tictactoe_dashboard_c.bc + +# /usr/local/opt/llvm/bin/llvm-objdump -color -source -disassemble "$OUTDIR"/tictactoe_dashboard_c.o \ No newline at end of file diff --git a/programs/bpf/tictactoe_dashboard_c/dump.sh b/programs/bpf/tictactoe_dashboard_c/dump.sh new file mode 100755 index 0000000000..3ad37ceb6c --- /dev/null +++ b/programs/bpf/tictactoe_dashboard_c/dump.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/local/opt/llvm/bin/llvm-objdump -color -source -disassemble ../../../target/release/tictactoe_dashboard_c.o \ No newline at end of file diff --git a/programs/bpf/tictactoe_dashboard_c/src/tictactoe_dashboard.c b/programs/bpf/tictactoe_dashboard_c/src/tictactoe_dashboard.c new file mode 100644 index 0000000000..2c9d439bd9 --- /dev/null +++ b/programs/bpf/tictactoe_dashboard_c/src/tictactoe_dashboard.c @@ -0,0 +1,236 @@ +//#include +//#include + +#if 1 +#define BPF_TRACE_PRINTK_IDX 6 +static int (*sol_print)(int, int, int, int, int) = (void *)BPF_TRACE_PRINTK_IDX; +#else +// relocation is another option +extern int sol_print(int, int, int, int, int); +#endif + +typedef long long unsigned int uint64_t; +typedef long long int int64_t; +typedef long unsigned int uint32_t; +typedef long int int32_t; +typedef unsigned char uint8_t; + +typedef enum { false = 0, true } bool; + +// TODO support BPF function calls rather then forcing everything to be inlined +#define SOL_FN_PREFIX __attribute__((always_inline)) static + +// TODO move this to a registered helper +SOL_FN_PREFIX void sol_memcpy(void *dst, void *src, int len) { + for (int i = 0; i < len; i++) { + *((uint8_t *)dst + i) = *((uint8_t *)src + i); + } +} + +#define sol_trace() sol_print(0, 0, 0xFF, 0xFF, (__LINE__)); +#define sol_panic() _sol_panic(__LINE__) +SOL_FN_PREFIX void _sol_panic(uint64_t line) { + sol_print(0, 0, 0xFF, 0xFF, line); + char *pv = (char *)1; + *pv = 1; +} + +#define SIZE_PUBKEY 32 +typedef struct { + uint8_t x[SIZE_PUBKEY]; +} SolPubkey; + +SOL_FN_PREFIX bool SolPubkey_same(SolPubkey *one, SolPubkey *two) { + for (int i = 0; i < SIZE_PUBKEY; i++) { + if (one->x[i] != two->x[i]) { + return false; + } + } + return true; +} + +typedef struct { + SolPubkey *key; + int64_t tokens; + uint64_t userdata_len; + uint8_t *userdata; + SolPubkey *program_id; +} SolKeyedAccounts; + +SOL_FN_PREFIX int sol_deserialize(uint8_t *src, uint64_t num_ka, + SolKeyedAccounts *ka, uint8_t **tx_data, + uint64_t *tx_data_len) { + if (num_ka != *(uint64_t *)src) { + return 0; + } + src += sizeof(uint64_t); + + // TODO fixed iteration loops ok? unrolled? + for (int i = 0; i < num_ka; + i++) { // TODO this should end up unrolled, confirm + // key + ka[i].key = (SolPubkey *)src; + src += SIZE_PUBKEY; + + // tokens + ka[i].tokens = *(uint64_t *)src; + src += sizeof(uint64_t); + + // account userdata + ka[i].userdata_len = *(uint64_t *)src; + src += sizeof(uint64_t); + ka[i].userdata = src; + src += ka[i].userdata_len; + + // program_id + ka[i].program_id = (SolPubkey *)src; + src += SIZE_PUBKEY; + } + // tx userdata + *tx_data_len = *(uint64_t *)src; + src += sizeof(uint64_t); + *tx_data = src; + + return 1; +} + +// -- Debug -- + +SOL_FN_PREFIX void print_key(SolPubkey *key) { + for (int j = 0; j < SIZE_PUBKEY; j++) { + sol_print(0, 0, 0, j, key->x[j]); + } +} + +SOL_FN_PREFIX void print_data(uint8_t *data, int len) { + for (int j = 0; j < len; j++) { + sol_print(0, 0, 0, j, data[j]); + } +} + +SOL_FN_PREFIX void print_params(uint64_t num_ka, SolKeyedAccounts *ka, + uint8_t *tx_data, uint64_t tx_data_len) { + sol_print(0, 0, 0, 0, num_ka); + for (int i = 0; i < num_ka; i++) { + // key + print_key(ka[i].key); + + // tokens + sol_print(0, 0, 0, 0, ka[i].tokens); + + // account userdata + print_data(ka[i].userdata, ka[i].userdata_len); + + // program_id + print_key(ka[i].program_id); + } + // tx userdata + print_data(tx_data, tx_data_len); +} + +// -- TicTacToe Dashboard -- + +// TODO put this in a common place for both tictactoe and tictactoe_dashboard +typedef enum { + State_Waiting, + State_XMove, + State_OMove, + State_XWon, + State_OWon, + State_Draw, +} State; + +// TODO put this in a common place for both tictactoe and tictactoe_dashboard +typedef enum { BoardItem_F, BoardItem_X, BoardItem_O } BoardItem; + +// TODO put this in a common place for both tictactoe and tictactoe_dashboard +typedef struct { + SolPubkey player_x; + SolPubkey player_o; + State state; + BoardItem board[9]; + int64_t keep_alive[2]; +} Game; + +#define MAX_GAMES_TRACKED 5 + +typedef struct { + // Latest pending game + SolPubkey pending; + // Last N completed games (0 is the latest) + SolPubkey completed[MAX_GAMES_TRACKED]; + // Index into completed pointing to latest game completed + uint32_t latest_game; + // Total number of completed games + uint32_t total; +} Dashboard; + +SOL_FN_PREFIX bool update(Dashboard *self, Game *game, SolPubkey *game_pubkey) { + switch (game->state) { + case State_Waiting: + sol_memcpy(&self->pending, game_pubkey, SIZE_PUBKEY); + break; + case State_XMove: + case State_OMove: + // Nothing to do. In progress games are not managed by the dashboard + break; + case State_XWon: + case State_OWon: + case State_Draw: + for (int i = 0; i < MAX_GAMES_TRACKED; i++) { + if (SolPubkey_same(&self->completed[i], game_pubkey)) { + // TODO: Once the PoH height is exposed to programs, it could be used + // to ensure + // that old games are not being re-added and causing total to + // increment incorrectly. + return false; + } + } + self->total += 1; + self->latest_game = (self->latest_game + 1) % MAX_GAMES_TRACKED; + sol_memcpy(self->completed[self->latest_game].x, game_pubkey, + SIZE_PUBKEY); + break; + + default: + break; + } + return true; +} + +// accounts[0] doesn't matter, anybody can cause a dashboard update +// accounts[1] must be a Dashboard account +// accounts[2] must be a Game account +uint64_t entrypoint(uint8_t *buf) { + SolKeyedAccounts ka[3]; + uint64_t tx_data_len; + uint8_t *tx_data; + int err = 0; + + if (1 != sol_deserialize(buf, 3, ka, &tx_data, &tx_data_len)) { + return false; + } + + // TODO check dashboard and game program ids (how to check now that they are + // not know values) + // TODO check validity of dashboard and game structures contents + if (sizeof(Dashboard) > ka[1].userdata_len) { + sol_print(0, 0, 0xFF, sizeof(Dashboard), ka[2].userdata_len); + return false; + } + Dashboard dashboard; + sol_memcpy(&dashboard, ka[1].userdata, sizeof(dashboard)); + + if (sizeof(Game) > ka[2].userdata_len) { + sol_print(0, 0, 0xFF, sizeof(Game), ka[2].userdata_len); + return false; + } + Game game; + sol_memcpy(&game, ka[2].userdata, sizeof(game)); + if (true != update(&dashboard, &game, ka[2].key)) { + return false; + } + + sol_memcpy(ka[1].userdata, &dashboard, sizeof(dashboard)); + return true; +} diff --git a/tests/programs.rs b/tests/programs.rs index f2ad3448fa..e324412dd4 100644 --- a/tests/programs.rs +++ b/tests/programs.rs @@ -11,7 +11,10 @@ use solana::mint::Mint; use solana::native_loader; use solana::signature::{Keypair, KeypairUtil}; use solana::system_transaction::SystemTransaction; +#[cfg(feature = "bpf_c")] +use solana::tictactoe_program::Command; use solana::transaction::Transaction; +use solana_program_interface::pubkey::Pubkey; // TODO test modified user data // TODO test failure if account tokens decrease but not assigned to program @@ -22,246 +25,485 @@ fn check_tx_results(bank: &Bank, tx: &Transaction, result: Vec Self { + let mint = Mint::new(50); + let bank = Bank::new(&mint); + let loader = Keypair::new(); + + // allocate, populate, finalize BPF loader + + let tx = Transaction::system_create( + &mint.keypair(), + loader.pubkey(), + mint.last_id(), + 1, + 56, // TODO How does the user know how much space to allocate for what should be an internally known size + native_loader::id(), + 0, + ); + check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + + let name = String::from(loader_name); + let tx = Transaction::write( + &loader, + native_loader::id(), + 0, + name.as_bytes().to_vec(), + mint.last_id(), + 0, + ); + check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + + let tx = Transaction::finalize(&loader, native_loader::id(), mint.last_id(), 0); + check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + + let tx = Transaction::system_spawn(&loader, mint.last_id(), 0); + check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + + Loader { + mint, + bank, + loader: loader.pubkey(), + } + } + + pub fn new_native() -> Self { + let mint = Mint::new(50); + let bank = Bank::new(&mint); + let loader = native_loader::id(); + + Loader { mint, bank, loader } + } +} + +struct Program { + program: Keypair, +} + +impl Program { + pub fn new(loader: &Loader, userdata: Vec, size: u64) -> Self { + let program = Keypair::new(); + + // allocate, populate, and finalize user program + + let tx = Transaction::system_create( + &loader.mint.keypair(), + program.pubkey(), + loader.mint.last_id(), + 1, + size, + loader.loader, + 0, + ); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + + let tx = Transaction::write( + &program, + loader.loader, + 0, + userdata, + loader.mint.last_id(), + 0, + ); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + + let tx = Transaction::finalize(&program, loader.loader, loader.mint.last_id(), 0); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + + let tx = Transaction::system_spawn(&program, loader.mint.last_id(), 0); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + + Program { program } + } +} + #[test] fn test_transaction_load_native() { logger::setup(); - let mint = Mint::new(50); - // TODO in a test like this how should the last_id be incremented, as used here it is always the same - // which leads to duplicate tx signature errors - let bank = Bank::new(&mint); - let program = Keypair::new(); - - // allocate, populate, finalize user program - - let tx = Transaction::system_create( - &mint.keypair(), - program.pubkey(), - mint.last_id(), - 1, - 56, // TODO How does the user know how much space to allocate, this is really an internally known size - native_loader::id(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - println!("id: {:?}", native_loader::id()); + let loader = Loader::new_native(); let name = String::from("noop"); - let tx = Transaction::write( - &program, - native_loader::id(), - 0, - name.as_bytes().to_vec(), - mint.last_id(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - println!("id after: {:?}", native_loader::id()); - - let tx = Transaction::finalize(&program, native_loader::id(), mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::system_spawn(&program, mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + let userdata = name.as_bytes().to_vec(); + let program = Program::new(&loader, userdata, 300); // Call user program let tx = Transaction::new( - &mint.keypair(), // TODO + &loader.mint.keypair(), // TODO &[], - program.pubkey(), + program.program.pubkey(), vec![1u8], - mint.last_id(), + loader.mint.last_id(), 0, ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); } #[test] -fn test_transaction_load_lua() { +fn test_program_lua_move_funds() { logger::setup(); - let mint = Mint::new(50); - // TODO in a test like this how should the last_id be incremented, as used here it is always the same - // which leads to duplicate tx signature errors - let bank = Bank::new(&mint); - let loader = Keypair::new(); - let program = Keypair::new(); - let from = Keypair::new(); - let to = Keypair::new().pubkey(); - - // allocate, populate, and finalize Lua loader - - let tx = Transaction::system_create( - &mint.keypair(), - loader.pubkey(), - mint.last_id(), - 1, - 56, // TODO How does the user know how much space to allocate for what should be an internally known size - native_loader::id(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let name = String::from("lua_loader"); - let tx = Transaction::write( - &loader, - native_loader::id(), - 0, - name.as_bytes().to_vec(), - mint.last_id(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::finalize(&loader, native_loader::id(), mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::system_spawn(&loader, mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - // allocate, populate, and finalize user program - - let bytes = r#" + let loader = Loader::new_dynamic("lua_loader"); + let userdata = r#" print("Lua Script!") local tokens, _ = string.unpack("I", data) accounts[1].tokens = accounts[1].tokens - tokens accounts[2].tokens = accounts[2].tokens + tokens "#.as_bytes() .to_vec(); - - let tx = Transaction::system_create( - &mint.keypair(), - program.pubkey(), - mint.last_id(), - 1, - 300, // TODO How does the user know how much space to allocate for what should be an internally known size - loader.pubkey(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::write(&program, loader.pubkey(), 0, bytes, mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::finalize(&program, loader.pubkey(), mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::system_spawn(&program, mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + let program = Program::new(&loader, userdata, 300); + let from = Keypair::new(); + let to = Keypair::new().pubkey(); // Call user program with two accounts let tx = Transaction::system_create( - &mint.keypair(), + &loader.mint.keypair(), from.pubkey(), - mint.last_id(), + loader.mint.last_id(), 10, 0, - program.pubkey(), + program.program.pubkey(), 0, ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); let tx = Transaction::system_create( - &mint.keypair(), + &loader.mint.keypair(), to, - mint.last_id(), + loader.mint.last_id(), 1, 0, - program.pubkey(), + program.program.pubkey(), 0, ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); let data = serialize(&10).unwrap(); - let tx = Transaction::new(&from, &[to], program.pubkey(), data, mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - assert_eq!(bank.get_balance(&from.pubkey()), 0); - assert_eq!(bank.get_balance(&to), 11); + let tx = Transaction::new( + &from, + &[to], + program.program.pubkey(), + data, + loader.mint.last_id(), + 0, + ); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + assert_eq!(loader.bank.get_balance(&from.pubkey()), 0); + assert_eq!(loader.bank.get_balance(&to), 11); } #[cfg(feature = "bpf_c")] #[test] -fn test_transaction_load_bpf() { +fn test_program_bpf_noop_c() { logger::setup(); - let mint = Mint::new(50); - // TODO in a test like this how should the last_id be incremented, as used here it is always the same - // which leads to duplicate tx signature errors - let bank = Bank::new(&mint); - let loader = Keypair::new(); - let program = Keypair::new(); - - // allocate, populate, finalize BPF loader - - let tx = Transaction::system_create( - &mint.keypair(), - loader.pubkey(), - mint.last_id(), - 1, - 56, // TODO How does the user know how much space to allocate for what should be an internally known size - native_loader::id(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let name = String::from("bpf_loader"); - let tx = Transaction::write( - &loader, - native_loader::id(), - 0, - name.as_bytes().to_vec(), - mint.last_id(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::finalize(&loader, native_loader::id(), mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::system_spawn(&loader, mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - // allocate, populate, and finalize user program - - let tx = Transaction::system_create( - &mint.keypair(), - program.pubkey(), - mint.last_id(), - 1, - 56, // TODO How does the user know how much space to allocate for what should be an internally known size - loader.pubkey(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - + let loader = Loader::new_dynamic("bpf_loader"); let name = String::from("noop_c"); - let tx = Transaction::write( - &program, - loader.pubkey(), - 0, - name.as_bytes().to_vec(), - mint.last_id(), - 0, - ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::finalize(&program, loader.pubkey(), mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); - - let tx = Transaction::system_spawn(&program, mint.last_id(), 0); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + let userdata = name.as_bytes().to_vec(); + let program = Program::new(&loader, userdata, 56); // Call user program let tx = Transaction::new( - &mint.keypair(), // TODO + &loader.mint.keypair(), // TODO &[], - program.pubkey(), + program.program.pubkey(), vec![1u8], - mint.last_id(), + loader.mint.last_id(), 0, ); - check_tx_results(&bank, &tx, bank.process_transactions(&vec![tx.clone()])); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); +} + +#[cfg(feature = "bpf_c")] +struct TicTacToe { + game: Keypair, +} + +#[cfg(feature = "bpf_c")] +impl TicTacToe { + pub fn new(loader: &Loader, program: &Program) -> Self { + let game = Keypair::new(); + + // Create game account + + let tx = Transaction::system_create( + &loader.mint.keypair(), + game.pubkey(), + loader.mint.last_id(), + 1, + 0x78, // corresponds to the C structure size + program.program.pubkey(), + 0, + ); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + + TicTacToe { game } + } + + pub fn id(&self) -> Pubkey { + self.game.pubkey().clone() + } + + pub fn init(&self, loader: &Loader, program: &Program, player: &Pubkey) { + let userdata = serialize(&Command::Init).unwrap(); + let tx = Transaction::new( + &self.game, + &[self.game.pubkey(), *player], + program.program.pubkey(), + userdata, + loader.mint.last_id(), + 0, + ); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + } + + pub fn command(&self, loader: &Loader, program: &Program, command: Command, player: &Pubkey) { + let userdata = serialize(&command).unwrap(); + let tx = Transaction::new( + &loader.mint.keypair(), + &[self.game.pubkey(), *player], + program.program.pubkey(), + userdata, + loader.mint.last_id(), + 0, + ); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + } + + pub fn get_player_x(&self, loader: &Loader) -> Vec { + loader + .bank + .get_account(&self.game.pubkey()) + .unwrap() + .userdata[0..32] + .to_vec() + } + + pub fn get_player_y(&self, loader: &Loader) -> Vec { + loader + .bank + .get_account(&self.game.pubkey()) + .unwrap() + .userdata[32..64] + .to_vec() + } + + pub fn game(&self, loader: &Loader) -> Vec { + loader + .bank + .get_account(&self.game.pubkey()) + .unwrap() + .userdata[64..68] + .to_vec() + } +} + +#[cfg(feature = "bpf_c")] +struct Dashboard { + dashboard: Keypair, +} + +#[cfg(feature = "bpf_c")] +impl Dashboard { + pub fn new(loader: &Loader, program: &Program) -> Self { + let dashboard = Keypair::new(); + + // Create game account + + let tx = Transaction::system_create( + &loader.mint.keypair(), + dashboard.pubkey(), + loader.mint.last_id(), + 1, + 0xD0, // corresponds to the C structure size + program.program.pubkey(), + 0, + ); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + + Dashboard { dashboard } + } + + pub fn update(&self, loader: &Loader, program: &Program, game: &Pubkey) { + let tx = Transaction::new( + &self.dashboard, + &[self.dashboard.pubkey(), *game], + program.program.pubkey(), + vec![], + loader.mint.last_id(), + 0, + ); + check_tx_results( + &loader.bank, + &tx, + loader.bank.process_transactions(&vec![tx.clone()]), + ); + } + + pub fn get_game(&self, loader: &Loader, since_last: usize) -> Vec { + let userdata = loader + .bank + .get_account(&self.dashboard.pubkey()) + .unwrap() + .userdata; + + // TODO serialize + let last_game = userdata[192] as usize; + let this_game = (last_game + since_last * 4) % 5; + let start = 32 + this_game * 32; + let end = start + 32; + + loader + .bank + .get_account(&self.dashboard.pubkey()) + .unwrap() + .userdata[start..end] + .to_vec() + } + + pub fn get_pending(&self, loader: &Loader) -> Vec { + loader + .bank + .get_account(&self.dashboard.pubkey()) + .unwrap() + .userdata[0..32] + .to_vec() + } +} + +#[cfg(feature = "bpf_c")] +#[test] +fn test_program_bpf_file_tictactoe_c() { + logger::setup(); + + let loader = Loader::new_dynamic("bpf_loader"); + let name = String::from("tictactoe_c"); + let userdata = name.as_bytes().to_vec(); + let program = Program::new(&loader, userdata, 56); + let player_x = Pubkey::new(&[0xA; 32]); + let player_y = Pubkey::new(&[0xB; 32]); + + let ttt = TicTacToe::new(&loader, &program); + ttt.init(&loader, &program, &player_x); + ttt.command(&loader, &program, Command::Join(0xAABBCCDD), &player_y); + ttt.command(&loader, &program, Command::Move(1, 1), &player_x); + ttt.command(&loader, &program, Command::Move(0, 0), &player_y); + ttt.command(&loader, &program, Command::Move(2, 0), &player_x); + ttt.command(&loader, &program, Command::Move(0, 2), &player_y); + ttt.command(&loader, &program, Command::Move(2, 2), &player_x); + ttt.command(&loader, &program, Command::Move(0, 1), &player_y); + + assert_eq!(player_x.as_ref(), &ttt.get_player_x(&loader)[..]); // validate x's key + assert_eq!(player_y.as_ref(), &ttt.get_player_y(&loader)[..]); // validate o's key + assert_eq!([4, 0, 0, 0], ttt.game(&loader)[..]); // validate that o won +} + +#[cfg(feature = "bpf_c")] +#[test] +fn test_program_bpf_file_tictactoe_dashboard_c() { + logger::setup(); + + let loader = Loader::new_dynamic("bpf_loader"); + let name = String::from("tictactoe_c"); + let userdata = name.as_bytes().to_vec(); + let ttt_program = Program::new(&loader, userdata, 56); + let player_x = Pubkey::new(&[0xA; 32]); + let player_y = Pubkey::new(&[0xB; 32]); + + let ttt1 = TicTacToe::new(&loader, &ttt_program); + ttt1.init(&loader, &ttt_program, &player_x); + ttt1.command(&loader, &ttt_program, Command::Join(0xAABBCCDD), &player_y); + ttt1.command(&loader, &ttt_program, Command::Move(1, 1), &player_x); + ttt1.command(&loader, &ttt_program, Command::Move(0, 0), &player_y); + ttt1.command(&loader, &ttt_program, Command::Move(2, 0), &player_x); + ttt1.command(&loader, &ttt_program, Command::Move(0, 2), &player_y); + ttt1.command(&loader, &ttt_program, Command::Move(2, 2), &player_x); + ttt1.command(&loader, &ttt_program, Command::Move(0, 1), &player_y); + + let ttt2 = TicTacToe::new(&loader, &ttt_program); + ttt2.init(&loader, &ttt_program, &player_x); + ttt2.command(&loader, &ttt_program, Command::Join(0xAABBCCDD), &player_y); + ttt2.command(&loader, &ttt_program, Command::Move(1, 1), &player_x); + ttt2.command(&loader, &ttt_program, Command::Move(0, 0), &player_y); + ttt2.command(&loader, &ttt_program, Command::Move(2, 0), &player_x); + ttt2.command(&loader, &ttt_program, Command::Move(0, 2), &player_y); + ttt2.command(&loader, &ttt_program, Command::Move(2, 2), &player_x); + ttt2.command(&loader, &ttt_program, Command::Move(0, 1), &player_y); + + let ttt3 = TicTacToe::new(&loader, &ttt_program); + ttt3.init(&loader, &ttt_program, &player_x); + + let name = String::from("tictactoe_dashboard_c"); + let userdata = name.as_bytes().to_vec(); + let dashboard_program = Program::new(&loader, userdata, 56); + let dashboard = Dashboard::new(&loader, &dashboard_program); + + dashboard.update(&loader, &dashboard_program, &ttt1.id()); + dashboard.update(&loader, &dashboard_program, &ttt2.id()); + dashboard.update(&loader, &dashboard_program, &ttt3.id()); + + assert_eq!(ttt1.id().as_ref(), &dashboard.get_game(&loader, 1)[..]); + assert_eq!(ttt2.id().as_ref(), &dashboard.get_game(&loader, 0)[..]); + assert_eq!(ttt3.id().as_ref(), &dashboard.get_pending(&loader)[..]); }