ckb-cli 1.4.0

ckb command line interface
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use crate::utils::other::calculate_type_id;
use ckb_chain_spec::consensus::TYPE_ID_CODE_HASH;
use ckb_sdk::constants::ONE_CKB;
use ckb_types::{
    bytes::Bytes,
    core::{Capacity, ScriptHashType},
    packed,
    prelude::*,
    H256,
};
use serde_derive::{Deserialize, Serialize};

use super::deployment::{Cell, CellRecipe, DepGroup, DepGroupRecipe};

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum StateChange<C, R> {
    Changed {
        // New data
        data: Bytes,
        data_hash: H256,
        config: C,
        old_recipe: R,
        old_type_id_args: Option<Bytes>,
        output_index: u64,
    },
    NewAdded {
        data: Bytes,
        data_hash: H256,
        config: C,
        output_index: u64,
    },
    Unchanged {
        data: Bytes,
        data_hash: H256,
        config: C,
        old_recipe: R,
        old_type_id_args: Option<Bytes>,
    },
    Removed {
        old_recipe: R,
    },
    Reference {
        config: C,
        tx_hash: H256,
        index: u32,
    },
}

impl<C, R> StateChange<C, R> {
    fn has_new_output(&self) -> bool {
        match self {
            StateChange::Changed { .. } => true,
            StateChange::NewAdded { .. } => true,
            StateChange::Removed { .. } => false,
            StateChange::Unchanged { .. } => false,
            StateChange::Reference { .. } => false,
        }
    }
}

#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub struct ReprStateChange {
    pub name: String,
    pub kind: String,
    pub old_capacity: u64,
    pub new_capacity: u64,
}

pub type CellChange = StateChange<Cell, CellRecipe>;
pub type DepGroupChange = StateChange<DepGroup, DepGroupRecipe>;

pub trait ChangeInfo {
    fn name(&self) -> &String;
    fn to_repr(&self, lock_script: &packed::Script) -> ReprStateChange;
    fn has_new_output(&self) -> bool;
    fn has_new_recipe(&self) -> bool;
    fn occupied_capacity(&self, lock_script: &packed::Script) -> u64;
    fn build_input(&self) -> Option<(packed::CellInput, u64)>;
    fn build_cell_output(
        &self,
        lock_script: &packed::Script,
        first_cell_input: &packed::CellInput,
    ) -> Option<(packed::CellOutput, Bytes)>;
}

impl ChangeInfo for CellChange {
    fn name(&self) -> &String {
        match self {
            StateChange::Changed { config, .. } => &config.name,
            StateChange::NewAdded { config, .. } => &config.name,
            StateChange::Unchanged { config, .. } => &config.name,
            StateChange::Reference { config, .. } => &config.name,
            StateChange::Removed { old_recipe } => &old_recipe.name,
        }
    }

    fn to_repr(&self, lock_script: &packed::Script) -> ReprStateChange {
        let new_capacity = self.occupied_capacity(lock_script);
        let (kind, old_capacity) = match self {
            StateChange::Changed { old_recipe, .. } => ("Changed", old_recipe.occupied_capacity),
            StateChange::NewAdded { .. } => ("NewAdded", 0),
            StateChange::Unchanged { .. } => ("Unchanged", new_capacity),
            StateChange::Reference { .. } => ("Reference", 0),
            StateChange::Removed { old_recipe } => ("Removed", old_recipe.occupied_capacity),
        };
        ReprStateChange {
            name: self.name().clone(),
            kind: kind.to_string(),
            old_capacity,
            new_capacity,
        }
    }

    fn has_new_output(&self) -> bool {
        StateChange::has_new_output(self)
    }

    fn has_new_recipe(&self) -> bool {
        !matches!(
            self,
            StateChange::Removed { .. } | StateChange::Reference { .. }
        )
    }

    fn occupied_capacity(&self, lock_script: &packed::Script) -> u64 {
        let (data, config) = match self {
            StateChange::Removed { .. } => return 0,
            StateChange::Reference { .. } => return 0,
            StateChange::Changed { data, config, .. } => (data, config),
            StateChange::Unchanged { data, config, .. } => (data, config),
            StateChange::NewAdded { data, config, .. } => (data, config),
        };
        let data_size = data.len() as u64;
        let type_script_size: u64 = if config.enable_type_id {
            32 + 1 + 32
        } else {
            0
        };
        lock_script.occupied_capacity().expect("capacity").as_u64()
            + (type_script_size + data_size + 8) * ONE_CKB
    }

