Skip to content

Commit 70126d9

Browse files
committed
Add more info for context load function this function is parse function
1 parent 99c8fce commit 70126d9

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ solana-zig/
66
zig-cache/
77
zig-out/
88
.zig-cache/
9+
.DS_Store

src/context.zig

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,30 @@ const PublicKey = @import("public_key.zig").PublicKey;
77

88
pub const Context = struct {
99
num_accounts: u64,
10-
accounts: [64]Account,
10+
// MAX support parse account number is 64
11+
accounts: [MAX_ACCOUNTS]Account,
1112
data: []const u8,
1213
program_id: *align(1) PublicKey,
1314

15+
// FUTURE: maybe future change this number
16+
const MAX_ACCOUNTS = 64;
17+
1418
pub fn load(input: [*]u8) !Context {
1519
var ptr: [*]u8 = input;
1620

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

30+
// Account Parse
2031
var i: usize = 0;
21-
var accounts: [64]Account = undefined;
22-
while (i < num_accounts.*) {
32+
var accounts: [MAX_ACCOUNTS]Account = undefined;
33+
while (i < num_accounts.*) : (i += 1) {
2334
const data: *Account.Data = @ptrCast(@alignCast(ptr));
2435
if (data.duplicate_index != std.math.maxInt(u8)) {
2536
ptr += @sizeOf(u64);
@@ -29,7 +40,6 @@ pub const Context = struct {
2940
ptr += Account.DATA_HEADER + data.data_len + ACCOUNT_DATA_PADDING + @sizeOf(u64);
3041
ptr = @ptrFromInt(std.mem.alignForward(u64, @intFromPtr(ptr), @alignOf(u64)));
3142
}
32-
i += 1;
3343
}
3444

3545
const data_len: *u64 = @ptrCast(@alignCast(ptr));

src/rent.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ pub const Rent = struct {
5858
log.print("failed to get rent sysvar: error code {}", .{result});
5959
return error.Unexpected;
6060
}
61+
} else {
62+
log.log("cannot get rent data in non-bpf context");
63+
return error.Unexpected;
6164
}
6265
return rent;
6366
}

0 commit comments

Comments
 (0)