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"
|
2022-08-24 11:04:05 +00:00
|
|
|
"time"
|
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"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2023-10-24 10:06:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2023-04-12 10:58:54 +00:00
|
|
|
nnsClient "github.com/nspcc-dev/neo-go/pkg/rpcclient/nns"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
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"
|
2021-10-07 12:12:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-18 08:33:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2021-07-22 09:52:44 +00:00
|
|
|
)
|
|
|
|
|
2022-08-24 11:04:05 +00:00
|
|
|
const defaultExpirationTime = 10 * 365 * 24 * time.Hour / time.Second
|
2021-07-22 09:52:44 +00:00
|
|
|
|
2023-08-29 10:41:57 +00:00
|
|
|
const frostfsOpsEmail = "ops@frostfs.info"
|
|
|
|
|
2021-07-22 09:52:44 +00:00
|
|
|
func (c *initializeContext) setNNS() 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
|
|
|
|
2022-12-23 17:35:35 +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(),
|
2023-08-29 10:41:57 +00:00
|
|
|
frostfsOpsEmail, int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
2021-11-24 07:21:24 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
2022-08-29 19:31:32 +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)
|
|
|
|
}
|
|
|
|
if err := c.awaitTx(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
alphaCs := c.getContract(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)
|
2021-10-07 12:25:34 +00:00
|
|
|
if err := c.nnsRegisterDomain(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
|
|
|
}
|
|
|
|
|
2021-07-22 09:52:44 +00:00
|
|
|
for _, ctrName := range contractList {
|
2021-10-25 13:19:56 +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"
|
2021-10-07 12:25:34 +00:00
|
|
|
if err := c.nnsRegisterDomain(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()
|
|
|
|
err = c.updateNNSGroup(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
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:58:45 +00:00
|
|
|
func (c *initializeContext) updateNNSGroup(nnsHash util.Uint160, pub *keys.PublicKey) error {
|
|
|
|
bw := io.NewBufBinWriter()
|
2023-03-10 07:44:56 +00:00
|
|
|
keyAlreadyAdded, domainRegCodeEmitted, err := c.emitUpdateNNSGroupScript(bw, nnsHash, pub)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-08-29 19:31:32 +00:00
|
|
|
return c.sendCommitteeTx(script, true)
|
2021-11-29 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 08:16:59 +00:00
|
|
|
// emitUpdateNNSGroupScript emits script for updating group key stored in NNS.
|
|
|
|
// First return value is true iff the key is already there and nothing should be done.
|
|
|
|
// Second return value is true iff a domain registration code was emitted.
|
|
|
|
func (c *initializeContext) emitUpdateNNSGroupScript(bw *io.BufBinWriter, nnsHash util.Uint160, pub *keys.PublicKey) (bool, bool, error) {
|
2022-04-07 08:10:35 +00:00
|
|
|
isAvail, err := nnsIsAvailable(c.Client, nnsHash, morphClient.NNSGroupKeyName)
|
2021-11-29 12:58:45 +00:00
|
|
|
if err != nil {
|
2022-09-02 08:16:59 +00:00
|
|
|
return false, false, err
|
2021-11-29 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isAvail {
|
2022-09-05 12:45:38 +00:00
|
|
|
currentPub, err := nnsResolveKey(c.ReadOnlyInvoker, nnsHash, morphClient.NNSGroupKeyName)
|
2021-11-29 12:58:45 +00:00
|
|
|
if err != nil {
|
2022-09-02 08:16:59 +00:00
|
|
|
return false, false, err
|
2021-11-29 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pub.Equal(currentPub) {
|
2022-09-02 08:16:59 +00:00
|
|
|
return true, false, nil
|
2021-11-29 12:58:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isAvail {
|
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
2022-01-29 11:15:05 +00:00
|
|
|
morphClient.NNSGroupKeyName, c.CommitteeAcc.Contract.ScriptHash(),
|
2023-08-29 10:41:57 +00:00
|
|
|
frostfsOpsEmail, int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
2021-11-29 12:58:45 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
|
|
|
}
|
2022-09-05 09:54:24 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "deleteRecords", callflag.All, "group.frostfs", int64(nns.TXT))
|
2021-11-29 12:58:45 +00:00
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "addRecord", callflag.All,
|
2022-12-23 17:35:35 +00:00
|
|
|
"group.frostfs", int64(nns.TXT), hex.EncodeToString(pub.Bytes()))
|
2021-11-29 12:58:45 +00:00
|
|
|
|
2022-09-02 08:16:59 +00:00
|
|
|
return false, isAvail, nil
|
2021-11-29 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
func getAlphabetNNSDomain(i int) string {
|
2022-12-23 17:35:35 +00:00
|
|
|
return 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string) ([]byte, bool, error) {
|
2022-04-07 08:10:35 +00:00
|
|
|
ok, err := nnsIsAvailable(c.Client, nnsHash, domain)
|
2021-08-06 11:28:07 +00:00
|
|
|
if err != nil {
|
2022-09-02 07:59:09 +00:00
|
|
|
return nil, false, err
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
2022-09-02 07:59:09 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
2021-08-06 11:28:07 +00:00
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
2021-10-07 12:12:15 +00:00
|
|
|
domain, c.CommitteeAcc.Contract.ScriptHash(),
|
2023-08-29 10:41:57 +00:00
|
|
|
frostfsOpsEmail, int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
2021-07-22 09:52:44 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
2022-08-15 14:05:00 +00:00
|
|
|
|
2022-09-02 07:59:09 +00:00
|
|
|
if bw.Err != nil {
|
|
|
|
panic(bw.Err)
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
2022-09-02 07:59:09 +00:00
|
|
|
return bw.Bytes(), false, nil
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 06:43:51 +00:00
|
|
|
s, err := morphUtil.NNSResolveHash(c.ReadOnlyInvoker, nnsHash, domain)
|
2022-09-02 07:59:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
2022-09-02 07:59:09 +00:00
|
|
|
return nil, s == expectedHash, nil
|
2021-12-27 10:26:22 +00:00
|
|
|
}
|
2021-08-06 11:28:07 +00:00
|
|
|
|
2021-12-27 10:26:22 +00:00
|
|
|
func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error {
|
2022-09-02 08:16: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))
|
2022-08-29 19:31:32 +00:00
|
|
|
return c.sendCommitteeTx(w.Bytes(), true)
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 11:25:02 +00:00
|
|
|
func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160, zone string) (bool, error) {
|
2022-09-05 12:45:38 +00:00
|
|
|
res, err := c.CommitteeAct.Call(nnsHash, "isAvailable", "name."+zone)
|
2021-07-22 09:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-09-05 12:45:38 +00:00
|
|
|
|
2022-07-18 08:33:53 +00:00
|
|
|
return res.State == vmstate.Halt.String(), nil
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:15:03 +00:00
|
|
|
var errMissingNNSRecord = errors.New("missing NNS record")
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
func nnsResolveKey(inv *invoker.Invoker, nnsHash util.Uint160, domain string) (*keys.PublicKey, error) {
|
2024-02-01 06:43:51 +00:00
|
|
|
res, err := morphUtil.NNSResolve(inv, nnsHash, domain)
|
2021-11-29 13:45:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-09 12:33:16 +00:00
|
|
|
if _, ok := res.Value().(stackitem.Null); ok {
|
2021-11-29 13:45:25 +00:00
|
|
|
return nil, errors.New("NNS record is missing")
|
|
|
|
}
|
2023-03-09 12:33:16 +00:00
|
|
|
arr, ok := res.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("API of the NNS contract method `resolve` has changed")
|
2021-11-29 13:45:25 +00:00
|
|
|
}
|
2023-03-09 12:33:16 +00:00
|
|
|
for i := range arr {
|
|
|
|
var bs []byte
|
|
|
|
bs, err = arr[i].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-29 13:45:25 +00:00
|
|
|
|
2023-03-09 12:33:16 +00:00
|
|
|
return keys.NewPublicKeyFromString(string(bs))
|
|
|
|
}
|
|
|
|
return nil, errors.New("no valid keys are found")
|
2021-11-29 13:45:25 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
func nnsIsAvailable(c morphUtil.Client, nnsHash util.Uint160, name string) (bool, error) {
|
2023-04-12 10:58:54 +00:00
|
|
|
switch c.(type) {
|
2022-07-28 16:22:32 +00:00
|
|
|
case *rpcclient.Client:
|
2023-04-12 10:58:54 +00:00
|
|
|
inv := invoker.New(c, nil)
|
|
|
|
reader := nnsClient.NewReader(inv, nnsHash)
|
|
|
|
return reader.IsAvailable(name)
|
2022-04-07 08:10:35 +00:00
|
|
|
default:
|
2024-01-31 14:26:26 +00:00
|
|
|
b, err := unwrap.Bool(morphUtil.InvokeFunction(c, nnsHash, "isAvailable", []any{name}, nil))
|
2022-04-07 12:47:13 +00:00
|
|
|
if err != nil {
|
2022-09-05 12:45:38 +00:00
|
|
|
return false, fmt.Errorf("`isAvailable`: invalid response: %w", err)
|
2022-04-07 12:47:13 +00:00
|
|
|
}
|
2022-09-05 12:45:38 +00:00
|
|
|
|
2022-04-07 12:47:13 +00:00
|
|
|
return b, nil
|
2022-04-07 08:10:35 +00:00
|
|
|
}
|
|
|
|
}
|