From 5f7d70c59c2d2a43d58a426068a0d3600862db56 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Wed, 22 Mar 2023 17:48:05 +0300 Subject: [PATCH] [#161] adm: Refactor deploy command Resolve funlen linter for deployContractCmd function Signed-off-by: Dmitrii Stepanov --- .../internal/modules/morph/deploy.go | 127 +++++++++--------- 1 file changed, 67 insertions(+), 60 deletions(-) diff --git a/cmd/frostfs-adm/internal/modules/morph/deploy.go b/cmd/frostfs-adm/internal/modules/morph/deploy.go index 97e216f35..a9098891c 100644 --- a/cmd/frostfs-adm/internal/modules/morph/deploy.go +++ b/cmd/frostfs-adm/internal/modules/morph/deploy.go @@ -57,7 +57,6 @@ func init() { ff.String(customZoneFlag, "frostfs", "Custom zone for NNS") } -// nolint: funlen func deployContractCmd(cmd *cobra.Command, args []string) error { v := viper.GetViper() c, err := newInitializeContext(cmd, v) @@ -101,80 +100,88 @@ func deployContractCmd(cmd *cobra.Command, args []string) error { cs.Manifest.Name) } - w := io.NewBufBinWriter() - if err := emitDeploymentArguments(w.BinWriter, args); err != nil { + writer := io.NewBufBinWriter() + if err := emitDeploymentArguments(writer.BinWriter, args); err != nil { return err } - emit.Bytes(w.BinWriter, cs.RawManifest) - emit.Bytes(w.BinWriter, cs.RawNEF) - emit.Int(w.BinWriter, 3) - emit.Opcodes(w.BinWriter, opcode.PACK) - emit.AppCallNoArgs(w.BinWriter, callHash, method, callflag.All) - emit.Opcodes(w.BinWriter, opcode.DROP) // contract state on stack + emit.Bytes(writer.BinWriter, cs.RawManifest) + emit.Bytes(writer.BinWriter, cs.RawNEF) + emit.Int(writer.BinWriter, 3) + emit.Opcodes(writer.BinWriter, opcode.PACK) + emit.AppCallNoArgs(writer.BinWriter, callHash, method, callflag.All) + emit.Opcodes(writer.BinWriter, opcode.DROP) // contract state on stack if !isUpdate { - bw := io.NewBufBinWriter() - emit.Instruction(bw.BinWriter, opcode.INITSSLOT, []byte{1}) - emit.AppCall(bw.BinWriter, nnsCs.Hash, "getPrice", callflag.All) - emit.Opcodes(bw.BinWriter, opcode.STSFLD0) - emit.AppCall(bw.BinWriter, nnsCs.Hash, "setPrice", callflag.All, 1) - - start := bw.Len() - needRecord := false - - ok, err := c.nnsRootRegistered(nnsCs.Hash, zone) + err := registerNNS(nnsCs, c, zone, domain, cs, writer) if err != nil { return err - } else if !ok { - needRecord = true - - emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All, - zone, c.CommitteeAcc.Contract.ScriptHash(), - "ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600)) - emit.Opcodes(bw.BinWriter, opcode.ASSERT) - - emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All, - domain, c.CommitteeAcc.Contract.ScriptHash(), - "ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600)) - emit.Opcodes(bw.BinWriter, opcode.ASSERT) - } else { - s, ok, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain) - if err != nil { - return err - } - needRecord = !ok - if len(s) != 0 { - bw.WriteBytes(s) - } - } - if needRecord { - emit.AppCall(bw.BinWriter, nnsCs.Hash, "deleteRecords", callflag.All, domain, int64(nns.TXT)) - emit.AppCall(bw.BinWriter, nnsCs.Hash, "addRecord", callflag.All, - domain, int64(nns.TXT), address.Uint160ToString(cs.Hash)) - } - - if bw.Err != nil { - panic(fmt.Errorf("BUG: can't create deployment script: %w", w.Err)) - } else if bw.Len() != start { - w.WriteBytes(bw.Bytes()) - emit.Opcodes(w.BinWriter, opcode.LDSFLD0, opcode.PUSH1, opcode.PACK) - emit.AppCallNoArgs(w.BinWriter, nnsCs.Hash, "setPrice", callflag.All) - - if needRecord { - c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE()) - } } } - if w.Err != nil { - panic(fmt.Errorf("BUG: can't create deployment script: %w", w.Err)) + if writer.Err != nil { + panic(fmt.Errorf("BUG: can't create deployment script: %w", writer.Err)) } - if err := c.sendCommitteeTx(w.Bytes(), false); err != nil { + if err := c.sendCommitteeTx(writer.Bytes(), false); err != nil { return err } return c.awaitTx() } +func registerNNS(nnsCs *state.Contract, c *initializeContext, zone string, domain string, cs *contractState, writer *io.BufBinWriter) error { + bw := io.NewBufBinWriter() + emit.Instruction(bw.BinWriter, opcode.INITSSLOT, []byte{1}) + emit.AppCall(bw.BinWriter, nnsCs.Hash, "getPrice", callflag.All) + emit.Opcodes(bw.BinWriter, opcode.STSFLD0) + emit.AppCall(bw.BinWriter, nnsCs.Hash, "setPrice", callflag.All, 1) + + start := bw.Len() + needRecord := false + + ok, err := c.nnsRootRegistered(nnsCs.Hash, zone) + if err != nil { + return err + } else if !ok { + needRecord = true + + emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All, + zone, c.CommitteeAcc.Contract.ScriptHash(), + "ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600)) + emit.Opcodes(bw.BinWriter, opcode.ASSERT) + + emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All, + domain, c.CommitteeAcc.Contract.ScriptHash(), + "ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600)) + emit.Opcodes(bw.BinWriter, opcode.ASSERT) + } else { + s, ok, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain) + if err != nil { + return err + } + needRecord = !ok + if len(s) != 0 { + bw.WriteBytes(s) + } + } + if needRecord { + emit.AppCall(bw.BinWriter, nnsCs.Hash, "deleteRecords", callflag.All, domain, int64(nns.TXT)) + emit.AppCall(bw.BinWriter, nnsCs.Hash, "addRecord", callflag.All, + domain, int64(nns.TXT), address.Uint160ToString(cs.Hash)) + } + + if bw.Err != nil { + panic(fmt.Errorf("BUG: can't create deployment script: %w", writer.Err)) + } else if bw.Len() != start { + writer.WriteBytes(bw.Bytes()) + emit.Opcodes(writer.BinWriter, opcode.LDSFLD0, opcode.PUSH1, opcode.PACK) + emit.AppCallNoArgs(writer.BinWriter, nnsCs.Hash, "setPrice", callflag.All) + + if needRecord { + c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE()) + } + } + return nil +} + func emitDeploymentArguments(w *io.BinWriter, args []string) error { _, ps, err := cmdargs.ParseParams(args, true) if err != nil {