Sync headers slice (#2085)

* pass slices of headers around, not vec of headers

* rustfmt
This commit is contained in:
Antioch Peverell 2018-12-05 16:50:32 +00:00 committed by GitHub
parent bf815aa5cd
commit a3523028dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 16 additions and 20 deletions

View file

@ -335,11 +335,7 @@ impl Chain {
/// Attempt to add new headers to the header chain (or fork).
/// This is only ever used during sync and is based on sync_head.
/// We update header_head here if our total work increases.
pub fn sync_block_headers(
&self,
headers: &Vec<BlockHeader>,
opts: Options,
) -> Result<(), Error> {
pub fn sync_block_headers(&self, headers: &[BlockHeader], opts: Options) -> Result<(), Error> {
let mut txhashset = self.txhashset.write();
let batch = self.store.batch()?;
let mut ctx = self.new_ctx(opts, batch, &mut txhashset)?;

View file

@ -182,7 +182,7 @@ pub fn process_block(b: &Block, ctx: &mut BlockContext) -> Result<Option<Tip>, E
/// Process the block header.
/// This is only ever used during sync and uses a context based on sync_head.
pub fn sync_block_headers(
headers: &Vec<BlockHeader>,
headers: &[BlockHeader],
ctx: &mut BlockContext,
) -> Result<Option<Tip>, Error> {
if let Some(header) = headers.first() {

View file

@ -546,11 +546,11 @@ impl ChainAdapter for TrackingAdapter {
self.adapter.header_received(bh, addr)
}
fn headers_received(&self, bh: Vec<core::BlockHeader>, addr: SocketAddr) -> bool {
fn headers_received(&self, bh: &[core::BlockHeader], addr: SocketAddr) -> bool {
self.adapter.headers_received(bh, addr)
}
fn locate_headers(&self, locator: Vec<Hash>) -> Vec<core::BlockHeader> {
fn locate_headers(&self, locator: &[Hash]) -> Vec<core::BlockHeader> {
self.adapter.locate_headers(locator)
}

View file

@ -532,7 +532,7 @@ impl ChainAdapter for Peers {
}
}
fn headers_received(&self, headers: Vec<core::BlockHeader>, peer_addr: SocketAddr) -> bool {
fn headers_received(&self, headers: &[core::BlockHeader], peer_addr: SocketAddr) -> bool {
if !self.adapter.headers_received(headers, peer_addr) {
// if the peer sent us a block header that's intrinsically bad
// they are either mistaken or malevolent, both of which require a ban
@ -543,7 +543,7 @@ impl ChainAdapter for Peers {
}
}
fn locate_headers(&self, hs: Vec<Hash>) -> Vec<core::BlockHeader> {
fn locate_headers(&self, hs: &[Hash]) -> Vec<core::BlockHeader> {
self.adapter.locate_headers(hs)
}

View file

@ -182,7 +182,7 @@ impl MessageHandler for Protocol {
Type::GetHeaders => {
// load headers from the locator
let loc: Locator = msg.body()?;
let headers = adapter.locate_headers(loc.hashes);
let headers = adapter.locate_headers(&loc.hashes);
// serialize and send all the headers over
Ok(Some(Response::new(
@ -216,7 +216,7 @@ impl MessageHandler for Protocol {
headers.push(header);
total_bytes_read += bytes_read;
}
adapter.headers_received(headers, self.addr);
adapter.headers_received(&headers, self.addr);
}
// Now check we read the correct total number of bytes off the stream.

View file

@ -231,10 +231,10 @@ impl ChainAdapter for DummyAdapter {
fn block_received(&self, _: core::Block, _: SocketAddr) -> bool {
true
}
fn headers_received(&self, _: Vec<core::BlockHeader>, _: SocketAddr) -> bool {
fn headers_received(&self, _: &[core::BlockHeader], _: SocketAddr) -> bool {
true
}
fn locate_headers(&self, _: Vec<Hash>) -> Vec<core::BlockHeader> {
fn locate_headers(&self, _: &[Hash]) -> Vec<core::BlockHeader> {
vec![]
}
fn get_block(&self, _: Hash) -> Option<core::Block> {

View file

@ -359,12 +359,12 @@ pub trait ChainAdapter: Sync + Send {
/// A set of block header has been received, typically in response to a
/// block
/// header request.
fn headers_received(&self, bh: Vec<core::BlockHeader>, addr: SocketAddr) -> bool;
fn headers_received(&self, bh: &[core::BlockHeader], addr: SocketAddr) -> bool;
/// Finds a list of block headers based on the provided locator. Tries to
/// identify the common chain and gets the headers that follow it
/// immediately.
fn locate_headers(&self, locator: Vec<Hash>) -> Vec<core::BlockHeader>;
fn locate_headers(&self, locator: &[Hash]) -> Vec<core::BlockHeader>;
/// Gets a full block by its hash.
fn get_block(&self, h: Hash) -> Option<core::Block>;

View file

@ -231,7 +231,7 @@ impl p2p::ChainAdapter for NetToChainAdapter {
true
}
fn headers_received(&self, bhs: Vec<core::BlockHeader>, addr: SocketAddr) -> bool {
fn headers_received(&self, bhs: &[core::BlockHeader], addr: SocketAddr) -> bool {
info!(
"Received block headers {:?} from {}",
bhs.iter().map(|x| x.hash()).collect::<Vec<_>>(),
@ -243,7 +243,7 @@ impl p2p::ChainAdapter for NetToChainAdapter {
}
// try to add headers to our header chain
let res = self.chain().sync_block_headers(&bhs, self.chain_opts());
let res = self.chain().sync_block_headers(bhs, self.chain_opts());
if let &Err(ref e) = &res {
debug!("Block headers refused by chain: {:?}", e);
@ -254,7 +254,7 @@ impl p2p::ChainAdapter for NetToChainAdapter {
true
}
fn locate_headers(&self, locator: Vec<Hash>) -> Vec<core::BlockHeader> {
fn locate_headers(&self, locator: &[Hash]) -> Vec<core::BlockHeader> {
debug!("locator: {:?}", locator);
let header = match self.find_common_header(locator) {
@ -402,7 +402,7 @@ impl NetToChainAdapter {
}
// Find the first locator hash that refers to a known header on our main chain.
fn find_common_header(&self, locator: Vec<Hash>) -> Option<BlockHeader> {
fn find_common_header(&self, locator: &[Hash]) -> Option<BlockHeader> {
for hash in locator {
if let Ok(header) = self.chain().get_block_header(&hash) {
if let Ok(header_at_height) = self.chain().get_header_by_height(header.height) {