Skip to content

Commit

Permalink
feat: update cpp-low-level template
Browse files Browse the repository at this point in the history
  • Loading branch information
endersonmaia committed Oct 24, 2024
1 parent d9a8191 commit 58e15e5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 171 deletions.
25 changes: 16 additions & 9 deletions cpp-low-level/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# syntax=docker.io/docker/dockerfile:1
ARG MACHINE_EMULATOR_TOOLS_VERSION=0.16.1
FROM --platform=linux/riscv64 ubuntu:22.04 AS builder

ARG DEBIAN_FRONTEND=noninteractive
RUN <<EOF
set -e
apt-get update
apt-get install -y --no-install-recommends \
autoconf \
automake \
build-essential \
ca-certificates \
curl \
libtool \
wget
pkg-config
rm -rf /var/lib/apt/lists/*
EOF

COPY --from=cartesi/sdk:0.9.0 /include/linux/cartesi /include/linux/cartesi
ARG MACHINE_EMULATOR_TOOLS_VERSION
ADD --checksum=sha256:ac93f0b8b2c8be85e1b3956f3e0206009fbb8c73f9327948a811b1864a2cee8d \
https://github.com/cartesi/machine-emulator-tools/releases/download/v${MACHINE_EMULATOR_TOOLS_VERSION}/libcmt-dev-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb \
/tmp/libcmt-dev-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb
RUN dpkg -i /tmp/libcmt-dev-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb \
&& rm /tmp/libcmt-dev-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb

WORKDIR /opt/cartesi/dapp
COPY . .
RUN make
Expand All @@ -33,7 +36,12 @@ rm -rf /var/lib/apt/lists/* /var/log/* /var/cache/*
useradd --create-home --user-group dapp
EOF

ARG MACHINE_EMULATOR_TOOLS_VERSION=0.16.1
ARG MACHINE_EMULATOR_TOOLS_VERSION
ADD https://github.com/cartesi/machine-emulator-tools/releases/download/v${MACHINE_EMULATOR_TOOLS_VERSION}/libcmt-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb \
/tmp/libcmt-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb
RUN dpkg -i /tmp/libcmt-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb \
&& rm /tmp/libcmt-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb

ADD https://github.com/cartesi/machine-emulator-tools/releases/download/v${MACHINE_EMULATOR_TOOLS_VERSION}/machine-emulator-tools-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb /
RUN dpkg -i /machine-emulator-tools-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb \
&& rm /machine-emulator-tools-v${MACHINE_EMULATOR_TOOLS_VERSION}.deb
Expand All @@ -42,5 +50,4 @@ ENV PATH="/opt/cartesi/bin:${PATH}"

WORKDIR /opt/cartesi/dapp
COPY --from=builder /opt/cartesi/dapp/dapp .

ENTRYPOINT ["/opt/cartesi/dapp/dapp"]
CMD ["/opt/cartesi/dapp/dapp"]
31 changes: 27 additions & 4 deletions cpp-low-level/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
CXX := g++
# Copyright Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

.PHONY: clean
UNAME:=$(shell uname)

dapp: dapp.cpp
$(CXX) -pthread -std=c++17 -I /include -o $@ $^
CC = gcc
STRIP = strip
CFLAGS +=-Wall -Wextra -pedantic -O2 `pkg-config --cflags libcmt`
LDLIBS += `pkg-config --libs libcmt`

all: dapp

dapp: export PKG_CONFIG_PATH ?= /usr/riscv64-linux-gnu/lib/pkgconfig
dapp: dapp.c
$(CC) $(CFLAGS) -o dapp dapp.c $(LDLIBS)
$(STRIP) dapp

clean:
@rm -rf dapp
91 changes: 91 additions & 0 deletions cpp-low-level/dapp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* Copyright Cartesi and individual authors (see AUTHORS)
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "libcmt/rollup.h"

static int finish_request(cmt_rollup_t *me, cmt_rollup_finish_t *finish) {
finish->accept_previous_request = true;
return cmt_rollup_finish(me, finish);
}

static int handle_advance_state_request(cmt_rollup_t *me, uint64_t *index) {
cmt_rollup_advance_t advance;
int rc = cmt_rollup_read_advance_state(me, &advance);
if (rc)
return rc;
*index = advance.index;
fprintf(stderr, "handling advance with index %d\n", (int) advance.index);
return 0;
}

static int handle_inspect_state_request(cmt_rollup_t *me) {
cmt_rollup_inspect_t inspect;
int rc = cmt_rollup_read_inspect_state(me, &inspect);
if (rc)
return rc;
fprintf(stderr, "handling inspect\n");
return 0;
}

static int handle_request(cmt_rollup_t *me, cmt_rollup_finish_t *finish, uint64_t *index) {
switch (finish->next_request_type) {
case HTIF_YIELD_REASON_ADVANCE:
return handle_advance_state_request(me, index);
case HTIF_YIELD_REASON_INSPECT:
return handle_inspect_state_request(me);
default:
/* unknown request type */
fprintf(stderr, "Unknown request type %d\n", finish->next_request_type);
return -1;
}
return 0;
}

int main() {
cmt_rollup_t rollup;
uint64_t advance_index = 0;

if (cmt_rollup_init(&rollup) != 0)
return EXIT_FAILURE;

/* Accept the initial request */
cmt_rollup_finish_t finish;
if (finish_request(&rollup, &finish) != 0) {
exit(1);
}

/* handle a request, then wait for next */
for (;;) {
if (handle_request(&rollup, &finish, &advance_index) != 0) {
break;
}
if (finish_request(&rollup, &finish) != 0) {
break;
}
}

cmt_rollup_fini(&rollup);
fprintf(stderr, "Exiting...\n");
return 1;
}
158 changes: 0 additions & 158 deletions cpp-low-level/dapp.cpp

This file was deleted.

0 comments on commit 58e15e5

Please sign in to comment.