mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-21 08:35:40 +00:00
core: fix max depth handling of best_containing (#1234)
This commit is contained in:
committed by
Robert Habermeier
parent
90f6a56b15
commit
e203c285e2
@@ -921,12 +921,14 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
|||||||
.ok_or_else(|| error::Error::from(format!("failed to get hash for block number {}", target_header.number())))?;
|
.ok_or_else(|| error::Error::from(format!("failed to get hash for block number {}", target_header.number())))?;
|
||||||
|
|
||||||
if canon_hash == target_hash {
|
if canon_hash == target_hash {
|
||||||
|
// if no block at the given max depth exists fallback to the best block
|
||||||
if let Some(max_number) = maybe_max_number {
|
if let Some(max_number) = maybe_max_number {
|
||||||
// something has to guarantee that max_number is in chain
|
if let Some(header) = self.backend.blockchain().hash(max_number)? {
|
||||||
return Ok(Some(self.backend.blockchain().hash(max_number)?.ok_or_else(|| error::Error::from(format!("failed to get hash for block number {}", max_number)))?));
|
return Ok(Some(header));
|
||||||
} else {
|
}
|
||||||
return Ok(Some(info.best_hash));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(Some(info.best_hash));
|
||||||
}
|
}
|
||||||
(self.backend.blockchain().leaves()?, info.best_hash)
|
(self.backend.blockchain().leaves()?, info.best_hash)
|
||||||
};
|
};
|
||||||
@@ -1688,6 +1690,26 @@ pub(crate) mod tests {
|
|||||||
assert_eq!(None, client.best_containing(d2.hash().clone(), Some(0)).unwrap());
|
assert_eq!(None, client.best_containing(d2.hash().clone(), Some(0)).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn best_containing_with_max_depth_higher_than_best() {
|
||||||
|
// block tree:
|
||||||
|
// G -> A1 -> A2
|
||||||
|
|
||||||
|
let client = test_client::new();
|
||||||
|
|
||||||
|
// G -> A1
|
||||||
|
let a1 = client.new_block().unwrap().bake().unwrap();
|
||||||
|
client.justify_and_import(BlockOrigin::Own, a1.clone()).unwrap();
|
||||||
|
|
||||||
|
// A1 -> A2
|
||||||
|
let a2 = client.new_block().unwrap().bake().unwrap();
|
||||||
|
client.justify_and_import(BlockOrigin::Own, a2.clone()).unwrap();
|
||||||
|
|
||||||
|
let genesis_hash = client.info().unwrap().chain.genesis_hash;
|
||||||
|
|
||||||
|
assert_eq!(a2.hash(), client.best_containing(genesis_hash, Some(10)).unwrap().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn key_changes_works() {
|
fn key_changes_works() {
|
||||||
let (client, _, test_cases) = prepare_client_with_key_changes();
|
let (client, _, test_cases) = prepare_client_with_key_changes();
|
||||||
|
|||||||
Reference in New Issue
Block a user