Skip to content

Commit

Permalink
Merge pull request #49 from everFinance/feature/inser-manifest-url
Browse files Browse the repository at this point in the history
Feature/inser manifest url
  • Loading branch information
zyjblockchain authored Aug 29, 2022
2 parents 95fb42f + 69555d6 commit 1f2467a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 18 deletions.
41 changes: 39 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (s *Arseeding) runAPI(port string) {
v1.GET("/bundle/fee/:size/:currency", s.bundleFee)
v1.GET("/bundle/orders/:signer", s.getOrders)
v1.GET("/:id", s.dataRoute) // get arTx data or bundleItem data
if s.EnableManifest {
v1.POST("/manifest_url/:id", s.setManifestUrl)
}

// submit native data with X-API-KEY
v1.POST("/bundle/data", s.submitNativeData)
Expand All @@ -91,7 +94,7 @@ func (s *Arseeding) runAPI(port string) {
func (s *Arseeding) arseedInfo(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Name": "Arseeding",
"Version": "v1.0.16",
"Version": "v1.0.18",
"Documentation": "https://web3infra.dev",
})
}
Expand Down Expand Up @@ -528,6 +531,7 @@ func (s *Arseeding) submitItem(c *gin.Context) {

c.JSON(http.StatusOK, schema.RespOrder{
ItemId: ord.ItemId,
Size: ord.Size,
Bundler: s.bundler.Signer.Address,
Currency: ord.Currency,
Decimals: ord.Decimals,
Expand Down Expand Up @@ -604,7 +608,7 @@ func (s *Arseeding) submitNativeData(c *gin.Context) {
return
}

c.JSON(http.StatusOK, schema.RespItemId{ItemId: order.ItemId})
c.JSON(http.StatusOK, schema.RespItemId{ItemId: order.ItemId, Size: order.Size})
}

func (s *Arseeding) getOrdersByApiKey(c *gin.Context) {
Expand Down Expand Up @@ -774,6 +778,39 @@ func (s *Arseeding) dataRoute(c *gin.Context) {
}
}

func (s *Arseeding) setManifestUrl(c *gin.Context) {
txId := c.Param("id")
mfUrl := expectedTxSandbox(txId)
if mfId, err := s.wdb.GetManifestId(mfUrl); err == nil {
c.JSON(http.StatusOK, schema.Manifest{
ManifestUrl: mfUrl,
ManifestId: mfId,
})
return
}

tags, err := getArTxOrItemTags(txId, s.store)
if err != nil {
internalErrorResponse(c, err.Error())
return
}
if s.EnableManifest && getTagValue(tags, schema.ContentType) == schema.ManifestType {
// insert new record
if err = s.wdb.InsertManifest(schema.Manifest{
ManifestUrl: mfUrl,
ManifestId: txId,
}); err != nil {
internalErrorResponse(c, err.Error())
return
}
}

c.JSON(http.StatusOK, schema.Manifest{
ManifestUrl: mfUrl,
ManifestId: txId,
})
}

func getTagValue(tags []types.Tag, name string) string {
for _, tg := range tags {
if tg.Name == name {
Expand Down
6 changes: 6 additions & 0 deletions jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,12 @@ func (s *Arseeding) processExpiredOrd() {
// delete bundle item from store
if err = s.DelItem(ord.ItemId); err != nil {
log.Error("DelItem", "err", err, "itemId", ord.ItemId)
continue
}
// delete manifest table
if err = s.wdb.DelManifest(ord.ItemId); err != nil {
log.Error("s.wdb.DelManifest", "err", err, "itemId", ord.ItemId)
continue
}
}
}
Expand Down
40 changes: 28 additions & 12 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ func handleManifest(maniData []byte, path string, db *Store) ([]types.Tag, []byt
}

func getArTxOrItemData(id string, db *Store) (decodeTags []types.Tag, data []byte, err error) {
// find arId
txMeta, err := db.LoadTxMeta(id)
if err == nil { // arTx id
data, err = txDataByMeta(txMeta, db)
if err != nil {
return
}
decodeTags, err = utils.TagsDecode(txMeta.Tags)
return
}

// not arId
// find bundle item
itemBinary, err := db.LoadItemBinary(id)
if err == nil {
Expand All @@ -61,10 +49,38 @@ func getArTxOrItemData(id string, db *Store) (decodeTags []types.Tag, data []byt
data, err = utils.Base64Decode(item.Data)
return
}

// not bundle item
// find arId
txMeta, err := db.LoadTxMeta(id)
if err == nil { // arTx id
data, err = txDataByMeta(txMeta, db)
if err != nil {
return
}
decodeTags, err = utils.TagsDecode(txMeta.Tags)
return
}
// txId not found in local, need proxy to gateway
return nil, nil, schema.ErrLocalNotExist
}

func getArTxOrItemTags(id string, db *Store) (decodeTags []types.Tag, err error) {
itemMeta, err := db.LoadItemMeta(id)
if err == nil {
decodeTags = itemMeta.Tags
return
}
// not bundle item
// find arId
txMeta, err := db.LoadTxMeta(id)
if err == nil { // arTx id
decodeTags, err = utils.TagsDecode(txMeta.Tags)
return
}
return nil, schema.ErrLocalNotExist
}

func getBundleItemData(id string, db *Store) (decodeTags []types.Tag, data []byte, err error) {
itemBinary, err := db.LoadItemBinary(id)
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion schema/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const (
)

type RespOrder struct {
ItemId string `json:"itemId"` // bundleItem id
ItemId string `json:"itemId"` // bundleItem id
Size int64 `json:"size"`
Bundler string `json:"bundler"` // fee receiver address
Currency string `json:"currency"` // payment token symbol
Decimals int `json:"decimals"`
Expand All @@ -22,6 +23,7 @@ type RespOrder struct {

type RespItemId struct {
ItemId string `json:"itemId"` // bundleItem id
Size int64 `json:"size"`
}

type Fee struct {
Expand Down
6 changes: 3 additions & 3 deletions schema/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Resource struct {
}

type Manifest struct {
ID uint `gorm:"primarykey"`
ManifestUrl string `gorm:"index:idx1"`
ManifestId string // arId
ID uint `gorm:"primarykey" json:"-"`
ManifestUrl string `gorm:"index:idx1,unique" json:"manifestUrl"`
ManifestId string `json:"manifestId"` // arId
}
4 changes: 4 additions & 0 deletions wdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,7 @@ func (w *Wdb) GetManifestId(mfUrl string) (string, error) {
err := w.Db.Model(&schema.Manifest{}).Where("manifest_url = ?", mfUrl).Last(&res).Error
return res.ManifestId, err
}

func (w *Wdb) DelManifest(id string) error {
return w.Db.Where("manifest_id = ?", id).Delete(&schema.Manifest{}).Error
}

0 comments on commit 1f2467a

Please sign in to comment.