Permalink
Browse files

cardano: Deprecate Block::get_header

Change all uses of method get_header to use method header instead
and work with the temporary reference value.
  • Loading branch information...
mzabaluev committed Jan 10, 2019
1 parent 9198a99 commit a755f5fb469308d523509641a644f85aaee60c5c
@@ -58,7 +58,7 @@ impl RawBlock {
pub fn to_header(&self) -> cbor_event::Result<RawBlockHeader> {
// TODO optimise if possible with the CBOR structure by skipping some prefix and some suffix ...
let blk = self.decode()?;
Ok(blk.get_header().to_raw())
Ok(blk.header().to_raw())
}
}

@@ -239,6 +239,7 @@ impl Block {
}
}

#[deprecated(note = "use header() instead")]
pub fn get_header(&self) -> BlockHeader {
match self {
&Block::BoundaryBlock(ref blk) => BlockHeader::BoundaryBlockHeader(blk.header.clone()),
@@ -283,7 +284,7 @@ impl core::property::Block for Block {
type Header = BlockHeader;

fn id(&self) -> Self::Id {
self.get_header().compute_hash()
self.header().compute_hash()
}

fn parent_id(&self) -> Self::Id {
@@ -300,7 +301,7 @@ impl core::property::Block for Block {
}

fn header(&self) -> BlockHeader {
self.get_header()
self.header().into()
}
}

@@ -145,7 +145,7 @@ pub fn verify_block(block_hash: &HeaderHash, blk: &Block) -> Result<(), Error> {
}
};

if block_hash != &blk.get_header().compute_hash() {
if block_hash != &blk.header().compute_hash() {
return Err(Error::WrongBlockHash);
}

@@ -81,7 +81,7 @@ impl ChainState {
add_error(&mut res, self.do_verify(block_hash, blk));

self.last_block = block_hash.clone();
self.last_date = Some(blk.get_header().get_blockdate());
self.last_date = Some(blk.header().blockdate());
// FIXME: count boundary blocks as part of the chain length?
self.chain_length += 1;

@@ -113,7 +113,8 @@ impl ChainState {
return Err(Error::WrongMagic);
}

let prev_block = blk.get_header().get_previous_header();
let hdr = blk.header();
let prev_block = hdr.previous_header();
if prev_block != self.last_block {
return Err(Error::WrongPreviousBlock(
prev_block,
@@ -122,7 +123,7 @@ impl ChainState {
}

// Check the block date.
let date = blk.get_header().get_blockdate();
let date = hdr.blockdate();

match self.last_date {
Some(last_date) => {
@@ -179,19 +179,19 @@ impl Api for HermesEndPoint {
while let Some(data) = packfile.next_block()? {
let block_raw = block::RawBlock(data);
let block = block_raw.decode()?;
let hdr = block.get_header();
let hdr = block.header();

assert!(hdr.get_blockdate().get_epochid() == epoch);
assert!(hdr.blockdate().get_epochid() == epoch);
//assert!(from.date != hdr.get_blockdate() || from.hash == hdr.compute_hash());

if from.date <= hdr.get_blockdate() {
if from.date <= hdr.blockdate() {
got_block(&hdr.compute_hash(), &block, &block_raw);
}

from = BlockRef {
hash: hdr.compute_hash(),
parent: hdr.get_previous_header(),
date: hdr.get_blockdate(),
parent: hdr.previous_header(),
date: hdr.blockdate(),
};
inclusive = false;
}
@@ -204,10 +204,12 @@ impl Api for HermesEndPoint {
loop {
let block_raw = self.get_block(&to)?;
let block = block_raw.decode()?;
let hdr = block.get_header();
assert!(hdr.get_blockdate() >= from.date);
let prev = hdr.get_previous_header();
blocks.push((hdr.compute_hash(), block, block_raw));
let (prev, hash) = {
let hdr = block.header();
assert!(hdr.blockdate() >= from.date);
(hdr.previous_header(), hdr.compute_hash())
};
blocks.push((hash, block, block_raw));
if (inclusive && prev == from.parent) || (!inclusive && prev == from.hash) {
break;
}
@@ -217,7 +217,7 @@ impl Api for OpenPeer {
to.hash.clone(),
&mut |rblk| {
let blk = rblk.decode()?;
got_block(&blk.get_header().compute_hash(), &blk, &rblk);
got_block(&blk.header().compute_hash(), &blk, &rblk);
Ok(())
},
)?;
@@ -60,12 +60,12 @@ fn net_sync_to<A: Api>(
Err(Error::NoSuchTag) => genesis_ref.clone(),
Err(err) => panic!(err),
Ok(block) => {
let header = block.get_header();
let header = block.header();
(
BlockRef {
hash: header.compute_hash().clone(),
parent: header.get_previous_header(),
date: header.get_blockdate(),
hash: header.compute_hash(),
parent: header.previous_header(),
date: header.blockdate(),
},
false,
)
@@ -152,10 +152,11 @@ fn net_sync_to<A: Api>(
loop {
let block_raw = storage.read_block(&cur_hash.into()).unwrap();
let block = block_raw.decode().unwrap();
let hdr = block.get_header();
assert!(hdr.get_blockdate().get_epochid() == first_unstable_epoch);
cur_hash = hdr.get_previous_header();
if hdr.get_blockdate().is_boundary() {
let hdr = block.header();
let blockdate = hdr.blockdate();
assert!(blockdate.get_epochid() == first_unstable_epoch);
cur_hash = hdr.previous_header();
if blockdate.is_boundary() {
break;
}
}
@@ -178,7 +179,7 @@ fn net_sync_to<A: Api>(
our_tip.1,
&tip,
&mut |block_hash, block, block_raw| {
let date = block.get_header().get_blockdate();
let date = block.header().blockdate();

// Flush the previous epoch (if any). FIXME: shouldn't rely on
// 'date' here since the block hasn't been verified yet.
@@ -196,11 +197,9 @@ fn net_sync_to<A: Api>(
}

// FIXME: propagate errors
chain_state.verify_block(block_hash, block).expect(&format!(
"Block {} ({}) failed to verify",
block_hash,
block.get_header().get_blockdate()
));
chain_state
.verify_block(block_hash, block)
.expect(&format!("Block {} ({}) failed to verify", block_hash, date));

if date.get_epochid() >= first_unstable_epoch {
// This block is not part of a stable epoch yet and could
@@ -340,11 +339,14 @@ fn get_unpacked_blocks_in_epoch(
let block_raw = storage.read_block(&cur_hash.clone().into()).unwrap();
blobs_to_delete.push(cur_hash.clone());
let block = block_raw.decode().unwrap();
let hdr = block.get_header();
assert!(hdr.get_blockdate().get_epochid() == epoch_id);
let (blockdate, prev_hash) = {
let hdr = block.header();
(hdr.blockdate(), hdr.previous_header())
};
assert!(blockdate.get_epochid() == epoch_id);
blocks.push((cur_hash, block_raw, block));
cur_hash = hdr.get_previous_header();
if hdr.get_blockdate().is_boundary() {
cur_hash = prev_hash;
if blockdate.is_boundary() {
break;
}
}
@@ -45,9 +45,10 @@ pub fn get_last_blockid(
}
if let Some(blk_raw) = last_blk_raw {
let blk = block::RawBlock(blk_raw).decode().unwrap();
let hdr = blk.get_header();
info!("last_blockid: {} {}", hdr.compute_hash(), hdr.get_slotid());
Some(hdr.compute_hash())
let hdr = blk.header();
let hash = hdr.compute_hash();
info!("last_blockid: {} {}", &hash, hdr.blockdate());
Some(hash)
} else {
None
}
@@ -336,7 +336,7 @@ pub fn restore_chain_state(
// prevent having to read the block again below, but
// require more memory.
blocks_to_apply.push(cur);
cur = blk.get_header().get_previous_header();
cur = blk.header().previous_header();
continue;
}
Err(err) => return Err(err),
@@ -47,9 +47,9 @@ pub fn epoch_create(
let mut last_block = None;
while let Some(rblk) = packreader_block_next(&mut reader).unwrap() {
let blk = rblk.decode().unwrap();
let hdr = blk.get_header();
let hdr = blk.header();
let hash = hdr.compute_hash();
let blockdate = hdr.get_blockdate();
let blockdate = hdr.blockdate();

while current_slotid != blockdate {
rp.append_missing_hash();
@@ -14,7 +14,7 @@ pub fn iter(storage: &Storage, from: BlockHash, to: BlockHash) -> Result<Range>
let mut finished = false;

for block in ri {
let hash = block.get_header().compute_hash().into();
let hash = block.header().compute_hash().into();
rp.push_front(hash);
if hash == from {
finished = true;
@@ -43,8 +43,7 @@ impl<'a> iter::Iterator for ReverseIter<'a> {
let loc = self.storage.block_location(&hash).expect("block location");
let blk = self.storage.read_block_at(&loc).unwrap();
let block = blk.decode().unwrap();
let hdr = block.get_header();
self.current_block = Some(hdr.get_previous_header());
self.current_block = Some(block.header().previous_header());
Some(block)
}
}
@@ -304,7 +304,7 @@ enum ReverseSearch {
}

fn previous_block(storage: &Storage, block: &Block) -> Block {
let prev_hash = block.get_header().get_previous_header();
let prev_hash = block.header().previous_header();
let blk = blob::read(&storage, &header_to_blockhash(&prev_hash))
.unwrap()
.decode()
@@ -354,13 +354,13 @@ pub fn resolve_date_to_blockhash(
Ok(rblk) => {
let blk = rblk.decode()?;
let found = block_reverse_search_from_tip(storage, &blk, |x| {
match x.get_header().get_blockdate().cmp(date) {
match x.header().blockdate().cmp(date) {
Ordering::Equal => Ok(ReverseSearch::Found),
Ordering::Greater => Ok(ReverseSearch::Continue),
Ordering::Less => Ok(ReverseSearch::Abort),
}
})?;
Ok(found.map(|x| header_to_blockhash(&x.get_header().compute_hash())))
Ok(found.map(|x| header_to_blockhash(&x.header().compute_hash())))
}
},
}
@@ -443,17 +443,17 @@ pub fn refpack_epoch_pack<S: AsRef<str>>(storage: &Storage, tag: &S) -> Result<(

while let Some(raw_block) = packreader_block_next(&mut pack)? {
let block = raw_block.decode()?;
let hdr = block.get_header();
let hdr = block.header();
let hash = hdr.compute_hash();
let date = hdr.get_blockdate();
let date = hdr.blockdate();

// either we have seen genesis yet or not
match current_state {
None => {
if !hdr.is_boundary_block() {
return Err(Error::EpochExpectingBoundary);
}
current_state = Some((hdr.get_blockdate().get_epochid(), 0, hdr.compute_hash()));
current_state = Some((hdr.blockdate().get_epochid(), 0, hdr.compute_hash()));
rp.append_hash(hash.into());
}
Some((current_epoch, expected_slotid, current_prevhash)) => match date.clone() {
@@ -467,12 +467,9 @@ pub fn refpack_epoch_pack<S: AsRef<str>>(storage: &Storage, tag: &S) -> Result<(
if slotid.slotid < expected_slotid {
return Err(Error::EpochSlotRewind(current_epoch, slotid.slotid));
}
if hdr.get_previous_header() != current_prevhash {
return Err(Error::EpochChainInvalid(
date,
hdr.get_previous_header(),
current_prevhash,
));
let prevhash = hdr.previous_header();
if prevhash != current_prevhash {
return Err(Error::EpochChainInvalid(date, prevhash, current_prevhash));
}

let mut current_slotid = expected_slotid;
@@ -514,10 +511,10 @@ fn epoch_integrity_check(

while let Some(raw_block) = packreader_block_next(&mut pack)? {
let block = raw_block.decode()?;
let hdr = block.get_header();
let hdr = block.header();
let hash = hdr.compute_hash();
let prevhash = hdr.get_previous_header();
let date = hdr.get_blockdate();
let prevhash = hdr.previous_header();
let date = hdr.blockdate();

// either we have seen genesis yet or not
match current_state {
@@ -528,7 +525,7 @@ fn epoch_integrity_check(
if last_known_hash != prevhash {
return Err(Error::EpochChainInvalid(date, last_known_hash, prevhash));
}
current_state = Some((hdr.get_blockdate().get_epochid(), 0, hdr.compute_hash()));
current_state = Some((date.get_epochid(), 0, hdr.compute_hash()));
}
Some((current_epoch, expected_slotid, current_prevhash)) => match date.clone() {
cardano::block::BlockDate::Boundary(_) => {

0 comments on commit a755f5f

Please sign in to comment.