From aeefa686a865d93286b6011f210b3add6869626f Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Sat, 26 Oct 2024 12:25:30 +0900 Subject: [PATCH] resolve comments --- config/config-docker.yml | 2 +- config/config-local.yml | 2 +- docs/docs.go | 56 ------------------- docs/swagger.json | 56 ------------------- docs/swagger.yaml | 36 ------------ internal/api/handlers/handler.go | 26 ++++----- internal/api/handlers/terms.go | 13 ++--- internal/api/routes.go | 4 +- internal/config/config.go | 16 ++---- internal/config/terms.go | 6 -- internal/db/interface.go | 2 +- internal/db/model/terms.go | 12 ++-- internal/services/terms.go | 7 +-- tests/config/config-test.yml | 2 +- .../integration_test/terms_acceptance_test.go | 11 ++-- 15 files changed, 45 insertions(+), 206 deletions(-) diff --git a/config/config-docker.yml b/config/config-docker.yml index 8fd30a98..ed016921 100644 --- a/config/config-docker.yml +++ b/config/config-docker.yml @@ -34,5 +34,5 @@ assets: host: "http://ord-poc.devnet.babylonchain.io" port: 8888 timeout: 1000 -terms_acceptance: +terms_acceptance_logging: enabled: true diff --git a/config/config-local.yml b/config/config-local.yml index 1ad22a6d..97f8f53b 100644 --- a/config/config-local.yml +++ b/config/config-local.yml @@ -34,5 +34,5 @@ assets: host: "http://ord-poc.devnet.babylonchain.io" port: 8888 timeout: 5000 -terms_acceptance: +terms_acceptance_logging: enabled: true diff --git a/docs/docs.go b/docs/docs.go index f8ea96bd..f44ec4a9 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -38,46 +38,6 @@ const docTemplate = `{ } } }, - "/terms-acceptance": { - "post": { - "description": "Accept terms by the staker's BTC address (Taproot or Native Segwit)", - "produces": [ - "application/json" - ], - "parameters": [ - { - "type": "string", - "description": "Staker BTC address in Taproot/Native Segwit format", - "name": "address", - "in": "query", - "required": true - }, - { - "description": "Terms acceptance request", - "name": "terms_accepted", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/handlers.TermsAcceptanceRequest" - } - } - ], - "responses": { - "200": { - "description": "Terms acceptance result", - "schema": { - "$ref": "#/definitions/handlers.TermsAcceptancePublic" - } - }, - "400": { - "description": "Error: Bad Request", - "schema": { - "$ref": "#/definitions/github_com_babylonlabs-io_staking-api-service_internal_types.Error" - } - } - } - } - }, "/v1/delegation": { "get": { "description": "Retrieves a delegation by a given transaction hash", @@ -514,22 +474,6 @@ const docTemplate = `{ } } }, - "handlers.TermsAcceptancePublic": { - "type": "object", - "properties": { - "status": { - "type": "boolean" - } - } - }, - "handlers.TermsAcceptanceRequest": { - "type": "object", - "properties": { - "terms_accepted": { - "type": "boolean" - } - } - }, "handlers.UnbondDelegationRequestPayload": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 6361e422..b903030b 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -30,46 +30,6 @@ } } }, - "/terms-acceptance": { - "post": { - "description": "Accept terms by the staker's BTC address (Taproot or Native Segwit)", - "produces": [ - "application/json" - ], - "parameters": [ - { - "type": "string", - "description": "Staker BTC address in Taproot/Native Segwit format", - "name": "address", - "in": "query", - "required": true - }, - { - "description": "Terms acceptance request", - "name": "terms_accepted", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/handlers.TermsAcceptanceRequest" - } - } - ], - "responses": { - "200": { - "description": "Terms acceptance result", - "schema": { - "$ref": "#/definitions/handlers.TermsAcceptancePublic" - } - }, - "400": { - "description": "Error: Bad Request", - "schema": { - "$ref": "#/definitions/github_com_babylonlabs-io_staking-api-service_internal_types.Error" - } - } - } - } - }, "/v1/delegation": { "get": { "description": "Retrieves a delegation by a given transaction hash", @@ -506,22 +466,6 @@ } } }, - "handlers.TermsAcceptancePublic": { - "type": "object", - "properties": { - "status": { - "type": "boolean" - } - } - }, - "handlers.TermsAcceptanceRequest": { - "type": "object", - "properties": { - "terms_accepted": { - "type": "boolean" - } - } - }, "handlers.UnbondDelegationRequestPayload": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index f82077e1..8ec56d27 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -68,16 +68,6 @@ definitions: status: type: integer type: object - handlers.TermsAcceptancePublic: - properties: - status: - type: boolean - type: object - handlers.TermsAcceptanceRequest: - properties: - terms_accepted: - type: boolean - type: object handlers.UnbondDelegationRequestPayload: properties: staker_signed_signature_hex: @@ -267,32 +257,6 @@ paths: schema: type: string summary: Health check endpoint - /terms-acceptance: - post: - description: Accept terms by the staker's BTC address (Taproot or Native Segwit) - parameters: - - description: Staker BTC address in Taproot/Native Segwit format - in: query - name: address - required: true - type: string - - description: Terms acceptance request - in: body - name: terms_accepted - required: true - schema: - $ref: '#/definitions/handlers.TermsAcceptanceRequest' - produces: - - application/json - responses: - "200": - description: Terms acceptance result - schema: - $ref: '#/definitions/handlers.TermsAcceptancePublic' - "400": - description: 'Error: Bad Request' - schema: - $ref: '#/definitions/github_com_babylonlabs-io_staking-api-service_internal_types.Error' /v1/delegation: get: description: Retrieves a delegation by a given transaction hash diff --git a/internal/api/handlers/handler.go b/internal/api/handlers/handler.go index d0421986..9e1941c2 100644 --- a/internal/api/handlers/handler.go +++ b/internal/api/handlers/handler.go @@ -173,26 +173,26 @@ func parseStateFilterQuery( return stateEnum, nil } -// parseTermsAcceptanceQuery parses the terms acceptance query and returns the address, public key, and terms accepted -func parseTermsAcceptanceQuery(request *http.Request, btcNetParam *chaincfg.Params) (string, string, bool, *types.Error) { - var req TermsAcceptanceRequest +// parseTermsAcceptanceLoggingRequest parses the terms acceptance request bdoy and returns the address and public key +func parseTermsAcceptanceLoggingRequest(request *http.Request, btcNetParam *chaincfg.Params) (string, string, *types.Error) { + var req TermsAcceptanceLoggingRequest if err := json.NewDecoder(request.Body).Decode(&req); err != nil { - return "", "", false, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Invalid request payload") + return "", "", types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Invalid request payload") } - address, err := parseBtcAddressQuery(request, "address", btcNetParam) - if err != nil { - return "", "", false, err + // Validate the Bitcoin address + if _, err := utils.CheckBtcAddressType(req.Address, btcNetParam); err != nil { + return "", "", types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Invalid Bitcoin address") } - publicKey, err := parsePublicKeyQuery(request, "public_key", false) - if err != nil { - return "", "", false, err + // Validate the public key + if _, err := utils.GetSchnorrPkFromHex(req.PublicKey); err != nil { + return "", "", types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Invalid public key") } - if address == "" || publicKey == "" { - return "", "", false, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Address and public key are required") + if req.Address == "" || req.PublicKey == "" { + return "", "", types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Address and public key are required") } - return address, publicKey, req.TermsAccepted, nil + return req.Address, req.PublicKey, nil } diff --git a/internal/api/handlers/terms.go b/internal/api/handlers/terms.go index b48ddf92..676c6869 100644 --- a/internal/api/handlers/terms.go +++ b/internal/api/handlers/terms.go @@ -6,23 +6,22 @@ import ( "github.com/babylonlabs-io/staking-api-service/internal/types" ) -type TermsAcceptanceRequest struct { - Address string `json:"address"` - TermsAccepted bool `json:"terms_accepted"` - PublicKey string `json:"public_key"` +type TermsAcceptanceLoggingRequest struct { + Address string `json:"address"` + PublicKey string `json:"public_key"` } type TermsAcceptancePublic struct { Status bool `json:"status"` } -func (h *Handler) AcceptTerms(request *http.Request) (*Result, *types.Error) { - address, publicKey, termsAccepted, err := parseTermsAcceptanceQuery(request, h.config.Server.BTCNetParam) +func (h *Handler) LogTermsAcceptance(request *http.Request) (*Result, *types.Error) { + address, publicKey, err := parseTermsAcceptanceLoggingRequest(request, h.config.Server.BTCNetParam) if err != nil { return nil, err } - if err := h.services.AcceptTerms(request.Context(), address, publicKey, termsAccepted); err != nil { + if err := h.services.AcceptTerms(request.Context(), address, publicKey); err != nil { return nil, err } diff --git a/internal/api/routes.go b/internal/api/routes.go index d6125988..abb729e4 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -10,8 +10,8 @@ func (a *Server) SetupRoutes(r *chi.Mux) { handlers := a.handlers r.Get("/healthcheck", registerHandler(handlers.HealthCheck)) - if a.cfg.TermsAcceptance.Enabled { - r.Post("/terms-acceptance", registerHandler(handlers.AcceptTerms)) + if a.cfg.TermsAcceptanceLogging.Enabled { + r.Post("/terms-acceptance", registerHandler(handlers.LogTermsAcceptance)) } r.Get("/v1/staker/delegations", registerHandler(handlers.GetStakerDelegations)) diff --git a/internal/config/config.go b/internal/config/config.go index 976cdc05..3ebad611 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,12 +10,12 @@ import ( ) type Config struct { - Server *ServerConfig `mapstructure:"server"` - Db *DbConfig `mapstructure:"db"` - Queue *queue.QueueConfig `mapstructure:"queue"` - Metrics *MetricsConfig `mapstructure:"metrics"` - Assets *AssetsConfig `mapstructure:"assets"` - TermsAcceptance *TermsAcceptanceConfig `mapstructure:"terms_acceptance"` + Server *ServerConfig `mapstructure:"server"` + Db *DbConfig `mapstructure:"db"` + Queue *queue.QueueConfig `mapstructure:"queue"` + Metrics *MetricsConfig `mapstructure:"metrics"` + Assets *AssetsConfig `mapstructure:"assets"` + TermsAcceptanceLogging *TermsAcceptanceConfig `mapstructure:"terms_acceptance_logging"` } func (cfg *Config) Validate() error { @@ -42,10 +42,6 @@ func (cfg *Config) Validate() error { } } - if err := cfg.TermsAcceptance.Validate(); err != nil { - return err - } - return nil } diff --git a/internal/config/terms.go b/internal/config/terms.go index 0a6312ec..04d94c2c 100644 --- a/internal/config/terms.go +++ b/internal/config/terms.go @@ -3,9 +3,3 @@ package config type TermsAcceptanceConfig struct { Enabled bool `mapstructure:"enabled"` } - -func (cfg *TermsAcceptanceConfig) Validate() error { - // No validation needed for Enabled field as it can be either true or false - - return nil -} diff --git a/internal/db/interface.go b/internal/db/interface.go index 3b849f51..d64280ba 100644 --- a/internal/db/interface.go +++ b/internal/db/interface.go @@ -104,7 +104,7 @@ type DBClient interface { ctx context.Context, paginationToken string, ) (*DbResultMap[model.DelegationDocument], error) - // Accept terms saves the acceptance of the terms of service of the public key + // SaveTermsAcceptance saves the acceptance of the terms of service of the public key SaveTermsAcceptance(ctx context.Context, termsAcceptance *model.TermsAcceptance) error } diff --git a/internal/db/model/terms.go b/internal/db/model/terms.go index 9fa1349d..3b055636 100644 --- a/internal/db/model/terms.go +++ b/internal/db/model/terms.go @@ -1,11 +1,11 @@ package model -import "time" +import ( + "go.mongodb.org/mongo-driver/bson/primitive" +) type TermsAcceptance struct { - Id string `bson:"_id"` - Address string `bson:"address"` - PublicKey string `bson:"public_key"` - TermsAccepted bool `bson:"terms_accepted"` - UpdatedAt time.Time `bson:"updated_at"` + Id primitive.ObjectID `bson:"_id,omitempty"` + Address string `bson:"address"` + PublicKey string `bson:"public_key"` } diff --git a/internal/services/terms.go b/internal/services/terms.go index d8b7f250..0ed1bf42 100644 --- a/internal/services/terms.go +++ b/internal/services/terms.go @@ -7,11 +7,10 @@ import ( "github.com/babylonlabs-io/staking-api-service/internal/types" ) -func (s *Services) AcceptTerms(ctx context.Context, address, publicKey string, termsAccepted bool) *types.Error { +func (s *Services) AcceptTerms(ctx context.Context, address, publicKey string) *types.Error { termsAcceptance := &model.TermsAcceptance{ - Address: address, - PublicKey: publicKey, - TermsAccepted: termsAccepted, + Address: address, + PublicKey: publicKey, } if err := s.DbClient.SaveTermsAcceptance(ctx, termsAcceptance); err != nil { diff --git a/tests/config/config-test.yml b/tests/config/config-test.yml index cd3259ad..db0e31c9 100644 --- a/tests/config/config-test.yml +++ b/tests/config/config-test.yml @@ -34,5 +34,5 @@ assets: host: "http://ord-poc.devnet.babylonchain.io" port: 8888 timeout: 100 -terms_acceptance: +terms_acceptance_logging: enabled: true diff --git a/tests/integration_test/terms_acceptance_test.go b/tests/integration_test/terms_acceptance_test.go index b39e5de6..ba37be2f 100644 --- a/tests/integration_test/terms_acceptance_test.go +++ b/tests/integration_test/terms_acceptance_test.go @@ -26,12 +26,13 @@ func TestTermsAcceptance(t *testing.T) { publicKey, _ := testutils.RandomPk() // Prepare request body - requestBody := handlers.TermsAcceptanceRequest{ - TermsAccepted: true, + requestBody := handlers.TermsAcceptanceLoggingRequest{ + Address: address, + PublicKey: publicKey, } bodyBytes, _ := json.Marshal(requestBody) - url := testServer.Server.URL + termsAcceptancePath + "?address=" + address + "&public_key=" + publicKey + url := testServer.Server.URL + termsAcceptancePath resp, err := http.Post(url, "application/json", bytes.NewReader(bodyBytes)) assert.NoError(t, err, "making POST request to terms acceptance endpoint should not fail") defer resp.Body.Close() @@ -52,9 +53,7 @@ func TestTermsAcceptanceInvalidAddress(t *testing.T) { invalidAddress := "invalidaddress" publicKey, _ := testutils.RandomPk() - requestBody := handlers.TermsAcceptanceRequest{ - TermsAccepted: true, - } + requestBody := handlers.TermsAcceptanceLoggingRequest{} bodyBytes, _ := json.Marshal(requestBody) url := testServer.Server.URL + termsAcceptancePath + "?address=" + invalidAddress + "&public_key=" + publicKey