Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Cobuild integration and Solana support #3

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[submodule "deps/ckb-c-stdlib-20210801"]
path = deps/ckb-c-stdlib-20210801
url = https://github.com/nervosnetwork/ckb-c-stdlib.git
branch = master
[submodule "deps/sparse-merkle-tree"]
path = deps/sparse-merkle-tree
url = https://github.com/nervosnetwork/sparse-merkle-tree.git
branch = master
[submodule "deps/secp256k1-20210801"]
path = deps/secp256k1-20210801
[submodule "deps/ckb-c-stdlib"]
path = deps/ckb-c-stdlib
url = https://github.com/nervosnetwork/ckb-c-stdlib.git
[submodule "deps/secp256k1"]
path = deps/secp256k1
url = https://github.com/nervosnetwork/secp256k1.git
branch = schnorr
[submodule "deps/ed25519"]
path = deps/ed25519
url = https://github.com/nervosnetwork/ed25519.git
98 changes: 56 additions & 42 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,67 +1,72 @@

TARGET := riscv64-unknown-linux-gnu
CC := $(TARGET)-gcc
LD := $(TARGET)-gcc
OBJCOPY := $(TARGET)-objcopy
CFLAGS := -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostdlib -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -I deps/secp256k1/src -I deps/secp256k1 -I deps/ckb-c-std-lib -I deps/ckb-c-std-lib/libc -I deps/ckb-c-std-lib/molecule -I c -I build -Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function -g
LDFLAGS := -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections
SECP256K1_SRC_20210801 := deps/secp256k1-20210801/src/ecmult_static_pre_context.h
CFLAGS := -g -fPIC -O3 -fno-builtin \
-nostdinc -nostdlib -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections \
-I deps/secp256k1/src -I deps/secp256k1 -I deps/ckb-c-stdlib -I deps/ckb-c-stdlib/libc \
-I deps/ckb-c-stdlib/molecule -I c -I build -I deps/sparse-merkle-tree/c \
-Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function -Wno-array-bounds -Wno-stringop-overflow

# to enable log
# -DCKB_C_STDLIB_PRINTF -DCKB_C_STDLIB_PRINTF_BUFFER_SIZE=1024

LDFLAGS := -nostdlib -nostartfiles -Wl,-static -Wl,--gc-sections

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))
SECP256K1_SRC := deps/secp256k1/src/ecmult_static_pre_context.h

PROTOCOL_HEADER := c/blockchain.h
PROTOCOL_SCHEMA := c/blockchain.mol
PROTOCOL_VERSION := d75e4c56ffa40e17fd2fe477da3f98c5578edcd1
PROTOCOL_URL := https://raw.githubusercontent.com/nervosnetwork/ckb/${PROTOCOL_VERSION}/util/types/schemas/blockchain.mol
MOLC := moleculec
MOLC_VERSION := 0.7.0

# docker pull nervos/ckb-riscv-gnu-toolchain:gnu-bionic-20191012
BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:aae8a3f79705f67d505d1f1d5ddc694a4fd537ed1c7e9622420a470d59ba2ec3
CLANG_FORMAT_DOCKER := kason223/clang-format@sha256:3cce35b0400a7d420ec8504558a02bdfc12fd2d10e40206f140c4545059cd95d
# docker pull nervos/ckb-riscv-gnu-toolchain:gnu-jammy-20230214
BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:d3f649ef8079395eb25a21ceaeb15674f47eaa2d8cc23adc8bcdae3d5abce6ec
CLANG_FORMAT_DOCKER := xujiandong/ckb-riscv-llvm-toolchain@sha256:6409ab0d3e335c74088b54f4f73252f4b3367ae364d5c7ca7acee82135f5af4d

all: build/omni_lock build/always_success

all-via-docker: ${PROTOCOL_HEADER}
docker run --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code && make"

all-via-docker:
docker run -u $(shell id -u):$(shell id -g) --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code && make"

build/always_success: c/always_success.c
$(CC) $(OMNI_LOCK_CFLAGS) $(LDFLAGS) -o $@ $<
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<
$(OBJCOPY) --only-keep-debug $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

build/secp256k1_data_info_20210801.h: build/dump_secp256k1_data_20210801
build/secp256k1_data_info.h: build/dump_secp256k1_data
$<

build/dump_secp256k1_data_20210801: c/dump_secp256k1_data_20210801.c $(SECP256K1_SRC_20210801)
build/dump_secp256k1_data: c/dump_secp256k1_data.c $(SECP256K1_SRC)
mkdir -p build
gcc -I deps/secp256k1-20210801/src -I deps/secp256k1-20210801 -o $@ $<
gcc -I deps/secp256k1/src -I deps/secp256k1 -I deps/ckb-c-stdlib -o $@ $<


$(SECP256K1_SRC_20210801):
cd deps/secp256k1-20210801 && \
$(SECP256K1_SRC):
cd deps/secp256k1 && \
./autogen.sh && \
CC=$(CC) LD=$(LD) ./configure --with-bignum=no --enable-ecmult-static-precomputation --enable-endomorphism --enable-module-recovery --host=$(TARGET) && \
CC=$(CC) LD=$(LD) ./configure --enable-ecmult-static-precomputation --with-ecmult-window=6 --enable-module-recovery --host=$(TARGET) && \
make src/ecmult_static_pre_context.h src/ecmult_static_context.h

build/ed25519/%.o: deps/ed25519/src/%.c
mkdir -p build/ed25519
$(CC) -c -DCKB_DECLARATION_ONLY -I deps/ed25519/src $(CFLAGS) -o $@ $^

