Skip to content
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
18 changes: 9 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

# syntax=docker.io/docker/dockerfile:1

ARG EMULATOR_VERSION=0.19.0
ARG EMULATOR_VERSION=0.20.0

# Build directories.
ARG GO_BUILD_PATH=/build/cartesi/go

FROM debian:bookworm-20250407 AS common-env
FROM debian:trixie-20250811 AS common-env

USER root

Expand All @@ -22,13 +22,13 @@ RUN <<EOF
apt-get update
apt-get install -y --no-install-recommends \
ca-certificates curl wget build-essential pkg-config libssl-dev
addgroup --system --gid 102 cartesi
adduser --system --uid 102 --ingroup cartesi --disabled-login --no-create-home --home /nonexistent --gecos "cartesi user" --shell /bin/false cartesi
groupadd --system --gid 102 cartesi
useradd --system --uid 102 --gid cartesi --shell /usr/sbin/nologin --no-create-home cartesi
ARCH=$(dpkg --print-architecture)
wget -O /tmp/cartesi-machine-emulator.deb "https://github.com/cartesi/machine-emulator/releases/download/v${EMULATOR_VERSION}/machine-emulator_${ARCH}.deb"
case "$ARCH" in
amd64) echo "adae6b030a8990e316997aad53d175192bfeaa84ad12ee19491366377073572b /tmp/cartesi-machine-emulator.deb" | sha256sum --check ;;
arm64) echo "15ebb64d8cd3296564d2297dd809d1d72c13a938976bb4ecc5e5c82e71bb8069 /tmp/cartesi-machine-emulator.deb" | sha256sum --check ;;
amd64) echo "46b2f37b889091df3b89a8909467935f8dd4a1426eeb0491b6a346a12f0c341c /tmp/cartesi-machine-emulator.deb" | sha256sum --check ;;
arm64) echo "27ea10571335ad174b75388e7de54a3d3434bd607554d8c0bdf6abca47ceae0d /tmp/cartesi-machine-emulator.deb" | sha256sum --check ;;
*) echo "unsupported architecture: $ARCH"; exit 1 ;;
esac
apt-get install -y --no-install-recommends /tmp/cartesi-machine-emulator.deb
Expand Down Expand Up @@ -146,7 +146,7 @@ RUN make build-debian-package DESTDIR=$PWD/_install
# (This stage copies the binaries from previous stages.)
# =============================================================================

FROM debian:bookworm-20250407 AS rollups-node
FROM debian:trixie-20250811 AS rollups-node

ARG NODE_RUNTIME_DIR=/var/lib/cartesi-rollups-node
ARG GO_BUILD_PATH
Expand All @@ -164,8 +164,8 @@ COPY --from=debian-packager \
ARG DEBIAN_FRONTEND=noninteractive
RUN <<EOF
set -e
addgroup --system --gid 102 cartesi
adduser --system --uid 102 --ingroup cartesi --disabled-login --no-create-home --home /nonexistent --gecos "cartesi user" --shell /bin/false cartesi
groupadd --system --gid 102 cartesi
useradd --system --uid 102 --gid cartesi --shell /usr/sbin/nologin --no-create-home cartesi
apt-get update
apt-get install -y --no-install-recommends \
ca-certificates \
Expand Down
29 changes: 23 additions & 6 deletions cmd/cartesi-rollups-cli/root/app/util/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package util

