2018-09-18 23:41:48 +00:00
|
|
|
// Copyright 2016 All in Bits, inc
|
|
|
|
// Modifications copyright 2018 Kava Labs
|
|
|
|
|
2018-05-25 13:46:33 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-08-14 22:13:54 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2018-05-25 13:46:33 +00:00
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2018-06-16 21:34:07 +00:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2018-05-25 13:46:33 +00:00
|
|
|
|
|
|
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
|
|
|
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-09-19 19:22:16 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2018-08-14 22:13:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake"
|
2018-07-15 14:08:08 +00:00
|
|
|
"github.com/kava-labs/kava/internal/x/paychan"
|
2018-05-25 13:46:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-06-16 21:34:07 +00:00
|
|
|
appName = "KavaApp"
|
2018-05-25 13:46:33 +00:00
|
|
|
)
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// Set default directories for data
|
|
|
|
var (
|
|
|
|
DefaultCLIHome = os.ExpandEnv("$HOME/.kvcli")
|
|
|
|
DefaultNodeHome = os.ExpandEnv("$HOME/.kvd")
|
|
|
|
)
|
|
|
|
|
2018-06-16 21:34:07 +00:00
|
|
|
type KavaApp struct {
|
2018-05-25 13:46:33 +00:00
|
|
|
*bam.BaseApp
|
|
|
|
cdc *wire.Codec
|
|
|
|
|
|
|
|
// keys to access the substores
|
2018-06-18 14:49:09 +00:00
|
|
|
keyMain *sdk.KVStoreKey
|
|
|
|
keyAccount *sdk.KVStoreKey
|
2018-08-14 22:13:54 +00:00
|
|
|
keyStake *sdk.KVStoreKey
|
|
|
|
keySlashing *sdk.KVStoreKey
|
|
|
|
keyFeeCollection *sdk.KVStoreKey
|
2018-09-19 19:22:16 +00:00
|
|
|
keyParams *sdk.KVStoreKey
|
|
|
|
tkeyParams *sdk.TransientStoreKey
|
2018-08-24 16:12:26 +00:00
|
|
|
keyPaychan *sdk.KVStoreKey
|
2018-05-25 13:46:33 +00:00
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// keepers
|
2018-06-16 21:34:07 +00:00
|
|
|
accountMapper auth.AccountMapper
|
|
|
|
feeCollectionKeeper auth.FeeCollectionKeeper
|
|
|
|
coinKeeper bank.Keeper
|
2018-08-14 22:13:54 +00:00
|
|
|
stakeKeeper stake.Keeper
|
|
|
|
slashingKeeper slashing.Keeper
|
2018-09-19 19:22:16 +00:00
|
|
|
paramsKeeper params.Keeper
|
|
|
|
paychanKeeper paychan.Keeper
|
2018-05-25 13:46:33 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// Creates a new KavaApp.
|
|
|
|
func NewKavaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptions ...func(*bam.BaseApp)) *KavaApp {
|
2018-05-25 13:46:33 +00:00
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// Create a codec for use across the whole app
|
|
|
|
cdc := CreateKavaAppCodec()
|
2018-05-25 13:46:33 +00:00
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// Create a new base app
|
2018-09-19 19:22:16 +00:00
|
|
|
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc) baseAppOptions...)
|
2018-08-14 22:13:54 +00:00
|
|
|
bApp.SetCommitMultiStoreTracer(traceStore)
|
|
|
|
|
|
|
|
// Create the kava app, extending baseApp
|
2018-06-16 21:34:07 +00:00
|
|
|
var app = &KavaApp{
|
2018-08-14 22:13:54 +00:00
|
|
|
BaseApp: bApp,
|
2018-06-18 14:49:09 +00:00
|
|
|
cdc: cdc,
|
|
|
|
keyMain: sdk.NewKVStoreKey("main"),
|
|
|
|
keyAccount: sdk.NewKVStoreKey("acc"),
|
2018-08-14 22:13:54 +00:00
|
|
|
keyStake: sdk.NewKVStoreKey("stake"),
|
|
|
|
keySlashing: sdk.NewKVStoreKey("slashing"),
|
|
|
|
keyFeeCollection: sdk.NewKVStoreKey("fee"),
|
2018-09-19 19:22:16 +00:00
|
|
|
keyParams: sdk.NewKVStoreKey("params"),
|
|
|
|
tkeyParams: sdk.NewTransientStoreKey("transient_params"),
|
|
|
|
keyPaychan: sdk.NewKVStoreKey("paychan"),
|
2018-05-25 13:46:33 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// Define the accountMapper and base account
|
2018-05-25 13:46:33 +00:00
|
|
|
app.accountMapper = auth.NewAccountMapper(
|
2018-09-19 19:22:16 +00:00
|
|
|
app.cdc,
|
2018-08-14 22:13:54 +00:00
|
|
|
app.keyAccount,
|
|
|
|
auth.ProtoBaseAccount,
|
2018-05-25 13:46:33 +00:00
|
|
|
)
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// Create the keepers
|
2018-05-25 13:46:33 +00:00
|
|
|
app.coinKeeper = bank.NewKeeper(app.accountMapper)
|
2018-09-19 19:22:16 +00:00
|
|
|
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
|
2018-08-14 22:13:54 +00:00
|
|
|
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
|
|
|
|
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection)
|
2018-09-19 19:22:16 +00:00
|
|
|
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Getter(), app.RegisterCodespace(slashing.DefaultCodespace))
|
|
|
|
app.paychanKeeper = paychan.NewKeeper(app.cdc, app.keyPaychan, app.coinKeeper)
|
2018-05-25 13:46:33 +00:00
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// Register the message handlers
|
2018-05-25 13:46:33 +00:00
|
|
|
app.Router().
|
2018-07-15 14:08:08 +00:00
|
|
|
AddRoute("bank", bank.NewHandler(app.coinKeeper)).
|
2018-08-14 22:13:54 +00:00
|
|
|
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
|
2018-08-24 16:12:26 +00:00
|
|
|
AddRoute("slashing", slashing.NewHandler(app.slashingKeeper)).
|
|
|
|
AddRoute("paychan", paychan.NewHandler(app.paychanKeeper))
|
2018-05-25 13:46:33 +00:00
|
|
|
|
2018-09-19 19:22:16 +00:00
|
|
|
// Set func to initialize the chain from appState in genesis file
|
2018-05-25 13:46:33 +00:00
|
|
|
app.SetInitChainer(app.initChainer)
|
2018-08-14 22:13:54 +00:00
|
|
|
|
|
|
|
// Set functions that run before and after txs / blocks
|
2018-06-16 21:34:07 +00:00
|
|
|
app.SetBeginBlocker(app.BeginBlocker)
|
|
|
|
app.SetEndBlocker(app.EndBlocker)
|
|
|
|
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
|
2018-08-14 22:13:54 +00:00
|
|
|
|
|
|
|
// Mount stores
|
2018-09-19 19:22:16 +00:00
|
|
|
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyStake, app.keySlashing, app.keyFeeCollection, app.keyParams, app.keyPaychan)
|
|
|
|
app.MountStore(app.tkeyParams, sdk.StoreTypeTransient)
|
2018-05-25 13:46:33 +00:00
|
|
|
err := app.LoadLatestVersion(app.keyMain)
|
|
|
|
if err != nil {
|
|
|
|
cmn.Exit(err.Error())
|
|
|
|
}
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// Creates a codec for use across the whole app.
|
|
|
|
func CreateKavaAppCodec() *wire.Codec {
|
|
|
|
cdc := wire.NewCodec()
|
2018-07-15 14:08:08 +00:00
|
|
|
paychan.RegisterWire(cdc)
|
2018-08-14 22:13:54 +00:00
|
|
|
bank.RegisterWire(cdc)
|
|
|
|
stake.RegisterWire(cdc)
|
|
|
|
slashing.RegisterWire(cdc)
|
2018-06-18 14:49:09 +00:00
|
|
|
auth.RegisterWire(cdc)
|
2018-08-14 22:13:54 +00:00
|
|
|
sdk.RegisterWire(cdc)
|
|
|
|
wire.RegisterCrypto(cdc)
|
2018-05-25 13:46:33 +00:00
|
|
|
return cdc
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// The function baseapp runs on receipt of a BeginBlock ABCI message
|
2018-06-16 21:34:07 +00:00
|
|
|
func (app *KavaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
|
2018-08-14 22:13:54 +00:00
|
|
|
tags := slashing.BeginBlocker(ctx, req, app.slashingKeeper)
|
2018-06-16 21:34:07 +00:00
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
return abci.ResponseBeginBlock{
|
|
|
|
Tags: tags.ToKVPairs(),
|
|
|
|
}
|
2018-06-16 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// The function baseapp runs on receipt of a EndBlock ABCI message
|
2018-06-16 21:34:07 +00:00
|
|
|
func (app *KavaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
2018-09-19 19:22:16 +00:00
|
|
|
tags := paychan.EndBlocker(ctx, app.paychanKeeper)
|
2018-08-14 22:13:54 +00:00
|
|
|
|
2018-09-19 19:22:16 +00:00
|
|
|
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
|
|
|
|
app.slashingKeeper.AddValidators(ctx, validatorUpdates)
|
2018-06-16 21:34:07 +00:00
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
return abci.ResponseEndBlock{
|
|
|
|
ValidatorUpdates: validatorUpdates,
|
2018-09-19 19:22:16 +00:00
|
|
|
Tags: tags,
|
2018-08-14 22:13:54 +00:00
|
|
|
}
|
2018-06-16 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:22:16 +00:00
|
|
|
// Initializes the app db from the appState in the genesis file. Baseapp runs this on receipt of an InitChain ABCI message
|
2018-06-16 21:34:07 +00:00
|
|
|
func (app *KavaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
2018-05-25 13:46:33 +00:00
|
|
|
stateJSON := req.AppStateBytes
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
var genesisState GenesisState
|
|
|
|
err := app.cdc.UnmarshalJSON(stateJSON, &genesisState)
|
2018-05-25 13:46:33 +00:00
|
|
|
if err != nil {
|
2018-08-14 22:13:54 +00:00
|
|
|
panic(err)
|
2018-05-25 13:46:33 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
// load the accounts
|
2018-05-25 13:46:33 +00:00
|
|
|
for _, gacc := range genesisState.Accounts {
|
2018-08-14 22:13:54 +00:00
|
|
|
acc := gacc.ToAccount()
|
2018-06-16 21:34:07 +00:00
|
|
|
acc.AccountNumber = app.accountMapper.GetNextAccountNumber(ctx)
|
2018-05-25 13:46:33 +00:00
|
|
|
app.accountMapper.SetAccount(ctx, acc)
|
|
|
|
}
|
2018-06-16 21:34:07 +00:00
|
|
|
|
|
|
|
// load the initial stake information
|
2018-09-19 19:22:16 +00:00
|
|
|
validators, err := stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
|
2018-08-14 22:13:54 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:22:16 +00:00
|
|
|
slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.StakeData)
|
2018-06-16 21:34:07 +00:00
|
|
|
|
2018-09-19 19:22:16 +00:00
|
|
|
return abci.ResponseInitChain{
|
|
|
|
Validators: validators,
|
|
|
|
}
|
2018-05-25 13:46:33 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
//
|
2018-06-16 21:34:07 +00:00
|
|
|
func (app *KavaApp) ExportAppStateAndValidators() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
|
2018-05-25 13:46:33 +00:00
|
|
|
ctx := app.NewContext(true, abci.Header{})
|
|
|
|
|
|
|
|
// iterate to get the accounts
|
2018-08-14 22:13:54 +00:00
|
|
|
accounts := []GenesisAccount{}
|
2018-06-16 21:34:07 +00:00
|
|
|
appendAccount := func(acc auth.Account) (stop bool) {
|
2018-08-14 22:13:54 +00:00
|
|
|
account := NewGenesisAccountI(acc)
|
2018-05-25 13:46:33 +00:00
|
|
|
accounts = append(accounts, account)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
app.accountMapper.IterateAccounts(ctx, appendAccount)
|
|
|
|
|
2018-08-14 22:13:54 +00:00
|
|
|
genState := GenesisState{
|
|
|
|
Accounts: accounts,
|
|
|
|
StakeData: stake.WriteGenesis(ctx, app.stakeKeeper),
|
2018-05-25 13:46:33 +00:00
|
|
|
}
|
2018-06-16 21:34:07 +00:00
|
|
|
appState, err = wire.MarshalJSONIndent(app.cdc, genState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-14 22:13:54 +00:00
|
|
|
validators = stake.WriteValidators(ctx, app.stakeKeeper)
|
|
|
|
return appState, validators, nil
|
2018-05-25 13:46:33 +00:00
|
|
|
}
|