Get counter app working, with cli tests
This commit is contained in:
@@ -7,10 +7,8 @@ import (
|
||||
|
||||
"github.com/tendermint/tmlibs/cli"
|
||||
|
||||
"github.com/tendermint/basecoin/app"
|
||||
"github.com/tendermint/basecoin/cmd/basecoin/commands"
|
||||
"github.com/tendermint/basecoin/docs/guide/counter/plugins/counter"
|
||||
"github.com/tendermint/basecoin/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -20,7 +18,7 @@ func main() {
|
||||
}
|
||||
|
||||
// TODO: register the counter here
|
||||
commands.Handler = app.DefaultHandler()
|
||||
commands.Handler = counter.NewCounterHandler()
|
||||
|
||||
RootCmd.AddCommand(
|
||||
commands.InitCmd,
|
||||
@@ -29,7 +27,6 @@ func main() {
|
||||
commands.VersionCmd,
|
||||
)
|
||||
|
||||
commands.RegisterStartPlugin("counter", func() types.Plugin { return counter.New() })
|
||||
cmd := cli.PrepareMainCmd(RootCmd, "CT", os.ExpandEnv("$HOME/.counter"))
|
||||
cmd.Execute()
|
||||
}
|
||||
|
||||
@@ -4,11 +4,12 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
wire "github.com/tendermint/go-wire"
|
||||
"github.com/tendermint/basecoin"
|
||||
"github.com/tendermint/light-client/commands"
|
||||
txcmd "github.com/tendermint/light-client/commands/txs"
|
||||
|
||||
bcmd "github.com/tendermint/basecoin/cmd/basecli/commands"
|
||||
"github.com/tendermint/basecoin/docs/guide/counter/plugins/counter"
|
||||
"github.com/tendermint/basecoin/txs"
|
||||
btypes "github.com/tendermint/basecoin/types"
|
||||
)
|
||||
|
||||
@@ -20,64 +21,59 @@ var CounterTxCmd = &cobra.Command{
|
||||
Long: `Add a vote to the counter.
|
||||
|
||||
You must pass --valid for it to count and the countfee will be added to the counter.`,
|
||||
RunE: counterTxCmd,
|
||||
RunE: doCounterTx,
|
||||
}
|
||||
|
||||
const (
|
||||
flagCountFee = "countfee"
|
||||
flagValid = "valid"
|
||||
FlagCountFee = "countfee"
|
||||
FlagValid = "valid"
|
||||
FlagSequence = "sequence" // FIXME: currently not supported...
|
||||
)
|
||||
|
||||
func init() {
|
||||
fs := CounterTxCmd.Flags()
|
||||
bcmd.AddAppTxFlags(fs)
|
||||
fs.String(flagCountFee, "", "Coins to send in the format <amt><coin>,<amt><coin>...")
|
||||
fs.Bool(flagValid, false, "Is count valid?")
|
||||
fs.String(FlagCountFee, "", "Coins to send in the format <amt><coin>,<amt><coin>...")
|
||||
fs.Bool(FlagValid, false, "Is count valid?")
|
||||
fs.Int(FlagSequence, -1, "Sequence number for this transaction")
|
||||
}
|
||||
|
||||
func counterTxCmd(cmd *cobra.Command, args []string) error {
|
||||
// Note: we don't support loading apptx from json currently, so skip that
|
||||
|
||||
// Read the app-specific flags
|
||||
name, data, err := getAppData()
|
||||
// TODO: doCounterTx is very similar to the sendtx one,
|
||||
// maybe we can pull out some common patterns?
|
||||
func doCounterTx(cmd *cobra.Command, args []string) error {
|
||||
// load data from json or flags
|
||||
var tx basecoin.Tx
|
||||
found, err := txcmd.LoadJSON(&tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found {
|
||||
tx, err = readCounterTxFlags()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Read the standard app-tx flags
|
||||
gas, fee, txInput, err := bcmd.ReadAppTxFlags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: make this more flexible for middleware
|
||||
// add the chain info
|
||||
tx = txs.NewChain(commands.GetChainID(), tx)
|
||||
stx := txs.NewSig(tx)
|
||||
|
||||
// Create AppTx and broadcast
|
||||
tx := &btypes.AppTx{
|
||||
Gas: gas,
|
||||
Fee: fee,
|
||||
Name: name,
|
||||
Input: txInput,
|
||||
Data: data,
|
||||
}
|
||||
res, err := bcmd.BroadcastAppTx(tx)
|
||||
// Sign if needed and post. This it the work-horse
|
||||
bres, err := txcmd.SignAndPostTx(stx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Output result
|
||||
return txcmd.OutputTx(res)
|
||||
return txcmd.OutputTx(bres)
|
||||
}
|
||||
|
||||
func getAppData() (name string, data []byte, err error) {
|
||||
countFee, err := btypes.ParseCoins(viper.GetString(flagCountFee))
|
||||
func readCounterTxFlags() (tx basecoin.Tx, err error) {
|
||||
feeCoins, err := btypes.ParseCoins(viper.GetString(FlagCountFee))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx := counter.CounterTx{
|
||||
Valid: viper.GetBool(flagValid),
|
||||
Fee: countFee,
|
||||
return tx, err
|
||||
}
|
||||
|
||||
name = counter.New().Name()
|
||||
data = wire.BinaryBytes(ctx)
|
||||
return
|
||||
tx = counter.NewCounterTx(viper.GetBool(FlagValid), feeCoins)
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ var CounterQueryCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func counterQueryCmd(cmd *cobra.Command, args []string) error {
|
||||
key := counter.New().StateKey()
|
||||
key := counter.StateKey()
|
||||
|
||||
var cp counter.CounterPluginState
|
||||
var cp counter.CounterState
|
||||
proof, err := proofcmd.GetAndParseAppProof(key, &cp)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/tendermint/basecoin"
|
||||
"github.com/tendermint/basecoin/errors"
|
||||
"github.com/tendermint/basecoin/modules/coin"
|
||||
"github.com/tendermint/basecoin/stack"
|
||||
"github.com/tendermint/basecoin/types"
|
||||
)
|
||||
|
||||
@@ -78,6 +79,17 @@ func ErrDecoding() error {
|
||||
// CounterHandler
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
func NewCounterHandler() basecoin.Handler {
|
||||
// use the default stack
|
||||
coin := coin.NewHandler()
|
||||
counter := CounterHandler{}
|
||||
dispatcher := stack.NewDispatcher(
|
||||
stack.WrapHandler(coin),
|
||||
stack.WrapHandler(counter),
|
||||
)
|
||||
return stack.NewDefault().Use(dispatcher)
|
||||
}
|
||||
|
||||
type CounterHandler struct {
|
||||
basecoin.NopOption
|
||||
}
|
||||
@@ -107,6 +119,7 @@ func (h CounterHandler) DeliverTx(ctx basecoin.Context, store types.KVStore, tx
|
||||
}
|
||||
|
||||
// TODO: handle coin movement.... ugh, need sequence to do this, right?
|
||||
// like, actually decrement the other account
|
||||
|
||||
// update the counter
|
||||
state, err := LoadState(store)
|
||||
@@ -135,16 +148,16 @@ func checkTx(ctx basecoin.Context, tx basecoin.Tx) (ctr CounterTx, err error) {
|
||||
// CounterStore
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
type CounterPluginState struct {
|
||||
Counter int
|
||||
TotalFees types.Coins
|
||||
type CounterState struct {
|
||||
Counter int `json:"counter"`
|
||||
TotalFees types.Coins `json:"total_fees"`
|
||||
}
|
||||
|
||||
func StateKey() []byte {
|
||||
return []byte(NameCounter + "/state")
|
||||
}
|
||||
|
||||
func LoadState(store types.KVStore) (state CounterPluginState, err error) {
|
||||
func LoadState(store types.KVStore) (state CounterState, err error) {
|
||||
bytes := store.Get(StateKey())
|
||||
if len(bytes) > 0 {
|
||||
err = wire.ReadBinaryBytes(bytes, &state)
|
||||
@@ -155,7 +168,7 @@ func LoadState(store types.KVStore) (state CounterPluginState, err error) {
|
||||
return state, nil
|
||||
}
|
||||
|
||||
func StoreState(store types.KVStore, state CounterPluginState) error {
|
||||
func StoreState(store types.KVStore, state CounterState) error {
|
||||
bytes := wire.BinaryBytes(state)
|
||||
store.Set(StateKey(), bytes)
|
||||
return nil
|
||||
|
||||
@@ -7,10 +7,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/basecoin"
|
||||
"github.com/tendermint/basecoin/app"
|
||||
"github.com/tendermint/basecoin/modules/coin"
|
||||
"github.com/tendermint/basecoin/stack"
|
||||
"github.com/tendermint/basecoin/txs"
|
||||
"github.com/tendermint/basecoin/types"
|
||||
"github.com/tendermint/go-wire"
|
||||
@@ -18,18 +15,6 @@ import (
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
)
|
||||
|
||||
// TODO: actually handle the counter here...
|
||||
func NewCounterHandler() basecoin.Handler {
|
||||
// use the default stack
|
||||
coin := coin.NewHandler()
|
||||
counter := CounterHandler{}
|
||||
dispatcher := stack.NewDispatcher(
|
||||
stack.WrapHandler(coin),
|
||||
stack.WrapHandler(counter),
|
||||
)
|
||||
return stack.NewDefault().Use(dispatcher)
|
||||
}
|
||||
|
||||
func TestCounterPlugin(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user