import (
"fmt"
"io"
"os"
"path"

Expand All @@ -14,16 +15,32 @@ import (
// Reads the Cartesi Machine hash from machineDir. Returns it as a hex string or
// an error
func ReadHash(machineDir string) (string, error) {
path := path.Join(machineDir, "hash")
hash, err := os.ReadFile(path)
path := path.Join(machineDir, "hash_tree.sht")
f, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("read hash: %w", err)
} else if len(hash) != common.HashLength {
return "", err
}
defer f.Close()

// root hash is located at this offset (0x60). Double check its value
// with the cartesi-machine-stored-hash tool.
_, err = f.Seek(0x60, io.SeekStart)
if err != nil {
return "", err
}

// read only 0x20 bytes from it, there are more hash values after it
rawHash := make([]byte, 0x20)
n, err := f.Read(rawHash)
if err != nil {
return "", err
}
if n != common.HashLength {
return "", fmt.Errorf(
"read hash: wrong size; expected %v bytes but read %v",
common.HashLength,
len(hash),
n,
)
}
return common.Bytes2Hex(hash), nil
return common.Bytes2Hex(rawHash), nil
}
32 changes: 25 additions & 7 deletions cmd/cartesi-rollups-cli/root/deploy/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path"
"strings"
Expand Down Expand Up @@ -535,21 +536,38 @@ func buildPrtApplicationDeployment(
return request, nil
}

// read the hash value from the cartesi machine hash file
// Reads the Cartesi Machine hash from machineDir. Returns it as a hex string or
// an error
func readHash(machineDir string) (common.Hash, error) {
zero := common.Hash{}
path := path.Join(machineDir, "hash")
hash, err := os.ReadFile(path)
path := path.Join(machineDir, "hash_tree.sht")
f, err := os.Open(path)
if err != nil {
return zero, fmt.Errorf("read hash: %w", err)
} else if len(hash) != common.HashLength {
return zero, err
}
defer f.Close()

// root hash is located at this offset (0x60). Double check its value
// with the cartesi-machine-stored-hash tool.
_, err = f.Seek(0x60, io.SeekStart)
if err != nil {
return zero, err
}

// read only 0x20 bytes from it, there are more hash values after it
rawHash := make([]byte, 0x20)
n, err := f.Read(rawHash)
if err != nil {
return zero, err
}
if n != common.HashLength {
return zero, fmt.Errorf(
"read hash: wrong size; expected %v bytes but read %v",
common.HashLength,
len(hash),
n,
)
}
return common.BytesToHash(hash), nil
return common.BytesToHash(rawHash), nil
}

func parseHexHash(hash string) (common.Hash, error) {
Expand Down
2 changes: 1 addition & 1 deletion control.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Homepage: https://docs.cartesi.io/cartesi-rollups/
Architecture: ARG_ARCH
Maintainer: Node Reference Unit <https://discord.com/channels/600597137524391947/1110564973115097179>
Provides: cartesi-rollups-node
Depends: cartesi-machine-emulator (>= 0.19.0), cartesi-machine-emulator (<< 0.20.0)
Depends: cartesi-machine-emulator (>= 0.20.0), cartesi-machine-emulator (<< 0.21.0)
Section: net
Priority: optional
Multi-Arch: no
Expand Down
2 changes: 1 addition & 1 deletion pkg/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ func SpawnServer(address string, timeout time.Duration) (*RemoteMachine, string,

func CreateMachine(config, runtimeConfig string) (*Machine, error) {
machine := &Machine{}
err := machine.Create(config, runtimeConfig)
err := machine.Create(config, runtimeConfig, "")
return machine, err
}
91 changes: 65 additions & 26 deletions pkg/emulator/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ import (

const HashSize = C.sizeof_cm_hash

type SharingMode = C.cm_sharing_mode

const (
SharingNone SharingMode = iota
SharingConfig
SharingAll
)

// Common type aliases
type Hash = [HashSize]byte

Expand Down Expand Up @@ -58,11 +66,12 @@ func (m *Machine) Delete() {
}

// create
func (m *Machine) Create(config, runtimeConfig string) error {
func (m *Machine) Create(config, runtimeConfig, dir string) error {
var err error
m.callCAPI(func() {
var cConfig *C.char
var cRuntime *C.char
var cDir *C.char
if config != "" {
cConfig = C.CString(config)
defer C.free(unsafe.Pointer(cConfig))
Expand All @@ -71,7 +80,11 @@ func (m *Machine) Create(config, runtimeConfig string) error {
cRuntime = C.CString(runtimeConfig)
defer C.free(unsafe.Pointer(cRuntime))
}
err = newError(C.cm_create_new(cConfig, cRuntime, &m.ptr))
if dir != "" {
cDir = C.CString(dir)
defer C.free(unsafe.Pointer(cDir))
}
err = newError(C.cm_create_new(cConfig, cRuntime, cDir, &m.ptr))
})
return err
}
Expand Down Expand Up @@ -119,35 +132,14 @@ func (m *Machine) GetInitialConfig() (string, error) {
return res, nil
}

// get_memory_ranges
func (m *Machine) GetMemoryRanges() (string, error) {
var ranges *C.char
var err error
var res string

m.callCAPI(func() {
err = newError(C.cm_get_memory_ranges(m.ptr, &ranges))
if err != nil || ranges == nil {
return
}
res = C.GoString(ranges)
// no need to free 'ranges' here, as it is a static string
})

if err != nil {
return "", err
}
return res, nil
}

// get_proof
func (m *Machine) GetProof(address uint64, log2size int32) (string, error) {
var proof *C.char
var err error
var res string

m.callCAPI(func() {
err = newError(C.cm_get_proof(m.ptr, C.uint64_t(address), C.int32_t(log2size), &proof))
err = newError(C.cm_get_proof(m.ptr, C.uint64_t(address), C.int32_t(log2size), C.int32_t(HashTreeLog2RootSize), &proof))
if err != nil || proof == nil {
return
}
Expand Down Expand Up @@ -241,7 +233,7 @@ func (m *Machine) Load(dir string, runtimeConfig string) error {
cRuntime = C.CString(runtimeConfig)
defer C.free(unsafe.Pointer(cRuntime))
}
err = newError(C.cm_load(m.ptr, cDir, cRuntime))
err = newError(C.cm_load(m.ptr, cDir, cRuntime, SharingNone))
})

return err
Expand Down Expand Up @@ -369,6 +361,53 @@ func (m *Machine) Run(mcycleEnd uint64) (BreakReason, error) {
return BreakReason(br), nil
}

// collect_mcycle_root_hashes
func (m *Machine) CollectMCycleRootHashes(mcycleEnd, mcyclePeriod, mcyclePhase uint64, log2BundleMcycleCount int32, previousBackTree string) ([]byte, error) {
var err error
var result *C.char

m.callCAPI(func() {
var previousBackTreeC *C.char
if previousBackTree != "" {
previousBackTreeC = C.CString(previousBackTree)
defer C.free(unsafe.Pointer(previousBackTreeC))
}
err = newError(C.cm_collect_mcycle_root_hashes(
m.ptr,
C.uint64_t(mcycleEnd),
C.uint64_t(mcyclePeriod),
C.uint64_t(mcyclePhase),
C.int32_t(log2BundleMcycleCount),
previousBackTreeC,
&result))
})

if err != nil {
return []byte{}, err
}
return []byte(C.GoString(result)), nil
}

// collect_uarch_cycle_root_hashes
func (m *Machine) CollectUarchCycleRootHashes(mcycleEnd uint64, log2BundleMcycleCount int32) ([]byte, error) {
var err error
var result *C.char

m.callCAPI(func() {
err = newError(C.cm_collect_uarch_cycle_root_hashes(
m.ptr,
C.uint64_t(mcycleEnd),
C.int32_t(log2BundleMcycleCount),
&result))
})

if err != nil {
return []byte{}, err
}
return []byte(C.GoString(result)), nil
}


// send_cmio_response
func (m *Machine) SendCmioResponse(reason uint16, data []byte) error {
var err error
Expand Down Expand Up @@ -414,7 +453,7 @@ func (m *Machine) Store(directory string) error {
m.callCAPI(func() {
cDir := C.CString(directory)
defer C.free(unsafe.Pointer(cDir))
err = newError(C.cm_store(m.ptr, cDir))
err = newError(C.cm_store(m.ptr, cDir, SharingAll))
})

return err
Expand Down
12 changes: 8 additions & 4 deletions pkg/emulator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,11 @@ const (
)

const (
CmioRxBufferStart uint64 = C.CM_PMA_CMIO_RX_BUFFER_START
CmioRxBufferLog2Size uint64 = C.CM_PMA_CMIO_RX_BUFFER_LOG2_SIZE
CmioRxBufferStart uint64 = C.CM_AR_CMIO_RX_BUFFER_START
CmioRxBufferLog2Size uint64 = C.CM_AR_CMIO_RX_BUFFER_LOG2_SIZE

CmioTxBufferStart uint64 = C.CM_PMA_CMIO_TX_BUFFER_START
CmioTxBufferLog2Size uint64 = C.CM_PMA_CMIO_TX_BUFFER_LOG2_SIZE
CmioTxBufferStart uint64 = C.CM_AR_CMIO_TX_BUFFER_START
CmioTxBufferLog2Size uint64 = C.CM_AR_CMIO_TX_BUFFER_LOG2_SIZE
)

type MachineRuntimeConfig struct {
Expand Down Expand Up @@ -335,3 +335,7 @@ func NewMachineRuntimeConfig() *MachineRuntimeConfig {
SoftYield: false,
}
}

const (
HashTreeLog2RootSize uint32 = C.CM_HASH_TREE_LOG2_ROOT_SIZE
)
Loading
Loading