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

307 lines
8.0 KiB
Go
Raw Normal View History

2018-06-18 14:49:09 +00:00
package app
import (
"encoding/json"
"errors"
2018-08-14 22:13:54 +00:00
"github.com/spf13/pflag"
"github.com/tendermint/tendermint/crypto"
2018-06-18 14:49:09 +00:00
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/server"
2018-08-14 22:13:54 +00:00
"github.com/cosmos/cosmos-sdk/server/config"
2018-06-18 14:49:09 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
2018-08-14 22:13:54 +00:00
"github.com/cosmos/cosmos-sdk/x/stake"
)
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
var (
2018-08-15 22:24:55 +00:00
// Tokens given to genesis validators and accounts
numStartingTokensValidators = int64(500000)
numStartingTokensAccounts = int64(500000)
2018-06-18 14:49:09 +00:00
)
2018-08-14 22:13:54 +00:00
// Initial app state to be written to (and read from) genesis file
type GenesisState struct {
Accounts []GenesisAccount `json:"accounts"`
StakeData stake.GenesisState `json:"stake"`
}
// A simplified version of a normal account. It doesn't have pubkey or sequence.
type GenesisAccount struct {
Address sdk.AccAddress `json:"address"`
Coins sdk.Coins `json:"coins"`
}
// TODO remove?
func NewGenesisAccount(acc *auth.BaseAccount) GenesisAccount {
return GenesisAccount{
Address: acc.Address,
Coins: acc.Coins,
}
}
// TODO remove?
func NewGenesisAccountI(acc auth.Account) GenesisAccount {
return GenesisAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
}
}
// Converts a GenesisAccount to auth.BaseAccount TODO rename
func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) {
return &auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins.Sort(),
}
}
// Create the appInit stuct for server init command
func KavaAppInit() server.AppInit {
fsAppGenState := pflag.NewFlagSet("", pflag.ContinueOnError)
fsAppGenTx := pflag.NewFlagSet("", pflag.ContinueOnError)
fsAppGenTx.String(server.FlagName, "", "validator moniker, required")
fsAppGenTx.String(server.FlagClientHome, DefaultCLIHome,
"home directory for the client, used for key generation")
fsAppGenTx.Bool(server.FlagOWK, false, "overwrite the accounts created")
2018-06-18 14:49:09 +00:00
return server.AppInit{
2018-08-14 22:13:54 +00:00
FlagsAppGenState: fsAppGenState,
FlagsAppGenTx: fsAppGenTx,
AppGenTx: KavaAppGenTx,
AppGenState: KavaAppGenStateJSON,
2018-06-18 14:49:09 +00:00
}
}
2018-08-14 22:13:54 +00:00
// Define format for GenTx json
type KavaGenTx struct {
Name string `json:"name"`
Address sdk.AccAddress `json:"address"`
PubKey string `json:"pub_key"`
}
// Generate a genesis transsction
func KavaAppGenTx(cdc *wire.Codec, pk crypto.PubKey, genTxConfig config.GenTx) (
2018-06-18 14:49:09 +00:00
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
// Generate address and secret key for the validator
2018-08-14 22:13:54 +00:00
if genTxConfig.Name == "" {
return nil, nil, tmtypes.GenesisValidator{}, errors.New("Must specify --name (validator moniker)")
}
var addr sdk.AccAddress
2018-06-18 14:49:09 +00:00
var secret string
2018-08-14 22:13:54 +00:00
addr, secret, err = server.GenerateSaveCoinKey(genTxConfig.CliRoot, genTxConfig.Name, "password", genTxConfig.Overwrite)
2018-06-18 14:49:09 +00:00
if err != nil {
return
}
2018-08-14 22:13:54 +00:00
// Create string to print out
mm := map[string]string{"secret": secret}
2018-06-18 14:49:09 +00:00
var bz []byte
2018-08-14 22:13:54 +00:00
bz, err = cdc.MarshalJSON(mm)
2018-06-18 14:49:09 +00:00
if err != nil {
return
}
2018-08-14 22:13:54 +00:00
cliPrint = json.RawMessage(bz)
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
// Create genTx and validator
appGenTx, _, validator, err = KavaAppGenTxNF(cdc, pk, addr, genTxConfig.Name)
return
}
// TODO combine with KavaAppGenTx
func KavaAppGenTxNF(cdc *wire.Codec, pk crypto.PubKey, addr sdk.AccAddress, name string) (
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
// Create the gentx
var bz []byte
genTx := KavaGenTx{
Name: name,
Address: addr,
PubKey: sdk.MustBech32ifyAccPub(pk),
}
bz, err = wire.MarshalJSONIndent(cdc, genTx)
2018-06-18 14:49:09 +00:00
if err != nil {
return
}
2018-08-14 22:13:54 +00:00
appGenTx = json.RawMessage(bz)
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
// Create the validator
2018-06-18 14:49:09 +00:00
validator = tmtypes.GenesisValidator{
PubKey: pk,
2018-08-15 22:24:55 +00:00
Power: numStartingTokensValidators,
2018-06-18 14:49:09 +00:00
}
return
}
2018-08-14 22:13:54 +00:00
// Create the core parameters for genesis initialization
// note that the pubkey input is this machines pubkey
func KavaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) {
2018-06-18 14:49:09 +00:00
if len(appGenTxs) == 0 {
err = errors.New("must provide at least 1 genesis transaction")
return
}
2018-08-14 22:13:54 +00:00
// start with the default staking genesis state
stakeData := stake.DefaultGenesisState()
2018-08-15 22:24:55 +00:00
// change denom of staking coin
stakeData.Params.BondDenom = "KVA"
2018-08-14 22:13:54 +00:00
// get genesis flag account information
genaccs := make([]GenesisAccount, len(appGenTxs))
2018-06-18 14:49:09 +00:00
for i, appGenTx := range appGenTxs {
2018-08-14 22:13:54 +00:00
var genTx KavaGenTx
2018-06-18 14:49:09 +00:00
err = cdc.UnmarshalJSON(appGenTx, &genTx)
if err != nil {
return
}
2018-08-15 01:31:05 +00:00
// create the genesis account
2018-06-18 14:49:09 +00:00
accAuth := auth.NewBaseAccountWithAddress(genTx.Address)
accAuth.Coins = sdk.Coins{
2018-08-15 22:24:55 +00:00
{"KVA", sdk.NewInt(numStartingTokensAccounts)},
2018-06-18 14:49:09 +00:00
}
2018-08-14 22:13:54 +00:00
acc := NewGenesisAccount(&accAuth)
2018-06-18 14:49:09 +00:00
genaccs[i] = acc
2018-08-15 22:24:55 +00:00
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewRat(numStartingTokensAccounts)) // increase the supply
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
// add the validator
if len(genTx.Name) > 0 {
desc := stake.NewDescription(genTx.Name, "", "", "")
validator := stake.NewValidator(genTx.Address,
sdk.MustGetAccPubKeyBech32(genTx.PubKey), desc)
2018-06-18 14:49:09 +00:00
2018-08-15 22:24:55 +00:00
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewRat(numStartingTokensValidators)) // increase the supply
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
// add some new shares to the validator
var issuedDelShares sdk.Rat
2018-08-15 22:24:55 +00:00
validator, stakeData.Pool, issuedDelShares = validator.AddTokensFromDel(stakeData.Pool, numStartingTokensValidators)
2018-08-14 22:13:54 +00:00
stakeData.Validators = append(stakeData.Validators, validator)
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
// create the self-delegation from the issuedDelShares
delegation := stake.Delegation{
DelegatorAddr: validator.Owner,
ValidatorAddr: validator.Owner,
Shares: issuedDelShares,
Height: 0,
}
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
stakeData.Bonds = append(stakeData.Bonds, delegation)
}
2018-06-18 14:49:09 +00:00
}
2018-08-14 22:13:54 +00:00
// create the final app state
genesisState = GenesisState{
Accounts: genaccs,
StakeData: stakeData,
2018-06-18 14:49:09 +00:00
}
return
}
2018-08-14 22:13:54 +00:00
// Run KavaAppGenState then convert to JSON
func KavaAppGenStateJSON(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) {
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
// create the final app state
genesisState, err := KavaAppGenState(cdc, appGenTxs)
2018-06-18 14:49:09 +00:00
if err != nil {
2018-08-14 22:13:54 +00:00
return nil, err
2018-06-18 14:49:09 +00:00
}
2018-08-14 22:13:54 +00:00
appState, err = wire.MarshalJSONIndent(cdc, genesisState)
2018-06-18 14:49:09 +00:00
return
}
2018-08-14 22:13:54 +00:00
/*
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
// A file, genesis.json, is created with the initial state of the kava network.
// This is done by creating an 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.
2018-06-18 14:49:09 +00:00
2018-08-14 22:13:54 +00:00
func CreateAppInit() server.AppInit {
return server.AppInit{
AppGenTx: KavaAppGenTx,
AppGenState: KavaAppGenState,
}
2018-06-18 14:49:09 +00:00
}
2018-08-14 22:13:54 +00:00
func KavaAppGenTx(cdc *wire.Codec, pk crypto.PubKey, genTxConfig config.GenTx) (
2018-06-18 14:49:09 +00:00
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
2018-08-14 22:13:54 +00:00
// Generate address and secret key for the validator
var addr sdk.AccAddress
2018-06-18 14:49:09 +00:00
var secret string
2018-08-14 22:13:54 +00:00
addr, secret, err = server.GenerateCoinKey()
2018-06-18 14:49:09 +00:00
if err != nil {
return
}
2018-08-14 22:13:54 +00:00
// Generate appGenTx -------------
2018-06-18 14:49:09 +00:00
var bz []byte
2018-08-14 22:13:54 +00:00
genTx := types.GenTx{
2018-06-18 14:49:09 +00:00
Address: addr,
PubKey: pk,
}
2018-08-14 22:13:54 +00:00
bz, err = wire.MarshalJSONIndent(cdc, genTx)
2018-06-18 14:49:09 +00:00
if err != nil {
return
}
appGenTx = json.RawMessage(bz)
2018-08-14 22:13:54 +00:00
// cliPrint -------------
mm := map[string]string{"secret": secret}
bz, err = cdc.MarshalJSON(mm)
if err != nil {
return
}
cliPrint = json.RawMessage(bz)
// validator -----------
2018-06-18 14:49:09 +00:00
validator = tmtypes.GenesisValidator{
PubKey: pk,
2018-08-14 22:13:54 +00:00
Power: 10,
2018-06-18 14:49:09 +00:00
}
2018-08-14 22:13:54 +00:00
2018-06-18 14:49:09 +00:00
return
}
2018-08-14 22:13:54 +00:00
func KavaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) {
2018-06-18 14:49:09 +00:00
if len(appGenTxs) == 0 {
2018-08-14 22:13:54 +00:00
err = errors.New("must provide at least 1 genesis transaction")
2018-06-18 14:49:09 +00:00
return
}
2018-08-14 22:13:54 +00:00
genaccs := make([]types.GenesisAccount, len(appGenTxs))
2018-06-18 14:49:09 +00:00
for i, appGenTx := range appGenTxs {
2018-08-14 22:13:54 +00:00
var genTx types.GenTx
2018-06-18 14:49:09 +00:00
err = cdc.UnmarshalJSON(appGenTx, &genTx)
if err != nil {
return
}
2018-08-14 22:13:54 +00:00
// create the genesis account
2018-06-18 14:49:09 +00:00
accAuth := auth.NewBaseAccountWithAddress(genTx.Address)
accAuth.Coins = sdk.Coins{
2018-08-14 22:13:54 +00:00
{"KVA", 10000000000},
2018-06-18 14:49:09 +00:00
}
2018-08-14 22:13:54 +00:00
acc := types.NewGenesisAccount(&accAuth)
2018-06-18 14:49:09 +00:00
genaccs[i] = acc
}
// create the final app state
2018-08-14 22:13:54 +00:00
genesisState := types.GenesisState{
Accounts: genaccs,
2018-06-18 14:49:09 +00:00
}
appState, err = wire.MarshalJSONIndent(cdc, genesisState)
2018-08-14 22:13:54 +00:00
2018-06-18 14:49:09 +00:00
return
}
2018-08-14 22:13:54 +00:00
2018-06-18 14:49:09 +00:00
*/