Skip to content

Commit

Permalink
add compiler for c contract
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Aug 31, 2024
1 parent 6e133ca commit b1dd442
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
20 changes: 15 additions & 5 deletions pkg/ckb/syscalls.cell
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package ckb

import (
"blockchain"
// "debug"
)

const (
SIZE_H256 = 32
SIZE_TX = 65536
SIZE_SCRIPT = 65536
)

func loadTxhash() blockchain.Byte32 {
func loadTxHash() blockchain.Byte32 {
data := make([]byte, SIZE_H256, SIZE_H256)
ptr := __slice_get_ptr(data)
size := 0
err := ckb_load_tx_hash(ptr, &size, 0)
if err != CKB_SUCCESS {
return blockchain.Byte32Default()
}
return blockchain.Byte32FromSliceUnchecked()
return blockchain.Byte32FromSliceUnchecked(data)
}
func loadTx() []byte {
data := make([]byte, SIZE_TX, SIZE_TX)
Expand All @@ -42,10 +44,18 @@ func loadScriptHash() blockchain.Byte32 {
if err != CKB_SUCCESS {
return blockchain.Byte32Default()
}
return blockchain.Byte32FromSliceUnchecked()
return blockchain.Byte32FromSliceUnchecked(data)
}
func loadScript() blockchain.Script {
data := make([]byte, SIZE_SCRIPT, SIZE_SCRIPT)
ptr := __slice_get_ptr(data)
size := 0
err := ckb_load_script(ptr, &size, 0)
if err != CKB_SUCCESS {
return blockchain.ScriptDefault()
}
return blockchain.ScriptFromSliceUnchecked(data)
}
// func load_script(addr uintptr, len uint64, offset uint) int
// func loadScript()
// func load_cell(addr uintptr, len uint64, offset uint, index uint, source uint) int
// func load_input(addr uintptr, len uint64, offset uint, index uint, source uint) int
// func load_header(addr uintptr, len uint64, offset uint, index uint, source uint) int
Expand Down
13 changes: 13 additions & 0 deletions scripts/cc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmd="clang -o a.out --target=riscv64 \
-march=rv64imc \
-nostdlib \
-Wall -Werror -Wextra -Wno-unused-parameter -Wno-nonnull -fno-builtin-printf -fno-builtin-memcmp -O3 -fdata-sections -ffunction-sections \
-I third-party/ckb-c-stdlib/libc \
-I third-party/ckb-c-stdlib/molecule \
-I third-party/ckb-c-stdlib \
-I third-party/sparse-merkle-tree/c \
-DCKB_C_STDLIB_PRINTF=1 \
-DCKB_PRINTF_DECLARATION_ONLY=1 \
$1"
echo $cmd
$cmd
4 changes: 2 additions & 2 deletions scripts/ckb-debug-tx.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cmd="ckb-debugger --tx-file dump.json \
--cell-index $1 \
--cell-type input \
--script-group-type lock \
--bin hi"
--script-group-type type \
--bin a.out"
echo $cmd
$cmd

0 comments on commit b1dd442

Please sign in to comment.