[#404] innerring: Enable notary support in morph client

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-02-20 13:44:51 +03:00 committed by Alex Vanin
parent 779a495625
commit ccaf4f5d55
3 changed files with 52 additions and 0 deletions

View file

@ -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)

View file

@ -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

View file

@ -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.