separated transaction_request to its own submodule, added basic tests for it

This commit is contained in:
debris 2016-03-11 12:31:45 +01:00
parent fd39975cf0
commit 190630cc6b
4 changed files with 106 additions and 33 deletions

View File

@ -27,6 +27,7 @@ use ethcore::block::{IsBlock};
use ethcore::views::*;
use ethcore::ethereum::Ethash;
use ethcore::ethereum::denominations::shannon;
use ethcore::transaction::Transaction as EthTransaction;
use v1::traits::{Eth, EthFilter};
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, TransactionRequest, OptionalValue, Index, Filter, Log};
use v1::helpers::{PollFilter, PollManager};
@ -274,7 +275,7 @@ impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S:
match accounts.account_secret(&transaction_request.from) {
Ok(secret) => {
let sync = take_weak!(self.sync);
let (transaction, _) = transaction_request.to_eth();
let transaction: EthTransaction = transaction_request.into();
let signed_transaction = transaction.sign(&secret);
let hash = signed_transaction.hash();
sync.insert_transaction(signed_transaction);

View File

@ -23,6 +23,7 @@ mod log;
mod optionals;
mod sync;
mod transaction;
mod transaction_request;
pub use self::block::{Block, BlockTransactions};
pub use self::block_number::BlockNumber;
@ -33,5 +34,5 @@ pub use self::log::Log;
pub use self::optionals::OptionalValue;
pub use self::sync::{SyncStatus, SyncInfo};
pub use self::transaction::Transaction;
pub use self::transaction::TransactionRequest;
pub use self::transaction_request::TransactionRequest;

View File

@ -17,8 +17,7 @@
use util::numbers::*;
use ethcore::transaction::{LocalizedTransaction, Action};
use v1::types::{Bytes, OptionalValue};
use serde::{Deserializer, Error};
use ethcore;
use serde::Error;
#[derive(Debug, Default, Serialize)]
pub struct Transaction {
@ -39,35 +38,6 @@ pub struct Transaction {
pub input: Bytes
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct TransactionRequest {
pub from: Address,
pub to: Option<Address>,
#[serde(rename="gasPrice")]
pub gas_price: Option<U256>,
pub gas: Option<U256>,
pub value: Option<U256>,
pub data: Bytes,
pub nonce: Option<U256>,
}
impl TransactionRequest {
/// maps transaction request to the transaction that can be signed and inserted
pub fn to_eth(self) -> (ethcore::transaction::Transaction, Address) {
(ethcore::transaction::Transaction {
nonce: self.nonce.unwrap_or(U256::zero()),
action: match self.to {
None => ethcore::transaction::Action::Create,
Some(addr) => ethcore::transaction::Action::Call(addr)
},
gas: self.gas.unwrap_or(U256::zero()),
gas_price: self.gas_price.unwrap_or(U256::zero()),
value: self.value.unwrap_or(U256::zero()),
data: self.data.to_vec()
}, self.from)
}
}
impl From<LocalizedTransaction> for Transaction {
fn from(t: LocalizedTransaction) -> Transaction {
Transaction {

View File

@ -0,0 +1,101 @@
// 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/>.
use util::hash::Address;
use util::numbers::{Uint, U256};
use ethcore::transaction::{Action, Transaction};
use v1::types::Bytes;
#[derive(Debug, Default, Deserialize)]
pub struct TransactionRequest {
pub from: Address,
pub to: Option<Address>,
#[serde(rename="gasPrice")]
pub gas_price: Option<U256>,
pub gas: Option<U256>,
pub value: Option<U256>,
pub data: Bytes,
pub nonce: Option<U256>,
}
impl Into<Transaction> for TransactionRequest {
fn into(self) -> Transaction {
Transaction {
nonce: self.nonce.unwrap_or(U256::zero()),
action: match self.to {
None => Action::Create,
Some(addr) => Action::Call(addr)
},
gas: self.gas.unwrap_or(U256::zero()),
gas_price: self.gas_price.unwrap_or(U256::zero()),
value: self.value.unwrap_or(U256::zero()),
data: self.data.to_vec()
}
}
}
#[cfg(test)]
mod tests {
use util::numbers::{Uint, U256};
use util::hash::Address;
use ethcore::transaction::{Transaction, Action};
use v1::types::Bytes;
use super::*;
#[test]
fn transaction_request_into_transaction() {
let tr = TransactionRequest {
from: Address::default(),
to: Some(Address::from(10)),
gas_price: Some(U256::from(20)),
gas: Some(U256::from(10_000)),
value: Some(U256::from(1)),
data: Bytes::new(vec![10, 20]),
nonce: Some(U256::from(12)),
};
assert_eq!(Transaction {
nonce: U256::from(12),
action: Action::Call(Address::from(10)),
gas: U256::from(10_000),
gas_price: U256::from(20),
value: U256::from(1),
data: vec![10, 20],
}, tr.into());
}
#[test]
fn empty_transaction_request_into_transaction() {
let tr = TransactionRequest {
from: Address::default(),
to: None,
gas_price: None,
gas: None,
value: None,
data: Bytes::new(vec![]),
nonce: None,
};
assert_eq!(Transaction {
nonce: U256::zero(),
action: Action::Create,
gas: U256::zero(),
gas_price: U256::zero(),
value: U256::zero(),
data: vec![],
}, tr.into());
}
}