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

Commit e34daee

Browse files
andresilvatomusdrw
andauthored
use new mmr root as commitment payload (#27)
* use new mmr root as commitment payload * fix mmr root codec index * warn on MMR root digest not found Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * add type alias for MMR root hash Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
1 parent 3a4cfa8 commit e34daee

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

client/beefy/src/lib.rs

+34-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use futures::{future, FutureExt, Stream, StreamExt};
2424
use log::{debug, error, info, trace, warn};
2525
use parking_lot::Mutex;
2626

27-
use beefy_primitives::{BeefyApi, Commitment, SignedCommitment, KEY_TYPE};
27+
use beefy_primitives::{BeefyApi, Commitment, ConsensusLog, MmrRootHash, SignedCommitment, BEEFY_ENGINE_ID, KEY_TYPE};
2828

2929
use sc_client_api::{Backend as BackendT, BlockchainEvents, FinalityNotification, Finalizer};
3030
use sc_network_gossip::{
@@ -36,7 +36,10 @@ use sp_application_crypto::{AppPublic, Public};
3636
use sp_blockchain::HeaderBackend;
3737
use sp_consensus::SyncOracle as SyncOracleT;
3838
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
39-
use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor, Zero};
39+
use sp_runtime::{
40+
generic::OpaqueDigestItemId,
41+
traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor, Zero},
42+
};
4043

4144
pub mod notification;
4245

@@ -165,7 +168,7 @@ struct BeefyWorker<Block: BlockT, Id, Signature, FinalityNotifications> {
165168
local_id: Id,
166169
key_store: SyncCryptoStorePtr,
167170
min_interval: u32,
168-
rounds: Rounds<Block::Hash, NumberFor<Block>, Id, Signature>,
171+
rounds: Rounds<MmrRootHash, NumberFor<Block>, Id, Signature>,
169172
finality_notifications: FinalityNotifications,
170173
gossip_engine: Arc<Mutex<GossipEngine<Block>>>,
171174
signed_commitment_sender: BeefySignedCommitmentSender<Block, Signature>,
@@ -231,13 +234,20 @@ where
231234
}
232235

233236
fn handle_finality_notification(&mut self, notification: FinalityNotification<Block>) {
234-
debug!(target: "beefy", "🥩 Finality notification: {:?}", notification);
237+
debug!(target: "beefy", "Finality notification: {:?}", notification);
235238

236239
if self.should_vote_on(*notification.header.number()) {
237-
// TODO: this needs to be properly populated by signing an MMR root as the payload
238-
// (and/or abstracting the "thing to sign") and with support for validator set changes.
240+
let mmr_root = if let Some(hash) = find_mmr_root_digest::<Block, Id>(&notification.header) {
241+
hash
242+
} else {
243+
warn!(target: "beefy", "🥩 No MMR root digest found for: {:?}", notification.header.hash());
244+
return;
245+
};
246+
247+
// TODO: this needs added support for validator set changes (and abstracting the
248+
// "thing to sign" would be nice).
239249
let commitment = Commitment {
240-
payload: notification.header.hash(),
250+
payload: mmr_root,
241251
block_number: notification.header.number(),
242252
validator_set_id: 0,
243253
is_set_transition_block: false,
@@ -282,14 +292,14 @@ where
282292
self.best_finalized_block = *notification.header.number();
283293
}
284294

285-
fn handle_vote(&mut self, round: (Block::Hash, NumberFor<Block>), vote: (Id, Signature)) {
295+
fn handle_vote(&mut self, round: (MmrRootHash, NumberFor<Block>), vote: (Id, Signature)) {
286296
// TODO: validate signature
287297
let vote_added = self.rounds.add_vote(round, vote);
288298

289299
if vote_added && self.rounds.is_done(&round) {
290300
if let Some(signatures) = self.rounds.drop(&round) {
291-
// TODO: this needs to be properly populated by signing an MMR root as the payload
292-
// (and/or abstracting the "thing to sign") and with support for validator set changes.
301+
// TODO: this needs added support for validator set changes (and abstracting the
302+
// "thing to sign" would be nice).
293303
let commitment = Commitment {
294304
payload: round.0,
295305
block_number: round.1,
@@ -311,7 +321,7 @@ where
311321
|notification| async move {
312322
debug!(target: "beefy", "Got vote message: {:?}", notification);
313323

314-
VoteMessage::<Block::Hash, NumberFor<Block>, Id, Signature>::decode(&mut &notification.message[..]).ok()
324+
VoteMessage::<MmrRootHash, NumberFor<Block>, Id, Signature>::decode(&mut &notification.message[..]).ok()
315325
},
316326
));
317327

@@ -413,6 +423,19 @@ pub async fn start_beefy_gadget<Block, Pair, Backend, Client, Network, SyncOracl
413423
worker.run().await
414424
}
415425

426+
/// Extract the MMR root hash from a digest in the given header, if it exists.
427+
fn find_mmr_root_digest<Block: BlockT, Id>(header: &Block::Header) -> Option<MmrRootHash>
428+
where
429+
Id: Codec,
430+
{
431+
header.digest().logs().iter().find_map(|log| {
432+
match log.try_to::<ConsensusLog<Id>>(OpaqueDigestItemId::Consensus(&BEEFY_ENGINE_ID)) {
433+
Some(ConsensusLog::MmrRoot(root)) => Some(root),
434+
_ => None,
435+
}
436+
})
437+
}
438+
416439
#[cfg(test)]
417440
mod tests {
418441
#[test]

client/beefy/src/notification.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnbound
2222

2323
/// Stream of signed commitments returned when subscribing.
2424
pub type SignedCommitment<Block, Signature> =
25-
beefy_primitives::SignedCommitment<NumberFor<Block>, <Block as BlockT>::Hash, Signature>;
25+
beefy_primitives::SignedCommitment<NumberFor<Block>, beefy_primitives::MmrRootHash, Signature>;
2626

2727
/// Stream of signed commitments returned when subscribing.
2828
type SignedCommitmentStream<Block, Signature> = TracingUnboundedReceiver<SignedCommitment<Block, Signature>>;

0 commit comments

Comments
 (0)