Skip to content

Commit c01d0ee

Browse files
committed
refactor(ethportal-api): reth type funcs to module
The type conversion functions will be reused in several places, and don't belong co-located with the header types.
1 parent ada1a43 commit c01d0ee

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

ethportal-api/src/types/execution/header.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use ethereum_types::{Bloom, H160, H256, H64, U256};
22
use reth_rpc_types::Header as RpcHeader;
33
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
4-
use ruint::Uint;
54
use serde::{Deserialize, Deserializer, Serialize, Serializer};
65
use ssz::{Encode, SszDecoderBuilder, SszEncoder};
76
use ssz_derive::{Decode, Encode};
87

98
use crate::types::bytes::ByteList;
109
use crate::utils::bytes::{hex_decode, hex_encode};
10+
use crate::utils::rethtypes::{u256_to_uint256, u64_to_uint256};
1111

1212
const LONDON_BLOCK_NUMBER: u64 = 12965000;
1313
const SHANGHAI_BLOCK_NUMBER: u64 = 17034871;
@@ -258,22 +258,6 @@ impl From<Header> for RpcHeader {
258258
}
259259
}
260260

261-
fn u256_to_uint256(u256: U256) -> Uint<256, 4> {
262-
let mut bytes = [0u8; 32];
263-
u256.to_big_endian(&mut bytes);
264-
Uint::from_be_bytes(bytes)
265-
}
266-
267-
fn u64_to_uint256(val: u64) -> Uint<256, 4> {
268-
let u64_bytes: &[u8] = &val.to_be_bytes();
269-
let high_zero_bytes: &[u8] = &[0u8; 24];
270-
let bytes: [u8; 32] = [high_zero_bytes, u64_bytes]
271-
.concat()
272-
.try_into()
273-
.expect("8 bytes + 24 bytes should be 32 bytes");
274-
Uint::from_be_bytes(bytes)
275-
}
276-
277261
#[derive(Debug, Clone, PartialEq, Eq)]
278262
pub struct TxHashes {
279263
pub hashes: Vec<H256>,

ethportal-api/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod bytes;
2+
pub mod rethtypes;
23
pub mod serde;

ethportal-api/src/utils/rethtypes.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use ethereum_types::U256;
2+
use ruint::Uint;
3+
4+
pub fn u256_to_uint256(u256: U256) -> Uint<256, 4> {
5+
let mut bytes = [0u8; 32];
6+
u256.to_big_endian(&mut bytes);
7+
Uint::from_be_bytes(bytes)
8+
}
9+
10+
pub fn u64_to_uint256(val: u64) -> Uint<256, 4> {
11+
let u64_bytes: &[u8] = &val.to_be_bytes();
12+
let high_zero_bytes: &[u8] = &[0u8; 24];
13+
let bytes: [u8; 32] = [high_zero_bytes, u64_bytes]
14+
.concat()
15+
.try_into()
16+
.expect("8 bytes + 24 bytes should be 32 bytes");
17+
Uint::from_be_bytes(bytes)
18+
}

0 commit comments

Comments
 (0)