Skip to content

Commit

Permalink
added qx; added docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
0xluk committed Sep 4, 2024
1 parent 8f31940 commit b7a7247
Show file tree
Hide file tree
Showing 30 changed files with 2,396 additions and 187 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/push-docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Deploy prod images to GHCR

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
push-store-image:
runs-on: ubuntu-latest
steps:
- name: 'Checkout GitHub Action'
uses: actions/checkout@main

- name: 'Login to GitHub Container Registry'
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{github.actor}}
password: ${{secrets.GITHUB_TOKEN}}

- name: 'Build Inventory Image'
run: |
docker build . --tag ghcr.io/qubic/go-qubic:${{github.ref_name}}
docker push ghcr.io/qubic/go-qubic:${{github.ref_name}}
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:1.22 AS builder
ENV CGO_ENABLED=0

WORKDIR /src
COPY . /src

RUN go mod tidy
RUN go build -o "/src/bin/go-node-proxy"

# We don't need golang to run binaries, just use alpine.
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y ca-certificates
COPY --from=builder /src/bin/go-node-proxy /app/go-node-proxy
RUN chmod +x /app/go-node-proxy

EXPOSE 8000
EXPOSE 8001

WORKDIR /app

ENTRYPOINT ["./go-node-proxy"]
110 changes: 110 additions & 0 deletions app/qubic-node-proxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package main

import (
"fmt"
"github.com/pkg/errors"
"github.com/qubic/go-qubic/internal/connector"
"github.com/qubic/go-qubic/server"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/ardanlabs/conf"
)

const prefix = "QUBIC_NODE_PROXY"

func main() {
if err := run(); err != nil {
log.Fatalf("main: exited with error: %s", err.Error())
}
}

func run() error {
var cfg struct {
Server struct {
HttpListenAddr string `conf:"default:0.0.0.0:8000"`
GrpcListenAddr string `conf:"default:0.0.0.0:8001"`
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:5s"`
ShutdownTimeout time.Duration `conf:"default:5s"`
}
Pool struct {
NodeFetcherUrl string `conf:"default:http://127.0.0.1:8080/status"`
NodeFetcherTimeout time.Duration `conf:"default:5s"`
InitialCap int `conf:"default:5"`
MaxIdle int `conf:"default:6"`
MaxCap int `conf:"default:10"`
IdleTimeout time.Duration `conf:"default:15s"`
}
NodeConnector struct {
ConnectionPort string `conf:"default:21841"`
ConnectionTimeout time.Duration `conf:"default:5s"`
HandlerRequestTimeout time.Duration `conf:"default:5s"`
}
}

if err := conf.Parse(os.Args[1:], prefix, &cfg); err != nil {
switch {
case errors.Is(err, conf.ErrHelpWanted):
usage, err := conf.Usage(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case errors.Is(err, conf.ErrVersionWanted):
version, err := conf.VersionString(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config version")
}
fmt.Println(version)
return nil
}
return errors.Wrap(err, "parsing config")
}

out, err := conf.String(&cfg)
if err != nil {
return errors.Wrap(err, "generating config for output")
}
log.Printf("main: Config :\n%v\n", out)

pfConfig := connector.PoolFetcherConfig{
URL: cfg.Pool.NodeFetcherUrl,
RequestTimeout: cfg.Pool.NodeFetcherTimeout,
}
cConfig := connector.Config{
ConnectionPort: cfg.NodeConnector.ConnectionPort,
ConnectionTimeout: cfg.NodeConnector.ConnectionTimeout,
HandlerRequestTimeout: cfg.NodeConnector.HandlerRequestTimeout,
}
pConfig := connector.PoolConfig{
InitialCap: cfg.Pool.InitialCap,
MaxCap: cfg.Pool.MaxCap,
MaxIdle: cfg.Pool.MaxIdle,
IdleTimeout: cfg.Pool.IdleTimeout,
}
conn, err := connector.NewPoolConnector(pfConfig, cConfig, pConfig)
if err != nil {
return errors.Wrap(err, "creating pool connector")
}

srv := server.NewServer(cfg.Server.GrpcListenAddr, cfg.Server.HttpListenAddr, conn)
err = srv.Start()
if err != nil {
return errors.Wrap(err, "starting server")
}

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)

for {
select {
case <-shutdown:
return errors.New("shutting down")
}
}
}
5 changes: 0 additions & 5 deletions clients/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"github.com/pkg/errors"
"github.com/qubic/go-qubic/clients/core/nodetypes"
"github.com/qubic/go-qubic/clients/quottery"
"github.com/qubic/go-qubic/common"
"github.com/qubic/go-qubic/internal/connector"
qubicpb "github.com/qubic/go-qubic/proto/v1"
Expand All @@ -20,10 +19,6 @@ func NewClient(connector *connector.Connector) *Client {
}
}

func (c *Client) QuotteryClient() *quottery.Client {
return quottery.NewClient(c.connector)
}

