2017-12-30 16:45:53 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-03-28 08:52:18 -07:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2017-12-30 16:45:53 -08:00
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
"github.com/tendermint/tendermint/abci/server"
|
2018-07-02 13:34:06 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2017-12-30 16:45:53 -08:00
|
|
|
|
2018-01-20 20:08:00 -08:00
|
|
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
2018-01-15 17:01:51 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2017-12-30 16:45:53 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2018-02-17 13:19:34 -08:00
|
|
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main")
|
|
|
|
|
2018-03-28 08:52:18 -07:00
|
|
|
rootDir := viper.GetString(cli.HomeFlag)
|
|
|
|
db, err := dbm.NewGoLevelDB("basecoind", filepath.Join(rootDir, "data"))
|
2018-02-17 13:19:34 -08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Capabilities key to access the main KVStore.
|
|
|
|
var capKeyMainStore = sdk.NewKVStoreKey("main")
|
2018-01-15 17:01:51 -08:00
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Create BaseApp.
|
2018-07-18 16:24:16 -07:00
|
|
|
var baseApp = bam.NewBaseApp("kvstore", logger, db, decodeTx)
|
2017-12-30 16:45:53 -08:00
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Set mounts for BaseApp's MultiStore.
|
2018-03-28 05:33:48 -07:00
|
|
|
baseApp.MountStoresIAVL(capKeyMainStore)
|
2018-01-15 17:01:51 -08:00
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Set a handler Route.
|
2018-04-18 21:49:24 -07:00
|
|
|
baseApp.Router().AddRoute("kvstore", Handler(capKeyMainStore))
|
2018-01-15 17:01:51 -08:00
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
// Load latest version.
|
|
|
|
if err := baseApp.LoadLatestVersion(capKeyMainStore); err != nil {
|
2017-12-30 16:45:53 -08:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the ABCI server
|
2018-06-13 15:13:51 -07:00
|
|
|
srv, err := server.NewServer("0.0.0.0:26658", "socket", baseApp)
|
2017-12-30 16:45:53 -08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-06-28 15:52:10 -07:00
|
|
|
err = srv.Start()
|
|
|
|
if err != nil {
|
|
|
|
cmn.Exit(err.Error())
|
|
|
|
}
|
2017-12-30 16:45:53 -08:00
|
|
|
|
|
|
|
// Wait forever
|
|
|
|
cmn.TrapSignal(func() {
|
|
|
|
// Cleanup
|
2018-06-28 15:52:10 -07:00
|
|
|
err = srv.Stop()
|
|
|
|
if err != nil {
|
|
|
|
cmn.Exit(err.Error())
|
|
|
|
}
|
2017-12-30 16:45:53 -08:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// KVStore Handler
|
|
|
|
func Handler(storeKey sdk.StoreKey) sdk.Handler {
|
2018-01-26 06:22:56 -08:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
2018-02-19 14:17:06 -08:00
|
|
|
dTx, ok := msg.(kvstoreTx)
|
2018-02-09 00:17:01 -08:00
|
|
|
if !ok {
|
2018-04-18 21:49:24 -07:00
|
|
|
panic("Handler should only receive kvstoreTx")
|
2018-02-09 00:17:01 -08:00
|
|
|
}
|
|
|
|
|
2018-01-15 17:01:51 -08:00
|
|
|
// tx is already unmarshalled
|
2018-02-09 00:17:01 -08:00
|
|
|
key := dTx.key
|
|
|
|
value := dTx.value
|
2017-12-30 16:45:53 -08:00
|
|
|
|
2018-01-15 17:01:51 -08:00
|
|
|
store := ctx.KVStore(storeKey)
|
|
|
|
store.Set(key, value)
|
2017-12-30 16:45:53 -08:00
|
|
|
|
2018-01-15 17:01:51 -08:00
|
|
|
return sdk.Result{
|
|
|
|
Code: 0,
|
|
|
|
Log: fmt.Sprintf("set %s=%s", key, value),
|
|
|
|
}
|
2017-12-30 16:45:53 -08:00
|
|
|
}
|
|
|
|
}
|