From ab6e5c3da2afc042d3dd29907a8e7ad8da297c42 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 16 May 2017 23:53:58 +0200 Subject: [PATCH] Add counter app to basecli --- cmd/basecli/adapters.go | 70 +++++++++++++++++++++++++++++++++++++++++ cmd/basecli/counter.go | 65 ++++++++++++++++++++++++++++++++++++-- cmd/basecli/main.go | 2 ++ 3 files changed, 134 insertions(+), 3 deletions(-) diff --git a/cmd/basecli/adapters.go b/cmd/basecli/adapters.go index 5b4869acd..56dfdae0b 100644 --- a/cmd/basecli/adapters.go +++ b/cmd/basecli/adapters.go @@ -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 ,...") + fs.String("fee", "", "Coins for the transaction fee of the format ") + 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" diff --git a/cmd/basecli/counter.go b/cmd/basecli/counter.go index 15608b9e9..7a6c9cc67 100644 --- a/cmd/basecli/counter.go +++ b/cmd/basecli/counter.go @@ -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 ,...") + 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) +} diff --git a/cmd/basecli/main.go b/cmd/basecli/main.go index a8f3a8419..0652e9611 100644 --- a/cmd/basecli/main.go +++ b/cmd/basecli/main.go @@ -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(