[#941] morph/notary: Add dynamic notary deposit till calculation

Do not set `till` as some constant:
use maximum of two values instead:
1. currentDepositTill;
2. currentHeight+epochDuration+constant.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-10-22 17:39:42 +03:00 committed by Alex Vanin
parent 0057eeb0f7
commit 78aab096fb

View file

@ -50,6 +50,7 @@ const (
defaultNotaryFallbackTime = 40 defaultNotaryFallbackTime = 40
notaryBalanceOfMethod = "balanceOf" notaryBalanceOfMethod = "balanceOf"
notaryExpirationOfMethod = "expirationOf"
setDesignateMethod = "designateAsRole" setDesignateMethod = "designateAsRole"
notaryBalanceErrMsg = "can't fetch notary balance" notaryBalanceErrMsg = "can't fetch notary balance"
@ -171,9 +172,19 @@ func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) (res util.Uin
return util.Uint256{}, fmt.Errorf("can't get blockchain height: %w", err) return util.Uint256{}, fmt.Errorf("can't get blockchain height: %w", err)
} }
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
}
gas, err := c.client.GetNativeContractHash(nativenames.Gas) gas, err := c.client.GetNativeContractHash(nativenames.Gas)
if err != nil { if err != nil {
return util.Uint256{}, err return util.Uint256{}, fmt.Errorf("can't get GAS script hash: %w", err)
} }
txHash, err := c.client.TransferNEP17( txHash, err := c.client.TransferNEP17(
@ -182,7 +193,7 @@ func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) (res util.Uin
gas, gas,
int64(amount), int64(amount),
0, 0,
[]interface{}{c.acc.PrivateKey().GetScriptHash(), int64(bc + delta)}, []interface{}{c.acc.PrivateKey().GetScriptHash(), till},
nil, nil,
) )
if err != nil { if err != nil {
@ -199,7 +210,7 @@ func (c *Client) DepositNotary(amount fixedn.Fixed8, delta uint32) (res util.Uin
c.logger.Debug("notary deposit invoke", c.logger.Debug("notary deposit invoke",
zap.Int64("amount", int64(amount)), zap.Int64("amount", int64(amount)),
zap.Uint32("expire_at", bc+delta), zap.Int64("expire_at", till),
zap.Stringer("tx_hash", txHash.Reverse())) zap.Stringer("tx_hash", txHash.Reverse()))
return txHash, nil return txHash, nil
@ -639,6 +650,24 @@ func (c *Client) notaryTxValidationLimit() (uint32, error) {
return rounded, nil return rounded, nil
} }
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
}
func invocationParams(args ...interface{}) ([]sc.Parameter, error) { func invocationParams(args ...interface{}) ([]sc.Parameter, error) {
params := make([]sc.Parameter, 0, len(args)) params := make([]sc.Parameter, 0, len(args))