    fn build_input(&self) -> Option<(packed::CellInput, u64)> {
        match self {
            StateChange::Changed { old_recipe, .. } => {
                let out_point = packed::OutPoint::new(old_recipe.tx_hash.pack(), old_recipe.index);
                let input = packed::CellInput::new(out_point, 0);
                Some((input, old_recipe.occupied_capacity))
            }
            _ => None,
        }
    }

    fn build_cell_output(
        &self,
        lock_script: &packed::Script,
        first_cell_input: &packed::CellInput,
    ) -> Option<(packed::CellOutput, Bytes)> {
        let (data, config, output_index, old_type_id_args) = match self {
            StateChange::Removed { .. } => return None,
            StateChange::Unchanged { .. } => return None,
            StateChange::Reference { .. } => return None,
            StateChange::Changed {
                data,
                config,
                old_type_id_args,
                output_index,
                ..
            } => (data, config, *output_index, old_type_id_args.clone()),
            StateChange::NewAdded {
                data,
                config,
                output_index,
                ..
            } => (data, config, *output_index, None),
        };
        let type_id_args = if config.enable_type_id {
            old_type_id_args.or_else(|| {
                Some(Bytes::from(
                    calculate_type_id(first_cell_input, output_index).to_vec(),
                ))
            })
        } else {
            None
        };
        let occupied_capacity = self.occupied_capacity(lock_script);
        let type_script_opt = type_id_args.map(|type_id_args| {
            packed::Script::new_builder()
                .code_hash(TYPE_ID_CODE_HASH.pack())
                .hash_type(ScriptHashType::Type.into())
                .args(Bytes::from(type_id_args.to_vec()).pack())
                .build()
        });
        let output = packed::CellOutput::new_builder()
            .capacity(Capacity::shannons(occupied_capacity).pack())
            .lock(lock_script.clone())
            .type_(
                packed::ScriptOpt::new_builder()
                    .set(type_script_opt)
                    .build(),
            )
            .build();
        Some((output, data.clone()))
    }
}

impl CellChange {
    pub fn build_new_recipe(
        &self,
        lock_script: &packed::Script,
        first_cell_input: &packed::CellInput,
        new_tx_hash: &H256,
    ) -> Option<CellRecipe> {
        let (tx_hash, index, data_hash, config, old_type_id_args) = match self {
            StateChange::Removed { .. } => return None,
            StateChange::Reference { .. } => return None,
            StateChange::Changed {
                data_hash,
                config,
                old_type_id_args,
                output_index,
                ..
            } => (
                new_tx_hash.clone(),
                *output_index as u32,
                data_hash.clone(),
                config,
                old_type_id_args.clone(),
            ),
            StateChange::Unchanged {
                data_hash,
                config,
                old_recipe,
                old_type_id_args,
                ..
            } => (
                old_recipe.tx_hash.clone(),
                old_recipe.index,
                data_hash.clone(),
                config,
                old_type_id_args.clone(),
            ),
            StateChange::NewAdded {
                data_hash,
                config,
                output_index,
                ..
            } => (
                new_tx_hash.clone(),
                *output_index as u32,
                data_hash.clone(),
                config,
                None,
            ),
        };
        let type_id = if config.enable_type_id {
            let args = old_type_id_args.unwrap_or_else(|| {
                Bytes::from(calculate_type_id(first_cell_input, index as u64).to_vec())
            });
            let type_script = packed::Script::new_builder()
                .code_hash(TYPE_ID_CODE_HASH.pack())
                .hash_type(ScriptHashType::Type.into())
                .args(Bytes::from(args.to_vec()).pack())
                .build();
            Some(type_script.calc_script_hash().unpack())
        } else {
            None
        };
        Some(CellRecipe {
            name: self.name().clone(),
            // To be replaced with final transaction hash
            tx_hash,
            index,
            occupied_capacity: self.occupied_capacity(lock_script),
            data_hash,
            type_id,
        })
    }
}

