Skip to content

Commit

Permalink
Refactoring: drop aliases columns (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Jun 10, 2021
1 parent e953881 commit 6a6f8d0
Show file tree
Hide file tree
Showing 26 changed files with 139 additions and 210 deletions.
3 changes: 3 additions & 0 deletions cmd/api/handlers/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (ctx *Context) contractPostprocessing(contract contract.Contract) (Contract
var res Contract
res.FromModel(contract)

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

if alias, err := ctx.TZIP.Get(contract.Network, contract.Address); err == nil {
res.Slug = alias.Slug
} else if !ctx.Storage.IsRecordNotFound(err) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ 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: contract.Alias,
Alias: ctx.CachedAlias(contract.Network, contract.Address),
ReleaseDate: contract.Timestamp.UTC(),
})

Expand Down
3 changes: 3 additions & 0 deletions cmd/api/handlers/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ func (ctx *Context) prepareOperation(operation operation.Operation, bmd []bigmap
var op Operation
op.FromModel(operation)

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

proto, err := ctx.CachedProtocolByID(operation.Network, operation.ProtocolID)
if err != nil {
return op, err
Expand Down
6 changes: 4 additions & 2 deletions cmd/api/handlers/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func (ctx *Context) GetSameContracts(c *gin.Context) {
}

var response SameContractsResponse
response.FromModel(sameContracts)

response.FromModel(sameContracts, ctx)
c.JSON(http.StatusOK, response)
}

Expand Down Expand Up @@ -105,6 +104,9 @@ func (ctx *Context) GetSimilarContracts(c *gin.Context) {
return
}
response.Contracts[i].FromModel(similar[i], diff)

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

c.JSON(http.StatusOK, response)
Expand Down
36 changes: 16 additions & 20 deletions cmd/api/handlers/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@ func (o *Operation) FromModel(operation operation.Operation) {
o.Level = operation.Level
o.Kind = operation.Kind.String()
o.Source = operation.Source
o.SourceAlias = operation.SourceAlias
o.Fee = operation.Fee
o.Counter = operation.Counter
o.GasLimit = operation.GasLimit
o.StorageLimit = operation.StorageLimit
o.Amount = operation.Amount
o.Destination = operation.Destination
o.DestinationAlias = operation.DestinationAlias
o.Delegate = operation.Delegate
o.Status = operation.Status.String()
o.Burned = operation.Burned
Expand All @@ -103,21 +101,19 @@ func (o *Operation) ToModel() operation.Operation {
Internal: o.Internal,
Timestamp: o.Timestamp,

Level: o.Level,
Kind: types.NewOperationKind(o.Kind),
Source: o.Source,
SourceAlias: o.SourceAlias,
Fee: o.Fee,
Counter: o.Counter,
GasLimit: o.GasLimit,
StorageLimit: o.StorageLimit,
Amount: o.Amount,
Destination: o.Destination,
DestinationAlias: o.DestinationAlias,
Delegate: o.Delegate,
Status: types.NewOperationStatus(o.Status),
Burned: o.Burned,
Entrypoint: o.Entrypoint,
Level: o.Level,
Kind: types.NewOperationKind(o.Kind),
Source: o.Source,
Fee: o.Fee,
Counter: o.Counter,
GasLimit: o.GasLimit,
StorageLimit: o.StorageLimit,
Amount: o.Amount,
Destination: o.Destination,
Delegate: o.Delegate,
Status: types.NewOperationStatus(o.Status),
Burned: o.Burned,
Entrypoint: o.Entrypoint,

AllocatedDestinationContract: o.AllocatedDestinationContract,
ConsumedGas: o.ConsumedGas,
Expand Down Expand Up @@ -164,9 +160,7 @@ type Contract struct {
// FromModel -
func (c *Contract) FromModel(contract contract.Contract) {
c.Address = contract.Address
c.Alias = contract.Alias
c.Delegate = contract.Delegate
c.DelegateAlias = contract.DelegateAlias
c.Entrypoints = contract.Entrypoints
c.Hash = contract.Hash
c.Language = contract.Language
Expand Down Expand Up @@ -464,13 +458,15 @@ type SameContractsResponse struct {
}

// FromModel -
func (c *SameContractsResponse) FromModel(same contract.SameResponse) {
func (c *SameContractsResponse) FromModel(same contract.SameResponse, ctx *Context) {
c.Count = same.Count

c.Contracts = make([]Contract, len(same.Contracts))
for i := range same.Contracts {
var contract Contract
contract.FromModel(same.Contracts[i])
contract.Alias = ctx.CachedAlias(same.Contracts[i].Network, same.Contracts[i].Address)
contract.DelegateAlias = ctx.CachedAlias(same.Contracts[i].Network, same.Contracts[i].Delegate)
c.Contracts[i] = contract
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/api/handlers/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func (ctx *Context) contractToTokens(contracts []contract.Contract, network type
Address: contracts[i].Address,
Manager: contracts[i].Manager,
Delegate: contracts[i].Delegate,
Alias: contracts[i].Alias,
DelegateAlias: contracts[i].DelegateAlias,
Alias: ctx.CachedAlias(contracts[i].Network, contracts[i].Address),
DelegateAlias: ctx.CachedAlias(contracts[i].Network, contracts[i].Delegate),
LastAction: contracts[i].LastAction,
TxCount: contracts[i].TxCount,
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/metrics/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ package main

import (
"github.com/baking-bad/bcdhub/internal/models"
"github.com/baking-bad/bcdhub/internal/models/types"
"github.com/baking-bad/bcdhub/internal/search"
)

func saveSearchModels(searcher search.Searcher, items []models.Model) error {
data := search.Prepare(items)

for i := range data {
switch typ := data[i].(type) {
case *search.Contract:
typ.Alias = ctx.CachedAlias(types.NewNetwork(typ.Network), typ.Address)
typ.DelegateAlias = ctx.CachedAlias(types.NewNetwork(typ.Network), typ.Delegate)
case *search.Operation:
typ.SourceAlias = ctx.CachedAlias(types.NewNetwork(typ.Network), typ.Source)
typ.DestinationAlias = ctx.CachedAlias(types.NewNetwork(typ.Network), typ.Destination)
typ.DelegateAlias = ctx.CachedAlias(types.NewNetwork(typ.Network), typ.Delegate)
}
}

return searcher.Save(data)
}
2 changes: 0 additions & 2 deletions internal/models/contract/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ type Contract struct {
TxCount int64 `json:"tx_count" gorm:",default:0"`
LastAction time.Time `json:"last_action"`
MigrationsCount int64 `json:"migrations_count,omitempty" gorm:",default:0"`
Alias string `json:"alias,omitempty"`
DelegateAlias string `json:"delegate_alias,omitempty"`
}

// NewEmptyContract -
Expand Down
1 change: 0 additions & 1 deletion internal/models/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ type GeneralRepository interface {
// Save - performs insert or update items.
Save(items []Model) error
BulkDelete([]Model) error
SetAlias(network types.Network, address, alias string) error
}
14 changes: 0 additions & 14 deletions internal/models/mock/general.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 10 additions & 13 deletions internal/models/operation/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,16 @@ type Operation struct {
ProtocolID int64 `json:"protocol" gorm:"type:SMALLINT"`
Hash string `json:"hash" gorm:"index:opg_idx;index:operations_hash_idx"`

Timestamp time.Time `json:"timestamp"`
Status types.OperationStatus `json:"status" gorm:"type:SMALLINT"`
Kind types.OperationKind `json:"kind" gorm:"type:SMALLINT"`
Initiator string `json:"initiator"`
Source string `json:"source" gorm:"index:source_idx"`
Destination string `json:"destination,omitempty" gorm:"index:destination_idx"`
Delegate string `json:"delegate,omitempty"`
Entrypoint string `json:"entrypoint,omitempty"`
SourceAlias string `json:"source_alias,omitempty"`
DestinationAlias string `json:"destination_alias,omitempty"`
DelegateAlias string `json:"delegate_alias,omitempty"`
Parameters []byte `json:"parameters,omitempty"`
DeffatedStorage []byte `json:"deffated_storage"`
Timestamp time.Time `json:"timestamp"`
Status types.OperationStatus `json:"status" gorm:"type:SMALLINT"`
Kind types.OperationKind `json:"kind" gorm:"type:SMALLINT"`
Initiator string `json:"initiator"`
Source string `json:"source" gorm:"index:source_idx"`
Destination string `json:"destination,omitempty" gorm:"index:destination_idx"`
Delegate string `json:"delegate,omitempty"`
Entrypoint string `json:"entrypoint,omitempty"`
Parameters []byte `json:"parameters,omitempty"`
DeffatedStorage []byte `json:"deffated_storage"`

Tags pq.StringArray `json:"tags,omitempty" gorm:"type:text[]"`

Expand Down
3 changes: 0 additions & 3 deletions internal/parsers/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ func (p *Parser) computeMetrics(operation *operation.Operation, c *contract.Cont
return err
}

c.Alias = p.ctx.CachedAlias(c.Network, c.Address)
c.DelegateAlias = p.ctx.CachedAlias(c.Network, c.Delegate)

if p.scriptSaver != nil {
return p.scriptSaver.Save(operation.Script, ScriptSaveContext{
Network: c.Network.String(),
Expand Down
4 changes: 0 additions & 4 deletions internal/parsers/operations/origination.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,13 @@ func (p Origination) Parse(data noderpc.Operation) (*parsers.Result, error) {
Nonce: data.Nonce,
ContentIndex: p.contentIdx,
Script: data.Script,
SourceAlias: p.ctx.CachedAlias(p.network, data.Source),
}

result.Operations = append(result.Operations, &origination)

p.fillInternal(&origination)

parseOperationResult(data, &origination)
if data.Destination != nil {
origination.DestinationAlias = p.ctx.CachedAlias(p.network, *data.Destination)
}

origination.SetBurned(p.constants)

Expand Down
12 changes: 0 additions & 12 deletions internal/parsers/operations/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,6 @@ func compareOperations(t *testing.T, one, two *operation.Operation) bool {
logger.Info("Entrypoint: %s != %s", one.Entrypoint, two.Entrypoint)
return false
}
if one.SourceAlias != two.SourceAlias {
logger.Info("SourceAlias: %s != %s", one.SourceAlias, two.SourceAlias)
return false
}
if one.DestinationAlias != two.DestinationAlias {
logger.Info("DestinationAlias: %s != %s", one.DestinationAlias, two.DestinationAlias)
return false
}
if one.DelegateAlias != two.DelegateAlias {
logger.Info("DelegateAlias: %s != %s", one.DelegateAlias, two.DelegateAlias)
return false
}
if len(one.Parameters) > 0 && len(two.Parameters) > 0 {
if !assert.JSONEq(t, string(one.Parameters), string(two.Parameters)) {
logger.Info("Parameters: %s != %s", one.Parameters, two.Parameters)
Expand Down
41 changes: 18 additions & 23 deletions internal/parsers/operations/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,24 @@ func (p Transaction) Parse(data noderpc.Operation) (*parsers.Result, error) {
}

tx := operation.Operation{
Network: p.network,
Hash: p.hash,
ProtocolID: proto.ID,
Level: p.head.Level,
Timestamp: p.head.Timestamp,
Kind: modelsTypes.NewOperationKind(data.Kind),
Initiator: data.Source,
Source: data.Source,
Fee: data.Fee,
Counter: data.Counter,
GasLimit: data.GasLimit,
StorageLimit: data.StorageLimit,
Amount: *data.Amount,
Destination: *data.Destination,
Delegate: data.Delegate,
Nonce: data.Nonce,
Parameters: data.Parameters,
ContentIndex: p.contentIdx,
SourceAlias: p.ctx.CachedAlias(p.network, data.Source),
DelegateAlias: p.ctx.CachedAlias(p.network, data.Delegate),
}
if data.Destination != nil {
tx.DestinationAlias = p.ctx.CachedAlias(p.network, *data.Destination)
Network: p.network,
Hash: p.hash,
ProtocolID: proto.ID,
Level: p.head.Level,
Timestamp: p.head.Timestamp,
Kind: modelsTypes.NewOperationKind(data.Kind),
Initiator: data.Source,
Source: data.Source,
Fee: data.Fee,
Counter: data.Counter,
GasLimit: data.GasLimit,
StorageLimit: data.StorageLimit,
Amount: *data.Amount,
Destination: *data.Destination,
Delegate: data.Delegate,
Nonce: data.Nonce,
Parameters: data.Parameters,
ContentIndex: p.contentIdx,
}

p.fillInternal(&tx)
Expand Down
49 changes: 0 additions & 49 deletions internal/postgres/core/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import (
"strings"

"github.com/baking-bad/bcdhub/internal/models"
"github.com/baking-bad/bcdhub/internal/models/contract"
"github.com/baking-bad/bcdhub/internal/models/operation"
"github.com/baking-bad/bcdhub/internal/models/types"
"github.com/iancoleman/strcase"
"gorm.io/gorm"
)

// UpdateDoc -
Expand All @@ -24,51 +20,6 @@ func (p *Postgres) UpdateFields(index string, id int64, data interface{}, fields
return p.DB.Table(index).Where("id = ?", id).Updates(updates).Error
}

// SetAlias -
func (p *Postgres) SetAlias(network types.Network, address, alias string) error {
return p.DB.Transaction(func(tx *gorm.DB) error {
for _, field := range []string{"source_alias", "destination_alias", "delegate_alias"} {
query := tx.Model(&operation.Operation{}).
Select(field).
Where("network = ?", network)
var op operation.Operation
switch field {
case "source_alias":
op.SourceAlias = alias
query.Where("source = ?", address)
case "destination_alias":
op.DestinationAlias = alias
query.Where("destination = ?", address)
case "delegate_alias":
query.Where("delegate = ?", address)
op.DelegateAlias = alias
}
if err := query.Updates(&op).Error; err != nil {
return err
}
}
for _, field := range []string{"alias", "delegate_alias"} {
query := tx.Model(&contract.Contract{}).
Select(field).
Where("network = ?", network)
var c contract.Contract
switch field {
case "alias":
c.Alias = alias
query.Where("address = ?", address)
case "delegate_alias":
c.DelegateAlias = alias
query.Where("delegate = ?", address)
}
if err := query.Updates(&c).Error; err != nil {
return err
}
}
return nil
})

}

// GetFieldsForModel -
func GetFieldsForModel(data interface{}, fields ...string) map[string]interface{} {
t := reflect.TypeOf(data)
Expand Down
2 changes: 1 addition & 1 deletion internal/search/bigmapdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ func (b *BigMapDiff) Prepare(model models.Model) {
b.Network = bmd.Network.String()
b.OperationHash = bmd.OperationHash
b.Ptr = bmd.Ptr
b.Timestamp = bmd.Timestamp
b.Timestamp = bmd.Timestamp.UTC()
b.ValueStrings = bmd.ValueStrings
}
Loading

0 comments on commit 6a6f8d0

Please sign in to comment.