mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 11:31:08 +03:00
a checking of is_on_current_chain on body sync don't need a batch (#1696)
This commit is contained in:
parent
8cfe9e64ac
commit
23180d6f86
2 changed files with 24 additions and 2 deletions
|
@ -925,8 +925,7 @@ impl Chain {
|
||||||
/// Checks the header_by_height index to verify the header is where we say
|
/// Checks the header_by_height index to verify the header is where we say
|
||||||
/// it is
|
/// it is
|
||||||
pub fn is_on_current_chain(&self, header: &BlockHeader) -> Result<(), Error> {
|
pub fn is_on_current_chain(&self, header: &BlockHeader) -> Result<(), Error> {
|
||||||
let batch = self.store.batch()?;
|
self.store
|
||||||
batch
|
|
||||||
.is_on_current_chain(header)
|
.is_on_current_chain(header)
|
||||||
.map_err(|e| ErrorKind::StoreErr(e, "chain is_on_current_chain".to_owned()).into())
|
.map_err(|e| ErrorKind::StoreErr(e, "chain is_on_current_chain".to_owned()).into())
|
||||||
}
|
}
|
||||||
|
|
|
@ -135,6 +135,29 @@ impl ChainStore {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We are on the current chain if -
|
||||||
|
// * the header by height index matches the header, and
|
||||||
|
// * we are not ahead of the current head
|
||||||
|
pub fn is_on_current_chain(&self, header: &BlockHeader) -> Result<(), Error> {
|
||||||
|
let head = self.head()?;
|
||||||
|
|
||||||
|
// check we are not out ahead of the current head
|
||||||
|
if header.height > head.height {
|
||||||
|
return Err(Error::NotFoundErr(String::from(
|
||||||
|
"header.height > head.height",
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let header_at_height = self.get_header_by_height(header.height)?;
|
||||||
|
if header.hash() == header_at_height.hash() {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(Error::NotFoundErr(String::from(
|
||||||
|
"header.hash == header_at_height.hash",
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_header_by_height(&self, height: u64) -> Result<BlockHeader, Error> {
|
pub fn get_header_by_height(&self, height: u64) -> Result<BlockHeader, Error> {
|
||||||
option_to_not_found(
|
option_to_not_found(
|
||||||
self.db.get_ser(&u64_to_key(HEADER_HEIGHT_PREFIX, height)),
|
self.db.get_ser(&u64_to_key(HEADER_HEIGHT_PREFIX, height)),
|
||||||
|
|
Loading…
Reference in a new issue