Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 088fa19

Browse files
authored
Append SignedCommitment to block justifications (#177)
* Append SignedCommitment * add BeefyParams * add WorkerParams * use warn * versioned variant for SignedCommitment
1 parent 532c858 commit 088fa19

File tree

4 files changed

+126
-26
lines changed

4 files changed

+126
-26
lines changed

client/beefy/src/lib.rs

+41-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use sc_network_gossip::{GossipEngine, Network as GossipNetwork};
2626
use sp_api::ProvideRuntimeApi;
2727
use sp_application_crypto::AppPublic;
2828
use sp_blockchain::HeaderBackend;
29-
use sp_consensus::SyncOracle as SyncOracleT;
3029
use sp_keystore::SyncCryptoStorePtr;
3130
use sp_runtime::traits::Block;
3231

@@ -78,18 +77,34 @@ where
7877
// empty
7978
}
8079

80+
/// BEEFY gadget initialization parameters.
81+
pub struct BeefyParams<B, P, BE, C, N>
82+
where
83+
B: Block,
84+
P: sp_core::Pair,
85+
P::Signature: Clone + Codec + Debug + PartialEq + TryFrom<Vec<u8>>,
86+
{
87+
/// BEEFY client
88+
pub client: Arc<C>,
89+
/// Client Backend
90+
pub backend: Arc<BE>,
91+
/// Local key store
92+
pub key_store: Option<SyncCryptoStorePtr>,
93+
/// Gossip network
94+
pub network: N,
95+
/// BEEFY signed commitment sender
96+
pub signed_commitment_sender: notification::BeefySignedCommitmentSender<B, P::Signature>,
97+
/// Minimal delta between blocks, BEEFY should vote for
98+
pub min_block_delta: u32,
99+
/// Prometheus metric registry
100+
pub prometheus_registry: Option<Registry>,
101+
}
102+
81103
/// Start the BEEFY gadget.
82104
///
83105
/// This is a thin shim around running and awaiting a BEEFY worker.
84-
pub async fn start_beefy_gadget<B, P, BE, C, N, SO>(
85-
client: Arc<C>,
86-
key_store: Option<SyncCryptoStorePtr>,
87-
network: N,
88-
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, P::Signature>,
89-
_sync_oracle: SO,
90-
min_block_delta: u32,
91-
prometheus_registry: Option<Registry>,
92-
) where
106+
pub async fn start_beefy_gadget<B, P, BE, C, N>(beefy_params: BeefyParams<B, P, BE, C, N>)
107+
where
93108
B: Block,
94109
P: sp_core::Pair,
95110
P::Public: AppPublic + Codec,
@@ -98,8 +113,17 @@ pub async fn start_beefy_gadget<B, P, BE, C, N, SO>(
98113
C: Client<B, BE, P>,
99114
C::Api: BeefyApi<B, P::Public>,
100115
N: GossipNetwork<B> + Clone + Send + 'static,
101-
SO: SyncOracleT + Send + 'static,
102116
{
117+
let BeefyParams {
118+
client,
119+
backend,
120+
key_store,
121+
network,
122+
signed_commitment_sender,
123+
min_block_delta,
124+
prometheus_registry,
125+
} = beefy_params;
126+
103127
let gossip_validator = Arc::new(gossip::BeefyGossipValidator::new());
104128
let gossip_engine = GossipEngine::new(network, BEEFY_PROTOCOL_NAME, gossip_validator.clone(), None);
105129

@@ -117,15 +141,18 @@ pub async fn start_beefy_gadget<B, P, BE, C, N, SO>(
117141
}
118142
});
119143

120-
let worker = worker::BeefyWorker::<_, _, BE, P>::new(
121-
client.clone(),
144+
let worker_params = worker::WorkerParams {
145+
client,
146+
backend,
122147
key_store,
123148
signed_commitment_sender,
124149
gossip_engine,
125150
gossip_validator,
126151
min_block_delta,
127152
metrics,
128-
);
153+
};
154+
155+
let worker = worker::BeefyWorker::<_, _, _, _>::new(worker_params);
129156

130157
worker.run().await
131158
}

client/beefy/src/worker.rs

+48-11
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ use sp_runtime::{
4242
};
4343

4444
use beefy_primitives::{
45-
BeefyApi, Commitment, ConsensusLog, MmrRootHash, SignedCommitment, ValidatorSet, VoteMessage, BEEFY_ENGINE_ID,
46-
GENESIS_AUTHORITY_SET_ID, KEY_TYPE,
45+
BeefyApi, Commitment, ConsensusLog, MmrRootHash, SignedCommitment, ValidatorSet, VersionedCommitment, VoteMessage,
46+
BEEFY_ENGINE_ID, GENESIS_AUTHORITY_SET_ID, KEY_TYPE,
4747
};
4848

