Skip to content

Commit

Permalink
refactor: removed snapshot storage
Browse files Browse the repository at this point in the history
  • Loading branch information
gligneul committed Jan 23, 2024
1 parent a04532f commit 7730431
Show file tree
Hide file tree
Showing 30 changed files with 205 additions and 1,461 deletions.
29 changes: 15 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ jobs:
with:
submodules: recursive

- uses: depot/setup-action@v1
- name: Build dependency images
uses: depot/bake-action@v1
with:
files: |
./docker-bake.hcl
./docker-bake.override.hcl
./docker-bake.platforms.hcl
targets: |
rollups-node-devnet
rollups-node-snapshot
project: ${{ vars.DEPOT_PROJECT }}
workdir: build
load: true

- name: 📦 Install protoc
run: sudo apt update && sudo apt install -y protobuf-compiler libprotobuf-dev

Expand Down Expand Up @@ -83,20 +98,6 @@ jobs:
with:
go-version-file: 'go.mod'

- uses: depot/setup-action@v1

- name: Build and load devnet image
uses: depot/bake-action@v1
with:
files: |
./docker-bake.hcl
./docker-bake.override.hcl
./docker-bake.platforms.hcl
targets: rollups-node-devnet
project: ${{ vars.DEPOT_PROJECT }}
workdir: build
load: true

- name: Run Go tests
working-directory: ${{ github.workspace }}
run: go test ./...
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed installation paths in the Docker image. Now all the binaries are installed in /usr/bin
instead of /opt/cartesi/bin.
- Changed the base Docker image to debian-bookworm instead cartesi/server-manager.

### Removed

- Removed concurrent block fetch in foldable `InputBox`.
- Removed snapshot-saving feature. Now, the node will always start from the beginning.

## [1.2.0]

Expand Down
26 changes: 11 additions & 15 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ ARG LINUX_KERNEL_VERSION
ARG ROM_VERSION

# Build directories.
ARG MACHINE_SNAPSHOT_BUILD_PATH=/build/machine-snapshot
ARG SNAPSHOT_BUILD_PATH=/build/snapshot
ARG DEVNET_BUILD_PATH=/build/devnet
ARG RUST_BUILD_PATH=/build/rollups-node/rust
ARG GO_BUILD_PATH=/build/rollups-node/go

# Runtime dir for the cartesi-machine snapshot.
ARG MACHINE_SNAPSHOT_RUNTIME_PATH=/usr/share/cartesi/snapshots
ARG SNAPSHOT_RUNTIME_PATH=/usr/share/cartesi/snapshot

####################################################################################################
# STAGE: emulator-base
Expand Down Expand Up @@ -87,12 +87,12 @@ ADD https://github.com/cartesi/image-kernel/releases/download/v${LINUX_VERSION}/
ADD https://github.com/cartesi/machine-emulator-rom/releases/download/v${ROM_VERSION}/rom-v${ROM_VERSION}.bin rom.bin

# Generate snapshot with echo and store it.
ARG MACHINE_SNAPSHOT_BUILD_PATH
WORKDIR ${MACHINE_SNAPSHOT_BUILD_PATH}
ARG SNAPSHOT_BUILD_PATH
WORKDIR /build
RUN cartesi-machine \
--ram-length=128Mi \
--rollup \
--store=$MACHINE_SNAPSHOT_BUILD_PATH/snapshot \
--store=$SNAPSHOT_BUILD_PATH \
-- "ioctl-echo-loop --vouchers=1 --notices=1 --reports=1 --verbose=1"

# STAGE: rollups-node-snapshot
Expand All @@ -103,14 +103,10 @@ RUN cartesi-machine \
FROM emulator-base as rollups-node-snapshot

# Copy image from the builder stage.
ARG MACHINE_SNAPSHOT_BUILD_PATH
ARG MACHINE_SNAPSHOT_RUNTIME_PATH
WORKDIR ${MACHINE_SNAPSHOT_RUNTIME_PATH}
COPY --from=snapshot-builder --chown=cartesi:cartesi ${MACHINE_SNAPSHOT_BUILD_PATH}/snapshot ./0_0
RUN <<EOF
ln -s ${MACHINE_SNAPSHOT_RUNTIME_PATH}/0_0 ${MACHINE_SNAPSHOT_RUNTIME_PATH}/latest
chown -R cartesi:cartesi ${MACHINE_SNAPSHOT_RUNTIME_PATH}
EOF
ARG SNAPSHOT_BUILD_PATH
ARG SNAPSHOT_RUNTIME_PATH
WORKDIR ${SNAPSHOT_RUNTIME_PATH}
COPY --from=snapshot-builder --chown=cartesi:cartesi ${SNAPSHOT_BUILD_PATH} ${SNAPSHOT_RUNTIME_PATH}

