During transaction processing we retrieve the type of msg and based on that we get the applicable handler from the router. This means that the router should only receive messages that it knows how to handle. Instead of using Get interfaces, we should cast to the actual type of transaction and then access the transaction details that way. It's okay to panic here, because if the DummyHandler receives a message that it cannot cast to the expected type it means something is wrong with the router. Instead of retrieving an arbitrary key by chance we should panic.
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/tendermint/abci/server"
|
|
cmn "github.com/tendermint/tmlibs/common"
|
|
dbm "github.com/tendermint/tmlibs/db"
|
|
"github.com/tendermint/tmlibs/log"
|
|
|
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
func main() {
|
|
|
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main")
|
|
|
|
db, err := dbm.NewGoLevelDB("basecoind", "data")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Capabilities key to access the main KVStore.
|
|
var capKeyMainStore = sdk.NewKVStoreKey("main")
|
|
|
|
// Create BaseApp.
|
|
var baseApp = bam.NewBaseApp("dummy", logger, db)
|
|
|
|
// Set mounts for BaseApp's MultiStore.
|
|
baseApp.MountStore(capKeyMainStore, sdk.StoreTypeIAVL)
|
|
|
|
// Set Tx decoder
|
|
baseApp.SetTxDecoder(decodeTx)
|
|
|
|
// Set a handler Route.
|
|
baseApp.Router().AddRoute("dummy", DummyHandler(capKeyMainStore))
|
|
|
|
// Load latest version.
|
|
if err := baseApp.LoadLatestVersion(capKeyMainStore); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Start the ABCI server
|
|
srv, err := server.NewServer("0.0.0.0:46658", "socket", baseApp)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
srv.Start()
|
|
|
|
// Wait forever
|
|
cmn.TrapSignal(func() {
|
|
// Cleanup
|
|
srv.Stop()
|
|
})
|
|
return
|
|
}
|
|
|
|
func DummyHandler(storeKey sdk.StoreKey) sdk.Handler {
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
|
dTx, ok := msg.(dummyTx)
|
|
if !ok {
|
|
panic("DummyHandler should only receive dummyTx")
|
|
}
|
|
|
|
// tx is already unmarshalled
|
|
key := dTx.key
|
|
value := dTx.value
|
|
|
|
store := ctx.KVStore(storeKey)
|
|
store.Set(key, value)
|
|
|
|
return sdk.Result{
|
|
Code: 0,
|
|
Log: fmt.Sprintf("set %s=%s", key, value),
|
|
}
|
|
}
|
|
}
|