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
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

ARG EMULATOR_VERSION=0.20.0
ARG EMULATOR_VERSION=0.21.0-test6

# Build directories.
ARG GO_BUILD_PATH=/build/cartesi/go
Expand All @@ -29,8 +29,8 @@ RUN <<EOF
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 "46b2f37b889091df3b89a8909467935f8dd4a1426eeb0491b6a346a12f0c341c /tmp/cartesi-machine-emulator.deb" | sha256sum --check ;;
arm64) echo "27ea10571335ad174b75388e7de54a3d3434bd607554d8c0bdf6abca47ceae0d /tmp/cartesi-machine-emulator.deb" | sha256sum --check ;;
amd64) echo "df61b4ce4cfef1783dc378ad7647a83a337ac83ba2c78988b0717449d8b9f941 /tmp/cartesi-machine-emulator.deb" | sha256sum --check ;;
arm64) echo "29cea4a43416c4c173c3db7b7892878c5968928c753779e9f0aebdc7524f9759 /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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,13 @@ withdraw-wallet: cartesi-rollups-cli ## Send a test withdrawal request from the

# Temporary test dependencies target while we are not using distribution packages
DOWNLOADS_DIR = test/downloads
CARTESI_TEST_MACHINE_IMAGES = $(DOWNLOADS_DIR)/linux.bin
CARTESI_TEST_MACHINE_IMAGES = $(DOWNLOADS_DIR)/linux.bin $(DOWNLOADS_DIR)/rootfs.ext2
$(CARTESI_TEST_MACHINE_IMAGES):
@mkdir -p $(DOWNLOADS_DIR)
@wget -nc -i test/dependencies -P $(DOWNLOADS_DIR)
@shasum -ca 256 test/dependencies.sha256
@cd $(DOWNLOADS_DIR) && ln -s rootfs-tools.ext2 rootfs.ext2
@cd $(DOWNLOADS_DIR) && ln -s linux-6.5.13-ctsi-1-v0.20.0.bin linux.bin
@cd $(DOWNLOADS_DIR) && ln -s linux-6.5.13-ctsi-2-uio-test1-v0.21.0.bin linux.bin

download-test-dependencies: | $(CARTESI_TEST_MACHINE_IMAGES)

