-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.zig
executable file
·144 lines (121 loc) · 5.9 KB
/
context.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const std = @import("std");
const sol = @import("sol.zig");
pub const Context = struct {
num_accounts: usize,
accounts: [*]u8,
data: []const u8,
program_id: *sol.PublicKey,
pub fn load(input: [*]u8) !Context {
var ptr: [*]u8 = input;
const num_accounts = std.mem.bytesToValue(usize, ptr[0..@sizeOf(usize)]);
ptr += @sizeOf(usize);
const accounts: [*]u8 = ptr;
var i: usize = 0;
while (i < num_accounts) : (i += 1) {
const account: *align(1) sol.Account.Data = @ptrCast(*align(1) sol.Account.Data, ptr);
if (account.duplicate_index != std.math.maxInt(u8)) {
ptr += @sizeOf(usize);
continue;
}
ptr += @sizeOf(sol.Account.Data);
ptr = @intToPtr([*]u8, std.mem.alignForward(@ptrToInt(ptr + account.data_len + 10 * 1024), @alignOf(usize)));
ptr += @sizeOf(u64);
}
const data_len = std.math.cast(usize, std.mem.bytesToValue(u64, ptr[0..@sizeOf(u64)])) orelse return error.DataTooLarge;
ptr += @sizeOf(u64);
const data = ptr[0..data_len];
ptr += data_len;
const program_id = @ptrCast(*sol.PublicKey, ptr);
ptr += @sizeOf(sol.PublicKey);
return Context{
.num_accounts = num_accounts,
.accounts = accounts,
.data = data,
.program_id = program_id,
};
}
pub fn loadAccountsAlloc(self: Context, comptime Accounts: type, gpa: std.mem.Allocator) !*Accounts {
const accounts = try gpa.create(Accounts);
errdefer gpa.destroy(accounts);
try self.populateAccounts(Accounts, accounts);
return accounts;
}
pub fn loadAccounts(self: Context, comptime Accounts: type) !Accounts {
var accounts: Accounts = undefined;
try self.populateAccounts(Accounts, &accounts);
return accounts;
}
fn populateAccounts(self: Context, comptime Accounts: type, accounts: *Accounts) !void {
comptime var min_accounts = 0;
comptime var last_field_is_slice = false;
comptime {
inline for (@typeInfo(Accounts).Struct.fields) |field, i| {
switch (field.type) {
sol.Account => min_accounts += 1,
[]sol.Account => {
if (i != @typeInfo(Accounts).Struct.fields.len - 1) {
@compileError("Only the last field of an 'Accounts' struct may be a slice of accounts.");
}
last_field_is_slice = true;
},
else => @compileError(""),
}
}
}
if (self.num_accounts < min_accounts) {
return error.NotEnoughAccounts;
}
var ptr: [*]u8 = self.accounts;
@setEvalBranchQuota(100_000);
inline for (@typeInfo(Accounts).Struct.fields) |field| {
switch (field.type) {
sol.Account => {
const account: *align(1) sol.Account.Data = @ptrCast(*align(1) sol.Account.Data, ptr);
if (account.duplicate_index != std.math.maxInt(u8)) {
inline for (@typeInfo(Accounts).Struct.fields) |cloned_field, cloned_index| {
if (cloned_field.type == sol.Account) {
if (account.duplicate_index == cloned_index) {
@field(accounts, field.name) = @field(accounts, cloned_field.name);
}
}
}
ptr += @sizeOf(usize);
} else {
const start = @ptrToInt(ptr);
ptr += @sizeOf(sol.Account.Data);
ptr = @intToPtr([*]u8, std.mem.alignForward(@ptrToInt(ptr + account.data_len + 10 * 1024), @alignOf(usize)));
ptr += @sizeOf(u64);
const end = @ptrToInt(ptr);
@field(accounts, field.name) = .{ .ptr = @alignCast(@alignOf(sol.Account.Data), account), .len = end - start };
}
},
[]sol.Account => {
const remaining_accounts = try sol.allocator.alloc(sol.Account, self.num_accounts + 1 - @typeInfo(Accounts).Struct.fields.len);
errdefer sol.allocator.free(remaining_accounts);
for (remaining_accounts) |*remaining_account| {
const account: *align(1) sol.Account.Data = @ptrCast(*align(1) sol.Account.Data, ptr);
if (account.duplicate_index != std.math.maxInt(u8)) {
inline for (@typeInfo(Accounts).Struct.fields) |cloned_field, cloned_index| {
if (cloned_field.type == sol.Account) {
if (account.duplicate_index == cloned_index) {
remaining_account.* = @field(accounts, cloned_field.name);
}
}
}
ptr += @sizeOf(usize);
} else {
const start = @ptrToInt(ptr);
ptr += @sizeOf(sol.Account.Data);
ptr = @intToPtr([*]u8, std.mem.alignForward(@ptrToInt(ptr + account.data_len + 10 * 1024), @alignOf(usize)));
ptr += @sizeOf(u64);
const end = @ptrToInt(ptr);
remaining_account.* = .{ .ptr = @alignCast(@alignOf(sol.Account.Data), account), .len = end - start };
}
}
@field(accounts, field.name) = remaining_accounts;
},
else => @compileError(""),
}
}
}
};