Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional verbose flag #181

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/plasmacli/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import (

// Flags
const (
Home = tmcli.HomeFlag
Home = tmcli.HomeFlag
Verbose = "verbose"
)
30 changes: 19 additions & 11 deletions cmd/plasmacli/subcmd/query/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package query
import (
"fmt"
"github.com/FourthState/plasma-mvp-sidechain/client"
"github.com/FourthState/plasma-mvp-sidechain/cmd/plasmacli/flags"
ks "github.com/FourthState/plasma-mvp-sidechain/cmd/plasmacli/store"
"github.com/cosmos/cosmos-sdk/client/context"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// InfoCmd returns the query information command
Expand All @@ -15,9 +17,11 @@ func InfoCmd() *cobra.Command {
}

var infoCmd = &cobra.Command{
Use: "info <account/address>",
Short: "Information on owned utxos valid and invalid",
Args: cobra.ExactArgs(1),
Use: "info <account/address>",
Short: "Information on owned utxos valid and invalid",
Long: `Information on owned utxos valid and invalid.
If --verbose is set, the input positions that created each output will also be displayed`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext()
cmd.SilenceUsage = true
Expand All @@ -43,20 +47,24 @@ var infoCmd = &cobra.Command{
fmt.Printf("UTXO %d\n", i)
fmt.Printf("Position: %s, Amount: %s, Spent: %t\nSpender Hash: %s\n", utxo.Position, utxo.Output.Amount.String(), utxo.Spent, utxo.SpenderTx)
fmt.Printf("Transaction Hash: 0x%x\nConfirmationHash: 0x%x\n", utxo.TxHash, utxo.ConfirmationHash)
/*
// TODO: Add --verbose flag that if set will query TxInput and print InputAddresses and InputPositions as well
// print inputs if applicable
positions := utxo.InputPositions
hamdiallam marked this conversation as resolved.
Show resolved Hide resolved
for i, p := range positions {
fmt.Printf("Input %d Position: %s\n", i, p)

// query for the inputs that created this out
if viper.GetBool(flags.Verbose) {
txInput, err := client.TxInput(ctx, utxo.Position)
if err != nil {
fmt.Printf("Error retrieving further information about the inputs of this UTXO: %s", err)
} else {
for i, input := range txInput.InputPositions {
fmt.Printf("Input %d: Position: %s\n", i, input)
}
}
*/
}

fmt.Printf("End UTXO %d info\n\n", i)
}

if len(utxos) == 0 {
fmt.Println("no information available for this address")
fmt.Println("No information available for this address")
}

return nil
Expand Down
77 changes: 77 additions & 0 deletions cmd/plasmacli/subcmd/query/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package query

import (
"encoding/hex"
"fmt"
"github.com/FourthState/plasma-mvp-sidechain/client"
"github.com/FourthState/plasma-mvp-sidechain/plasma"
"github.com/FourthState/plasma-mvp-sidechain/utils"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/spf13/cobra"
"strings"
)

// TxCommand -
func TxCommand() *cobra.Command {
return txCmd
}

var txCmd *cobra.Command = &cobra.Command{
Use: "tx <txHash/position>",
Short: "Query for information about a single transaction or transaction output",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext()
cmd.SilenceUsage = true
arg := strings.TrimSpace(args[0])

// argument validation
if pos, err := plasma.FromPositionString(arg); err != nil {
// utxo position
txOutput, err := client.TxOutput(ctx, pos)
if err != nil {
fmt.Printf("Error quering transaction output %s: %s\n", pos, err)
return err
}

// display output information
fmt.Printf("TxOutput: %s\n", pos)
fmt.Printf("Confirmation Hash: 0x%x\n", txOutput.ConfirmationHash)
fmt.Printf("Transaction Hash: 0x%x\n", txOutput.TxHash)
fmt.Printf("Spent: %v\n", txOutput.Spent)
if txOutput.Spent {
fmt.Printf("Spending Tx Hash: 0x%x\n", txOutput.SpenderTx)
}

} else {
// transaction hash
txHash, err := hex.DecodeString(utils.RemoveHexPrefix(arg))
if err != nil {
fmt.Printf("Error decoding tx hash hex-encoded string: %s\n", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reasoning for printing the error as opposed to wrapping it? Also, error messages returned should begin with lowercase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a good point. Should just return it

return err
}

tx, err := client.Tx(ctx, txHash)
if err != nil {
fmt.Printf("Error queyring trasnsaction hash: 0x%x\n", txHash)
return err
}

// display transaction information
fmt.Printf("Block %d, Tx Index: %d\n", tx.Position.BlockNum, tx.Position.TxIndex)
fmt.Printf("TxHash: 0x%x\n", txHash)
fmt.Printf("Confirmation Hash: 0x%x\n", tx.ConfirmationHash)
// we expect the length of `SpenderTx` and `Spent` to be the same
for i, spender := range tx.SpenderTxs {
spent := tx.Spent[i]
fmt.Printf("Ouput %d:\n\tSpent: %v\n", i, spent)
if spent {
// keep the indentendation
fmt.Printf("\tSpender Tx: 0x%x\n", spender)
}
}
}

return nil
},
}
1 change: 1 addition & 0 deletions cmd/plasmacli/subcmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var homeDir = os.ExpandEnv("$HOME/.plasmacli/")
func RootCmd() *cobra.Command {
cobra.EnableCommandSorting = false
rootCmd.PersistentFlags().String(flags.Home, homeDir, "home directory for plasmacli")
rootCmd.PersistentFlags().Bool(flags.Verbose, false, "additional output (if applicable)")
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions cmd/plasmacli/subcmd/tx/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
)

// IncludeCmd -
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you plan to update these godoc's or nah?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update them. Found a bug in plasmad and paused on the cli work

func IncludeCmd() *cobra.Command {
includeCmd.Flags().Int64P(replayF, "r", 0, "Replay Nonce that can be incremented to allow for resubmissions of include deposit messages")
includeCmd.Flags().String(addressF, "", "address represented as hex string")
Expand Down Expand Up @@ -82,6 +83,7 @@ var includeCmd = &cobra.Command{
if _, err := ctx.BroadcastTxAsync(txBytes); err != nil {
return err
}
fmt.Println("Transaction submitted")
} else {
res, err := ctx.BroadcastTxAndAwaitCommit(txBytes)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/plasmacli/subcmd/tx/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
signPrompt = "Would you like to finalize this transaction? [Y/n]"
)

// SignCmd -
func SignCmd() *cobra.Command {
signCmd.Flags().String(ownerF, "", "Owner of the output (required with position flag)")
signCmd.Flags().String(positionF, "", "Position of transaction to finalize (required with owner flag)")
Expand Down
1 change: 1 addition & 0 deletions cmd/plasmacli/subcmd/tx/spend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"
)

// SpendCmd -
func SpendCmd() *cobra.Command {
spendCmd.Flags().String(positionF, "", "UTXO Positions to be spent, format: (blknum0.txindex0.oindex0.depositnonce0)::(blknum1.txindex1.oindex1.depositnonce1)")
spendCmd.Flags().StringP(confirmSigs0F, "0", "", "Input Confirmation Signatures for first input to be spent (separated by commas)")
Expand Down
7 changes: 2 additions & 5 deletions handlers/anteHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,8 @@ func TestAnteValidTx(t *testing.T) {
ds.StoreOutputs(ctx, tx)
ds.SpendDeposit(ctx, big.NewInt(2), tx.Transaction.TxHash())

output, check := ds.GetOutput(ctx, getPosition("(1.0.0.0)"))
if !check {
fmt.Println("WHy?")
}
fmt.Printf("Output: %v\n", output)
_, check := ds.GetOutput(ctx, getPosition("(1.0.0.0)"))
require.True(t, check) // sanity

// store confirm sig into correct format
var confirmSig [65]byte
Expand Down
22 changes: 19 additions & 3 deletions plasma/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
)

// Position -
type Position struct {
BlockNum *big.Int
TxIndex uint16
Expand All @@ -20,6 +21,7 @@ const (
txIndexFactor = 10
)

// NewPosition
func NewPosition(blkNum *big.Int, txIndex uint16, oIndex uint8, depositNonce *big.Int) Position {
if depositNonce == nil {
depositNonce = big.NewInt(0)
Expand All @@ -36,6 +38,7 @@ func NewPosition(blkNum *big.Int, txIndex uint16, oIndex uint8, depositNonce *bi
}
}

// Bytes serializes `p`
func (p Position) Bytes() []byte {
bytes, _ := rlp.EncodeToBytes(&p)
return bytes
Expand Down Expand Up @@ -65,18 +68,22 @@ func (p Position) ValidateBasic() error {
return nil
}

// IsDeposit -
func (p Position) IsDeposit() bool {
return p.DepositNonce.Sign() != 0
}

// IsFee -
func (p Position) IsFee() bool {
return p.TxIndex == 1<<16-1
}

// IsNilPosition -
func (p Position) IsNilPosition() bool {
return p.BlockNum.Sign() == 0 && p.DepositNonce.Sign() == 0
}

// Priority
func (p Position) Priority() *big.Int {
if p.IsDeposit() {
return p.DepositNonce
Expand All @@ -92,6 +99,7 @@ func (p Position) Priority() *big.Int {
return temp.Add(temp, big.NewInt(int64(p.OutputIndex)))
}

// ToBigIntArray -
func (p Position) ToBigIntArray() [4]*big.Int {
return [4]*big.Int{p.BlockNum, big.NewInt(int64(p.TxIndex)), big.NewInt(int64(p.OutputIndex)), p.DepositNonce}
}
Expand All @@ -107,6 +115,9 @@ func (p Position) String() string {
p.BlockNum, p.TxIndex, p.OutputIndex, p.DepositNonce)
}

// FromPositionString constructs a `Position` from the string representation
// "(blockNumber, txIndex, outputIndex, depositNonce)". The returned error will
// also reflect an invalid position object in addition to any deserialization errors
func FromPositionString(posStr string) (Position, error) {
posStr = strings.TrimSpace(posStr)
if string(posStr[0]) != "(" || string(posStr[len(posStr)-1]) != ")" {
Expand Down Expand Up @@ -160,11 +171,16 @@ func FromPositionString(posStr string) (Position, error) {
return pos, pos.ValidateBasic()
}

// Return the position of a utxo given the exit key
// FromExitKey creates the position of a utxo given the exit key
func FromExitKey(key *big.Int, deposit bool) Position {
if deposit {
return NewPosition(big.NewInt(0), 0, 0, key)
} else {
return NewPosition(new(big.Int).Div(key, big.NewInt(blockIndexFactor)), uint16(key.Int64()%blockIndexFactor/txIndexFactor), uint8(key.Int64()%2), big.NewInt(0))
}

return NewPosition(
new(big.Int).Div(key, big.NewInt(blockIndexFactor)),
uint16(key.Int64()%blockIndexFactor/txIndexFactor),
uint8(key.Int64()%2),
big.NewInt(0),
)
}
2 changes: 2 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func IsZeroAddress(addr common.Address) bool {
return bytes.Equal(addr[:], ZeroAddress[:])
}

// RemoveHexPrefix will remove the "0x" prefix to a hex string and also fix the byte string
// to an even length
func RemoveHexPrefix(hexStr string) string {
if len(hexStr) >= 2 && (hexStr[:2] == "0x" || hexStr[:2] == "0X") {
hexStr = hexStr[2:]
Expand Down