-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathactive_set.zig
196 lines (161 loc) · 6.56 KB
/
active_set.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
184
185
186
187
188
189
190
191
192
193
194
195
196
const std = @import("std");
const network = @import("zig-network");
const sig = @import("../sig.zig");
const KeyPair = std.crypto.sign.Ed25519.KeyPair;
const EndPoint = network.EndPoint;
const Pubkey = sig.core.Pubkey;
const Bloom = sig.bloom.Bloom;
const SignedGossipData = sig.gossip.data.SignedGossipData;
const LegacyContactInfo = sig.gossip.data.LegacyContactInfo;
const ThreadSafeContactInfo = sig.gossip.data.ThreadSafeContactInfo;
const GossipTable = sig.gossip.table.GossipTable;
const getWallclockMs = sig.time.getWallclockMs;
const shuffleFirstN = sig.utils.slice.shuffleFirstN;
const NUM_ACTIVE_SET_ENTRIES: usize = 25;
pub const GOSSIP_PUSH_FANOUT: usize = 6;
const MIN_NUM_BLOOM_ITEMS: usize = 512;
const BLOOM_FALSE_RATE: f64 = 0.1;
const BLOOM_MAX_BITS: usize = 1024 * 8 * 4;
pub const ActiveSet = struct {
// store pubkeys as keys in gossip table bc the data can change
// For each peer, a bloom filter is used to store pruned origins
peers: std.AutoHashMap(Pubkey, Bloom),
allocator: std.mem.Allocator,
const Self = @This();
pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.peers = std.AutoHashMap(Pubkey, Bloom).init(allocator),
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void {
var iter = self.peers.iterator();
while (iter.next()) |entry| {
entry.value_ptr.deinit();
}
self.peers.deinit();
}
pub fn len(self: *const Self) u32 {
return self.peers.count();
}
pub fn initRotate(
self: *Self,
random: std.Random,
peers: []ThreadSafeContactInfo,
) error{OutOfMemory}!void {
// clear the existing
var iter = self.peers.iterator();
while (iter.next()) |entry| {
entry.value_ptr.deinit();
}
self.peers.clearRetainingCapacity();
if (peers.len == 0) {
return;
}
const size = @min(peers.len, NUM_ACTIVE_SET_ENTRIES);
shuffleFirstN(random, ThreadSafeContactInfo, peers, size);
const bloom_num_items = @max(peers.len, MIN_NUM_BLOOM_ITEMS);
for (0..size) |i| {
const entry = try self.peers.getOrPut(peers[i].pubkey);
if (entry.found_existing == false) {
// *full* hard restart on blooms -- labs doesnt do this - bug?
const bloom = try Bloom.initRandom(
self.allocator,
random,
bloom_num_items,
BLOOM_FALSE_RATE,
BLOOM_MAX_BITS,
);
entry.value_ptr.* = bloom;
}
}
}
pub fn prune(self: *Self, from: Pubkey, origin: Pubkey) void {
// we only prune peers which we are sending push messages to
if (self.peers.getEntry(from)) |entry| {
const origin_bytes = origin.data;
entry.value_ptr.add(&origin_bytes);
}
}
/// get a set of GOSSIP_PUSH_FANOUT peers to send push messages to
/// while accounting for peers that have been pruned from
/// the given origin Pubkey
pub fn getFanoutPeers(
self: *const Self,
allocator: std.mem.Allocator,
origin: Pubkey,
table: *const GossipTable,
) error{OutOfMemory}!std.ArrayList(EndPoint) {
var active_set_endpoints = try std.ArrayList(EndPoint).initCapacity(
allocator,
GOSSIP_PUSH_FANOUT,
);
errdefer active_set_endpoints.deinit();
var iter = self.peers.iterator();
while (iter.next()) |entry| {
// lookup peer contact info
const peer_info = table.getThreadSafeContactInfo(entry.key_ptr.*) orelse continue;
const peer_gossip_addr = peer_info.gossip_addr orelse continue;
peer_gossip_addr.sanitize() catch continue;
// check if peer has been pruned
const origin_bytes = origin.data;
if (entry.value_ptr.contains(&origin_bytes)) {
continue;
}
active_set_endpoints.appendAssumeCapacity(peer_gossip_addr.toEndpoint());
if (active_set_endpoints.items.len == GOSSIP_PUSH_FANOUT) {
break;
}
}
return active_set_endpoints;
}
};
test "init/denit" {
const alloc = std.testing.allocator;
var table = try GossipTable.init(alloc);
defer table.deinit();
// insert some contacts
var prng = std.rand.DefaultPrng.init(100);
var gossip_peers = try std.ArrayList(ThreadSafeContactInfo).initCapacity(alloc, 10);
defer gossip_peers.deinit();
for (0..GOSSIP_PUSH_FANOUT) |_| {
const data = LegacyContactInfo.initRandom(prng.random());
try gossip_peers.append(ThreadSafeContactInfo.fromLegacyContactInfo(data));
const keypair = try KeyPair.create(null);
const value = SignedGossipData.initSigned(&keypair, .{
.LegacyContactInfo = data,
});
_ = try table.insert(value, getWallclockMs());
}
var active_set = ActiveSet.init(alloc);
defer active_set.deinit();
try active_set.initRotate(prng.random(), gossip_peers.items);
try std.testing.expect(active_set.len() == GOSSIP_PUSH_FANOUT);
const origin = Pubkey.initRandom(prng.random());
var fanout = try active_set.getFanoutPeers(alloc, origin, &table);
defer fanout.deinit();
const no_prune_fanout_len = fanout.items.len;
try std.testing.expect(no_prune_fanout_len > 0);
var iter = active_set.peers.keyIterator();
const peer_pubkey = iter.next().?.*;
active_set.prune(peer_pubkey, origin);
var fanout_with_prune = try active_set.getFanoutPeers(alloc, origin, &table);
defer fanout_with_prune.deinit();
try std.testing.expectEqual(no_prune_fanout_len, fanout_with_prune.items.len + 1);
}
test "gracefully rotates with duplicate contact ids" {
const alloc = std.testing.allocator;
var prng = std.rand.DefaultPrng.init(100);
var gossip_peers = try std.ArrayList(ThreadSafeContactInfo).initCapacity(alloc, 10);
defer gossip_peers.deinit();
var data = try LegacyContactInfo.initRandom(prng.random()).toContactInfo(alloc);
var dupe = try LegacyContactInfo.initRandom(prng.random()).toContactInfo(alloc);
defer data.deinit();
defer dupe.deinit();
dupe.pubkey = data.pubkey;
try gossip_peers.append(ThreadSafeContactInfo.fromContactInfo(data));
try gossip_peers.append(ThreadSafeContactInfo.fromContactInfo(dupe));
var active_set = ActiveSet.init(alloc);
defer active_set.deinit();
try active_set.initRotate(prng.random(), gossip_peers.items);
}