mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
add initial keeper tests
This commit is contained in:
parent
469dd84a32
commit
97a7f79ed8
@ -3,90 +3,199 @@ package paychan
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
//"github.com/stretchr/testify/assert"
|
//"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// example from x/bank
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
func TestKeeper(t *testing.T) {
|
func TestKeeper(t *testing.T) {
|
||||||
// ms, authKey := setupMultiStore()
|
// Setup
|
||||||
|
// create multistore and key
|
||||||
|
ms, authKey, paychanKey := setupMultiStore()
|
||||||
|
|
||||||
// cdc := wire.NewCodec()
|
// create and initialise codec(s)
|
||||||
// auth.RegisterBaseAccount(cdc)
|
cdc := setupCodec()
|
||||||
|
|
||||||
// ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
|
// create context
|
||||||
// accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
|
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
|
||||||
// coinKeeper := NewKeeper(accountMapper)
|
|
||||||
|
|
||||||
// addr := sdk.Address([]byte("addr1"))
|
// create accountMapper
|
||||||
// addr2 := sdk.Address([]byte("addr2"))
|
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
|
||||||
// addr3 := sdk.Address([]byte("addr3"))
|
|
||||||
// acc := accountMapper.NewAccountWithAddress(ctx, addr)
|
|
||||||
|
|
||||||
// // Test GetCoins/SetCoins
|
// create coinkeeper
|
||||||
// accountMapper.SetAccount(ctx, acc)
|
coinKeeper := bank.NewKeeper(accountMapper)
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{}))
|
|
||||||
|
|
||||||
// coinKeeper.SetCoins(ctx, addr, sdk.Coins{{"foocoin", 10}})
|
// create keeper
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 10}}))
|
paychanKeeper := NewKeeper(cdc, paychanKey, coinKeeper)
|
||||||
|
|
||||||
// // Test HasCoins
|
// Test no paychans exist
|
||||||
// assert.True(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"foocoin", 10}}))
|
_, exists := paychanKeeper.GetPaychan(ctx, sdk.Address{}, sdk.Address{}, 0)
|
||||||
// assert.True(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"foocoin", 5}}))
|
if exists {
|
||||||
// assert.False(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"foocoin", 15}}))
|
t.Error("payment channel found when none exist")
|
||||||
// assert.False(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"barcoin", 5}}))
|
}
|
||||||
|
|
||||||
// // Test AddCoins
|
// Test paychan can be set and get
|
||||||
// coinKeeper.AddCoins(ctx, addr, sdk.Coins{{"foocoin", 15}})
|
p := Paychan{
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 25}}))
|
sender: sdk.Address([]byte("senderAddress")),
|
||||||
|
receiver: sdk.Address([]byte("receiverAddress")),
|
||||||
|
id: 0,
|
||||||
|
balance: sdk.Coins{{"KVA", 100}},
|
||||||
|
}
|
||||||
|
paychanKeeper.setPaychan(ctx, p)
|
||||||
|
|
||||||
// coinKeeper.AddCoins(ctx, addr, sdk.Coins{{"barcoin", 15}})
|
_, exists = paychanKeeper.GetPaychan(ctx, p.sender, p.receiver, p.id)
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 15}, {"foocoin", 25}}))
|
if !exists {
|
||||||
|
t.Error("payment channel not found")
|
||||||
|
}
|
||||||
|
|
||||||
// // Test SubtractCoins
|
// Test create paychan under normal conditions
|
||||||
// coinKeeper.SubtractCoins(ctx, addr, sdk.Coins{{"foocoin", 10}})
|
senderAddress := sdk.Address([]byte("senderAddress"))
|
||||||
// coinKeeper.SubtractCoins(ctx, addr, sdk.Coins{{"barcoin", 5}})
|
senderFunds := sdk.Coins{{"KVA", 100}}
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 10}, {"foocoin", 15}}))
|
receiverAddress := sdk.Address([]byte("receiverAddress"))
|
||||||
|
balance := sdk.Coins{{"KVA", 10}}
|
||||||
|
|
||||||
// coinKeeper.SubtractCoins(ctx, addr, sdk.Coins{{"barcoin", 11}})
|
coinKeeper.SetCoins(ctx, senderAddress, senderFunds)
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 10}, {"foocoin", 15}}))
|
|
||||||
|
|
||||||
// coinKeeper.SubtractCoins(ctx, addr, sdk.Coins{{"barcoin", 10}})
|
_, err := paychanKeeper.CreatePaychan(ctx, senderAddress, receiverAddress, balance)
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 15}}))
|
if err != nil {
|
||||||
// assert.False(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"barcoin", 1}}))
|
t.Error("unexpected error created payment channel", err)
|
||||||
|
}
|
||||||
|
|
||||||
// // Test SendCoins
|
p, _ = paychanKeeper.GetPaychan(ctx, senderAddress, receiverAddress, 0)
|
||||||
// coinKeeper.SendCoins(ctx, addr, addr2, sdk.Coins{{"foocoin", 5}})
|
if !p.balance.IsEqual(balance) {
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 10}}))
|
t.Error("payment channel balance incorrect", p.balance, balance)
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"foocoin", 5}}))
|
}
|
||||||
|
expectedNewSenderFunds := senderFunds.Minus(balance)
|
||||||
// _, err2 := coinKeeper.SendCoins(ctx, addr, addr2, sdk.Coins{{"foocoin", 50}})
|
if !coinKeeper.GetCoins(ctx, senderAddress).IsEqual(expectedNewSenderFunds) {
|
||||||
// assert.Implements(t, (*sdk.Error)(nil), err2)
|
t.Error("sender has incorrect balance after paychan creation")
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 10}}))
|
}
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"foocoin", 5}}))
|
|
||||||
|
|
||||||
// coinKeeper.AddCoins(ctx, addr, sdk.Coins{{"barcoin", 30}})
|
|
||||||
// coinKeeper.SendCoins(ctx, addr, addr2, sdk.Coins{{"barcoin", 10}, {"foocoin", 5}})
|
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 20}, {"foocoin", 5}}))
|
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"barcoin", 10}, {"foocoin", 10}}))
|
|
||||||
|
|
||||||
// // Test InputOutputCoins
|
|
||||||
// input1 := NewInput(addr2, sdk.Coins{{"foocoin", 2}})
|
|
||||||
// output1 := NewOutput(addr, sdk.Coins{{"foocoin", 2}})
|
|
||||||
// coinKeeper.InputOutputCoins(ctx, []Input{input1}, []Output{output1})
|
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 20}, {"foocoin", 7}}))
|
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"barcoin", 10}, {"foocoin", 8}}))
|
|
||||||
|
|
||||||
// inputs := []Input{
|
|
||||||
// NewInput(addr, sdk.Coins{{"foocoin", 3}}),
|
|
||||||
// NewInput(addr2, sdk.Coins{{"barcoin", 3}, {"foocoin", 2}}),
|
|
||||||
// }
|
|
||||||
|
|
||||||
// outputs := []Output{
|
|
||||||
// NewOutput(addr, sdk.Coins{{"barcoin", 1}}),
|
|
||||||
// NewOutput(addr3, sdk.Coins{{"barcoin", 2}, {"foocoin", 5}}),
|
|
||||||
// }
|
|
||||||
// coinKeeper.InputOutputCoins(ctx, inputs, outputs)
|
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 21}, {"foocoin", 4}}))
|
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"barcoin", 7}, {"foocoin", 6}}))
|
|
||||||
// assert.True(t, coinKeeper.GetCoins(ctx, addr3).IsEqual(sdk.Coins{{"barcoin", 2}, {"foocoin", 5}}))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// example from x/bank
|
||||||
|
|
||||||
|
//func TestKeeper(t *testing.T) {
|
||||||
|
// ms, authKey := setupMultiStore()
|
||||||
|
|
||||||
|
// cdc := wire.NewCodec()
|
||||||
|
// auth.RegisterBaseAccount(cdc)
|
||||||
|
|
||||||
|
// ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
|
||||||
|
// accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
|
||||||
|
// coinKeeper := NewKeeper(accountMapper)
|
||||||
|
|
||||||
|
// addr := sdk.Address([]byte("addr1"))
|
||||||
|
// addr2 := sdk.Address([]byte("addr2"))
|
||||||
|
// addr3 := sdk.Address([]byte("addr3"))
|
||||||
|
// acc := accountMapper.NewAccountWithAddress(ctx, addr)
|
||||||
|
|
||||||
|
// // Test GetCoins/SetCoins
|
||||||
|
// accountMapper.SetAccount(ctx, acc)
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{}))
|
||||||
|
|
||||||
|
// coinKeeper.SetCoins(ctx, addr, sdk.Coins{{"foocoin", 10}})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 10}}))
|
||||||
|
|
||||||
|
// // Test HasCoins
|
||||||
|
// assert.True(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"foocoin", 10}}))
|
||||||
|
// assert.True(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"foocoin", 5}}))
|
||||||
|
// assert.False(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"foocoin", 15}}))
|
||||||
|
// assert.False(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"barcoin", 5}}))
|
||||||
|
|
||||||
|
// // Test AddCoins
|
||||||
|
// coinKeeper.AddCoins(ctx, addr, sdk.Coins{{"foocoin", 15}})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 25}}))
|
||||||
|
|
||||||
|
// coinKeeper.AddCoins(ctx, addr, sdk.Coins{{"barcoin", 15}})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 15}, {"foocoin", 25}}))
|
||||||
|
|
||||||
|
// // Test SubtractCoins
|
||||||
|
// coinKeeper.SubtractCoins(ctx, addr, sdk.Coins{{"foocoin", 10}})
|
||||||
|
// coinKeeper.SubtractCoins(ctx, addr, sdk.Coins{{"barcoin", 5}})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 10}, {"foocoin", 15}}))
|
||||||
|
|
||||||
|
// coinKeeper.SubtractCoins(ctx, addr, sdk.Coins{{"barcoin", 11}})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 10}, {"foocoin", 15}}))
|
||||||
|
|
||||||
|
// coinKeeper.SubtractCoins(ctx, addr, sdk.Coins{{"barcoin", 10}})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 15}}))
|
||||||
|
// assert.False(t, coinKeeper.HasCoins(ctx, addr, sdk.Coins{{"barcoin", 1}}))
|
||||||
|
|
||||||
|
// // Test SendCoins
|
||||||
|
// coinKeeper.SendCoins(ctx, addr, addr2, sdk.Coins{{"foocoin", 5}})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 10}}))
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"foocoin", 5}}))
|
||||||
|
|
||||||
|
// _, err2 := coinKeeper.SendCoins(ctx, addr, addr2, sdk.Coins{{"foocoin", 50}})
|
||||||
|
// assert.Implements(t, (*sdk.Error)(nil), err2)
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"foocoin", 10}}))
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"foocoin", 5}}))
|
||||||
|
|
||||||
|
// coinKeeper.AddCoins(ctx, addr, sdk.Coins{{"barcoin", 30}})
|
||||||
|
// coinKeeper.SendCoins(ctx, addr, addr2, sdk.Coins{{"barcoin", 10}, {"foocoin", 5}})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 20}, {"foocoin", 5}}))
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"barcoin", 10}, {"foocoin", 10}}))
|
||||||
|
|
||||||
|
// // Test InputOutputCoins
|
||||||
|
// input1 := NewInput(addr2, sdk.Coins{{"foocoin", 2}})
|
||||||
|
// output1 := NewOutput(addr, sdk.Coins{{"foocoin", 2}})
|
||||||
|
// coinKeeper.InputOutputCoins(ctx, []Input{input1}, []Output{output1})
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 20}, {"foocoin", 7}}))
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"barcoin", 10}, {"foocoin", 8}}))
|
||||||
|
|
||||||
|
// inputs := []Input{
|
||||||
|
// NewInput(addr, sdk.Coins{{"foocoin", 3}}),
|
||||||
|
// NewInput(addr2, sdk.Coins{{"barcoin", 3}, {"foocoin", 2}}),
|
||||||
|
// }
|
||||||
|
|
||||||
|
// outputs := []Output{
|
||||||
|
// NewOutput(addr, sdk.Coins{{"barcoin", 1}}),
|
||||||
|
// NewOutput(addr3, sdk.Coins{{"barcoin", 2}, {"foocoin", 5}}),
|
||||||
|
// }
|
||||||
|
// coinKeeper.InputOutputCoins(ctx, inputs, outputs)
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr).IsEqual(sdk.Coins{{"barcoin", 21}, {"foocoin", 4}}))
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"barcoin", 7}, {"foocoin", 6}}))
|
||||||
|
// assert.True(t, coinKeeper.GetCoins(ctx, addr3).IsEqual(sdk.Coins{{"barcoin", 2}, {"foocoin", 5}}))
|
||||||
|
|
||||||
|
//}
|
||||||
|
Loading…
Reference in New Issue
Block a user