Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #557 from ethcore/transaction_cell
Browse files Browse the repository at this point in the history
Changing RefCell to Cell in transaction.
  • Loading branch information
Gav Wood committed Mar 1, 2016
2 parents 7c3d7fc + aab274d commit 173d383
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
52 changes: 27 additions & 25 deletions ethcore/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ impl FromJson for SignedTransaction {
v: match json.find("v") { Some(ref j) => u16::from_json(j) as u8, None => 0 },
r: match json.find("r") { Some(j) => xjson!(j), None => x!(0) },
s: match json.find("s") { Some(j) => xjson!(j), None => x!(0) },
hash: RefCell::new(None),
hash: Cell::new(None),
sender: match json.find("sender") {
Some(&Json::String(ref sender)) => RefCell::new(Some(address_from_hex(clean(sender)))),
_ => RefCell::new(None),
Some(&Json::String(ref sender)) => Cell::new(Some(address_from_hex(clean(sender)))),
_ => Cell::new(None),
}
}
}
Expand All @@ -127,8 +127,8 @@ impl Transaction {
r: r,
s: s,
v: v + 27,
hash: RefCell::new(None),
sender: RefCell::new(None)
hash: Cell::new(None),
sender: Cell::new(None),
}
}

Expand All @@ -140,8 +140,8 @@ impl Transaction {
r: U256::zero(),
s: U256::zero(),
v: 0,
hash: RefCell::new(None),
sender: RefCell::new(None)
hash: Cell::new(None),
sender: Cell::new(None),
}
}

Expand Down Expand Up @@ -171,9 +171,9 @@ pub struct SignedTransaction {
/// The S field of the signature; helps describe the point on the curve.
s: U256,
/// Cached hash.
hash: RefCell<Option<H256>>,
hash: Cell<Option<H256>>,
/// Cached sender.
sender: RefCell<Option<Address>>
sender: Cell<Option<Address>>,
}

impl PartialEq for SignedTransaction {
Expand Down Expand Up @@ -208,8 +208,8 @@ impl Decodable for SignedTransaction {
v: try!(d.val_at(6)),
r: try!(d.val_at(7)),
s: try!(d.val_at(8)),
hash: RefCell::new(None),
sender: RefCell::new(None),
hash: Cell::new(None),
sender: Cell::new(None),
})
}
}
Expand Down Expand Up @@ -238,13 +238,14 @@ impl SignedTransaction {

/// Get the hash of this header (sha3 of the RLP).
pub fn hash(&self) -> H256 {
let mut hash = self.hash.borrow_mut();
match &mut *hash {
&mut Some(ref h) => h.clone(),
hash @ &mut None => {
*hash = Some(self.rlp_sha3());
hash.as_ref().unwrap().clone()
}
let hash = self.hash.get();
match hash {
Some(h) => h,
None => {
let h = self.rlp_sha3();
self.hash.set(Some(h));
h
}
}
}

Expand All @@ -265,13 +266,14 @@ impl SignedTransaction {

/// Returns transaction sender.
pub fn sender(&self) -> Result<Address, Error> {
let mut sender = self.sender.borrow_mut();
match &mut *sender {
&mut Some(ref h) => Ok(h.clone()),
sender @ &mut None => {
*sender = Some(From::from(try!(ec::recover(&self.signature(), &self.unsigned.hash())).sha3()));
Ok(sender.as_ref().unwrap().clone())
}
let sender = self.sender.get();
match sender {
Some(s) => Ok(s),
None => {
let s = Address::from(try!(ec::recover(&self.signature(), &self.unsigned.hash())).sha3());
self.sender.set(Some(s));
Ok(s)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions util/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ macro_rules! impl_hash {
}
}

impl Copy for $from {}
#[cfg_attr(feature="dev", allow(expl_impl_clone_on_copy))]
impl Clone for $from {
fn clone(&self) -> $from {
unsafe {
Expand Down

0 comments on commit 173d383

Please sign in to comment.