wip genesis parsing

This commit is contained in:
rigelrozanski 2018-02-07 03:23:30 +00:00
parent 7643dea255
commit 6eaafa496a
5 changed files with 73 additions and 29 deletions

View File

@ -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{}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
})
}

View File

@ -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)
}