Expand Down
30 changes: 28 additions & 2 deletions cmd/cartesi-rollups-machine-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -165,7 +166,7 @@ func replayInputsBatch(ctx context.Context, opts replayOptions, tmp string, inpu

args := []string{
"--quiet",
"--no-rollback",
"--no-revert",
"--load=" + opts.Template,
fmt.Sprintf("--cmio-advance-state=input:%s,input_index_begin:0,input_index_end:%d",
filepath.Join(tmp, "input-%i.bin"), len(inputs)),
Expand Down Expand Up @@ -374,7 +375,7 @@ func generateMachineProof(

args := []string{
"--quiet",
"--no-rollback",
"--no-revert",
"--load=" + snapshot,
fmt.Sprintf("--initial-proof=address:0x%x,log2_size:%d,filename:%s", address, log2Size, tmpPath),
"--",
Expand All @@ -392,6 +393,23 @@ func generateMachineProof(
if err := json.Unmarshal(raw, &proof); err != nil {
return nil, fmt.Errorf("parse machine proof: %w", err)
}

// convert the file hashes from base64 to hex
proof.RootHash, err = base64ToHex(proof.RootHash)
if err != nil {
return nil, err
}
proof.TargetHash, err = base64ToHex(proof.TargetHash)
if err != nil {
return nil, err
}
for i := range proof.SiblingHashes {
proof.SiblingHashes[i], err = base64ToHex(proof.SiblingHashes[i])
if err != nil {
return nil, err
}
}

return &proof, nil
}

Expand Down Expand Up @@ -478,3 +496,11 @@ func ensure0x(s string) string {
func strip0x(s string) string {
return strings.TrimPrefix(strings.TrimPrefix(s, "0x"), "0X")
}

func base64ToHex(s string) (string, error) {
raw, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return "", fmt.Errorf("expected %s to be a hash in base64. %w", s, err)
}
return common.Hash(raw).Hex(), nil
}
Comment on lines +500 to +506
11 changes: 4 additions & 7 deletions cmd/cartesi-rollups-machine-tool/replay_dave.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

local cartesi = require("cartesi")

local checkpoint_address = 0xfe0

local function usage(message)
if message then io.stderr:write(message, "\n") end
io.stderr:write(
Expand Down Expand Up @@ -75,16 +73,15 @@ end

local function advance_one(machine, input_path, input_number)
local checkpoint = machine:get_root_hash()
machine:write_memory(checkpoint_address, checkpoint)
machine:send_cmio_response(cartesi.CMIO_YIELD_REASON_ADVANCE_STATE, read_all(input_path))
machine:send_cmio_response(checkpoint, cartesi.HTIF_YIELD_REASON_ADVANCE_STATE, read_all(input_path))
run_until_manual_yield(machine)

local _, reason, data = machine:receive_cmio_request()
if reason == cartesi.CMIO_YIELD_MANUAL_REASON_RX_REJECTED then
if reason == cartesi.HTIF_YIELD_MANUAL_REASON_RX_REJECTED then
error(string.format("Dave replay input %d was rejected", input_number))
elseif reason == cartesi.CMIO_YIELD_MANUAL_REASON_TX_EXCEPTION then
elseif reason == cartesi.HTIF_YIELD_MANUAL_REASON_TX_EXCEPTION then
error(string.format("Dave replay input %d raised an exception", input_number))
elseif reason ~= cartesi.CMIO_YIELD_MANUAL_REASON_RX_ACCEPTED then
elseif reason ~= cartesi.HTIF_YIELD_MANUAL_REASON_RX_ACCEPTED then
error(string.format("Dave replay input %d ended with unexpected yield reason %s", input_number, tostring(reason)))
end
if #data ~= 32 then
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.20.0), cartesi-machine-emulator (<< 0.21.0)
Depends: cartesi-machine-emulator (>= 0.21.0), cartesi-machine-emulator (<< 0.22.0)
Section: net
Priority: optional
Multi-Arch: no
Expand Down
12 changes: 2 additions & 10 deletions internal/manager/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,8 @@ func (m *MachineInstanceImpl) Advance(ctx context.Context, input []byte, epochIn
advanceCtx, cancel := context.WithTimeout(ctx, m.advanceTimeout)
defer cancel()

if computeHashes {
// write the checkpoint hash before processing
err = fork.WriteCheckpointHash(advanceCtx, prevMachineHash)
if err != nil {
return nil, errors.Join(err, fork.Close())
}
}

// Process the input
advanceResp, err := fork.Advance(advanceCtx, input, computeHashes)
advanceResp, err := fork.Advance(advanceCtx, input, prevMachineHash, computeHashes)
status, err := toInputStatus(advanceResp.Accepted, err)
if err != nil {
return nil, errors.Join(err, fork.Close())
Expand Down Expand Up @@ -409,7 +401,7 @@ func (m *MachineInstanceImpl) Inspect(ctx context.Context, query []byte) (*Inspe
defer cancel()

// Process the query
accepted, reports, inspectErr := fork.Inspect(inspectCtx, query)
accepted, reports, inspectErr := fork.Inspect(inspectCtx, query, machine.Hash{})

// Create the result
result := &InspectResult{
Expand Down
27 changes: 2 additions & 25 deletions internal/manager/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,6 @@ func (s *MachineInstanceSuite) TestAdvance() {
require := s.Require()
inner, fork, machine := s.setupAdvance()

// Set up WriteCheckpointHash to succeed
fork.CheckpointHashError = nil

res, err := machine.Advance(context.Background(), []byte{}, 0, 5, true)
require.Nil(err)
require.NotNil(res)
Expand All @@ -523,20 +520,6 @@ func (s *MachineInstanceSuite) TestAdvance() {
_ = inner
})

s.Run("CollectHashesWriteCheckpointError", func() {
require := s.Require()
_, fork, machine := s.setupAdvance()

errCheckpoint := errors.New("checkpoint write error")
fork.CheckpointHashError = errCheckpoint

res, err := machine.Advance(context.Background(), []byte{}, 0, 5, true)
require.Error(err)
require.Nil(res)
require.ErrorIs(err, errCheckpoint)
require.Equal(uint64(5), machine.processedInputs.Load())
})

s.Run("SequentialAdvances", func() {
// Advance is serialized by advanceMutex — concurrent advance on the
// same machine never happens by design. This test verifies that two
Expand Down Expand Up @@ -1475,8 +1458,6 @@ type MockRollupsMachine struct {
HashReturn machine.Hash
HashError error

CheckpointHashError error

AdvanceAcceptedReturn bool
AdvanceOutputsReturn []machine.Output
AdvanceReportsReturn []machine.Report
Expand Down Expand Up @@ -1516,11 +1497,7 @@ func (m *MockRollupsMachine) OutputsHashProof(_ context.Context) ([]machine.Hash
return m.OutputsHashProofReturn, m.OutputsHashProofError
}

func (m *MockRollupsMachine) WriteCheckpointHash(_ context.Context, _ machine.Hash) error {
return m.CheckpointHashError
}

func (m *MockRollupsMachine) Advance(_ context.Context, _ []byte, _ bool) (*machine.AdvanceResponse, error) {
func (m *MockRollupsMachine) Advance(_ context.Context, _ []byte, _ machine.Hash, _ bool) (*machine.AdvanceResponse, error) {
return &machine.AdvanceResponse{
Accepted: m.AdvanceAcceptedReturn,
Outputs: m.AdvanceOutputsReturn,
Expand All @@ -1531,7 +1508,7 @@ func (m *MockRollupsMachine) Advance(_ context.Context, _ []byte, _ bool) (*mach
}, m.AdvanceError
}

func (m *MockRollupsMachine) Inspect(_ context.Context, _ []byte) (bool, []machine.Report, error) {
func (m *MockRollupsMachine) Inspect(_ context.Context, _ []byte, _ machine.Hash) (bool, []machine.Report, error) {
return m.InspectAcceptedReturn, m.InspectReportsReturn, m.InspectError
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package emulator

// #cgo LDFLAGS: -lcartesi -lcartesi_jsonrpc
// #include <stdlib.h>
// #include "cartesi-machine/jsonrpc-machine-c-api.h"
// #include "cartesi-machine/cm-jsonrpc.h"
import "C"

import (
Expand Down
20 changes: 13 additions & 7 deletions pkg/emulator/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package emulator

// #include <stdlib.h>
// #include <string.h>
// #include "cartesi-machine/machine-c-api.h"
// #include "cartesi-machine/cm.h"
import "C"

import (
Expand Down Expand Up @@ -354,7 +354,7 @@ func (m *Machine) Run(mcycleEnd uint64) (BreakReason, error) {
}

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

Expand All @@ -368,7 +368,7 @@ func (m *Machine) CollectMCycleRootHashes(mcycleEnd, mcyclePeriod, mcyclePhase u
err = newError(C.cm_collect_mcycle_root_hashes(
m.ptr,
C.uint64_t(mcycleEnd),
C.uint64_t(mcyclePeriod),
C.uint64_t(log2McyclePeriod),
C.uint64_t(mcyclePhase),
C.int32_t(log2BundleMcycleCount),
previousBackTreeC,
Expand All @@ -383,16 +383,22 @@ func (m *Machine) CollectMCycleRootHashes(mcycleEnd, mcyclePeriod, mcyclePhase u
}

// collect_uarch_cycle_root_hashes
func (m *Machine) CollectUarchCycleRootHashes(mcycleEnd uint64, log2BundleMcycleCount int32) ([]byte, error) {
func (m *Machine) CollectUarchCycleRootHashes(mcycleEnd uint64, log2BundleUarchCycleCount int32, revertUarchTail string) ([]byte, error) {
var err error
var result []byte

m.callCAPI(func() {
var cResult *C.char
var revertUarchTailC *C.char
if revertUarchTail != "" {
revertUarchTailC = C.CString(revertUarchTail)
defer C.free(unsafe.Pointer(revertUarchTailC))
}
err = newError(C.cm_collect_uarch_cycle_root_hashes(
m.ptr,
C.uint64_t(mcycleEnd),
C.int32_t(log2BundleMcycleCount),
C.int32_t(log2BundleUarchCycleCount),
revertUarchTailC,
&cResult))
result = []byte(C.GoString(cResult))
})
Expand All @@ -404,7 +410,7 @@ func (m *Machine) CollectUarchCycleRootHashes(mcycleEnd uint64, log2BundleMcycle
}

// send_cmio_response
func (m *Machine) SendCmioResponse(reason uint16, data []byte) error {
func (m *Machine) SendCmioResponse(revertRootHash Hash, reason uint16, data []byte) error {
var err error

m.callCAPI(func() {
Expand All @@ -413,9 +419,9 @@ func (m *Machine) SendCmioResponse(reason uint16, data []byte) error {
if sizeData > 0 {
ptrData = (*C.uint8_t)(unsafe.Pointer(&data[0]))
}

err = newError(C.cm_send_cmio_response(
m.ptr,
(*[32]C.uint8_t)(unsafe.Pointer(&revertRootHash)),
C.uint16_t(reason),
ptrData,
sizeData,
Expand Down
2 changes: 1 addition & 1 deletion pkg/emulator/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package emulator

// #include <stdlib.h>
// #include "cartesi-machine/jsonrpc-machine-c-api.h"
// #include "cartesi-machine/cm-jsonrpc.h"
import "C"

import (
Expand Down
Loading