2021-02-20 10:22:59 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2023-04-18 15:40:20 +00:00
|
|
|
"crypto/elliptic"
|
2021-11-09 14:52:48 +00:00
|
|
|
"encoding/binary"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-11-30 16:48:18 +00:00
|
|
|
"math"
|
2022-08-29 15:32:54 +00:00
|
|
|
"math/big"
|
2021-04-10 18:11:47 +00:00
|
|
|
"strings"
|
2023-05-26 09:15:50 +00:00
|
|
|
"time"
|
2021-04-10 18:11:47 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/rand"
|
2021-02-20 10:22:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2021-04-08 14:40:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
|
2021-02-20 10:22:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-20 10:44:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2022-11-18 09:44:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
2023-04-05 14:52:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2023-04-18 15:40:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2022-11-03 12:44:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
|
2021-02-20 10:22:59 +00:00
|
|
|
sc "github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-04-18 15:40:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2021-02-20 10:22:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-11-03 12:44:59 +00:00
|
|
|
notaryInfo struct {
|
2023-04-20 15:53:48 +00:00
|
|
|
txValidTime uint32 // minimum amount of blocks when mainTx will be valid
|
|
|
|
roundTime uint32 // extra amount of blocks to synchronize sidechain height diff of inner ring nodes
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2021-04-20 15:11:35 +00:00
|
|
|
alphabetSource AlphabetKeys // source of alphabet node keys to prepare witness
|
|
|
|
|
2021-02-20 10:22:59 +00:00
|
|
|
notary util.Uint160
|
|
|
|
proxy util.Uint160
|
|
|
|
}
|
|
|
|
|
|
|
|
notaryCfg struct {
|
2021-06-01 09:29:32 +00:00
|
|
|
proxy util.Uint160
|
|
|
|
|
2023-04-20 15:53:48 +00:00
|
|
|
txValidTime, roundTime uint32
|
2021-04-20 15:11:35 +00:00
|
|
|
|
|
|
|
alphabetSource AlphabetKeys
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 15:11:35 +00:00
|
|
|
AlphabetKeys func() (keys.PublicKeys, error)
|
2021-02-20 10:22:59 +00:00
|
|
|
NotaryOption func(*notaryCfg)
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-04-20 15:53:48 +00:00
|
|
|
defaultNotaryValidTime = 50
|
|
|
|
defaultNotaryRoundTime = 100
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2024-12-12 12:27:17 +00:00
|
|
|
setDesignateMethod = "designateAsRole"
|
2021-02-25 16:53:30 +00:00
|
|
|
|
2021-04-19 14:50:44 +00:00
|
|
|
notaryNotEnabledPanicMsg = "notary support was not enabled on this client"
|
2021-02-20 10:22:59 +00:00
|
|
|
)
|
|
|
|
|
2021-04-20 15:11:35 +00:00
|
|
|
func defaultNotaryConfig(c *Client) *notaryCfg {
|
2021-02-20 10:22:59 +00:00
|
|
|
return ¬aryCfg{
|
2021-04-20 15:11:35 +00:00
|
|
|
txValidTime: defaultNotaryValidTime,
|
|
|
|
roundTime: defaultNotaryRoundTime,
|
|
|
|
alphabetSource: c.Committee,
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:37:56 +00:00
|
|
|
// EnableNotarySupport creates notary structure in client that provides
|
2021-04-20 15:11:35 +00:00
|
|
|
// ability for client to get alphabet keys from committee or provided source
|
|
|
|
// and use proxy contract script hash to create tx for notary contract.
|
2021-07-23 14:37:56 +00:00
|
|
|
func (c *Client) EnableNotarySupport(opts ...NotaryOption) error {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return ErrConnectionLost
|
|
|
|
}
|
|
|
|
|
2021-04-20 15:11:35 +00:00
|
|
|
cfg := defaultNotaryConfig(c)
|
2021-02-20 10:22:59 +00:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(cfg)
|
|
|
|
}
|
|
|
|
|
2021-06-01 09:29:32 +00:00
|
|
|
if cfg.proxy.Equals(util.Uint160{}) {
|
2021-11-30 11:56:01 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
cfg.proxy, err = c.NNSContractAddress(NNSProxyContractName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get proxy contract addess from NNS: %w", err)
|
|
|
|
}
|
2021-06-01 09:29:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 12:44:59 +00:00
|
|
|
notaryCfg := ¬aryInfo{
|
2021-06-01 09:29:32 +00:00
|
|
|
proxy: cfg.proxy,
|
2021-04-20 15:11:35 +00:00
|
|
|
txValidTime: cfg.txValidTime,
|
|
|
|
roundTime: cfg.roundTime,
|
|
|
|
alphabetSource: cfg.alphabetSource,
|
2022-11-03 12:44:59 +00:00
|
|
|
notary: notary.Hash,
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
c.notary = notaryCfg
|
2021-08-26 07:59:02 +00:00
|
|
|
|
|
|
|
return nil
|
2021-04-29 13:31:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 11:09:55 +00:00
|
|
|
// IsNotaryEnabled returns true if EnableNotarySupport has been successfully
|
|
|
|
// called before.
|
|
|
|
func (c *Client) IsNotaryEnabled() bool {
|
|
|
|
return c.notary != nil
|
|
|
|
}
|
|
|
|
|
2021-07-16 13:53:23 +00:00
|
|
|
// ProbeNotary checks if native `Notary` contract is presented on chain.
|
2021-08-26 07:59:02 +00:00
|
|
|
func (c *Client) ProbeNotary() (res bool) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
2021-08-26 07:59:02 +00:00
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
if c.inactive {
|
|
|
|
return false
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 12:44:59 +00:00
|
|
|
_, err := c.client.GetContractStateByAddressOrName(nativenames.Notary)
|
2021-07-16 13:53:23 +00:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:44:51 +00:00
|
|
|
// DepositNotary calls notary deposit method. Deposit is required to operate
|
|
|
|
// with notary contract. It used by notary contract in to produce fallback tx
|
|
|
|
// if main tx failed to create. Deposit isn't last forever, so it should
|
|
|
|
// be called periodically. Notary support should be enabled in client to
|
|
|
|
// use this function.
|
2021-04-19 14:50:44 +00:00
|
|
|
//
|
2021-06-01 09:29:32 +00:00
|
|
|
// This function must be invoked with notary enabled otherwise it throws panic.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) DepositNotary(ctx context.Context, amount fixedn.Fixed8, delta uint32) (util.Uint256, error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return util.Uint256{}, ErrConnectionLost
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 10:44:51 +00:00
|
|
|
if c.notary == nil {
|
2021-04-19 14:50:44 +00:00
|
|
|
panic(notaryNotEnabledPanicMsg)
|
2021-02-20 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 08:58:50 +00:00
|
|
|
bc, err := c.rpcActor.GetBlockCount()
|
2021-02-20 10:44:51 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return util.Uint256{}, fmt.Errorf("get blockchain height: %w", err)
|
2021-02-20 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
2024-12-12 12:27:17 +00:00
|
|
|
r := notary.NewReader(c.rpcActor)
|
|
|
|
currentTill, err := r.ExpirationOf(c.acc.PrivateKey().GetScriptHash())
|
2021-10-22 14:39:42 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return util.Uint256{}, fmt.Errorf("get previous expiration value: %w", err)
|
2021-10-22 14:39:42 +00:00
|
|
|
}
|
|
|
|
|
2024-12-12 12:27:17 +00:00
|
|
|
till := max(int64(bc+delta), int64(currentTill))
|
2024-10-21 13:27:28 +00:00
|
|
|
res, _, err := c.depositNotary(ctx, amount, till)
|
2024-10-31 10:13:03 +00:00
|
|
|
return res, err
|
2022-11-30 16:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DepositEndlessNotary calls notary deposit method. Unlike `DepositNotary`,
|
|
|
|
// this method sets notary deposit till parameter to a maximum possible value.
|
|
|
|
// This allows to avoid ValidAfterDeposit failures.
|
|
|
|
//
|
|
|
|
// This function must be invoked with notary enabled otherwise it throws panic.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) DepositEndlessNotary(ctx context.Context, amount fixedn.Fixed8) (util.Uint256, uint32, error) {
|
2022-11-30 16:48:18 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
2024-10-31 10:13:03 +00:00
|
|
|
return util.Uint256{}, 0, ErrConnectionLost
|
2022-11-30 16:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.notary == nil {
|
|
|
|
panic(notaryNotEnabledPanicMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// till value refers to a block height and it is uint32 value in neo-go
|
2024-10-21 13:27:28 +00:00
|
|
|
return c.depositNotary(ctx, amount, math.MaxUint32)
|
2022-11-30 16:48:18 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) depositNotary(ctx context.Context, amount fixedn.Fixed8, till int64) (util.Uint256, uint32, error) {
|
2022-08-31 08:58:50 +00:00
|
|
|
txHash, vub, err := c.gasToken.Transfer(
|
2022-08-29 15:32:54 +00:00
|
|
|
c.accAddr,
|
2021-02-20 10:44:51 +00:00
|
|
|
c.notary.notary,
|
2022-08-29 15:32:54 +00:00
|
|
|
big.NewInt(int64(amount)),
|
2023-02-21 11:42:45 +00:00
|
|
|
[]any{c.acc.PrivateKey().GetScriptHash(), till})
|
2021-02-20 10:44:51 +00:00
|
|
|
if err != nil {
|
2022-11-18 09:44:53 +00:00
|
|
|
if !errors.Is(err, neorpc.ErrAlreadyExists) {
|
2024-12-12 12:06:20 +00:00
|
|
|
return util.Uint256{}, 0, fmt.Errorf("make notary deposit: %w", err)
|
2022-11-18 09:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction is already in mempool waiting to be processed.
|
|
|
|
// This is an expected situation if we restart the service.
|
2024-10-21 13:27:28 +00:00
|
|
|
c.logger.Info(ctx, logs.ClientNotaryDepositHasAlreadyBeenMade,
|
2022-11-18 09:44:53 +00:00
|
|
|
zap.Int64("amount", int64(amount)),
|
|
|
|
zap.Int64("expire_at", till),
|
|
|
|
zap.Uint32("vub", vub),
|
|
|
|
zap.Error(err))
|
2024-10-31 10:13:03 +00:00
|
|
|
return util.Uint256{}, 0, nil
|
2021-02-20 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
c.logger.Info(ctx, logs.ClientNotaryDepositInvoke,
|
2021-02-20 10:44:51 +00:00
|
|
|
zap.Int64("amount", int64(amount)),
|
2021-10-22 14:39:42 +00:00
|
|
|
zap.Int64("expire_at", till),
|
2022-08-29 15:32:54 +00:00
|
|
|
zap.Uint32("vub", vub),
|
2021-04-02 11:27:26 +00:00
|
|
|
zap.Stringer("tx_hash", txHash.Reverse()))
|
2021-02-20 10:44:51 +00:00
|
|
|
|
2024-10-31 10:13:03 +00:00
|
|
|
return txHash, vub, nil
|
2021-02-20 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:53:30 +00:00
|
|
|
// GetNotaryDeposit returns deposit of client's account in notary contract.
|
|
|
|
// Notary support should be enabled in client to use this function.
|
2021-04-19 14:50:44 +00:00
|
|
|
//
|
2021-06-01 09:29:32 +00:00
|
|
|
// This function must be invoked with notary enabled otherwise it throws panic.
|
2021-08-26 07:59:02 +00:00
|
|
|
func (c *Client) GetNotaryDeposit() (res int64, err error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return 0, ErrConnectionLost
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:53:30 +00:00
|
|
|
if c.notary == nil {
|
2021-04-19 14:50:44 +00:00
|
|
|
panic(notaryNotEnabledPanicMsg)
|
2021-02-25 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sh := c.acc.PrivateKey().PublicKey().GetScriptHash()
|
|
|
|
|
2024-12-12 12:27:17 +00:00
|
|
|
r := notary.NewReader(c.rpcActor)
|
|
|
|
bigIntDeposit, err := r.BalanceOf(sh)
|
2021-02-25 16:53:30 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:27:17 +00:00
|
|
|
return 0, fmt.Errorf("get notary deposit: %w", err)
|
2021-02-25 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bigIntDeposit.Int64(), nil
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:52:48 +00:00
|
|
|
// UpdateNotaryListPrm groups parameters of UpdateNotaryList operation.
|
|
|
|
type UpdateNotaryListPrm struct {
|
|
|
|
list keys.PublicKeys
|
|
|
|
hash util.Uint256
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetList sets a list of the new notary role keys.
|
2021-11-09 14:52:48 +00:00
|
|
|
func (u *UpdateNotaryListPrm) SetList(list keys.PublicKeys) {
|
|
|
|
u.list = list
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHash sets hash of the transaction that led to the update
|
|
|
|
// of the notary role in the designate contract.
|
|
|
|
func (u *UpdateNotaryListPrm) SetHash(hash util.Uint256) {
|
|
|
|
u.hash = hash
|
|
|
|
}
|
|
|
|
|
2021-03-10 09:24:26 +00:00
|
|
|
// UpdateNotaryList updates list of notary nodes in designate contract. Requires
|
|
|
|
// committee multi signature.
|
2021-04-19 14:50:44 +00:00
|
|
|
//
|
2021-06-01 09:29:32 +00:00
|
|
|
// This function must be invoked with notary enabled otherwise it throws panic.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) UpdateNotaryList(ctx context.Context, prm UpdateNotaryListPrm) error {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return ErrConnectionLost
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 09:24:26 +00:00
|
|
|
if c.notary == nil {
|
2021-04-19 14:50:44 +00:00
|
|
|
panic(notaryNotEnabledPanicMsg)
|
2021-03-10 09:24:26 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 09:28:34 +00:00
|
|
|
nonce, vub, err := c.CalculateNonceAndVUB(&prm.hash)
|
2021-11-09 14:52:48 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return fmt.Errorf("calculate nonce and `valicUntilBlock` values: %w", err)
|
2021-11-09 14:52:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 07:59:02 +00:00
|
|
|
return c.notaryInvokeAsCommittee(
|
2024-10-21 13:27:28 +00:00
|
|
|
ctx,
|
2021-03-10 09:24:26 +00:00
|
|
|
setDesignateMethod,
|
2021-11-09 14:52:48 +00:00
|
|
|
nonce,
|
|
|
|
vub,
|
2021-04-08 14:40:49 +00:00
|
|
|
noderoles.P2PNotary,
|
2021-11-09 14:52:48 +00:00
|
|
|
prm.list,
|
2021-03-10 09:24:26 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:52:48 +00:00
|
|
|
// UpdateAlphabetListPrm groups parameters of UpdateNeoFSAlphabetList operation.
|
|
|
|
type UpdateAlphabetListPrm struct {
|
|
|
|
list keys.PublicKeys
|
|
|
|
hash util.Uint256
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetList sets a list of the new alphabet role keys.
|
2021-11-09 14:52:48 +00:00
|
|
|
func (u *UpdateAlphabetListPrm) SetList(list keys.PublicKeys) {
|
|
|
|
u.list = list
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHash sets hash of the transaction that led to the update
|
|
|
|
// of the alphabet role in the designate contract.
|
|
|
|
func (u *UpdateAlphabetListPrm) SetHash(hash util.Uint256) {
|
|
|
|
u.hash = hash
|
|
|
|
}
|
|
|
|
|
2021-03-23 09:44:48 +00:00
|
|
|
// UpdateNeoFSAlphabetList updates list of alphabet nodes in designate contract.
|
2022-04-21 11:28:05 +00:00
|
|
|
// As for sidechain list should contain all inner ring nodes.
|
2021-03-23 09:44:48 +00:00
|
|
|
// Requires committee multi signature.
|
2021-04-19 14:50:44 +00:00
|
|
|
//
|
2021-06-01 09:29:32 +00:00
|
|
|
// This function must be invoked with notary enabled otherwise it throws panic.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) UpdateNeoFSAlphabetList(ctx context.Context, prm UpdateAlphabetListPrm) error {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return ErrConnectionLost
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 09:44:48 +00:00
|
|
|
if c.notary == nil {
|
2021-04-19 14:50:44 +00:00
|
|
|
panic(notaryNotEnabledPanicMsg)
|
2021-03-23 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 09:28:34 +00:00
|
|
|
nonce, vub, err := c.CalculateNonceAndVUB(&prm.hash)
|
2021-11-09 14:52:48 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return fmt.Errorf("calculate nonce and `valicUntilBlock` values: %w", err)
|
2021-11-09 14:52:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 07:59:02 +00:00
|
|
|
return c.notaryInvokeAsCommittee(
|
2024-10-21 13:27:28 +00:00
|
|
|
ctx,
|
2021-03-23 09:44:48 +00:00
|
|
|
setDesignateMethod,
|
2021-11-09 14:52:48 +00:00
|
|
|
nonce,
|
|
|
|
vub,
|
2021-04-08 14:40:49 +00:00
|
|
|
noderoles.NeoFSAlphabet,
|
2021-11-09 14:52:48 +00:00
|
|
|
prm.list,
|
2021-03-23 09:44:48 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-19 14:50:44 +00:00
|
|
|
// NotaryInvoke invokes contract method by sending tx to notary contract in
|
2021-05-21 07:39:21 +00:00
|
|
|
// blockchain. Fallback tx is a `RET`. If Notary support is not enabled
|
|
|
|
// it fallbacks to a simple `Invoke()`.
|
2021-02-20 10:22:59 +00:00
|
|
|
//
|
2023-11-07 15:13:26 +00:00
|
|
|
// Returns valid until block value.
|
|
|
|
//
|
2021-11-09 14:52:48 +00:00
|
|
|
// `nonce` and `vub` are used only if notary is enabled.
|
2024-11-14 07:01:59 +00:00
|
|
|
func (c *Client) NotaryInvoke(ctx context.Context, contract util.Uint160, fee fixedn.Fixed8, nonce uint32, vub *uint32, method string, args ...any) (InvokeRes, error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
2024-11-14 07:01:59 +00:00
|
|
|
return InvokeRes{}, ErrConnectionLost
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 14:50:44 +00:00
|
|
|
if c.notary == nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
return c.Invoke(ctx, contract, fee, method, args...)
|
2021-04-19 14:50:44 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
return c.notaryInvoke(ctx, false, true, contract, nonce, vub, method, args...)
|
2021-08-25 11:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotaryInvokeNotAlpha does the same as NotaryInvoke but does not use client's
|
|
|
|
// private key in Invocation script. It means that main TX of notary request is
|
|
|
|
// not expected to be signed by the current node.
|
|
|
|
//
|
|
|
|
// Considered to be used by non-IR nodes.
|
2024-11-14 07:01:59 +00:00
|
|
|
func (c *Client) NotaryInvokeNotAlpha(ctx context.Context, contract util.Uint160, fee fixedn.Fixed8, vubP *uint32, method string, args ...any) (InvokeRes, error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
2024-11-14 07:01:59 +00:00
|
|
|
return InvokeRes{}, ErrConnectionLost
|
2021-08-25 11:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.notary == nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
return c.Invoke(ctx, contract, fee, method, args...)
|
2021-08-25 11:02:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
return c.notaryInvoke(ctx, false, false, contract, rand.Uint32(), vubP, method, args...)
|
2021-03-09 13:24:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 16:23:18 +00:00
|
|
|
// NotarySignAndInvokeTX signs and sends notary request that was received from
|
|
|
|
// Notary service.
|
|
|
|
// NOTE: does not fallback to simple `Invoke()`. Expected to be used only for
|
|
|
|
// TXs retrieved from the received notary requests.
|
|
|
|
func (c *Client) NotarySignAndInvokeTX(mainTx *transaction.Transaction) error {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return ErrConnectionLost
|
2021-09-03 16:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
alphabetList, err := c.notary.alphabetSource()
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return fmt.Errorf("fetch current alphabet keys: %w", err)
|
2021-09-03 16:23:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
cosigners, err := c.notaryCosignersFromTx(mainTx, alphabetList)
|
2021-09-03 16:23:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
nAct, err := notary.NewActor(c.client, cosigners, c.acc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign exactly the same transaction we've got from the received Notary request.
|
|
|
|
err = nAct.Sign(mainTx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("faield to sign notary request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mainH, fbH, untilActual, err := nAct.Notarize(mainTx, nil)
|
2021-09-03 16:23:18 +00:00
|
|
|
|
|
|
|
if err != nil && !alreadyOnChainError(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
c.logger.Debug(context.Background(), logs.ClientNotaryRequestWithPreparedMainTXInvoked,
|
2023-04-18 15:40:20 +00:00
|
|
|
zap.String("tx_hash", mainH.StringLE()),
|
|
|
|
zap.Uint32("valid_until_block", untilActual),
|
|
|
|
zap.String("fallback_hash", fbH.StringLE()))
|
2021-09-03 16:23:18 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) notaryInvokeAsCommittee(ctx context.Context, method string, nonce, vub uint32, args ...any) error {
|
2022-11-03 12:44:59 +00:00
|
|
|
designate := c.GetDesignateHash()
|
2024-10-21 13:27:28 +00:00
|
|
|
_, err := c.notaryInvoke(ctx, true, true, designate, nonce, &vub, method, args...)
|
2023-11-07 15:13:26 +00:00
|
|
|
return err
|
2021-03-09 13:24:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 07:01:59 +00:00
|
|
|
func (c *Client) notaryInvoke(ctx context.Context, committee, invokedByAlpha bool, contract util.Uint160, nonce uint32, vub *uint32, method string, args ...any) (InvokeRes, error) {
|
2023-06-08 13:37:46 +00:00
|
|
|
start := time.Now()
|
|
|
|
success := false
|
|
|
|
defer func() {
|
|
|
|
c.metrics.ObserveInvoke("notaryInvoke", contract.String(), method, success, time.Since(start))
|
|
|
|
}()
|
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
alphabetList, err := c.notary.alphabetSource()
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
2024-11-14 07:01:59 +00:00
|
|
|
return InvokeRes{}, err
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
until, err := c.getUntilValue(vub)
|
2023-04-05 14:52:32 +00:00
|
|
|
if err != nil {
|
2024-11-14 07:01:59 +00:00
|
|
|
return InvokeRes{}, err
|
2023-04-05 14:52:32 +00:00
|
|
|
}
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
cosigners, err := c.notaryCosigners(invokedByAlpha, alphabetList, committee)
|
2023-04-05 14:52:32 +00:00
|
|
|
if err != nil {
|
2024-11-14 07:01:59 +00:00
|
|
|
return InvokeRes{}, err
|
2021-11-16 15:51:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
nAct, err := notary.NewActor(c.client, cosigners, c.acc)
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
2024-11-14 07:01:59 +00:00
|
|
|
return InvokeRes{}, err
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
mainH, fbH, untilActual, err := nAct.Notarize(nAct.MakeTunedCall(contract, method, nil, func(r *result.Invoke, t *transaction.Transaction) error {
|
|
|
|
if r.State != vmstate.Halt.String() {
|
|
|
|
return wrapFrostFSError(¬HaltStateError{state: r.State, exception: r.FaultException})
|
|
|
|
}
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
t.ValidUntilBlock = until
|
|
|
|
t.Nonce = nonce
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
return nil
|
|
|
|
}, args...))
|
2021-04-09 20:10:59 +00:00
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
if err != nil && !alreadyOnChainError(err) {
|
2024-11-14 07:01:59 +00:00
|
|
|
return InvokeRes{}, err
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
c.logger.Debug(ctx, logs.ClientNotaryRequestInvoked,
|
2023-04-05 14:52:32 +00:00
|
|
|
zap.String("method", method),
|
2023-04-18 15:40:20 +00:00
|
|
|
zap.Uint32("valid_until_block", untilActual),
|
|
|
|
zap.String("tx_hash", mainH.StringLE()),
|
|
|
|
zap.String("fallback_hash", fbH.StringLE()))
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2023-06-08 13:37:46 +00:00
|
|
|
success = true
|
2024-11-14 07:01:59 +00:00
|
|
|
return InvokeRes{Hash: mainH, VUB: until}, nil
|
2023-04-05 14:52:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
func (c *Client) notaryCosignersFromTx(mainTx *transaction.Transaction, alphabetList keys.PublicKeys) ([]actor.SignerAccount, error) {
|
|
|
|
multiaddrAccount, err := c.notaryMultisigAccount(alphabetList, false, true)
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
2023-04-05 14:52:32 +00:00
|
|
|
return nil, err
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
// Here we need to add a committee signature (second witness) to the pre-validated
|
|
|
|
// main transaction without creating a new one. However, Notary actor demands the
|
|
|
|
// proper set of signers for constructor, thus, fill it from the main transaction's signers list.
|
|
|
|
s := make([]actor.SignerAccount, 2, 3)
|
|
|
|
s[0] = actor.SignerAccount{
|
|
|
|
// Proxy contract that will pay for the execution.
|
|
|
|
Signer: mainTx.Signers[0],
|
|
|
|
Account: notary.FakeContractAccount(mainTx.Signers[0].Account),
|
|
|
|
}
|
|
|
|
s[1] = actor.SignerAccount{
|
|
|
|
// Inner ring multisignature.
|
|
|
|
Signer: mainTx.Signers[1],
|
|
|
|
Account: multiaddrAccount,
|
|
|
|
}
|
|
|
|
if len(mainTx.Signers) > 3 {
|
|
|
|
// Invoker signature (simple signature account of storage node is expected).
|
|
|
|
var acc *wallet.Account
|
|
|
|
script := mainTx.Scripts[2].VerificationScript
|
|
|
|
if len(script) == 0 {
|
|
|
|
acc = notary.FakeContractAccount(mainTx.Signers[2].Account)
|
|
|
|
} else {
|
|
|
|
pubBytes, ok := vm.ParseSignatureContract(script)
|
|
|
|
if ok {
|
|
|
|
pub, err := keys.NewPublicKeyFromBytes(pubBytes, elliptic.P256())
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("parse verification script of signer #2: invalid public key: %w", err)
|
2023-04-18 15:40:20 +00:00
|
|
|
}
|
|
|
|
acc = notary.FakeSimpleAccount(pub)
|
|
|
|
} else {
|
|
|
|
m, pubsBytes, ok := vm.ParseMultiSigContract(script)
|
|
|
|
if !ok {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, errors.New("parse verification script of signer #2: unknown witness type")
|
2023-04-18 15:40:20 +00:00
|
|
|
}
|
|
|
|
pubs := make(keys.PublicKeys, len(pubsBytes))
|
|
|
|
for i := range pubs {
|
|
|
|
pubs[i], err = keys.NewPublicKeyFromBytes(pubsBytes[i], elliptic.P256())
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("parse verification script of signer #2: invalid public key #%d: %w", i, err)
|
2023-04-18 15:40:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
acc, err = notary.FakeMultisigAccount(m, pubs)
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, fmt.Errorf("create fake account for signer #2: %w", err)
|
2023-04-18 15:40:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = append(s, actor.SignerAccount{
|
|
|
|
Signer: mainTx.Signers[2],
|
|
|
|
Account: acc,
|
|
|
|
})
|
2023-04-05 14:52:32 +00:00
|
|
|
}
|
2021-11-09 14:52:48 +00:00
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
return s, nil
|
2023-04-05 14:52:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
func (c *Client) notaryCosigners(invokedByAlpha bool, ir []*keys.PublicKey, committee bool) ([]actor.SignerAccount, error) {
|
|
|
|
multiaddrAccount, err := c.notaryMultisigAccount(ir, committee, invokedByAlpha)
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
2023-04-05 14:52:32 +00:00
|
|
|
return nil, err
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
2023-04-18 15:40:20 +00:00
|
|
|
s := make([]actor.SignerAccount, 2, 3)
|
|
|
|
// Proxy contract that will pay for the execution.
|
|
|
|
s[0] = actor.SignerAccount{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: c.notary.proxy,
|
2024-02-20 15:59:35 +00:00
|
|
|
// Do not change this:
|
|
|
|
// We must be able to call NNS contract indirectly from the Container contract.
|
|
|
|
// Thus, CalledByEntry is not sufficient.
|
|
|
|
// In future we may restrict this to all the usecases we have.
|
|
|
|
Scopes: transaction.Global,
|
2023-04-18 15:40:20 +00:00
|
|
|
},
|
|
|
|
Account: notary.FakeContractAccount(c.notary.proxy),
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
2023-04-18 15:40:20 +00:00
|
|
|
// Inner ring multisignature.
|
|
|
|
s[1] = actor.SignerAccount{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: multiaddrAccount.ScriptHash(),
|
2023-02-17 19:27:29 +00:00
|
|
|
Scopes: c.cfg.signer.Scopes,
|
|
|
|
AllowedContracts: c.cfg.signer.AllowedContracts,
|
|
|
|
AllowedGroups: c.cfg.signer.AllowedGroups,
|
2023-04-18 15:40:20 +00:00
|
|
|
},
|
|
|
|
Account: multiaddrAccount,
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 15:51:15 +00:00
|
|
|
if !invokedByAlpha {
|
2023-04-18 15:40:20 +00:00
|
|
|
// Invoker signature.
|
|
|
|
s = append(s, actor.SignerAccount{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: hash.Hash160(c.acc.GetVerificationScript()),
|
|
|
|
Scopes: c.cfg.signer.Scopes,
|
|
|
|
AllowedContracts: c.cfg.signer.AllowedContracts,
|
|
|
|
AllowedGroups: c.cfg.signer.AllowedGroups,
|
|
|
|
},
|
|
|
|
Account: c.acc,
|
|
|
|
})
|
2021-11-16 15:51:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
// The last one is Notary contract that will be added to the signers list
|
|
|
|
// by Notary actor automatically.
|
|
|
|
return s, nil
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 15:40:20 +00:00
|
|
|
func (c *Client) getUntilValue(vub *uint32) (uint32, error) {
|
|
|
|
if vub != nil {
|
|
|
|
return *vub, nil
|
2021-11-16 15:51:15 +00:00
|
|
|
}
|
2023-04-18 15:40:20 +00:00
|
|
|
return c.notaryTxValidationLimit()
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 11:02:16 +00:00
|
|
|
func (c *Client) notaryMultisigAccount(ir []*keys.PublicKey, committee, invokedByAlpha bool) (*wallet.Account, error) {
|
2022-11-05 11:39:41 +00:00
|
|
|
m := sigCount(ir, committee)
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2021-08-25 11:02:16 +00:00
|
|
|
var multisigAccount *wallet.Account
|
2022-11-05 11:39:41 +00:00
|
|
|
var err error
|
2021-08-25 11:02:16 +00:00
|
|
|
if invokedByAlpha {
|
|
|
|
multisigAccount = wallet.NewAccountFromPrivateKey(c.acc.PrivateKey())
|
|
|
|
err := multisigAccount.ConvertMultisig(m, ir)
|
|
|
|
if err != nil {
|
2023-02-05 15:59:38 +00:00
|
|
|
// wrap error as FrostFS-specific since the call is not related to any client
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, wrapFrostFSError(fmt.Errorf("convert account to inner ring multisig wallet: %w", err))
|
2021-08-25 11:02:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// alphabet multisig redeem script is
|
|
|
|
// used as verification script for
|
|
|
|
// inner ring multiaddress witness
|
2022-11-05 11:39:41 +00:00
|
|
|
multisigAccount, err = notary.FakeMultisigAccount(m, ir)
|
|
|
|
if err != nil {
|
2023-02-05 15:59:38 +00:00
|
|
|
// wrap error as FrostFS-specific since the call is not related to any client
|
2024-12-12 12:06:20 +00:00
|
|
|
return nil, wrapFrostFSError(fmt.Errorf("make inner ring multisig wallet: %w", err))
|
2021-08-25 11:02:16 +00:00
|
|
|
}
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return multisigAccount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) notaryTxValidationLimit() (uint32, error) {
|
2022-08-31 08:58:50 +00:00
|
|
|
bc, err := c.rpcActor.GetBlockCount()
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return 0, fmt.Errorf("get current blockchain height: %w", err)
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 15:37:04 +00:00
|
|
|
minTime := bc + c.notary.txValidTime
|
|
|
|
rounded := (minTime/c.notary.roundTime + 1) * c.notary.roundTime
|
2021-02-20 10:22:59 +00:00
|
|
|
|
|
|
|
return rounded, nil
|
|
|
|
}
|
|
|
|
|
2022-11-05 11:39:41 +00:00
|
|
|
// sigCount returns the number of required signature.
|
2023-02-05 15:59:38 +00:00
|
|
|
// For FrostFS Alphabet M is a 2/3+1 of it (like in dBFT).
|
2022-11-05 11:39:41 +00:00
|
|
|
// If committee is true, returns M as N/2+1.
|
|
|
|
func sigCount(ir []*keys.PublicKey, committee bool) int {
|
2021-03-09 13:24:56 +00:00
|
|
|
if committee {
|
2022-11-05 11:39:41 +00:00
|
|
|
return sc.GetMajorityHonestNodeCount(len(ir))
|
2021-03-09 13:24:56 +00:00
|
|
|
}
|
2022-11-05 11:39:41 +00:00
|
|
|
return sc.GetDefaultHonestNodeCount(len(ir))
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithTxValidTime returns a notary support option for client
|
|
|
|
// that specifies minimum amount of blocks when mainTx will be valid.
|
|
|
|
func WithTxValidTime(t uint32) NotaryOption {
|
|
|
|
return func(c *notaryCfg) {
|
|
|
|
c.txValidTime = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRoundTime returns a notary support option for client
|
|
|
|
// that specifies extra blocks to synchronize side chain
|
|
|
|
// height diff of inner ring nodes.
|
|
|
|
func WithRoundTime(t uint32) NotaryOption {
|
|
|
|
return func(c *notaryCfg) {
|
|
|
|
c.roundTime = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 15:11:35 +00:00
|
|
|
// WithAlphabetSource returns a notary support option for client
|
|
|
|
// that specifies function to return list of alphabet node keys.
|
|
|
|
// By default notary subsystem uses committee as a source. This is
|
|
|
|
// valid for side chain but notary in main chain should override it.
|
|
|
|
func WithAlphabetSource(t AlphabetKeys) NotaryOption {
|
|
|
|
return func(c *notaryCfg) {
|
|
|
|
c.alphabetSource = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 09:29:32 +00:00
|
|
|
// WithProxyContract sets proxy contract hash.
|
|
|
|
func WithProxyContract(h util.Uint160) NotaryOption {
|
|
|
|
return func(c *notaryCfg) {
|
|
|
|
c.proxy = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:58:22 +00:00
|
|
|
// Neo RPC node can return `neorpc.ErrInvalidAttribute` error with
|
2021-04-10 18:11:47 +00:00
|
|
|
// `conflicting transaction <> is already on chain` message. This
|
|
|
|
// error is expected and ignored. As soon as main tx persisted on
|
|
|
|
// chain everything is fine. This happens because notary contract
|
|
|
|
// requires 5 out of 7 signatures to send main tx, thus last two
|
|
|
|
// notary requests may be processed after main tx appeared on chain.
|
|
|
|
func alreadyOnChainError(err error) bool {
|
2023-10-25 12:58:22 +00:00
|
|
|
if !errors.Is(err, neorpc.ErrInvalidAttribute) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-10-12 16:04:48 +00:00
|
|
|
const alreadyOnChainErrorMessage = "already on chain"
|
|
|
|
|
2021-04-10 18:11:47 +00:00
|
|
|
return strings.Contains(err.Error(), alreadyOnChainErrorMessage)
|
|
|
|
}
|
2021-10-12 12:24:19 +00:00
|
|
|
|
|
|
|
// CalculateNotaryDepositAmount calculates notary deposit amount
|
|
|
|
// using the rule:
|
2022-08-15 16:20:20 +00:00
|
|
|
//
|
|
|
|
// IF notaryBalance < gasBalance * gasMul {
|
|
|
|
// DEPOSIT gasBalance / gasDiv
|
|
|
|
// } ELSE {
|
|
|
|
// DEPOSIT 1
|
|
|
|
// }
|
|
|
|
//
|
2021-10-12 12:24:19 +00:00
|
|
|
// gasMul and gasDiv must be positive.
|
|
|
|
func CalculateNotaryDepositAmount(c *Client, gasMul, gasDiv int64) (fixedn.Fixed8, error) {
|
|
|
|
notaryBalance, err := c.GetNotaryDeposit()
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return 0, fmt.Errorf("get notary balance: %w", err)
|
2021-10-12 12:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gasBalance, err := c.GasBalance()
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return 0, fmt.Errorf("get GAS balance: %w", err)
|
2021-10-12 12:24:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 15:19:27 +00:00
|
|
|
if gasBalance == 0 {
|
|
|
|
return 0, errors.New("zero gas balance, nothing to deposit")
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:24:19 +00:00
|
|
|
var depositAmount int64
|
|
|
|
|
|
|
|
if gasBalance*gasMul > notaryBalance {
|
|
|
|
depositAmount = gasBalance / gasDiv
|
|
|
|
} else {
|
|
|
|
depositAmount = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixedn.Fixed8(depositAmount), nil
|
|
|
|
}
|
2021-11-09 14:52:48 +00:00
|
|
|
|
|
|
|
// CalculateNonceAndVUB calculates nonce and ValidUntilBlock values
|
2022-05-13 10:21:50 +00:00
|
|
|
// based on transaction hash.
|
2023-11-08 09:28:34 +00:00
|
|
|
func (c *Client) CalculateNonceAndVUB(hash *util.Uint256) (nonce uint32, vub uint32, err error) {
|
2023-04-25 12:09:20 +00:00
|
|
|
return c.calculateNonceAndVUB(hash, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateNonceAndVUBControl calculates nonce and rounded ValidUntilBlock values
|
|
|
|
// based on transaction hash for use in control transactions.
|
2023-11-08 09:28:34 +00:00
|
|
|
func (c *Client) CalculateNonceAndVUBControl(hash *util.Uint256) (nonce uint32, vub uint32, err error) {
|
2023-04-25 12:09:20 +00:00
|
|
|
return c.calculateNonceAndVUB(hash, true)
|
|
|
|
}
|
|
|
|
|
2023-11-08 09:28:34 +00:00
|
|
|
// If hash specified, transaction's height and hash are used to compute VUB and nonce.
|
|
|
|
// If not, then current block height used to compute VUB and nonce.
|
|
|
|
func (c *Client) calculateNonceAndVUB(hash *util.Uint256, roundBlockHeight bool) (nonce uint32, vub uint32, err error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return 0, 0, ErrConnectionLost
|
2021-11-09 14:52:48 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 17:31:26 +00:00
|
|
|
if c.notary == nil {
|
|
|
|
return 0, 0, nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 09:28:34 +00:00
|
|
|
var height uint32
|
2021-11-09 14:52:48 +00:00
|
|
|
|
2023-11-08 09:28:34 +00:00
|
|
|
if hash != nil {
|
|
|
|
height, err = c.getTransactionHeight(*hash)
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return 0, 0, fmt.Errorf("get transaction height: %w", err)
|
2023-11-08 09:28:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
height, err = c.rpcActor.GetBlockCount()
|
|
|
|
if err != nil {
|
2024-12-12 12:06:20 +00:00
|
|
|
return 0, 0, fmt.Errorf("get chain height: %w", err)
|
2023-11-08 09:28:34 +00:00
|
|
|
}
|
2021-11-09 14:52:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 12:09:20 +00:00
|
|
|
// For control transactions, we round down the block height to control the
|
|
|
|
// probability of all nodes producing the same transaction, since it depends
|
|
|
|
// on this value.
|
|
|
|
if roundBlockHeight {
|
|
|
|
inc := c.rpcActor.GetVersion().Protocol.MaxValidUntilBlockIncrement
|
|
|
|
height = height / inc * inc
|
|
|
|
}
|
|
|
|
|
2023-11-08 09:28:34 +00:00
|
|
|
if hash != nil {
|
|
|
|
return binary.LittleEndian.Uint32(hash.BytesLE()), height + c.notary.txValidTime, nil
|
|
|
|
}
|
|
|
|
return height + c.notary.txValidTime, height + c.notary.txValidTime, nil
|
2021-11-09 14:52:48 +00:00
|
|
|
}
|
2022-02-10 11:58:14 +00:00
|
|
|
|
|
|
|
func (c *Client) getTransactionHeight(h util.Uint256) (uint32, error) {
|
2023-05-26 09:15:50 +00:00
|
|
|
success := false
|
|
|
|
startedAt := time.Now()
|
|
|
|
defer func() {
|
2023-06-13 13:15:59 +00:00
|
|
|
c.cache.metrics.AddMethodDuration("TxHeight", success, time.Since(startedAt))
|
2023-05-26 09:15:50 +00:00
|
|
|
}()
|
|
|
|
|
2022-03-31 13:12:42 +00:00
|
|
|
if rh, ok := c.cache.txHeights.Get(h); ok {
|
2023-05-26 09:15:50 +00:00
|
|
|
success = true
|
2022-12-30 09:47:18 +00:00
|
|
|
return rh, nil
|
2022-02-10 11:58:14 +00:00
|
|
|
}
|
|
|
|
height, err := c.client.GetTransactionHeight(h)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-03-31 13:12:42 +00:00
|
|
|
c.cache.txHeights.Add(h, height)
|
2023-05-26 09:15:50 +00:00
|
|
|
success = true
|
2022-02-10 11:58:14 +00:00
|
|
|
return height, nil
|
|
|
|
}
|