Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filler ng #40

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions .github/workflows/docker-testimage.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
name: Docker Image TESTBUILD

on:
workflow_dispatch
name: Docker Image
on: push

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: 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:test
file: ./docker/Dockerfile
build-args: |
version=${{github.ref_name}}
- name: Add SHORT_SHA env property with commit short sha
run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $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: 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:${SHORT_SHA} \
--push .

docker buildx imagetools inspect supertypo/kaspa-rest-server:${SHORT_SHA}
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM python:3.12-slim

ARG REPO_DIR

EXPOSE 8000

ENV KASPAD_HOST_1=n.seeder1.kaspad.net:16110
ARG version
ENV VERSION=$version

RUN apt update
RUN apt install uvicorn gunicorn -y

WORKDIR /app
COPY . .

RUN python -m pip install --upgrade pip
RUN pip install poetry
RUN poetry install --no-root --no-interaction

# make pipenv commands still running
RUN ln /usr/local/bin/poetry /usr/local/bin/pipenv

CMD poetry run gunicorn -b 0.0.0.0:8000 -w 4 -k uvicorn.workers.UvicornWorker main:app --timeout 120
2 changes: 1 addition & 1 deletion dbsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

_logger = logging.getLogger(__name__)

engine = create_async_engine(os.getenv("SQL_URI", "postgresql+asyncpg://127.0.0.1:5432"), pool_pre_ping=True, echo=False)
engine = create_async_engine(os.getenv("SQL_URI", "postgresql+asyncpg://127.0.0.1:5432"), pool_pre_ping=True, echo=True)
Base = declarative_base()

session_maker = sessionmaker(engine)
Expand Down
139 changes: 120 additions & 19 deletions endpoints/get_address_transactions.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
# encoding: utf-8
import re
import time
from enum import Enum
from typing import List

from fastapi import Path, Query
from fastapi import Path, Query, HTTPException
from pydantic import BaseModel
from sqlalchemy import text, func
from sqlalchemy.future import select
from starlette.responses import Response

from dbsession import async_session
from endpoints import sql_db_only
from endpoints.get_transactions import search_for_transactions, TxSearch, TxModel
from models.AddressKnown import AddressKnown
from models.TxAddrMapping import TxAddrMapping
from server import app

DESC_RESOLVE_PARAM = "Use this parameter if you want to fetch the TransactionInput previous outpoint details." \
" Light fetches only the address and amount. Full fetches the whole TransactionOutput and " \
"adds it into each TxInput."

REGEX_KASPA_ADDRESS = "^kaspa(test)?\:[a-z0-9]{61,63}$"


class TransactionsReceivedAndSpent(BaseModel):
tx_received: str
Expand All @@ -32,6 +38,11 @@ class TransactionCount(BaseModel):
total: int


class AddressName(BaseModel):
address: str
name: str


