forked from TrueCloudLab/frostfs-node
[#1748] neofs-adm: Refactor NNS price handling
Make `nnsRegisterDomainScript` simpler. Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
parent
9290a7e1fe
commit
11e6f03aa2
3 changed files with 63 additions and 51 deletions
|
@ -138,7 +138,7 @@ func deployContractCmd(cmd *cobra.Command, args []string) error {
|
||||||
emit.AppCall(bw.BinWriter, nnsCs.Hash, "addRecord", callflag.All,
|
emit.AppCall(bw.BinWriter, nnsCs.Hash, "addRecord", callflag.All,
|
||||||
domain, int64(nns.TXT), cs.Hash.StringLE())
|
domain, int64(nns.TXT), cs.Hash.StringLE())
|
||||||
} else {
|
} else {
|
||||||
s, ok, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain, false)
|
s, ok, err := c.nnsRegisterDomainScript(nnsCs.Hash, cs.Hash, domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
||||||
|
@ -159,7 +158,6 @@ func (c *initializeContext) updateContracts() error {
|
||||||
}
|
}
|
||||||
nnsHash := nnsCs.Hash
|
nnsHash := nnsCs.Hash
|
||||||
|
|
||||||
totalGasCost := int64(0)
|
|
||||||
w := io2.NewBufBinWriter()
|
w := io2.NewBufBinWriter()
|
||||||
|
|
||||||
var keysParam []interface{}
|
var keysParam []interface{}
|
||||||
|
@ -220,6 +218,12 @@ func (c *initializeContext) updateContracts() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Reset()
|
w.Reset()
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
for _, ctrName := range contractList {
|
for _, ctrName := range contractList {
|
||||||
cs := c.getContract(ctrName)
|
cs := c.getContract(ctrName)
|
||||||
|
|
||||||
|
@ -258,21 +262,17 @@ func (c *initializeContext) updateContracts() error {
|
||||||
return fmt.Errorf("can't deploy %s contract: %s", ctrName, res.FaultException)
|
return fmt.Errorf("can't deploy %s contract: %s", ctrName, res.FaultException)
|
||||||
}
|
}
|
||||||
|
|
||||||
totalGasCost += res.GasConsumed
|
|
||||||
w.WriteBytes(res.Script)
|
w.WriteBytes(res.Script)
|
||||||
|
|
||||||
if method == deployMethodName {
|
if method == deployMethodName {
|
||||||
// same actions are done in initializeContext.setNNS, can be unified
|
// same actions are done in initializeContext.setNNS, can be unified
|
||||||
domain := ctrName + ".neofs"
|
domain := ctrName + ".neofs"
|
||||||
script, ok, err := c.nnsRegisterDomainScript(nnsHash, cs.Hash, domain, true)
|
script, ok, err := c.nnsRegisterDomainScript(nnsHash, cs.Hash, domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
if script != nil {
|
w.WriteBytes(script)
|
||||||
totalGasCost += defaultRegisterSysfee + native.GASFactor
|
|
||||||
w.WriteBytes(script)
|
|
||||||
}
|
|
||||||
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
||||||
domain, int64(nns.TXT), cs.Hash.StringLE())
|
domain, int64(nns.TXT), cs.Hash.StringLE())
|
||||||
}
|
}
|
||||||
|
@ -281,14 +281,16 @@ func (c *initializeContext) updateContracts() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
groupKey := c.ContractWallet.Accounts[0].PrivateKey().PublicKey()
|
groupKey := c.ContractWallet.Accounts[0].PrivateKey().PublicKey()
|
||||||
sysFee, err := c.emitUpdateNNSGroupScript(w, nnsHash, groupKey)
|
_, _, err = c.emitUpdateNNSGroupScript(w, nnsHash, groupKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.Command.Printf("NNS: Set %s -> %s\n", morphClient.NNSGroupKeyName, hex.EncodeToString(groupKey.Bytes()))
|
c.Command.Printf("NNS: Set %s -> %s\n", morphClient.NNSGroupKeyName, hex.EncodeToString(groupKey.Bytes()))
|
||||||
|
|
||||||
totalGasCost += sysFee
|
emit.Opcodes(w.BinWriter, opcode.LDSFLD0)
|
||||||
if err := c.sendCommitteeTx(w.Bytes(), totalGasCost, false); err != nil {
|
emit.AppCallNoArgs(w.BinWriter, nnsHash, "setPrice", callflag.All)
|
||||||
|
|
||||||
|
if err := c.sendCommitteeTx(w.Bytes(), -1, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return c.awaitTx()
|
return c.awaitTx()
|
||||||
|
|
|
@ -4,12 +4,10 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
|
@ -84,49 +82,82 @@ func (c *initializeContext) setNNS() error {
|
||||||
|
|
||||||
func (c *initializeContext) updateNNSGroup(nnsHash util.Uint160, pub *keys.PublicKey) error {
|
func (c *initializeContext) updateNNSGroup(nnsHash util.Uint160, pub *keys.PublicKey) error {
|
||||||
bw := io.NewBufBinWriter()
|
bw := io.NewBufBinWriter()
|
||||||
sysFee, err := c.emitUpdateNNSGroupScript(bw, nnsHash, pub)
|
needUpdate, needRegister, err := c.emitUpdateNNSGroupScript(bw, nnsHash, pub)
|
||||||
if err != nil || sysFee == 0 {
|
if !needUpdate || err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return c.sendCommitteeTx(bw.Bytes(), sysFee, true)
|
|
||||||
|
script := bw.Bytes()
|
||||||
|
if needRegister {
|
||||||
|
w := io.NewBufBinWriter()
|
||||||
|
emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
|
||||||
|
wrapRegisterScriptWithPrice(w, nnsHash, script)
|
||||||
|
script = w.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.sendCommitteeTx(script, -1, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *initializeContext) emitUpdateNNSGroupScript(bw *io.BufBinWriter, nnsHash util.Uint160, pub *keys.PublicKey) (int64, error) {
|
// 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) {
|
||||||
isAvail, err := nnsIsAvailable(c.Client, nnsHash, morphClient.NNSGroupKeyName)
|
isAvail, err := nnsIsAvailable(c.Client, nnsHash, morphClient.NNSGroupKeyName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return false, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isAvail {
|
if !isAvail {
|
||||||
currentPub, err := nnsResolveKey(c.Client, nnsHash, morphClient.NNSGroupKeyName)
|
currentPub, err := nnsResolveKey(c.Client, nnsHash, morphClient.NNSGroupKeyName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return false, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if pub.Equal(currentPub) {
|
if pub.Equal(currentPub) {
|
||||||
return 0, nil
|
return true, false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sysFee := int64(native.GASFactor)
|
|
||||||
if isAvail {
|
if isAvail {
|
||||||
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
||||||
morphClient.NNSGroupKeyName, c.CommitteeAcc.Contract.ScriptHash(),
|
morphClient.NNSGroupKeyName, c.CommitteeAcc.Contract.ScriptHash(),
|
||||||
"ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
"ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
||||||
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
||||||
sysFee += defaultRegisterSysfee
|
|
||||||
}
|
}
|
||||||
emit.AppCall(bw.BinWriter, nnsHash, "addRecord", callflag.All,
|
emit.AppCall(bw.BinWriter, nnsHash, "addRecord", callflag.All,
|
||||||
"group.neofs", int64(nns.TXT), hex.EncodeToString(pub.Bytes()))
|
"group.neofs", int64(nns.TXT), hex.EncodeToString(pub.Bytes()))
|
||||||
|
|
||||||
return sysFee, bw.Err
|
return false, isAvail, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAlphabetNNSDomain(i int) string {
|
func getAlphabetNNSDomain(i int) string {
|
||||||
return alphabetContract + strconv.FormatUint(uint64(i), 10) + ".neofs"
|
return alphabetContract + strconv.FormatUint(uint64(i), 10) + ".neofs"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string, setPrice bool) ([]byte, bool, error) {
|
// 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) {
|
||||||
ok, err := nnsIsAvailable(c.Client, nnsHash, domain)
|
ok, err := nnsIsAvailable(c.Client, nnsHash, domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
|
@ -134,33 +165,11 @@ func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.U
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
bw := io.NewBufBinWriter()
|
bw := io.NewBufBinWriter()
|
||||||
var price *big.Int
|
|
||||||
if setPrice {
|
|
||||||
res, err := invokeFunction(c.Client, nnsHash, "getPrice", nil, nil)
|
|
||||||
if err != nil || res.State != vmstate.Halt.String() || len(res.Stack) == 0 {
|
|
||||||
return nil, false, errors.New("could not get NNS's price")
|
|
||||||
}
|
|
||||||
|
|
||||||
price, err = res.Stack[0].TryInteger()
|
|
||||||
if err != nil {
|
|
||||||
return nil, false, fmt.Errorf("unexpected `GetPrice` stack returned: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// set minimal registration price
|
|
||||||
emit.AppCall(bw.BinWriter, nnsHash, "setPrice", callflag.All, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// register domain
|
|
||||||
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
||||||
domain, c.CommitteeAcc.Contract.ScriptHash(),
|
domain, c.CommitteeAcc.Contract.ScriptHash(),
|
||||||
"ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
"ops@nspcc.ru", int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
||||||
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
||||||
|
|
||||||
if setPrice {
|
|
||||||
// set registration price back
|
|
||||||
emit.AppCall(bw.BinWriter, nnsHash, "setPrice", callflag.All, price)
|
|
||||||
}
|
|
||||||
|
|
||||||
if bw.Err != nil {
|
if bw.Err != nil {
|
||||||
panic(bw.Err)
|
panic(bw.Err)
|
||||||
}
|
}
|
||||||
|
@ -175,17 +184,18 @@ func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.U
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error {
|
func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error {
|
||||||
script, ok, err := c.nnsRegisterDomainScript(nnsHash, expectedHash, domain, true)
|
script, ok, err := c.nnsRegisterDomainScript(nnsHash, expectedHash, domain)
|
||||||
if ok || err != nil {
|
if ok || err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w := io.NewBufBinWriter()
|
w := io.NewBufBinWriter()
|
||||||
w.WriteBytes(script)
|
emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
|
||||||
|
wrapRegisterScriptWithPrice(w, nnsHash, script)
|
||||||
|
|
||||||
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
||||||
domain, int64(nns.TXT), expectedHash.StringLE())
|
domain, int64(nns.TXT), expectedHash.StringLE())
|
||||||
sysFee := int64(defaultRegisterSysfee + native.GASFactor)
|
return c.sendCommitteeTx(w.Bytes(), -1, true)
|
||||||
return c.sendCommitteeTx(w.Bytes(), sysFee, true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160, zone string) (bool, error) {
|
func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160, zone string) (bool, error) {
|
||||||
|
|
Loading…
Reference in a new issue