diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 2141d17ab..e92f58bdc 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -35,8 +35,9 @@ type BaseApp struct { // unmarshal []byte into sdk.Tx txDecoder sdk.TxDecoder - // unmarshal rawjsonbytes to the initialize application - InitStater sdk.InitStater // TODO make unexposed once certain refactoring from basecoin -> baseapp + // unmarshal rawjsonbytes to initialize the application + // TODO unexpose and call from InitChain + InitStater sdk.InitStater // ante handler for fee and auth defaultAnteHandler sdk.AnteHandler @@ -204,6 +205,7 @@ func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOp // Implements ABCI func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) { // TODO: Use req.Validators + // TODO: Use req.AppStateJSON (?) return } diff --git a/baseapp/genesis.go b/baseapp/genesis.go index 943f24bb8..b2dad3e33 100644 --- a/baseapp/genesis.go +++ b/baseapp/genesis.go @@ -3,53 +3,14 @@ package baseapp import ( "encoding/json" "io/ioutil" - "time" - - crypto "github.com/tendermint/go-crypto" - cmn "github.com/tendermint/tmlibs/common" ) -// TODO this is dup code from tendermint-core to avoid dep issues -// should probably remove from both SDK / Tendermint and move to tmlibs -// ^^^^^^^^^^^^^ This is my preference to avoid DEP hell -// or sync up versioning and just reference tendermint +// TODO: remove from here and pass the AppState +// through InitChain // GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set. type GenesisDoc struct { - GenesisTime time.Time `json:"genesis_time"` - ChainID string `json:"chain_id"` - ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"` - Validators []GenesisValidator `json:"validators"` - AppHash cmn.HexBytes `json:"app_hash"` - AppState json.RawMessage `json:"app_state,omitempty"` -} - -//nolint TODO remove -type ConsensusParams struct { - BlockSize `json:"block_size_params"` - TxSize `json:"tx_size_params"` - BlockGossip `json:"block_gossip_params"` - EvidenceParams `json:"evidence_params"` -} -type GenesisValidator struct { - PubKey crypto.PubKey `json:"pub_key"` - Power int64 `json:"power"` - Name string `json:"name"` -} -type BlockSize struct { - MaxBytes int `json:"max_bytes"` // NOTE: must not be 0 nor greater than 100MB - MaxTxs int `json:"max_txs"` - MaxGas int64 `json:"max_gas"` -} -type TxSize struct { - MaxBytes int `json:"max_bytes"` - MaxGas int64 `json:"max_gas"` -} -type BlockGossip struct { - BlockPartSizeBytes int `json:"block_part_size_bytes"` // NOTE: must not be 0 -} -type EvidenceParams struct { - MaxAge int64 `json:"max_age"` // only accept new evidence more recent than this + AppState json.RawMessage `json:"app_state,omitempty"` } // GenesisDocFromFile reads JSON data from a file and unmarshalls it into a GenesisDoc. diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index daba43f89..4f26d4944 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -51,15 +51,15 @@ func NewBasecoinApp(genesisPath string) *BasecoinApp { panic(fmt.Errorf("error loading genesis state: %v", err)) } - // TODO: InitChain with validators from genesis transaction bytes - // set up the cache store for ctx, get ctx + // TODO: can InitChain handle this too ? app.BaseApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{}}) ctx := app.BaseApp.NewContext(false, nil) // context for DeliverTx + // TODO: combine with InitChain and let tendermint invoke it. err = app.BaseApp.InitStater(ctx, genesisiDoc.AppState) if err != nil { - panic(fmt.Errorf("error loading application genesis state: %v", err)) + panic(fmt.Errorf("error initializing application genesis state: %v", err)) } app.loadStores() diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index edc4deeef..ce071eeed 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -52,17 +52,17 @@ func TestSendMsg(t *testing.T) { coins, err := sdk.ParseCoins("77foocoin,99barcoin") require.Nil(t, err) baseAcc := auth.BaseAccount{ - Address: addr, - Coins: coins, - PubKey: pk, - Sequence: 0, + Address: addr, + Coins: coins, } acc := &types.AppAccount{baseAcc, "foobart"} - gaccs := []*GenesisAccount{ - NewGenesisAccount(acc), + genesisState := GenesisState{ + Accounts: []*GenesisAccount{ + NewGenesisAccount(acc), + }, } - bytes, err := json.MarshalIndent(&gaccs, "", "\t") + bytes, err := json.MarshalIndent(genesisState, "", "\t") app := tba.BasecoinApp ctx := app.BaseApp.NewContext(false, nil) // context for DeliverTx diff --git a/examples/basecoin/app/init_baseapp.go b/examples/basecoin/app/init_baseapp.go index aac14e4a5..c1fcb94c6 100644 --- a/examples/basecoin/app/init_baseapp.go +++ b/examples/basecoin/app/init_baseapp.go @@ -8,7 +8,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" crypto "github.com/tendermint/go-crypto" - cmn "github.com/tendermint/tmlibs/common" ) // initCapKeys, initBaseApp, initStores, initHandlers. @@ -34,44 +33,6 @@ func (app *BasecoinApp) initBaseAppTxDecoder() { }) } -// We use GenesisAccount instead of types.AppAccount for cleaner json input of PubKey -type GenesisAccount struct { - Name string `json:"name"` - Address crypto.Address `json:"address"` - Coins sdk.Coins `json:"coins"` - PubKey cmn.HexBytes `json:"public_key"` - Sequence int64 `json:"sequence"` -} - -func NewGenesisAccount(aa *types.AppAccount) *GenesisAccount { - return &GenesisAccount{ - Name: aa.Name, - Address: aa.Address, - Coins: aa.Coins, - PubKey: aa.PubKey.Bytes(), - Sequence: aa.Sequence, - } -} - -// convert GenesisAccount to AppAccount -func (ga *GenesisAccount) toAppAccount() (acc *types.AppAccount, err error) { - - pk, err := crypto.PubKeyFromBytes(ga.PubKey) - if err != nil { - return - } - baseAcc := auth.BaseAccount{ - Address: ga.Address, - Coins: ga.Coins, - PubKey: pk, - Sequence: ga.Sequence, - } - return &types.AppAccount{ - BaseAccount: baseAcc, - Name: ga.Name, - }, nil -} - // define the custom logic for basecoin initialization func (app *BasecoinApp) initBaseAppInitStater() { accountMapper := app.accountMapper @@ -81,14 +42,13 @@ func (app *BasecoinApp) initBaseAppInitStater() { return nil } - var gaccs []*GenesisAccount - - err := json.Unmarshal(state, &gaccs) + genesisState := new(GenesisState) + err := json.Unmarshal(state, genesisState) if err != nil { return sdk.ErrGenesisParse("").TraceCause(err, "") } - for _, gacc := range gaccs { + for _, gacc := range genesisState.Accounts { acc, err := gacc.toAppAccount() if err != nil { return sdk.ErrGenesisParse("").TraceCause(err, "") @@ -98,3 +58,36 @@ func (app *BasecoinApp) initBaseAppInitStater() { return nil }) } + +//----------------------------------------------------- + +type GenesisState struct { + Accounts []*GenesisAccount `accounts` +} + +// GenesisAccount doesn't need pubkey or sequence +type GenesisAccount struct { + Name string `json:"name"` + Address crypto.Address `json:"address"` + Coins sdk.Coins `json:"coins"` +} + +func NewGenesisAccount(aa *types.AppAccount) *GenesisAccount { + return &GenesisAccount{ + Name: aa.Name, + Address: aa.Address, + Coins: aa.Coins, + } +} + +// convert GenesisAccount to AppAccount +func (ga *GenesisAccount) toAppAccount() (acc *types.AppAccount, err error) { + baseAcc := auth.BaseAccount{ + Address: ga.Address, + Coins: ga.Coins, + } + return &types.AppAccount{ + BaseAccount: baseAcc, + Name: ga.Name, + }, nil +} diff --git a/types/errors.go b/types/errors.go index f54a8fcc1..77cea98ac 100644 --- a/types/errors.go +++ b/types/errors.go @@ -24,13 +24,14 @@ const ( CodeOK CodeType = 0 CodeInternal CodeType = 1 CodeTxParse CodeType = 2 - CodeGenesisParse CodeType = 3 - CodeBadNonce CodeType = 4 - CodeUnauthorized CodeType = 5 - CodeInsufficientFunds CodeType = 6 - CodeUnknownRequest CodeType = 7 - CodeUnrecognizedAddress CodeType = 8 - CodeInvalidSequence CodeType = 9 + CodeBadNonce CodeType = 3 + CodeUnauthorized CodeType = 4 + CodeInsufficientFunds CodeType = 5 + CodeUnknownRequest CodeType = 6 + CodeUnrecognizedAddress CodeType = 7 + CodeInvalidSequence CodeType = 8 + + CodeGenesisParse CodeType = 0xdead // TODO: remove ? ) // NOTE: Don't stringer this, we'll put better messages in later.