Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

[OSE-176] Operations can be cancelled #216

Merged
merged 8 commits into from
Dec 15, 2022

Conversation

andresuribe87
Copy link
Contributor

@andresuribe87 andresuribe87 commented Dec 12, 2022

Overview

This PR implements the CancelOperation endpoint.

Description

After an operation is cancelled, the associated resource's status must be recorded. We currently only support operations related to Submission resources. In order to do so, this PR moved a number of methods around so there are no dependency cycles. The rule I followed was: submissions can depend on operations, but not the other way around.

Happy to hear feedback about better organizing packages.

How Has This Been Tested?

See added unit tests.

Checklist

Before submitting this PR, please make sure:

  • I have read the CONTRIBUTING document.
  • My code is consistent with the rest of the project
  • I have tagged the relevant reviewers and/or interested parties
  • I have updated the READMEs and other documentation of affected packages

References

SIP6

@andresuribe87 andresuribe87 marked this pull request as ready for review December 13, 2022 16:09
@codecov-commenter
Copy link

codecov-commenter commented Dec 13, 2022

Codecov Report

Attention: Patch coverage is 12.76596% with 41 lines in your changes missing coverage. Please review.

Project coverage is 23.07%. Comparing base (f5ac154) to head (8353da1).
Report is 291 commits behind head on main.

Files with missing lines Patch % Lines
pkg/service/operation/submission/submission.go 17.64% 28 Missing ⚠️
pkg/server/router/operation.go 0.00% 13 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #216      +/-   ##
==========================================
+ Coverage   22.67%   23.07%   +0.39%     
==========================================
  Files          26       25       -1     
  Lines        2293     2254      -39     
==========================================
  Hits          520      520              
+ Misses       1691     1652      -39     
  Partials       82       82              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

const ParentResource = "/presentations/submissions"

// Status indicates the current state of a submission.
type Status uint8
Copy link
Contributor

@nitro-neal nitro-neal Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a uint8 and not define a type for status

type (
Status string
)

const (
StatusPending Status = "pending"
StatusDenied Status = "denied"
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synced offline. We're storing uints, and exposing strings.

db *storage.BoltDB
}

func (b BoltOperationStorage) CancelOperation(id string) (opstorage.StoredOperation, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (b BoltOperationStorage) CancelOperation(id string) (opstorage.StoredOperation, error) {
func (b BoltOperationStorage) CancelOperation(id string) (*opstorage.StoredOperation, error) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

_, opData, err := b.db.UpdateValueAndOperation(
submission.Namespace, submission.ResourceID(id), storage.NewUpdater(map[string]any{
"status": submission.StatusCancelled,
"reason": cancelledReason,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when is this reason ever used/surfaced?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetSubmission returns an object that has a reason field. It's meant to be a simple record and can be empty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think it'll be useful for auditing purposes

if err != nil {
return util.LoggingErrorMsgf(err, "marshalling operation with id: %s", id)
}
return b.db.Write(namespace.FromID(id), id, jsonBytes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth pulling this off as a separate value to be able to wrap the error with a log in case it fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was actually moved only. That said, I've updated this to wrap and log.

Comment on lines 94 to 100
if err != nil {
stored = append(stored, nextOp)
continue
}
if include {
stored = append(stored, nextOp)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err != nil {
stored = append(stored, nextOp)
continue
}
if include {
stored = append(stored, nextOp)
}
if err != nil || include {
stored = append(stored, nextOp)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return nil
}

func NewBoltOperationStorage(db *storage.BoltDB) (*BoltOperationStorage, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I usually put the constructors at the top of the file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -102,8 +103,20 @@ func (s Service) GetOperation(request GetOperationRequest) (Operation, error) {
return serviceModel(storedOp)
}

func (s Service) CancelOperation(request CancelOperationRequest) (Operation, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (s Service) CancelOperation(request CancelOperationRequest) (Operation, error) {
func (s Service) CancelOperation(request CancelOperationRequest) (*Operation, error) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 29 to 32
const Namespace = "presentation_submission"

// ParentResource is the prefix of the submission parent resource.
const ParentResource = "/presentations/submissions"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit could put these in the same const () block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

DefinitionID: definition.PresentationDefinition.ID,
},
},
{
Status: "pending",
PresentationSubmission: &exchange.PresentationSubmission{
ID: operation.SubmissionID(op2.ID),
ID: submission.ResourceID(op2.ID),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why resource over submission? submission is more meaningful in context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to ID, as submission is already in the package name.

@@ -102,8 +103,20 @@ func (s Service) GetOperation(request GetOperationRequest) (Operation, error) {
return serviceModel(storedOp)
}

func (s Service) CancelOperation(request CancelOperationRequest) (Operation, error) {
if err := request.Validate(); err != nil {
return Operation{}, errors.Wrap(err, "invalid request")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Operation{}, errors.Wrap(err, "invalid request")
return nil, errors.Wrap(err, "invalid request")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


storedOp, err := s.storage.CancelOperation(request.ID)
if err != nil {
return Operation{}, errors.Wrap(err, "marking as done")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Operation{}, errors.Wrap(err, "marking as done")
return nil, errors.Wrap(err, "marking as done")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@decentralgabe decentralgabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving pending few small comments

@andresuribe87 andresuribe87 merged commit 384f92e into TBD54566975:main Dec 15, 2022
andresuribe87 added a commit that referenced this pull request Dec 15, 2022
* modity storage

* modify tests

* modify service storage

* change module name

* update

* fix warnings

* [OSE-176] Operations can be cancelled (#216)

* Initial draft of cancellation

* Linters

* Moving a constant.

* Better homes for many methods and variables.

* PR comments

* Lets make pointers.

* Rename func

* add schema caching (#219)

Co-authored-by: Andres Uribe <[email protected]>

* modity storage

* modify tests

* modify service storage

* change module name

* update

* fix warnings

* fix server test

* Fixes to the merge problems.

* Small fixes

* Lint

Co-authored-by: Andres Uribe <[email protected]>
Co-authored-by: Gabe <[email protected]>
Co-authored-by: Andres Uribe Gonzalez <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants