2021-11-26 12:19:20 +00:00
|
|
|
package subnetevents
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/subnet"
|
|
|
|
subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-11-26 12:19:20 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Put represents a notification about NeoFS subnet creation.
|
2021-11-26 12:19:20 +00:00
|
|
|
// Generated by a contract when intending to create a subnet.
|
|
|
|
type Put interface {
|
2022-04-21 11:28:05 +00:00
|
|
|
// Contains the ID of the subnet to be created.
|
2021-11-26 12:19:20 +00:00
|
|
|
eventWithID
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ReadCreator reads the user ID of the subnet creator.
|
|
|
|
// Returns an error if the ID is missing.
|
2022-05-17 13:59:46 +00:00
|
|
|
ReadCreator(id *user.ID) error
|
2021-11-26 12:19:20 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ReadInfo reads information about a subnet to be created.
|
2021-11-26 12:19:20 +00:00
|
|
|
ReadInfo(info *subnet.Info) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutValidator asserts intent to create a subnet.
|
|
|
|
type PutValidator struct{}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// errDiffOwner is returned when the subnet owners differ.
|
2021-11-26 12:19:20 +00:00
|
|
|
var errDiffOwner = errors.New("diff subnet owners")
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// errDiffID is returned when the subnet IDs differ.
|
2021-11-26 12:19:20 +00:00
|
|
|
var errDiffID = errors.New("diff subnet IDs")
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Assert processes the attempt to create a subnet. It approves the creation through nil return.
|
2021-11-26 12:19:20 +00:00
|
|
|
//
|
|
|
|
// All read errors of Put are forwarded.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// It returns an error on:
|
2021-11-26 12:19:20 +00:00
|
|
|
// * zero subnet creation;
|
|
|
|
// * empty ID or different from the one wired into info;
|
|
|
|
// * empty owner ID or different from the one wired into info.
|
|
|
|
func (x PutValidator) Assert(event Put) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// read ID
|
|
|
|
var id subnetid.ID
|
|
|
|
if err = event.ReadID(&id); err != nil {
|
|
|
|
return fmt.Errorf("read ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevent zero subnet creation
|
|
|
|
if subnetid.IsZero(id) {
|
|
|
|
return zeroSubnetOp{
|
|
|
|
op: "creation",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read creator's user ID in NeoFS system
|
2022-05-17 13:59:46 +00:00
|
|
|
var creator user.ID
|
2021-11-26 12:19:20 +00:00
|
|
|
if err = event.ReadCreator(&creator); err != nil {
|
|
|
|
return fmt.Errorf("read creator: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read information about the subnet
|
|
|
|
var info subnet.Info
|
|
|
|
if err = event.ReadInfo(&info); err != nil {
|
|
|
|
return fmt.Errorf("read info: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// check if the explicit ID equals to the one from info
|
2022-06-15 08:08:10 +00:00
|
|
|
if !subnet.AssertReference(info, id) {
|
2021-11-26 12:19:20 +00:00
|
|
|
return errDiffID
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// check if the explicit creator equals to the one from info
|
2022-06-15 08:08:10 +00:00
|
|
|
if !subnet.AssertOwnership(info, creator) {
|
2021-11-26 12:19:20 +00:00
|
|
|
return errDiffOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|