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 1 commit
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
Prev Previous commit
Next Next commit
fix block_hash loading
  • Loading branch information
rphmeier committed May 9, 2018
commit d1f980d509c3f2490ec594fe8f5ddf09716fa7f2
94 changes: 53 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions substrate/client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ substrate-runtime-support = { path = "../../../substrate/runtime-support" }
substrate-codec = { path = "../../../substrate/codec" }

[dev-dependencies]
kvdb-memorydb = { git = "https://github.com/paritytech/parity.git" }
28 changes: 19 additions & 9 deletions substrate/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ extern crate substrate_state_machine as state_machine;
extern crate substrate_primitives as primitives;
extern crate substrate_runtime_support as runtime_support;
extern crate substrate_codec as codec;
#[macro_use] extern crate log;

#[macro_use]
extern crate log;

#[cfg(test)]
extern crate kvdb_memorydb;

use std::sync::Arc;
use std::path::PathBuf;
Expand All @@ -33,7 +38,7 @@ use parking_lot::RwLock;
use runtime_support::Hashable;
use primitives::blake2_256;
use kvdb_rocksdb::{Database, DatabaseConfig};
use kvdb::DBTransaction;
use kvdb::{KeyValueDB, DBTransaction};
use primitives::block::{self, Id as BlockId, HeaderHash};
use state_machine::backend::Backend as StateBackend;
use state_machine::CodeExecutor;
Expand Down Expand Up @@ -99,7 +104,7 @@ struct Meta {

/// Block database
pub struct BlockchainDb {
db: Arc<Database>,
db: Arc<KeyValueDB>,
meta: RwLock<Meta>,
}

Expand Down Expand Up @@ -145,7 +150,7 @@ impl BlockchainDb {
}
}

fn new(db: Arc<Database>) -> Result<BlockchainDb, client::error::Error> {
fn new(db: Arc<KeyValueDB>) -> Result<BlockchainDb, client::error::Error> {
let (best_hash, best_number) = if let Some(Some(header)) = db.get(columns::META, meta::BEST_BLOCK).and_then(|id|
match id {
Some(id) => db.get(columns::HEADER, &id).map(|h| h.map(|b| block::Header::decode(&mut &b[..]))),
Expand Down Expand Up @@ -243,9 +248,9 @@ impl client::blockchain::Backend for BlockchainDb {
}

fn hash(&self, number: block::Number) -> Result<Option<block::HeaderHash>, client::error::Error> {
Ok(self.db.get(columns::BLOCK_INDEX, &number_to_db_key(number))
.map_err(db_err)?
.map(|hash| block::HeaderHash::from_slice(&hash)))
self.read_db(BlockId::Number(number), columns::HEADER).map(|x|
x.map(|raw| blake2_256(&raw[..])).map(Into::into)
)
}
}

Expand Down Expand Up @@ -304,13 +309,13 @@ impl state_machine::Backend for DbState {

/// In-memory backend. Keeps all states and blocks in memory. Useful for testing.
pub struct Backend {
db: Arc<Database>,
db: Arc<KeyValueDB>,
blockchain: BlockchainDb,
old_states: RwLock<HashMap<BlockKey, state_machine::backend::InMemory>>,
}

impl Backend {
/// Create a new instance of in-mem backend.
/// Create a new instance of database backend.
pub fn new(config: &DatabaseSettings) -> Result<Backend, client::error::Error> {
let mut db_config = DatabaseConfig::with_columns(columns::NUM_COLUMNS);
db_config.memory_budget = config.cache_size;
Expand All @@ -334,6 +339,7 @@ impl Backend {
old_states: RwLock::new(old_states)
})
}

}

impl client::backend::Backend for Backend {
Expand Down Expand Up @@ -397,3 +403,7 @@ impl client::backend::Backend for Backend {
}
}

#[cfg(test)]
mod tests {

}
2 changes: 2 additions & 0 deletions substrate/network/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl ChainSync {
PeerSyncState::AncestorSearch(n) => {
match response.blocks.get(0) {
Some(ref block) => {
trace!(target: "sync", "Got ancestry block #{} ({}) from peer {}", n, block.hash, peer_id);
match protocol.chain().block_hash(n) {
Ok(Some(block_hash)) if block_hash == block.hash => {
peer.common_hash = block.hash;
Expand Down Expand Up @@ -423,6 +424,7 @@ impl ChainSync {
}

fn request_ancestry(io: &mut SyncIo, protocol: &Protocol, peer_id: PeerId, block: BlockNumber) {
trace!(target: "sync", "Requesting ancestry block #{} from {}", block, peer_id);
let request = message::BlockRequest {
id: 0,
fields: vec![message::BlockAttribute::Header, message::BlockAttribute::Justification],
Expand Down