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

Refactoring: change ORM, testing and migrate to timescale #995

Merged
merged 74 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
e8c8057
Change ORM to Bun
aopoltorzhicky Sep 23, 2023
f993afd
Add storage tests
aopoltorzhicky Sep 24, 2023
efc343b
Refactoring: rollback
aopoltorzhicky Sep 24, 2023
c3b44b9
Refactoring: saving block and migrations
aopoltorzhicky Sep 24, 2023
afe4190
Remove Save function from model interface
aopoltorzhicky Sep 24, 2023
4671b35
Refactoring: protocol migrations
aopoltorzhicky Sep 25, 2023
e42930f
Tests: protocol migration postgres functions
aopoltorzhicky Sep 25, 2023
0472b0d
Fix: rollback
aopoltorzhicky Sep 25, 2023
0d73dd0
Fix: rollback protocol
aopoltorzhicky Sep 25, 2023
2060d3a
Refactoring: saving accounts
aopoltorzhicky Sep 25, 2023
b080be1
Refactoring: Contract.tx_count -> Account.operations_count
aopoltorzhicky Sep 26, 2023
5fd9d8a
Timescale
aopoltorzhicky Sep 26, 2023
7923be7
Feature: add stats model
aopoltorzhicky Sep 26, 2023
d8553ea
Extend stats
aopoltorzhicky Sep 26, 2023
3d1654d
Refactoring: remove unused functions
aopoltorzhicky Sep 26, 2023
1a38e14
Refactoring: operations API
aopoltorzhicky Sep 26, 2023
9e47b10
Account statistics
aopoltorzhicky Sep 27, 2023
f97d496
Feature: add ticket model
aopoltorzhicky Sep 27, 2023
079890f
Feature: add ticket balances
aopoltorzhicky Sep 28, 2023
af61715
Fix: create extension once
aopoltorzhicky Sep 28, 2023
62ea122
Fix: change varchar to text
aopoltorzhicky Sep 28, 2023
61502bc
Fix: register models
aopoltorzhicky Sep 28, 2023
b6ada5c
Fix: register m2m models
aopoltorzhicky Sep 28, 2023
7fd6556
Fix: smart rollup count
aopoltorzhicky Sep 28, 2023
028a10e
Fix: save big map states
aopoltorzhicky Sep 28, 2023
2969531
Feature: add ticket balance endpoint
aopoltorzhicky Sep 28, 2023
32a952f
Fix: content type for ticket balance
aopoltorzhicky Sep 28, 2023
65ad2d9
Fix: response in ticket balances
aopoltorzhicky Sep 28, 2023
a55c4bf
Fix: rollback test
aopoltorzhicky Sep 28, 2023
bb8fca2
Remove concurrent index creation
aopoltorzhicky Sep 28, 2023
9b8f475
Fix: one ticket to all updates
aopoltorzhicky Sep 28, 2023
9f85e19
Fix: ticket hash
aopoltorzhicky Sep 28, 2023
0ed3593
Fix: tests
aopoltorzhicky Sep 28, 2023
bd32538
Fix: index creation
aopoltorzhicky Sep 29, 2023
54ea49a
Fix and optimize babylon migration
aopoltorzhicky Sep 29, 2023
ace8508
Fix and optimize babylon migration
aopoltorzhicky Sep 29, 2023
bbd13a9
Remove bmd repo from babylon migration
aopoltorzhicky Sep 29, 2023
6ab775d
Change test data for babylon migration test
aopoltorzhicky Sep 29, 2023
6dab1c6
Fix: tx_rollup_origination
aopoltorzhicky Sep 30, 2023
4e6abfa
Fix: recently called contracts endpoint
aopoltorzhicky Oct 1, 2023
c666e50
Fix: operations count for account
aopoltorzhicky Oct 1, 2023
b399bdb
Fix: parsing node error
aopoltorzhicky Oct 1, 2023
1ffe875
Fix: smart rollup accounts counting
aopoltorzhicky Oct 1, 2023
a0ef554
Fix: failed originations
aopoltorzhicky Oct 2, 2023
81816dc
Fix: saving origination accounts
aopoltorzhicky Oct 2, 2023
111b947
Fix: same API
aopoltorzhicky Oct 4, 2023
2b90b46
Disable oxfordnet
aopoltorzhicky Oct 5, 2023
abd10a2
Upgrade to go1.21
aopoltorzhicky Oct 7, 2023
c9131dd
Add fromatter tests for new primitives
aopoltorzhicky Oct 8, 2023
ca8023f
Cache scripts
aopoltorzhicky Oct 8, 2023
9a0e26f
Create contract indexes
aopoltorzhicky Oct 8, 2023
4cd71f3
Add codeql
aopoltorzhicky Oct 8, 2023
f4f2aab
Add codeql
aopoltorzhicky Oct 8, 2023
419ac4c
Remove codeql
aopoltorzhicky Oct 8, 2023
7c9faf8
Linter update and some API improvements
aopoltorzhicky Oct 8, 2023
495a4a3
Fix: linter job
aopoltorzhicky Oct 8, 2023
7417031
Refactoring: logger
aopoltorzhicky Oct 8, 2023
06d9369
Fix: recently called contracts
aopoltorzhicky Oct 10, 2023
d780ab9
Fix: global constants per network API
aopoltorzhicky Oct 10, 2023
119dedc
Fix: add gin gonic timeout
aopoltorzhicky Oct 10, 2023
1a707ee
Fix: post-creating index concurrently
aopoltorzhicky Oct 10, 2023
547c2e4
Fix: increase timeout
aopoltorzhicky Oct 10, 2023
e525f8f
Add log
aopoltorzhicky Oct 10, 2023
6e57b83
Fix: gin timeout
aopoltorzhicky Oct 10, 2023
3665cff
Fix: add GetContractTickets endpoint
aopoltorzhicky Oct 13, 2023
d67c1da
Optimization of GetLastAction query
aopoltorzhicky Oct 13, 2023
f12193b
Fix: global constants query
aopoltorzhicky Oct 13, 2023
0bbe309
Fix: last action in same API
aopoltorzhicky Oct 13, 2023
42edd01
Optimize last query
aopoltorzhicky Oct 13, 2023
f76ef45
Fix: sanbox tagging
aopoltorzhicky Oct 15, 2023
0bdd391
Fix: errors scan
aopoltorzhicky Oct 15, 2023
374e718
Fix: errors
aopoltorzhicky Oct 15, 2023
133355e
Add filters to ticket updates endpoints
aopoltorzhicky Oct 15, 2023
c925903
Add ticket_id to ticket updates response
aopoltorzhicky Oct 15, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
context: .
file: build/sandbox/Dockerfile
build-args: |
TAG=4.6.2
TAG=4.6.3
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.2
version: v1.54.2
args: --timeout=3m
test:
name: Run tests
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.20'
go-version: '1.21'
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.2
version: v1.54.2
args: --timeout=3m
test:
name: Run tests
Expand Down
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ linters:
- ineffassign
- containedctx
- tenv
- musttag
- mirror
- tagalign
- zerologlint

