mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 11:31:08 +03:00
Sync headers slice (#2085)
* pass slices of headers around, not vec of headers * rustfmt
This commit is contained in:
parent
bf815aa5cd
commit
a3523028dc
8 changed files with 16 additions and 20 deletions
|
@ -335,11 +335,7 @@ impl Chain {
|
||||||
/// Attempt to add new headers to the header chain (or fork).
|
/// Attempt to add new headers to the header chain (or fork).
|
||||||
/// This is only ever used during sync and is based on sync_head.
|
/// This is only ever used during sync and is based on sync_head.
|
||||||
/// We update header_head here if our total work increases.
|
/// We update header_head here if our total work increases.
|
||||||
pub fn sync_block_headers(
|
pub fn sync_block_headers(&self, headers: &[BlockHeader], opts: Options) -> Result<(), Error> {
|
||||||
&self,
|
|
||||||
headers: &Vec<BlockHeader>,
|
|
||||||
opts: Options,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
let mut txhashset = self.txhashset.write();
|
let mut txhashset = self.txhashset.write();
|
||||||
let batch = self.store.batch()?;
|
let batch = self.store.batch()?;
|
||||||
let mut ctx = self.new_ctx(opts, batch, &mut txhashset)?;
|
let mut ctx = self.new_ctx(opts, batch, &mut txhashset)?;
|
||||||
|
|
|
@ -182,7 +182,7 @@ pub fn process_block(b: &Block, ctx: &mut BlockContext) -> Result<Option<Tip>, E
|
||||||
/// Process the block header.
|
/// Process the block header.
|
||||||
/// This is only ever used during sync and uses a context based on sync_head.
|
/// This is only ever used during sync and uses a context based on sync_head.
|
||||||
pub fn sync_block_headers(
|
pub fn sync_block_headers(
|
||||||
headers: &Vec<BlockHeader>,
|
headers: &[BlockHeader],
|
||||||
ctx: &mut BlockContext,
|
ctx: &mut BlockContext,
|
||||||
) -> Result<Option<Tip>, Error> {
|
) -> Result<Option<Tip>, Error> {
|
||||||
if let Some(header) = headers.first() {
|
if let Some(header) = headers.first() {
|
||||||
|
|
|
@ -546,11 +546,11 @@ impl ChainAdapter for TrackingAdapter {
|
||||||
self.adapter.header_received(bh, addr)
|
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)
|
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)
|
self.adapter.locate_headers(locator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 !self.adapter.headers_received(headers, peer_addr) {
|
||||||
// if the peer sent us a block header that's intrinsically bad
|
// if the peer sent us a block header that's intrinsically bad
|
||||||
// they are either mistaken or malevolent, both of which require a ban
|
// 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)
|
self.adapter.locate_headers(hs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -182,7 +182,7 @@ impl MessageHandler for Protocol {
|
||||||
Type::GetHeaders => {
|
Type::GetHeaders => {
|
||||||
// load headers from the locator
|
// load headers from the locator
|
||||||
let loc: Locator = msg.body()?;
|
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
|
// serialize and send all the headers over
|
||||||
Ok(Some(Response::new(
|
Ok(Some(Response::new(
|
||||||
|
@ -216,7 +216,7 @@ impl MessageHandler for Protocol {
|
||||||
headers.push(header);
|
headers.push(header);
|
||||||
total_bytes_read += bytes_read;
|
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.
|
// Now check we read the correct total number of bytes off the stream.
|
||||||
|
|
|
@ -231,10 +231,10 @@ impl ChainAdapter for DummyAdapter {
|
||||||
fn block_received(&self, _: core::Block, _: SocketAddr) -> bool {
|
fn block_received(&self, _: core::Block, _: SocketAddr) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
fn headers_received(&self, _: Vec<core::BlockHeader>, _: SocketAddr) -> bool {
|
fn headers_received(&self, _: &[core::BlockHeader], _: SocketAddr) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
fn locate_headers(&self, _: Vec<Hash>) -> Vec<core::BlockHeader> {
|
fn locate_headers(&self, _: &[Hash]) -> Vec<core::BlockHeader> {
|
||||||
vec![]
|
vec![]
|
||||||
}
|
}
|
||||||
fn get_block(&self, _: Hash) -> Option<core::Block> {
|
fn get_block(&self, _: Hash) -> Option<core::Block> {
|
||||||
|
|
|
@ -359,12 +359,12 @@ pub trait ChainAdapter: Sync + Send {
|
||||||
/// A set of block header has been received, typically in response to a
|
/// A set of block header has been received, typically in response to a
|
||||||
/// block
|
/// block
|
||||||
/// header request.
|
/// 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
|
/// Finds a list of block headers based on the provided locator. Tries to
|
||||||
/// identify the common chain and gets the headers that follow it
|
/// identify the common chain and gets the headers that follow it
|
||||||
/// immediately.
|
/// 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.
|
/// Gets a full block by its hash.
|
||||||
fn get_block(&self, h: Hash) -> Option<core::Block>;
|
fn get_block(&self, h: Hash) -> Option<core::Block>;
|
||||||
|
|
|
@ -231,7 +231,7 @@ impl p2p::ChainAdapter for NetToChainAdapter {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn headers_received(&self, bhs: Vec<core::BlockHeader>, addr: SocketAddr) -> bool {
|
fn headers_received(&self, bhs: &[core::BlockHeader], addr: SocketAddr) -> bool {
|
||||||
info!(
|
info!(
|
||||||
"Received block headers {:?} from {}",
|
"Received block headers {:?} from {}",
|
||||||
bhs.iter().map(|x| x.hash()).collect::<Vec<_>>(),
|
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
|
// 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 {
|
if let &Err(ref e) = &res {
|
||||||
debug!("Block headers refused by chain: {:?}", e);
|
debug!("Block headers refused by chain: {:?}", e);
|
||||||
|
|
||||||
|
@ -254,7 +254,7 @@ impl p2p::ChainAdapter for NetToChainAdapter {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn locate_headers(&self, locator: Vec<Hash>) -> Vec<core::BlockHeader> {
|
fn locate_headers(&self, locator: &[Hash]) -> Vec<core::BlockHeader> {
|
||||||
debug!("locator: {:?}", locator);
|
debug!("locator: {:?}", locator);
|
||||||
|
|
||||||
let header = match self.find_common_header(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.
|
// 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 {
|
for hash in locator {
|
||||||
if let Ok(header) = self.chain().get_block_header(&hash) {
|
if let Ok(header) = self.chain().get_block_header(&hash) {
|
||||||
if let Ok(header_at_height) = self.chain().get_header_by_height(header.height) {
|
if let Ok(header_at_height) = self.chain().get_header_by_height(header.height) {
|
||||||
|
|
Loading…
Reference in a new issue