[#7] Add container delete notification handler

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
remotes/KirillovDenis/release/v0.21.1
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
Signature []byte
}
// ContainerParams for container put invocation.
RemoveContainerParams struct {
ContainerID []byte
Signature []byte
}
)
const (
putContainerMethod = "put"
putContainerMethod = "put"
deleteContainerMethod = "delete"
)
// RegisterContainer invokes Put method.
@ -31,3 +38,15 @@ func RegisterContainer(cli *client.Client, con util.Uint160, p *ContainerParams)
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
// stage. Therefore client has to provide some extra SysFee to operate at
// 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"
chequeMethod = "cheque"

View File

@ -26,3 +26,19 @@ func (cp *Processor) handlePut(ev event.Event) {
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))
}
}
// 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

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