run:
go: '1.19'
go: '1.21'
timeout: 3m
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BACKUP?=dump_latest.gz

api:
docker-compose up -d db
cd cmd/api && go run .
cd cmd/api && go run -tags=jsoniter .

indexer:
docker-compose up -d db
Expand Down Expand Up @@ -79,10 +79,10 @@ ps:
docker ps --format "table {{.Names}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}"

sandbox-pull:
TAG=4.4.0 docker-compose -f docker-compose.flextesa.yml pull
TAG_FRONT=4.6.2 TAG_BACK=4.7.2 docker-compose -f docker-compose.flextesa.yml pull

flextesa-sandbox:
COMPOSE_PROJECT_NAME=bcdbox TAG=4.6.2 docker-compose -f docker-compose.flextesa.yml up -d
COMPOSE_PROJECT_NAME=bcdbox TAG_FRONT=4.6.2 TAG_BACK=4.7.2 docker-compose -f docker-compose.flextesa.yml up -d

sandbox-down:
COMPOSE_PROJECT_NAME=bcdbox docker-compose -f docker-compose.flextesa.yml down
Expand Down
4 changes: 2 additions & 2 deletions build/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------------------
# The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.20-alpine as builder
FROM golang:1.21-alpine as builder

ENV CGO_ENABLED=0
ENV GO111MODULE=on
Expand All @@ -20,7 +20,7 @@ COPY cmd/api cmd/api
COPY internal internal

