Skip to content

Commit 0575724

Browse files
Refactoring: change ORM, testing and migrate to timescale (#995)
* Change ORM to Bun * Add storage tests * Refactoring: rollback * Refactoring: saving block and migrations * Remove Save function from model interface * Refactoring: protocol migrations * Tests: protocol migration postgres functions * Fix: rollback * Fix: rollback protocol * Refactoring: saving accounts * Refactoring: Contract.tx_count -> Account.operations_count * Timescale * Feature: add stats model * Extend stats * Refactoring: remove unused functions * Refactoring: operations API * Account statistics * Feature: add ticket model * Feature: add ticket balances * Fix: create extension once * Fix: change varchar to text * Fix: register models * Fix: register m2m models * Fix: smart rollup count * Fix: save big map states * Feature: add ticket balance endpoint * Fix: content type for ticket balance * Fix: response in ticket balances * Fix: rollback test * Remove concurrent index creation * Fix: one ticket to all updates * Fix: ticket hash * Fix: tests * Fix: index creation * Fix and optimize babylon migration * Fix and optimize babylon migration * Remove bmd repo from babylon migration * Change test data for babylon migration test * Fix: tx_rollup_origination * Fix: recently called contracts endpoint * Fix: operations count for account * Fix: parsing node error * Fix: smart rollup accounts counting * Fix: failed originations * Fix: saving origination accounts * Fix: same API * Disable oxfordnet * Upgrade to go1.21 * Add fromatter tests for new primitives * Cache scripts * Create contract indexes * Add codeql * Add codeql * Remove codeql * Linter update and some API improvements * Fix: linter job * Refactoring: logger * Fix: recently called contracts * Fix: global constants per network API * Fix: add gin gonic timeout * Fix: post-creating index concurrently * Fix: increase timeout * Add log * Fix: gin timeout * Fix: add GetContractTickets endpoint * Optimization of GetLastAction query * Fix: global constants query * Fix: last action in same API * Optimize last query * Fix: sanbox tagging * Fix: errors scan * Fix: errors * Add filters to ticket updates endpoints * Add ticket_id to ticket updates response
1 parent d282d8d commit 0575724

File tree

258 files changed

+25349
-5659
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

258 files changed

+25349
-5659
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
context: .
4747
file: build/sandbox/Dockerfile
4848
build-args: |
49-
TAG=4.6.2
49+
TAG=4.6.3
5050
push: true
5151
cache-from: type=gha
5252
cache-to: type=gha,mode=max

.github/workflows/tests_master.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ jobs:
1111
steps:
1212
- uses: actions/setup-go@v3
1313
with:
14-
go-version: '1.20'
14+
go-version: '1.21'
1515
- uses: actions/checkout@v3
1616
- name: golangci-lint
1717
uses: golangci/golangci-lint-action@v3
1818
with:
19-
version: v1.51.2
19+
version: v1.54.2
2020
args: --timeout=3m
2121
test:
2222
name: Run tests

.github/workflows/tests_pr.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ jobs:
1111
steps:
1212
- uses: actions/setup-go@v3
1313
with:
14-
go-version: '1.20'
14+
go-version: '1.21'
1515
- uses: actions/checkout@v3
1616
- name: golangci-lint
1717
uses: golangci/golangci-lint-action@v3
1818
with:
19-
version: v1.51.2
19+
version: v1.54.2
2020
args: --timeout=3m
2121
test:
2222
name: Run tests

.golangci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ linters:
1111
- ineffassign
1212
- containedctx
1313
- tenv
14+
- musttag
15+
- mirror
16+
- tagalign
17+
- zerologlint
1418

1519
run:
16-
go: '1.19'
20+
go: '1.21'
1721
timeout: 3m

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ BACKUP?=dump_latest.gz
88

99
api:
1010
docker-compose up -d db
11-
cd cmd/api && go run .
11+
cd cmd/api && go run -tags=jsoniter .
1212

1313
indexer:
1414
docker-compose up -d db
@@ -79,10 +79,10 @@ ps:
7979
docker ps --format "table {{.Names}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}"
8080

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

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

8787
sandbox-down:
8888
COMPOSE_PROJECT_NAME=bcdbox docker-compose -f docker-compose.flextesa.yml down

build/api/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ---------------------------------------------------------------------
22
# The first stage container, for building the application
33
# ---------------------------------------------------------------------
4-
FROM golang:1.20-alpine as builder
4+
FROM golang:1.21-alpine as builder
55

66
ENV CGO_ENABLED=0
77
ENV GO111MODULE=on
@@ -20,7 +20,7 @@ COPY cmd/api cmd/api
2020
COPY internal internal
2121

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

2525
WORKDIR $GOPATH/src/github.com/baking-bad/bcdhub
2626
COPY scripts scripts

build/indexer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ---------------------------------------------------------------------
22
# The first stage container, for building the application
33
# ---------------------------------------------------------------------
4-
FROM golang:1.20-alpine as builder
4+
FROM golang:1.21-alpine as builder
55

66
ENV CGO_ENABLED=0
77
ENV GO111MODULE=on

cmd/api/handlers/account.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55

66
"github.com/baking-bad/bcdhub/internal/bcd"
77
"github.com/baking-bad/bcdhub/internal/config"
8-
"github.com/baking-bad/bcdhub/internal/logger"
98
"github.com/gin-gonic/gin"
9+
"github.com/rs/zerolog/log"
1010
)
1111

1212
// GetInfo godoc
@@ -33,32 +33,31 @@ func GetInfo() gin.HandlerFunc {
3333
return
3434
}
3535

36-
acc, err := ctx.Accounts.Get(req.Address)
37-
if handleError(c, ctx.Storage, err, 0) {
38-
return
39-
}
40-
stats, err := ctx.Operations.ContractStats(acc.Address)
36+
acc, err := ctx.Accounts.Get(c.Request.Context(), req.Address)
4137
if handleError(c, ctx.Storage, err, 0) {
4238
return
4339
}
40+
4441
var balance int64
4542
if !(bcd.IsRollupAddressLazy(acc.Address) || bcd.IsSmartRollupAddressLazy(acc.Address)) {
46-
block, err := ctx.Blocks.Last()
43+
block, err := ctx.Blocks.Last(c.Request.Context())
4744
if handleError(c, ctx.Storage, err, 0) {
4845
return
4946
}
5047
balance, err = ctx.Cache.TezosBalance(c, acc.Address, block.Level)
5148
if err != nil {
52-
logger.Err(err)
49+
log.Err(err).Msg("receiving tezos balance")
5350
}
5451
}
5552
c.SecureJSON(http.StatusOK, AccountInfo{
56-
Address: acc.Address,
57-
Alias: acc.Alias,
58-
TxCount: stats.Count,
59-
Balance: balance,
60-
LastAction: stats.LastAction.UTC(),
61-
AccountType: acc.Type.String(),
53+
Address: acc.Address,
54+
OperationsCount: acc.OperationsCount,
55+
EventsCount: acc.EventsCount,
56+
MigrationsCount: acc.MigrationsCount,
57+
TicketUpdatesCount: acc.TicketUpdatesCount,
58+
Balance: balance,
59+
LastAction: acc.LastAction.UTC(),
60+
AccountType: acc.Type.String(),
6261
})
6362
}
6463

cmd/api/handlers/bigmap.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handlers
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67

@@ -38,7 +39,7 @@ func GetBigMap() gin.HandlerFunc {
3839
return
3940
}
4041

41-
stats, err := ctx.BigMapDiffs.GetStats(req.Ptr)
42+
stats, err := ctx.BigMapDiffs.GetStats(c.Request.Context(), req.Ptr)
4243
if handleError(c, ctx.Storage, err, 0) {
4344
return
4445
}
@@ -52,29 +53,29 @@ func GetBigMap() gin.HandlerFunc {
5253
}
5354

