2021-02-20 10:22:59 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
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-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"
|
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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-11-03 12:44:59 +00:00
|
|
|
notaryInfo struct {
|
2021-02-20 10:22:59 +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-08-06 13:24:16 +00:00
|
|
|
fallbackTime uint32 // mainTx's ValidUntilBlock - fallbackTime + 1 is when fallbackTx is sent
|
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
|
|
|
|
|
2021-02-20 10:22:59 +00:00
|
|
|
txValidTime, roundTime, fallbackTime 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 (
|
|
|
|
defaultNotaryValidTime = 50
|
|
|
|
defaultNotaryRoundTime = 100
|
|
|
|
defaultNotaryFallbackTime = 40
|
|
|
|
|
2021-10-22 14:39:42 +00:00
|
|
|
notaryBalanceOfMethod = "balanceOf"
|
|
|
|
notaryExpirationOfMethod = "expirationOf"
|
|
|
|
setDesignateMethod = "designateAsRole"
|
2021-02-25 16:53:30 +00:00
|
|
|
|
2021-04-19 14:50:44 +00:00
|
|
|
notaryBalanceErrMsg = "can't fetch notary balance"
|
|
|
|
notaryNotEnabledPanicMsg = "notary support was not enabled on this client"
|
2021-02-20 10:22:59 +00:00
|
|
|
)
|
|
|
|
|
2021-04-19 14:50:44 +00:00
|
|
|
var errUnexpectedItems = errors.New("invalid number of NEO VM arguments on stack")
|
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,
|
|
|
|
fallbackTime: defaultNotaryFallbackTime,
|
|
|
|
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,
|
|
|
|
fallbackTime: cfg.fallbackTime,
|
|
|
|
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.
|
2021-08-26 07:59:02 +00:00
|
|
|
func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) (res util.Uint256, err 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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return util.Uint256{}, fmt.Errorf("can't get blockchain height: %w", err)
|
2021-02-20 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 14:39:42 +00:00
|
|
|
currentTill, err := c.depositExpirationOf()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("can't get previous expiration value: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
till := int64(bc + delta)
|
|
|
|
if till < currentTill {
|
|
|
|
till = currentTill
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:48:18 +00:00
|
|
|
return c.depositNotary(amount, till)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (c *Client) DepositEndlessNotary(amount fixedn.Fixed8) (res util.Uint256, err error) {
|
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return util.Uint256{}, ErrConnectionLost
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.notary == nil {
|
|
|
|
panic(notaryNotEnabledPanicMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// till value refers to a block height and it is uint32 value in neo-go
|
|
|
|
return c.depositNotary(amount, math.MaxUint32)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) depositNotary(amount fixedn.Fixed8, till int64) (res util.Uint256, err 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) {
|
|
|
|
return util.Uint256{}, fmt.Errorf("can't make notary deposit: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction is already in mempool waiting to be processed.
|
|
|
|
// This is an expected situation if we restart the service.
|
2023-03-28 15:34:15 +00:00
|
|
|
c.logger.Info("notary deposit has already been made",
|
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))
|
|
|
|
return util.Uint256{}, nil
|
2021-02-20 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 15:34:15 +00:00
|
|
|
c.logger.Info("notary deposit invoke",
|
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
|
|
|
|
2021-04-27 11:07:06 +00:00
|
|
|
return txHash, 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()
|
|
|
|
|
|
|
|
items, err := c.TestInvoke(c.notary.notary, notaryBalanceOfMethod, sh)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return 0, fmt.Errorf("%v: %w", notaryBalanceErrMsg, err)
|
2021-02-25 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(items) != 1 {
|
2023-01-10 13:07:47 +00:00
|
|
|
return 0, wrapFrostFSError(fmt.Errorf("%v: %w", notaryBalanceErrMsg, errUnexpectedItems))
|
2021-02-25 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bigIntDeposit, err := items[0].TryInteger()
|
|
|
|
if err != nil {
|
2023-01-10 13:07:47 +00:00
|
|
|
return 0, wrapFrostFSError(fmt.Errorf("%v: %w", notaryBalanceErrMsg, 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.
|
2021-11-09 14:52:48 +00:00
|
|
|
func (c *Client) UpdateNotaryList(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
|
|
|
}
|
|
|
|
|
2021-11-09 14:52:48 +00:00
|
|
|
nonce, vub, err := c.CalculateNonceAndVUB(prm.hash)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not calculate nonce and `valicUntilBlock` values: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-08-26 07:59:02 +00:00
|
|
|
return c.notaryInvokeAsCommittee(
|
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.
|
2021-11-09 14:52:48 +00:00
|
|
|
func (c *Client) UpdateNeoFSAlphabetList(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
|
|
|
}
|
|
|
|
|
2021-11-09 14:52:48 +00:00
|
|
|
nonce, vub, err := c.CalculateNonceAndVUB(prm.hash)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not calculate nonce and `valicUntilBlock` values: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-08-26 07:59:02 +00:00
|
|
|
return c.notaryInvokeAsCommittee(
|
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
|
|
|
//
|
2021-11-09 14:52:48 +00:00
|
|
|
// `nonce` and `vub` are used only if notary is enabled.
|
2023-02-21 11:42:45 +00:00
|
|
|
func (c *Client) NotaryInvoke(contract util.Uint160, fee fixedn.Fixed8, nonce uint32, vub *uint32, method string, args ...any) 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-04-19 14:50:44 +00:00
|
|
|
if c.notary == nil {
|
2021-05-21 07:39:21 +00:00
|
|
|
return c.Invoke(contract, fee, method, args...)
|
2021-04-19 14:50:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:52:48 +00:00
|
|
|
return c.notaryInvoke(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.
|
2023-02-21 11:42:45 +00:00
|
|
|
func (c *Client) NotaryInvokeNotAlpha(contract util.Uint160, fee fixedn.Fixed8, method string, args ...any) error {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return ErrConnectionLost
|
2021-08-25 11:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.notary == nil {
|
|
|
|
return c.Invoke(contract, fee, method, args...)
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:07:59 +00:00
|
|
|
return c.notaryInvoke(false, false, contract, rand.Uint32(), nil, 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 {
|
|
|
|
return fmt.Errorf("could not fetch current alphabet keys: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
multiaddrAccount, err := c.notaryMultisigAccount(alphabetList, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:30:08 +00:00
|
|
|
// error appears only if client
|
|
|
|
// is in inactive mode; that has
|
|
|
|
// been already checked above
|
|
|
|
magicNumber, _ := c.MagicNumber()
|
|
|
|
|
2021-09-03 16:23:18 +00:00
|
|
|
// mainTX is expected to be pre-validated: second witness must exist and be empty
|
|
|
|
mainTx.Scripts[1].VerificationScript = multiaddrAccount.GetVerificationScript()
|
|
|
|
mainTx.Scripts[1].InvocationScript = append(
|
|
|
|
[]byte{byte(opcode.PUSHDATA1), 64},
|
2022-03-27 17:30:08 +00:00
|
|
|
multiaddrAccount.PrivateKey().SignHashable(uint32(magicNumber), mainTx)...,
|
2021-09-03 16:23:18 +00:00
|
|
|
)
|
|
|
|
|
2023-04-03 13:30:05 +00:00
|
|
|
//lint:ignore SA1019 https://git.frostfs.info/TrueCloudLab/frostfs-node/issues/202
|
2021-09-03 16:23:18 +00:00
|
|
|
resp, err := c.client.SignAndPushP2PNotaryRequest(mainTx,
|
|
|
|
[]byte{byte(opcode.RET)},
|
|
|
|
-1,
|
|
|
|
0,
|
|
|
|
c.notary.fallbackTime,
|
|
|
|
c.acc)
|
|
|
|
if err != nil && !alreadyOnChainError(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Debug("notary request with prepared main TX invoked",
|
|
|
|
zap.Uint32("fallback_valid_for", c.notary.fallbackTime),
|
|
|
|
zap.Stringer("tx_hash", resp.Hash().Reverse()))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func (c *Client) notaryInvokeAsCommittee(method string, nonce, vub uint32, args ...any) error {
|
2022-11-03 12:44:59 +00:00
|
|
|
designate := c.GetDesignateHash()
|
2021-11-09 14:52:48 +00:00
|
|
|
return c.notaryInvoke(true, true, designate, nonce, &vub, method, args...)
|
2021-03-09 13:24:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func (c *Client) notaryInvoke(committee, invokedByAlpha bool, contract util.Uint160, nonce uint32, vub *uint32, method string, args ...any) error {
|
2023-04-05 14:52:32 +00:00
|
|
|
alphabetList, err := c.notary.alphabetSource()
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
cosigners, err := c.notaryCosigners(invokedByAlpha, alphabetList, committee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
params, err := invocationParams(args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-11-16 15:51:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
test, err := c.makeTestInvocation(contract, method, params, cosigners)
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
multiaddrAccount, err := c.notaryMultisigAccount(alphabetList, committee, invokedByAlpha)
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
until, err := c.getUntilValue(vub)
|
2021-02-20 10:22:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
mainTx, err := c.buildMainTx(invokedByAlpha, nonce, alphabetList, test, cosigners, multiaddrAccount, until)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-09 20:10:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
//lint:ignore SA1019 https://git.frostfs.info/TrueCloudLab/frostfs-node/issues/202
|
|
|
|
resp, err := c.client.SignAndPushP2PNotaryRequest(mainTx,
|
|
|
|
[]byte{byte(opcode.RET)},
|
|
|
|
-1,
|
|
|
|
0,
|
|
|
|
c.notary.fallbackTime,
|
|
|
|
c.acc)
|
|
|
|
if err != nil && !alreadyOnChainError(err) {
|
|
|
|
return err
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
c.logger.Debug("notary request invoked",
|
|
|
|
zap.String("method", method),
|
|
|
|
zap.Uint32("valid_until_block", until),
|
|
|
|
zap.Uint32("fallback_valid_for", c.notary.fallbackTime),
|
|
|
|
zap.Stringer("tx_hash", resp.Hash().Reverse()))
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) makeTestInvocation(contract util.Uint160, method string, params []sc.Parameter, cosigners []transaction.Signer) (*result.Invoke, error) {
|
|
|
|
test, err := c.client.InvokeFunction(contract, method, params, cosigners)
|
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-05 14:52:32 +00:00
|
|
|
if test.State != HaltState {
|
|
|
|
return nil, wrapFrostFSError(¬HaltStateError{state: test.State, exception: test.FaultException})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(test.Script) == 0 {
|
|
|
|
return nil, wrapFrostFSError(errEmptyInvocationScript)
|
|
|
|
}
|
|
|
|
return test, nil
|
|
|
|
}
|
2021-11-09 14:52:48 +00:00
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
func (c *Client) getUntilValue(vub *uint32) (uint32, error) {
|
2021-11-09 14:52:48 +00:00
|
|
|
if vub != nil {
|
2023-04-05 14:52:32 +00:00
|
|
|
return *vub, nil
|
|
|
|
}
|
|
|
|
return c.notaryTxValidationLimit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) buildMainTx(invokedByAlpha bool, nonce uint32, alphabetList keys.PublicKeys, test *result.Invoke,
|
|
|
|
cosigners []transaction.Signer, multiaddrAccount *wallet.Account, until uint32) (*transaction.Transaction, error) {
|
|
|
|
// after test invocation we build main multisig transaction
|
|
|
|
|
|
|
|
u8n := uint8(len(alphabetList))
|
|
|
|
|
|
|
|
if !invokedByAlpha {
|
|
|
|
u8n++
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepare main tx
|
|
|
|
mainTx := &transaction.Transaction{
|
2021-09-23 16:56:13 +00:00
|
|
|
Nonce: nonce,
|
2021-02-20 10:22:59 +00:00
|
|
|
SystemFee: test.GasConsumed,
|
|
|
|
ValidUntilBlock: until,
|
|
|
|
Script: test.Script,
|
|
|
|
Attributes: []transaction.Attribute{
|
|
|
|
{
|
|
|
|
Type: transaction.NotaryAssistedT,
|
|
|
|
Value: &transaction.NotaryAssisted{NKeys: u8n},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Signers: cosigners,
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate notary fee
|
2023-04-03 13:30:05 +00:00
|
|
|
//lint:ignore SA1019 https://git.frostfs.info/TrueCloudLab/frostfs-node/issues/202
|
2021-02-20 10:22:59 +00:00
|
|
|
notaryFee, err := c.client.CalculateNotaryFee(u8n)
|
|
|
|
if err != nil {
|
2023-04-05 14:52:32 +00:00
|
|
|
return nil, err
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add network fee for cosigners
|
2022-08-31 09:05:28 +00:00
|
|
|
//nolint:staticcheck // waits for neo-go v0.99.3 with notary actors
|
2023-04-03 13:30:05 +00:00
|
|
|
//lint:ignore SA1019 https://git.frostfs.info/TrueCloudLab/frostfs-node/issues/202
|
2021-02-20 10:22:59 +00:00
|
|
|
err = c.client.AddNetworkFee(
|
|
|
|
mainTx,
|
2021-03-18 15:44:23 +00:00
|
|
|
notaryFee,
|
2021-11-16 15:51:15 +00:00
|
|
|
c.notaryAccounts(invokedByAlpha, multiaddrAccount)...,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// define witnesses
|
2021-08-25 11:02:16 +00:00
|
|
|
mainTx.Scripts = c.notaryWitnesses(invokedByAlpha, multiaddrAccount, mainTx)
|
2021-02-20 10:22:59 +00:00
|
|
|
|
2023-04-05 14:52:32 +00:00
|
|
|
return mainTx, nil
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 15:51:15 +00:00
|
|
|
func (c *Client) notaryCosigners(invokedByAlpha bool, ir []*keys.PublicKey, committee bool) ([]transaction.Signer, error) {
|
|
|
|
s := make([]transaction.Signer, 0, 4)
|
2021-02-20 10:22:59 +00:00
|
|
|
|
|
|
|
// first we have proxy contract signature, as it will pay for the execution
|
|
|
|
s = append(s, transaction.Signer{
|
|
|
|
Account: c.notary.proxy,
|
|
|
|
Scopes: transaction.None,
|
|
|
|
})
|
|
|
|
|
|
|
|
// then we have inner ring multiaddress signature
|
2022-11-05 11:39:41 +00:00
|
|
|
m := sigCount(ir, committee)
|
2021-02-20 10:22:59 +00:00
|
|
|
|
|
|
|
multisigScript, err := sc.CreateMultiSigRedeemScript(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
|
2023-01-10 13:07:47 +00:00
|
|
|
return nil, wrapFrostFSError(fmt.Errorf("can't create ir multisig redeem script: %w", err))
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = append(s, transaction.Signer{
|
2021-08-05 14:17:54 +00:00
|
|
|
Account: hash.Hash160(multisigScript),
|
2023-05-11 14:05:24 +00:00
|
|
|
Scopes: c.cfg.signer.Scopes,
|
|
|
|
AllowedContracts: c.cfg.signer.AllowedContracts,
|
|
|
|
AllowedGroups: c.cfg.signer.AllowedGroups,
|
2021-02-20 10:22:59 +00:00
|
|
|
})
|
|
|
|
|
2021-11-16 15:51:15 +00:00
|
|
|
if !invokedByAlpha {
|
|
|
|
// then we have invoker signature
|
|
|
|
s = append(s, transaction.Signer{
|
|
|
|
Account: hash.Hash160(c.acc.GetVerificationScript()),
|
2023-05-11 14:05:24 +00:00
|
|
|
Scopes: c.cfg.signer.Scopes,
|
|
|
|
AllowedContracts: c.cfg.signer.AllowedContracts,
|
|
|
|
AllowedGroups: c.cfg.signer.AllowedGroups,
|
2021-11-16 15:51:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:22:59 +00:00
|
|
|
// last one is a placeholder for notary contract signature
|
|
|
|
s = append(s, transaction.Signer{
|
|
|
|
Account: c.notary.notary,
|
|
|
|
Scopes: transaction.None,
|
|
|
|
})
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:51:15 +00:00
|
|
|
func (c *Client) notaryAccounts(invokedByAlpha bool, multiaddr *wallet.Account) []*wallet.Account {
|
2021-02-20 10:22:59 +00:00
|
|
|
if multiaddr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:51:15 +00:00
|
|
|
a := make([]*wallet.Account, 0, 4)
|
2021-02-20 10:22:59 +00:00
|
|
|
|
|
|
|
// first we have proxy account, as it will pay for the execution
|
2022-11-23 14:27:29 +00:00
|
|
|
a = append(a, notary.FakeContractAccount(c.notary.proxy))
|
2021-02-20 10:22:59 +00:00
|
|
|
|
|
|
|
// then we have inner ring multiaddress account
|
|
|
|
a = append(a, multiaddr)
|
|
|
|
|
2021-11-16 15:51:15 +00:00
|
|
|
if !invokedByAlpha {
|
|
|
|
// then we have invoker account
|
|
|
|
a = append(a, c.acc)
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:22:59 +00:00
|
|
|
// last one is a placeholder for notary contract account
|
|
|
|
a = append(a, &wallet.Account{
|
|
|
|
Contract: &wallet.Contract{},
|
|
|
|
})
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:02:16 +00:00
|
|
|
func (c *Client) notaryWitnesses(invokedByAlpha bool, multiaddr *wallet.Account, tx *transaction.Transaction) []transaction.Witness {
|
2021-02-20 10:22:59 +00:00
|
|
|
if multiaddr == nil || tx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 15:51:15 +00:00
|
|
|
w := make([]transaction.Witness, 0, 4)
|
2021-02-20 10:22:59 +00:00
|
|
|
|
|
|
|
// first we have empty proxy witness, because notary will execute `Verify`
|
|
|
|
// method on the proxy contract to check witness
|
|
|
|
w = append(w, transaction.Witness{
|
|
|
|
InvocationScript: []byte{},
|
|
|
|
VerificationScript: []byte{},
|
|
|
|
})
|
|
|
|
|
|
|
|
// then we have inner ring multiaddress witness
|
2021-08-25 11:02:16 +00:00
|
|
|
|
|
|
|
// invocation script should be of the form:
|
|
|
|
// { PUSHDATA1, 64, signatureBytes... }
|
|
|
|
// to pass Notary module verification
|
|
|
|
var invokeScript []byte
|
|
|
|
|
2022-03-27 17:30:08 +00:00
|
|
|
magicNumber, _ := c.MagicNumber()
|
|
|
|
|
2021-08-25 11:02:16 +00:00
|
|
|
if invokedByAlpha {
|
|
|
|
invokeScript = append(
|
2021-02-20 10:22:59 +00:00
|
|
|
[]byte{byte(opcode.PUSHDATA1), 64},
|
2022-03-27 17:30:08 +00:00
|
|
|
multiaddr.PrivateKey().SignHashable(uint32(magicNumber), tx)...,
|
2021-08-25 11:02:16 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// we can't provide alphabet node signature
|
|
|
|
// because Storage Node doesn't own alphabet's
|
|
|
|
// private key. Thus, add dummy witness with
|
|
|
|
// empty bytes instead of signature
|
|
|
|
invokeScript = append(
|
|
|
|
[]byte{byte(opcode.PUSHDATA1), 64},
|
|
|
|
make([]byte, 64)...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
w = append(w, transaction.Witness{
|
|
|
|
InvocationScript: invokeScript,
|
2021-02-20 10:22:59 +00:00
|
|
|
VerificationScript: multiaddr.GetVerificationScript(),
|
|
|
|
})
|
|
|
|
|
2021-11-16 15:51:15 +00:00
|
|
|
if !invokedByAlpha {
|
|
|
|
// then we have invoker witness
|
|
|
|
invokeScript = append(
|
|
|
|
[]byte{byte(opcode.PUSHDATA1), 64},
|
2022-03-27 17:30:08 +00:00
|
|
|
c.acc.PrivateKey().SignHashable(uint32(magicNumber), tx)...,
|
2021-11-16 15:51:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
w = append(w, transaction.Witness{
|
|
|
|
InvocationScript: invokeScript,
|
|
|
|
VerificationScript: c.acc.GetVerificationScript(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:22:59 +00:00
|
|
|
// last one is a placeholder for notary contract witness
|
|
|
|
w = append(w, transaction.Witness{
|
|
|
|
InvocationScript: append(
|
|
|
|
[]byte{byte(opcode.PUSHDATA1), 64},
|
|
|
|
make([]byte, 64)...,
|
|
|
|
),
|
|
|
|
VerificationScript: []byte{},
|
|
|
|
})
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
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
|
2023-01-10 13:07:47 +00:00
|
|
|
return nil, wrapFrostFSError(fmt.Errorf("can't 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
|
2023-01-10 13:07:47 +00:00
|
|
|
return nil, wrapFrostFSError(fmt.Errorf("can't 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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return 0, fmt.Errorf("can't get current blockchain height: %w", err)
|
2021-02-20 10:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
min := bc + c.notary.txValidTime
|
|
|
|
rounded := (min/c.notary.roundTime + 1) * c.notary.roundTime
|
|
|
|
|
|
|
|
return rounded, nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:39:42 +00:00
|
|
|
func (c *Client) depositExpirationOf() (int64, error) {
|
|
|
|
expirationRes, err := c.TestInvoke(c.notary.notary, notaryExpirationOfMethod, c.acc.PrivateKey().GetScriptHash())
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("can't invoke method: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(expirationRes) != 1 {
|
|
|
|
return 0, fmt.Errorf("method returned unexpected item count: %d", len(expirationRes))
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTillBig, err := expirationRes[0].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("can't parse deposit till value: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentTillBig.Int64(), nil
|
|
|
|
}
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func invocationParams(args ...any) ([]sc.Parameter, error) {
|
2021-02-20 10:22:59 +00:00
|
|
|
params := make([]sc.Parameter, 0, len(args))
|
|
|
|
|
|
|
|
for i := range args {
|
|
|
|
param, err := toStackParameter(args[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params = append(params, param)
|
|
|
|
}
|
|
|
|
|
|
|
|
return params, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFallbackTime returns a notary support option for client
|
|
|
|
// that specifies amount of blocks before fallbackTx will be sent.
|
|
|
|
// Should be less than TxValidTime.
|
|
|
|
func WithFallbackTime(t uint32) NotaryOption {
|
|
|
|
return func(c *notaryCfg) {
|
|
|
|
c.fallbackTime = t
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 18:11:47 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 18:11:47 +00:00
|
|
|
// Neo RPC node can return `core.ErrInvalidAttribute` error with
|
|
|
|
// `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 {
|
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 {
|
|
|
|
return 0, fmt.Errorf("could not get notary balance: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gasBalance, err := c.GasBalance()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not get GAS balance: %w", err)
|
|
|
|
}
|
|
|
|
|
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.
|
2021-11-09 14:52:48 +00:00
|
|
|
func (c *Client) CalculateNonceAndVUB(hash util.Uint256) (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
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:52:48 +00:00
|
|
|
nonce = binary.LittleEndian.Uint32(hash.BytesLE())
|
|
|
|
|
2022-02-10 11:58:14 +00:00
|
|
|
height, err := c.getTransactionHeight(hash)
|
2021-11-09 14:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("could not get transaction height: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nonce, height + c.notary.txValidTime, nil
|
|
|
|
}
|
2022-02-10 11:58:14 +00:00
|
|
|
|
|
|
|
func (c *Client) getTransactionHeight(h util.Uint256) (uint32, error) {
|
2022-03-31 13:12:42 +00:00
|
|
|
if rh, ok := c.cache.txHeights.Get(h); ok {
|
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)
|
2022-02-10 11:58:14 +00:00
|
|
|
return height, nil
|
|
|
|
}
|