From 17fbc549b46577909983bcd873cf1b6b93a2a1b5 Mon Sep 17 00:00:00 2001 From: xjd Date: Mon, 4 Mar 2024 09:56:22 +0800 Subject: [PATCH] Validate actions to lock script hashes --- Makefile | 2 +- c/cobuild.c | 49 +++++++++++---------- c/cobuild.h | 7 ++- tests/omni_lock/ckb_syscall_omni_lock_sim.h | 6 ++- tests/omni_lock/omni_lock_sim.c | 7 +-- tests/omni_lock_rust/tests/test_otx.rs | 7 ++- 6 files changed, 45 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index cb242e3..74786b0 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ ALL_C_SOURCE := $(wildcard c/omni_lock.c c/omni_lock_acp.h c/omni_lock_time_lock c/blake2b_decl_only.h c/cobuild.h c/cobuild.c c/molecule2_verify.h) fmt: - docker run --rm -v `pwd`:/code ${CLANG_FORMAT_DOCKER} bash -c "cd code && clang-format -i -style=Google $(ALL_C_SOURCE)" + docker run --rm -v `pwd`:/code ${CLANG_FORMAT_DOCKER} bash -c "cd code && clang-format -i -style='{BasedOnStyle: google, SortIncludes: false}' $(ALL_C_SOURCE)" git diff --exit-code $(ALL_C_SOURCE) mol: diff --git a/c/cobuild.c b/c/cobuild.c index 483cff5..532bff1 100644 --- a/c/cobuild.c +++ b/c/cobuild.c @@ -37,7 +37,7 @@ int ckb_exit(signed char); #endif #define BLAKE2B_BLOCK_SIZE 32 -#define MAX_TYPESCRIPT_COUNT 512 +#define MAX_SCRIPT_COUNT 512 #define CHECK2(cond, code) \ do { \ @@ -401,16 +401,15 @@ static int hash_cmp(const void *h1, const void *h2) { return memcmp(h1, h2, BLAKE2B_BLOCK_SIZE); } -static int collect_type_script_hash(uint8_t *type_script_hash, - uint32_t *type_script_hash_count, - size_t source) { +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, - CKB_CELL_FIELD_TYPE_HASH); + err = ckb_load_cell_by_field(hash, &len, 0, i, source, field); if (err == CKB_INDEX_OUT_OF_BOUND) { err = 0; break; @@ -420,10 +419,10 @@ static int collect_type_script_hash(uint8_t *type_script_hash, continue; } CHECK(err); - CHECK2(*type_script_hash_count < MAX_TYPESCRIPT_COUNT, ERROR_GENERAL); - memcpy(&type_script_hash[(*type_script_hash_count) * BLAKE2B_BLOCK_SIZE], - hash, BLAKE2B_BLOCK_SIZE); - (*type_script_hash_count)++; + 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: @@ -437,23 +436,25 @@ static int collect_type_script_hash(uint8_t *type_script_hash, static int check_type_script_existing(MessageType msg) { int err = 0; // cache all type script hashes in input/output cells - static uint8_t type_script_hash[BLAKE2B_BLOCK_SIZE * MAX_TYPESCRIPT_COUNT] = { - 0}; - static uint32_t type_script_hash_count = 0; - static int type_script_hash_initialized = 0; - - if (type_script_hash_initialized == 0) { - err = collect_type_script_hash(type_script_hash, &type_script_hash_count, - CKB_SOURCE_INPUT); + 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_type_script_hash(type_script_hash, &type_script_hash_count, - CKB_SOURCE_OUTPUT); + err = collect_script_hash(script_hash, &script_hash_count, CKB_SOURCE_INPUT, + CKB_CELL_FIELD_LOCK_HASH); CHECK(err); + // sort for fast searching - qsort(type_script_hash, type_script_hash_count, BLAKE2B_BLOCK_SIZE, - hash_cmp); + qsort(script_hash, script_hash_count, BLAKE2B_BLOCK_SIZE, hash_cmp); - type_script_hash_initialized = 1; + script_hash_initialized = true; } ActionVecType actions = msg.t->actions(&msg); @@ -466,7 +467,7 @@ static int check_type_script_existing(MessageType msg) { 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, type_script_hash, type_script_hash_count, + 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); diff --git a/c/cobuild.h b/c/cobuild.h index 3a87ef5..a6cb923 100644 --- a/c/cobuild.h +++ b/c/cobuild.h @@ -8,8 +8,11 @@ #include "molecule2_reader.h" #include "mol2_utils.h" -typedef int (*ScriptEntryType)(const Env* env, const uint8_t* signing_message_hash, mol2_cursor_t seal); -int ckb_cobuild_entry(const Env* env, ScriptEntryType entry, bool* cobuild_enabled); +typedef int (*ScriptEntryType)(const Env* env, + const uint8_t* signing_message_hash, + mol2_cursor_t seal); +int ckb_cobuild_entry(const Env* env, ScriptEntryType entry, + bool* cobuild_enabled); int ckb_cobuild_normal_entry(const Env* env, ScriptEntryType entry); #endif diff --git a/tests/omni_lock/ckb_syscall_omni_lock_sim.h b/tests/omni_lock/ckb_syscall_omni_lock_sim.h index d77d2a6..e2dc25a 100644 --- a/tests/omni_lock/ckb_syscall_omni_lock_sim.h +++ b/tests/omni_lock/ckb_syscall_omni_lock_sim.h @@ -370,12 +370,14 @@ void convert_setting_to_states(void) { { mol_builder_t witness_builder; MolBuilder_BytesVec_init(&witness_builder); - MolBuilder_BytesVec_push(&witness_builder, g_states.witness[0].ptr, g_states.witness[0].size); + MolBuilder_BytesVec_push(&witness_builder, g_states.witness[0].ptr, + g_states.witness[0].size); mol_seg_res_t witness_res = MolBuilder_BytesVec_build(witness_builder); mol_builder_t tx_builder; MolBuilder_Transaction_init(&tx_builder); - MolBuilder_Transaction_set_witnesses(&tx_builder, witness_res.seg.ptr, witness_res.seg.size); + MolBuilder_Transaction_set_witnesses(&tx_builder, witness_res.seg.ptr, + witness_res.seg.size); mol_seg_res_t tx_res = MolBuilder_Transaction_build(tx_builder); g_states.transaction.ptr = tx_res.seg.ptr; diff --git a/tests/omni_lock/omni_lock_sim.c b/tests/omni_lock/omni_lock_sim.c index bbea603..6110f6d 100644 --- a/tests/omni_lock/omni_lock_sim.c +++ b/tests/omni_lock/omni_lock_sim.c @@ -16,9 +16,10 @@ void debug_print_hex(const char* prefix, const uint8_t* buf, size_t length) { printf("\n"); } -int ckb_cobuild_entry(const Env* env, ScriptEntryType entry, bool* cobuild_enabled) { - (void) env; - (void) entry; +int ckb_cobuild_entry(const Env* env, ScriptEntryType entry, + bool* cobuild_enabled) { + (void)env; + (void)entry; *cobuild_enabled = false; return 0; diff --git a/tests/omni_lock_rust/tests/test_otx.rs b/tests/omni_lock_rust/tests/test_otx.rs index d568bb1..53a6259 100644 --- a/tests/omni_lock_rust/tests/test_otx.rs +++ b/tests/omni_lock_rust/tests/test_otx.rs @@ -819,7 +819,12 @@ fn generate_otx_d0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra .script_hash(px.create_script(&cell_meta_always_success, &[]).calc_script_hash()) .data(ckb_types::bytes::Bytes::from(vec![0x42; 128]).pack()) .build(); - let action_vec = schemas::basic::ActionVec::new_builder().push(action).build(); + let action2 = schemas::basic::Action::new_builder() + .script_info_hash(ckb_types::packed::Byte32::from_slice(&[0x00; 32]).unwrap()) + .script_hash(px.create_script(&cell_meta_omni_lock, &args).calc_script_hash()) + .data(ckb_types::bytes::Bytes::from(vec![0x42; 128]).pack()) + .build(); + let action_vec = schemas::basic::ActionVec::new_builder().push(action).push(action2).build(); let msgs = schemas::basic::Message::new_builder().actions(action_vec).build(); msgs };