bee_inx/
raw.rs

1// Copyright 2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use std::marker::PhantomData;
5
6use bee_block as bee;
7use inx::proto;
8use packable::{Packable, PackableExt};
9
10use crate::Error;
11
12/// Represents a type as raw bytes.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct Raw<T: Packable> {
15    data: Vec<u8>,
16    _phantom: PhantomData<T>,
17}
18
19impl<T: Packable> Raw<T> {
20    #[must_use]
21    pub fn data(self) -> Vec<u8> {
22        self.data
23    }
24
25    pub fn inner(self, visitor: &T::UnpackVisitor) -> Result<T, Error> {
26        let unpacked = T::unpack_verified(self.data, visitor)
27            .map_err(|e| bee_block::InxError::InvalidRawBytes(format!("{:?}", e)))?;
28        Ok(unpacked)
29    }
30
31    pub fn inner_unverified(self) -> Result<T, Error> {
32        let unpacked =
33            T::unpack_unverified(self.data).map_err(|e| bee_block::InxError::InvalidRawBytes(format!("{:?}", e)))?;
34        Ok(unpacked)
35    }
36}
37
38impl<T: Packable> From<Vec<u8>> for Raw<T> {
39    fn from(value: Vec<u8>) -> Self {
40        Self {
41            data: value,
42            _phantom: PhantomData,
43        }
44    }
45}
46
47impl From<proto::RawOutput> for Raw<bee::output::Output> {
48    fn from(value: proto::RawOutput) -> Self {
49        value.data.into()
50    }
51}
52
53impl From<Raw<bee::output::Output>> for proto::RawOutput {
54    fn from(value: Raw<bee::output::Output>) -> Self {
55        Self { data: value.data }
56    }
57}
58
59impl From<proto::RawBlock> for Raw<bee::Block> {
60    fn from(value: proto::RawBlock) -> Self {
61        value.data.into()
62    }
63}
64
65impl From<Raw<bee::Block>> for proto::RawBlock {
66    fn from(value: Raw<bee::Block>) -> Self {
67        Self { data: value.data }
68    }
69}
70
71impl From<proto::RawMilestone> for Raw<bee::payload::Payload> {
72    fn from(value: proto::RawMilestone) -> Self {
73        value.data.into()
74    }
75}
76
77impl From<Raw<bee::payload::Payload>> for proto::RawMilestone {
78    fn from(value: Raw<bee::payload::Payload>) -> Self {
79        Self { data: value.data }
80    }
81}
82
83#[cfg(test)]
84mod test {
85    use bee::{payload::Payload, rand::output::rand_output};
86
87    use super::*;
88    use crate::ProtocolParameters;
89
90    #[test]
91    fn raw_output() {
92        let protocol_parameters = bee::protocol::protocol_parameters();
93
94        let output = rand_output(protocol_parameters.token_supply());
95
96        let proto = proto::RawOutput {
97            data: output.pack_to_vec(),
98        };
99        let raw: Raw<bee::output::Output> = proto.into();
100        assert_eq!(output, raw.clone().inner_unverified().unwrap());
101        assert_eq!(output, raw.inner(&protocol_parameters).unwrap());
102    }
103
104    #[test]
105    fn raw_protocol_parameters() {
106        let protocol_parameters = bee::protocol::protocol_parameters();
107        let proto = proto::RawProtocolParameters::from(protocol_parameters.clone());
108
109        let pp: ProtocolParameters = proto.into();
110        assert_eq!(protocol_parameters, pp.params.inner(&()).unwrap());
111    }
112
113    #[test]
114    fn raw_milestone() {
115        // The `RawMilestone` field in the protobuf definitions contains a `Payload`.
116        let data = vec![
117            7, 0, 0, 0, 235, 183, 17, 0, 150, 184, 45, 99, 2, 126, 53, 176, 136, 103, 202, 201, 164, 84, 234, 102, 52,
118            171, 19, 86, 241, 78, 148, 108, 76, 99, 18, 176, 43, 136, 175, 205, 186, 39, 155, 115, 158, 5, 27, 222, 99,
119            26, 188, 240, 18, 171, 222, 80, 175, 161, 110, 80, 181, 171, 223, 86, 77, 122, 35, 69, 184, 169, 73, 177,
120            144, 255, 64, 2, 125, 223, 36, 189, 63, 74, 113, 243, 26, 162, 78, 159, 68, 191, 74, 63, 138, 111, 55, 217,
121            124, 187, 99, 14, 129, 112, 177, 54, 75, 51, 29, 94, 194, 108, 58, 181, 252, 101, 231, 242, 208, 69, 255,
122            219, 80, 85, 132, 62, 19, 136, 1, 113, 123, 196, 54, 170, 134, 192, 96, 146, 169, 124, 108, 9, 66, 101,
123            184, 243, 122, 69, 16, 194, 200, 45, 205, 89, 164, 188, 244, 218, 182, 112, 143, 192, 61, 158, 79, 230, 66,
124            8, 64, 112, 65, 89, 168, 34, 147, 58, 185, 109, 59, 175, 9, 6, 150, 11, 165, 117, 104, 4, 25, 45, 224, 43,
125            75, 68, 184, 151, 155, 248, 80, 131, 42, 72, 179, 204, 16, 104, 158, 232, 234, 48, 144, 225, 232, 43, 143,
126            243, 228, 66, 2, 194, 2, 71, 151, 52, 184, 136, 100, 74, 7, 87, 13, 21, 233, 253, 237, 32, 38, 144, 37,
127            129, 139, 141, 63, 242, 146, 133, 0, 180, 108, 136, 28, 207, 191, 37, 198, 11, 137, 29, 134, 99, 176, 132,
128            59, 191, 33, 180, 34, 49, 180, 253, 241, 60, 0, 0, 0, 7, 0, 19, 204, 220, 47, 93, 61, 154, 62, 190, 6, 7,
129            76, 107, 73, 180, 144, 144, 221, 121, 202, 114, 224, 74, 191, 32, 241, 15, 135, 26, 216, 41, 59, 122, 225,
130            0, 114, 25, 221, 109, 248, 208, 189, 23, 229, 232, 113, 134, 209, 154, 197, 121, 222, 84, 21, 18, 147, 180,
131            111, 33, 93, 249, 6, 204, 9, 26, 237, 90, 63, 46, 154, 127, 209, 143, 213, 188, 44, 179, 7, 16, 7, 34, 236,
132            37, 72, 255, 227, 76, 214, 28, 226, 26, 172, 50, 134, 62, 2, 0, 69, 135, 4, 13, 224, 89, 7, 183, 8, 6, 200,
133            114, 91, 218, 225, 247, 55, 7, 133, 153, 59, 42, 19, 146, 8, 226, 71, 136, 93, 78, 209, 248, 82, 246, 16,
134            217, 225, 93, 30, 94, 42, 56, 146, 50, 115, 34, 65, 71, 64, 224, 194, 3, 214, 49, 48, 56, 208, 151, 197,
135            57, 199, 32, 180, 93, 252, 207, 59, 34, 51, 132, 123, 206, 223, 57, 161, 194, 183, 41, 94, 140, 69, 160,
136            132, 255, 227, 90, 71, 235, 62, 93, 68, 59, 220, 239, 57, 14, 0, 72, 138, 195, 251, 27, 141, 245, 239, 140,
137            74, 203, 78, 241, 243, 227, 208, 57, 197, 215, 25, 125, 184, 112, 148, 166, 26, 246, 99, 32, 114, 35, 19,
138            203, 209, 234, 117, 79, 52, 95, 178, 186, 163, 163, 159, 170, 181, 193, 3, 182, 201, 232, 216, 116, 93,
139            226, 76, 232, 36, 89, 29, 233, 5, 148, 181, 151, 178, 220, 239, 110, 156, 86, 130, 144, 246, 74, 26, 30,
140            236, 107, 221, 23, 137, 209, 176, 180, 103, 115, 225, 155, 13, 28, 244, 22, 239, 8, 13, 0, 97, 249, 95,
141            237, 48, 182, 233, 191, 11, 45, 3, 147, 143, 86, 211, 87, 137, 255, 127, 14, 161, 34, 208, 28, 92, 27, 126,
142            134, 149, 37, 226, 24, 56, 237, 87, 0, 183, 96, 184, 224, 155, 230, 148, 157, 39, 243, 29, 27, 81, 195,
143            174, 227, 154, 43, 171, 243, 96, 112, 165, 211, 36, 106, 128, 27, 250, 221, 229, 201, 27, 196, 48, 204,
144            181, 177, 52, 194, 228, 93, 199, 171, 145, 162, 168, 150, 223, 118, 5, 193, 191, 116, 67, 176, 103, 6, 144,
145            6, 0, 179, 180, 201, 32, 144, 151, 32, 186, 95, 124, 48, 221, 220, 15, 145, 105, 191, 130, 67, 181, 41,
146            182, 1, 252, 71, 118, 184, 203, 10, 140, 162, 83, 134, 51, 45, 102, 215, 241, 16, 125, 176, 111, 63, 214,
147            168, 199, 112, 168, 105, 0, 25, 67, 255, 97, 58, 143, 219, 230, 17, 215, 200, 128, 112, 90, 220, 93, 241,
148            80, 76, 206, 157, 200, 213, 240, 89, 195, 31, 8, 194, 33, 30, 18, 79, 140, 157, 224, 224, 67, 73, 172, 194,
149            64, 145, 164, 118, 0, 0, 189, 237, 1, 233, 58, 223, 122, 98, 49, 24, 253, 55, 95, 217, 61, 199, 215, 221,
150            242, 34, 50, 66, 57, 202, 227, 62, 78, 76, 71, 236, 59, 14, 154, 61, 180, 80, 240, 189, 219, 129, 80, 214,
151            131, 79, 250, 52, 200, 162, 28, 109, 179, 218, 110, 189, 14, 147, 73, 24, 82, 10, 196, 123, 202, 106, 236,
152            42, 166, 232, 18, 155, 99, 43, 173, 108, 151, 198, 155, 171, 129, 234, 233, 58, 16, 231, 104, 108, 59, 34,
153            215, 202, 244, 254, 137, 121, 118, 6, 0, 241, 143, 63, 106, 45, 148, 11, 155, 172, 211, 8, 71, 19, 246,
154            135, 125, 178, 32, 100, 173, 164, 51, 92, 181, 58, 225, 218, 117, 4, 79, 151, 141, 220, 110, 246, 198, 208,
155            240, 129, 72, 75, 125, 143, 175, 179, 148, 34, 93, 8, 191, 115, 17, 43, 131, 229, 248, 79, 213, 224, 190,
156            148, 117, 4, 49, 199, 71, 137, 238, 244, 142, 136, 193, 25, 99, 42, 171, 156, 93, 233, 59, 161, 12, 111,
157            255, 59, 211, 40, 133, 187, 207, 67, 194, 150, 109, 56, 15,
158        ];
159        let raw = Raw::<Payload>::from(data);
160        assert!(raw.inner_unverified().is_ok());
161    }
162}