Add counter app to basecli

This commit is contained in:
Ethan Frey 2017-05-16 23:53:58 +02:00
parent 41c0f7cb69
commit ab6e5c3da2
3 changed files with 134 additions and 3 deletions

View File

@ -140,6 +140,76 @@ func (t SendTxReader) ReadTxFlags(flags interface{}) (interface{}, error) {
return &send, nil
}
/******** AppTx *********/
type AppFlags struct {
Fee string
Gas int64
Amount string
Sequence int
}
func AppFlagSet() (*flag.FlagSet, AppFlags) {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String("amount", "", "Coins to send in the format <amt><coin>,<amt><coin>...")
fs.String("fee", "", "Coins for the transaction fee of the format <amt><coin>")
fs.Int64("gas", 0, "Amount of gas for this transaction")
fs.Int("sequence", -1, "Sequence number for this transaction")
return fs, AppFlags{}
}
// AppTxReader allows us to create AppTx
type AppTxReader struct {
ChainID string
}
func (t AppTxReader) ReadTxJSON(data []byte) (interface{}, error) {
return nil, errors.New("Not implemented...")
}
func (t AppTxReader) ReadTxFlags(data *AppFlags, app string, appData []byte) (interface{}, error) {
// TODO: figure out a cleaner way to do this... until then
// just close your eyes and continue...
manager := keycmd.GetKeyManager()
name := viper.GetString("name")
info, err := manager.Get(name)
//parse the fee and amounts into coin types
feeCoin, err := btypes.ParseCoin(data.Fee)
if err != nil {
return nil, err
}
amountCoins, err := btypes.ParseCoins(data.Amount)
if err != nil {
return nil, err
}
// craft the tx
input := btypes.TxInput{
Address: info.Address,
Coins: amountCoins,
Sequence: data.Sequence,
}
if data.Sequence == 1 {
input.PubKey = info.PubKey
}
tx := btypes.AppTx{
Gas: data.Gas,
Fee: feeCoin,
Input: input,
Name: app,
Data: appData,
}
// wrap it in the proper signer thing...
send := AppTx{
chainID: t.ChainID,
Tx: &tx,
}
return &send, nil
}
/** TODO copied from basecoin cli - put in common somewhere? **/
// Returns true for non-empty hex-string prefixed with "0x"

View File

@ -1,24 +1,83 @@
package main
import (
"fmt"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
wire "github.com/tendermint/go-wire"
lightclient "github.com/tendermint/light-client"
"github.com/tendermint/light-client/commands"
"github.com/tendermint/light-client/commands/txs"
"github.com/tendermint/basecoin/plugins/counter"
btypes "github.com/tendermint/basecoin/types"
)
type CounterPresenter struct{}
func (_ CounterPresenter) MakeKey(str string) ([]byte, error) {
key := counter.New().StateKey()
fmt.Println(string(key))
return key, nil
}
func (_ CounterPresenter) ParseData(raw []byte) (interface{}, error) {
fmt.Println("Data", len(raw))
var cp counter.CounterPluginState
err := wire.ReadBinaryBytes(raw, &cp)
return cp, err
}
/**** build out the tx ****/
var (
_ txs.ReaderMaker = CounterTxMaker{}
_ lightclient.TxReader = CounterTxReader{}
)
type CounterTxMaker struct{}
func (m CounterTxMaker) MakeReader() (lightclient.TxReader, error) {
chainID := viper.GetString(commands.ChainFlag)
return CounterTxReader{AppTxReader{ChainID: chainID}}, nil
}
// define flags
type CounterFlags struct {
AppFlags `mapstructure:",squash"`
Valid bool
CountFee string
}
func (m CounterTxMaker) Flags() (*flag.FlagSet, interface{}) {
fs, app := AppFlagSet()
fs.String("countfee", "", "Coins to send in the format <amt><coin>,<amt><coin>...")
fs.Bool("valid", false, "Is count valid?")
return fs, &CounterFlags{AppFlags: app}
}
// parse flags
type CounterTxReader struct {
App AppTxReader
}
func (t CounterTxReader) ReadTxJSON(data []byte) (interface{}, error) {
// TODO: something. maybe?
return t.App.ReadTxJSON(data)
}
func (t CounterTxReader) ReadTxFlags(flags interface{}) (interface{}, error) {
data := flags.(*CounterFlags)
countFee, err := btypes.ParseCoins(data.CountFee)
if err != nil {
return nil, err
}
ctx := counter.CounterTx{
Valid: viper.GetBool("valid"),
Fee: countFee,
}
txBytes := wire.BinaryBytes(ctx)
return t.App.ReadTxFlags(&data.AppFlags, counter.New().Name(), txBytes)
}

View File

@ -32,7 +32,9 @@ func main() {
proofs.StatePresenters.Register("account", AccountPresenter{})
proofs.StatePresenters.Register("counter", CounterPresenter{})
proofs.TxPresenters.Register("base", BaseTxPresenter{})
txs.Register("send", SendTxMaker{})
txs.Register("counter", CounterTxMaker{})
// set up the various commands to use
BaseCli.AddCommand(