Skip to content

Commit

Permalink
PR and lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b committed Oct 24, 2023
1 parent 4fafddf commit c4951f6
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 230 deletions.
118 changes: 0 additions & 118 deletions db/gen/coredb/tmp_token_normalization_backfill.sql.go

This file was deleted.

10 changes: 0 additions & 10 deletions db/queries/core/tmp_token_normalization_backfill.sql

This file was deleted.

4 changes: 3 additions & 1 deletion publicapi/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ func (api FeedAPI) PostTokens(ctx context.Context, tokenIDs []persist.DBID, ment
Action: persist.ActionMentionCommunity,
MentionID: mention.ID,
})

if err != nil {
return "", err
}
default:
return "", fmt.Errorf("invalid mention type: %+v", mention)
}
Expand Down
13 changes: 9 additions & 4 deletions publicapi/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,15 +740,15 @@ func (api InteractionAPI) GetAdmireByActorIDAndPostID(ctx context.Context, actor
func (api InteractionAPI) GetAdmireByActorIDAndTokenID(ctx context.Context, actorID persist.DBID, tokenID persist.DBID) (*db.Admire, error) {
// Validate
if err := validate.ValidateFields(api.validator, validate.ValidationMap{
"actorID": validate.WithTag(actorID, "required"),
"actorID": validate.WithTag(actorID, "required"),
"tokenID": validate.WithTag(tokenID, "required"),
}); err != nil {
return nil, err
}

admire, err := api.loaders.AdmireByActorIDAndTokenID.Load(db.GetAdmireByActorIDAndTokenIDParams{
ActorID: actorID,
TokenID: tokenID,
ActorID: actorID,
TokenID: tokenID,
})

if err != nil {
Expand Down Expand Up @@ -991,6 +991,9 @@ func (api InteractionAPI) comment(ctx context.Context, comment string, feedEvent
comment = validate.SanitizationPolicy.Sanitize(comment)

dbMentions, err := mentionInputsToMentions(ctx, mentions, api.queries)
if err != nil {
return "", err
}

commentID, resultMentions, err := api.repos.CommentRepository.CreateComment(ctx, feedEventID, postID, actor, replyToID, comment, dbMentions)
if err != nil {
Expand Down Expand Up @@ -1076,7 +1079,9 @@ func (api InteractionAPI) comment(ctx context.Context, comment string, feedEvent
MentionID: mention.ID,
Action: persist.ActionMentionCommunity,
})

if err != nil {
return "", err
}
default:
return "", fmt.Errorf("invalid mention type: %+v", mention)
}
Expand Down
9 changes: 0 additions & 9 deletions publicapi/merch.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ var uriToMerchType = map[string]int{
"ipfs://QmSPdA9Gg8xAdVxWvUyGkdFKQ8YMVYnGjYcr3cGMcBH1ae": merchTypeCard,
}

type merchAttribute struct {
TraitType string `json:"trait_type"`
Value string `json:"value"`
}

type merchMetadata struct {
Attributes []merchAttribute `json:"attributes"`
}

type MerchAPI struct {
repos *postgres.Repositories
queries *db.Queries
Expand Down
3 changes: 3 additions & 0 deletions publicapi/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ func (api TokenAPI) UpdateTokenInfo(ctx context.Context, tokenID persist.DBID, c
OwnerUserID: userID,
CollectorsNote: util.ToNullString(collectorsNote, true),
})
if err != nil {
return err
}

galleryID, err := api.queries.GetGalleryIDByCollectionID(ctx, collectionID)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions server/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,6 @@ func newTokenMetadataCache() *tokenMetadataCache {

func newManagedTokens(ctx context.Context, tm *tokenmanage.Manager) multichain.SubmitTokensF {
return func(ctx context.Context, tokenDefinitionIDs []persist.DBID) error {
if len(tokenDefinitionIDs) == 0 {
return nil
}
return tm.SubmitBatch(ctx, tokenDefinitionIDs)
}
}
3 changes: 0 additions & 3 deletions server/wire_gen.go

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

26 changes: 0 additions & 26 deletions service/persist/postgres/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,32 +382,6 @@ func (t *TokenRepository) Upsert(pCtx context.Context, pToken persist.Token) err
return err
}

// UpdateByID updates a token by its ID
func (t *TokenRepository) UpdateByID(pCtx context.Context, pID persist.DBID, pUpdate interface{}) error {

var res sql.Result
var err error
switch update := pUpdate.(type) {
case persist.TokenUpdateOwnerInput:
res, err = t.updateOwnerUnsafeStmt.ExecContext(pCtx, update.OwnerAddress, []persist.AddressAtBlock{{Address: persist.Address(update.OwnerAddress), Block: update.BlockNumber}}, update.BlockNumber, pID)
case persist.TokenUpdateBalanceInput:
res, err = t.updateBalanceUnsafeStmt.ExecContext(pCtx, update.Quantity, update.BlockNumber, pID)
default:
return fmt.Errorf("unsupported update type: %T", pUpdate)
}
if err != nil {
return err
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return persist.ErrTokenNotFoundByID{ID: pID}
}
return nil
}

// MostRecentBlock returns the most recent block number of any token
func (t *TokenRepository) MostRecentBlock(pCtx context.Context) (persist.BlockNumber, error) {
var blockNumber persist.BlockNumber
Expand Down
1 change: 0 additions & 1 deletion service/persist/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ type TokenRepository interface {
DeleteByID(context.Context, DBID) error
BulkUpsert(context.Context, []Token) error
Upsert(context.Context, Token) error
UpdateByID(context.Context, DBID, interface{}) error
MostRecentBlock(context.Context) (BlockNumber, error)
TokenExistsByTokenIdentifiers(context.Context, TokenID, EthereumAddress) (bool, error)
}
Expand Down
34 changes: 0 additions & 34 deletions service/persist/token_gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,11 @@ package persist

import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/lib/pq"
)

type AddressAtBlockList []AddressAtBlock

func (l AddressAtBlockList) Value() (driver.Value, error) {
return pq.Array(l).Value()
}

// Scan implements the Scanner interface for the DBIDList type
func (l *AddressAtBlockList) Scan(value interface{}) error {
return pq.Array(l).Scan(value)
}

// AddressAtBlock represents an address at a specific block
type AddressAtBlock struct {
Address Address `json:"address"`
Block BlockNumber `json:"block"`
}

// TokenIdentifiers represents a unique identifier for a token
type TokenIdentifiers struct {
TokenID TokenID `json:"token_id"`
Expand Down Expand Up @@ -115,17 +95,3 @@ func NewContractIdentifiers(pContractAddress Address, pChain Chain) ContractIden
Chain: pChain,
}
}

// Scan implements the database/sql Scanner interface for the AddressAtBlock type
func (a *AddressAtBlock) Scan(src interface{}) error {
if src == nil {
*a = AddressAtBlock{}
return nil
}
return json.Unmarshal(src.([]uint8), a)
}

// Value implements the database/sql/driver Valuer interface for the AddressAtBlock type
func (a AddressAtBlock) Value() (driver.Value, error) {
return json.Marshal(a)
}
17 changes: 11 additions & 6 deletions service/tokenmanage/tokenmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/mikeydub/go-gallery/service/throttle"
)

type MaxRetryF func(tID persist.TokenIdentifiers) int

type Manager struct {
cache *redis.Cache
processRegistry *registry
Expand All @@ -22,7 +24,7 @@ type Manager struct {
// delayer sets the linear delay for retrying tokens up to MaxRetries
delayer *limiters.KeyRateLimiter
// maxRetryF is a function that defines the maximum number of times a token can be reenqueued before it is not retried again
maxRetryF func(id persist.DBID) int
maxRetryF MaxRetryF
}

func New(ctx context.Context, taskClient *cloudtasks.Client) *Manager {
Expand All @@ -35,7 +37,7 @@ func New(ctx context.Context, taskClient *cloudtasks.Client) *Manager {
}
}

func NewWithRetries(ctx context.Context, taskClient *cloudtasks.Client, maxRetryF func(persist.DBID) int) *Manager {
func NewWithRetries(ctx context.Context, taskClient *cloudtasks.Client, maxRetryF MaxRetryF) *Manager {
m := New(ctx, taskClient)
m.maxRetryF = maxRetryF
m.delayer = limiters.NewKeyRateLimiter(ctx, m.cache, "retry", 2, 1*time.Minute)
Expand All @@ -51,7 +53,7 @@ func (m Manager) Processing(ctx context.Context, tDefID persist.DBID) bool {
// StartProcessing marks a token as processing. It returns a callback that must be called when work on the token is finished in order to mark
// it as finished. If withRetry is true, the callback will attempt to reenqueue the token if an error is passed. attemps is ignored when MaxRetries
// is set to the default value of 0.
func (m Manager) StartProcessing(ctx context.Context, tDefID persist.DBID, attempts int) (func(err error) error, error) {
func (m Manager) StartProcessing(ctx context.Context, tDefID persist.DBID, tID persist.TokenIdentifiers, attempts int) (func(err error) error, error) {
err := m.throttle.Lock(ctx, "lock:"+tDefID.String())
if err != nil {
return nil, err
Expand All @@ -78,7 +80,7 @@ func (m Manager) StartProcessing(ctx context.Context, tDefID persist.DBID, attem
callback := func(err error) error {
close(stop)
<-done
m.tryRetry(ctx, tDefID, err, attempts)
m.tryRetry(ctx, tDefID, tID, err, attempts)
m.throttle.Unlock(ctx, "lock:"+tDefID.String())
return nil
}
Expand All @@ -88,14 +90,17 @@ func (m Manager) StartProcessing(ctx context.Context, tDefID persist.DBID, attem

// SubmitBatch enqueues tokens for processing.
func (m Manager) SubmitBatch(ctx context.Context, tDefIDs []persist.DBID) error {
if len(tDefIDs) == 0 {
return nil
}
m.processRegistry.setManyEnqueue(ctx, tDefIDs)
message := task.TokenProcessingBatchMessage{BatchID: persist.GenerateID(), TokenDefinitionIDs: tDefIDs}
logger.For(ctx).WithField("batchID", message.BatchID).Infof("enqueued batch: %s (size=%d)", message.BatchID, len(tDefIDs))
return task.CreateTaskForTokenProcessing(ctx, message, m.taskClient)
}

func (m Manager) tryRetry(ctx context.Context, tDefID persist.DBID, err error, attempts int) error {
if err == nil || m.maxRetryF == nil || attempts >= m.maxRetryF(tDefID) {
func (m Manager) tryRetry(ctx context.Context, tDefID persist.DBID, tID persist.TokenIdentifiers, err error, attempts int) error {
if err == nil || m.maxRetryF == nil || attempts >= m.maxRetryF(tID) {
m.processRegistry.finish(ctx, tDefID)
return nil
}
Expand Down
Loading

0 comments on commit c4951f6

Please sign in to comment.