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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::{core, packed};

/*
 * Blockchain
 */

impl<'r> packed::ScriptReader<'r> {
    pub fn check_data(&self) -> bool {
        core::ScriptHashType::verify_value(self.hash_type().into())
    }
}

impl<'r> packed::ScriptOptReader<'r> {
    pub fn check_data(&self) -> bool {
        self.to_opt()
            .map(|i| core::ScriptHashType::verify_value(i.hash_type().into()))
            .unwrap_or(true)
    }
}

impl<'r> packed::CellOutputReader<'r> {
    pub fn check_data(&self) -> bool {
        self.lock().check_data() && self.type_().check_data()
    }
}

impl<'r> packed::CellOutputVecReader<'r> {
    pub fn check_data(&self) -> bool {
        self.iter().all(|i| i.check_data())
    }
}

impl<'r> packed::CellDepReader<'r> {
    pub fn check_data(&self) -> bool {
        core::DepType::verify_value(self.dep_type().into())
    }
}

impl<'r> packed::CellDepVecReader<'r> {
    pub fn check_data(&self) -> bool {
        self.iter().all(|i| i.check_data())
    }
}

impl<'r> packed::RawTransactionReader<'r> {
    pub fn check_data(&self) -> bool {
        self.outputs().len() == self.outputs_data().len()
            && self.cell_deps().check_data()
            && self.outputs().check_data()
    }
}

impl<'r> packed::TransactionReader<'r> {
    pub fn check_data(&self) -> bool {
        self.raw().check_data()
    }
}

impl<'r> packed::TransactionVecReader<'r> {
    pub fn check_data(&self) -> bool {
        self.iter().all(|i| i.check_data())
    }
}

impl<'r> packed::BlockReader<'r> {
    pub fn check_data(&self) -> bool {
        self.transactions().check_data()
    }
}

/*
 * Network
 */

impl<'r> packed::BlockTransactionsReader<'r> {
    pub fn check_data(&self) -> bool {
        self.transactions().check_data()
    }
}

impl<'r> packed::RelayTransactionReader<'r> {
    pub fn check_data(&self) -> bool {
        self.transaction().check_data()
    }
}

impl<'r> packed::RelayTransactionVecReader<'r> {
    pub fn check_data(&self) -> bool {
        self.iter().all(|i| i.check_data())
    }
}

impl<'r> packed::RelayTransactionsReader<'r> {
    pub fn check_data(&self) -> bool {
        self.transactions().check_data()
    }
}

impl<'r> packed::SendBlockReader<'r> {
    pub fn check_data(&self) -> bool {
        self.block().check_data()
    }
}

#[cfg(test)]
mod tests {
    use crate::{packed, prelude::*};

    fn create_transaction(
        outputs: &[&packed::CellOutput],
        outputs_data: &[&[u8]],
        cell_deps: &[&packed::CellDep],
    ) -> packed::Transaction {
        let outputs = outputs
            .iter()
            .map(|d| d.to_owned().to_owned())
            .collect::<Vec<packed::CellOutput>>();
        let outputs_data = outputs_data
            .iter()
            .map(|d| d.to_owned().to_owned().pack())
            .collect::<Vec<packed::Bytes>>();
        let cell_deps = cell_deps
            .iter()
            .map(|d| d.to_owned().to_owned())
            .collect::<Vec<packed::CellDep>>();
        let raw = packed::RawTransaction::new_builder()
            .outputs(outputs.into_iter().pack())
            .outputs_data(outputs_data.into_iter().pack())
            .cell_deps(cell_deps.into_iter().pack())
            .build();
        packed::Transaction::new_builder().raw(raw).build()
    }

    fn test_check_data_via_transaction(
        expected: bool,
        outputs: &[&packed::CellOutput],
        outputs_data: &[&[u8]],
        cell_deps: &[&packed::CellDep],
    ) {
        let tx = create_transaction(outputs, outputs_data, cell_deps);
        assert_eq!(tx.as_reader().check_data(), expected);
    }

    #[test]
    fn check_data() {
        let ht_right = 1.into();
        let ht_error = 2.into();
        let dt_right = 1.into();
        let dt_error = 2.into();

        let script_right = packed::Script::new_builder().hash_type(ht_right).build();
        let script_error = packed::Script::new_builder().hash_type(ht_error).build();

        let script_opt_right = packed::ScriptOpt::new_builder()
            .set(Some(script_right.clone()))
            .build();
        let script_opt_error = packed::ScriptOpt::new_builder()
            .set(Some(script_error.clone()))
            .build();

        let output_right1 = packed::CellOutput::new_builder()
            .lock(script_right.clone())
            .build();
        let output_right2 = packed::CellOutput::new_builder()
            .type_(script_opt_right.clone())
            .build();
        let output_error1 = packed::CellOutput::new_builder()
            .lock(script_error.clone())
            .build();
        let output_error2 = packed::CellOutput::new_builder()
            .type_(script_opt_error.clone())
            .build();
        let output_error3 = packed::CellOutput::new_builder()
            .lock(script_right)
            .type_(script_opt_error)
            .build();
        let output_error4 = packed::CellOutput::new_builder()
            .lock(script_error)
            .type_(script_opt_right)
            .build();

        let cell_dep_right = packed::CellDep::new_builder().dep_type(dt_right).build();
        let cell_dep_error = packed::CellDep::new_builder().dep_type(dt_error).build();

        test_check_data_via_transaction(true, &[], &[], &[]);
        test_check_data_via_transaction(true, &[&output_right1], &[&[]], &[&cell_dep_right]);
        test_check_data_via_transaction(
            true,
            &[&output_right1, &output_right2],
            &[&[], &[]],
            &[&cell_dep_right, &cell_dep_right],
        );
        test_check_data_via_transaction(false, &[&output_error1], &[&[]], &[]);
        test_check_data_via_transaction(false, &[&output_error2], &[&[]], &[]);
        test_check_data_via_transaction(false, &[&output_error3], &[&[]], &[]);
        test_check_data_via_transaction(false, &[&output_error4], &[&[]], &[]);
        test_check_data_via_transaction(false, &[], &[], &[&cell_dep_error]);
        test_check_data_via_transaction(
            false,
            &[
                &output_right1,
                &output_right2,
                &output_error1,
                &output_error2,
                &output_error3,
                &output_error4,
            ],
            &[&[], &[], &[], &[], &[], &[]],
            &[&cell_dep_right, &cell_dep_error],
        );
        test_check_data_via_transaction(false, &[&output_right1], &[], &[&cell_dep_right]);
        test_check_data_via_transaction(false, &[], &[&[]], &[&cell_dep_right]);
    }
}