-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add C version of CPI program (#6)
* feat: add C CPI program * add forgotten CI step & more comments
- Loading branch information
1 parent
6071b5c
commit 89efc3b
Showing
5 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
LOCAL_PATH := ../../solana-c-sdk/c/ | ||
|
||
.PHONY: all clean | ||
|
||
SRC_DIR ?= ./src | ||
OUT_DIR ?= ./out | ||
|
||
all: $(OUT_DIR)/solana_program_rosetta_cpi.so | ||
|
||
clean: | ||
rm -rf $(OUT_DIR) | ||
|
||
LLVM_DIR = $(LOCAL_PATH)../dependencies/platform-tools/llvm | ||
LLVM_SYSTEM_INC_DIRS := $(LLVM_DIR)/lib/clang/17/include | ||
STD_INC_DIRS := $(LLVM_DIR)/include | ||
STD_LIB_DIRS := $(LLVM_DIR)/lib | ||
|
||
CC := $(LLVM_DIR)/bin/clang | ||
LLD := $(LLVM_DIR)/bin/ld.lld | ||
|
||
SYSTEM_INC_DIRS := \ | ||
$(LOCAL_PATH)inc \ | ||
$(LLVM_SYSTEM_INC_DIRS) \ | ||
|
||
C_FLAGS := \ | ||
-Werror \ | ||
-O2 \ | ||
-fno-builtin \ | ||
-std=c17 \ | ||
$(addprefix -isystem,$(SYSTEM_INC_DIRS)) \ | ||
$(addprefix -I,$(STD_INC_DIRS)) \ | ||
-target sbf \ | ||
-fPIC | ||
|
||
SBF_LLD_FLAGS := \ | ||
-z notext \ | ||
-shared \ | ||
--Bdynamic \ | ||
$(LOCAL_PATH)sbf.ld \ | ||
--entry entrypoint \ | ||
-L $(STD_LIB_DIRS) \ | ||
-lc \ | ||
|
||
$(OUT_DIR)/main.o: $(SRC_DIR)/main.c | ||
mkdir -p $(OUT_DIR) | ||
$(CC) $(C_FLAGS) -o $(OUT_DIR)/main.o -c $(SRC_DIR)/main.c | ||
|
||
$(OUT_DIR)/solana_program_rosetta_cpi.so: $(OUT_DIR)/main.o | ||
$(LLD) $(SBF_LLD_FLAGS) -o $(OUT_DIR)/solana_program_rosetta_cpi.so $(OUT_DIR)/main.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @brief C-based CPI BPF program | ||
*/ | ||
#include <solana_sdk.h> | ||
|
||
/// Amount of bytes of account data to allocate | ||
#define SIZE 42 | ||
|
||
extern uint64_t entrypoint(const uint8_t *input) | ||
{ | ||
SolAccountInfo accounts[2]; | ||
SolParameters params = (SolParameters){.ka = accounts}; | ||
|
||
if (!sol_deserialize(input, ¶ms, SOL_ARRAY_SIZE(accounts))) | ||
{ | ||
return ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
SolAccountInfo allocated_info = params.ka[0]; | ||
SolAccountInfo system_program_info = params.ka[1]; | ||
|
||
uint8_t seed[] = {'Y', 'o', 'u', ' ', 'p', 'a', 's', 's', | ||
' ', 'b', 'u', 't', 't', 'e', 'r'}; | ||
const SolSignerSeed seeds[] = {{seed, SOL_ARRAY_SIZE(seed)}, | ||
{¶ms.data[0], 1}}; | ||
|
||
const SolSignerSeeds signers_seeds[] = {{seeds, SOL_ARRAY_SIZE(seeds)}}; | ||
|
||
SolPubkey expected_allocated_key; | ||
if (SUCCESS != sol_create_program_address(seeds, SOL_ARRAY_SIZE(seeds), | ||
params.program_id, | ||
&expected_allocated_key)) | ||
{ | ||
return ERROR_INVALID_INSTRUCTION_DATA; | ||
} | ||
|
||
// allocated key does not match the derived address | ||
if (!SolPubkey_same(&expected_allocated_key, allocated_info.key)) | ||
{ | ||
return ERROR_INVALID_ARGUMENT; | ||
} | ||
|
||
SolAccountMeta arguments[] = {{allocated_info.key, true, true}}; | ||
uint8_t data[4 + 8]; // Enough room for the Allocate instruction | ||
*(uint16_t *)data = 8; // Allocate instruction enum value | ||
*(uint64_t *)(data + 4) = SIZE; // Size to allocate | ||
const SolInstruction instruction = {system_program_info.key, arguments, | ||
SOL_ARRAY_SIZE(arguments), data, | ||
SOL_ARRAY_SIZE(data)}; | ||
|
||
return sol_invoke_signed(&instruction, params.ka, params.ka_num, | ||
signers_seeds, SOL_ARRAY_SIZE(signers_seeds)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters