2021-07-19 11:04:19 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-18 12:20:04 +00:00
|
|
|
"errors"
|
2022-04-07 08:14:10 +00:00
|
|
|
"fmt"
|
2021-11-30 11:45:40 +00:00
|
|
|
"time"
|
2021-07-19 11:04:19 +00:00
|
|
|
|
2022-04-07 08:10:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-11-30 11:45:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-06-02 14:24:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2022-06-02 14:24:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2022-04-07 08:10:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2021-11-30 11:45:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-04-07 08:10:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2021-11-30 11:45:40 +00:00
|
|
|
"github.com/spf13/cobra"
|
2021-07-19 11:04:19 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2022-04-07 08:10:35 +00:00
|
|
|
// Client represents N3 client interface capable of test-invoking scripts
|
|
|
|
// and sending signed transactions to chain.
|
|
|
|
type Client interface {
|
|
|
|
GetBlockCount() (uint32, 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)
|
2022-07-28 16:22:32 +00:00
|
|
|
CreateTxFromScript([]byte, *wallet.Account, int64, int64, []rpcclient.SignerAccount) (*transaction.Transaction, error)
|
2022-04-07 08:10:35 +00:00
|
|
|
NEP17BalanceOf(util.Uint160, util.Uint160) (int64, error)
|
|
|
|
InvokeScript([]byte, []transaction.Signer) (*result.Invoke, error)
|
|
|
|
SendRawTransaction(*transaction.Transaction) (util.Uint256, error)
|
2022-06-02 14:24:03 +00:00
|
|
|
GetCommittee() (keys.PublicKeys, error)
|
|
|
|
CalculateNotaryFee(uint8) (int64, error)
|
|
|
|
AddNetworkFee(*transaction.Transaction, int64, ...*wallet.Account) error
|
2022-07-28 16:22:32 +00:00
|
|
|
SignAndPushInvocationTx([]byte, *wallet.Account, int64, fixedn.Fixed8, []rpcclient.SignerAccount) (util.Uint256, error)
|
2022-06-02 14:24:03 +00:00
|
|
|
SignAndPushP2PNotaryRequest(*transaction.Transaction, []byte, int64, int64, uint32, *wallet.Account) (*payload.P2PNotaryRequest, error)
|
2022-04-07 08:10:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 11:45:40 +00:00
|
|
|
type clientContext struct {
|
2022-04-07 08:10:35 +00:00
|
|
|
Client Client
|
2021-11-30 11:45:40 +00:00
|
|
|
Hashes []util.Uint256
|
|
|
|
WaitDuration time.Duration
|
|
|
|
PollInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2022-04-07 08:10:35 +00:00
|
|
|
func getN3Client(v *viper.Viper) (Client, error) {
|
2021-12-15 18:05:21 +00:00
|
|
|
// number of opened connections
|
|
|
|
// by neo-go client per one host
|
2022-02-07 13:58:56 +00:00
|
|
|
const (
|
|
|
|
maxConnsPerHost = 10
|
|
|
|
requestTimeout = time.Second * 10
|
|
|
|
)
|
2021-12-15 18:05:21 +00:00
|
|
|
|
2022-02-07 13:58:56 +00:00
|
|
|
ctx := context.Background()
|
2021-07-19 11:04:19 +00:00
|
|
|
endpoint := v.GetString(endpointFlag)
|
2021-10-18 12:20:04 +00:00
|
|
|
if endpoint == "" {
|
|
|
|
return nil, errors.New("missing endpoint")
|
|
|
|
}
|
2022-07-28 16:22:32 +00:00
|
|
|
c, err := rpcclient.New(ctx, endpoint, rpcclient.Options{
|
2022-02-07 13:58:56 +00:00
|
|
|
MaxConnsPerHost: maxConnsPerHost,
|
|
|
|
RequestTimeout: requestTimeout,
|
|
|
|
})
|
2021-07-19 11:04:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
2021-11-30 11:45:40 +00:00
|
|
|
|
2022-04-07 08:10:35 +00:00
|
|
|
func defaultClientContext(c Client) *clientContext {
|
2021-11-30 11:57:58 +00:00
|
|
|
return &clientContext{
|
|
|
|
Client: c,
|
|
|
|
WaitDuration: time.Second * 30,
|
|
|
|
PollInterval: time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 11:45:40 +00:00
|
|
|
func (c *clientContext) sendTx(tx *transaction.Transaction, cmd *cobra.Command, await bool) error {
|
|
|
|
h, err := c.Client.SendRawTransaction(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-07 08:14:10 +00:00
|
|
|
if h != tx.Hash() {
|
|
|
|
return fmt.Errorf("sent and actual tx hashes mismatch:\n\tsent: %v\n\tactual: %v", tx.Hash().StringLE(), h.StringLE())
|
|
|
|
}
|
|
|
|
|
2021-11-30 11:45:40 +00:00
|
|
|
c.Hashes = append(c.Hashes, h)
|
|
|
|
|
|
|
|
if await {
|
|
|
|
return c.awaitTx(cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|