cosmos-sdk/x/stake/test_common.go

117 lines
3.5 KiB
Go
Raw Normal View History

2018-01-25 12:11:58 -08:00
package stake
import (
"encoding/hex"
"fmt"
"testing"
"github.com/stretchr/testify/require"
2018-01-25 12:11:58 -08:00
abci "github.com/tendermint/abci/types"
2018-01-25 12:11:58 -08:00
crypto "github.com/tendermint/go-crypto"
dbm "github.com/tendermint/tmlibs/db"
2018-01-25 12:11:58 -08:00
"github.com/cosmos/cosmos-sdk/store"
2018-01-25 12:11:58 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
)
2018-01-25 13:11:40 -08:00
func subspace(prefix []byte) (start, end []byte) {
2018-02-02 08:51:08 -08:00
end = make([]byte, len(prefix))
copy(end, prefix)
2018-01-25 13:11:40 -08:00
end[len(end)-1]++
2018-02-02 08:51:08 -08:00
return prefix, end
2018-01-25 13:11:40 -08:00
}
func createTestInput(t *testing.T, isCheckTx bool) (sdk.KVStore, sdk.Context, sdk.StoreKey) {
2018-02-02 08:38:40 -08:00
db := dbm.NewMemDB()
key := sdk.NewKVStoreKey("stake")
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
2018-02-02 08:38:40 -08:00
err := ms.LoadLatestVersion()
require.Nil(t, err)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil)
store := ms.GetKVStore(key)
return store, ctx, key
}
2018-01-25 12:11:58 -08:00
func newAddrs(n int) (addrs []crypto.Address) {
for i := 0; i < n; i++ {
addrs = append(addrs, []byte(fmt.Sprintf("addr%d", i)))
}
return
}
func newPubKey(pk string) (res crypto.PubKey) {
pkBytes, err := hex.DecodeString(pk)
if err != nil {
panic(err)
}
//res, err = crypto.PubKeyFromBytes(pkBytes)
var pkEd crypto.PubKeyEd25519
copy(pkEd[:], pkBytes[:])
return pkEd.Wrap()
2018-01-25 12:11:58 -08:00
}
// dummy pubkeys used for testing
var pks = []crypto.PubKey{
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB53"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB54"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB55"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB56"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB58"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB59"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB60"),
}
// NOTE: PubKey is supposed to be the binaryBytes of the crypto.PubKey
// instead this is just being set the address here for testing purposes
func candidatesFromActors(store sdk.KVStore, addrs []crypto.Address, amts []int64) {
2018-01-25 12:11:58 -08:00
for i := 0; i < len(addrs); i++ {
c := &Candidate{
Status: Unbonded,
PubKey: pks[i],
Address: addrs[i],
2018-02-23 05:13:55 -08:00
Assets: sdk.NewRat(amts[i]),
Liabilities: sdk.NewRat(amts[i]),
VotingPower: sdk.NewRat(amts[i]),
2018-01-25 12:11:58 -08:00
}
saveCandidate(store, c)
}
}
func saveCandidate(store sdk.KVStore, c *Candidate) {} // TODO
2018-01-25 12:11:58 -08:00
func candidatesFromActorsEmpty(addrs []crypto.Address) (candidates Candidates) {
for i := 0; i < len(addrs); i++ {
c := &Candidate{
Status: Unbonded,
PubKey: pks[i],
Address: addrs[i],
2018-02-23 05:13:55 -08:00
Assets: sdk.ZeroRat,
Liabilities: sdk.ZeroRat,
VotingPower: sdk.ZeroRat,
2018-01-25 12:11:58 -08:00
}
candidates = append(candidates, c)
}
return
}
//// helper function test if Candidate is changed asabci.Validator
//func testChange(t *testing.T, val Validator, chg *abci.Validator) {
//assert := assert.New(t)
//assert.Equal(val.PubKey.Bytes(), chg.PubKey)
//assert.Equal(val.VotingPower.Evaluate(), chg.Power)
//}
//// helper function test if Candidate is removed as abci.Validator
//func testRemove(t *testing.T, val Validator, chg *abci.Validator) {
//assert := assert.New(t)
//assert.Equal(val.PubKey.Bytes(), chg.PubKey)
//assert.Equal(int64(0), chg.Power)
//}