Changing RefCell to Cell in transaction. Implementing Copy on Uints.

This commit is contained in:
Tomusdrw 2016-02-18 23:01:35 +01:00 committed by Tomasz Drwięga
parent 7c3d7fc460
commit aab274d3ef
2 changed files with 29 additions and 25 deletions

View File

@ -100,10 +100,10 @@ impl FromJson for SignedTransaction {
v: match json.find("v") { Some(ref j) => u16::from_json(j) as u8, None => 0 }, 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) }, r: match json.find("r") { Some(j) => xjson!(j), None => x!(0) },
s: match json.find("s") { 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") { sender: match json.find("sender") {
Some(&Json::String(ref sender)) => RefCell::new(Some(address_from_hex(clean(sender)))), Some(&Json::String(ref sender)) => Cell::new(Some(address_from_hex(clean(sender)))),
_ => RefCell::new(None), _ => Cell::new(None),
} }
} }
} }
@ -127,8 +127,8 @@ impl Transaction {
r: r, r: r,
s: s, s: s,
v: v + 27, v: v + 27,
hash: RefCell::new(None), hash: Cell::new(None),
sender: RefCell::new(None) sender: Cell::new(None),
} }
} }
@ -140,8 +140,8 @@ impl Transaction {
r: U256::zero(), r: U256::zero(),
s: U256::zero(), s: U256::zero(),
v: 0, v: 0,
hash: RefCell::new(None), hash: Cell::new(None),
sender: RefCell::new(None) sender: Cell::new(None),
} }
} }
@ -171,9 +171,9 @@ pub struct SignedTransaction {
/// The S field of the signature; helps describe the point on the curve. /// The S field of the signature; helps describe the point on the curve.
s: U256, s: U256,
/// Cached hash. /// Cached hash.
hash: RefCell<Option<H256>>, hash: Cell<Option<H256>>,
/// Cached sender. /// Cached sender.
sender: RefCell<Option<Address>> sender: Cell<Option<Address>>,
} }
impl PartialEq for SignedTransaction { impl PartialEq for SignedTransaction {
@ -208,8 +208,8 @@ impl Decodable for SignedTransaction {
v: try!(d.val_at(6)), v: try!(d.val_at(6)),
r: try!(d.val_at(7)), r: try!(d.val_at(7)),
s: try!(d.val_at(8)), s: try!(d.val_at(8)),
hash: RefCell::new(None), hash: Cell::new(None),
sender: RefCell::new(None), sender: Cell::new(None),
}) })
} }
} }
@ -238,13 +238,14 @@ impl SignedTransaction {
/// Get the hash of this header (sha3 of the RLP). /// Get the hash of this header (sha3 of the RLP).
pub fn hash(&self) -> H256 { pub fn hash(&self) -> H256 {
let mut hash = self.hash.borrow_mut(); let hash = self.hash.get();
match &mut *hash { match hash {
&mut Some(ref h) => h.clone(), Some(h) => h,
hash @ &mut None => { None => {
*hash = Some(self.rlp_sha3()); let h = self.rlp_sha3();
hash.as_ref().unwrap().clone() self.hash.set(Some(h));
} h
}
} }
} }
@ -265,13 +266,14 @@ impl SignedTransaction {
/// Returns transaction sender. /// Returns transaction sender.
pub fn sender(&self) -> Result<Address, Error> { pub fn sender(&self) -> Result<Address, Error> {
let mut sender = self.sender.borrow_mut(); let sender = self.sender.get();
match &mut *sender { match sender {
&mut Some(ref h) => Ok(h.clone()), Some(s) => Ok(s),
sender @ &mut None => { None => {
*sender = Some(From::from(try!(ec::recover(&self.signature(), &self.unsigned.hash())).sha3())); let s = Address::from(try!(ec::recover(&self.signature(), &self.unsigned.hash())).sha3());
Ok(sender.as_ref().unwrap().clone()) self.sender.set(Some(s));
} Ok(s)
}
} }
} }

View File

@ -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 { impl Clone for $from {
fn clone(&self) -> $from { fn clone(&self) -> $from {
unsafe { unsafe {