forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(x/accounts/defaults/lockup): Add lockup unit tests (cosmos#2…
- Loading branch information
1 parent
25e99c5
commit c349885
Showing
8 changed files
with
880 additions
and
13 deletions.
There are no files selected for viewing
235 changes: 235 additions & 0 deletions
235
x/accounts/defaults/lockup/continuous_locking_account_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
package lockup | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"cosmossdk.io/core/header" | ||
"cosmossdk.io/core/store" | ||
"cosmossdk.io/log" | ||
"cosmossdk.io/math" | ||
lockuptypes "cosmossdk.io/x/accounts/defaults/lockup/types" | ||
) | ||
|
||
func setupContinousAccount(t *testing.T, ctx context.Context, ss store.KVStoreService) *ContinuousLockingAccount { | ||
t.Helper() | ||
deps := makeMockDependencies(ss) | ||
owner := "owner" | ||
|
||
acc, err := NewContinuousLockingAccount(deps) | ||
require.NoError(t, err) | ||
_, err = acc.Init(ctx, &lockuptypes.MsgInitLockupAccount{ | ||
Owner: owner, | ||
EndTime: time.Now().Add(time.Minute * 2), | ||
StartTime: time.Now(), | ||
}) | ||
require.NoError(t, err) | ||
|
||
return acc | ||
} | ||
|
||
func TestContinousAccountDelegate(t *testing.T) { | ||
ctx, ss := newMockContext(t) | ||
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{ | ||
Time: time.Now(), | ||
}) | ||
|
||
acc := setupContinousAccount(t, sdkCtx, ss) | ||
_, err := acc.Delegate(sdkCtx, &lockuptypes.MsgDelegate{ | ||
Sender: "owner", | ||
ValidatorAddress: "val_address", | ||
Amount: sdk.NewCoin("test", math.NewInt(1)), | ||
}) | ||
require.NoError(t, err) | ||
|
||
delLocking, err := acc.DelegatedLocking.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delLocking.Equal(math.NewInt(1))) | ||
|
||
startTime, err := acc.StartTime.Get(sdkCtx) | ||
require.NoError(t, err) | ||
|
||
// Update context time to unlocked half of the original locking amount | ||
sdkCtx = sdkCtx.WithHeaderInfo(header.Info{ | ||
Time: startTime.Add(time.Minute * 1), | ||
}) | ||
|
||
_, err = acc.Delegate(sdkCtx, &lockuptypes.MsgDelegate{ | ||
Sender: "owner", | ||
ValidatorAddress: "val_address", | ||
Amount: sdk.NewCoin("test", math.NewInt(5)), | ||
}) | ||
require.NoError(t, err) | ||
|
||
delLocking, err = acc.DelegatedLocking.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delLocking.Equal(math.NewInt(5))) | ||
|
||
delFree, err := acc.DelegatedFree.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delFree.Equal(math.NewInt(1))) | ||
} | ||
|
||
func TestContinousAccountUndelegate(t *testing.T) { | ||
ctx, ss := newMockContext(t) | ||
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{ | ||
Time: time.Now(), | ||
}) | ||
|
||
acc := setupContinousAccount(t, sdkCtx, ss) | ||
// Delegate first | ||
_, err := acc.Delegate(sdkCtx, &lockuptypes.MsgDelegate{ | ||
Sender: "owner", | ||
ValidatorAddress: "val_address", | ||
Amount: sdk.NewCoin("test", math.NewInt(1)), | ||
}) | ||
require.NoError(t, err) | ||
|
||
delLocking, err := acc.DelegatedLocking.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delLocking.Equal(math.NewInt(1))) | ||
|
||
// Undelegate | ||
_, err = acc.Undelegate(sdkCtx, &lockuptypes.MsgUndelegate{ | ||
Sender: "owner", | ||
ValidatorAddress: "val_address", | ||
Amount: sdk.NewCoin("test", math.NewInt(1)), | ||
}) | ||
require.NoError(t, err) | ||
|
||
delLocking, err = acc.DelegatedLocking.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delLocking.Equal(math.ZeroInt())) | ||
|
||
startTime, err := acc.StartTime.Get(sdkCtx) | ||
require.NoError(t, err) | ||
|
||
// Update context time to unlocked half of the original locking amount | ||
sdkCtx = sdkCtx.WithHeaderInfo(header.Info{ | ||
Time: startTime.Add(time.Minute * 1), | ||
}) | ||
|
||
_, err = acc.Delegate(sdkCtx, &lockuptypes.MsgDelegate{ | ||
Sender: "owner", | ||
ValidatorAddress: "val_address", | ||
Amount: sdk.NewCoin("test", math.NewInt(6)), | ||
}) | ||
require.NoError(t, err) | ||
|
||
delLocking, err = acc.DelegatedLocking.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delLocking.Equal(math.NewInt(5))) | ||
|
||
delFree, err := acc.DelegatedFree.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delFree.Equal(math.NewInt(1))) | ||
|
||
// Undelegate | ||
_, err = acc.Undelegate(sdkCtx, &lockuptypes.MsgUndelegate{ | ||
Sender: "owner", | ||
ValidatorAddress: "val_address", | ||
Amount: sdk.NewCoin("test", math.NewInt(4)), | ||
}) | ||
require.NoError(t, err) | ||
|
||
delLocking, err = acc.DelegatedLocking.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delLocking.Equal(math.NewInt(2))) | ||
|
||
delFree, err = acc.DelegatedFree.Get(ctx, "test") | ||
require.NoError(t, err) | ||
require.True(t, delFree.Equal(math.ZeroInt())) | ||
} | ||
|
||
func TestContinousAccountSendCoins(t *testing.T) { | ||
ctx, ss := newMockContext(t) | ||
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{ | ||
Time: time.Now(), | ||
}) | ||
|
||
acc := setupContinousAccount(t, sdkCtx, ss) | ||
_, err := acc.SendCoins(sdkCtx, &lockuptypes.MsgSend{ | ||
Sender: "owner", | ||
ToAddress: "receiver", | ||
Amount: sdk.NewCoins(sdk.NewCoin("test", math.NewInt(5))), | ||
}) | ||
require.Error(t, err) | ||
|
||
startTime, err := acc.StartTime.Get(sdkCtx) | ||
require.NoError(t, err) | ||
|
||
// Update context time to unlocked half of the original locking amount | ||
sdkCtx = sdkCtx.WithHeaderInfo(header.Info{ | ||
Time: startTime.Add(time.Minute * 1), | ||
}) | ||
|
||
_, err = acc.SendCoins(sdkCtx, &lockuptypes.MsgSend{ | ||
Sender: "owner", | ||
ToAddress: "receiver", | ||
Amount: sdk.NewCoins(sdk.NewCoin("test", math.NewInt(5))), | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestContinousAccountWithdrawUnlockedCoins(t *testing.T) { | ||
ctx, ss := newMockContext(t) | ||
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{ | ||
Time: time.Now(), | ||
}) | ||
|
||
acc := setupContinousAccount(t, sdkCtx, ss) | ||
_, err := acc.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{ | ||
Withdrawer: "owner", | ||
ToAddress: "receiver", | ||
Denoms: []string{"test"}, | ||
}) | ||
require.Error(t, err) | ||
|
||
startTime, err := acc.StartTime.Get(sdkCtx) | ||
require.NoError(t, err) | ||
|
||
// Update context time to unlocked half of the original locking amount | ||
sdkCtx = sdkCtx.WithHeaderInfo(header.Info{ | ||
Time: startTime.Add(time.Minute * 1), | ||
}) | ||
|
||
_, err = acc.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{ | ||
Withdrawer: "owner", | ||
ToAddress: "receiver", | ||
Denoms: []string{"test"}, | ||
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestContinousAccountGetLockCoinInfo(t *testing.T) { | ||
ctx, ss := newMockContext(t) | ||
sdkCtx := sdk.NewContext(nil, true, log.NewNopLogger()).WithContext(ctx).WithHeaderInfo(header.Info{ | ||
Time: time.Now(), | ||
}) | ||
|
||
acc := setupContinousAccount(t, sdkCtx, ss) | ||
|
||
unlocked, locked, err := acc.GetLockCoinsInfo(sdkCtx, time.Now()) | ||
require.NoError(t, err) | ||
require.True(t, unlocked.AmountOf("test").Equal(math.ZeroInt())) | ||
require.True(t, locked.AmountOf("test").Equal(math.NewInt(10))) | ||
|
||
startTime, err := acc.StartTime.Get(sdkCtx) | ||
require.NoError(t, err) | ||
|
||
// unlocked half locked token | ||
unlocked, locked, err = acc.GetLockCoinsInfo(sdkCtx, startTime.Add(time.Minute*1)) | ||
require.NoError(t, err) | ||
require.True(t, unlocked.AmountOf("test").Equal(math.NewInt(5))) | ||
require.True(t, locked.AmountOf("test").Equal(math.NewInt(5))) | ||
|
||
// unlocked full locked token | ||
unlocked, locked, err = acc.GetLockCoinsInfo(sdkCtx, startTime.Add(time.Minute*2)) | ||
require.NoError(t, err) | ||
require.True(t, unlocked.AmountOf("test").Equal(math.NewInt(10))) | ||
require.True(t, locked.AmountOf("test").Equal(math.ZeroInt())) | ||
} |
Oops, something went wrong.