Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually aborting a distribution #99

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api-scipts/distributions.http
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ content-type: application/json
GET http://localhost:3000/v1/distributions/{{ distributionId }} HTTP/1.1
content-type: application/json

### Cancel
DELETE http://localhost:3000/v1/distributions/{{ distributionId }} HTTP/1.1
### Abort
POST http://localhost:3000/v1/distributions/{{ distributionId }}/abort HTTP/1.1
content-type: application/json
14 changes: 7 additions & 7 deletions reference/Flow-PDS-API.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ paths:
name: distributionId
in: path
required: true
description: Distribution offchain ID
get:
summary: Get Distribution
operationId: get-distribution-by-id
Expand All @@ -106,23 +107,22 @@ paths:
schema:
$ref: ../models/Distribution-Get.yaml
description: Returns the details for a distribution.
'/distributions/{distributionId}/cancel':
'/distributions/{distributionId}/abort':
parameters:
- schema:
type: string
name: distributionId
in: path
required: true
delete:
summary: Cancel distribution
operationId: cancel-distribution
description: Distribution offchain ID
post:
summary: Abort distribution
operationId: abort-distribution
responses:
'200':
description: OK
description: |-
THIS ENDOINT IS NOT IMPLEMENTED
description: Forcibly abort the process, which will put the Distribution into the Invalid state.

Cancel a distribution which is not in "complete" state. PDS should return all withdrawn NFTs and once all have been returned revoke all capabilities created during this distribution and delete the distribution from database.
components:
schemas: {}
responses:
Expand Down
7 changes: 3 additions & 4 deletions service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,15 @@ func (app *App) GetDistribution(ctx context.Context, id uuid.UUID) (*Distributio
return distribution, nil
}

// CancelDistribution cancels a distribution. Spec and implementation for
// distribution cancelling is not finished yet.
func (app *App) CancelDistribution(ctx context.Context, id uuid.UUID) error {
// AbortDistribution aborts a distribution.
func (app *App) AbortDistribution(ctx context.Context, id uuid.UUID) error {
return app.db.Transaction(func(tx *gorm.DB) error {
distribution, err := GetDistribution(tx, id)
if err != nil {
return err
}

if err := app.contract.Cancel(ctx, tx, distribution); err != nil {
if err := app.contract.Abort(ctx, tx, distribution); err != nil {
return err
}

Expand Down
45 changes: 32 additions & 13 deletions service/app/contract_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,26 +314,45 @@ func (c *Contract) StartMinting(ctx context.Context, db *gorm.DB, dist *Distribu
return nil
}

// Cancel needs to be specced and implemented.
func (c *Contract) Cancel(ctx context.Context, db *gorm.DB, dist *Distribution) error {
// TODO (latenssi)
// Abort a distribution
func (c *Contract) Abort(ctx context.Context, db *gorm.DB, dist *Distribution) error {

c.logger.WithFields(log.Fields{
"method": "Cancel",
"method": "Abort",
"ID": dist.ID,
}).Info("Cancel")
}).Info("Abort")

return fmt.Errorf("cancel is not yet implemented")
if err := dist.SetInvalid(); err != nil {
return err
}

// if err := dist.SetCancelled(); err != nil {
// return err
// }
if err := UpdateDistribution(db, dist); err != nil {
return err
}

// if err := UpdateDistribution(db, dist); err != nil {
// return err
// }
// Update distribution state onchain
txScript := util.ParseCadenceTemplate(UPDATE_STATE_SCRIPT)
arguments := []cadence.Value{
cadence.UInt64(dist.FlowID.Int64),
cadence.UInt8(1),
}
t, err := transactions.NewTransaction(UPDATE_STATE_SCRIPT, txScript, arguments)
if err != nil {
return err
}

if err := t.Save(db); err != nil {
return err
}

// return nil
c.logger.WithFields(log.Fields{
"method": "Abort",
"ID": dist.ID,
"state": 1,
"stateStr": "invalid",
}).Info("Distribution state update transaction saved")

return nil
}

// UpdateSettlementStatus polls for 'Deposit' events regarding the given distributions
Expand Down
6 changes: 3 additions & 3 deletions service/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func HandleGetDistribution(logger *log.Logger, app *app.App) http.HandlerFunc {
}
}

// Cancel a distribution
func HandleCancelDistribution(logger *log.Logger, app *app.App) http.HandlerFunc {
// Abort a distribution
func HandleAbortDistribution(logger *log.Logger, app *app.App) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

Expand All @@ -103,7 +103,7 @@ func HandleCancelDistribution(logger *log.Logger, app *app.App) http.HandlerFunc
return
}

if err := app.CancelDistribution(r.Context(), id); err != nil {
if err := app.AbortDistribution(r.Context(), id); err != nil {
handleError(rw, logger, err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion service/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewRouter(logger *log.Logger, app *app.App) http.Handler {
rv.HandleFunc("/distributions", HandleCreateDistribution(logger, app)).Methods(http.MethodPost)
rv.HandleFunc("/distributions", HandleListDistributions(logger, app)).Methods(http.MethodGet)
rv.HandleFunc("/distributions/{id}", HandleGetDistribution(logger, app)).Methods(http.MethodGet)
rv.HandleFunc("/distributions/{id}", HandleCancelDistribution(logger, app)).Methods(http.MethodDelete)
rv.HandleFunc("/distributions/{id}/abort", HandleAbortDistribution(logger, app)).Methods(http.MethodPost)

// Use middleware
h := UseCors(r)
Expand Down