Skip to content

Commit

Permalink
Refactoring: accounts table (#796)
Browse files Browse the repository at this point in the history
* Refactoring: scripts in separate model

* Refactoring: script to separate table. Some perfomance improvements

* Refactoring: accounts table

* Fix: database indexes
  • Loading branch information
aopoltorzhicky authored Jan 10, 2022
1 parent 658315e commit 830d81e
Show file tree
Hide file tree
Showing 158 changed files with 2,384 additions and 2,001 deletions.
45 changes: 36 additions & 9 deletions cmd/api/handlers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"

"github.com/baking-bad/bcdhub/internal/bcd"
"github.com/baking-bad/bcdhub/internal/models/contract_metadata"
"github.com/baking-bad/bcdhub/internal/models/types"
"github.com/baking-bad/bcdhub/internal/models/tzip"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -57,7 +57,7 @@ func (ctx *Context) GetInfo(c *gin.Context) {
LastAction: stats.LastAction.UTC(),
}

alias, err := ctx.TZIP.Get(req.NetworkID(), req.Address)
alias, err := ctx.ContractMetadata.Get(req.NetworkID(), req.Address)
if err != nil {
if !ctx.Storage.IsRecordNotFound(err) {
ctx.handleError(c, err, 0)
Expand Down Expand Up @@ -92,12 +92,22 @@ func (ctx *Context) GetBatchTokenBalances(c *gin.Context) {
if err := c.BindQuery(&queryParams); ctx.handleError(c, err, http.StatusBadRequest) {
return
}

network := req.NetworkID()

accountIDs := make([]int64, 0)
address := strings.Split(queryParams.Address, ",")
for i := range address {
if !bcd.IsAddress(address[i]) {
ctx.handleError(c, errors.Errorf("Invalid address: %s", address[i]), http.StatusBadRequest)
return
}

acc, err := ctx.Accounts.Get(network, address[i])
if ctx.handleError(c, err, http.StatusNotFound) {
return
}
accountIDs = append(accountIDs, acc.ID)
}

if len(address) > maxTokenBalanceBatch {
Expand All @@ -106,7 +116,7 @@ func (ctx *Context) GetBatchTokenBalances(c *gin.Context) {
}
}

balances, err := ctx.TokenBalances.Batch(req.NetworkID(), address)
balances, err := ctx.TokenBalances.Batch(req.NetworkID(), accountIDs)
if ctx.handleError(c, err, 0) {
return
}
Expand Down Expand Up @@ -166,7 +176,12 @@ func (ctx *Context) GetAccountTokenBalances(c *gin.Context) {
}

func (ctx *Context) getAccountBalances(network types.Network, address string, req tokenBalanceRequest) (*TokenBalances, error) {
balances, err := ctx.Domains.TokenBalances(network, req.Contract, address, req.Size, req.Offset, req.SortBy, req.HideEmpty)
acc, err := ctx.Accounts.Get(network, address)
if err != nil {
return nil, err
}

balances, err := ctx.Domains.TokenBalances(network, req.Contract, acc.ID, req.Size, req.Offset, req.SortBy, req.HideEmpty)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -220,7 +235,12 @@ func (ctx *Context) GetAccountTokensCountByContract(c *gin.Context) {
if err := c.BindQuery(&queryParams); ctx.handleError(c, err, http.StatusBadRequest) {
return
}
res, err := ctx.TokenBalances.CountByContract(req.NetworkID(), req.Address, queryParams.HideEmpty)
network := req.NetworkID()
acc, err := ctx.Accounts.Get(network, req.Address)
if ctx.handleError(c, err, http.StatusNotFound) {
return
}
res, err := ctx.TokenBalances.CountByContract(network, acc.ID, queryParams.HideEmpty)
if ctx.handleError(c, err, 0) {
return
}
Expand Down Expand Up @@ -251,20 +271,27 @@ func (ctx *Context) GetAccountTokensCountByContractWithMetadata(c *gin.Context)
if err := c.BindQuery(&queryParams); ctx.handleError(c, err, http.StatusBadRequest) {
return
}
res, err := ctx.TokenBalances.CountByContract(req.NetworkID(), req.Address, queryParams.HideEmpty)

network := req.NetworkID()
acc, err := ctx.Accounts.Get(network, req.Address)
if ctx.handleError(c, err, http.StatusNotFound) {
return
}

res, err := ctx.TokenBalances.CountByContract(network, acc.ID, queryParams.HideEmpty)
if ctx.handleError(c, err, 0) {
return
}

response := make(map[string]TokensCountWithMetadata)
for address, count := range res {
metadata, err := ctx.Cache.ContractMetadata(req.NetworkID(), address)
metadata, err := ctx.Cache.ContractMetadata(network, address)
if err != nil {
if !ctx.Storage.IsRecordNotFound(err) && ctx.handleError(c, err, 0) {
return
} else {
metadata = &tzip.TZIP{
Network: req.NetworkID(),
metadata = &contract_metadata.ContractMetadata{
Network: network,
Address: address,
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (ctx *Context) GetBySlug(c *gin.Context) {
return
}

a, err := ctx.TZIP.GetBySlug(req.Slug)
a, err := ctx.ContractMetadata.GetBySlug(req.Slug)
if ctx.handleError(c, err, 0) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/bigmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (ctx *Context) GetBigMap(c *gin.Context) {
}
}

alias, err := ctx.TZIP.Get(req.NetworkID(), res.Address)
alias, err := ctx.ContractMetadata.Get(req.NetworkID(), res.Address)
if err != nil {
if !ctx.Storage.IsRecordNotFound(err) {
ctx.handleError(c, err, 0)
Expand Down
5 changes: 1 addition & 4 deletions cmd/api/handlers/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ func (ctx *Context) contractPostprocessing(contract contract.Contract) (Contract
var res Contract
res.FromModel(contract)

res.Alias = ctx.Cache.Alias(contract.Network, contract.Address)
res.DelegateAlias = ctx.Cache.Alias(contract.Network, contract.Delegate.String())

if alias, err := ctx.TZIP.Get(contract.Network, contract.Address); err == nil {
if alias, err := ctx.ContractMetadata.Get(contract.Network, contract.Account.Address); err == nil {
res.Slug = alias.Slug
} else if !ctx.Storage.IsRecordNotFound(err) {
return res, err
Expand Down
26 changes: 12 additions & 14 deletions cmd/api/handlers/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,16 @@ func (ctx *Context) GetDApp(c *gin.Context) {

func (ctx *Context) appendDAppInfo(dapp dapp.DApp, withDetails bool) (DApp, error) {
result := DApp{
Name: dapp.Name,
ShortDescription: dapp.ShortDescription,
FullDescription: dapp.FullDescription,
WebSite: dapp.WebSite,
Slug: dapp.Slug,
AgoraReviewPostID: dapp.AgoraReviewPostID,
AgoraQAPostID: dapp.AgoraQAPostID,
Authors: dapp.Authors,
SocialLinks: dapp.SocialLinks,
Interfaces: dapp.Interfaces,
Categories: dapp.Categories,
Soon: dapp.Soon,
Name: dapp.Name,
ShortDescription: dapp.ShortDescription,
FullDescription: dapp.FullDescription,
WebSite: dapp.WebSite,
Slug: dapp.Slug,
Authors: dapp.Authors,
SocialLinks: dapp.SocialLinks,
Interfaces: dapp.Interfaces,
Categories: dapp.Categories,
Soon: dapp.Soon,
}

if len(dapp.Pictures) > 0 {
Expand Down Expand Up @@ -159,8 +157,8 @@ func (ctx *Context) appendDAppInfo(dapp dapp.DApp, withDetails bool) (DApp, erro
}
result.Contracts = append(result.Contracts, DAppContract{
Network: contract.Network.String(),
Address: contract.Address,
Alias: ctx.Cache.Alias(contract.Network, contract.Address),
Address: contract.Account.Address,
Alias: contract.Account.Alias,
ReleaseDate: contract.Timestamp.UTC(),
})

Expand Down
10 changes: 5 additions & 5 deletions cmd/api/handlers/entrypoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ func (ctx *Context) GetEntrypointSchema(c *gin.Context) {

op, err := ctx.Operations.Get(
map[string]interface{}{
"network": req.NetworkID(),
"destination": req.Address,
"kind": modelTypes.OperationKindTransaction,
"entrypoint": esReq.EntrypointName,
"status": modelTypes.OperationStatusApplied,
"operation.network": req.NetworkID(),
"destination.address": req.Address,
"kind": modelTypes.OperationKindTransaction,
"entrypoint": esReq.EntrypointName,
"status": modelTypes.OperationStatusApplied,
},
1,
true,
Expand Down
16 changes: 11 additions & 5 deletions cmd/api/handlers/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/baking-bad/bcdhub/internal/models/migration"
"github.com/baking-bad/bcdhub/internal/models/types"
"github.com/gin-gonic/gin"
)

Expand All @@ -27,27 +28,32 @@ func (ctx *Context) GetContractMigrations(c *gin.Context) {
return
}

migrations, err := ctx.Migrations.Get(req.NetworkID(), req.Address)
contract, err := ctx.Contracts.Get(req.NetworkID(), req.Address)
if ctx.handleError(c, err, 0) {
return
}

result, err := prepareMigrations(ctx, migrations)
migrations, err := ctx.Migrations.Get(contract.ID)
if ctx.handleError(c, err, 0) {
return
}

result, err := prepareMigrations(ctx, req.NetworkID(), migrations)
if ctx.handleError(c, err, 0) {
return
}

c.SecureJSON(http.StatusOK, result)
}

func prepareMigrations(ctx *Context, data []migration.Migration) ([]Migration, error) {
func prepareMigrations(ctx *Context, network types.Network, data []migration.Migration) ([]Migration, error) {
result := make([]Migration, len(data))
for i := range data {
proto, err := ctx.Cache.ProtocolByID(data[i].Network, data[i].ProtocolID)
proto, err := ctx.Cache.ProtocolByID(network, data[i].ProtocolID)
if err != nil && !ctx.Storage.IsRecordNotFound(err) {
return nil, err
}
prevProto, err := ctx.Cache.ProtocolByID(data[i].Network, data[i].PrevProtocolID)
prevProto, err := ctx.Cache.ProtocolByID(network, data[i].PrevProtocolID)
if err != nil && !ctx.Storage.IsRecordNotFound(err) {
return nil, err
}
Expand Down
17 changes: 11 additions & 6 deletions cmd/api/handlers/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ func (ctx *Context) GetContractOperations(c *gin.Context) {
return
}

account, err := ctx.Accounts.Get(req.NetworkID(), req.Address)
if ctx.handleError(c, err, http.StatusNotFound) {
return
}

filters := prepareFilters(filtersReq)
ops, err := ctx.Operations.GetByContract(req.NetworkID(), req.Address, filtersReq.Size, filters)
ops, err := ctx.Operations.GetByAccount(account, filtersReq.Size, filters)
if ctx.handleError(c, err, 0) {
return
}
Expand Down Expand Up @@ -199,7 +204,7 @@ func (ctx *Context) GetOperationDiff(c *gin.Context) {
}
result.Protocol = proto.Hash

storageBytes, err := ctx.Contracts.ScriptPart(operation.Network, operation.Destination, proto.SymLink, consts.STORAGE)
storageBytes, err := ctx.Contracts.ScriptPart(operation.Network, operation.Destination.Address, proto.SymLink, consts.STORAGE)
if ctx.handleError(c, err, 0) {
return
}
Expand All @@ -214,7 +219,7 @@ func (ctx *Context) GetOperationDiff(c *gin.Context) {
return
}

if err := ctx.setStorageDiff(operation.Destination, operation.DeffatedStorage, &result, bmd, storageType); ctx.handleError(c, err, 0) {
if err := ctx.setStorageDiff(operation.Destination.Address, operation.DeffatedStorage, &result, bmd, storageType); ctx.handleError(c, err, 0) {
return
}
}
Expand Down Expand Up @@ -312,8 +317,8 @@ func (ctx *Context) prepareOperation(operation operation.Operation, bmd []bigmap
var op Operation
op.FromModel(operation)

op.SourceAlias = ctx.Cache.Alias(operation.Network, operation.Source)
op.DestinationAlias = ctx.Cache.Alias(operation.Network, operation.Destination)
op.SourceAlias = operation.Source.Alias
op.DestinationAlias = operation.Destination.Alias

proto, err := ctx.Cache.ProtocolByID(operation.Network, operation.ProtocolID)
if err != nil {
Expand Down Expand Up @@ -497,7 +502,7 @@ func (ctx *Context) getErrorLocation(operation operation.Operation, window int)
if err != nil {
return GetErrorLocationResponse{}, err
}
code, err := ctx.getScriptBytes(operation.Network, operation.Destination, proto.SymLink)
code, err := ctx.getScriptBytes(operation.Network, operation.Destination.Address, proto.SymLink)
if err != nil {
return GetErrorLocationResponse{}, err
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/api/handlers/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,13 @@ func (ctx *Context) GetSimilarContracts(c *gin.Context) {
}
for i := range similar {
diff, err := ctx.getContractCodeDiff(
CodeDiffLeg{Address: contract.Address, Network: contract.Network},
CodeDiffLeg{Address: similar[i].Address, Network: similar[i].Network},
CodeDiffLeg{Address: contract.Account.Address, Network: contract.Network},
CodeDiffLeg{Address: similar[i].Account.Address, Network: similar[i].Network},
)
if ctx.handleError(c, err, 0) {
return
}
response.Contracts[i].FromModel(similar[i], diff)

response.Contracts[i].Alias = ctx.Cache.Alias(similar[i].Network, similar[i].Address)
response.Contracts[i].DelegateAlias = ctx.Cache.Alias(similar[i].Network, similar[i].Delegate.String())
}

c.SecureJSON(http.StatusOK, response)
Expand Down
Loading

0 comments on commit 830d81e

Please sign in to comment.