[#7] Add container delete notification handler

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-02 19:04:29 +03:00
parent 480b3fd1a9
commit 5e5e431534
5 changed files with 80 additions and 13 deletions

View file

@ -13,10 +13,17 @@ type (
Container []byte Container []byte
Signature []byte Signature []byte
} }
// ContainerParams for container put invocation.
RemoveContainerParams struct {
ContainerID []byte
Signature []byte
}
) )
const ( const (
putContainerMethod = "put" putContainerMethod = "put"
deleteContainerMethod = "delete"
) )
// RegisterContainer invokes Put method. // RegisterContainer invokes Put method.
@ -31,3 +38,15 @@ func RegisterContainer(cli *client.Client, con util.Uint160, p *ContainerParams)
p.Key.Bytes(), p.Key.Bytes(),
) )
} }
// RegisterContainer invokes Delete method.
func RemoveContainer(cli *client.Client, con util.Uint160, p *RemoveContainerParams) error {
if cli == nil {
return client.ErrNilClient
}
return cli.Invoke(con, extraFee, deleteContainerMethod,
p.ContainerID,
p.Signature,
)
}

View file

@ -26,7 +26,7 @@ const (
// for invocation calculated based on testinvoke which happens at collection // for invocation calculated based on testinvoke which happens at collection
// stage. Therefore client has to provide some extra SysFee to operate at // stage. Therefore client has to provide some extra SysFee to operate at
// execution stage. Otherwise invocation will fail due to gas limit. // execution stage. Otherwise invocation will fail due to gas limit.
extraFee = 5000_0000 // 0.5 Fixed8 gas extraFee = 1_5000_0000 // 1.5 Fixed8 gas
checkIsInnerRingMethod = "isInnerRing" checkIsInnerRingMethod = "isInnerRing"
chequeMethod = "cheque" chequeMethod = "cheque"

View file

@ -26,3 +26,19 @@ func (cp *Processor) handlePut(ev event.Event) {
zap.Int("capacity", cp.pool.Cap())) zap.Int("capacity", cp.pool.Cap()))
} }
} }
func (cp *Processor) handleDelete(ev event.Event) {
del := ev.(containerEvent.Delete) // todo: check panic in production
cp.log.Info("notification",
zap.String("type", "container delete"),
zap.String("id", base58.Encode(del.ContainerID())))
// send event to the worker pool
err := cp.pool.Submit(func() { cp.processContainerDelete(&del) })
if err != nil {
// todo: move into controlled degradation stage
cp.log.Warn("container processor worker pool drained",
zap.Int("capacity", cp.pool.Cap()))
}
}

View file

@ -24,3 +24,21 @@ func (cp *Processor) processContainerPut(put *containerEvent.Put) {
cp.log.Error("can't invoke new container", zap.Error(err)) cp.log.Error("can't invoke new container", zap.Error(err))
} }
} }
// Process delete container operation from the user by checking container sanity
// and sending approve tx back to morph.
func (cp *Processor) processContainerDelete(delete *containerEvent.Delete) {
if !cp.activeState.IsActive() {
cp.log.Info("passive mode, ignore container put")
return
}
err := invoke.RemoveContainer(cp.morphClient, cp.containerContract,
&invoke.RemoveContainerParams{
ContainerID: delete.ContainerID(),
Signature: delete.Signature(),
})
if err != nil {
cp.log.Error("can't invoke delete container", zap.Error(err))
}
}

View file

@ -37,6 +37,7 @@ type (
const ( const (
putNotification = "containerPut" putNotification = "containerPut"
deleteNotification = "containerDelete"
) )
// New creates container contract processor instance. // New creates container contract processor instance.
@ -71,11 +72,18 @@ func (cp *Processor) ListenerParsers() []event.ParserInfo {
var parsers []event.ParserInfo var parsers []event.ParserInfo
// container put event // container put event
deposit := event.ParserInfo{} put := event.ParserInfo{}
deposit.SetType(putNotification) put.SetType(putNotification)
deposit.SetScriptHash(cp.containerContract) put.SetScriptHash(cp.containerContract)
deposit.SetParser(containerEvent.ParsePut) put.SetParser(containerEvent.ParsePut)
parsers = append(parsers, deposit) parsers = append(parsers, put)
// container del event
del := event.ParserInfo{}
del.SetType(deleteNotification)
del.SetScriptHash(cp.containerContract)
del.SetParser(containerEvent.ParseDelete)
parsers = append(parsers, del)
return parsers return parsers
} }
@ -85,11 +93,17 @@ func (cp *Processor) ListenerHandlers() []event.HandlerInfo {
var handlers []event.HandlerInfo var handlers []event.HandlerInfo
// container put handler // container put handler
deposit := event.HandlerInfo{} put := event.HandlerInfo{}
deposit.SetType(putNotification) put.SetType(putNotification)
deposit.SetScriptHash(cp.containerContract) put.SetScriptHash(cp.containerContract)
deposit.SetHandler(cp.handlePut) put.SetHandler(cp.handlePut)
handlers = append(handlers, deposit) handlers = append(handlers, put)
del := event.HandlerInfo{}
del.SetType(deleteNotification)
del.SetScriptHash(cp.containerContract)
del.SetHandler(cp.handleDelete)
handlers = append(handlers, del)
return handlers return handlers
} }