2021-07-21 10:09:59 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
2021-10-21 14:57:38 +00:00
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
2022-02-02 14:19:27 +00:00
|
|
|
"encoding/hex"
|
2021-07-21 10:09:59 +00:00
|
|
|
"encoding/json"
|
2021-11-29 10:15:03 +00:00
|
|
|
"errors"
|
2021-07-21 10:09:59 +00:00
|
|
|
"fmt"
|
2021-10-21 14:57:38 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2021-10-21 14:57:38 +00:00
|
|
|
"strings"
|
2021-07-21 10:09:59 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
|
2024-01-31 14:26:26 +00:00
|
|
|
morphUtil "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring"
|
|
|
|
morphClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
2023-04-11 11:59:24 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
2021-07-21 10:09:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-09-05 10:04:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2021-12-27 10:26:22 +00:00
|
|
|
io2 "github.com/nspcc-dev/neo-go/pkg/io"
|
2022-08-29 19:31:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2022-11-23 14:44:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2023-04-13 14:42:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2021-07-21 10:09:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-12-27 11:13:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2021-07-21 10:09:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-12-27 11:13:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2023-04-13 14:42:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2023-11-08 13:36:55 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-07-21 10:09:59 +00:00
|
|
|
)
|
|
|
|
|
2023-12-04 13:12:59 +00:00
|
|
|
const frostfsIDAdminConfigKey = "frostfsid.admin"
|
2023-11-08 13:36:55 +00:00
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
var (
|
|
|
|
contractList = []string{
|
2024-02-01 07:06:05 +00:00
|
|
|
morphUtil.BalanceContract,
|
|
|
|
morphUtil.ContainerContract,
|
|
|
|
morphUtil.FrostfsIDContract,
|
|
|
|
morphUtil.NetmapContract,
|
|
|
|
morphUtil.PolicyContract,
|
|
|
|
morphUtil.ProxyContract,
|
2021-10-25 13:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fullContractList = append([]string{
|
2024-02-01 07:06:05 +00:00
|
|
|
morphUtil.FrostfsContract,
|
|
|
|
morphUtil.ProcessingContract,
|
|
|
|
morphUtil.NNSContract,
|
|
|
|
morphUtil.AlphabetContract,
|
2021-10-25 13:19:56 +00:00
|
|
|
}, contractList...)
|
2023-04-13 14:42:59 +00:00
|
|
|
|
|
|
|
netmapConfigKeys = []string{
|
2023-04-11 11:59:24 +00:00
|
|
|
netmap.EpochDurationConfig,
|
|
|
|
netmap.MaxObjectSizeConfig,
|
|
|
|
netmap.ContainerFeeConfig,
|
|
|
|
netmap.ContainerAliasFeeConfig,
|
|
|
|
netmap.IrCandidateFeeConfig,
|
|
|
|
netmap.WithdrawFeeConfig,
|
|
|
|
netmap.HomomorphicHashingDisabledKey,
|
|
|
|
netmap.MaintenanceModeAllowedConfig,
|
2023-04-13 14:42:59 +00:00
|
|
|
}
|
2021-10-25 13:19:56 +00:00
|
|
|
)
|
2021-07-21 10:09:59 +00:00
|
|
|
|
|
|
|
type contractState struct {
|
|
|
|
NEF *nef.File
|
|
|
|
RawNEF []byte
|
|
|
|
Manifest *manifest.Manifest
|
|
|
|
RawManifest []byte
|
|
|
|
Hash util.Uint160
|
|
|
|
}
|
|
|
|
|
2021-11-29 10:15:03 +00:00
|
|
|
const (
|
|
|
|
updateMethodName = "update"
|
|
|
|
deployMethodName = "deploy"
|
|
|
|
)
|
2021-09-30 15:11:35 +00:00
|
|
|
|
2021-09-21 12:21:38 +00:00
|
|
|
func (c *initializeContext) deployNNS(method string) error {
|
2024-02-01 07:06:05 +00:00
|
|
|
cs := c.getContract(morphUtil.NNSContract)
|
2021-11-29 12:33:46 +00:00
|
|
|
h := cs.Hash
|
2021-07-21 10:09:59 +00:00
|
|
|
|
2022-02-02 14:19:27 +00:00
|
|
|
nnsCs, err := c.nnsContractState()
|
2023-10-24 10:06:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if nnsCs != nil {
|
2022-02-02 14:19:27 +00:00
|
|
|
if nnsCs.NEF.Checksum == cs.NEF.Checksum {
|
2022-11-08 12:34:02 +00:00
|
|
|
if method == deployMethodName {
|
|
|
|
c.Command.Println("NNS contract is already deployed.")
|
|
|
|
} else {
|
|
|
|
c.Command.Println("NNS contract is already updated.")
|
|
|
|
}
|
2021-11-29 12:33:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-02 14:19:27 +00:00
|
|
|
h = nnsCs.Hash
|
2021-11-29 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = c.addManifestGroup(h, cs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't sign manifest group: %v", err)
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 13:23:34 +00:00
|
|
|
params := getContractDeployParameters(cs, nil)
|
2021-07-21 10:09:59 +00:00
|
|
|
|
2022-11-23 14:44:03 +00:00
|
|
|
invokeHash := management.Hash
|
2021-10-07 13:32:11 +00:00
|
|
|
if method == updateMethodName {
|
2022-04-08 13:23:34 +00:00
|
|
|
invokeHash = nnsCs.Hash
|
2021-09-21 12:21:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 12:12:59 +00:00
|
|
|
tx, err := c.CommitteeAct.MakeCall(invokeHash, method, params...)
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
2024-02-01 07:06:05 +00:00
|
|
|
return fmt.Errorf("failed to create deploy tx for %s: %w", morphUtil.NNSContract, err)
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 06:58:12 +00:00
|
|
|
if err := c.multiSignAndSend(tx, morphUtil.CommitteeAccountName); err != nil {
|
2021-07-21 10:09:59 +00:00
|
|
|
return fmt.Errorf("can't send deploy transaction: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
2021-12-27 10:26:22 +00:00
|
|
|
func (c *initializeContext) updateContracts() error {
|
2024-02-01 07:06:05 +00:00
|
|
|
alphaCs := c.getContract(morphUtil.AlphabetContract)
|
2021-07-30 11:29:05 +00:00
|
|
|
|
2022-02-02 14:19:27 +00:00
|
|
|
nnsCs, err := c.nnsContractState()
|
2021-08-06 11:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nnsHash := nnsCs.Hash
|
|
|
|
|
2021-12-27 10:26:22 +00:00
|
|
|
w := io2.NewBufBinWriter()
|
|
|
|
|
2021-12-27 11:13:48 +00:00
|
|
|
// Update script size for a single-node committee is close to the maximum allowed size of 65535.
|
|
|
|
// Because of this we want to reuse alphabet contract NEF and manifest for different updates.
|
|
|
|
// The generated script is as following.
|
2022-07-27 08:06:58 +00:00
|
|
|
// 1. Initialize static slot for alphabet NEF.
|
|
|
|
// 2. Store NEF into the static slot.
|
2021-12-27 11:13:48 +00:00
|
|
|
// 3. Push parameters for each alphabet contract on stack.
|
2022-07-27 08:06:58 +00:00
|
|
|
// 4. Add contract group to the manifest.
|
|
|
|
// 5. For each alphabet contract, invoke `update` using parameters on stack and
|
|
|
|
// NEF from step 2 and manifest from step 4.
|
|
|
|
emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
|
2021-12-27 11:13:48 +00:00
|
|
|
emit.Bytes(w.BinWriter, alphaCs.RawNEF)
|
2022-07-27 08:06:58 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.STSFLD0)
|
|
|
|
|
2023-03-22 15:29:52 +00:00
|
|
|
keysParam, err := c.deployAlphabetAccounts(nnsHash, w, alphaCs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-27 10:26:22 +00:00
|
|
|
|
2023-03-22 15:29:52 +00:00
|
|
|
w.Reset()
|
2022-07-27 08:06:58 +00:00
|
|
|
|
2023-03-22 15:29:52 +00:00
|
|
|
if err = c.deployOrUpdateContracts(w, nnsHash, keysParam); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-27 08:06:58 +00:00
|
|
|
|
2023-03-22 15:29:52 +00:00
|
|
|
groupKey := c.ContractWallet.Accounts[0].PrivateKey().PublicKey()
|
|
|
|
_, _, err = c.emitUpdateNNSGroupScript(w, nnsHash, groupKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-27 11:13:48 +00:00
|
|
|
}
|
2023-03-22 15:29:52 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", morphClient.NNSGroupKeyName, hex.EncodeToString(groupKey.Bytes()))
|
|
|
|
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.LDSFLD0)
|
|
|
|
emit.Int(w.BinWriter, 1)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
|
|
|
emit.AppCallNoArgs(w.BinWriter, nnsHash, "setPrice", callflag.All)
|
2021-12-27 10:26:22 +00:00
|
|
|
|
2022-08-29 19:31:32 +00:00
|
|
|
if err := c.sendCommitteeTx(w.Bytes(), false); err != nil {
|
2023-03-22 15:29:52 +00:00
|
|
|
return err
|
2022-07-27 08:06:58 +00:00
|
|
|
}
|
2023-03-22 15:29:52 +00:00
|
|
|
return c.awaitTx()
|
|
|
|
}
|
2021-12-27 10:26:22 +00:00
|
|
|
|
2023-03-22 15:29:52 +00:00
|
|
|
func (c *initializeContext) deployOrUpdateContracts(w *io2.BufBinWriter, nnsHash util.Uint160, keysParam []any) error {
|
2022-09-02 08:16:59 +00:00
|
|
|
emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
|
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "getPrice", callflag.All)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.STSFLD0)
|
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "setPrice", callflag.All, 1)
|
|
|
|
|
2021-12-27 10:26:22 +00:00
|
|
|
for _, ctrName := range contractList {
|
|
|
|
cs := c.getContract(ctrName)
|
|
|
|
|
|
|
|
method := updateMethodName
|
2024-02-01 06:58:12 +00:00
|
|
|
ctrHash, err := morphUtil.NNSResolveHash(c.ReadOnlyInvoker, nnsHash, morphUtil.DomainOf(ctrName))
|
2021-12-27 10:26:22 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errMissingNNSRecord) {
|
|
|
|
// if contract not found we deploy it instead of update
|
|
|
|
method = deployMethodName
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("can't resolve hash for contract update: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 07:49:19 +00:00
|
|
|
err = c.addManifestGroup(ctrHash, cs)
|
2021-11-29 12:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't sign manifest group: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:44:03 +00:00
|
|
|
invokeHash := management.Hash
|
2021-11-24 06:19:39 +00:00
|
|
|
if method == updateMethodName {
|
2021-12-27 10:26:22 +00:00
|
|
|
invokeHash = ctrHash
|
|
|
|
}
|
|
|
|
|
2024-01-24 08:02:05 +00:00
|
|
|
args, err := c.getContractDeployData(ctrName, keysParam, updateMethodName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: getting update params: %v", ctrName, err)
|
|
|
|
}
|
|
|
|
params := getContractDeployParameters(cs, args)
|
2022-10-17 08:46:17 +00:00
|
|
|
res, err := c.CommitteeAct.MakeCall(invokeHash, method, params...)
|
2021-12-27 10:26:22 +00:00
|
|
|
if err != nil {
|
2022-11-08 12:34:02 +00:00
|
|
|
if method != updateMethodName || !strings.Contains(err.Error(), common.ErrAlreadyUpdated) {
|
|
|
|
return fmt.Errorf("deploy contract: %w", err)
|
|
|
|
}
|
|
|
|
c.Command.Printf("%s contract is already updated.\n", ctrName)
|
|
|
|
continue
|
2021-12-27 10:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteBytes(res.Script)
|
|
|
|
|
|
|
|
if method == deployMethodName {
|
|
|
|
// same actions are done in initializeContext.setNNS, can be unified
|
2022-12-23 17:35:35 +00:00
|
|
|
domain := ctrName + ".frostfs"
|
2022-09-02 08:16:59 +00:00
|
|
|
script, ok, err := c.nnsRegisterDomainScript(nnsHash, cs.Hash, domain)
|
2021-11-24 06:19:39 +00:00
|
|
|
if err != nil {
|
2021-12-27 10:26:22 +00:00
|
|
|
return err
|
2021-11-24 06:19:39 +00:00
|
|
|
}
|
2022-09-02 07:59:09 +00:00
|
|
|
if !ok {
|
2022-09-02 08:16:59 +00:00
|
|
|
w.WriteBytes(script)
|
2022-09-05 09:54:24 +00:00
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "deleteRecords", callflag.All, domain, int64(nns.TXT))
|
2022-09-02 07:59:09 +00:00
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
|
|
|
domain, int64(nns.TXT), cs.Hash.StringLE())
|
2022-09-05 10:04:24 +00:00
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
|
|
|
domain, int64(nns.TXT), address.Uint160ToString(cs.Hash))
|
2021-12-27 10:26:22 +00:00
|
|
|
}
|
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
|
2021-11-24 06:19:39 +00:00
|
|
|
}
|
2021-12-27 10:26:22 +00:00
|
|
|
}
|
2023-03-22 15:29:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-27 10:26:22 +00:00
|
|
|
|
2023-03-22 15:29:52 +00:00
|
|
|
func (c *initializeContext) deployAlphabetAccounts(nnsHash util.Uint160, w *io2.BufBinWriter, alphaCs *contractState) ([]any, error) {
|
|
|
|
var keysParam []any
|
2021-11-29 12:58:45 +00:00
|
|
|
|
2023-03-22 15:29:52 +00:00
|
|
|
baseGroups := alphaCs.Manifest.Groups
|
|
|
|
|
|
|
|
// alphabet contracts should be deployed by individual nodes to get different hashes.
|
|
|
|
for i, acc := range c.Accounts {
|
2024-02-01 06:43:51 +00:00
|
|
|
ctrHash, err := morphUtil.NNSResolveHash(c.ReadOnlyInvoker, nnsHash, getAlphabetNNSDomain(i))
|
2023-03-22 15:29:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't resolve hash for contract update: %w", err)
|
|
|
|
}
|
2022-09-02 08:16:59 +00:00
|
|
|
|
2023-03-22 15:29:52 +00:00
|
|
|
keysParam = append(keysParam, acc.PrivateKey().PublicKey().Bytes())
|
|
|
|
|
|
|
|
params := c.getAlphabetDeployItems(i, len(c.Wallets))
|
|
|
|
emit.Array(w.BinWriter, params...)
|
|
|
|
|
|
|
|
alphaCs.Manifest.Groups = baseGroups
|
|
|
|
err = c.addManifestGroup(ctrHash, alphaCs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't sign manifest group: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
emit.Bytes(w.BinWriter, alphaCs.RawManifest)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.LDSFLD0)
|
|
|
|
emit.Int(w.BinWriter, 3)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
|
|
|
emit.AppCallNoArgs(w.BinWriter, ctrHash, updateMethodName, callflag.All)
|
|
|
|
}
|
2022-08-29 19:31:32 +00:00
|
|
|
if err := c.sendCommitteeTx(w.Bytes(), false); err != nil {
|
2023-03-22 15:29:52 +00:00
|
|
|
if !strings.Contains(err.Error(), common.ErrAlreadyUpdated) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.Command.Println("Alphabet contracts are already updated.")
|
2021-12-27 10:26:22 +00:00
|
|
|
}
|
2023-03-22 15:29:52 +00:00
|
|
|
|
|
|
|
return keysParam, nil
|
2021-12-27 10:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) deployContracts() error {
|
2024-02-01 07:06:05 +00:00
|
|
|
alphaCs := c.getContract(morphUtil.AlphabetContract)
|
2021-12-27 10:26:22 +00:00
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
var keysParam []any
|
2021-12-27 10:26:22 +00:00
|
|
|
|
2022-06-07 14:32:58 +00:00
|
|
|
baseGroups := alphaCs.Manifest.Groups
|
|
|
|
|
2021-12-27 10:26:22 +00:00
|
|
|
// alphabet contracts should be deployed by individual nodes to get different hashes.
|
|
|
|
for i, acc := range c.Accounts {
|
|
|
|
ctrHash := state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name)
|
2021-11-24 06:19:39 +00:00
|
|
|
if c.isUpdated(ctrHash, alphaCs) {
|
2021-08-06 11:28:07 +00:00
|
|
|
c.Command.Printf("Alphabet contract #%d is already deployed.\n", i)
|
2021-07-21 10:09:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-07 14:32:58 +00:00
|
|
|
alphaCs.Manifest.Groups = baseGroups
|
|
|
|
err := c.addManifestGroup(ctrHash, alphaCs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't sign manifest group: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:47:13 +00:00
|
|
|
keysParam = append(keysParam, acc.PrivateKey().PublicKey().Bytes())
|
2022-04-08 13:23:34 +00:00
|
|
|
params := getContractDeployParameters(alphaCs, c.getAlphabetDeployItems(i, len(c.Wallets)))
|
2021-07-21 10:09:59 +00:00
|
|
|
|
2022-08-29 19:31:32 +00:00
|
|
|
act, err := actor.NewSimple(c.Client, acc)
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
2022-08-29 19:31:32 +00:00
|
|
|
return fmt.Errorf("could not create actor: %w", err)
|
2021-09-21 12:15:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 14:44:03 +00:00
|
|
|
txHash, vub, err := act.SendCall(management.Hash, deployMethodName, params...)
|
2022-08-29 19:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't deploy alphabet #%d contract: %w", i, err)
|
2021-09-21 12:19:22 +00:00
|
|
|
}
|
2022-08-29 19:31:32 +00:00
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
c.SentTxs = append(c.SentTxs, morphUtil.HashVUBPair{Hash: txHash, Vub: vub})
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ctrName := range contractList {
|
2021-11-29 10:50:49 +00:00
|
|
|
cs := c.getContract(ctrName)
|
2021-11-24 06:19:39 +00:00
|
|
|
|
|
|
|
ctrHash := cs.Hash
|
|
|
|
if c.isUpdated(ctrHash, cs) {
|
2021-08-06 11:28:07 +00:00
|
|
|
c.Command.Printf("%s contract is already deployed.\n", ctrName)
|
2021-07-21 10:09:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:33:46 +00:00
|
|
|
err := c.addManifestGroup(ctrHash, cs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't sign manifest group: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-01-24 08:02:05 +00:00
|
|
|
args, err := c.getContractDeployData(ctrName, keysParam, deployMethodName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: getting deploy params: %v", ctrName, err)
|
|
|
|
}
|
|
|
|
params := getContractDeployParameters(cs, args)
|
2022-11-23 14:44:03 +00:00
|
|
|
res, err := c.CommitteeAct.MakeCall(management.Hash, deployMethodName, params...)
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
2021-09-21 12:15:20 +00:00
|
|
|
return fmt.Errorf("can't deploy %s contract: %w", ctrName, err)
|
|
|
|
}
|
2021-07-21 10:09:59 +00:00
|
|
|
|
2022-08-29 19:31:32 +00:00
|
|
|
if err := c.sendCommitteeTx(res.Script, false); err != nil {
|
2021-07-21 10:09:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
2021-11-24 06:19:39 +00:00
|
|
|
func (c *initializeContext) isUpdated(ctrHash util.Uint160, cs *contractState) bool {
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(c.ReadOnlyInvoker)
|
|
|
|
realCs, err := r.GetContract(ctrHash)
|
|
|
|
return err == nil && realCs != nil && realCs.NEF.Checksum == cs.NEF.Checksum
|
2021-11-24 06:19:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
func (c *initializeContext) getContract(ctrName string) *contractState {
|
|
|
|
return c.Contracts[ctrName]
|
|
|
|
}
|
2021-10-21 14:57:38 +00:00
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
func (c *initializeContext) readContracts(names []string) error {
|
|
|
|
var (
|
|
|
|
fi os.FileInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if c.ContractPath != "" {
|
|
|
|
fi, err = os.Stat(c.ContractPath)
|
2021-10-21 14:57:38 +00:00
|
|
|
if err != nil {
|
2021-10-25 13:19:56 +00:00
|
|
|
return fmt.Errorf("invalid contracts path: %w", err)
|
2021-10-21 14:57:38 +00:00
|
|
|
}
|
2021-10-25 13:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.ContractPath != "" && fi.IsDir() {
|
|
|
|
for _, ctrName := range names {
|
2022-07-27 14:57:30 +00:00
|
|
|
cs, err := readContract(filepath.Join(c.ContractPath, ctrName), ctrName)
|
2021-10-25 13:19:56 +00:00
|
|
|
if err != nil {
|
2022-07-27 14:57:30 +00:00
|
|
|
return err
|
2021-10-25 13:19:56 +00:00
|
|
|
}
|
|
|
|
c.Contracts[ctrName] = cs
|
2021-10-21 14:57:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-25 13:19:56 +00:00
|
|
|
var r io.ReadCloser
|
2024-02-03 17:17:12 +00:00
|
|
|
if c.ContractPath != "" {
|
|
|
|
r, err = os.Open(c.ContractPath)
|
|
|
|
} else if c.ContractURL != "" {
|
|
|
|
r, err = downloadContracts(c.Command, c.ContractURL)
|
|
|
|
} else {
|
|
|
|
r, err = downloadContractsFromRepository(c.Command)
|
2021-10-25 13:19:56 +00:00
|
|
|
}
|
2024-02-03 17:17:12 +00:00
|
|
|
|
2021-10-21 14:57:38 +00:00
|
|
|
if err != nil {
|
2021-10-25 13:19:56 +00:00
|
|
|
return fmt.Errorf("can't open contracts archive: %w", err)
|
2021-10-21 14:57:38 +00:00
|
|
|
}
|
2021-10-25 13:19:56 +00:00
|
|
|
defer r.Close()
|
2021-10-21 14:57:38 +00:00
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
m, err := readContractsFromArchive(r, names)
|
2021-10-21 14:57:38 +00:00
|
|
|
if err != nil {
|
2021-10-25 13:19:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
2022-07-27 14:57:30 +00:00
|
|
|
if err := m[name].parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-25 13:19:56 +00:00
|
|
|
c.Contracts[name] = m[name]
|
2021-10-21 14:57:38 +00:00
|
|
|
}
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
2021-10-21 14:57:38 +00:00
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
for _, ctrName := range names {
|
2024-02-01 07:06:05 +00:00
|
|
|
if ctrName != morphUtil.AlphabetContract {
|
2022-07-27 14:57:30 +00:00
|
|
|
cs := c.Contracts[ctrName]
|
2021-10-25 13:19:56 +00:00
|
|
|
cs.Hash = state.CreateContractHash(c.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
cs.NEF.Checksum, cs.Manifest.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-27 14:57:30 +00:00
|
|
|
func readContract(ctrPath, ctrName string) (*contractState, error) {
|
|
|
|
rawNef, err := os.ReadFile(filepath.Join(ctrPath, ctrName+"_contract.nef"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err)
|
|
|
|
}
|
|
|
|
rawManif, err := os.ReadFile(filepath.Join(ctrPath, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cs := &contractState{
|
|
|
|
RawNEF: rawNef,
|
|
|
|
RawManifest: rawManif,
|
|
|
|
}
|
|
|
|
|
|
|
|
return cs, cs.parse()
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
func (cs *contractState) parse() error {
|
2021-10-21 14:57:38 +00:00
|
|
|
nf, err := nef.FileFromBytes(cs.RawNEF)
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
2021-10-25 13:19:56 +00:00
|
|
|
return fmt.Errorf("can't parse NEF file: %w", err)
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
2021-10-21 14:57:38 +00:00
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
m := new(manifest.Manifest)
|
|
|
|
if err := json.Unmarshal(cs.RawManifest, m); err != nil {
|
|
|
|
return fmt.Errorf("can't parse manifest file: %w", err)
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
cs.NEF = &nf
|
|
|
|
cs.Manifest = m
|
|
|
|
return nil
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 14:57:38 +00:00
|
|
|
func readContractsFromArchive(file io.Reader, names []string) (map[string]*contractState, error) {
|
|
|
|
m := make(map[string]*contractState, len(names))
|
|
|
|
for i := range names {
|
|
|
|
m[names[i]] = new(contractState)
|
|
|
|
}
|
|
|
|
|
|
|
|
gr, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("contracts file must be tar.gz archive: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := tar.NewReader(gr)
|
|
|
|
for h, err := r.Next(); ; h, err = r.Next() {
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
dir, _ := filepath.Split(h.Name)
|
|
|
|
ctrName := filepath.Base(dir)
|
2021-10-21 14:57:38 +00:00
|
|
|
|
|
|
|
cs, ok := m[ctrName]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2022-02-02 13:28:08 +00:00
|
|
|
case strings.HasSuffix(h.Name, filepath.Join(ctrName, ctrName+"_contract.nef")):
|
2022-03-18 11:12:58 +00:00
|
|
|
cs.RawNEF, err = io.ReadAll(r)
|
2021-10-21 14:57:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err)
|
|
|
|
}
|
|
|
|
case strings.HasSuffix(h.Name, "config.json"):
|
2022-03-18 11:12:58 +00:00
|
|
|
cs.RawManifest, err = io.ReadAll(r)
|
2021-10-21 14:57:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m[ctrName] = cs
|
|
|
|
}
|
|
|
|
|
|
|
|
for ctrName, cs := range m {
|
|
|
|
if cs.RawNEF == nil {
|
|
|
|
return nil, fmt.Errorf("NEF for %s contract wasn't found", ctrName)
|
|
|
|
}
|
|
|
|
if cs.RawManifest == nil {
|
|
|
|
return nil, fmt.Errorf("manifest for %s contract wasn't found", ctrName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func getContractDeployParameters(cs *contractState, deployData []any) []any {
|
|
|
|
return []any{cs.RawNEF, cs.RawManifest, deployData}
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 08:02:05 +00:00
|
|
|
func (c *initializeContext) getContractDeployData(ctrName string, keysParam []any, method string) ([]any, error) {
|
2023-11-07 12:19:55 +00:00
|
|
|
items := make([]any, 0, 6)
|
2021-07-21 10:09:59 +00:00
|
|
|
|
|
|
|
switch ctrName {
|
2024-02-01 07:06:05 +00:00
|
|
|
case morphUtil.FrostfsContract:
|
2021-07-21 10:09:59 +00:00
|
|
|
items = append(items,
|
2024-02-01 07:06:05 +00:00
|
|
|
c.Contracts[morphUtil.ProcessingContract].Hash,
|
2022-04-07 12:47:13 +00:00
|
|
|
keysParam,
|
|
|
|
smartcontract.Parameter{})
|
2024-02-01 07:06:05 +00:00
|
|
|
case morphUtil.ProcessingContract:
|
|
|
|
items = append(items, c.Contracts[morphUtil.FrostfsContract].Hash)
|
2024-01-24 08:02:05 +00:00
|
|
|
return items[1:], nil // no notary info
|
2024-02-01 07:06:05 +00:00
|
|
|
case morphUtil.BalanceContract:
|
2021-07-21 10:09:59 +00:00
|
|
|
items = append(items,
|
2024-02-01 07:06:05 +00:00
|
|
|
c.Contracts[morphUtil.NetmapContract].Hash,
|
|
|
|
c.Contracts[morphUtil.ContainerContract].Hash)
|
|
|
|
case morphUtil.ContainerContract:
|
2021-09-30 14:42:16 +00:00
|
|
|
// In case if NNS is updated multiple times, we can't calculate
|
|
|
|
// it's actual hash based on local data, thus query chain.
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(c.ReadOnlyInvoker)
|
|
|
|
nnsCs, err := r.GetContractByID(1)
|
2021-09-30 14:42:16 +00:00
|
|
|
if err != nil {
|
2024-01-24 08:04:37 +00:00
|
|
|
return nil, fmt.Errorf("get nns contract: %w", err)
|
2021-09-30 14:42:16 +00:00
|
|
|
}
|
2021-07-21 10:09:59 +00:00
|
|
|
items = append(items,
|
2024-02-01 07:06:05 +00:00
|
|
|
c.Contracts[morphUtil.NetmapContract].Hash,
|
|
|
|
c.Contracts[morphUtil.BalanceContract].Hash,
|
|
|
|
c.Contracts[morphUtil.FrostfsIDContract].Hash,
|
2022-04-07 12:47:13 +00:00
|
|
|
nnsCs.Hash,
|
|
|
|
"container")
|
2024-02-01 07:06:05 +00:00
|
|
|
case morphUtil.FrostfsIDContract:
|
2024-01-24 08:19:18 +00:00
|
|
|
var (
|
|
|
|
h util.Uint160
|
|
|
|
found bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if method == updateMethodName {
|
|
|
|
h, found, err = c.getFrostfsIDAdminFromContract()
|
|
|
|
}
|
|
|
|
if method != updateMethodName || err == nil && !found {
|
|
|
|
h, found, err = getFrostfsIDAdmin(viper.GetViper())
|
|
|
|
}
|
2023-11-08 13:36:55 +00:00
|
|
|
if err != nil {
|
2024-01-24 08:02:05 +00:00
|
|
|
return nil, err
|
2023-11-08 13:36:55 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 13:12:59 +00:00
|
|
|
if found {
|
|
|
|
items = append(items, h)
|
|
|
|
} else {
|
2024-02-01 07:06:05 +00:00
|
|
|
items = append(items, c.Contracts[morphUtil.ProxyContract].Hash)
|
2023-12-04 13:12:59 +00:00
|
|
|
}
|
2024-02-01 07:06:05 +00:00
|
|
|
case morphUtil.NetmapContract:
|
2023-04-13 14:42:59 +00:00
|
|
|
md := getDefaultNetmapContractConfigMap()
|
|
|
|
if method == updateMethodName {
|
2024-01-24 16:43:17 +00:00
|
|
|
if err := c.mergeNetmapConfig(md); err != nil {
|
2024-01-24 08:02:05 +00:00
|
|
|
return nil, err
|
2023-04-13 14:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var configParam []any
|
|
|
|
for k, v := range md {
|
|
|
|
configParam = append(configParam, k, v)
|
2021-07-29 10:06:25 +00:00
|
|
|
}
|
2023-04-13 14:42:59 +00:00
|
|
|
|
2021-07-21 10:09:59 +00:00
|
|
|
items = append(items,
|
2024-02-01 07:06:05 +00:00
|
|
|
c.Contracts[morphUtil.BalanceContract].Hash,
|
|
|
|
c.Contracts[morphUtil.ContainerContract].Hash,
|
2022-04-07 12:47:13 +00:00
|
|
|
keysParam,
|
|
|
|
configParam)
|
2024-02-01 07:06:05 +00:00
|
|
|
case morphUtil.ProxyContract:
|
2021-12-30 07:45:48 +00:00
|
|
|
items = nil
|
2024-02-01 07:06:05 +00:00
|
|
|
case morphUtil.PolicyContract:
|
|
|
|
items = append(items, c.Contracts[morphUtil.ProxyContract].Hash)
|
2021-07-21 10:09:59 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid contract name: %s", ctrName))
|
|
|
|
}
|
2024-01-24 08:02:05 +00:00
|
|
|
return items, nil
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 08:19:18 +00:00
|
|
|
func (c *initializeContext) getFrostfsIDAdminFromContract() (util.Uint160, bool, error) {
|
|
|
|
r := management.NewReader(c.ReadOnlyInvoker)
|
|
|
|
cs, err := r.GetContractByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, false, fmt.Errorf("get nns contract: %w", err)
|
|
|
|
}
|
2024-02-01 07:06:05 +00:00
|
|
|
fidHash, err := morphUtil.NNSResolveHash(c.ReadOnlyInvoker, cs.Hash, morphUtil.DomainOf(morphUtil.FrostfsIDContract))
|
2024-01-24 08:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, false, fmt.Errorf("resolve frostfsid contract hash: %w", err)
|
|
|
|
}
|
|
|
|
item, err := unwrap.Item(c.ReadOnlyInvoker.Call(fidHash, "getAdmin"))
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, false, fmt.Errorf("getAdmin: %w", err)
|
|
|
|
}
|
|
|
|
if _, ok := item.(stackitem.Null); ok {
|
|
|
|
return util.Uint160{}, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := item.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, true, fmt.Errorf("getAdmin: decode result: %w", err)
|
|
|
|
}
|
|
|
|
h, err := util.Uint160DecodeBytesBE(bs)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, true, fmt.Errorf("getAdmin: decode result: %w", err)
|
|
|
|
}
|
|
|
|
return h, true, nil
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:42:59 +00:00
|
|
|
func (c *initializeContext) getNetConfigFromNetmapContract() ([]stackitem.Item, error) {
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(c.ReadOnlyInvoker)
|
|
|
|
cs, err := r.GetContractByID(1)
|
2023-04-13 14:42:59 +00:00
|
|
|
if err != nil {
|
2024-01-24 08:04:37 +00:00
|
|
|
return nil, fmt.Errorf("get nns contract: %w", err)
|
2023-04-13 14:42:59 +00:00
|
|
|
}
|
2024-02-01 07:06:05 +00:00
|
|
|
nmHash, err := morphUtil.NNSResolveHash(c.ReadOnlyInvoker, cs.Hash, morphUtil.DomainOf(morphUtil.NetmapContract))
|
2023-04-13 14:42:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't get netmap contract hash: %w", err)
|
|
|
|
}
|
|
|
|
arr, err := unwrap.Array(c.ReadOnlyInvoker.Call(nmHash, "listConfig"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't fetch list of network config keys from the netmap contract")
|
|
|
|
}
|
|
|
|
return arr, err
|
|
|
|
}
|
|
|
|
|
2024-01-24 16:43:17 +00:00
|
|
|
func (c *initializeContext) mergeNetmapConfig(md map[string]any) error {
|
|
|
|
arr, err := c.getNetConfigFromNetmapContract()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m, err := parseConfigFromNetmapContract(arr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range m {
|
|
|
|
for _, key := range netmapConfigKeys {
|
|
|
|
if k == key {
|
|
|
|
md[k] = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func (c *initializeContext) getAlphabetDeployItems(i, n int) []any {
|
2023-11-07 12:19:55 +00:00
|
|
|
items := make([]any, 5)
|
2024-02-01 07:06:05 +00:00
|
|
|
items[0] = c.Contracts[morphUtil.NetmapContract].Hash
|
|
|
|
items[1] = c.Contracts[morphUtil.ProxyContract].Hash
|
2023-11-07 12:19:55 +00:00
|
|
|
items[2] = innerring.GlagoliticLetter(i).String()
|
|
|
|
items[3] = int64(i)
|
|
|
|
items[4] = int64(n)
|
2021-12-27 11:13:48 +00:00
|
|
|
return items
|
|
|
|
}
|