Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
}
Expand Down
24 changes: 21 additions & 3 deletions core/client/db/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
23 changes: 11 additions & 12 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

Expand Down Expand Up @@ -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,
Expand All @@ -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(),
})
}
Expand Down Expand Up @@ -381,15 +373,15 @@ 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),
_ => return Err(error::ErrorKind::ChangesTriesNotSupported.into()),
};

key_changes::<_, Blake2Hasher>(
config,
&config,
storage,
self.require_block_number_from_id(&BlockId::Hash(first))?.as_(),
&ChangesTrieAnchorBlockId {
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down