Skip to content
Draft
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
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 @@ -522,21 +523,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 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
}
93 changes: 66 additions & 27 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,27 +132,6 @@ 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
Expand Down Expand Up @@ -229,7 +221,7 @@ func (m *Machine) IsEmpty() (bool, error) {
}

// load
func (m *Machine) Load(dir string, runtimeConfig string) error {
func (m *Machine) Load(dir string, runtimeConfig string, sharingMode SharingMode) error {
var err error

m.callCAPI(func() {
Expand All @@ -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, sharingMode))
})

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 @@ -408,13 +447,13 @@ func (m *Machine) SetRuntimeConfig(runtimeJSON string) error {
}

// store
func (m *Machine) Store(directory string) error {
func (m *Machine) Store(directory string, sharingMode SharingMode) error {
var err 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, sharingMode))
})

return err
Expand Down
8 changes: 4 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
Loading
Loading