BlockId removal: refactor: Backend::append_justification (#12551)

* BlockId removal: refactor: Backend::append_justification

It changes the arguments of `Backend::append_justification`
from: block: `BlockId<Block>` to: hash: `&Block::Hash`

This PR is part of `BlockId::Number` refactoring analysis (paritytech/substrate#11292)

* Error message improved

Co-authored-by: Adrian Catangiu <adrian@parity.io>

* single error message in beefy::finalize

* println removed

Co-authored-by: Adrian Catangiu <adrian@parity.io>
This commit is contained in:
Michal Kucharczyk
2022-10-30 18:58:29 +01:00
committed by GitHub
parent 9bc59da6b3
commit 0ef7e261a3
4 changed files with 38 additions and 34 deletions
+2 -3
View File
@@ -27,7 +27,6 @@ use sp_blockchain;
use sp_consensus::BlockOrigin;
use sp_core::offchain::OffchainStorage;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, HashFor, NumberFor},
Justification, Justifications, StateVersion, Storage,
};
@@ -485,12 +484,12 @@ pub trait Backend<Block: BlockT>: AuxStore + Send + Sync {
justification: Option<Justification>,
) -> sp_blockchain::Result<()>;
/// Append justification to the block with the given Id.
/// Append justification to the block with the given `hash`.
///
/// This should only be called for blocks that are already finalized.
fn append_justification(
&self,
block: BlockId<Block>,
hash: &Block::Hash,
justification: Justification,
) -> sp_blockchain::Result<()>;
+10 -10
View File
@@ -295,10 +295,9 @@ impl<Block: BlockT> Blockchain<Block> {
fn append_justification(
&self,
id: BlockId<Block>,
hash: &Block::Hash,
justification: Justification,
) -> sp_blockchain::Result<()> {
let hash = self.expect_block_hash_from_id(&id)?;
let mut storage = self.storage.write();
let block = storage
@@ -745,10 +744,10 @@ where
fn append_justification(
&self,
block: BlockId<Block>,
hash: &Block::Hash,
justification: Justification,
) -> sp_blockchain::Result<()> {
self.blockchain.append_justification(block, justification)
self.blockchain.append_justification(hash, justification)
}
fn blockchain(&self) -> &Self::Blockchain {
@@ -865,26 +864,27 @@ mod tests {
fn append_and_retrieve_justifications() {
let blockchain = test_blockchain();
let last_finalized = blockchain.last_finalized().unwrap();
let block = BlockId::Hash(last_finalized);
blockchain.append_justification(block, (ID2, vec![4])).unwrap();
blockchain.append_justification(&last_finalized, (ID2, vec![4])).unwrap();
let justifications = {
let mut just = Justifications::from((ID1, vec![3]));
just.append((ID2, vec![4]));
just
};
assert_eq!(blockchain.justifications(block).unwrap(), Some(justifications));
assert_eq!(
blockchain.justifications(BlockId::Hash(last_finalized)).unwrap(),
Some(justifications)
);
}
#[test]
fn store_duplicate_justifications_is_forbidden() {
let blockchain = test_blockchain();
let last_finalized = blockchain.last_finalized().unwrap();
let block = BlockId::Hash(last_finalized);
blockchain.append_justification(block, (ID2, vec![0])).unwrap();
blockchain.append_justification(&last_finalized, (ID2, vec![0])).unwrap();
assert!(matches!(
blockchain.append_justification(block, (ID2, vec![1])),
blockchain.append_justification(&last_finalized, (ID2, vec![1])),
Err(sp_blockchain::Error::BadJustification(_)),
));
}