client: fix decode block number as u32 (#3130)

* client: fix panic on decode block number as u32

* client: add test for number_index_key

* client: add another test at client level

* client: address review grumbles
This commit is contained in:
André Silva
2019-07-18 15:55:14 +01:00
committed by GitHub
parent e8a2fee6d6
commit 991e9ae263
4 changed files with 81 additions and 45 deletions
+9 -9
View File
@@ -883,7 +883,7 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
transaction,
columns::KEY_LOOKUP,
r.number
);
)?;
}
// canonicalize: set the number lookup to map to this block's hash.
@@ -894,18 +894,18 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
columns::KEY_LOOKUP,
e.number,
e.hash
);
)?;
}
}
let lookup_key = utils::number_and_hash_to_lookup_key(best_to.0, &best_to.1);
let lookup_key = utils::number_and_hash_to_lookup_key(best_to.0, &best_to.1)?;
transaction.put(columns::META, meta_keys::BEST_BLOCK, &lookup_key);
utils::insert_number_to_key_mapping(
transaction,
columns::KEY_LOOKUP,
best_to.0,
best_to.1,
);
)?;
Ok((enacted, retracted))
}
@@ -946,7 +946,7 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
if let Some(justification) = justification {
transaction.put(
columns::JUSTIFICATION,
&utils::number_and_hash_to_lookup_key(number, hash),
&utils::number_and_hash_to_lookup_key(number, hash)?,
&justification.encode(),
);
}
@@ -1021,7 +1021,7 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
let number = pending_block.header.number().clone();
// blocks are keyed by number + hash.
let lookup_key = utils::number_and_hash_to_lookup_key(number, hash);
let lookup_key = utils::number_and_hash_to_lookup_key(number, hash)?;
let (enacted, retracted) = if pending_block.leaf_state.is_best() {
self.set_head_with_transaction(&mut transaction, parent_hash, (number, hash))?
@@ -1034,7 +1034,7 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
columns::KEY_LOOKUP,
number,
hash,
);
)?;
transaction.put(columns::HEADER, &lookup_key, &pending_block.header.encode());
if let Some(body) = pending_block.body {
@@ -1177,7 +1177,7 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
if self.storage.state_db.best_canonical().map(|c| f_num.saturated_into::<u64>() > c).unwrap_or(true) {
let parent_hash = f_header.parent_hash().clone();
let lookup_key = utils::number_and_hash_to_lookup_key(f_num, f_hash.clone());
let lookup_key = utils::number_and_hash_to_lookup_key(f_num, f_hash.clone())?;
transaction.put(columns::META, meta_keys::FINALIZED_BLOCK, &lookup_key);
let commit = self.storage.state_db.canonicalize_block(&f_hash)
@@ -1344,7 +1344,7 @@ impl<Block> client::backend::Backend<Block, Blake2Hasher> for Backend<Block> whe
let hash = self.blockchain.hash(best)?.ok_or_else(
|| client::error::Error::UnknownBlock(
format!("Error reverting to {}. Block hash not found.", best)))?;
let key = utils::number_and_hash_to_lookup_key(best.clone(), &hash);
let key = utils::number_and_hash_to_lookup_key(best.clone(), &hash)?;
transaction.put(columns::META, meta_keys::BEST_BLOCK, &key);
transaction.delete(columns::KEY_LOOKUP, removed.hash().as_ref());
children::remove_children(&mut transaction, columns::META, meta_keys::CHILDREN_PREFIX, hash);