-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from binance-chain/develop
validate minitoken send
- Loading branch information
Showing
9 changed files
with
218 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
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
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,124 @@ | ||
package bank | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
|
||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
) | ||
|
||
func setup() (sdk.Context, sdk.Handler, BaseKeeper, auth.AccountKeeper) { | ||
ms, authKey := setupMultiStore() | ||
|
||
cdc := codec.New() | ||
auth.RegisterBaseAccount(cdc) | ||
accountCache := getAccountCache(cdc, ms, authKey) | ||
|
||
ctx := sdk.NewContext(ms, abci.Header{}, sdk.RunTxModeDeliver, log.NewNopLogger()).WithAccountCache(accountCache) | ||
accountKeeper := auth.NewAccountKeeper(cdc, authKey, auth.ProtoBaseAccount) | ||
bankKeeper := NewBaseKeeper(accountKeeper) | ||
handler := NewHandler(bankKeeper) | ||
sdk.UpgradeMgr.AddUpgradeHeight(sdk.BEP8, int64(-1)) | ||
return ctx, handler, bankKeeper, accountKeeper | ||
} | ||
|
||
func TestHandleSendToken(t *testing.T) { | ||
ctx, handler, bankKeeper, accountKeeper := setup() | ||
|
||
ctx = ctx.WithValue(baseapp.TxHashKey, "000") | ||
addr := sdk.AccAddress([]byte("addr1")) | ||
addr2 := sdk.AccAddress([]byte("addr2")) | ||
acc := accountKeeper.NewAccountWithAddress(ctx, addr) | ||
acc2 := accountKeeper.NewAccountWithAddress(ctx, addr2) | ||
|
||
accountKeeper.SetAccount(ctx, acc) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{})) | ||
|
||
//transfer BEP2 token Successfully | ||
bankKeeper.SetCoins(ctx, addr, sdk.Coins{sdk.NewCoin("NNB-000", 100)}) | ||
|
||
msg := createSendMsg(acc.GetAddress(), acc2.GetAddress(), sdk.Coins{sdk.NewCoin("NNB-000", 60)}) | ||
sdkResult := handler(ctx, msg) | ||
require.Equal(t, true, sdkResult.Code.IsOK()) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{sdk.NewCoin("NNB-000", 40)})) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{sdk.NewCoin("NNB-000", 60)})) | ||
} | ||
|
||
func TestHandleSendMiniToken(t *testing.T) { | ||
ctx, handler, bankKeeper, accountKeeper := setup() | ||
|
||
ctx = ctx.WithValue(baseapp.TxHashKey, "000") | ||
addr := sdk.AccAddress([]byte("addr1")) | ||
addr2 := sdk.AccAddress([]byte("addr2")) | ||
addr3 := sdk.AccAddress([]byte("addr3")) | ||
acc := accountKeeper.NewAccountWithAddress(ctx, addr) | ||
acc2 := accountKeeper.NewAccountWithAddress(ctx, addr2) | ||
|
||
accountKeeper.SetAccount(ctx, acc) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{})) | ||
|
||
//Transfer Mini token | ||
//Fail to Transfer with value < 1e8 | ||
MiniTokenFoo := "foocoin-000M" | ||
bankKeeper.SetCoins(ctx, addr, sdk.Coins{sdk.NewCoin(MiniTokenFoo, 10e8)}) | ||
msg := createSendMsg(acc.GetAddress(), acc2.GetAddress(), sdk.Coins{sdk.NewCoin(MiniTokenFoo, 2)}) | ||
sdkResult := handler(ctx, msg) | ||
require.Equal(t, false, sdkResult.Code.IsOK()) | ||
require.Contains(t, sdkResult.Log, "transfer amount is too small") | ||
|
||
//Success with amount >= 1e8 | ||
msg = createSendMsg(acc.GetAddress(), acc2.GetAddress(), sdk.Coins{sdk.NewCoin(MiniTokenFoo, 1e8)}) | ||
sdkResult = handler(ctx, msg) | ||
require.Equal(t, true, sdkResult.Code.IsOK()) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{sdk.NewCoin(MiniTokenFoo, 9e8)})) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{sdk.NewCoin(MiniTokenFoo, 1e8)})) | ||
|
||
//Fail to Multisend | ||
MiniTokenBar := "barcoin-000M" | ||
bankKeeper.SetCoins(ctx, addr2, sdk.Coins{sdk.NewCoin(MiniTokenBar, 10), sdk.NewCoin(MiniTokenFoo, 1e8)}) | ||
|
||
inputs := []Input{ | ||
NewInput(addr, sdk.Coins{sdk.NewCoin(MiniTokenFoo, 3e8)}), | ||
NewInput(addr2, sdk.Coins{sdk.NewCoin(MiniTokenBar, 3), sdk.NewCoin(MiniTokenFoo, 1e8)}), | ||
} | ||
|
||
outputs := []Output{ | ||
NewOutput(addr, sdk.Coins{sdk.NewCoin(MiniTokenBar, 1)}), | ||
NewOutput(addr3, sdk.Coins{sdk.NewCoin(MiniTokenBar, 2), sdk.NewCoin(MiniTokenFoo, 4e8)}), | ||
} | ||
msg = NewMsgSend(inputs, outputs) | ||
sdkResult = handler(ctx, msg) | ||
require.Equal(t, false, sdkResult.Code.IsOK()) | ||
require.Contains(t, sdkResult.Log, "transfer amount is too small") | ||
|
||
// | ||
//Success with all balance | ||
inputs = []Input{ | ||
NewInput(addr, sdk.Coins{sdk.NewCoin(MiniTokenFoo, 3e8)}), | ||
NewInput(addr2, sdk.Coins{sdk.NewCoin(MiniTokenBar, 10), sdk.NewCoin(MiniTokenFoo, 1e8)}), | ||
} | ||
|
||
outputs = []Output{ | ||
NewOutput(addr, sdk.Coins{sdk.NewCoin(MiniTokenBar, 4)}), | ||
NewOutput(addr3, sdk.Coins{sdk.NewCoin(MiniTokenBar, 6), sdk.NewCoin(MiniTokenFoo, 4e8)}), | ||
} | ||
msg = NewMsgSend(inputs, outputs) | ||
sdkResult = handler(ctx, msg) | ||
require.Equal(t, true, sdkResult.Code.IsOK()) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{sdk.NewCoin(MiniTokenBar, 4), sdk.NewCoin(MiniTokenFoo, 6e8)})) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{})) | ||
require.True(t, bankKeeper.GetCoins(ctx, addr3).IsEqual(sdk.Coins{sdk.NewCoin(MiniTokenBar, 6), sdk.NewCoin(MiniTokenFoo, 4e8)})) | ||
} | ||
|
||
func createSendMsg(from sdk.AccAddress, to sdk.AccAddress, coins sdk.Coins) sdk.Msg { | ||
input := NewInput(from, coins) | ||
output := NewOutput(to, coins) | ||
msg := NewMsgSend([]Input{input}, []Output{output}) | ||
return msg | ||
} |
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
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
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,74 @@ | ||
package bank | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
) | ||
|
||
const ( | ||
MiniTokenSymbolSuffixLen = 4 // probably enough. if it collides (unlikely) the issuer can just use another tx. | ||
MiniTokenSymbolMSuffix = "M" | ||
MiniTokenMinExecutionAmount int64 = 100000000 // 1 with 8 decimal digits | ||
) | ||
|
||
func checkAndValidateMiniTokenCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, coins sdk.Coins) sdk.Error { | ||
var err sdk.Error | ||
for _, coin := range coins { | ||
if isMiniTokenSymbol(coin.Denom) { | ||
err = validateMiniTokenAmount(ctx, am, addr, coin) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func isMiniTokenSymbol(symbol string) bool { | ||
parts, err := splitSuffixedTokenSymbol(symbol) | ||
if err != nil { | ||
return false | ||
} | ||
suffixPart := parts[1] | ||
|
||
return len(suffixPart) == MiniTokenSymbolSuffixLen && strings.HasSuffix(suffixPart, MiniTokenSymbolMSuffix) | ||
} | ||
|
||
func validateMiniTokenAmount(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, coin sdk.Coin) sdk.Error { | ||
if MiniTokenMinExecutionAmount <= coin.Amount { | ||
return nil | ||
} | ||
|
||
coins := getCoins(ctx, am, addr) | ||
balance := coins.AmountOf(coin.Denom) | ||
if balance < coin.Amount { | ||
return sdk.ErrInsufficientCoins("not enough token to send") | ||
} | ||
|
||
useAllBalance := balance == coin.Amount | ||
|
||
if !useAllBalance { | ||
return sdk.ErrInvalidCoins(fmt.Sprintf("transfer amount is too small, the min amount is %d or total account balance", | ||
MiniTokenMinExecutionAmount)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func splitSuffixedTokenSymbol(suffixed string) ([]string, error) { | ||
|
||
split := strings.SplitN(suffixed, "-", 2) | ||
|
||
if len(split) != 2 { | ||
return nil, fmt.Errorf("suffixed token symbol must contain a hyphen ('-')") | ||
} | ||
|
||
if strings.Contains(split[1], "-") { | ||
return nil, fmt.Errorf("suffixed token symbol must contain just one hyphen ('-')") | ||
} | ||
|
||
return split, nil | ||
} |