From 7c9faf8c3f46a3c7d62f1bf1f8e66e098c0197e1 Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 8 Oct 2023 22:55:54 +0200 Subject: [PATCH] Linter update and some API improvements --- .golangci.yml | 6 +- Makefile | 2 +- cmd/api/handlers/requests.go | 127 +++++++++++---------- cmd/api/handlers/responses.go | 150 +++++++++++++------------ cmd/api/handlers/tickets.go | 16 ++- internal/bcd/raw_script.go | 8 +- internal/config/context.go | 2 +- internal/config/options.go | 2 +- internal/models/contract/constant.go | 6 +- internal/models/contract/repository.go | 8 +- internal/models/mock/ticket/mock.go | 12 +- internal/models/ticket/repository.go | 8 +- internal/postgres/tests/ticket_test.go | 11 +- internal/postgres/ticket/storage.go | 17 ++- scripts/bcdctl/rollback.go | 4 +- 15 files changed, 210 insertions(+), 169 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 9a0745881..7c762d18f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,7 +11,11 @@ linters: - ineffassign - containedctx - tenv + - musttag + - mirror + - tagalign + # - zerologlint run: - go: '1.19' + go: '1.21' timeout: 3m diff --git a/Makefile b/Makefile index 9356bc126..3bea78aa4 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ api: indexer: docker-compose up -d db - cd cmd/indexer && go build -a -installsuffix cgo -o ../../indexer . + cd cmd/indexer && go run . seo: ifeq ($(BCD_ENV), development) diff --git a/cmd/api/handlers/requests.go b/cmd/api/handlers/requests.go index e66b3b5e4..7b9c746d6 100644 --- a/cmd/api/handlers/requests.go +++ b/cmd/api/handlers/requests.go @@ -8,8 +8,8 @@ import ( ) type getAccountRequest struct { - Address string `uri:"address" binding:"required,address"` - Network string `uri:"network" binding:"required,network"` + Address string `binding:"required,address" uri:"address"` + Network string `binding:"required,network" uri:"network"` } // NetworkID - @@ -18,8 +18,8 @@ func (req getAccountRequest) NetworkID() types.Network { } type getContractRequest struct { - Address string `uri:"address" binding:"required,contract"` - Network string `uri:"network" binding:"required,network"` + Address string `binding:"required,contract" uri:"address"` + Network string `binding:"required,network" uri:"network"` } // NetworkID - @@ -34,7 +34,7 @@ type getContractCodeRequest struct { } type withStatsRequest struct { - Stats *bool `form:"stats,omitempty" binding:"omitempty"` + Stats *bool `binding:"omitempty" form:"stats,omitempty"` } // HasStats - @@ -44,21 +44,21 @@ func (req withStatsRequest) HasStats() bool { // CodeDiffLeg - type CodeDiffLeg struct { - Address string `json:"address" binding:"required,address"` - Network types.Network `json:"network" binding:"required,network"` + Address string `binding:"required,address" json:"address"` + Network types.Network `binding:"required,network" json:"network"` Protocol string `json:"protocol,omitempty"` Level int64 `json:"level,omitempty"` } // CodeDiffRequest - type CodeDiffRequest struct { - Left CodeDiffLeg `json:"left" binding:"required"` - Right CodeDiffLeg `json:"right" binding:"required"` + Left CodeDiffLeg `binding:"required" json:"left"` + Right CodeDiffLeg `binding:"required" json:"right"` } type getBigMapRequest struct { - Network string `uri:"network" binding:"required,network"` - Ptr int64 `uri:"ptr" binding:"min=0"` + Network string `binding:"required,network" uri:"network"` + Ptr int64 `binding:"min=0" uri:"ptr"` } // NetworkID - @@ -67,9 +67,9 @@ func (req getBigMapRequest) NetworkID() types.Network { } type getBigMapByKeyHashRequest struct { - Network string `uri:"network" binding:"required,network"` - Ptr int64 `uri:"ptr" binding:"min=0"` - KeyHash string `uri:"key_hash" binding:"required"` + Network string `binding:"required,network" uri:"network"` + Ptr int64 `binding:"min=0" uri:"ptr"` + KeyHash string `binding:"required" uri:"key_hash"` } // OauthRequest - @@ -84,33 +84,33 @@ type OauthParams struct { } type opgForAddressRequest struct { - LastID int64 `form:"last_id" binding:"omitempty"` - Size uint64 `form:"size" binding:"min=0"` + LastID int64 `binding:"omitempty" form:"last_id"` + Size uint64 `binding:"min=0" form:"size"` } type pageableRequest struct { - Offset int64 `form:"offset" binding:"min=0"` - Size int64 `form:"size" binding:"min=0,bcd_max_size=10"` + Offset int64 `binding:"min=0" form:"offset"` + Size int64 `binding:"min=0,bcd_max_size=10" form:"size"` } // OPGRequest - type OPGRequest struct { getByNetwork - Hash string `uri:"hash" binding:"required,opg" example:"ooy4c6G2BZzybYEY3vRQ7WXGL63tFmamTeGTHdjUxhd6ckbSNnb"` + Hash string `binding:"required,opg" example:"ooy4c6G2BZzybYEY3vRQ7WXGL63tFmamTeGTHdjUxhd6ckbSNnb" uri:"hash"` } // OperationGroupContentRequest - type OperationGroupContentRequest struct { OPGRequest - Counter int64 `uri:"counter" binding:"required" example:"123456"` + Counter int64 `binding:"required" example:"123456" uri:"counter"` } // ImplicitOperationRequest - type ImplicitOperationRequest struct { getByNetwork - Counter int64 `uri:"counter" binding:"required" example:"123456"` + Counter int64 `binding:"required" example:"123456" uri:"counter"` } // FormatterRequest - @@ -121,7 +121,7 @@ type FormatterRequest struct { } type getByNetwork struct { - Network string `uri:"network" binding:"required,network" example:"mainnet"` + Network string `binding:"required,network" example:"mainnet" uri:"network"` } // NetworkID - @@ -131,58 +131,58 @@ func (req getByNetwork) NetworkID() types.Network { type bigMapSearchRequest struct { pageableRequest - MaxLevel *int64 `form:"max_level,omitempty" binding:"omitempty,gt_int64_ptr=MinLevel"` - MinLevel *int64 `form:"min_level,omitempty" binding:"omitempty"` + MaxLevel *int64 `binding:"omitempty,gt_int64_ptr=MinLevel" form:"max_level,omitempty"` + MinLevel *int64 `binding:"omitempty" form:"min_level,omitempty"` } type opgRequest struct { - WithMempool bool `form:"with_mempool" binding:"omitempty"` - WithStorageDiff bool `form:"with_storage_diff" binding:"omitempty"` + WithMempool bool `binding:"omitempty" form:"with_mempool"` + WithStorageDiff bool `binding:"omitempty" form:"with_storage_diff"` } type getEntrypointDataRequest struct { - Name string `json:"name" binding:"required"` - Data map[string]interface{} `json:"data" binding:"required"` + Name string `binding:"required" json:"name"` + Data map[string]interface{} `binding:"required" json:"data"` Format string `json:"format"` } type getOperationByIDRequest struct { - ID int64 `uri:"id" binding:"required"` + ID int64 `binding:"required" uri:"id"` } type runOperationRequest struct { - Data map[string]interface{} `json:"data" binding:"required"` - Name string `json:"name" binding:"required"` + Data map[string]interface{} `binding:"required" json:"data"` + Name string `binding:"required" json:"name"` Amount int64 `json:"amount,omitempty"` - Source string `json:"source,omitempty" binding:"omitempty,address"` + Source string `binding:"omitempty,address" json:"source,omitempty"` } type runCodeRequest struct { - Data map[string]interface{} `json:"data" binding:"required"` - Name string `json:"name" binding:"required"` + Data map[string]interface{} `binding:"required" json:"data"` + Name string `binding:"required" json:"name"` Amount int64 `json:"amount,omitempty"` GasLimit int64 `json:"gas_limit,omitempty"` - Source string `json:"source,omitempty" binding:"omitempty,address"` - Sender string `json:"sender,omitempty" binding:"omitempty,address"` + Source string `binding:"omitempty,address" json:"source,omitempty"` + Sender string `binding:"omitempty,address" json:"sender,omitempty"` } type storageSchemaRequest struct { - FillType string `form:"fill_type,omitempty" binding:"omitempty,fill_type"` + FillType string `binding:"omitempty,fill_type" form:"fill_type,omitempty"` } type entrypointSchemaRequest struct { - FillType string `form:"fill_type,omitempty" binding:"omitempty"` - EntrypointName string `form:"entrypoint" binding:"required"` - Hash string `form:"hash,omitempty" binding:"omitempty"` - Counter *uint64 `form:"counter,omitempty" binding:"omitempty"` + FillType string `binding:"omitempty" form:"fill_type,omitempty"` + EntrypointName string `binding:"required" form:"entrypoint"` + Hash string `binding:"omitempty" form:"hash,omitempty"` + Counter *uint64 `binding:"omitempty" form:"counter,omitempty"` } type forkRequest struct { - Address string `json:"address,omitempty" binding:"omitempty,address,required_with=Network"` - Network string `json:"network,omitempty" binding:"omitempty,network,required_with=Address"` - Script string `json:"script,omitempty" binding:"omitempty"` + Address string `binding:"omitempty,address,required_with=Network" json:"address,omitempty"` + Network string `binding:"omitempty,network,required_with=Address" json:"network,omitempty"` + Script string `binding:"omitempty" json:"script,omitempty"` - Storage map[string]interface{} `json:"storage" binding:"required"` + Storage map[string]interface{} `binding:"required" json:"storage"` } // NetworkID - @@ -191,12 +191,12 @@ func (req forkRequest) NetworkID() types.Network { } type storageRequest struct { - Level int `form:"level" binding:"omitempty,gte=1"` + Level int `binding:"omitempty,gte=1" form:"level"` } // GetTokenStatsRequest - type GetTokenStatsRequest struct { - Period string `form:"period" binding:"oneof=all year month week day hour" example:"year"` + Period string `binding:"oneof=all year month week day hour" example:"year" form:"period"` Contracts string `form:"contracts"` } @@ -209,26 +209,26 @@ func (req GetTokenStatsRequest) Addresses() []string { } type executeViewRequest struct { - Data map[string]interface{} `json:"data" binding:"required"` - Name string `json:"name" binding:"required_if=Kind on-chain"` - Implementation *int `json:"implementation" binding:"required_if=Kind on-chain"` - Kind ViewSchemaKind `json:"kind" binding:"required"` + Data map[string]interface{} `binding:"required" json:"data"` + Name string `binding:"required_if=Kind on-chain" json:"name"` + Implementation *int `binding:"required_if=Kind on-chain" json:"implementation"` + Kind ViewSchemaKind `binding:"required" json:"kind"` Amount int64 `json:"amount,omitempty"` GasLimit int64 `json:"gas_limit,omitempty"` - Source string `json:"source,omitempty" binding:"omitempty,address"` - Sender string `json:"sender,omitempty" binding:"omitempty,address"` - View *contract.ViewImplementation `json:"view,omitempty" binding:"required_if=Kind off-chain"` + Source string `binding:"omitempty,address" json:"source,omitempty"` + Sender string `binding:"omitempty,address" json:"sender,omitempty"` + View *contract.ViewImplementation `binding:"required_if=Kind off-chain" json:"view,omitempty"` } type getGlobalConstantRequest struct { - Address string `uri:"address" binding:"required,global_constant"` + Address string `binding:"required,global_constant" uri:"address"` } type globalConstantsListRequest struct { pageableRequest - OrderBy string `form:"order_by" binding:"omitempty,oneof=level timestamp links_count address"` - Sort string `form:"sort" binding:"omitempty,oneof=asc desc"` + OrderBy string `binding:"omitempty,oneof=level timestamp links_count address" form:"order_by"` + Sort string `binding:"omitempty,oneof=asc desc" form:"sort"` } type globalConstantsContractsRequest struct { @@ -237,19 +237,24 @@ type globalConstantsContractsRequest struct { } type getViewsArgs struct { - Kind ViewSchemaKind `form:"kind" binding:"omitempty,oneof=off-chain on-chain"` + Kind ViewSchemaKind `binding:"omitempty,oneof=off-chain on-chain" form:"kind"` } type findContract struct { - Tags string `form:"tags" binding:"omitempty"` + Tags string `binding:"omitempty" form:"tags"` } type smartRollupListRequest struct { pageableRequest - Sort string `form:"sort" binding:"omitempty,oneof=asc desc"` + Sort string `binding:"omitempty,oneof=asc desc" form:"sort"` } type getSmartRollupRequest struct { - Address string `uri:"address" binding:"required,smart_rollup"` + Address string `binding:"required,smart_rollup" uri:"address"` +} + +type ticketBalancesRequest struct { + pageableRequest + WithoutZeroBalances bool `binding:"omitempty" form:"skip_empty"` } diff --git a/cmd/api/handlers/responses.go b/cmd/api/handlers/responses.go index e26df1c8e..f1ed1f670 100644 --- a/cmd/api/handlers/responses.go +++ b/cmd/api/handlers/responses.go @@ -22,47 +22,47 @@ import ( // Error - type Error struct { - Message string `json:"message" example:"text"` + Message string `example:"text" json:"message"` } // Operation - type Operation struct { - ID int64 `json:"id,omitempty" extensions:"x-nullable"` - Level int64 `json:"level,omitempty" extensions:"x-nullable"` - Fee int64 `json:"fee,omitempty" extensions:"x-nullable"` - Counter int64 `json:"counter,omitempty" extensions:"x-nullable"` - GasLimit int64 `json:"gas_limit,omitempty" extensions:"x-nullable"` - StorageLimit int64 `json:"storage_limit,omitempty" extensions:"x-nullable"` - Amount int64 `json:"amount,omitempty" extensions:"x-nullable"` - Balance int64 `json:"balance,omitempty" extensions:"x-nullable"` - Burned int64 `json:"burned,omitempty" extensions:"x-nullable"` - AllocatedDestinationContractBurned int64 `json:"allocated_destination_contract_burned,omitempty" extensions:"x-nullable"` + ID int64 `extensions:"x-nullable" json:"id,omitempty"` + Level int64 `extensions:"x-nullable" json:"level,omitempty"` + Fee int64 `extensions:"x-nullable" json:"fee,omitempty"` + Counter int64 `extensions:"x-nullable" json:"counter,omitempty"` + GasLimit int64 `extensions:"x-nullable" json:"gas_limit,omitempty"` + StorageLimit int64 `extensions:"x-nullable" json:"storage_limit,omitempty"` + Amount int64 `extensions:"x-nullable" json:"amount,omitempty"` + Balance int64 `extensions:"x-nullable" json:"balance,omitempty"` + Burned int64 `extensions:"x-nullable" json:"burned,omitempty"` + AllocatedDestinationContractBurned int64 `extensions:"x-nullable" json:"allocated_destination_contract_burned,omitempty"` IndexedTime int64 `json:"-"` ContentIndex int64 `json:"content_index"` - ConsumedGas int64 `json:"consumed_gas,omitempty" extensions:"x-nullable" example:"100"` - StorageSize int64 `json:"storage_size,omitempty" extensions:"x-nullable" example:"200"` - PaidStorageSizeDiff int64 `json:"paid_storage_size_diff,omitempty" extensions:"x-nullable" example:"300"` + ConsumedGas int64 `example:"100" extensions:"x-nullable" json:"consumed_gas,omitempty"` + StorageSize int64 `example:"200" extensions:"x-nullable" json:"storage_size,omitempty"` + PaidStorageSizeDiff int64 `example:"300" extensions:"x-nullable" json:"paid_storage_size_diff,omitempty"` TicketUpdatesCount int `json:"ticket_updates_count"` BigMapDiffsCount int `json:"big_map_diffs_count"` - Errors []*tezerrors.Error `json:"errors,omitempty" extensions:"x-nullable"` - Parameters interface{} `json:"parameters,omitempty" extensions:"x-nullable"` - StorageDiff *ast.MiguelNode `json:"storage_diff,omitempty" extensions:"x-nullable"` - Payload []*ast.MiguelNode `json:"payload,omitempty" extensions:"x-nullable"` - RawMempool interface{} `json:"rawMempool,omitempty" extensions:"x-nullable"` + Errors []*tezerrors.Error `extensions:"x-nullable" json:"errors,omitempty"` + Parameters interface{} `extensions:"x-nullable" json:"parameters,omitempty"` + StorageDiff *ast.MiguelNode `extensions:"x-nullable" json:"storage_diff,omitempty"` + Payload []*ast.MiguelNode `extensions:"x-nullable" json:"payload,omitempty"` + RawMempool interface{} `extensions:"x-nullable" json:"rawMempool,omitempty"` Timestamp time.Time `json:"timestamp"` Protocol string `json:"protocol"` - Hash string `json:"hash,omitempty" extensions:"x-nullable"` + Hash string `extensions:"x-nullable" json:"hash,omitempty"` Network string `json:"network"` Kind string `json:"kind"` - Source string `json:"source,omitempty" extensions:"x-nullable"` - Destination string `json:"destination,omitempty" extensions:"x-nullable"` - PublicKey string `json:"public_key,omitempty" extensions:"x-nullable"` - ManagerPubKey string `json:"manager_pubkey,omitempty" extensions:"x-nullable"` - Delegate string `json:"delegate,omitempty" extensions:"x-nullable"` + Source string `extensions:"x-nullable" json:"source,omitempty"` + Destination string `extensions:"x-nullable" json:"destination,omitempty"` + PublicKey string `extensions:"x-nullable" json:"public_key,omitempty"` + ManagerPubKey string `extensions:"x-nullable" json:"manager_pubkey,omitempty"` + Delegate string `extensions:"x-nullable" json:"delegate,omitempty"` Status string `json:"status"` - Entrypoint string `json:"entrypoint,omitempty" extensions:"x-nullable"` - Tag string `json:"tag,omitempty" extensions:"x-nullable"` - AllocatedDestinationContract bool `json:"allocated_destination_contract,omitempty" extensions:"x-nullable" example:"true"` + Entrypoint string `extensions:"x-nullable" json:"entrypoint,omitempty"` + Tag string `extensions:"x-nullable" json:"tag,omitempty"` + AllocatedDestinationContract bool `example:"true" extensions:"x-nullable" json:"allocated_destination_contract,omitempty"` Internal bool `json:"internal"` Mempool bool `json:"mempool"` Storage stdJSON.RawMessage `json:"-"` @@ -154,21 +154,21 @@ type Contract struct { Timestamp time.Time `json:"timestamp"` Hash string `json:"hash,omitempty"` - Tags []string `json:"tags,omitempty" extensions:"x-nullable"` - Hardcoded []string `json:"hardcoded,omitempty" extensions:"x-nullable"` - FailStrings []string `json:"fail_strings,omitempty" extensions:"x-nullable"` - Annotations []string `json:"annotations,omitempty" extensions:"x-nullable"` - Entrypoints []string `json:"entrypoints,omitempty" extensions:"x-nullable"` + Tags []string `extensions:"x-nullable" json:"tags,omitempty"` + Hardcoded []string `extensions:"x-nullable" json:"hardcoded,omitempty"` + FailStrings []string `extensions:"x-nullable" json:"fail_strings,omitempty"` + Annotations []string `extensions:"x-nullable" json:"annotations,omitempty"` + Entrypoints []string `extensions:"x-nullable" json:"entrypoints,omitempty"` Address string `json:"address"` - Manager string `json:"manager,omitempty" extensions:"x-nullable"` - Delegate string `json:"delegate,omitempty" extensions:"x-nullable"` + Manager string `extensions:"x-nullable" json:"manager,omitempty"` + Delegate string `extensions:"x-nullable" json:"delegate,omitempty"` - FoundBy string `json:"found_by,omitempty" extensions:"x-nullable"` - LastAction time.Time `json:"last_action,omitempty" extensions:"x-nullable"` - TxCount int64 `json:"tx_count,omitempty" extensions:"x-nullable"` - MigrationsCount int64 `json:"migrations_count,omitempty" extensions:"x-nullable"` - Slug string `json:"slug,omitempty" extensions:"x-nullable"` + FoundBy string `extensions:"x-nullable" json:"found_by,omitempty"` + LastAction time.Time `extensions:"x-nullable" json:"last_action,omitempty"` + TxCount int64 `extensions:"x-nullable" json:"tx_count,omitempty"` + MigrationsCount int64 `extensions:"x-nullable" json:"migrations_count,omitempty"` + Slug string `extensions:"x-nullable" json:"slug,omitempty"` } // FromModel - @@ -221,7 +221,7 @@ func (c *RecentlyCalledContract) FromModel(contract contract.Contract) { // OperationResponse - type OperationResponse struct { Operations []Operation `json:"operations"` - LastID string `json:"last_id,omitempty" extensions:"x-nullable" example:"1588640276994159"` + LastID string `example:"1588640276994159" extensions:"x-nullable" json:"last_id,omitempty"` } // BigMapItem - @@ -247,14 +247,14 @@ type GetBigMapResponse struct { Ptr int64 `json:"ptr"` ActiveKeys uint `json:"active_keys"` TotalKeys uint `json:"total_keys"` - Typedef []ast.Typedef `json:"typedef,omitempty" extensions:"x-nullable"` + Typedef []ast.Typedef `extensions:"x-nullable" json:"typedef,omitempty"` } // Migration - type Migration struct { Level int64 `json:"level"` Timestamp time.Time `json:"timestamp"` - Hash string `json:"hash,omitempty" extensions:"x-nullable"` + Hash string `extensions:"x-nullable" json:"hash,omitempty"` Protocol string `json:"protocol"` PrevProtocol string `json:"prev_protocol"` Kind string `json:"kind"` @@ -269,15 +269,15 @@ type BigMapDiffItem struct { // BigMapDiffByKeyResponse - type BigMapDiffByKeyResponse struct { - Key interface{} `json:"key,omitempty" extensions:"x-nullable"` + Key interface{} `extensions:"x-nullable" json:"key,omitempty"` KeyHash string `json:"key_hash"` - Values []BigMapDiffItem `json:"values,omitempty" extensions:"x-nullable"` + Values []BigMapDiffItem `extensions:"x-nullable" json:"values,omitempty"` Total int64 `json:"total"` } // BigMapKeyStateResponse - type BigMapKeyStateResponse struct { - Key interface{} `json:"key,omitempty" extensions:"x-nullable"` + Key interface{} `extensions:"x-nullable" json:"key,omitempty"` KeyHash string `json:"key_hash"` KeyString string `json:"key_string"` Value interface{} `json:"value"` @@ -296,11 +296,11 @@ type CodeDiffResponse struct { // NetworkStats - type NetworkStats struct { - ContractsCount int64 `json:"contracts_count" example:"10"` - OperationsCount int64 `json:"operations_count" example:"100"` - ContractCalls uint64 `json:"contract_calls" example:"100"` - UniqueContracts uint64 `json:"unique_contracts" example:"100"` - FACount uint64 `json:"fa_count" example:"100"` + ContractsCount int64 `example:"10" json:"contracts_count"` + OperationsCount int64 `example:"100" json:"operations_count"` + ContractCalls uint64 `example:"100" json:"contract_calls"` + UniqueContracts uint64 `example:"100" json:"unique_contracts"` + FACount uint64 `example:"100" json:"fa_count"` Protocols []Protocol `json:"protocols"` } @@ -321,7 +321,7 @@ type SearchBigMapDiff struct { type EntrypointSchema struct { ast.EntrypointType Schema *ast.JSONSchema `json:"schema"` - DefaultModel ast.JSONModel `json:"default_model,omitempty" extensions:"x-nullable"` + DefaultModel ast.JSONModel `extensions:"x-nullable" json:"default_model,omitempty"` } // GetErrorLocationResponse - @@ -335,10 +335,10 @@ type GetErrorLocationResponse struct { // Protocol - type Protocol struct { - Hash string `json:"hash" example:"PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb"` - StartLevel int64 `json:"start_level" example:"851969"` - EndLevel int64 `json:"end_level,omitempty" example:"0" extensions:"x-nullable"` - Alias string `json:"alias" example:"Carthage"` + Hash string `example:"PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb" json:"hash"` + StartLevel int64 `example:"851969" json:"start_level"` + EndLevel int64 `example:"0" extensions:"x-nullable" json:"end_level,omitempty"` + Alias string `example:"Carthage" json:"alias"` } // FromModel - @@ -351,17 +351,17 @@ func (p *Protocol) FromModel(protocol protocol.Protocol) { // Block - type Block struct { - Network string `json:"network" example:"mainnet"` - Hash string `json:"hash" example:"BLyAEwaXShJuZasvUezHUfLqzZ48V8XrPvXF2wRaH15tmzEpsHT"` - Level int64 `json:"level" example:"100"` - Predecessor string `json:"predecessor" example:"BMWVEwEYw9m5iaHzqxDfkPzZTV4rhkSouRh3DkVMVGkxZ3EVaNs"` - ChainID string `json:"chain_id" example:"NetXdQprcVkpaWU"` - Timestamp time.Time `json:"timestamp" example:"2018-06-30T18:05:27Z"` - Protocol string `json:"protocol" example:"PtCJ7pwoxe8JasnHY8YonnLYjcVHmhiARPJvqcC6VfHT5s8k8sY"` - CostPerByte int64 `json:"cost_per_byte" example:"250"` - HardGasLimitPerOperation int64 `json:"hard_gas_limit_per_operation" example:"1040000"` - HardStorageLimitPerOperation int64 `json:"hard_storage_limit_per_operation" example:"60000"` - TimeBetweenBlocks int64 `json:"time_between_blocks" example:"30"` + Network string `example:"mainnet" json:"network"` + Hash string `example:"BLyAEwaXShJuZasvUezHUfLqzZ48V8XrPvXF2wRaH15tmzEpsHT" json:"hash"` + Level int64 `example:"100" json:"level"` + Predecessor string `example:"BMWVEwEYw9m5iaHzqxDfkPzZTV4rhkSouRh3DkVMVGkxZ3EVaNs" json:"predecessor"` + ChainID string `example:"NetXdQprcVkpaWU" json:"chain_id"` + Timestamp time.Time `example:"2018-06-30T18:05:27Z" json:"timestamp"` + Protocol string `example:"PtCJ7pwoxe8JasnHY8YonnLYjcVHmhiARPJvqcC6VfHT5s8k8sY" json:"protocol"` + CostPerByte int64 `example:"250" json:"cost_per_byte"` + HardGasLimitPerOperation int64 `example:"1040000" json:"hard_gas_limit_per_operation"` + HardStorageLimitPerOperation int64 `example:"60000" json:"hard_storage_limit_per_operation"` + TimeBetweenBlocks int64 `example:"30" json:"time_between_blocks"` Stats *Stats `json:"stats,omitempty"` } @@ -427,14 +427,14 @@ type SeriesFloat [][]float64 type BigMapHistoryResponse struct { Address string `json:"address"` Ptr int64 `json:"ptr"` - Items []BigMapHistoryItem `json:"items,omitempty" extensions:"x-nullable"` + Items []BigMapHistoryItem `extensions:"x-nullable" json:"items,omitempty"` } // BigMapHistoryItem - type BigMapHistoryItem struct { Action string `json:"action"` - SourcePtr *int64 `json:"source_ptr,omitempty" extensions:"x-nullable"` - DestinationPtr *int64 `json:"destination_ptr,omitempty" extensions:"x-nullable"` + SourcePtr *int64 `extensions:"x-nullable" json:"source_ptr,omitempty"` + DestinationPtr *int64 `extensions:"x-nullable" json:"destination_ptr,omitempty"` Timestamp time.Time `json:"timestamp"` Level int64 `json:"level"` } @@ -485,7 +485,7 @@ type ViewSchema struct { Implementation int `json:"implementation"` Description string `json:"description,omitempty"` Schema *ast.JSONSchema `json:"schema"` - DefaultModel interface{} `json:"default_model,omitempty" extensions:"x-nullable"` + DefaultModel interface{} `extensions:"x-nullable" json:"default_model,omitempty"` Error string `json:"error,omitempty"` Kind ViewSchemaKind `json:"kind"` } @@ -556,9 +556,9 @@ type CodeFromMichelsonResponse struct { // CodeFromMichelsonStorage - type CodeFromMichelsonStorage struct { - Type []ast.Typedef `json:"type,omitempty" extensions:"x-nullable"` + Type []ast.Typedef `extensions:"x-nullable" json:"type,omitempty"` Schema *ast.JSONSchema `json:"schema"` - DefaultModel ast.JSONModel `json:"default_model,omitempty" extensions:"x-nullable"` + DefaultModel ast.JSONModel `extensions:"x-nullable" json:"default_model,omitempty"` } // OPGResponse - @@ -606,7 +606,7 @@ type Event struct { Timestamp time.Time `json:"timestamp"` Level int64 `json:"level"` Tag string `json:"tag"` - Payload []*ast.MiguelNode `json:"payload,omitempty" extensions:"x-nullable"` + Payload []*ast.MiguelNode `extensions:"x-nullable" json:"payload,omitempty"` } // NewEvent - @@ -701,11 +701,13 @@ type TicketBalance struct { Amount string `json:"amount"` ContentType []ast.Typedef `json:"content_type"` Content *ast.MiguelNode `json:"content,omitempty"` + TicketId int64 `json:"ticket_id"` } func NewTicketBalance(balance ticket.Balance) TicketBalance { return TicketBalance{ Ticketer: balance.Ticket.Ticketer.Address, Amount: balance.Amount.String(), + TicketId: balance.TicketId, } } diff --git a/cmd/api/handlers/tickets.go b/cmd/api/handlers/tickets.go index 3676cca75..095c55ea8 100644 --- a/cmd/api/handlers/tickets.go +++ b/cmd/api/handlers/tickets.go @@ -41,7 +41,7 @@ func GetContractTicketUpdates() gin.HandlerFunc { return } - updates, err := ctx.TicketUpdates.Updates(c.Request.Context(), req.Address, args.Size, args.Offset) + updates, err := ctx.Tickets.Updates(c.Request.Context(), req.Address, args.Size, args.Offset) if handleError(c, ctx.Storage, err, 0) { return } @@ -67,7 +67,7 @@ func GetTicketUpdatesForOperation() gin.HandlerFunc { if handleError(c, ctx.Storage, err, 0) { return } - updates, err := ctx.TicketUpdates.UpdatesForOperation(c.Request.Context(), req.ID) + updates, err := ctx.Tickets.UpdatesForOperation(c.Request.Context(), req.ID) if handleError(c, ctx.Storage, err, 0) { return } @@ -91,12 +91,22 @@ func GetTicketBalancesForAccount() gin.HandlerFunc { return } + var args ticketBalancesRequest + if err := c.BindQuery(&args); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, Error{Message: err.Error()}) + return + } + acc, err := ctx.Accounts.Get(c.Request.Context(), req.Address) if handleError(c, ctx.Storage, err, 0) { return } - balances, err := ctx.TicketUpdates.BalancesForAccount(c.Request.Context(), acc.ID, 10, 0) + balances, err := ctx.Tickets.BalancesForAccount(c.Request.Context(), acc.ID, ticket.BalanceRequest{ + Limit: args.Size, + Offset: args.Offset, + WithoutZeroBalances: args.WithoutZeroBalances, + }) if handleError(c, ctx.Storage, err, 0) { return } diff --git a/internal/bcd/raw_script.go b/internal/bcd/raw_script.go index 65043cf69..21b0428a3 100644 --- a/internal/bcd/raw_script.go +++ b/internal/bcd/raw_script.go @@ -10,10 +10,10 @@ import ( // RawScript - type RawScript struct { - Code []byte - Parameter []byte - Storage []byte - Views []byte + Code []byte `json:"-"` + Parameter []byte `json:"-"` + Storage []byte `json:"-"` + Views []byte `json:"-"` } type prim struct { diff --git a/internal/config/context.go b/internal/config/context.go index 5324b18ce..5caa316a0 100644 --- a/internal/config/context.go +++ b/internal/config/context.go @@ -44,7 +44,7 @@ type Context struct { Migrations migration.Repository Operations operation.Repository Protocols protocol.Repository - TicketUpdates ticket.Repository + Tickets ticket.Repository Domains domains.Repository Scripts contract.ScriptRepository SmartRollups smartrollup.Repository diff --git a/internal/config/options.go b/internal/config/options.go index 77e7ba413..4742fef26 100644 --- a/internal/config/options.go +++ b/internal/config/options.go @@ -100,7 +100,7 @@ func WithStorage(cfg StorageConfig, appName string, maxPageSize int64) ContextOp ctx.Protocols = protocol.NewStorage(conn) ctx.GlobalConstants = global_constant.NewStorage(conn) ctx.Domains = domains.NewStorage(conn) - ctx.TicketUpdates = ticket.NewStorage(conn) + ctx.Tickets = ticket.NewStorage(conn) ctx.Scripts = contractStorage ctx.SmartRollups = smartrollup.NewStorage(conn) ctx.Stats = stats.NewStorage(conn) diff --git a/internal/models/contract/constant.go b/internal/models/contract/constant.go index 7d82359a6..6105362a7 100644 --- a/internal/models/contract/constant.go +++ b/internal/models/contract/constant.go @@ -10,11 +10,11 @@ import ( type GlobalConstant struct { bun.BaseModel `bun:"global_constants"` - ID int64 `bun:"id,pk,notnull,autoincrement" json:"-"` + ID int64 `bun:"id,pk,notnull,autoincrement"` Timestamp time.Time `json:"timestamp"` Level int64 `json:"level"` - Address string `bun:"address,type:text" json:"address"` - Value []byte `json:"value,omitempty"` + Address string `bun:"address,type:text"` + Value []byte Scripts []Script `bun:"m2m:script_constants,join:GlobalConstant=Script"` } diff --git a/internal/models/contract/repository.go b/internal/models/contract/repository.go index 20beb7092..db702691a 100644 --- a/internal/models/contract/repository.go +++ b/internal/models/contract/repository.go @@ -39,8 +39,8 @@ type ConstantRepository interface { // ListGlobalConstantItem - type ListGlobalConstantItem struct { - Timestamp time.Time `json:"timestamp" pg:"timestamp"` - Level int64 `json:"level" pg:"level"` - Address string `json:"address" pg:"address"` - LinksCount uint64 `json:"links_count" pg:"links_count"` + Timestamp time.Time `bun:"timestamp"` + Level int64 `bun:"level"` + Address string `bun:"address"` + LinksCount uint64 `bun:"links_count"` } diff --git a/internal/models/mock/ticket/mock.go b/internal/models/mock/ticket/mock.go index a33623d20..a184c4532 100644 --- a/internal/models/mock/ticket/mock.go +++ b/internal/models/mock/ticket/mock.go @@ -40,18 +40,18 @@ func (m *MockRepository) EXPECT() *MockRepositoryMockRecorder { } // BalancesForAccount mocks base method. -func (m *MockRepository) BalancesForAccount(ctx context.Context, accountId, limit, offset int64) ([]ticket.Balance, error) { +func (m *MockRepository) BalancesForAccount(ctx context.Context, accountId int64, req ticket.BalanceRequest) ([]ticket.Balance, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "BalancesForAccount", ctx, accountId, limit, offset) + ret := m.ctrl.Call(m, "BalancesForAccount", ctx, accountId, req) ret0, _ := ret[0].([]ticket.Balance) ret1, _ := ret[1].(error) return ret0, ret1 } // BalancesForAccount indicates an expected call of BalancesForAccount. -func (mr *MockRepositoryMockRecorder) BalancesForAccount(ctx, accountId, limit, offset any) *RepositoryBalancesForAccountCall { +func (mr *MockRepositoryMockRecorder) BalancesForAccount(ctx, accountId, req any) *RepositoryBalancesForAccountCall { mr.mock.ctrl.T.Helper() - call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BalancesForAccount", reflect.TypeOf((*MockRepository)(nil).BalancesForAccount), ctx, accountId, limit, offset) + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BalancesForAccount", reflect.TypeOf((*MockRepository)(nil).BalancesForAccount), ctx, accountId, req) return &RepositoryBalancesForAccountCall{Call: call} } @@ -67,13 +67,13 @@ func (c *RepositoryBalancesForAccountCall) Return(arg0 []ticket.Balance, arg1 er } // Do rewrite *gomock.Call.Do -func (c *RepositoryBalancesForAccountCall) Do(f func(context.Context, int64, int64, int64) ([]ticket.Balance, error)) *RepositoryBalancesForAccountCall { +func (c *RepositoryBalancesForAccountCall) Do(f func(context.Context, int64, ticket.BalanceRequest) ([]ticket.Balance, error)) *RepositoryBalancesForAccountCall { c.Call = c.Call.Do(f) return c } // DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *RepositoryBalancesForAccountCall) DoAndReturn(f func(context.Context, int64, int64, int64) ([]ticket.Balance, error)) *RepositoryBalancesForAccountCall { +func (c *RepositoryBalancesForAccountCall) DoAndReturn(f func(context.Context, int64, ticket.BalanceRequest) ([]ticket.Balance, error)) *RepositoryBalancesForAccountCall { c.Call = c.Call.DoAndReturn(f) return c } diff --git a/internal/models/ticket/repository.go b/internal/models/ticket/repository.go index 78fed2e9b..dc41f9048 100644 --- a/internal/models/ticket/repository.go +++ b/internal/models/ticket/repository.go @@ -2,9 +2,15 @@ package ticket import "context" +type BalanceRequest struct { + Limit int64 + Offset int64 + WithoutZeroBalances bool +} + //go:generate mockgen -source=$GOFILE -destination=../mock/ticket/mock.go -package=ticket -typed type Repository interface { Updates(ctx context.Context, ticketer string, limit, offset int64) ([]TicketUpdate, error) UpdatesForOperation(ctx context.Context, operationId int64) ([]TicketUpdate, error) - BalancesForAccount(ctx context.Context, accountId int64, limit, offset int64) ([]Balance, error) + BalancesForAccount(ctx context.Context, accountId int64, req BalanceRequest) ([]Balance, error) } diff --git a/internal/postgres/tests/ticket_test.go b/internal/postgres/tests/ticket_test.go index c6f3a5f61..c4c8cf9e1 100644 --- a/internal/postgres/tests/ticket_test.go +++ b/internal/postgres/tests/ticket_test.go @@ -3,6 +3,8 @@ package tests import ( "context" "time" + + "github.com/baking-bad/bcdhub/internal/models/ticket" ) func (s *StorageTestSuite) TestTicketGet() { @@ -45,7 +47,10 @@ func (s *StorageTestSuite) TestBalancesForAccount() { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - balances, err := s.ticketUpdates.BalancesForAccount(ctx, 131, 10, 0) + balances, err := s.ticketUpdates.BalancesForAccount(ctx, 131, ticket.BalanceRequest{ + Limit: 10, + WithoutZeroBalances: true, + }) s.Require().NoError(err) s.Require().Len(balances, 2) @@ -62,7 +67,9 @@ func (s *StorageTestSuite) TestBalancesForAccountEmpty() { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - balances, err := s.ticketUpdates.BalancesForAccount(ctx, 12, 10, 0) + balances, err := s.ticketUpdates.BalancesForAccount(ctx, 12, ticket.BalanceRequest{ + Limit: 10, + }) s.Require().NoError(err) s.Require().Len(balances, 0) } diff --git a/internal/postgres/ticket/storage.go b/internal/postgres/ticket/storage.go index 1e66bbcbb..bac522bc8 100644 --- a/internal/postgres/ticket/storage.go +++ b/internal/postgres/ticket/storage.go @@ -64,7 +64,7 @@ func (storage *Storage) UpdatesForOperation(ctx context.Context, operationId int return } -func (storage *Storage) BalancesForAccount(ctx context.Context, accountId int64, limit, offset int64) (balances []ticket.Balance, err error) { +func (storage *Storage) BalancesForAccount(ctx context.Context, accountId int64, req ticket.BalanceRequest) (balances []ticket.Balance, err error) { query := storage.DB. NewSelect(). Model(&balances). @@ -72,15 +72,22 @@ func (storage *Storage) BalancesForAccount(ctx context.Context, accountId int64, Relation("Ticket.Ticketer", func(sq *bun.SelectQuery) *bun.SelectQuery { return sq.Column("address") }). - Where("account_id = ?", accountId). - Offset(int(offset)) + Where("account_id = ?", accountId) - if limit > 0 && limit < 100 { - query.Limit(int(limit)) + if req.Offset > 0 { + query.Offset(int(req.Offset)) + } + + if req.Limit > 0 && req.Limit < 100 { + query.Limit(int(req.Limit)) } else { query.Limit(10) } + if req.WithoutZeroBalances { + query.Where("amount > 0") + } + err = query.Scan(ctx) return } diff --git a/scripts/bcdctl/rollback.go b/scripts/bcdctl/rollback.go index 095fdf27b..bc198571d 100644 --- a/scripts/bcdctl/rollback.go +++ b/scripts/bcdctl/rollback.go @@ -10,8 +10,8 @@ import ( ) type rollbackCommand struct { - Level int64 `short:"l" long:"level" description:"Level to rollback"` - Network string `short:"n" long:"network" description:"Network"` + Level int64 `description:"Level to rollback" long:"level" short:"l"` + Network string `description:"Network" long:"network" short:"n"` } var rollbackCmd rollbackCommand