mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
simplify WIP
This commit is contained in:
parent
a2d4299b99
commit
bf5c8487ce
@ -16,11 +16,11 @@ import (
|
|||||||
"github.com/cosmos/cosmos-sdk/version"
|
"github.com/cosmos/cosmos-sdk/version"
|
||||||
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
||||||
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
||||||
ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli"
|
//ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli"
|
||||||
stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli"
|
//stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/internal/app"
|
"github.com/kava-labs/kava/internal/app"
|
||||||
"github.com/kava-labs/kava/internal/types"
|
//"github.com/kava-labs/kava/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// rootCmd is the entry point for this binary
|
// rootCmd is the entry point for this binary
|
||||||
@ -51,18 +51,18 @@ func main() {
|
|||||||
// add query/post commands (custom to binary)
|
// add query/post commands (custom to binary)
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
client.GetCommands(
|
client.GetCommands(
|
||||||
authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)),
|
authcmd.GetAccountCmd("acc", cdc, authcmd.GetAccountDecoder(cdc)),
|
||||||
)...)
|
)...)
|
||||||
|
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
client.PostCommands(
|
client.PostCommands(
|
||||||
bankcmd.SendTxCmd(cdc),
|
bankcmd.SendTxCmd(cdc),
|
||||||
ibccmd.IBCTransferCmd(cdc),
|
//ibccmd.IBCTransferCmd(cdc),
|
||||||
ibccmd.IBCRelayCmd(cdc),
|
//ibccmd.IBCRelayCmd(cdc),
|
||||||
stakecmd.GetCmdCreateValidator(cdc),
|
//stakecmd.GetCmdCreateValidator(cdc),
|
||||||
stakecmd.GetCmdEditValidator(cdc),
|
//stakecmd.GetCmdEditValidator(cdc),
|
||||||
stakecmd.GetCmdDelegate(cdc),
|
//stakecmd.GetCmdDelegate(cdc),
|
||||||
stakecmd.GetCmdUnbond(cdc),
|
//stakecmd.GetCmdUnbond(cdc),
|
||||||
)...)
|
)...)
|
||||||
|
|
||||||
// add proxy, version and key info
|
// add proxy, version and key info
|
||||||
|
@ -26,7 +26,7 @@ func main() {
|
|||||||
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
|
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
|
||||||
}
|
}
|
||||||
|
|
||||||
server.AddCommands(ctx, cdc, rootCmd, server.DefaultAppInit,
|
server.AddCommands(ctx, cdc, rootCmd, app.CreateAppInit(),
|
||||||
server.ConstructAppCreator(newApp, "kava"),
|
server.ConstructAppCreator(newApp, "kava"),
|
||||||
server.ConstructAppExporter(exportAppStateAndTMValidators, "kava"))
|
server.ConstructAppExporter(exportAppStateAndTMValidators, "kava"))
|
||||||
|
|
||||||
|
@ -14,10 +14,9 @@ import (
|
|||||||
"github.com/cosmos/cosmos-sdk/wire"
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||||
"github.com/cosmos/cosmos-sdk/x/ibc"
|
//"github.com/cosmos/cosmos-sdk/x/ibc"
|
||||||
"github.com/cosmos/cosmos-sdk/x/slashing"
|
//"github.com/cosmos/cosmos-sdk/x/slashing"
|
||||||
"github.com/cosmos/cosmos-sdk/x/stake"
|
//"github.com/cosmos/cosmos-sdk/x/stake"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/internal/types"
|
"github.com/kava-labs/kava/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,17 +32,17 @@ type KavaApp struct {
|
|||||||
// keys to access the substores
|
// keys to access the substores
|
||||||
keyMain *sdk.KVStoreKey
|
keyMain *sdk.KVStoreKey
|
||||||
keyAccount *sdk.KVStoreKey
|
keyAccount *sdk.KVStoreKey
|
||||||
keyIBC *sdk.KVStoreKey
|
//keyIBC *sdk.KVStoreKey
|
||||||
keyStake *sdk.KVStoreKey
|
//keyStake *sdk.KVStoreKey
|
||||||
keySlashing *sdk.KVStoreKey
|
//keySlashing *sdk.KVStoreKey
|
||||||
|
|
||||||
// Manage getting and setting accounts
|
// Manage getting and setting accounts
|
||||||
accountMapper auth.AccountMapper
|
accountMapper auth.AccountMapper
|
||||||
feeCollectionKeeper auth.FeeCollectionKeeper
|
feeCollectionKeeper auth.FeeCollectionKeeper
|
||||||
coinKeeper bank.Keeper
|
coinKeeper bank.Keeper
|
||||||
ibcMapper ibc.Mapper
|
//ibcMapper ibc.Mapper
|
||||||
stakeKeeper stake.Keeper
|
//stakeKeeper stake.Keeper
|
||||||
slashingKeeper slashing.Keeper
|
//slashingKeeper slashing.Keeper
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewKavaApp(logger log.Logger, db dbm.DB) *KavaApp {
|
func NewKavaApp(logger log.Logger, db dbm.DB) *KavaApp {
|
||||||
@ -57,37 +56,37 @@ func NewKavaApp(logger log.Logger, db dbm.DB) *KavaApp {
|
|||||||
cdc: cdc,
|
cdc: cdc,
|
||||||
keyMain: sdk.NewKVStoreKey("main"),
|
keyMain: sdk.NewKVStoreKey("main"),
|
||||||
keyAccount: sdk.NewKVStoreKey("acc"),
|
keyAccount: sdk.NewKVStoreKey("acc"),
|
||||||
keyIBC: sdk.NewKVStoreKey("ibc"),
|
//keyIBC: sdk.NewKVStoreKey("ibc"),
|
||||||
keyStake: sdk.NewKVStoreKey("stake"),
|
//keyStake: sdk.NewKVStoreKey("stake"),
|
||||||
keySlashing: sdk.NewKVStoreKey("slashing"),
|
//keySlashing: sdk.NewKVStoreKey("slashing"),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the accountMapper.
|
// Define the accountMapper.
|
||||||
app.accountMapper = auth.NewAccountMapper(
|
app.accountMapper = auth.NewAccountMapper(
|
||||||
cdc,
|
cdc,
|
||||||
app.keyAccount, // target store
|
app.keyAccount, // target store
|
||||||
&types.AppAccount{}, // prototype
|
&auth.BaseAccount{},
|
||||||
)
|
)
|
||||||
|
|
||||||
// add accountMapper/handlers
|
// add accountMapper/handlers
|
||||||
app.coinKeeper = bank.NewKeeper(app.accountMapper)
|
app.coinKeeper = bank.NewKeeper(app.accountMapper)
|
||||||
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
|
//app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
|
||||||
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
|
//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.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.RegisterCodespace(slashing.DefaultCodespace))
|
||||||
|
|
||||||
// register message routes
|
// register message routes
|
||||||
app.Router().
|
app.Router().
|
||||||
AddRoute("auth", auth.NewHandler(app.accountMapper)).
|
AddRoute("auth", auth.NewHandler(app.accountMapper)).
|
||||||
AddRoute("bank", bank.NewHandler(app.coinKeeper)).
|
AddRoute("bank", bank.NewHandler(app.coinKeeper))
|
||||||
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
|
//AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
|
||||||
AddRoute("stake", stake.NewHandler(app.stakeKeeper))
|
//AddRoute("stake", stake.NewHandler(app.stakeKeeper))
|
||||||
|
|
||||||
// Initialize BaseApp.
|
// Initialize BaseApp.
|
||||||
app.SetInitChainer(app.initChainer)
|
app.SetInitChainer(app.initChainer)
|
||||||
app.SetBeginBlocker(app.BeginBlocker)
|
app.SetBeginBlocker(app.BeginBlocker)
|
||||||
app.SetEndBlocker(app.EndBlocker)
|
app.SetEndBlocker(app.EndBlocker)
|
||||||
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
|
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
|
||||||
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake, app.keySlashing)
|
app.MountStoresIAVL(app.keyMain, app.keyAccount) //, app.keyIBC, app.keyStake, app.keySlashing)
|
||||||
err := app.LoadLatestVersion(app.keyMain)
|
err := app.LoadLatestVersion(app.keyMain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmn.Exit(err.Error())
|
cmn.Exit(err.Error())
|
||||||
@ -101,35 +100,38 @@ func MakeCodec() *wire.Codec {
|
|||||||
wire.RegisterCrypto(cdc) // Register crypto.
|
wire.RegisterCrypto(cdc) // Register crypto.
|
||||||
sdk.RegisterWire(cdc) // Register Msgs
|
sdk.RegisterWire(cdc) // Register Msgs
|
||||||
bank.RegisterWire(cdc)
|
bank.RegisterWire(cdc)
|
||||||
stake.RegisterWire(cdc)
|
//stake.RegisterWire(cdc)
|
||||||
slashing.RegisterWire(cdc)
|
//slashing.RegisterWire(cdc)
|
||||||
ibc.RegisterWire(cdc)
|
//ibc.RegisterWire(cdc)
|
||||||
|
auth.RegisterWire(cdc)
|
||||||
|
|
||||||
// register custom AppAccount
|
// register custom AppAccount
|
||||||
cdc.RegisterInterface((*auth.Account)(nil), nil)
|
//cdc.RegisterInterface((*auth.Account)(nil), nil)
|
||||||
cdc.RegisterConcrete(&types.AppAccount{}, "basecoin/Account", nil)
|
//cdc.RegisterConcrete(&types.BaseAccount{}, "kava/Account", nil)
|
||||||
return cdc
|
return cdc
|
||||||
}
|
}
|
||||||
|
|
||||||
// application updates every end block
|
// application updates every end block
|
||||||
func (app *KavaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
|
func (app *KavaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
|
||||||
tags := slashing.BeginBlocker(ctx, req, app.slashingKeeper)
|
//tags := slashing.BeginBlocker(ctx, req, app.slashingKeeper)
|
||||||
|
|
||||||
return abci.ResponseBeginBlock{
|
//return abci.ResponseBeginBlock{
|
||||||
Tags: tags.ToKVPairs(),
|
// Tags: tags.ToKVPairs(),
|
||||||
}
|
//}
|
||||||
|
return abci.ResponseBeginBlock{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// application updates every end block
|
// application updates every end block
|
||||||
func (app *KavaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
func (app *KavaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
||||||
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
|
//validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
|
||||||
|
|
||||||
return abci.ResponseEndBlock{
|
//return abci.ResponseEndBlock{
|
||||||
ValidatorUpdates: validatorUpdates,
|
// ValidatorUpdates: validatorUpdates,
|
||||||
}
|
//}
|
||||||
|
return abci.ResponseEndBlock{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Custom logic for basecoin initialization
|
// Custom logic for initialization
|
||||||
func (app *KavaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
func (app *KavaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
||||||
stateJSON := req.AppStateBytes
|
stateJSON := req.AppStateBytes
|
||||||
|
|
||||||
@ -151,7 +153,7 @@ func (app *KavaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load the initial stake information
|
// load the initial stake information
|
||||||
stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
|
//stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
|
||||||
|
|
||||||
return abci.ResponseInitChain{}
|
return abci.ResponseInitChain{}
|
||||||
}
|
}
|
||||||
@ -161,9 +163,9 @@ func (app *KavaApp) ExportAppStateAndValidators() (appState json.RawMessage, val
|
|||||||
ctx := app.NewContext(true, abci.Header{})
|
ctx := app.NewContext(true, abci.Header{})
|
||||||
|
|
||||||
// iterate to get the accounts
|
// iterate to get the accounts
|
||||||
accounts := []*types.GenesisAccount{}
|
accounts := []types.GenesisAccount{}
|
||||||
appendAccount := func(acc auth.Account) (stop bool) {
|
appendAccount := func(acc auth.Account) (stop bool) {
|
||||||
account := &types.GenesisAccount{
|
account := types.GenesisAccount{
|
||||||
Address: acc.GetAddress(),
|
Address: acc.GetAddress(),
|
||||||
Coins: acc.GetCoins(),
|
Coins: acc.GetCoins(),
|
||||||
}
|
}
|
||||||
@ -179,6 +181,8 @@ func (app *KavaApp) ExportAppStateAndValidators() (appState json.RawMessage, val
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
validators = stake.WriteValidators(ctx, app.stakeKeeper)
|
|
||||||
|
validators = make([]tmtypes.GenesisValidator, 0) // TODO export the actual validators
|
||||||
|
|
||||||
return appState, validators, err
|
return appState, validators, err
|
||||||
}
|
}
|
||||||
|
307
internal/app/genesis.go
Normal file
307
internal/app/genesis.go
Normal file
@ -0,0 +1,307 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
//"github.com/spf13/pflag"
|
||||||
|
//"github.com/spf13/viper"
|
||||||
|
crypto "github.com/tendermint/go-crypto"
|
||||||
|
tmtypes "github.com/tendermint/tendermint/types"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/server"
|
||||||
|
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/stake"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// I want to be able to create a genesis.json with the initial state of the kava network.
|
||||||
|
// This is done by creating a custom AppInit object to be handed to the server when it creates commands.
|
||||||
|
// When `kvd init` is run, a genesis tx is created. Then, from that, an initial app state.
|
||||||
|
|
||||||
|
func CreateAppInit() server.AppInit {
|
||||||
|
return server.AppInit{
|
||||||
|
AppGenTx: KavaAppGenTx,
|
||||||
|
AppGenState: KavaAppGenState,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func KavaAppGenTx(cdc *wire.Codec, pk crypto.PubKey) (
|
||||||
|
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
|
||||||
|
|
||||||
|
// Generate address and secret key for the validator
|
||||||
|
var addr sdk.Address
|
||||||
|
var secret string
|
||||||
|
addr, secret, err = server.GenerateCoinKey()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate appGenTx -------------
|
||||||
|
var bz []byte
|
||||||
|
genTx := types.GenTx{
|
||||||
|
Address: addr,
|
||||||
|
PubKey: pk,
|
||||||
|
}
|
||||||
|
bz, err = wire.MarshalJSONIndent(cdc, genTx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
appGenTx = json.RawMessage(bz)
|
||||||
|
|
||||||
|
// cliPrint -------------
|
||||||
|
mm := map[string]string{"secret": secret}
|
||||||
|
bz, err = cdc.MarshalJSON(mm)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cliPrint = json.RawMessage(bz)
|
||||||
|
|
||||||
|
// validator -----------
|
||||||
|
validator = tmtypes.GenesisValidator{
|
||||||
|
PubKey: pk,
|
||||||
|
Power: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func KavaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) {
|
||||||
|
|
||||||
|
if len(appGenTxs) == 0 {
|
||||||
|
err = errors.New("must provide at least 1 genesis transaction")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
genaccs := make([]types.GenesisAccount, len(appGenTxs))
|
||||||
|
for i, appGenTx := range appGenTxs {
|
||||||
|
|
||||||
|
var genTx types.GenTx
|
||||||
|
err = cdc.UnmarshalJSON(appGenTx, &genTx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the genesis account
|
||||||
|
accAuth := auth.NewBaseAccountWithAddress(genTx.Address)
|
||||||
|
accAuth.Coins = sdk.Coins{
|
||||||
|
{"KVA", 10000000000},
|
||||||
|
}
|
||||||
|
accAuth.PubKey = genTx.PubKey
|
||||||
|
acc := types.NewGenesisAccount(&accAuth)
|
||||||
|
genaccs[i] = acc
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the final app state
|
||||||
|
genesisState := types.GenesisState{
|
||||||
|
Accounts: genaccs,
|
||||||
|
}
|
||||||
|
appState, err = wire.MarshalJSONIndent(cdc, genesisState)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
// --------------------- default init --------------------------
|
||||||
|
|
||||||
|
// simple default application init
|
||||||
|
var DefaultAppInit = AppInit{
|
||||||
|
AppGenTx: SimpleAppGenTx,
|
||||||
|
AppGenState: SimpleAppGenState,
|
||||||
|
}
|
||||||
|
|
||||||
|
// simple genesis tx
|
||||||
|
type SimpleGenTx struct {
|
||||||
|
Addr sdk.Address `json:"addr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a genesis transaction
|
||||||
|
func SimpleAppGenTx(cdc *wire.Codec, pk crypto.PubKey) (
|
||||||
|
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
|
||||||
|
|
||||||
|
var addr sdk.Address
|
||||||
|
var secret string
|
||||||
|
addr, secret, err = GenerateCoinKey()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var bz []byte
|
||||||
|
simpleGenTx := SimpleGenTx{addr}
|
||||||
|
bz, err = cdc.MarshalJSON(simpleGenTx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
appGenTx = json.RawMessage(bz)
|
||||||
|
|
||||||
|
mm := map[string]string{"secret": secret}
|
||||||
|
bz, err = cdc.MarshalJSON(mm)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cliPrint = json.RawMessage(bz)
|
||||||
|
|
||||||
|
validator = tmtypes.GenesisValidator{
|
||||||
|
PubKey: pk,
|
||||||
|
Power: 10,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the genesis app state
|
||||||
|
func SimpleAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) {
|
||||||
|
|
||||||
|
if len(appGenTxs) != 1 {
|
||||||
|
err = errors.New("must provide a single genesis transaction")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var genTx SimpleGenTx
|
||||||
|
err = cdc.UnmarshalJSON(appGenTxs[0], &genTx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
appState = json.RawMessage(fmt.Sprintf(`{
|
||||||
|
"accounts": [{
|
||||||
|
"address": "%s",
|
||||||
|
"coins": [
|
||||||
|
{
|
||||||
|
"denom": "mycoin",
|
||||||
|
"amount": 9007199254740992
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}]
|
||||||
|
}`, genTx.Addr.String()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------- gaia init ----------------------
|
||||||
|
|
||||||
|
// simple genesis tx
|
||||||
|
type GaiaGenTx struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Address sdk.Address `json:"address"`
|
||||||
|
PubKey crypto.PubKey `json:"pub_key"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a gaia genesis transaction with flags
|
||||||
|
func GaiaAppGenTx(cdc *wire.Codec, pk crypto.PubKey) (
|
||||||
|
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
|
||||||
|
clientRoot := viper.GetString(flagClientHome)
|
||||||
|
overwrite := viper.GetBool(flagOWK)
|
||||||
|
name := viper.GetString(flagName)
|
||||||
|
if name == "" {
|
||||||
|
return nil, nil, tmtypes.GenesisValidator{}, errors.New("Must specify --name (validator moniker)")
|
||||||
|
}
|
||||||
|
|
||||||
|
var addr sdk.Address
|
||||||
|
var secret string
|
||||||
|
addr, secret, err = server.GenerateSaveCoinKey(clientRoot, name, "1234567890", overwrite)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mm := map[string]string{"secret": secret}
|
||||||
|
var bz []byte
|
||||||
|
bz, err = cdc.MarshalJSON(mm)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cliPrint = json.RawMessage(bz)
|
||||||
|
appGenTx,_,validator,err = GaiaAppGenTxNF(cdc, pk, addr, name, overwrite)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a gaia genesis transaction without flags
|
||||||
|
func GaiaAppGenTxNF(cdc *wire.Codec, pk crypto.PubKey, addr sdk.Address, name string, overwrite bool) (
|
||||||
|
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
|
||||||
|
|
||||||
|
var bz []byte
|
||||||
|
gaiaGenTx := GaiaGenTx{
|
||||||
|
Name: name,
|
||||||
|
Address: addr,
|
||||||
|
PubKey: pk,
|
||||||
|
}
|
||||||
|
bz, err = wire.MarshalJSONIndent(cdc, gaiaGenTx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
appGenTx = json.RawMessage(bz)
|
||||||
|
|
||||||
|
validator = tmtypes.GenesisValidator{
|
||||||
|
PubKey: pk,
|
||||||
|
Power: freeFermionVal,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the core parameters for genesis initialization for gaia
|
||||||
|
// note that the pubkey input is this machines pubkey
|
||||||
|
func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) {
|
||||||
|
|
||||||
|
if len(appGenTxs) == 0 {
|
||||||
|
err = errors.New("must provide at least genesis transaction")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// start with the default staking genesis state
|
||||||
|
stakeData := stake.DefaultGenesisState()
|
||||||
|
|
||||||
|
// get genesis flag account information
|
||||||
|
genaccs := make([]GenesisAccount, len(appGenTxs))
|
||||||
|
for i, appGenTx := range appGenTxs {
|
||||||
|
|
||||||
|
var genTx GaiaGenTx
|
||||||
|
err = cdc.UnmarshalJSON(appGenTx, &genTx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the genesis account, give'm few steaks and a buncha token with there name
|
||||||
|
accAuth := auth.NewBaseAccountWithAddress(genTx.Address)
|
||||||
|
accAuth.Coins = sdk.Coins{
|
||||||
|
{genTx.Name + "Token", 1000},
|
||||||
|
{"steak", freeFermionsAcc},
|
||||||
|
}
|
||||||
|
acc := NewGenesisAccount(&accAuth)
|
||||||
|
genaccs[i] = acc
|
||||||
|
stakeData.Pool.LooseUnbondedTokens += freeFermionsAcc // increase the supply
|
||||||
|
|
||||||
|
// add the validator
|
||||||
|
if len(genTx.Name) > 0 {
|
||||||
|
desc := stake.NewDescription(genTx.Name, "", "", "")
|
||||||
|
validator := stake.NewValidator(genTx.Address, genTx.PubKey, desc)
|
||||||
|
validator.PoolShares = stake.NewBondedShares(sdk.NewRat(freeFermionVal))
|
||||||
|
stakeData.Validators = append(stakeData.Validators, validator)
|
||||||
|
|
||||||
|
// pool logic
|
||||||
|
stakeData.Pool.BondedTokens += freeFermionVal
|
||||||
|
stakeData.Pool.BondedShares = sdk.NewRat(stakeData.Pool.BondedTokens)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the final app state
|
||||||
|
genesisState = GenesisState{
|
||||||
|
Accounts: genaccs,
|
||||||
|
StakeData: stakeData,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GaiaAppGenState but with JSON
|
||||||
|
func GaiaAppGenStateJSON(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) {
|
||||||
|
|
||||||
|
// create the final app state
|
||||||
|
genesisState, err := GaiaAppGenState(cdc, appGenTxs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
appState, err = wire.MarshalJSONIndent(cdc, genesisState)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*/
|
@ -2,11 +2,13 @@ package types
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/wire"
|
crypto "github.com/tendermint/go-crypto"
|
||||||
|
//"github.com/cosmos/cosmos-sdk/wire"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
"github.com/cosmos/cosmos-sdk/x/stake"
|
//"github.com/cosmos/cosmos-sdk/x/stake"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
var _ auth.Account = (*AppAccount)(nil)
|
var _ auth.Account = (*AppAccount)(nil)
|
||||||
|
|
||||||
// Custom extensions for this application. This is just an example of
|
// Custom extensions for this application. This is just an example of
|
||||||
@ -37,38 +39,42 @@ func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder {
|
|||||||
return acct, err
|
return acct, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
//___________________________________________________________________________________
|
//___________________________________________________________________________________
|
||||||
|
|
||||||
|
type GenTx struct {
|
||||||
|
Address sdk.Address `json:"address"`
|
||||||
|
PubKey crypto.PubKey `json:"pub_key"`
|
||||||
|
}
|
||||||
|
|
||||||
// State to Unmarshal
|
// State to Unmarshal
|
||||||
type GenesisState struct {
|
type GenesisState struct {
|
||||||
Accounts []*GenesisAccount `json:"accounts"`
|
Accounts []GenesisAccount `json:"accounts"`
|
||||||
StakeData stake.GenesisState `json:"stake"`
|
//StakeData stake.GenesisState `json:"stake"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenesisAccount doesn't need pubkey or sequence
|
// GenesisAccount doesn't need pubkey or sequence
|
||||||
type GenesisAccount struct {
|
type GenesisAccount struct {
|
||||||
Name string `json:"name"`
|
//Name string `json:"name"`
|
||||||
Address sdk.Address `json:"address"`
|
Address sdk.Address `json:"address"`
|
||||||
Coins sdk.Coins `json:"coins"`
|
Coins sdk.Coins `json:"coins"`
|
||||||
|
PubKey crypto.PubKey `json:"pub_key"` //add in pub key so I can send coins?
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGenesisAccount(aa *AppAccount) *GenesisAccount {
|
func NewGenesisAccount(aa *auth.BaseAccount) GenesisAccount {
|
||||||
return &GenesisAccount{
|
return GenesisAccount{
|
||||||
Name: aa.Name,
|
//Name: aa.Name,
|
||||||
Address: aa.Address,
|
Address: aa.Address,
|
||||||
Coins: aa.Coins.Sort(),
|
Coins: aa.Coins.Sort(),
|
||||||
|
PubKey: aa.PubKey, // add in pub key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert GenesisAccount to AppAccount
|
// convert GenesisAccount to AppAccount
|
||||||
func (ga *GenesisAccount) ToAppAccount() (acc *AppAccount, err error) {
|
func (ga *GenesisAccount) ToAppAccount() (acc *auth.BaseAccount, err error) {
|
||||||
baseAcc := auth.BaseAccount{
|
return &auth.BaseAccount{
|
||||||
Address: ga.Address,
|
Address: ga.Address,
|
||||||
Coins: ga.Coins.Sort(),
|
Coins: ga.Coins.Sort(),
|
||||||
}
|
PubKey: ga.PubKey, // add in pub key
|
||||||
return &AppAccount{
|
|
||||||
BaseAccount: baseAcc,
|
|
||||||
Name: ga.Name,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user