From e53a580ef2adb14a06f44f6784f86ba80c9d14ec Mon Sep 17 00:00:00 2001 From: husobee Date: Mon, 5 Feb 2024 13:14:43 -0500 Subject: [PATCH] review feedback, change response time for aretimelimitedv2credssubmitted --- services/skus/credentials.go | 6 +++--- services/skus/datastore.go | 27 ++++++++++++------------- services/skus/instrumented_datastore.go | 2 +- services/skus/mockdatastore.go | 9 ++++----- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/services/skus/credentials.go b/services/skus/credentials.go index 98dbe8f23..1749891e5 100644 --- a/services/skus/credentials.go +++ b/services/skus/credentials.go @@ -331,16 +331,16 @@ func (s *Service) doTLV2Exist(ctx context.Context, requestID uuid.UUID, item *mo } // Check TLV2 to see if we have credentials signed that match incoming blinded tokens. - alreadySubmitted, mismatch, err := s.Datastore.AreTimeLimitedV2CredsSubmitted(ctx, requestID, blindedCreds...) + credsSubmitted, err := s.Datastore.AreTimeLimitedV2CredsSubmitted(ctx, requestID, blindedCreds...) if err != nil { return fmt.Errorf("error validating credentials exist for order item: %w", err) } - if alreadySubmitted { + if credsSubmitted.AlreadySubmitted { // No need to create order credentials, since these are already submitted. return errCredsAlreadySubmitted } - if mismatch { + if credsSubmitted.Mismatch { // conflict because those credentials were submitted with a different request id return errCredsAlreadySubmittedMismatch } diff --git a/services/skus/datastore.go b/services/skus/datastore.go index 10f4d1dd7..9b928b6ac 100644 --- a/services/skus/datastore.go +++ b/services/skus/datastore.go @@ -80,7 +80,7 @@ type Datastore interface { GetOrderCreds(orderID uuid.UUID, isSigned bool) ([]OrderCreds, error) SendSigningRequest(ctx context.Context, signingRequestWriter SigningRequestWriter) error InsertSignedOrderCredentialsTx(ctx context.Context, tx *sqlx.Tx, signedOrderResult *SigningOrderResult) error - AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID uuid.UUID, blindedCreds ...string) (bool, bool, error) + AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID uuid.UUID, blindedCreds ...string) (*AreTimeLimitedV2CredsSubmittedResult, error) GetTimeLimitedV2OrderCredsByOrder(orderID uuid.UUID) (*TimeLimitedV2Creds, error) GetTLV2Creds(ctx context.Context, dbi sqlx.QueryerContext, ordID, itemID, reqID uuid.UUID) (*TimeLimitedV2Creds, error) DeleteTimeLimitedV2OrderCredsByOrderTx(ctx context.Context, tx *sqlx.Tx, orderID uuid.UUID) error @@ -950,11 +950,18 @@ type TimeAwareSubIssuedCreds struct { RequestID string `json:"-" db:"request_id"` } -func (pg *Postgres) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID uuid.UUID, blindedCreds ...string) (bool, bool, error) { +type AreTimeLimitedV2CredsSubmittedResult struct { + AlreadySubmitted bool `db:"already_submitted"` + Mismatch bool `db:"mismatch"` +} + +func (pg *Postgres) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID uuid.UUID, blindedCreds ...string) (*AreTimeLimitedV2CredsSubmittedResult, error) { if len(blindedCreds) < 1 { - return false, false, errors.New("invalid parameter to tlv2 creds signed") + return nil, errors.New("invalid parameter to tlv2 creds signed") } + var result = AreTimeLimitedV2CredsSubmittedResult{} + query := ` select exists( select 1 from time_limited_v2_order_creds where blinded_creds->>0 = $1 @@ -963,20 +970,12 @@ func (pg *Postgres) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestI select 1 from time_limited_v2_order_creds where blinded_creds->>0 = $1 and request_id != $2 ) as mismatch ` - - type data struct { - AlreadySubmitted bool `db:"already_submitted"` - Mismatch bool `db:"mismatch"` - } - - var d = data{} - - err := pg.RawDB().Get(&d, query, blindedCreds[0], requestID) + err := pg.RawDB().Get(&result, query, blindedCreds[0], requestID) if err != nil { - return false, false, err + return nil, err } - return d.AlreadySubmitted, d.Mismatch, nil + return &result, nil } // GetTimeLimitedV2OrderCredsByOrder returns all the non expired time limited v2 order credentials for a given order. diff --git a/services/skus/instrumented_datastore.go b/services/skus/instrumented_datastore.go index 41d30031a..1907b5bac 100644 --- a/services/skus/instrumented_datastore.go +++ b/services/skus/instrumented_datastore.go @@ -87,7 +87,7 @@ func (_d DatastoreWithPrometheus) AppendOrderMetadataInt64(ctx context.Context, } // AreTimeLimitedV2CredsSubmitted implements Datastore -func (_d DatastoreWithPrometheus) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID uuid.UUID, blindedCreds ...string) (b1 bool, b2 bool, err error) { +func (_d DatastoreWithPrometheus) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID uuid.UUID, blindedCreds ...string) (ap1 *AreTimeLimitedV2CredsSubmittedResult, err error) { _since := time.Now() defer func() { result := "ok" diff --git a/services/skus/mockdatastore.go b/services/skus/mockdatastore.go index f05b6533f..b7263a06d 100644 --- a/services/skus/mockdatastore.go +++ b/services/skus/mockdatastore.go @@ -85,17 +85,16 @@ func (mr *MockDatastoreMockRecorder) AppendOrderMetadataInt64(arg0, arg1, arg2, } // AreTimeLimitedV2CredsSubmitted mocks base method. -func (m *MockDatastore) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID go_uuid.UUID, blindedCreds ...string) (bool, bool, error) { +func (m *MockDatastore) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID go_uuid.UUID, blindedCreds ...string) (*AreTimeLimitedV2CredsSubmittedResult, error) { m.ctrl.T.Helper() varargs := []interface{}{ctx, requestID} for _, a := range blindedCreds { varargs = append(varargs, a) } ret := m.ctrl.Call(m, "AreTimeLimitedV2CredsSubmitted", varargs...) - ret0, _ := ret[0].(bool) - ret1, _ := ret[1].(bool) - ret2, _ := ret[2].(error) - return ret0, ret1, ret2 + ret0, _ := ret[0].(*AreTimeLimitedV2CredsSubmittedResult) + ret1, _ := ret[1].(error) + return ret0, ret1 } // AreTimeLimitedV2CredsSubmitted indicates an expected call of AreTimeLimitedV2CredsSubmitted.