Skip to content

Commit

Permalink
Fix: cache
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Mar 22, 2022
1 parent 25a6626 commit 8c21688
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
6 changes: 6 additions & 0 deletions cmd/indexer/indexer/indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ func (bi *BoostIndexer) createIndices() {
logger.Error().Err(err).Msg("can't create index")
}

if _, err := bi.Context.StorageDB.DB.Model((*operation.Operation)(nil)).Exec(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS operations_status_idx ON ?TableName (status)
`); err != nil {
logger.Error().Err(err).Msg("can't create index")
}

// Scripts
if _, err := bi.Context.StorageDB.DB.Model((*contract.Script)(nil)).Exec(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS scripts_project_id_idx ON ?TableName (project_id)
Expand Down
8 changes: 7 additions & 1 deletion cmd/metrics/services/contract_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"sync"

"github.com/baking-bad/bcdhub/internal/bcd/ast"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/handlers"
"github.com/baking-bad/bcdhub/internal/logger"
Expand Down Expand Up @@ -42,11 +43,16 @@ func (cm *ContractMetadataHandler) Handle(ctx context.Context, items []models.Mo
return errors.Errorf("[ContractMetadata.Handle] invalid type: expected *bigmapdiff.BigMapDiff got %T", items[i])
}

storageType, err := cm.Cache.StorageType(bmd.Network, bmd.Contract, bmd.Protocol.SymLink)
storageTypeBytes, err := cm.Cache.StorageTypeBytes(bmd.Network, bmd.Contract, bmd.Protocol.SymLink)
if err != nil {
return errors.Errorf("[ContractMetadata.Handle] can't get storage type for '%s' in %s: %s", bmd.Contract, bmd.Network.String(), err)
}

storageType, err := ast.NewTypedAstFromBytes(storageTypeBytes)
if err != nil {
return errors.Errorf("[ContractMetadata.Handle] can't parse storage type for '%s' in %s: %s", bmd.Contract, bmd.Network.String(), err)
}

localWg.Add(1)
go func() {
defer localWg.Done()
Expand Down
8 changes: 7 additions & 1 deletion cmd/metrics/services/token_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"sync"

"github.com/baking-bad/bcdhub/internal/bcd/ast"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/handlers"
"github.com/baking-bad/bcdhub/internal/logger"
Expand Down Expand Up @@ -41,11 +42,16 @@ func (tm *TokenMetadataHandler) Handle(ctx context.Context, items []models.Model
return errors.Errorf("[TokenMetadata.Handle] invalid type: expected *domains.BigMapDiff got %T", items[i])
}

storageType, err := tm.Cache.StorageType(bmd.Network, bmd.Contract, bmd.Protocol.SymLink)
storageTypeBytes, err := tm.Cache.StorageTypeBytes(bmd.Network, bmd.Contract, bmd.Protocol.SymLink)
if err != nil {
return errors.Errorf("[TokenMetadata.Handle] can't get storage type for '%s' in %s: %s", bmd.Contract, bmd.Network.String(), err)
}

storageType, err := ast.NewTypedAstFromBytes(storageTypeBytes)
if err != nil {
return errors.Errorf("[TokenMetadata.Handle] can't parse storage type for '%s' in %s: %s", bmd.Contract, bmd.Network.String(), err)
}

localWg.Add(1)
go func() {
defer localWg.Done()
Expand Down
36 changes: 25 additions & 11 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/baking-bad/bcdhub/internal/bcd"
"github.com/baking-bad/bcdhub/internal/bcd/ast"
"github.com/baking-bad/bcdhub/internal/bcd/consts"
"github.com/baking-bad/bcdhub/internal/models/account"
"github.com/baking-bad/bcdhub/internal/models/block"
Expand Down Expand Up @@ -35,7 +34,7 @@ type Cache struct {
// NewCache -
func NewCache(rpc map[types.Network]noderpc.INode, blocks block.Repository, accounts account.Repository, contracts contract.Repository, protocols protocol.Repository, cm contract_metadata.Repository, sanitizer *bluemonday.Policy) *Cache {
return &Cache{
ccache.New(ccache.Configure().MaxSize(100000)),
ccache.New(ccache.Configure().MaxSize(1000)),
rpc,
blocks,
accounts,
Expand Down Expand Up @@ -124,6 +123,26 @@ func (cache *Cache) Contract(network types.Network, address string) (*contract.C
return &cntr, nil
}

// ContractTags -
func (cache *Cache) ContractTags(network types.Network, address string) (types.Tags, error) {
if !bcd.IsContract(address) {
return 0, nil
}

key := fmt.Sprintf("contract:%d:%s", network, address)
item, err := cache.Fetch(key, time.Minute*10, func() (interface{}, error) {
c, err := cache.contracts.Get(network, address)
if err != nil {
return 0, err
}
return c.Tags, nil
})
if err != nil {
return 0, err
}
return item.Value().(types.Tags), nil
}

// ProjectIDByHash -
func (cache *Cache) ProjectIDByHash(hash string) string {
return fmt.Sprintf("project_id:%s", hash)
Expand Down Expand Up @@ -151,7 +170,6 @@ func (cache *Cache) TezosBalance(network types.Network, address string, level in

key := fmt.Sprintf("tezos_balance:%d:%s:%d", network, address, level)
item, err := cache.Fetch(key, 30*time.Second, func() (interface{}, error) {

return node.GetContractBalance(address, level)
})
if err != nil {
Expand Down Expand Up @@ -180,24 +198,20 @@ func (cache *Cache) ScriptBytes(network types.Network, address, symLink string)
return item.Value().([]byte), nil
}

// StorageType -
func (cache *Cache) StorageType(network types.Network, address, symLink string) (*ast.TypedAst, error) {
// StorageTypeBytes -
func (cache *Cache) StorageTypeBytes(network types.Network, address, symLink string) ([]byte, error) {
if !bcd.IsContract(address) {
return nil, nil
}

key := fmt.Sprintf("storage:%d:%s", network, address)
item, err := cache.Fetch(key, 5*time.Minute, func() (interface{}, error) {
data, err := cache.contracts.ScriptPart(network, address, symLink, consts.STORAGE)
if err != nil {
return nil, err
}
return ast.NewTypedAstFromBytes(data)
return cache.contracts.ScriptPart(network, address, symLink, consts.STORAGE)
})
if err != nil {
return nil, err
}
return item.Value().(*ast.TypedAst), nil
return item.Value().([]byte), nil
}

// ProtocolByID -
Expand Down
16 changes: 8 additions & 8 deletions internal/parsers/operations/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ func setTags(ctx *config.Context, contract *contract.Contract, op *operation.Ope
return nil
}

var tags types.Tags
if contract == nil {
c, err := ctx.Cache.Contract(op.Network, op.Destination.Address)
c, err := ctx.Cache.ContractTags(op.Network, op.Destination.Address)
if err != nil {
if ctx.Storage.IsRecordNotFound(err) {
return nil
}
return err
}
if c == nil {
return nil
}
contract = c
tags = c
} else {
tags = contract.Tags
}

if contract.Tags.Has(types.FA12Tag) {
if tags.Has(types.FA12Tag) {
op.Tags.Set(types.FA12Tag)
}
if contract.Tags.Has(types.FA2Tag) {
if tags.Has(types.FA2Tag) {
op.Tags.Set(types.FA2Tag)
}
if contract.Tags.Has(types.LedgerTag) {
if tags.Has(types.LedgerTag) {
op.Tags.Set(types.LedgerTag)
}
return nil
Expand Down

0 comments on commit 8c21688

Please sign in to comment.