From afe07b29ee48e803476c5a6ae53c2ff8c932710d Mon Sep 17 00:00:00 2001 From: Marcelo Politzer <251334+mpolitzer@users.noreply.github.com> Date: Tue, 6 Jan 2026 16:29:33 -0300 Subject: [PATCH] feat: bump emulator to v0.20.0 --- Dockerfile | 18 ++-- .../root/app/util/machine.go | 29 ++++-- .../root/deploy/application.go | 32 +++++-- control.template | 2 +- pkg/emulator/emulator.go | 2 +- pkg/emulator/machine.go | 91 +++++++++++++------ pkg/emulator/types.go | 12 ++- pkg/machine/libcartesi.go | 68 ++++++++++++++ pkg/machine/libcartesi_test.go | 13 ++- test/dependencies | 2 +- test/dependencies.sha256 | 2 +- 11 files changed, 211 insertions(+), 60 deletions(-) diff --git a/Dockerfile b/Dockerfile index 75f413847..e8d954354 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -22,13 +22,13 @@ RUN < 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 diff --git a/pkg/emulator/emulator.go b/pkg/emulator/emulator.go index 82893b3dc..9845edfe1 100644 --- a/pkg/emulator/emulator.go +++ b/pkg/emulator/emulator.go @@ -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 } diff --git a/pkg/emulator/machine.go b/pkg/emulator/machine.go index 7fb624d43..08bbb4d71 100644 --- a/pkg/emulator/machine.go +++ b/pkg/emulator/machine.go @@ -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 @@ -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)) @@ -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 } @@ -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 @@ -147,7 +139,7 @@ func (m *Machine) GetProof(address uint64, log2size int32) (string, 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 } @@ -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 @@ -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 @@ -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 diff --git a/pkg/emulator/types.go b/pkg/emulator/types.go index f57fe9d1f..31623cd89 100644 --- a/pkg/emulator/types.go +++ b/pkg/emulator/types.go @@ -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 { @@ -335,3 +335,7 @@ func NewMachineRuntimeConfig() *MachineRuntimeConfig { SoftYield: false, } } + +const ( + HashTreeLog2RootSize uint32 = C.CM_HASH_TREE_LOG2_ROOT_SIZE +) diff --git a/pkg/machine/libcartesi.go b/pkg/machine/libcartesi.go index 7d396f1de..56ee4f10f 100644 --- a/pkg/machine/libcartesi.go +++ b/pkg/machine/libcartesi.go @@ -11,6 +11,7 @@ import ( "time" "github.com/cartesi/rollups-node/pkg/emulator" + "github.com/ethereum/go-ethereum/common" ) // RemoteMachineInterface defines the interface that LibCartesiBackend needs from a remote machine @@ -18,6 +19,7 @@ type RemoteMachineInterface interface { SetTimeout(timeoutMs int64) error Load(dir string, runtimeConfig string) error Run(mcycleEnd uint64) (emulator.BreakReason, error) + CollectMCycleRootHashes(mcycleEnd, mcyclePeriod, mcyclePhase uint64, log2BundleMcycleCount int32, previousBackTree string) ([]byte, error) GetRootHash() (emulator.Hash, error) GetProof(address uint64, log2size int32) (string, error) ReadReg(reg emulator.RegID) (uint64, error) @@ -39,6 +41,16 @@ type proofJson struct { TargetHash Hash `json:"target_hash"` } +// Struct for the decoded `result` field of cm_collect_mcycle_root_hashes (originally returned as json). +// The value comes back as a json string, that needs to be decoded to this struct below. +// BackTree may or may not be present, check cm_collect_mcycle_root_hashes documentation for details. +type CollectMCycleRootHashesState struct { + RootHashes [][]byte `json:"hashes"` + MCyclePhase uint64 `json:"mcycle_phase"` + BreakReason string `json:"break_reason"` + BackTree json.RawMessage `json:"back_tree"` +} + func decodeB64To32(dst *Hash, s string) error { // accepts Std (with '=') and Raw (without '=') n, err := base64.StdEncoding.Decode(dst[:], []byte(s)) @@ -234,6 +246,62 @@ func (e *LibCartesiBackend) RunAndCollectRootHashes( mcycleEnd uint64, state *HashCollectorState, timeout time.Duration, +) (reason BreakReason, err error) { + return e.RunAndCollectRootHashesNew(mcycleEnd, state, timeout) +} + +func (e *LibCartesiBackend) RunAndCollectRootHashesNew( + mcycleEnd uint64, + state *HashCollectorState, + timeout time.Duration, +) (reason BreakReason, err error) { + if err := e.inner.SetTimeout(timeout.Milliseconds()); err != nil { + return 0, fmt.Errorf("failed to set operation timeout: %w", err) + } + result, err := e.inner.CollectMCycleRootHashes(mcycleEnd, state.Period, state.Phase, state.BundleLog2, "") + if err != nil { + return Failed, err + } + + var hcs CollectMCycleRootHashesState + err = json.Unmarshal(result, &hcs) + if err != nil { + fmt.Println(string(result)) + return Failed, err + } + + var br BreakReason + switch hcs.BreakReason { + case "failed": + br = Failed + case "halted": + br = Halted + case "reached_target_mcycle": + br = ReachedTargetMcycle + case "yielded_automatically": + br = YieldedAutomatically + case "yielded_softly": + br = YieldedSoftly + case "yielded_manually": + br = YieldedManually + default: + return Failed, fmt.Errorf("unimplemented break reason on RunAndCollectRootHashesNew") + } + + state.Hashes = nil + for _, hash := range hcs.RootHashes { + state.Hashes = append(state.Hashes, common.BytesToHash(hash)) + } + state.Phase = hcs.MCyclePhase + state.BackTree = hcs.BackTree + // state.MaxHashes = ? + return br, nil +} + +func (e *LibCartesiBackend) RunAndCollectRootHashesOld( + mcycleEnd uint64, + state *HashCollectorState, + timeout time.Duration, ) (reason BreakReason, err error) { if state == nil { return Failed, errors.New("nil state") diff --git a/pkg/machine/libcartesi_test.go b/pkg/machine/libcartesi_test.go index 4f78d08b1..854ba8759 100644 --- a/pkg/machine/libcartesi_test.go +++ b/pkg/machine/libcartesi_test.go @@ -33,7 +33,7 @@ func (s *LibCartesiSuite) TestLoad() { // Test successful load s.mockRemoteMachine.On("SetTimeout", int64(5000)).Return(nil) - s.mockRemoteMachine.On("Load", "/test/dir", "config").Return(nil) + s.mockRemoteMachine.On("Load", "/test/dir", "config", mock.Anything).Return(nil) err := s.backend.Load("/test/dir", "config", 5*time.Second) require.NoError(err) @@ -53,7 +53,7 @@ func (s *LibCartesiSuite) TestLoad() { s.mockRemoteMachine = new(MockRemoteMachine) s.backend = &LibCartesiBackend{inner: s.mockRemoteMachine} s.mockRemoteMachine.On("SetTimeout", int64(5000)).Return(nil) - s.mockRemoteMachine.On("Load", "/test/dir", "config").Return(errors.New("load error")) + s.mockRemoteMachine.On("Load", "/test/dir", "config", mock.Anything).Return(errors.New("load error")) err = s.backend.Load("/test/dir", "config", 5*time.Second) require.Error(err) @@ -288,7 +288,7 @@ func (s *LibCartesiSuite) TestStore() { // Test successful store s.mockRemoteMachine.On("SetTimeout", int64(5000)).Return(nil) - s.mockRemoteMachine.On("Store", "/test/dir").Return(nil) + s.mockRemoteMachine.On("Store", "/test/dir", mock.Anything).Return(nil) err := s.backend.Store("/test/dir", 5*time.Second) require.NoError(err) @@ -308,7 +308,7 @@ func (s *LibCartesiSuite) TestStore() { s.mockRemoteMachine = new(MockRemoteMachine) s.backend = &LibCartesiBackend{inner: s.mockRemoteMachine} s.mockRemoteMachine.On("SetTimeout", int64(5000)).Return(nil) - s.mockRemoteMachine.On("Store", "/test/dir").Return(errors.New("store error")) + s.mockRemoteMachine.On("Store", "/test/dir", mock.Anything).Return(errors.New("store error")) err = s.backend.Store("/test/dir", 5*time.Second) require.Error(err) @@ -434,6 +434,11 @@ func (m *MockRemoteMachine) Run(mcycleEnd uint64) (emulator.BreakReason, error) return args.Get(0).(emulator.BreakReason), args.Error(1) } +func (m *MockRemoteMachine) CollectMCycleRootHashes(mcycleEnd, mcyclePeriod, mcyclePhase uint64, log2BundleMcycleCount int32, previousBackTree string) ([]byte, error) { + args := m.Called() + return args.Get(0).([]byte), args.Error(1) +} + func (m *MockRemoteMachine) GetRootHash() (emulator.Hash, error) { args := m.Called() return args.Get(0).(Hash), args.Error(1) diff --git a/test/dependencies b/test/dependencies index 557d9b45d..092280d4b 100644 --- a/test/dependencies +++ b/test/dependencies @@ -1,2 +1,2 @@ https://github.com/cartesi/image-kernel/releases/download/v0.20.0/linux-6.5.13-ctsi-1-v0.20.0.bin -https://github.com/cartesi/machine-guest-tools/releases/download/v0.17.0/rootfs-tools.ext2 +https://github.com/cartesi/machine-guest-tools/releases/download/v0.17.2/rootfs-tools.ext2 diff --git a/test/dependencies.sha256 b/test/dependencies.sha256 index 5674cb2ce..92e988b1e 100644 --- a/test/dependencies.sha256 +++ b/test/dependencies.sha256 @@ -1,2 +1,2 @@ 65dd100ff6204346ac2f50f772721358b5c1451450ceb39a154542ee27b4c947 test/downloads/linux-6.5.13-ctsi-1-v0.20.0.bin -8eb9d03b2653fc6090caf4ae3fb49b44fe1ccd57d9903dd696c0a3024ea1a031 test/downloads/rootfs-tools.ext2 +675a49e3c9bada29f25d5b559707b34553b94280c03f44ccb8203c2cf453b541 test/downloads/rootfs-tools.ext2