|
1 | 1 | const std = @import("std"); |
2 | 2 | const PublicKey = @import("public_key.zig").PublicKey; |
| 3 | +const testing = std.testing; |
3 | 4 |
|
4 | 5 | pub const ACCOUNT_DATA_PADDING = 10 * 1024; |
5 | 6 |
|
6 | 7 | pub const Account = struct { |
7 | 8 | pub const DATA_HEADER = 88; |
8 | 9 | /// A Solana account sliced from what is provided as inputs to the BPF virtual machine. |
9 | | - pub const Data = packed struct { |
| 10 | + pub const Data = extern struct { |
10 | 11 | duplicate_index: u8, |
11 | 12 | is_signer: u8, |
12 | 13 | is_writable: u8, |
@@ -44,16 +45,16 @@ pub const Account = struct { |
44 | 45 | data_len: u64, |
45 | 46 | data: [*]u8, |
46 | 47 | owner_id: *const PublicKey, |
47 | | - rent_epoch: u64, |
48 | | - is_signer: bool, |
49 | | - is_writable: bool, |
50 | | - is_executable: bool, |
| 48 | + unused: u64 = undefined, |
| 49 | + is_signer: u8, |
| 50 | + is_writable: u8, |
| 51 | + is_executable: u8, |
51 | 52 | }; |
52 | 53 |
|
53 | 54 | ptr: *Account.Data, |
54 | 55 |
|
55 | 56 | pub fn fromDataPtr(ptr: *Account.Data) Account { |
56 | | - return Account { .ptr = ptr }; |
| 57 | + return Account{ .ptr = ptr }; |
57 | 58 | } |
58 | 59 |
|
59 | 60 | pub fn id(self: Account) PublicKey { |
@@ -107,18 +108,37 @@ pub const Account = struct { |
107 | 108 |
|
108 | 109 | pub fn info(self: Account) Account.Info { |
109 | 110 | const data_ptr = @as([*]u8, @ptrFromInt(@intFromPtr(self.ptr))) + DATA_HEADER; |
110 | | - const rent_epoch = @as(*u64, @ptrFromInt(std.mem.alignForward(u64, @intFromPtr(self.ptr) + self.ptr.data_len + ACCOUNT_DATA_PADDING, @alignOf(u64)))); |
111 | 111 |
|
112 | 112 | return .{ |
113 | 113 | .id = &self.ptr.id, |
114 | 114 | .lamports = &self.ptr.lamports, |
115 | 115 | .data_len = self.ptr.data_len, |
116 | 116 | .data = data_ptr, |
117 | 117 | .owner_id = &self.ptr.owner_id, |
118 | | - .rent_epoch = rent_epoch.*, |
119 | 118 | .is_signer = self.ptr.is_signer, |
120 | 119 | .is_writable = self.ptr.is_writable, |
121 | 120 | .is_executable = self.ptr.is_executable, |
122 | 121 | }; |
123 | 122 | } |
124 | 123 | }; |
| 124 | + |
| 125 | +test "account: create info from account" { |
| 126 | + var data: Account.Data = .{ |
| 127 | + .duplicate_index = 255, |
| 128 | + .is_signer = 1, |
| 129 | + .is_writable = 1, |
| 130 | + .is_executable = 0, |
| 131 | + .original_data_len = 10, |
| 132 | + .id = PublicKey.from(.{1} ** 32), |
| 133 | + .owner_id = PublicKey.from(.{2} ** 32), |
| 134 | + .lamports = 1, |
| 135 | + .data_len = 10, |
| 136 | + }; |
| 137 | + const account: Account = .{ .ptr = &data }; |
| 138 | + const info = account.info(); |
| 139 | + try testing.expectEqual(info.id, &data.id); |
| 140 | + try testing.expectEqual(info.is_signer, data.is_signer); |
| 141 | + try testing.expectEqual(info.is_writable, data.is_writable); |
| 142 | + try testing.expectEqual(info.lamports, &data.lamports); |
| 143 | + try testing.expectEqual(info.data, account.data().ptr); |
| 144 | +} |
0 commit comments