[#1633] neofs-adm: Allow to update arbitrary contracts

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
Evgenii Stratonikov 2022-07-28 11:46:03 +03:00 committed by fyrchik
parent fc06b6e89a
commit dd30703a6b

View file

@ -17,6 +17,7 @@ import (
const ( const (
contractPathFlag = "contract" contractPathFlag = "contract"
updateFlag = "update"
) )
var deployCmd = &cobra.Command{ var deployCmd = &cobra.Command{
@ -40,6 +41,8 @@ func init() {
ff.StringP(endpointFlag, "r", "", "N3 RPC node endpoint") ff.StringP(endpointFlag, "r", "", "N3 RPC node endpoint")
ff.String(contractPathFlag, "", "Path to the contract directory") ff.String(contractPathFlag, "", "Path to the contract directory")
_ = deployCmd.MarkFlagFilename(contractPathFlag) _ = deployCmd.MarkFlagFilename(contractPathFlag)
ff.Bool(updateFlag, false, "Update an existing contract")
} }
func deployContractCmd(cmd *cobra.Command, _ []string) error { func deployContractCmd(cmd *cobra.Command, _ []string) error {
@ -61,10 +64,28 @@ func deployContractCmd(cmd *cobra.Command, _ []string) error {
return err return err
} }
nnsCs, err := c.Client.GetContractStateByID(1)
if err != nil {
return fmt.Errorf("can't fetch NNS contract state: %w", err)
}
callHash := c.nativeHash(nativenames.Management)
method := deployMethodName
domain := ctrName + ".neofs"
isUpdate, _ := cmd.Flags().GetBool(updateFlag)
if isUpdate {
cs.Hash, err = nnsResolveHash(c.Client, nnsCs.Hash, domain)
if err != nil {
return fmt.Errorf("can't fetch contract hash from NNS: %w", err)
}
callHash = cs.Hash
method = updateMethodName
} else {
cs.Hash = state.CreateContractHash( cs.Hash = state.CreateContractHash(
c.CommitteeAcc.Contract.ScriptHash(), c.CommitteeAcc.Contract.ScriptHash(),
cs.NEF.Checksum, cs.NEF.Checksum,
cs.Manifest.Name) cs.Manifest.Name)
}
err = c.addManifestGroup(cs.Hash, cs) err = c.addManifestGroup(cs.Hash, cs)
if err != nil { if err != nil {
@ -72,28 +93,25 @@ func deployContractCmd(cmd *cobra.Command, _ []string) error {
} }
params := getContractDeployParameters(cs, nil) params := getContractDeployParameters(cs, nil)
callHash := c.nativeHash(nativenames.Management)
nnsCs, err := c.Client.GetContractStateByID(1) w := io.NewBufBinWriter()
if err != nil { emit.AppCall(w.BinWriter, callHash, method, callflag.All, params...)
return fmt.Errorf("can't fetch NNS contract state: %w", err) emit.Opcodes(w.BinWriter, opcode.DROP) // contract state on stack
} if !isUpdate {
domain := ctrName + ".neofs"
s, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain) s, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain)
if err != nil { if err != nil {
return err return err
} }
if len(s) != 0 {
w := io.NewBufBinWriter() c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
emit.AppCall(w.BinWriter, callHash, deployMethodName, callflag.All, params...)
emit.Opcodes(w.BinWriter, opcode.DROP) // contract state on stack
w.WriteBytes(s) w.WriteBytes(s)
}
}
if w.Err != nil { if w.Err != nil {
panic(fmt.Errorf("BUG: can't create deployment script: %w", w.Err)) panic(fmt.Errorf("BUG: can't create deployment script: %w", w.Err))
} }
c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
if err := c.sendCommitteeTx(w.Bytes(), -1, false); err != nil { if err := c.sendCommitteeTx(w.Bytes(), -1, false); err != nil {
return err return err
} }