From ccaf4f5d5566954ceb917abbe8a00fe4fb8d4dda Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Sat, 20 Feb 2021 13:44:51 +0300 Subject: [PATCH] [#404] innerring: Enable notary support in morph client Signed-off-by: Alex Vanin --- cmd/neofs-ir/defaults.go | 1 + pkg/innerring/innerring.go | 15 +++++++++++++++ pkg/morph/client/notary.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/cmd/neofs-ir/defaults.go b/cmd/neofs-ir/defaults.go index 59546b4b7..6c4ac4051 100644 --- a/cmd/neofs-ir/defaults.go +++ b/cmd/neofs-ir/defaults.go @@ -65,6 +65,7 @@ func defaultConfiguration(cfg *viper.Viper) { cfg.SetDefault("contracts.balance", "") cfg.SetDefault("contracts.container", "") cfg.SetDefault("contracts.audit", "") + cfg.SetDefault("contracts.proxy", "") // alphabet contracts cfg.SetDefault("contracts.alphabet.amount", 7) diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index d1380fea8..37a953321 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -88,6 +88,7 @@ type ( balance util.Uint160 // in morph container util.Uint160 // in morph audit util.Uint160 // in morph + proxy util.Uint160 // in morph alphabet alphabetContracts // in morph } @@ -236,6 +237,14 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error return nil, err } + err = server.morphClient.EnableNotarySupport( + server.contracts.proxy, + server.contracts.netmap, + ) + if err != nil { + return nil, err + } + if cfg.GetBool("without_mainnet") { // This works as long as event Listener starts listening loop once, // otherwise Server.Start will run two similar routines. @@ -569,6 +578,7 @@ func parseContracts(cfg *viper.Viper) (*contracts, error) { balanceContractStr := cfg.GetString("contracts.balance") containerContractStr := cfg.GetString("contracts.container") auditContractStr := cfg.GetString("contracts.audit") + proxyContractStr := cfg.GetString("contracts.proxy") result.netmap, err = util.Uint160DecodeStringLE(netmapContractStr) if err != nil { @@ -595,6 +605,11 @@ func parseContracts(cfg *viper.Viper) (*contracts, error) { return nil, errors.Wrap(err, "ir: can't read audit script-hash") } + result.proxy, err = util.Uint160DecodeStringLE(proxyContractStr) + if err != nil { + return nil, errors.Wrap(err, "ir: can't read proxy script-hash") + } + result.alphabet, err = parseAlphabetContracts(cfg) if err != nil { return nil, err diff --git a/pkg/morph/client/notary.go b/pkg/morph/client/notary.go index f540ef550..51c853828 100644 --- a/pkg/morph/client/notary.go +++ b/pkg/morph/client/notary.go @@ -7,6 +7,7 @@ import ( "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" + "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" 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" @@ -90,6 +91,41 @@ func (c *Client) EnableNotarySupport(proxy, netmap util.Uint160, opts ...NotaryO return nil } +// 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. +func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) error { + if c.notary == nil { + return errNotaryNotEnabled + } + + bc, err := c.client.GetBlockCount() + if err != nil { + return errors.Wrap(err, "can't get blockchain height") + } + + txHash, err := c.client.TransferNEP17( + c.acc, + c.notary.notary, + c.gas, + int64(amount), + 0, + []interface{}{c.acc.PrivateKey().GetScriptHash(), int64(bc + delta)}, + ) + if err != nil { + return errors.Wrap(err, "can't make notary deposit") + } + + c.logger.Debug("notary deposit invoke", + zap.Int64("amount", int64(amount)), + zap.Uint32("expire_at", bc+delta), + zap.Stringer("tx_hash", txHash)) + + return nil +} + // Invoke invokes contract method by sending tx to notary contract in // blockchain. Fallback tx is a `RET`. Notary support should be enabled // in client to use this function.