Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add strict linters #11

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ linters:
enable:
- asasalint
- asciicheck
- cyclop
- bodyclose
- durationcheck
- errcheck
- errorlint
- gci
- gocognit
- gocritic
- gocyclo
- gosec
- govet
Expand All @@ -34,3 +35,16 @@ linters:
issues:
max-issues-per-linter: 0 # Unlimited
max-same-issues: 0 # Unlimited

linters-settings:
govet:
enable-all: true
disable:
- shadow
- fieldalignment
gocognit:
min-complexity: 20
gocyclo:
min-complexity: 20
gocritic:
disabled-tags: ["opinionated", "experimental"]
72 changes: 41 additions & 31 deletions internal/database/dialer/cockroachdb/client_billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,44 @@ func (c *client) SaveBillingRecordCollected(ctx context.Context, billingRecord *
return c.database.WithContext(ctx).Create(&value).Error
}

func (c *client) prepareBillingCollectTokensCreateOrUpdateConsumptionLog(nowTime time.Time, k *table.GatewayKey, tx *gorm.DB) error {
var possibleExistLog table.GatewayConsumptionLog
err := tx.Where("consumption_date = ? AND key_id = ?", nowTime, k.ID).
First(&possibleExistLog).
Error

if err != nil {
// nolint: gocritic
if errors.Is(err, gorm.ErrRecordNotFound) {
// Fine, let's create it
err = tx.Create(&table.GatewayConsumptionLog{
KeyID: k.ID,
ConsumptionDate: nowTime,
RuUsed: k.RuUsedCurrent,
APICalls: k.APICallsCurrent,
}).Error
} else {
// TODO: Error happens, but we don't know what's this, create a new record for now.
err = tx.Create(&table.GatewayConsumptionLog{
KeyID: k.ID,
ConsumptionDate: nowTime,
RuUsed: k.RuUsedCurrent,
APICalls: k.APICallsCurrent,
}).Error
}
} else {
// Already exists - this shouldn't happen; but when it happens, it happens
err = tx.Model(&table.GatewayConsumptionLog{}).
Where("id = ?", possibleExistLog.ID).
Updates(map[string]interface{}{
"ru_used": gorm.Expr("ru_used + ?", k.RuUsedCurrent),
"api_calls": gorm.Expr("api_calls + ?", k.APICallsCurrent),
}).Error
}

return err
}

func (c *client) PrepareBillingCollectTokens(ctx context.Context, nowTime time.Time) (*map[common.Address]schema.BillingCollectDataPerAddress, error) {
// Get all keys whose ru_used_current is > 0
var activeKeys []table.GatewayKey
Expand All @@ -66,38 +104,10 @@ func (c *client) PrepareBillingCollectTokens(ctx context.Context, nowTime time.T
for _, k := range activeKeys {
// w/ database tx
err = c.database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
k := k

// Create or update consumption log
var possibleExistLog table.GatewayConsumptionLog
err = tx.Where("consumption_date = ? AND key_id = ?", nowTime, k.ID).
First(&possibleExistLog).
Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// Fine, let's create it
err = tx.Create(&table.GatewayConsumptionLog{
KeyID: k.ID,
ConsumptionDate: nowTime,
RuUsed: k.RuUsedCurrent,
APICalls: k.APICallsCurrent,
}).Error
} else {
// Error happens, but we don't know what's this, create a new record for now.
err = tx.Create(&table.GatewayConsumptionLog{
KeyID: k.ID,
ConsumptionDate: nowTime,
RuUsed: k.RuUsedCurrent,
APICalls: k.APICallsCurrent,
}).Error
}
} else {
// Already exists - this shouldn't happen; but when it happens, it happens
err = tx.Model(&table.GatewayConsumptionLog{}).
Where("id = ?", possibleExistLog.ID).
Updates(map[string]interface{}{
"ru_used": gorm.Expr("ru_used + ?", k.RuUsedCurrent),
"api_calls": gorm.Expr("api_calls + ?", k.APICallsCurrent),
}).Error
}
err = c.prepareBillingCollectTokensCreateOrUpdateConsumptionLog(nowTime, &k, tx)
if err != nil {
zap.L().Error("create or update consumption log", zap.Error(err), zap.Any("key", k))
// but no need to stop here - data error can be fixed later, let's focus on billing now
Expand Down
4 changes: 2 additions & 2 deletions internal/service/hub/model/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func KeyCreate(ctx context.Context, accountAddress common.Address, keyName strin
return &Key{k, databaseClient, controlClient}, nil
}

func KeyGetByID(ctx context.Context, KeyID uint64, activeOnly bool, databaseClient *gorm.DB, controlClient *control.StateClientWriter) (*Key, bool, error) {
func KeyGetByID(ctx context.Context, keyID uint64, activeOnly bool, databaseClient *gorm.DB, controlClient *control.StateClientWriter) (*Key, bool, error) {
queryBase := databaseClient.WithContext(ctx).Model(&table.GatewayKey{})

if activeOnly {
Expand All @@ -59,7 +59,7 @@ func KeyGetByID(ctx context.Context, KeyID uint64, activeOnly bool, databaseClie

var k table.GatewayKey

if err := queryBase.Where("id = ?", KeyID).First(&k).Error; err != nil {
if err := queryBase.Where("id = ?", keyID).First(&k).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, false, nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/service/hub/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (

type Opts func(*config)

//func SetSwaggerHost(host string) Opts {
// func SetSwaggerHost(host string) Opts {
// return func(o *config) {
// o.SwaggerHost = host
// }
//}
//
//func SetSwaggerAuthorizer(f func(*http.Request) bool) Opts {
// func SetSwaggerAuthorizer(f func(*http.Request) bool) Opts {
// return func(o *config) {
// o.Authorizer = f
// }
//}
//
//func SetSwaggerIsHTTPS(b bool) Opts {
// func SetSwaggerIsHTTPS(b bool) Opts {
// return func(o *config) {
// o.IsHTTPS = b
// }
Expand Down
7 changes: 4 additions & 3 deletions internal/service/indexer/l2/report_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ func (s *server) sendNotificationMessage(txErr error, txHash string, txFunc stri
// Send base information
var errReason string

if errors.Is(txErr, errors.New("transaction failed")) {
switch {
case errors.Is(txErr, errors.New("transaction failed")):
errReason = "Failed"
} else if errors.Is(txErr, context.DeadlineExceeded) {
case errors.Is(txErr, context.DeadlineExceeded):
errReason = "Timeout (/!\\ doesn't mean it failed)"
} else {
default:
// Unknown error
errReason = txErr.Error()
}
Expand Down
Loading