Skip to main content

ethexe_common/
utils.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4use crate::db::{BlockMeta, BlockMetaStorageRW, OnChainStorageRW, PreparedBlockData};
5use gprimitives::H256;
6
7/// Decodes hexed string to a byte array.
8pub fn decode_to_array<const N: usize>(s: &str) -> Result<[u8; N], hex::FromHexError> {
9    // Strip the "0x" prefix if it exists.
10    let stripped = s.strip_prefix("0x").unwrap_or(s);
11
12    // Decode
13    let mut buf = [0u8; N];
14    hex::decode_to_slice(stripped, &mut buf)?;
15
16    Ok(buf)
17}
18
19/// Converts u64 to a 48-bit unsigned integer, represented as a byte array in big-endian order.
20pub const fn u64_into_uint48_be_bytes_lossy(val: u64) -> [u8; 6] {
21    let [_, _, b1, b2, b3, b4, b5, b6] = val.to_be_bytes();
22
23    [b1, b2, b3, b4, b5, b6]
24}
25
26pub fn setup_block_in_db<DB: OnChainStorageRW + BlockMetaStorageRW>(
27    db: &DB,
28    block_hash: H256,
29    block_data: PreparedBlockData,
30) {
31    db.set_block_header(block_hash, block_data.header);
32    db.set_block_events(block_hash, &block_data.events);
33    db.set_block_synced(block_hash);
34
35    db.mutate_block_meta(block_hash, |meta| {
36        *meta = BlockMeta {
37            prepared: true,
38            codes_queue: Some(block_data.codes_queue),
39            last_committed_batch: Some(block_data.last_committed_batch),
40            last_committed_mb: Some(block_data.last_committed_mb),
41            last_committed_eb: Some(block_data.last_committed_eb),
42            latest_era_validators_committed: Some(block_data.latest_era_with_committed_validators),
43        }
44    });
45}