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

217 lines
6.7 KiB
Go
Raw Normal View History

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-08-14 22:13:54 +00:00
//"github.com/cosmos/cosmos-sdk/x/gov"
2018-06-18 14:49:09 +00:00
//"github.com/cosmos/cosmos-sdk/x/ibc"
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-05-25 13:46:33 +00:00
2018-08-14 22:13:54 +00:00
//keyIBC *sdk.KVStoreKey
keyStake *sdk.KVStoreKey
keySlashing *sdk.KVStoreKey
//keyGov *sdk.KVStoreKey
keyFeeCollection *sdk.KVStoreKey
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-07-15 14:08:08 +00:00
paychanKeeper paychan.Keeper
2018-06-18 14:49:09 +00:00
//ibcMapper ibc.Mapper
2018-08-14 22:13:54 +00:00
stakeKeeper stake.Keeper
slashingKeeper slashing.Keeper
//govKeeper gov.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
bApp := bam.NewBaseApp(appName, cdc, logger, db, baseAppOptions...)
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-07-15 14:08:08 +00:00
keyPaychan: sdk.NewKVStoreKey("paychan"),
2018-06-18 14:49:09 +00:00
//keyIBC: sdk.NewKVStoreKey("ibc"),
2018-08-14 22:13:54 +00:00
keyStake: sdk.NewKVStoreKey("stake"),
keySlashing: sdk.NewKVStoreKey("slashing"),
//keyGov: sdk.NewKVStoreKey("gov"),
keyFeeCollection: sdk.NewKVStoreKey("fee"),
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(
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-07-15 14:08:08 +00:00
app.paychanKeeper = paychan.NewKeeper(app.cdc, app.keyPaychan, app.coinKeeper)
2018-06-18 14:49:09 +00:00
//app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
2018-08-14 22:13:54 +00:00
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.RegisterCodespace(slashing.DefaultCodespace))
//app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.coinKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace))
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection)
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-06-18 14:49:09 +00:00
//AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
2018-08-14 22:13:54 +00:00
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
AddRoute("slashing", slashing.NewHandler(app.slashingKeeper)).
AddRoute("paychan", paychan.NewHandler(app.paychanKeeper))
2018-08-14 22:13:54 +00:00
//AddRoute("gov", gov.NewHandler(app.govKeeper))
2018-05-25 13:46:33 +00:00
2018-08-14 22:13:54 +00:00
// Set func to initialze 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
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyStake, app.keySlashing, app.keyFeeCollection, app.keyPaychan)
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-06-18 14:49:09 +00:00
//ibc.RegisterWire(cdc)
2018-08-14 22:13:54 +00:00
bank.RegisterWire(cdc)
stake.RegisterWire(cdc)
slashing.RegisterWire(cdc)
//gov.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-01 23:29:51 +00:00
paychan.EndBlocker(ctx, app.paychanKeeper)
2018-08-14 22:13:54 +00:00
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
//tags, _ := gov.EndBlocker(ctx, app.govKeeper)
2018-06-16 21:34:07 +00:00
2018-08-14 22:13:54 +00:00
return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
//Tags: tags,
}
2018-06-16 21:34:07 +00:00
}
2018-08-14 22:13:54 +00:00
// Initialzes 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-08-14 22:13:54 +00:00
err = stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
if err != nil {
panic(err)
}
//gov.InitGenesis(ctx, app.govKeeper, gov.DefaultGenesisState())
2018-06-16 21:34:07 +00:00
2018-05-25 13:46:33 +00:00
return abci.ResponseInitChain{}
}
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
}