class PreviousOutpointLookupMode(str, Enum):
no = "no"
light = "light"
Expand All @@ -41,14 +52,13 @@ class PreviousOutpointLookupMode(str, Enum):
@app.get("/addresses/{kaspaAddress}/transactions",
response_model=TransactionForAddressResponse,
response_model_exclude_unset=True,
tags=["Kaspa addresses"],
deprecated=True)
tags=["Kaspa addresses"])
@sql_db_only
async def get_transactions_for_address(
kaspaAddress: str = Path(
description="Kaspa address as string e.g. "
"kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
regex="^kaspa\:[a-z0-9]{61,63}$")):
regex=REGEX_KASPA_ADDRESS)):
"""
Get all transactions for a given address from database
"""
Expand All @@ -59,26 +69,27 @@ async def get_transactions_for_address(
# WHERE "script_public_key_address" = 'kaspa:qp7d7rzrj34s2k3qlxmguuerfh2qmjafc399lj6606fc7s69l84h7mrj49hu6'
#
# ORDER by transactions_outputs.transaction_id
kaspaAddress = re.sub(r"^kaspa(test)?:", "",
kaspaAddress) # Custom query bypasses the TypeDecorator, must handle it manually
async with async_session() as session:
resp = await session.execute(text(f"""
SELECT transactions_outputs.transaction_id, transactions_outputs.index, transactions_inputs.transaction_id as inp_transaction,
transactions.block_time, transactions.transaction_id

FROM transactions
LEFT JOIN transactions_outputs ON transactions.transaction_id = transactions_outputs.transaction_id
LEFT JOIN transactions_inputs ON transactions_inputs.previous_outpoint_hash = transactions.transaction_id AND transactions_inputs.previous_outpoint_index = transactions_outputs.index
WHERE "script_public_key_address" = :kaspaAddress
ORDER by transactions.block_time DESC
LIMIT 500"""),
{'kaspaAddress': kaspaAddress})
resp = await session.execute(
text(f"""
SELECT o.transaction_id, i.transaction_id
FROM transactions t
LEFT JOIN transactions_outputs o ON t.transaction_id = o.transaction_id
LEFT JOIN transactions_inputs i ON i.previous_outpoint_hash = t.transaction_id AND i.previous_outpoint_index = o.index
WHERE o.script_public_key_address = :kaspaAddress
ORDER by t.block_time DESC
LIMIT 500"""),
{'kaspaAddress': kaspaAddress})

resp = resp.all()

# build response
tx_list = []
for x in resp:
tx_list.append({"tx_received": x[0],
"tx_spent": x[2]})
tx_list.append({"tx_received": x[0].hex() if x[0] is not None else None,
"tx_spent": x[1].hex() if x[1] is not None else None})
return {
"transactions": tx_list
}
Expand All @@ -93,7 +104,7 @@ async def get_full_transactions_for_address(
kaspaAddress: str = Path(
description="Kaspa address as string e.g. "
"kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
regex="^kaspa\:[a-z0-9]{61,63}$"),
regex=REGEX_KASPA_ADDRESS),
limit: int = Query(
description="The number of records to get",
ge=1,
Expand Down Expand Up @@ -129,6 +140,70 @@ async def get_full_transactions_for_address(
resolve_previous_outpoints)


@app.get("/addresses/{kaspaAddress}/full-transactions-page",
response_model=List[TxModel],
response_model_exclude_unset=True,
tags=["Kaspa addresses"])
@sql_db_only
async def get_full_transactions_for_address_page(
response: Response,
kaspaAddress: str = Path(
description="Kaspa address as string e.g. "
"kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
regex=REGEX_KASPA_ADDRESS),
limit: int = Query(
description="The max number of records to get. "
"For paging combine with using 'before' from oldest previous result, "
"repeat until an **empty** resultset is returned."
"The actual number of transactions returned can be higher if there are transactions with the same block time at the limit.",
ge=1,
le=500,
default=50),
before: int = Query(
description="Only include transactions with block time before this (epoch-millis)",
ge=0,
default=0),
fields: str = "",
resolve_previous_outpoints: PreviousOutpointLookupMode =
Query(default="no",
description=DESC_RESOLVE_PARAM)):
"""
Get all transactions for a given address from database.
And then get their related full transaction data
"""

async with async_session() as s:
# Doing it this way as opposed to adding it directly in the IN clause
# so I can re-use the same result in tx_list, TxInput and TxOutput
before = int(time.time() * 1000) if before == 0 else before
tx_within_limit_before = await s.execute(select(TxAddrMapping.transaction_id,
TxAddrMapping.block_time)
.filter(TxAddrMapping.address == kaspaAddress)
.filter(TxAddrMapping.block_time < before)
.limit(limit)
.order_by(TxAddrMapping.block_time.desc())
)

tx_ids_and_block_times = [(x.transaction_id, x.block_time) for x in tx_within_limit_before.all()]
tx_ids = {tx_id for tx_id, block_time in tx_ids_and_block_times}
oldest_block_time = tx_ids_and_block_times[-1][1]

if len(tx_ids_and_block_times) == limit:
# To avoid gaps when transactions with the same block_time are at the boundry between pages.
# Get the time of the last transaction and fetch additional transactions for the same address and timestamp
tx_with_same_block_time = await s.execute(select(TxAddrMapping.transaction_id)
.filter(TxAddrMapping.address == kaspaAddress)
.filter(TxAddrMapping.block_time == oldest_block_time))
tx_ids.update([x for x in tx_with_same_block_time.scalars().all()])

response.headers["X-Current-Page"] = str(len(tx_ids))
response.headers["X-Oldest-Epoch-Millis"] = str(oldest_block_time)

return await search_for_transactions(TxSearch(transactionIds=list(tx_ids)),
fields,
resolve_previous_outpoints)


@app.get("/addresses/{kaspaAddress}/transactions-count",
response_model=TransactionCount,
tags=["Kaspa addresses"])
Expand All @@ -137,7 +212,7 @@ async def get_transaction_count_for_address(
kaspaAddress: str = Path(
description="Kaspa address as string e.g. "
"kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
regex="^kaspa\:[a-z0-9]{61,63}$")
regex=REGEX_KASPA_ADDRESS)
):
"""
Count the number of transactions associated with this address
Expand All @@ -149,3 +224,29 @@ async def get_transaction_count_for_address(
tx_count = await s.execute(count_query)

return TransactionCount(total=tx_count.scalar())


@app.get("/addresses/{kaspaAddress}/name",
response_model=AddressName | None,
tags=["Kaspa addresses"])
@sql_db_only
async def get_name_for_address(
response: Response,
kaspaAddress: str = Path(description="Kaspa address as string e.g. "
"kaspa:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqkx9awp4e",
regex=REGEX_KASPA_ADDRESS)
):
"""
Get the name for an address
"""
async with async_session() as s:
r = (await s.execute(
select(AddressKnown)
.filter(AddressKnown.address == kaspaAddress))).first()

response.headers["Cache-Control"] = "public, max-age=600"
if r:
return AddressName(address=r.AddressKnown.address, name=r.AddressKnown.name)
else:
raise HTTPException(status_code=404, detail="Address name not found",
headers={"Cache-Control": "public, max-age=600"})
3 changes: 2 additions & 1 deletion endpoints/get_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from server import app, kaspad_client

REGEX_KASPA_ADDRESS = "^kaspa(test)?\:[a-z0-9]{61,63}$"

class BalanceResponse(BaseModel):
address: str = "kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00"
Expand All @@ -15,7 +16,7 @@ class BalanceResponse(BaseModel):
async def get_balance_from_kaspa_address(
kaspaAddress: str = Path(
description="Kaspa address as string e.g. kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
regex="^kaspa\:[a-z0-9]{61,63}$")):
regex=REGEX_KASPA_ADDRESS)):
"""
Get balance for a given kaspa address
"""
Expand Down
Loading
Loading