Skip to content

Commit

Permalink
fix 401 screner (#2792)
Browse files Browse the repository at this point in the history
* fix models
  • Loading branch information
golangisfun123 authored Jun 26, 2024
1 parent 8d92615 commit f60de8c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 51 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"editor.fontLigatures": "'calt', 'liga', 'ss01', 'ss02', 'ss03', 'ss04', 'ss05', 'ss06', 'ss07', 'ss08', 'ss09'",
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
Expand Down
10 changes: 7 additions & 3 deletions contrib/screener-api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,13 @@ func (c clientImpl) BlacklistAddress(ctx context.Context, appsecret string, appi

// BlackListBody is the json payload that represents a blacklisted address.
type BlackListBody struct {
Type string `json:"type"`
ID string `json:"id"`
Data string `json:"data"`
Type string `json:"type"`
ID string `json:"id"`
Data Data `json:"data"`
}

// Data is the data field in the BlackListBody.
type Data struct {
Address string `json:"address"`
Network string `json:"network"`
Tag string `json:"tag"`
Expand Down
13 changes: 6 additions & 7 deletions contrib/screener-api/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ type BlacklistedAddress struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`

Type string `gorm:"column:type" json:"type"`
ID string `gorm:"column:id;primary_key" json:"id"`
Data string `gorm:"column:data" json:"data"`
Address string `gorm:"column:address" json:"address"`
Network string `gorm:"column:network" json:"network"`
Tag string `gorm:"column:tag" json:"tag"`
Remark string `gorm:"column:remark" json:"remark"`
ID string `gorm:"column:id;primaryKey" json:"id"`
Type string `gorm:"column:type" json:"type"`
Address string `gorm:"column:address" json:"address"`
Network string `gorm:"column:network" json:"network"`
Tag string `gorm:"column:tag" json:"tag"`
Remark string `gorm:"column:remark" json:"remark"`
}
2 changes: 1 addition & 1 deletion contrib/screener-api/db/sql/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *Store) PutBlacklistedAddress(ctx context.Context, body db.BlacklistedAd
{Name: idName},
},
DoUpdates: clause.AssignmentColumns([]string{
idName, typeName, dataName, addressName, networkName, tagName, remarkName},
idName, typeName, dataAddressName, dataRemarkName, dataNetworkName, dataTagName},
),
}).Create(&body)
if dbTx.Error != nil {
Expand Down
22 changes: 10 additions & 12 deletions contrib/screener-api/db/sql/base/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import (
func init() {
namer := dbcommon.NewNamer(GetAllModels())

addressName = namer.GetConsistentName("Address")
typeName = namer.GetConsistentName("Type")
idName = namer.GetConsistentName("ID")
dataName = namer.GetConsistentName("Data")
networkName = namer.GetConsistentName("Network")
tagName = namer.GetConsistentName("Tag")
remarkName = namer.GetConsistentName("Remark")
dataAddressName = namer.GetConsistentName("Address")
dataNetworkName = namer.GetConsistentName("Network")
dataTagName = namer.GetConsistentName("Tag")
dataRemarkName = namer.GetConsistentName("Remark")
}

var (
addressName string
typeName string
idName string
dataName string
networkName string
tagName string
remarkName string
typeName string
idName string
dataAddressName string
dataNetworkName string
dataTagName string
dataRemarkName string
)
26 changes: 12 additions & 14 deletions contrib/screener-api/screener/screener.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,24 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {
span.SetAttributes(
attribute.String("type", blacklistBody.Type),
(attribute.String("id", blacklistBody.ID)),
(attribute.String("data", blacklistBody.Data)),
(attribute.String("network", blacklistBody.Network)),
(attribute.String("tag", blacklistBody.Tag)),
(attribute.String("remark", blacklistBody.Remark)),
(attribute.String("address", blacklistBody.Address)),
(attribute.String("network", blacklistBody.Data.Network)),
(attribute.String("tag", blacklistBody.Data.Tag)),
(attribute.String("remark", blacklistBody.Data.Remark)),
(attribute.String("address", blacklistBody.Data.Address)),
)

blacklistedAddress := db.BlacklistedAddress{
Type: blacklistBody.Type,
ID: blacklistBody.ID,
Data: blacklistBody.Data,
Network: blacklistBody.Network,
Tag: blacklistBody.Tag,
Remark: blacklistBody.Remark,
Address: strings.ToLower(blacklistBody.Address),
Network: blacklistBody.Data.Network,
Tag: blacklistBody.Data.Tag,
Remark: blacklistBody.Data.Remark,
Address: strings.ToLower(blacklistBody.Data.Address),
}

s.blacklistCacheMux.Lock()
defer s.blacklistCacheMux.Unlock()
s.blacklistCache[blacklistBody.Address] = true
s.blacklistCache[blacklistBody.Data.Address] = true

switch blacklistBody.Type {
case "create":
Expand All @@ -306,7 +304,7 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Address)))
span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
return

Expand All @@ -317,7 +315,7 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Address)))
span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
return

Expand All @@ -328,7 +326,7 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Address)))
span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
return

Expand Down
22 changes: 8 additions & 14 deletions contrib/screener-api/screener/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package screener_test
import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -198,23 +197,18 @@ func blacklistTestWithOperation(t *testing.T, operation string, apiClient client
return statuses, fmt.Errorf("error generating random number: %w", err)
}

dataMap := map[string]string{"key": fmt.Sprintf("value-%d", randomNumber)}
dataStr, err := json.Marshal(dataMap)
if err != nil {
return statuses, fmt.Errorf("error marshaling data: %w", err)
}

var body client.BlackListBody

if operation == "create" || operation == "update" {
body = client.BlackListBody{
Type: operation,
ID: fmt.Sprintf("unique-id-%d", randomNumber),
Data: string(dataStr),
Address: fmt.Sprintf("address-%d", randomNumber),
Network: fmt.Sprintf("network-%d", randomNumber),
Tag: fmt.Sprintf("tag-%d", randomNumber),
Remark: "remark",
Type: operation,
ID: fmt.Sprintf("unique-id-%d", randomNumber),
Data: client.Data{
Address: fmt.Sprintf("address-%d", randomNumber),
Network: fmt.Sprintf("network-%d", randomNumber),
Tag: fmt.Sprintf("tag-%d", randomNumber),
Remark: "remark",
},
}
} else {
body = client.BlackListBody{
Expand Down

0 comments on commit f60de8c

Please sign in to comment.