-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpubkey_index_map.zig
More file actions
163 lines (139 loc) · 5.06 KB
/
pubkey_index_map.zig
File metadata and controls
163 lines (139 loc) · 5.06 KB
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
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const PUBKEY_INDEX_MAP_KEY_SIZE = 48;
// max value of u32 is 4,294,967,295 which is enough for Ethereum
// this value is the same to napi-rs binding, see https://github.com/ChainSafe/pubkey-index-map/blob/09cf357940e43742bcb29c1c35472d82a8aa52cb/src/lib.rs#L9
pub const Val = u32;
pub const Key = [PUBKEY_INDEX_MAP_KEY_SIZE]u8;
const AutoHashMap = std.AutoHashMap(Key, Val);
/// a generic implementation for both zig application and Bun ffi
pub const PubkeyIndexMap = struct {
// this HashMap copies key/value using its own allocator
// this duplicates all items at Bun side
map: AutoHashMap,
pub fn init(allocator: Allocator) !*PubkeyIndexMap {
const instance = try allocator.create(PubkeyIndexMap);
instance.* = .{ .map = AutoHashMap.init(allocator) };
return instance;
}
pub fn deinit(self: *PubkeyIndexMap) void {
const allocator = self.map.allocator;
self.map.deinit();
allocator.destroy(self);
}
pub fn set(self: *PubkeyIndexMap, key: []const u8, value: Val) !void {
if (key.len != PUBKEY_INDEX_MAP_KEY_SIZE) {
return error.InvalidKeyLen;
}
var fixed_key: Key = undefined;
@memcpy(&fixed_key, key);
try self.map.put(fixed_key, value);
}
pub fn get(self: *const PubkeyIndexMap, key: []const u8) ?Val {
if (key.len != PUBKEY_INDEX_MAP_KEY_SIZE) {
return null;
}
var fixed_key: Key = undefined;
@memcpy(&fixed_key, key);
return self.map.get(fixed_key);
}
pub fn has(self: *const PubkeyIndexMap, key: []const u8) bool {
if (key.len != PUBKEY_INDEX_MAP_KEY_SIZE) {
return false;
}
var fixed_key: Key = undefined;
@memcpy(&fixed_key, key);
return self.map.getKey(fixed_key) != null;
}
pub fn delete(self: *PubkeyIndexMap, key: []const u8) bool {
if (key.len != PUBKEY_INDEX_MAP_KEY_SIZE) {
return false;
}
var fixed_key: Key = undefined;
@memcpy(&fixed_key, key);
return self.map.remove(fixed_key);
}
pub fn size(self: *const PubkeyIndexMap) u32 {
return self.map.count();
}
pub fn clear(self: *PubkeyIndexMap) void {
self.map.clearAndFree();
}
pub fn clone(self: *const PubkeyIndexMap) !*PubkeyIndexMap {
const allocator = self.map.allocator;
const instance = try allocator.create(PubkeyIndexMap);
instance.* = .{ .map = try self.map.clone() };
return instance;
}
};
test "PubkeyIndexMap" {
const allocator = std.testing.allocator;
const instance = try PubkeyIndexMap.init(allocator);
defer instance.deinit();
var key: [PUBKEY_INDEX_MAP_KEY_SIZE]u8 = [_]u8{5} ** PUBKEY_INDEX_MAP_KEY_SIZE;
const value = 42;
try instance.set(key[0..], value);
var result = instance.get(key[0..]);
if (result) |v| {
try std.testing.expectEqual(v, value);
} else {
try std.testing.expect(false);
}
// C pointer
var key_ptr: [*c]const u8 = key[0..].ptr;
result = instance.get(key_ptr[0..key.len]);
if (result) |v| {
try std.testing.expectEqual(v, value);
} else {
try std.testing.expect(false);
}
key[1] = 1; // change key
result = instance.get(key[0..]);
try std.testing.expect(result == null);
// C pointer
result = instance.get(key_ptr[0..key.len]);
try std.testing.expect(result == null);
// new instance with same value
const key2: [PUBKEY_INDEX_MAP_KEY_SIZE]u8 = [_]u8{5} ** PUBKEY_INDEX_MAP_KEY_SIZE;
result = instance.get(key2[0..]);
if (result) |v| {
try std.testing.expectEqual(v, value);
} else {
try std.testing.expect(false);
}
// C pointer
key_ptr = key2[0..].ptr;
result = instance.get(key_ptr[0..key.len]);
if (result) |v| {
try std.testing.expectEqual(v, value);
} else {
try std.testing.expect(false);
}
// has
try std.testing.expect(instance.has(key_ptr[0..key.len]));
// size
try std.testing.expectEqual(1, instance.size());
try instance.set(([_]u8{255} ** PUBKEY_INDEX_MAP_KEY_SIZE)[0..], 100);
try std.testing.expectEqual(2, instance.size());
// delete
var del_res = instance.delete(([_]u8{254} ** PUBKEY_INDEX_MAP_KEY_SIZE)[0..]);
try std.testing.expect(!del_res);
del_res = instance.delete(([_]u8{255} ** PUBKEY_INDEX_MAP_KEY_SIZE)[0..]);
try std.testing.expect(del_res);
try std.testing.expectEqual(1, instance.size());
// clone
const clone_instance = try instance.clone();
defer clone_instance.deinit();
try std.testing.expectEqual(1, clone_instance.size());
result = clone_instance.get(key_ptr[0..key.len]);
if (result) |v| {
try std.testing.expectEqual(v, value);
} else {
try std.testing.expect(false);
}
// clear
instance.clear();
try std.testing.expectEqual(0, instance.size());
// cloned instance is not affected
try std.testing.expectEqual(1, clone_instance.size());
}