# Set dummy command.
CMD /bin/bash
Expand Down Expand Up @@ -156,8 +152,8 @@ ARG DEVNET_BUILD_PATH
WORKDIR ${DEVNET_BUILD_PATH}

# Copy machine snapshot hash.
ARG MACHINE_SNAPSHOT_BUILD_PATH
COPY --from=snapshot-builder ${MACHINE_SNAPSHOT_BUILD_PATH}/snapshot/hash hash.bin
ARG SNAPSHOT_BUILD_PATH
COPY --from=snapshot-builder ${SNAPSHOT_BUILD_PATH}/hash hash.bin

# Generate Anvil state.
COPY scripts scripts
Expand Down
6 changes: 3 additions & 3 deletions build/compose-snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ services:
machine_snapshot_setup:
image: cartesi/rollups-node-snapshot:devel
volumes:
- machine:/usr/share/cartesi/snapshots
- machine:/usr/share/cartesi/snapshot

node:
depends_on:
machine_snapshot_setup:
condition: service_completed_successfully
environment:
CARTESI_SNAPSHOT_DIR: "/usr/share/cartesi/snapshots"
CARTESI_SNAPSHOT_DIR: "/usr/share/cartesi/snapshot"
volumes:
- machine:/usr/share/cartesi/snapshots
- machine:/usr/share/cartesi/snapshot

volumes:
machine: {}
2 changes: 1 addition & 1 deletion cmd/cartesi-rollups-cli/root/savesnapshot/savesnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

