Transaction struct improvements.

This commit is contained in:
Gav Wood 2016-01-11 13:52:40 +01:00
parent 30f74fc692
commit eb03993c6d

View File

@ -1,37 +1,33 @@
use util::*; use util::*;
pub enum Action {
Create,
Call(Address),
}
/// A set of information describing an externally-originating message call /// A set of information describing an externally-originating message call
/// or contract creation operation. /// or contract creation operation.
pub struct Transaction { pub struct Transaction {
nonce: U256, nonce: U256,
gas_price: U256, gas_price: U256,
gas: U256, gas: U256,
to: Option<Address>, action: Action,
value: U256, value: U256,
data: Bytes, data: Bytes,
hash: RefCell<Option<H256>>, //TODO: make this private hash: RefCell<Option<H256>>, //TODO: make this private
} }
impl Transaction {
/// Is this transaction meant to create a contract?
pub fn is_contract_creation(&self) -> bool {
self.to.is_none()
}
/// Is this transaction meant to send a message?
pub fn is_message_call(&self) -> bool {
!self.is_contract_creation()
}
}
impl RlpStandard for Transaction { impl RlpStandard for Transaction {
fn rlp_append(&self, s: &mut RlpStream) { fn rlp_append(&self, s: &mut RlpStream) {
s.append_list(6); s.append_list(6);
s.append(&self.nonce); s.append(&self.nonce);
s.append(&self.gas_price); s.append(&self.gas_price);
s.append(&self.gas); s.append(&self.gas);
s.append(&self.to); match self.action {
Action::Create => s.append_empty_data(),
Action::Call(ref to) => s.append(to),
};
s.append(&self.value); s.append(&self.value);
s.append(&self.data); s.append(&self.data);
} }
@ -54,18 +50,19 @@ impl Transaction {
pub fn note_dirty(&self) { pub fn note_dirty(&self) {
*self.hash.borrow_mut() = None; *self.hash.borrow_mut() = None;
} }
/// Returns transaction type.
pub fn action(&self) -> &Action { &self.action }
} }
impl Encodable for Transaction { impl Decodable for Action {
fn encode<E>(&self, encoder: &mut E) where E: Encoder { fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
encoder.emit_list(| e | { let rlp = decoder.as_rlp();
self.nonce.encode(e); if rlp.is_empty() {
self.gas_price.encode(e); Ok(Action::Create)
self.gas.encode(e); } else {
self.to.encode(e); Ok(Action::Call(try!(rlp.as_val())))
self.value.encode(e); }
self.data.encode(e);
})
} }
} }
@ -77,7 +74,7 @@ impl Decodable for Transaction {
nonce: try!(Decodable::decode(&d[0])), nonce: try!(Decodable::decode(&d[0])),
gas_price: try!(Decodable::decode(&d[1])), gas_price: try!(Decodable::decode(&d[1])),
gas: try!(Decodable::decode(&d[2])), gas: try!(Decodable::decode(&d[2])),
to: try!(Decodable::decode(&d[3])), action: try!(Decodable::decode(&d[3])),
value: try!(Decodable::decode(&d[4])), value: try!(Decodable::decode(&d[4])),
data: try!(Decodable::decode(&d[5])), data: try!(Decodable::decode(&d[5])),
hash: RefCell::new(None) hash: RefCell::new(None)
@ -86,4 +83,3 @@ impl Decodable for Transaction {
Ok(transaction) Ok(transaction)
} }
} }