2016-01-09 12:30:41 +01:00
|
|
|
use util::*;
|
2015-12-09 00:45:33 +01:00
|
|
|
|
2015-12-20 13:16:12 +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 {
|
|
|
|
nonce: U256,
|
|
|
|
gas_price: U256,
|
|
|
|
gas: U256,
|
2015-12-20 13:16:12 +01:00
|
|
|
to: Option<Address>,
|
2015-12-09 00:45:33 +01:00
|
|
|
value: U256,
|
2015-12-13 16:44:14 +01:00
|
|
|
data: Bytes,
|
2016-01-09 23:47:15 +01:00
|
|
|
|
|
|
|
hash: RefCell<Option<H256>>, //TODO: make this private
|
2015-12-09 00:45:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-09 02:09:42 +01:00
|
|
|
impl Transaction {
|
2015-12-20 13:16:12 +01:00
|
|
|
/// Is this transaction meant to create a contract?
|
2015-12-09 02:09:42 +01:00
|
|
|
pub fn is_contract_creation(&self) -> bool {
|
2015-12-20 13:16:12 +01:00
|
|
|
self.to.is_none()
|
2015-12-09 02:09:42 +01:00
|
|
|
}
|
2016-01-08 22:04:21 +01:00
|
|
|
|
2015-12-20 13:16:12 +01:00
|
|
|
/// Is this transaction meant to send a message?
|
2015-12-09 02:09:42 +01:00
|
|
|
pub fn is_message_call(&self) -> bool {
|
|
|
|
!self.is_contract_creation()
|
|
|
|
}
|
2016-01-09 23:47:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RlpStandard for Transaction {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.append_list(6);
|
|
|
|
s.append(&self.nonce);
|
|
|
|
s.append(&self.gas_price);
|
|
|
|
s.append(&self.gas);
|
|
|
|
s.append(&self.to);
|
|
|
|
s.append(&self.value);
|
|
|
|
s.append(&self.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08 22:04:21 +01:00
|
|
|
|
2016-01-09 23:47:15 +01:00
|
|
|
/// Note that some fields have changed. Resets the memoised hash.
|
|
|
|
pub fn note_dirty(&self) {
|
|
|
|
*self.hash.borrow_mut() = None;
|
2016-01-08 22:04:21 +01:00
|
|
|
}
|
2015-12-09 02:09:42 +01:00
|
|
|
}
|
|
|
|
|
2015-12-09 00:45:33 +01:00
|
|
|
impl Encodable for Transaction {
|
|
|
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
|
|
|
encoder.emit_list(| e | {
|
|
|
|
self.nonce.encode(e);
|
|
|
|
self.gas_price.encode(e);
|
|
|
|
self.gas.encode(e);
|
2015-12-20 13:16:12 +01:00
|
|
|
self.to.encode(e);
|
2015-12-09 00:45:33 +01:00
|
|
|
self.value.encode(e);
|
|
|
|
self.data.encode(e);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for Transaction {
|
2015-12-20 13:16:12 +01:00
|
|
|
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());
|
|
|
|
|
|
|
|
let transaction = Transaction {
|
|
|
|
nonce: try!(Decodable::decode(&d[0])),
|
|
|
|
gas_price: try!(Decodable::decode(&d[1])),
|
|
|
|
gas: try!(Decodable::decode(&d[2])),
|
2015-12-20 13:16:12 +01:00
|
|
|
to: 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-09 23:47:15 +01:00
|
|
|
hash: RefCell::new(None)
|
2015-12-14 12:09:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(transaction)
|
2015-12-09 00:45:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|