1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use bytes::Bytes;
use ethereum_types::{H256, U256};
use hash::keccak;
use super::ViewRlp;
pub struct TransactionView<'a> {
rlp: ViewRlp<'a>
}
impl<'a> TransactionView<'a> {
pub fn new(rlp: ViewRlp<'a>) -> TransactionView<'a> {
TransactionView {
rlp: rlp
}
}
pub fn rlp(&self) -> &ViewRlp<'a> {
&self.rlp
}
pub fn hash(&self) -> H256 {
keccak(self.rlp.as_raw())
}
pub fn nonce(&self) -> U256 { self.rlp.val_at(0) }
pub fn gas_price(&self) -> U256 { self.rlp.val_at(1) }
pub fn gas(&self) -> U256 { self.rlp.val_at(2) }
pub fn value(&self) -> U256 { self.rlp.val_at(4) }
pub fn data(&self) -> Bytes { self.rlp.val_at(5) }
pub fn v(&self) -> u8 { let r: u16 = self.rlp.val_at(6); r as u8 }
pub fn r(&self) -> U256 { self.rlp.val_at(7) }
pub fn s(&self) -> U256 { self.rlp.val_at(8) }
}
#[cfg(test)]
mod tests {
use rustc_hex::FromHex;
use super::TransactionView;
#[test]
fn test_transaction_view() {
let rlp = "f87c80018261a894095e7baea6a6c7c4c2dfeb977efac326af552d870a9d00000000000000000000000000000000000000000000000000000000001ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804".from_hex().unwrap();
let view = view!(TransactionView, &rlp);
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());
assert_eq!(view.data(), "0000000000000000000000000000000000000000000000000000000000".from_hex().unwrap());
assert_eq!(view.r(), "48b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353".into());
assert_eq!(view.s(), "efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804".into());
assert_eq!(view.v(), 0x1b);
}
}