adm: Suport proxy accounts #883

Merged
fyrchik merged 3 commits from alexvanin/frostfs-node:feature/proxy-accounts into master 2024-09-04 19:51:05 +00:00
11 changed files with 121 additions and 15 deletions

View file

@ -96,7 +96,7 @@ func newPolicyContractInterface(cmd *cobra.Command) (*morph.ContractStorage, *ac
nnsCs, err := r.GetContractByID(1)
commonCmd.ExitOnErr(cmd, "can't get NNS contract state: %w", err)
ch, err = nnsResolveHash(inv, nnsCs.Hash, policyContract+".frostfs")
ch, err = nnsResolveHash(inv, nnsCs.Hash, domainOf(policyContract))
commonCmd.ExitOnErr(cmd, "unable to resolve policy contract hash: %w", err)
return morph.NewContractStorage(ac, ch), ac

View file

@ -63,7 +63,7 @@ func dumpBalances(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("can't get NNS contract info: %w", err)
}
nmHash, err = nnsResolveHash(inv, nnsCs.Hash, netmapContract+".frostfs")
nmHash, err = nnsResolveHash(inv, nnsCs.Hash, domainOf(netmapContract))
if err != nil {
return fmt.Errorf("can't get netmap contract hash: %w", err)
}
@ -136,7 +136,7 @@ func printStorageNodeBalances(cmd *cobra.Command, inv *invoker.Invoker, nmHash u
}
func printProxyContractBalance(cmd *cobra.Command, inv *invoker.Invoker, nnsHash util.Uint160) error {
h, err := nnsResolveHash(inv, nnsHash, proxyContract+".frostfs")
h, err := nnsResolveHash(inv, nnsHash, domainOf(proxyContract))
if err != nil {
return fmt.Errorf("can't get hash of the proxy contract: %w", err)
}

View file

@ -37,7 +37,7 @@ func dumpNetworkConfig(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("can't get NNS contract info: %w", err)
}
nmHash, err := nnsResolveHash(inv, cs.Hash, netmapContract+".frostfs")
nmHash, err := nnsResolveHash(inv, cs.Hash, domainOf(netmapContract))
if err != nil {
return fmt.Errorf("can't get netmap contract hash: %w", err)
}
@ -95,7 +95,7 @@ func setConfigCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("can't get NNS contract info: %w", err)
}
nmHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, netmapContract+".frostfs")
nmHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, domainOf(netmapContract))
if err != nil {
return fmt.Errorf("can't get netmap contract hash: %w", err)
}

View file

