Skip to content

Commit

Permalink
wip genesis parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Feb 8, 2018
1 parent 7643dea commit 6eaafa4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 28 deletions.
16 changes: 8 additions & 8 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

//--------------------
Expand All @@ -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{}
Expand Down
17 changes: 8 additions & 9 deletions baseapp/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ type TestApp struct {
*abci.ResponseEndBlock
}

// NewTestApp - new app for tests
func NewTestApp(bapp *BaseApp) *TestApp {
app := &TestApp{
BaseApp: bapp,
}
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.")
Expand Down Expand Up @@ -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
}
Expand Down
29 changes: 28 additions & 1 deletion examples/basecoin/app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package app

import (
"encoding/json"
"testing"

"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"
"github.com/stretchr/testify/assert"

crypto "github.com/tendermint/go-crypto"
)

Expand Down Expand Up @@ -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
}
20 changes: 16 additions & 4 deletions examples/basecoin/app/init_baseapp.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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
})
}
18 changes: 12 additions & 6 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 6eaafa4

Please sign in to comment.