0g-chain/internal/app/app_test.go

78 lines
2.0 KiB
Go
Raw Normal View History

2018-05-25 13:46:33 +00:00
package app
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-06-16 21:34:07 +00:00
"github.com/cosmos/cosmos-sdk/wire"
2018-05-25 13:46:33 +00:00
"github.com/cosmos/cosmos-sdk/x/auth"
2018-06-16 21:34:07 +00:00
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/kava-labs/kava/internal/types"
2018-05-25 13:46:33 +00:00
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
)
2018-06-16 21:34:07 +00:00
func setGenesis(bapp *KavaApp, accs ...auth.BaseAccount) error {
2018-05-25 13:46:33 +00:00
genaccs := make([]*types.GenesisAccount, len(accs))
for i, acc := range accs {
2018-06-16 21:34:07 +00:00
genaccs[i] = types.NewGenesisAccount(&types.AppAccount{acc, "foobart"})
2018-05-25 13:46:33 +00:00
}
genesisState := types.GenesisState{
2018-06-16 21:34:07 +00:00
Accounts: genaccs,
StakeData: stake.DefaultGenesisState(),
2018-05-25 13:46:33 +00:00
}
2018-06-16 21:34:07 +00:00
stateBytes, err := wire.MarshalJSONIndent(bapp.cdc, genesisState)
2018-05-25 13:46:33 +00:00
if err != nil {
return err
}
// Initialize the chain
vals := []abci.Validator{}
2018-06-16 21:34:07 +00:00
bapp.InitChain(abci.RequestInitChain{Validators: vals, AppStateBytes: stateBytes})
2018-05-25 13:46:33 +00:00
bapp.Commit()
return nil
}
//_______________________________________________________________________
func TestGenesis(t *testing.T) {
2018-06-16 21:34:07 +00:00
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
db := dbm.NewMemDB()
bapp := NewKavaApp(logger, db)
2018-05-25 13:46:33 +00:00
// Construct some genesis bytes to reflect basecoin/types/AppAccount
pk := crypto.GenPrivKeyEd25519().PubKey()
addr := pk.Address()
coins, err := sdk.ParseCoins("77foocoin,99barcoin")
require.Nil(t, err)
baseAcc := auth.BaseAccount{
Address: addr,
Coins: coins,
}
acc := &types.AppAccount{baseAcc, "foobart"}
2018-06-16 21:34:07 +00:00
err = setGenesis(bapp, baseAcc)
require.Nil(t, err)
2018-05-25 13:46:33 +00:00
// A checkTx context
ctx := bapp.BaseApp.NewContext(true, abci.Header{})
res1 := bapp.accountMapper.GetAccount(ctx, baseAcc.Address)
assert.Equal(t, acc, res1)
// reload app and ensure the account is still there
2018-06-16 21:34:07 +00:00
bapp = NewKavaApp(logger, db)
2018-05-25 13:46:33 +00:00
ctx = bapp.BaseApp.NewContext(true, abci.Header{})
res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address)
assert.Equal(t, acc, res1)
}