@ -35,7 +35,7 @@ func getContainerContractHash(cmd *cobra.Command, inv *invoker.Invoker) (util.Ui
if err != nil {
return util.Uint160{}, fmt.Errorf("can't get NNS contract state: %w", err)
}
ch, err = nnsResolveHash(inv, nnsCs.Hash, containerContract+".frostfs")
ch, err = nnsResolveHash(inv, nnsCs.Hash, domainOf(containerContract))
if err != nil {
return util.Uint160{}, err
}
@ -306,7 +306,7 @@ func fetchContainerContractHash(wCtx *initializeContext) (util.Uint160, error) {
return util.Uint160{}, fmt.Errorf("can't get NNS contract state: %w", err)
}
ch, err := nnsResolveHash(wCtx.ReadOnlyInvoker, nnsCs.Hash, containerContract+".frostfs")
ch, err := nnsResolveHash(wCtx.ReadOnlyInvoker, nnsCs.Hash, domainOf(containerContract))
if err != nil {
return util.Uint160{}, fmt.Errorf("can't fetch container contract hash: %w", err)
}

View file

@ -87,7 +87,7 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error {
for _, ctrName := range contractList {
bw.Reset()
emit.AppCall(bw.BinWriter, cs.Hash, "resolve", callflag.ReadOnly,
ctrName+".frostfs", int64(nns.TXT))
domainOf(ctrName), int64(nns.TXT))
res, err := c.InvokeScript(bw.Bytes(), nil)
if err != nil {

View file

@ -27,7 +27,7 @@ func forceNewEpochCmd(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("can't get NNS contract info: %w", err)
}
nmHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, netmapContract+".frostfs")
nmHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, domainOf(netmapContract))
if err != nil {
return fmt.Errorf("can't get netmap contract hash: %w", err)
}

View file

@ -91,6 +91,10 @@ const (
deployMethodName = "deploy"
)
func domainOf(contract string) string {
return contract + ".frostfs"
}
func (c *initializeContext) deployNNS(method string) error {
cs := c.getContract(nnsContract)
h := cs.Hash
@ -198,7 +202,7 @@ func (c *initializeContext) deployOrUpdateContracts(w *io2.BufBinWriter, nnsHash
cs := c.getContract(ctrName)
method := updateMethodName
ctrHash, err := nnsResolveHash(c.ReadOnlyInvoker, nnsHash, ctrName+".frostfs")
ctrHash, err := nnsResolveHash(c.ReadOnlyInvoker, nnsHash, domainOf(ctrName))
if err != nil {
if errors.Is(err, errMissingNNSRecord) {
// if contract not found we deploy it instead of update
@ -546,7 +550,7 @@ func (c *initializeContext) getContractDeployData(ctrName string, keysParam []an
if found {
items = append(items, h)
} else {
items = append(items, nil)
items = append(items, c.Contracts[proxyContract].Hash)
}
case netmapContract:
md := getDefaultNetmapContractConfigMap()
@ -582,7 +586,7 @@ func (c *initializeContext) getContractDeployData(ctrName string, keysParam []an
case proxyContract:
items = nil
case policyContract:
items = []any{nil}
items = append(items, c.Contracts[proxyContract].Hash)
default:
panic(fmt.Sprintf("invalid contract name: %s", ctrName))
}
@ -595,7 +599,7 @@ func (c *initializeContext) getNetConfigFromNetmapContract() ([]stackitem.Item,
if err != nil {
return nil, fmt.Errorf("NNS is not yet deployed: %w", err)
}
nmHash, err := nnsResolveHash(c.ReadOnlyInvoker, cs.Hash, netmapContract+".frostfs")
nmHash, err := nnsResolveHash(c.ReadOnlyInvoker, cs.Hash, domainOf(netmapContract))
if err != nil {
return nil, fmt.Errorf("can't get netmap contract hash: %w", err)
}

View file

@ -20,7 +20,7 @@ func listNetmapCandidatesNodes(cmd *cobra.Command, _ []string) {
cs, err := r.GetContractByID(1)
commonCmd.ExitOnErr(cmd, "can't get NNS contract info: %w", err)
nmHash, err := nnsResolveHash(inv, cs.Hash, netmapContract+".frostfs")
nmHash, err := nnsResolveHash(inv, cs.Hash, domainOf(netmapContract))
commonCmd.ExitOnErr(cmd, "can't get netmap contract hash: %w", err)
res, err := inv.Call(nmHash, "netmapCandidates")

View file

@ -0,0 +1,68 @@
package morph
import (
"fmt"
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
"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/spf13/cobra"
"github.com/spf13/viper"
)
const (
accountAddressFlag = "account"
)
func addProxyAccount(cmd *cobra.Command, _ []string) {
acc, _ := cmd.Flags().GetString(accountAddressFlag)
addr, err := address.StringToUint160(acc)
commonCmd.ExitOnErr(cmd, "invalid account: %w", err)
err = processAccount(cmd, addr, "addAccount")
commonCmd.ExitOnErr(cmd, "processing error: %w", err)
}
func removeProxyAccount(cmd *cobra.Command, _ []string) {
acc, _ := cmd.Flags().GetString(accountAddressFlag)
addr, err := address.StringToUint160(acc)
commonCmd.ExitOnErr(cmd, "invalid account: %w", err)
err = processAccount(cmd, addr, "removeAccount")
commonCmd.ExitOnErr(cmd, "processing error: %w", err)
}
func processAccount(cmd *cobra.Command, addr util.Uint160, method string) error {
wCtx, err := newInitializeContext(cmd, viper.GetViper())
if err != nil {
return fmt.Errorf("can't to initialize context: %w", err)
}
r := management.NewReader(wCtx.ReadOnlyInvoker)
cs, err := r.GetContractByID(1)
if err != nil {
return fmt.Errorf("can't get NNS contract info: %w", err)
}
proxyHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, domainOf(proxyContract))
if err != nil {
return fmt.Errorf("can't get proxy contract hash: %w", err)
}
bw := io.NewBufBinWriter()
emit.AppCall(bw.BinWriter, proxyHash, method, callflag.All, addr)
if err := wCtx.sendConsensusTx(bw.Bytes()); err != nil {
return err
}
if err = wCtx.awaitTx(); err != nil {
return err
}
cmd.Println("Proxy contract has been updated")
return nil
}

View file

@ -40,7 +40,7 @@ func removeNodesCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("can't get NNS contract info: %w", err)
}
nmHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, netmapContract+".frostfs")
nmHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, domainOf(netmapContract))
if err != nil {
return fmt.Errorf("can't get netmap contract hash: %w", err)
}

View file

@ -241,6 +241,26 @@ var (
},
Run: listNetmapCandidatesNodes,
}
proxyAddAccountCmd = &cobra.Command{
Use: "proxy-add-account",
Short: "Adds account to proxy contract",
PreRun: func(cmd *cobra.Command, _ []string) {
_ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag))
_ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag))
},
Run: addProxyAccount,
}
proxyRemoveAccountCmd = &cobra.Command{
Use: "proxy-remove-account",
Short: "Remove from proxy contract",
PreRun: func(cmd *cobra.Command, _ []string) {
_ = viper.BindPFlag(alphabetWalletsFlag, cmd.Flags().Lookup(alphabetWalletsFlag))
_ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag))
},
Run: removeProxyAccount,
}
)
func init() {
@ -267,6 +287,20 @@ func init() {
initAddRuleChainCmd()
initRemoveRuleChainCmd()
initListRuleChainsCmd()
initProxyAddAccount()
initProxyRemoveAccount()
}
func initProxyAddAccount() {
RootCmd.AddCommand(proxyAddAccountCmd)
proxyAddAccountCmd.Flags().StringP(endpointFlag, endpointFlagShort, "", endpointFlagDesc)
proxyAddAccountCmd.Flags().String(accountAddressFlag, "", "Wallet address string")
}
func initProxyRemoveAccount() {
RootCmd.AddCommand(proxyRemoveAccountCmd)
proxyRemoveAccountCmd.Flags().StringP(endpointFlag, endpointFlagShort, "", endpointFlagDesc)
proxyRemoveAccountCmd.Flags().String(accountAddressFlag, "", "Wallet address string")
}
func initNetmapCandidatesCmd() {