Skip to content

Commit 8661e20

Browse files
committed
Code cleanup, rustfmt
1 parent 75f21f7 commit 8661e20

File tree

5 files changed

+32
-225
lines changed

5 files changed

+32
-225
lines changed

src/evm_utils/src/transaction.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use crate::types::transaction::Transaction;
44

55
#[query]
66
fn create_transaction(data: Transaction) -> Result<Vec<u8>, String> {
7-
let raw = data.encode()
8-
.map_err(|x| format!("Error while encoding transaction {}", x))?;
9-
7+
let raw = data
8+
.encode()
9+
.map_err(|x| format!("Error while encoding transaction {}", x))?;
10+
1011
Ok(raw)
1112
}
1213

@@ -17,4 +18,4 @@ fn parse_transaction(data: Vec<u8>) -> Result<Transaction, String> {
1718
.map_err(|x| format!("Error while decoding transaction {}", x))?;
1819

1920
Ok(item)
20-
}
21+
}

src/evm_utils/src/types/address.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{fmt::Display};
1+
use std::fmt::Display;
22

33
use ic_cdk::export::candid::{CandidType, Deserialize};
4-
use rlp::{Decodable, Encodable, RlpStream, DecoderError, Rlp};
4+
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
55

66
#[derive(CandidType, Deserialize, Clone, PartialEq, Eq)]
77
pub struct Address(pub [u8; 20]);
@@ -10,10 +10,12 @@ impl Decodable for Address {
1010
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
1111
let data = rlp.data()?;
1212
if data.len() != 20 {
13-
return Err(DecoderError::Custom("Invalid number of bytes for ETH address"));
13+
return Err(DecoderError::Custom(
14+
"Invalid number of bytes for ETH address",
15+
));
1416
}
1517

16-
let mut buf = [0u8;20];
18+
let mut buf = [0u8; 20];
1719
buf.copy_from_slice(&data[..20]);
1820
Ok(Self(buf))
1921
}

src/evm_utils/src/types/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
pub mod address;
12
pub mod num;
23
pub mod rlp;
34
pub mod transaction;
4-
pub mod address;

src/evm_utils/src/types/num.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use ic_cdk::export::candid::{CandidType, Deserialize};
2-
use rlp::{Decodable, Encodable, RlpStream, DecoderError, Rlp};
2+
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
33

44
#[derive(CandidType, Deserialize, Clone, PartialEq, Eq)]
55
pub struct U256(pub [u8; 32]);
66
impl U256 {
77
pub fn zero() -> Self {
8-
Self([0u8;32])
8+
Self([0u8; 32])
99
}
1010
pub fn leading_zeros(&self) -> usize {
1111
let mut count = 0;
@@ -50,10 +50,14 @@ impl U256 {
5050
#[inline]
5151
fn fits_word(&self) -> bool {
5252
let U256(ref arr) = self;
53-
for i in 1..24 { if arr[i] != 0 { return false; } }
53+
for i in 1..24 {
54+
if arr[i] != 0 {
55+
return false;
56+
}
57+
}
5458
return true;
5559
}
56-
}
60+
}
5761

5862
impl From<[u8; 32]> for U256 {
5963
#[inline]
@@ -93,21 +97,20 @@ impl Decodable for U256 {
9397
0 => Ok(U256::zero()),
9498
l if l <= 32 => {
9599
if bytes[0] == 0 {
96-
return Err(DecoderError::RlpInvalidIndirection)
100+
return Err(DecoderError::RlpInvalidIndirection);
97101
}
98102
let mut res = U256::zero();
99103

100104
for (i, byte) in bytes.iter().enumerate().take(l) {
101-
res.0[32 - l+i] = *byte;
105+
res.0[32 - l + i] = *byte;
102106
}
103107
Ok(res)
104-
},
108+
}
105109
_ => Err(DecoderError::RlpIsTooBig),
106110
})
107111
}
108112
}
109113

