From 74a143cee3e6149e2b08bf52d4a3ce936f77a4b8 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 7 Dec 2020 11:43:32 +0300 Subject: [PATCH] core: allow to change deposit's `till` for owner only --- pkg/core/native/notary.go | 20 +++++++++++++------- pkg/core/native_notary_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/pkg/core/native/notary.go b/pkg/core/native/notary.go index f20010bca2..0d692670ca 100644 --- a/pkg/core/native/notary.go +++ b/pkg/core/native/notary.go @@ -33,7 +33,8 @@ const ( notaryContractID = reservedContractID - 1 // prefixDeposit is a prefix for storing Notary deposits. - prefixDeposit = 1 + prefixDeposit = 1 + defaultDepositDeltaTill = 5760 ) // 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{}) { to = toUint160(additionalParams[0]) } - till := toUint32(additionalParams[1]) + + allowedChangeTill := ic.Tx.Sender() == to currentHeight := ic.Chain.BlockHeight() + deposit := n.getDepositFor(ic.DAO, to) + till := toUint32(additionalParams[1]) if till < currentHeight { panic(fmt.Errorf("`till` shouldn't be less then the chain's height %d", currentHeight)) } - - deposit := n.getDepositFor(ic.DAO, to) + if deposit != nil && till < deposit.Till { + panic(fmt.Errorf("`till` shouldn't be less then the previous value %d", deposit.Till)) + } if deposit == nil { 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())) @@ -172,10 +177,11 @@ func (n *Notary) onPayment(ic *interop.Context, args []stackitem.Item) stackitem deposit = &state.Deposit{ Amount: new(big.Int), } - } else { - if till < deposit.Till { - panic(fmt.Errorf("`till` shouldn't be less then the previous value %d", deposit.Till)) + if !allowedChangeTill { + till = currentHeight + defaultDepositDeltaTill } + } else if !allowedChangeTill { // only deposit's owner is allowed to set or update `till` + till = deposit.Till } deposit.Amount.Add(deposit.Amount, amount) deposit.Till = till diff --git a/pkg/core/native_notary_test.go b/pkg/core/native_notary_test.go index 33d9703a63..2a67bc4235 100644 --- a/pkg/core/native_notary_test.go +++ b/pkg/core/native_notary_test.go @@ -1,6 +1,7 @@ package core import ( + "math" "testing" "github.com/nspcc-dev/neo-go/internal/testchain" @@ -24,7 +25,7 @@ func TestNotaryContractPipeline(t *testing.T) { notaryHash := chain.contracts.Notary.Hash gasHash := chain.contracts.GAS.Hash - depositLock := 30 + depositLock := 100 // check Notary contract has no GAS on the account 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()) require.NoError(t, err) 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) {