Skip to content

Commit

Permalink
fixes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Feb 13, 2018
1 parent 658d763 commit d9ebe34
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 104 deletions.
6 changes: 4 additions & 2 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
45 changes: 3 additions & 42 deletions baseapp/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions examples/basecoin/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 36 additions & 43 deletions examples/basecoin/app/init_baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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, "")
Expand All @@ -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
}
15 changes: 8 additions & 7 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d9ebe34

Please sign in to comment.