[#722] neofs-adm: Hide N3 client behind an interface

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-04-07 11:10:35 +03:00 committed by LeL
parent 57200e18cd
commit 37f9d083fb
9 changed files with 126 additions and 46 deletions

View file

@ -13,7 +13,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
@ -57,11 +56,13 @@ func dumpBalances(cmd *cobra.Command, _ []string) error {
return err return err
} }
gasHash, err := c.GetNativeContractHash(nativenames.Gas) ns, err := getNativeHashes(c)
if err != nil { if err != nil || ns[nativenames.Gas].Equals(util.Uint160{}) {
return fmt.Errorf("can't fetch hash of the GAS contract: %w", err) return errors.New("can't fetch hash of the GAS contract")
} }
gasHash := ns[nativenames.Gas]
if !notaryEnabled || dumpStorage || dumpAlphabet || dumpProxy { if !notaryEnabled || dumpStorage || dumpAlphabet || dumpProxy {
nnsCs, err = c.GetContractStateByID(1) nnsCs, err = c.GetContractStateByID(1)
if err != nil { if err != nil {
@ -169,7 +170,7 @@ func dumpBalances(cmd *cobra.Command, _ []string) error {
return nil return nil
} }
func fetchIRNodes(c *client.Client, nmHash util.Uint160) ([]accBalancePair, error) { func fetchIRNodes(c Client, nmHash util.Uint160) ([]accBalancePair, error) {
var irList []accBalancePair var irList []accBalancePair
if notaryEnabled { if notaryEnabled {
@ -233,7 +234,7 @@ func printBalances(cmd *cobra.Command, prefix string, accounts []accBalancePair)
} }
} }
func fetchBalances(c *client.Client, gasHash util.Uint160, accounts []accBalancePair) error { func fetchBalances(c Client, gasHash util.Uint160, accounts []accBalancePair) error {
w := io.NewBufBinWriter() w := io.NewBufBinWriter()
for i := range accounts { for i := range accounts {
emit.AppCall(w.BinWriter, gasHash, "balanceOf", callflag.ReadStates, accounts[i].scriptHash) emit.AppCall(w.BinWriter, gasHash, "balanceOf", callflag.ReadStates, accounts[i].scriptHash)

View file

@ -44,7 +44,7 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error {
irSize := 0 irSize := 0
for ; irSize < lastGlagoliticLetter; irSize++ { for ; irSize < lastGlagoliticLetter; irSize++ {
ok, err := c.NNSIsAvailable(cs.Hash, getAlphabetNNSDomain(irSize)) ok, err := nnsIsAvailable(c, cs.Hash, getAlphabetNNSDomain(irSize))
if err != nil { if err != nil {
return err return err
} else if ok { } else if ok {

View file

@ -344,6 +344,31 @@ func (c *initializeContext) sendCommitteeTx(script []byte, sysFee int64, tryGrou
return c.multiSignAndSend(tx, committeeAccountName) return c.multiSignAndSend(tx, committeeAccountName)
} }
// sendSingleTx creates transaction signed by a simple account and pushes in onto the chain.
// It neither waits until tx persists nor checks the execution result.
func (c *initializeContext) sendSingleTx(script []byte, sysFee int64, acc *wallet.Account) error {
tx, err := c.Client.CreateTxFromScript(script, acc, sysFee, 0, []client.SignerAccount{{
Signer: transaction.Signer{
Account: acc.Contract.ScriptHash(),
Scopes: transaction.CalledByEntry,
},
Account: acc,
}})
if err != nil {
return err
}
magic, err := c.Client.GetNetwork()
if err != nil {
return fmt.Errorf("can't fetch network magic: %w", err)
}
if err := acc.SignTx(magic, tx); err != nil {
return fmt.Errorf("can't sign tx: %w", err)
}
return c.sendTx(tx, c.Command, false)
}
func getWalletAccount(w *wallet.Wallet, typ string) (*wallet.Account, error) { func getWalletAccount(w *wallet.Wallet, typ string) (*wallet.Account, error) {
for i := range w.Accounts { for i := range w.Accounts {
if w.Accounts[i].Label == typ { if w.Accounts[i].Label == typ {
@ -353,7 +378,7 @@ func getWalletAccount(w *wallet.Wallet, typ string) (*wallet.Account, error) {
return nil, fmt.Errorf("account for '%s' not found", typ) return nil, fmt.Errorf("account for '%s' not found", typ)
} }
func getNativeHashes(c *client.Client) (map[string]util.Uint160, error) { func getNativeHashes(c Client) (map[string]util.Uint160, error) {
ns, err := c.GetNativeContracts() ns, err := c.GetNativeContracts()
if err != nil { if err != nil {
return nil, fmt.Errorf("can't get native contract hashes: %w", err) return nil, fmt.Errorf("can't get native contract hashes: %w", err)

View file

@ -305,12 +305,11 @@ func (c *initializeContext) deployContracts() error {
params := getContractDeployParameters(alphaCs.RawNEF, alphaCs.RawManifest, params := getContractDeployParameters(alphaCs.RawNEF, alphaCs.RawManifest,
c.getAlphabetDeployParameters(i, len(c.Wallets))) c.getAlphabetDeployParameters(i, len(c.Wallets)))
signer := transaction.Signer{
res, err := c.Client.InvokeFunction(invokeHash, deployMethodName, params, []transaction.Signer{{
Account: acc.Contract.ScriptHash(), Account: acc.Contract.ScriptHash(),
Scopes: transaction.CalledByEntry, Scopes: transaction.CalledByEntry,
} }})
res, err := c.Client.InvokeFunction(invokeHash, deployMethodName, params, []transaction.Signer{signer})
if err != nil { if err != nil {
return fmt.Errorf("can't deploy alphabet #%d contract: %w", i, err) return fmt.Errorf("can't deploy alphabet #%d contract: %w", i, err)
} }
@ -318,15 +317,9 @@ func (c *initializeContext) deployContracts() error {
return fmt.Errorf("can't deploy alpabet #%d contract: %s", i, res.FaultException) return fmt.Errorf("can't deploy alpabet #%d contract: %s", i, res.FaultException)
} }
h, err := c.Client.SignAndPushInvocationTx(res.Script, acc, -1, 0, []client.SignerAccount{{ if err := c.sendSingleTx(res.Script, res.GasConsumed, acc); err != nil {
Signer: signer, return err
Account: acc,
}})
if err != nil {
return fmt.Errorf("can't push deploy transaction: %w", err)
} }
c.Hashes = append(c.Hashes, h)
} }
for _, ctrName := range contractList { for _, ctrName := range contractList {

View file

@ -90,7 +90,7 @@ func (c *initializeContext) updateNNSGroup(nnsHash util.Uint160, pub *keys.Publi
} }
func (c *initializeContext) emitUpdateNNSGroupScript(bw *io.BufBinWriter, nnsHash util.Uint160, pub *keys.PublicKey) (int64, error) { func (c *initializeContext) emitUpdateNNSGroupScript(bw *io.BufBinWriter, nnsHash util.Uint160, pub *keys.PublicKey) (int64, error) {
isAvail, err := c.Client.NNSIsAvailable(nnsHash, morphClient.NNSGroupKeyName) isAvail, err := nnsIsAvailable(c.Client, nnsHash, morphClient.NNSGroupKeyName)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -125,7 +125,7 @@ func getAlphabetNNSDomain(i int) string {
} }
func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string) ([]byte, error) { func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string) ([]byte, error) {
ok, err := c.Client.NNSIsAvailable(nnsHash, domain) ok, err := nnsIsAvailable(c.Client, nnsHash, domain)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -176,7 +176,7 @@ func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160) (bool, error
var errMissingNNSRecord = errors.New("missing NNS record") var errMissingNNSRecord = errors.New("missing NNS record")
// Returns errMissingNNSRecord if invocation fault exception contains "token not found". // Returns errMissingNNSRecord if invocation fault exception contains "token not found".
func nnsResolveHash(c *client.Client, nnsHash util.Uint160, domain string) (util.Uint160, error) { func nnsResolveHash(c Client, nnsHash util.Uint160, domain string) (util.Uint160, error) {
item, err := nnsResolve(c, nnsHash, domain) item, err := nnsResolve(c, nnsHash, domain)
if err != nil { if err != nil {
return util.Uint160{}, err return util.Uint160{}, err
@ -184,7 +184,7 @@ func nnsResolveHash(c *client.Client, nnsHash util.Uint160, domain string) (util
return parseNNSResolveResult(item) return parseNNSResolveResult(item)
} }
func nnsResolve(c *client.Client, nnsHash util.Uint160, domain string) (stackitem.Item, error) { func nnsResolve(c Client, nnsHash util.Uint160, domain string) (stackitem.Item, error) {
result, err := c.InvokeFunction(nnsHash, "resolve", []smartcontract.Parameter{ result, err := c.InvokeFunction(nnsHash, "resolve", []smartcontract.Parameter{
{ {
Type: smartcontract.StringType, Type: smartcontract.StringType,
@ -210,7 +210,7 @@ func nnsResolve(c *client.Client, nnsHash util.Uint160, domain string) (stackite
return result.Stack[len(result.Stack)-1], nil return result.Stack[len(result.Stack)-1], nil
} }
func nnsResolveKey(c *client.Client, nnsHash util.Uint160, domain string) (*keys.PublicKey, error) { func nnsResolveKey(c Client, nnsHash util.Uint160, domain string) (*keys.PublicKey, error) {
item, err := nnsResolve(c, nnsHash, domain) item, err := nnsResolve(c, nnsHash, domain)
if err != nil { if err != nil {
return nil, err return nil, err
@ -243,3 +243,12 @@ func parseNNSResolveResult(res stackitem.Item) (util.Uint160, error) {
} }
return util.Uint160DecodeStringLE(string(bs)) return util.Uint160DecodeStringLE(string(bs))
} }
func nnsIsAvailable(c Client, nnsHash util.Uint160, name string) (bool, error) {
switch ct := c.(type) {
case *client.Client:
return ct.NNSIsAvailable(nnsHash, name)
default:
panic("unimplemented")
}
}

View file

@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native" "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/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
@ -35,7 +34,7 @@ func (c *initializeContext) registerCandidates() error {
} }
} }
regPrice, err := c.Client.GetCandidateRegisterPrice() regPrice, err := getCandidateRegisterPrice(c.Client)
if err != nil { if err != nil {
return fmt.Errorf("can't fetch registration price: %w", err) return fmt.Errorf("can't fetch registration price: %w", err)
} }
@ -46,18 +45,9 @@ func (c *initializeContext) registerCandidates() error {
emit.AppCall(w.BinWriter, neoHash, "registerCandidate", callflag.States, acc.PrivateKey().PublicKey().Bytes()) emit.AppCall(w.BinWriter, neoHash, "registerCandidate", callflag.States, acc.PrivateKey().PublicKey().Bytes())
emit.Opcodes(w.BinWriter, opcode.ASSERT) emit.Opcodes(w.BinWriter, opcode.ASSERT)
h, err := c.Client.SignAndPushInvocationTx(w.Bytes(), acc, sysGas, 0, []client.SignerAccount{{ if err := c.sendSingleTx(w.Bytes(), sysGas, acc); err != nil {
Signer: transaction.Signer{
Account: acc.Contract.ScriptHash(),
Scopes: transaction.CalledByEntry,
},
Account: acc,
}})
if err != nil {
return err return err
} }
c.Hashes = append(c.Hashes, h)
} }
return c.awaitTx() return c.awaitTx()
@ -93,3 +83,12 @@ func (c *initializeContext) transferNEOFinished(neoHash util.Uint160) (bool, err
bal, err := c.Client.NEP17BalanceOf(neoHash, c.CommitteeAcc.Contract.ScriptHash()) bal, err := c.Client.NEP17BalanceOf(neoHash, c.CommitteeAcc.Contract.ScriptHash())
return bal < native.NEOTotalSupply, err return bal < native.NEOTotalSupply, err
} }
func getCandidateRegisterPrice(c Client) (int64, error) {
switch ct := c.(type) {
case *client.Client:
return ct.GetCandidateRegisterPrice()
default:
panic("unimplemented")
}
}

View file

@ -6,8 +6,13 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native" "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/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
scContext "github.com/nspcc-dev/neo-go/pkg/smartcontract/context" scContext "github.com/nspcc-dev/neo-go/pkg/smartcontract/context"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/wallet"
) )
const ( const (
@ -56,7 +61,7 @@ func (c *initializeContext) transferFunds() error {
}, },
) )
tx, err := c.Client.CreateNEP17MultiTransferTx(c.ConsensusAcc, 0, transfers, []client.SignerAccount{{ tx, err := createNEP17MultiTransferTx(c.Client, c.ConsensusAcc, 0, transfers, []client.SignerAccount{{
Signer: transaction.Signer{ Signer: transaction.Signer{
Account: c.ConsensusAcc.Contract.ScriptHash(), Account: c.ConsensusAcc.Contract.ScriptHash(),
Scopes: transaction.CalledByEntry, Scopes: transaction.CalledByEntry,
@ -138,7 +143,11 @@ func (c *initializeContext) transferGASToProxy() error {
return err return err
} }
tx, err := c.Client.CreateNEP17TransferTx(c.CommitteeAcc, proxyCs.Hash, gasHash, initialProxyGASAmount, 0, nil, nil) tx, err := createNEP17MultiTransferTx(c.Client, c.CommitteeAcc, 0, []client.TransferTarget{{
Token: gasHash,
Address: proxyCs.Hash,
Amount: initialProxyGASAmount,
}}, nil)
if err != nil { if err != nil {
return err return err
} }
@ -149,3 +158,25 @@ func (c *initializeContext) transferGASToProxy() error {
return c.awaitTx() return c.awaitTx()
} }
func createNEP17MultiTransferTx(c Client, acc *wallet.Account, netFee int64,
recipients []client.TransferTarget, cosigners []client.SignerAccount) (*transaction.Transaction, error) {
from := acc.Contract.ScriptHash()
w := io.NewBufBinWriter()
for i := range recipients {
emit.AppCall(w.BinWriter, recipients[i].Token, "transfer", callflag.All,
from, recipients[i].Address, recipients[i].Amount, recipients[i].Data)
emit.Opcodes(w.BinWriter, opcode.ASSERT)
}
if w.Err != nil {
return nil, fmt.Errorf("failed to create transfer script: %w", w.Err)
}
return c.CreateTxFromScript(w.Bytes(), acc, -1, netFee, append([]client.SignerAccount{{
Signer: transaction.Signer{
Account: from,
Scopes: transaction.CalledByEntry,
},
Account: acc,
}}, cosigners...))
}

View file

@ -5,21 +5,46 @@ import (
"errors" "errors"
"time" "time"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"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"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// Client represents N3 client interface capable of test-invoking scripts
// and sending signed transactions to chain.
type Client interface {
GetBlockCount() (uint32, error)
GetDesignatedByRole(noderoles.Role, uint32) (keys.PublicKeys, error)
GetContractStateByID(int32) (*state.Contract, error)
GetContractStateByHash(util.Uint160) (*state.Contract, error)
GetNativeContracts() ([]state.NativeContract, error)
GetNetwork() (netmode.Magic, error)
GetApplicationLog(util.Uint256, *trigger.Type) (*result.ApplicationLog, error)
CreateTxFromScript([]byte, *wallet.Account, int64, int64, []client.SignerAccount) (*transaction.Transaction, error)
NEP17BalanceOf(util.Uint160, util.Uint160) (int64, error)
InvokeFunction(util.Uint160, string, []smartcontract.Parameter, []transaction.Signer) (*result.Invoke, error)
InvokeScript([]byte, []transaction.Signer) (*result.Invoke, error)
SendRawTransaction(*transaction.Transaction) (util.Uint256, error)
}
type clientContext struct { type clientContext struct {
Client *client.Client Client Client
Hashes []util.Uint256 Hashes []util.Uint256
WaitDuration time.Duration WaitDuration time.Duration
PollInterval time.Duration PollInterval time.Duration
} }
func getN3Client(v *viper.Viper) (*client.Client, error) { func getN3Client(v *viper.Viper) (Client, error) {
// number of opened connections // number of opened connections
// by neo-go client per one host // by neo-go client per one host
const ( const (
@ -45,7 +70,7 @@ func getN3Client(v *viper.Viper) (*client.Client, error) {
return c, nil return c, nil
} }
func defaultClientContext(c *client.Client) *clientContext { func defaultClientContext(c Client) *clientContext {
return &clientContext{ return &clientContext{
Client: c, Client: c,
WaitDuration: time.Second * 30, WaitDuration: time.Second * 30,

View file

@ -25,10 +25,7 @@ func setPolicyCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("can't to initialize context: %w", err) return fmt.Errorf("can't to initialize context: %w", err)
} }
policyHash, err := wCtx.Client.GetNativeContractHash(nativenames.Policy) policyHash := wCtx.nativeHash(nativenames.Policy)
if err != nil {
return fmt.Errorf("can't get policy contract hash: %w", err)
}
bw := io.NewBufBinWriter() bw := io.NewBufBinWriter()
for i := range args { for i := range args {