WORKDIR $GOPATH/src/github.com/baking-bad/bcdhub/cmd/api/
RUN go build -a -installsuffix cgo -o /go/bin/api .
RUN go build -tags=jsoniter -a -installsuffix cgo -o /go/bin/api .

WORKDIR $GOPATH/src/github.com/baking-bad/bcdhub
COPY scripts scripts
Expand Down
2 changes: 1 addition & 1 deletion build/indexer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------------------
# The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.20-alpine as builder
FROM golang:1.21-alpine as builder

ENV CGO_ENABLED=0
ENV GO111MODULE=on
Expand Down
27 changes: 13 additions & 14 deletions cmd/api/handlers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/baking-bad/bcdhub/internal/bcd"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/logger"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)

// GetInfo godoc
Expand All @@ -33,32 +33,31 @@ func GetInfo() gin.HandlerFunc {
return
}

acc, err := ctx.Accounts.Get(req.Address)
if handleError(c, ctx.Storage, err, 0) {
return
}
stats, err := ctx.Operations.ContractStats(acc.Address)
acc, err := ctx.Accounts.Get(c.Request.Context(), req.Address)
if handleError(c, ctx.Storage, err, 0) {
return
}

var balance int64
if !(bcd.IsRollupAddressLazy(acc.Address) || bcd.IsSmartRollupAddressLazy(acc.Address)) {
block, err := ctx.Blocks.Last()
block, err := ctx.Blocks.Last(c.Request.Context())
if handleError(c, ctx.Storage, err, 0) {
return
}
balance, err = ctx.Cache.TezosBalance(c, acc.Address, block.Level)
if err != nil {
logger.Err(err)
log.Err(err).Msg("receiving tezos balance")
}
}
c.SecureJSON(http.StatusOK, AccountInfo{
Address: acc.Address,
Alias: acc.Alias,
TxCount: stats.Count,
Balance: balance,
LastAction: stats.LastAction.UTC(),
AccountType: acc.Type.String(),
Address: acc.Address,
OperationsCount: acc.OperationsCount,
EventsCount: acc.EventsCount,
MigrationsCount: acc.MigrationsCount,
TicketUpdatesCount: acc.TicketUpdatesCount,
Balance: balance,
LastAction: acc.LastAction.UTC(),
AccountType: acc.Type.String(),
})
}

Expand Down
57 changes: 29 additions & 28 deletions cmd/api/handlers/bigmap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"context"
"fmt"
"net/http"

Expand Down Expand Up @@ -38,7 +39,7 @@ func GetBigMap() gin.HandlerFunc {
return
}

stats, err := ctx.BigMapDiffs.GetStats(req.Ptr)
stats, err := ctx.BigMapDiffs.GetStats(c.Request.Context(), req.Ptr)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand All @@ -52,29 +53,29 @@ func GetBigMap() gin.HandlerFunc {
}

