[#505] ir/container: Slightly refactor Put and Delete handlers

Split up `processContainerPut` and `processContainerDelete` methods of
container `Processor` into two sub-methods: checking the event and its
assertion.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-19 17:55:43 +03:00 committed by Alex Vanin
parent a3ac294902
commit 9259ae640e

View file

@ -1,6 +1,8 @@
package container package container
import ( import (
"fmt"
containerSDK "github.com/nspcc-dev/neofs-api-go/pkg/container" containerSDK "github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-node/pkg/core/container" "github.com/nspcc-dev/neofs-node/pkg/core/container"
containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container" containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
@ -20,33 +22,45 @@ func (cp *Processor) processContainerPut(put *containerEvent.Put) {
return return
} }
cnrData := put.Container() err := cp.checkPutContainer(put)
if err != nil {
// unmarshal container structure cp.log.Error("put container check failed",
cnr := containerSDK.New()
if err := cnr.Unmarshal(cnrData); err != nil {
cp.log.Info("could not unmarshal container structure",
zap.String("error", err.Error()), zap.String("error", err.Error()),
) )
return return
} }
cp.approvePutContainer(put)
}
func (cp *Processor) checkPutContainer(e *containerEvent.Put) error {
// unmarshal container structure
cnr := containerSDK.New()
err := cnr.Unmarshal(e.Container())
if err != nil {
return fmt.Errorf("invalid binary container: %w", err)
}
// perform format check // perform format check
if err := container.CheckFormat(cnr); err != nil { err = container.CheckFormat(cnr)
cp.log.Info("container with incorrect format detected", if err != nil {
zap.String("error", err.Error()), return fmt.Errorf("incorrect container format: %w", err)
)
return
} }
return nil
}
func (cp *Processor) approvePutContainer(e *containerEvent.Put) {
err := cp.morphClient.NotaryInvoke(cp.containerContract, cp.feeProvider.SideChainFee(), putContainerMethod, err := cp.morphClient.NotaryInvoke(cp.containerContract, cp.feeProvider.SideChainFee(), putContainerMethod,
cnrData, e.Container(),
put.Signature(), e.Signature(),
put.PublicKey()) e.PublicKey())
if err != nil { if err != nil {
cp.log.Error("can't invoke new container", zap.Error(err)) cp.log.Error("could not approve put container",
zap.String("error", err.Error()),
)
} }
} }
@ -58,10 +72,29 @@ func (cp *Processor) processContainerDelete(delete *containerEvent.Delete) {
return return
} }
err := cp.morphClient.NotaryInvoke(cp.containerContract, cp.feeProvider.SideChainFee(), deleteContainerMethod, err := cp.checkDeleteContainer(delete)
delete.ContainerID(),
delete.Signature())
if err != nil { if err != nil {
cp.log.Error("can't invoke delete container", zap.Error(err)) cp.log.Error("delete container check failed",
zap.String("error", err.Error()),
)
return
}
cp.approveDeleteContainer(delete)
}
func (cp *Processor) checkDeleteContainer(e *containerEvent.Delete) error {
return nil
}
func (cp *Processor) approveDeleteContainer(e *containerEvent.Delete) {
err := cp.morphClient.NotaryInvoke(cp.containerContract, cp.feeProvider.SideChainFee(), deleteContainerMethod,
e.ContainerID(),
e.Signature())
if err != nil {
cp.log.Error("could not approve delete container",
zap.String("error", err.Error()),
)
} }
} }