Refactor BaseApp without breaking change
This commit is contained in:
parent
3f15ae7b15
commit
fa31165438
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
@ -15,8 +16,10 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/store"
|
"github.com/cosmos/cosmos-sdk/store"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/version"
|
"github.com/cosmos/cosmos-sdk/version"
|
||||||
"github.com/cosmos/cosmos-sdk/wire"
|
|
||||||
|
// TODO: Remove dependency on auth and wire
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Key to store the header in the DB itself.
|
// Key to store the header in the DB itself.
|
||||||
|
@ -42,7 +45,7 @@ type BaseApp struct {
|
||||||
// initialized on creation
|
// initialized on creation
|
||||||
Logger log.Logger
|
Logger log.Logger
|
||||||
name string // application name from abci.Info
|
name string // application name from abci.Info
|
||||||
cdc *wire.Codec // Amino codec
|
cdc *wire.Codec // Amino codec (DEPRECATED)
|
||||||
db dbm.DB // common DB backend
|
db dbm.DB // common DB backend
|
||||||
cms sdk.CommitMultiStore // Main (uncached) state
|
cms sdk.CommitMultiStore // Main (uncached) state
|
||||||
router Router // handle any kind of message
|
router Router // handle any kind of message
|
||||||
|
@ -74,6 +77,7 @@ var _ abci.Application = (*BaseApp)(nil)
|
||||||
// Create and name new BaseApp
|
// Create and name new BaseApp
|
||||||
// NOTE: The db is used to store the version number for now.
|
// NOTE: The db is used to store the version number for now.
|
||||||
// Accepts variable number of option functions, which act on the BaseApp to set configuration choices
|
// Accepts variable number of option functions, which act on the BaseApp to set configuration choices
|
||||||
|
// DEPRECATED
|
||||||
func NewBaseApp(name string, cdc *wire.Codec, logger log.Logger, db dbm.DB, options ...func(*BaseApp)) *BaseApp {
|
func NewBaseApp(name string, cdc *wire.Codec, logger log.Logger, db dbm.DB, options ...func(*BaseApp)) *BaseApp {
|
||||||
app := &BaseApp{
|
app := &BaseApp{
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
|
@ -83,7 +87,28 @@ func NewBaseApp(name string, cdc *wire.Codec, logger log.Logger, db dbm.DB, opti
|
||||||
cms: store.NewCommitMultiStore(db),
|
cms: store.NewCommitMultiStore(db),
|
||||||
router: NewRouter(),
|
router: NewRouter(),
|
||||||
codespacer: sdk.NewCodespacer(),
|
codespacer: sdk.NewCodespacer(),
|
||||||
txDecoder: defaultTxDecoder(cdc),
|
txDecoder: auth.DefaultTxDecoder(cdc),
|
||||||
|
}
|
||||||
|
// Register the undefined & root codespaces, which should not be used by any modules
|
||||||
|
app.codespacer.RegisterOrPanic(sdk.CodespaceRoot)
|
||||||
|
for _, option := range options {
|
||||||
|
option(app)
|
||||||
|
}
|
||||||
|
return app
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and name new BaseApp
|
||||||
|
// Does not set cdc and instead takes a user-defined txDecoder. If nil, BaseApp uses defaultTxDecoder
|
||||||
|
// TODO: Rename to NewBaseApp and remove above constructor once auth, wire dependencies removed
|
||||||
|
func NewBaseAppNoCodec(name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, options ...func(*BaseApp)) *BaseApp {
|
||||||
|
app := &BaseApp{
|
||||||
|
Logger: logger,
|
||||||
|
name: name,
|
||||||
|
db: db,
|
||||||
|
cms: store.NewCommitMultiStore(db),
|
||||||
|
router: NewRouter(),
|
||||||
|
codespacer: sdk.NewCodespacer(),
|
||||||
|
txDecoder: txDecoder,
|
||||||
}
|
}
|
||||||
// Register the undefined & root codespaces, which should not be used by any modules
|
// Register the undefined & root codespaces, which should not be used by any modules
|
||||||
app.codespacer.RegisterOrPanic(sdk.CodespaceRoot)
|
app.codespacer.RegisterOrPanic(sdk.CodespaceRoot)
|
||||||
|
@ -121,34 +146,11 @@ func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the txDecoder function
|
// Set the txDecoder function
|
||||||
|
// DEPRECATED
|
||||||
func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
|
func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
|
||||||
app.txDecoder = txDecoder
|
app.txDecoder = txDecoder
|
||||||
}
|
}
|
||||||
|
|
||||||
// default custom logic for transaction decoding
|
|
||||||
// TODO: remove auth and wire dependencies from baseapp
|
|
||||||
// - move this to auth.DefaultTxDecoder
|
|
||||||
// - set the default here to JSON decode like docs/examples/app1 (it will fail
|
|
||||||
// for multiple messages ;))
|
|
||||||
// - pass a TxDecoder into NewBaseApp, instead of a codec.
|
|
||||||
func defaultTxDecoder(cdc *wire.Codec) sdk.TxDecoder {
|
|
||||||
return func(txBytes []byte) (sdk.Tx, sdk.Error) {
|
|
||||||
var tx = auth.StdTx{}
|
|
||||||
|
|
||||||
if len(txBytes) == 0 {
|
|
||||||
return nil, sdk.ErrTxDecode("txBytes are empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
// StdTx.Msg is an interface. The concrete types
|
|
||||||
// are registered by MakeTxCodec
|
|
||||||
err := cdc.UnmarshalBinary(txBytes, &tx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, sdk.ErrTxDecode("").TraceSDK(err.Error())
|
|
||||||
}
|
|
||||||
return tx, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// nolint - Set functions
|
// nolint - Set functions
|
||||||
func (app *BaseApp) SetInitChainer(initChainer sdk.InitChainer) {
|
func (app *BaseApp) SetInitChainer(initChainer sdk.InitChainer) {
|
||||||
app.initChainer = initChainer
|
app.initChainer = initChainer
|
||||||
|
@ -349,7 +351,20 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abc
|
||||||
default:
|
default:
|
||||||
result = sdk.ErrUnknownRequest(fmt.Sprintf("Unknown query: %s", path)).Result()
|
result = sdk.ErrUnknownRequest(fmt.Sprintf("Unknown query: %s", path)).Result()
|
||||||
}
|
}
|
||||||
value := app.cdc.MustMarshalBinary(result)
|
|
||||||
|
// Encode with amino if defined, else use json
|
||||||
|
// TODO: Use JSON encoding only once app.cdc removed
|
||||||
|
var value []byte
|
||||||
|
if app.cdc != nil {
|
||||||
|
value = app.cdc.MustMarshalBinary(result)
|
||||||
|
} else {
|
||||||
|
var err error
|
||||||
|
value, err = json.Marshal(result)
|
||||||
|
if err != nil {
|
||||||
|
return sdk.ErrInternal("Encoding result failed").QueryResult()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return abci.ResponseQuery{
|
return abci.ResponseQuery{
|
||||||
Code: uint32(sdk.ABCICodeOK),
|
Code: uint32(sdk.ABCICodeOK),
|
||||||
Value: value,
|
Value: value,
|
||||||
|
|
|
@ -3,6 +3,7 @@ package baseapp
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -30,7 +31,7 @@ func newBaseApp(name string) *BaseApp {
|
||||||
db := dbm.NewMemDB()
|
db := dbm.NewMemDB()
|
||||||
codec := wire.NewCodec()
|
codec := wire.NewCodec()
|
||||||
registerTestCodec(codec)
|
registerTestCodec(codec)
|
||||||
return NewBaseApp(name, codec, logger, db)
|
return NewBaseAppNoCodec(name, logger, db, testTxDecoder(codec))
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerTestCodec(cdc *wire.Codec) {
|
func registerTestCodec(cdc *wire.Codec) {
|
||||||
|
@ -49,8 +50,6 @@ func setupBaseApp(t *testing.T) (*BaseApp, *sdk.KVStoreKey, *sdk.KVStoreKey) {
|
||||||
app := newBaseApp(t.Name())
|
app := newBaseApp(t.Name())
|
||||||
require.Equal(t, t.Name(), app.Name())
|
require.Equal(t, t.Name(), app.Name())
|
||||||
|
|
||||||
app.SetTxDecoder(testTxDecoder(app.cdc))
|
|
||||||
|
|
||||||
// make some cap keys
|
// make some cap keys
|
||||||
capKey1 := sdk.NewKVStoreKey("key1")
|
capKey1 := sdk.NewKVStoreKey("key1")
|
||||||
capKey2 := sdk.NewKVStoreKey("key2")
|
capKey2 := sdk.NewKVStoreKey("key2")
|
||||||
|
@ -85,7 +84,7 @@ func TestLoadVersion(t *testing.T) {
|
||||||
logger := defaultLogger()
|
logger := defaultLogger()
|
||||||
db := dbm.NewMemDB()
|
db := dbm.NewMemDB()
|
||||||
name := t.Name()
|
name := t.Name()
|
||||||
app := NewBaseApp(name, nil, logger, db)
|
app := NewBaseAppNoCodec(name, logger, db, nil)
|
||||||
|
|
||||||
// make a cap key and mount the store
|
// make a cap key and mount the store
|
||||||
capKey := sdk.NewKVStoreKey("main")
|
capKey := sdk.NewKVStoreKey("main")
|
||||||
|
@ -114,7 +113,7 @@ func TestLoadVersion(t *testing.T) {
|
||||||
commitID2 := sdk.CommitID{2, res.Data}
|
commitID2 := sdk.CommitID{2, res.Data}
|
||||||
|
|
||||||
// reload with LoadLatestVersion
|
// reload with LoadLatestVersion
|
||||||
app = NewBaseApp(name, nil, logger, db)
|
app = NewBaseAppNoCodec(name, logger, db, nil)
|
||||||
app.MountStoresIAVL(capKey)
|
app.MountStoresIAVL(capKey)
|
||||||
err = app.LoadLatestVersion(capKey)
|
err = app.LoadLatestVersion(capKey)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
@ -122,7 +121,7 @@ func TestLoadVersion(t *testing.T) {
|
||||||
|
|
||||||
// reload with LoadVersion, see if you can commit the same block and get
|
// reload with LoadVersion, see if you can commit the same block and get
|
||||||
// the same result
|
// the same result
|
||||||
app = NewBaseApp(name, nil, logger, db)
|
app = NewBaseAppNoCodec(name, logger, db, nil)
|
||||||
app.MountStoresIAVL(capKey)
|
app.MountStoresIAVL(capKey)
|
||||||
err = app.LoadVersion(1, capKey)
|
err = app.LoadVersion(1, capKey)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
@ -142,9 +141,7 @@ func testLoadVersionHelper(t *testing.T, app *BaseApp, expectedHeight int64, exp
|
||||||
func TestOptionFunction(t *testing.T) {
|
func TestOptionFunction(t *testing.T) {
|
||||||
logger := defaultLogger()
|
logger := defaultLogger()
|
||||||
db := dbm.NewMemDB()
|
db := dbm.NewMemDB()
|
||||||
codec := wire.NewCodec()
|
bap := NewBaseAppNoCodec("starting name", logger, db, nil, testChangeNameHelper("new name"))
|
||||||
registerTestCodec(codec)
|
|
||||||
bap := NewBaseApp("starting name", codec, logger, db, testChangeNameHelper("new name"))
|
|
||||||
require.Equal(t, bap.name, "new name", "BaseApp should have had name changed via option function")
|
require.Equal(t, bap.name, "new name", "BaseApp should have had name changed via option function")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,7 +213,7 @@ func TestInitChainer(t *testing.T) {
|
||||||
// we can reload the same app later
|
// we can reload the same app later
|
||||||
db := dbm.NewMemDB()
|
db := dbm.NewMemDB()
|
||||||
logger := defaultLogger()
|
logger := defaultLogger()
|
||||||
app := NewBaseApp(name, nil, logger, db)
|
app := NewBaseAppNoCodec(name, logger, db, nil)
|
||||||
capKey := sdk.NewKVStoreKey("main")
|
capKey := sdk.NewKVStoreKey("main")
|
||||||
capKey2 := sdk.NewKVStoreKey("key2")
|
capKey2 := sdk.NewKVStoreKey("key2")
|
||||||
app.MountStoresIAVL(capKey, capKey2)
|
app.MountStoresIAVL(capKey, capKey2)
|
||||||
|
@ -257,7 +254,7 @@ func TestInitChainer(t *testing.T) {
|
||||||
require.Equal(t, value, res.Value)
|
require.Equal(t, value, res.Value)
|
||||||
|
|
||||||
// reload app
|
// reload app
|
||||||
app = NewBaseApp(name, nil, logger, db)
|
app = NewBaseAppNoCodec(name, logger, db, nil)
|
||||||
app.MountStoresIAVL(capKey, capKey2)
|
app.MountStoresIAVL(capKey, capKey2)
|
||||||
err = app.LoadLatestVersion(capKey) // needed to make stores non-nil
|
err = app.LoadLatestVersion(capKey) // needed to make stores non-nil
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
@ -444,9 +441,13 @@ func TestCheckTx(t *testing.T) {
|
||||||
|
|
||||||
app.InitChain(abci.RequestInitChain{})
|
app.InitChain(abci.RequestInitChain{})
|
||||||
|
|
||||||
|
// Create same codec used in txDecoder
|
||||||
|
codec := wire.NewCodec()
|
||||||
|
registerTestCodec(codec)
|
||||||
|
|
||||||
for i := int64(0); i < nTxs; i++ {
|
for i := int64(0); i < nTxs; i++ {
|
||||||
tx := newTxCounter(i, 0)
|
tx := newTxCounter(i, 0)
|
||||||
txBytes, err := app.cdc.MarshalBinary(tx)
|
txBytes, err := codec.MarshalBinary(tx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
r := app.CheckTx(txBytes)
|
r := app.CheckTx(txBytes)
|
||||||
assert.True(t, r.IsOK(), fmt.Sprintf("%v", r))
|
assert.True(t, r.IsOK(), fmt.Sprintf("%v", r))
|
||||||
|
@ -481,6 +482,10 @@ func TestDeliverTx(t *testing.T) {
|
||||||
deliverKey := []byte("deliver-key")
|
deliverKey := []byte("deliver-key")
|
||||||
app.Router().AddRoute(typeMsgCounter, handlerMsgCounter(t, capKey, deliverKey))
|
app.Router().AddRoute(typeMsgCounter, handlerMsgCounter(t, capKey, deliverKey))
|
||||||
|
|
||||||
|
// Create same codec used in txDecoder
|
||||||
|
codec := wire.NewCodec()
|
||||||
|
registerTestCodec(codec)
|
||||||
|
|
||||||
nBlocks := 3
|
nBlocks := 3
|
||||||
txPerHeight := 5
|
txPerHeight := 5
|
||||||
for blockN := 0; blockN < nBlocks; blockN++ {
|
for blockN := 0; blockN < nBlocks; blockN++ {
|
||||||
|
@ -488,7 +493,7 @@ func TestDeliverTx(t *testing.T) {
|
||||||
for i := 0; i < txPerHeight; i++ {
|
for i := 0; i < txPerHeight; i++ {
|
||||||
counter := int64(blockN*txPerHeight + i)
|
counter := int64(blockN*txPerHeight + i)
|
||||||
tx := newTxCounter(counter, counter)
|
tx := newTxCounter(counter, counter)
|
||||||
txBytes, err := app.cdc.MarshalBinary(tx)
|
txBytes, err := codec.MarshalBinary(tx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
res := app.DeliverTx(txBytes)
|
res := app.DeliverTx(txBytes)
|
||||||
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
|
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
|
||||||
|
@ -518,12 +523,16 @@ func TestMultiMsgDeliverTx(t *testing.T) {
|
||||||
app.Router().AddRoute(typeMsgCounter, handlerMsgCounter(t, capKey, deliverKey))
|
app.Router().AddRoute(typeMsgCounter, handlerMsgCounter(t, capKey, deliverKey))
|
||||||
app.Router().AddRoute(typeMsgCounter2, handlerMsgCounter(t, capKey, deliverKey2))
|
app.Router().AddRoute(typeMsgCounter2, handlerMsgCounter(t, capKey, deliverKey2))
|
||||||
|
|
||||||
|
// Create same codec used in txDecoder
|
||||||
|
codec := wire.NewCodec()
|
||||||
|
registerTestCodec(codec)
|
||||||
|
|
||||||
// run a multi-msg tx
|
// run a multi-msg tx
|
||||||
// with all msgs the same type
|
// with all msgs the same type
|
||||||
{
|
{
|
||||||
app.BeginBlock(abci.RequestBeginBlock{})
|
app.BeginBlock(abci.RequestBeginBlock{})
|
||||||
tx := newTxCounter(0, 0, 1, 2)
|
tx := newTxCounter(0, 0, 1, 2)
|
||||||
txBytes, err := app.cdc.MarshalBinary(tx)
|
txBytes, err := codec.MarshalBinary(tx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
res := app.DeliverTx(txBytes)
|
res := app.DeliverTx(txBytes)
|
||||||
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
|
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
|
||||||
|
@ -544,7 +553,7 @@ func TestMultiMsgDeliverTx(t *testing.T) {
|
||||||
tx := newTxCounter(1, 3)
|
tx := newTxCounter(1, 3)
|
||||||
tx.Msgs = append(tx.Msgs, msgCounter2{0})
|
tx.Msgs = append(tx.Msgs, msgCounter2{0})
|
||||||
tx.Msgs = append(tx.Msgs, msgCounter2{1})
|
tx.Msgs = append(tx.Msgs, msgCounter2{1})
|
||||||
txBytes, err := app.cdc.MarshalBinary(tx)
|
txBytes, err := codec.MarshalBinary(tx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
res := app.DeliverTx(txBytes)
|
res := app.DeliverTx(txBytes)
|
||||||
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
|
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
|
||||||
|
@ -589,6 +598,10 @@ func TestSimulateTx(t *testing.T) {
|
||||||
})
|
})
|
||||||
app.InitChain(abci.RequestInitChain{})
|
app.InitChain(abci.RequestInitChain{})
|
||||||
|
|
||||||
|
// Create same codec used in txDecoder
|
||||||
|
codec := wire.NewCodec()
|
||||||
|
registerTestCodec(codec)
|
||||||
|
|
||||||
nBlocks := 3
|
nBlocks := 3
|
||||||
for blockN := 0; blockN < nBlocks; blockN++ {
|
for blockN := 0; blockN < nBlocks; blockN++ {
|
||||||
count := int64(blockN + 1)
|
count := int64(blockN + 1)
|
||||||
|
@ -607,7 +620,7 @@ func TestSimulateTx(t *testing.T) {
|
||||||
require.Equal(t, int64(gasConsumed), result.GasUsed)
|
require.Equal(t, int64(gasConsumed), result.GasUsed)
|
||||||
|
|
||||||
// simulate by calling Query with encoded tx
|
// simulate by calling Query with encoded tx
|
||||||
txBytes, err := app.cdc.MarshalBinary(tx)
|
txBytes, err := codec.MarshalBinary(tx)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
query := abci.RequestQuery{
|
query := abci.RequestQuery{
|
||||||
Path: "/app/simulate",
|
Path: "/app/simulate",
|
||||||
|
@ -617,7 +630,8 @@ func TestSimulateTx(t *testing.T) {
|
||||||
require.True(t, queryResult.IsOK(), queryResult.Log)
|
require.True(t, queryResult.IsOK(), queryResult.Log)
|
||||||
|
|
||||||
var res sdk.Result
|
var res sdk.Result
|
||||||
app.cdc.MustUnmarshalBinary(queryResult.Value, &res)
|
err = json.Unmarshal(queryResult.Value, &res)
|
||||||
|
require.Nil(t, err, "Result unmarshalling failed")
|
||||||
require.True(t, res.IsOK(), res.Log)
|
require.True(t, res.IsOK(), res.Log)
|
||||||
require.Equal(t, gasConsumed, res.GasUsed, res.Log)
|
require.Equal(t, gasConsumed, res.GasUsed, res.Log)
|
||||||
app.EndBlock(abci.RequestEndBlock{})
|
app.EndBlock(abci.RequestEndBlock{})
|
||||||
|
|
|
@ -60,7 +60,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp {
|
||||||
|
|
||||||
// create your application object
|
// create your application object
|
||||||
var app = &GaiaApp{
|
var app = &GaiaApp{
|
||||||
BaseApp: bam.NewBaseApp(appName, cdc, logger, db),
|
BaseApp: bam.NewBaseAppNoCodec(appName, logger, db, auth.DefaultTxDecoder(cdc)),
|
||||||
cdc: cdc,
|
cdc: cdc,
|
||||||
keyMain: sdk.NewKVStoreKey("main"),
|
keyMain: sdk.NewKVStoreKey("main"),
|
||||||
keyAccount: sdk.NewKVStoreKey("acc"),
|
keyAccount: sdk.NewKVStoreKey("acc"),
|
||||||
|
|
|
@ -9,7 +9,6 @@ import (
|
||||||
|
|
||||||
bapp "github.com/cosmos/cosmos-sdk/baseapp"
|
bapp "github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/wire"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -18,17 +17,12 @@ const (
|
||||||
|
|
||||||
func NewApp1(logger log.Logger, db dbm.DB) *bapp.BaseApp {
|
func NewApp1(logger log.Logger, db dbm.DB) *bapp.BaseApp {
|
||||||
|
|
||||||
cdc := wire.NewCodec()
|
|
||||||
|
|
||||||
// Create the base application object.
|
// Create the base application object.
|
||||||
app := bapp.NewBaseApp(app1Name, cdc, logger, db)
|
app := bapp.NewBaseAppNoCodec(app1Name, logger, db, tx1Decoder)
|
||||||
|
|
||||||
// Create a key for accessing the account store.
|
// Create a key for accessing the account store.
|
||||||
keyAccount := sdk.NewKVStoreKey("acc")
|
keyAccount := sdk.NewKVStoreKey("acc")
|
||||||
|
|
||||||
// Determine how transactions are decoded.
|
|
||||||
app.SetTxDecoder(txDecoder)
|
|
||||||
|
|
||||||
// Register message routes.
|
// Register message routes.
|
||||||
// Note the handler gets access to the account store.
|
// Note the handler gets access to the account store.
|
||||||
app.Router().
|
app.Router().
|
||||||
|
@ -225,7 +219,7 @@ func (tx app1Tx) GetMsgs() []sdk.Msg {
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON decode MsgSend.
|
// JSON decode MsgSend.
|
||||||
func txDecoder(txBytes []byte) (sdk.Tx, sdk.Error) {
|
func tx1Decoder(txBytes []byte) (sdk.Tx, sdk.Error) {
|
||||||
var tx app1Tx
|
var tx app1Tx
|
||||||
err := json.Unmarshal(txBytes, &tx)
|
err := json.Unmarshal(txBytes, &tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -37,7 +37,7 @@ func NewApp2(logger log.Logger, db dbm.DB) *bapp.BaseApp {
|
||||||
cdc := NewCodec()
|
cdc := NewCodec()
|
||||||
|
|
||||||
// Create the base application object.
|
// Create the base application object.
|
||||||
app := bapp.NewBaseApp(app2Name, cdc, logger, db)
|
app := bapp.NewBaseAppNoCodec(app2Name, logger, db, tx2Decoder(cdc))
|
||||||
|
|
||||||
// Create a key for accessing the account store.
|
// Create a key for accessing the account store.
|
||||||
keyAccount := sdk.NewKVStoreKey("acc")
|
keyAccount := sdk.NewKVStoreKey("acc")
|
||||||
|
@ -191,6 +191,19 @@ func (tx app2Tx) GetSignatures() []auth.StdSignature {
|
||||||
return tx.Signatures
|
return tx.Signatures
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Amino decode app2Tx. Capable of decoding both MsgSend and MsgIssue
|
||||||
|
func tx2Decoder(cdc *wire.Codec) sdk.TxDecoder {
|
||||||
|
return func(txBytes []byte) (sdk.Tx, sdk.Error) {
|
||||||
|
var tx app2Tx
|
||||||
|
err := cdc.UnmarshalBinary(txBytes, &tx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, sdk.ErrTxDecode(err.Error())
|
||||||
|
}
|
||||||
|
return tx, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
|
|
||||||
// Simple anteHandler that ensures msg signers have signed.
|
// Simple anteHandler that ensures msg signers have signed.
|
||||||
|
|
|
@ -21,7 +21,7 @@ func NewApp3(logger log.Logger, db dbm.DB) *bapp.BaseApp {
|
||||||
cdc := NewCodec()
|
cdc := NewCodec()
|
||||||
|
|
||||||
// Create the base application object.
|
// Create the base application object.
|
||||||
app := bapp.NewBaseApp(app3Name, cdc, logger, db)
|
app := bapp.NewBaseAppNoCodec(app3Name, logger, db, auth.DefaultTxDecoder(cdc))
|
||||||
|
|
||||||
// Create a key for accessing the account store.
|
// Create a key for accessing the account store.
|
||||||
keyAccount := sdk.NewKVStoreKey("acc")
|
keyAccount := sdk.NewKVStoreKey("acc")
|
||||||
|
|
|
@ -22,7 +22,7 @@ func NewApp4(logger log.Logger, db dbm.DB) *bapp.BaseApp {
|
||||||
cdc := NewCodec()
|
cdc := NewCodec()
|
||||||
|
|
||||||
// Create the base application object.
|
// Create the base application object.
|
||||||
app := bapp.NewBaseApp(app3Name, cdc, logger, db)
|
app := bapp.NewBaseAppNoCodec(app4Name, logger, db, auth.DefaultTxDecoder(cdc))
|
||||||
|
|
||||||
// Create a key for accessing the account store.
|
// Create a key for accessing the account store.
|
||||||
keyAccount := sdk.NewKVStoreKey("acc")
|
keyAccount := sdk.NewKVStoreKey("acc")
|
||||||
|
|
|
@ -53,7 +53,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
|
||||||
// create your application type
|
// create your application type
|
||||||
var app = &BasecoinApp{
|
var app = &BasecoinApp{
|
||||||
cdc: cdc,
|
cdc: cdc,
|
||||||
BaseApp: bam.NewBaseApp(appName, cdc, logger, db),
|
BaseApp: bam.NewBaseAppNoCodec(appName, logger, db, auth.DefaultTxDecoder(cdc)),
|
||||||
keyMain: sdk.NewKVStoreKey("main"),
|
keyMain: sdk.NewKVStoreKey("main"),
|
||||||
keyAccount: sdk.NewKVStoreKey("acc"),
|
keyAccount: sdk.NewKVStoreKey("acc"),
|
||||||
keyIBC: sdk.NewKVStoreKey("ibc"),
|
keyIBC: sdk.NewKVStoreKey("ibc"),
|
||||||
|
@ -78,6 +78,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
|
||||||
app.SetBeginBlocker(app.BeginBlocker)
|
app.SetBeginBlocker(app.BeginBlocker)
|
||||||
app.SetEndBlocker(app.EndBlocker)
|
app.SetEndBlocker(app.EndBlocker)
|
||||||
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
|
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
|
||||||
|
app.SetTxDecoder(auth.DefaultTxDecoder(cdc))
|
||||||
|
|
||||||
// mount the multistore and load the latest state
|
// mount the multistore and load the latest state
|
||||||
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC)
|
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC)
|
||||||
|
|
|
@ -58,7 +58,7 @@ func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp {
|
||||||
|
|
||||||
// Create your application object.
|
// Create your application object.
|
||||||
var app = &DemocoinApp{
|
var app = &DemocoinApp{
|
||||||
BaseApp: bam.NewBaseApp(appName, cdc, logger, db),
|
BaseApp: bam.NewBaseAppNoCodec(appName, logger, db, auth.DefaultTxDecoder(cdc)),
|
||||||
cdc: cdc,
|
cdc: cdc,
|
||||||
capKeyMainStore: sdk.NewKVStoreKey("main"),
|
capKeyMainStore: sdk.NewKVStoreKey("main"),
|
||||||
capKeyAccountStore: sdk.NewKVStoreKey("acc"),
|
capKeyAccountStore: sdk.NewKVStoreKey("acc"),
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/tendermint/tendermint/crypto"
|
"github.com/tendermint/tendermint/crypto"
|
||||||
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ sdk.Tx = (*StdTx)(nil)
|
var _ sdk.Tx = (*StdTx)(nil)
|
||||||
|
@ -162,3 +163,22 @@ type StdSignature struct {
|
||||||
AccountNumber int64 `json:"account_number"`
|
AccountNumber int64 `json:"account_number"`
|
||||||
Sequence int64 `json:"sequence"`
|
Sequence int64 `json:"sequence"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// default custom logic for transaction decoding
|
||||||
|
func DefaultTxDecoder(cdc *wire.Codec) sdk.TxDecoder {
|
||||||
|
return func(txBytes []byte) (sdk.Tx, sdk.Error) {
|
||||||
|
var tx = StdTx{}
|
||||||
|
|
||||||
|
if len(txBytes) == 0 {
|
||||||
|
return nil, sdk.ErrTxDecode("txBytes are empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// StdTx.Msg is an interface. The concrete types
|
||||||
|
// are registered by MakeTxCodec
|
||||||
|
err := cdc.UnmarshalBinary(txBytes, &tx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, sdk.ErrTxDecode("").TraceSDK(err.Error())
|
||||||
|
}
|
||||||
|
return tx, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ func NewApp() *App {
|
||||||
|
|
||||||
// Create your application object
|
// Create your application object
|
||||||
app := &App{
|
app := &App{
|
||||||
BaseApp: bam.NewBaseApp("mock", cdc, logger, db),
|
BaseApp: bam.NewBaseAppNoCodec("mock", logger, db, auth.DefaultTxDecoder(cdc)),
|
||||||
Cdc: cdc,
|
Cdc: cdc,
|
||||||
KeyMain: sdk.NewKVStoreKey("main"),
|
KeyMain: sdk.NewKVStoreKey("main"),
|
||||||
KeyAccount: sdk.NewKVStoreKey("acc"),
|
KeyAccount: sdk.NewKVStoreKey("acc"),
|
||||||
|
|
Loading…
Reference in New Issue