-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathaccount.zig
183 lines (155 loc) · 5.12 KB
/
account.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const sig = @import("../sig.zig");
const Pubkey = sig.core.Pubkey;
const Epoch = sig.core.Epoch;
const AccountInFile = sig.accounts_db.accounts_file.AccountInFile;
pub const Account = struct {
lamports: u64,
data: []u8,
owner: Pubkey,
executable: bool,
rent_epoch: Epoch,
pub fn deinit(self: Account, allocator: std.mem.Allocator) void {
allocator.free(self.data);
}
pub fn initRandom(allocator: std.mem.Allocator, random: std.Random, data_len: usize) !Account {
const data = try allocator.alloc(u8, data_len);
random.bytes(data);
return .{
.lamports = random.int(u64),
.data = data,
.owner = Pubkey.initRandom(random),
.executable = random.boolean(),
.rent_epoch = random.int(Epoch),
};
}
// creates a copy of the account. most important is the copy of the data slice.
pub fn clone(self: *const Account, allocator: std.mem.Allocator) !Account {
const data = try allocator.dupe(u8, self.data);
return .{
.lamports = self.lamports,
.data = data,
.owner = self.owner,
.executable = self.executable,
.rent_epoch = self.rent_epoch,
};
}
pub fn equals(self: *const Account, other: *const Account) bool {
return std.mem.eql(u8, self.data, other.data) and
self.lamports == other.lamports and
self.owner.equals(&other.owner) and
self.executable == other.executable and
self.rent_epoch == other.rent_epoch;
}
/// gets the snapshot size of the account (when serialized)
pub fn getSizeInFile(self: *const Account) usize {
return std.mem.alignForward(
usize,
AccountInFile.STATIC_SIZE + self.data.len,
@sizeOf(u64),
);
}
/// computes the hash of the account
pub fn hash(self: *const Account, pubkey: *const Pubkey) Hash {
return hashAccount(
self.lamports,
self.data,
&self.owner.data,
self.executable,
self.rent_epoch,
&pubkey.data,
);
}
/// writes account to buf in snapshot format
pub fn writeToBuf(self: *const Account, pubkey: *const Pubkey, buf: []u8) usize {
var offset: usize = 0;
const storage_info = AccountInFile.StorageInfo{
.write_version_obsolete = 0,
.data_len = self.data.len,
.pubkey = pubkey.*,
};
offset += storage_info.writeToBuf(buf[offset..]);
const account_info = AccountInFile.AccountInfo{
.lamports = self.lamports,
.rent_epoch = self.rent_epoch,
.owner = self.owner,
.executable = self.executable,
};
offset += account_info.writeToBuf(buf[offset..]);
const account_hash = self.hash(pubkey);
@memcpy(buf[offset..(offset + 32)], &account_hash.data);
offset += 32;
offset = std.mem.alignForward(usize, offset, @sizeOf(u64));
@memcpy(buf[offset..(offset + self.data.len)], self.data);
offset += self.data.len;
offset = std.mem.alignForward(usize, offset, @sizeOf(u64));
return offset;
}
};
/// helper function for writing to memory
pub fn writeIntLittleMem(
x: anytype,
memory: []u8,
) usize {
const Tx = @TypeOf(x);
const x_size: usize = @bitSizeOf(Tx) / 8;
std.mem.writeInt(Tx, memory[0..x_size], x, .little);
return x_size;
}
const std = @import("std");
const Blake3 = std.crypto.hash.Blake3;
const Hash = @import("hash.zig").Hash;
pub fn hashAccount(
lamports: u64,
data: []const u8,
owner_pubkey_data: []const u8,
executable: bool,
rent_epoch: u64,
address_pubkey_data: []const u8,
) Hash {
var hasher = Blake3.init(.{});
var hash_buf: [32]u8 = undefined;
var int_buf: [8]u8 = undefined;
std.mem.writeInt(u64, &int_buf, lamports, .little);
hasher.update(&int_buf);
std.mem.writeInt(u64, &int_buf, rent_epoch, .little);
hasher.update(&int_buf);
hasher.update(data);
if (executable) {
hasher.update(&[_]u8{1});
} else {
hasher.update(&[_]u8{0});
}
hasher.update(owner_pubkey_data);
hasher.update(address_pubkey_data);
hasher.final(&hash_buf);
const hash = Hash{
.data = hash_buf,
};
return hash;
}
test "core.account: test account hash matches rust" {
var data: [3]u8 = .{ 1, 2, 3 };
var account: Account = .{
.lamports = 10,
.data = &data,
.owner = Pubkey.ZEROES,
.executable = false,
.rent_epoch = 20,
};
const pubkey = Pubkey.ZEROES;
const hash = hashAccount(
account.lamports,
account.data,
&account.owner.data,
account.executable,
account.rent_epoch,
&pubkey.data,
);
const expected_hash: [32]u8 = .{
170, 75, 87, 73, 60, 156, 174, 14, 105,
6, 129, 108, 167, 156, 166, 213, 28, 4,
163, 187, 252, 155, 24, 253, 158, 13, 86,
100, 103, 89, 232, 28,
};
try std.testing.expect(std.mem.eql(u8, &expected_hash, &hash.data));
}