cosmos-sdk/x/stake/test_common.go

182 lines
5.5 KiB
Go
Raw Normal View History

2018-01-25 12:11:58 -08:00
package stake
import (
"encoding/hex"
"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"
2018-03-17 11:18:04 -07:00
oldwire "github.com/tendermint/go-wire"
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-03-17 11:18:04 -07:00
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
2018-01-25 12:11:58 -08:00
)
2018-03-26 07:48:15 -07:00
// dummy addresses used for testing
var (
addrs = []sdk.Address{
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6160"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6161"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6162"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6163"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6164"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6165"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6166"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6167"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6168"),
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6169"),
}
// dummy pubkeys used for testing
pks = []crypto.PubKey{
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB53"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB54"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB55"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB56"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB58"),
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB59"),
}
emptyAddr sdk.Address
emptyPubkey crypto.PubKey
)
// default params for testing
func defaultParams() Params {
return Params{
InflationRateChange: sdk.NewRat(13, 100),
InflationMax: sdk.NewRat(20, 100),
InflationMin: sdk.NewRat(7, 100),
GoalBonded: sdk.NewRat(67, 100),
MaxValidators: 100,
BondDenom: "fermion",
}
}
// initial pool for testing
func initialPool() Pool {
return Pool{
TotalSupply: 0,
BondedShares: sdk.ZeroRat,
UnbondedShares: sdk.ZeroRat,
BondedPool: 0,
UnbondedPool: 0,
InflationLastTime: 0,
Inflation: sdk.NewRat(7, 100),
}
}
2018-03-26 07:48:15 -07:00
// XXX reference the common declaration of this function
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
}
2018-03-17 11:18:04 -07:00
// custom tx codec
// TODO: use new go-wire
func makeTestCodec() *wire.Codec {
const msgTypeSend = 0x1
const msgTypeIssue = 0x2
const msgTypeDeclareCandidacy = 0x3
const msgTypeEditCandidacy = 0x4
const msgTypeDelegate = 0x5
const msgTypeUnbond = 0x6
var _ = oldwire.RegisterInterface(
struct{ sdk.Msg }{},
oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend},
oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue},
oldwire.ConcreteType{MsgDeclareCandidacy{}, msgTypeDeclareCandidacy},
oldwire.ConcreteType{MsgEditCandidacy{}, msgTypeEditCandidacy},
oldwire.ConcreteType{MsgDelegate{}, msgTypeDelegate},
oldwire.ConcreteType{MsgUnbond{}, msgTypeUnbond},
)
const accTypeApp = 0x1
var _ = oldwire.RegisterInterface(
struct{ sdk.Account }{},
oldwire.ConcreteType{&auth.BaseAccount{}, accTypeApp},
2018-03-17 11:18:04 -07:00
)
cdc := wire.NewCodec()
// cdc.RegisterInterface((*sdk.Msg)(nil), nil)
// bank.RegisterWire(cdc) // Register bank.[SendMsg,IssueMsg] types.
// crypto.RegisterWire(cdc) // Register crypto.[PubKey,PrivKey,Signature] types.
return cdc
}
func paramsNoInflation() Params {
return Params{
InflationRateChange: sdk.ZeroRat,
InflationMax: sdk.ZeroRat,
InflationMin: sdk.ZeroRat,
GoalBonded: sdk.NewRat(67, 100),
2018-03-20 14:21:18 -07:00
MaxValidators: 100,
2018-03-17 11:18:04 -07:00
BondDenom: "fermion",
}
}
// hogpodge of all sorts of input required for testing
2018-04-01 09:05:58 -07:00
func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context, sdk.AccountMapper, Keeper) {
2018-02-02 08:38:40 -08:00
db := dbm.NewMemDB()
2018-03-17 11:18:04 -07:00
keyStake := sdk.NewKVStoreKey("stake")
2018-03-20 14:21:18 -07:00
keyMain := keyStake //sdk.NewKVStoreKey("main") //TODO fix multistore
ms := store.NewCommitMultiStore(db)
2018-03-17 11:18:04 -07:00
ms.MountStoreWithDB(keyStake, 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)
2018-03-17 11:18:04 -07:00
cdc := makeTestCodec()
accountMapper := auth.NewAccountMapperSealed(
keyMain, // target store
&auth.BaseAccount{}, // prototype
)
ck := bank.NewCoinKeeper(accountMapper)
2018-03-20 14:21:18 -07:00
keeper := NewKeeper(ctx, cdc, keyStake, ck)
2018-04-03 11:50:31 -07:00
keeper.setPool(ctx, initialPool())
keeper.setParams(ctx, defaultParams())
2018-03-20 14:21:18 -07:00
2018-03-19 08:53:20 -07:00
// fill all the addresses with some coins
for _, addr := range addrs {
2018-03-29 05:27:35 -07:00
ck.AddCoins(ctx, addr, sdk.Coins{
{keeper.GetParams(ctx).BondDenom, initCoins},
})
2018-03-19 08:53:20 -07:00
}
2018-03-20 14:21:18 -07:00
return ctx, accountMapper, keeper
2018-01-25 12:11:58 -08:00
}
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
}
2018-03-17 11:18:04 -07:00
// for incode address generation
func testAddr(addr string) sdk.Address {
2018-03-19 08:53:20 -07:00
res, err := sdk.GetAddress(addr)
2018-03-17 11:18:04 -07:00
if err != nil {
panic(err)
}
return res
}