if stats.Total == 0 {
actions, err := ctx.BigMapActions.Get(req.Ptr, 1, 0)
actions, err := ctx.BigMapActions.Get(c.Request.Context(), req.Ptr, 1, 0)
if handleError(c, ctx.Storage, err, 0) {
return
}
if len(actions) > 0 {
res.Address = actions[0].Address
}
} else {
destination, err := ctx.Accounts.Get(res.Address)
destination, err := ctx.Accounts.Get(c.Request.Context(), res.Address)
if handleError(c, ctx.Storage, err, 0) {
return
}
res.ContractAlias = destination.Alias

operation, err := ctx.Operations.Last(
c.Request.Context(),
map[string]interface{}{
"status": types.OperationStatusApplied,
"destination_id": destination.ID,
}, 0)
if handleError(c, ctx.Storage, err, 0) {
return
}
proto, err := ctx.Cache.ProtocolByID(operation.ProtocolID)
proto, err := ctx.Cache.ProtocolByID(c.Request.Context(), operation.ProtocolID)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand All @@ -94,7 +95,7 @@ func GetBigMap() gin.HandlerFunc {
return
}

script, err := ctx.Contracts.ScriptPart(res.Address, proto.SymLink, consts.STORAGE)
script, err := ctx.Contracts.ScriptPart(c.Request.Context(), res.Address, proto.SymLink, consts.STORAGE)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand Down Expand Up @@ -146,7 +147,7 @@ func GetBigMapHistory() gin.HandlerFunc {
return
}

bm, err := ctx.BigMapActions.Get(req.Ptr, 0, 0)
bm, err := ctx.BigMapActions.Get(c.Request.Context(), req.Ptr, 0, 0)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand Down Expand Up @@ -189,7 +190,7 @@ func GetBigMapKeys() gin.HandlerFunc {
return
}

keys, err := ctx.BigMapDiffs.Keys(bigmapdiff.GetContext{
keys, err := ctx.BigMapDiffs.Keys(c.Request.Context(), bigmapdiff.GetContext{
Ptr: &req.Ptr,
Size: pageReq.Size,
Offset: pageReq.Offset,
Expand All @@ -201,12 +202,12 @@ func GetBigMapKeys() gin.HandlerFunc {
return
}

symLink, err := getCurrentSymLink(ctx.Blocks)
symLink, err := getCurrentSymLink(c.Request.Context(), ctx.Blocks)
if handleError(c, ctx.Storage, err, 0) {
return
}

response, err := prepareBigMapKeys(ctx, keys, symLink)
response, err := prepareBigMapKeys(c.Request.Context(), ctx, keys, symLink)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand Down Expand Up @@ -246,7 +247,7 @@ func GetBigMapByKeyHash() gin.HandlerFunc {
return
}

bm, total, err := ctx.BigMapDiffs.GetByPtrAndKeyHash(req.Ptr, req.KeyHash, pageReq.Size, pageReq.Offset)
bm, total, err := ctx.BigMapDiffs.GetByPtrAndKeyHash(c.Request.Context(), req.Ptr, req.KeyHash, pageReq.Size, pageReq.Offset)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand All @@ -255,12 +256,12 @@ func GetBigMapByKeyHash() gin.HandlerFunc {
c.SecureJSON(http.StatusNoContent, gin.H{})
return
}
symLink, err := getCurrentSymLink(ctx.Blocks)
symLink, err := getCurrentSymLink(c.Request.Context(), ctx.Blocks)
if handleError(c, ctx.Storage, err, 0) {
return
}

response, err := prepareBigMapItem(ctx, bm, req.KeyHash, symLink)
response, err := prepareBigMapItem(c.Request.Context(), ctx, bm, req.KeyHash, symLink)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand Down Expand Up @@ -294,17 +295,17 @@ func GetCurrentBigMapKeyHash() gin.HandlerFunc {
return
}

state, err := ctx.BigMapDiffs.Current(req.KeyHash, req.Ptr)
state, err := ctx.BigMapDiffs.Current(c.Request.Context(), req.KeyHash, req.Ptr)
if handleError(c, ctx.Storage, err, 0) {
return
}

symLink, err := getCurrentSymLink(ctx.Blocks)
symLink, err := getCurrentSymLink(c.Request.Context(), ctx.Blocks)
if handleError(c, ctx.Storage, err, 0) {
return
}

bigMapType, err := getBigMapType(ctx, state.Contract, state.Ptr, symLink)
bigMapType, err := getBigMapType(c.Request.Context(), ctx, state.Contract, state.Ptr, symLink)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand Down Expand Up @@ -348,7 +349,7 @@ func GetBigMapDiffCount() gin.HandlerFunc {
return
}

count, err := ctx.BigMapDiffs.Count(req.Ptr)
count, err := ctx.BigMapDiffs.Count(c.Request.Context(), req.Ptr)
if err != nil {
if ctx.Storage.IsRecordNotFound(err) {
c.SecureJSON(http.StatusOK, CountResponse{})
Expand All @@ -357,16 +358,16 @@ func GetBigMapDiffCount() gin.HandlerFunc {
handleError(c, ctx.Storage, err, 0)
return
}
c.SecureJSON(http.StatusOK, CountResponse{count})
c.SecureJSON(http.StatusOK, CountResponse{int64(count)})
}
}

func prepareBigMapKeys(ctx *config.Context, data []bigmapdiff.BigMapState, symLink string) ([]BigMapResponseItem, error) {
func prepareBigMapKeys(c context.Context, ctx *config.Context, data []bigmapdiff.BigMapState, symLink string) ([]BigMapResponseItem, error) {
if len(data) == 0 {
return []BigMapResponseItem{}, nil
}

bigMapType, err := getBigMapType(ctx, data[0].Contract, data[0].Ptr, symLink)
bigMapType, err := getBigMapType(c, ctx, data[0].Contract, data[0].Ptr, symLink)
if err != nil {
return nil, err
}
Expand All @@ -393,12 +394,12 @@ func prepareBigMapKeys(ctx *config.Context, data []bigmapdiff.BigMapState, symLi
return res, nil
}

func prepareBigMapItem(ctx *config.Context, data []bigmapdiff.BigMapDiff, keyHash, symLink string) (res BigMapDiffByKeyResponse, err error) {
func prepareBigMapItem(c context.Context, ctx *config.Context, data []bigmapdiff.BigMapDiff, keyHash, symLink string) (res BigMapDiffByKeyResponse, err error) {
if len(data) == 0 {
return
}

bigMapType, err := getBigMapType(ctx, data[0].Contract, data[0].Ptr, symLink)
bigMapType, err := getBigMapType(c, ctx, data[0].Contract, data[0].Ptr, symLink)
if err != nil {
return
}
Expand All @@ -424,13 +425,13 @@ func prepareBigMapItem(ctx *config.Context, data []bigmapdiff.BigMapDiff, keyHas
return
}

func getBigMapType(ctx *config.Context, contract string, ptr int64, symLink string) (*ast.BigMap, error) {
storageType, err := getStorageType(ctx.Contracts, contract, symLink)
func getBigMapType(c context.Context, ctx *config.Context, contract string, ptr int64, symLink string) (*ast.BigMap, error) {
storageType, err := getStorageType(c, ctx.Contracts, contract, symLink)
if err != nil {
return nil, err
}

actions, err := ctx.BigMapActions.Get(ptr, 2, 0)
actions, err := ctx.BigMapActions.Get(c, ptr, 2, 0)
if err != nil {
return nil, err
}
Expand All @@ -443,18 +444,18 @@ func getBigMapType(ctx *config.Context, contract string, ptr int64, symLink stri
operationID = actions[1].OperationID
}
}
operation, err := ctx.Operations.GetByID(operationID)
operation, err := ctx.Operations.GetByID(c, operationID)
if err != nil {
return nil, err
}
deffatedStorage = operation.DeffatedStorage
} else {
contract, err := ctx.Accounts.Get(contract)
contract, err := ctx.Accounts.Get(c, contract)
if err != nil {
return nil, err
}

operation, err := ctx.Operations.Last(map[string]interface{}{
operation, err := ctx.Operations.Last(c, map[string]interface{}{
"destination_id": contract.ID,
"status": types.OperationStatusApplied,
}, 0)
Expand Down
Loading