Make the block header struct's internals private (#2000)

* Make the block header struct's internals private

Currently, this involves a lot of explicit cloning, but we
could migrate the return types of the get_* functions to
be copies rather than references since they are mostly copy
types anyway.

I opted to eliminate the constructor in favor of using
Default::default() plus calling a bunch of setters. This
is similar to the model that a Google Protobuf client uses
and I think it looks fine.

* Drop some unnecessary cloning by comparing references

* Fix compiler errors from callsites in tests.
This commit is contained in:
Nipunn Koorapati
2016-08-29 02:35:24 -07:00
committed by Arkadiy Paronyan
parent 2d883c43c9
commit 4389742ca3
21 changed files with 336 additions and 336 deletions

View File

@@ -270,7 +270,7 @@ impl BlockCollection {
match self.head {
None if hash == self.heads[0] => {
trace!("New head {}", hash);
self.head = Some(info.parent_hash);
self.head = Some(info.parent_hash().clone());
},
_ => ()
}
@@ -280,8 +280,8 @@ impl BlockCollection {
body: None,
};
let header_id = HeaderId {
transactions_root: info.transactions_root,
uncles: info.uncles_hash
transactions_root: info.transactions_root().clone(),
uncles: info.uncles_hash().clone(),
};
if header_id.transactions_root == rlp::SHA3_NULL_RLP && header_id.uncles == rlp::SHA3_EMPTY_LIST_RLP {
// empty body, just mark as downloaded
@@ -294,7 +294,7 @@ impl BlockCollection {
self.header_ids.insert(header_id, hash.clone());
}
self.parents.insert(info.parent_hash.clone(), hash.clone());
self.parents.insert(info.parent_hash().clone(), hash.clone());
self.blocks.insert(hash.clone(), block);
trace!(target: "sync", "New header: {}", hash.hex());
Ok(hash)

View File

@@ -503,7 +503,7 @@ impl ChainSync {
let mut valid_response = item_count == 0; //empty response is valid
for i in 0..item_count {
let info: BlockHeader = try!(r.val_at(i));
let number = BlockNumber::from(info.number);
let number = BlockNumber::from(info.number());
// Check if any of the headers matches the hash we requested
if !valid_response {
if let Some(expected) = expected_hash {
@@ -645,11 +645,11 @@ impl ChainSync {
trace!(target: "sync", "New block already queued {:?}", h);
},
Ok(_) => {
if header.number == self.last_imported_block + 1 {
self.last_imported_block = header.number;
if header.number() == self.last_imported_block + 1 {
self.last_imported_block = header.number();
self.last_imported_hash = header.hash();
}
trace!(target: "sync", "New block queued {:?} ({})", h, header.number);
trace!(target: "sync", "New block queued {:?} ({})", h, header.number());
},
Err(BlockImportError::Block(BlockError::UnknownParent(p))) => {
unknown = true;
@@ -1539,12 +1539,12 @@ mod tests {
fn get_dummy_block(order: u32, parent_hash: H256) -> Bytes {
let mut header = Header::new();
header.gas_limit = 0.into();
header.difficulty = (order * 100).into();
header.timestamp = (order * 10) as u64;
header.number = order as u64;
header.parent_hash = parent_hash;
header.state_root = H256::zero();
header.set_gas_limit(0.into());
header.set_difficulty((order * 100).into());
header.set_timestamp((order * 10) as u64);
header.set_number(order as u64);
header.set_parent_hash(parent_hash);
header.set_state_root(H256::zero());
let mut rlp = RlpStream::new_list(3);
rlp.append(&header);