axon_abi/
builder.rs

1use axon_tools::keccak_256;
2use axon_tools::types::{MetadataVersion, NodePubKey, ValidatorExtend, H256};
3
4use crate::image_cell_abi::{CellInfo, CellOutput, OutPoint, Script};
5use crate::light_client_abi::Header;
6use crate::metadata_abi::{self, CkbRelatedInfo, Metadata};
7
8#[derive(Default, Clone, Debug)]
9pub struct CkbHeaderBuilder {
10    version:           u32,
11    compact_target:    u32,
12    timestamp:         u64,
13    number:            u64,
14    epoch:             u64,
15    parent_hash:       [u8; 32],
16    transactions_root: [u8; 32],
17    proposals_hash:    [u8; 32],
18    extra_hash:        [u8; 32],
19    dao:               [u8; 32],
20    nonce:             u128,
21    extension:         Vec<u8>,
22    block_hash:        [u8; 32],
23}
24
25impl CkbHeaderBuilder {
26    pub fn build(self) -> Header {
27        Header {
28            version:           self.version,
29            compact_target:    self.compact_target,
30            timestamp:         self.timestamp,
31            number:            self.number,
32            epoch:             self.epoch,
33            parent_hash:       self.parent_hash,
34            transactions_root: self.transactions_root,
35            proposals_hash:    self.proposals_hash,
36            extra_hash:        self.extra_hash,
37            dao:               self.dao,
38            nonce:             self.nonce,
39            extension:         self.extension.into(),
40            block_hash:        self.block_hash,
41        }
42    }
43
44    pub fn version(mut self, version: u32) -> Self {
45        self.version = version;
46        self
47    }
48
49    pub fn compact_target(mut self, compact_target: u32) -> Self {
50        self.compact_target = compact_target;
51        self
52    }
53
54    pub fn timestamp(mut self, timestamp: u64) -> Self {
55        self.timestamp = timestamp;
56        self
57    }
58
59    pub fn number(mut self, number: u64) -> Self {
60        self.number = number;
61        self
62    }
63
64    pub fn epoch(mut self, epoch: u64) -> Self {
65        self.epoch = epoch;
66        self
67    }
68
69    pub fn parent_hash(mut self, parent_hash: [u8; 32]) -> Self {
70        self.parent_hash = parent_hash;
71        self
72    }
73
74    pub fn transactions_root(mut self, transactions_root: [u8; 32]) -> Self {
75        self.transactions_root = transactions_root;
76        self
77    }
78
79    pub fn proposals_hash(mut self, proposals_hash: [u8; 32]) -> Self {
80        self.proposals_hash = proposals_hash;
81        self
82    }
83
84    pub fn extra_hash(mut self, extra_hash: [u8; 32]) -> Self {
85        self.extra_hash = extra_hash;
86        self
87    }
88
89    pub fn dao(mut self, dao: [u8; 32]) -> Self {
90        self.dao = dao;
91        self
92    }
93
94    pub fn nonce(mut self, nonce: u128) -> Self {
95        self.nonce = nonce;
96        self
97    }
98
99    pub fn extension(mut self, extension: Vec<u8>) -> Self {
100        self.extension = extension;
101        self
102    }
103
104    pub fn block_hash(mut self, block_hash: [u8; 32]) -> Self {
105        self.block_hash = block_hash;
106        self
107    }
108}
109
110#[derive(Default, Clone, Debug)]
111pub struct OutPointBuilder {
112    tx_hash: [u8; 32],
113    index:   u32,
114}
115
116impl OutPointBuilder {
117    pub fn build(self) -> OutPoint {
118        OutPoint {
119            tx_hash: self.tx_hash,
120            index:   self.index,
121        }
122    }
123
124    pub fn tx_hash(mut self, tx_hash: [u8; 32]) -> Self {
125        self.tx_hash = tx_hash;
126        self
127    }
128
129    pub fn index(mut self, index: u32) -> Self {
130        self.index = index;
131        self
132    }
133}
134
135#[derive(Default, Clone, Debug)]
136pub struct ScriptBuilder {
137    code_hash: [u8; 32],
138    hash_type: u8,
139    args:      Vec<u8>,
140}
141
142impl ScriptBuilder {
143    pub fn build(self) -> Script {
144        Script {
145            code_hash: self.code_hash,
146            hash_type: self.hash_type,
147            args:      self.args.into(),
148        }
149    }
150
151    pub fn code_hash(mut self, code_hash: [u8; 32]) -> Self {
152        self.code_hash = code_hash;
153        self
154    }
155
156    pub fn hash_type(mut self, hash_type: u8) -> Self {
157        self.hash_type = hash_type;
158        self
159    }
160
161    pub fn args(mut self, args: Vec<u8>) -> Self {
162        self.args = args;
163        self
164    }
165}
166
167#[derive(Default, Clone, Debug)]
168pub struct CellOutputBuilder {
169    capacity: u64,
170    lock:     Script,
171    type_:    Option<Script>,
172}
173
174impl CellOutputBuilder {
175    pub fn build(self) -> CellOutput {
176        CellOutput {
177            capacity: self.capacity,
178            lock:     self.lock,
179            type_:    self.type_.map(|s| vec![s]).unwrap_or_default(),
180        }
181    }
182
183    pub fn capacity(mut self, capacity: u64) -> Self {
184        self.capacity = capacity;
185        self
186    }
187
188    pub fn lock(mut self, lock: Script) -> Self {
189        self.lock = lock;
190        self
191    }
192
193    pub fn type_(mut self, type_: Option<Script>) -> Self {
194        self.type_ = type_;
195        self
196    }
197}
198
199#[derive(Default, Clone, Debug)]
200pub struct CellInfoBuilder {
201    out_point: OutPoint,
202    output:    CellOutput,
203    data:      Vec<u8>,
204}
205
206impl CellInfoBuilder {
207    pub fn build(self) -> CellInfo {
208        CellInfo {
209            out_point: self.out_point,
210            output:    self.output,
211            data:      self.data.into(),
212        }
213    }
214
215    pub fn out_point(mut self, out_point: OutPoint) -> Self {
216        self.out_point = out_point;
217        self
218    }
219
220    pub fn output(mut self, output: CellOutput) -> Self {
221        self.output = output;
222        self
223    }
224
225    pub fn data(mut self, data: Vec<u8>) -> Self {
226        self.data = data;
227        self
228    }
229}
230
231#[derive(Clone)]
232pub struct MetadataBuilder {
233    version:         MetadataVersion,
234    epoch:           u64,
235    gas_limit:       u64,
236    gas_price:       u64,
237    interval:        u64,
238    verifier_list:   Vec<ValidatorExtend>,
239    propose_ratio:   u64,
240    prevote_ratio:   u64,
241    precommit_ratio: u64,
242    brake_ratio:     u64,
243    tx_num_limit:    u64,
244    max_tx_size:     u64,
245}
246
247impl Default for MetadataBuilder {
248    fn default() -> Self {
249        MetadataBuilder {
250            version:         MetadataVersion {
251                start: 1,
252                end:   100,
253            },
254            epoch:           0,
255            gas_limit:       4294967295000,
256            gas_price:       1,
257            interval:        3000,
258            verifier_list:   vec![],
259            propose_ratio:   15,
260            prevote_ratio:   10,
261            precommit_ratio: 10,
262            brake_ratio:     10,
263            tx_num_limit:    2000,
264            max_tx_size:     409600000,
265        }
266    }
267}
268
269impl MetadataBuilder {
270    pub fn build(self) -> Metadata {
271        let verifiers = self
272            .verifier_list
273            .iter()
274            .map(|v| metadata_abi::ValidatorExtend {
275                bls_pub_key:    v.bls_pub_key.clone().into(),
276                pub_key:        v.pub_key.clone().into(),
277                address:        v.address,
278                propose_weight: v.propose_weight,
279                vote_weight:    v.vote_weight,
280            })
281            .collect::<Vec<_>>();
282
283        Metadata {
284            version:         metadata_abi::MetadataVersion {
285                start: self.version.start,
286                end:   self.version.end,
287            },
288            epoch:           self.epoch,
289            gas_limit:       self.gas_limit,
290            gas_price:       self.gas_price,
291            interval:        self.interval,
292            verifier_list:   verifiers,
293            propose_ratio:   self.propose_ratio,
294            prevote_ratio:   self.prevote_ratio,
295            precommit_ratio: self.precommit_ratio,
296            brake_ratio:     self.brake_ratio,
297            tx_num_limit:    self.tx_num_limit,
298            max_tx_size:     self.max_tx_size,
299            propose_counter: vec![],
300        }
301    }
302
303    pub fn version(mut self, start: u64, end: u64) -> Self {
304        self.version = MetadataVersion { start, end };
305        self
306    }
307
308    pub fn epoch(mut self, epoch: u64) -> Self {
309        self.epoch = epoch;
310        self
311    }
312
313    pub fn gas_limit(mut self, gas_limit: u64) -> Self {
314        self.gas_limit = gas_limit;
315        self
316    }
317
318    pub fn gas_price(mut self, gas_price: u64) -> Self {
319        self.gas_price = gas_price;
320        self
321    }
322
323    pub fn interval(mut self, interval: u64) -> Self {
324        self.interval = interval;
325        self
326    }
327
328    pub fn verifier_list(mut self, public_keys: Vec<NodePubKey>) -> Self {
329        self.verifier_list = public_keys
330            .iter()
331            .map(|pk| ValidatorExtend {
332                bls_pub_key:    pk.bls_pub_key.clone(),
333                pub_key:        pk.pub_key.clone(),
334                address:        H256(keccak_256(&pk.pub_key)).into(),
335                propose_weight: 1,
336                vote_weight:    1,
337            })
338            .collect();
339
340        self
341    }
342
343    pub fn propose_ratio(mut self, propose_ratio: u64) -> Self {
344        self.propose_ratio = propose_ratio;
345        self
346    }
347
348    pub fn prevote_ratio(mut self, prevote_ratio: u64) -> Self {
349        self.prevote_ratio = prevote_ratio;
350        self
351    }
352
353    pub fn precommit_ratio(mut self, precommit_ratio: u64) -> Self {
354        self.precommit_ratio = precommit_ratio;
355        self
356    }
357
358    pub fn brake_ratio(mut self, brake_ratio: u64) -> Self {
359        self.brake_ratio = brake_ratio;
360        self
361    }
362
363    pub fn tx_num_limit(mut self, tx_num_limit: u64) -> Self {
364        self.tx_num_limit = tx_num_limit;
365        self
366    }
367
368    pub fn max_tx_size(mut self, max_tx_size: u64) -> Self {
369        self.max_tx_size = max_tx_size;
370        self
371    }
372}
373
374#[derive(Default)]
375pub struct CkbRelatedInfoBuilder {
376    metadata_type_id:     [u8; 32],
377    checkpoint_type_id:   [u8; 32],
378    xudt_args:            [u8; 32],
379    stake_smt_type_id:    [u8; 32],
380    delegate_smt_type_id: [u8; 32],
381    reward_smt_type_id:   [u8; 32],
382}
383
384impl CkbRelatedInfoBuilder {
385    pub fn build(self) -> CkbRelatedInfo {
386        CkbRelatedInfo {
387            metadata_type_id:     self.metadata_type_id,
388            checkpoint_type_id:   self.checkpoint_type_id,
389            xudt_args:            self.xudt_args,
390            stake_smt_type_id:    self.stake_smt_type_id,
391            delegate_smt_type_id: self.delegate_smt_type_id,
392            reward_smt_type_id:   self.reward_smt_type_id,
393        }
394    }
395
396    pub fn metadata_type_id(mut self, metadata_type_id: [u8; 32]) -> Self {
397        self.metadata_type_id = metadata_type_id;
398        self
399    }
400
401    pub fn checkpoint_type_id(mut self, checkpoint_type_id: [u8; 32]) -> Self {
402        self.checkpoint_type_id = checkpoint_type_id;
403        self
404    }
405
406    pub fn xudt_args(mut self, xudt_args: [u8; 32]) -> Self {
407        self.xudt_args = xudt_args;
408        self
409    }
410
411    pub fn stake_smt_type_id(mut self, stake_smt_type_id: [u8; 32]) -> Self {
412        self.stake_smt_type_id = stake_smt_type_id;
413        self
414    }
415
416    pub fn delegate_smt_type_id(mut self, delegate_smt_type_id: [u8; 32]) -> Self {
417        self.delegate_smt_type_id = delegate_smt_type_id;
418        self
419    }
420
421    pub fn reward_smt_type_id(mut self, reward_smt_type_id: [u8; 32]) -> Self {
422        self.reward_smt_type_id = reward_smt_type_id;
423        self
424    }
425}