build/impl.o: deps/ckb-c-std-lib/libc/src/impl.c
$(CC) -c $(filter-out -DCKB_DECLARATION_ONLY, $(CFLAGS_MBEDTLS)) $(LDFLAGS_MBEDTLS) -o $@ $^
build/libed25519.a: build/ed25519/sign.o build/ed25519/verify.o build/ed25519/sha512.o build/ed25519/sc.o build/ed25519/keypair.o \
build/ed25519/key_exchange.o build/ed25519/ge.o build/ed25519/fe.o build/ed25519/add_scalar.o
$(AR) cr $@ $^

${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)
tests/omni_lock/omni_lock_sim.c tests/omni_lock/ckb_syscall_omni_lock_sim.h tests/omni_lock/omni_lock_supply.h\
c/cobuild.h c/molecule2_verify.h mol2_utils.h)

fmt:
docker run --rm -v `pwd`:/code ${CLANG_FORMAT_DOCKER} bash -c "cd code && clang-format -i -style=Google $(ALL_C_SOURCE)"
docker run -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)

mol:
make omni_lock_mol
make cobuild_mol

c/xudt_rce_mol.h: c/xudt_rce.mol
${MOLC} --language c --schema-file $< > $@
Expand All @@ -76,25 +81,34 @@ omni_lock_mol:
${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/omni_lock: c/omni_lock.c c/omni_lock_supply.h c/omni_lock_acp.h c/secp256k1_lock.h build/secp256k1_data_info_20210801.h $(SECP256K1_SRC_20210801) c/ckb_identity.h
$(CC) $(OMNI_LOCK_CFLAGS) $(LDFLAGS) -o $@ $<
build/omni_lock: c/omni_lock.c c/omni_lock_supply.h c/omni_lock_acp.h build/secp256k1_data_info.h $(SECP256K1_SRC) \
c/ckb_identity.h c/mol2_utils.h c/cobuild_basic_mol2.h c/molecule2_verify.h \
c/cobuild.h c/mol2_utils.h c/molecule2_verify.h build/libed25519.a
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< build/libed25519.a
cp $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@


clean:
rm -rf build/secp256k1_data_info_20210801.h build/dump_secp256k1_data_20210801
rm -rf build/secp256k1_data_20210801
cobuild_mol:
${MOLC} --language rust --schema-file c/basic.mol | rustfmt > tests/omni_lock_rust/src/schemas/basic.rs
${MOLC} --language rust --schema-file c/top_level.mol | rustfmt > tests/omni_lock_rust/src/schemas/top_level.rs
${MOLC} --language - --schema-file c/basic.mol --format json > build/cobuild_basic_mol2.json
moleculec-c2 --input build/cobuild_basic_mol2.json | clang-format -style=Google > c/cobuild_basic_mol2.h
${MOLC} --language - --schema-file c/top_level.mol --format json > build/cobuild_top_level_mol2.json
moleculec-c2 --input build/cobuild_top_level_mol2.json | clang-format -style=Google > c/cobuild_top_level_mol2.h

clean: clean2
rm -rf build/secp256k1_data_info.h build/dump_secp256k1_data
rm -f build/secp256k1_data
rm -rf build/ed25519 build/libed25519.a
cd deps/secp256k1 && [ -f "Makefile" ] && make clean

# not clean libraries, e.g. secp256k1
clean2:
rm -rf build/*.debug
rm -f build/omni_lock
cd deps/secp256k1-20210801 && [ -f "Makefile" ] && make clean

install-tools:
if [ ! -x "$$(command -v "${MOLC}")" ] \
|| [ "$$(${MOLC} --version | awk '{ print $$2 }' | tr -d ' ')" != "${MOLC_VERSION}" ]; then \
cargo install --force --version "${MOLC_VERSION}" "${MOLC}"; \
fi

rm -f build/*.o
rm -f build/always_success

dist: clean all

.PHONY: all all-via-docker dist clean package-clean package publish
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ cd tests/omni_lock_rust && cargo test
## Deployment

See [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0042-omnilock/0042-omnilock.md)

81 changes: 81 additions & 0 deletions c/basic.mol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import blockchain;

array Hash [byte; 32];
vector String <byte>; // UTF-8 encoded
option Uint32Opt (Uint32);

table Action {
script_info_hash: Byte32, // script info
script_hash: Byte32, // script
data: Bytes, // action data
}

vector ActionVec <Action>;

table Message {
actions: ActionVec,
}

table ScriptInfo {
// The dapp name and domain the script belongs to
name: String,
url: String,

// Script info.
// schema: script action schema
// message_type: the entry action type used in WitnessLayout
script_hash: Byte32,
schema: String,
message_type: String,
}

vector ScriptInfoVec <ScriptInfo>;

table ResolvedInputs {
outputs: CellOutputVec,
outputs_data: BytesVec,
}

table BuildingPacketV1 {
message: Message,
payload: Transaction,
resolved_inputs: ResolvedInputs,
change_output: Uint32Opt,
script_infos: ScriptInfoVec,
lock_actions: ActionVec,
}

union BuildingPacket {
BuildingPacketV1,
}

table SighashAll {
message: Message,
seal: Bytes,
}

table SighashAllOnly {
seal: Bytes,
}

table SealPair {
script_hash: Byte32,
seal: Bytes,
}
vector SealPairVec <SealPair>;

table OtxStart {
start_input_cell: Uint32,
start_output_cell: Uint32,
start_cell_deps: Uint32,
start_header_deps: Uint32,
}

table Otx {
input_cells: Uint32,
output_cells: Uint32,
cell_deps: Uint32,
header_deps: Uint32,
message: Message,
seals: SealPairVec,
}
Loading