From 234d7498de03e98c10669b79ffcd6ae6b1a7fcc0 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 25 Jun 2018 22:43:57 -0400 Subject: [PATCH] docs/examples: templates for more examples --- docs/core/examples/{app1/main.go => app1.go} | 18 ++-- docs/core/examples/app2.go | 87 ++++++++++++++++++++ docs/core/examples/app3.go | 50 +++++++++++ docs/core/examples/app4.go | 49 +++++++++++ 4 files changed, 195 insertions(+), 9 deletions(-) rename docs/core/examples/{app1/main.go => app1.go} (90%) create mode 100644 docs/core/examples/app2.go create mode 100644 docs/core/examples/app3.go create mode 100644 docs/core/examples/app4.go diff --git a/docs/core/examples/app1/main.go b/docs/core/examples/app1.go similarity index 90% rename from docs/core/examples/app1/main.go rename to docs/core/examples/app1.go index 59cc39e08c..09c9aa134b 100644 --- a/docs/core/examples/app1/main.go +++ b/docs/core/examples/app1.go @@ -14,17 +14,17 @@ import ( ) const ( - appName = "MyApp" + app1Name = "App1" ) -func NewApp(logger log.Logger, db dbm.DB) *bapp.BaseApp { +func NewApp1(logger log.Logger, db dbm.DB) *bapp.BaseApp { // TODO: make this an interface or pass in // a TxDecoder instead. cdc := wire.NewCodec() // Create the base application object. - app := bapp.NewBaseApp(appName, cdc, logger, db) + app := bapp.NewBaseApp(app1Name, cdc, logger, db) // Create a key for accessing the account store. keyAccount := sdk.NewKVStoreKey("acc") @@ -35,7 +35,7 @@ func NewApp(logger log.Logger, db dbm.DB) *bapp.BaseApp { // Register message routes. // Note the handler gets access to the account store. app.Router(). - AddRoute("bank", NewHandler(keyAccount)) + AddRoute("bank", NewApp1Handler(keyAccount)) // Mount stores and load the latest state. app.MountStoresIAVL(keyAccount) @@ -99,7 +99,7 @@ func (msg MsgSend) GetSigners() []sdk.Address { //------------------------------------------------------------------ // Handler for the message -func NewHandler(keyAcc *sdk.KVStoreKey) sdk.Handler { +func NewApp1Handler(keyAcc *sdk.KVStoreKey) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { switch msg := msg.(type) { case MsgSend: @@ -142,23 +142,23 @@ type acc struct { // Tx // Simple tx to wrap the Msg. -type tx struct { +type app1Tx struct { MsgSend } // This tx only has one Msg. -func (tx tx) GetMsgs() []sdk.Msg { +func (tx app1Tx) GetMsgs() []sdk.Msg { return []sdk.Msg{tx.MsgSend} } // TODO: remove the need for this -func (tx tx) GetMemo() string { +func (tx app1Tx) GetMemo() string { return "" } // JSON decode MsgSend. func txDecoder(txBytes []byte) (sdk.Tx, sdk.Error) { - var tx tx + var tx app1Tx err := json.Unmarshal(txBytes, &tx) if err != nil { return nil, sdk.ErrTxDecode(err.Error()) diff --git a/docs/core/examples/app2.go b/docs/core/examples/app2.go new file mode 100644 index 0000000000..fec259ca99 --- /dev/null +++ b/docs/core/examples/app2.go @@ -0,0 +1,87 @@ +package app + +import ( + "reflect" + + cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" + "github.com/tendermint/tmlibs/log" + + bapp "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" +) + +const ( + app2Name = "App2" +) + +func NewCodec() *wire.Codec { + // TODO register + return nil +} + +func NewApp2(logger log.Logger, db dbm.DB) *bapp.BaseApp { + + cdc := NewCodec() + + // Create the base application object. + app := bapp.NewBaseApp(app2Name, cdc, logger, db) + + // Create a key for accessing the account store. + keyAccount := sdk.NewKVStoreKey("acc") + keyIssuer := sdk.NewKVStoreKey("issuer") + + // Register message routes. + // Note the handler gets access to the account store. + app.Router(). + AddRoute("bank", NewApp2Handler(keyAccount, keyIssuer)) + + // Mount stores and load the latest state. + app.MountStoresIAVL(keyAccount, keyIssuer) + err := app.LoadLatestVersion(keyAccount) + if err != nil { + cmn.Exit(err.Error()) + } + return app +} + +//------------------------------------------------------------------ +// Msgs + +// TODO: MsgIssue + +//------------------------------------------------------------------ +// Handler for the message + +func NewApp1Handler(keyAcc *sdk.KVStoreKey) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { + switch msg := msg.(type) { + case MsgSend: + return handleMsgSend(ctx, keyAcc, msg) + case MsgIssue: + // TODO + default: + errMsg := "Unrecognized bank Msg type: " + reflect.TypeOf(msg).Name() + return sdk.ErrUnknownRequest(errMsg).Result() + } + } +} + +//------------------------------------------------------------------ +// Tx + +// Simple tx to wrap the Msg. +type app2Tx struct { + sdk.Msg +} + +// This tx only has one Msg. +func (tx app2Tx) GetMsgs() []sdk.Msg { + return []sdk.Msg{tx.Msg} +} + +// TODO: remove the need for this +func (tx app2Tx) GetMemo() string { + return "" +} diff --git a/docs/core/examples/app3.go b/docs/core/examples/app3.go new file mode 100644 index 0000000000..b176816b75 --- /dev/null +++ b/docs/core/examples/app3.go @@ -0,0 +1,50 @@ +package app + +import ( + cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" + "github.com/tendermint/tmlibs/log" + + bapp "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +const ( + app3Name = "App3" +) + +func NewApp3(logger log.Logger, db dbm.DB) *bapp.BaseApp { + + cdc := NewCodec() + + // Create the base application object. + app := bapp.NewBaseApp(app3Name, cdc, logger, db) + + // Create a key for accessing the account store. + keyAccount := sdk.NewKVStoreKey("acc") + keyIssuer := sdk.NewKVStoreKey("issuer") + + // TODO: accounts, ante handler + + // Register message routes. + // Note the handler gets access to the account store. + app.Router(). + AddRoute("bank", NewApp2Handler(keyAccount, keyIssuer)) + + // Mount stores and load the latest state. + app.MountStoresIAVL(keyAccount, keyIssuer) + err := app.LoadLatestVersion(keyAccount) + if err != nil { + cmn.Exit(err.Error()) + } + return app +} + +//------------------------------------------------------------------ +// StdTx + +//------------------------------------------------------------------ +// Account + +//------------------------------------------------------------------ +// Ante Handler diff --git a/docs/core/examples/app4.go b/docs/core/examples/app4.go new file mode 100644 index 0000000000..ae63b4afba --- /dev/null +++ b/docs/core/examples/app4.go @@ -0,0 +1,49 @@ +package app + +import ( + cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" + "github.com/tendermint/tmlibs/log" + + bapp "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +const ( + app4Name = "App4" +) + +func NewApp4(logger log.Logger, db dbm.DB) *bapp.BaseApp { + + cdc := NewCodec() + + // Create the base application object. + app := bapp.NewBaseApp(app4Name, cdc, logger, db) + + // Create a key for accessing the account store. + keyAccount := sdk.NewKVStoreKey("acc") + keyIssuer := sdk.NewKVStoreKey("issuer") + + // TODO: accounts, ante handler + + // TODO: AccountMapper, CoinKeepr + + // Register message routes. + // Note the handler gets access to the account store. + app.Router(). + AddRoute("bank", NewApp2Handler(keyAccount, keyIssuer)) + + // Mount stores and load the latest state. + app.MountStoresIAVL(keyAccount, keyIssuer) + err := app.LoadLatestVersion(keyAccount) + if err != nil { + cmn.Exit(err.Error()) + } + return app +} + +//------------------------------------------------------------------ +// AccountMapper + +//------------------------------------------------------------------ +// CoinsKeeper