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