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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use super::{TransactionRepr, TRANSACTION_CREATE_FIXED_SIZE, TRANSACTION_SCRIPT_FIXED_SIZE};
use crate::transaction::types::StorageSlot;
use crate::{Input, Output, Transaction, Witness};
use fuel_types::bytes::{self, SizedBytes, WORD_SIZE};
use fuel_types::Word;
use std::io::{self, Write};
impl Transaction {
pub fn try_from_bytes(bytes: &[u8]) -> io::Result<(usize, Self)> {
let mut tx = Self::default();
let n = tx.write(bytes)?;
Ok((n, tx))
}
}
impl io::Read for Transaction {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let n = self.serialized_size();
if buf.len() < n {
return Err(bytes::eof());
}
let mut buf = match self {
Self::Script {
gas_price,
gas_limit,
maturity,
receipts_root,
script,
script_data,
inputs,
outputs,
witnesses,
..
} => {
let buf = bytes::store_number_unchecked(buf, TransactionRepr::Script as Word);
let buf = bytes::store_number_unchecked(buf, *gas_price);
let buf = bytes::store_number_unchecked(buf, *gas_limit);
let buf = bytes::store_number_unchecked(buf, *maturity);
let buf = bytes::store_number_unchecked(buf, script.len() as Word);
let buf = bytes::store_number_unchecked(buf, script_data.len() as Word);
let buf = bytes::store_number_unchecked(buf, inputs.len() as Word);
let buf = bytes::store_number_unchecked(buf, outputs.len() as Word);
let buf = bytes::store_number_unchecked(buf, witnesses.len() as Word);
let buf = bytes::store_array_unchecked(buf, receipts_root);
let (_, buf) = bytes::store_raw_bytes(buf, script.as_slice())?;
let (_, buf) = bytes::store_raw_bytes(buf, script_data.as_slice())?;
buf
}
Self::Create {
gas_price,
gas_limit,
maturity,
bytecode_length,
bytecode_witness_index,
salt,
storage_slots,
inputs,
outputs,
witnesses,
..
} => {
let buf = bytes::store_number_unchecked(buf, TransactionRepr::Create as Word);
let buf = bytes::store_number_unchecked(buf, *gas_price);
let buf = bytes::store_number_unchecked(buf, *gas_limit);
let buf = bytes::store_number_unchecked(buf, *maturity);
let buf = bytes::store_number_unchecked(buf, *bytecode_length);
let buf = bytes::store_number_unchecked(buf, *bytecode_witness_index);
let buf = bytes::store_number_unchecked(buf, storage_slots.len() as Word);
let buf = bytes::store_number_unchecked(buf, inputs.len() as Word);
let buf = bytes::store_number_unchecked(buf, outputs.len() as Word);
let buf = bytes::store_number_unchecked(buf, witnesses.len() as Word);
let mut buf = bytes::store_array_unchecked(buf, salt);
for storage_slot in storage_slots.iter_mut() {
let storage_len = storage_slot.read(buf)?;
buf = &mut buf[storage_len..];
}
buf
}
};
for input in self._inputs_mut() {
let input_len = input.read(buf)?;
buf = &mut buf[input_len..];
}
for output in self._outputs_mut() {
let output_len = output.read(buf)?;
buf = &mut buf[output_len..];
}
for witness in self._witnesses_mut() {
let witness_len = witness.read(buf)?;
buf = &mut buf[witness_len..];
}
Ok(n)
}
}
impl Write for Transaction {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if buf.len() < WORD_SIZE {
return Err(bytes::eof());
}
let (identifier, buf): (Word, _) = unsafe { bytes::restore_number_unchecked(buf) };
let identifier = TransactionRepr::try_from(identifier)?;
match identifier {
TransactionRepr::Script => {
let mut n = TRANSACTION_SCRIPT_FIXED_SIZE;
if buf.len() < n - WORD_SIZE {
return Err(bytes::eof());
}
let (gas_price, buf) = unsafe { bytes::restore_number_unchecked(buf) };
let (gas_limit, buf) = unsafe { bytes::restore_number_unchecked(buf) };
let (maturity, buf) = unsafe { bytes::restore_number_unchecked(buf) };
let (script_len, buf) = unsafe { bytes::restore_usize_unchecked(buf) };
let (script_data_len, buf) = unsafe { bytes::restore_usize_unchecked(buf) };
let (inputs_len, buf) = unsafe { bytes::restore_usize_unchecked(buf) };
let (outputs_len, buf) = unsafe { bytes::restore_usize_unchecked(buf) };
let (witnesses_len, buf) = unsafe { bytes::restore_usize_unchecked(buf) };
let (receipts_root, buf) = unsafe { bytes::restore_array_unchecked(buf) };
let receipts_root = receipts_root.into();
let (size, script, buf) = bytes::restore_raw_bytes(buf, script_len)?;
n += size;
let (size, script_data, mut buf) = bytes::restore_raw_bytes(buf, script_data_len)?;
n += size;
let mut inputs = vec![Input::default(); inputs_len];
for input in inputs.iter_mut() {
let input_len = input.write(buf)?;
buf = &buf[input_len..];
n += input_len;
}
let mut outputs = vec![Output::default(); outputs_len];
for output in outputs.iter_mut() {
let output_len = output.write(buf)?;
buf = &buf[output_len..];
n += output_len;
}
let mut witnesses = vec![Witness::default(); witnesses_len];
for witness in witnesses.iter_mut() {
let witness_len = witness.write(buf)?;
buf = &buf[witness_len..];
n += witness_len;
}
*self = Transaction::Script {
gas_price,
gas_limit,
maturity,
receipts_root,
script,
script_data,
inputs,
outputs,
witnesses,
metadata: None,
};
Ok(n)
}
TransactionRepr::Create => {
let mut n = TRANSACTION_CREATE_FIXED_SIZE;
if buf.len() < n - WORD_SIZE {
return Err(bytes::eof());
}
let (gas_price, buf) = unsafe { bytes::restore_number_unchecked(buf) };
let (gas_limit, buf) = unsafe { bytes::restore_number_unchecked(buf) };
let (maturity, buf) = unsafe { bytes::restore_number_unchecked(buf) };
let (bytecode_length, buf) = unsafe { bytes::restore_number_unchecked(buf) };
let (bytecode_witness_index, buf) = unsafe { bytes::restore_u8_unchecked(buf) };
let (storage_slots_len, buf) = unsafe { bytes::restore_u16_unchecked(buf) };
let (inputs_len, buf) = unsafe { bytes::restore_usize_unchecked(buf) };
let (outputs_len, buf) = unsafe { bytes::restore_usize_unchecked(buf) };
let (witnesses_len, buf) = unsafe { bytes::restore_usize_unchecked(buf) };
let (salt, mut buf) = unsafe { bytes::restore_array_unchecked(buf) };
let salt = salt.into();
let mut storage_slots = vec![StorageSlot::default(); storage_slots_len as usize];
n += StorageSlot::SLOT_SIZE * storage_slots_len as usize;
for storage_slot in storage_slots.iter_mut() {
let _ = storage_slot.write(buf)?;
buf = &buf[StorageSlot::SLOT_SIZE..];
}
let mut inputs = vec![Input::default(); inputs_len];
for input in inputs.iter_mut() {
let input_len = input.write(buf)?;
buf = &buf[input_len..];
n += input_len;
}
let mut outputs = vec![Output::default(); outputs_len];
for output in outputs.iter_mut() {
let output_len = output.write(buf)?;
buf = &buf[output_len..];
n += output_len;
}
let mut witnesses = vec![Witness::default(); witnesses_len];
for witness in witnesses.iter_mut() {
let witness_len = witness.write(buf)?;
buf = &buf[witness_len..];
n += witness_len;
}
*self = Self::Create {
gas_price,
gas_limit,
maturity,
bytecode_length,
bytecode_witness_index,
salt,
storage_slots,
inputs,
outputs,
witnesses,
metadata: None,
};
Ok(n)
}
}
}
fn flush(&mut self) -> io::Result<()> {
self._inputs_mut()
.iter_mut()
.try_for_each(|input| input.flush())?;
self._outputs_mut()
.iter_mut()
.try_for_each(|output| output.flush())?;
self._witnesses_mut()
.iter_mut()
.try_for_each(|witness| witness.flush())?;
if let Transaction::Create { storage_slots, .. } = self {
storage_slots.iter_mut().try_for_each(|slot| slot.flush())?;
}
Ok(())
}
}