This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
worker.rs
405 lines (350 loc) · 11.9 KB
/
worker.rs
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::{
convert::{TryFrom, TryInto},
fmt::Debug,
marker::PhantomData,
sync::Arc,
};
use beefy_primitives::{
BeefyApi, Commitment, ConsensusLog, MmrRootHash, SignedCommitment, ValidatorSet, ValidatorSetId, BEEFY_ENGINE_ID,
KEY_TYPE,
};
use codec::{Codec, Decode, Encode};
use futures::{future, FutureExt, StreamExt};
use hex::ToHex;
use log::{debug, error, info, trace, warn};
use parking_lot::Mutex;
use sc_client_api::{Backend, FinalityNotification, FinalityNotifications};
use sc_network_gossip::GossipEngine;
use sp_api::BlockId;
use sp_application_crypto::{AppPublic, Public};
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use sp_runtime::{
generic::OpaqueDigestItemId,
traits::{Block, Hash, Header, NumberFor, Zero},
};
use crate::{
error::{self},
metrics::Metrics,
notification, round, Client,
};
/// Gossip engine messages topic
pub(crate) fn topic<B: Block>() -> B::Hash
where
B: Block,
{
<<B::Header as Header>::Hashing as Hash>::hash(b"beefy")
}
#[derive(Debug, Decode, Encode)]
struct VoteMessage<Hash, Number, Id, Signature> {
commitment: Commitment<Number, Hash>,
id: Id,
signature: Signature,
}
#[derive(PartialEq)]
/// Worker lifecycle state
enum State {
/// A new worker that still needs to be initialized.
New,
/// A worker that validates and votes for commitments
Validate,
/// A worker that acts as a goosip relay only
Gossip,
}
pub(crate) struct BeefyWorker<B, S, C, BE, P>
where
B: Block,
BE: Backend<B>,
P: sp_core::Pair,
P::Public: AppPublic + Codec,
P::Signature: Clone + Codec + Debug + PartialEq + TryFrom<Vec<u8>>,
C: Client<B, BE, P>,
{
state: State,
local_id: Option<P::Public>,
key_store: SyncCryptoStorePtr,
min_interval: u32,
rounds: round::Rounds<MmrRootHash, NumberFor<B>, P::Public, S>,
finality_notifications: FinalityNotifications<B>,
gossip_engine: Arc<Mutex<GossipEngine<B>>>,
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, S>,
best_finalized_block: NumberFor<B>,
best_block_voted_on: NumberFor<B>,
validator_set_id: ValidatorSetId,
client: Arc<C>,
metrics: Option<Metrics>,
_backend: PhantomData<BE>,
_pair: PhantomData<P>,
}
impl<B, S, C, BE, P> BeefyWorker<B, S, C, BE, P>
where
B: Block,
BE: Backend<B>,
P: sp_core::Pair,
P::Public: AppPublic + Codec,
P::Signature: Clone + Codec + Debug + PartialEq + TryFrom<Vec<u8>>,
C: Client<B, BE, P>,
C::Api: BeefyApi<B, P::Public>,
{
/// Retrun a new BEEFY worker instance.
///
/// Note that full BEEFY worker initialization can only be completed, if an
/// on-chain BEEFY pallet is available. Reason is that the current active
/// validator set has to be fetched from the on-chain BEFFY pallet.
///
/// For this reason, BEEFY worker initialization completes only after a finality
/// notification has been received. Such a notifcation is basically an indication
/// that an on-chain BEEFY pallet may be available.
pub(crate) fn new(
client: Arc<C>,
key_store: SyncCryptoStorePtr,
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, S>,
gossip_engine: GossipEngine<B>,
metrics: Option<Metrics>,
) -> Self {
BeefyWorker {
state: State::New,
local_id: None,
key_store,
min_interval: 2,
rounds: round::Rounds::new(vec![]),
finality_notifications: client.finality_notification_stream(),
gossip_engine: Arc::new(Mutex::new(gossip_engine)),
signed_commitment_sender,
best_finalized_block: Zero::zero(),
best_block_voted_on: Zero::zero(),
validator_set_id: 0,
client,
metrics,
_backend: PhantomData,
_pair: PhantomData,
}
}
fn init_validator_set(&mut self) -> Result<(), error::Lifecycle> {
let at = BlockId::hash(self.client.info().best_hash);
let validator_set = self
.client
.runtime_api()
.validator_set(&at)
.map_err(|err| error::Lifecycle::MissingValidatorSet(err.to_string()))?;
let local_id = match validator_set
.validators
.iter()
.find(|id| SyncCryptoStore::has_keys(&*self.key_store, &[(id.to_raw_vec(), KEY_TYPE)]))
{
Some(id) => {
info!(target: "beefy", "🥩 Starting BEEFY worker with local id: {:?}", id);
self.state = State::Validate;
Some(id.clone())
}
None => {
info!(target: "beefy", "🥩 No local id found, BEEFY worker will be gossip only.");
self.state = State::Gossip;
None
}
};
self.local_id = local_id;
self.rounds = round::Rounds::new(validator_set.validators.clone());
// we are actually interested in the best finalized block with the BEEFY pallet
// being available on-chain. That is why we set `best_finalized_block` here and
// not as part of `new()` already.
self.best_finalized_block = self.client.info().finalized_number;
debug!(target: "beefy", "🥩 Validator set with id {} initialized", validator_set.id);
Ok(())
}
}
impl<B, S, C, BE, P> BeefyWorker<B, S, C, BE, P>
where
B: Block,
S: Clone + Codec + Debug + PartialEq + std::convert::TryFrom<Vec<u8>>,
BE: Backend<B>,
P: sp_core::Pair,
P::Public: AppPublic + Codec,
P::Signature: Clone + Codec + Debug + PartialEq + TryFrom<Vec<u8>>,
C: Client<B, BE, P>,
C::Api: BeefyApi<B, P::Public>,
{
fn should_vote_on(&self, number: NumberFor<B>) -> bool {
use sp_runtime::{traits::Saturating, SaturatedConversion};
// we only vote as a validator
if self.state != State::Validate {
return false;
}
let diff = self.best_finalized_block.saturating_sub(self.best_block_voted_on);
let diff = diff.saturated_into::<u32>();
let next_power_of_two = (diff / 2).next_power_of_two();
let next_block_to_vote_on = self.best_block_voted_on + self.min_interval.max(next_power_of_two).into();
trace!(
target: "beefy",
"should_vote_on: #{:?}, diff: {:?}, next_power_of_two: {:?}, next_block_to_vote_on: #{:?}",
number,
diff,
next_power_of_two,
next_block_to_vote_on,
);
number == next_block_to_vote_on
}
fn sign_commitment(&self, id: &P::Public, commitment: &[u8]) -> Result<S, error::Crypto<P::Public>> {
let sig = SyncCryptoStore::sign_with(&*self.key_store, KEY_TYPE, &id.to_public_crypto_pair(), &commitment)
.map_err(|e| error::Crypto::CannotSign((*id).clone(), e.to_string()))?
.ok_or_else(|| error::Crypto::CannotSign((*id).clone(), "No key in KeyStore found".into()))?;
let sig = sig
.clone()
.try_into()
.map_err(|_| error::Crypto::InvalidSignature(sig.encode_hex(), (*id).clone()))?;
Ok(sig)
}
fn handle_finality_notification(&mut self, notification: FinalityNotification<B>) {
debug!(target: "beefy", "🥩 Finality notification: {:?}", notification);
if self.should_vote_on(*notification.header.number()) {
let local_id = if let Some(id) = &self.local_id {
id
} else {
error!(target: "beefy", "🥩 Missing validator id - can't vote for: {:?}", notification.header.hash());
return;
};
if let Some(new) = find_authorities_change::<B, P::Public>(¬ification.header) {
debug!(target: "beefy", "🥩 New validator set: {:?}", new);
if let Some(metrics) = self.metrics.as_ref() {
metrics.beefy_validator_set_id.set(new.id);
}
self.validator_set_id = new.id;
};
let mmr_root = if let Some(hash) = find_mmr_root_digest::<B, P::Public>(¬ification.header) {
hash
} else {
warn!(target: "beefy", "🥩 No MMR root digest found for: {:?}", notification.header.hash());
return;
};
let commitment = Commitment {
payload: mmr_root,
block_number: notification.header.number(),
validator_set_id: self.validator_set_id,
};
let signature = match self.sign_commitment(local_id, commitment.encode().as_ref()) {
Ok(sig) => sig,
Err(err) => {
warn!(target: "beefy", "🥩 Error signing commitment: {:?}", err);
return;
}
};
self.best_block_voted_on = *notification.header.number();
let message = VoteMessage {
commitment,
id: local_id.clone(),
signature,
};
self.gossip_engine
.lock()
.gossip_message(topic::<B>(), message.encode(), false);
debug!(target: "beefy", "🥩 Sent vote message: {:?}", message);
if let Some(metrics) = self.metrics.as_ref() {
metrics.beefy_gadget_votes.inc();
}
self.handle_vote(
(message.commitment.payload, *message.commitment.block_number),
(message.id, message.signature),
);
}
self.best_finalized_block = *notification.header.number();
}
fn handle_vote(&mut self, round: (MmrRootHash, NumberFor<B>), vote: (P::Public, S)) {
// TODO: validate signature
let vote_added = self.rounds.add_vote(round, vote);
if vote_added && self.rounds.is_done(&round) {
if let Some(signatures) = self.rounds.drop(&round) {
let commitment = Commitment {
payload: round.0,
block_number: round.1,
validator_set_id: self.validator_set_id,
};
let signed_commitment = SignedCommitment { commitment, signatures };
info!(target: "beefy", "🥩 Round #{} concluded, committed: {:?}.", round.1, signed_commitment);
self.signed_commitment_sender.notify(signed_commitment);
}
}
}
pub(crate) async fn run(mut self) {
let mut votes = Box::pin(self.gossip_engine.lock().messages_for(topic::<B>()).filter_map(
|notification| async move {
debug!(target: "beefy", "🥩 Got vote message: {:?}", notification);
VoteMessage::<MmrRootHash, NumberFor<B>, P::Public, S>::decode(&mut ¬ification.message[..]).ok()
},
));
loop {
let engine = self.gossip_engine.clone();
let gossip_engine = future::poll_fn(|cx| engine.lock().poll_unpin(cx));
futures::select! {
notification = self.finality_notifications.next().fuse() => {
if let Some(notification) = notification {
if self.state == State::New {
match self.init_validator_set() {
Ok(()) => (),
Err(err) => {
// this is not treated as an error here because there really is
// nothing a node operator could do in order to remedy the root cause.
debug!(target: "beefy", "🥩 Init validator set failed: {:?}", err);
}
}
}
self.handle_finality_notification(notification);
} else {
return;
}
},
vote = votes.next().fuse() => {
if let Some(vote) = vote {
self.handle_vote(
(vote.commitment.payload, vote.commitment.block_number),
(vote.id, vote.signature),
);
} else {
return;
}
},
_ = gossip_engine.fuse() => {
error!(target: "beefy", "🥩 Gossip engine has terminated.");
return;
}
}
}
}
}
/// Extract the MMR root hash from a digest in the given header, if it exists.
fn find_mmr_root_digest<B, Id>(header: &B::Header) -> Option<MmrRootHash>
where
B: Block,
Id: Codec,
{
header.digest().logs().iter().find_map(|log| {
match log.try_to::<ConsensusLog<Id>>(OpaqueDigestItemId::Consensus(&BEEFY_ENGINE_ID)) {
Some(ConsensusLog::MmrRoot(root)) => Some(root),
_ => None,
}
})
}
/// Scan the `header` digest log for a BEEFY validator set change. Return either the new
/// validator set or `None` in case no validator set change has been signaled.
fn find_authorities_change<B, Id>(header: &B::Header) -> Option<ValidatorSet<Id>>
where
B: Block,
Id: Codec,
{
let id = OpaqueDigestItemId::Consensus(&BEEFY_ENGINE_ID);
let filter = |log: ConsensusLog<Id>| match log {
ConsensusLog::AuthoritiesChange(validator_set) => Some(validator_set),
_ => None,
};
header.digest().convert_first(|l| l.try_to(id).and_then(filter))
}