Skip to content

Commit

Permalink
Added: golangci-lint config. Fixed all issues
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanSerikov authored and aopoltorzhicky committed Oct 27, 2020
1 parent 2221f02 commit f863ebf
Show file tree
Hide file tree
Showing 72 changed files with 339 additions and 341 deletions.
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
linters:
enable:
- bodyclose
- goconst
- gocritic
- gofmt
- interfacer
- maligned
- prealloc
- unconvert
- unparam
2 changes: 1 addition & 1 deletion cmd/api/handlers/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewContext(cfg config.Config) (*Context, error) {
mapTokens[tokenKey{
Network: tokens[i].Network,
Contract: tokens[i].Address,
TokenID: int64(tokens[i].TokenID),
TokenID: tokens[i].TokenID,
}] = TokenMetadata{
Contract: tokens[i].Address,
TokenID: tokens[i].TokenID,
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (ctx *Context) appendDAppInfo(dapp *tzip.DApp, withDetails bool) (DApp, err
tokenMetadata, err := ctx.ES.GetTokenMetadata(elastic.GetTokenMetadataContext{
Contract: token.Contract,
Network: consts.Mainnet,
TokenID: int64(token.TokenID),
TokenID: token.TokenID,
})
if err != nil {
if elastic.IsRecordNotFound(err) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/api/handlers/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func (ctx *Context) getMempoolEvents(subscriptions []database.Subscription) ([]e

for _, item := range res.Array() {
status := item.Get("status").String()
if status == "applied" {
status = "pending"
if status == consts.Applied {
status = "pending" //nolint
}

op := elastic.EventOperation{
Expand Down Expand Up @@ -150,7 +150,7 @@ func (ctx *Context) getMempoolEvents(subscriptions []database.Subscription) ([]e
return events, err
}
} else {
op.Entrypoint = "default"
op.Entrypoint = consts.DefaultEntrypoint
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/api/handlers/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (ctx *Context) prepareMempoolOperations(res gjson.Result, address, network
}
for _, item := range res.Array() {
status := item.Get("status").String()
if status == "applied" {
if status == consts.Applied {
status = "pending"
}

Expand Down Expand Up @@ -108,7 +108,7 @@ func (ctx *Context) prepareMempoolOperations(res gjson.Result, address, network
}
}
} else {
op.Entrypoint = "default"
op.Entrypoint = consts.DefaultEntrypoint
}
}

Expand Down
43 changes: 17 additions & 26 deletions cmd/api/handlers/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ func (ctx *Context) GetOperation(c *gin.Context) {
}

if len(op) == 0 {
operation, err := ctx.getOperationFromMempool(req.Hash)
if handleError(c, err, http.StatusNotFound) {
return
}

operation := ctx.getOperationFromMempool(req.Hash)
c.JSON(http.StatusOK, []Operation{operation})
return
}
Expand Down Expand Up @@ -148,7 +144,7 @@ func (ctx *Context) GetOperationErrorLocation(c *gin.Context) {
c.JSON(http.StatusOK, response)
}

func (ctx *Context) getOperationFromMempool(hash string) (Operation, error) {
func (ctx *Context) getOperationFromMempool(hash string) Operation {
var wg sync.WaitGroup
var opCh = make(chan Operation, len(ctx.TzKTServices))

Expand All @@ -161,7 +157,7 @@ func (ctx *Context) getOperationFromMempool(hash string) (Operation, error) {

wg.Wait()

return <-opCh, nil
return <-opCh
}

func (ctx *Context) getOperation(network, hash string, ops chan<- Operation, wg *sync.WaitGroup) {
Expand All @@ -181,10 +177,7 @@ func (ctx *Context) getOperation(network, hash string, ops chan<- Operation, wg
return
}

operation, err := ctx.prepareMempoolOperation(res, network, hash)
if err != nil {
return
}
operation := ctx.prepareMempoolOperation(res, network)

ops <- operation
}
Expand Down Expand Up @@ -237,8 +230,8 @@ func prepareOperation(es elastic.IElastic, operation models.Operation, bmd []mod
if err := formatErrors(operation.Errors, &op); err != nil {
return op, err
}
if operation.DeffatedStorage != "" && strings.HasPrefix(op.Destination, "KT") && op.Status == "applied" {
if err := setStorageDiff(es, op.Destination, op.Network, operation.DeffatedStorage, &op, bmd); err != nil {
if operation.DeffatedStorage != "" && strings.HasPrefix(op.Destination, "KT") && op.Status == consts.Applied {
if err := setStorageDiff(es, op.Destination, operation.DeffatedStorage, &op, bmd); err != nil {
return op, err
}
}
Expand Down Expand Up @@ -290,7 +283,7 @@ func setParameters(es elastic.IElastic, parameters string, op *Operation) error
return nil
}

func setStorageDiff(es elastic.IElastic, address, network, storage string, op *Operation, bmd []models.BigMapDiff) error {
func setStorageDiff(es elastic.IElastic, address, storage string, op *Operation, bmd []models.BigMapDiff) error {
metadata, err := meta.GetContractMetadata(es, address)
if err != nil {
return err
Expand All @@ -317,13 +310,11 @@ func getStorageDiff(es elastic.IElastic, bmd []models.BigMapDiff, address, stora
exOp, err := es.GetLastOperation(address, prev.Network, prev.IndexedTime)
if err == nil {
exDeffatedStorage = exOp.DeffatedStorage
} else {
if !elastic.IsRecordNotFound(err) {
return nil, err
}
} else if !elastic.IsRecordNotFound(err) {
return nil, err
}

prevStorage, err = getEnrichStorageMiguel(es, prevBmd, prev.Protocol, prev.DeffatedStorage, exDeffatedStorage, metadata, isSimulating)
prevStorage, err = getEnrichStorageMiguel(prevBmd, prev.Protocol, prev.DeffatedStorage, exDeffatedStorage, metadata, isSimulating)
if err != nil {
return nil, err
}
Expand All @@ -335,7 +326,7 @@ func getStorageDiff(es elastic.IElastic, bmd []models.BigMapDiff, address, stora
prevStorage = nil
}

currentStorage, err := getEnrichStorageMiguel(es, bmd, op.Protocol, storage, prevDeffatedStorage, metadata, isSimulating)
currentStorage, err := getEnrichStorageMiguel(bmd, op.Protocol, storage, prevDeffatedStorage, metadata, isSimulating)
if err != nil {
return nil, err
}
Expand All @@ -347,7 +338,7 @@ func getStorageDiff(es elastic.IElastic, bmd []models.BigMapDiff, address, stora
return currentStorage, nil
}

func getEnrichStorageMiguel(es elastic.IElastic, bmd []models.BigMapDiff, protocol, storage, prevStorage string, metadata *meta.ContractMetadata, isSimulating bool) (*newmiguel.Node, error) {
func getEnrichStorageMiguel(bmd []models.BigMapDiff, protocol, storage, prevStorage string, metadata *meta.ContractMetadata, isSimulating bool) (*newmiguel.Node, error) {
store, err := enrichStorage(storage, prevStorage, bmd, protocol, false, isSimulating)
if err != nil {
return nil, err
Expand Down Expand Up @@ -379,11 +370,11 @@ func getPrevBmd(es elastic.IElastic, bmd []models.BigMapDiff, indexedTime int64,
return es.GetBigMapDiffsPrevious(bmd, indexedTime, address)
}

func (ctx *Context) prepareMempoolOperation(res gjson.Result, network, hash string) (Operation, error) {
func (ctx *Context) prepareMempoolOperation(res gjson.Result, network string) Operation {
item := res.Array()[0]

status := item.Get("status").String()
if status == "applied" {
if status == consts.Applied {
status = "pending"
}

Expand Down Expand Up @@ -411,18 +402,18 @@ func (ctx *Context) prepareMempoolOperation(res gjson.Result, network, hash stri
op.Errors = cerrors.ParseArray(item.Get("errors"))

if op.Kind != consts.Transaction {
return op, nil
return op
}

if strings.HasPrefix(op.Destination, "KT") && op.Protocol != "" {
if params := item.Get("parameters"); params.Exists() {
ctx.buildOperationParameters(params, &op)
} else {
op.Entrypoint = "default"
op.Entrypoint = consts.DefaultEntrypoint
}
}

return op, nil
return op
}

func (ctx *Context) buildOperationParameters(params gjson.Result, op *Operation) {
Expand Down
16 changes: 8 additions & 8 deletions cmd/api/handlers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,35 +123,35 @@ func (s subRequest) getMask() uint {
var b uint

if s.WatchSame {
b = b | WatchSame
b |= WatchSame
}

if s.WatchSimilar {
b = b | WatchSimilar
b |= WatchSimilar
}

if s.WatchMempool {
b = b | WatchMempool
b |= WatchMempool
}

if s.WatchMigrations {
b = b | WatchMigrations
b |= WatchMigrations
}

if s.WatchDeployments {
b = b | WatchDeployments
b |= WatchDeployments
}

if s.WatchCalls {
b = b | WatchCalls
b |= WatchCalls
}

if s.WatchErrors {
b = b | WatchErrors
b |= WatchErrors
}

if s.SentryEnabled {
b = b | SentryEnabled
b |= SentryEnabled
}

return b
Expand Down
45 changes: 20 additions & 25 deletions cmd/api/handlers/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,39 @@ type Error struct {

// Operation -
type Operation struct {
ID string `json:"id,omitempty"`
Protocol string `json:"protocol"`
Hash string `json:"hash,omitempty"`
Internal bool `json:"internal"`
Network string `json:"network"`
Timestamp time.Time `json:"timestamp"`

Level int64 `json:"level"`
Kind string `json:"kind"`
Source string `json:"source,omitempty"`
SourceAlias string `json:"source_alias,omitempty"`
Fee int64 `json:"fee,omitempty"`
Counter int64 `json:"counter,omitempty"`
GasLimit int64 `json:"gas_limit,omitempty"`
StorageLimit int64 `json:"storage_limit,omitempty"`
Amount int64 `json:"amount,omitempty"`
Balance int64 `json:"balance,omitempty"`
Burned int64 `json:"burned,omitempty"`
AllocatedDestinationContractBurned int64 `json:"allocated_destination_contract_burned,omitempty"`
IndexedTime int64 `json:"-"`
ContentIndex int64 `json:"content_index"`
Errors []cerrors.IError `json:"errors,omitempty"`
Result *OperationResult `json:"result,omitempty"`
Parameters interface{} `json:"parameters,omitempty"`
StorageDiff interface{} `json:"storage_diff,omitempty"`
RawMempool interface{} `json:"rawMempool"`
Timestamp time.Time `json:"timestamp"`
ID string `json:"id,omitempty"`
Protocol string `json:"protocol"`
Hash string `json:"hash,omitempty"`
Network string `json:"network"`
Kind string `json:"kind"`
Source string `json:"source,omitempty"`
SourceAlias string `json:"source_alias,omitempty"`
Destination string `json:"destination,omitempty"`
DestinationAlias string `json:"destination_alias,omitempty"`
PublicKey string `json:"public_key,omitempty"`
ManagerPubKey string `json:"manager_pubkey,omitempty"`
Balance int64 `json:"balance,omitempty"`
Delegate string `json:"delegate,omitempty"`
Status string `json:"status"`
Entrypoint string `json:"entrypoint,omitempty"`
Errors []cerrors.IError `json:"errors,omitempty"`
Burned int64 `json:"burned,omitempty"`
AllocatedDestinationContractBurned int64 `json:"allocated_destination_contract_burned,omitempty"`

Result *OperationResult `json:"result,omitempty"`

Parameters interface{} `json:"parameters,omitempty"`
StorageDiff interface{} `json:"storage_diff,omitempty"`
Mempool bool `json:"mempool"`

IndexedTime int64 `json:"-"`
ContentIndex int64 `json:"content_index"`

RawMempool interface{} `json:"rawMempool"`
Internal bool `json:"internal"`
Mempool bool `json:"mempool"`
}

// ParseJSON -
Expand Down
44 changes: 21 additions & 23 deletions cmd/api/handlers/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,26 @@ func getSearchFilters(req searchRequest) map[string]interface{} {

func postProcessing(result elastic.SearchResult) (elastic.SearchResult, error) {
for i := range result.Items {
switch result.Items[i].Type {
case elastic.DocBigMapDiff:
bmd := result.Items[i].Body.(models.BigMapDiff)
key, err := stringer.StringifyInterface(bmd.Key)
if err != nil {
return result, err
}

result.Items[i].Body = SearchBigMapDiff{
Ptr: bmd.Ptr,
Key: key,
KeyHash: bmd.KeyHash,
Value: bmd.Value,
Level: bmd.Level,
Address: bmd.Address,
Network: bmd.Network,
Timestamp: bmd.Timestamp,
FoundBy: bmd.FoundBy,
}
if result.Items[i].Type != elastic.DocBigMapDiff {
continue
}

bmd := result.Items[i].Body.(models.BigMapDiff)
key, err := stringer.StringifyInterface(bmd.Key)
if err != nil {
return result, err
}

result.Items[i].Body = SearchBigMapDiff{
Ptr: bmd.Ptr,
Key: key,
KeyHash: bmd.KeyHash,
Value: bmd.Value,
Level: bmd.Level,
Address: bmd.Address,
Network: bmd.Network,
Timestamp: bmd.Timestamp,
FoundBy: bmd.FoundBy,
}
}
return result, nil
Expand All @@ -121,10 +122,7 @@ func (ctx *Context) searchInMempool(q string) (elastic.SearchItem, error) {
return elastic.SearchItem{}, err
}

operation, err := ctx.getOperationFromMempool(q)
if err != nil {
return elastic.SearchItem{}, err
}
operation := ctx.getOperationFromMempool(q)

return elastic.SearchItem{
Type: elastic.DocOperations,
Expand Down
7 changes: 2 additions & 5 deletions cmd/api/handlers/transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ func (ctx *Context) GetContractTransfers(c *gin.Context) {
if handleError(c, err, 0) {
return
}
response, err := ctx.transfersPostprocessing(transfers)
if handleError(c, err, 0) {
return
}
response := ctx.transfersPostprocessing(transfers)
c.JSON(http.StatusOK, response)
}

func (ctx *Context) transfersPostprocessing(transfers elastic.TransfersResponse) (response TransferResponse, err error) {
func (ctx *Context) transfersPostprocessing(transfers elastic.TransfersResponse) (response TransferResponse) {
response.Total = transfers.Total
response.Transfers = make([]Transfer, len(transfers.Transfers))

Expand Down
Loading

0 comments on commit f863ebf

Please sign in to comment.