Skip to content

Commit

Permalink
Validate actions to lock script hashes (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong authored Mar 4, 2024
1 parent c9be032 commit 50c7adf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
49 changes: 25 additions & 24 deletions c/cobuild.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 { \
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand All @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions c/cobuild.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions tests/omni_lock/ckb_syscall_omni_lock_sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions tests/omni_lock/omni_lock_sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion tests/omni_lock_rust/tests/test_otx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down

0 comments on commit 50c7adf

Please sign in to comment.