Skip to content

Commit

Permalink
feat: add multi-stage build and fix db instance problem
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemarlon committed Oct 13, 2024
1 parent 4c90a80 commit 0793945
Show file tree
Hide file tree
Showing 29 changed files with 1,191 additions and 2,209 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bidings:

.PHONY: test
test:
@go test -p 1 ./... -coverprofile=./coverage.md -v
@go test ./... -coverprofile=./coverage.md -v

.PHONY: coverage
coverage: test
Expand Down
63 changes: 44 additions & 19 deletions build/Dockerfile.machine
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG RUST_VERSION=1.81.0
ARG APP_NAME=verifier

################################################################################
# cross build stage
FROM ubuntu:22.04 as build-stage
# Create a stage for building the application.

ARG DEBIAN_FRONTEND=noninteractive
RUN <<EOF
set -e
apt update
apt upgrade -y
apt install -y --no-install-recommends \
build-essential \
ca-certificates \
g++-riscv64-linux-gnu \
wget
EOF
FROM rust:${RUST_VERSION}-alpine AS rust_build
ARG APP_NAME
WORKDIR /app

ARG GOVERSION=1.22.1
# Install host build dependencies.
RUN apk add --no-cache clang lld musl-dev libressl-dev pkgconfig git

RUN rustup target add riscv64gc-unknown-linux-gnu

# Build the application.
# Leverage a cache mount to /usr/local/cargo/registry/
# for downloaded dependencies, a cache mount to /usr/local/cargo/git/db
# for git repository dependencies, and a cache mount to /app/target/ for
# compiled dependencies which will speed up subsequent builds.
# Leverage a bind mount to the src directory to avoid having to copy the
# source code into the container. Once built, copy the executable to an
# output directory before the cache mounted /app/target is unmounted.
RUN --mount=type=bind,source=./cmd/dapp/lib/src,target=src \
--mount=type=bind,source=./cmd/dapp/lib/Cargo.toml,target=Cargo.toml \
--mount=type=cache,target=/app/target/ \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/ \
cargo build --release --target riscv64gc-unknown-linux-gnu && \
cp target/riscv64gc-unknown-linux-gnu/release/lib${APP_NAME}.a /usr/lib

################################################################################
# cross build stage
FROM golang:1.22.5-bookworm AS build
ARG APP_NAME
WORKDIR /src

RUN wget https://go.dev/dl/go${GOVERSION}.linux-$(dpkg --print-architecture).tar.gz && \
tar -C /usr/local -xzf go${GOVERSION}.linux-$(dpkg --print-architecture).tar.gz
# Install Go dependencies
RUN apt-get update && apt-get install -y gcc musl-dev libstdc++6 g++-riscv64-linux-gnu

ENV PATH=/usr/local/go/bin:${PATH}
COPY --from=rust_build /usr/lib/lib${APP_NAME}.a /usr/lib/

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
Expand All @@ -38,7 +63,7 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \
# source code into the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,target=. \
CGO_ENABLED=1 GOARCH=riscv64 GOOS=linux CC=riscv64-linux-gnu-gcc go build -o /bin/app ./cmd/dapp/
CGO_ENABLED=1 GOARCH=riscv64 GOOS=linux CC=riscv64-linux-gnu-gcc go build -ldflags '-extldflags "-static"' -o /bin/app ./cmd/dapp/

# runtime stage: produces final image that will be executed
FROM --platform=linux/riscv64 riscv64/ubuntu:22.04 as runtime
Expand Down Expand Up @@ -68,7 +93,7 @@ RUN chown -R dapp:dapp .

ENV PATH="/opt/cartesi/bin:${PATH}"

COPY --from=build-stage /bin/app app
COPY --from=build /bin/app app

ENTRYPOINT ["rollup-init"]

Expand Down
38 changes: 28 additions & 10 deletions cmd/dapp/dapp.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
package main

/*
#cgo LDFLAGS: -L./ -lverifier
#cgo CFLAGS: -I./include
#include <stdint.h>
int32_t add_numbers(int32_t a, int32_t b);
*/
import (
"C"
"log"

"github.com/tribeshq/tribes/configs"
"github.com/tribeshq/tribes/pkg/router"
)

func NewDApp() *router.Router {
//////////////////////// Setup Database //////////////////////////
db, err := configs.SetupSQlite()
if err != nil {
log.Fatalf("Failed to setup sqlite database: %v", err)
}

//////////////////////// Setup Handlers //////////////////////////
ah, err := NewAdvanceHandlers()
ah, err := NewAdvanceHandlers(db)
if err != nil {
log.Fatalf("Failed to initialize advance handlers from wire: %v", err)
}

ih, err := NewInspectHandlers()
ih, err := NewInspectHandlers(db)
if err != nil {
log.Fatalf("Failed to initialize inspect handlers from wire: %v", err)
}

ms, err := NewMiddlewares()
ms, err := NewMiddlewares(db)
if err != nil {
log.Fatalf("Failed to initialize middlewares from wire: %v", err)
}
Expand All @@ -36,14 +51,12 @@ func NewDApp() *router.Router {
app.HandleAdvance("createAuction", ms.TLSN.Middleware(ah.AuctionAdvanceHandlers.CreateAuctionHandler))
app.HandleAdvance("finishAuction", ah.AuctionAdvanceHandlers.FinishAuctionHandler)

// app.HandleAdvance("withdrawApp", ms.RBAC.Middleware(ah.UserAdvanceHandlers.WithdrawAppHandler, "admin"))
app.HandleAdvance("withdraw", ah.UserAdvanceHandlers.WithdrawHandler)

app.HandleAdvance("createUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.CreateUserHandler, "admin"))
app.HandleAdvance("deleteUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.DeleteUserByAddressHandler, "admin"))

//////////////////////// Inspect //////////////////////////

app.HandleInspect("auction", ih.AuctionInspectHandlers.FindAllAuctionsHandler)
app.HandleInspect("auction/{id}", ih.AuctionInspectHandlers.FindAuctionByIdHandler)

Expand All @@ -62,18 +75,24 @@ func NewDApp() *router.Router {
}

func NewDAppMemory() *router.Router {
//////////////////////// Setup Database //////////////////////////
db, err := configs.SetupSQliteMemory()
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}

//////////////////////// Setup Handlers //////////////////////////
ah, err := NewAdvanceHandlersMemory()
ah, err := NewAdvanceHandlersMemory(db)
if err != nil {
log.Fatalf("Failed to initialize advance handlers from wire: %v", err)
}

ih, err := NewInspectHandlersMemory()
ih, err := NewInspectHandlersMemory(db)
if err != nil {
log.Fatalf("Failed to initialize inspect handlers from wire: %v", err)
}

ms, err := NewMiddlewaresMemory()
ms, err := NewMiddlewaresMemory(db)
if err != nil {
log.Fatalf("Failed to initialize middlewares from wire: %v", err)
}
Expand All @@ -91,7 +110,6 @@ func NewDAppMemory() *router.Router {
app.HandleAdvance("createAuction", ms.TLSN.Middleware(ah.AuctionAdvanceHandlers.CreateAuctionHandler))
app.HandleAdvance("finishAuction", ah.AuctionAdvanceHandlers.FinishAuctionHandler)

// app.HandleAdvance("withdrawApp", ms.RBAC.Middleware(ah.UserAdvanceHandlers.WithdrawAppHandler, "admin"))
app.HandleAdvance("withdraw", ah.UserAdvanceHandlers.WithdrawHandler)

app.HandleAdvance("createUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.CreateUserHandler, "admin"))
Expand Down
Loading

0 comments on commit 0793945

Please sign in to comment.