-
Notifications
You must be signed in to change notification settings - Fork 19
/
active_set.zig
179 lines (146 loc) · 6.16 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
const std = @import("std");
const network = @import("zig-network");
const EndPoint = network.EndPoint;
const Thread = std.Thread;
const AtomicBool = std.atomic.Atomic(bool);
const UdpSocket = network.Socket;
const Tuple = std.meta.Tuple;
const crds = @import("../gossip/crds.zig");
const CrdsValue = crds.CrdsValue;
const KeyPair = std.crypto.sign.Ed25519.KeyPair;
const Pubkey = @import("../core/pubkey.zig").Pubkey;
const get_wallclock = @import("../gossip/crds.zig").get_wallclock;
const _crds_table = @import("../gossip/crds_table.zig");
const CrdsTable = _crds_table.CrdsTable;
const pull_request = @import("../gossip/pull_request.zig");
const GossipService = @import("../gossip/gossip_service.zig").GossipService;
const Bloom = @import("../bloom/bloom.zig").Bloom;
const RwMux = @import("../sync/mux.zig").RwMux;
const NUM_ACTIVE_SET_ENTRIES: usize = 25;
pub const CRDS_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 crds table bc the data can change
peers: [NUM_ACTIVE_SET_ENTRIES]Pubkey,
pruned_peers: std.AutoHashMap(Pubkey, Bloom),
len: u8 = 0,
allocator: std.mem.Allocator,
const Self = @This();
pub fn init(
allocator: std.mem.Allocator,
) Self {
return Self{ .peers = undefined, .pruned_peers = std.AutoHashMap(Pubkey, Bloom).init(allocator), .len = 0, .allocator = allocator };
}
pub fn deinit(self: *Self) void {
for (self.peers[0..self.len]) |peer| {
var entry = self.pruned_peers.getEntry(peer).?;
entry.value_ptr.deinit();
}
self.pruned_peers.deinit();
self.len = 0;
}
pub fn rotate(
self: *Self,
crds_peers: []crds.LegacyContactInfo,
) error{OutOfMemory}!void {
// clear the existing
for (self.peers[0..self.len]) |peer| {
var entry = self.pruned_peers.getEntry(peer).?;
entry.value_ptr.deinit();
}
self.len = 0;
self.pruned_peers.clearRetainingCapacity();
if (crds_peers.len == 0) {
return;
}
const size = @min(crds_peers.len, NUM_ACTIVE_SET_ENTRIES);
var rng = std.rand.DefaultPrng.init(get_wallclock());
pull_request.shuffle_first_n(rng.random(), crds.LegacyContactInfo, crds_peers, size);
const bloom_num_items = @max(crds_peers.len, MIN_NUM_BLOOM_ITEMS);
for (0..size) |i| {
self.peers[i] = crds_peers[i].id;
// *full* hard restart on blooms -- labs doesnt do this - bug?
var bloom = try Bloom.random(
self.allocator,
bloom_num_items,
BLOOM_FALSE_RATE,
BLOOM_MAX_BITS,
);
try self.pruned_peers.put(self.peers[i], bloom);
}
self.len = size;
}
pub fn prune(self: *Self, from: Pubkey, origin: Pubkey) void {
// we only prune peers which we are sending push messages to
if (self.pruned_peers.getEntry(from)) |entry| {
const origin_bytes = origin.data;
entry.value_ptr.add(&origin_bytes);
}
}
/// get a set of CRDS_GOSSIP_PUSH_FANOUT peers to send push messages to
/// while accounting for peers that have been pruned from
/// the given origin Pubkey
pub fn get_fanout_peers(
self: *const Self,
allocator: std.mem.Allocator,
origin: Pubkey,
crds_table: *const CrdsTable,
) error{OutOfMemory}!std.ArrayList(EndPoint) {
var active_set_endpoints = try std.ArrayList(EndPoint).initCapacity(allocator, CRDS_GOSSIP_PUSH_FANOUT);
errdefer active_set_endpoints.deinit();
// change to while loop
for (self.peers[0..self.len]) |peer_pubkey| {
// lookup peer contact info
const peer_info = crds_table.get(crds.CrdsValueLabel{
.LegacyContactInfo = peer_pubkey,
}) orelse @panic("crds lookup error: peer contactInfo not found");
const peer_gossip_addr = peer_info.value.data.LegacyContactInfo.gossip;
crds.sanitize_socket(&peer_gossip_addr) catch continue;
// check if peer has been pruned
const entry = self.pruned_peers.getEntry(peer_pubkey) orelse unreachable;
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 == CRDS_GOSSIP_PUSH_FANOUT) {
break;
}
}
return active_set_endpoints;
}
};
test "gossip.active_set: init/deinit" {
var alloc = std.testing.allocator;
var crds_table = try CrdsTable.init(alloc);
defer crds_table.deinit();
// insert some contacts
var rng = std.rand.DefaultPrng.init(100);
var gossip_peers = try std.ArrayList(crds.LegacyContactInfo).initCapacity(alloc, 10);
defer gossip_peers.deinit();
for (0..CRDS_GOSSIP_PUSH_FANOUT) |_| {
var data = crds.LegacyContactInfo.random(rng.random());
try gossip_peers.append(data);
var keypair = try KeyPair.create(null);
var value = try CrdsValue.initSigned(crds.CrdsData{
.LegacyContactInfo = data,
}, &keypair);
try crds_table.insert(value, get_wallclock());
}
var active_set = ActiveSet.init(alloc);
defer active_set.deinit();
try active_set.rotate(gossip_peers.items);
try std.testing.expect(active_set.len == CRDS_GOSSIP_PUSH_FANOUT);
const origin = Pubkey.random(rng.random(), .{});
var fanout = try active_set.get_fanout_peers(alloc, origin, &crds_table);
defer fanout.deinit();
const no_prune_fanout_len = fanout.items.len;
try std.testing.expect(no_prune_fanout_len > 0);
const peer_pubkey = active_set.peers[0];
active_set.prune(peer_pubkey, origin);
var fanout_with_prune = try active_set.get_fanout_peers(alloc, origin, &crds_table);
defer fanout_with_prune.deinit();
try std.testing.expectEqual(no_prune_fanout_len, fanout_with_prune.items.len + 1);
}