2024-02-02 06:54:47 +00:00
|
|
|
package contract
|
2022-07-27 14:57:30 +00:00
|
|
|
|
|
|
|
import (
|
2022-07-28 10:41:51 +00:00
|
|
|
"encoding/json"
|
2022-07-27 14:57:30 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
|
2024-02-02 12:04:48 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags"
|
2024-02-02 12:26:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants"
|
2024-02-02 12:36:14 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper"
|
2022-07-28 10:41:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/cmdargs"
|
2022-07-27 14:57:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-09-01 08:53:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2022-07-27 14:57:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-11-23 14:44:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2022-07-28 10:41:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/services/rpcsrv/params"
|
2022-07-27 14:57:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
contractPathFlag = "contract"
|
2022-07-28 08:46:03 +00:00
|
|
|
updateFlag = "update"
|
2022-07-27 14:57:30 +00:00
|
|
|
)
|
|
|
|
|
2024-02-02 06:54:47 +00:00
|
|
|
var DeployCmd = &cobra.Command{
|
2022-07-27 14:57:30 +00:00
|
|
|
Use: "deploy",
|
|
|
|
Short: "Deploy additional smart-contracts",
|
|
|
|
Long: `Deploy additional smart-contract which are not related to core.
|
2022-07-28 10:41:51 +00:00
|
|
|
All contracts are deployed by the committee, so access to the alphabet wallets is required.
|
|
|
|
Optionally, arguments can be provided to be passed to a contract's _deploy function.
|
2022-08-24 12:12:53 +00:00
|
|
|
The syntax is the same as for 'neo-go contract testinvokefunction' command.
|
|
|
|
Compiled contract file name must contain '_contract.nef' suffix.
|
|
|
|
Contract's manifest file name must be 'config.json'.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNS name is taken by stripping '_contract.nef' from the NEF file (similar to frostfs contracts).`,
|
2022-07-27 14:57:30 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2024-02-02 12:04:48 +00:00
|
|
|
_ = viper.BindPFlag(commonflags.AlphabetWalletsFlag, cmd.Flags().Lookup(commonflags.AlphabetWalletsFlag))
|
|
|
|
_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
|
2022-07-27 14:57:30 +00:00
|
|
|
},
|
|
|
|
RunE: deployContractCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2024-02-02 06:54:47 +00:00
|
|
|
ff := DeployCmd.Flags()
|
2022-07-27 14:57:30 +00:00
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
ff.String(commonflags.AlphabetWalletsFlag, "", commonflags.AlphabetWalletsFlagDesc)
|
|
|
|
_ = DeployCmd.MarkFlagFilename(commonflags.AlphabetWalletsFlag)
|
2022-07-27 14:57:30 +00:00
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
ff.StringP(commonflags.EndpointFlag, "r", "", commonflags.EndpointFlagDesc)
|
2022-07-27 14:57:30 +00:00
|
|
|
ff.String(contractPathFlag, "", "Path to the contract directory")
|
2024-02-02 06:54:47 +00:00
|
|
|
_ = DeployCmd.MarkFlagFilename(contractPathFlag)
|
2022-07-28 08:46:03 +00:00
|
|
|
|
|
|
|
ff.Bool(updateFlag, false, "Update an existing contract")
|
2024-02-02 12:04:48 +00:00
|
|
|
ff.String(commonflags.CustomZoneFlag, "frostfs", "Custom zone for NNS")
|
2022-07-27 14:57:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 10:41:51 +00:00
|
|
|
func deployContractCmd(cmd *cobra.Command, args []string) error {
|
2022-07-27 14:57:30 +00:00
|
|
|
v := viper.GetViper()
|
2024-02-02 12:36:14 +00:00
|
|
|
c, err := helper.NewInitializeContext(cmd, v)
|
2022-07-27 14:57:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initialization error: %w", err)
|
|
|
|
}
|
2024-02-01 11:30:59 +00:00
|
|
|
defer c.Close()
|
2022-07-27 14:57:30 +00:00
|
|
|
|
|
|
|
ctrPath, _ := cmd.Flags().GetString(contractPathFlag)
|
|
|
|
ctrName, err := probeContractName(ctrPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
cs, err := helper.ReadContract(ctrPath, ctrName)
|
2022-07-27 14:57:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(c.ReadOnlyInvoker)
|
|
|
|
nnsCs, err := r.GetContractByID(1)
|
2022-07-27 14:57:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch NNS contract state: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:44:03 +00:00
|
|
|
callHash := management.Hash
|
2024-02-02 12:26:57 +00:00
|
|
|
method := constants.DeployMethodName
|
2024-02-02 12:04:48 +00:00
|
|
|
zone, _ := cmd.Flags().GetString(commonflags.CustomZoneFlag)
|
2022-07-29 08:16:17 +00:00
|
|
|
domain := ctrName + "." + zone
|
2022-07-28 08:46:03 +00:00
|
|
|
isUpdate, _ := cmd.Flags().GetBool(updateFlag)
|
|
|
|
if isUpdate {
|
2024-02-02 12:36:14 +00:00
|
|
|
cs.Hash, err = helper.NNSResolveHash(c.ReadOnlyInvoker, nnsCs.Hash, domain)
|
2022-07-28 08:46:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch contract hash from NNS: %w", err)
|
|
|
|
}
|
|
|
|
callHash = cs.Hash
|
2024-02-02 12:26:57 +00:00
|
|
|
method = constants.UpdateMethodName
|
2022-07-28 08:46:03 +00:00
|
|
|
} else {
|
|
|
|
cs.Hash = state.CreateContractHash(
|
|
|
|
c.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
cs.NEF.Checksum,
|
|
|
|
cs.Manifest.Name)
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:48:05 +00:00
|
|
|
writer := io.NewBufBinWriter()
|
|
|
|
if err := emitDeploymentArguments(writer.BinWriter, args); err != nil {
|
2022-07-28 10:41:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-22 14:48:05 +00:00
|
|
|
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
|
2022-07-28 08:46:03 +00:00
|
|
|
if !isUpdate {
|
2023-03-22 14:48:05 +00:00
|
|
|
err := registerNNS(nnsCs, c, zone, domain, cs, writer)
|
2022-07-28 08:46:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:48:05 +00:00
|
|
|
if writer.Err != nil {
|
|
|
|
panic(fmt.Errorf("BUG: can't create deployment script: %w", writer.Err))
|
2022-07-27 14:57:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
if err := c.SendCommitteeTx(writer.Bytes(), false); err != nil {
|
2022-07-27 14:57:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-02-01 11:30:59 +00:00
|
|
|
return c.AwaitTx()
|
2022-07-27 14:57:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
func registerNNS(nnsCs *state.Contract, c *helper.InitializeContext, zone string, domain string, cs *helper.ContractState, writer *io.BufBinWriter) error {
|
2023-03-22 14:48:05 +00:00
|
|
|
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
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
ok, err := c.NNSRootRegistered(nnsCs.Hash, zone)
|
2023-03-22 14:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
needRecord = true
|
|
|
|
|
|
|
|
emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All,
|
|
|
|
zone, c.CommitteeAcc.Contract.ScriptHash(),
|
2024-02-02 12:26:57 +00:00
|
|
|
constants.FrostfsOpsEmail, int64(3600), int64(600), int64(constants.DefaultExpirationTime), int64(3600))
|
2023-03-22 14:48:05 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
|
|
|
|
|
|
|
emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All,
|
|
|
|
domain, c.CommitteeAcc.Contract.ScriptHash(),
|
2024-02-02 12:26:57 +00:00
|
|
|
constants.FrostfsOpsEmail, int64(3600), int64(600), int64(constants.DefaultExpirationTime), int64(3600))
|
2023-03-22 14:48:05 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
|
|
|
} else {
|
2024-02-01 11:30:59 +00:00
|
|
|
s, ok, err := c.NNSRegisterDomainScript(nnsCs.Hash, cs.Hash, domain)
|
2023-03-22 14:48:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-28 10:41:51 +00:00
|
|
|
func emitDeploymentArguments(w *io.BinWriter, args []string) error {
|
|
|
|
_, ps, err := cmdargs.ParseParams(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ps) == 0 {
|
|
|
|
emit.Opcodes(w, opcode.NEWARRAY0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ps) != 1 {
|
|
|
|
return fmt.Errorf("at most one argument is expected for deploy, got %d", len(ps))
|
|
|
|
}
|
|
|
|
|
|
|
|
// We could emit this directly, but round-trip through JSON is more robust.
|
|
|
|
// This a CLI, so optimizing the conversion is not worth the effort.
|
|
|
|
data, err := json.Marshal(ps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pp params.Params
|
|
|
|
if err := json.Unmarshal(data, &pp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return params.ExpandArrayIntoScript(w, pp)
|
|
|
|
}
|
|
|
|
|
2022-07-27 14:57:30 +00:00
|
|
|
func probeContractName(ctrPath string) (string, error) {
|
|
|
|
ds, err := os.ReadDir(ctrPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("can't read directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctrName string
|
|
|
|
for i := range ds {
|
|
|
|
if strings.HasSuffix(ds[i].Name(), "_contract.nef") {
|
|
|
|
ctrName = strings.TrimSuffix(ds[i].Name(), "_contract.nef")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctrName == "" {
|
|
|
|
return "", fmt.Errorf("can't find any NEF files in %s", ctrPath)
|
|
|
|
}
|
|
|
|
return ctrName, nil
|
|
|
|
}
|