Skip to content

Commit

Permalink
test freezing coins
Browse files Browse the repository at this point in the history
  • Loading branch information
dev10 committed Aug 20, 2019
1 parent fe860d0 commit 580ca65
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.3.0
github.com/tendermint/go-amino v0.15.0
github.com/tendermint/tendermint v0.32.1
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec // indirect
Expand Down
15 changes: 12 additions & 3 deletions x/assetmanagement/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,19 @@ func (acc *CustomAccount) SetFrozenCoins(frozen sdk.Coins) error {
return nil
}

func AreAnyCoinsZero(coins *sdk.Coins) bool {
for _, coin := range *coins {
if sdk.NewInt(0).Equal(coin.Amount) {
return true
}
}
return false
}

// FreezeCoins freezes unfrozen coins for account according to input
func (acc *CustomAccount) FreezeCoins(coinsToFreeze sdk.Coins) error {
// Have enough coins to freeze?
if coinsToFreeze == nil || coinsToFreeze.Empty() || coinsToFreeze.IsAnyNegative() {
if coinsToFreeze == nil || coinsToFreeze.Empty() || coinsToFreeze.IsAnyNegative() || AreAnyCoinsZero(&coinsToFreeze) {
return sdk.ErrInvalidCoins("No coins chosen to freeze")
}

Expand All @@ -73,7 +82,7 @@ func (acc *CustomAccount) FreezeCoins(coinsToFreeze sdk.Coins) error {
}

// Freeze coins
if newBalance, success := currentCoins.SafeSub(coinsToFreeze); success {
if newBalance, isNegative := currentCoins.SafeSub(coinsToFreeze); !isNegative {
if err := acc.SetCoins(newBalance); err != nil {
return sdk.ErrInvalidCoins(fmt.Sprintf("failed to set coins: %s", err))
}
Expand Down Expand Up @@ -115,7 +124,7 @@ func (acc *CustomAccount) UnfreezeCoins(coinsToUnfreeze sdk.Coins) error {
currentCoins = currentCoins.Add(coinsToUnfreeze)
}

if newFrozenBalance, success := currentlyFrozen.SafeSub(coinsToUnfreeze); success {
if newFrozenBalance, isNegative := currentlyFrozen.SafeSub(coinsToUnfreeze); !isNegative {
if err := acc.SetFrozenCoins(newFrozenBalance); err != nil {
return sdk.ErrInvalidCoins(fmt.Sprintf("failed to set frozen coins: %s", err))
}
Expand Down
38 changes: 38 additions & 0 deletions x/assetmanagement/types/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package types

import (
"testing"

"github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func TestFreeze(t *testing.T) {
_, pub1, addr1 := KeyTestPubAddr()

// Not enough starting coins to move
account1 := NewCustomAccount(addr1, nil, nil, pub1, 1, 2)
err := account1.FreezeCoins(NewTestCoins("abc", 1))
require.NotNil(t, err)
err = account1.FreezeCoins(NewTestCoins("abc", 0))
require.NotNil(t, err)

// Not enough coins chosen
coinSymbol := "ab1"
coins := NewTestCoins(coinSymbol, 100)
frozenCoins := NewTestCoins(coinSymbol, 0)
account2 := NewCustomAccount(addr1, coins, frozenCoins, pub1, 1, 2)

err = account2.FreezeCoins(NewTestCoins(coinSymbol, 0))
require.NotNil(t, err)

// Too many coins to freeze
err = account2.FreezeCoins(NewTestCoins(coinSymbol, 101))
require.NotNil(t, err)

// Can freeze
err = account2.FreezeCoins(NewTestCoins(coinSymbol, 1))
require.Nil(t, err)
require.Equal(t, true, types.NewInt(99).Equal(account2.Coins.AmountOf(coinSymbol)))
require.Equal(t, true, types.NewInt(1).Equal(account2.FrozenCoins.AmountOf(coinSymbol)))
}
23 changes: 23 additions & 0 deletions x/assetmanagement/types/test_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// nolint noalias
package types

import (
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// coins to more than cover the fee
func NewTestCoins(symbol string, amount int64) sdk.Coins {
return sdk.Coins{
sdk.NewInt64Coin(symbol, amount),
}
}

func KeyTestPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) {
key := secp256k1.GenPrivKey()
pub := key.PubKey()
addr := sdk.AccAddress(pub.Address())
return key, pub, addr
}

0 comments on commit 580ca65

Please sign in to comment.