[#979] adm: Deploy missing contracts during updating

Make `nnsResolveHash` function to return declared error on `token not
found` fault exception. Catch this error in `deployContracts` method,
and switch to deployment if updating contract is missing.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-11-29 13:15:03 +03:00 committed by LeL
parent e8f8e58e90
commit 2f2ca2e0bd
3 changed files with 25 additions and 6 deletions

View file

@ -4,6 +4,7 @@ import (
"archive/tar"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
@ -83,7 +84,10 @@ type contractState struct {
Hash util.Uint160
}
const updateMethodName = "update"
const (
updateMethodName = "update"
deployMethodName = "deploy"
)
func (c *initializeContext) deployNNS(method string) error {
cs := c.getContract(nnsContract)
@ -214,10 +218,18 @@ func (c *initializeContext) deployContracts(method string) error {
cs := c.Contracts[ctrName]
ctrHash := cs.Hash
methodCur := method // prevent overriding in if-block
if method == updateMethodName {
ctrHash, err = nnsResolveHash(c.Client, nnsHash, ctrName+".neofs")
if err != nil {
return fmt.Errorf("can't resolve hash for contract update: %w", err)
if errors.Is(err, errMissingNNSRecord) {
// if contract not found we deploy it instead of update
methodCur = deployMethodName
} else {
return fmt.Errorf("can't resolve hash for contract update: %w", err)
}
}
}
if c.isUpdated(ctrHash, cs) {
@ -226,7 +238,7 @@ func (c *initializeContext) deployContracts(method string) error {
}
invokeHash := mgmtHash
if method == updateMethodName {
if methodCur == updateMethodName {
invokeHash = ctrHash
}
@ -237,7 +249,7 @@ func (c *initializeContext) deployContracts(method string) error {
Scopes: transaction.Global,
}
res, err := c.Client.InvokeFunction(invokeHash, method, params, []transaction.Signer{signer})
res, err := c.Client.InvokeFunction(invokeHash, methodCur, params, []transaction.Signer{signer})
if err != nil {
return fmt.Errorf("can't deploy %s contract: %w", ctrName, err)
}