diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 93f9822..4b832b0 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -23,7 +23,3 @@ jobs: run: cd tests/xudt_rce_rust && cargo test - name: Run xudt simulator tests run: cd tests/xudt_rce && bash run.sh - - name: Run omni_lock tests - run: cd tests/omni_lock_rust && cargo test - - name: Run omni_lock simulator tests - run: cd tests/omni_lock && bash run.sh diff --git a/Makefile b/Makefile index b25b3f7..52dbf9c 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,6 @@ SECP256K1_SRC_20210801 := deps/secp256k1-20210801/src/ecmult_static_pre_context. XUDT_RCE_CFLAGS=$(subst ckb-c-std-lib,ckb-c-stdlib-20210713,$(CFLAGS)) -I deps/sparse-merkle-tree/c -OMNI_LOCK_CFLAGS :=$(subst ckb-c-std-lib,ckb-c-stdlib-20210801,$(CFLAGS)) -I deps/sparse-merkle-tree/c -OMNI_LOCK_CFLAGS := $(subst secp256k1,secp256k1-20210801,$(OMNI_LOCK_CFLAGS)) - PROTOCOL_HEADER := c/blockchain.h PROTOCOL_SCHEMA := c/blockchain.mol PROTOCOL_VERSION := d75e4c56ffa40e17fd2fe477da3f98c5578edcd1 @@ -28,7 +25,8 @@ PASSED_MBEDTLS_CFLAGS := -O3 -fPIC -nostdinc -nostdlib -DCKB_DECLARATION_ONLY -I BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:aae8a3f79705f67d505d1f1d5ddc694a4fd537ed1c7e9622420a470d59ba2ec3 CLANG_FORMAT_DOCKER := kason223/clang-format@sha256:3cce35b0400a7d420ec8504558a02bdfc12fd2d10e40206f140c4545059cd95d -all: build/simple_udt build/anyone_can_pay build/always_success build/validate_signature_rsa build/xudt_rce build/rce_validator build/omni_lock +all: build/simple_udt build/anyone_can_pay build/always_success build/validate_signature_rsa build/xudt_rce build/rce_validator \ + $(SECP256K1_SRC_20210801) build/secp256k1_data_info_20210801.h all-via-docker: ${PROTOCOL_HEADER} docker run --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code && make" @@ -112,9 +110,7 @@ validate_signature_rsa_clean: ${PROTOCOL_SCHEMA}: curl -L -o $@ ${PROTOCOL_URL} -ALL_C_SOURCE := $(wildcard c/omni_lock.c c/omni_lock_acp.h c/omni_lock_time_lock.h \ - tests/omni_lock/omni_lock_sim.c tests/omni_lock/ckb_syscall_omni_lock_sim.h tests/omni_lock/omni_lock_supply.h\ - c/rce_validator.c /always_success.c c/rce.h c/xudt_rce.c \ +ALL_C_SOURCE := $(wildcard c/rce_validator.c /always_success.c c/rce.h c/xudt_rce.c \ c/rce_validator.c tests/xudt_rce/*.c tests/xudt_rce/*.h\ c/validate_signature_rsa.h c/validate_signature_rsa.c) @@ -129,7 +125,6 @@ mol: make c/xudt_rce_mol.h make c/xudt_rce_mol2.h make tests/xudt_rce_rust/src/xudt_rce_mol.rs - make omni_lock_mol tests/xudt_rce_rust/src/xudt_rce_mol.rs: c/xudt_rce.mol ${MOLC} --language rust --schema-file $< | rustfmt > $@ @@ -141,11 +136,6 @@ c/xudt_rce_mol2.h: c/xudt_rce.mol moleculec --language - --schema-file c/xudt_rce.mol --format json > build/blockchain_mol2.json moleculec-c2 --input build/blockchain_mol2.json | clang-format -style=Google > c/xudt_rce_mol2.h -omni_lock_mol: - ${MOLC} --language rust --schema-file c/omni_lock.mol | rustfmt > tests/omni_lock_rust/src/omni_lock.rs - ${MOLC} --language c --schema-file c/omni_lock.mol > c/omni_lock_mol.h - ${MOLC} --language - --schema-file c/omni_lock.mol --format json > build/omni_lock_mol2.json - moleculec-c2 --input build/omni_lock_mol2.json | clang-format -style=Google > c/omni_lock_mol2.h build/xudt_rce: c/xudt_rce.c c/rce.h $(CC) $(XUDT_RCE_CFLAGS) $(LDFLAGS) -o $@ $< @@ -157,10 +147,6 @@ build/rce_validator: c/rce_validator.c c/rce.h $(OBJCOPY) --only-keep-debug $@ $@.debug $(OBJCOPY) --strip-debug --strip-all $@ -build/omni_lock: c/omni_lock.c c/omni_lock_supply.h c/omni_lock_acp.h c/rce.h c/secp256k1_lock.h build/secp256k1_data_info_20210801.h $(SECP256K1_SRC_20210801) c/ckb_identity.h - $(CC) $(OMNI_LOCK_CFLAGS) $(LDFLAGS) -o $@ $< - cp $@ $@.debug - $(OBJCOPY) --strip-debug --strip-all $@ publish: git diff --exit-code Cargo.toml @@ -190,7 +176,6 @@ clean: rm -rf build/*.debug rm -f build/xudt_rce rm -f build/rce_validator - rm -f build/omni_lock cd deps/secp256k1 && [ -f "Makefile" ] && make clean cd deps/secp256k1-20210801 && [ -f "Makefile" ] && make clean make -C deps/mbedtls/library clean diff --git a/README.md b/README.md index 1b7fd77..a3b4747 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,19 @@ # ckb production scripts CKB scripts used in production. + +### Build + +``` +git submodule update --init --recursive +make all-via-docker +``` + +### RFC and Deployment + +* [sUDT](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0025-simple-udt/0025-simple-udt.md) +* [Anyone Can Pay](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0026-anyone-can-pay/0026-anyone-can-pay.md) +* [xUDT](https://github.com/nervosnetwork/rfcs/pull/428) + +### Omnilock +It is moved to a [new repo](https://github.com/cryptape/omnilock). diff --git a/build.rs b/build.rs index c86c201..af95f5e 100644 --- a/build.rs +++ b/build.rs @@ -37,10 +37,6 @@ const BINARIES: &[(&str, &str)] = &[ "rce_validator", "d9476d96e0f5d41eca88bbc5e8c819ac38592a33285a4828a58d17873f278b3c", ), - ( - "omni_lock", - "86ea7ee58a0ecacfb1f7f2675a06d96223e0597dfe06a2474f9c06a72a7ccabc", - ), ]; fn main() { diff --git a/c/omni_lock.c b/c/omni_lock.c deleted file mode 100644 index 7199bfc..0000000 --- a/c/omni_lock.c +++ /dev/null @@ -1,459 +0,0 @@ -// uncomment to enable printf in CKB-VM -// #define CKB_C_STDLIB_PRINTF - -// it's used by blockchain-api2.h, the behavior when panic -#ifndef MOL2_EXIT -#define MOL2_EXIT ckb_exit -#endif -int ckb_exit(signed char); -// in secp256k1_ctz64_var: we don't have __builtin_ctzl in gcc for RISC-V -#define __builtin_ctzl secp256k1_ctz64_var_debruijn - -// clang-format off -#include -#include "blockchain-api2.h" -#define MOLECULEC_VERSION 7000 -#include "blockchain.h" -#include "ckb_consts.h" - -#if defined(CKB_USE_SIM) -// exclude ckb_dlfcn.h -#define CKB_C_STDLIB_CKB_DLFCN_H_ -#include "ckb_syscall_omni_lock_sim.h" -#else -#include "ckb_syscalls.h" -#endif -// secp256k1_helper_20210801.h is not part of ckb-c-stdlib, can't be included in ckb_identity.h -// An upgraded version is provided. -#include "secp256k1_helper_20210801.h" -#include "ckb_swappable_signatures.h" -#include "validate_signature_rsa.h" - -#include "ckb_identity.h" -#include "ckb_smt.h" - -// CHECK is defined in secp256k1 -#undef CHECK -#include "rce.h" -#include "omni_lock_mol2.h" - -#include "omni_lock_acp.h" -#include "omni_lock_time_lock.h" -#include "omni_lock_supply.h" - -// clang-format on - -#define SCRIPT_SIZE 32768 -#define MAX_LOCK_SCRIPT_HASH_COUNT 2048 -#define MAX_SIGNATURE_SIZE 1024 -#define OMNI_ROOT_MASK 1 -#define ACP_MASK (1 << 1) -#define SINCE_MASK (1 << 2) -#define SUPPLY_MASK (1 << 3) - -#define MAX_CODE_SIZE (1024 * 400) - -enum OmniLockErrorCode { - // omni lock error code is starting from 80 - ERROR_UNKNOWN_FLAGS = 80, - ERROR_PROOF_LENGTH_MISMATCHED, - ERROR_NO_OMNIRULE, - ERROR_NO_WHITE_LIST, - ERROR_INVALID_IDENTITY_ID, - ERROR_INVALID_OMNI_LOCK_ARGS, - ERROR_ISO9796_2_VERIFY, - ERROR_ARGS_FORMAT, -}; - -// parsed from args in lock script -typedef struct ArgsType { - CkbIdentityType id; - - uint8_t omni_lock_flags; - - bool has_omni_root; - uint8_t omni_root[32]; - - bool has_since; - uint64_t since; - - bool has_acp; - int ckb_minimum; // Used for ACP - int udt_minimum; // used for ACP - - bool has_supply; - uint8_t info_cell[32]; // type script hash -} ArgsType; - -// parsed from lock in witness -typedef struct WitnessLockType { - bool has_identity; - bool has_signature; - bool has_proofs; - - CkbIdentityType id; - uint32_t signature_size; - uint8_t signature[MAX_SIGNATURE_SIZE]; - uint32_t preimage_size; - uint8_t preimage[MAX_PREIMAGE_SIZE]; - - SmtProofEntryVecType proofs; -} WitnessLockType; - -// make compiler happy -int make_cursor_from_witness(WitnessArgsType *witness, bool *_input) { - return -1; -} - -// -// move cur by offset within seg. -// return NULL if out of bounds. -uint8_t *safe_move_to(mol_seg_t seg, uint8_t *cur, uint32_t offset) { - uint8_t *end = seg.ptr + seg.size; - - if (cur < seg.ptr || cur >= end) { - return NULL; - } - uint8_t *next = cur + offset; - if (next < seg.ptr || next >= end) { - return NULL; - } - return next; -} - -bool is_memory_enough(mol_seg_t seg, const uint8_t *cur, uint32_t len) { - uint8_t *end = seg.ptr + seg.size; - - if (cur < seg.ptr || cur >= end) { - return false; - } - const uint8_t *next = cur + len; - // == end is allowed - if (next < seg.ptr || next > end) { - return false; - } - return true; -} - -// memory layout of args: -// -// -int parse_args(ArgsType *args) { - int err = 0; - uint8_t script[SCRIPT_SIZE]; - uint64_t len = SCRIPT_SIZE; - err = ckb_checked_load_script(script, &len, 0); - CHECK(err); - - mol_seg_t script_seg; - script_seg.ptr = script; - script_seg.size = (mol_num_t)len; - - mol_errno mol_err = MolReader_Script_verify(&script_seg, false); - CHECK2(mol_err == MOL_OK, ERROR_ENCODING); - - mol_seg_t args_seg = MolReader_Script_get_args(&script_seg); - mol_seg_t seg = MolReader_Bytes_raw_bytes(&args_seg); - - uint8_t *cur = seg.ptr; - - // parse flags - CHECK2(is_memory_enough(seg, cur, 1), ERROR_ARGS_FORMAT); - uint8_t flags = *cur; - args->id.flags = flags; - cur = safe_move_to(seg, cur, 1); - CHECK2(cur != NULL, ERROR_ARGS_FORMAT); - - // parse blake160 - CHECK2(is_memory_enough(seg, cur, 20), ERROR_ARGS_FORMAT); - memcpy(args->id.id, cur, BLAKE160_SIZE); - cur = safe_move_to(seg, cur, 20); - CHECK2(cur != NULL, ERROR_ARGS_FORMAT); - - CHECK2(is_memory_enough(seg, cur, 1), ERROR_ARGS_FORMAT); - args->omni_lock_flags = *cur; - cur = safe_move_to(seg, cur, 1); - - args->has_omni_root = args->omni_lock_flags & OMNI_ROOT_MASK; - args->has_acp = args->omni_lock_flags & ACP_MASK; - args->has_since = args->omni_lock_flags & SINCE_MASK; - args->has_supply = args->omni_lock_flags & SUPPLY_MASK; - uint32_t expected_size = 0; - if (args->has_omni_root) { - expected_size += 32; - } - if (args->has_acp) { - expected_size += 2; - } - if (args->has_since) { - expected_size += 8; - } - if (args->has_supply) { - expected_size += 32; - } - - if (expected_size == 0) { - CHECK2(cur == NULL, ERROR_ARGS_FORMAT); - } else { - CHECK2(cur != NULL, ERROR_ARGS_FORMAT); - CHECK2(is_memory_enough(seg, cur, expected_size), ERROR_ARGS_FORMAT); - if (args->has_omni_root) { - memcpy(args->omni_root, cur, 32); - cur += 32; // it's safe to move, already checked - } - if (args->has_acp) { - args->ckb_minimum = cur[0]; - args->udt_minimum = cur[1]; - cur += 2; - } - if (args->has_since) { - args->since = *(uint64_t *)cur; - cur += 8; - } - if (args->has_supply) { - memcpy(args->info_cell, cur, 32); - cur += 32; - } - CHECK2(cur == (seg.ptr + seg.size), ERROR_INVALID_MOL_FORMAT); - } - -exit: - return err; -} - -static uint32_t read_from_witness(uintptr_t arg[], uint8_t *ptr, uint32_t len, - uint32_t offset) { - int err; - uint64_t output_len = len; - err = ckb_load_witness(ptr, &output_len, offset, arg[0], arg[1]); - if (err != 0) { - return 0; - } - if (output_len > len) { - return len; - } else { - return (uint32_t)output_len; - } -} - -uint8_t g_witness_data_source[DEFAULT_DATA_SOURCE_LENGTH]; -int make_witness(WitnessArgsType *witness) { - int err = 0; - uint64_t witness_len = 0; - size_t source = CKB_SOURCE_GROUP_INPUT; - err = ckb_load_witness(NULL, &witness_len, 0, 0, source); - // when witness is missing, empty or not accessible, make it zero length. - // don't fail, because owner lock without omni doesn't require witness. - // when it's zero length, any further actions on witness will fail. - if (err != 0) { - witness_len = 0; - } - - mol2_cursor_t cur; - - cur.offset = 0; - cur.size = (mol_num_t)witness_len; - - mol2_data_source_t *ptr = (mol2_data_source_t *)g_witness_data_source; - - ptr->read = read_from_witness; - ptr->total_size = (uint32_t)witness_len; - // pass index and source as args - ptr->args[0] = 0; - ptr->args[1] = source; - - ptr->cache_size = 0; - ptr->start_point = 0; - ptr->max_cache_size = MAX_CACHE_SIZE; - cur.data_source = ptr; - - *witness = make_WitnessArgs(&cur); - - return 0; -} - -int smt_verify_identity(CkbIdentityType *id, SmtProofEntryVecType *proofs, - RceState *rce_state) { - int err = 0; - uint32_t proof_len = proofs->t->len(proofs); - CHECK2(proof_len == rce_state->rcrules_count, ERROR_PROOF_LENGTH_MISMATCHED); - - uint8_t key[SMT_KEY_BYTES] = {0}; - key[0] = id->flags; - memcpy(key + 1, id->id, BLAKE160_SIZE); - - smt_pair_t entries[1]; - smt_state_t states; - smt_state_init(&states, entries, 1); - smt_state_insert(&states, key, SMT_VALUE_EMPTY); - - uint8_t proof_mask = 0x3; // both - for (uint32_t i = 0; i < proof_len; i++) { - bool existing = false; - SmtProofEntryType proof_entry = proofs->t->get(proofs, i, &existing); - CHECK2(existing, ERROR_INVALID_MOL_FORMAT); - mol2_cursor_t proof = proof_entry.t->proof(&proof_entry); - - const RCRule *current_rule = &rce_state->rcrules[i]; - err = rce_verify_one_rule(rce_state, &states, NULL, NULL, proof_mask, proof, - current_rule); - CHECK(err); - } - if (rce_state->has_wl) { - if (rce_state->both_on_wl) { - err = 0; - } else { - err = ERROR_NOT_ON_WHITE_LIST; - } - } else { - // all black list, it's not allowed - err = ERROR_NO_WHITE_LIST; - } -exit: - return err; -} - -int parse_witness_lock(WitnessLockType *witness_lock) { - int err = 0; - witness_lock->has_signature = false; - witness_lock->has_identity = false; - witness_lock->has_proofs = false; - - bool witness_existing = false; - - WitnessArgsType witness_args; - err = make_witness(&witness_args); - CHECK(err); - witness_existing = witness_args.cur.size > 0; - - // witness or witness lock can be empty if owner lock without omni is used - if (!witness_existing) return err; - - BytesOptType mol_lock = witness_args.t->lock(&witness_args); - if (mol_lock.t->is_none(&mol_lock)) return err; - - mol2_cursor_t mol_lock_bytes = mol_lock.t->unwrap(&mol_lock); - // convert Bytes to OmniLockWitnessLock - OmniLockWitnessLockType mol_witness_lock = - make_OmniLockWitnessLock(&mol_lock_bytes); - IdentityOptType identity_opt = - mol_witness_lock.t->omni_identity(&mol_witness_lock); - witness_lock->has_identity = identity_opt.t->is_some(&identity_opt); - if (witness_lock->has_identity) { - IdentityType omni_identity = identity_opt.t->unwrap(&identity_opt); - mol2_cursor_t id_cur = omni_identity.t->identity(&omni_identity); - - uint8_t buff[CKB_IDENTITY_LEN] = {0}; - uint32_t read_len = mol2_read_at(&id_cur, buff, sizeof(buff)); - CHECK2(read_len == CKB_IDENTITY_LEN, ERROR_INVALID_MOL_FORMAT); - witness_lock->id.flags = buff[0]; - memcpy(witness_lock->id.id, buff + 1, CKB_IDENTITY_LEN - 1); - - witness_lock->proofs = omni_identity.t->proofs(&omni_identity); - witness_lock->has_proofs = true; - } - - BytesOptType signature_opt = mol_witness_lock.t->signature(&mol_witness_lock); - witness_lock->has_signature = signature_opt.t->is_some(&signature_opt); - if (witness_lock->has_signature) { - mol2_cursor_t signature_cursor = signature_opt.t->unwrap(&signature_opt); - witness_lock->signature_size = mol2_read_at( - &signature_cursor, witness_lock->signature, signature_cursor.size); - CHECK2(signature_cursor.size == witness_lock->signature_size, - ERROR_INVALID_MOL_FORMAT); - } - BytesOptType preimage_opt = mol_witness_lock.t->preimage(&mol_witness_lock); - if (preimage_opt.t->is_some(&preimage_opt)) { - mol2_cursor_t preimage_cursor = preimage_opt.t->unwrap(&preimage_opt); - witness_lock->preimage_size = mol2_read_at( - &preimage_cursor, witness_lock->preimage, preimage_cursor.size); - CHECK2(preimage_cursor.size == witness_lock->preimage_size, - ERROR_INVALID_MOL_FORMAT); - } else { - witness_lock->preimage_size = 0; - } - -exit: - return err; -} - -#ifdef CKB_USE_SIM -int simulator_main() { -#else -int main() { -#endif - // don't move code_buff into global variable. It doesn't work. - // it's a ckb-vm bug: the global variable will be freezed: - // https://github.com/nervosnetwork/ckb-vm/blob/d43f58d6bf8cc6210721fdcdb6e5ecba513ade0c/src/machine/elf_adaptor.rs#L28-L32 - // The code can't be loaded into frozen memory. - uint8_t code_buff[MAX_CODE_SIZE] __attribute__((aligned(RISCV_PGSIZE))); - - int err = 0; - - WitnessLockType witness_lock = {0}; - ArgsType args = {0}; - // this identity can be either from witness lock (witness_lock.id) or script - // args (args.id) - CkbIdentityType identity = {0}; - - err = parse_witness_lock(&witness_lock); - CHECK(err); - - err = parse_args(&args); - CHECK(err); - - if (args.has_omni_root) { - if (witness_lock.has_identity) { - identity = witness_lock.id; - } else { - identity = args.id; - } - } else { - identity = args.id; - } - - // regulation compliance, also as administrators - if (witness_lock.has_identity) { - CHECK2(args.has_omni_root, ERROR_INVALID_MOL_FORMAT); - CHECK2(witness_lock.has_proofs, ERROR_INVALID_MOL_FORMAT); - - RceState rce_state; - rce_init_state(&rce_state); - rce_state.rcrules_in_input_cell = true; - err = rce_gather_rcrules_recursively(&rce_state, args.omni_root, 0); - CHECK(err); - CHECK2(rce_state.rcrules_count > 0, ERROR_NO_OMNIRULE); - CHECK2(rce_state.has_wl, ERROR_NO_WHITE_LIST); - - // verify blake160 against proof, using omni rules - err = smt_verify_identity(&identity, &witness_lock.proofs, &rce_state); - CHECK(err); - } else { - // time lock is not used for administrators - if (args.has_since) { - err = check_since(args.since); - CHECK(err); - } - if (args.has_supply) { - err = check_supply(args.info_cell); - CHECK(err); - } - // ACP without signature is not used for administrators - if (args.has_acp && !witness_lock.has_signature) { - uint64_t min_ckb_amount = 0; - uint128_t min_udt_amount = 0; - process_amount(args.ckb_minimum, args.udt_minimum, &min_ckb_amount, - &min_udt_amount); - // skip checking identity to follow ACP - return check_payment_unlock(min_ckb_amount, min_udt_amount); - } - } - ckb_identity_init_code_buffer(code_buff, MAX_CODE_SIZE); - err = ckb_verify_identity(&identity, witness_lock.signature, - witness_lock.signature_size, witness_lock.preimage, - witness_lock.preimage_size); - CHECK(err); -exit: - return err; -} diff --git a/c/omni_lock.mol b/c/omni_lock.mol deleted file mode 100644 index fa32706..0000000 --- a/c/omni_lock.mol +++ /dev/null @@ -1,16 +0,0 @@ -import xudt_rce; - -array Auth[byte; 21]; - -table Identity { - identity: Auth, - proofs: SmtProofEntryVec, -} -option IdentityOpt (Identity); - -// the data structure used in lock field of witness -table OmniLockWitnessLock { - signature: BytesOpt, - omni_identity: IdentityOpt, - preimage: BytesOpt, -} \ No newline at end of file diff --git a/c/omni_lock_acp.h b/c/omni_lock_acp.h deleted file mode 100644 index 58fb755..0000000 --- a/c/omni_lock_acp.h +++ /dev/null @@ -1,267 +0,0 @@ -/* UDT anyone-can-pay lock script - * For simplify, we call a cell with anyone-can-pay lock a wallet cell. - * - * Wallet cell can be unlocked without a signature, if: - * - * 1. There is 1 output wallet cell that has the same type hash with the - * unlocked wallet cell. - * 2. The UDT or CKB(if type script is none) in the output wallet is more than - * the unlocked wallet. - * 3. if the type script is none, the cell data is empty. - * - * otherwise, the script perform secp256k1_blake160_sighash_all verification. - */ - -#include "defs.h" -#include "overflow_add.h" -#include "quick_pow10.h" - -#define BLAKE2B_BLOCK_SIZE 32 -#define SCRIPT_SIZE 32768 -#define CKB_LEN 8 -#define UDT_LEN 16 -#define MAX_WITNESS_SIZE 32768 -#define MAX_TYPE_HASH 256 - -/* anyone can pay errors */ -#define ERROR_WITNESS_SIZE -22 -#define ERROR_OVERFLOW -41 -#define ERROR_OUTPUT_AMOUNT_NOT_ENOUGH -42 -#define ERROR_TOO_MUCH_TYPE_HASH_INPUTS -43 -#define ERROR_NO_PAIR -44 -#define ERROR_DUPLICATED_INPUTS -45 -#define ERROR_DUPLICATED_OUTPUTS -46 - -typedef struct { - int is_ckb_only; - unsigned char type_hash[BLAKE2B_BLOCK_SIZE]; - uint64_t ckb_amount; - uint128_t udt_amount; - uint32_t output_cnt; -} InputWallet; - -int check_payment_unlock(uint64_t min_ckb_amount, uint128_t min_udt_amount) { - unsigned char lock_hash[BLAKE2B_BLOCK_SIZE]; - InputWallet input_wallets[MAX_TYPE_HASH]; - uint64_t len = BLAKE2B_BLOCK_SIZE; - /* load wallet lock hash */ - int ret = ckb_load_script_hash(lock_hash, &len, 0); - if (ret != CKB_SUCCESS) { - return ERROR_SYSCALL; - } - if (len > BLAKE2B_BLOCK_SIZE) { - return ERROR_SCRIPT_TOO_LONG; - } - - /* iterate inputs and find input wallet cell */ - int i = 0; - len = BLAKE2B_BLOCK_SIZE; - while (1) { - if (i >= MAX_TYPE_HASH) { - return ERROR_TOO_MUCH_TYPE_HASH_INPUTS; - } - - ret = ckb_checked_load_cell_by_field(input_wallets[i].type_hash, &len, 0, i, - CKB_SOURCE_GROUP_INPUT, - CKB_CELL_FIELD_TYPE_HASH); - if (ret == CKB_INDEX_OUT_OF_BOUND) { - break; - } - - if (ret == CKB_SUCCESS) { - if (len != BLAKE2B_BLOCK_SIZE) { - return ERROR_ENCODING; - } - } else if (ret != CKB_ITEM_MISSING) { - return ERROR_SYSCALL; - } - - input_wallets[i].is_ckb_only = ret == CKB_ITEM_MISSING; - - /* load amount */ - len = CKB_LEN; - ret = ckb_checked_load_cell_by_field( - (uint8_t *)&input_wallets[i].ckb_amount, &len, 0, i, - CKB_SOURCE_GROUP_INPUT, CKB_CELL_FIELD_CAPACITY); - if (ret != CKB_SUCCESS) { - return ERROR_SYSCALL; - } - if (len != CKB_LEN) { - return ERROR_ENCODING; - } - len = UDT_LEN; - ret = ckb_load_cell_data((uint8_t *)&input_wallets[i].udt_amount, &len, 0, - i, CKB_SOURCE_GROUP_INPUT); - if (ret != CKB_ITEM_MISSING && ret != CKB_SUCCESS) { - return ERROR_SYSCALL; - } - - if (input_wallets[i].is_ckb_only) { - /* ckb only wallet should has no data */ - if (len != 0) { - return ERROR_ENCODING; - } - } else { - if (len < UDT_LEN) { - return ERROR_ENCODING; - } - } - - i++; - } - - int input_wallets_cnt = i; - - /* iterate outputs wallet cell */ - i = 0; - while (1) { - uint8_t output_lock_hash[BLAKE2B_BLOCK_SIZE]; - uint8_t output_type_hash[BLAKE2B_BLOCK_SIZE]; - uint64_t len = BLAKE2B_BLOCK_SIZE; - /* check lock hash */ - ret = ckb_checked_load_cell_by_field(output_lock_hash, &len, 0, i, - CKB_SOURCE_OUTPUT, - CKB_CELL_FIELD_LOCK_HASH); - if (ret == CKB_INDEX_OUT_OF_BOUND) { - break; - } - if (ret != CKB_SUCCESS) { - return ret; - } - if (len != BLAKE2B_BLOCK_SIZE) { - return ERROR_ENCODING; - } - int has_same_lock = - memcmp(output_lock_hash, lock_hash, BLAKE2B_BLOCK_SIZE) == 0; - if (!has_same_lock) { - i++; - continue; - } - /* load type hash */ - len = BLAKE2B_BLOCK_SIZE; - ret = ckb_checked_load_cell_by_field(output_type_hash, &len, 0, i, - CKB_SOURCE_OUTPUT, - CKB_CELL_FIELD_TYPE_HASH); - if (ret == CKB_INDEX_OUT_OF_BOUND) { - break; - } - if (ret == CKB_SUCCESS) { - if (len != BLAKE2B_BLOCK_SIZE) { - return ERROR_ENCODING; - } - } else if (ret != CKB_ITEM_MISSING) { - return ERROR_SYSCALL; - } - int is_ckb_only = ret == CKB_ITEM_MISSING; - - /* load amount */ - uint64_t ckb_amount; - uint128_t udt_amount; - len = CKB_LEN; - ret = ckb_checked_load_cell_by_field((uint8_t *)&ckb_amount, &len, 0, i, - CKB_SOURCE_OUTPUT, - CKB_CELL_FIELD_CAPACITY); - if (ret != CKB_SUCCESS) { - return ERROR_SYSCALL; - } - if (len != CKB_LEN) { - return ERROR_ENCODING; - } - len = UDT_LEN; - ret = ckb_load_cell_data((uint8_t *)&udt_amount, &len, 0, i, - CKB_SOURCE_OUTPUT); - if (ret != CKB_ITEM_MISSING && ret != CKB_SUCCESS) { - return ERROR_SYSCALL; - } - - if (is_ckb_only) { - /* ckb only wallet should has no data */ - if (len != 0) { - return ERROR_ENCODING; - } - } else { - if (len < UDT_LEN) { - return ERROR_ENCODING; - } - } - - /* find input wallet which has same type hash */ - int found_inputs = 0; - for (int j = 0; j < input_wallets_cnt; j++) { - int has_same_type = 0; - /* check type hash */ - if (is_ckb_only) { - has_same_type = input_wallets[j].is_ckb_only; - } else { - has_same_type = memcmp(output_type_hash, input_wallets[j].type_hash, - BLAKE2B_BLOCK_SIZE) == 0; - } - if (!has_same_type) { - continue; - } - /* compare amount */ - uint64_t min_output_ckb_amount; - uint128_t min_output_udt_amount; - int overflow; - overflow = uint64_overflow_add( - &min_output_ckb_amount, input_wallets[j].ckb_amount, min_ckb_amount); - int meet_ckb_cond = !overflow && ckb_amount >= min_output_ckb_amount; - overflow = uint128_overflow_add( - &min_output_udt_amount, input_wallets[j].udt_amount, min_udt_amount); - int meet_udt_cond = !overflow && udt_amount >= min_output_udt_amount; - - /* fail if can't meet both conditions */ - if (!(meet_ckb_cond || meet_udt_cond)) { - return ERROR_OUTPUT_AMOUNT_NOT_ENOUGH; - } - /* output coins must meet condition, or remain the old amount */ - if ((!meet_ckb_cond && ckb_amount != input_wallets[j].ckb_amount) || - (!meet_udt_cond && udt_amount != input_wallets[j].udt_amount)) { - return ERROR_OUTPUT_AMOUNT_NOT_ENOUGH; - } - - /* increase counter */ - found_inputs++; - input_wallets[j].output_cnt += 1; - if (found_inputs > 1) { - return ERROR_DUPLICATED_INPUTS; - } - if (input_wallets[j].output_cnt > 1) { - return ERROR_DUPLICATED_OUTPUTS; - } - } - - /* one output should pair with one input */ - if (found_inputs == 0) { - return ERROR_NO_PAIR; - } else if (found_inputs > 1) { - return ERROR_DUPLICATED_INPUTS; - } - - i++; - } - - /* check inputs wallet, one input should pair with one output */ - for (int j = 0; j < input_wallets_cnt; j++) { - if (input_wallets[j].output_cnt == 0) { - return ERROR_NO_PAIR; - } else if (input_wallets[j].output_cnt > 1) { - return ERROR_DUPLICATED_OUTPUTS; - } - } - - return CKB_SUCCESS; -} - -void process_amount(int min_ckb, int min_udt, uint64_t *min_ckb_amount, - uint128_t *min_udt_amount) { - int is_overflow = quick_pow10(min_ckb, min_ckb_amount); - if (is_overflow) { - *min_ckb_amount = MAX_UINT64; - } - - is_overflow = uint128_quick_pow10(min_udt, min_udt_amount); - if (is_overflow) { - *min_udt_amount = MAX_UINT128; - } -} diff --git a/c/omni_lock_mol.h b/c/omni_lock_mol.h deleted file mode 100644 index 1e82f24..0000000 --- a/c/omni_lock_mol.h +++ /dev/null @@ -1,376 +0,0 @@ -// Generated by Molecule 0.7.2 - -#define MOLECULEC_VERSION 7000 -#define MOLECULE_API_VERSION_MIN 7000 - -#include "molecule_reader.h" -#include "molecule_builder.h" - -#ifndef OMNI_LOCK_H -#define OMNI_LOCK_H - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#ifndef MOLECULE_API_DECORATOR -#define __DEFINE_MOLECULE_API_DECORATOR_OMNI_LOCK -#define MOLECULE_API_DECORATOR -#endif /* MOLECULE_API_DECORATOR */ - -#include "xudt_rce_mol.h" - -/* - * Reader APIs - */ - -#define MolReader_Auth_verify(s, c) mol_verify_fixed_size(s, 21) -#define MolReader_Auth_get_nth0(s) mol_slice_by_offset(s, 0, 1) -#define MolReader_Auth_get_nth1(s) mol_slice_by_offset(s, 1, 1) -#define MolReader_Auth_get_nth2(s) mol_slice_by_offset(s, 2, 1) -#define MolReader_Auth_get_nth3(s) mol_slice_by_offset(s, 3, 1) -#define MolReader_Auth_get_nth4(s) mol_slice_by_offset(s, 4, 1) -#define MolReader_Auth_get_nth5(s) mol_slice_by_offset(s, 5, 1) -#define MolReader_Auth_get_nth6(s) mol_slice_by_offset(s, 6, 1) -#define MolReader_Auth_get_nth7(s) mol_slice_by_offset(s, 7, 1) -#define MolReader_Auth_get_nth8(s) mol_slice_by_offset(s, 8, 1) -#define MolReader_Auth_get_nth9(s) mol_slice_by_offset(s, 9, 1) -#define MolReader_Auth_get_nth10(s) mol_slice_by_offset(s, 10, 1) -#define MolReader_Auth_get_nth11(s) mol_slice_by_offset(s, 11, 1) -#define MolReader_Auth_get_nth12(s) mol_slice_by_offset(s, 12, 1) -#define MolReader_Auth_get_nth13(s) mol_slice_by_offset(s, 13, 1) -#define MolReader_Auth_get_nth14(s) mol_slice_by_offset(s, 14, 1) -#define MolReader_Auth_get_nth15(s) mol_slice_by_offset(s, 15, 1) -#define MolReader_Auth_get_nth16(s) mol_slice_by_offset(s, 16, 1) -#define MolReader_Auth_get_nth17(s) mol_slice_by_offset(s, 17, 1) -#define MolReader_Auth_get_nth18(s) mol_slice_by_offset(s, 18, 1) -#define MolReader_Auth_get_nth19(s) mol_slice_by_offset(s, 19, 1) -#define MolReader_Auth_get_nth20(s) mol_slice_by_offset(s, 20, 1) -MOLECULE_API_DECORATOR mol_errno MolReader_Identity_verify (const mol_seg_t*, bool); -#define MolReader_Identity_actual_field_count(s) mol_table_actual_field_count(s) -#define MolReader_Identity_has_extra_fields(s) mol_table_has_extra_fields(s, 2) -#define MolReader_Identity_get_identity(s) mol_table_slice_by_index(s, 0) -#define MolReader_Identity_get_proofs(s) mol_table_slice_by_index(s, 1) -MOLECULE_API_DECORATOR mol_errno MolReader_IdentityOpt_verify (const mol_seg_t*, bool); -#define MolReader_IdentityOpt_is_none(s) mol_option_is_none(s) -MOLECULE_API_DECORATOR mol_errno MolReader_OmniLockWitnessLock_verify (const mol_seg_t*, bool); -#define MolReader_OmniLockWitnessLock_actual_field_count(s) mol_table_actual_field_count(s) -#define MolReader_OmniLockWitnessLock_has_extra_fields(s) mol_table_has_extra_fields(s, 3) -#define MolReader_OmniLockWitnessLock_get_signature(s) mol_table_slice_by_index(s, 0) -#define MolReader_OmniLockWitnessLock_get_omni_identity(s) mol_table_slice_by_index(s, 1) -#define MolReader_OmniLockWitnessLock_get_preimage(s) mol_table_slice_by_index(s, 2) - -/* - * Builder APIs - */ - -#define MolBuilder_Auth_init(b) mol_builder_initialize_fixed_size(b, 21) -#define MolBuilder_Auth_set_nth0(b, p) mol_builder_set_byte_by_offset(b, 0, p) -#define MolBuilder_Auth_set_nth1(b, p) mol_builder_set_byte_by_offset(b, 1, p) -#define MolBuilder_Auth_set_nth2(b, p) mol_builder_set_byte_by_offset(b, 2, p) -#define MolBuilder_Auth_set_nth3(b, p) mol_builder_set_byte_by_offset(b, 3, p) -#define MolBuilder_Auth_set_nth4(b, p) mol_builder_set_byte_by_offset(b, 4, p) -#define MolBuilder_Auth_set_nth5(b, p) mol_builder_set_byte_by_offset(b, 5, p) -#define MolBuilder_Auth_set_nth6(b, p) mol_builder_set_byte_by_offset(b, 6, p) -#define MolBuilder_Auth_set_nth7(b, p) mol_builder_set_byte_by_offset(b, 7, p) -#define MolBuilder_Auth_set_nth8(b, p) mol_builder_set_byte_by_offset(b, 8, p) -#define MolBuilder_Auth_set_nth9(b, p) mol_builder_set_byte_by_offset(b, 9, p) -#define MolBuilder_Auth_set_nth10(b, p) mol_builder_set_byte_by_offset(b, 10, p) -#define MolBuilder_Auth_set_nth11(b, p) mol_builder_set_byte_by_offset(b, 11, p) -#define MolBuilder_Auth_set_nth12(b, p) mol_builder_set_byte_by_offset(b, 12, p) -#define MolBuilder_Auth_set_nth13(b, p) mol_builder_set_byte_by_offset(b, 13, p) -#define MolBuilder_Auth_set_nth14(b, p) mol_builder_set_byte_by_offset(b, 14, p) -#define MolBuilder_Auth_set_nth15(b, p) mol_builder_set_byte_by_offset(b, 15, p) -#define MolBuilder_Auth_set_nth16(b, p) mol_builder_set_byte_by_offset(b, 16, p) -#define MolBuilder_Auth_set_nth17(b, p) mol_builder_set_byte_by_offset(b, 17, p) -#define MolBuilder_Auth_set_nth18(b, p) mol_builder_set_byte_by_offset(b, 18, p) -#define MolBuilder_Auth_set_nth19(b, p) mol_builder_set_byte_by_offset(b, 19, p) -#define MolBuilder_Auth_set_nth20(b, p) mol_builder_set_byte_by_offset(b, 20, p) -#define MolBuilder_Auth_build(b) mol_builder_finalize_simple(b) -#define MolBuilder_Auth_clear(b) mol_builder_discard(b) -#define MolBuilder_Identity_init(b) mol_table_builder_initialize(b, 256, 2) -#define MolBuilder_Identity_set_identity(b, p, l) mol_table_builder_add(b, 0, p, l) -#define MolBuilder_Identity_set_proofs(b, p, l) mol_table_builder_add(b, 1, p, l) -MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_Identity_build (mol_builder_t); -#define MolBuilder_Identity_clear(b) mol_builder_discard(b) -#define MolBuilder_IdentityOpt_init(b) mol_builder_initialize_fixed_size(b, 0) -#define MolBuilder_IdentityOpt_set(b, p, l) mol_option_builder_set(b, p, l) -#define MolBuilder_IdentityOpt_build(b) mol_builder_finalize_simple(b) -#define MolBuilder_IdentityOpt_clear(b) mol_builder_discard(b) -#define MolBuilder_OmniLockWitnessLock_init(b) mol_table_builder_initialize(b, 64, 3) -#define MolBuilder_OmniLockWitnessLock_set_signature(b, p, l) mol_table_builder_add(b, 0, p, l) -#define MolBuilder_OmniLockWitnessLock_set_omni_identity(b, p, l) mol_table_builder_add(b, 1, p, l) -#define MolBuilder_OmniLockWitnessLock_set_preimage(b, p, l) mol_table_builder_add(b, 2, p, l) -MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_OmniLockWitnessLock_build (mol_builder_t); -#define MolBuilder_OmniLockWitnessLock_clear(b) mol_builder_discard(b) - -/* - * Default Value - */ - -#define ____ 0x00 - -MOLECULE_API_DECORATOR const uint8_t MolDefault_Auth[21] = { - ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, - ____, ____, ____, ____, ____, ____, ____, ____, ____, -}; -MOLECULE_API_DECORATOR const uint8_t MolDefault_Identity[37] = { - 0x25, ____, ____, ____, 0x0c, ____, ____, ____, 0x21, ____, ____, ____, - ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, - ____, ____, ____, ____, ____, ____, ____, ____, ____, 0x04, ____, ____, - ____, -}; -MOLECULE_API_DECORATOR const uint8_t MolDefault_IdentityOpt[0] = {}; -MOLECULE_API_DECORATOR const uint8_t MolDefault_OmniLockWitnessLock[16] = { - 0x10, ____, ____, ____, 0x10, ____, ____, ____, 0x10, ____, ____, ____, - 0x10, ____, ____, ____, -}; - -#undef ____ - -/* - * Reader Functions - */ - -MOLECULE_API_DECORATOR mol_errno MolReader_Identity_verify (const mol_seg_t *input, bool compatible) { - if (input->size < MOL_NUM_T_SIZE) { - return MOL_ERR_HEADER; - } - uint8_t *ptr = input->ptr; - mol_num_t total_size = mol_unpack_number(ptr); - if (input->size != total_size) { - return MOL_ERR_TOTAL_SIZE; - } - if (input->size < MOL_NUM_T_SIZE * 2) { - return MOL_ERR_HEADER; - } - ptr += MOL_NUM_T_SIZE; - mol_num_t offset = mol_unpack_number(ptr); - if (offset % 4 > 0 || offset < MOL_NUM_T_SIZE*2) { - return MOL_ERR_OFFSET; - } - mol_num_t field_count = offset / 4 - 1; - if (field_count < 2) { - return MOL_ERR_FIELD_COUNT; - } else if (!compatible && field_count > 2) { - return MOL_ERR_FIELD_COUNT; - } - if (input->size < MOL_NUM_T_SIZE*(field_count+1)){ - return MOL_ERR_HEADER; - } - mol_num_t offsets[field_count+1]; - offsets[0] = offset; - for (mol_num_t i=1; i offsets[i]) { - return MOL_ERR_OFFSET; - } - } - if (offsets[field_count-1] > total_size) { - return MOL_ERR_OFFSET; - } - offsets[field_count] = total_size; - mol_seg_t inner; - mol_errno errno; - inner.ptr = input->ptr + offsets[0]; - inner.size = offsets[1] - offsets[0]; - errno = MolReader_Auth_verify(&inner, compatible); - if (errno != MOL_OK) { - return MOL_ERR_DATA; - } - inner.ptr = input->ptr + offsets[1]; - inner.size = offsets[2] - offsets[1]; - errno = MolReader_SmtProofEntryVec_verify(&inner, compatible); - if (errno != MOL_OK) { - return MOL_ERR_DATA; - } - return MOL_OK; -} -MOLECULE_API_DECORATOR mol_errno MolReader_IdentityOpt_verify (const mol_seg_t *input, bool compatible) { - if (input->size != 0) { - return MolReader_Identity_verify(input, compatible); - } else { - return MOL_OK; - } -} -MOLECULE_API_DECORATOR mol_errno MolReader_OmniLockWitnessLock_verify (const mol_seg_t *input, bool compatible) { - if (input->size < MOL_NUM_T_SIZE) { - return MOL_ERR_HEADER; - } - uint8_t *ptr = input->ptr; - mol_num_t total_size = mol_unpack_number(ptr); - if (input->size != total_size) { - return MOL_ERR_TOTAL_SIZE; - } - if (input->size < MOL_NUM_T_SIZE * 2) { - return MOL_ERR_HEADER; - } - ptr += MOL_NUM_T_SIZE; - mol_num_t offset = mol_unpack_number(ptr); - if (offset % 4 > 0 || offset < MOL_NUM_T_SIZE*2) { - return MOL_ERR_OFFSET; - } - mol_num_t field_count = offset / 4 - 1; - if (field_count < 3) { - return MOL_ERR_FIELD_COUNT; - } else if (!compatible && field_count > 3) { - return MOL_ERR_FIELD_COUNT; - } - if (input->size < MOL_NUM_T_SIZE*(field_count+1)){ - return MOL_ERR_HEADER; - } - mol_num_t offsets[field_count+1]; - offsets[0] = offset; - for (mol_num_t i=1; i offsets[i]) { - return MOL_ERR_OFFSET; - } - } - if (offsets[field_count-1] > total_size) { - return MOL_ERR_OFFSET; - } - offsets[field_count] = total_size; - mol_seg_t inner; - mol_errno errno; - inner.ptr = input->ptr + offsets[0]; - inner.size = offsets[1] - offsets[0]; - errno = MolReader_BytesOpt_verify(&inner, compatible); - if (errno != MOL_OK) { - return MOL_ERR_DATA; - } - inner.ptr = input->ptr + offsets[1]; - inner.size = offsets[2] - offsets[1]; - errno = MolReader_IdentityOpt_verify(&inner, compatible); - if (errno != MOL_OK) { - return MOL_ERR_DATA; - } - inner.ptr = input->ptr + offsets[2]; - inner.size = offsets[3] - offsets[2]; - errno = MolReader_BytesOpt_verify(&inner, compatible); - if (errno != MOL_OK) { - return MOL_ERR_DATA; - } - return MOL_OK; -} - -/* - * Builder Functions - */ - -MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_Identity_build (mol_builder_t builder) { - mol_seg_res_t res; - res.errno = MOL_OK; - mol_num_t offset = 12; - mol_num_t len; - res.seg.size = offset; - len = builder.number_ptr[1]; - res.seg.size += len == 0 ? 21 : len; - len = builder.number_ptr[3]; - res.seg.size += len == 0 ? 4 : len; - res.seg.ptr = (uint8_t*)malloc(res.seg.size); - uint8_t *dst = res.seg.ptr; - mol_pack_number(dst, &res.seg.size); - dst += MOL_NUM_T_SIZE; - mol_pack_number(dst, &offset); - dst += MOL_NUM_T_SIZE; - len = builder.number_ptr[1]; - offset += len == 0 ? 21 : len; - mol_pack_number(dst, &offset); - dst += MOL_NUM_T_SIZE; - len = builder.number_ptr[3]; - offset += len == 0 ? 4 : len; - uint8_t *src = builder.data_ptr; - len = builder.number_ptr[1]; - if (len == 0) { - len = 21; - memcpy(dst, &MolDefault_Auth, len); - } else { - mol_num_t of = builder.number_ptr[0]; - memcpy(dst, src+of, len); - } - dst += len; - len = builder.number_ptr[3]; - if (len == 0) { - len = 4; - memcpy(dst, &MolDefault_SmtProofEntryVec, len); - } else { - mol_num_t of = builder.number_ptr[2]; - memcpy(dst, src+of, len); - } - dst += len; - mol_builder_discard(builder); - return res; -} -MOLECULE_API_DECORATOR mol_seg_res_t MolBuilder_OmniLockWitnessLock_build (mol_builder_t builder) { - mol_seg_res_t res; - res.errno = MOL_OK; - mol_num_t offset = 16; - mol_num_t len; - res.seg.size = offset; - len = builder.number_ptr[1]; - res.seg.size += len == 0 ? 0 : len; - len = builder.number_ptr[3]; - res.seg.size += len == 0 ? 0 : len; - len = builder.number_ptr[5]; - res.seg.size += len == 0 ? 0 : len; - res.seg.ptr = (uint8_t*)malloc(res.seg.size); - uint8_t *dst = res.seg.ptr; - mol_pack_number(dst, &res.seg.size); - dst += MOL_NUM_T_SIZE; - mol_pack_number(dst, &offset); - dst += MOL_NUM_T_SIZE; - len = builder.number_ptr[1]; - offset += len == 0 ? 0 : len; - mol_pack_number(dst, &offset); - dst += MOL_NUM_T_SIZE; - len = builder.number_ptr[3]; - offset += len == 0 ? 0 : len; - mol_pack_number(dst, &offset); - dst += MOL_NUM_T_SIZE; - len = builder.number_ptr[5]; - offset += len == 0 ? 0 : len; - uint8_t *src = builder.data_ptr; - len = builder.number_ptr[1]; - if (len == 0) { - len = 0; - memcpy(dst, &MolDefault_BytesOpt, len); - } else { - mol_num_t of = builder.number_ptr[0]; - memcpy(dst, src+of, len); - } - dst += len; - len = builder.number_ptr[3]; - if (len == 0) { - len = 0; - memcpy(dst, &MolDefault_IdentityOpt, len); - } else { - mol_num_t of = builder.number_ptr[2]; - memcpy(dst, src+of, len); - } - dst += len; - len = builder.number_ptr[5]; - if (len == 0) { - len = 0; - memcpy(dst, &MolDefault_BytesOpt, len); - } else { - mol_num_t of = builder.number_ptr[4]; - memcpy(dst, src+of, len); - } - dst += len; - mol_builder_discard(builder); - return res; -} - -#ifdef __DEFINE_MOLECULE_API_DECORATOR_OMNI_LOCK -#undef MOLECULE_API_DECORATOR -#undef __DEFINE_MOLECULE_API_DECORATOR_OMNI_LOCK -#endif /* __DEFINE_MOLECULE_API_DECORATOR_OMNI_LOCK */ - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* OMNI_LOCK_H */ diff --git a/c/omni_lock_mol2.h b/c/omni_lock_mol2.h deleted file mode 100644 index e62ee87..0000000 --- a/c/omni_lock_mol2.h +++ /dev/null @@ -1,221 +0,0 @@ - -#ifndef _OMNI_LOCK_MOL2_API2_H_ -#define _OMNI_LOCK_MOL2_API2_H_ - -#ifndef MOLECULEC_VERSION -#define MOLECULEC_VERSION 6001 -#endif -#ifndef MOLECULE_API_VERSION_MIN -#define MOLECULE_API_VERSION_MIN 5000 -#endif - -#define MOLECULEC2_VERSION 6001 -#define MOLECULE2_API_VERSION_MIN 5000 - -#include "molecule2_reader.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -// ----forward declaration-------- -struct AuthType; -struct AuthVTable; -struct AuthVTable *GetAuthVTable(void); -struct AuthType make_Auth(mol2_cursor_t *cur); -uint32_t Auth_len_impl(struct AuthType *); -uint8_t Auth_get_impl(struct AuthType *, uint32_t, bool *); -struct IdentityType; -struct IdentityVTable; -struct IdentityVTable *GetIdentityVTable(void); -struct IdentityType make_Identity(mol2_cursor_t *cur); -mol2_cursor_t Identity_get_identity_impl(struct IdentityType *); -struct SmtProofEntryVecType Identity_get_proofs_impl(struct IdentityType *); -struct IdentityOptType; -struct IdentityOptVTable; -struct IdentityOptVTable *GetIdentityOptVTable(void); -struct IdentityOptType make_IdentityOpt(mol2_cursor_t *cur); -bool IdentityOpt_is_none_impl(struct IdentityOptType *); -bool IdentityOpt_is_some_impl(struct IdentityOptType *); -struct IdentityType IdentityOpt_unwrap_impl(struct IdentityOptType *); -struct OmniLockWitnessLockType; -struct OmniLockWitnessLockVTable; -struct OmniLockWitnessLockVTable *GetOmniLockWitnessLockVTable(void); -struct OmniLockWitnessLockType make_OmniLockWitnessLock(mol2_cursor_t *cur); -struct BytesOptType OmniLockWitnessLock_get_signature_impl( - struct OmniLockWitnessLockType *); -struct IdentityOptType OmniLockWitnessLock_get_omni_identity_impl( - struct OmniLockWitnessLockType *); -struct BytesOptType OmniLockWitnessLock_get_preimage_impl( - struct OmniLockWitnessLockType *); - -// ----definition----------------- -typedef struct AuthVTable { - uint32_t (*len)(struct AuthType *); - uint8_t (*get)(struct AuthType *, uint32_t, bool *); -} AuthVTable; -typedef struct AuthType { - mol2_cursor_t cur; - AuthVTable *t; -} AuthType; - -typedef struct IdentityVTable { - mol2_cursor_t (*identity)(struct IdentityType *); - struct SmtProofEntryVecType (*proofs)(struct IdentityType *); -} IdentityVTable; -typedef struct IdentityType { - mol2_cursor_t cur; - IdentityVTable *t; -} IdentityType; - -typedef struct IdentityOptVTable { - bool (*is_none)(struct IdentityOptType *); - bool (*is_some)(struct IdentityOptType *); - struct IdentityType (*unwrap)(struct IdentityOptType *); -} IdentityOptVTable; -typedef struct IdentityOptType { - mol2_cursor_t cur; - IdentityOptVTable *t; -} IdentityOptType; - -typedef struct OmniLockWitnessLockVTable { - struct BytesOptType (*signature)(struct OmniLockWitnessLockType *); - struct IdentityOptType (*omni_identity)(struct OmniLockWitnessLockType *); - struct BytesOptType (*preimage)(struct OmniLockWitnessLockType *); -} OmniLockWitnessLockVTable; -typedef struct OmniLockWitnessLockType { - mol2_cursor_t cur; - OmniLockWitnessLockVTable *t; -} OmniLockWitnessLockType; - -#ifndef MOLECULEC_C2_DECLARATION_ONLY - -// ----implementation------------- -struct AuthType make_Auth(mol2_cursor_t *cur) { - AuthType ret; - ret.cur = *cur; - ret.t = GetAuthVTable(); - return ret; -} -struct AuthVTable *GetAuthVTable(void) { - static AuthVTable s_vtable; - static int inited = 0; - if (inited) return &s_vtable; - s_vtable.len = Auth_len_impl; - s_vtable.get = Auth_get_impl; - return &s_vtable; -} -uint32_t Auth_len_impl(AuthType *this) { return 21; } -uint8_t Auth_get_impl(AuthType *this, uint32_t index, bool *existing) { - uint8_t ret = {0}; - mol2_cursor_res_t res = mol2_slice_by_offset2(&this->cur, 1 * index, 1); - if (res.errno != MOL2_OK) { - *existing = false; - return ret; - } else { - *existing = true; - } - ret = convert_to_Uint8(&res.cur); - return ret; -} -struct IdentityType make_Identity(mol2_cursor_t *cur) { - IdentityType ret; - ret.cur = *cur; - ret.t = GetIdentityVTable(); - return ret; -} -struct IdentityVTable *GetIdentityVTable(void) { - static IdentityVTable s_vtable; - static int inited = 0; - if (inited) return &s_vtable; - s_vtable.identity = Identity_get_identity_impl; - s_vtable.proofs = Identity_get_proofs_impl; - return &s_vtable; -} -mol2_cursor_t Identity_get_identity_impl(IdentityType *this) { - mol2_cursor_t ret; - mol2_cursor_t ret2 = mol2_table_slice_by_index(&this->cur, 0); - ret = convert_to_array(&ret2); - return ret; -} -SmtProofEntryVecType Identity_get_proofs_impl(IdentityType *this) { - SmtProofEntryVecType ret; - mol2_cursor_t cur = mol2_table_slice_by_index(&this->cur, 1); - ret.cur = cur; - ret.t = GetSmtProofEntryVecVTable(); - return ret; -} -struct IdentityOptType make_IdentityOpt(mol2_cursor_t *cur) { - IdentityOptType ret; - ret.cur = *cur; - ret.t = GetIdentityOptVTable(); - return ret; -} -struct IdentityOptVTable *GetIdentityOptVTable(void) { - static IdentityOptVTable s_vtable; - static int inited = 0; - if (inited) return &s_vtable; - s_vtable.is_none = IdentityOpt_is_none_impl; - s_vtable.is_some = IdentityOpt_is_some_impl; - s_vtable.unwrap = IdentityOpt_unwrap_impl; - return &s_vtable; -} -bool IdentityOpt_is_none_impl(IdentityOptType *this) { - return mol2_option_is_none(&this->cur); -} -bool IdentityOpt_is_some_impl(IdentityOptType *this) { - return !mol2_option_is_none(&this->cur); -} -IdentityType IdentityOpt_unwrap_impl(IdentityOptType *this) { - IdentityType ret; - mol2_cursor_t cur = this->cur; - ret.cur = cur; - ret.t = GetIdentityVTable(); - return ret; -} -struct OmniLockWitnessLockType make_OmniLockWitnessLock(mol2_cursor_t *cur) { - OmniLockWitnessLockType ret; - ret.cur = *cur; - ret.t = GetOmniLockWitnessLockVTable(); - return ret; -} -struct OmniLockWitnessLockVTable *GetOmniLockWitnessLockVTable(void) { - static OmniLockWitnessLockVTable s_vtable; - static int inited = 0; - if (inited) return &s_vtable; - s_vtable.signature = OmniLockWitnessLock_get_signature_impl; - s_vtable.omni_identity = OmniLockWitnessLock_get_omni_identity_impl; - s_vtable.preimage = OmniLockWitnessLock_get_preimage_impl; - return &s_vtable; -} -BytesOptType OmniLockWitnessLock_get_signature_impl( - OmniLockWitnessLockType *this) { - BytesOptType ret; - mol2_cursor_t cur = mol2_table_slice_by_index(&this->cur, 0); - ret.cur = cur; - ret.t = GetBytesOptVTable(); - return ret; -} -IdentityOptType OmniLockWitnessLock_get_omni_identity_impl( - OmniLockWitnessLockType *this) { - IdentityOptType ret; - mol2_cursor_t cur = mol2_table_slice_by_index(&this->cur, 1); - ret.cur = cur; - ret.t = GetIdentityOptVTable(); - return ret; -} -BytesOptType OmniLockWitnessLock_get_preimage_impl( - OmniLockWitnessLockType *this) { - BytesOptType ret; - mol2_cursor_t cur = mol2_table_slice_by_index(&this->cur, 2); - ret.cur = cur; - ret.t = GetBytesOptVTable(); - return ret; -} -#endif // MOLECULEC_C2_DECLARATION_ONLY - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif // _OMNI_LOCK_MOL2_API2_H_ diff --git a/c/omni_lock_supply.h b/c/omni_lock_supply.h deleted file mode 100644 index 0fc61bc..0000000 --- a/c/omni_lock_supply.h +++ /dev/null @@ -1,212 +0,0 @@ -#ifndef _OMNI_LOCK_SUPPLY_H_ -#define _OMNI_LOCK_SUPPLY_H_ -#include "ckb_syscalls.h" - -typedef unsigned __int128 uint128_t; - -enum SupplyErrorCode { - ERROR_EXCEED_SUPPLY = 90, - ERROR_SUPPLY_AMOUNT, - ERROR_BURN, - ERROR_NO_INFO_CELL, -}; - -// <1 byte version> <16 bytes current supply> <16 bytes max supply> <32 bytes sUDT script hash> -#define MIN_INFO_CELL_LEN 65 -#define INFO_CELL_CONST_OFFSET 17 - -typedef struct SupplyContextType { - size_t input_info_cell_index; - size_t input_info_cell_count; - size_t output_info_cell_index; - size_t output_info_cell_count; - - uint8_t version; - uint128_t input_current_supply; - uint128_t output_current_supply; - uint128_t max_supply; - uint8_t sudt_script_hash[32]; - - uint128_t input_amount; - uint128_t output_amount; -} SupplyContextType; - -int add_assign_amount(uint128_t* sum, const uint128_t* delta) { - *sum += *delta; - - if (*sum < *delta) { - return ERROR_SUPPLY_AMOUNT; - } else { - return 0; - } -} - -typedef int (iterate_func_t)(size_t index, size_t source, SupplyContextType* ctx); - -int locate_info_cell(size_t index, size_t source, SupplyContextType* ctx) { - if (source == CKB_SOURCE_INPUT) { - ctx->input_info_cell_index = index; - ctx->input_info_cell_count += 1; - if (ctx->input_info_cell_count > 1) { - return CKB_INVALID_DATA; - } else { - return 0; - } - } else if (source == CKB_SOURCE_OUTPUT) { - ctx->output_info_cell_index = index; - ctx->output_info_cell_count += 1; - if (ctx->output_info_cell_count > 1) { - return CKB_INVALID_DATA; - } else { - return 0; - } - } else { - return CKB_INVALID_DATA; - }; -} - -int accumulate_amount(size_t index, size_t source, SupplyContextType* ctx) { - int err = 0; - uint128_t amount = 0; - uint64_t len = 16; - err = ckb_checked_load_cell_data((uint8_t*)&amount, &len, 0, index, source); - CHECK(err); - if (source == CKB_SOURCE_INPUT) { - err = add_assign_amount(&ctx->input_amount, &amount); - CHECK(err); - } else if (source == CKB_SOURCE_OUTPUT) { - err = add_assign_amount(&ctx->output_amount, &amount); - CHECK(err); - } else { - CHECK2(false, ERROR_SUPPLY_AMOUNT); - } -exit: - return err; -} - -int compare_cells_data(size_t input_index, size_t output_index) { - int err = 0; - int err2 = 0; - uint8_t input_version = 0; - uint8_t output_version = 0; - uint64_t input_len = 1; - uint64_t output_len = 1; - err = ckb_load_cell_data(&input_version, &input_len, 0, input_index, CKB_SOURCE_INPUT); - CHECK(err); - err = ckb_load_cell_data(&output_version, &output_len, 0, output_index, CKB_SOURCE_OUTPUT); - CHECK(err); - CHECK2(input_len == output_len, CKB_INVALID_DATA); - CHECK2(input_len >= MIN_INFO_CELL_LEN, CKB_INVALID_DATA); - CHECK2(input_version == output_version, CKB_INVALID_DATA); - - uint8_t input_buff[1024]; - uint8_t output_buff[1024]; - uint128_t offset = INFO_CELL_CONST_OFFSET; - while (offset < input_len) { - uint64_t input_read_len = sizeof(input_buff); - uint64_t output_read_len = sizeof(output_buff); - err = ckb_load_cell_data(input_buff, &input_read_len, offset, input_index, CKB_SOURCE_INPUT); - err2 = ckb_load_cell_data(output_buff, &output_read_len, offset, output_index, CKB_SOURCE_OUTPUT); - CHECK2(err == 0 && err == err2, CKB_INVALID_DATA); - - input_read_len = - (input_read_len > sizeof(input_buff)) ? sizeof(input_buff) : input_read_len; - output_read_len = - (output_read_len > sizeof(input_buff)) ? sizeof(input_buff) : output_read_len; - CHECK2(input_read_len == output_read_len, CKB_INVALID_DATA); - int same = memcmp(input_buff, output_buff, input_read_len); - CHECK2(same == 0, CKB_INVALID_DATA); - offset += input_read_len; - } - -exit: - return err; -} - - -int iterate_by_type_script_hash(uint8_t* hash, size_t source, iterate_func_t func, SupplyContextType* ctx) { - int err = 0; - size_t i = 0; - uint8_t hash2[32] = {0}; - while (1) { - uint64_t len = 32; - err = ckb_load_cell_by_field(hash2, &len, 0, i, source, CKB_CELL_FIELD_TYPE_HASH); - if (err == CKB_INDEX_OUT_OF_BOUND) { - err = 0; - break; - } - if (err == CKB_ITEM_MISSING) { - i += 1; - continue; - } - CHECK2(err == 0, err); - - if (memcmp(hash, hash2, 32) == 0) { - err = func(i, source, ctx); - CHECK(err); - } - i += 1; - } - -exit: - return err; -} - -int check_supply(uint8_t* cell_id) { - int err = 0; - SupplyContextType ctx = {0}; - // locate the input info cell - err = iterate_by_type_script_hash(cell_id, CKB_SOURCE_INPUT, locate_info_cell, &ctx); - CHECK(err); - // locate the output info cell - err = iterate_by_type_script_hash(cell_id, CKB_SOURCE_OUTPUT, locate_info_cell, &ctx); - CHECK(err); - - CHECK2(ctx.input_info_cell_count == 1, ERROR_NO_INFO_CELL); - CHECK2(ctx.output_info_cell_count == 1, ERROR_NO_INFO_CELL); - - // check input/output info cells are same beginning with special index - err = compare_cells_data(ctx.input_info_cell_index, ctx.output_info_cell_index); - CHECK(err); - uint8_t info_cell[MIN_INFO_CELL_LEN] = {0}; - uint64_t info_cell_len = sizeof(info_cell); - err = ckb_load_cell_data(info_cell, &info_cell_len, 0, ctx.input_info_cell_index, CKB_SOURCE_INPUT); - CHECK(err); - CHECK2(info_cell_len >= MIN_INFO_CELL_LEN, CKB_INVALID_DATA); - ctx.version = info_cell[0]; - memcpy(&ctx.input_current_supply, info_cell + 1, 16); - memcpy(&ctx.max_supply, info_cell + 17, 16); - memcpy(ctx.sudt_script_hash, info_cell+ 33, 32); - // check version - CHECK2(ctx.version == 0, CKB_INVALID_DATA); - - info_cell_len = sizeof(info_cell); - err = ckb_load_cell_data(info_cell, &info_cell_len, 0, ctx.output_info_cell_index, CKB_SOURCE_OUTPUT); - CHECK(err); - memcpy(&ctx.output_current_supply, info_cell + 1, 16); - // check input/output current supply - CHECK2(ctx.output_current_supply <= ctx.max_supply, ERROR_EXCEED_SUPPLY); - CHECK2(ctx.input_current_supply <= ctx.max_supply, ERROR_EXCEED_SUPPLY); - - // collect issued amount, naively - err = iterate_by_type_script_hash(ctx.sudt_script_hash, CKB_SOURCE_INPUT, accumulate_amount, &ctx); - CHECK(err); - err = iterate_by_type_script_hash(ctx.sudt_script_hash, CKB_SOURCE_OUTPUT, accumulate_amount, &ctx); - CHECK(err); - - // issued sUDT amount is same to supply amount delta - if (ctx.output_amount >= ctx.input_amount) { - uint128_t issued_amount = ctx.output_amount - ctx.input_amount; - uint128_t temp_amount = ctx.input_current_supply; - err = add_assign_amount(&temp_amount, &issued_amount); - CHECK(err); - CHECK2(temp_amount == ctx.output_current_supply, ERROR_SUPPLY_AMOUNT); - } else { - CHECK2(false, ERROR_BURN); - } - -exit: - return err; -} - -#endif //_OMNI_LOCK_SUPPLY_H_ diff --git a/c/omni_lock_time_lock.h b/c/omni_lock_time_lock.h deleted file mode 100644 index de9df57..0000000 --- a/c/omni_lock_time_lock.h +++ /dev/null @@ -1,101 +0,0 @@ -#ifndef OMNI_LOCK_TIME_LOCK_H_ -#define OMNI_LOCK_TIME_LOCK_H_ - -#define ERROR_INCORRECT_SINCE_FLAGS -23 -#define ERROR_INCORRECT_SINCE_VALUE -24 -/* since */ -#define SINCE_VALUE_BITS 56 -#define SINCE_VALUE_MASK 0x00ffffffffffffff -#define SINCE_EPOCH_FRACTION_FLAG 0b00100000 - -/* a and b are since value, - return 0 if a is equals to b, - return -1 if a is less than b, - return 1 if a is greater than b */ -int epoch_number_with_fraction_cmp(uint64_t a, uint64_t b) { - static const size_t NUMBER_OFFSET = 0; - static const size_t NUMBER_BITS = 24; - static const uint64_t NUMBER_MAXIMUM_VALUE = (1 << NUMBER_BITS); - static const uint64_t NUMBER_MASK = (NUMBER_MAXIMUM_VALUE - 1); - static const size_t INDEX_OFFSET = NUMBER_BITS; - static const size_t INDEX_BITS = 16; - static const uint64_t INDEX_MAXIMUM_VALUE = (1 << INDEX_BITS); - static const uint64_t INDEX_MASK = (INDEX_MAXIMUM_VALUE - 1); - static const size_t LENGTH_OFFSET = NUMBER_BITS + INDEX_BITS; - static const size_t LENGTH_BITS = 16; - static const uint64_t LENGTH_MAXIMUM_VALUE = (1 << LENGTH_BITS); - static const uint64_t LENGTH_MASK = (LENGTH_MAXIMUM_VALUE - 1); - - /* extract a epoch */ - uint64_t a_epoch = (a >> NUMBER_OFFSET) & NUMBER_MASK; - uint64_t a_index = (a >> INDEX_OFFSET) & INDEX_MASK; - uint64_t a_len = (a >> LENGTH_OFFSET) & LENGTH_MASK; - - /* extract b epoch */ - uint64_t b_epoch = (b >> NUMBER_OFFSET) & NUMBER_MASK; - uint64_t b_index = (b >> INDEX_OFFSET) & INDEX_MASK; - uint64_t b_len = (b >> LENGTH_OFFSET) & LENGTH_MASK; - - if (a_epoch < b_epoch) { - return -1; - } else if (a_epoch > b_epoch) { - return 1; - } else { - /* a and b is in the same epoch, - compare a_index / a_len <=> b_index / b_len - */ - uint64_t a_block = a_index * b_len; - uint64_t b_block = b_index * a_len; - /* compare block */ - if (a_block < b_block) { - return -1; - } else if (a_block > b_block) { - return 1; - } else { - return 0; - } - } -} - -/* check since, - for all inputs the since field must have the exactly same flags with the since - constraint, and the value of since must greater or equals than the since - contstaint */ -int check_since(uint64_t since) { - size_t i = 0; - uint64_t len = 0; - uint64_t input_since; - /* the 8 msb is flag */ - uint8_t since_flags = since >> SINCE_VALUE_BITS; - uint64_t since_value = since & SINCE_VALUE_MASK; - int ret; - while (1) { - len = sizeof(uint64_t); - ret = - ckb_load_input_by_field(&input_since, &len, 0, i, - CKB_SOURCE_GROUP_INPUT, CKB_INPUT_FIELD_SINCE); - if (ret == CKB_INDEX_OUT_OF_BOUND) { - break; - } - if (ret != CKB_SUCCESS || len != sizeof(uint64_t)) { - return ERROR_SYSCALL; - } - uint8_t input_since_flags = input_since >> SINCE_VALUE_BITS; - uint64_t input_since_value = input_since & SINCE_VALUE_MASK; - if (since_flags != input_since_flags) { - return ERROR_INCORRECT_SINCE_FLAGS; - } - if (input_since_flags == SINCE_EPOCH_FRACTION_FLAG) { - ret = epoch_number_with_fraction_cmp(input_since_value, since_value); - if (ret < 0) { - return ERROR_INCORRECT_SINCE_VALUE; - } - } else if (input_since_value < since_value) { - return ERROR_INCORRECT_SINCE_VALUE; - } - i += 1; - } - return CKB_SUCCESS; -} - -#endif // OMNI_LOCK_TIME_LOCK_H_ diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..4934985 --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +1.69.0 diff --git a/tests/omni_lock/CMakeLists.txt b/tests/omni_lock/CMakeLists.txt deleted file mode 100644 index 1ed8f16..0000000 --- a/tests/omni_lock/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -cmake_minimum_required(VERSION 3.12) -project(omni_lock_simulator C) - -set(CMAKE_C_STANDARD 11) -# uncomment it for sanitize -#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize=undefined") - -# uncomment it for coverage test -#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage") -#add_definitions(-DCKB_COVERAGE) -#add_definitions(-Wno-incompatible-pointer-types) - -#add_definitions(-DCKB_SIMULATOR) -add_definitions(-D__SHARED_LIBRARY__) -add_definitions(-DCKB_DECLARATION_ONLY) -add_definitions(-DCKB_USE_SIM) - -include_directories(../../tests/omni_lock) -include_directories(../../deps/ckb-c-stdlib-20210801) -include_directories(../../deps/sparse-merkle-tree/c) -# include_directories(deps/ckb-c-stdlib/libc) -# on simulator, we don't use own "libc" -include_directories(../../deps/secp256k1/src) -include_directories(../../deps/secp256k1) -include_directories(../../deps/ckb-c-stdlib-20210801/molecule) -include_directories(../../c) -include_directories(../../build) - -add_executable(omni_lock_simulator ../../tests/omni_lock/omni_lock_sim.c) -target_compile_definitions(omni_lock_simulator PUBLIC -D_FILE_OFFSET_BITS=64 -DCKB_DECLARATION_ONLY) -target_include_directories(omni_lock_simulator PUBLIC deps/ckb-c-stdlib-20210801/libc) -target_link_libraries(omni_lock_simulator dl) diff --git a/tests/omni_lock/ckb_syscall_omni_lock_sim.h b/tests/omni_lock/ckb_syscall_omni_lock_sim.h deleted file mode 100644 index a393668..0000000 --- a/tests/omni_lock/ckb_syscall_omni_lock_sim.h +++ /dev/null @@ -1,910 +0,0 @@ -// note, this macro must be same as in ckb_syscall.h -#ifndef CKB_C_STDLIB_CKB_SYSCALLS_H_ -#define CKB_C_STDLIB_CKB_SYSCALLS_H_ -#include -#include - -#undef ASSERT -#include -#define ASSERT assert -#include - -#include "include/secp256k1.h" -#include "include/secp256k1_recovery.h" -#include "omni_lock_mol.h" -#include "xudt_rce_mol.h" - -#define VERY_LONG_DATA_SIZE 655360 -#define MAX_PROOF_COUNT 64 -#define SPECIAL_SECP256K1_INDEX 1111 - -#define countof(s) (sizeof(s) / sizeof(s[0])) -mol_seg_t build_bytes(const uint8_t* data, uint32_t len); -mol_seg_t build_script(const uint8_t* code_hash, uint8_t hash_type, - const uint8_t* args, uint32_t args_len); -int ckb_load_tx_hash(void* addr, uint64_t* len, size_t offset); -int ckb_load_witness(void* addr, uint64_t* len, size_t offset, size_t index, - size_t source); -int calculate_hash(uint8_t* msg); -void secp256k1_sign(const uint8_t* msg, uint8_t* serialized_sig, - uint8_t* pubkey_hash); -typedef struct slice_t { - uint8_t* ptr; - uint32_t size; -} slice_t; - -slice_t new_slice(uint32_t size) { - slice_t res = {.ptr = malloc(size), .size = size}; - memset(res.ptr, 0, size); - return res; -} - -slice_t copy_slice(uint8_t* ptr, uint32_t size) { - slice_t s = new_slice(size); - memcpy(s.ptr, ptr, size); - return s; -} - -void delete_slice(slice_t* t) { - free(t->ptr); - t->ptr = NULL; - t->size = 0; -} - -typedef struct SIMRCRule { - uint8_t flags; - uint8_t smt_root[32]; -} SIMRCRule; - -// set by users -typedef struct RcLockSettingType { - uint8_t flags; // identity's flags - uint8_t blake160[20]; // identity's blake160 - bool use_rc; // rc or not: if yes, the identity is in witness; otherwise it's - // in args in lock script. - uint8_t rc_root[32]; // in args in lock script - uint8_t signature[65]; // in witness - - slice_t proofs[MAX_PROOF_COUNT]; - uint32_t proof_count; - SIMRCRule rc_rules[MAX_PROOF_COUNT]; - // rc_rules with same length as proof - - slice_t input_lsh[64]; - uint8_t input_lsh_count; - - // info cell - bool use_supply; - unsigned __int128 input_current_supply; - unsigned __int128 input_max_supply; - unsigned __int128 output_current_supply; - unsigned __int128 output_max_supply; - unsigned __int128 input_sudt; - unsigned __int128 output_sudt; - - // test scheme - bool wrong_signature; - bool wrong_pubkey_hash; - // owner lock without rc doesn't require witness - bool empty_witness; -} RcLockSettingType; - -RcLockSettingType g_setting = {0}; - -void init_input(RcLockSettingType* input) { - memset(input, 0, sizeof(RcLockSettingType)); -} - -// input cell, output cell -typedef struct RcLockCellType { - uint8_t type_script_hash[32]; - slice_t data; -} RcLockCellType; - -// states are generated by input -typedef struct RcLockStates { - RcLockSettingType setting; - - slice_t witness[32]; - uint32_t witness_count; - - slice_t script; - - slice_t cell_data[64]; - uint32_t cell_data_count; - - // tsh: type script hash - uint8_t info_cell_tsh[32]; - uint8_t sudt_script_hash[32]; - - RcLockCellType input_cells[64]; - uint32_t input_cells_count; - RcLockCellType output_cells[64]; - uint32_t output_cells_count; -} RcLockStates; - -RcLockStates g_states; - -mol_seg_t build_proof(slice_t proof) { - mol_builder_t builder; - MolBuilder_SmtProofEntry_init(&builder); - mol_seg_t bytes = build_bytes(proof.ptr, proof.size); - MolBuilder_SmtProofEntry_set_proof(&builder, bytes.ptr, bytes.size); - MolBuilder_SmtProofEntry_set_mask(&builder, 3); - free(bytes.ptr); - - mol_seg_res_t res = MolBuilder_SmtProofEntry_build(builder); - ASSERT(res.errno == 0); - return res.seg; -} - -mol_seg_t build_proof_vec(slice_t* proof, uint32_t proof_len) { - mol_builder_t proofs; - MolBuilder_SmtProofEntryVec_init(&proofs); - - for (uint32_t i = 0; i < proof_len; i++) { - mol_seg_t b = build_proof(proof[i]); - MolBuilder_SmtProofEntryVec_push(&proofs, b.ptr, b.size); - free(b.ptr); - } - mol_seg_res_t res = MolBuilder_SmtProofEntryVec_build(proofs); - ASSERT(res.errno == 0); - return res.seg; -} - -mol_seg_t build_auth(uint8_t flags, uint8_t* blake160) { - mol_builder_t b; - MolBuilder_Auth_init(&b); - b.data_ptr[0] = flags; - memcpy(&b.data_ptr[1], blake160, 20); - mol_seg_res_t res = MolBuilder_Auth_build(b); - ASSERT(res.errno == 0); - return res.seg; -} - -mol_seg_t build_rc_identity(mol_seg_t* identity, mol_seg_t* proofs) { - mol_builder_t b; - MolBuilder_Identity_init(&b); - MolBuilder_Identity_set_identity(&b, identity->ptr, identity->size); - MolBuilder_Identity_set_proofs(&b, proofs->ptr, proofs->size); - - mol_seg_res_t res = MolBuilder_Identity_build(b); - ASSERT(res.errno == 0); - return res.seg; -} - -mol_seg_t build_witness_lock() { - mol_builder_t witness_lock; - MolBuilder_OmniLockWitnessLock_init(&witness_lock); - - mol_seg_t signature = - build_bytes(g_setting.signature, sizeof(g_setting.signature)); - mol_seg_t proofs = build_proof_vec(g_setting.proofs, g_setting.proof_count); - mol_seg_t identity = build_auth(g_setting.flags, g_setting.blake160); - mol_seg_t rc_identity = build_rc_identity(&identity, &proofs); - - MolBuilder_OmniLockWitnessLock_set_signature(&witness_lock, signature.ptr, - signature.size); - if (g_setting.use_rc) { - MolBuilder_OmniLockWitnessLock_set_omni_identity(&witness_lock, rc_identity.ptr, - rc_identity.size); - } - - free(signature.ptr); - free(proofs.ptr); - free(identity.ptr); - free(rc_identity.ptr); - - mol_seg_res_t res = MolBuilder_OmniLockWitnessLock_build(witness_lock); - ASSERT(res.errno == 0); - return res.seg; -} - -slice_t build_rcrule(SIMRCRule* rcrule) { - mol_builder_t b2; - mol_union_builder_initialize(&b2, 64, 0, MolDefault_RCRule, 33); - - mol_builder_t b; - MolBuilder_RCRule_init(&b); - MolBuilder_RCRule_set_flags(&b, rcrule->flags); - MolBuilder_RCRule_set_smt_root(&b, rcrule->smt_root); - mol_seg_res_t res = MolBuilder_RCRule_build(b); - ASSERT(res.errno == 0); - MolBuilder_RCData_set_RCRule(&b2, res.seg.ptr, res.seg.size); - free(res.seg.ptr); - - mol_seg_res_t res2 = MolBuilder_RCData_build(b2); - ASSERT(res2.errno == 0); - slice_t ret = {.ptr = res2.seg.ptr, .size = res2.seg.size}; - return ret; -} - -void convert_rcrule(uint8_t* rc_root) { - for (uint32_t i = 0; i < g_states.cell_data_count; i++) { - free(g_states.cell_data[i].ptr); - } - g_states.cell_data_count = 0; - - mol_builder_t b2; - mol_union_builder_initialize(&b2, 64, 0, MolDefault_RCRule, 33); - - mol_builder_t b; - MolBuilder_RCCellVec_init(&b); - for (uint32_t i = 0; i < g_setting.proof_count; i++) { - uint8_t hash[32] = {0}; - // very small 2 byte index as hash - *((uint16_t*)hash) = i; - MolBuilder_RCCellVec_push(&b, hash); - - g_states.cell_data[i] = build_rcrule(g_setting.rc_rules + i); - } - mol_seg_res_t res = MolBuilder_RCCellVec_build(b); - ASSERT(res.errno == 0); - MolBuilder_RCData_set_RCCellVec(&b2, res.seg.ptr, res.seg.size); - free(res.seg.ptr); - - mol_seg_res_t res2 = MolBuilder_RCData_build(b2); - ASSERT(res2.errno == 0); - slice_t ret = {.ptr = res2.seg.ptr, .size = res2.seg.size}; - - g_states.cell_data[g_setting.proof_count] = ret; - g_states.cell_data_count = g_setting.proof_count + 1; - - *((uint16_t*)rc_root) = g_setting.proof_count; -} - -void convert_witness(void) { - for (size_t i = 0; i < g_states.witness_count; i++) { - free(g_states.witness[i].ptr); - } - g_states.witness_count = 0; - - // Witness - mol_seg_t witness_lock = build_witness_lock(); - mol_seg_t witness_lock_bytes = - build_bytes(witness_lock.ptr, witness_lock.size); - - mol_builder_t witness; - MolBuilder_WitnessArgs_init(&witness); - MolBuilder_WitnessArgs_set_lock(&witness, witness_lock_bytes.ptr, - witness_lock_bytes.size); - uint8_t random_data[VERY_LONG_DATA_SIZE] = {1}; - mol_seg_t witness_input_type = build_bytes(random_data, sizeof(random_data)); - MolBuilder_WitnessArgs_set_input_type(&witness, witness_input_type.ptr, - witness_input_type.size); - - free(witness_input_type.ptr); - free(witness_lock.ptr); - free(witness_lock_bytes.ptr); - - mol_seg_res_t res = MolBuilder_WitnessArgs_build(witness); - ASSERT(res.errno == 0); - - g_states.witness[0].ptr = res.seg.ptr; - g_states.witness[0].size = res.seg.size; - g_states.witness_count = 1; -} - -slice_t make_info_cell(unsigned __int128 current_supply, - unsigned __int128 max_supply, - uint8_t* sudt_script_hash) { - slice_t s = new_slice(65); - s.ptr[0] = 0; - memcpy(&s.ptr[1], ¤t_supply, 16); - memcpy(&s.ptr[17], &max_supply, 16); - memcpy(&s.ptr[33], sudt_script_hash, 32); - return s; -} - -slice_t make_sudt_cell(unsigned __int128 amount) { - slice_t s = new_slice(16); - memcpy(s.ptr, &amount, 16); - return s; -} - -void convert_cell() { - for (uint32_t i = 0; i < g_states.input_cells_count; i++) { - free(g_states.input_cells[i].data.ptr); - } - g_states.input_cells_count = 0; - - for (uint32_t i = 0; i < g_states.output_cells_count; i++) { - free(g_states.output_cells[i].data.ptr); - } - g_states.output_cells_count = 0; - - // special type script hash - g_states.info_cell_tsh[0] = 1; - g_states.sudt_script_hash[0] = 2; - - g_states.input_cells_count = 2; - memcpy(g_states.input_cells[0].type_script_hash, g_states.info_cell_tsh, 32); - g_states.input_cells[0].data = make_info_cell( - g_states.setting.input_current_supply, g_states.setting.input_max_supply, - g_states.sudt_script_hash); - memcpy(g_states.input_cells[1].type_script_hash, g_states.sudt_script_hash, - 32); - g_states.input_cells[1].data = make_sudt_cell(g_setting.input_sudt); - - g_states.output_cells_count = 2; - memcpy(g_states.output_cells[0].type_script_hash, g_states.info_cell_tsh, 32); - g_states.output_cells[0].data = make_info_cell( - g_states.setting.output_current_supply, - g_states.setting.output_max_supply, g_states.sudt_script_hash); - memcpy(g_states.output_cells[1].type_script_hash, g_states.sudt_script_hash, - 32); - g_states.output_cells[1].data = make_sudt_cell(g_setting.output_sudt); -} - -void convert_setting_to_states(void) { - g_states.setting = g_setting; - convert_cell(); - // IdentityFlagsPubkeyHash - if (g_setting.flags == 0) { - // make witness skeleton - convert_witness(); - // sign - uint8_t msg[32] = {0}; - uint8_t sig[65] = {0}; - uint8_t pubkey_hash[32] = {0}; - calculate_hash(msg); - secp256k1_sign(msg, sig, pubkey_hash); - - memcpy(g_setting.blake160, pubkey_hash, 20); - if (g_setting.wrong_pubkey_hash) { - g_setting.blake160[0] ^= 0x1; - } - memcpy(g_setting.signature, sig, 65); - if (g_setting.wrong_signature) { - g_setting.signature[0] ^= 0x1; - } - } - // will use rcrule, set rc root manually - if (g_setting.use_rc) { - convert_rcrule(g_setting.rc_root); - } - // make witness again, with correct signature - convert_witness(); - - // Script - uint8_t script_args[1 + 20 + 1 + 32 + 2 + 8] = {0}; - uint32_t script_args_len = 22; - - if (g_setting.use_rc) { - memcpy(script_args + 1 + 20 + 1, g_setting.rc_root, 32); - script_args[1 + 20] |= 1; - script_args_len += 32; - } else if (g_setting.use_supply) { - script_args[0] = g_setting.flags; - memcpy(script_args + 1, g_setting.blake160, 20); - - memcpy(script_args + 1 + 20 + 1, g_states.info_cell_tsh, 32); - script_args[1 + 20] |= 1 << 3; - script_args_len += 32; - } else { - script_args[0] = g_setting.flags; - memcpy(script_args + 1, g_setting.blake160, 20); - } - - uint8_t code_hash[32] = {0}; - - mol_seg_t script = build_script(code_hash, 0, script_args, script_args_len); - g_states.script.ptr = script.ptr; - g_states.script.size = script.size; -} - -void secp256k1_sign(const uint8_t* msg, uint8_t* serialized_sig, - uint8_t* pubkey_hash) { - int ret = 0; - secp256k1_pubkey pubkey; - secp256k1_ecdsa_recoverable_signature sig; - uint8_t key[32] = {0}; - for (size_t i = 0; i < sizeof(key); i++) { - key[i] = i; - } - int recid = 0; - - size_t serialized_pubkey_len = 33; - uint8_t serialized_pubkey[33]; - - secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | - SECP256K1_CONTEXT_VERIFY); - ret = secp256k1_ecdsa_sign_recoverable(ctx, &sig, msg, key, NULL, NULL); - ASSERT(ret); - - ret = secp256k1_ecdsa_recoverable_signature_serialize_compact( - ctx, serialized_sig, &recid, &sig); - ASSERT(ret); - serialized_sig[64] = recid; - - ret = secp256k1_ec_pubkey_create(ctx, &pubkey, key); - ASSERT(ret); - ret = secp256k1_ec_pubkey_serialize(ctx, serialized_pubkey, - &serialized_pubkey_len, &pubkey, - SECP256K1_EC_COMPRESSED); - ASSERT(ret); - - blake2b_state blake2b_ctx; - blake2b_init(&blake2b_ctx, 32); - blake2b_update(&blake2b_ctx, serialized_pubkey, serialized_pubkey_len); - blake2b_final(&blake2b_ctx, pubkey_hash, 32); - -#if 0 - printf("{"); - for (int i = 0; i < 20; i++) { - printf("%d,", pubkey_hash[i]); - } - printf("}\n"); -#endif - // verify, self testing - { - secp256k1_ecdsa_recoverable_signature signature; - ret = secp256k1_ecdsa_recoverable_signature_parse_compact( - ctx, &signature, serialized_sig, serialized_sig[64]); - ASSERT(ret); - - secp256k1_pubkey pubkey2; - - ret = secp256k1_ecdsa_recover(ctx, &pubkey2, &signature, msg); - ASSERT(ret); - - uint8_t temp[33]; - size_t temp_size = sizeof(temp); - ret = secp256k1_ec_pubkey_serialize(ctx, temp, &temp_size, &pubkey2, - SECP256K1_EC_COMPRESSED); - ASSERT(ret); - ASSERT(temp_size == 33); - ret = memcmp(serialized_pubkey, temp, temp_size); - ASSERT(ret == 0); - } -} - -static int extract_witness_lock2(uint8_t* witness, uint64_t len, - mol_seg_t* lock_bytes_seg) { - if (len < 20) { - return 1; - } - uint32_t lock_length = *((uint32_t*)(&witness[16])); - if (len < 20 + lock_length) { - return 1; - } else { - lock_bytes_seg->ptr = &witness[20]; - lock_bytes_seg->size = lock_length; - } - return 0; -} - -int calculate_hash(uint8_t* msg) { - int ret; - uint64_t len = 0; - unsigned char temp[VERY_LONG_DATA_SIZE + 1024]; - uint64_t witness_len = VERY_LONG_DATA_SIZE + 1024; - /* Load witness of first input */ - ret = ckb_load_witness(temp, &witness_len, 0, 0, CKB_SOURCE_GROUP_INPUT); - ASSERT(ret == 0); - - /* load signature */ - mol_seg_t lock_bytes_seg; - ret = extract_witness_lock2(temp, witness_len, &lock_bytes_seg); - ASSERT(ret == 0); - - /* Load tx hash */ - unsigned char tx_hash[32]; - len = 32; - ret = ckb_load_tx_hash(tx_hash, &len, 0); - ASSERT(ret == 0); - /* Prepare sign message */ - blake2b_state blake2b_ctx; - blake2b_init(&blake2b_ctx, 32); - blake2b_update(&blake2b_ctx, tx_hash, 32); - - /* Clear lock field to zero, then digest the first witness */ - memset((void*)lock_bytes_seg.ptr, 0, lock_bytes_seg.size); - blake2b_update(&blake2b_ctx, (char*)&witness_len, sizeof(uint64_t)); - blake2b_update(&blake2b_ctx, temp, witness_len); - - blake2b_final(&blake2b_ctx, msg, 32); - return 0; -} - -int ckb_exit(int8_t code) { - exit(code); - return 0; -} - -int ckb_load_tx_hash(void* addr, uint64_t* len, size_t offset) { - ASSERT(offset == 0); - ASSERT(*len == 32); - for (int i = 0; i < *len; i++) { - ((uint8_t*)addr)[i] = 0; - } - return 0; -} - -int ckb_checked_load_script(void* addr, uint64_t* len, size_t offset); - -int ckb_load_cell(void* addr, uint64_t* len, size_t offset, size_t index, - size_t source); - -int ckb_load_input(void* addr, uint64_t* len, size_t offset, size_t index, - size_t source); - -int ckb_load_header(void* addr, uint64_t* len, size_t offset, size_t index, - size_t source); - -int ckb_load_script_hash(void* addr, uint64_t* len, size_t offset) { return 0; } - -int ckb_checked_load_script_hash(void* addr, uint64_t* len, size_t offset) { - uint64_t old_len = *len; - int ret = ckb_load_script_hash(addr, len, offset); - if (ret == CKB_SUCCESS && (*len) > old_len) { - ret = CKB_LENGTH_NOT_ENOUGH; - } - return ret; -} - -int ckb_load_witness(void* addr, uint64_t* len, size_t offset, size_t index, - size_t source) { - if (index > 0) { - return CKB_INDEX_OUT_OF_BOUND; - } - - if (g_setting.empty_witness) { - *len = 0; - return 0; - } - - slice_t seg = g_states.witness[0]; - - if (addr == NULL) { - *len = seg.size; - return 0; - } - if (seg.size <= offset) { - *len = 0; - return 0; - } - uint32_t remaining = seg.size - offset; - if (remaining > *len) { - memcpy(addr, seg.ptr + offset, *len); - } else { - memcpy(addr, seg.ptr + offset, remaining); - } - *len = remaining; - - return 0; -} - -int ckb_checked_load_witness(void* addr, uint64_t* len, size_t offset, - size_t index, size_t source) { - uint64_t old_len = *len; - int ret = ckb_load_witness(addr, len, offset, index, source); - if (ret == CKB_SUCCESS && (*len) > old_len) { - ret = CKB_LENGTH_NOT_ENOUGH; - } - return ret; -} - -mol_seg_t build_bytes(const uint8_t* data, uint32_t len) { - mol_builder_t b; - mol_seg_res_t res; - MolBuilder_Bytes_init(&b); - for (uint32_t i = 0; i < len; i++) { - MolBuilder_Bytes_push(&b, data[i]); - } - res = MolBuilder_Bytes_build(b); - return res.seg; -} - -mol_seg_t build_script(const uint8_t* code_hash, uint8_t hash_type, - const uint8_t* args, uint32_t args_len) { - mol_builder_t b; - mol_seg_res_t res; - MolBuilder_Script_init(&b); - - MolBuilder_Script_set_code_hash(&b, code_hash, 32); - MolBuilder_Script_set_hash_type(&b, hash_type); - mol_seg_t bytes = build_bytes(args, args_len); - MolBuilder_Script_set_args(&b, bytes.ptr, bytes.size); - - res = MolBuilder_Script_build(b); - assert(res.errno == 0); - assert(MolReader_Script_verify(&res.seg, false) == 0); - free(bytes.ptr); - return res.seg; -} - -int ckb_checked_load_script(void* addr, uint64_t* len, size_t offset) { - ASSERT(offset == 0); - ASSERT(*len > g_states.script.size); - - memcpy(addr, g_states.script.ptr, g_states.script.size); - *len = g_states.script.size; - return 0; -} - -int ckb_load_script(void* addr, uint64_t* len, size_t offset) { - ASSERT(offset == 0); - ASSERT(*len > g_states.script.size); - - memcpy(addr, g_states.script.ptr, g_states.script.size); - *len = g_states.script.size; - return 0; -} - -int ckb_load_cell_by_field(void* addr, uint64_t* len, size_t offset, - size_t index, size_t source, size_t field); - -int ckb_load_header_by_field(void* addr, uint64_t* len, size_t offset, - size_t index, size_t source, size_t field); - -int ckb_load_input_by_field(void* addr, uint64_t* len, size_t offset, - size_t index, size_t source, size_t field) { - return -1; -} - -int ckb_load_cell_code(void* addr, size_t memory_size, size_t content_offset, - size_t content_size, size_t index, size_t source); - -// follow store_data(machine: &mut Mac, data: &[u8]) -int store_data(slice_t* cur, void* addr, uint64_t* len, size_t offset) { - if (addr == NULL) { - *len = cur->size - offset; - return 0; - } - offset = cur->size > offset ? offset : cur->size; - uint32_t full_size = cur->size - offset; - uint32_t real_size = *len > full_size ? full_size : *len; - memcpy(addr, cur->ptr + offset, real_size); - *len = full_size; - return 0; -} - -int ckb_load_cell_data(void* addr, uint64_t* len, size_t offset, size_t index, - size_t source) { - if (source == CKB_SOURCE_CELL_DEP && index == SPECIAL_SECP256K1_INDEX) { - ASSERT(*len == 1048576); - FILE* input = fopen("build/secp256k1_data", "rb"); - size_t read_item = fread(addr, *len, 1, input); - ASSERT(read_item == 1); - - return 0; - } - - if (source == CKB_SOURCE_CELL_DEP) { - ASSERT(index < g_states.cell_data_count); - slice_t* cur = g_states.cell_data + index; - return store_data(cur, addr, len, offset); - } - if (source == CKB_SOURCE_OUTPUT || source == CKB_SOURCE_INPUT) { - slice_t* cur = NULL; - if (source == CKB_SOURCE_INPUT) { - if (index < g_states.input_cells_count) { - cur = &g_states.input_cells[index].data; - } else { - return CKB_INDEX_OUT_OF_BOUND; - } - } - if (source == CKB_SOURCE_OUTPUT) { - if (index < g_states.output_cells_count) { - cur = &g_states.output_cells[index].data; - } else { - return CKB_INDEX_OUT_OF_BOUND; - } - } - return store_data(cur, addr, len, offset); - } - return CKB_LENGTH_NOT_ENOUGH; -} - -int ckb_checked_load_cell_data(void* addr, uint64_t* len, size_t offset, - size_t index, size_t source) { - return ckb_load_cell_data(addr, len, offset, index, source); -} - -int ckb_debug(const char* s); - -/* load the actual witness for the current type verify group. - use this instead of ckb_load_witness if type contract needs args to verify - input/output. - */ -int load_actual_type_witness(uint8_t* buf, uint64_t* len, size_t index, - size_t* type_source); - -int ckb_look_for_dep_with_hash(const uint8_t* data_hash, size_t* index); - -int ckb_calculate_inputs_len() { return 1; } - -int ckb_load_cell_by_field(void* addr, uint64_t* len, size_t offset, - size_t index, size_t source, size_t field) { - if (field == CKB_CELL_FIELD_LOCK_HASH) { - if (source == CKB_SOURCE_GROUP_OUTPUT || source == CKB_SOURCE_OUTPUT) { - ASSERT(false); - } else if (source == CKB_SOURCE_INPUT) { - ASSERT(offset == 0); - ASSERT(*len >= 32); - if (index >= g_states.setting.input_lsh_count) { - return CKB_INDEX_OUT_OF_BOUND; - } - memcpy(addr, g_states.setting.input_lsh[index].ptr, 32); - *len = 32; - } else { - ASSERT(false); - } - return 0; - } else if (source == CKB_SOURCE_CELL_DEP && - field == CKB_CELL_FIELD_DATA_HASH) { - if (index == SPECIAL_SECP256K1_INDEX) { - static uint8_t ckb_secp256k1_data_hash[32] = { - 151, 153, 190, 226, 81, 185, 117, 184, 44, 69, 160, - 33, 84, 206, 40, 206, 200, 156, 88, 83, 236, 193, - 77, 18, 183, 184, 204, 207, 193, 158, 10, 244}; - memcpy(addr, ckb_secp256k1_data_hash, 32); - } else { - memset(addr, 0, 32); - } - return 0; - } else if (field == CKB_CELL_FIELD_TYPE_HASH) { - if (source == CKB_SOURCE_INPUT) { - if (index < g_states.input_cells_count) { - ASSERT(*len >= 32 && offset == 0); - memcpy(addr, g_states.input_cells[index].type_script_hash, 32); - *len = 32; - return 0; - } else { - return CKB_INDEX_OUT_OF_BOUND; - } - } else if (source == CKB_SOURCE_OUTPUT) { - if (index < g_states.output_cells_count) { - ASSERT(*len >= 32 && offset == 0); - memcpy(addr, g_states.output_cells[index].type_script_hash, 32); - *len = 32; - return 0; - } else { - return CKB_INDEX_OUT_OF_BOUND; - } - } else { - ASSERT(false); - } - return 0; - } else { - ASSERT(false); - } - return -1; -} - -int ckb_checked_load_cell_by_field(void* addr, uint64_t* len, size_t offset, - size_t index, size_t source, size_t field) { - uint64_t old_len = *len; - int ret = ckb_load_cell_by_field(addr, len, offset, index, source, field); - if (ret == 0 && (*len) > old_len) { - ret = CKB_LENGTH_NOT_ENOUGH; - } - return ret; -} - -int ckb_look_for_dep_with_hash2(const uint8_t* code_hash, uint8_t hash_type, - size_t* index) { - *index = *(uint16_t*)code_hash; - return 0; -} - -#include - -#define ERROR_MEMORY_NOT_ENOUGH -23 -#define ERROR_DYNAMIC_LOADING -24 -#define RISCV_PGSIZE 4096 -#define ROUNDUP(a, b) ((((a)-1) / (b) + 1) * (b)) -#define MAX_PATH_SIZE 1024 - -typedef struct LibMappingEntry { - uint8_t dep_cell_hash[32]; - char path[MAX_PATH_SIZE]; -} LibMappingEntry; - -#define MAX_LIB_MAPPING_COUNT 1024 -LibMappingEntry g_lib_mapping[MAX_LIB_MAPPING_COUNT]; -int g_lib_size = 0; - -void ckbsim_map_lib(const uint8_t* dep_cell_hash, const char* path) { - if (g_lib_size >= MAX_LIB_MAPPING_COUNT) { - ASSERT(false); - return; - } - ASSERT(strlen(path) < MAX_PATH_SIZE); - - memcpy(g_lib_mapping[g_lib_size].dep_cell_hash, dep_cell_hash, 32); - strcpy(g_lib_mapping[g_lib_size].path, path); - - g_lib_size++; -} - -bool file_exists(const char* path) { - FILE* fp = fopen(path, "r"); - if (fp != NULL) { - fclose(fp); - }; - return fp != NULL; -} - -void file_with_so(const char* input, char* output, uint32_t output_len) { - strcpy(output, input); - char* pos = strchr(output, '.'); - if (pos != NULL) { - *pos = 0; - strcat(output, ".so"); - } -} - -int ckbsim_get_lib(const uint8_t* dep_cell_hash, char* path) { - for (int i = 0; i < g_lib_size; i++) { - if (memcmp(g_lib_mapping[i].dep_cell_hash, dep_cell_hash, 32) == 0) { - const char* target = g_lib_mapping[i].path; - if (file_exists(target)) { - strcpy(path, target); - } else { - char output[1024] = {0}; - file_with_so(target, output, sizeof(output)); - if (file_exists(output)) { - strcpy(path, output); - } else { - ASSERT(false); - return -1; - } - } - return 0; - } - } - return 1; -} - -size_t get_file_size(const char* path) { - FILE* fp = fopen(path, "r"); - ASSERT(fp != NULL); - fseek(fp, 0L, SEEK_END); - long size = ftell(fp); - fclose(fp); - - return size; -} - -int ckb_dlopen2(const uint8_t* dep_cell_hash, uint8_t hash_type, - uint8_t* aligned_addr, size_t aligned_size, void** handle, - size_t* consumed_size) { - int err = 0; - (void)hash_type; - (void)aligned_size; - - char path[MAX_PATH_SIZE] = {0}; - ASSERT((aligned_size % RISCV_PGSIZE) == 0); - ASSERT(((size_t)aligned_addr) % RISCV_PGSIZE == 0); - - err = ckbsim_get_lib(dep_cell_hash, path); - ASSERT(err == 0); - - *handle = dlopen(path, RTLD_NOW); - *consumed_size = 0; - - if (*handle == NULL) { - printf("Error occurs in dlopen: %s\n", dlerror()); - ASSERT(false); - return -1; - } - return 0; -} - -void* ckb_dlsym(void* handle, const char* symbol) { - void* ret = dlsym(handle, symbol); - ASSERT(ret != NULL); - return ret; -} - -int ckb_exec_cell(const uint8_t* code_hash, uint8_t hash_type, uint32_t offset, - uint32_t length, int argc, const char* argv[]) { - return 0; -} - -#undef ASSERT -#define ASSERT(s) (void)0 - -#endif diff --git a/tests/omni_lock/exec_fuzzer/.gitignore b/tests/omni_lock/exec_fuzzer/.gitignore deleted file mode 100644 index a6a70af..0000000 --- a/tests/omni_lock/exec_fuzzer/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -fuzz-*.log -exec_fuzzer -exec_coverage -crash-* diff --git a/tests/omni_lock/exec_fuzzer/Makefile b/tests/omni_lock/exec_fuzzer/Makefile deleted file mode 100644 index cba0089..0000000 --- a/tests/omni_lock/exec_fuzzer/Makefile +++ /dev/null @@ -1,64 +0,0 @@ -OS = Unknown -ifneq ($(shell uname -a | grep -i Darwin),) - OS = MacOS -endif -ifneq ($(shell uname -a | grep -i Linux),) - OS = Linux -endif -ifeq ($(OS),Unknown) - echo "error: unsupported OS"; exit 1 -endif - -NPROC?=4 -CC=clang -LLVM_PROFDATA=llvm-profdata -LLVM_COV=llvm-cov - -CORPUS_DIR=corpus - -FUZZER_FLAGS=-g -O1 -fsanitize=fuzzer,address,undefined -fsanitize-recover=address -I ../../../c -I ../../../deps/ckb-c-stdlib-20210801 - -COVERAGE_DIR=coverage -COVERAGE_FLAGS=-fprofile-instr-generate -fcoverage-mapping -I ../../../c -I ../../../deps/ckb-c-stdlib-20210801 - -ifeq ($(OS),MacOS) - COVERAGE_FLAGS+=-Wl,-U,_LLVMFuzzerCustomMutator -Wl,-U,_LLVMFuzzerInitialize -endif - -EXTERNAL_HEADERS=../../../deps/ckb-c-stdlib-20210801/ckb_exec.h - -all: fuzzer coverage - -show: $(COVERAGE_DIR)/fuzzer.profdata - $(LLVM_COV) show --instr-profile=$(COVERAGE_DIR)/fuzzer.profdata exec_coverage - -report: $(COVERAGE_DIR)/fuzzer.profdata coverage $(EXTERNAL_HEADERS) - $(LLVM_COV) report --show-functions --instr-profile=$(COVERAGE_DIR)/fuzzer.profdata exec_coverage $(EXTERNAL_HEADERS) - -fuzzer: $(EXTERNAL_HEADERS) - clang $(FUZZER_FLAGS) exec_fuzzer.c -o exec_fuzzer - -coverage: $(EXTERNAL_HEADERS) - clang $(COVERAGE_FLAGS) exec_coverage.c exec_fuzzer.c -o exec_coverage - -start-fuzzer: fuzzer - ASAN_OPTIONS=halt_on_error=0 ./exec_fuzzer -max_len=50000 -len_control=2 -workers=$(NPROC) -jobs=$(NPROC) corpus - -start-fuzzer2: fuzzer - ./exec_fuzzer -max_len=50000 -len_control=2 corpus - -clean: - rm -rf exec_fuzzer exec_coverage exec_fuzzer.dSYM - -#%.h: -# ln -s $(CURDIR)/../$@ $(CURDIR)/$@ - -%.profraw: coverage - LLVM_PROFILE_FILE=$@ ./exec_coverage $(CORPUS_DIR)/* - -%.profdata: %.profraw - $(LLVM_PROFDATA) merge --sparse $< -o $@ - -.PHONY: all fuzzer coverage report - -.PRECIOUS: $(COVERAGE_DIR)/fuzzer.profraw $(COVERAGE_DIR)/fuzzer.profdata diff --git a/tests/omni_lock/exec_fuzzer/exec_coverage.c b/tests/omni_lock/exec_fuzzer/exec_coverage.c deleted file mode 100644 index 87e372e..0000000 --- a/tests/omni_lock/exec_fuzzer/exec_coverage.c +++ /dev/null @@ -1,41 +0,0 @@ -/*===- StandaloneFuzzTargetMain.c - standalone main() for fuzz targets. ---===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// This main() function can be linked to a fuzz target (i.e. a library -// that exports LLVMFuzzerTestOneInput() and possibly LLVMFuzzerInitialize()) -// instead of libFuzzer. This main() function will not perform any fuzzing -// but will simply feed all input files one by one to the fuzz target. -// -// Use this file to provide reproducers for bugs when linking against libFuzzer -// or other fuzzing engine is undesirable. -//===----------------------------------------------------------------------===*/ -#include -#include -#include - -extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size); -__attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv); - -int main(int argc, char **argv) { - fprintf(stderr, "StandaloneFuzzTargetMain: running %d inputs\n", argc - 1); - // if (LLVMFuzzerInitialize) LLVMFuzzerInitialize(&argc, &argv); - for (int i = 1; i < argc; i++) { - fprintf(stderr, "Running: %s\n", argv[i]); - FILE *f = fopen(argv[i], "r"); - assert(f); - fseek(f, 0, SEEK_END); - size_t len = ftell(f); - fseek(f, 0, SEEK_SET); - unsigned char *buf = (unsigned char *)malloc(len); - size_t n_read = fread(buf, 1, len, f); - fclose(f); - assert(n_read == len); - LLVMFuzzerTestOneInput(buf, len); - free(buf); - fprintf(stderr, "Done: %s: (%zd bytes)\n", argv[i], n_read); - } -} diff --git a/tests/omni_lock/exec_fuzzer/exec_func.h b/tests/omni_lock/exec_fuzzer/exec_func.h deleted file mode 100644 index 5dbfdf8..0000000 --- a/tests/omni_lock/exec_fuzzer/exec_func.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef XUDT_RCE_SIMULATOR_TESTS_XUDT_RCE_SMT_FUZZER_SMT_FUNC_H_ -#define XUDT_RCE_SIMULATOR_TESTS_XUDT_RCE_SMT_FUZZER_SMT_FUNC_H_ -#include -#include -#include -#include "ckb_exec.h" - - -int exec_func2(uint8_t* data, size_t size) { - int err = 0; - - char buff[size]; - memcpy(buff, data, size); - - char* next = buff; - uint8_t* param = NULL; - uint32_t len = 0; - - while (true) { - err = ckb_exec_decode_params(next, ¶m, &len, &next); - if (err != 0) - break; - } - - return 0; -} - - -int exec_func(uint8_t* data, size_t size) { - int err = 0; - - if (size < 200) - return 1; - - CkbBinaryArgsType bin = {0}; - ckb_exec_reset(&bin); - - uint8_t input_len = *data % 70; - uint8_t* end = data + size; - uint8_t* ptr = data + 1; - for (int i = 0; i < input_len; i++) { - uint32_t len = *ptr; - ptr++; - if ((ptr + len) >= end) - break; - err = ckb_exec_append(&bin, ptr, len); - if (err != 0) - break; - } - - CkbHexArgsType hex; - err = ckb_exec_encode_params(&bin, &hex); - if (err != 0) - return err; - char* next = hex.buff; - - uint8_t* param = NULL; - uint32_t len = 0; - while (true) { - err = ckb_exec_decode_params(next, ¶m, &len, &next); - if (err != 0) - break; - } - - return 0; -} - -#endif //XUDT_RCE_SIMULATOR_TESTS_XUDT_RCE_SMT_FUZZER_SMT_FUNC_H_ diff --git a/tests/omni_lock/exec_fuzzer/exec_fuzzer.c b/tests/omni_lock/exec_fuzzer/exec_fuzzer.c deleted file mode 100644 index 7aef350..0000000 --- a/tests/omni_lock/exec_fuzzer/exec_fuzzer.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "exec_func.h" - - -int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { - exec_func(data, size); - exec_func2(data, size); - return 0; -} diff --git a/tests/omni_lock/omni_lock_sim.c b/tests/omni_lock/omni_lock_sim.c deleted file mode 100644 index 1f94ed9..0000000 --- a/tests/omni_lock/omni_lock_sim.c +++ /dev/null @@ -1,686 +0,0 @@ -#if defined(CKB_COVERAGE) -#define ASSERT(s) (void)0 -#else -#define ASSERT(s) (void)0 -#endif - -int ckb_exit(signed char code); - -#include "omni_lock.c" -#include "utest.h" -void debug_print_hex(const char* prefix, const uint8_t* buf, size_t length) { - printf("%s: ", prefix); - for (size_t i = 0; i < length; i++) { - printf("%02x ", buf[i]); - } - printf("\n"); -} - -/* hex2bin modified from - * https://chromium.googlesource.com/chromium/deps/xz/+/77022065014d48cf51d83322264ab4836fd175ec/debug/hex2bin.c - */ -int getbin(int x) { - if (x >= '0' && x <= '9') return x - '0'; - if (x >= 'A' && x <= 'F') return x - 'A' + 10; - return x - 'a' + 10; -} - -int hex2bin(uint8_t* buf, const char* src) { - size_t length = strlen(src) / 2; - if (src[0] == '0' && (src[1] == 'x' || src[1] == 'X')) { - src += 2; - length--; - } - for (size_t i = 0; i < length; i++) { - buf[i] = (getbin(src[i * 2]) << 4) | getbin(src[i * 2 + 1]); - } - return length; -} - -UTEST(pubkey_hash, pass) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsCkb; - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(0, r); -} - -UTEST(pubkey_hash, wrong_signature) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsCkb; - - g_setting.wrong_signature = true; - convert_setting_to_states(); - - int r = simulator_main(); - bool b = (r == ERROR_IDENTITY_PUBKEY_BLAKE160_HASH || - r == ERROR_IDENTITY_SECP_RECOVER_PUBKEY || - r == ERROR_IDENTITY_SECP_PARSE_SIGNATURE); - ASSERT_TRUE(b); -} - -UTEST(pubkey_hash, wrong_pubkey_hash) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsCkb; - - g_setting.wrong_pubkey_hash = true; - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_IDENTITY_PUBKEY_BLAKE160_HASH, r); -} - -UTEST(owner_lock, pass) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsOwnerLock; - - uint8_t blake160[20] = {0xBE, 0xEF}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(0, r); -} - -UTEST(owner_lock_without_witness, pass) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsOwnerLock; - g_setting.empty_witness = true; - - uint8_t blake160[20] = {0xBE, 0xEF}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(0, r); -} - -UTEST(owner_lock, not_pass) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsOwnerLock; - - uint8_t blake160[20] = {0xBE, 0xEF}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - g_setting.input_lsh_count = 1; - - g_setting.blake160[0] = 0x00; - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_IDENTITY_LOCK_SCRIPT_HASH_NOT_FOUND, r); -} - -void set_smt_settings(uint8_t flags, slice_t* rc_rules, slice_t* proofs, - uint8_t* smt_flags, size_t count) { - g_setting.flags = flags; - g_setting.proof_count = count; - for (size_t i = 0; i < count; i++) { - g_setting.proofs[i] = copy_slice(proofs[i].ptr, proofs[i].size); - memcpy(g_setting.rc_rules[i].smt_root, rc_rules[i].ptr, rc_rules[i].size); - g_setting.rc_rules[i].flags = smt_flags[i]; // white list - } -} - -UTEST(owner_lock_rc, on_wl_pass) { - init_input(&g_setting); - - uint8_t rcrule[] = {159, 133, 75, 106, 72, 73, 103, 41, 196, 8, 212, - 55, 174, 168, 36, 255, 109, 95, 175, 167, 147, 30, - 228, 18, 129, 140, 100, 52, 22, 147, 205, 107}; - uint8_t proof[] = {76, 79, 0}; - - slice_t proof_slice = copy_slice(proof, sizeof(proof)); - slice_t rcrule_slice = copy_slice(rcrule, sizeof(rcrule)); - uint8_t smt_flags = 0x2; // white list - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsOwnerLock, &rcrule_slice, &proof_slice, - &smt_flags, 1); - - // the true smt key will be {252(IdentityFlagsOwnerLockRc), 11, 0, 0, ...} - uint8_t blake160[20] = {11}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(0, r); -} - -UTEST(owner_lock_rc, no_rcrule_not_pass) { - init_input(&g_setting); - - g_setting.flags = IdentityFlagsOwnerLock; - g_setting.use_rc = true; - - uint8_t blake160[20] = {11}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_NO_OMNIRULE, r); -} - -UTEST(owner_lock_rc, one_white_list_not_pass) { - init_input(&g_setting); - - uint8_t rcrule[] = {211, 74, 208, 180, 70, 53, 171, 243, 13, 204, 148, - 95, 152, 163, 82, 95, 176, 182, 11, 34, 193, 72, - 23, 199, 82, 246, 171, 83, 254, 32, 178, 80}; - uint8_t proof[] = {76, 80, 12, 158, 217, 132, 249, 31, 18, 82, 14, 27, - 234, 204, 18, 228, 250, 0, 102, 243, 203, 175, 243, 145, - 127, 36, 142, 33, 32, 101, 195, 51, 115, 252, 192}; - - rcrule[0] ^= 0x1; // make it not pass - - slice_t proof_slice = copy_slice(proof, sizeof(proof)); - slice_t rcrule_slice = copy_slice(rcrule, sizeof(rcrule)); - uint8_t smt_flags = 0x2; // white list - - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsOwnerLock, &rcrule_slice, &proof_slice, - &smt_flags, 1); - - // the true smt key will be {3(IdentityFlagsOwnerLockRc), 11, 0, 0, ...} - uint8_t blake160[20] = {11}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_NOT_ON_WHITE_LIST, r); -} - -UTEST(owner_lock_rc, on_wl_and_not_on_bl_pass) { - init_input(&g_setting); - - uint8_t wl_rcrule[] = {159, 133, 75, 106, 72, 73, 103, 41, 196, 8, 212, - 55, 174, 168, 36, 255, 109, 95, 175, 167, 147, 30, - 228, 18, 129, 140, 100, 52, 22, 147, 205, 107}; - uint8_t wl_proof[] = {76, 79, 0}; - - uint8_t bl_rcrule[] = {168, 80, 89, 3, 35, 129, 5, 27, 65, 82, 213, - 117, 185, 162, 43, 149, 213, 164, 98, 184, 94, 65, - 91, 135, 214, 227, 189, 4, 220, 178, 227, 42}; - uint8_t bl_proof[] = { - 76, 79, 7, 81, 7, 63, 58, 65, 167, 46, 21, 220, 77, 188, 92, - 185, 239, 12, 35, 148, 150, 255, 14, 165, 64, 239, 20, 200, 146, 49, - 217, 63, 118, 168, 100, 220, 216, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 79, 248}; - - slice_t rcrules[2] = {copy_slice(wl_rcrule, sizeof(wl_rcrule)), - copy_slice(bl_rcrule, sizeof(bl_rcrule))}; - slice_t proofs[2] = {copy_slice(wl_proof, sizeof(wl_proof)), - copy_slice(bl_proof, sizeof(bl_proof))}; - uint8_t smt_flags[2] = {0x2, 0x0}; // white list, black list - - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsOwnerLock, rcrules, proofs, smt_flags, - countof(smt_flags)); - - // the true smt key will be {252(IdentityFlagsOwnerLockRc), 11, 0, 0, ...} - uint8_t blake160[20] = {11}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(0, r); -} - -UTEST(owner_lock_rc, not_on_bl_not_pass) { - init_input(&g_setting); - - uint8_t bl_rcrule[] = {151, 81, 37, 242, 189, 99, 62, 175, 115, 9, 251, - 94, 105, 190, 173, 153, 42, 249, 87, 115, 253, 152, - 110, 88, 1, 58, 224, 21, 51, 99, 72, 182}; - uint8_t bl_proof[] = {76, 80, 11, 151, 81, 37, 242, 189, 99, - 62, 175, 115, 9, 251, 94, 105, 190, 173, - 153, 42, 249, 87, 115, 253, 152, 110, 88, - 1, 58, 224, 21, 51, 99, 72, 182}; - - slice_t rcrules[1] = {copy_slice(bl_rcrule, sizeof(bl_rcrule))}; - slice_t proofs[1] = {copy_slice(bl_proof, sizeof(bl_proof))}; - uint8_t smt_flags[1] = {0x0}; - - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsOwnerLock, rcrules, proofs, smt_flags, - countof(smt_flags)); - - // the true smt key will be {3(IdentityFlagsOwnerLockRc), 11, 0, 0, ...} - uint8_t blake160[20] = {11}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_NO_WHITE_LIST, r); -} - -UTEST(owner_lock_rc, on_wl_and_on_bl_not_pass) { - init_input(&g_setting); - - uint8_t wl_rcrule[] = {211, 74, 208, 180, 70, 53, 171, 243, 13, 204, 148, - 95, 152, 163, 82, 95, 176, 182, 11, 34, 193, 72, - 23, 199, 82, 246, 171, 83, 254, 32, 178, 80}; - uint8_t wl_proof[] = {76, 80, 12, 158, 217, 132, 249, 31, 18, - 82, 14, 27, 234, 204, 18, 228, 250, 0, - 102, 243, 203, 175, 243, 145, 127, 36, 142, - 33, 32, 101, 195, 51, 115, 252, 192}; - - uint8_t bl_rcrule[] = {151, 81, 37, 242, 189, 99, 62, 175, 115, 9, 251, - 94, 105, 190, 173, 153, 42, 249, 87, 115, 253, 152, - 110, 88, 1, 58, 224, 21, 51, 99, 72, 182}; - uint8_t bl_proof[] = {76, 80, 11, 151, 81, 37, 242, 189, 99, - 62, 175, 115, 9, 251, 94, 105, 190, 173, - 153, 42, 249, 87, 115, 253, 152, 110, 88, - 1, 58, 224, 21, 51, 99, 72, 182}; - - bl_rcrule[0] ^= 0x1; // make "verify it not on black list" failed - - slice_t rcrules[2] = {copy_slice(wl_rcrule, sizeof(wl_rcrule)), - copy_slice(bl_rcrule, sizeof(bl_rcrule))}; - slice_t proofs[2] = {copy_slice(wl_proof, sizeof(wl_proof)), - copy_slice(bl_proof, sizeof(bl_proof))}; - uint8_t smt_flags[2] = {0x2, 0x0}; // white list, black list - - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsOwnerLock, rcrules, proofs, smt_flags, - countof(smt_flags)); - - // the true smt key will be {3(IdentityFlagsOwnerLockRc), 11, 0, 0, ...} - uint8_t blake160[20] = {11}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_ON_BLACK_LIST, r); -} - -UTEST(pubkey_hash_rc, on_wl_pass) { - init_input(&g_setting); - - uint8_t rcrule[] = {241, 250, 221, 178, 97, 206, 227, 76, 247, 86, 15, - 13, 224, 40, 106, 171, 39, 247, 249, 55, 102, 207, - 117, 5, 60, 58, 55, 240, 44, 60, 255, 96}; - uint8_t proof[] = {76, 79, 0}; - - slice_t proof_slice = copy_slice(proof, sizeof(proof)); - slice_t rcrule_slice = copy_slice(rcrule, sizeof(rcrule)); - uint8_t smt_flags = 0x2; // white list - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsCkb, &rcrule_slice, &proof_slice, &smt_flags, - 1); - - // verify "0, " is on white list - // blake160 of pubkey - uint8_t blake160[20] = {184, 141, 45, 152, 205, 241, 253, 130, 123, 22, - 22, 120, 131, 153, 185, 150, 222, 107, 17, 248}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(0, r); -} - -UTEST(pubkey_hash_rc, on_wl_not_on_bl_pass) { - init_input(&g_setting); - - uint8_t wl_rcrule[] = {241, 250, 221, 178, 97, 206, 227, 76, 247, 86, 15, - 13, 224, 40, 106, 171, 39, 247, 249, 55, 102, 207, - 117, 5, 60, 58, 55, 240, 44, 60, 255, 96}; - uint8_t wl_proof[] = {76, 79, 0}; - - uint8_t bl_rcrule[] = {220, 30, 240, 125, 208, 148, 191, 239, 30, 182, 138, - 224, 75, 161, 67, 129, 152, 18, 247, 112, 183, 207, - 53, 232, 242, 129, 198, 64, 228, 117, 165, 107}; - uint8_t bl_proof[] = { - 76, 79, 167, 81, 167, 51, 82, 32, 99, 102, 127, 50, 182, 204, 35, - 239, 150, 110, 13, 253, 100, 7, 66, 92, 124, 215, 234, 135, 21, 99, - 3, 231, 190, 156, 195, 122, 79, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 79, 88}; - - slice_t rcrule_slice[] = {copy_slice(wl_rcrule, sizeof(wl_rcrule)), - copy_slice(bl_rcrule, sizeof(bl_rcrule))}; - slice_t proof_slice[] = {copy_slice(wl_proof, sizeof(wl_proof)), - copy_slice(bl_proof, sizeof(bl_proof))}; - uint8_t smt_flags[] = {0x2, 0}; - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsCkb, rcrule_slice, proof_slice, smt_flags, - countof(smt_flags)); - - // verify "0, " on white list - // verify "0, " not on black list - // blake160 of pubkey - uint8_t blake160[20] = {184, 141, 45, 152, 205, 241, 253, 130, 123, 22, - 22, 120, 131, 153, 185, 150, 222, 107, 17, 248}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(0, r); -} - -UTEST(pubkey_hash_rc, not_bl_not_pass) { - init_input(&g_setting); - - uint8_t bl_proof[] = {76, 80, 167, 151, 81, 37, 242, 189, 99, - 62, 175, 115, 9, 251, 94, 105, 190, 173, - 153, 42, 249, 87, 115, 253, 152, 110, 88, - 1, 58, 224, 21, 51, 99, 72, 182}; - uint8_t bl_rcrule[] = {151, 81, 37, 242, 189, 99, 62, 175, 115, 9, 251, - 94, 105, 190, 173, 153, 42, 249, 87, 115, 253, 152, - 110, 88, 1, 58, 224, 21, 51, 99, 72, 182}; - - slice_t rcrule_slice[] = {copy_slice(bl_rcrule, sizeof(bl_rcrule))}; - slice_t proof_slice[] = {copy_slice(bl_proof, sizeof(bl_proof))}; - uint8_t smt_flags[] = {0}; - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsCkb, rcrule_slice, proof_slice, smt_flags, - countof(smt_flags)); - - // blake160 of pubkey - uint8_t blake160[20] = {184, 141, 45, 152, 205, 241, 253, 130, 123, 22, - 22, 120, 131, 153, 185, 150, 222, 107, 17, 248}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_NO_WHITE_LIST, r); -} - -UTEST(pubkey_hash_rc, on_wl_on_bl_not_pass) { - init_input(&g_setting); - - uint8_t wl_rcrule[] = {180, 114, 189, 251, 177, 152, 11, 244, 252, 16, 169, - 249, 218, 80, 90, 86, 112, 210, 59, 128, 251, 216, - 193, 197, 100, 4, 225, 10, 237, 48, 69, 42}; - uint8_t wl_proof[] = {76, 80, 167, 158, 217, 132, 249, 31, 18, - 82, 14, 27, 234, 204, 18, 228, 250, 0, - 102, 243, 203, 175, 243, 145, 127, 36, 142, - 33, 32, 101, 195, 51, 115, 252, 192}; - - uint8_t bl_proof[] = {76, 80, 167, 151, 81, 37, 242, 189, 99, - 62, 175, 115, 9, 251, 94, 105, 190, 173, - 153, 42, 249, 87, 115, 253, 152, 110, 88, - 1, 58, 224, 21, 51, 99, 72, 182}; - uint8_t bl_rcrule[] = {151, 81, 37, 242, 189, 99, 62, 175, 115, 9, 251, - 94, 105, 190, 173, 153, 42, 249, 87, 115, 253, 152, - 110, 88, 1, 58, 224, 21, 51, 99, 72, 182}; - bl_rcrule[0] ^= 0x1; // make "not on black list" failed - - slice_t rcrule_slice[] = {copy_slice(wl_rcrule, sizeof(wl_rcrule)), - copy_slice(bl_rcrule, sizeof(bl_rcrule))}; - slice_t proof_slice[] = {copy_slice(wl_proof, sizeof(wl_proof)), - copy_slice(bl_proof, sizeof(bl_proof))}; - uint8_t smt_flags[] = {0x2, 0}; - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsCkb, rcrule_slice, proof_slice, smt_flags, - countof(smt_flags)); - - // blake160 of pubkey - uint8_t blake160[20] = {184, 141, 45, 152, 205, 241, 253, 130, 123, 22, - 22, 120, 131, 153, 185, 150, 222, 107, 17, 248}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_ON_BLACK_LIST, r); -} - -UTEST(pubkey_hash_rc, not_on_wl_not_pass) { - init_input(&g_setting); - - uint8_t rcrule[] = {180, 114, 189, 251, 177, 152, 11, 244, 252, 16, 169, - 249, 218, 80, 90, 86, 112, 210, 59, 128, 251, 216, - 193, 197, 100, 4, 225, 10, 237, 48, 69, 42}; - uint8_t proof[] = {76, 80, 167, 158, 217, 132, 249, 31, 18, 82, 14, 27, - 234, 204, 18, 228, 250, 0, 102, 243, 203, 175, 243, 145, - 127, 36, 142, 33, 32, 101, 195, 51, 115, 252, 192}; - rcrule[0] ^= 0x1; - - slice_t proof_slice = copy_slice(proof, sizeof(proof)); - slice_t rcrule_slice = copy_slice(rcrule, sizeof(rcrule)); - uint8_t smt_flags = 0x2; // white list - g_setting.use_rc = true; - set_smt_settings(IdentityFlagsCkb, &rcrule_slice, &proof_slice, &smt_flags, - 1); - - // blake160 of pubkey - uint8_t blake160[20] = {184, 141, 45, 152, 205, 241, 253, 130, 123, 22, - 22, 120, 131, 153, 185, 150, 222, 107, 17, 248}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_NOT_ON_WHITE_LIST, r); -} - -UTEST(pubkey_hash_rc, no_rcrule_not_pass) { - init_input(&g_setting); - g_setting.use_rc = true; - g_setting.flags = IdentityFlagsCkb; - - uint8_t blake160[20] = {11}; - - g_setting.input_lsh[0] = new_slice(32); - memcpy(g_setting.input_lsh[0].ptr, blake160, sizeof(blake160)); - g_setting.input_lsh[0].size = 32; - - g_setting.input_lsh_count = 1; - memcpy(g_setting.blake160, blake160, sizeof(blake160)); - - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_NO_OMNIRULE, r); -} - -UTEST(exec, random) { - int err = 0; - CkbBinaryArgsType bin = {0}; - ckb_exec_reset(&bin); - for (int i = 0; i < 64; i++) { - uint32_t len = i; - uint8_t buff[len]; - for (int j = 0; j < len; j++) { - buff[j] = j & 0xFF; - } - err = ckb_exec_append(&bin, buff, len); - ASSERT_TRUE(err == 0); - } - - CkbHexArgsType hex; - err = ckb_exec_encode_params(&bin, &hex); - ASSERT_TRUE(err == 0); - - char* next_iterate_argv = hex.buff; - uint8_t* param = NULL; - uint32_t len = 0; - for (int i = 0; i < bin.count; i++) { - err = ckb_exec_decode_params(next_iterate_argv, ¶m, &len, - &next_iterate_argv); - ASSERT_EQ(err, 0); - ASSERT_EQ(len, bin.len[i]); - ASSERT_EQ(memcmp(param, bin.params[i], len), 0); - } - ASSERT_EQ(next_iterate_argv, NULL); - err = ckb_exec_decode_params(next_iterate_argv, ¶m, &len, - &next_iterate_argv); - ASSERT_EQ(len, 0); - ASSERT_TRUE(err != 0); -} - -UTEST(supply, pass) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsCkb; - g_setting.use_supply = true; - g_setting.input_current_supply = 1; - g_setting.input_max_supply = 100; - g_setting.output_current_supply = 1; - g_setting.output_max_supply = 100; - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(0, r); -} - -UTEST(supply, max_supply_changed) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsCkb; - g_setting.use_supply = true; - g_setting.input_current_supply = 1; - g_setting.input_max_supply = 101; - g_setting.output_current_supply = 1; - g_setting.output_max_supply = 100; - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(CKB_INVALID_DATA, r); -} - -UTEST(supply, exceed_max_supply) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsCkb; - g_setting.use_supply = true; - g_setting.input_current_supply = 1; - g_setting.input_max_supply = 100; - g_setting.output_current_supply = 201; - g_setting.output_max_supply = 100; - // issue 200, more than max supply 100 - g_setting.input_sudt = 0; - g_setting.output_sudt = 200; - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_EXCEED_SUPPLY, r); -} - -UTEST(supply, issued_amount_not_correct) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsCkb; - g_setting.use_supply = true; - g_setting.input_current_supply = 1; - g_setting.input_max_supply = 100; - g_setting.output_current_supply = 2; - g_setting.output_max_supply = 100; - // issue 9 but actually is 2 - g_setting.input_sudt = 0; - g_setting.output_sudt = 9; - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_SUPPLY_AMOUNT, r); -} - -UTEST(supply, burn_amount) { - init_input(&g_setting); - g_setting.flags = IdentityFlagsCkb; - g_setting.use_supply = true; - g_setting.input_current_supply = 19; - g_setting.input_max_supply = 100; - g_setting.output_current_supply = 10; - g_setting.output_max_supply = 100; - // burn 9 - g_setting.input_sudt = 9; - g_setting.output_sudt = 0; - convert_setting_to_states(); - - int r = simulator_main(); - ASSERT_EQ(ERROR_BURN, r); -} - -UTEST_MAIN(); diff --git a/tests/omni_lock/run.sh b/tests/omni_lock/run.sh deleted file mode 100644 index a1fc4b3..0000000 --- a/tests/omni_lock/run.sh +++ /dev/null @@ -1,12 +0,0 @@ - -set -e -FOLDER=simulator-build-debug -cd "$(dirname "${BASH_SOURCE[0]}")" -mkdir -p ${FOLDER} -cd ${FOLDER} -cmake -DCMAKE_C_COMPILER=clang .. -make all -cd ../../.. -echo "Running tests" -tests/omni_lock/${FOLDER}/omni_lock_simulator -echo "Done" diff --git a/tests/omni_lock/utest.h b/tests/omni_lock/utest.h deleted file mode 100644 index 1d3d832..0000000 --- a/tests/omni_lock/utest.h +++ /dev/null @@ -1,932 +0,0 @@ -/* - The latest version of this library is available on GitHub; - https://github.com/sheredom/utest.h -*/ - -/* - This is free and unencumbered software released into the public domain. - - Anyone is free to copy, modify, publish, use, compile, sell, or - distribute this software, either in source code form or as a compiled - binary, for any purpose, commercial or non-commercial, and by any - means. - - In jurisdictions that recognize copyright laws, the author or authors - of this software dedicate any and all copyright interest in the - software to the public domain. We make this dedication for the benefit - of the public at large and to the detriment of our heirs and - successors. We intend this dedication to be an overt act of - relinquishment in perpetuity of all present and future rights to this - software under copyright law. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - OTHER DEALINGS IN THE SOFTWARE. - - For more information, please refer to -*/ - -#ifndef SHEREDOM_UTEST_H_INCLUDED -#define SHEREDOM_UTEST_H_INCLUDED - -#ifdef _MSC_VER -/* - Disable warning about not inlining 'inline' functions. - TODO: We'll fix this later by not using fprintf within our macros, and - instead use snprintf to a realloc'ed buffer. -*/ -#pragma warning(disable : 4710) - -/* - Disable warning about inlining functions that are not marked 'inline'. - TODO: add a UTEST_NOINLINE onto the macro generated functions to fix this. -*/ -#pragma warning(disable : 4711) -#pragma warning(push, 1) -#endif - -#if defined(_MSC_VER) -typedef __int64 utest_int64_t; -typedef unsigned __int64 utest_uint64_t; -#else -#include -typedef int64_t utest_int64_t; -typedef uint64_t utest_uint64_t; -#endif - -#include -#include -#include -#include - -#if defined(_MSC_VER) -#pragma warning(pop) -#endif - -#if defined(_MSC_VER) -#if defined(_M_IX86) -#define _X86_ -#endif - -#if defined(_M_AMD64) -#define _AMD64_ -#endif - -#pragma warning(push, 1) -#include -#include -#pragma warning(pop) - -#elif defined(__linux__) - -/* - slightly obscure include here - we need to include glibc's features.h, but - we don't want to just include a header that might not be defined for other - c libraries like musl. Instead we include limits.h, which we know on all - glibc distributions includes features.h -*/ -#include - -#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) -#include - -#if ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) -/* glibc is version 2.17 or above, so we can just use clock_gettime */ -#define UTEST_USE_CLOCKGETTIME -#else -#include -#include -#endif -#endif - -#elif defined(__APPLE__) -#include -#endif - -#if defined(_MSC_VER) -#define UTEST_PRId64 "I64d" -#define UTEST_PRIu64 "I64u" -#define UTEST_INLINE __forceinline - -#pragma section(".CRT$XCU", read) -#define UTEST_INITIALIZER(f) \ - static void __cdecl f(void); \ - __declspec(allocate(".CRT$XCU")) void(__cdecl * f##_)(void) = f; \ - static void __cdecl f(void) -#else -#if defined(__linux__) -#if defined(__clang__) -#if __has_warning("-Wreserved-id-macro") -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wreserved-id-macro" -#endif -#endif - -#define __STDC_FORMAT_MACROS 1 - -#if defined(__clang__) -#if __has_warning("-Wreserved-id-macro") -#pragma clang diagnostic pop -#endif -#endif -#endif - -#include - -#define UTEST_PRId64 PRId64 -#define UTEST_PRIu64 PRIu64 -#define UTEST_INLINE inline - -#define UTEST_INITIALIZER(f) \ - static void f(void) __attribute__((constructor)); \ - static void f(void) -#endif - -#if defined(__cplusplus) -#define UTEST_CAST(type, x) static_cast(x) -#define UTEST_PTR_CAST(type, x) reinterpret_cast(x) -#define UTEST_EXTERN extern "C" -#define UTEST_NULL NULL -#else -#define UTEST_CAST(type, x) ((type)x) -#define UTEST_PTR_CAST(type, x) ((type)x) -#define UTEST_EXTERN extern -#define UTEST_NULL 0 -#endif - -#ifdef _MSC_VER -/* - io.h contains definitions for some structures with natural padding. This is - uninteresting, but for some reason MSVC's behaviour is to warn about - including this system header. That *is* interesting -*/ -#pragma warning(disable : 4820) -#pragma warning(push, 1) -#include -#pragma warning(pop) -#define UTEST_COLOUR_OUTPUT() (_isatty(_fileno(stdout))) -#else -#include -#define UTEST_COLOUR_OUTPUT() (isatty(STDOUT_FILENO)) -#endif - -static UTEST_INLINE utest_int64_t utest_ns(void) { -#ifdef _MSC_VER - LARGE_INTEGER counter; - LARGE_INTEGER frequency; - QueryPerformanceCounter(&counter); - QueryPerformanceFrequency(&frequency); - return UTEST_CAST(utest_int64_t, - (counter.QuadPart * 1000000000) / frequency.QuadPart); -#elif defined(__linux) - struct timespec ts; - const clockid_t cid = CLOCK_REALTIME; -#if defined(UTEST_USE_CLOCKGETTIME) - clock_gettime(cid, &ts); -#else - syscall(SYS_clock_gettime, cid, &ts); -#endif - return UTEST_CAST(utest_int64_t, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; -#elif __APPLE__ - return UTEST_CAST(utest_int64_t, mach_absolute_time()); -#endif -} - -typedef void (*utest_testcase_t)(int *, size_t); - -struct utest_test_state_s { - utest_testcase_t func; - size_t index; - char *name; -}; - -struct utest_state_s { - struct utest_test_state_s *tests; - size_t tests_length; - FILE *output; -}; - -/* extern to the global state utest needs to execute */ -UTEST_EXTERN struct utest_state_s utest_state; - -#if defined(_MSC_VER) -#define UTEST_WEAK __forceinline -#else -#define UTEST_WEAK __attribute__((weak)) -#endif - -#if defined(_MSC_VER) -#define UTEST_UNUSED -#else -#define UTEST_UNUSED __attribute__((unused)) -#endif - -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wvariadic-macros" -#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" -#endif -#define UTEST_PRINTF(...) \ - if (utest_state.output) { \ - fprintf(utest_state.output, __VA_ARGS__); \ - } \ - printf(__VA_ARGS__) -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - -#ifdef _MSC_VER -#define UTEST_SNPRINTF(BUFFER, N, ...) _snprintf_s(BUFFER, N, N, __VA_ARGS__) -#else -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wvariadic-macros" -#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" -#endif -#define UTEST_SNPRINTF(...) snprintf(__VA_ARGS__) -#ifdef __clang__ -#pragma clang diagnostic pop -#endif -#endif - -#if defined(__cplusplus) -/* if we are using c++ we can use overloaded methods (its in the language) */ -#define UTEST_OVERLOADABLE -#elif defined(__clang__) -/* otherwise, if we are using clang with c - use the overloadable attribute */ -#define UTEST_OVERLOADABLE __attribute__((overloadable)) -#endif - -#if defined(UTEST_OVERLOADABLE) -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f) { - UTEST_PRINTF("%f", UTEST_CAST(double, f)); -} - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d) { - UTEST_PRINTF("%f", d); -} - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d) { - UTEST_PRINTF("%Lf", d); -} - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i) { - UTEST_PRINTF("%d", i); -} - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i) { - UTEST_PRINTF("%u", i); -} - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i) { - UTEST_PRINTF("%ld", i); -} - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i) { - UTEST_PRINTF("%lu", i); -} - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const void *p); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const void *p) { - UTEST_PRINTF("%p", p); -} - -/* - long long is a c++11 extension -*/ -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || \ - defined(__cplusplus) && (__cplusplus >= 201103L) - -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" -#endif - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i) { - UTEST_PRINTF("%lld", i); -} - -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i); -UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer( - long long unsigned int i) { - UTEST_PRINTF("%llu", i); -} - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - -#endif -#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) -#define utest_type_printer(val) \ - UTEST_PRINTF(_Generic((val), int \ - : "%d", long \ - : "%ld", long long \ - : "%lld", unsigned \ - : "%u", unsigned long \ - : "%lu", unsigned long long \ - : "%llu", float \ - : "%f", double \ - : "%f", long double \ - : "%Lf", default \ - : _Generic((val - val), ptrdiff_t \ - : "%p", default \ - : "undef")), \ - (val)) -#else -/* - we don't have the ability to print the values we got, so we create a macro - to tell our users we can't do anything fancy -*/ -#define utest_type_printer(...) UTEST_PRINTF("undef") -#endif - -#if defined(__cplusplus) && (__cplusplus >= 201103L) -#define UTEST_AUTO(x) auto -#elif !defined(__cplusplus) - -#if defined(__clang__) -/* clang-format off */ -/* had to disable clang-format here because it malforms the pragmas */ -#define UTEST_AUTO(x) \ - _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"-Wgnu-auto-type\"") __auto_type \ - _Pragma("clang diagnostic pop") -/* clang-format on */ -#else -#define UTEST_AUTO(x) __auto_type -#endif - -#else -#define UTEST_AUTO(x) typeof(x + 0) -#endif - -#if defined(__clang__) -#define UTEST_EXPECT(x, y, cond) \ - { \ - _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \ - _Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \ - _Pragma("clang diagnostic ignored \"-Wfloat-equal\"") \ - UTEST_AUTO(x) xEval = (x); \ - UTEST_AUTO(y) yEval = (y); \ - if (!((xEval)cond(yEval))) { \ - _Pragma("clang diagnostic pop") \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : "); \ - utest_type_printer(xEval); \ - UTEST_PRINTF("\n"); \ - UTEST_PRINTF(" Actual : "); \ - utest_type_printer(yEval); \ - UTEST_PRINTF("\n"); \ - *utest_result = 1; \ - } \ - } -#elif defined(__GNUC__) -#define UTEST_EXPECT(x, y, cond) \ - { \ - UTEST_AUTO(x) xEval = (x); \ - UTEST_AUTO(y) yEval = (y); \ - if (!((xEval)cond(yEval))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : "); \ - utest_type_printer(xEval); \ - UTEST_PRINTF("\n"); \ - UTEST_PRINTF(" Actual : "); \ - utest_type_printer(yEval); \ - UTEST_PRINTF("\n"); \ - *utest_result = 1; \ - } \ - } -#else -#define UTEST_EXPECT(x, y, cond) \ - { \ - if (!((x)cond(y))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - *utest_result = 1; \ - } \ - } -#endif - -#define EXPECT_TRUE(x) \ - if (!(x)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : true\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ - *utest_result = 1; \ - } - -#define EXPECT_FALSE(x) \ - if (x) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : false\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ - *utest_result = 1; \ - } - -#define EXPECT_EQ(x, y) UTEST_EXPECT(x, y, ==) -#define EXPECT_NE(x, y) UTEST_EXPECT(x, y, !=) -#define EXPECT_LT(x, y) UTEST_EXPECT(x, y, <) -#define EXPECT_LE(x, y) UTEST_EXPECT(x, y, <=) -#define EXPECT_GT(x, y) UTEST_EXPECT(x, y, >) -#define EXPECT_GE(x, y) UTEST_EXPECT(x, y, >=) - -#define EXPECT_STREQ(x, y) \ - if (0 != strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ - *utest_result = 1; \ - } - -#define EXPECT_STRNE(x, y) \ - if (0 == strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ - *utest_result = 1; \ - } - -#if defined(__clang__) -#define UTEST_ASSERT(x, y, cond) \ - { \ - _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \ - _Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \ - _Pragma("clang diagnostic ignored \"-Wfloat-equal\"") \ - UTEST_AUTO(x) xEval = (x); \ - UTEST_AUTO(y) yEval = (y); \ - if (!((xEval)cond(yEval))) { \ - _Pragma("clang diagnostic pop") \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : "); \ - utest_type_printer(xEval); \ - UTEST_PRINTF("\n"); \ - UTEST_PRINTF(" Actual : "); \ - utest_type_printer(yEval); \ - UTEST_PRINTF("\n"); \ - *utest_result = 1; \ - return; \ - } \ - } -#elif defined(__GNUC__) -#define UTEST_ASSERT(x, y, cond) \ - { \ - UTEST_AUTO(x) xEval = (x); \ - UTEST_AUTO(y) yEval = (y); \ - if (!((xEval)cond(yEval))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : "); \ - utest_type_printer(xEval); \ - UTEST_PRINTF("\n"); \ - UTEST_PRINTF(" Actual : "); \ - utest_type_printer(yEval); \ - UTEST_PRINTF("\n"); \ - *utest_result = 1; \ - return; \ - } \ - } -#else -#define UTEST_ASSERT(x, y, cond) \ - { \ - if (!((x)cond(y))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - *utest_result = 1; \ - return; \ - } \ - } -#endif - -#define ASSERT_TRUE(x) \ - if (!(x)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : true\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ - *utest_result = 1; \ - return; \ - } - -#define ASSERT_FALSE(x) \ - if (x) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : false\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ - *utest_result = 1; \ - return; \ - } - -#define ASSERT_EQ(x, y) UTEST_ASSERT(x, y, ==) -#define ASSERT_NE(x, y) UTEST_ASSERT(x, y, !=) -#define ASSERT_LT(x, y) UTEST_ASSERT(x, y, <) -#define ASSERT_LE(x, y) UTEST_ASSERT(x, y, <=) -#define ASSERT_GT(x, y) UTEST_ASSERT(x, y, >) -#define ASSERT_GE(x, y) UTEST_ASSERT(x, y, >=) - -#define ASSERT_STREQ(x, y) \ - EXPECT_STREQ(x, y); \ - if (0 != strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ - *utest_result = 1; \ - return; \ - } - -#define ASSERT_STRNE(x, y) \ - EXPECT_STRNE(x, y); \ - if (0 == strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ - *utest_result = 1; \ - return; \ - } - -#define UTEST(SET, NAME) \ - UTEST_EXTERN struct utest_state_s utest_state; \ - static void utest_run_##SET##_##NAME(int *utest_result); \ - static void utest_##SET##_##NAME(int *utest_result, size_t utest_index) { \ - (void)utest_index; \ - utest_run_##SET##_##NAME(utest_result); \ - } \ - UTEST_INITIALIZER(utest_register_##SET##_##NAME) { \ - const size_t index = utest_state.tests_length++; \ - const char *name_part = #SET "." #NAME; \ - const size_t name_size = strlen(name_part) + 1; \ - char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ - utest_state.tests = \ - UTEST_PTR_CAST(struct utest_test_state_s *, \ - realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ - sizeof(struct utest_test_state_s) * \ - utest_state.tests_length)); \ - utest_state.tests[index].func = &utest_##SET##_##NAME; \ - utest_state.tests[index].name = name; \ - UTEST_SNPRINTF(name, name_size, "%s", name_part); \ - } \ - void utest_run_##SET##_##NAME(int *utest_result) - -#define UTEST_F_SETUP(FIXTURE) \ - static void utest_f_setup_##FIXTURE(int *utest_result, \ - struct FIXTURE *utest_fixture) - -#define UTEST_F_TEARDOWN(FIXTURE) \ - static void utest_f_teardown_##FIXTURE(int *utest_result, \ - struct FIXTURE *utest_fixture) - -#define UTEST_F(FIXTURE, NAME) \ - UTEST_EXTERN struct utest_state_s utest_state; \ - static void utest_f_setup_##FIXTURE(int *, struct FIXTURE *); \ - static void utest_f_teardown_##FIXTURE(int *, struct FIXTURE *); \ - static void utest_run_##FIXTURE##_##NAME(int *, struct FIXTURE *); \ - static void utest_f_##FIXTURE##_##NAME(int *utest_result, \ - size_t utest_index) { \ - struct FIXTURE fixture; \ - (void)utest_index; \ - memset(&fixture, 0, sizeof(fixture)); \ - utest_f_setup_##FIXTURE(utest_result, &fixture); \ - if (0 != *utest_result) { \ - return; \ - } \ - utest_run_##FIXTURE##_##NAME(utest_result, &fixture); \ - utest_f_teardown_##FIXTURE(utest_result, &fixture); \ - } \ - UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME) { \ - const size_t index = utest_state.tests_length++; \ - const char *name_part = #FIXTURE "." #NAME; \ - const size_t name_size = strlen(name_part) + 1; \ - char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ - utest_state.tests = \ - UTEST_PTR_CAST(struct utest_test_state_s *, \ - realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ - sizeof(struct utest_test_state_s) * \ - utest_state.tests_length)); \ - utest_state.tests[index].func = &utest_f_##FIXTURE##_##NAME; \ - utest_state.tests[index].name = name; \ - UTEST_SNPRINTF(name, name_size, "%s", name_part); \ - } \ - void utest_run_##FIXTURE##_##NAME(int *utest_result, \ - struct FIXTURE *utest_fixture) - -#define UTEST_I_SETUP(FIXTURE) \ - static void utest_i_setup_##FIXTURE( \ - int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) - -#define UTEST_I_TEARDOWN(FIXTURE) \ - static void utest_i_teardown_##FIXTURE( \ - int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) - -#define UTEST_I(FIXTURE, NAME, INDEX) \ - UTEST_EXTERN struct utest_state_s utest_state; \ - static void utest_run_##FIXTURE##_##NAME##_##INDEX(int *, struct FIXTURE *); \ - static void utest_i_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ - size_t index) { \ - struct FIXTURE fixture; \ - memset(&fixture, 0, sizeof(fixture)); \ - utest_i_setup_##FIXTURE(utest_result, &fixture, index); \ - if (0 != *utest_result) { \ - return; \ - } \ - utest_run_##FIXTURE##_##NAME##_##INDEX(utest_result, &fixture); \ - utest_i_teardown_##FIXTURE(utest_result, &fixture, index); \ - } \ - UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME##_##INDEX) { \ - size_t i; \ - utest_uint64_t iUp; \ - for (i = 0; i < (INDEX); i++) { \ - const size_t index = utest_state.tests_length++; \ - const char *name_part = #FIXTURE "." #NAME; \ - const size_t name_size = strlen(name_part) + 32; \ - char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ - utest_state.tests = \ - UTEST_PTR_CAST(struct utest_test_state_s *, \ - realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ - sizeof(struct utest_test_state_s) * \ - utest_state.tests_length)); \ - utest_state.tests[index].func = &utest_i_##FIXTURE##_##NAME##_##INDEX; \ - utest_state.tests[index].index = i; \ - utest_state.tests[index].name = name; \ - iUp = UTEST_CAST(utest_uint64_t, i); \ - UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, iUp); \ - } \ - } \ - void utest_run_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ - struct FIXTURE *utest_fixture) - -UTEST_WEAK -int utest_should_filter_test(const char *filter, const char *testcase); -UTEST_WEAK int utest_should_filter_test(const char *filter, - const char *testcase) { - if (filter) { - const char *filter_cur = filter; - const char *testcase_cur = testcase; - const char *filter_wildcard = UTEST_NULL; - - while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { - if ('*' == *filter_cur) { - /* store the position of the wildcard */ - filter_wildcard = filter_cur; - - /* skip the wildcard character */ - filter_cur++; - - while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { - if ('*' == *filter_cur) { - /* - we found another wildcard (filter is something like *foo*) so we - exit the current loop, and return to the parent loop to handle - the wildcard case - */ - break; - } else if (*filter_cur != *testcase_cur) { - /* otherwise our filter didn't match, so reset it */ - filter_cur = filter_wildcard; - } - - /* move testcase along */ - testcase_cur++; - - /* move filter along */ - filter_cur++; - } - - if (('\0' == *filter_cur) && ('\0' == *testcase_cur)) { - return 0; - } - - /* if the testcase has been exhausted, we don't have a match! */ - if ('\0' == *testcase_cur) { - return 1; - } - } else { - if (*testcase_cur != *filter_cur) { - /* test case doesn't match filter */ - return 1; - } else { - /* move our filter and testcase forward */ - testcase_cur++; - filter_cur++; - } - } - } - - if (('\0' != *filter_cur) || - (('\0' != *testcase_cur) && - ((filter == filter_cur) || ('*' != filter_cur[-1])))) { - /* we have a mismatch! */ - return 1; - } - } - - return 0; -} - -static UTEST_INLINE int utest_strncmp(const char *a, const char *b, size_t n) { - /* strncmp breaks on Wall / Werror on gcc/clang, so we avoid using it */ - unsigned i; - - for (i = 0; i < n; i++) { - if (a[i] < b[i]) { - return -1; - } else if (a[i] > b[i]) { - return 1; - } - } - - return 0; -} - -static UTEST_INLINE FILE *utest_fopen(const char *filename, const char *mode) { -#ifdef _MSC_VER - FILE *file; - if (0 == fopen_s(&file, filename, mode)) { - return file; - } else { - return 0; - } -#else - return fopen(filename, mode); -#endif -} - -UTEST_WEAK int utest_main(int argc, const char *const argv[]); -UTEST_WEAK int utest_main(int argc, const char *const argv[]) { - utest_uint64_t failed = 0; - size_t index = 0; - size_t *failed_testcases = UTEST_NULL; - size_t failed_testcases_length = 0; - const char *filter = UTEST_NULL; - utest_uint64_t ran_tests = 0; - - enum colours { RESET, GREEN, RED }; - - const int use_colours = UTEST_COLOUR_OUTPUT(); - const char *colours[] = {"\033[0m", "\033[32m", "\033[31m"}; - if (!use_colours) { - for (index = 0; index < sizeof colours / sizeof colours[0]; index++) { - colours[index] = ""; - } - } - /* loop through all arguments looking for our options */ - for (index = 1; index < UTEST_CAST(size_t, argc); index++) { - /* Informational switches */ - const char help_str[] = "--help"; - const char list_str[] = "--list-tests"; - /* Test config switches */ - const char filter_str[] = "--filter="; - const char output_str[] = "--output="; - - if (0 == utest_strncmp(argv[index], help_str, strlen(help_str))) { - printf( - "utest.h - the single file unit testing solution for C/C++!\n" - "Command line Options:\n" - " --help Show this message and exit.\n" - " --filter= Filter the test cases to run (EG. MyTest*.a " - "would run MyTestCase.a but not MyTestCase.b).\n" - " --list-tests List testnames, one per line. Output names " - "can be passed to --filter.\n" - " --output= Output an xunit XML file to the file " - "specified in .\n"); - goto cleanup; - } else if (0 == - utest_strncmp(argv[index], filter_str, strlen(filter_str))) { - /* user wants to filter what test cases run! */ - filter = argv[index] + strlen(filter_str); - } else if (0 == - utest_strncmp(argv[index], output_str, strlen(output_str))) { - utest_state.output = utest_fopen(argv[index] + strlen(output_str), "w+"); - } else if (0 == utest_strncmp(argv[index], list_str, strlen(list_str))) { - for (index = 0; index < utest_state.tests_length; index++) { - UTEST_PRINTF("%s\n", utest_state.tests[index].name); - } - /* when printing the test list, don't actually run the tests */ - return 0; - } - } - - for (index = 0; index < utest_state.tests_length; index++) { - if (utest_should_filter_test(filter, utest_state.tests[index].name)) { - continue; - } - - ran_tests++; - } - - printf("%s[==========]%s Running %" UTEST_PRIu64 " test cases.\n", - colours[GREEN], colours[RESET], UTEST_CAST(utest_uint64_t, ran_tests)); - - if (utest_state.output) { - fprintf(utest_state.output, "\n"); - fprintf(utest_state.output, - "\n", - UTEST_CAST(utest_uint64_t, ran_tests)); - fprintf(utest_state.output, - "\n", - UTEST_CAST(utest_uint64_t, ran_tests)); - } - - for (index = 0; index < utest_state.tests_length; index++) { - int result = 0; - utest_int64_t ns = 0; - - if (utest_should_filter_test(filter, utest_state.tests[index].name)) { - continue; - } - - printf("%s[ RUN ]%s %s\n", colours[GREEN], colours[RESET], - utest_state.tests[index].name); - - if (utest_state.output) { - fprintf(utest_state.output, "", - utest_state.tests[index].name); - } - - ns = utest_ns(); - utest_state.tests[index].func(&result, utest_state.tests[index].index); - ns = utest_ns() - ns; - - if (utest_state.output) { - fprintf(utest_state.output, "\n"); - } - - if (0 != result) { - const size_t failed_testcase_index = failed_testcases_length++; - failed_testcases = UTEST_PTR_CAST( - size_t *, realloc(UTEST_PTR_CAST(void *, failed_testcases), - sizeof(size_t) * failed_testcases_length)); - failed_testcases[failed_testcase_index] = index; - failed++; - printf("%s[ FAILED ]%s %s (%" UTEST_PRId64 "ns)\n", colours[RED], - colours[RESET], utest_state.tests[index].name, ns); - } else { - printf("%s[ OK ]%s %s (%" UTEST_PRId64 "ns)\n", colours[GREEN], - colours[RESET], utest_state.tests[index].name, ns); - } - } - - printf("%s[==========]%s %" UTEST_PRIu64 " test cases ran.\n", colours[GREEN], - colours[RESET], ran_tests); - printf("%s[ PASSED ]%s %" UTEST_PRIu64 " tests.\n", colours[GREEN], - colours[RESET], ran_tests - failed); - - if (0 != failed) { - printf("%s[ FAILED ]%s %" UTEST_PRIu64 " tests, listed below:\n", - colours[RED], colours[RESET], failed); - for (index = 0; index < failed_testcases_length; index++) { - printf("%s[ FAILED ]%s %s\n", colours[RED], colours[RESET], - utest_state.tests[failed_testcases[index]].name); - } - } - - if (utest_state.output) { - fprintf(utest_state.output, "\n\n"); - } - -cleanup: - for (index = 0; index < utest_state.tests_length; index++) { - free(UTEST_PTR_CAST(void *, utest_state.tests[index].name)); - } - - free(UTEST_PTR_CAST(void *, failed_testcases)); - free(UTEST_PTR_CAST(void *, utest_state.tests)); - - if (utest_state.output) { - fclose(utest_state.output); - } - - return UTEST_CAST(int, failed); -} - -/* - we need, in exactly one source file, define the global struct that will hold - the data we need to run utest. This macro allows the user to declare the - data without having to use the UTEST_MAIN macro, thus allowing them to write - their own main() function. -*/ -#define UTEST_STATE() struct utest_state_s utest_state = {0, 0, 0} - -/* - define a main() function to call into utest.h and start executing tests! A - user can optionally not use this macro, and instead define their own main() - function and manually call utest_main. The user must, in exactly one source - file, use the UTEST_STATE macro to declare a global struct variable that - utest requires. -*/ -#define UTEST_MAIN() \ - UTEST_STATE(); \ - int main(int argc, const char *const argv[]) { \ - return utest_main(argc, argv); \ - } - -#endif /* SHEREDOM_UTEST_H_INCLUDED */ diff --git a/tests/omni_lock_rust/Cargo.lock b/tests/omni_lock_rust/Cargo.lock deleted file mode 100644 index 96e126b..0000000 --- a/tests/omni_lock_rust/Cargo.lock +++ /dev/null @@ -1,1483 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - -[[package]] -name = "aho-corasick" -version = "0.6.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5" -dependencies = [ - "memchr 2.4.1", -] - -[[package]] -name = "anyhow" -version = "1.0.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "595d3cfa7a60d4555cb5067b99f07142a08ea778de5cf993f7b75c7d8fabc486" - -[[package]] -name = "autocfg" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" - -[[package]] -name = "autocfg" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" - -[[package]] -name = "bit-vec" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f59bbe95d4e52a6398ec21238d31577f2b28a9d86807f06ca59d191d8440d0bb" - -[[package]] -name = "bitflags" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" - -[[package]] -name = "blake2b-ref" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95916998c798756098a4eb1b3f2cd510659705a9817bf203d61abd30fbec3e7b" - -[[package]] -name = "blake2b-rs" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e35e362830ef90ecea16f09b21b75d22d33a8562a679c74ab4f4fa49b4fcb87" -dependencies = [ - "cc", -] - -[[package]] -name = "block-buffer" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" -dependencies = [ - "block-padding", - "generic-array", -] - -[[package]] -name = "block-padding" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" - -[[package]] -name = "byteorder" -version = "1.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" - -[[package]] -name = "bytes" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" -dependencies = [ - "serde", -] - -[[package]] -name = "cc" -version = "1.0.69" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" - -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "ckb-chain-spec" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6545c270115cdbed0b5ec452a4ef8753dac8990ac0996110531041a00f0842f9" -dependencies = [ - "ckb-constant", - "ckb-crypto", - "ckb-dao-utils", - "ckb-error", - "ckb-hash", - "ckb-jsonrpc-types", - "ckb-pow", - "ckb-rational", - "ckb-resource", - "ckb-traits", - "ckb-types", - "serde", - "toml", -] - -[[package]] -name = "ckb-channel" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0048e1732dc769525b332a20a4eb0942f84e1ba3cbcbcb286dacd3a585f6e50a" -dependencies = [ - "crossbeam-channel", -] - -[[package]] -name = "ckb-constant" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "237ea9a8e7ced2731a46bbbf23b40a7df4c5229bb2226396ba70bfe4cdc85e3c" - -[[package]] -name = "ckb-crypto" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c69f679e843fae7294679efdbe6239aaf9469a40f0b3145e075708879b26513" -dependencies = [ - "ckb-fixed-hash", - "faster-hex", - "lazy_static", - "rand 0.7.3", - "secp256k1", - "thiserror", -] - -[[package]] -name = "ckb-dao-utils" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915bbb801dee8f14e47714b18b8c8216c574156c7a1bd922fba2f73d73a8f10a" -dependencies = [ - "byteorder", - "ckb-error", - "ckb-types", -] - -[[package]] -name = "ckb-error" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "945d804f5648bd3060b0d19336952093f3bbc6cc568533230156d5f579fef24c" -dependencies = [ - "anyhow", - "ckb-occupied-capacity", - "derive_more", - "thiserror", -] - -[[package]] -name = "ckb-fixed-hash" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1e74fcc7ab5e6274991cdd7b3a49583b45f63c1d82cda639edd259d7cf73bd2" -dependencies = [ - "ckb-fixed-hash-core", - "ckb-fixed-hash-macros", -] - -[[package]] -name = "ckb-fixed-hash-core" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b845d71fee1b55f68453c8db69b54a43db1370147f0f79909324eaf030a28e2e" -dependencies = [ - "faster-hex", - "serde", - "thiserror", -] - -[[package]] -name = "ckb-fixed-hash-macros" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff13f0f845ae9fb1073239094e6cadaf7af213181e7462df4f73cdb81d6b5ee6" -dependencies = [ - "ckb-fixed-hash-core", - "proc-macro2", - "quote 1.0.9", - "syn 1.0.74", -] - -[[package]] -name = "ckb-hash" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f409236f9c815cebf15dea49924bf3f316d47682f710fa9493135993a57d7913" -dependencies = [ - "blake2b-ref", - "blake2b-rs", -] - -[[package]] -name = "ckb-jsonrpc-types" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9b11f78288ab8f299caf7b316706ca41d8b7a67a5e20719245d584230dce003" -dependencies = [ - "ckb-types", - "faster-hex", - "serde", - "serde_json", -] - -[[package]] -name = "ckb-logger" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14feffc8b90fd0e5dead8b8c0609faf5e6b5f3c06ceb487b8f0c91f378dc92cf" -dependencies = [ - "log 0.4.14", -] - -[[package]] -name = "ckb-occupied-capacity" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a42877e9e929241d9965b8fa0337c3dc93715c452df0c305f4a6cceda4ecdda" -dependencies = [ - "ckb-occupied-capacity-core", - "ckb-occupied-capacity-macros", -] - -[[package]] -name = "ckb-occupied-capacity-core" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80447a92eeef022f9dff53413d46900cbab5a8e28bd5ef49c88512a3b8aa5d6d" -dependencies = [ - "serde", -] - -[[package]] -name = "ckb-occupied-capacity-macros" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab646c39f21c297ce0fd274bfe27361830adf2d64d0f15b2ef320dea2001cb5" -dependencies = [ - "ckb-occupied-capacity-core", - "quote 1.0.9", - "syn 1.0.74", -] - -[[package]] -name = "ckb-pow" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98b034068b7746d5cc666c95fcdaf355bd9c302f3d68207e9c6e988e7307c8ad" -dependencies = [ - "byteorder", - "ckb-hash", - "ckb-types", - "eaglesong", - "log 0.4.14", - "serde", -] - -[[package]] -name = "ckb-rational" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80648e2f04b2429c96497dfd786edbbaabcde45d3f566ef96bb84c6fe1c3999c" -dependencies = [ - "numext-fixed-uint", - "serde", -] - -[[package]] -name = "ckb-resource" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21111aa1eaf33e998163bdc35bb01a22cb5a71660a8c694dbaaa7455668807e1" -dependencies = [ - "ckb-system-scripts", - "ckb-types", - "includedir", - "includedir_codegen", - "phf", - "serde", - "tempfile", - "walkdir", -] - -[[package]] -name = "ckb-script" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86c9aa8b59733bbf92195d9596a24e369db27614593b522582ab24e2436c255c" -dependencies = [ - "byteorder", - "ckb-chain-spec", - "ckb-error", - "ckb-hash", - "ckb-logger", - "ckb-traits", - "ckb-types", - "ckb-vm", - "ckb-vm-definitions", - "faster-hex", - "goblin 0.2.3", - "serde", -] - -[[package]] -name = "ckb-system-scripts" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfbce46ad2de7563c7e456888d4a29df3d461106a989836c08627d044409e928" -dependencies = [ - "blake2b-rs", - "faster-hex", - "includedir", - "includedir_codegen", - "phf", -] - -[[package]] -name = "ckb-traits" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8629d61183ab240fe0c598b5723c16bc9741691df2261574ff04c6fb87cb070d" -dependencies = [ - "ckb-types", -] - -[[package]] -name = "ckb-types" -version = "0.100.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31588d3154ce8e3bd7d815b6e8223afecbcf3ca05a58a26db371393eb0ffe42a" -dependencies = [ - "bit-vec", - "bitflags", - "bytes", - "ckb-channel", - "ckb-error", - "ckb-fixed-hash", - "ckb-hash", - "ckb-occupied-capacity", - "ckb-rational", - "derive_more", - "merkle-cbt", - "molecule", - "numext-fixed-uint", - "once_cell", -] - -[[package]] -name = "ckb-vm" -version = "0.20.0-rc2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3edaa6828e414b2d8cf773acaa561fd104ac595028de9326df790a1f7ba40f8" -dependencies = [ - "byteorder", - "bytes", - "cc", - "ckb-vm-definitions", - "derive_more", - "goblin 0.2.3", - "goblin 0.4.0", - "libc", - "mapr", - "rand 0.7.3", - "scroll", - "serde", -] - -[[package]] -name = "ckb-vm-debug-utils" -version = "0.1.0" -source = "git+https://github.com/nervosnetwork/ckb-vm-debug-utils.git?rev=f72995f#f72995fc02f665aef356c81c3e0d5030fdac88c2" -dependencies = [ - "byteorder", - "bytes", - "ckb-vm", - "env_logger", - "gdb-remote-protocol", - "libc", - "log 0.4.14", - "nix", -] - -[[package]] -name = "ckb-vm-definitions" -version = "0.20.0-rc5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deb44cd2753aaeb2dc8a3df7b293ddfc3cbaaf8232d819650c0fc365cacda34f" - -[[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -dependencies = [ - "bitflags", -] - -[[package]] -name = "convert_case" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" - -[[package]] -name = "crc32fast" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" -dependencies = [ - "cfg-if 1.0.0", -] - -[[package]] -name = "crossbeam-channel" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8ec7fcd21571dc78f96cc96243cab8d8f035247c3efd16c687be154c3fa9efa" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.6.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6" -dependencies = [ - "cfg-if 0.1.10", - "lazy_static", -] - -[[package]] -name = "derive_more" -version = "0.99.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40eebddd2156ce1bb37b20bbe5151340a31828b1f2d22ba4141f3531710e38df" -dependencies = [ - "convert_case", - "proc-macro2", - "quote 1.0.9", - "rustc_version", - "syn 1.0.74", -] - -[[package]] -name = "digest" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" -dependencies = [ - "generic-array", -] - -[[package]] -name = "eaglesong" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d978bd5d343e8ab9b5c0fc8d93ff9c602fdc96616ffff9c05ac7a155419b824" - -[[package]] -name = "env_logger" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" -dependencies = [ - "log 0.3.9", - "regex", -] - -[[package]] -name = "faster-hex" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b02a61fea82de8484f9d7ce99b698b0e190ef8de71035690a961a43d7b18a2ad" - -[[package]] -name = "flate2" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd3aec53de10fe96d7d8c565eb17f2c687bb5518a2ec453b5b1252964526abe0" -dependencies = [ - "cfg-if 1.0.0", - "crc32fast", - "libc", - "miniz_oxide", -] - -[[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -dependencies = [ - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" - -[[package]] -name = "fuchsia-cprng" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" - -[[package]] -name = "gdb-remote-protocol" -version = "0.1.0" -source = "git+https://github.com/luser/rust-gdb-remote-protocol?rev=565ab0c#565ab0c0ac189c82b0e7df217860599fc1ae6287" -dependencies = [ - "log 0.4.14", - "nom", - "strum", - "strum_macros", -] - -[[package]] -name = "generic-array" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" -dependencies = [ - "typenum", - "version_check", -] - -[[package]] -name = "getrandom" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" -dependencies = [ - "cfg-if 1.0.0", - "libc", - "wasi", -] - -[[package]] -name = "goblin" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d20fd25aa456527ce4f544271ae4fea65d2eda4a6561ea56f39fb3ee4f7e3884" -dependencies = [ - "log 0.4.14", - "plain", - "scroll", -] - -[[package]] -name = "goblin" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "532a09cd3df2c6bbfc795fb0434bff8f22255d1d07328180e918a2e6ce122d4d" -dependencies = [ - "log 0.4.14", - "plain", - "scroll", -] - -[[package]] -name = "heapsize" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461" -dependencies = [ - "winapi", -] - -[[package]] -name = "includedir" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e97402f770a519ebea51b27131c3b6558cfd2375aff21294bad806bad91bf0b6" -dependencies = [ - "flate2", - "phf", -] - -[[package]] -name = "includedir_codegen" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af7d542be113fd84855692fb536c16cc4c09527724d1dca8953047d71cccadef" -dependencies = [ - "flate2", - "phf_codegen", - "walkdir", -] - -[[package]] -name = "itoa" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" - -[[package]] -name = "keccak" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7" - -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] -name = "libc" -version = "0.2.98" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" - -[[package]] -name = "log" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" -dependencies = [ - "log 0.4.14", -] - -[[package]] -name = "log" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" -dependencies = [ - "cfg-if 1.0.0", -] - -[[package]] -name = "mapr" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46a28a55dbc005b2f6f123c4058933d57add373d362f6fd3a76aab4fe6973500" -dependencies = [ - "libc", - "winapi", -] - -[[package]] -name = "memchr" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a" -dependencies = [ - "libc", -] - -[[package]] -name = "memchr" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" - -[[package]] -name = "merkle-cbt" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f1ac8428ec02d6caa5a79c15e851d84d5dc7a00df0429a8aa860d104f0a81be" -dependencies = [ - "cfg-if 0.1.10", -] - -[[package]] -name = "miniz_oxide" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" -dependencies = [ - "adler", - "autocfg 1.0.1", -] - -[[package]] -name = "molecule" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c58d1147f4d0d51ce72c56a6099ae5dbfd007b250271bb965f82dd10fca2bf02" -dependencies = [ - "bytes", - "cfg-if 1.0.0", - "faster-hex", -] - -[[package]] -name = "nix" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b2e0b4f3320ed72aaedb9a5ac838690a8047c7b275da22711fddff4f8a14229" -dependencies = [ - "bitflags", - "cc", - "cfg-if 0.1.10", - "libc", - "void", -] - -[[package]] -name = "nom" -version = "3.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b" -dependencies = [ - "memchr 1.0.2", -] - -[[package]] -name = "numext-constructor" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "621fe0f044729f810c6815cdd77e8f5e0cd803ce4f6a38380ebfc1322af98661" -dependencies = [ - "proc-macro2", - "quote 1.0.9", - "syn 1.0.74", -] - -[[package]] -name = "numext-fixed-uint" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c68c76f96d589d1009a666c5072f37f3114d682696505f2cf445f27766c7d70" -dependencies = [ - "numext-fixed-uint-core", - "numext-fixed-uint-hack", -] - -[[package]] -name = "numext-fixed-uint-core" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6aab1d6457b97b49482f22a92f0f58a2f39bdd7f3b2f977eae67e8bc206aa980" -dependencies = [ - "heapsize", - "numext-constructor", - "rand 0.7.3", - "serde", - "thiserror", -] - -[[package]] -name = "numext-fixed-uint-hack" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0200f8d55c36ec1b6a8cf810115be85d4814f045e0097dfd50033ba25adb4c9e" -dependencies = [ - "numext-fixed-uint-core", - "proc-macro2", - "quote 1.0.9", - "syn 1.0.74", -] - -[[package]] -name = "omni-lock-test" -version = "0.1.0" -dependencies = [ - "blake2b-rs", - "ckb-chain-spec", - "ckb-crypto", - "ckb-error", - "ckb-hash", - "ckb-script", - "ckb-traits", - "ckb-types", - "ckb-vm", - "ckb-vm-debug-utils", - "gdb-remote-protocol", - "lazy_static", - "openssl", - "rand 0.6.5", - "sha3", - "sparse-merkle-tree", -] - -[[package]] -name = "once_cell" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" - -[[package]] -name = "opaque-debug" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" - -[[package]] -name = "openssl" -version = "0.10.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "549430950c79ae24e6d02e0b7404534ecf311d94cc9f861e9e4020187d13d885" -dependencies = [ - "bitflags", - "cfg-if 1.0.0", - "foreign-types", - "libc", - "once_cell", - "openssl-sys", -] - -[[package]] -name = "openssl-sys" -version = "0.9.65" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a7907e3bfa08bb85105209cdfcb6c63d109f8f6c1ed6ca318fff5c1853fbc1d" -dependencies = [ - "autocfg 1.0.1", - "cc", - "libc", - "pkg-config", - "vcpkg", -] - -[[package]] -name = "pest" -version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53" -dependencies = [ - "ucd-trie", -] - -[[package]] -name = "phf" -version = "0.7.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18" -dependencies = [ - "phf_shared", -] - -[[package]] -name = "phf_codegen" -version = "0.7.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e" -dependencies = [ - "phf_generator", - "phf_shared", -] - -[[package]] -name = "phf_generator" -version = "0.7.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662" -dependencies = [ - "phf_shared", - "rand 0.6.5", -] - -[[package]] -name = "phf_shared" -version = "0.7.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" -dependencies = [ - "siphasher", -] - -[[package]] -name = "pkg-config" -version = "0.3.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" - -[[package]] -name = "plain" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" - -[[package]] -name = "ppv-lite86" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" - -[[package]] -name = "proc-macro2" -version = "1.0.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" -dependencies = [ - "unicode-xid 0.2.2", -] - -[[package]] -name = "quote" -version = "0.3.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" - -[[package]] -name = "quote" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rand" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" -dependencies = [ - "autocfg 0.1.7", - "libc", - "rand_chacha 0.1.1", - "rand_core 0.4.2", - "rand_hc 0.1.0", - "rand_isaac", - "rand_jitter", - "rand_os", - "rand_pcg 0.1.2", - "rand_xorshift", - "winapi", -] - -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom", - "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc 0.2.0", - "rand_pcg 0.2.1", -] - -[[package]] -name = "rand_chacha" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" -dependencies = [ - "autocfg 0.1.7", - "rand_core 0.3.1", -] - -[[package]] -name = "rand_chacha" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" -dependencies = [ - "ppv-lite86", - "rand_core 0.5.1", -] - -[[package]] -name = "rand_core" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" -dependencies = [ - "rand_core 0.4.2", -] - -[[package]] -name = "rand_core" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" - -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom", -] - -[[package]] -name = "rand_hc" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core 0.5.1", -] - -[[package]] -name = "rand_isaac" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rand_jitter" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" -dependencies = [ - "libc", - "rand_core 0.4.2", - "winapi", -] - -[[package]] -name = "rand_os" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" -dependencies = [ - "cloudabi", - "fuchsia-cprng", - "libc", - "rand_core 0.4.2", - "rdrand", - "winapi", -] - -[[package]] -name = "rand_pcg" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" -dependencies = [ - "autocfg 0.1.7", - "rand_core 0.4.2", -] - -[[package]] -name = "rand_pcg" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" -dependencies = [ - "rand_core 0.5.1", -] - -[[package]] -name = "rand_xorshift" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "rdrand" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" -dependencies = [ - "rand_core 0.3.1", -] - -[[package]] -name = "redox_syscall" -version = "0.1.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" - -[[package]] -name = "regex" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" -dependencies = [ - "aho-corasick", - "memchr 2.4.1", - "regex-syntax", - "thread_local", - "utf8-ranges", -] - -[[package]] -name = "regex-syntax" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" -dependencies = [ - "ucd-util", -] - -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - -[[package]] -name = "rustc_version" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" -dependencies = [ - "semver", -] - -[[package]] -name = "ryu" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" - -[[package]] -name = "same-file" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "scroll" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fda28d4b4830b807a8b43f7b0e6b5df875311b3e7621d84577188c175b6ec1ec" -dependencies = [ - "scroll_derive", -] - -[[package]] -name = "scroll_derive" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aaaae8f38bb311444cfb7f1979af0bc9240d95795f75f9ceddf6a59b79ceffa0" -dependencies = [ - "proc-macro2", - "quote 1.0.9", - "syn 1.0.74", -] - -[[package]] -name = "secp256k1" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6179428c22c73ac0fbb7b5579a56353ce78ba29759b3b8575183336ea74cdfb" -dependencies = [ - "secp256k1-sys", -] - -[[package]] -name = "secp256k1-sys" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11553d210db090930f4432bea123b31f70bbf693ace14504ea2a35e796c28dd2" -dependencies = [ - "cc", -] - -[[package]] -name = "semver" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" -dependencies = [ - "semver-parser", -] - -[[package]] -name = "semver-parser" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" -dependencies = [ - "pest", -] - -[[package]] -name = "serde" -version = "1.0.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" -dependencies = [ - "proc-macro2", - "quote 1.0.9", - "syn 1.0.74", -] - -[[package]] -name = "serde_json" -version = "1.0.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "sha3" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" -dependencies = [ - "block-buffer", - "digest", - "keccak", - "opaque-debug", -] - -[[package]] -name = "siphasher" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" - -[[package]] -name = "sparse-merkle-tree" -version = "0.5.2-rc1" -source = "git+https://github.com/nervosnetwork/sparse-merkle-tree.git?rev=2dce546#2dce546eab6f7eaaab3a0886247fd12ac798ad28" -dependencies = [ - "blake2b-rs", - "cfg-if 0.1.10", -] - -[[package]] -name = "strum" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ca6e4730f517e041e547ffe23d29daab8de6b73af4b6ae2a002108169f5e7da" - -[[package]] -name = "strum_macros" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3384590878eb0cab3b128e844412e2d010821e7e091211b9d87324173ada7db8" -dependencies = [ - "quote 0.3.15", - "syn 0.11.11", -] - -[[package]] -name = "syn" -version = "0.11.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" -dependencies = [ - "quote 0.3.15", - "synom", - "unicode-xid 0.0.4", -] - -[[package]] -name = "syn" -version = "1.0.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" -dependencies = [ - "proc-macro2", - "quote 1.0.9", - "unicode-xid 0.2.2", -] - -[[package]] -name = "synom" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" -dependencies = [ - "unicode-xid 0.0.4", -] - -[[package]] -name = "tempfile" -version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" -dependencies = [ - "cfg-if 0.1.10", - "libc", - "rand 0.7.3", - "redox_syscall", - "remove_dir_all", - "winapi", -] - -[[package]] -name = "thiserror" -version = "1.0.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" -dependencies = [ - "proc-macro2", - "quote 1.0.9", - "syn 1.0.74", -] - -[[package]] -name = "thread_local" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" -dependencies = [ - "lazy_static", -] - -[[package]] -name = "toml" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" -dependencies = [ - "serde", -] - -[[package]] -name = "typenum" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" - -[[package]] -name = "ucd-trie" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" - -[[package]] -name = "ucd-util" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c85f514e095d348c279b1e5cd76795082cf15bd59b93207832abe0b1d8fed236" - -[[package]] -name = "unicode-xid" -version = "0.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" - -[[package]] -name = "unicode-xid" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" - -[[package]] -name = "utf8-ranges" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" - -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - -[[package]] -name = "version_check" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" - -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - -[[package]] -name = "walkdir" -version = "2.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" -dependencies = [ - "same-file", - "winapi", - "winapi-util", -] - -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/tests/omni_lock_rust/Cargo.toml b/tests/omni_lock_rust/Cargo.toml deleted file mode 100644 index 86fe702..0000000 --- a/tests/omni_lock_rust/Cargo.toml +++ /dev/null @@ -1,32 +0,0 @@ -[package] -name = "omni-lock-test" -version = "0.1.0" -authors = ["xjd ( - addr: &str, - script_type: ScriptGroupType, - script_hash: Byte32, - program: &Bytes, - args: &[Bytes], - verifier: &TransactionScriptsVerifier<'a, DL>, -) { - let script_group = get_script_group(&verifier, script_type, &script_hash).unwrap(); - - // GDB path - let listener = TcpListener::bind(addr).expect("listen"); - let script_version = ScriptVersion::V1; - let max_cycle = 70_000_000u64; - - for res in listener.incoming() { - if let Ok(stream) = res { - let core_machine = AsmCoreMachine::new( - script_version.vm_isa(), - script_version.vm_version(), - max_cycle, - ); - let builder = DefaultMachineBuilder::new(core_machine) - .instruction_cycle_func(verifier.cost_model()) - .syscall(Box::new(Stdio::new(true))); - let builder = verifier - .generate_syscalls(script_version, script_group) - .into_iter() - .fold(builder, |builder, syscall| builder.syscall(syscall)); - let mut machine = AsmMachine::new(builder.build(), None); - let bytes = machine.load_program(&program, args).expect("load program"); - machine - .machine - .add_cycles(transferred_byte_cycles(bytes)) - .expect("load program cycles"); - machine.machine.set_running(true); - let h = GdbHandler::new(machine); - process_packets_from(stream.try_clone().unwrap(), stream, h); - } - } -} - -fn get_script_group<'a, DL: CellDataProvider + HeaderProvider>( - verifier: &'a TransactionScriptsVerifier<'a, DL>, - group_type: ScriptGroupType, - hash: &Byte32, -) -> Option<&'a ScriptGroup> { - for (t, h, g) in verifier.groups() { - if group_type == t && h == hash { - return Some(g); - } - } - None -} diff --git a/tests/omni_lock_rust/src/lib.rs b/tests/omni_lock_rust/src/lib.rs deleted file mode 100644 index 3e548dc..0000000 --- a/tests/omni_lock_rust/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod debug_utils; -pub mod omni_lock; -pub mod xudt_rce_mol; diff --git a/tests/omni_lock_rust/src/omni_lock.rs b/tests/omni_lock_rust/src/omni_lock.rs deleted file mode 100644 index caadfe4..0000000 --- a/tests/omni_lock_rust/src/omni_lock.rs +++ /dev/null @@ -1,1158 +0,0 @@ -// Generated by Molecule 0.7.0 - -#![allow(unused_imports)] - -use ckb_types::molecule; -use ckb_types::packed::*; -use ckb_types::prelude::*; -// these lines above are manually added -// replace "::molecule" to "molecule" in below code -use super::xudt_rce_mol::*; - -use molecule::prelude::*; - -#[derive(Clone)] -pub struct Auth(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Auth { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Auth { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Auth { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl ::core::default::Default for Auth { - fn default() -> Self { - let v: Vec = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - Auth::new_unchecked(v.into()) - } -} -impl Auth { - pub const TOTAL_SIZE: usize = 21; - pub const ITEM_SIZE: usize = 1; - pub const ITEM_COUNT: usize = 21; - pub fn nth0(&self) -> Byte { - Byte::new_unchecked(self.0.slice(0..1)) - } - pub fn nth1(&self) -> Byte { - Byte::new_unchecked(self.0.slice(1..2)) - } - pub fn nth2(&self) -> Byte { - Byte::new_unchecked(self.0.slice(2..3)) - } - pub fn nth3(&self) -> Byte { - Byte::new_unchecked(self.0.slice(3..4)) - } - pub fn nth4(&self) -> Byte { - Byte::new_unchecked(self.0.slice(4..5)) - } - pub fn nth5(&self) -> Byte { - Byte::new_unchecked(self.0.slice(5..6)) - } - pub fn nth6(&self) -> Byte { - Byte::new_unchecked(self.0.slice(6..7)) - } - pub fn nth7(&self) -> Byte { - Byte::new_unchecked(self.0.slice(7..8)) - } - pub fn nth8(&self) -> Byte { - Byte::new_unchecked(self.0.slice(8..9)) - } - pub fn nth9(&self) -> Byte { - Byte::new_unchecked(self.0.slice(9..10)) - } - pub fn nth10(&self) -> Byte { - Byte::new_unchecked(self.0.slice(10..11)) - } - pub fn nth11(&self) -> Byte { - Byte::new_unchecked(self.0.slice(11..12)) - } - pub fn nth12(&self) -> Byte { - Byte::new_unchecked(self.0.slice(12..13)) - } - pub fn nth13(&self) -> Byte { - Byte::new_unchecked(self.0.slice(13..14)) - } - pub fn nth14(&self) -> Byte { - Byte::new_unchecked(self.0.slice(14..15)) - } - pub fn nth15(&self) -> Byte { - Byte::new_unchecked(self.0.slice(15..16)) - } - pub fn nth16(&self) -> Byte { - Byte::new_unchecked(self.0.slice(16..17)) - } - pub fn nth17(&self) -> Byte { - Byte::new_unchecked(self.0.slice(17..18)) - } - pub fn nth18(&self) -> Byte { - Byte::new_unchecked(self.0.slice(18..19)) - } - pub fn nth19(&self) -> Byte { - Byte::new_unchecked(self.0.slice(19..20)) - } - pub fn nth20(&self) -> Byte { - Byte::new_unchecked(self.0.slice(20..21)) - } - pub fn raw_data(&self) -> molecule::bytes::Bytes { - self.as_bytes() - } - pub fn as_reader<'r>(&'r self) -> AuthReader<'r> { - AuthReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Auth { - type Builder = AuthBuilder; - const NAME: &'static str = "Auth"; - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Auth(data) - } - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - AuthReader::from_slice(slice).map(|reader| reader.to_entity()) - } - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - AuthReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - fn as_builder(self) -> Self::Builder { - Self::new_builder().set([ - self.nth0(), - self.nth1(), - self.nth2(), - self.nth3(), - self.nth4(), - self.nth5(), - self.nth6(), - self.nth7(), - self.nth8(), - self.nth9(), - self.nth10(), - self.nth11(), - self.nth12(), - self.nth13(), - self.nth14(), - self.nth15(), - self.nth16(), - self.nth17(), - self.nth18(), - self.nth19(), - self.nth20(), - ]) - } -} -#[derive(Clone, Copy)] -pub struct AuthReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for AuthReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for AuthReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for AuthReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - let raw_data = hex_string(&self.raw_data()); - write!(f, "{}(0x{})", Self::NAME, raw_data) - } -} -impl<'r> AuthReader<'r> { - pub const TOTAL_SIZE: usize = 21; - pub const ITEM_SIZE: usize = 1; - pub const ITEM_COUNT: usize = 21; - pub fn nth0(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[0..1]) - } - pub fn nth1(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[1..2]) - } - pub fn nth2(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[2..3]) - } - pub fn nth3(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[3..4]) - } - pub fn nth4(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[4..5]) - } - pub fn nth5(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[5..6]) - } - pub fn nth6(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[6..7]) - } - pub fn nth7(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[7..8]) - } - pub fn nth8(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[8..9]) - } - pub fn nth9(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[9..10]) - } - pub fn nth10(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[10..11]) - } - pub fn nth11(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[11..12]) - } - pub fn nth12(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[12..13]) - } - pub fn nth13(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[13..14]) - } - pub fn nth14(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[14..15]) - } - pub fn nth15(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[15..16]) - } - pub fn nth16(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[16..17]) - } - pub fn nth17(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[17..18]) - } - pub fn nth18(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[18..19]) - } - pub fn nth19(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[19..20]) - } - pub fn nth20(&self) -> ByteReader<'r> { - ByteReader::new_unchecked(&self.as_slice()[20..21]) - } - pub fn raw_data(&self) -> &'r [u8] { - self.as_slice() - } -} -impl<'r> molecule::prelude::Reader<'r> for AuthReader<'r> { - type Entity = Auth; - const NAME: &'static str = "AuthReader"; - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - fn new_unchecked(slice: &'r [u8]) -> Self { - AuthReader(slice) - } - fn as_slice(&self) -> &'r [u8] { - self.0 - } - fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len != Self::TOTAL_SIZE { - return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); - } - Ok(()) - } -} -pub struct AuthBuilder(pub(crate) [Byte; 21]); -impl ::core::fmt::Debug for AuthBuilder { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:?})", Self::NAME, &self.0[..]) - } -} -impl ::core::default::Default for AuthBuilder { - fn default() -> Self { - AuthBuilder([ - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - Byte::default(), - ]) - } -} -impl AuthBuilder { - pub const TOTAL_SIZE: usize = 21; - pub const ITEM_SIZE: usize = 1; - pub const ITEM_COUNT: usize = 21; - pub fn set(mut self, v: [Byte; 21]) -> Self { - self.0 = v; - self - } - pub fn nth0(mut self, v: Byte) -> Self { - self.0[0] = v; - self - } - pub fn nth1(mut self, v: Byte) -> Self { - self.0[1] = v; - self - } - pub fn nth2(mut self, v: Byte) -> Self { - self.0[2] = v; - self - } - pub fn nth3(mut self, v: Byte) -> Self { - self.0[3] = v; - self - } - pub fn nth4(mut self, v: Byte) -> Self { - self.0[4] = v; - self - } - pub fn nth5(mut self, v: Byte) -> Self { - self.0[5] = v; - self - } - pub fn nth6(mut self, v: Byte) -> Self { - self.0[6] = v; - self - } - pub fn nth7(mut self, v: Byte) -> Self { - self.0[7] = v; - self - } - pub fn nth8(mut self, v: Byte) -> Self { - self.0[8] = v; - self - } - pub fn nth9(mut self, v: Byte) -> Self { - self.0[9] = v; - self - } - pub fn nth10(mut self, v: Byte) -> Self { - self.0[10] = v; - self - } - pub fn nth11(mut self, v: Byte) -> Self { - self.0[11] = v; - self - } - pub fn nth12(mut self, v: Byte) -> Self { - self.0[12] = v; - self - } - pub fn nth13(mut self, v: Byte) -> Self { - self.0[13] = v; - self - } - pub fn nth14(mut self, v: Byte) -> Self { - self.0[14] = v; - self - } - pub fn nth15(mut self, v: Byte) -> Self { - self.0[15] = v; - self - } - pub fn nth16(mut self, v: Byte) -> Self { - self.0[16] = v; - self - } - pub fn nth17(mut self, v: Byte) -> Self { - self.0[17] = v; - self - } - pub fn nth18(mut self, v: Byte) -> Self { - self.0[18] = v; - self - } - pub fn nth19(mut self, v: Byte) -> Self { - self.0[19] = v; - self - } - pub fn nth20(mut self, v: Byte) -> Self { - self.0[20] = v; - self - } -} -impl molecule::prelude::Builder for AuthBuilder { - type Entity = Auth; - const NAME: &'static str = "AuthBuilder"; - fn expected_length(&self) -> usize { - Self::TOTAL_SIZE - } - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - writer.write_all(self.0[0].as_slice())?; - writer.write_all(self.0[1].as_slice())?; - writer.write_all(self.0[2].as_slice())?; - writer.write_all(self.0[3].as_slice())?; - writer.write_all(self.0[4].as_slice())?; - writer.write_all(self.0[5].as_slice())?; - writer.write_all(self.0[6].as_slice())?; - writer.write_all(self.0[7].as_slice())?; - writer.write_all(self.0[8].as_slice())?; - writer.write_all(self.0[9].as_slice())?; - writer.write_all(self.0[10].as_slice())?; - writer.write_all(self.0[11].as_slice())?; - writer.write_all(self.0[12].as_slice())?; - writer.write_all(self.0[13].as_slice())?; - writer.write_all(self.0[14].as_slice())?; - writer.write_all(self.0[15].as_slice())?; - writer.write_all(self.0[16].as_slice())?; - writer.write_all(self.0[17].as_slice())?; - writer.write_all(self.0[18].as_slice())?; - writer.write_all(self.0[19].as_slice())?; - writer.write_all(self.0[20].as_slice())?; - Ok(()) - } - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Auth::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct Identity(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for Identity { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for Identity { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for Identity { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "identity", self.identity())?; - write!(f, ", {}: {}", "proofs", self.proofs())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for Identity { - fn default() -> Self { - let v: Vec = vec![ - 37, 0, 0, 0, 12, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 4, 0, 0, 0, - ]; - Identity::new_unchecked(v.into()) - } -} -impl Identity { - pub const FIELD_COUNT: usize = 2; - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - pub fn identity(&self) -> Auth { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - Auth::new_unchecked(self.0.slice(start..end)) - } - pub fn proofs(&self) -> SmtProofEntryVec { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SmtProofEntryVec::new_unchecked(self.0.slice(start..end)) - } else { - SmtProofEntryVec::new_unchecked(self.0.slice(start..)) - } - } - pub fn as_reader<'r>(&'r self) -> IdentityReader<'r> { - IdentityReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for Identity { - type Builder = IdentityBuilder; - const NAME: &'static str = "Identity"; - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - Identity(data) - } - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - IdentityReader::from_slice(slice).map(|reader| reader.to_entity()) - } - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - IdentityReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .identity(self.identity()) - .proofs(self.proofs()) - } -} -#[derive(Clone, Copy)] -pub struct IdentityReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for IdentityReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for IdentityReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for IdentityReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "identity", self.identity())?; - write!(f, ", {}: {}", "proofs", self.proofs())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> IdentityReader<'r> { - pub const FIELD_COUNT: usize = 2; - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - pub fn identity(&self) -> AuthReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - AuthReader::new_unchecked(&self.as_slice()[start..end]) - } - pub fn proofs(&self) -> SmtProofEntryVecReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[12..]) as usize; - SmtProofEntryVecReader::new_unchecked(&self.as_slice()[start..end]) - } else { - SmtProofEntryVecReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for IdentityReader<'r> { - type Entity = Identity; - const NAME: &'static str = "IdentityReader"; - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - fn new_unchecked(slice: &'r [u8]) -> Self { - IdentityReader(slice) - } - fn as_slice(&self) -> &'r [u8] { - self.0 - } - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - AuthReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - SmtProofEntryVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct IdentityBuilder { - pub(crate) identity: Auth, - pub(crate) proofs: SmtProofEntryVec, -} -impl IdentityBuilder { - pub const FIELD_COUNT: usize = 2; - pub fn identity(mut self, v: Auth) -> Self { - self.identity = v; - self - } - pub fn proofs(mut self, v: SmtProofEntryVec) -> Self { - self.proofs = v; - self - } -} -impl molecule::prelude::Builder for IdentityBuilder { - type Entity = Identity; - const NAME: &'static str = "IdentityBuilder"; - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.identity.as_slice().len() - + self.proofs.as_slice().len() - } - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.identity.as_slice().len(); - offsets.push(total_size); - total_size += self.proofs.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.identity.as_slice())?; - writer.write_all(self.proofs.as_slice())?; - Ok(()) - } - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - Identity::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct IdentityOpt(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for IdentityOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for IdentityOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for IdentityOpt { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - if let Some(v) = self.to_opt() { - write!(f, "{}(Some({}))", Self::NAME, v) - } else { - write!(f, "{}(None)", Self::NAME) - } - } -} -impl ::core::default::Default for IdentityOpt { - fn default() -> Self { - let v: Vec = vec![]; - IdentityOpt::new_unchecked(v.into()) - } -} -impl IdentityOpt { - pub fn is_none(&self) -> bool { - self.0.is_empty() - } - pub fn is_some(&self) -> bool { - !self.0.is_empty() - } - pub fn to_opt(&self) -> Option { - if self.is_none() { - None - } else { - Some(Identity::new_unchecked(self.0.clone())) - } - } - pub fn as_reader<'r>(&'r self) -> IdentityOptReader<'r> { - IdentityOptReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for IdentityOpt { - type Builder = IdentityOptBuilder; - const NAME: &'static str = "IdentityOpt"; - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - IdentityOpt(data) - } - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - IdentityOptReader::from_slice(slice).map(|reader| reader.to_entity()) - } - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - IdentityOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - fn as_builder(self) -> Self::Builder { - Self::new_builder().set(self.to_opt()) - } -} -#[derive(Clone, Copy)] -pub struct IdentityOptReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for IdentityOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for IdentityOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for IdentityOptReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - if let Some(v) = self.to_opt() { - write!(f, "{}(Some({}))", Self::NAME, v) - } else { - write!(f, "{}(None)", Self::NAME) - } - } -} -impl<'r> IdentityOptReader<'r> { - pub fn is_none(&self) -> bool { - self.0.is_empty() - } - pub fn is_some(&self) -> bool { - !self.0.is_empty() - } - pub fn to_opt(&self) -> Option> { - if self.is_none() { - None - } else { - Some(IdentityReader::new_unchecked(self.as_slice())) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for IdentityOptReader<'r> { - type Entity = IdentityOpt; - const NAME: &'static str = "IdentityOptReader"; - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - fn new_unchecked(slice: &'r [u8]) -> Self { - IdentityOptReader(slice) - } - fn as_slice(&self) -> &'r [u8] { - self.0 - } - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - if !slice.is_empty() { - IdentityReader::verify(&slice[..], compatible)?; - } - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct IdentityOptBuilder(pub(crate) Option); -impl IdentityOptBuilder { - pub fn set(mut self, v: Option) -> Self { - self.0 = v; - self - } -} -impl molecule::prelude::Builder for IdentityOptBuilder { - type Entity = IdentityOpt; - const NAME: &'static str = "IdentityOptBuilder"; - fn expected_length(&self) -> usize { - self.0 - .as_ref() - .map(|ref inner| inner.as_slice().len()) - .unwrap_or(0) - } - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - self.0 - .as_ref() - .map(|ref inner| writer.write_all(inner.as_slice())) - .unwrap_or(Ok(())) - } - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - IdentityOpt::new_unchecked(inner.into()) - } -} -#[derive(Clone)] -pub struct OmniLockWitnessLock(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for OmniLockWitnessLock { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for OmniLockWitnessLock { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for OmniLockWitnessLock { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "signature", self.signature())?; - write!(f, ", {}: {}", "omni_identity", self.omni_identity())?; - write!(f, ", {}: {}", "preimage", self.preimage())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl ::core::default::Default for OmniLockWitnessLock { - fn default() -> Self { - let v: Vec = vec![16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0]; - OmniLockWitnessLock::new_unchecked(v.into()) - } -} -impl OmniLockWitnessLock { - pub const FIELD_COUNT: usize = 3; - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - pub fn signature(&self) -> BytesOpt { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - BytesOpt::new_unchecked(self.0.slice(start..end)) - } - pub fn omni_identity(&self) -> IdentityOpt { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - IdentityOpt::new_unchecked(self.0.slice(start..end)) - } - pub fn preimage(&self) -> BytesOpt { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[16..]) as usize; - BytesOpt::new_unchecked(self.0.slice(start..end)) - } else { - BytesOpt::new_unchecked(self.0.slice(start..)) - } - } - pub fn as_reader<'r>(&'r self) -> OmniLockWitnessLockReader<'r> { - OmniLockWitnessLockReader::new_unchecked(self.as_slice()) - } -} -impl molecule::prelude::Entity for OmniLockWitnessLock { - type Builder = OmniLockWitnessLockBuilder; - const NAME: &'static str = "OmniLockWitnessLock"; - fn new_unchecked(data: molecule::bytes::Bytes) -> Self { - OmniLockWitnessLock(data) - } - fn as_bytes(&self) -> molecule::bytes::Bytes { - self.0.clone() - } - fn as_slice(&self) -> &[u8] { - &self.0[..] - } - fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { - OmniLockWitnessLockReader::from_slice(slice).map(|reader| reader.to_entity()) - } - fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { - OmniLockWitnessLockReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) - } - fn new_builder() -> Self::Builder { - ::core::default::Default::default() - } - fn as_builder(self) -> Self::Builder { - Self::new_builder() - .signature(self.signature()) - .omni_identity(self.omni_identity()) - .preimage(self.preimage()) - } -} -#[derive(Clone, Copy)] -pub struct OmniLockWitnessLockReader<'r>(&'r [u8]); -impl<'r> ::core::fmt::LowerHex for OmniLockWitnessLockReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl<'r> ::core::fmt::Debug for OmniLockWitnessLockReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl<'r> ::core::fmt::Display for OmniLockWitnessLockReader<'r> { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} {{ ", Self::NAME)?; - write!(f, "{}: {}", "signature", self.signature())?; - write!(f, ", {}: {}", "omni_identity", self.omni_identity())?; - write!(f, ", {}: {}", "preimage", self.preimage())?; - let extra_count = self.count_extra_fields(); - if extra_count != 0 { - write!(f, ", .. ({} fields)", extra_count)?; - } - write!(f, " }}") - } -} -impl<'r> OmniLockWitnessLockReader<'r> { - pub const FIELD_COUNT: usize = 3; - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - pub fn field_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - pub fn count_extra_fields(&self) -> usize { - self.field_count() - Self::FIELD_COUNT - } - pub fn has_extra_fields(&self) -> bool { - Self::FIELD_COUNT != self.field_count() - } - pub fn signature(&self) -> BytesOptReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[4..]) as usize; - let end = molecule::unpack_number(&slice[8..]) as usize; - BytesOptReader::new_unchecked(&self.as_slice()[start..end]) - } - pub fn omni_identity(&self) -> IdentityOptReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[8..]) as usize; - let end = molecule::unpack_number(&slice[12..]) as usize; - IdentityOptReader::new_unchecked(&self.as_slice()[start..end]) - } - pub fn preimage(&self) -> BytesOptReader<'r> { - let slice = self.as_slice(); - let start = molecule::unpack_number(&slice[12..]) as usize; - if self.has_extra_fields() { - let end = molecule::unpack_number(&slice[16..]) as usize; - BytesOptReader::new_unchecked(&self.as_slice()[start..end]) - } else { - BytesOptReader::new_unchecked(&self.as_slice()[start..]) - } - } -} -impl<'r> molecule::prelude::Reader<'r> for OmniLockWitnessLockReader<'r> { - type Entity = OmniLockWitnessLock; - const NAME: &'static str = "OmniLockWitnessLockReader"; - fn to_entity(&self) -> Self::Entity { - Self::Entity::new_unchecked(self.as_slice().to_owned().into()) - } - fn new_unchecked(slice: &'r [u8]) -> Self { - OmniLockWitnessLockReader(slice) - } - fn as_slice(&self) -> &'r [u8] { - self.0 - } - fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { - use molecule::verification_error as ve; - let slice_len = slice.len(); - if slice_len < molecule::NUMBER_SIZE { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); - } - let total_size = molecule::unpack_number(slice) as usize; - if slice_len != total_size { - return ve!(Self, TotalSizeNotMatch, total_size, slice_len); - } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } - if slice_len < molecule::NUMBER_SIZE * 2 { - return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); - } - let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; - if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { - return ve!(Self, OffsetsNotMatch); - } - if slice_len < offset_first { - return ve!(Self, HeaderIsBroken, offset_first, slice_len); - } - let field_count = offset_first / molecule::NUMBER_SIZE - 1; - if field_count < Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - } else if !compatible && field_count > Self::FIELD_COUNT { - return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count); - }; - let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] - .chunks_exact(molecule::NUMBER_SIZE) - .map(|x| molecule::unpack_number(x) as usize) - .collect(); - offsets.push(total_size); - if offsets.windows(2).any(|i| i[0] > i[1]) { - return ve!(Self, OffsetsNotMatch); - } - BytesOptReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - IdentityOptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?; - BytesOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; - Ok(()) - } -} -#[derive(Debug, Default)] -pub struct OmniLockWitnessLockBuilder { - pub(crate) signature: BytesOpt, - pub(crate) omni_identity: IdentityOpt, - pub(crate) preimage: BytesOpt, -} -impl OmniLockWitnessLockBuilder { - pub const FIELD_COUNT: usize = 3; - pub fn signature(mut self, v: BytesOpt) -> Self { - self.signature = v; - self - } - pub fn omni_identity(mut self, v: IdentityOpt) -> Self { - self.omni_identity = v; - self - } - pub fn preimage(mut self, v: BytesOpt) -> Self { - self.preimage = v; - self - } -} -impl molecule::prelude::Builder for OmniLockWitnessLockBuilder { - type Entity = OmniLockWitnessLock; - const NAME: &'static str = "OmniLockWitnessLockBuilder"; - fn expected_length(&self) -> usize { - molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1) - + self.signature.as_slice().len() - + self.omni_identity.as_slice().len() - + self.preimage.as_slice().len() - } - fn write(&self, writer: &mut W) -> molecule::io::Result<()> { - let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); - let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); - offsets.push(total_size); - total_size += self.signature.as_slice().len(); - offsets.push(total_size); - total_size += self.omni_identity.as_slice().len(); - offsets.push(total_size); - total_size += self.preimage.as_slice().len(); - writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; - for offset in offsets.into_iter() { - writer.write_all(&molecule::pack_number(offset as molecule::Number))?; - } - writer.write_all(self.signature.as_slice())?; - writer.write_all(self.omni_identity.as_slice())?; - writer.write_all(self.preimage.as_slice())?; - Ok(()) - } - fn build(&self) -> Self::Entity { - let mut inner = Vec::with_capacity(self.expected_length()); - self.write(&mut inner) - .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); - OmniLockWitnessLock::new_unchecked(inner.into()) - } -} diff --git a/tests/omni_lock_rust/src/xudt_rce_mol.rs b/tests/omni_lock_rust/src/xudt_rce_mol.rs deleted file mode 100644 index 7417bd8..0000000 --- a/tests/omni_lock_rust/src/xudt_rce_mol.rs +++ /dev/null @@ -1,3287 +0,0 @@ -// Generated by Molecule 0.7.0 - -#![allow(unused_imports)] - -use ckb_types::molecule; -use ckb_types::packed::*; -use ckb_types::prelude::*; -// these lines above are manually added -// replace "::molecule" to "molecule" in below code - -#[derive(Clone)] -pub struct ScriptVec(molecule::bytes::Bytes); -impl ::core::fmt::LowerHex for ScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - use molecule::hex_string; - if f.alternate() { - write!(f, "0x")?; - } - write!(f, "{}", hex_string(self.as_slice())) - } -} -impl ::core::fmt::Debug for ScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{}({:#x})", Self::NAME, self) - } -} -impl ::core::fmt::Display for ScriptVec { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "{} [", Self::NAME)?; - for i in 0..self.len() { - if i == 0 { - write!(f, "{}", self.get_unchecked(i))?; - } else { - write!(f, ", {}", self.get_unchecked(i))?; - } - } - write!(f, "]") - } -} -impl ::core::default::Default for ScriptVec { - fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - ScriptVec::new_unchecked(v.into()) - } -} -impl ScriptVec { - pub fn total_size(&self) -> usize { - molecule::unpack_number(self.as_slice()) as usize - } - pub fn item_count(&self) -> usize { - if self.total_size() == molecule::NUMBER_SIZE { - 0 - } else { - (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 - } - } - pub fn len(&self) -> usize { - self.item_count() - } - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - pub fn get(&self, idx: usize) -> Option