2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-02 15:29:53 +01:00
|
|
|
//! Transaction data structure.
|
|
|
|
|
2016-05-16 18:33:32 +02:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::cell::*;
|
2016-09-01 14:29:59 +02:00
|
|
|
use rlp::*;
|
2016-08-24 18:35:21 +02:00
|
|
|
use util::sha3::Hashable;
|
|
|
|
use util::{H256, Address, U256, Bytes};
|
|
|
|
use ethkey::{Signature, sign, Secret, recover, public_to_address, Error as EthkeyError};
|
2016-01-12 11:44:16 +01:00
|
|
|
use error::*;
|
2016-01-12 01:30:30 +01:00
|
|
|
use evm::Schedule;
|
2016-02-09 15:17:01 +01:00
|
|
|
use header::BlockNumber;
|
2016-03-25 13:08:57 +01:00
|
|
|
use ethjson;
|
2015-12-09 00:45:33 +01:00
|
|
|
|
2016-05-16 18:33:32 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Binary)]
|
2016-02-02 23:45:50 +01:00
|
|
|
/// Transaction action type.
|
2016-01-11 13:52:40 +01:00
|
|
|
pub enum Action {
|
2016-02-02 23:45:50 +01:00
|
|
|
/// Create creates new contract.
|
2016-01-11 13:52:40 +01:00
|
|
|
Create,
|
2016-02-02 23:45:50 +01:00
|
|
|
/// Calls contract at given address.
|
2016-05-25 13:23:24 +02:00
|
|
|
/// In the case of a transfer, this is the receiver's address.'
|
2016-01-11 13:52:40 +01:00
|
|
|
Call(Address),
|
2016-01-07 19:05:44 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 19:18:22 +01:00
|
|
|
impl Default for Action {
|
|
|
|
fn default() -> Action { Action::Create }
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:48:29 +01:00
|
|
|
impl Decodable for Action {
|
|
|
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
|
|
|
let rlp = decoder.as_rlp();
|
2016-02-23 11:40:23 +01:00
|
|
|
if rlp.is_empty() {
|
|
|
|
Ok(Action::Create)
|
|
|
|
} else {
|
|
|
|
Ok(Action::Call(try!(rlp.as_val())))
|
|
|
|
}
|
2016-02-04 23:48:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-20 13:16:12 +01:00
|
|
|
/// A set of information describing an externally-originating message call
|
|
|
|
/// or contract creation operation.
|
2016-05-16 18:33:32 +02:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq, Binary)]
|
2015-12-09 00:45:33 +01:00
|
|
|
pub struct Transaction {
|
2016-02-02 15:55:44 +01:00
|
|
|
/// Nonce.
|
2016-01-07 19:05:44 +01:00
|
|
|
pub nonce: U256,
|
2016-02-02 15:55:44 +01:00
|
|
|
/// Gas price.
|
2016-01-07 19:05:44 +01:00
|
|
|
pub gas_price: U256,
|
2016-02-02 15:55:44 +01:00
|
|
|
/// Gas paid up front for transaction execution.
|
2016-01-07 19:05:44 +01:00
|
|
|
pub gas: U256,
|
2016-02-02 15:55:44 +01:00
|
|
|
/// Action, can be either call or contract create.
|
2016-01-11 15:23:27 +01:00
|
|
|
pub action: Action,
|
2016-02-02 15:55:44 +01:00
|
|
|
/// Transfered value.
|
2016-01-07 19:05:44 +01:00
|
|
|
pub value: U256,
|
2016-02-02 15:55:44 +01:00
|
|
|
/// Transaction data.
|
2016-01-11 15:23:27 +01:00
|
|
|
pub data: Bytes,
|
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 {
|
2016-02-04 17:23:53 +01:00
|
|
|
/// Append object with a without signature into RLP stream
|
|
|
|
pub fn rlp_append_unsigned_transaction(&self, s: &mut RlpStream) {
|
|
|
|
s.begin_list(6);
|
2016-01-09 23:47:15 +01:00
|
|
|
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(),
|
2016-02-04 17:23:53 +01:00
|
|
|
Action::Call(ref to) => s.append(to)
|
2016-01-11 13:52:40 +01:00
|
|
|
};
|
2016-01-09 23:47:15 +01:00
|
|
|
s.append(&self.value);
|
|
|
|
s.append(&self.data);
|
2016-01-11 20:36:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 13:08:57 +01:00
|
|
|
impl From<ethjson::state::Transaction> for SignedTransaction {
|
|
|
|
fn from(t: ethjson::state::Transaction) -> Self {
|
2016-07-27 17:12:14 +02:00
|
|
|
let to: Option<ethjson::hash::Address> = t.to.into();
|
2016-03-25 13:08:57 +01:00
|
|
|
Transaction {
|
|
|
|
nonce: t.nonce.into(),
|
|
|
|
gas_price: t.gas_price.into(),
|
|
|
|
gas: t.gas_limit.into(),
|
|
|
|
action: match to {
|
|
|
|
Some(to) => Action::Call(to.into()),
|
|
|
|
None => Action::Create
|
|
|
|
},
|
|
|
|
value: t.value.into(),
|
|
|
|
data: t.data.into(),
|
|
|
|
}.sign(&t.secret.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 13:01:26 +02:00
|
|
|
impl From<ethjson::transaction::Transaction> for SignedTransaction {
|
|
|
|
fn from(t: ethjson::transaction::Transaction) -> Self {
|
2016-07-27 17:12:14 +02:00
|
|
|
let to: Option<ethjson::hash::Address> = t.to.into();
|
2016-03-29 13:01:26 +02:00
|
|
|
SignedTransaction {
|
|
|
|
unsigned: Transaction {
|
|
|
|
nonce: t.nonce.into(),
|
|
|
|
gas_price: t.gas_price.into(),
|
|
|
|
gas: t.gas_limit.into(),
|
|
|
|
action: match to {
|
|
|
|
Some(to) => Action::Call(to.into()),
|
|
|
|
None => Action::Create
|
|
|
|
},
|
|
|
|
value: t.value.into(),
|
|
|
|
data: t.data.into(),
|
2016-01-14 21:58:37 +01:00
|
|
|
},
|
2016-03-29 13:01:26 +02:00
|
|
|
r: t.r.into(),
|
|
|
|
s: t.s.into(),
|
|
|
|
v: t.v.into(),
|
|
|
|
sender: Cell::new(None),
|
|
|
|
hash: Cell::new(None)
|
2016-01-14 21:58:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 23:47:15 +01:00
|
|
|
impl Transaction {
|
2016-01-11 21:57:22 +01:00
|
|
|
/// The message hash of the transaction.
|
2016-02-23 11:40:23 +01:00
|
|
|
pub fn hash(&self) -> H256 {
|
2016-02-04 17:23:53 +01:00
|
|
|
let mut stream = RlpStream::new();
|
|
|
|
self.rlp_append_unsigned_transaction(&mut stream);
|
|
|
|
stream.out().sha3()
|
2016-01-12 18:10:10 +01:00
|
|
|
}
|
2016-01-12 01:30:30 +01:00
|
|
|
|
2016-01-12 17:40:34 +01:00
|
|
|
/// Signs the transaction as coming from `sender`.
|
2016-02-04 17:23:53 +01:00
|
|
|
pub fn sign(self, secret: &Secret) -> SignedTransaction {
|
2016-08-24 18:35:21 +02:00
|
|
|
let sig = sign(secret, &self.hash()).unwrap();
|
|
|
|
self.with_signature(sig)
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Signs the transaction with signature.
|
2016-08-24 18:35:21 +02:00
|
|
|
pub fn with_signature(self, sig: Signature) -> SignedTransaction {
|
2016-02-04 17:23:53 +01:00
|
|
|
SignedTransaction {
|
2016-02-05 13:50:35 +01:00
|
|
|
unsigned: self,
|
2016-08-24 18:35:21 +02:00
|
|
|
r: sig.r().into(),
|
|
|
|
s: sig.s().into(),
|
|
|
|
v: sig.v() + 27,
|
2016-02-18 23:01:35 +01:00
|
|
|
hash: Cell::new(None),
|
|
|
|
sender: Cell::new(None),
|
2016-02-04 17:23:53 +01:00
|
|
|
}
|
2016-01-12 17:40:34 +01:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:48:29 +01:00
|
|
|
/// Useful for test incorrectly signed transactions.
|
2016-02-02 23:45:50 +01:00
|
|
|
#[cfg(test)]
|
2016-03-26 13:30:02 +01:00
|
|
|
pub fn invalid_sign(self) -> SignedTransaction {
|
2016-02-04 23:48:29 +01:00
|
|
|
SignedTransaction {
|
2016-02-05 13:50:35 +01:00
|
|
|
unsigned: self,
|
2016-08-04 08:52:31 +02:00
|
|
|
r: U256::default(),
|
|
|
|
s: U256::default(),
|
2016-02-04 23:48:29 +01:00
|
|
|
v: 0,
|
2016-02-18 23:01:35 +01:00
|
|
|
hash: Cell::new(None),
|
|
|
|
sender: Cell::new(None),
|
2016-01-12 11:44:16 +01:00
|
|
|
}
|
2016-01-12 01:30:30 +01:00
|
|
|
}
|
2016-01-12 18:10:10 +01:00
|
|
|
|
2016-03-26 13:30:02 +01:00
|
|
|
/// Specify the sender; this won't survive the serialize/deserialize process, but can be cloned.
|
|
|
|
pub fn fake_sign(self, from: Address) -> SignedTransaction {
|
|
|
|
SignedTransaction {
|
|
|
|
unsigned: self,
|
2016-08-04 08:52:31 +02:00
|
|
|
r: U256::default(),
|
|
|
|
s: U256::default(),
|
2016-03-26 13:30:02 +01:00
|
|
|
v: 0,
|
|
|
|
hash: Cell::new(None),
|
|
|
|
sender: Cell::new(Some(from)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 01:30:30 +01:00
|
|
|
/// Get the transaction cost in gas for the given params.
|
2016-01-12 17:40:34 +01:00
|
|
|
pub fn gas_required_for(is_create: bool, data: &[u8], schedule: &Schedule) -> u64 {
|
2016-01-12 01:30:30 +01:00
|
|
|
data.iter().fold(
|
2016-01-12 17:40:34 +01:00
|
|
|
(if is_create {schedule.tx_create_gas} else {schedule.tx_gas}) as u64,
|
|
|
|
|g, b| g + (match *b { 0 => schedule.tx_data_zero_gas, _ => schedule.tx_data_non_zero_gas }) as u64
|
2016-01-12 01:30:30 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-01-12 11:44:16 +01:00
|
|
|
/// Get the transaction cost in gas for this transaction.
|
2016-01-12 17:40:34 +01:00
|
|
|
pub fn gas_required(&self, schedule: &Schedule) -> u64 {
|
2016-01-12 11:44:16 +01:00
|
|
|
Self::gas_required_for(match self.action{Action::Create=>true, Action::Call(_)=>false}, &self.data, schedule)
|
|
|
|
}
|
2015-12-09 00:45:33 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 13:17:44 +01:00
|
|
|
/// Signed transaction information.
|
2016-05-16 18:33:32 +02:00
|
|
|
#[derive(Debug, Clone, Eq, Binary)]
|
2016-02-04 17:23:53 +01:00
|
|
|
pub struct SignedTransaction {
|
2016-02-04 23:48:29 +01:00
|
|
|
/// Plain Transaction.
|
2016-02-05 13:50:35 +01:00
|
|
|
unsigned: Transaction,
|
2016-02-04 23:48:29 +01:00
|
|
|
/// The V field of the signature, either 27 or 28; helps describe the point on the curve.
|
2016-02-04 17:23:53 +01:00
|
|
|
v: u8,
|
2016-02-04 23:48:29 +01:00
|
|
|
/// The R field of the signature; helps describe the point on the curve.
|
2016-02-04 17:23:53 +01:00
|
|
|
r: U256,
|
2016-02-04 23:48:29 +01:00
|
|
|
/// The S field of the signature; helps describe the point on the curve.
|
2016-02-04 17:23:53 +01:00
|
|
|
s: U256,
|
2016-02-04 23:48:29 +01:00
|
|
|
/// Cached hash.
|
2016-02-18 23:01:35 +01:00
|
|
|
hash: Cell<Option<H256>>,
|
2016-02-04 23:48:29 +01:00
|
|
|
/// Cached sender.
|
2016-02-18 23:01:35 +01:00
|
|
|
sender: Cell<Option<Address>>,
|
2016-02-04 17:23:53 +01:00
|
|
|
}
|
|
|
|
|
2016-02-08 15:53:22 +01:00
|
|
|
impl PartialEq for SignedTransaction {
|
|
|
|
fn eq(&self, other: &SignedTransaction) -> bool {
|
|
|
|
self.unsigned == other.unsigned && self.v == other.v && self.r == other.r && self.s == other.s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 17:23:53 +01:00
|
|
|
impl Deref for SignedTransaction {
|
|
|
|
type Target = Transaction;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2016-02-05 13:50:35 +01:00
|
|
|
&self.unsigned
|
2016-02-04 17:23:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for SignedTransaction {
|
2015-12-20 13:16:12 +01:00
|
|
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
2016-01-29 13:59:29 +01:00
|
|
|
let d = decoder.as_rlp();
|
|
|
|
if d.item_count() != 9 {
|
2016-01-12 01:30:30 +01:00
|
|
|
return Err(DecoderError::RlpIncorrectListLen);
|
|
|
|
}
|
2016-02-04 17:23:53 +01:00
|
|
|
Ok(SignedTransaction {
|
2016-02-05 13:50:35 +01:00
|
|
|
unsigned: Transaction {
|
2016-02-04 17:23:53 +01:00
|
|
|
nonce: try!(d.val_at(0)),
|
|
|
|
gas_price: try!(d.val_at(1)),
|
|
|
|
gas: try!(d.val_at(2)),
|
|
|
|
action: try!(d.val_at(3)),
|
|
|
|
value: try!(d.val_at(4)),
|
|
|
|
data: try!(d.val_at(5)),
|
|
|
|
},
|
2016-01-29 13:59:29 +01:00
|
|
|
v: try!(d.val_at(6)),
|
|
|
|
r: try!(d.val_at(7)),
|
|
|
|
s: try!(d.val_at(8)),
|
2016-02-18 23:01:35 +01:00
|
|
|
hash: Cell::new(None),
|
|
|
|
sender: Cell::new(None),
|
2016-01-11 20:36:29 +01:00
|
|
|
})
|
2015-12-09 00:45:33 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 17:23:53 +01:00
|
|
|
|
|
|
|
impl Encodable for SignedTransaction {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) { self.rlp_append_sealed_transaction(s) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SignedTransaction {
|
|
|
|
/// Append object with a signature into RLP stream
|
|
|
|
pub fn rlp_append_sealed_transaction(&self, s: &mut RlpStream) {
|
|
|
|
s.begin_list(9);
|
|
|
|
s.append(&self.nonce);
|
2016-02-04 23:48:29 +01:00
|
|
|
s.append(&self.gas_price);
|
2016-02-04 17:23:53 +01:00
|
|
|
s.append(&self.gas);
|
|
|
|
match self.action {
|
|
|
|
Action::Create => s.append_empty_data(),
|
|
|
|
Action::Call(ref to) => s.append(to)
|
|
|
|
};
|
|
|
|
s.append(&self.value);
|
|
|
|
s.append(&self.data);
|
|
|
|
s.append(&self.v);
|
|
|
|
s.append(&self.r);
|
|
|
|
s.append(&self.s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the hash of this header (sha3 of the RLP).
|
|
|
|
pub fn hash(&self) -> H256 {
|
2016-02-18 23:01:35 +01:00
|
|
|
let hash = self.hash.get();
|
|
|
|
match hash {
|
|
|
|
Some(h) => h,
|
|
|
|
None => {
|
2016-09-01 14:29:59 +02:00
|
|
|
let h = (&*self.rlp_bytes()).sha3();
|
2016-02-18 23:01:35 +01:00
|
|
|
self.hash.set(Some(h));
|
|
|
|
h
|
|
|
|
}
|
2016-02-04 17:23:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 0 is `v` is 27, 1 if 28, and 4 otherwise.
|
|
|
|
pub fn standard_v(&self) -> u8 { match self.v { 27 => 0, 28 => 1, _ => 4 } }
|
|
|
|
|
|
|
|
/// Construct a signature object from the sig.
|
2016-08-24 18:35:21 +02:00
|
|
|
pub fn signature(&self) -> Signature {
|
|
|
|
Signature::from_rsv(&self.r.into(), &self.s.into(), self.standard_v())
|
|
|
|
}
|
2016-01-12 11:44:16 +01:00
|
|
|
|
2016-01-16 18:30:27 +01:00
|
|
|
/// Checks whether the signature has a low 's' value.
|
|
|
|
pub fn check_low_s(&self) -> Result<(), Error> {
|
2016-08-24 18:35:21 +02:00
|
|
|
if !self.signature().is_low_s() {
|
|
|
|
Err(EthkeyError::InvalidSignature.into())
|
2016-01-16 18:30:27 +01:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 17:23:53 +01:00
|
|
|
/// Returns transaction sender.
|
|
|
|
pub fn sender(&self) -> Result<Address, Error> {
|
2016-02-18 23:01:35 +01:00
|
|
|
let sender = self.sender.get();
|
|
|
|
match sender {
|
|
|
|
Some(s) => Ok(s),
|
|
|
|
None => {
|
2016-08-24 18:35:21 +02:00
|
|
|
let s = public_to_address(&try!(recover(&self.signature(), &self.unsigned.hash())));
|
2016-02-18 23:01:35 +01:00
|
|
|
self.sender.set(Some(s));
|
|
|
|
Ok(s)
|
|
|
|
}
|
2016-02-04 17:23:53 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 23:48:29 +01:00
|
|
|
|
2016-01-12 11:44:16 +01:00
|
|
|
/// Do basic validation, checking for valid signature and minimum gas,
|
2016-02-03 15:33:58 +01:00
|
|
|
// TODO: consider use in block validation.
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(feature = "json-tests")]
|
2016-02-04 23:48:29 +01:00
|
|
|
pub fn validate(self, schedule: &Schedule, require_low: bool) -> Result<SignedTransaction, Error> {
|
2016-08-24 18:35:21 +02:00
|
|
|
if require_low && !self.signature().is_low_s() {
|
|
|
|
return Err(EthkeyError::InvalidSignature.into())
|
2016-01-13 01:19:05 +01:00
|
|
|
}
|
2016-01-12 11:44:16 +01:00
|
|
|
try!(self.sender());
|
2016-01-12 17:40:34 +01:00
|
|
|
if self.gas < U256::from(self.gas_required(&schedule)) {
|
2016-05-16 18:33:32 +02:00
|
|
|
Err(From::from(TransactionError::InvalidGasLimit(::util::OutOfBounds{min: Some(U256::from(self.gas_required(&schedule))), max: None, found: self.gas})))
|
2016-01-12 11:44:16 +01:00
|
|
|
} else {
|
|
|
|
Ok(self)
|
|
|
|
}
|
2016-01-12 01:30:30 +01:00
|
|
|
}
|
2015-12-09 02:09:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 15:17:01 +01:00
|
|
|
/// Signed Transaction that is a part of canon blockchain.
|
2016-05-16 18:33:32 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq, Binary)]
|
2016-02-09 15:17:01 +01:00
|
|
|
pub struct LocalizedTransaction {
|
|
|
|
/// Signed part.
|
|
|
|
pub signed: SignedTransaction,
|
|
|
|
/// Block number.
|
|
|
|
pub block_number: BlockNumber,
|
|
|
|
/// Block hash.
|
|
|
|
pub block_hash: H256,
|
|
|
|
/// Transaction index within block.
|
|
|
|
pub transaction_index: usize
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for LocalizedTransaction {
|
|
|
|
type Target = SignedTransaction;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.signed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 16:20:29 +01:00
|
|
|
#[test]
|
|
|
|
fn sender_test() {
|
2016-05-16 18:33:32 +02:00
|
|
|
let t: SignedTransaction = decode(&::rustc_serialize::hex::FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap());
|
2016-01-12 16:20:29 +01:00
|
|
|
assert_eq!(t.data, b"");
|
|
|
|
assert_eq!(t.gas, U256::from(0x5208u64));
|
|
|
|
assert_eq!(t.gas_price, U256::from(0x01u64));
|
|
|
|
assert_eq!(t.nonce, U256::from(0x00u64));
|
|
|
|
if let Action::Call(ref to) = t.action {
|
2016-08-08 11:18:48 +02:00
|
|
|
assert_eq!(*to, "095e7baea6a6c7c4c2dfeb977efac326af552d87".into());
|
2016-01-12 16:20:29 +01:00
|
|
|
} else { panic!(); }
|
|
|
|
assert_eq!(t.value, U256::from(0x0au64));
|
2016-08-08 11:18:48 +02:00
|
|
|
assert_eq!(t.sender().unwrap(), "0f65fe9276bc9a24ae7083ae28e2660ef72df99e".into());
|
2016-01-12 01:30:30 +01:00
|
|
|
}
|
2016-01-12 18:10:10 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn signing() {
|
2016-08-24 18:35:21 +02:00
|
|
|
use ethkey::{Random, Generator};
|
|
|
|
|
|
|
|
let key = Random.generate().unwrap();
|
2016-02-04 23:48:29 +01:00
|
|
|
let t = Transaction {
|
|
|
|
action: Action::Create,
|
|
|
|
nonce: U256::from(42),
|
|
|
|
gas_price: U256::from(3000),
|
|
|
|
gas: U256::from(50_000),
|
|
|
|
value: U256::from(1),
|
|
|
|
data: b"Hello!".to_vec()
|
|
|
|
}.sign(&key.secret());
|
2016-01-12 18:10:10 +01:00
|
|
|
assert_eq!(Address::from(key.public().sha3()), t.sender().unwrap());
|
2016-01-17 15:56:09 +01:00
|
|
|
}
|
2016-03-26 13:30:02 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fake_signing() {
|
|
|
|
let t = Transaction {
|
|
|
|
action: Action::Create,
|
|
|
|
nonce: U256::from(42),
|
|
|
|
gas_price: U256::from(3000),
|
|
|
|
gas: U256::from(50_000),
|
|
|
|
value: U256::from(1),
|
|
|
|
data: b"Hello!".to_vec()
|
|
|
|
}.fake_sign(Address::from(0x69));
|
|
|
|
assert_eq!(Address::from(0x69), t.sender().unwrap());
|
|
|
|
|
|
|
|
let t = t.clone();
|
|
|
|
assert_eq!(Address::from(0x69), t.sender().unwrap());
|
|
|
|
}
|