github.com/aergoio/aergo@v1.3.1/contract/system/staking_test.go (about) 1 /** 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package system 7 8 import ( 9 "math/big" 10 "testing" 11 12 "github.com/aergoio/aergo/types" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestBasicStakingUnstaking(t *testing.T) { 17 scs, sender, receiver := initTest(t) 18 defer deinitTest() 19 20 tx := &types.Tx{ 21 Body: &types.TxBody{ 22 Account: sender.ID(), 23 Amount: types.StakingMinimum.Bytes(), 24 Payload: []byte(`{"Name":"v1stake"}`), 25 }, 26 } 27 minplusmin := new(big.Int).Add(types.StakingMinimum, types.StakingMinimum) 28 sender.AddBalance(minplusmin) 29 30 ci, err := ValidateSystemTx(sender.ID(), tx.GetBody(), sender, scs, 0) 31 _, err = staking(tx.Body, sender, receiver, scs, 0, ci) 32 assert.NoError(t, err, "staking failed") 33 assert.Equal(t, sender.Balance(), types.StakingMinimum, "sender.Balance() should be 0 after staking") 34 saved, err := getStaking(scs, tx.Body.Account) 35 assert.Equal(t, types.StakingMinimum.Bytes(), saved.Amount, "saved staking value") 36 total, err := getStakingTotal(scs) 37 assert.Equal(t, types.StakingMinimum, total, "total value") 38 39 tx.Body.Payload = []byte(`{"Name":"v1unstake"}`) 40 ci, err = ValidateSystemTx(sender.ID(), tx.GetBody(), sender, scs, StakingDelay-1) 41 assert.Equal(t, err, types.ErrLessTimeHasPassed, "should be return ErrLessTimeHasPassed") 42 43 ci, err = ValidateSystemTx(sender.ID(), tx.GetBody(), sender, scs, StakingDelay) 44 assert.NoError(t, err, "should be success") 45 _, err = unstaking(tx.Body, sender, receiver, scs, StakingDelay, ci) 46 assert.NoError(t, err, "should be success") 47 assert.Equal(t, sender.Balance(), minplusmin, "sender.Balance() cacluation failed") 48 saved, err = getStaking(scs, tx.Body.Account) 49 assert.Equal(t, new(big.Int).SetUint64(0).Bytes(), saved.Amount, "saved staking value") 50 total, err = getStakingTotal(scs) 51 assert.NoError(t, err, "should be success") 52 assert.Equal(t, new(big.Int).SetUint64(0), total, "total value") 53 } 54 55 func TestStaking1Unstaking2(t *testing.T) { 56 scs, sender, receiver := initTest(t) 57 defer deinitTest() 58 const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4" 59 tx := &types.Tx{ 60 Body: &types.TxBody{ 61 Account: sender.ID(), 62 Amount: types.StakingMinimum.Bytes(), 63 Payload: []byte(`{"Name":"v1stake"}`), 64 }, 65 } 66 sender.AddBalance(types.MaxAER) 67 68 ci, err := ValidateSystemTx(sender.ID(), tx.GetBody(), sender, scs, 0) 69 _, err = staking(tx.Body, sender, receiver, scs, 0, ci) 70 assert.Equal(t, err, nil, "staking failed") 71 assert.Equal(t, sender.Balance().Bytes(), new(big.Int).Sub(types.MaxAER, types.StakingMinimum).Bytes(), 72 "sender.Balance() should be 'MaxAER - StakingMin' after staking") 73 74 tx.Body.Payload = []byte(`{"Name":"v1unstake"}`) 75 _, err = ValidateSystemTx(sender.ID(), tx.GetBody(), sender, scs, StakingDelay-1) 76 assert.Equal(t, err, types.ErrLessTimeHasPassed, "should be return ErrLessTimeHasPassed") 77 78 tx.Body.Amount = new(big.Int).Add(types.StakingMinimum, types.StakingMinimum).Bytes() 79 _, err = ValidateSystemTx(sender.ID(), tx.GetBody(), sender, scs, StakingDelay) 80 assert.Error(t, err, "should return exceed error") 81 } 82 83 func TestUnstakingError(t *testing.T) { 84 initTest(t) 85 defer deinitTest() 86 const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4" 87 scs, err := cdb.GetStateDB().OpenContractStateAccount(types.ToAccountID([]byte("aergo.system"))) 88 assert.Equal(t, err, nil, "could not open contract state") 89 90 account, err := types.DecodeAddress(testSender) 91 assert.Equal(t, err, nil, "could not decode test address") 92 93 tx := &types.Tx{ 94 Body: &types.TxBody{ 95 Account: account, 96 Amount: types.StakingMinimum.Bytes(), 97 Payload: []byte(`{"Name":"v1unstake"}`), 98 }, 99 } 100 sender, err := sdb.GetAccountStateV(tx.Body.Account) 101 assert.NoError(t, err, "could not get test address state") 102 receiver, err := sdb.GetAccountStateV(tx.Body.Recipient) 103 assert.NoError(t, err, "could not get test address state") 104 sender.AddBalance(types.MaxAER) 105 106 _, err = ExecuteSystemTx(scs, tx.Body, sender, receiver, 0) 107 assert.EqualError(t, types.ErrMustStakeBeforeUnstake, err.Error(), "should be success") 108 }