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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use std::{
collections::{HashMap, HashSet},
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use futures::{stream::BoxStream, StreamExt};
use nimiq_blockchain::Blockchain;
use nimiq_blockchain_interface::{AbstractBlockchain, BlockchainEvent};
use nimiq_hash::{Blake2bHash, Hash};
use nimiq_keys::Address;
use nimiq_network_interface::{
network::{Network, NetworkEvent, SubscribeEvents},
request::{request_handler, Handle},
};
use nimiq_primitives::account::AccountType;
use nimiq_transaction::account::staking_contract::IncomingStakingTransactionData;
use nimiq_utils::spawn;
use parking_lot::RwLock;
use crate::{
messages::{
AddressNotification, AddressSubscriptionOperation, AddressSubscriptionTopic,
NotificationEvent, RequestSubscribeToAddress,
},
SubscribeToAddressesError,
SubscribeToAddressesError::*,
};
/// The max number of peers that can be subscribed.
pub const MAX_SUBSCRIBED_PEERS: usize = 50;
/// The max number of addresses that can be subscribed, per peer.
pub const MAX_SUBSCRIBED_PEERS_ADDRESSES: usize = 250;
impl<N: Network> Handle<N, Arc<RwLock<RemoteEventDispatcherState<N>>>>
for RequestSubscribeToAddress
{
fn handle(
&self,
peer_id: N::PeerId,
state: &Arc<RwLock<RemoteEventDispatcherState<N>>>,
) -> Result<(), SubscribeToAddressesError> {
match self.operation {
AddressSubscriptionOperation::Subscribe => {
if let Some(peer_addresses) = state.read().subscribed_peers.get(&peer_id) {
// We need to check if this peer already has too many addresses subscribed to us
if peer_addresses.len() > MAX_SUBSCRIBED_PEERS_ADDRESSES {
return Err(TooManyAddresses);
}
} else {
// If this is a new peer, we need to check if we can attend it
if state.read().number_of_peers() > MAX_SUBSCRIBED_PEERS {
return Err(TooManyPeers);
}
}
state
.write()
.add_addresses(&peer_id, self.addresses.clone());
}
AddressSubscriptionOperation::Unsubscribe => {
// If we don't know this peer, we don't do anything
if !state.read().contains_peer(&peer_id) {
return Err(InvalidOperation);
}
state
.write()
.remove_addresses(&peer_id, self.addresses.clone());
}
}
Ok(())
}
}
/// The state that is maintained by the remote event dispatcher:
/// essentially the addresses and peers that are subscribed to us.
pub struct RemoteEventDispatcherState<N: Network> {
/// HashMap containing a mapping from peers to their interesting addresses
subscribed_peers: HashMap<N::PeerId, HashSet<Address>>,
/// Maintains the current list of interesting addresses and the peers that are interested in those addresses
subscriptions: HashMap<Address, HashSet<N::PeerId>>,
}
impl<N: Network> RemoteEventDispatcherState<N> {
/// Creates a new state, only state should be needed per client.
pub fn new() -> Self {
Self {
subscribed_peers: HashMap::new(),
subscriptions: HashMap::new(),
}
}
/// Returns if a peer is part of our state.
pub fn contains_peer(&self, peer_id: &N::PeerId) -> bool {
self.subscribed_peers.contains_key(peer_id)
}
/// Returns the number of peers that are currently subscribed to us.
pub fn number_of_peers(&self) -> usize {
self.subscribed_peers.len()
}
/// Adds a new address for an specific peer.
pub fn add_addresses(&mut self, peer_id: &N::PeerId, addresses: Vec<Address>) {
// If we already knew this peer, then we just update its interesting addresses
if let Some(interesting_addresses) = self.subscribed_peers.get_mut(peer_id) {
interesting_addresses.extend(addresses.iter().cloned())
} else {
// Otherwise, we insert a new entry for this peer
self.subscribed_peers
.insert(*peer_id, HashSet::from_iter(addresses.iter().cloned()));
}
// Now we update our address mapping
for address in addresses {
if let Some(peers) = self.subscriptions.get_mut(&address) {
peers.insert(*peer_id);
} else {
let mut new_peers = HashSet::new();
new_peers.insert(*peer_id);
self.subscriptions.insert(address, new_peers);
}
}
}
/// Removes a peer from the state, used when a peer leaves the network.
pub fn remove_peer(&mut self, peer_id: &N::PeerId) {
if let Some(peer_addresses) = self.subscribed_peers.get(peer_id) {
// Obtain the addresses that are interested to this peer and remove the peer from those addresses.
peer_addresses.iter().for_each(|address| {
if let Some(peers) = self.subscriptions.get_mut(address) {
peers.remove(peer_id);
}
});
}
// Finally remove the peer
self.subscribed_peers.remove(peer_id);
}
/// Remove addresses from an specific peer, if there are no more addresses from this peer we remove it.
pub fn remove_addresses(&mut self, peer_id: &N::PeerId, addresses: Vec<Address>) {
if let Some(peer_addresses) = self.subscribed_peers.get_mut(peer_id) {
addresses.iter().for_each(|address| {
peer_addresses.remove(address);
if let Some(peers) = self.subscriptions.get_mut(address) {
peers.remove(peer_id);
}
});
if peer_addresses.is_empty() {
// If this peer doesn't have any interesting address left, then we just remove it
self.subscribed_peers.remove(peer_id);
}
}
}
/// Obtains the peers that are currently subscribed to us.
pub fn get_peers(&self, address: &Address) -> Option<HashSet<N::PeerId>> {
self.subscriptions.get(address).cloned()
}
}
impl<N: Network> Default for RemoteEventDispatcherState<N> {
fn default() -> Self {
Self::new()
}
}
/// The remote event dispatcher: used to keep track of peers that are subscribed to us
/// because they are interested in different events.
/// This is mainly used by light clients who want to fetch information from Full & History nodes
pub struct RemoteEventDispatcher<N: Network> {
/// The state that we maintain, with the peers and their interesting addresses
state: Arc<RwLock<RemoteEventDispatcherState<N>>>,
/// Blockchain reference, to get blocks from it
blockchain: Arc<RwLock<Blockchain>>,
/// Reference to the network, to allow for message validation
network: Arc<N>,
/// Stream of blockchain events
blockchain_event_rx: BoxStream<'static, BlockchainEvent>,
/// Stream of network events
network_event_rx: SubscribeEvents<N::PeerId>,
}
impl<N: Network> RemoteEventDispatcher<N> {
/// Creates a new `RemoteEventDispatcher` instance and sets up the necessary
/// event streams to handle incoming address subscription requests and
/// observe blockchain and network events.
pub fn new(network: Arc<N>, blockchain: Arc<RwLock<Blockchain>>) -> Self {
let state = Arc::new(RwLock::new(RemoteEventDispatcherState::new()));
// Spawn the network receiver that will take care of processing address subscription requests
let stream = network.receive_requests::<RequestSubscribeToAddress>();
spawn(request_handler(&network, stream, &Arc::clone(&state)));
let blockchain_event_rx = blockchain.read().notifier_as_stream();
let network_events = network.subscribe_events();
Self {
state,
blockchain: Arc::clone(&blockchain),
network: Arc::clone(&network),
blockchain_event_rx,
network_event_rx: network_events,
}
}
/// This is a helper function to determine if we need to create notifications
fn add_notification_receipts(
&self,
address: &Address,
txn_hash: Blake2bHash,
block_number: u32,
peer_receipts: &mut HashMap<N::PeerId, Vec<(Blake2bHash, u32)>>,
) {
if let Some(peers) = self.state.read().get_peers(address) {
for peer in peers {
if let Some(receipts) = peer_receipts.get_mut(&peer) {
receipts.push((txn_hash.clone(), block_number))
} else {
peer_receipts.insert(peer, vec![(txn_hash.clone(), block_number)]);
}
}
}
}
}
impl<N: Network> Future for RemoteEventDispatcher<N> {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// Listen, and process blockchain events
while let Poll::Ready(Some(event)) = self.blockchain_event_rx.poll_next_unpin(cx) {
let mut new_blocks = vec![];
match event {
BlockchainEvent::Extended(block_hash) => {
let block = self
.blockchain
.read()
.get_block(&block_hash, true, None)
.expect("Head block not found");
new_blocks.push(block);
}
BlockchainEvent::Rebranched(_reverted_blocks, adopted_blocks) => {
// We don't notify about reverted block, only adopted blocks
new_blocks.extend(adopted_blocks.into_iter().map(|(_, block)| block));
}
BlockchainEvent::HistoryAdopted(_) => {
// In the future we might be interested in other events
}
BlockchainEvent::Stored(_block) => {
// Stored events are not reported as they are not on the main chain.
// If they ever become main chain blocks, they will be reported then with the respective
// BlockchainEvent::Rebranched(..)
}
BlockchainEvent::EpochFinalized(_)
| BlockchainEvent::Finalized(_)
| BlockchainEvent::ProtocolUpgrade(..) => {
// These events are not relevant for the notifications, as they are complements to the 'Extended' event,
// and don't report new blocks by themselves.
}
}
// This hash map is used to collect all the notifications for a given peer.
let mut peer_receipts: HashMap<N::PeerId, Vec<(Blake2bHash, u32)>> = HashMap::new();
// Collect all possible notifications
for block in new_blocks {
if let Some(transactions) = block.transactions() {
// First collect the list of peers that will be notified
for txn in transactions {
let txn = txn.get_raw_transaction();
// Process transaction sender
self.add_notification_receipts(
&txn.sender,
txn.hash(),
block.block_number(),
&mut peer_receipts,
);
// Process transaction recipients
self.add_notification_receipts(
&txn.recipient,
txn.hash(),
block.block_number(),
&mut peer_receipts,
);
// Process staking transaction (which are a special case)
if txn.recipient_type == AccountType::Staking {
// Parse transaction data
let proof = IncomingStakingTransactionData::parse(txn).unwrap();
if let IncomingStakingTransactionData::AddStake { staker_address } =
proof
{
self.add_notification_receipts(
&staker_address,
txn.hash(),
block.block_number(),
&mut peer_receipts,
);
}
// In the future we might add other staking notifications
}
}
}
}
// Notify all interested peers
for (peer_id, receipts) in peer_receipts {
let network = Arc::clone(&self.network);
spawn({
async move {
let _ = network
.publish_subtopic::<AddressSubscriptionTopic>(
peer_id.to_string(),
AddressNotification {
receipts: receipts.clone(),
event: NotificationEvent::BlockchainExtend,
},
)
.await;
}
});
}
}
// Listen and process network events
while let Poll::Ready(Some(result)) = self.network_event_rx.poll_next_unpin(cx) {
match result {
Ok(NetworkEvent::PeerLeft(peer_id)) => {
// Remove the peer from internal data structures.
self.state.write().remove_peer(&peer_id);
}
Ok(_) => {}
Err(_) => return Poll::Pending,
}
}
Poll::Pending
}
}