impl ChangeInfo for DepGroupChange {
    fn name(&self) -> &String {
        match self {
            StateChange::Changed { config, .. } => &config.name,
            StateChange::NewAdded { config, .. } => &config.name,
            StateChange::Unchanged { config, .. } => &config.name,
            StateChange::Reference { config, .. } => &config.name,
            StateChange::Removed { old_recipe } => &old_recipe.name,
        }
    }

    fn to_repr(&self, lock_script: &packed::Script) -> ReprStateChange {
        let new_capacity = self.occupied_capacity(lock_script);
        let (kind, old_capacity) = match self {
            StateChange::Changed { old_recipe, .. } => ("Changed", old_recipe.occupied_capacity),
            StateChange::NewAdded { .. } => ("NewAdded", 0),
            StateChange::Unchanged { .. } => ("Unchanged", new_capacity),
            StateChange::Reference { .. } => ("Reference", 0),
            StateChange::Removed { old_recipe } => ("Removed", old_recipe.occupied_capacity),
        };
        ReprStateChange {
            name: self.name().clone(),
            kind: kind.to_string(),
            old_capacity,
            new_capacity,
        }
    }

    fn has_new_output(&self) -> bool {
        StateChange::has_new_output(self)
    }

    fn has_new_recipe(&self) -> bool {
        !matches!(
            self,
            StateChange::Removed { .. } | StateChange::Reference { .. }
        )
    }

    fn occupied_capacity(&self, lock_script: &packed::Script) -> u64 {
        let data = match self {
            StateChange::Removed { .. } => return 0,
            StateChange::Changed { data, .. } => data,
            StateChange::Unchanged { data, .. } => data,
            StateChange::Reference { .. } => return 0,
            StateChange::NewAdded { data, .. } => data,
        };
        let data_size = data.len() as u64;
        lock_script.occupied_capacity().expect("capacity").as_u64() + (data_size + 8) * ONE_CKB
    }

    fn build_input(&self) -> Option<(packed::CellInput, u64)> {
        match self {
            StateChange::Changed { old_recipe, .. } => {
                let out_point = packed::OutPoint::new(old_recipe.tx_hash.pack(), old_recipe.index);
                let input = packed::CellInput::new(out_point, 0);
                Some((input, old_recipe.occupied_capacity))
            }
            _ => None,
        }
    }

    fn build_cell_output(
        &self,
        lock_script: &packed::Script,
        _first_cell_input: &packed::CellInput,
    ) -> Option<(packed::CellOutput, Bytes)> {
        let data = match self {
            StateChange::Removed { .. } => return None,
            StateChange::Unchanged { .. } => return None,
            StateChange::Reference { .. } => return None,
            StateChange::Changed { data, .. } => data,
            StateChange::NewAdded { data, .. } => data,
        };
        let occupied_capacity = self.occupied_capacity(lock_script);
        let output = packed::CellOutput::new_builder()
            .capacity(Capacity::shannons(occupied_capacity).pack())
            .lock(lock_script.clone())
            .build();
        Some((output, data.clone()))
    }
}

impl DepGroupChange {
    pub fn build_new_recipe(
        &self,
        lock_script: &packed::Script,
        new_tx_hash: H256,
    ) -> Option<DepGroupRecipe> {
        let (tx_hash, index, data_hash) = match self {
            StateChange::Removed { .. } => {
                return None;
            }
            StateChange::Changed {
                data_hash,
                output_index,
                ..
            } => (new_tx_hash, *output_index as u32, data_hash.clone()),
            StateChange::Unchanged {
                data_hash,
                old_recipe,
                ..
            } => (
                old_recipe.tx_hash.clone(),
                old_recipe.index,
                data_hash.clone(),
            ),
            StateChange::Reference { .. } => {
                return None;
            }
            StateChange::NewAdded {
                data_hash,
                output_index,
                ..
            } => (new_tx_hash, *output_index as u32, data_hash.clone()),
        };
        Some(DepGroupRecipe {
            name: self.name().clone(),
            // To be replaced with final transaction hash
            tx_hash,
            index,
            data_hash,
            occupied_capacity: self.occupied_capacity(lock_script),
        })
    }
}