110-
111114
#[cfg(test)]
112115
mod test {
113116
use super::U256;
@@ -139,4 +142,4 @@ mod test {
139142
let item2 = U256([1u8; 32]);
140143
assert!(!item2.is_zero());
141144
}
142-
}
145+
}

src/evm_utils/src/types/transaction.rs

Lines changed: 8 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use std::{
33
fmt::{self, Display},
44
};
55

6+
use bytes::BufMut;
67
use bytes::BytesMut;
78
use ic_cdk::export::candid::{CandidType, Deserialize};
89
use rlp::{Decodable, Encodable, RlpStream};
9-
use bytes::BufMut;
1010

1111
use super::{address::Address, num::U256};
1212

@@ -52,10 +52,10 @@ impl Transaction {
5252
buf.put_u8(2u8); //write EIP1559 identifier
5353
buf.extend_from_slice(rlp::encode(a).as_ref());
5454
buf.to_vec()
55-
},
56-
Transaction::EIP2930 => vec![]
55+
}
56+
Transaction::EIP2930 => vec![],
5757
};
58-
58+
5959
Ok(data)
6060
}
6161
}
@@ -233,12 +233,11 @@ impl Encodable for Transaction1559 {
233233
}
234234
}
235235

236-
237236
#[cfg(test)]
238237
mod test {
239238
use std::error::Error;
240239

241-
use super::{Transaction};
240+
use super::Transaction;
242241

243242
#[test]
244243
fn decode_legacy_transaction() -> Result<(), Box<dyn Error>> {
@@ -248,7 +247,7 @@ mod test {
248247
let tx = Transaction::decode(&data)?;
249248
match tx {
250249
Transaction::Legacy(_) => Ok(()),
251-
_ => panic!("Wrong transaction type")
250+
_ => panic!("Wrong transaction type"),
252251
}
253252
}
254253

@@ -277,11 +276,10 @@ mod test {
277276

278277
match tx {
279278
Transaction::EIP1559(_) => Ok(()),
280-
_ => panic!("Wrong transaction type")
279+
_ => panic!("Wrong transaction type"),
281280
}
282281
}
283282

284-
285283
#[test]
286284
fn encode_1559_transaction() -> Result<(), Box<dyn Error>> {
287285
let data_hex = "0x02f871015585037d46c34985037d46c34983027d0594e68df8dc3931aaab2077c57bbd2cbcedd17fcfce808457386225c080a0ac97a4d2f460d238fddaaed992047547d04c17ae454d6219a3a96699115ffcf5a006c2d3bd79f9b3321438721609ff6dddb0e50b8d9e38d02b68456590c33dee47";
@@ -290,7 +288,7 @@ mod test {
290288
let tx = Transaction::decode(&data)?;
291289

292290
let encoded = Transaction::encode(&tx)?;
293-
let encoded_hex = format!("0x{}",hex::encode(&encoded));
291+
let encoded_hex = format!("0x{}", hex::encode(&encoded));
294292

295293
assert_eq!(data, encoded);
296294
assert_eq!(data_hex, &encoded_hex);
@@ -299,8 +297,6 @@ mod test {
299297
}
300298
}
301299

302-
303-
304300
// pub struct Transaction2930 {
305301
// pub chain_id: u64,
306302
// pub nonce: u64,
@@ -314,198 +310,3 @@ mod test {
314310
// pub r: String,
315311
// pub s: String,
316312
// }
317-
318-
// impl From<(Vec<u8>, u64)> for TransactionLegacy {
319-
// fn from(data: (Vec<u8>, u64)) -> Self {
320-
// let rlp = rlp::Rlp::new(&data.0[..]);
321-
322-
// let nonce_hex = rlp.at(0).as_val::<Vec<u8>>();
323-
// let nonce = vec_u8_to_u64(&nonce_hex);
324-
325-
// let gas_price_hex = rlp.at(1).as_val::<Vec<u8>>();
326-
// let gas_price = vec_u8_to_u64(&gas_price_hex);
327-
328-
// let gas_limit_hex = rlp.at(2).as_val::<Vec<u8>>();
329-
// let gas_limit = vec_u8_to_u64(&gas_limit_hex);
330-
331-
// let to_hex = rlp.at(3).as_val::<Vec<u8>>();
332-
// let to = vec_u8_to_string(&to_hex);
333-
334-
// let value_hex = rlp.at(4).as_val::<Vec<u8>>();
335-
// let value = vec_u8_to_u64(&value_hex);
336-
337-
// let data_tx_hex = rlp.at(5).as_val::<Vec<u8>>();
338-
// let data_tx = vec_u8_to_string(&data_tx_hex);
339-
340-
// let v_hex = rlp.at(6).as_val::<Vec<u8>>();
341-
// let v = vec_u8_to_string(&v_hex);
342-
343-
// let r_hex = rlp.at(7).as_val::<Vec<u8>>();
344-
// let r = vec_u8_to_string(&r_hex);
345-
346-
// let s_hex = rlp.at(8).as_val::<Vec<u8>>();
347-
// let s = vec_u8_to_string(&s_hex);
348-
349-
// let chain_id =data.1;
350-
351-
// TransactionLegacy {
352-
// chain_id,
353-
// nonce,
354-
// gas_price,
355-
// gas_limit,
356-
// to,
357-
// value,
358-
// data: data_tx,
359-
// v,
360-
// r,
361-
// s,
362-
// }
363-
// }
364-
// }
365-
366-
// impl From<Vec<u8>> for Transaction2930 {
367-
// fn from(data: Vec<u8>) -> Self {
368-
// let rlp = rlp::Rlp::new(&data[1..]);
369-
370-
// let chain_id_hex = rlp.at(0).as_val::<Vec<u8>>();
371-
// let chain_id = vec_u8_to_u64(&chain_id_hex);
372-
373-
// let nonce_hex = rlp.at(1).as_val::<Vec<u8>>();
374-
// let nonce = vec_u8_to_u64(&nonce_hex);
375-
376-
// let gas_price_hex = rlp.at(2).as_val::<Vec<u8>>();
377-
// let gas_price = vec_u8_to_u64(&gas_price_hex);
378-
379-
// let gas_limit_hex = rlp.at(3).as_val::<Vec<u8>>();
380-
// let gas_limit = vec_u8_to_u64(&gas_limit_hex);
381-
382-
// let to_hex = rlp.at(4).as_val::<Vec<u8>>();
383-
// let to = vec_u8_to_string(&to_hex);
384-
385-
// let value_hex = rlp.at(5).as_val::<Vec<u8>>();
386-
// let value = vec_u8_to_u64(&value_hex);
387-
388-
// let data_tx_hex = rlp.at(6).as_val::<Vec<u8>>();
389-
// let data_tx = vec_u8_to_string(&data_tx_hex);
390-
391-
// let access_list = decode_access_list(&rlp.at(7).as_raw().to_vec());
392-
393-
// let v_hex = rlp.at(8).as_val::<Vec<u8>>();
394-
// let v = vec_u8_to_string(&v_hex);
395-
396-
// let r_hex = rlp.at(9).as_val::<Vec<u8>>();
397-
// let r = vec_u8_to_string(&r_hex);
398-
399-
// let s_hex = rlp.at(10).as_val::<Vec<u8>>();
400-
// let s = vec_u8_to_string(&s_hex);
401-
// Transaction2930 {
402-
// chain_id,
403-
// nonce,
404-
// gas_price,
405-
// gas_limit,
406-
// to,
407-
// data: data_tx,
408-
// value,
409-
// access_list,
410-
// v,
411-
// r,
412-
// s,
413-
// }
414-
415-
// }
416-
// }
417-
418-
// impl From<Vec<u8>> for Transaction1559 {
419-
// fn from(data: Vec<u8>) -> Self {
420-
// let rlp = rlp::Rlp::new(&data[1..]);
421-
422-
// // let chain_id_hex = rlp.at(0).as_val::<Vec<u8>>();
423-
// // let chain_id = vec_u8_to_u64(&chain_id_hex);
424-
425-
// // let nonce_hex = rlp.at(1).as_val::<Vec<u8>>();
426-
// // let nonce = vec_u8_to_u64(&nonce_hex);
427-
428-
// // let max_priority_fee_per_gas_hex = rlp.at(2).as_val::<Vec<u8>>();
429-
// // let max_priority_fee_per_gas = vec_u8_to_u64(&max_priority_fee_per_gas_hex);
430-
431-
// // let max_fee_per_gas_hex = rlp.at(3).as_val::<Vec<u8>>();
432-
433-
// // let max_fee_per_gas = vec_u8_to_u64(&max_fee_per_gas_hex);
434-
435-
// // let gas_limit_hex = rlp.at(4).as_val::<Vec<u8>>();
436-
// // let gas_limit = vec_u8_to_u64(&gas_limit_hex);
437-
438-
// // let to_hex = rlp.at(5).as_val::<Vec<u8>>();
439-
// // let to = vec_u8_to_string(&to_hex);
440-
441-
// // let value_hex = rlp.at(6).as_val::<Vec<u8>>();
442-
// // let value = vec_u8_to_u64(&value_hex);
443-
444-
// // let data_tx_hex = rlp.at(7).as_val::<Vec<u8>>();
445-
// // let data_tx = vec_u8_to_string(&data_tx_hex);
446-
447-
// // let access_list = decode_access_list(&rlp.at(8).as_raw().to_vec());
448-
449-
// // let v_hex = rlp.at(9).as_val::<Vec<u8>>();
450-
// // let v = vec_u8_to_string(&v_hex);
451-
452-
// // let r_hex = rlp.at(10).as_val::<Vec<u8>>();
453-
// // let r = vec_u8_to_string(&r_hex);
454-
455-
// // let s_hex = rlp.at(11).as_val::<Vec<u8>>();
456-
// // let s = vec_u8_to_string(&s_hex);
457-
458-
// // Transaction1559 {
459-
// // chain_id,
460-
// // nonce,
461-
// // max_priority_fee_per_gas,
462-
// // max_fee_per_gas,
463-
// // gas_limit,
464-
// // to,
465-
// // value,
466-
// // data: data_tx,
467-
// // access_list,
468-
// // v,
469-
// // r,
470-
// // s,
471-
// // }
472-
// }
473-
// }
474-
475-
// fn encode_access_list(access_list: &Vec<(String, Vec<String>)>) -> Vec<u8> {
476-
// let mut stream = rlp::RlpStream::new_list(access_list.len());
477-
478-
// for list in access_list {
479-
// let mut stream_tuple = rlp::RlpStream::new_list(2);
480-
481-
// // append address
482-
// stream_tuple.append(&string_to_vec_u8(&list.0[..]));
483-
484-
// // append storage keys
485-
// let mut stream_storage_keys = rlp::RlpStream::new_list(list.1.len());
486-
// for storage_key in list.1.clone() {
487-
// stream_storage_keys.append(&string_to_vec_u8(&storage_key[..]));
488-
// }
489-
// stream_tuple.append_raw(&stream_storage_keys.out(), 1);
490-
491-
// // append (address, storage_keys)
492-
// stream.append_raw(&stream_tuple.out(), 1);
493-
// }
494-
495-
// stream.out().to_vec()
496-
// }
497-
498-
// fn decode_access_list(access_list: &Vec<u8>) -> Vec<(String, Vec<String>)> {
499-
// let mut decoded_access_list = vec![];
500-
// let rlp = rlp::Rlp::new(access_list);
501-
// for item in rlp.iter() {
502-
// let address = item.at(0).as_val();
503-
// let storage_keys_u8 = item.at(1).as_list::<Vec<u8>>();
504-
// let storage_keys = storage_keys_u8
505-
// .iter()
506-
// .map(|x| vec_u8_to_string(x))
507-
// .collect::<Vec<String>>();
508-
// decoded_access_list.push((vec_u8_to_string(&address), storage_keys));
509-
// }
510-
// decoded_access_list
511-
// }

0 commit comments

Comments
 (0)