mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-21 22:41:02 +00:00
Fix light client startup (#1130)
* fix light client startup * fix grumbles
This commit is contained in:
committed by
GitHub
parent
d149f3358a
commit
62c590ef25
@@ -168,7 +168,7 @@ impl<Block: BlockT> BlockchainDb<Block> {
|
||||
is_finalized: bool
|
||||
) {
|
||||
let mut meta = self.meta.write();
|
||||
if number == Zero::zero() {
|
||||
if number.is_zero() {
|
||||
meta.genesis_hash = hash;
|
||||
meta.finalized_hash = hash;
|
||||
}
|
||||
@@ -778,7 +778,7 @@ impl<Block> client::backend::Backend<Block, Blake2Hasher> for Backend<Block> whe
|
||||
|
||||
transaction.put(columns::HASH_LOOKUP, hash.as_ref(), &lookup_key);
|
||||
|
||||
if number == Zero::zero() {
|
||||
if number.is_zero() {
|
||||
transaction.put(columns::META, meta_keys::FINALIZED_BLOCK, &lookup_key);
|
||||
transaction.put(columns::META, meta_keys::GENESIS_HASH, hash.as_ref());
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ impl<Block> LightStorage<Block>
|
||||
) {
|
||||
let mut meta = self.meta.write();
|
||||
|
||||
if number == Zero::zero() {
|
||||
if number.is_zero() {
|
||||
meta.genesis_hash = hash;
|
||||
meta.finalized_hash = hash;
|
||||
}
|
||||
@@ -369,6 +369,11 @@ impl<Block> LightBlockchainStorage<Block> for LightStorage<Block>
|
||||
transaction.put(columns::HEADER, &lookup_key, &header.encode());
|
||||
transaction.put(columns::HASH_LOOKUP, hash.as_ref(), &lookup_key);
|
||||
|
||||
if number.is_zero() {
|
||||
transaction.put(columns::META, meta_keys::FINALIZED_BLOCK, &lookup_key);
|
||||
transaction.put(columns::META, meta_keys::GENESIS_HASH, hash.as_ref());
|
||||
}
|
||||
|
||||
let finalized = match leaf_state {
|
||||
NewBlockState::Final => true,
|
||||
_ => false,
|
||||
@@ -385,7 +390,7 @@ impl<Block> LightBlockchainStorage<Block> for LightStorage<Block>
|
||||
let mut cache = self.cache.0.write();
|
||||
let cache_ops = cache.transaction(&mut transaction)
|
||||
.on_block_insert(
|
||||
ComplexBlockId::new(*header.parent_hash(), if number == Zero::zero() { Zero::zero() } else { number - One::one() }),
|
||||
ComplexBlockId::new(*header.parent_hash(), if number.is_zero() { Zero::zero() } else { number - One::one() }),
|
||||
ComplexBlockId::new(hash, number),
|
||||
authorities,
|
||||
finalized,
|
||||
@@ -429,7 +434,7 @@ impl<Block> LightBlockchainStorage<Block> for LightStorage<Block>
|
||||
let mut cache = self.cache.0.write();
|
||||
let cache_ops = cache.transaction(&mut transaction)
|
||||
.on_block_finalize(
|
||||
ComplexBlockId::new(*header.parent_hash(), if number == Zero::zero() { Zero::zero() } else { number - One::one() }),
|
||||
ComplexBlockId::new(*header.parent_hash(), if number.is_zero() { Zero::zero() } else { number - One::one() }),
|
||||
ComplexBlockId::new(hash, number)
|
||||
)?
|
||||
.into_ops();
|
||||
@@ -860,4 +865,17 @@ pub(crate) mod tests {
|
||||
assert_eq!(db.cache().authorities_at(BlockId::Hash(hash6_2)), Some(vec![[4u8; 32].into()]));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn database_is_reopened() {
|
||||
let db = LightStorage::new_test();
|
||||
let hash0 = insert_final_block(&db, None, || default_header(&Default::default(), 0));
|
||||
assert_eq!(db.info().unwrap().best_hash, hash0);
|
||||
assert_eq!(db.header(BlockId::Hash(hash0)).unwrap().unwrap().hash(), hash0);
|
||||
|
||||
let db = db.db;
|
||||
let db = LightStorage::from_kvdb(db).unwrap();
|
||||
assert_eq!(db.info().unwrap().best_hash, hash0);
|
||||
assert_eq!(db.header(BlockId::Hash::<Block>(hash0)).unwrap().unwrap().hash(), hash0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,6 @@ pub struct Client<B, E, Block, RA> where Block: BlockT {
|
||||
importing_block: RwLock<Option<Block::Hash>>, // holds the block hash currently being imported. TODO: replace this with block queue
|
||||
block_execution_strategy: ExecutionStrategy,
|
||||
api_execution_strategy: ExecutionStrategy,
|
||||
changes_trie_config: Option<ChangesTrieConfiguration>,
|
||||
_phantom: PhantomData<RA>,
|
||||
}
|
||||
|
||||
@@ -245,12 +244,6 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
backend.commit_operation(op)?;
|
||||
}
|
||||
|
||||
// changes trie configuration should never change => we can read it in advance
|
||||
let changes_trie_config = backend.state_at(BlockId::Number(backend.blockchain().info()?.best_number))?
|
||||
.storage(well_known_keys::CHANGES_TRIE_CONFIG)
|
||||
.map_err(|e| error::Error::from_state(Box::new(e)))?
|
||||
.and_then(|c| Decode::decode(&mut &*c));
|
||||
|
||||
Ok(Client {
|
||||
backend,
|
||||
executor,
|
||||
@@ -261,7 +254,6 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
importing_block: Default::default(),
|
||||
block_execution_strategy,
|
||||
api_execution_strategy,
|
||||
changes_trie_config,
|
||||
_phantom: Default::default(),
|
||||
})
|
||||
}
|
||||
@@ -381,7 +373,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
last: Block::Hash,
|
||||
key: &[u8]
|
||||
) -> error::Result<Vec<(NumberFor<Block>, u32)>> {
|
||||
let config = self.changes_trie_config.as_ref();
|
||||
let config = self.changes_trie_config()?;
|
||||
let storage = self.backend.changes_trie_storage();
|
||||
let (config, storage) = match (config, storage) {
|
||||
(Some(config), Some(storage)) => (config, storage),
|
||||
@@ -389,7 +381,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
};
|
||||
|
||||
key_changes::<_, Blake2Hasher>(
|
||||
config,
|
||||
&config,
|
||||
storage,
|
||||
self.require_block_number_from_id(&BlockId::Hash(first))?.as_(),
|
||||
&ChangesTrieAnchorBlockId {
|
||||
@@ -463,7 +455,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
}
|
||||
}
|
||||
|
||||
let config = self.changes_trie_config.as_ref();
|
||||
let config = self.changes_trie_config()?;
|
||||
let storage = self.backend.changes_trie_storage();
|
||||
let (config, storage) = match (config, storage) {
|
||||
(Some(config), Some(storage)) => (config, storage),
|
||||
@@ -484,7 +476,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
|
||||
// fetch key changes proof
|
||||
let key_changes_proof = key_changes_proof::<_, Blake2Hasher>(
|
||||
config,
|
||||
&config,
|
||||
&recording_storage,
|
||||
self.require_block_number_from_id(&BlockId::Hash(first))?.as_(),
|
||||
&ChangesTrieAnchorBlockId {
|
||||
@@ -975,6 +967,13 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
|
||||
unreachable!("this is a bug. `target_hash` is in blockchain but wasn't found following all leaves backwards");
|
||||
}
|
||||
|
||||
fn changes_trie_config(&self) -> Result<Option<ChangesTrieConfiguration>, Error> {
|
||||
Ok(self.backend.state_at(BlockId::Number(self.backend.blockchain().info()?.best_number))?
|
||||
.storage(well_known_keys::CHANGES_TRIE_CONFIG)
|
||||
.map_err(|e| error::Error::from_state(Box::new(e)))?
|
||||
.and_then(|c| Decode::decode(&mut &*c)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, E, Block, RA> ChainHeaderBackend<Block> for Client<B, E, Block, RA> where
|
||||
|
||||
Reference in New Issue
Block a user