openethereum/src/transaction.rs

113 lines
2.9 KiB
Rust
Raw Normal View History

use util::*;
2016-01-11 20:36:29 +01:00
use basic_types::*;
2016-01-11 20:47:19 +01:00
use error::Error;
2015-12-09 00:45:33 +01:00
2016-01-11 13:52:40 +01:00
pub enum Action {
Create,
Call(Address),
2016-01-07 19:05:44 +01:00
}
/// A set of information describing an externally-originating message call
/// or contract creation operation.
2015-12-09 00:45:33 +01:00
pub struct Transaction {
2016-01-07 19:05:44 +01:00
pub nonce: U256,
pub gas_price: U256,
pub gas: U256,
2016-01-11 15:23:27 +01:00
pub action: Action,
2016-01-07 19:05:44 +01:00
pub value: U256,
2016-01-11 15:23:27 +01:00
pub data: Bytes,
2016-01-11 20:36:29 +01:00
pub signature: Signature,
2015-12-09 00:45:33 +01:00
hash: RefCell<Option<H256>>, //TODO: make this private
2015-12-09 00:45:33 +01:00
}
2016-01-07 19:05:44 +01:00
2016-01-11 20:36:29 +01:00
impl Transaction {
pub fn rlp_append_opt(&self, s: &mut RlpStream, with_seal: Seal) {
s.append_list(6 + match with_seal { Seal::With => 3, _ => 0 });
s.append(&self.nonce);
s.append(&self.gas_price);
s.append(&self.gas);
2016-01-11 13:52:40 +01:00
match self.action {
Action::Create => s.append_empty_data(),
Action::Call(ref to) => s.append(to),
};
s.append(&self.value);
s.append(&self.data);
2016-01-11 20:36:29 +01:00
match with_seal {
Seal::With => {
s.append(&(self.signature.as_slice()[64] as u16));
s.append(&&self.signature.as_slice()[0..32]);
s.append(&&self.signature.as_slice()[32..64]);
},
_ => {}
}
2016-01-07 21:29:36 +01:00
}
2016-01-11 20:36:29 +01:00
pub fn rlp_bytes_opt(&self, with_seal: Seal) -> Bytes {
let mut s = RlpStream::new();
self.rlp_append_opt(&mut s, with_seal);
s.out()
}
2016-01-11 20:47:19 +01:00
pub fn rlp_sha3_opt(&self, with_seal: Seal) -> H256 { self.rlp_bytes_opt(with_seal).sha3() }
2016-01-11 20:36:29 +01:00
}
impl RlpStandard for Transaction {
fn rlp_append(&self, s: &mut RlpStream) { self.rlp_append_opt(s, Seal::With) }
}
2016-01-07 21:29:36 +01:00
impl Transaction {
/// 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()
}
}
}
2016-01-07 19:05:44 +01:00
/// Note that some fields have changed. Resets the memoised hash.
pub fn note_dirty(&self) {
*self.hash.borrow_mut() = None;
2016-01-07 19:05:44 +01:00
}
/// Returns transaction type.
2016-01-11 13:52:40 +01:00
pub fn action(&self) -> &Action { &self.action }
2016-01-08 22:04:21 +01:00
2016-01-11 15:23:27 +01:00
/// Returns transaction sender.
2016-01-11 20:47:19 +01:00
pub fn sender(&self) -> Result<Address, Error> {
let p = try!(ec::recover(&self.signature, &self.rlp_sha3_opt(Seal::Without)));
Ok(From::from(p.sha3()))
2016-01-11 20:36:29 +01:00
}
}
2016-01-11 13:52:40 +01:00
impl Decodable for Action {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
let rlp = decoder.as_rlp();
if rlp.is_empty() {
Ok(Action::Create)
} else {
Ok(Action::Call(try!(rlp.as_val())))
}
2015-12-09 00:45:33 +01:00
}
}
impl Decodable for Transaction {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
2015-12-14 12:09:32 +01:00
let d = try!(decoder.as_list());
2016-01-11 20:36:29 +01:00
Ok(Transaction {
2015-12-14 12:09:32 +01:00
nonce: try!(Decodable::decode(&d[0])),
gas_price: try!(Decodable::decode(&d[1])),
gas: try!(Decodable::decode(&d[2])),
2016-01-11 13:52:40 +01:00
action: try!(Decodable::decode(&d[3])),
2015-12-14 12:09:32 +01:00
value: try!(Decodable::decode(&d[4])),
data: try!(Decodable::decode(&d[5])),
2016-01-11 20:36:29 +01:00
signature: Signature::from_rsv(&try!(Decodable::decode(&d[6])), &try!(Decodable::decode(&d[7])), try!(u16::decode(&d[8])) as u8),
hash: RefCell::new(None)
2016-01-11 20:36:29 +01:00
})
2015-12-09 00:45:33 +01:00
}
}