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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#[cfg(feature = "std")]
use super::Transaction;

#[cfg(feature = "std")]
use fuel_types::bytes::SizedBytes;

use fuel_types::Bytes32;

use alloc::vec::Vec;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Metadata {
    id: Bytes32,
    script_data_offset: Option<usize>,
    input_predicate_offset: Vec<Option<(usize, usize)>>,
    inputs_offset: Vec<usize>,
    outputs_offset: Vec<usize>,
    witnesses_offset: Vec<usize>,
}

impl Metadata {
    pub const fn new(
        id: Bytes32,
        script_data_offset: Option<usize>,
        input_predicate_offset: Vec<Option<(usize, usize)>>,
        inputs_offset: Vec<usize>,
        outputs_offset: Vec<usize>,
        witnesses_offset: Vec<usize>,
    ) -> Self {
        Self {
            id,
            script_data_offset,
            input_predicate_offset,
            inputs_offset,
            outputs_offset,
            witnesses_offset,
        }
    }

    pub const fn id(&self) -> &Bytes32 {
        &self.id
    }

    pub fn script_data_offset(&self) -> Option<usize> {
        self.script_data_offset
    }

    pub fn input_predicate_offset(&self, index: usize) -> Option<(usize, usize)> {
        self.input_predicate_offset.get(index).copied().flatten()
    }

    pub fn inputs_offset(&self, index: usize) -> Option<usize> {
        self.inputs_offset.get(index).copied()
    }

    pub fn outputs_offset(&self, index: usize) -> Option<usize> {
        self.outputs_offset.get(index).copied()
    }

    pub fn witnesses_offset(&self, index: usize) -> Option<usize> {
        self.witnesses_offset.get(index).copied()
    }
}

#[cfg(feature = "std")]
impl Transaction {
    fn metadata_mut(&mut self) -> &mut Option<Metadata> {
        match self {
            Self::Script { metadata, .. } => metadata,
            Self::Create { metadata, .. } => metadata,
        }
    }

    pub fn precompute_metadata(&mut self) {
        let id = self._id();

        let script_data_offset = self._script_data_offset();
        let input_predicate_offset = self
            .inputs()
            .iter()
            .enumerate()
            .map(|(i, _)| self._input_predicate_offset(i))
            .collect();

        let offset = self.inputs_offset();
        let inputs_offset = self
            .inputs()
            .iter()
            .scan(offset, |offset, input| {
                let i = *offset;
                *offset += input.serialized_size();

                Some(i)
            })
            .collect();

        let offset = self.outputs_offset();
        let outputs_offset = self
            .outputs()
            .iter()
            .scan(offset, |offset, output| {
                let i = *offset;
                *offset += output.serialized_size();

                Some(i)
            })
            .collect();

        let offset = self.witnesses_offset();
        let witnesses_offset = self
            .witnesses()
            .iter()
            .scan(offset, |offset, witness| {
                let i = *offset;
                *offset += witness.serialized_size();

                Some(i)
            })
            .collect();

        let metadata = Metadata::new(
            id,
            script_data_offset,
            input_predicate_offset,
            inputs_offset,
            outputs_offset,
            witnesses_offset,
        );

        self.metadata_mut().replace(metadata);
    }
}