2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// 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.
|
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
use std::ops::Deref;
|
2016-09-01 14:29:59 +02:00
|
|
|
use rlp::*;
|
2017-08-30 19:18:28 +02:00
|
|
|
use hash::keccak;
|
2017-08-30 16:04:47 +02:00
|
|
|
use heapsize::HeapSizeOf;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
|
|
|
use bigint::hash::H256;
|
2017-09-06 20:47:45 +02:00
|
|
|
use util::Address;
|
|
|
|
use bytes::Bytes;
|
2016-10-20 23:41:15 +02:00
|
|
|
use ethkey::{Signature, Secret, Public, 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
|
|
|
|
2017-04-19 14:30:00 +02:00
|
|
|
/// Fake address for unsigned transactions as defined by EIP-86.
|
2017-09-04 16:36:49 +02:00
|
|
|
pub const UNSIGNED_SENDER: Address = ::bigint::hash::H160([0xff; 20]);
|
2017-04-19 14:30:00 +02:00
|
|
|
|
2017-05-30 11:52:33 +02:00
|
|
|
/// System sender address for internal state updates.
|
2017-09-04 16:36:49 +02:00
|
|
|
pub const SYSTEM_ADDRESS: Address = ::bigint::hash::H160([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xfe]);
|
2017-05-30 11:52:33 +02:00
|
|
|
|
2016-02-02 23:45:50 +01:00
|
|
|
/// Transaction action type.
|
2017-07-12 13:09:17 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
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 {
|
2017-03-22 14:41:46 +01:00
|
|
|
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2016-02-23 11:40:23 +01:00
|
|
|
if rlp.is_empty() {
|
|
|
|
Ok(Action::Create)
|
|
|
|
} else {
|
2016-12-27 12:53:56 +01:00
|
|
|
Ok(Action::Call(rlp.as_val()?))
|
2016-02-23 11:40:23 +01:00
|
|
|
}
|
2016-02-04 23:48:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 06:01:46 +02:00
|
|
|
impl Encodable for Action {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
match *self {
|
|
|
|
Action::Create => s.append_internal(&""),
|
|
|
|
Action::Call(ref addr) => s.append_internal(addr),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 19:32:10 +01:00
|
|
|
/// Transaction activation condition.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Condition {
|
|
|
|
/// Valid at this block number or later.
|
|
|
|
Number(BlockNumber),
|
|
|
|
/// Valid at this unix time or later.
|
|
|
|
Timestamp(u64),
|
|
|
|
}
|
|
|
|
|
2015-12-20 13:16:12 +01:00
|
|
|
/// A set of information describing an externally-originating message call
|
|
|
|
/// or contract creation operation.
|
2016-12-21 15:09:35 +01:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
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
|
2017-08-21 13:46:58 +02:00
|
|
|
pub fn rlp_append_unsigned_transaction(&self, s: &mut RlpStream, chain_id: Option<u64>) {
|
|
|
|
s.begin_list(if chain_id.is_none() { 6 } else { 9 });
|
2016-01-09 23:47:15 +01:00
|
|
|
s.append(&self.nonce);
|
|
|
|
s.append(&self.gas_price);
|
|
|
|
s.append(&self.gas);
|
2017-08-20 06:01:46 +02:00
|
|
|
s.append(&self.action);
|
2016-01-09 23:47:15 +01:00
|
|
|
s.append(&self.value);
|
|
|
|
s.append(&self.data);
|
2017-08-21 13:46:58 +02:00
|
|
|
if let Some(n) = chain_id {
|
2016-11-03 22:22:25 +01:00
|
|
|
s.append(&n);
|
|
|
|
s.append(&0u8);
|
|
|
|
s.append(&0u8);
|
|
|
|
}
|
2016-01-11 20:36:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:50:24 +02:00
|
|
|
impl HeapSizeOf for Transaction {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.data.heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2017-05-19 17:06:36 +02:00
|
|
|
let secret = t.secret.map(|s| Secret::from_slice(&s.0));
|
2017-04-19 14:30:00 +02:00
|
|
|
let tx = Transaction {
|
2016-03-25 13:08:57 +01:00
|
|
|
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(),
|
2017-04-19 14:30:00 +02:00
|
|
|
};
|
|
|
|
match secret {
|
|
|
|
Some(s) => tx.sign(&s, None),
|
2017-04-19 19:10:12 +02:00
|
|
|
None => tx.null_sign(1),
|
2017-04-19 14:30:00 +02:00
|
|
|
}
|
2016-03-25 13:08:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
impl From<ethjson::transaction::Transaction> for UnverifiedTransaction {
|
2016-03-29 13:01:26 +02:00
|
|
|
fn from(t: ethjson::transaction::Transaction) -> Self {
|
2016-07-27 17:12:14 +02:00
|
|
|
let to: Option<ethjson::hash::Address> = t.to.into();
|
2017-01-13 09:51:36 +01:00
|
|
|
UnverifiedTransaction {
|
2016-03-29 13:01:26 +02:00
|
|
|
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(),
|
2017-01-13 09:51:36 +01:00
|
|
|
hash: 0.into(),
|
|
|
|
}.compute_hash()
|
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.
|
2017-08-21 13:46:58 +02:00
|
|
|
pub fn hash(&self, chain_id: Option<u64>) -> H256 {
|
2016-02-04 17:23:53 +01:00
|
|
|
let mut stream = RlpStream::new();
|
2017-08-21 13:46:58 +02:00
|
|
|
self.rlp_append_unsigned_transaction(&mut stream, chain_id);
|
2017-08-30 19:18:28 +02:00
|
|
|
keccak(stream.as_raw())
|
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`.
|
2017-08-21 13:46:58 +02:00
|
|
|
pub fn sign(self, secret: &Secret, chain_id: Option<u64>) -> SignedTransaction {
|
|
|
|
let sig = ::ethkey::sign(secret, &self.hash(chain_id))
|
2016-10-20 23:41:15 +02:00
|
|
|
.expect("data is valid and context has signing capabilities; qed");
|
2017-08-21 13:46:58 +02:00
|
|
|
SignedTransaction::new(self.with_signature(sig, chain_id))
|
2017-01-13 09:51:36 +01:00
|
|
|
.expect("secret is valid so it's recoverable")
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Signs the transaction with signature.
|
2017-08-21 13:46:58 +02:00
|
|
|
pub fn with_signature(self, sig: Signature, chain_id: Option<u64>) -> UnverifiedTransaction {
|
2017-01-13 09:51:36 +01:00
|
|
|
UnverifiedTransaction {
|
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(),
|
2017-08-21 13:46:58 +02:00
|
|
|
v: sig.v() as u64 + if let Some(n) = chain_id { 35 + n * 2 } else { 27 },
|
2017-01-13 09:51:36 +01:00
|
|
|
hash: 0.into(),
|
|
|
|
}.compute_hash()
|
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)]
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn invalid_sign(self) -> UnverifiedTransaction {
|
|
|
|
UnverifiedTransaction {
|
2016-02-05 13:50:35 +01:00
|
|
|
unsigned: self,
|
2017-04-19 14:30:00 +02:00
|
|
|
r: U256::one(),
|
|
|
|
s: U256::one(),
|
2016-02-04 23:48:29 +01:00
|
|
|
v: 0,
|
2017-01-13 09:51:36 +01:00
|
|
|
hash: 0.into(),
|
|
|
|
}.compute_hash()
|
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 {
|
2017-01-13 09:51:36 +01:00
|
|
|
transaction: UnverifiedTransaction {
|
|
|
|
unsigned: self,
|
2017-04-19 14:30:00 +02:00
|
|
|
r: U256::one(),
|
|
|
|
s: U256::one(),
|
2017-01-13 09:51:36 +01:00
|
|
|
v: 0,
|
|
|
|
hash: 0.into(),
|
|
|
|
}.compute_hash(),
|
|
|
|
sender: from,
|
2017-04-19 14:30:00 +02:00
|
|
|
public: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add EIP-86 compatible empty signature.
|
2017-08-21 13:46:58 +02:00
|
|
|
pub fn null_sign(self, chain_id: u64) -> SignedTransaction {
|
2017-04-19 14:30:00 +02:00
|
|
|
SignedTransaction {
|
|
|
|
transaction: UnverifiedTransaction {
|
|
|
|
unsigned: self,
|
|
|
|
r: U256::zero(),
|
|
|
|
s: U256::zero(),
|
2017-08-21 13:46:58 +02:00
|
|
|
v: chain_id,
|
2017-04-19 14:30:00 +02:00
|
|
|
hash: 0.into(),
|
|
|
|
}.compute_hash(),
|
|
|
|
sender: UNSIGNED_SENDER,
|
|
|
|
public: None,
|
2016-03-26 13:30:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-12 13:09:17 +02:00
|
|
|
/// Signed transaction information without verified signature.
|
2017-01-13 09:51:36 +01:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct UnverifiedTransaction {
|
2016-02-04 23:48:29 +01:00
|
|
|
/// Plain Transaction.
|
2016-02-05 13:50:35 +01:00
|
|
|
unsigned: Transaction,
|
2016-11-03 22:22:25 +01:00
|
|
|
/// The V field of the signature; the LS bit described which half of the curve our point falls
|
2017-08-21 13:46:58 +02:00
|
|
|
/// in. The MS bits describe which chain this transaction is for. If 27/28, its for all chains.
|
2016-12-04 19:48:26 +01:00
|
|
|
v: u64,
|
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,
|
2017-01-13 09:51:36 +01:00
|
|
|
/// Hash of the transaction
|
|
|
|
hash: H256,
|
2016-02-08 15:53:22 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
impl Deref for UnverifiedTransaction {
|
2016-02-04 17:23:53 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
impl Decodable for UnverifiedTransaction {
|
2017-03-22 14:41:46 +01:00
|
|
|
fn decode(d: &UntrustedRlp) -> Result<Self, DecoderError> {
|
|
|
|
if d.item_count()? != 9 {
|
2016-01-12 01:30:30 +01:00
|
|
|
return Err(DecoderError::RlpIncorrectListLen);
|
|
|
|
}
|
2017-08-30 19:18:28 +02:00
|
|
|
let hash = keccak(d.as_raw());
|
2017-01-13 09:51:36 +01:00
|
|
|
Ok(UnverifiedTransaction {
|
2016-02-05 13:50:35 +01:00
|
|
|
unsigned: Transaction {
|
2016-12-27 12:53:56 +01:00
|
|
|
nonce: d.val_at(0)?,
|
|
|
|
gas_price: d.val_at(1)?,
|
|
|
|
gas: d.val_at(2)?,
|
|
|
|
action: d.val_at(3)?,
|
|
|
|
value: d.val_at(4)?,
|
|
|
|
data: d.val_at(5)?,
|
2016-02-04 17:23:53 +01:00
|
|
|
},
|
2016-12-27 12:53:56 +01:00
|
|
|
v: d.val_at(6)?,
|
|
|
|
r: d.val_at(7)?,
|
|
|
|
s: d.val_at(8)?,
|
2017-01-13 09:51:36 +01:00
|
|
|
hash: hash,
|
2016-01-11 20:36:29 +01:00
|
|
|
})
|
2015-12-09 00:45:33 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 17:23:53 +01:00
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
impl Encodable for UnverifiedTransaction {
|
2016-02-04 17:23:53 +01:00
|
|
|
fn rlp_append(&self, s: &mut RlpStream) { self.rlp_append_sealed_transaction(s) }
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
impl UnverifiedTransaction {
|
|
|
|
/// Used to compute hash of created transactions
|
|
|
|
fn compute_hash(mut self) -> UnverifiedTransaction {
|
2017-08-30 19:18:28 +02:00
|
|
|
let hash = keccak(&*self.rlp_bytes());
|
2017-01-13 09:51:36 +01:00
|
|
|
self.hash = hash;
|
|
|
|
self
|
2016-09-27 16:50:24 +02:00
|
|
|
}
|
|
|
|
|
2017-04-19 14:30:00 +02:00
|
|
|
/// Checks is signature is empty.
|
|
|
|
pub fn is_unsigned(&self) -> bool {
|
|
|
|
self.r.is_zero() && self.s.is_zero()
|
|
|
|
}
|
|
|
|
|
2016-02-04 17:23:53 +01:00
|
|
|
/// Append object with a signature into RLP stream
|
2016-11-03 22:22:25 +01:00
|
|
|
fn rlp_append_sealed_transaction(&self, s: &mut RlpStream) {
|
2016-02-04 17:23:53 +01:00
|
|
|
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);
|
2017-08-20 06:01:46 +02:00
|
|
|
s.append(&self.action);
|
2016-02-04 17:23:53 +01:00
|
|
|
s.append(&self.value);
|
|
|
|
s.append(&self.data);
|
|
|
|
s.append(&self.v);
|
|
|
|
s.append(&self.r);
|
|
|
|
s.append(&self.s);
|
|
|
|
}
|
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
/// Reference to unsigned part of this transaction.
|
|
|
|
pub fn as_unsigned(&self) -> &Transaction {
|
|
|
|
&self.unsigned
|
2016-02-04 17:23:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-03 22:22:25 +01:00
|
|
|
/// 0 if `v` would have been 27 under "Electrum" notation, 1 if 28 or 4 if invalid.
|
2016-12-04 19:48:26 +01:00
|
|
|
pub fn standard_v(&self) -> u8 { match self.v { v if v == 27 || v == 28 || v > 36 => ((v - 1) % 2) as u8, _ => 4 } }
|
2016-11-03 22:22:25 +01:00
|
|
|
|
2016-11-27 14:11:37 +01:00
|
|
|
/// The `v` value that appears in the RLP.
|
2016-12-05 19:55:53 +01:00
|
|
|
pub fn original_v(&self) -> u64 { self.v }
|
2016-11-27 14:11:37 +01:00
|
|
|
|
2017-08-21 13:46:58 +02:00
|
|
|
/// The chain ID, or `None` if this is a global transaction.
|
|
|
|
pub fn chain_id(&self) -> Option<u64> {
|
2016-11-03 22:22:25 +01:00
|
|
|
match self.v {
|
2017-04-19 14:30:00 +02:00
|
|
|
v if self.is_unsigned() => Some(v),
|
2016-11-04 15:13:51 +01:00
|
|
|
v if v > 36 => Some((v - 35) / 2),
|
|
|
|
_ => None,
|
2016-11-03 22:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 17:23:53 +01:00
|
|
|
|
|
|
|
/// 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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:18:28 +02:00
|
|
|
/// Get the hash of this header (keccak of the RLP).
|
2017-01-13 09:51:36 +01:00
|
|
|
pub fn hash(&self) -> H256 {
|
|
|
|
self.hash
|
2016-02-04 17:23:53 +01:00
|
|
|
}
|
2016-02-04 23:48:29 +01:00
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
/// Recovers the public key of the sender.
|
|
|
|
pub fn recover_public(&self) -> Result<Public, Error> {
|
2017-08-21 13:46:58 +02:00
|
|
|
Ok(recover(&self.signature(), &self.unsigned.hash(self.chain_id()))?)
|
2016-09-22 14:48:22 +02: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")]
|
2017-08-21 13:46:58 +02:00
|
|
|
pub fn validate(self, schedule: &Schedule, require_low: bool, allow_chain_id_of_one: bool, allow_empty_signature: bool) -> Result<UnverifiedTransaction, Error> {
|
|
|
|
let chain_id = if allow_chain_id_of_one { Some(1) } else { None };
|
2017-04-19 14:30:00 +02:00
|
|
|
self.verify_basic(require_low, chain_id, allow_empty_signature)?;
|
|
|
|
if !allow_empty_signature || !self.is_unsigned() {
|
|
|
|
self.recover_public()?;
|
2016-01-13 01:19:05 +01:00
|
|
|
}
|
2016-01-12 17:40:34 +01:00
|
|
|
if self.gas < U256::from(self.gas_required(&schedule)) {
|
2017-09-05 15:25:06 +02:00
|
|
|
return Err(TransactionError::InvalidGasLimit(::unexpected::OutOfBounds{min: Some(U256::from(self.gas_required(&schedule))), max: None, found: self.gas}).into())
|
2016-01-12 11:44:16 +01:00
|
|
|
}
|
2017-04-19 14:30:00 +02:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify basic signature params. Does not attempt sender recovery.
|
|
|
|
pub fn verify_basic(&self, check_low_s: bool, chain_id: Option<u64>, allow_empty_signature: bool) -> Result<(), Error> {
|
|
|
|
if check_low_s && !(allow_empty_signature && self.is_unsigned()) {
|
|
|
|
self.check_low_s()?;
|
|
|
|
}
|
|
|
|
// EIP-86: Transactions of this form MUST have gasprice = 0, nonce = 0, value = 0, and do NOT increment the nonce of account 0.
|
|
|
|
if allow_empty_signature && self.is_unsigned() && !(self.gas_price.is_zero() && self.value.is_zero() && self.nonce.is_zero()) {
|
|
|
|
return Err(EthkeyError::InvalidSignature.into())
|
|
|
|
}
|
2017-08-21 13:46:58 +02:00
|
|
|
match (self.chain_id(), chain_id) {
|
2017-04-19 14:30:00 +02:00
|
|
|
(None, _) => {},
|
|
|
|
(Some(n), Some(m)) if n == m => {},
|
2017-08-21 13:46:58 +02:00
|
|
|
_ => return Err(TransactionError::InvalidChainId.into()),
|
2017-04-19 14:30:00 +02:00
|
|
|
};
|
|
|
|
Ok(())
|
2016-01-12 01:30:30 +01:00
|
|
|
}
|
2015-12-09 02:09:42 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 09:51:36 +01:00
|
|
|
/// A `UnverifiedTransaction` with successfully recovered `sender`.
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct SignedTransaction {
|
|
|
|
transaction: UnverifiedTransaction,
|
|
|
|
sender: Address,
|
2017-04-19 14:30:00 +02:00
|
|
|
public: Option<Public>,
|
2017-01-13 09:51:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HeapSizeOf for SignedTransaction {
|
|
|
|
fn heap_size_of_children(&self) -> usize {
|
|
|
|
self.transaction.unsigned.heap_size_of_children()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for SignedTransaction {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) { self.transaction.rlp_append_sealed_transaction(s) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SignedTransaction {
|
|
|
|
type Target = UnverifiedTransaction;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.transaction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SignedTransaction> for UnverifiedTransaction {
|
|
|
|
fn from(tx: SignedTransaction) -> Self {
|
|
|
|
tx.transaction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SignedTransaction {
|
|
|
|
/// Try to verify transaction and recover sender.
|
|
|
|
pub fn new(transaction: UnverifiedTransaction) -> Result<Self, Error> {
|
2017-04-19 14:30:00 +02:00
|
|
|
if transaction.is_unsigned() {
|
|
|
|
Ok(SignedTransaction {
|
|
|
|
transaction: transaction,
|
|
|
|
sender: UNSIGNED_SENDER,
|
|
|
|
public: None,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let public = transaction.recover_public()?;
|
|
|
|
let sender = public_to_address(&public);
|
|
|
|
Ok(SignedTransaction {
|
|
|
|
transaction: transaction,
|
|
|
|
sender: sender,
|
|
|
|
public: Some(public),
|
|
|
|
})
|
|
|
|
}
|
2017-01-13 09:51:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns transaction sender.
|
|
|
|
pub fn sender(&self) -> Address {
|
|
|
|
self.sender
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a public key of the sender.
|
2017-04-19 14:30:00 +02:00
|
|
|
pub fn public_key(&self) -> Option<Public> {
|
2017-01-13 09:51:36 +01:00
|
|
|
self.public
|
|
|
|
}
|
2017-04-19 14:30:00 +02:00
|
|
|
|
|
|
|
/// Checks is signature is empty.
|
|
|
|
pub fn is_unsigned(&self) -> bool {
|
|
|
|
self.transaction.is_unsigned()
|
|
|
|
}
|
2017-01-13 09:51:36 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 15:17:01 +01:00
|
|
|
/// Signed Transaction that is a part of canon blockchain.
|
2016-12-29 19:48:28 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2016-02-09 15:17:01 +01:00
|
|
|
pub struct LocalizedTransaction {
|
|
|
|
/// Signed part.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub signed: UnverifiedTransaction,
|
2016-02-09 15:17:01 +01:00
|
|
|
/// Block number.
|
|
|
|
pub block_number: BlockNumber,
|
|
|
|
/// Block hash.
|
|
|
|
pub block_hash: H256,
|
|
|
|
/// Transaction index within block.
|
2017-01-13 09:51:36 +01:00
|
|
|
pub transaction_index: usize,
|
|
|
|
/// Cached sender
|
|
|
|
pub cached_sender: Option<Address>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalizedTransaction {
|
|
|
|
/// Returns transaction sender.
|
|
|
|
/// Panics if `LocalizedTransaction` is constructed using invalid `UnverifiedTransaction`.
|
|
|
|
pub fn sender(&mut self) -> Address {
|
|
|
|
if let Some(sender) = self.cached_sender {
|
|
|
|
return sender;
|
|
|
|
}
|
2017-04-19 14:30:00 +02:00
|
|
|
if self.is_unsigned() {
|
|
|
|
return UNSIGNED_SENDER.clone();
|
|
|
|
}
|
2017-01-13 09:51:36 +01:00
|
|
|
let sender = public_to_address(&self.recover_public()
|
|
|
|
.expect("LocalizedTransaction is always constructed from transaction from blockchain; Blockchain only stores verified transactions; qed"));
|
|
|
|
self.cached_sender = Some(sender);
|
|
|
|
sender
|
|
|
|
}
|
2016-02-09 15:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for LocalizedTransaction {
|
2017-01-13 09:51:36 +01:00
|
|
|
type Target = UnverifiedTransaction;
|
2016-02-09 15:17:01 +01:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.signed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 20:09:05 +01:00
|
|
|
/// Queued transaction with additional information.
|
2016-12-21 15:09:35 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2016-12-15 18:19:19 +01:00
|
|
|
pub struct PendingTransaction {
|
|
|
|
/// Signed transaction data.
|
|
|
|
pub transaction: SignedTransaction,
|
2017-02-03 19:32:10 +01:00
|
|
|
/// To be activated at this condition. `None` for immediately.
|
|
|
|
pub condition: Option<Condition>,
|
2016-12-15 18:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PendingTransaction {
|
|
|
|
/// Create a new pending transaction from signed transaction.
|
2017-02-03 19:32:10 +01:00
|
|
|
pub fn new(signed: SignedTransaction, condition: Option<Condition>) -> Self {
|
2016-12-15 18:19:19 +01:00
|
|
|
PendingTransaction {
|
|
|
|
transaction: signed,
|
2017-02-03 19:32:10 +01:00
|
|
|
condition: condition,
|
2016-12-15 18:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:36:53 +01:00
|
|
|
impl Deref for PendingTransaction {
|
|
|
|
type Target = SignedTransaction;
|
|
|
|
|
|
|
|
fn deref(&self) -> &SignedTransaction { &self.transaction }
|
|
|
|
}
|
|
|
|
|
2016-12-15 18:19:19 +01:00
|
|
|
impl From<SignedTransaction> for PendingTransaction {
|
|
|
|
fn from(t: SignedTransaction) -> Self {
|
|
|
|
PendingTransaction {
|
|
|
|
transaction: t,
|
2017-02-03 19:32:10 +01:00
|
|
|
condition: None,
|
2016-12-15 18:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 13:09:17 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
2017-08-30 19:18:28 +02:00
|
|
|
use hash::keccak;
|
2017-07-12 13:09:17 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sender_test() {
|
|
|
|
let t: UnverifiedTransaction = decode(&::rustc_hex::FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap());
|
|
|
|
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 {
|
|
|
|
assert_eq!(*to, "095e7baea6a6c7c4c2dfeb977efac326af552d87".into());
|
|
|
|
} else { panic!(); }
|
|
|
|
assert_eq!(t.value, U256::from(0x0au64));
|
|
|
|
assert_eq!(public_to_address(&t.recover_public().unwrap()), "0f65fe9276bc9a24ae7083ae28e2660ef72df99e".into());
|
2017-08-21 13:46:58 +02:00
|
|
|
assert_eq!(t.chain_id(), None);
|
2017-07-12 13:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn signing() {
|
|
|
|
use ethkey::{Random, Generator};
|
|
|
|
|
|
|
|
let key = Random.generate().unwrap();
|
|
|
|
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(), None);
|
2017-08-30 19:18:28 +02:00
|
|
|
assert_eq!(Address::from(keccak(key.public())), t.sender());
|
2017-08-21 13:46:58 +02:00
|
|
|
assert_eq!(t.chain_id(), None);
|
2017-07-12 13:09:17 +02: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());
|
2017-08-21 13:46:58 +02:00
|
|
|
assert_eq!(t.chain_id(), None);
|
2017-07-12 13:09:17 +02:00
|
|
|
|
|
|
|
let t = t.clone();
|
|
|
|
assert_eq!(Address::from(0x69), t.sender());
|
2017-08-21 13:46:58 +02:00
|
|
|
assert_eq!(t.chain_id(), None);
|
2017-07-12 13:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-08-21 13:46:58 +02:00
|
|
|
fn should_recover_from_chain_specific_signing() {
|
2017-07-12 13:09:17 +02:00
|
|
|
use ethkey::{Random, Generator};
|
|
|
|
let key = Random.generate().unwrap();
|
|
|
|
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(), Some(69));
|
2017-08-30 19:18:28 +02:00
|
|
|
assert_eq!(Address::from(keccak(key.public())), t.sender());
|
2017-08-21 13:46:58 +02:00
|
|
|
assert_eq!(t.chain_id(), Some(69));
|
2017-07-12 13:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_agree_with_vitalik() {
|
|
|
|
use rustc_hex::FromHex;
|
|
|
|
|
|
|
|
let test_vector = |tx_data: &str, address: &'static str| {
|
|
|
|
let signed = decode(&FromHex::from_hex(tx_data).unwrap());
|
|
|
|
let signed = SignedTransaction::new(signed).unwrap();
|
|
|
|
assert_eq!(signed.sender(), address.into());
|
2017-08-21 13:46:58 +02:00
|
|
|
flushln!("chainid: {:?}", signed.chain_id());
|
2017-07-12 13:09:17 +02:00
|
|
|
};
|
2016-11-03 22:22:25 +01:00
|
|
|
|
2017-07-12 13:09:17 +02:00
|
|
|
test_vector("f864808504a817c800825208943535353535353535353535353535353535353535808025a0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116da0044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d", "0xf0f6f18bca1b28cd68e4357452947e021241e9ce");
|
|
|
|
test_vector("f864018504a817c80182a410943535353535353535353535353535353535353535018025a0489efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bcaa0489efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6", "0x23ef145a395ea3fa3deb533b8a9e1b4c6c25d112");
|
|
|
|
test_vector("f864028504a817c80282f618943535353535353535353535353535353535353535088025a02d7c5bef027816a800da1736444fb58a807ef4c9603b7848673f7e3a68eb14a5a02d7c5bef027816a800da1736444fb58a807ef4c9603b7848673f7e3a68eb14a5", "0x2e485e0c23b4c3c542628a5f672eeab0ad4888be");
|
|
|
|
test_vector("f865038504a817c803830148209435353535353535353535353535353535353535351b8025a02a80e1ef1d7842f27f2e6be0972bb708b9a135c38860dbe73c27c3486c34f4e0a02a80e1ef1d7842f27f2e6be0972bb708b9a135c38860dbe73c27c3486c34f4de", "0x82a88539669a3fd524d669e858935de5e5410cf0");
|
|
|
|
test_vector("f865048504a817c80483019a28943535353535353535353535353535353535353535408025a013600b294191fc92924bb3ce4b969c1e7e2bab8f4c93c3fc6d0a51733df3c063a013600b294191fc92924bb3ce4b969c1e7e2bab8f4c93c3fc6d0a51733df3c060", "0xf9358f2538fd5ccfeb848b64a96b743fcc930554");
|
|
|
|
test_vector("f865058504a817c8058301ec309435353535353535353535353535353535353535357d8025a04eebf77a833b30520287ddd9478ff51abbdffa30aa90a8d655dba0e8a79ce0c1a04eebf77a833b30520287ddd9478ff51abbdffa30aa90a8d655dba0e8a79ce0c1", "0xa8f7aba377317440bc5b26198a363ad22af1f3a4");
|
|
|
|
test_vector("f866068504a817c80683023e3894353535353535353535353535353535353535353581d88025a06455bf8ea6e7463a1046a0b52804526e119b4bf5136279614e0b1e8e296a4e2fa06455bf8ea6e7463a1046a0b52804526e119b4bf5136279614e0b1e8e296a4e2d", "0xf1f571dc362a0e5b2696b8e775f8491d3e50de35");
|
|
|
|
test_vector("f867078504a817c807830290409435353535353535353535353535353535353535358201578025a052f1a9b320cab38e5da8a8f97989383aab0a49165fc91c737310e4f7e9821021a052f1a9b320cab38e5da8a8f97989383aab0a49165fc91c737310e4f7e9821021", "0xd37922162ab7cea97c97a87551ed02c9a38b7332");
|
|
|
|
test_vector("f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10", "0x9bddad43f934d313c2b79ca28a432dd2b7281029");
|
|
|
|
test_vector("f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb", "0x3c24d7329e92f84f08556ceb6df1cdb0104ca49f");
|
|
|
|
}
|
2016-11-28 13:20:49 +01:00
|
|
|
}
|