core: allow to change deposit's till for owner only

This commit is contained in:
Anna Shaleva 2020-12-07 11:43:32 +03:00
parent c6dbdddba9
commit 74a143cee3
2 changed files with 37 additions and 8 deletions

View file

@ -33,7 +33,8 @@ const (
notaryContractID = reservedContractID - 1 notaryContractID = reservedContractID - 1
// prefixDeposit is a prefix for storing Notary deposits. // prefixDeposit is a prefix for storing Notary deposits.
prefixDeposit = 1 prefixDeposit = 1
defaultDepositDeltaTill = 5760
) )
// newNotary returns Notary native contract. // newNotary returns Notary native contract.
@ -158,13 +159,17 @@ func (n *Notary) onPayment(ic *interop.Context, args []stackitem.Item) stackitem
if !additionalParams[0].Equals(stackitem.Null{}) { if !additionalParams[0].Equals(stackitem.Null{}) {
to = toUint160(additionalParams[0]) to = toUint160(additionalParams[0])
} }
till := toUint32(additionalParams[1])
allowedChangeTill := ic.Tx.Sender() == to
currentHeight := ic.Chain.BlockHeight() currentHeight := ic.Chain.BlockHeight()
deposit := n.getDepositFor(ic.DAO, to)
till := toUint32(additionalParams[1])
if till < currentHeight { if till < currentHeight {
panic(fmt.Errorf("`till` shouldn't be less then the chain's height %d", currentHeight)) panic(fmt.Errorf("`till` shouldn't be less then the chain's height %d", currentHeight))
} }
if deposit != nil && till < deposit.Till {
deposit := n.getDepositFor(ic.DAO, to) panic(fmt.Errorf("`till` shouldn't be less then the previous value %d", deposit.Till))
}
if deposit == nil { if deposit == nil {
if amount.Cmp(big.NewInt(2*transaction.NotaryServiceFeePerKey)) < 0 { if amount.Cmp(big.NewInt(2*transaction.NotaryServiceFeePerKey)) < 0 {
panic(fmt.Errorf("first deposit can not be less then %d, got %d", 2*transaction.NotaryServiceFeePerKey, amount.Int64())) panic(fmt.Errorf("first deposit can not be less then %d, got %d", 2*transaction.NotaryServiceFeePerKey, amount.Int64()))
@ -172,10 +177,11 @@ func (n *Notary) onPayment(ic *interop.Context, args []stackitem.Item) stackitem
deposit = &state.Deposit{ deposit = &state.Deposit{
Amount: new(big.Int), Amount: new(big.Int),
} }
} else { if !allowedChangeTill {
if till < deposit.Till { till = currentHeight + defaultDepositDeltaTill
panic(fmt.Errorf("`till` shouldn't be less then the previous value %d", deposit.Till))
} }
} else if !allowedChangeTill { // only deposit's owner is allowed to set or update `till`
till = deposit.Till
} }
deposit.Amount.Add(deposit.Amount, amount) deposit.Amount.Add(deposit.Amount, amount)
deposit.Till = till deposit.Till = till

View file

@ -1,6 +1,7 @@
package core package core
import ( import (
"math"
"testing" "testing"
"github.com/nspcc-dev/neo-go/internal/testchain" "github.com/nspcc-dev/neo-go/internal/testchain"
@ -24,7 +25,7 @@ func TestNotaryContractPipeline(t *testing.T) {
notaryHash := chain.contracts.Notary.Hash notaryHash := chain.contracts.Notary.Hash
gasHash := chain.contracts.GAS.Hash gasHash := chain.contracts.GAS.Hash
depositLock := 30 depositLock := 100
// check Notary contract has no GAS on the account // check Notary contract has no GAS on the account
checkBalanceOf(t, chain, notaryHash, 0) checkBalanceOf(t, chain, notaryHash, 0)
@ -217,6 +218,28 @@ func TestNotaryContractPipeline(t *testing.T) {
withdrawRes, err = invokeContractMethod(chain, 100000000, notaryHash, "withdraw", testchain.MultisigScriptHash(), testchain.MultisigScriptHash()) withdrawRes, err = invokeContractMethod(chain, 100000000, notaryHash, "withdraw", testchain.MultisigScriptHash(), testchain.MultisigScriptHash())
require.NoError(t, err) require.NoError(t, err)
checkResult(t, withdrawRes, stackitem.NewBool(false)) checkResult(t, withdrawRes, stackitem.NewBool(false))
// `onPayment`: good first deposit to other account, should set default `till` even if other `till` value is provided
transferTx = transferTokenFromMultisigAccount(t, chain, notaryHash, gasHash, 2*transaction.NotaryServiceFeePerKey, acc.PrivateKey().PublicKey().GetScriptHash(), int64(math.MaxUint32-1))
res, err = chain.GetAppExecResults(transferTx.Hash(), trigger.Application)
require.NoError(t, err)
require.Equal(t, vm.HaltState, res[0].VMState)
require.Equal(t, 0, len(res[0].Stack))
checkBalanceOf(t, chain, notaryHash, 2*transaction.NotaryServiceFeePerKey)
till, err = invokeContractMethod(chain, 100000000, notaryHash, "expirationOf", acc.PrivateKey().PublicKey().GetScriptHash())
require.NoError(t, err)
checkResult(t, till, stackitem.Make(5760+chain.BlockHeight()-2))
// `onPayment`: good second deposit to other account, shouldn't update `till` even if other `till` value is provided
transferTx = transferTokenFromMultisigAccount(t, chain, notaryHash, gasHash, 2*transaction.NotaryServiceFeePerKey, acc.PrivateKey().PublicKey().GetScriptHash(), int64(math.MaxUint32-1))
res, err = chain.GetAppExecResults(transferTx.Hash(), trigger.Application)
require.NoError(t, err)
require.Equal(t, vm.HaltState, res[0].VMState)
require.Equal(t, 0, len(res[0].Stack))
checkBalanceOf(t, chain, notaryHash, 4*transaction.NotaryServiceFeePerKey)
till, err = invokeContractMethod(chain, 100000000, notaryHash, "expirationOf", acc.PrivateKey().PublicKey().GetScriptHash())
require.NoError(t, err)
checkResult(t, till, stackitem.Make(5760+chain.BlockHeight()-4))
} }
func TestNotaryNodesReward(t *testing.T) { func TestNotaryNodesReward(t *testing.T) {