Skip to content

Commit

Permalink
Generate genesis within the image. Improve status check
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Sep 12, 2024
1 parent e37a40b commit 8c8b59e
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 67 deletions.
12 changes: 2 additions & 10 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,10 @@ jobs:
- name: Download required file
run: chmod +x ./scripts/download-jar.sh && ./scripts/download-jar.sh

- name: Build Image
- name: Build And Push
uses: docker/build-push-action@v6
with:
context: .
load: true
push: false
push: true
tags: |
${{ env.IMAGE_NAME }}:test
${{ env.IMAGE_NAME }}:latest
- name: Test
run: chmod +x ./scripts/test.sh && ./scripts/test.sh ${{ env.IMAGE_NAME }}:test

- name: Push Image
run: docker push ${{ env.IMAGE_NAME }}:latest
11 changes: 8 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@ WORKDIR /opt/sidecar
COPY ./sidecar/ .
RUN go build -o /opt/sidecar/sidecar main.go

FROM openjdk:24-slim AS ton-node
FROM ubuntu:22.04 AS ton-node

ARG WORKDIR="/opt/my-local-ton"

# Install dependencies && drop apt cache
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl jq vim lsb-release \
openjdk-21-jre-headless curl jq vim lsb-release \
&& rm -rf /var/lib/apt/lists/*

COPY scripts/my-local-ton.jar $WORKDIR/
COPY scripts/entrypoint.sh $WORKDIR/
COPY scripts/genesis.sh $WORKDIR/

COPY --from=go-builder /opt/sidecar $WORKDIR/

WORKDIR $WORKDIR

# Ensure whether the build is working
# Ensure whether the build works
RUN chmod +x entrypoint.sh \
&& chmod +x sidecar \
&& chmod +x genesis.sh \
&& java -jar my-local-ton.jar debug nogui test-binaries;

# Warmup the state
RUN ./genesis.sh

# Lite Client
EXPOSE 4443

Expand Down
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ build-no-cache: # Build w/o cache

test-sidecar: ## Test sidecar
go test ./sidecar/...

test-image: ## Test image
./scripts/test.sh ton-local:latest
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This docker image represents a fully working TON node with 1 validator and a faucet wallet.

> Note: use for LOCAL development only.
## How it works

- It uses [my-local-ton](https://github.com/neodix42/MyLocalTon) project without GUI.
Expand Down
68 changes: 68 additions & 0 deletions scripts/genesis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash

node_command="java -jar my-local-ton.jar with-validators-1 nogui debug"
sidecar_command="./sidecar"

timeout_seconds=300
poll_interval=10

status_url="http://localhost:8000/status"

start_processes() {
echo "Starting node..."
$node_command &
node_pid=$!

echo "Starting sidecar..."
$sidecar_command &
sidecar_pid=$!
}

check_status() {
response=$(curl -s -w "\n%{http_code}" $status_url)
body=$(echo "$response" | head -n 1)
http_status=$(echo "$response" | tail -n 1)

if [ "$http_status" == "200" ]; then
echo "Pass: $body"
return 0
else
echo "Waiting: $body"
return 1
fi
}

shutdown_processes() {
echo "Shutting down node (PID: $node_pid)..."
kill -SIGTERM $node_pid

echo "Shutting down sidecar (PID: $sidecar_pid)..."
kill -SIGTERM $sidecar_pid

wait $node_pid
wait $sidecar_pid

echo "Node and sidecar shut down successfully."
}

# Handling script termination gracefully
trap shutdown_processes SIGTERM SIGINT

start_processes

echo "Checking $status_url (timeout: $timeout_seconds seconds)"

elapsed=0
while [ $elapsed -lt $timeout_seconds ]; do
if check_status; then
shutdown_processes
exit 0
fi

sleep $poll_interval
elapsed=$((elapsed + poll_interval))
done

echo "FAIL. Timeout reached ($timeout_seconds seconds)."
shutdown_processes
exit 1
51 changes: 0 additions & 51 deletions scripts/test.sh

This file was deleted.

16 changes: 16 additions & 0 deletions sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"net/http"
"os"
"os/exec"
)

const (
Expand Down Expand Up @@ -84,6 +85,10 @@ func statusHandler(w http.ResponseWriter, _ *http.Request) error {
return fmt.Errorf("lite client config %q not found: %w", liteClientConfigPath, err)
}

if err := checkLiteClient(); err != nil {
return fmt.Errorf("lite client check failed: %w", err)
}

faucet, err := extractFaucetFromSettings(settingsPath)
if err != nil {
return err
Expand All @@ -103,6 +108,7 @@ func statusHandler(w http.ResponseWriter, _ *http.Request) error {
}

jsonResponse(w, http.StatusOK, map[string]string{"status": "OK"})

return nil
}

Expand Down Expand Up @@ -169,3 +175,13 @@ func ip2int(ip net.IP) uint32 {
func uint32ToBytes(n uint32) []byte {
return []byte(fmt.Sprintf("%d", n))
}

func checkLiteClient() error {
const (
bin = basePath + "/genesis/bin/lite-client"
config = basePath + "/genesis/db/my-ton-global.config.json"
timeoutSec = "2"
)

return exec.Command(bin, "--timeout", timeoutSec, "--global-config", config, "-c", "last").Run()
}

0 comments on commit 8c8b59e

Please sign in to comment.