From 8d37ef7d8e9c1a2f7bcb2466e46520131fe8c688 Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 4 Jan 2016 13:25:32 +0100 Subject: [PATCH] Removed Copy trait from H256 --- src/genesis.rs | 4 ++-- src/header.rs | 26 +++++++++++++------------- src/sync/chain.rs | 10 +++++----- src/sync/tests.rs | 13 +++++++------ 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/genesis.rs b/src/genesis.rs index 3e95c098a..13d228d79 100644 --- a/src/genesis.rs +++ b/src/genesis.rs @@ -1,6 +1,6 @@ use std::io::Read; use std::str::FromStr; -use std::cell::Cell; +use std::cell::RefCell; use std::collections::HashMap; use rustc_serialize::base64::FromBase64; use rustc_serialize::json::Json; @@ -83,7 +83,7 @@ impl Genesis { let nonce = H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap(); vec![mixhash.to_vec(), nonce.to_vec()] }, - hash: Cell::new(None) + hash: RefCell::new(None) }; let mut state = HashMap::new(); diff --git a/src/header.rs b/src/header.rs index b9281144f..2a581f6ce 100644 --- a/src/header.rs +++ b/src/header.rs @@ -1,4 +1,4 @@ -use std::cell::Cell; +use std::cell::RefCell; use util::hash::*; use util::sha3::*; use util::bytes::*; @@ -31,16 +31,16 @@ pub struct Header { pub difficulty: U256, pub seal: Vec, - pub hash: Cell>, //TODO: make this private + pub hash: RefCell>, //TODO: make this private } impl Header { pub fn new() -> Header { Header { - parent_hash: ZERO_H256, + parent_hash: ZERO_H256.clone(), timestamp: BAD_U256, number: ZERO_U256, - author: ZERO_ADDRESS, + author: ZERO_ADDRESS.clone(), transactions_root: SHA3_NULL_RLP, uncles_hash: SHA3_EMPTY_LIST_RLP, @@ -48,26 +48,26 @@ impl Header { state_root: SHA3_NULL_RLP, receipts_root: SHA3_NULL_RLP, - log_bloom: ZERO_LOGBLOOM, + log_bloom: ZERO_LOGBLOOM.clone(), gas_used: ZERO_U256, gas_limit: ZERO_U256, difficulty: ZERO_U256, seal: vec![], - hash: Cell::new(None), + hash: RefCell::new(None), } } pub fn hash(&self) -> H256 { - let hash = self.hash.get(); - match hash { - Some(h) => h, - None => { + let mut hash = self.hash.borrow_mut(); + match &mut *hash { + &mut Some(ref h) => h.clone(), + hash @ &mut None => { let mut stream = RlpStream::new(); stream.append(self); let h = stream.raw().sha3(); - self.hash.set(Some(h.clone())); - h + *hash = Some(h.clone()); + h.clone() } } } @@ -92,7 +92,7 @@ impl Decodable for Header { timestamp: try!(r.val_at(11)), extra_data: try!(r.val_at(12)), seal: vec![], - hash: Cell::new(Some(r.raw().sha3())) + hash: RefCell::new(Some(r.raw().sha3())) }; for i in 13..r.item_count() { diff --git a/src/sync/chain.rs b/src/sync/chain.rs index 0954e6760..5d89115e3 100644 --- a/src/sync/chain.rs +++ b/src/sync/chain.rs @@ -257,7 +257,7 @@ impl ChainSync { BlockStatus::InChain => { self.have_common_block = true; self.last_imported_block = number; - self.last_imported_hash = hash; + self.last_imported_hash = hash.clone(); trace!(target: "sync", "Found common header {} ({})", number, hash); }, _ => { @@ -283,7 +283,7 @@ impl ChainSync { } let hdr = Header { data: r.at(i).raw().to_vec(), - hash: hash, + hash: hash.clone(), parent: info.parent_hash(), }; self.headers.insert_item(number, hdr); @@ -597,17 +597,17 @@ impl ChainSync { ImportResult::AlreadyInChain => { trace!(target: "sync", "Block already in chain {:?}", h); self.last_imported_block = headers.0 + i as BlockNumber; - self.last_imported_hash = *h; + self.last_imported_hash = h.clone(); }, ImportResult::AlreadyQueued(_) => { trace!(target: "sync", "Block already queued {:?}", h); self.last_imported_block = headers.0 + i as BlockNumber; - self.last_imported_hash = *h; + self.last_imported_hash = h.clone(); }, ImportResult::Queued(QueueStatus::Known) => { trace!(target: "sync", "Block queued {:?}", h); self.last_imported_block = headers.0 + i as BlockNumber; - self.last_imported_hash = *h; + self.last_imported_hash = h.clone(); imported += 1; }, ImportResult::Queued(QueueStatus::Unknown) => { diff --git a/src/sync/tests.rs b/src/sync/tests.rs index 21ba006a6..e5c8dfe43 100644 --- a/src/sync/tests.rs +++ b/src/sync/tests.rs @@ -5,7 +5,8 @@ use util::uint::{U256}; use util::sha3::Hashable; use util::rlp::{self, Rlp, RlpStream, View, Stream}; use util::network::{PeerId, PacketId, Error as NetworkError}; -use eth::{BlockChainClient, BlockStatus, BlockNumber, TreeRoute, BlockQueueStatus, BlockChainInfo, ImportResult, BlockHeader, QueueStatus}; +use eth::{BlockChainClient, BlockStatus, BlockNumber, TreeRoute, BlockQueueStatus, BlockChainInfo, ImportResult, QueueStatus}; +use header::Header as BlockHeader; use sync::{SyncIo}; use sync::chain::{ChainSync}; @@ -28,7 +29,7 @@ impl TestBlockChainClient { difficulty: From::from(0), }; client.add_blocks(1, true); // add genesis block - client.genesis_hash = client.last_hash; + client.genesis_hash = client.last_hash.clone(); client } @@ -36,7 +37,7 @@ impl TestBlockChainClient { for n in self.numbers.len()..(self.numbers.len() + count) { let mut header = BlockHeader::new(); header.difficulty = From::from(n); - header.parent_hash = self.last_hash; + header.parent_hash = self.last_hash.clone(); header.number = From::from(n); let mut uncles = RlpStream::new_list(if empty {0} else {1}); if !empty { @@ -142,7 +143,7 @@ impl BlockChainClient for TestBlockChainClient { if number > 0 { let mut n = number - 1; while n > 0 && self.numbers[&n] != parent_hash { - *self.numbers.get_mut(&n).unwrap() = parent_hash; + *self.numbers.get_mut(&n).unwrap() = parent_hash.clone(); n -= 1; parent_hash = Rlp::new(&self.blocks[&parent_hash]).val_at::(0).parent_hash; } @@ -167,8 +168,8 @@ impl BlockChainClient for TestBlockChainClient { BlockChainInfo { total_difficulty: self.difficulty, pending_total_difficulty: self.difficulty, - genesis_hash: self.genesis_hash, - last_block_hash: self.last_hash, + genesis_hash: self.genesis_hash.clone(), + last_block_hash: self.last_hash.clone(), last_block_number: self.blocks.len() as BlockNumber - 1, } }