Skip to content

Commit

Permalink
better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-babylonlabs committed Oct 22, 2024
1 parent 0dfde9a commit c0e8862
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type GenericEvent struct {
EventType queueClient.EventType `json:"event_type"`
}

func ReplayUnprocessableMessages(ctx context.Context, cfg *config.Config, queues *queueclients.QueueClients, db dbclient.DBClientInterface) (err error) {
func ReplayUnprocessableMessages(ctx context.Context, cfg *config.Config, queues *queueclients.QueueClients, db dbclient.DBClient) (err error) {
// Fetch unprocessable messages
unprocessableMessages, err := db.FindUnprocessableMessages(ctx)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/shared/api/handlers/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
)

type Handler struct {
Config *config.Config
Service service.ServiceInterface
Config *config.Config
Service service.SharedServiceProvider
}

func New(ctx context.Context, config *config.Config, service service.ServiceInterface) (*Handler, error) {
func New(ctx context.Context, config *config.Config, service service.SharedServiceProvider) (*Handler, error) {
return &Handler{Config: config, Service: service}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/shared/api/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Handlers struct {
}

func New(ctx context.Context, config *config.Config, services *services.Services) (*Handlers, error) {
sharedHandler, err := handler.New(ctx, config, services.Service)
sharedHandler, err := handler.New(ctx, config, services.SharedService)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/shared/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
dbmodel "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model"
)

type DBClientInterface interface {
type DBClient interface {
Ping(ctx context.Context) error
// InsertPkAddressMappings inserts the btc public key and
// its corresponding btc addresses into the database.
Expand Down
6 changes: 3 additions & 3 deletions internal/shared/db/clients/db_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

type DbClients struct {
MongoClient *mongo.Client
SharedDBClient dbclient.DBClientInterface
V1DBClient v1dbclient.V1DBClientInterface
V2DBClient v2dbclient.V2DBClientInterface
SharedDBClient dbclient.DBClient
V1DBClient v1dbclient.V1DBClient
V2DBClient v2dbclient.V2DBClient
}

func New(ctx context.Context, cfg *config.Config) (*DbClients, error) {
Expand Down
10 changes: 2 additions & 8 deletions internal/shared/http/client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ import (

var ALLOWED_METHODS = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"}

type HttpClientInterface interface {
GetBaseURL() string
GetDefaultRequestTimeout() int
GetHttpClient() *http.Client
}

type HttpClientOptions struct {
Timeout int
Path string
Expand All @@ -38,7 +32,7 @@ func isAllowedMethod(method string) bool {
}

func sendRequest[I any, R any](
ctx context.Context, client HttpClientInterface, method string, opts *HttpClientOptions, input *I,
ctx context.Context, client HttpClient, method string, opts *HttpClientOptions, input *I,
) (*R, *types.Error) {
if !isAllowedMethod(method) {
return nil, types.NewInternalServiceError(fmt.Errorf("method %s is not allowed", method))
Expand Down Expand Up @@ -122,7 +116,7 @@ func sendRequest[I any, R any](
}

func SendRequest[I any, R any](
ctx context.Context, client HttpClientInterface, method string, opts *HttpClientOptions, input *I,
ctx context.Context, client HttpClient, method string, opts *HttpClientOptions, input *I,
) (*R, *types.Error) {
timer := metrics.StartClientRequestDurationTimer(
client.GetBaseURL(), method, opts.TemplatePath,
Expand Down
9 changes: 9 additions & 0 deletions internal/shared/http/client/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package client

import "net/http"

type HttpClient interface {
GetBaseURL() string
GetDefaultRequestTimeout() int
GetHttpClient() *http.Client
}
6 changes: 3 additions & 3 deletions internal/shared/http/clients/http_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

type Clients struct {
Ordinals ordinals.OrdinalsClientInterface
Ordinals ordinals.OrdinalsClient
}

func New(cfg *config.Config) *Clients {
var ordinalsClient *ordinals.OrdinalsClient
var ordinalsClient ordinals.OrdinalsClient
// If the assets config is set, create the ordinal related clients
if cfg.Assets != nil {
ordinalsClient = ordinals.NewOrdinalsClient(cfg.Assets.Ordinals)
ordinalsClient = ordinals.New(cfg.Assets.Ordinals)
}

return &Clients{
Expand Down
2 changes: 1 addition & 1 deletion internal/shared/http/clients/ordinals/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/shared/types"
)

type OrdinalsClientInterface interface {
type OrdinalsClient interface {
GetBaseURL() string
GetDefaultRequestTimeout() int
GetHttpClient() *http.Client
Expand Down
14 changes: 7 additions & 7 deletions internal/shared/http/clients/ordinals/ordinals.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type OrdinalsOutputResponse struct {
Runes json.RawMessage `json:"runes"`
}

type OrdinalsClient struct {
type Ordinals struct {
config *config.OrdinalsConfig
defaultHeaders map[string]string
httpClient *http.Client
}

func NewOrdinalsClient(config *config.OrdinalsConfig) *OrdinalsClient {
func New(config *config.OrdinalsConfig) *Ordinals {
// Client is disabled if config is nil
if config == nil {
return nil
Expand All @@ -33,27 +33,27 @@ func NewOrdinalsClient(config *config.OrdinalsConfig) *OrdinalsClient {
"Content-Type": "application/json",
"Accept": "application/json",
}
return &OrdinalsClient{
return &Ordinals{
config,
headers,
httpClient,
}
}

// Necessary for the BaseClient interface
func (c *OrdinalsClient) GetBaseURL() string {
func (c *Ordinals) GetBaseURL() string {
return fmt.Sprintf("%s:%s", c.config.Host, c.config.Port)
}

func (c *OrdinalsClient) GetDefaultRequestTimeout() int {
func (c *Ordinals) GetDefaultRequestTimeout() int {
return c.config.Timeout
}

func (c *OrdinalsClient) GetHttpClient() *http.Client {
func (c *Ordinals) GetHttpClient() *http.Client {
return c.httpClient
}

func (c *OrdinalsClient) FetchUTXOInfos(
func (c *Ordinals) FetchUTXOInfos(
ctx context.Context, utxos []types.UTXOIdentifier,
) ([]OrdinalsOutputResponse, *types.Error) {
path := "/outputs"
Expand Down
2 changes: 1 addition & 1 deletion internal/shared/observability/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func StartHealthCheckCron(ctx context.Context, queueClients *queueclients.QueueC
return nil
}

func queueHealthCheck(queueClient queueclient.QueueClientInterface) {
func queueHealthCheck(queueClient queueclient.QueueClient) {
if err := queueClient.IsConnectionHealthy(); err != nil {
logger.Error().Err(err).Msg("One or more queue connections are not healthy.")
// Record service unavailable in metrics
Expand Down
6 changes: 3 additions & 3 deletions internal/shared/queue/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import (
"github.com/rs/zerolog/log"
)

type QueueClient struct {
type Queue struct {
ProcessingTimeout time.Duration
MaxRetryAttempts int32
StatsQueueClient client.QueueClient
}

func New(ctx context.Context, cfg *queueConfig.QueueConfig, service *services.Services) *QueueClient {
func New(ctx context.Context, cfg *queueConfig.QueueConfig, service *services.Services) *Queue {
statsQueueClient, err := client.NewQueueClient(
cfg, client.StakingStatsQueueName,
)
if err != nil {
log.Fatal().Err(err).Msg("error while creating StatsQueueClient")
}

return &QueueClient{
return &Queue{
ProcessingTimeout: time.Duration(cfg.QueueProcessingTimeout) * time.Second,
MaxRetryAttempts: cfg.MsgMaxRetryAttempts,
StatsQueueClient: statsQueueClient,
Expand Down
2 changes: 1 addition & 1 deletion internal/shared/queue/client/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package queueclient

type QueueClientInterface interface {
type QueueClient interface {
StartReceivingMessages()
StopReceivingMessages()
IsConnectionHealthy() error
Expand Down
2 changes: 1 addition & 1 deletion internal/shared/services/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/shared/types"
)

type ServiceInterface interface {
type SharedServiceProvider interface {
DoHealthCheck(ctx context.Context) error
VerifyUTXOs(ctx context.Context, utxos []types.UTXOIdentifier, address string) ([]*SafeUTXOPublic, *types.Error)
SaveUnprocessableMessages(ctx context.Context, messages string, receipt string) *types.Error
Expand Down
12 changes: 6 additions & 6 deletions internal/shared/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

type Services struct {
Service service.ServiceInterface
V1Service v1service.V1ServiceInterface
V2Service v2service.V2ServiceInterface
SharedService service.SharedServiceProvider
V1Service v1service.V1ServiceProvider
V2Service v2service.V2ServiceProvider
}

func New(
Expand All @@ -40,9 +40,9 @@ func New(
}

services := Services{
Service: service,
V1Service: v1Service,
V2Service: v2Service,
SharedService: service,
V1Service: v1Service,
V2Service: v2Service,
}

return &services, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/v1/api/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

type V1Handler struct {
*handler.Handler
Service v1service.V1ServiceInterface
Service v1service.V1ServiceProvider
}

func New(
ctx context.Context, handler *handler.Handler, v1Service v1service.V1ServiceInterface,
ctx context.Context, handler *handler.Handler, v1Service v1service.V1ServiceProvider,
) (*V1Handler, error) {
return &V1Handler{
Handler: handler,
Expand Down
4 changes: 2 additions & 2 deletions internal/v1/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
v1dbmodel "github.com/babylonlabs-io/staking-api-service/internal/v1/db/model"
)

type V1DBClientInterface interface {
dbclient.DBClientInterface
type V1DBClient interface {
dbclient.DBClient
SaveActiveStakingDelegation(
ctx context.Context, stakingTxHashHex, stakerPkHex, fpPkHex string,
stakingTxHex string, amount, startHeight, timelock, outputIndex uint64,
Expand Down
6 changes: 3 additions & 3 deletions internal/v1/queue/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type V1QueueClient struct {
*queueclient.QueueClient
*queueclient.Queue
Handler *v1queuehandler.V1QueueHandler
ActiveStakingQueueClient client.QueueClient
ExpiredStakingQueueClient client.QueueClient
Expand All @@ -18,7 +18,7 @@ type V1QueueClient struct {
BtcInfoQueueClient client.QueueClient
}

func New(cfg *queueConfig.QueueConfig, handler *v1queuehandler.V1QueueHandler, queueClient *queueclient.QueueClient) *V1QueueClient {
func New(cfg *queueConfig.QueueConfig, handler *v1queuehandler.V1QueueHandler, queueClient *queueclient.Queue) *V1QueueClient {
activeStakingQueueClient, err := client.NewQueueClient(
cfg, client.ActiveStakingQueueName,
)
Expand Down Expand Up @@ -52,7 +52,7 @@ func New(cfg *queueConfig.QueueConfig, handler *v1queuehandler.V1QueueHandler, q
log.Fatal().Err(err).Msg("error while creating BtcInfoQueueClient")
}
return &V1QueueClient{
QueueClient: queueClient,
Queue: queueClient,
Handler: handler,
ActiveStakingQueueClient: activeStakingQueueClient,
ExpiredStakingQueueClient: expiredStakingQueueClient,
Expand Down
4 changes: 2 additions & 2 deletions internal/v1/queue/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

type V1QueueHandler struct {
*queuehandler.QueueHandler
Service v1service.V1ServiceInterface
Service v1service.V1ServiceProvider
}

func New(queueHandler *queuehandler.QueueHandler, service v1service.V1ServiceInterface) *V1QueueHandler {
func New(queueHandler *queuehandler.QueueHandler, service v1service.V1ServiceProvider) *V1QueueHandler {
return &V1QueueHandler{
QueueHandler: queueHandler,
Service: service,
Expand Down
4 changes: 2 additions & 2 deletions internal/v1/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
v1model "github.com/babylonlabs-io/staking-api-service/internal/v1/db/model"
)

type V1ServiceInterface interface {
service.ServiceInterface
type V1ServiceProvider interface {
service.SharedServiceProvider
// Delegation
DelegationsByStakerPk(ctx context.Context, stakerPk string, state types.DelegationState, pageToken string) ([]DelegationPublic, string, *types.Error)
SaveActiveStakingDelegation(ctx context.Context, txHashHex, stakerPkHex, finalityProviderPkHex string, value, startHeight uint64, stakingTimestamp int64, timeLock, stakingOutputIndex uint64, stakingTxHex string, isOverflow bool) *types.Error
Expand Down
4 changes: 2 additions & 2 deletions internal/v2/api/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

type V2Handler struct {
*handler.Handler
Service v2service.V2ServiceInterface
Service v2service.V2ServiceProvider
}

func New(
ctx context.Context, handler *handler.Handler, v2Service v2service.V2ServiceInterface,
ctx context.Context, handler *handler.Handler, v2Service v2service.V2ServiceProvider,
) (*V2Handler, error) {
return &V2Handler{
Handler: handler,
Expand Down
4 changes: 2 additions & 2 deletions internal/v2/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import (
dbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/client"
)

type V2DBClientInterface interface {
dbclient.DBClientInterface
type V2DBClient interface {
dbclient.DBClient
}
8 changes: 4 additions & 4 deletions internal/v2/queue/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

type V2QueueClient struct {
*queueclient.QueueClient
*queueclient.Queue
Handler *v2queuehandler.V2QueueHandler
}

func New(cfg *queueConfig.QueueConfig, handler *v2queuehandler.V2QueueHandler, queueClient *queueclient.QueueClient) *V2QueueClient {
func New(cfg *queueConfig.QueueConfig, handler *v2queuehandler.V2QueueHandler, queueClient *queueclient.Queue) *V2QueueClient {
return &V2QueueClient{
QueueClient: queueClient,
Handler: handler,
Queue: queueClient,
Handler: handler,
}
}
4 changes: 2 additions & 2 deletions internal/v2/queue/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

type V2QueueHandler struct {
*queuehandler.QueueHandler
Service v1service.V1ServiceInterface
Service v1service.V1ServiceProvider
}

func New(queueHandler *queuehandler.QueueHandler, service v1service.V1ServiceInterface) *V2QueueHandler {
func New(queueHandler *queuehandler.QueueHandler, service v1service.V1ServiceProvider) *V2QueueHandler {
return &V2QueueHandler{
QueueHandler: queueHandler,
Service: service,
Expand Down
4 changes: 2 additions & 2 deletions internal/v2/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/shared/types"
)

type V2ServiceInterface interface {
service.ServiceInterface
type V2ServiceProvider interface {
service.SharedServiceProvider
GetFinalityProviders(ctx context.Context, paginationKey string) ([]FinalityProviderPublic, string, *types.Error)
GetGlobalParams(ctx context.Context) (GlobalParamsPublic, *types.Error)
GetOverallStats(ctx context.Context) (OverallStatsPublic, *types.Error)
Expand Down

0 comments on commit c0e8862

Please sign in to comment.