fuel_core_chain_config/config/
blob.rs

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
use crate::{
    serialization::HexIfHumanReadable,
    TableEntry,
};
use fuel_core_types::{
    fuel_types::BlobId,
    fuel_vm::{
        BlobBytes,
        BlobData,
    },
};
use serde::{
    Deserialize,
    Serialize,
};
use serde_with::serde_as;

#[serde_as]
#[derive(Default, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct BlobConfig {
    pub blob_id: BlobId,
    #[serde_as(as = "HexIfHumanReadable")]
    pub payload: Vec<u8>,
}

#[cfg(feature = "test-helpers")]
impl crate::Randomize for BlobConfig {
    fn randomize(mut rng: impl ::rand::Rng) -> Self {
        use fuel_core_types::fuel_tx::BlobIdExt;

        let payload_len = rng.gen_range(32..128);
        let mut payload = vec![0; payload_len as usize];
        rng.fill_bytes(&mut payload);

        let blob_id = BlobId::compute(&payload);

        Self { blob_id, payload }
    }
}

impl From<TableEntry<BlobData>> for BlobConfig {
    fn from(value: TableEntry<BlobData>) -> Self {
        BlobConfig {
            blob_id: value.key,
            payload: value.value.0.to_vec(),
        }
    }
}

impl From<BlobConfig> for TableEntry<BlobData> {
    fn from(config: BlobConfig) -> Self {
        Self {
            key: config.blob_id,
            value: BlobBytes(config.payload),
        }
    }
}