From 6eaafa496af3a394094d8920ea2434d7b7c4b73b Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Feb 2018 03:23:30 +0000 Subject: [PATCH] wip genesis parsing --- baseapp/baseapp.go | 16 +++++++------- baseapp/testapp.go | 17 +++++++-------- examples/basecoin/app/app_test.go | 31 +++++++++++++++++++++++++-- examples/basecoin/app/init_baseapp.go | 20 +++++++++++++---- types/errors.go | 18 ++++++++++------ 5 files changed, 73 insertions(+), 29 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 4e0f3e003e..80cd19eebe 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -32,13 +32,16 @@ type BaseApp struct { // Main (uncached) state cms sdk.CommitMultiStore - // Unmarshal []byte into sdk.Tx + // unmarshal []byte into sdk.Tx txDecoder sdk.TxDecoder - // Ante handler for fee and auth + // unmarshal rawjsonbytes to the initialize application + initStater sdk.InitStater + + // ante handler for fee and auth defaultAnteHandler sdk.AnteHandler - // Handle any kind of message + // handle any kind of message router Router //-------------------- @@ -50,14 +53,11 @@ type BaseApp struct { // DeliverTx state, a cache-wrap of `.cms` msDeliver sdk.CacheMultiStore - // Current block header + // current block header header *abci.Header - // Cached validator changes from DeliverTx + // cached validator changes from DeliverTx valUpdates []abci.Validator - - // function to - initStater sdk.InitStater } var _ abci.Application = &BaseApp{} diff --git a/baseapp/testapp.go b/baseapp/testapp.go index aa13c62b2d..389593d65a 100644 --- a/baseapp/testapp.go +++ b/baseapp/testapp.go @@ -17,7 +17,6 @@ type TestApp struct { *abci.ResponseEndBlock } -// NewTestApp - new app for tests func NewTestApp(bapp *BaseApp) *TestApp { app := &TestApp{ BaseApp: bapp, @@ -25,7 +24,7 @@ func NewTestApp(bapp *BaseApp) *TestApp { return app } -// RunBeginBlock - Execute BaseApp BeginBlock +// execute BaseApp BeginBlock func (tapp *TestApp) RunBeginBlock() { if tapp.header != nil { panic("TestApp.header not nil, BeginBlock already run, or EndBlock not yet run.") @@ -58,43 +57,43 @@ func (tapp *TestApp) ensureBeginBlock() { } } -// RunCheckTx - run tx through CheckTx of TestApp +// run tx through CheckTx of TestApp func (tapp *TestApp) RunCheckTx(tx sdk.Tx) sdk.Result { tapp.ensureBeginBlock() return tapp.BaseApp.runTx(true, nil, tx) } -// RunDeliverTx - run tx through DeliverTx of TestApp +// run tx through DeliverTx of TestApp func (tapp *TestApp) RunDeliverTx(tx sdk.Tx) sdk.Result { tapp.ensureBeginBlock() return tapp.BaseApp.runTx(false, nil, tx) } -// RunCheckMsg - run tx through CheckTx of TestApp +// run tx through CheckTx of TestApp // NOTE: Skips authentication by wrapping msg in testTx{}. func (tapp *TestApp) RunCheckMsg(msg sdk.Msg) sdk.Result { var tx = testTx{msg} return tapp.RunCheckTx(tx) } -// RunDeliverMsg - run tx through DeliverTx of TestApp +// run tx through DeliverTx of TestApp // NOTE: Skips authentication by wrapping msg in testTx{}. func (tapp *TestApp) RunDeliverMsg(msg sdk.Msg) sdk.Result { var tx = testTx{msg} return tapp.RunDeliverTx(tx) } -// CommitMultiStore - return the commited multistore +// return the commited multistore func (tapp *TestApp) CommitMultiStore() sdk.CommitMultiStore { return tapp.BaseApp.cms } -// MultiStoreCheck - return a cache-wrap CheckTx state of multistore +// return a cache-wrap CheckTx state of multistore func (tapp *TestApp) MultiStoreCheck() sdk.MultiStore { return tapp.BaseApp.msCheck } -// MultiStoreDeliver - return a cache-wrap DeliverTx state of multistore +// return a cache-wrap DeliverTx state of multistore func (tapp *TestApp) MultiStoreDeliver() sdk.MultiStore { return tapp.BaseApp.msDeliver } diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 206257d2cc..15b98bd4ca 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -1,11 +1,17 @@ package app import ( + "encoding/json" "testing" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/examples/basecoin/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + crypto "github.com/tendermint/go-crypto" ) @@ -37,4 +43,25 @@ func TestSendMsg(t *testing.T) { // Run a Deliver on SendMsg. res = tba.RunDeliverMsg(msg) assert.Equal(t, sdk.CodeUnrecognizedAddress, res.Code, res.Log) + + // TODO seperate this test, need a closer on db? keep getting resource unavailable + + // construct some genesis bytes to reflect basecoin/types/AppAccount + pk := crypto.GenPrivKeyEd25519().PubKey() + addr := pk.Address() + coins, err := sdk.ParseCoins("77foocoin,99barcoin") + require.Nil(t, err) + baseAcc := auth.BaseAccount{ + Address: addr, + Coins: coins, + PubKey: pk, + Sequence: 0, + } + accs := []types.AppAccount{ + {baseAcc, "foobart"}, + {baseAcc, "endofzeworld"}, + } + bytes, err := json.MarshalIndent(&accs, "", "\t") + _ = bytes + // XXX test the json bytes in the InitStater } diff --git a/examples/basecoin/app/init_baseapp.go b/examples/basecoin/app/init_baseapp.go index 7ae6dd57d0..7b3bb7bcaa 100644 --- a/examples/basecoin/app/init_baseapp.go +++ b/examples/basecoin/app/init_baseapp.go @@ -1,7 +1,10 @@ package app import ( + "encoding/json" + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/examples/basecoin/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -28,12 +31,21 @@ func (app *BasecoinApp) initBaseAppTxDecoder() { }) } -// used to define the custom logic for initialization +// define the custom logic for basecoin initialization func (app *BasecoinApp) initBaseAppInitStater() { - //accountMapper := app.accountMapper + accountMapper := app.accountMapper app.BaseApp.SetInitStater(func(ctx sdk.Context, stateJSON []byte) sdk.Error { - // TODO: parse JSON - //accountMapper.SetAccount(ctx, ...) + + var accs []*types.AppAccount + + err := json.Unmarshal(stateJSON, &accs) + if err != nil { + return sdk.ErrGenesisParse("").TraceCause(err, "") + } + + for _, acc := range accs { + accountMapper.SetAccount(ctx, acc) + } return nil }) } diff --git a/types/errors.go b/types/errors.go index 2290ff833e..f54a8fcc17 100644 --- a/types/errors.go +++ b/types/errors.go @@ -24,12 +24,13 @@ const ( CodeOK CodeType = 0 CodeInternal CodeType = 1 CodeTxParse CodeType = 2 - CodeBadNonce CodeType = 3 - CodeUnauthorized CodeType = 4 - CodeInsufficientFunds CodeType = 5 - CodeUnknownRequest CodeType = 6 - CodeUnrecognizedAddress CodeType = 7 - CodeInvalidSequence CodeType = 8 + CodeGenesisParse CodeType = 3 + CodeBadNonce CodeType = 4 + CodeUnauthorized CodeType = 5 + CodeInsufficientFunds CodeType = 6 + CodeUnknownRequest CodeType = 7 + CodeUnrecognizedAddress CodeType = 8 + CodeInvalidSequence CodeType = 9 ) // NOTE: Don't stringer this, we'll put better messages in later. @@ -39,6 +40,8 @@ func CodeToDefaultMsg(code CodeType) string { return "Internal error" case CodeTxParse: return "Tx parse error" + case CodeGenesisParse: + return "Genesis parse error" case CodeBadNonce: return "Bad nonce" case CodeUnauthorized: @@ -67,6 +70,9 @@ func ErrInternal(msg string) Error { func ErrTxParse(msg string) Error { return newError(CodeTxParse, msg) } +func ErrGenesisParse(msg string) Error { + return newError(CodeGenesisParse, msg) +} func ErrBadNonce(msg string) Error { return newError(CodeBadNonce, msg) }