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
use beefy_primitives::{BeefyApi, MmrRootHash};
use prometheus::Registry;
use sc_client_api::{Backend, BlockchainEvents, Finalizer};
use sc_consensus::BlockImport;
use sc_network::ProtocolName;
use sc_network_gossip::Network as GossipNetwork;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_consensus::{Error as ConsensusError, SyncOracle};
use sp_keystore::SyncCryptoStorePtr;
use sp_mmr_primitives::MmrApi;
use sp_runtime::traits::Block;
use std::sync::Arc;
mod error;
mod gossip;
mod keystore;
mod metrics;
mod round;
mod worker;
pub mod import;
pub mod justification;
pub mod notification;
#[cfg(test)]
mod tests;
use crate::{
import::BeefyBlockImport,
notification::{
BeefyBestBlockSender, BeefyBestBlockStream, BeefyVersionedFinalityProofSender,
BeefyVersionedFinalityProofStream,
},
};
pub use beefy_protocol_name::standard_name as protocol_standard_name;
pub(crate) mod beefy_protocol_name {
use sc_chain_spec::ChainSpec;
use sc_network::ProtocolName;
const NAME: &str = "/beefy/1";
pub(crate) const LEGACY_NAMES: [&str; 1] = ["/paritytech/beefy/1"];
pub fn standard_name<Hash: AsRef<[u8]>>(
genesis_hash: &Hash,
chain_spec: &Box<dyn ChainSpec>,
) -> ProtocolName {
let chain_prefix = match chain_spec.fork_id() {
Some(fork_id) => format!("/{}/{}", hex::encode(genesis_hash), fork_id),
None => format!("/{}", hex::encode(genesis_hash)),
};
format!("{}{}", chain_prefix, NAME).into()
}
}
pub fn beefy_peers_set_config(
protocol_name: ProtocolName,
) -> sc_network::config::NonDefaultSetConfig {
let mut cfg = sc_network::config::NonDefaultSetConfig::new(protocol_name, 1024 * 1024);
cfg.allow_non_reserved(25, 25);
cfg.add_fallback_names(beefy_protocol_name::LEGACY_NAMES.iter().map(|&n| n.into()).collect());
cfg
}
pub trait Client<B, BE>:
BlockchainEvents<B> + HeaderBackend<B> + Finalizer<B, BE> + Send + Sync
where
B: Block,
BE: Backend<B>,
{
}
impl<B, BE, T> Client<B, BE> for T
where
B: Block,
BE: Backend<B>,
T: BlockchainEvents<B>
+ HeaderBackend<B>
+ Finalizer<B, BE>
+ ProvideRuntimeApi<B>
+ Send
+ Sync,
{
}
#[derive(Clone)]
pub struct BeefyVoterLinks<B: Block> {
pub from_block_import_justif_stream: BeefyVersionedFinalityProofStream<B>,
pub to_rpc_justif_sender: BeefyVersionedFinalityProofSender<B>,
pub to_rpc_best_block_sender: BeefyBestBlockSender<B>,
}
#[derive(Clone)]
pub struct BeefyRPCLinks<B: Block> {
pub from_voter_justif_stream: BeefyVersionedFinalityProofStream<B>,
pub from_voter_best_beefy_stream: BeefyBestBlockStream<B>,
}
pub fn beefy_block_import_and_links<B, BE, RuntimeApi, I>(
wrapped_block_import: I,
backend: Arc<BE>,
runtime: Arc<RuntimeApi>,
) -> (BeefyBlockImport<B, BE, RuntimeApi, I>, BeefyVoterLinks<B>, BeefyRPCLinks<B>)
where
B: Block,
BE: Backend<B>,
I: BlockImport<B, Error = ConsensusError, Transaction = sp_api::TransactionFor<RuntimeApi, B>>
+ Send
+ Sync,
RuntimeApi: ProvideRuntimeApi<B> + Send + Sync,
RuntimeApi::Api: BeefyApi<B>,
{
let (to_rpc_justif_sender, from_voter_justif_stream) =
notification::BeefyVersionedFinalityProofStream::<B>::channel();
let (to_rpc_best_block_sender, from_voter_best_beefy_stream) =
notification::BeefyBestBlockStream::<B>::channel();
let (to_voter_justif_sender, from_block_import_justif_stream) =
notification::BeefyVersionedFinalityProofStream::<B>::channel();
let import =
BeefyBlockImport::new(backend, runtime, wrapped_block_import, to_voter_justif_sender);
let voter_links = BeefyVoterLinks {
from_block_import_justif_stream,
to_rpc_justif_sender,
to_rpc_best_block_sender,
};
let rpc_links = BeefyRPCLinks { from_voter_best_beefy_stream, from_voter_justif_stream };
(import, voter_links, rpc_links)
}
pub struct BeefyParams<B, BE, C, N, R>
where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
R: ProvideRuntimeApi<B>,
R::Api: BeefyApi<B> + MmrApi<B, MmrRootHash>,
N: GossipNetwork<B> + Clone + SyncOracle + Send + Sync + 'static,
{
pub client: Arc<C>,
pub backend: Arc<BE>,
pub runtime: Arc<R>,
pub key_store: Option<SyncCryptoStorePtr>,
pub network: N,
pub min_block_delta: u32,
pub prometheus_registry: Option<Registry>,
pub protocol_name: ProtocolName,
pub links: BeefyVoterLinks<B>,
}
pub async fn start_beefy_gadget<B, BE, C, N, R>(beefy_params: BeefyParams<B, BE, C, N, R>)
where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
R: ProvideRuntimeApi<B>,
R::Api: BeefyApi<B> + MmrApi<B, MmrRootHash>,
N: GossipNetwork<B> + Clone + SyncOracle + Send + Sync + 'static,
{
let BeefyParams {
client,
backend,
runtime,
key_store,
network,
min_block_delta,
prometheus_registry,
protocol_name,
links,
} = beefy_params;
let sync_oracle = network.clone();
let gossip_validator = Arc::new(gossip::GossipValidator::new());
let gossip_engine = sc_network_gossip::GossipEngine::new(
network,
protocol_name,
gossip_validator.clone(),
None,
);
let metrics =
prometheus_registry.as_ref().map(metrics::Metrics::register).and_then(
|result| match result {
Ok(metrics) => {
log::debug!(target: "beefy", "🥩 Registered metrics");
Some(metrics)
},
Err(err) => {
log::debug!(target: "beefy", "🥩 Failed to register metrics: {:?}", err);
None
},
},
);
let worker_params = worker::WorkerParams {
client,
backend,
runtime,
sync_oracle,
key_store: key_store.into(),
gossip_engine,
gossip_validator,
links,
metrics,
min_block_delta,
};
let worker = worker::BeefyWorker::<_, _, _, _, _>::new(worker_params);
worker.run().await
}