Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ solana-zig/
zig-cache/
zig-out/
.zig-cache/
.DS_Store
14 changes: 10 additions & 4 deletions program-test/pubkey/main.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const sol = @import("solana_program_sdk");

export fn entrypoint(input: [*]u8) u64 {
const context = sol.Context.load(input) catch return 1;
sol.print("Hello zig program {s}", .{context.program_id});
return 0;
fn processInstruction(program_id: *sol.PublicKey, accounts: []sol.Account, data: []const u8) sol.ProgramError!void {
_ = accounts;
_ = data;
sol.print("Hello zig program {s}", .{program_id});
return;
}

// Declare the program entrypoint
comptime {
sol.entrypoint(processInstruction);
}
2 changes: 1 addition & 1 deletion program-test/tests/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn program_test() -> ProgramTest {
#[tokio::test]
async fn call() {
let pt = program_test();
let mut context = pt.start_with_context().await;
let context = pt.start_with_context().await;
let blockhash = context.banks_client.get_latest_blockhash().await.unwrap();
let transaction = Transaction::new_signed_with_payer(
&[Instruction {
Expand Down
18 changes: 14 additions & 4 deletions src/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ const PublicKey = @import("public_key.zig").PublicKey;

pub const Context = struct {
num_accounts: u64,
accounts: [64]Account,
// MAX support parse account number is 64
accounts: [MAX_ACCOUNTS]Account,
data: []const u8,
program_id: *align(1) PublicKey,

// FUTURE: maybe future change this number
const MAX_ACCOUNTS = 64;

pub fn load(input: [*]u8) !Context {
var ptr: [*]u8 = input;

// Get the number of accounts
const num_accounts: *u64 = @ptrCast(@alignCast(ptr));
// Check if the number of accounts is within the supported range
if (num_accounts.* > MAX_ACCOUNTS) {
return error.MaxAccountsExceeded;
}
// next ptr point to account data
ptr += @sizeOf(u64);

// Account Parse
var i: usize = 0;
var accounts: [64]Account = undefined;
while (i < num_accounts.*) {
var accounts: [MAX_ACCOUNTS]Account = undefined;
while (i < num_accounts.*) : (i += 1) {
const data: *Account.Data = @ptrCast(@alignCast(ptr));
if (data.duplicate_index != std.math.maxInt(u8)) {
ptr += @sizeOf(u64);
Expand All @@ -29,7 +40,6 @@ pub const Context = struct {
ptr += Account.DATA_HEADER + data.data_len + ACCOUNT_DATA_PADDING + @sizeOf(u64);
ptr = @ptrFromInt(std.mem.alignForward(u64, @intFromPtr(ptr), @alignOf(u64)));
}
i += 1;
}

const data_len: *u64 = @ptrCast(@alignCast(ptr));
Expand Down
22 changes: 22 additions & 0 deletions src/entrypoint.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const PublicKey = @import("public_key.zig").PublicKey;
const Account = @import("account.zig").Account;
const ProgramError = @import("error.zig").ProgramError;
const Context = @import("context.zig").Context;

const processInstruction = fn (program_id: *PublicKey, accounts: []Account, data: []const u8) ProgramError!void;

pub fn declareEntrypoint(comptime process_instruction: processInstruction) void {
const S = struct {
pub export fn entrypoint(input: [*]u8) callconv(.C) u64 {
var context = Context.load(input) catch return 1;
process_instruction(context.program_id, context.accounts[0..context.num_accounts], context.data) catch |err| return @intFromError(err);
return 0;
}
};
_ = &S.entrypoint;
}

/// Helper macro-like function for simple entrypoint declaration
pub inline fn entrypoint(comptime process_instruction: processInstruction) void {
declareEntrypoint(process_instruction);
}
6 changes: 6 additions & 0 deletions src/error.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const ProgramError = error{
AlreadyInUse,
InvalidAccountType,
Uninitialized,
IncorrectSize,
};
Comment on lines +1 to +6
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to add these and have them work similarly to the program error in the Rust SDK, may as well just copy all the variants at https://github.com/anza-xyz/solana-sdk/blob/2d88eb491e52705ec73ee8204dc41d510c298909/program-error/src/lib.rs#L57, but that will require also setting the values individually, ie:

const ProgramError = enum(u64) {
    InvalidArgument = 1 << 32,
    InvalidInstructionData = 2 << 32,
};

And so on. That'll be a bit more conformant than this error type.

On the other hand, it's not a proper zig error, which makes me think we might want to omit this entirely, and allow processInstruction to return any error. What do you think?

3 changes: 3 additions & 0 deletions src/rent.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub const Rent = struct {
log.print("failed to get rent sysvar: error code {}", .{result});
return error.Unexpected;
}
} else {
log.log("cannot get rent data in non-bpf context");
return error.Unexpected;
}
return rent;
}
Expand Down
2 changes: 2 additions & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub usingnamespace @import("clock.zig");
pub usingnamespace @import("rent.zig");
pub usingnamespace @import("log.zig");
pub usingnamespace @import("hash.zig");
pub usingnamespace @import("error.zig");
pub usingnamespace @import("entrypoint.zig");

pub const blake3 = @import("blake3.zig");
//pub const system_program = @import("system_program.zig");
Expand Down