Skip to content

Commit

Permalink
chore(misc/loop): Setup the portal loop infra
Browse files Browse the repository at this point in the history
  • Loading branch information
albttx committed Nov 30, 2023
1 parent 726349d commit 716d261
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 16 deletions.
26 changes: 26 additions & 0 deletions misc/loop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The portal loop :infinity:

## What is it ?

It's a Gnoland node that aim to run with always the latest version of gno and never loose transactions history.

For more information, see issue on github [gnolang/gno#1239](https://github.com/gnolang/gno/issues/1239)


## How to use

Start the loop with:

``` sh
$ docker compose up -d
```

The `snapshotter` container will exec the script [switch.sh](./scripts/switch.sh) every day at 10am (defined in the docker image).

This script is doing:

- Pull the new docker image [ghcr.io/gnolang/gno]
- Backup the txs using [gnolang/tx-archive](https://github.com/gnolang/tx-archive)
- Start a new docker container with the backups files
- Changing the proxy (traefik) to redirect to the new portal loop
- Stop the previous loop
33 changes: 33 additions & 0 deletions misc/loop/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: "3"
services:
traefik:
image: "traefik:v2.10"
network_mode: host
container_name: "traefik"
command:
- "--api.insecure=true"
- "--providers.file=true"
- "--providers.file.watch=true"
- "--providers.file.directory=/etc/traefik/configs"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- ./traefik:/etc/traefik/configs

snapshotter:
build:
dockerfile: ./snapshotter/Dockerfile
command: ["/usr/sbin/crond", "-l", "2", "-f"]
working_dir: /app
network_mode: host
volumes:
- ./scripts:/scripts
- ./backups:/backups
- ./traefik:/etc/traefik/configs
- "/var/run/docker.sock:/var/run/docker.sock:ro"
environment:
HOST_PWD: $PWD
extra_hosts:
- gno.land:127.0.0.1
32 changes: 16 additions & 16 deletions misc/loop/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ module loop
go 1.20

require (
github.com/gnolang/gno v0.0.0-20231112174927-b1a53c018ea4
github.com/gnolang/gno v0.0.0-20231124185034-aaeb808f18d7
github.com/gnolang/tx-archive v0.1.1
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/badger/v3 v3.2103.4 // indirect
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gnolang/goleveldb v0.0.9 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gnolang/goleveldb v0.1.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/klauspost/compress v1.17.3 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.8.5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
Expand All @@ -37,15 +37,15 @@ require (
github.com/rs/cors v1.10.1 // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
go.etcd.io/bbolt v1.3.8 // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/tools v0.16.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

Expand Down
97 changes: 97 additions & 0 deletions misc/loop/go.sum

Large diffs are not rendered by default.

File renamed without changes.
24 changes: 24 additions & 0 deletions misc/loop/scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env sh

MONIKER=${MONIKER:-"gnode"}
P2P_LADDR=${P2P_LADDR:-"tcp://0.0.0.0:26656"}
RPC_LADDR=${RPC_LADDR:-"tcp://0.0.0.0:26657"}

GENESIS_BACKUP_FILE=${GENESIS_BACKUP_FILE:-""}

SEEDS=${SEEDS:-""}
PERSISTENT_PEERS=${PERSISTENT_PEERS:-""}

gnoland start \
--skip-start=true \
--skip-failing-genesis-txs \
--genesis-txs-file "/${GENESIS_BACKUP_FILE}"

sed -i "s#^moniker = \".*\"#moniker = \"${MONIKER}\"#" ./testdir/config/config.toml
sed -i "s#^laddr = \".*:26656\"#laddr = \"${P2P_LADDR}\"#" ./testdir/config/config.toml
sed -i "s#^laddr = \".*:26657\"#laddr = \"${RPC_LADDR}\"#" ./testdir/config/config.toml

sed -i "s#^seeds = \".*\"#seeds = \"${SEEDS}\"#" ./testdir/config/config.toml
sed -i "s#^persistent_peers = \".*\"#persistent_peers = \"${PERSISTENT_PEERS}\"#" ./testdir/config/config.toml

exec gnoland start --skip-failing-genesis-txs
75 changes: 75 additions & 0 deletions misc/loop/scripts/switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env sh

set -e

# This script goal is to:
# 1. Snapshot txs for current running node
# 2. Start a new node with this tx in genesis file
# 3. switch traefik node


BACKUP_DIR="/backups"

NOW=$(date +'%Y-%m-%d_%s')
BACKUP_FILE="${BACKUP_DIR}/backup_${NOW}.jsonl"
BACKUP_LEGACY_FILE=$(echo ${BACKUP_FILE} | sed 's/.jsonl$/-legacy.jsonl/')

HOST_PWD="${HOST_PWD:=$(pwd)}"

TX_ARCHIVE_CMD=${TX_ARCHIVE_CMD:-"tx-archive"}

CONTAINER_NAME="gno-${NOW}"

# TODO: SET THE CURRENT PORTAL LOOP in read only mode

# Get latest version of gno
docker pull ghcr.io/gnolang/gno

# If there is no portal loop running, we start one
if docker ps --format json | jq '.Labels' | grep -q "the-portal-loop"; then
${TX_ARCHIVE_CMD} backup \
--overwrite=true \
--remote "gno.land:80" \
--from-block 1 \
--output-path="${BACKUP_FILE}"

cat ${BACKUP_FILE} | jq -c -M '.tx' > ${BACKUP_LEGACY_FILE}
fi

docker volume create ${CONTAINER_NAME}

docker run -it \
-d \
--name "$CONTAINER_NAME" \
-v ${HOST_PWD}/scripts:/scripts \
-v ${HOST_PWD}/backups:/backups \
-v ${CONTAINER_NAME}:/opt/gno/src/testdir \
-p 26656 \
-p 26657 \
-e MONIKER="the-portal-loop" \
-e GENESIS_BACKUP_FILE="${BACKUP_LEGACY_FILE}" \
--label "the-portal-loop=${CONTAINER_NAME}" \
--entrypoint /scripts/start.sh \
ghcr.io/gnolang/gno

sleep 5

PORTS=$(docker inspect $CONTAINER_NAME | jq '.[0].NetworkSettings.Ports')

RPC_PORT=$(echo $PORTS | jq -r '."26657/tcp"[0].HostPort')

echo "New instance is running on: localhost:${RPC_PORT}"

# wait for RPC to be up
curl -s --retry 10 --retry-delay 5 --retry-all-errors -o /dev/null "localhost:${RPC_PORT}/status"


# NOTE: Could be good to wait 5 blocks

# Update traefik url
sed -i -E "s#localhost:[0-9]+#localhost:${RPC_PORT}#" /etc/traefik/configs/gno.yml

# Delete previous container
docker rm -f $(docker ps --format json --filter "label=the-portal-loop" | jq -r '.ID' | tail -n +2)

docker volume prune -f
10 changes: 10 additions & 0 deletions misc/loop/snapshotter/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ghcr.io/gnolang/tx-archive as archive-bin

FROM docker

RUN apk add bash curl jq

COPY --from=archive-bin /tx-archive /usr/local/bin/tx-archive

# Every day at 10am
RUN echo "* 10 * * * sh /scripts/switch.sh" > /var/spool/cron/crontabs/root
15 changes: 15 additions & 0 deletions misc/loop/traefik/gno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
http:
routers:
gno-portal-loop:
service: gno-portal-loop
# tls: "true"
rule: "Host(`gno.land`)"
entrypoints: "web"
middlewares: []

services:
gno-portal-loop:
loadBalancer:
servers:
- url: "http://localhost:26657"

0 comments on commit 716d261

Please sign in to comment.