2021-07-22 09:52:44 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
2021-11-29 12:58:45 +00:00
|
|
|
"encoding/hex"
|
2021-10-07 12:12:15 +00:00
|
|
|
"errors"
|
2021-07-22 09:52:44 +00:00
|
|
|
"fmt"
|
2021-07-28 14:36:15 +00:00
|
|
|
"strconv"
|
2021-07-22 09:52:44 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"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
|
|
|
morphClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
2021-07-22 09:52:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-11-29 12:58:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-09-01 10:48:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2021-07-22 09:52:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2023-10-24 10:06:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2021-07-22 09:52:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
)
|
|
|
|
|
2024-02-01 12:21:51 +00:00
|
|
|
func setNNS(c *morphUtil.InitializeContext) error {
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(c.ReadOnlyInvoker)
|
|
|
|
nnsCs, err := r.GetContractByID(1)
|
2021-11-24 07:21:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-22 09:52:44 +00:00
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
ok, err := c.NNSRootRegistered(nnsCs.Hash, "frostfs")
|
2021-07-22 09:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
bw := io.NewBufBinWriter()
|
2021-11-24 07:21:24 +00:00
|
|
|
emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All,
|
2022-12-23 17:35:35 +00:00
|
|
|
"frostfs", c.CommitteeAcc.Contract.ScriptHash(),
|
2024-02-01 12:21:51 +00:00
|
|
|
morphUtil.FrostfsOpsEmail, int64(3600), int64(600), int64(morphUtil.DefaultExpirationTime), int64(3600))
|
2021-11-24 07:21:24 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
2024-02-01 11:30:59 +00:00
|
|
|
if err := c.SendCommitteeTx(bw.Bytes(), true); err != nil {
|
2021-07-22 09:52:44 +00:00
|
|
|
return fmt.Errorf("can't add domain root to NNS: %w", err)
|
|
|
|
}
|
2024-02-01 11:30:59 +00:00
|
|
|
if err := c.AwaitTx(); err != nil {
|
2021-07-22 09:52:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
alphaCs := c.GetContract(morphUtil.AlphabetContract)
|
2021-07-30 11:26:44 +00:00
|
|
|
for i, acc := range c.Accounts {
|
2021-07-28 14:36:15 +00:00
|
|
|
alphaCs.Hash = state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name)
|
|
|
|
|
|
|
|
domain := getAlphabetNNSDomain(i)
|
2024-02-01 11:30:59 +00:00
|
|
|
if err := nnsRegisterDomain(c, nnsCs.Hash, alphaCs.Hash, domain); err != nil {
|
2021-07-28 14:36:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-10 08:38:48 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, alphaCs.Hash.StringLE())
|
2021-07-28 14:36:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 12:21:51 +00:00
|
|
|
for _, ctrName := range morphUtil.ContractList {
|
2024-02-01 11:30:59 +00:00
|
|
|
cs := c.GetContract(ctrName)
|
2021-07-22 09:52:44 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
domain := ctrName + ".frostfs"
|
2024-02-01 11:30:59 +00:00
|
|
|
if err := nnsRegisterDomain(c, nnsCs.Hash, cs.Hash, domain); err != nil {
|
2021-07-22 09:52:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-10 08:38:48 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
2021-07-22 09:52:44 +00:00
|
|
|
|
2022-02-02 14:19:27 +00:00
|
|
|
groupKey := c.ContractWallet.Accounts[0].PrivateKey().PublicKey()
|
2024-02-01 11:30:59 +00:00
|
|
|
err = updateNNSGroup(c, nnsCs.Hash, groupKey)
|
2021-11-29 12:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-29 11:15:05 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", morphClient.NNSGroupKeyName, hex.EncodeToString(groupKey.Bytes()))
|
2021-11-29 12:58:45 +00:00
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
return c.AwaitTx()
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 12:21:51 +00:00
|
|
|
func updateNNSGroup(c *morphUtil.InitializeContext, nnsHash util.Uint160, pub *keys.PublicKey) error {
|
2021-11-29 12:58:45 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
2024-02-01 11:30:59 +00:00
|
|
|
keyAlreadyAdded, domainRegCodeEmitted, err := c.EmitUpdateNNSGroupScript(bw, nnsHash, pub)
|
2023-03-10 07:44:56 +00:00
|
|
|
if keyAlreadyAdded || err != nil {
|
2021-11-29 12:58:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-09-02 08:16:59 +00:00
|
|
|
|
|
|
|
script := bw.Bytes()
|
2023-03-10 07:44:56 +00:00
|
|
|
if domainRegCodeEmitted {
|
2022-09-02 08:16:59 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
|
|
|
|
wrapRegisterScriptWithPrice(w, nnsHash, script)
|
|
|
|
script = w.Bytes()
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
return c.SendCommitteeTx(script, true)
|
2021-11-29 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
func getAlphabetNNSDomain(i int) string {
|
2024-02-01 07:06:05 +00:00
|
|
|
return morphUtil.AlphabetContract + strconv.FormatUint(uint64(i), 10) + ".frostfs"
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 08:16:59 +00:00
|
|
|
// wrapRegisterScriptWithPrice wraps a given script with `getPrice`/`setPrice` calls for NNS.
|
|
|
|
// It is intended to be used for a single transaction, and not as a part of other scripts.
|
|
|
|
// It is assumed that script already contains static slot initialization code, the first one
|
|
|
|
// (with index 0) is used to store the price.
|
|
|
|
func wrapRegisterScriptWithPrice(w *io.BufBinWriter, nnsHash util.Uint160, s []byte) {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "getPrice", callflag.All)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.STSFLD0)
|
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "setPrice", callflag.All, 1)
|
|
|
|
|
|
|
|
w.WriteBytes(s)
|
|
|
|
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.LDSFLD0, opcode.PUSH1, opcode.PACK)
|
|
|
|
emit.AppCallNoArgs(w.BinWriter, nnsHash, "setPrice", callflag.All)
|
|
|
|
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(fmt.Errorf("BUG: can't wrap register script: %w", w.Err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 12:21:51 +00:00
|
|
|
func nnsRegisterDomain(c *morphUtil.InitializeContext, nnsHash, expectedHash util.Uint160, domain string) error {
|
2024-02-01 11:30:59 +00:00
|
|
|
script, ok, err := c.NNSRegisterDomainScript(nnsHash, expectedHash, domain)
|
2022-09-02 07:59:09 +00:00
|
|
|
if ok || err != nil {
|
2021-12-27 10:26:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-09-02 07:59:09 +00:00
|
|
|
|
|
|
|
w := io.NewBufBinWriter()
|
2022-09-02 08:16:59 +00:00
|
|
|
emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
|
|
|
|
wrapRegisterScriptWithPrice(w, nnsHash, 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), expectedHash.StringLE())
|
2022-09-05 10:04:24 +00:00
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
|
|
|
domain, int64(nns.TXT), address.Uint160ToString(expectedHash))
|
2024-02-01 11:30:59 +00:00
|
|
|
return c.SendCommitteeTx(w.Bytes(), true)
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:15:03 +00:00
|
|
|
var errMissingNNSRecord = errors.New("missing NNS record")
|