Skip to content
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
13 changes: 5 additions & 8 deletions core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,22 +416,19 @@ impl ImportBlock for Client {
Ok(self.importer.block_queue.import(unverified)?)
}

fn import_header(&self, bytes: Bytes) -> Result<BlockHash, BlockImportError> {
let unverified = encoded::Header::new(bytes).decode();
{
if self.block_chain().is_known_header(&unverified.hash()) {
return Err(BlockImportError::Import(ImportError::AlreadyInChain))
}
fn import_header(&self, unverified: Header) -> Result<BlockHash, BlockImportError> {
if self.block_chain().is_known_header(&unverified.hash()) {
return Err(BlockImportError::Import(ImportError::AlreadyInChain))
}
Ok(self.importer.header_queue.import(unverified)?)
}

fn import_trusted_header(&self, header: &Header) -> Result<BlockHash, BlockImportError> {
fn import_trusted_header(&self, header: Header) -> Result<BlockHash, BlockImportError> {
if self.block_chain().is_known_header(&header.hash()) {
return Err(BlockImportError::Import(ImportError::AlreadyInChain))
}
let import_lock = self.importer.import_lock.lock();
self.importer.import_trusted_header(header, self, &import_lock);
self.importer.import_trusted_header(&header, self, &import_lock);
Ok(header.hash())
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ pub trait ImportBlock {
fn import_block(&self, bytes: Bytes) -> Result<BlockHash, BlockImportError>;

/// Import a header into the blockchain
fn import_header(&self, bytes: Bytes) -> Result<BlockHash, BlockImportError>;
fn import_header(&self, header: Header) -> Result<BlockHash, BlockImportError>;

/// Import a trusted header into the blockchain
/// Trusted header doesn't go through any verifications and doesn't update the best header
fn import_trusted_header(&self, header: &Header) -> Result<BlockHash, BlockImportError>;
fn import_trusted_header(&self, header: Header) -> Result<BlockHash, BlockImportError>;

/// Import a trusted block into the blockchain
/// Trusted block doesn't go through any verifications and doesn't update the best block
Expand Down
4 changes: 2 additions & 2 deletions core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ impl ImportBlock for TestBlockChainClient {
Ok(h)
}

fn import_header(&self, _bytes: Bytes) -> Result<BlockHash, BlockImportError> {
fn import_header(&self, _bytes: Header) -> Result<BlockHash, BlockImportError> {
unimplemented!()
}

fn import_trusted_header(&self, _header: &Header) -> Result<BlockHash, BlockImportError> {
fn import_trusted_header(&self, _header: Header) -> Result<BlockHash, BlockImportError> {
unimplemented!()
}

Expand Down
35 changes: 20 additions & 15 deletions sync/src/block/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ impl Extension {
match response {
ResponseMessage::Headers(headers) => {
self.dismiss_request(from, id);
self.on_header_response(from, &headers)
self.on_header_response(from, headers)
}
ResponseMessage::Bodies(bodies) => {
self.check_sync_variable();
Expand Down Expand Up @@ -938,17 +938,29 @@ impl Extension {
}
}

fn on_header_response(&mut self, from: &NodeId, headers: &[Header]) {
fn on_header_response(&mut self, from: &NodeId, mut headers: Vec<Header>) {
ctrace!(SYNC, "Received header response from({}) with length({})", from, headers.len());
match self.state {
State::SnapshotHeader(hash, _) => match headers {
[parent, header] if header.hash() == hash => {
State::SnapshotHeader(hash, _) => {
if headers.len() != 2 || headers[1].hash() != hash {
cdebug!(
SYNC,
"Peer {} responded with a invalid response. requested hash: {}, response length: {}",
from,
hash,
headers.len()
)
} else {
let header = headers.pop().expect("headers.len() == 2");
let header_hash = header.hash();
let parent = headers.pop().expect("headers.len() == 1");
let parent_hash = parent.hash();
match self.client.import_trusted_header(parent) {
Ok(_)
| Err(BlockImportError::Import(ImportError::AlreadyInChain))
| Err(BlockImportError::Import(ImportError::AlreadyQueued)) => {}
Err(err) => {
cwarn!(SYNC, "Cannot import header({}): {:?}", parent.hash(), err);
cwarn!(SYNC, "Cannot import header({}): {:?}", parent_hash, err);
return
}
}
Expand All @@ -957,20 +969,13 @@ impl Extension {
| Err(BlockImportError::Import(ImportError::AlreadyInChain))
| Err(BlockImportError::Import(ImportError::AlreadyQueued)) => {}
Err(err) => {
cwarn!(SYNC, "Cannot import header({}): {:?}", header.hash(), err);
cwarn!(SYNC, "Cannot import header({}): {:?}", header_hash, err);
return
}
}
self.move_state();
}
_ => cdebug!(
SYNC,
"Peer {} responded with a invalid response. requested hash: {}, response length: {}",
from,
hash,
headers.len()
),
},
}
State::Full => {
let (mut completed, peer_is_caught_up) = if let Some(peer) = self.header_downloaders.get_mut(from) {
peer.import_headers(&headers);
Expand All @@ -985,7 +990,7 @@ impl Extension {

for header in completed {
let hash = header.hash();
match self.client.import_header(header.rlp_bytes()) {
match self.client.import_header(header) {
Err(BlockImportError::Import(ImportError::AlreadyInChain)) => exists.push(hash),
Err(BlockImportError::Import(ImportError::AlreadyQueued)) => queued.push(hash),
// FIXME: handle import errors
Expand Down