2018-07-08 22:09:07 +00:00
|
|
|
package paychan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-07-13 13:27:55 +00:00
|
|
|
//"github.com/stretchr/testify/assert"
|
2018-07-13 15:22:58 +00:00
|
|
|
|
|
|
|
abci "github.com/tendermint/abci/types"
|
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
|
|
|
"github.com/tendermint/tmlibs/log"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-07-08 22:09:07 +00:00
|
|
|
)
|
|
|
|
|
2018-07-13 15:22:58 +00:00
|
|
|
// GetPaychan
|
|
|
|
// - gets a paychan if it exists, and not if it doesn't
|
|
|
|
// setPaychan
|
|
|
|
// - sets a paychan
|
|
|
|
// CreatePaychan
|
|
|
|
// - creates a paychan under normal conditions
|
|
|
|
// ClosePaychan
|
|
|
|
// - closes a paychan under normal conditions
|
|
|
|
// GetPaychans
|
|
|
|
// paychanKey
|
|
|
|
|
|
|
|
func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) {
|
|
|
|
// create db
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
// create keys
|
|
|
|
authKey := sdk.NewKVStoreKey("authkey")
|
|
|
|
paychanKey := sdk.NewKVStoreKey("paychankey")
|
|
|
|
// create new multistore around db
|
|
|
|
ms := store.NewCommitMultiStore(db) // DB handle plus store key maps
|
|
|
|
// register separate stores in the multistore
|
|
|
|
ms.MountStoreWithDB(authKey, sdk.StoreTypeIAVL, db) // sets store key map
|
|
|
|
ms.MountStoreWithDB(paychanKey, sdk.StoreTypeIAVL, db)
|
|
|
|
ms.LoadLatestVersion()
|
|
|
|
|
|
|
|
return ms, authKey, paychanKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupCodec() *wire.Codec {
|
|
|
|
cdc := wire.NewCodec()
|
|
|
|
auth.RegisterBaseAccount(cdc)
|
|
|
|
// TODO might need to register paychan struct
|
|
|
|
return cdc
|
|
|
|
}
|
|
|
|
|
2018-07-08 22:09:07 +00:00
|
|
|
func TestKeeper(t *testing.T) {
|
2018-07-13 15:22:58 +00:00
|
|
|
// Setup
|
2018-07-15 11:41:55 +00:00
|
|
|
|
2018-07-13 15:22:58 +00:00
|
|
|
// create multistore and key
|
|
|
|
ms, authKey, paychanKey := setupMultiStore()
|
|
|
|
|
|
|
|
// create and initialise codec(s)
|
|
|
|
cdc := setupCodec()
|
|
|
|
|
|
|
|
// create context
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
|
|
|
|
|
|
|
|
// create accountMapper
|
|
|
|
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
|
|
|
|
|
|
|
|
// create coinkeeper
|
|
|
|
coinKeeper := bank.NewKeeper(accountMapper)
|
|
|
|
|
|
|
|
// create keeper
|
|
|
|
paychanKeeper := NewKeeper(cdc, paychanKey, coinKeeper)
|
|
|
|
|
|
|
|
// Test no paychans exist
|
|
|
|
_, exists := paychanKeeper.GetPaychan(ctx, sdk.Address{}, sdk.Address{}, 0)
|
|
|
|
if exists {
|
|
|
|
t.Error("payment channel found when none exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test paychan can be set and get
|
|
|
|
p := Paychan{
|
2018-07-15 11:41:55 +00:00
|
|
|
Sender: sdk.Address([]byte("senderAddress")),
|
|
|
|
Receiver: sdk.Address([]byte("receiverAddress")),
|
|
|
|
Id: 0,
|
|
|
|
Balance: sdk.Coins{{"KVA", 100}},
|
2018-07-13 15:22:58 +00:00
|
|
|
}
|
|
|
|
paychanKeeper.setPaychan(ctx, p)
|
|
|
|
|
2018-07-15 11:41:55 +00:00
|
|
|
_, exists = paychanKeeper.GetPaychan(ctx, p.Sender, p.Receiver, p.Id)
|
2018-07-13 15:22:58 +00:00
|
|
|
if !exists {
|
|
|
|
t.Error("payment channel not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test create paychan under normal conditions
|
|
|
|
senderAddress := sdk.Address([]byte("senderAddress"))
|
|
|
|
senderFunds := sdk.Coins{{"KVA", 100}}
|
|
|
|
receiverAddress := sdk.Address([]byte("receiverAddress"))
|
|
|
|
balance := sdk.Coins{{"KVA", 10}}
|
|
|
|
|
|
|
|
coinKeeper.SetCoins(ctx, senderAddress, senderFunds)
|
|
|
|
|
|
|
|
_, err := paychanKeeper.CreatePaychan(ctx, senderAddress, receiverAddress, balance)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("unexpected error created payment channel", err)
|
|
|
|
}
|
|
|
|
|
2018-07-15 11:41:55 +00:00
|
|
|
p, exists = paychanKeeper.GetPaychan(ctx, senderAddress, receiverAddress, 0)
|
|
|
|
if !exists {
|
|
|
|
t.Error("payment channel missing")
|
|
|
|
}
|
|
|
|
if !p.Balance.IsEqual(balance) {
|
|
|
|
t.Error("payment channel balance incorrect", p.Balance, balance)
|
2018-07-13 15:22:58 +00:00
|
|
|
}
|
|
|
|
expectedNewSenderFunds := senderFunds.Minus(balance)
|
|
|
|
if !coinKeeper.GetCoins(ctx, senderAddress).IsEqual(expectedNewSenderFunds) {
|
|
|
|
t.Error("sender has incorrect balance after paychan creation")
|
|
|
|
}
|
2018-07-08 22:09:07 +00:00
|
|
|
|
2018-07-15 11:41:55 +00:00
|
|
|
// Test close paychan under normal conditions
|
|
|
|
senderFunds = coinKeeper.GetCoins(ctx, senderAddress)
|
|
|
|
receiverAmount := sdk.Coins{{"KVA", 9}}
|
|
|
|
_, err = paychanKeeper.ClosePaychan(ctx, senderAddress, receiverAddress, 0, receiverAmount)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("unexpected error closing payment channel", err)
|
|
|
|
}
|
|
|
|
// paychan shouldn't exist
|
|
|
|
_, exists = paychanKeeper.GetPaychan(ctx, senderAddress, receiverAddress, 0)
|
|
|
|
if exists {
|
|
|
|
t.Error("payment channel should not exist")
|
|
|
|
}
|
|
|
|
// sender's funds should have increased
|
|
|
|
expectedNewSenderFunds = senderFunds.Plus(balance.Minus(receiverAmount))
|
|
|
|
if !coinKeeper.GetCoins(ctx, senderAddress).IsEqual(expectedNewSenderFunds) {
|
|
|
|
t.Error("sender has incorrect balance after paychan creation", expectedNewSenderFunds)
|
|
|
|
}
|
|
|
|
// receiver's funds should have increased
|
|
|
|
expectedNewReceiverFunds := receiverAmount // started at zero
|
|
|
|
if !coinKeeper.GetCoins(ctx, receiverAddress).IsEqual(expectedNewReceiverFunds) {
|
|
|
|
t.Error("receiver has incorrect balance after paychan creation")
|
|
|
|
}
|
2018-07-13 15:22:58 +00:00
|
|
|
|
2018-07-15 11:41:55 +00:00
|
|
|
}
|