cosmos-sdk/examples/dummy/main.go

144 lines
2.7 KiB
Go
Raw Normal View History

2017-12-30 16:45:53 -08:00
package main
import (
"bytes"
"fmt"
"os"
"github.com/tendermint/abci/server"
2018-01-06 14:22:21 -08:00
crypto "github.com/tendermint/go-crypto"
2017-12-30 16:45:53 -08:00
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
bam "github.com/cosmos/cosmos-sdk/baseapp"
2017-12-30 16:45:53 -08:00
"github.com/cosmos/cosmos-sdk/store"
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() {
db, err := dbm.NewGoLevelDB("dummy", "dummy-data")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// create CommitStoreLoader
cacheSize := 10000
numHistory := int64(100)
loader := store.NewIAVLStoreLoader(db, cacheSize, numHistory)
2018-01-15 17:01:51 -08:00
// key to access the main KVStore
var mainStoreKey = sdk.NewKVStoreKey("main")
2017-12-30 16:45:53 -08:00
// Create MultiStore
2018-01-06 12:31:48 -08:00
multiStore := store.NewCommitMultiStore(db)
2018-01-15 17:01:51 -08:00
multiStore.SetSubstoreLoader(mainStoreKey, loader)
2017-12-30 16:45:53 -08:00
// Set everything on the baseApp and load latest
baseApp := bam.NewBaseApp("dummy", multiStore)
2018-01-15 17:01:51 -08:00
// Set Tx decoder
baseApp.SetTxDecoder(decodeTx)
2018-01-15 17:01:51 -08:00
baseApp.Router().AddRoute("dummy", DummyHandler(mainStoreKey))
2018-01-15 17:01:51 -08:00
if err := baseApp.LoadLatestVersion(mainStoreKey); err != nil {
2017-12-30 16:45:53 -08:00
fmt.Println(err)
os.Exit(1)
}
// Start the ABCI server
srv, err := server.NewServer("0.0.0.0:46658", "socket", baseApp)
2017-12-30 16:45:53 -08:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
srv.Start()
// Wait forever
cmn.TrapSignal(func() {
// Cleanup
srv.Stop()
})
return
}
type dummyTx struct {
key []byte
value []byte
bytes []byte
}
func (tx dummyTx) Get(key interface{}) (value interface{}) {
switch k := key.(type) {
case string:
switch k {
case "key":
return tx.key
case "value":
return tx.value
}
}
return nil
}
2018-01-15 17:01:51 -08:00
func (tx dummyTx) Type() string {
return "dummy"
}
func (tx dummyTx) GetSignBytes() []byte {
2017-12-30 16:45:53 -08:00
return tx.bytes
}
// Should the app be calling this? Or only handlers?
func (tx dummyTx) ValidateBasic() error {
return nil
}
2018-01-15 17:01:51 -08:00
func (tx dummyTx) GetSigners() []crypto.Address {
2017-12-30 16:45:53 -08:00
return nil
}
2018-01-15 17:01:51 -08:00
func (tx dummyTx) GetSignatures() []sdk.StdSignature {
return nil
}
func (tx dummyTx) GetFeePayer() crypto.Address {
2017-12-30 16:45:53 -08:00
return nil
}
2018-01-15 17:01:51 -08:00
func decodeTx(txBytes []byte) (sdk.Tx, error) {
var tx sdk.Tx
2017-12-30 16:45:53 -08:00
split := bytes.Split(txBytes, []byte("="))
if len(split) == 1 {
k := split[0]
tx = dummyTx{k, k, txBytes}
} else if len(split) == 2 {
k, v := split[0], split[1]
tx = dummyTx{k, v, txBytes}
} else {
2018-01-15 17:01:51 -08:00
return nil, fmt.Errorf("too many =")
2017-12-30 16:45:53 -08:00
}
2018-01-15 17:01:51 -08:00
return tx, nil
2017-12-30 16:45:53 -08:00
}
2018-01-15 17:01:51 -08:00
func DummyHandler(storeKey sdk.SubstoreKey) sdk.Handler {
return func(ctx sdk.Context, tx sdk.Tx) sdk.Result {
// tx is already unmarshalled
key := tx.Get("key").([]byte)
value := tx.Get("value").([]byte)
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
}
}