From 9c45ebb234bf4d16b876d165d35c478f10fb79d2 Mon Sep 17 00:00:00 2001 From: lAmeR1 <42315864+lAmeR1@users.noreply.github.com> Date: Tue, 7 May 2024 23:41:21 +0200 Subject: [PATCH] Devnet simnet support (#41) * devnet and simnet support * fix docker build for main --------- Co-authored-by: lAmeR1 --- .github/workflows/docker-image.yml | 46 ++++++++++++++++++-------- .github/workflows/docker-testimage.yml | 2 +- constants.py | 27 +++++++++++---- endpoints/__init__.py | 4 +-- endpoints/get_balance.py | 3 +- 5 files changed, 57 insertions(+), 25 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index eac6804..93e78b5 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -1,26 +1,44 @@ -name: Docker Image CI - +name: Docker Image on: release: types: [ published ] jobs: build: - runs-on: ubuntu-latest + strategy: + matrix: + os: [ ubuntu-latest ] + arch: [ amd64, arm64 ] + + runs-on: ${{ matrix.os }} + steps: - - name: Checkout - uses: actions/checkout@v3 + - name: Add SHORT_SHA env property with commit short sha + run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-7`" >> $GITHUB_ENV + - name: Checkout code + uses: actions/checkout@v2 + - name: Login to Docker Hub uses: docker/login-action@v2 with: username: lamer1 password: ${{ secrets.DOCKER_ACCESS_TOKEN }} - - name: Build and push - uses: docker/build-push-action@v4 - with: - context: . - push: true - tags: supertypo/kaspa-rest-server:${{github.ref_name}}, supertypo/kaspa-rest-server:latest - file: ./docker/Dockerfile - build-args: | - version=${{github.ref_name}} + + - name: Set up QEMU (for ARM64 emulation) + if: matrix.arch == 'arm64' + run: | + sudo apt-get install -y qemu-user-static + + - name: Set up Docker Buildx (for multi-platform builds) + uses: docker/setup-buildx-action@v1 + + - name: Build and push Docker image + run: | + docker buildx build \ + --platform linux/amd64,linux/arm64 \ + -t supertypo/kaspa-rest-server:${{github.ref_name}} \ + -t supertypo/kaspa-rest-server:latest + --build-arg version=${{github.ref_name}} + --push . + + docker buildx imagetools inspect supertypo/kaspa-rest-server:${SHORT_SHA} diff --git a/.github/workflows/docker-testimage.yml b/.github/workflows/docker-testimage.yml index ba39f34..3a03dcd 100644 --- a/.github/workflows/docker-testimage.yml +++ b/.github/workflows/docker-testimage.yml @@ -12,7 +12,7 @@ jobs: steps: - name: Add SHORT_SHA env property with commit short sha - run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV + run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-7`" >> $GITHUB_ENV - name: Checkout code uses: actions/checkout@v2 diff --git a/constants.py b/constants.py index cecf0bc..7eda8e0 100644 --- a/constants.py +++ b/constants.py @@ -1,9 +1,24 @@ import os -IS_TESTNET = os.getenv('TESTNET', 'false').lower() == 'true' -REGEX_KASPA_ADDRESS = "^kaspa:[a-z0-9]{61,63}$" if not IS_TESTNET else "^kaspatest:[a-z0-9]{61,63}$" +NETWORK_TYPE = os.getenv('NETWORK_TYPE', 'mainnet').lower() -# address constants -ADDRESS_PREFIX = "kaspatest" if IS_TESTNET else "kaspa" -ADDRESS_EXAMPLE = "kaspatest:qpqz2vxj23kvh0m73ta2jjn2u4cv4tlufqns2eap8mxyyt0rvrxy6ejkful67" if IS_TESTNET \ - else "kaspa:qqkqkzjvr7zwxxmjxjkmxxdwju9kjs6e9u82uh59z07vgaks6gg62v8707g73" +match NETWORK_TYPE: + case "mainnet": + address_prefix = "kaspa" + address_example = "kaspa:qqkqkzjvr7zwxxmjxjkmxxdwju9kjs6e9u82uh59z07vgaks6gg62v8707g73" + case "testnet": + address_prefix = "kaspatest" + address_example = "kaspatest:qpqz2vxj23kvh0m73ta2jjn2u4cv4tlufqns2eap8mxyyt0rvrxy6ejkful67" + case "simnet": + address_prefix = "kaspasim" + address_example = "kaspasim:qpqz2vxj23kvh0m73ta2jjn2u4cv4tlufqns2eap8mxyyt0rvrxy6ejkful67" + case "devnet": + address_prefix = "kaspadev" + address_example = "kaspadev:qpqz2vxj23kvh0m73ta2jjn2u4cv4tlufqns2eap8mxyyt0rvrxy6ejkful67" + case _: + raise ValueError(f'Network type {NETWORK_TYPE} not supported.') + +ADDRESS_PREFIX = address_prefix +ADDRESS_EXAMPLE = address_example + +REGEX_KASPA_ADDRESS = f"^{ADDRESS_PREFIX}:[a-z0-9]{61, 63}$" diff --git a/endpoints/__init__.py b/endpoints/__init__.py index efe588a..83c1246 100644 --- a/endpoints/__init__.py +++ b/endpoints/__init__.py @@ -3,7 +3,7 @@ from fastapi import HTTPException from functools import wraps -from constants import IS_TESTNET +from constants import NETWORK_TYPE def filter_fields(response_dict, fields): @@ -29,7 +29,7 @@ async def wrapper(*args, **kwargs): def mainnet_only(func): @wraps(func) async def wrapper(*args, **kwargs): - if IS_TESTNET: + if NETWORK_TYPE != "mainnet": raise HTTPException(status_code=503, detail="Endpoint not available. " "This endpoint is only available in mainnet.") return await func(*args, **kwargs) diff --git a/endpoints/get_balance.py b/endpoints/get_balance.py index d51e6f0..b89bbe9 100644 --- a/endpoints/get_balance.py +++ b/endpoints/get_balance.py @@ -1,9 +1,8 @@ # encoding: utf-8 -import os from fastapi import Path, HTTPException from pydantic import BaseModel -from constants import ADDRESS_EXAMPLE, REGEX_KASPA_ADDRESS, IS_TESTNET +from constants import ADDRESS_EXAMPLE, REGEX_KASPA_ADDRESS from server import app, kaspad_client