Merge branch 'queryReg' into develop

This commit is contained in:
Ethan Buchman
2017-02-19 14:08:31 -05:00
11 changed files with 149 additions and 55 deletions
+6 -12
View File
@@ -48,10 +48,10 @@ var (
Usage: "Destination address for the transaction",
}
AmountFlag = cli.IntFlag{
AmountFlag = cli.StringFlag{
Name: "amount",
Value: 0,
Usage: "Amount of coins to send in the transaction",
Value: "",
Usage: "Coins to send in transaction of the format <amt><coin>,<amt2><coin2>,... (eg: 1btc,2gold,5silver)",
}
FromFlag = cli.StringFlag{
@@ -66,22 +66,16 @@ var (
Usage: "Sequence number for the account",
}
CoinFlag = cli.StringFlag{
Name: "coin",
Value: "mycoin",
Usage: "Specify a coin denomination",
}
GasFlag = cli.IntFlag{
Name: "gas",
Value: 0,
Usage: "The amount of gas for the transaction",
}
FeeFlag = cli.IntFlag{
FeeFlag = cli.StringFlag{
Name: "fee",
Value: 0,
Usage: "The transaction fee",
Value: "",
Usage: "Coins for the transaction fee of the format <amt><coin>",
}
DataFlag = cli.StringFlag{
+5
View File
@@ -66,6 +66,11 @@ var (
}
)
// Register a subcommand of QueryCmd for plugin specific query functionality
func RegisterQuerySubcommand(cmd cli.Command) {
QueryCmd.Subcommands = append(QueryCmd.Subcommands, cmd)
}
func cmdQuery(c *cli.Context) error {
if len(c.Args()) != 1 {
return errors.New("query command requires an argument ([key])")
+35 -21
View File
@@ -23,7 +23,6 @@ var TxFlags = []cli.Flag{
FromFlag,
AmountFlag,
CoinFlag,
GasFlag,
FeeFlag,
SeqFlag,
@@ -71,9 +70,9 @@ func RegisterTxSubcommand(cmd cli.Command) {
func cmdSendTx(c *cli.Context) error {
toHex := c.String("to")
fromFile := c.String("from")
amount := int64(c.Int("amount"))
coin := c.String("coin")
gas, fee := c.Int("gas"), int64(c.Int("fee"))
amount := c.String("amount")
gas := int64(c.Int("gas"))
fee := c.String("fee")
chainID := c.String("chain_id")
// convert destination address to bytes
@@ -91,12 +90,22 @@ func cmdSendTx(c *cli.Context) error {
return err
}
//parse the fee and amounts into coin types
feeCoin, err := ParseCoin(fee)
if err != nil {
return err
}
amountCoins, err := ParseCoins(amount)
if err != nil {
return err
}
// craft the tx
input := types.NewTxInput(privKey.PubKey, types.Coins{types.Coin{coin, amount}}, sequence)
output := newOutput(to, coin, amount)
input := types.NewTxInput(privKey.PubKey, amountCoins, sequence)
output := newOutput(to, amountCoins)
tx := &types.SendTx{
Gas: int64(gas),
Fee: types.Coin{coin, fee},
Gas: gas,
Fee: feeCoin,
Inputs: []types.TxInput{input},
Outputs: []types.TxOutput{output},
}
@@ -128,9 +137,9 @@ func cmdAppTx(c *cli.Context) error {
func AppTx(c *cli.Context, name string, data []byte) error {
fromFile := c.String("from")
amount := int64(c.Int("amount"))
coin := c.String("coin")
gas, fee := c.Int("gas"), int64(c.Int("fee"))
amount := c.String("amount")
fee := c.String("fee")
gas := int64(c.Int("gas"))
chainID := c.String("chain_id")
privKey := tmtypes.LoadPrivValidator(fromFile)
@@ -140,10 +149,20 @@ func AppTx(c *cli.Context, name string, data []byte) error {
return err
}
input := types.NewTxInput(privKey.PubKey, types.Coins{types.Coin{coin, amount}}, sequence)
//parse the fee and amounts into coin types
feeCoin, err := ParseCoin(fee)
if err != nil {
return err
}
amountCoins, err := ParseCoins(amount)
if err != nil {
return err
}
input := types.NewTxInput(privKey.PubKey, amountCoins, sequence)
tx := &types.AppTx{
Gas: int64(gas),
Fee: types.Coin{coin, fee},
Gas: gas,
Fee: feeCoin,
Name: name,
Input: input,
Data: data,
@@ -205,15 +224,10 @@ func getSeq(c *cli.Context, address []byte) (int, error) {
return acc.Sequence + 1, nil
}
func newOutput(to []byte, coin string, amount int64) types.TxOutput {
func newOutput(to []byte, amount types.Coins) types.TxOutput {
return types.TxOutput{
Address: to,
Coins: types.Coins{
types.Coin{
Denom: coin,
Amount: amount,
},
},
Coins: amount,
}
}
+43 -1
View File
@@ -3,9 +3,13 @@ package commands
import (
"encoding/hex"
"errors"
"regexp"
"strconv"
"strings"
"github.com/urfave/cli"
"github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/types"
abci "github.com/tendermint/abci/types"
@@ -35,6 +39,44 @@ func StripHex(s string) string {
return s
}
//regex codes for extracting coins from CLI input
var reDenom = regexp.MustCompile("([^\\d\\W]+)")
var reAmt = regexp.MustCompile("(\\d+)")
func ParseCoin(str string) (types.Coin, error) {
var coin types.Coin
if len(str) > 0 {
amt, err := strconv.Atoi(reAmt.FindString(str))
if err != nil {
return coin, err
}
denom := reDenom.FindString(str)
coin = types.Coin{denom, int64(amt)}
}
return coin, nil
}
func ParseCoins(str string) (types.Coins, error) {
split := strings.Split(str, ",")
var coins []types.Coin
for _, el := range split {
if len(el) > 0 {
coin, err := ParseCoin(el)
if err != nil {
return coins, err
}
coins = append(coins, coin)
}
}
return coins, nil
}
func Query(tmAddr string, key []byte) (*abci.ResponseQuery, error) {
clientURI := client.NewClientURI(tmAddr)
tmResult := new(ctypes.TMResult)
@@ -58,7 +100,7 @@ func Query(tmAddr string, key []byte) (*abci.ResponseQuery, error) {
// fetch the account by querying the app
func getAcc(tmAddr string, address []byte) (*types.Account, error) {
key := append([]byte("base/a/"), address...)
key := state.AccountKey(address)
response, err := Query(tmAddr, key)
if err != nil {
return nil, err
+40
View File
@@ -0,0 +1,40 @@
package commands
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/basecoin/types"
)
//Test the parse coin and parse coins functionality
func TestParse(t *testing.T) {
makeCoin := func(str string) types.Coin {
coin, err := ParseCoin(str)
if err != nil {
panic(err.Error())
}
return coin
}
makeCoins := func(str string) types.Coins {
coin, err := ParseCoins(str)
if err != nil {
panic(err.Error())
}
return coin
}
//testing ParseCoin Function
assert.True(t, types.Coin{} == makeCoin(""), "parseCoin makes bad empty coin")
assert.True(t, types.Coin{"fooCoin", 1} == makeCoin("1fooCoin"), "parseCoin makes bad coins")
assert.True(t, types.Coin{"barCoin", 10} == makeCoin("10 barCoin"), "parseCoin makes bad coins")
//testing ParseCoins Function
assert.True(t, types.Coins{{"fooCoin", 1}}.IsEqual(makeCoins("1fooCoin")), "parseCoins doesn't parse a single coin")
assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99barCoin,1fooCoin")),
"parseCoins doesn't properly parse two coins")
assert.True(t, types.Coins{{"barCoin", 99}, {"fooCoin", 1}}.IsEqual(makeCoins("99 barCoin, 1 fooCoin")),
"parseCoins doesn't properly parse two coins which use spaces")
}