a checking of is_on_current_chain on body sync don't need a batch (#1696)

This commit is contained in:
Gary Yu 2018-10-09 20:37:06 +08:00 committed by GitHub
parent 8cfe9e64ac
commit 23180d6f86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View file

@ -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())
} }

View file

@ -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)),