Skip to content

Commit 3b5534c

Browse files
michalkucharczykark0f
authored andcommitted
BlockId removal: refactor: Finalizer (paritytech#12528)
* BlockId removal: refactor: Finalizer It changes the arguments of methods of `Finalizer` trait from: block: `BlockId<Block>` to: hash: `&Block::Hash` This PR is part of BlockId::Number refactoring analysis (paritytech/substrate#11292) * minor corrections * failing test corrected * minor rework
1 parent d694e46 commit 3b5534c

File tree

16 files changed

+113
-118
lines changed

16 files changed

+113
-118
lines changed

client/api/src/backend.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub trait Finalizer<Block: BlockT, B: Backend<Block>> {
252252
fn apply_finality(
253253
&self,
254254
operation: &mut ClientImportOperation<Block, B>,
255-
id: BlockId<Block>,
255+
block: &Block::Hash,
256256
justification: Option<Justification>,
257257
notify: bool,
258258
) -> sp_blockchain::Result<()>;
@@ -272,7 +272,7 @@ pub trait Finalizer<Block: BlockT, B: Backend<Block>> {
272272
/// while performing major synchronization work.
273273
fn finalize_block(
274274
&self,
275-
id: BlockId<Block>,
275+
block: &Block::Hash,
276276
justification: Option<Justification>,
277277
notify: bool,
278278
) -> sp_blockchain::Result<()>;

client/beefy/src/tests.rs

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,10 @@ fn finalize_block_and_wait_for_beefy(
509509
let (best_blocks, versioned_finality_proof) = get_beefy_streams(&mut net.lock(), peers.clone());
510510

511511
for block in finalize_targets {
512-
let finalize = BlockId::number(*block);
513512
peers.clone().for_each(|(index, _)| {
514-
net.lock()
515-
.peer(index)
516-
.client()
517-
.as_client()
518-
.finalize_block(finalize, None)
519-
.unwrap();
513+
let client = net.lock().peer(index).client().as_client();
514+
let finalize = client.expect_block_hash_from_id(&BlockId::number(*block)).unwrap();
515+
client.finalize_block(&finalize, None).unwrap();
520516
})
521517
}
522518

@@ -604,17 +600,23 @@ fn lagging_validators() {
604600
);
605601

606602
// Alice finalizes #25, Bob lags behind
607-
let finalize = BlockId::number(25);
603+
let finalize = net
604+
.lock()
605+
.peer(0)
606+
.client()
607+
.as_client()
608+
.expect_block_hash_from_id(&BlockId::number(25))
609+
.unwrap();
608610
let (best_blocks, versioned_finality_proof) = get_beefy_streams(&mut net.lock(), peers.clone());
609-
net.lock().peer(0).client().as_client().finalize_block(finalize, None).unwrap();
611+
net.lock().peer(0).client().as_client().finalize_block(&finalize, None).unwrap();
610612
// verify nothing gets finalized by BEEFY
611613
let timeout = Some(Duration::from_millis(250));
612614
streams_empty_after_timeout(best_blocks, &net, &mut runtime, timeout);
613615
streams_empty_after_timeout(versioned_finality_proof, &net, &mut runtime, None);
614616

615617
// Bob catches up and also finalizes #25
616618
let (best_blocks, versioned_finality_proof) = get_beefy_streams(&mut net.lock(), peers.clone());
617-
net.lock().peer(1).client().as_client().finalize_block(finalize, None).unwrap();
619+
net.lock().peer(1).client().as_client().finalize_block(&finalize, None).unwrap();
618620
// expected beefy finalizes block #17 from diff-power-of-two
619621
wait_for_best_beefy_blocks(best_blocks, &net, &mut runtime, &[23, 24, 25]);
620622
wait_for_beefy_signed_commitments(versioned_finality_proof, &net, &mut runtime, &[23, 24, 25]);
@@ -628,16 +630,22 @@ fn lagging_validators() {
628630

629631
// Alice finalizes session-boundary mandatory block #60, Bob lags behind
630632
let (best_blocks, versioned_finality_proof) = get_beefy_streams(&mut net.lock(), peers.clone());
631-
let finalize = BlockId::number(60);
632-
net.lock().peer(0).client().as_client().finalize_block(finalize, None).unwrap();
633+
let finalize = net
634+
.lock()
635+
.peer(0)
636+
.client()
637+
.as_client()
638+
.expect_block_hash_from_id(&BlockId::number(60))
639+
.unwrap();
640+
net.lock().peer(0).client().as_client().finalize_block(&finalize, None).unwrap();
633641
// verify nothing gets finalized by BEEFY
634642
let timeout = Some(Duration::from_millis(250));
635643
streams_empty_after_timeout(best_blocks, &net, &mut runtime, timeout);
636644
streams_empty_after_timeout(versioned_finality_proof, &net, &mut runtime, None);
637645

638646
// Bob catches up and also finalizes #60 (and should have buffered Alice's vote on #60)
639647
let (best_blocks, versioned_finality_proof) = get_beefy_streams(&mut net.lock(), peers);
640-
net.lock().peer(1).client().as_client().finalize_block(finalize, None).unwrap();
648+
net.lock().peer(1).client().as_client().finalize_block(&finalize, None).unwrap();
641649
// verify beefy skips intermediary votes, and successfully finalizes mandatory block #60
642650
wait_for_best_beefy_blocks(best_blocks, &net, &mut runtime, &[60]);
643651
wait_for_beefy_signed_commitments(versioned_finality_proof, &net, &mut runtime, &[60]);
@@ -681,24 +689,16 @@ fn correct_beefy_payload() {
681689
get_beefy_streams(&mut net.lock(), [(0, BeefyKeyring::Alice)].into_iter());
682690

683691
// now 2 good validators and 1 bad one are voting
684-
net.lock()
692+
let hashof11 = net
693+
.lock()
685694
.peer(0)
686695
.client()
687696
.as_client()
688-
.finalize_block(BlockId::number(11), None)
689-
.unwrap();
690-
net.lock()
691-
.peer(1)
692-
.client()
693-
.as_client()
694-
.finalize_block(BlockId::number(11), None)
695-
.unwrap();
696-
net.lock()
697-
.peer(3)
698-
.client()
699-
.as_client()
700-
.finalize_block(BlockId::number(11), None)
697+
.expect_block_hash_from_id(&BlockId::number(11))
701698
.unwrap();
699+
net.lock().peer(0).client().as_client().finalize_block(&hashof11, None).unwrap();
700+
net.lock().peer(1).client().as_client().finalize_block(&hashof11, None).unwrap();
701+
net.lock().peer(3).client().as_client().finalize_block(&hashof11, None).unwrap();
702702

703703
// verify consensus is _not_ reached
704704
let timeout = Some(Duration::from_millis(250));
@@ -708,12 +708,7 @@ fn correct_beefy_payload() {
708708
// 3rd good validator catches up and votes as well
709709
let (best_blocks, versioned_finality_proof) =
710710
get_beefy_streams(&mut net.lock(), [(0, BeefyKeyring::Alice)].into_iter());
711-
net.lock()
712-
.peer(2)
713-
.client()
714-
.as_client()
715-
.finalize_block(BlockId::number(11), None)
716-
.unwrap();
711+
net.lock().peer(2).client().as_client().finalize_block(&hashof11, None).unwrap();
717712

718713
// verify consensus is reached
719714
wait_for_best_beefy_blocks(best_blocks, &net, &mut runtime, &[11]);
@@ -923,12 +918,9 @@ fn on_demand_beefy_justification_sync() {
923918

924919
let (dave_best_blocks, _) =
925920
get_beefy_streams(&mut net.lock(), [(dave_index, BeefyKeyring::Dave)].into_iter());
926-
net.lock()
927-
.peer(dave_index)
928-
.client()
929-
.as_client()
930-
.finalize_block(BlockId::number(1), None)
931-
.unwrap();
921+
let client = net.lock().peer(dave_index).client().as_client();
922+
let hashof1 = client.expect_block_hash_from_id(&BlockId::number(1)).unwrap();
923+
client.finalize_block(&hashof1, None).unwrap();
932924
// Give Dave task some cpu cycles to process the finality notification,
933925
run_for(Duration::from_millis(100), &net, &mut runtime);
934926
// freshly spun up Dave now needs to listen for gossip to figure out the state of his peers.

client/beefy/src/worker.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,11 +1506,9 @@ pub(crate) mod tests {
15061506
// push 15 blocks with `AuthorityChange` digests every 10 blocks
15071507
net.generate_blocks_and_sync(15, 10, &validator_set, false);
15081508
// finalize 13 without justifications
1509-
net.peer(0)
1510-
.client()
1511-
.as_client()
1512-
.finalize_block(BlockId::number(13), None)
1513-
.unwrap();
1509+
let hashof13 =
1510+
backend.blockchain().expect_block_hash_from_id(&BlockId::Number(13)).unwrap();
1511+
net.peer(0).client().as_client().finalize_block(&hashof13, None).unwrap();
15141512

15151513
// Test initialization at session boundary.
15161514
{

client/consensus/babe/src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ fn revert_not_allowed_for_finalized() {
820820
let canon = propose_and_import_blocks_wrap(BlockId::Number(0), 3);
821821

822822
// Finalize best block
823-
client.finalize_block(BlockId::Hash(canon[2]), None, false).unwrap();
823+
client.finalize_block(&canon[2], None, false).unwrap();
824824

825825
// Revert canon chain to last finalized block
826826
revert(client.clone(), backend, 100).expect("revert should work for baked test scenario");
@@ -882,7 +882,7 @@ fn importing_epoch_change_block_prunes_tree() {
882882

883883
// We finalize block #13 from the canon chain, so on the next epoch
884884
// change the tree should be pruned, to not contain F (#7).
885-
client.finalize_block(BlockId::Hash(canon_hashes[12]), None, false).unwrap();
885+
client.finalize_block(&canon_hashes[12], None, false).unwrap();
886886
propose_and_import_blocks_wrap(BlockId::Hash(client.chain_info().best_hash), 7);
887887

888888
// at this point no hashes from the first fork must exist on the tree
@@ -909,7 +909,7 @@ fn importing_epoch_change_block_prunes_tree() {
909909
.any(|h| fork_3.contains(h)),);
910910

911911
// finalizing block #25 from the canon chain should prune out the second fork
912-
client.finalize_block(BlockId::Hash(canon_hashes[24]), None, false).unwrap();
912+
client.finalize_block(&canon_hashes[24], None, false).unwrap();
913913
propose_and_import_blocks_wrap(BlockId::Hash(client.chain_info().best_hash), 8);
914914

915915
// at this point no hashes from the second fork must exist on the tree
@@ -1049,7 +1049,7 @@ fn obsolete_blocks_aux_data_cleanup() {
10491049
assert!(aux_data_check(&fork3_hashes, true));
10501050

10511051
// Finalize A3
1052-
client.finalize_block(BlockId::Number(3), None, true).unwrap();
1052+
client.finalize_block(&fork1_hashes[2], None, true).unwrap();
10531053

10541054
// Wiped: A1, A2
10551055
assert!(aux_data_check(&fork1_hashes[..2], false));
@@ -1060,7 +1060,7 @@ fn obsolete_blocks_aux_data_cleanup() {
10601060
// Present C4, C5
10611061
assert!(aux_data_check(&fork3_hashes, true));
10621062

1063-
client.finalize_block(BlockId::Number(4), None, true).unwrap();
1063+
client.finalize_block(&fork1_hashes[3], None, true).unwrap();
10641064

10651065
// Wiped: A3
10661066
assert!(aux_data_check(&fork1_hashes[2..3], false));

client/consensus/manual-seal/src/finalize_block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
use crate::rpc;
2222
use sc_client_api::backend::{Backend as ClientBackend, Finalizer};
23-
use sp_runtime::{generic::BlockId, traits::Block as BlockT, Justification};
23+
use sp_runtime::{traits::Block as BlockT, Justification};
2424
use std::{marker::PhantomData, sync::Arc};
2525

2626
/// params for block finalization.
@@ -46,7 +46,7 @@ where
4646
{
4747
let FinalizeBlockParams { hash, mut sender, justification, finalizer, .. } = params;
4848

49-
match finalizer.finalize_block(BlockId::Hash(hash), justification, true) {
49+
match finalizer.finalize_block(&hash, justification, true) {
5050
Err(e) => {
5151
log::warn!("Failed to finalize block {}", e);
5252
rpc::send_result(&mut sender, Err(e.into()))

client/finality-grandpa/src/environment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ where
13521352
// ideally some handle to a synchronization oracle would be used
13531353
// to avoid unconditionally notifying.
13541354
client
1355-
.apply_finality(import_op, BlockId::Hash(hash), persisted_justification, true)
1355+
.apply_finality(import_op, &hash, persisted_justification, true)
13561356
.map_err(|e| {
13571357
warn!(target: "afg", "Error applying finality to block {:?}: {}", (hash, number), e);
13581358
e

client/finality-grandpa/src/finality_proof.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ mod tests {
309309
}
310310

311311
for block in to_finalize {
312-
client.finalize_block(BlockId::Number(*block), None).unwrap();
312+
let hash = blocks[*block as usize - 1].hash();
313+
client.finalize_block(&hash, None).unwrap();
313314
}
314315
(client, backend, blocks)
315316
}
@@ -489,7 +490,7 @@ mod tests {
489490
let grandpa_just8 = GrandpaJustification::from_commit(&client, round, commit).unwrap();
490491

491492
client
492-
.finalize_block(BlockId::Number(8), Some((ID, grandpa_just8.encode().clone())))
493+
.finalize_block(&block8.hash(), Some((ID, grandpa_just8.encode().clone())))
493494
.unwrap();
494495

495496
// Authority set change at block 8, so the justification stored there will be used in the

client/finality-grandpa/src/tests.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,12 @@ fn grandpa_environment_respects_voting_rules() {
14571457
);
14581458

14591459
// we finalize block 19 with block 21 being the best block
1460-
peer.client().finalize_block(BlockId::Number(19), None, false).unwrap();
1460+
let hashof19 = peer
1461+
.client()
1462+
.as_client()
1463+
.expect_block_hash_from_id(&BlockId::Number(19))
1464+
.unwrap();
1465+
peer.client().finalize_block(&hashof19, None, false).unwrap();
14611466

14621467
// the 3/4 environment should propose block 21 for voting
14631468
assert_eq!(
@@ -1479,7 +1484,12 @@ fn grandpa_environment_respects_voting_rules() {
14791484
);
14801485

14811486
// we finalize block 21 with block 21 being the best block
1482-
peer.client().finalize_block(BlockId::Number(21), None, false).unwrap();
1487+
let hashof21 = peer
1488+
.client()
1489+
.as_client()
1490+
.expect_block_hash_from_id(&BlockId::Number(21))
1491+
.unwrap();
1492+
peer.client().finalize_block(&hashof21, None, false).unwrap();
14831493

14841494
// even though the default environment will always try to not vote on the
14851495
// best block, there's a hard rule that we can't cast any votes lower than

client/finality-grandpa/src/warp_proof.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ mod tests {
327327
use sp_consensus::BlockOrigin;
328328
use sp_finality_grandpa::GRANDPA_ENGINE_ID;
329329
use sp_keyring::Ed25519Keyring;
330-
use sp_runtime::{generic::BlockId, traits::Header as _};
330+
use sp_runtime::traits::Header as _;
331331
use std::sync::Arc;
332332
use substrate_test_runtime_client::{
333333
ClientBlockImportExt, ClientExt, DefaultTestClientBuilderExt, TestClientBuilder,
@@ -412,10 +412,7 @@ mod tests {
412412
let justification = GrandpaJustification::from_commit(&client, 42, commit).unwrap();
413413

414414
client
415-
.finalize_block(
416-
BlockId::Hash(target_hash),
417-
Some((GRANDPA_ENGINE_ID, justification.encode())),
418-
)
415+
.finalize_block(&target_hash, Some((GRANDPA_ENGINE_ID, justification.encode())))
419416
.unwrap();
420417

421418
authority_set_changes.push((current_set_id, n));

client/network/sync/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,9 +3172,7 @@ mod test {
31723172

31733173
let finalized_block = blocks[MAX_BLOCKS_TO_LOOK_BACKWARDS as usize * 2 - 1].clone();
31743174
let just = (*b"TEST", Vec::new());
3175-
client
3176-
.finalize_block(BlockId::Hash(finalized_block.hash()), Some(just))
3177-
.unwrap();
3175+
client.finalize_block(&finalized_block.hash(), Some(just)).unwrap();
31783176
sync.update_chain_info(&info.best_hash, info.best_number);
31793177

31803178
let peer_id1 = PeerId::random();
@@ -3303,9 +3301,7 @@ mod test {
33033301

33043302
let finalized_block = blocks[MAX_BLOCKS_TO_LOOK_BACKWARDS as usize * 2 - 1].clone();
33053303
let just = (*b"TEST", Vec::new());
3306-
client
3307-
.finalize_block(BlockId::Hash(finalized_block.hash()), Some(just))
3308-
.unwrap();
3304+
client.finalize_block(&finalized_block.hash(), Some(just)).unwrap();
33093305
sync.update_chain_info(&info.best_hash, info.best_number);
33103306

33113307
let peer_id1 = PeerId::random();

0 commit comments

Comments
 (0)