Skip to content

Commit

Permalink
Update molecule codes (#62)
Browse files Browse the repository at this point in the history
* Update molecule codes
* synchronized from: https://github.com/nervosnetwork/molecule/tree/master/bindings/c/include
* Update clang-format with docker
  • Loading branch information
XuJiandong authored Sep 12, 2024
1 parent be84dcf commit 744c62e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ jobs:
- uses: actions/checkout@v1
- name: Update submodules
run: git submodule update --init
- name: Build contract
- name: Check format
run: make fmt
- name: Check compilation
run: make all-via-docker
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ LIB := libdummylibc.a
CFLAGS := -Wall -Werror -Wextra -Wno-unused-parameter -Wno-dangling-pointer -Wno-nonnull -Wno-nonnull-compare -fno-builtin-printf -fno-builtin-memcmp -O3 -g -fdata-sections -ffunction-sections

LDFLAGS := -nostdlib -nostartfiles -Wl,-static -Wl,--gc-sections
EXTRA := -I . -I libc -I molecule -Wno-unused-function
EXTRA := -I . -I libc -I molecule -Wno-unused-function -Wno-array-bounds -Wno-stringop-overflow

# nervos/ckb-riscv-gnu-toolchain:jammy-20230214
BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:d175f4a766b4b17a44bd9bbeca8e24ab2427ba615738016dc49e194046e6b28b
CLANG_FORMAT_DOCKER := xujiandong/ckb-riscv-llvm-toolchain@sha256:6409ab0d3e335c74088b54f4f73252f4b3367ae364d5c7ca7acee82135f5af4d


default: fmt
Expand All @@ -20,9 +21,12 @@ all: tests/ci
tests/ci: tests/ci.c
$(CC) $(CFLAGS) $(EXTRA) $(LDFLAGS) -o $@ $<

ALL_C_SOURCE := $(wildcard *.h */*.h *.c */*.c libc/*.h libc/src/*.c)

fmt:
clang-format -i -style=Google $(wildcard *.h */*.h *.c */*.c libc/*.h libc/src/*.c)
git diff --exit-code
docker run -u $(shell id -u):$(shell id -g) --rm -v `pwd`:/code ${CLANG_FORMAT_DOCKER} bash -c "cd code && clang-format -i -style='{BasedOnStyle: google, SortIncludes: false}' $(ALL_C_SOURCE)"
git diff --exit-code $(ALL_C_SOURCE)


$(LIB): impl.o
$(AR) rcs $@ $^
Expand Down
4 changes: 2 additions & 2 deletions ckb_keccak256.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ static void sha3_process_block(uint64_t hash[25], const uint64_t *block) {
sha3_permutation(hash);
}

//#define SHA3_FINALIZED 0x80000000
//#define SHA3_FINALIZED 0x8000
// #define SHA3_FINALIZED 0x80000000
// #define SHA3_FINALIZED 0x8000

/**
* Calculate message hash.
Expand Down
20 changes: 0 additions & 20 deletions molecule/molecule_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ MOLECULE_API_DECORATOR void mol_builder_initialize_fixed_size(
builder->data_cap = 0;
} else {
builder->data_ptr = (uint8_t *)malloc(fixed_size);
if (builder->data_ptr == NULL) {
return;
}
memset(builder->data_ptr, 0x00, fixed_size);
builder->data_used = fixed_size;
builder->data_cap = fixed_size;
Expand All @@ -97,9 +94,6 @@ MOLECULE_API_DECORATOR void mol_union_builder_initialize(
mol_builder_t *builder, mol_num_t data_capacity, mol_num_t item_id,
const uint8_t *default_ptr, mol_num_t default_len) {
builder->data_ptr = (uint8_t *)malloc(data_capacity);
if (builder->data_ptr == NULL) {
return;
}
builder->data_cap = data_capacity;
mol_pack_number(builder->data_ptr, &item_id);
builder->data_used = MOL_NUM_T_SIZE + default_len;
Expand All @@ -117,15 +111,9 @@ MOLECULE_API_DECORATOR void mol_builder_initialize_with_capacity(
mol_builder_t *builder, mol_num_t data_capacity,
mol_num_t number_capacity) {
builder->data_ptr = (uint8_t *)malloc(data_capacity);
if (builder->data_ptr == NULL) {
return;
}
builder->data_used = 0;
builder->data_cap = data_capacity;
builder->number_ptr = (mol_num_t *)malloc(number_capacity);
if (builder->number_ptr == NULL) {
return;
}
builder->number_used = 0;
builder->number_cap = number_capacity;
}
Expand Down Expand Up @@ -303,10 +291,6 @@ mol_fixvec_builder_finalize(mol_builder_t builder) {
res.errno = MOL_OK;
res.seg.size = MOL_NUM_T_SIZE + builder.data_used;
res.seg.ptr = (uint8_t *)malloc(res.seg.size);
if (res.seg.ptr == NULL) {
res.errno = MOL_ERR;
return res;
}
mol_pack_number(res.seg.ptr, &builder.number_ptr[0]);
if (builder.data_used > 0) {
memcpy((res.seg.ptr + MOL_NUM_T_SIZE), builder.data_ptr, builder.data_used);
Expand All @@ -321,10 +305,6 @@ mol_dynvec_builder_finalize(mol_builder_t builder) {
res.errno = MOL_OK;
res.seg.size = MOL_NUM_T_SIZE + builder.number_used + builder.data_used;
res.seg.ptr = (uint8_t *)malloc(res.seg.size);
if (res.seg.ptr == NULL) {
res.errno = MOL_ERR;
return res;
}
mol_pack_number(res.seg.ptr, &res.seg.size);
mol_num_t number_count = builder.number_used / MOL_NUM_T_SIZE;
mol_num_t header_size = MOL_NUM_T_SIZE + builder.number_used;
Expand Down
12 changes: 12 additions & 0 deletions molecule/molecule_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ mol_fixvec_slice_raw_bytes(const mol_seg_t *input) {
return seg;
}

// Check if a segment(`part`) is contained by `total`
MOLECULE_API_DECORATOR bool mol_contained_by(const mol_seg_t *part,
const mol_seg_t *total) {
if (part->ptr < total->ptr) {
return MOL_ERR_OFFSET;
}
if ((part->ptr + part->size) > (total->ptr + total->size)) {
return MOL_ERR_OFFSET;
}
return MOL_OK;
}

/*
* Undef macros which are internal use only.
*/
Expand Down
2 changes: 1 addition & 1 deletion simulator/ckb_syscall_simulator.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// make assert working under release
#undef NDEBUG
//#define CKB_SIMULATOR_VERBOSE
// #define CKB_SIMULATOR_VERBOSE

#include "ckb_syscall_simulator.h"

Expand Down
4 changes: 1 addition & 3 deletions tests/ci.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@
#include "molecule2_reader.h"
#include "molecule_builder.h"

int main(int argc, const char* argv[]) {
return 0;
}
int main(int argc, const char* argv[]) { return 0; }

0 comments on commit 744c62e

Please sign in to comment.