Skip to content

Commit

Permalink
Merge pull request #23 from bnb-chain/v2
Browse files Browse the repository at this point in the history
feat: add listing table to fix monitor interval mismatch between bsc and gnfd
  • Loading branch information
cosinlink authored May 16, 2024
2 parents 38774f6 + e9d0a77 commit 055b61b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/mind-marketplace-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ func main() {

categoryDao := dao.NewDbCategoryDao(db)
itemDao := dao.NewDbItemDao(db)
listingDao := dao.NewDbListingDao(db)

gnfdBlockDao := dao.NewDbGnfdBlockDao(db)
gnfdClient := monitor.NewGnfdCompositClients(config.GnfdRpcAddrs, config.GnfdChainId, false)
gnfdProcessor := monitor.NewGnfdBlockProcessor(gnfdClient, gnfdBlockDao, categoryDao, itemDao, db, metricServer,
gnfdProcessor := monitor.NewGnfdBlockProcessor(gnfdClient, gnfdBlockDao, categoryDao, itemDao, listingDao, db, metricServer,
config.GroupBucketRegex, config.GroupBucketPrefix, config.GroupObjectRegex, config.GroupObjectPrefix)
gnfdMonitor := monitor.NewMonitor(gnfdProcessor, config.GnfdStartHeight)

Expand Down
37 changes: 37 additions & 0 deletions dao/listing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dao

import (
"context"
"github.com/bnb-chain/mind-marketplace-backend/database"
"gorm.io/gorm"
)

type ListingDao interface {
Create(context context.Context, block *database.Listing) error
GetByGroupId(context context.Context, groupId int64) (database.Listing, error)
}

type DbListingDao struct {
db *gorm.DB
}

func NewDbListingDao(db *gorm.DB) *DbListingDao {
return &DbListingDao{
db: db,
}
}

func (dao *DbListingDao) Create(context context.Context, listing *database.Listing) error {
if err := dao.db.Create(listing).Error; err != nil {
return err
}
return nil
}

func (dao *DbListingDao) GetByGroupId(context context.Context, groupId int64) (database.Listing, error) {
var listing = database.Listing{}
if err := dao.db.Where("group_id = ?", groupId).Take(&listing).Error; err != nil {
return listing, err
}
return listing, nil
}
3 changes: 3 additions & 0 deletions database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func ConnectDBWithConfig(config *util.DBConfig) (*gorm.DB, error) {
if err = db.AutoMigrate(&GnfdBlock{}); err != nil {
panic(err)
}
if err = db.AutoMigrate(&Listing{}); err != nil {
panic(err)
}
return db.Debug(), nil
} else {
return nil, fmt.Errorf("dialect %s not supported", config.DBDialect)
Expand Down
1 change: 1 addition & 0 deletions database/data.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE SCHEMA IF NOT EXISTS `data-marketplace` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

use data-marketplace;
insert into categories(id, name) values (100, 'Uncategorized');
insert into categories(id, name) values (1, 'AI Model');
insert into categories(id, name) values (2, 'Code Resource');
Expand Down
12 changes: 12 additions & 0 deletions database/listing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package database

import (
"github.com/shopspring/decimal"
)

type Listing struct {
Id int64 `json:"id" gorm:"primaryKey"`
Price decimal.Decimal `json:"price" gorm:"type:decimal(65,0);"`
ListBscHeight uint64 `gorm:"NOT NULL;"`
GroupId int64 `json:"group_id" gorm:"uniqueIndex:idx_list_group_id;not null;"`
}
8 changes: 6 additions & 2 deletions monitor/bsc_block_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,12 @@ func (p *BscBlockProcessor) handleEventList(blockHeight uint64, l types.Log) (st
// item should be existed
_, err = p.itemDao.GetByGroupId(context.Background(), event.GroupId.Int64(), true)
if err != nil {
util.Logger.Errorf("processor: %s, fail to find item %d err: %s", p.Name(), event.GroupId.Int64(), err)
return "", err
util.Logger.Errorf("processor: %s, fail to find item %d err: %s, write to listing table", p.Name(), event.GroupId.Int64(), err)

// insert into a temp table: listing
rawSql := fmt.Sprintf("insert into listing (price, list_bsc_height, group_id) values (%s, %d, %d)",
event.Price, blockHeight, event.GroupId)
return rawSql, nil
}

// only list Objects, i.e., pictures
Expand Down
17 changes: 16 additions & 1 deletion monitor/gnfd_block_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type GnfdBlockProcessor struct {
blockDao dao.GnfdBlockDao
categoryDao dao.CategoryDao
itemDao dao.ItemDao
listingDao dao.ListingDao
db *gorm.DB
metricServer *metric.MetricService

Expand All @@ -38,7 +39,7 @@ type GnfdBlockProcessor struct {
}

func NewGnfdBlockProcessor(client *GnfdCompositeClients,
blockDao dao.GnfdBlockDao, categoryDao dao.CategoryDao, itemDao dao.ItemDao, db *gorm.DB,
blockDao dao.GnfdBlockDao, categoryDao dao.CategoryDao, itemDao dao.ItemDao, listingDao dao.ListingDao, db *gorm.DB,
metricServer *metric.MetricService,
groupBucketRegex string,
groupBucketPrefix string,
Expand All @@ -49,6 +50,7 @@ func NewGnfdBlockProcessor(client *GnfdCompositeClients,
blockDao: blockDao,
categoryDao: categoryDao,
itemDao: itemDao,
listingDao: listingDao,
db: db,
metricServer: metricServer,
groupBucketRegex: groupBucketRegex,
Expand Down Expand Up @@ -230,6 +232,19 @@ func (p *GnfdBlockProcessor) handleEventCreateGroup(blockHeight uint64, event ab
return rawSql, err
}

listing, err := p.listingDao.GetByGroupId(context.Background(), int64(createGroup.GroupId.Uint64()))
if err == nil && listing.Id > 0 {
return fmt.Sprintf("insert into items (category_id, group_id, group_name, name, owner_address, status, description, url, listed_at, created_gnfd_height, price)"+
" values (%d, %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %s)",
categoryId, createGroup.GroupId.Uint64(), createGroup.GroupName, resourceName, createGroup.Owner,
database.ItemListed, escape(extra.Desc), escape(extra.Url), block.Time.Unix(), blockHeight, listing.Price), nil
}

if err != nil {
util.Logger.Errorf("processor: %s, fail to get listing err: %s", p.Name(), err)
return rawSql, err
}

return fmt.Sprintf("insert into items (category_id, group_id, group_name, name, owner_address, status, description, url, listed_at, created_gnfd_height)"+
" values (%d, %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d)",
categoryId, createGroup.GroupId.Uint64(), createGroup.GroupName, resourceName, createGroup.Owner,
Expand Down

0 comments on commit 055b61b

Please sign in to comment.