func (c *Client) GetTickInfo(ctx context.Context) (*qubicpb.TickInfo, error) {
var result nodetypes.TickInfo

Expand Down
2 changes: 0 additions & 2 deletions clients/core/nodetypes/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type TickData struct {
Day uint8
Month uint8
Year uint8
UnionData [256]byte
Timelock [32]byte
TransactionDigests [NumberOfTransactionsPerTick][32]byte
ContractFees [1024]int64
Expand Down Expand Up @@ -105,7 +104,6 @@ func (tdc *tickDataConverter) toProto() (*qubicpb.TickData, error) {
Epoch: uint32(tdc.rawTd.Epoch),
Tick: tdc.rawTd.Tick,
Timestamp: timestamppb.New(date),
VarStruct: base64.StdEncoding.EncodeToString(tdc.rawTd.UnionData[:]),
TimeLock: base64.StdEncoding.EncodeToString(tdc.rawTd.Timelock[:]),
TransactionIds: transactionIds,
ContractFees: contractFeesToProto(tdc.rawTd.ContractFees),
Expand Down
4 changes: 2 additions & 2 deletions clients/core/nodetypes/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (tx *Transaction) MarshallBinary() ([]byte, error) {
return buff.Bytes(), nil
}

func (tx *Transaction) UnmarshallBinary(r io.Reader) error {
func (tx *Transaction) UnmarshallFromReader(r io.Reader) error {
err := binary.Read(r, binary.LittleEndian, &tx.SourcePublicKey)
if err != nil {
return errors.Wrap(err, "reading source public key from reader")
Expand Down Expand Up @@ -226,7 +226,7 @@ func (txs *Transactions) UnmarshallFromReader(r io.Reader) error {

var tx Transaction

err = tx.UnmarshallBinary(r)
err = tx.UnmarshallFromReader(r)
if err != nil {
return errors.Wrap(err, "unmarshalling transaction")
}
Expand Down
2 changes: 1 addition & 1 deletion clients/core/nodetypes/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestTransaction_MarshallUnmarshall(t *testing.T) {
}

var unmarshalledTx Transaction
err = unmarshalledTx.UnmarshallBinary(bytes.NewReader(marshalled))
err = unmarshalledTx.UnmarshallFromReader(bytes.NewReader(marshalled))
if err != nil {
t.Fatalf("Got err when unmarshalling tx. err: %s", err.Error())
}
Expand Down
31 changes: 15 additions & 16 deletions clients/quottery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package quottery
import (
"context"
"github.com/pkg/errors"
"github.com/qubic/go-qubic/clients/quottery/nodetypes"
"github.com/qubic/go-qubic/common"
"github.com/qubic/go-qubic/internal/connector"
qubicpb "github.com/qubic/go-qubic/proto/v1"
Expand All @@ -21,12 +20,12 @@ func NewClient(connector *connector.Connector) *Client {

func (c *Client) GetBasicInfo(ctx context.Context) (*qubicpb.BasicInfo, error) {
rcf := connector.RequestContractFunction{
ContractIndex: nodetypes.QuotteryContractID,
InputType: nodetypes.ViewID.BasicInfo,
ContractIndex: contractID,
InputType: ViewID.BasicInfo,
InputSize: 0,
}

var result nodetypes.BasicInfo
var result BasicInfo
err := c.connector.PerformSmartContractRequest(ctx, rcf, nil, &result)
if err != nil {
return nil, errors.Wrap(err, "performing smart contract request")
Expand All @@ -42,8 +41,8 @@ func (c *Client) GetBasicInfo(ctx context.Context) (*qubicpb.BasicInfo, error) {

func (c *Client) GetBetInfo(ctx context.Context, betID uint32) (*qubicpb.BetInfo, error) {
rcf := connector.RequestContractFunction{
ContractIndex: nodetypes.QuotteryContractID,
InputType: nodetypes.ViewID.BetInfo,
ContractIndex: contractID,
InputType: ViewID.BetInfo,
InputSize: 4, // sizeof betID; uint32
}

Expand All @@ -53,7 +52,7 @@ func (c *Client) GetBetInfo(ctx context.Context, betID uint32) (*qubicpb.BetInfo
BetID: betID,
}

var result nodetypes.BetInfo
var result BetInfo
err := c.connector.PerformSmartContractRequest(ctx, rcf, request, &result)
if err != nil {
return nil, errors.Wrap(err, "performing smart contract request")
Expand All @@ -69,12 +68,12 @@ func (c *Client) GetBetInfo(ctx context.Context, betID uint32) (*qubicpb.BetInfo

func (c *Client) GetActiveBets(ctx context.Context) (*qubicpb.ActiveBets, error) {
rcf := connector.RequestContractFunction{
ContractIndex: nodetypes.QuotteryContractID,
InputType: nodetypes.ViewID.ActiveBet,
ContractIndex: contractID,
InputType: ViewID.ActiveBet,
InputSize: 0,
}

var result nodetypes.ActiveBets
var result ActiveBets
err := c.connector.PerformSmartContractRequest(ctx, rcf, nil, &result)
if err != nil {
return nil, errors.Wrap(err, "performing smart contract request")
Expand All @@ -87,8 +86,8 @@ func (c *Client) GetActiveBets(ctx context.Context) (*qubicpb.ActiveBets, error)

func (c *Client) GetActiveBetsByCreator(ctx context.Context, creatorID common.Identity) (*qubicpb.ActiveBets, error) {
rcf := connector.RequestContractFunction{
ContractIndex: nodetypes.QuotteryContractID,
InputType: nodetypes.ViewID.ActiveBetByCreator,
ContractIndex: contractID,
InputType: ViewID.ActiveBetByCreator,
InputSize: 32,
}

Expand All @@ -103,7 +102,7 @@ func (c *Client) GetActiveBetsByCreator(ctx context.Context, creatorID common.Id
CreatorPubKey: creatorPubKey,
}

var result nodetypes.ActiveBets
var result ActiveBets
err = c.connector.PerformSmartContractRequest(ctx, rcf, request, &result)
if err != nil {
return nil, errors.Wrap(err, "performing smart contract request")
Expand All @@ -116,8 +115,8 @@ func (c *Client) GetActiveBetsByCreator(ctx context.Context, creatorID common.Id

func (c *Client) GetBettorsByBetOption(ctx context.Context, betID, betOption uint32) (*qubicpb.BetOptionBettors, error) {
rcf := connector.RequestContractFunction{
ContractIndex: nodetypes.QuotteryContractID,
InputType: nodetypes.ViewID.BetDetail,
ContractIndex: contractID,
InputType: ViewID.BetDetail,
InputSize: 8, // sizeof betID + betOptions; 2 * uint32
}

Expand All @@ -129,7 +128,7 @@ func (c *Client) GetBettorsByBetOption(ctx context.Context, betID, betOption uin
BetOption: betOption,
}

var result nodetypes.BetOptionDetail
var result BetOptionDetail
err := c.connector.PerformSmartContractRequest(ctx, rcf, request, &result)
if err != nil {
return nil, errors.Wrap(err, "performing smart contract request")
Expand Down
9 changes: 4 additions & 5 deletions clients/quottery/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package quottery

import (
"github.com/pkg/errors"
"github.com/qubic/go-qubic/clients/quottery/nodetypes"
"github.com/qubic/go-qubic/common"
qubicpb "github.com/qubic/go-qubic/proto/v1"
"google.golang.org/protobuf/types/known/timestamppb"
Expand All @@ -13,7 +12,7 @@ var BetInfoConverter betInfoConverter

type betInfoConverter struct{}

func (bic betInfoConverter) ToProto(bi nodetypes.BetInfo) (*qubicpb.BetInfo, error) {
func (bic betInfoConverter) ToProto(bi BetInfo) (*qubicpb.BetInfo, error) {
var creatorID common.Identity
err := creatorID.FromPubKey(bi.Creator, false)
if err != nil {
Expand Down Expand Up @@ -117,7 +116,7 @@ var ActiveBetsConverter activeBetsConverter

type activeBetsConverter struct{}

func (abc activeBetsConverter) ToProto(ab nodetypes.ActiveBets) *qubicpb.ActiveBets {
func (abc activeBetsConverter) ToProto(ab ActiveBets) *qubicpb.ActiveBets {
betIDs := make([]uint32, 0, ab.Count)

for i := 0; i < int(ab.Count); i++ {
Expand All @@ -133,7 +132,7 @@ var BasicInfoConverter basicInfoConverter

type basicInfoConverter struct{}

func (bic basicInfoConverter) ToProto(bi nodetypes.BasicInfo) (*qubicpb.BasicInfo, error) {
func (bic basicInfoConverter) ToProto(bi BasicInfo) (*qubicpb.BasicInfo, error) {
gameOperatorID, err := common.PubKeyToIdentity(bi.GameOperatorPubKey)
if err != nil {
return nil, errors.Wrapf(err, "converting game operator id with pubkey: %s", bi.GameOperatorPubKey)
Expand Down Expand Up @@ -169,7 +168,7 @@ var BetOptionBettorsConverter betOptionBettorsConverter

type betOptionBettorsConverter struct{}

func (bobc betOptionBettorsConverter) ToProto(bod nodetypes.BetOptionDetail) (*qubicpb.BetOptionBettors, error) {
func (bobc betOptionBettorsConverter) ToProto(bod BetOptionDetail) (*qubicpb.BetOptionBettors, error) {
bettorIDs := make([]string, 0)

// declare once to avoid unnecessary memory allocation
Expand Down
Loading

0 comments on commit b7a7247

Please sign in to comment.