5455
if stats.Total == 0 {
55-
actions, err := ctx.BigMapActions.Get(req.Ptr, 1, 0)
56+
actions, err := ctx.BigMapActions.Get(c.Request.Context(), req.Ptr, 1, 0)
5657
if handleError(c, ctx.Storage, err, 0) {
5758
return
5859
}
5960
if len(actions) > 0 {
6061
res.Address = actions[0].Address
6162
}
6263
} else {
63-
destination, err := ctx.Accounts.Get(res.Address)
64+
destination, err := ctx.Accounts.Get(c.Request.Context(), res.Address)
6465
if handleError(c, ctx.Storage, err, 0) {
6566
return
6667
}
67-
res.ContractAlias = destination.Alias
6868

6969
operation, err := ctx.Operations.Last(
70+
c.Request.Context(),
7071
map[string]interface{}{
7172
"status": types.OperationStatusApplied,
7273
"destination_id": destination.ID,
7374
}, 0)
7475
if handleError(c, ctx.Storage, err, 0) {
7576
return
7677
}
77-
proto, err := ctx.Cache.ProtocolByID(operation.ProtocolID)
78+
proto, err := ctx.Cache.ProtocolByID(c.Request.Context(), operation.ProtocolID)
7879
if handleError(c, ctx.Storage, err, 0) {
7980
return
8081
}
@@ -94,7 +95,7 @@ func GetBigMap() gin.HandlerFunc {
9495
return
9596
}
9697

97-
script, err := ctx.Contracts.ScriptPart(res.Address, proto.SymLink, consts.STORAGE)
98+
script, err := ctx.Contracts.ScriptPart(c.Request.Context(), res.Address, proto.SymLink, consts.STORAGE)
9899
if handleError(c, ctx.Storage, err, 0) {
99100
return
100101
}
@@ -146,7 +147,7 @@ func GetBigMapHistory() gin.HandlerFunc {
146147
return
147148
}
148149

149-
bm, err := ctx.BigMapActions.Get(req.Ptr, 0, 0)
150+
bm, err := ctx.BigMapActions.Get(c.Request.Context(), req.Ptr, 0, 0)
150151
if handleError(c, ctx.Storage, err, 0) {
151152
return
152153
}
@@ -189,7 +190,7 @@ func GetBigMapKeys() gin.HandlerFunc {
189190
return
190191
}
191192

192-
keys, err := ctx.BigMapDiffs.Keys(bigmapdiff.GetContext{
193+
keys, err := ctx.BigMapDiffs.Keys(c.Request.Context(), bigmapdiff.GetContext{
193194
Ptr: &req.Ptr,
194195
Size: pageReq.Size,
195196
Offset: pageReq.Offset,
@@ -201,12 +202,12 @@ func GetBigMapKeys() gin.HandlerFunc {
201202
return
202203
}
203204

204-
symLink, err := getCurrentSymLink(ctx.Blocks)
205+
symLink, err := getCurrentSymLink(c.Request.Context(), ctx.Blocks)
205206
if handleError(c, ctx.Storage, err, 0) {
206207
return
207208
}
208209

209-
response, err := prepareBigMapKeys(ctx, keys, symLink)
210+
response, err := prepareBigMapKeys(c.Request.Context(), ctx, keys, symLink)
210211
if handleError(c, ctx.Storage, err, 0) {
211212
return
212213
}
@@ -246,7 +247,7 @@ func GetBigMapByKeyHash() gin.HandlerFunc {
246247
return
247248
}
248249

249-
bm, total, err := ctx.BigMapDiffs.GetByPtrAndKeyHash(req.Ptr, req.KeyHash, pageReq.Size, pageReq.Offset)
250+
bm, total, err := ctx.BigMapDiffs.GetByPtrAndKeyHash(c.Request.Context(), req.Ptr, req.KeyHash, pageReq.Size, pageReq.Offset)
250251
if handleError(c, ctx.Storage, err, 0) {
251252
return
252253
}
@@ -255,12 +256,12 @@ func GetBigMapByKeyHash() gin.HandlerFunc {
255256
c.SecureJSON(http.StatusNoContent, gin.H{})
256257
return
257258
}
258-
symLink, err := getCurrentSymLink(ctx.Blocks)
259+
symLink, err := getCurrentSymLink(c.Request.Context(), ctx.Blocks)
259260
if handleError(c, ctx.Storage, err, 0) {
260261
return
261262
}
262263

263-
response, err := prepareBigMapItem(ctx, bm, req.KeyHash, symLink)
264+
response, err := prepareBigMapItem(c.Request.Context(), ctx, bm, req.KeyHash, symLink)
264265
if handleError(c, ctx.Storage, err, 0) {
265266
return
266267
}
@@ -294,17 +295,17 @@ func GetCurrentBigMapKeyHash() gin.HandlerFunc {
294295
return
295296
}
296297

297-
state, err := ctx.BigMapDiffs.Current(req.KeyHash, req.Ptr)
298+
state, err := ctx.BigMapDiffs.Current(c.Request.Context(), req.KeyHash, req.Ptr)
298299
if handleError(c, ctx.Storage, err, 0) {
299300
return
300301
}
301302

302-
symLink, err := getCurrentSymLink(ctx.Blocks)
303+
symLink, err := getCurrentSymLink(c.Request.Context(), ctx.Blocks)
303304
if handleError(c, ctx.Storage, err, 0) {
304305
return
305306
}
306307

307-
bigMapType, err := getBigMapType(ctx, state.Contract, state.Ptr, symLink)
308+
bigMapType, err := getBigMapType(c.Request.Context(), ctx, state.Contract, state.Ptr, symLink)
308309
if handleError(c, ctx.Storage, err, 0) {
309310
return
310311
}
@@ -348,7 +349,7 @@ func GetBigMapDiffCount() gin.HandlerFunc {
348349
return
349350
}
350351

351-
count, err := ctx.BigMapDiffs.Count(req.Ptr)
352+
count, err := ctx.BigMapDiffs.Count(c.Request.Context(), req.Ptr)
352353
if err != nil {
353354
if ctx.Storage.IsRecordNotFound(err) {
354355
c.SecureJSON(http.StatusOK, CountResponse{})
@@ -357,16 +358,16 @@ func GetBigMapDiffCount() gin.HandlerFunc {
357358
handleError(c, ctx.Storage, err, 0)
358359
return
359360
}
360-
c.SecureJSON(http.StatusOK, CountResponse{count})
361+
c.SecureJSON(http.StatusOK, CountResponse{int64(count)})
361362
}
362363
}
363364

364-
func prepareBigMapKeys(ctx *config.Context, data []bigmapdiff.BigMapState, symLink string) ([]BigMapResponseItem, error) {
365+
func prepareBigMapKeys(c context.Context, ctx *config.Context, data []bigmapdiff.BigMapState, symLink string) ([]BigMapResponseItem, error) {
365366
if len(data) == 0 {
366367
return []BigMapResponseItem{}, nil
367368
}
368369

369-
bigMapType, err := getBigMapType(ctx, data[0].Contract, data[0].Ptr, symLink)
370+
bigMapType, err := getBigMapType(c, ctx, data[0].Contract, data[0].Ptr, symLink)
370371
if err != nil {
371372
return nil, err
372373
}
@@ -393,12 +394,12 @@ func prepareBigMapKeys(ctx *config.Context, data []bigmapdiff.BigMapState, symLi
393394
return res, nil
394395
}
395396

396-
func prepareBigMapItem(ctx *config.Context, data []bigmapdiff.BigMapDiff, keyHash, symLink string) (res BigMapDiffByKeyResponse, err error) {
397+
func prepareBigMapItem(c context.Context, ctx *config.Context, data []bigmapdiff.BigMapDiff, keyHash, symLink string) (res BigMapDiffByKeyResponse, err error) {
397398
if len(data) == 0 {
398399
return
399400
}
400401

401-
bigMapType, err := getBigMapType(ctx, data[0].Contract, data[0].Ptr, symLink)
402+
bigMapType, err := getBigMapType(c, ctx, data[0].Contract, data[0].Ptr, symLink)
402403
if err != nil {
403404
return
404405
}
@@ -424,13 +425,13 @@ func prepareBigMapItem(ctx *config.Context, data []bigmapdiff.BigMapDiff, keyHas
424425
return
425426
}
426427

427-
func getBigMapType(ctx *config.Context, contract string, ptr int64, symLink string) (*ast.BigMap, error) {
428-
storageType, err := getStorageType(ctx.Contracts, contract, symLink)
428+
func getBigMapType(c context.Context, ctx *config.Context, contract string, ptr int64, symLink string) (*ast.BigMap, error) {
429+
storageType, err := getStorageType(c, ctx.Contracts, contract, symLink)
429430
if err != nil {
430431
return nil, err
431432
}
432433

433-
actions, err := ctx.BigMapActions.Get(ptr, 2, 0)
434+
actions, err := ctx.BigMapActions.Get(c, ptr, 2, 0)
434435
if err != nil {
435436
return nil, err
436437
}
@@ -443,18 +444,18 @@ func getBigMapType(ctx *config.Context, contract string, ptr int64, symLink stri
443444
operationID = actions[1].OperationID
444445
}
445446
}
446-
operation, err := ctx.Operations.GetByID(operationID)
447+
operation, err := ctx.Operations.GetByID(c, operationID)
447448
if err != nil {
448449
return nil, err
449450
}
450451
deffatedStorage = operation.DeffatedStorage
451452
} else {
452-
contract, err := ctx.Accounts.Get(contract)
453+
contract, err := ctx.Accounts.Get(c, contract)
453454
if err != nil {
454455
return nil, err
455456
}
456457

457-
operation, err := ctx.Operations.Last(map[string]interface{}{
458+
operation, err := ctx.Operations.Last(c, map[string]interface{}{
458459
"destination_id": contract.ID,
459460
"status": types.OperationStatusApplied,
460461
}, 0)

0 commit comments

Comments
 (0)