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
11 changes: 6 additions & 5 deletions sync/src/block/downloader/header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Kodebox, Inc.
// Copyright 2018-2020 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
Expand All @@ -15,9 +15,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use super::super::message::RequestMessage;
use ccore::encoded::Header;
use ccore::{BlockChainClient, BlockId};
use ctypes::BlockHash;
use ctypes::{BlockHash, Header};
use primitives::U256;
use std::cmp::Ordering;
use std::collections::HashMap;
Expand All @@ -36,9 +35,11 @@ pub struct HeaderDownloader {

seq: U256,
best_hash: BlockHash,
/// The last header we downloaded from this peer.
pivot: BlockHash,
request_time: Option<Instant>,
downloaded: HashMap<BlockHash, Header>,
/// Headers that are importing now.
queued: HashMap<BlockHash, Header>,
trial: usize,
}
Expand Down Expand Up @@ -102,7 +103,7 @@ impl HeaderDownloader {
Some(header) => header.clone(),
None => match self.downloaded.get(&self.pivot) {
Some(header) => header.clone(),
None => self.client.block_header(&BlockId::Hash(self.pivot)).unwrap(),
None => self.client.block_header(&BlockId::Hash(self.pivot)).unwrap().decode(),
},
}
}
Expand Down Expand Up @@ -162,7 +163,7 @@ impl HeaderDownloader {
);
} else if first_header_number == pivot_header.number() {
if pivot_header.number() != 0 {
self.pivot = pivot_header.parent_hash();
self.pivot = *pivot_header.parent_hash();
}
} else {
cerror!(
Expand Down
7 changes: 3 additions & 4 deletions sync/src/block/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,20 +973,19 @@ impl Extension {
},
State::Full => {
let (mut completed, peer_is_caught_up) = if let Some(peer) = self.header_downloaders.get_mut(from) {
let encoded: Vec<_> = headers.iter().map(|h| EncodedHeader::new(h.rlp_bytes().to_vec())).collect();
peer.import_headers(&encoded);
peer.import_headers(&headers);
(peer.downloaded(), peer.is_caught_up())
} else {
(Vec::new(), true)
};
completed.sort_unstable_by_key(EncodedHeader::number);
completed.sort_unstable_by_key(Header::number);

let mut exists = Vec::new();
let mut queued = Vec::new();

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