forked from cryptape/omnilock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cobuild.c
893 lines (815 loc) · 28.5 KB
/
cobuild.c
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
// abbrev.
// * smh: signing message hash
// clang-format off
#define CKB_DECLARATION_ONLY
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifndef MOL2_EXIT
#define MOL2_EXIT ckb_exit
#endif
int ckb_exit(signed char);
#include "molecule2_reader.h"
#include "blockchain-api2.h"
#include "cobuild_basic_mol2.h"
#include "cobuild_top_level_mol2.h"
#include "cobuild.h"
#include "molecule2_verify.h"
#define BLAKE2_IMPL_H
#define BLAKE2_REF_C
#include "blake2b.h"
#undef BLAKE2_REF_C
#undef BLAKE2_IMPL_H
#include "ckb_consts.h"
#include "ckb_syscall_apis.h"
// clang-format on
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#define BLAKE2B_BLOCK_SIZE 32
#define MAX_SCRIPT_COUNT 512
#define CHECK2(cond, code) \
do { \
if (!(cond)) { \
printf("error at %s:%d, error code %d", __FILE__, __LINE__, code); \
err = code; \
ASSERT(0); \
goto exit; \
} \
} while (0)
#define CHECK(_code) \
do { \
int code = (_code); \
if (code != 0) { \
printf("error at %s:%d, error code %d", __FILE__, __LINE__, code); \
err = code; \
ASSERT(0); \
goto exit; \
} \
} while (0)
#define CHECK_LOOP(err) \
if (err == CKB_INDEX_OUT_OF_BOUND) { \
err = 0; \
break; \
} \
CHECK(err)
enum CobuildErrorCode {
// cobuild error code is from 110
ERROR_GENERAL = 110,
ERROR_HASH,
ERROR_NONEMPTY_WITNESS,
ERROR_SIGHASHALL_DUP,
ERROR_SIGHASHALL_NOSEAL,
ERROR_MESSAGE,
ERROR_TYPESCRIPT_MISSING,
ERROR_SEAL,
ERROR_FLOW,
ERROR_OTX_START_DUP,
ERROR_WRONG_OTX,
ERROR_NOT_COBUILD,
ERROR_NO_CALLBACK,
ERROR_MOL2_UNEXPECTED,
ERROR_OVERFLOW,
};
enum MessageCalculationFlow {
MessageCalculationFlowBlake2b = 0,
};
typedef struct OtxStart {
uint32_t start_input_cell;
uint32_t start_output_cell;
uint32_t start_cell_deps;
uint32_t start_header_deps;
} OtxStart;
typedef struct Otx {
uint32_t input_cells;
uint32_t output_cells;
uint32_t cell_deps;
uint32_t header_deps;
} Otx;
const char *PERSONAL_SIGHASH_ALL = "ckb-tcob-sighash";
const char *PERSONAL_SIGHASH_ALL_ONLY = "ckb-tcob-sgohash";
const char *PERSONAL_OTX = "ckb-tcob-otxhash";
#ifdef CKB_C_STDLIB_PRINTF
static void bin_to_hex(const uint8_t *source, uint8_t *dest, size_t len) {
const static uint8_t HEX_TABLE[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
for (int i = 0; i < len; i++) {
dest[i * 2] = HEX_TABLE[source[i] >> 4];
dest[i * 2 + 1] = HEX_TABLE[source[i] & 0x0F];
}
}
void print_raw_data(const char *name, uint8_t *data, size_t len) {
uint8_t str[924] = {0};
const int limit = (sizeof(str) - 1) / 2;
if (len > limit) {
printf("The data length (%d) is too long, truncated to %d", len, limit);
len = limit;
}
bin_to_hex(data, str, len);
printf("%s(len=%d): %s", name, len, str);
}
void print_cursor(const char *name, mol2_cursor_t cursor) {
uint8_t data[256] = {0};
uint32_t read_len = mol2_read_at(&cursor, data, sizeof(data));
if (read_len >= sizeof(data)) {
printf("the cursor length (%d) is too long, truncated to %d", cursor.size,
read_len);
}
print_raw_data(name, data, MIN(read_len, sizeof(data)));
}
// After being enabled, there will be a lot of logs.
// #define BLAKE2B_UPDATE blake2b_update_debug
#define BLAKE2B_UPDATE blake2b_update
int blake2b_update_debug(blake2b_state *S, const void *pin, size_t inlen) {
blake2b_update(S, pin, inlen);
print_raw_data("blake2b_update", (uint8_t *)pin, inlen);
return 0;
}
#else
void print_raw_data(const char *name, const uint8_t *data, size_t len) {}
void print_cursor(const char *name, mol2_cursor_t cursor) {}
#define BLAKE2B_UPDATE blake2b_update
#endif
static void store32(void *dst, uint32_t w) {
uint8_t *p = (uint8_t *)dst;
p[0] = (uint8_t)(w >> 0);
p[1] = (uint8_t)(w >> 8);
p[2] = (uint8_t)(w >> 16);
p[3] = (uint8_t)(w >> 24);
}
int ckb_blake2b_init_personal(blake2b_state *S, size_t outlen,
const char *personal) {
blake2b_param P[1];
if ((!outlen) || (outlen > BLAKE2B_OUTBYTES)) return -1;
P->digest_length = (uint8_t)outlen;
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
store32(&P->leaf_length, 0);
store32(&P->node_offset, 0);
store32(&P->xof_length, 0);
P->node_depth = 0;
P->inner_length = 0;
memset(P->reserved, 0, sizeof(P->reserved));
memset(P->salt, 0, sizeof(P->salt));
memset(P->personal, 0, sizeof(P->personal));
for (int i = 0; i < BLAKE2B_PERSONALBYTES; ++i) {
(P->personal)[i] = personal[i];
}
return blake2b_init_param(S, P);
}
int new_sighash_all_blake2b(blake2b_state *S) {
return ckb_blake2b_init_personal(S, 32, PERSONAL_SIGHASH_ALL);
}
int new_sighash_all_only_blake2b(blake2b_state *S) {
return ckb_blake2b_init_personal(S, 32, PERSONAL_SIGHASH_ALL_ONLY);
}
int new_otx_blake2b(blake2b_state *S) {
return ckb_blake2b_init_personal(S, 32, PERSONAL_OTX);
}
static inline int get_witness_layout(BytesVecType witnesses, uint32_t index,
WitnessLayoutType *witness_layout) {
bool existing = false;
mol2_cursor_t witness = witnesses.t->get(&witnesses, index, &existing);
if (!existing) {
return ERROR_MOL2_UNEXPECTED;
}
WitnessLayoutType witness_layout2 = make_WitnessLayout(&witness);
if (verify_WitnessLayout(&witness_layout2)) {
return ERROR_GENERAL;
}
if (witness_layout != NULL) {
*witness_layout = witness_layout2;
}
return 0;
}
// for lock script with message, the other witness in script group except first
// one should be empty
int ckb_check_others_in_group() {
int err = ERROR_GENERAL;
for (size_t index = 1;; index++) {
uint64_t witness_len = 0;
err = ckb_load_witness(0, &witness_len, 0, index, CKB_SOURCE_GROUP_INPUT);
CHECK_LOOP(err);
// tested by test_non_empty_witness
CHECK2(witness_len == 0, ERROR_NONEMPTY_WITNESS);
}
exit:
return err;
}
int ckb_fetch_sighash_message(BytesVecType witnesses, MessageType *message) {
int err = 0;
bool has_message = false;
uint32_t witness_len = witnesses.t->len(&witnesses);
for (uint32_t index = 0; index < witness_len; index++) {
WitnessLayoutType witness_layout = {0};
if (get_witness_layout(witnesses, index, &witness_layout) == 0) {
uint32_t id = witness_layout.t->item_id(&witness_layout);
if (id == WitnessLayoutSighashAll) {
// tested by:
// tested_by_sighashall_dup
CHECK2(!has_message, ERROR_SIGHASHALL_DUP);
SighashAllType s = witness_layout.t->as_SighashAll(&witness_layout);
*message = s.t->message(&s);
has_message = true;
}
}
// there are some possibilities:
// 1. an invalid witness (e.g. empty)
// 2. WitnessArgs
// 3. Other cobuild WitnessLayout(e.g. SighashAllOnly)
// tested by:
// tested_by_append_witnessed_less_than_4
// tested_by_append_witnessargs
// tested_by_append_other_witnesslayout
}
exit:
return err;
}
// step 2
static inline int ckb_fetch_otx_start(BytesVecType witnesses, bool *has_otx,
size_t *i, OtxStart *otx_start) {
int err = ERROR_GENERAL;
*has_otx = false;
uint32_t witness_len = witnesses.t->len(&witnesses);
for (uint32_t index = 0; index < witness_len; index++) {
WitnessLayoutType witness_layout = {0};
err = get_witness_layout(witnesses, index, &witness_layout);
if (err == 0) {
uint32_t id = witness_layout.t->item_id(&witness_layout);
if (id == WitnessLayoutOtxStart) {
// step 4
// test_cobuild_otx_double_otx_start
CHECK2(!*has_otx, ERROR_OTX_START_DUP);
*has_otx = true;
*i = index;
OtxStartType start = witness_layout.t->as_OtxStart(&witness_layout);
otx_start->start_input_cell = start.t->start_input_cell(&start);
otx_start->start_output_cell = start.t->start_output_cell(&start);
otx_start->start_cell_deps = start.t->start_cell_deps(&start);
otx_start->start_header_deps = start.t->start_header_deps(&start);
}
}
}
if (has_otx) {
err = 0;
}
exit:
return err;
}
// hash input cell, including CellOutput and cell data
static int hash_input_cell(blake2b_state *ctx, size_t index, size_t *count) {
// this data source is on stack. When this function returns, all cursors bound
// to this buffer become invalid.
uint8_t data_source[DEFAULT_DATA_SOURCE_LENGTH];
int err = 0;
// CellOutput
uint64_t cell_len = MAX_CACHE_SIZE;
err = ckb_load_cell(MOL2_CACHE_PTR(data_source), &cell_len, 0, index,
CKB_SOURCE_INPUT);
CHECK(err);
mol2_cursor_t cell_cursor = {0};
uint32_t cache_size = (uint32_t)cell_len;
if (cache_size > MAX_CACHE_SIZE) {
cache_size = MAX_CACHE_SIZE;
}
ckb_new_cursor_with_data(&cell_cursor, cell_len, read_from_cell, data_source,
MAX_CACHE_SIZE, index, CKB_SOURCE_INPUT, cache_size);
ckb_hash_cursor(ctx, cell_cursor);
(*count) += cell_len;
// Cell data
uint64_t cell_data_len = MAX_CACHE_SIZE;
err = ckb_load_cell_data(MOL2_CACHE_PTR(data_source), &cell_data_len, 0,
index, CKB_SOURCE_INPUT);
CHECK(err);
mol2_cursor_t cell_data_cursor = {0};
cache_size = (uint32_t)cell_data_len;
if (cache_size > MAX_CACHE_SIZE) {
cache_size = MAX_CACHE_SIZE;
}
ckb_new_cursor_with_data(&cell_data_cursor, cell_data_len,
read_from_cell_data, data_source, MAX_CACHE_SIZE,
index, CKB_SOURCE_INPUT, cache_size);
// only hash as uint32_t. 4 bytes is enough
BLAKE2B_UPDATE(ctx, &cell_data_len, 4);
(*count) += 4;
err = ckb_hash_cursor(ctx, cell_data_cursor);
CHECK(err);
(*count) += cell_data_cursor.size;
exit:
return err;
}
int ckb_generate_smh(const Env *env, mol2_cursor_t message_cursor,
uint8_t *smh) {
bool has_message = message_cursor.size > 0;
int err = 0;
blake2b_state ctx;
size_t count = 0;
// use different hash based on message
if (has_message) {
// tested by test_input_cell_data_size_0
new_sighash_all_blake2b(&ctx);
ckb_hash_cursor(&ctx, message_cursor);
count += message_cursor.size;
} else {
// tested by:
// tested_by_no_has_message
new_sighash_all_only_blake2b(&ctx);
}
// hash tx hash
BLAKE2B_UPDATE(&ctx, env->tx_hash, sizeof(env->tx_hash));
count += sizeof(env->tx_hash);
TransactionType tx = env->tx;
RawTransactionType raw = tx.t->raw(&tx);
CellInputVecType inputs = raw.t->inputs(&raw);
uint32_t input_len = inputs.t->len(&inputs);
BytesVecType witnesses = tx.t->witnesses(&tx);
uint32_t witness_len = witnesses.t->len(&witnesses);
// hash input cell and data
for (uint32_t index = 0; index < input_len; index++) {
err = hash_input_cell(&ctx, index, &count);
CHECK(err);
}
// hash remaining witnesses
for (uint32_t index = input_len; index < witness_len; index++) {
bool existing = false;
mol2_cursor_t witness_cursor =
witnesses.t->get(&witnesses, index, &existing);
CHECK2(existing, ERROR_MOL2_UNEXPECTED);
uint32_t witness_len = witness_cursor.size;
BLAKE2B_UPDATE(&ctx, &witness_len, 4);
count += 4;
err = ckb_hash_cursor(&ctx, witness_cursor);
count += witness_cursor.size;
CHECK(err);
}
blake2b_final(&ctx, smh, BLAKE2B_BLOCK_SIZE);
printf("ckb_generate_smh total hashed %d bytes", count);
exit:
return err;
}
static int hash_cmp(const void *h1, const void *h2) {
return memcmp(h1, h2, BLAKE2B_BLOCK_SIZE);
}
static int collect_script_hash(uint8_t *script_hash,
uint32_t *script_hash_count, size_t source,
size_t field) {
int err = 0;
size_t i = 0;
while (1) {
uint8_t hash[BLAKE2B_BLOCK_SIZE] = {0};
uint64_t len = BLAKE2B_BLOCK_SIZE;
err = ckb_load_cell_by_field(hash, &len, 0, i, source, field);
if (err == CKB_INDEX_OUT_OF_BOUND) {
err = 0;
break;
}
if (err == CKB_ITEM_MISSING) {
i += 1;
continue;
}
CHECK(err);
CHECK2(*script_hash_count < MAX_SCRIPT_COUNT, ERROR_GENERAL);
memcpy(&script_hash[(*script_hash_count) * BLAKE2B_BLOCK_SIZE], hash,
BLAKE2B_BLOCK_SIZE);
(*script_hash_count)++;
i += 1;
}
exit:
return err;
}
// For each action in the message, ensure a corresponding type script hash
// (including input/output) matches the action.script_hash. Let A be the set of
// action.script_hash, and B be the set of all input/output script hashes; A ∈ B
// should be satisfied.
static int check_type_script_existing(MessageType msg) {
int err = 0;
// cache all type script hashes in input/output cells
static uint8_t script_hash[BLAKE2B_BLOCK_SIZE * MAX_SCRIPT_COUNT] = {0};
static uint32_t script_hash_count = 0;
static bool script_hash_initialized = false;
if (!script_hash_initialized) {
err = collect_script_hash(script_hash, &script_hash_count, CKB_SOURCE_INPUT,
CKB_CELL_FIELD_TYPE_HASH);
CHECK(err);
err = collect_script_hash(script_hash, &script_hash_count,
CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
CHECK(err);
err = collect_script_hash(script_hash, &script_hash_count, CKB_SOURCE_INPUT,
CKB_CELL_FIELD_LOCK_HASH);
CHECK(err);
// sort for fast searching
qsort(script_hash, script_hash_count, BLAKE2B_BLOCK_SIZE, hash_cmp);
script_hash_initialized = true;
}
ActionVecType actions = msg.t->actions(&msg);
uint32_t len = actions.t->len(&actions);
for (uint32_t i = 0; i < len; i++) {
bool existing = false;
ActionType action = actions.t->get(&actions, i, &existing);
CHECK2(existing, ERROR_GENERAL);
mol2_cursor_t hash = action.t->script_hash(&action);
uint8_t hash_buff[BLAKE2B_BLOCK_SIZE] = {0};
uint32_t len = mol2_read_at(&hash, hash_buff, BLAKE2B_BLOCK_SIZE);
CHECK2(len == BLAKE2B_BLOCK_SIZE, ERROR_MESSAGE);
void *found = bsearch(hash_buff, script_hash, script_hash_count,
BLAKE2B_BLOCK_SIZE, hash_cmp);
// test_cobuild_otx_noexistent_type_script_hash
CHECK2(found != NULL, ERROR_TYPESCRIPT_MISSING);
}
exit:
return err;
}
// Parse the `original_seal` and return underlying seal after adjustment. The
// first byte of `seal` is considered as an id of message calculation flow.
static int parse_seal(const mol2_cursor_t original_seal, mol2_cursor_t *seal,
uint8_t *message_calculation_flow) {
int err = 0;
uint32_t prefix_length = 1;
uint8_t prefix[1] = {0};
uint32_t len = mol2_read_at(&original_seal, prefix, prefix_length);
CHECK2(len == prefix_length, ERROR_SEAL);
*message_calculation_flow = prefix[0];
*seal = mol2_cursor_slice_start(&original_seal, prefix_length);
exit:
return err;
}
int ckb_cobuild_normal_entry(const Env *env, ScriptEntryType callback) {
TransactionType tx = env->tx;
BytesVecType witnesses = tx.t->witnesses(&tx);
int err = ERROR_GENERAL;
uint8_t smh[BLAKE2B_BLOCK_SIZE];
mol2_cursor_t seal = {0};
MessageType message = {0};
// step 8.a, 8.b
err = ckb_fetch_sighash_message(witnesses, &message);
CHECK(err);
bool has_message = message.cur.size > 0;
if (has_message) {
print_cursor("message", message.cur);
// step 8.c
err = check_type_script_existing(message);
CHECK(err);
}
uint8_t seal_source[DEFAULT_DATA_SOURCE_LENGTH];
mol2_cursor_t original_seal = {0};
{
// step 8.d
// step 8.f
mol2_cursor_t witness = {0};
err = ckb_new_witness_cursor(&witness, seal_source, MAX_CACHE_SIZE, 0,
CKB_SOURCE_GROUP_INPUT);
CHECK(err);
WitnessLayoutType witness_layout = make_WitnessLayout(&witness);
CHECK2(!verify_WitnessLayout(&witness_layout), ERROR_SIGHASHALL_NOSEAL);
uint32_t id = witness_layout.t->item_id(&witness_layout);
switch (id) {
case WitnessLayoutSighashAll: {
SighashAllType s = witness_layout.t->as_SighashAll(&witness_layout);
original_seal = s.t->seal(&s);
} break;
case WitnessLayoutSighashAllOnly: {
SighashAllOnlyType o =
witness_layout.t->as_SighashAllOnly(&witness_layout);
original_seal = o.t->seal(&o);
} break;
default: {
// the union id should be SighashAll or SighashAllOnly. otherwise, it
// fails and mark it as non cobuild. tested by test_wrong_union_id
printf("error in fetch_seal, id = %u", id);
CHECK2(false, ERROR_SIGHASHALL_NOSEAL);
} break;
}
}
print_cursor("seal", original_seal);
// step 8.e
err = ckb_check_others_in_group();
// tested by test_non_empty_witness
CHECK(err);
// support more message calculation flows base on the first byte of seal
uint8_t message_calculation_flow = 0;
err = parse_seal(original_seal, &seal, &message_calculation_flow);
CHECK(err);
if (message_calculation_flow == MessageCalculationFlowBlake2b) {
// step 8.g
err = ckb_generate_smh(env, message.cur, smh);
CHECK(err);
print_raw_data("smh", smh, BLAKE2B_BLOCK_SIZE);
} else {
// we can add more message calculation flows in the further, based on the
// first byte of seal
CHECK2(false, ERROR_FLOW);
}
err = callback(env, smh, seal);
if (err) {
printf("callback failed: %d", err);
// terminated immediately
ckb_exit(err);
}
exit:
return err;
}
int ckb_generate_otx_smh(const Env *env, mol2_cursor_t message_cursor,
uint8_t *smh, const OtxStart *start, const Otx *size) {
int err = 0;
blake2b_state ctx;
size_t count = 0;
new_otx_blake2b(&ctx);
printf(
"start_input_cell = %d, start_output_cell = %d, start_cell_deps = %d, "
"start_header_deps = %d",
start->start_input_cell, start->start_output_cell, start->start_cell_deps,
start->start_header_deps);
printf(
"input_cells = %d, output_cells = %d, cell_deps = %d, header_deps = %d",
size->input_cells, size->output_cells, size->cell_deps,
size->header_deps);
err = ckb_hash_cursor(&ctx, message_cursor);
CHECK(err);
count += message_cursor.size;
BLAKE2B_UPDATE(&ctx, &size->input_cells, 4);
count += 4;
TransactionType tx = env->tx;
RawTransactionType raw = tx.t->raw(&tx);
CellInputVecType inputs = raw.t->inputs(&raw);
// hash input cell and data
CHECK2(start->start_input_cell + size->input_cells >= start->start_input_cell,
ERROR_OVERFLOW);
for (size_t index = start->start_input_cell;
index < (start->start_input_cell + size->input_cells); index++) {
// CellInput
bool existing = false;
CellInputType input = inputs.t->get(&inputs, index, &existing);
CHECK2(existing, ERROR_MOL2_UNEXPECTED);
err = ckb_hash_cursor(&ctx, input.cur);
CHECK(err);
count += input.cur.size;
err = hash_input_cell(&ctx, index, &count);
CHECK(err);
}
// hash output cell and data
CHECK2(
start->start_output_cell + size->output_cells >= start->start_output_cell,
ERROR_OVERFLOW);
BLAKE2B_UPDATE(&ctx, &size->output_cells, 4);
count += 4;
CellOutputVecType outputs = raw.t->outputs(&raw);
BytesVecType outputs_data = raw.t->outputs_data(&raw);
for (size_t index = start->start_output_cell;
index < (start->start_output_cell + size->output_cells); index++) {
bool existing = false;
CellOutputType output = outputs.t->get(&outputs, index, &existing);
CHECK2(existing, ERROR_MOL2_UNEXPECTED);
err = ckb_hash_cursor(&ctx, output.cur);
CHECK(err);
count += output.cur.size;
existing = false;
mol2_cursor_t output_data_cursor =
outputs_data.t->get(&outputs_data, index, &existing);
CHECK2(existing, ERROR_MOL2_UNEXPECTED);
uint32_t data_len = output_data_cursor.size;
BLAKE2B_UPDATE(&ctx, &data_len, 4);
count += 4;
err = ckb_hash_cursor(&ctx, output_data_cursor);
CHECK(err);
count += output_data_cursor.size;
}
// hash cell deps
CHECK2(start->start_cell_deps + size->cell_deps >= start->start_cell_deps,
ERROR_OVERFLOW);
BLAKE2B_UPDATE(&ctx, &size->cell_deps, 4);
count += 4;
CellDepVecType cell_deps = raw.t->cell_deps(&raw);
for (size_t index = start->start_cell_deps;
index < (start->start_cell_deps + size->cell_deps); index++) {
bool existing = false;
CellDepType cell_dep = cell_deps.t->get(&cell_deps, index, &existing);
CHECK2(existing, ERROR_MOL2_UNEXPECTED);
err = ckb_hash_cursor(&ctx, cell_dep.cur);
count += cell_dep.cur.size;
}
// hash header deps
CHECK2(
start->start_header_deps + size->header_deps >= start->start_header_deps,
ERROR_OVERFLOW);
BLAKE2B_UPDATE(&ctx, &size->header_deps, 4);
count += 4;
Byte32VecType header_deps = raw.t->header_deps(&raw);
for (size_t index = start->start_header_deps;
index < (start->start_header_deps + size->header_deps); index++) {
bool existing = false;
mol2_cursor_t header_dep_cursor =
header_deps.t->get(&header_deps, index, &existing);
CHECK2(existing, ERROR_MOL2_UNEXPECTED);
err = ckb_hash_cursor(&ctx, header_dep_cursor);
count += header_dep_cursor.size;
}
printf("ckb_generate_otx_smh totally hashed %d bytes", count);
blake2b_final(&ctx, smh, BLAKE2B_BLOCK_SIZE);
exit:
return err;
}
int ckb_cobuild_entry(const Env *env, ScriptEntryType callback,
bool *cobuild_enabled) {
int err = 0;
size_t execution_count = 0;
uint8_t smh[BLAKE2B_BLOCK_SIZE] = {0};
mol2_cursor_t original_seal = {0};
mol2_cursor_t seal = {0};
TransactionType tx = env->tx;
BytesVecType witnesses = tx.t->witnesses(&tx);
uint32_t witness_len = witnesses.t->len(&witnesses);
// Legacy Flow Handling
*cobuild_enabled = false;
for (uint32_t i = 0; i < witness_len; i++) {
if (get_witness_layout(witnesses, i, NULL) == 0) {
*cobuild_enabled = true;
break;
}
}
if (!*cobuild_enabled) {
goto exit;
}
// step 1
uint32_t is = 0, ie = 0, os = 0, oe = 0, cs = 0, ce = 0, hs = 0, he = 0;
size_t i = 0;
bool has_otx = false;
OtxStart otx_start = {0};
// step 2
// step 4
err = ckb_fetch_otx_start(witnesses, &has_otx, &i, &otx_start);
CHECK(err);
if (!has_otx) {
// step 3
printf("No otx detected");
return ckb_cobuild_normal_entry(env, callback);
}
// step 5
is = otx_start.start_input_cell;
ie = is;
os = otx_start.start_output_cell;
oe = os;
cs = otx_start.start_cell_deps;
ce = cs;
hs = otx_start.start_header_deps;
he = hs;
printf("ie = %d, oe = %d, ce = %d, he = %d", ie, oe, ce, he);
uint32_t index = i + 1;
printf("Otx starts at index %d(inclusive)", index);
for (; index < witness_len; index++) {
WitnessLayoutType witness_layout = {0};
err = get_witness_layout(witnesses, index, &witness_layout);
if (err != 0) {
// step 6, not WitnessLayoutOtx
break;
}
uint32_t id = witness_layout.t->item_id(&witness_layout);
if (id != WitnessLayoutOtx) {
// step 6
// test_cobuild_otx_noexistent_otx_id && err == 0
break;
}
OtxType otx = witness_layout.t->as_Otx(&witness_layout);
MessageType message = otx.t->message(&otx);
Otx size = {
.input_cells = otx.t->input_cells(&otx),
.output_cells = otx.t->output_cells(&otx),
.cell_deps = otx.t->cell_deps(&otx),
.header_deps = otx.t->header_deps(&otx),
};
// 6.b
if (size.input_cells == 0 && size.output_cells == 0 &&
size.cell_deps == 0 && size.header_deps == 0) {
// test_cobuild_otx_msg_size_all_0
CHECK2(false, ERROR_WRONG_OTX);
}
// step 6.c
err = check_type_script_existing(message);
CHECK(err);
// step 6.d
bool found = false;
size_t end = (size_t)(ie + otx.t->input_cells(&otx));
for (size_t index2 = ie; index2 < end; index2++) {
uint8_t hash[BLAKE2B_BLOCK_SIZE];
uint64_t len = BLAKE2B_BLOCK_SIZE;
err = ckb_load_cell_by_field(hash, &len, 0, index2, CKB_SOURCE_INPUT,
CKB_CELL_FIELD_LOCK_HASH);
CHECK(err);
if (memcmp(hash, env->current_script_hash, sizeof(hash)) == 0) {
found = true;
break;
}
}
if (!found) {
ie += otx.t->input_cells(&otx);
oe += otx.t->output_cells(&otx);
ce += otx.t->cell_deps(&otx);
he += otx.t->header_deps(&otx);
continue;
}
// step 6.e
OtxStart start = {
.start_input_cell = ie,
.start_output_cell = oe,
.start_cell_deps = ce,
.start_header_deps = he,
};
err = ckb_generate_otx_smh(env, message.cur, smh, &start, &size);
CHECK(err);
print_raw_data("smh", smh, BLAKE2B_BLOCK_SIZE);
// step 6.f
bool seal_found = false;
SealPairVecType seals = otx.t->seals(&otx);
uint32_t seal_len = seals.t->len(&seals);
for (uint32_t seal_index = 0; seal_index < seal_len; seal_index++) {
bool existing = false;
uint8_t hash[BLAKE2B_BLOCK_SIZE];
SealPairType loop_seal = seals.t->get(&seals, seal_index, &existing);
CHECK2(existing, ERROR_GENERAL);
mol2_cursor_t script_hash = loop_seal.t->script_hash(&loop_seal);
size_t len = mol2_read_at(&script_hash, hash, sizeof(hash));
CHECK2(len == sizeof(hash), ERROR_GENERAL);
if (memcmp(hash, env->current_script_hash, sizeof(hash)) == 0) {
// step 6.g
original_seal = loop_seal.t->seal(&loop_seal);
print_cursor("seal", original_seal);
// duplicated seals are ignored
seal_found = true;
break;
}
}
// test_cobuild_otx_no_seal
CHECK2(seal_found, ERROR_SEAL);
// support more message calculation flows base on the first byte of seal
uint8_t message_calculation_flow = 0;
err = parse_seal(original_seal, &seal, &message_calculation_flow);
CHECK(err);
if (message_calculation_flow == MessageCalculationFlowBlake2b) {
execution_count++;
err = callback(env, smh, seal);
if (err) {
printf("callback failed: %d", err);
// terminated immediately
ckb_exit(err);
}
} else {
// test_cobuild_otx_msg_flow
CHECK2(false, ERROR_FLOW);
}
// step 6.h
ie += otx.t->input_cells(&otx);
oe += otx.t->output_cells(&otx);
ce += otx.t->cell_deps(&otx);
he += otx.t->header_deps(&otx);
} // end of step 6 loop
printf("Otx ends at index %d(exclusive)", index);
// step 7
size_t j = index;
for (uint32_t index = 0; index < witness_len; index++) {
// [0, i) [j, +infinity)
if (index < i || index >= j) {
WitnessLayoutType witness_layout = {0};
err = get_witness_layout(witnesses, index, &witness_layout);
if (err == 0) {
// test_cobuild_otx_noexistent_otx_id
uint32_t id = witness_layout.t->item_id(&witness_layout);
CHECK2(id != WitnessLayoutOtx, ERROR_WRONG_OTX);
}
}
}
// step 8
bool found = false;
for (size_t index = 0;; index++) {
// scan all input cell in [0, is) and [ie, +infinity)
// if is == ie, it is always true
if (index < is || index >= ie) {
uint8_t hash[BLAKE2B_BLOCK_SIZE];
uint64_t len = BLAKE2B_BLOCK_SIZE;
err = ckb_load_cell_by_field(hash, &len, 0, index, CKB_SOURCE_INPUT,
CKB_CELL_FIELD_LOCK_HASH);
CHECK_LOOP(err);
if (memcmp(hash, env->current_script_hash, sizeof(hash)) == 0) {
printf(
"Same lock script found beyond otx, at index %d. "
"ckb_cobuild_normal_entry called.",
index);
found = true;
break;
}
}
}
if (found) {
printf("extra callback is invoked");
execution_count++;
err = ckb_cobuild_normal_entry(env, callback);
CHECK(err);
}
CHECK2(execution_count > 0, ERROR_NO_CALLBACK);
printf("execution_count = %d", execution_count);
exit:
return err;
}