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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
//! EIp-2930 Ethereum transaction with access list type definition and RLP encoding.
use crate::{
account::Signature, serialization, transaction::accesslist::AccessList, transaction::rlp,
};
use ethaddr::Address;
use ethnum::U256;
use serde::Deserialize;
/// An EIP-2930 Ethereum transaction.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct Eip2930Transaction {
/// The chain ID for the transaction.
#[serde(rename = "chainId", with = "ethnum::serde::permissive")]
pub chain_id: U256,
/// The nonce for the transaction.
#[serde(with = "ethnum::serde::permissive")]
pub nonce: U256,
/// The gas price in Wei for the transaction.
#[serde(rename = "gasPrice", with = "ethnum::serde::permissive")]
pub gas_price: U256,
/// The gas limit for the transaction.
#[serde(with = "ethnum::serde::permissive")]
pub gas: U256,
/// The target address for the transaction. This can also be `None` to
/// indicate a contract creation transaction.
pub to: Option<Address>,
/// The amount of Ether to send with the transaction.
#[serde(with = "ethnum::serde::permissive")]
pub value: U256,
/// The calldata to use for the transaction.
#[serde(with = "serialization::bytes")]
pub data: Vec<u8>,
/// List of addresses and storage keys that the transaction plans to access.
#[serde(rename = "accessList")]
pub access_list: AccessList,
}
impl Eip2930Transaction {
/// Returns the RLP encoded transaction with an optional signature.
pub fn rlp_encode(&self, signature: Option<Signature>) -> Vec<u8> {
let fields = [
rlp::uint(self.chain_id),
rlp::uint(self.nonce),
rlp::uint(self.gas_price),
rlp::uint(self.gas),
self.to
.map_or_else(|| rlp::bytes(b""), |to| rlp::bytes(&*to)),
rlp::uint(self.value),
rlp::bytes(&self.data),
self.access_list.rlp_encode(),
];
let tail = signature.map(|signature| {
[
rlp::uint(signature.y_parity()),
rlp::uint(signature.r()),
rlp::uint(signature.s()),
]
});
[
&[0x01][..],
&rlp::iter(fields.iter().chain(tail.iter().flatten())),
]
.concat()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::transaction::accesslist::StorageSlot;
use ethaddr::address;
use ethnum::AsU256 as _;
use hex_literal::hex;
use serde_json::json;
#[test]
fn deserialize_json() {
let mut tx = json!({
"chainId": "0xff",
"nonce": 42,
"gasPrice": 13.37e9,
"gas": 21000,
"value": "13370000000000000000",
"data": "0x",
"accessList": [],
});
assert_eq!(
serde_json::from_value::<Eip2930Transaction>(tx.clone()).unwrap(),
Eip2930Transaction {
chain_id: 255.as_u256(),
nonce: 42.as_u256(),
gas_price: 13.37e9.as_u256(),
gas: 21_000.as_u256(),
to: None,
value: 13.37e18.as_u256(),
data: vec![],
access_list: AccessList::default(),
}
);
tx["to"] = json!("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
assert_eq!(
serde_json::from_value::<Eip2930Transaction>(tx)
.unwrap()
.to
.unwrap(),
address!("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"),
);
}
#[test]
fn encode() {
assert_eq!(
Eip2930Transaction {
chain_id: 1.as_u256(),
nonce: 66.as_u256(),
gas_price: 42e9.as_u256(),
gas: 30_000.as_u256(),
to: Some(address!("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")),
value: 13.37e18.as_u256(),
data: vec![],
access_list: AccessList::default(),
}
.rlp_encode(None),
hex!(
"01eb01428509c765240082753094deadbeefdeadbeefdeadbeefdeadbeefdead
beef88b98bc829a6f9000080c0"
)
.to_owned(),
);
assert_eq!(
Eip2930Transaction {
chain_id: 1.as_u256(),
nonce: 777.as_u256(),
gas_price: 42e9.as_u256(),
gas: 100_000.as_u256(),
to: None,
value: 0.as_u256(),
data: hex!(
"363d3d373d3d3d363d73deadbeefdeadbeefdeadbeefdeadbeefdeadbeef5af43d82803e90
3d91602b57fd5bf3"
)
.to_vec(),
access_list: AccessList(vec![
(
address!("0x1111111111111111111111111111111111111111"),
vec![
StorageSlot(hex!(
"a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0"
)),
StorageSlot(hex!(
"a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1"
)),
],
),
(
address!("0x2222222222222222222222222222222222222222"),
vec![],
),
]),
}
.rlp_encode(None),
hex!(
"01f8b2018203098509c7652400830186a08080ad363d3d373d3d3d363d73dead
beefdeadbeefdeadbeefdeadbeefdeadbeef5af43d82803e903d91602b57fd5b
f3f872f859941111111111111111111111111111111111111111f842a0a0a0a0
a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a1a1
a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1d694
2222222222222222222222222222222222222222c0"
)
.to_vec(),
);
}
}
|