4949
use crate::{
@@ -54,6 +54,22 @@ use crate::{
5454
notification, round, Client,
5555
};
5656

57+
pub(crate) struct WorkerParams<B, P, BE, C>
58+
where
59+
B: Block,
60+
P: sp_core::Pair,
61+
P::Signature: Clone + Codec + Debug + PartialEq + TryFrom<Vec<u8>>,
62+
{
63+
pub client: Arc<C>,
64+
pub backend: Arc<BE>,
65+
pub key_store: Option<SyncCryptoStorePtr>,
66+
pub signed_commitment_sender: notification::BeefySignedCommitmentSender<B, P::Signature>,
67+
pub gossip_engine: GossipEngine<B>,
68+
pub gossip_validator: Arc<BeefyGossipValidator<B, P>>,
69+
pub min_block_delta: u32,
70+
pub metrics: Option<Metrics>,
71+
}
72+
5773
/// A BEEFY worker plays the BEEFY protocol
5874
pub(crate) struct BeefyWorker<B, C, BE, P>
5975
where
@@ -65,6 +81,7 @@ where
6581
C: Client<B, BE, P>,
6682
{
6783
client: Arc<C>,
84+
backend: Arc<BE>,
6885
key_store: Option<SyncCryptoStorePtr>,
6986
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, P::Signature>,
7087
gossip_engine: Arc<Mutex<GossipEngine<B>>>,
@@ -101,17 +118,21 @@ where
101118
/// BEEFY pallet has been deployed on-chain.
102119
///
103120
/// The BEEFY pallet is needed in order to keep track of the BEEFY authority set.
104-
pub(crate) fn new(
105-
client: Arc<C>,
106-
key_store: Option<SyncCryptoStorePtr>,
107-
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, P::Signature>,
108-
gossip_engine: GossipEngine<B>,
109-
gossip_validator: Arc<BeefyGossipValidator<B, P>>,
110-
min_block_delta: u32,
111-
metrics: Option<Metrics>,
112-
) -> Self {
121+
pub(crate) fn new(worker_params: WorkerParams<B, P, BE, C>) -> Self {
122+
let WorkerParams {
123+
client,
124+
backend,
125+
key_store,
126+
signed_commitment_sender,
127+
gossip_engine,
128+
gossip_validator,
129+
min_block_delta,
130+
metrics,
131+
} = worker_params;
132+
113133
BeefyWorker {
114134
client: client.clone(),
135+
backend,
115136
key_store,
116137
signed_commitment_sender,
117138
gossip_engine: Arc::new(Mutex::new(gossip_engine)),
@@ -313,6 +334,22 @@ where
313334

314335
debug!(target: "beefy", "🥩 Round #{} concluded, committed: {:?}.", round.1, signed_commitment);
315336

337+
if self
338+
.backend
339+
.append_justification(
340+
BlockId::Number(round.1),
341+
(
342+
BEEFY_ENGINE_ID,
343+
VersionedCommitment::V1(signed_commitment.clone()).encode(),
344+
),
345+
)
346+
.is_err()
347+
{
348+
// this is a warning for now, because until the round lifecycle is improved, we will
349+
// conclude certain rounds multiple times.
350+
warn!(target: "beefy", "🥩 Failed to append justification: {:?}", signed_commitment);
351+
}
352+
316353
self.signed_commitment_sender.notify(signed_commitment);
317354
self.best_beefy_block = Some(round.1);
318355

primitives/beefy/src/commitment.rs

+36
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,24 @@ impl<TBlockNumber, TPayload, TSignature> SignedCommitment<TBlockNumber, TPayload
9595
}
9696
}
9797

98+
/// A [SignedCommitment] with a version number. This variant will be appended
99+
/// to the block justifications for the block for which the signed commitment
100+
/// has been generated.
101+
#[derive(Clone, Debug, PartialEq, codec::Encode, codec::Decode)]
102+
pub enum VersionedCommitment<N, P, S> {
103+
#[codec(index = 1)]
104+
/// Current active version
105+
V1(SignedCommitment<N, P, S>),
106+
}
107+
98108
#[cfg(test)]
99109
mod tests {
100110
use super::*;
101111
use codec::Decode;
102112

103113
type TestCommitment = Commitment<u128, String>;
104114
type TestSignedCommitment = SignedCommitment<u128, String, Vec<u8>>;
115+
type TestVersionedCommitment = VersionedCommitment<u128, String, Vec<u8>>;
105116

106117
#[test]
107118
fn commitment_encode_decode() {
@@ -195,4 +206,29 @@ mod tests {
195206
assert!(c < d);
196207
assert!(b < d);
197208
}
209+
210+
#[test]
211+
fn versioned_commitment_encode_decode() {
212+
let commitment: TestCommitment = Commitment {
213+
payload: "Hello World!".into(),
214+
block_number: 5,
215+
validator_set_id: 0,
216+
};
217+
218+
let signed = SignedCommitment {
219+
commitment,
220+
signatures: vec![None, None, Some(vec![1, 2, 3, 4]), Some(vec![5, 6, 7, 8])],
221+
};
222+
223+
let versioned = TestVersionedCommitment::V1(signed.clone());
224+
225+
let encoded = codec::Encode::encode(&versioned);
226+
227+
assert_eq!(1, encoded[0]);
228+
assert_eq!(encoded[1..], codec::Encode::encode(&signed));
229+
230+
let decoded = TestVersionedCommitment::decode(&mut &*encoded);
231+
232+
assert_eq!(decoded, Ok(versioned));
233+
}
198234
}

primitives/beefy/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
mod commitment;
3636
pub mod witness;
3737

38-
pub use commitment::{Commitment, SignedCommitment};
38+
pub use commitment::{Commitment, SignedCommitment, VersionedCommitment};
3939

4040
use codec::{Codec, Decode, Encode};
4141
use sp_core::H256;

0 commit comments

Comments
 (0)