func init() {
Cmd.Flags().StringVar(&sourceDockerImage, "docker-image",
"cartesi/rollups-machine-snapshot:devel",
"cartesi/rollups-node-snapshot:devel",
"Docker image containing the Cartesi Machine snapshot to be used")

Cmd.Flags().StringVar(&tempContainerName, "temp-container-name", "temp-machine",
Expand Down
8 changes: 2 additions & 6 deletions cmd/cartesi-rollups-node/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,9 @@ func newAdvanceRunner() services.CommandService {
if config.GetCartesiFeatureHostMode() || config.GetCartesiFeatureDisableMachineHashCheck() {
s.Env = append(s.Env, "SNAPSHOT_VALIDATION_ENABLED=false")
}
if config.GetCartesiFeatureHostMode() {
s.Env = append(s.Env, "SNAPSHOT_ENABLED=false")
} else {
s.Env = append(s.Env,
fmt.Sprintf("SNAPSHOT_DIR=%v", config.GetCartesiSnapshotDir()))
if !config.GetCartesiFeatureHostMode() {
s.Env = append(s.Env,
fmt.Sprintf("SNAPSHOT_LATEST=%v/latest", config.GetCartesiSnapshotDir()))
fmt.Sprintf("MACHINE_SNAPSHOT_PATH=%v", config.GetCartesiSnapshotDir()))
}
s.Env = append(s.Env, os.Environ()...)
return s
Expand Down
2 changes: 1 addition & 1 deletion docs/cartesi-rollups-cli_save-snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cartesi-rollups-cli save-snapshot

```
--dest-dir string directory where to store the Cartesi Machine snapshot to be used by the local Node (default "./machine-snapshot")
--docker-image string Docker image containing the Cartesi Machine snapshot to be used (default "cartesi/rollups-machine-snapshot:devel")
--docker-image string Docker image containing the Cartesi Machine snapshot to be used (default "cartesi/rollups-node-snapshot:devel")
-h, --help help for save-snapshot
--temp-container-name string Name of the temporary container needed to extract the machine snapshot files (default "temp-machine")
```
Expand Down
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ At the end of each epoch, the node will send claims to the blockchain.

## `CARTESI_SNAPSHOT_DIR`

Path to the directory of the cartesi-machine snapshots.
Path to the directory with the cartesi-machine snapshot that will be loaded by the node.

* **Type:** `string`

2 changes: 1 addition & 1 deletion internal/config/generate/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ The node will begin to read blockchain events from this block."""
[snapshot.CARTESI_SNAPSHOT_DIR]
go-type = "string"
description = """
Path to the directory of the cartesi-machine snapshots."""
Path to the directory with the cartesi-machine snapshot that will be loaded by the node."""

#
# Auth
Expand Down
62 changes: 15 additions & 47 deletions internal/machine/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
package machine

import (
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/cartesi/rollups-node/internal/config"
)

const SNAPSHOT_CONTAINER_PATH = "/usr/share/cartesi/snapshot"

func runCommand(name string, args ...string) error {
config.InfoLogger.Printf("%v %v", name, strings.Join(args, " "))
cmd := exec.Command(name, args...)
Expand All @@ -26,42 +29,26 @@ func runCommand(name string, args ...string) error {
return nil
}

func getAbsolutePath(relativePath string) (string, error) {
// Get the absolute path for the current working directory
currentDir, err := os.Getwd()
if err != nil {
return "", err
}

// Join the current directory and the relative path to get the absolute path
absolutePath := filepath.Join(currentDir, relativePath)

// Clean the path to remove any redundant elements
absolutePath, err = filepath.Abs(absolutePath)
if err != nil {
return "", err
}

return absolutePath, nil
}

func fileExists(filePath string) bool {
_, err := os.Stat(filePath)

// Check if the file exists by examining the error returned by os.Stat
return !os.IsNotExist(err)
return !errors.Is(err, fs.ErrNotExist)
}

func Save(sourceDockerImage string, destDir string, tempContainerName string) error {

// Create Snapshot dir
err := os.MkdirAll(destDir, os.ModePerm)
if err != nil {
return err
// Remove previous snapshot dir
if fileExists(destDir) {
config.InfoLogger.Println("removing previous snapshot")
err := os.RemoveAll(destDir)
if err != nil {
return err
}
}

// Copy machine snapshot from Docker Container
err = runCommand("docker", "create", "--name", tempContainerName, sourceDockerImage)
err := runCommand("docker", "create", "--name", tempContainerName, sourceDockerImage)
if err != nil {
return err
}
Expand All @@ -73,27 +60,8 @@ func Save(sourceDockerImage string, destDir string, tempContainerName string) er
}
}()

err = runCommand("docker", "cp", fmt.Sprintf("%v:/var/opt/cartesi/machine-snapshots/0_0",
tempContainerName), filepath.Join(destDir, "0_0"))
if err != nil {
return err
}

// Create 'latest' symlink
symLinkPath := filepath.Join(destDir, "latest")
if fileExists(symLinkPath) {
err = os.Remove(symLinkPath)
if err != nil {
return err
}
}

fullPath, pathErr := getAbsolutePath(filepath.Join(destDir, "0_0"))
if pathErr != nil {
return pathErr
}

err = os.Symlink(fullPath, symLinkPath)
fromDir := fmt.Sprintf("%v:%v", tempContainerName, SNAPSHOT_CONTAINER_PATH)
err = runCommand("docker", "cp", fromDir, destDir)
if err != nil {
return err
}
Expand Down
4 changes: 0 additions & 4 deletions offchain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions offchain/advance-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@ name = "cartesi-rollups-advance-runner"
path = "src/main.rs"

[dependencies]
contracts = { path = "../contracts" }
grpc-interfaces = { path = "../grpc-interfaces" }
http-health-check = { path = "../http-health-check" }
log = { path = "../log" }
rollups-events = { path = "../rollups-events" }

async-trait.workspace = true
backoff = { workspace = true, features = ["tokio"] }
clap = { workspace = true, features = ["derive", "env"] }
ethers.workspace = true
hex.workspace = true
sha3 = { workspace = true, features = ["std"] }
snafu.workspace = true
tokio = { workspace = true, features = ["macros", "time", "rt-multi-thread"] }
tonic.workspace = true
tracing.workspace = true
url.workspace = true
uuid = { workspace = true, features = ["v4"] }

[dev-dependencies]
Expand Down
Loading

0 comments on commit 7730431

Please sign in to comment.