2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2016-06-28 12:08:30 +02: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/>.
|
|
|
|
|
|
|
|
//! View onto transaction rlp
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::Bytes;
|
2018-02-23 20:01:33 +01:00
|
|
|
use ethereum_types::{H256, U256};
|
2017-08-30 19:18:28 +02:00
|
|
|
use hash::keccak;
|
2018-04-16 15:52:12 +02:00
|
|
|
// use rlp::{Rlp, Decodable};
|
|
|
|
use super::ViewRlp;
|
2016-06-28 12:08:30 +02:00
|
|
|
|
|
|
|
/// View onto transaction rlp.
|
|
|
|
pub struct TransactionView<'a> {
|
2018-04-16 15:52:12 +02:00
|
|
|
rlp: ViewRlp<'a>
|
2016-06-28 12:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TransactionView<'a> {
|
2018-04-16 15:52:12 +02:00
|
|
|
/// Creates new view onto valid transaction rlp.
|
|
|
|
/// Use the `view!` macro to create this view in order to capture debugging info.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #[macro_use]
|
|
|
|
/// extern crate ethcore;
|
|
|
|
///
|
|
|
|
/// use ethcore::views::{TransactionView};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let bytes : &[u8] = &[];
|
|
|
|
/// let tx_view = view!(TransactionView, bytes);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn new(rlp: ViewRlp<'a>) -> TransactionView<'a> {
|
2016-06-28 12:08:30 +02:00
|
|
|
TransactionView {
|
|
|
|
rlp: rlp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return reference to underlaying rlp.
|
2018-04-16 15:52:12 +02:00
|
|
|
pub fn rlp(&self) -> &ViewRlp<'a> {
|
2016-06-28 12:08:30 +02:00
|
|
|
&self.rlp
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:18:28 +02:00
|
|
|
/// Returns transaction hash.
|
|
|
|
pub fn hash(&self) -> H256 {
|
|
|
|
keccak(self.rlp.as_raw())
|
|
|
|
}
|
|
|
|
|
2016-06-28 12:08:30 +02:00
|
|
|
/// Get the nonce field of the transaction.
|
|
|
|
pub fn nonce(&self) -> U256 { self.rlp.val_at(0) }
|
|
|
|
|
|
|
|
/// Get the gas_price field of the transaction.
|
|
|
|
pub fn gas_price(&self) -> U256 { self.rlp.val_at(1) }
|
|
|
|
|
|
|
|
/// Get the gas field of the transaction.
|
|
|
|
pub fn gas(&self) -> U256 { self.rlp.val_at(2) }
|
|
|
|
|
|
|
|
/// Get the value field of the transaction.
|
|
|
|
pub fn value(&self) -> U256 { self.rlp.val_at(4) }
|
|
|
|
|
|
|
|
/// Get the data field of the transaction.
|
|
|
|
pub fn data(&self) -> Bytes { self.rlp.val_at(5) }
|
|
|
|
|
|
|
|
/// Get the v field of the transaction.
|
|
|
|
pub fn v(&self) -> u8 { let r: u16 = self.rlp.val_at(6); r as u8 }
|
|
|
|
|
|
|
|
/// Get the r field of the transaction.
|
|
|
|
pub fn r(&self) -> U256 { self.rlp.val_at(7) }
|
|
|
|
|
|
|
|
/// Get the s field of the transaction.
|
|
|
|
pub fn s(&self) -> U256 { self.rlp.val_at(8) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-07-06 11:26:14 +02:00
|
|
|
use rustc_hex::FromHex;
|
2016-06-28 12:08:30 +02:00
|
|
|
use super::TransactionView;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transaction_view() {
|
|
|
|
let rlp = "f87c80018261a894095e7baea6a6c7c4c2dfeb977efac326af552d870a9d00000000000000000000000000000000000000000000000000000000001ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804".from_hex().unwrap();
|
|
|
|
|
2018-04-16 15:52:12 +02:00
|
|
|
let view = view!(TransactionView, &rlp);
|
2018-02-23 20:01:33 +01:00
|
|
|
assert_eq!(view.nonce(), 0.into());
|
|
|
|
assert_eq!(view.gas_price(), 1.into());
|
|
|
|
assert_eq!(view.gas(), 0x61a8.into());
|
|
|
|
assert_eq!(view.value(), 0xa.into());
|
2016-06-28 12:08:30 +02:00
|
|
|
assert_eq!(view.data(), "0000000000000000000000000000000000000000000000000000000000".from_hex().unwrap());
|
2018-02-23 20:01:33 +01:00
|
|
|
assert_eq!(view.r(), "48b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353".into());
|
|
|
|
assert_eq!(view.s(), "efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804".into());
|
2016-06-28 12:08:30 +02:00
|
|
|
assert_eq!(view.v(), 0x1b);
|
|
|
|
}
|
|
|
|
}
|