From e0c76b4e3082ab8e373ec65c9af8f7b61e72941c Mon Sep 17 00:00:00 2001 From: Wesley Graham Date: Fri, 3 Jan 2020 14:30:16 -0800 Subject: [PATCH 1/3] sig query (#161) --- cmd/plasmacli/subcmd/eth/query/confirmsig.go | 91 ++++++++++++++++++++ cmd/plasmacli/subcmd/eth/query/root.go | 2 + 2 files changed, 93 insertions(+) create mode 100644 cmd/plasmacli/subcmd/eth/query/confirmsig.go diff --git a/cmd/plasmacli/subcmd/eth/query/confirmsig.go b/cmd/plasmacli/subcmd/eth/query/confirmsig.go new file mode 100644 index 0000000..5936e31 --- /dev/null +++ b/cmd/plasmacli/subcmd/eth/query/confirmsig.go @@ -0,0 +1,91 @@ +package query + +import ( + "encoding/json" + "fmt" + "github.com/FourthState/plasma-mvp-sidechain/cmd/plasmacli/config" + "github.com/FourthState/plasma-mvp-sidechain/plasma" + "github.com/FourthState/plasma-mvp-sidechain/store" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/ethereum/go-ethereum/rlp" + "github.com/spf13/cobra" +) + +// SigCmd returns the query confirm sig command +func SigCmd() *cobra.Command { + config.AddPersistentTMFlags(sigCmd) + sigCmd.Flags().Bool(useNodeF, false, "trust connected full node") + return sigCmd +} + +var sigCmd = &cobra.Command{ + Use: "sig ", + Short: "Query confirm signature information for a given position", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + // parse position + pos, err := plasma.FromPositionString(args[0]) + if err != nil { + return err + } + + var sigs []byte + ctx := context.NewCLIContext() + sigs, err = getSigs(ctx, pos) + if err != nil { + return fmt.Errorf("failed to retrieve confirm signature information: %s", err) + } + + switch len(sigs) { + case 65: + fmt.Printf("Confirmation Signatures: 0x%x\n", sigs[:]) + case 130: + fmt.Printf("Confirmation Signatures: 0x%x, 0x%x\n", sigs[:65], sigs[65:]) + } + + return nil + }, +} + +// Returns confirm sig results for given position +// Trusts connected full node +func getSigs(ctx context.CLIContext, position plasma.Position) ([]byte, error) { + key := store.GetOutputKey(position) + hash, err := ctx.QueryStore(key, store.DataStoreName) + if err != nil { + return nil, err + } + + txKey := store.GetTxKey(hash) + txBytes, err := ctx.QueryStore(txKey, store.DataStoreName) + + var tx store.Transaction + if err := rlp.DecodeBytes(txBytes, &tx); err != nil { + return nil, fmt.Errorf("transaction decoding failed: %s", err.Error()) + } + + // Look for confirmation signatures + // Ignore error if no confirm sig currently exists in store + var sigs []byte + if len(tx.SpenderTxs[position.OutputIndex]) > 0 { + queryPath := fmt.Sprintf("custom/data/tx/%s", tx.SpenderTxs[position.OutputIndex]) + data, err := ctx.Query(queryPath, nil) + if err != nil { + return nil, err + } + + var spenderTx store.Transaction + if err := json.Unmarshal(data, &spenderTx); err != nil { + return nil, fmt.Errorf("unmarshaling json query response: %s", err) + } + for _, input := range spenderTx.Transaction.Inputs { + if input.Position.String() == position.String() { + for _, sig := range input.ConfirmSignatures { + sigs = append(sigs, sig[:]...) + } + } + } + } + + return sigs, nil +} diff --git a/cmd/plasmacli/subcmd/eth/query/root.go b/cmd/plasmacli/subcmd/eth/query/root.go index 280e772..3b43e82 100644 --- a/cmd/plasmacli/subcmd/eth/query/root.go +++ b/cmd/plasmacli/subcmd/eth/query/root.go @@ -16,6 +16,7 @@ var ( indexF = "index" limitF = "limit" positionF = "position" + useNodeF = "use-node" ) // RootCmd returns the eth query command @@ -25,6 +26,7 @@ func RootCmd() *cobra.Command { BlockCmd(), DepositCmd(), ExitsCmd(), + SigCmd(), RootchainCmd(), ) From f2f9269c5659d83b4055da5c8b87c9924def8f1d Mon Sep 17 00:00:00 2001 From: Wesley Graham Date: Wed, 4 Mar 2020 11:45:20 -0800 Subject: [PATCH 2/3] Add sig logic to client, polish sigquery --- client/query.go | 46 +++++++++++++++++ cmd/plasmacli/subcmd/eth/query/confirmsig.go | 54 ++------------------ cmd/plasmacli/subcmd/eth/query/root.go | 1 - 3 files changed, 51 insertions(+), 50 deletions(-) diff --git a/client/query.go b/client/query.go index 2a27419..77549ec 100644 --- a/client/query.go +++ b/client/query.go @@ -7,6 +7,7 @@ import ( "github.com/FourthState/plasma-mvp-sidechain/store" "github.com/cosmos/cosmos-sdk/client/context" ethcmn "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" "math/big" ) @@ -154,3 +155,48 @@ func Blocks(ctx context.CLIContext, startingHeight *big.Int) ([]store.Block, err return blocks, nil } + +// Returns confirm sig results for given position +// Trusts connected full node +func Signatures(ctx context.CLIContext, position plasma.Position) ([]byte, error) { + key := store.GetOutputKey(position) + hash, err := ctx.QueryStore(key, store.DataStoreName) + if err != nil { + return nil, err + } + + txKey := store.GetTxKey(hash) + txBytes, err := ctx.QueryStore(txKey, store.DataStoreName) + + var tx store.Transaction + if err := rlp.DecodeBytes(txBytes, &tx); err != nil { + return nil, fmt.Errorf("transaction decoding failed: %s", err.Error()) + } + + // Look for confirmation signatures + // Ignore error if no confirm sig currently exists in store + var sigs []byte + if len(tx.SpenderTxs[position.OutputIndex]) > 0 { + queryPath := fmt.Sprintf("custom/%s/%s/%s", + store.QuerierRouteName, store.QueryTx, tx.SpenderTxs[position.OutputIndex]) + data, err := ctx.Query(queryPath, nil) + if err != nil { + return nil, err + } + + var spenderTx store.Transaction + if err := json.Unmarshal(data, &spenderTx); err != nil { + return nil, fmt.Errorf("unmarshaling json query response: %s", err) + } + for _, input := range spenderTx.Transaction.Inputs { + if input.Position.String() == position.String() { + for _, sig := range input.ConfirmSignatures { + sigs = append(sigs, sig[:]...) + } + } + } + } + + return sigs, nil +} + diff --git a/cmd/plasmacli/subcmd/eth/query/confirmsig.go b/cmd/plasmacli/subcmd/eth/query/confirmsig.go index 5936e31..bc37499 100644 --- a/cmd/plasmacli/subcmd/eth/query/confirmsig.go +++ b/cmd/plasmacli/subcmd/eth/query/confirmsig.go @@ -1,20 +1,17 @@ package query import ( - "encoding/json" "fmt" + "github.com/FourthState/plasma-mvp-sidechain/client" "github.com/FourthState/plasma-mvp-sidechain/cmd/plasmacli/config" "github.com/FourthState/plasma-mvp-sidechain/plasma" - "github.com/FourthState/plasma-mvp-sidechain/store" "github.com/cosmos/cosmos-sdk/client/context" - "github.com/ethereum/go-ethereum/rlp" "github.com/spf13/cobra" ) // SigCmd returns the query confirm sig command func SigCmd() *cobra.Command { config.AddPersistentTMFlags(sigCmd) - sigCmd.Flags().Bool(useNodeF, false, "trust connected full node") return sigCmd } @@ -31,12 +28,14 @@ var sigCmd = &cobra.Command{ var sigs []byte ctx := context.NewCLIContext() - sigs, err = getSigs(ctx, pos) + sigs, err = client.Signatures(ctx, pos) if err != nil { return fmt.Errorf("failed to retrieve confirm signature information: %s", err) } switch len(sigs) { + case 0: + fmt.Printf("No Confirm Signatures Found") case 65: fmt.Printf("Confirmation Signatures: 0x%x\n", sigs[:]) case 130: @@ -45,47 +44,4 @@ var sigCmd = &cobra.Command{ return nil }, -} - -// Returns confirm sig results for given position -// Trusts connected full node -func getSigs(ctx context.CLIContext, position plasma.Position) ([]byte, error) { - key := store.GetOutputKey(position) - hash, err := ctx.QueryStore(key, store.DataStoreName) - if err != nil { - return nil, err - } - - txKey := store.GetTxKey(hash) - txBytes, err := ctx.QueryStore(txKey, store.DataStoreName) - - var tx store.Transaction - if err := rlp.DecodeBytes(txBytes, &tx); err != nil { - return nil, fmt.Errorf("transaction decoding failed: %s", err.Error()) - } - - // Look for confirmation signatures - // Ignore error if no confirm sig currently exists in store - var sigs []byte - if len(tx.SpenderTxs[position.OutputIndex]) > 0 { - queryPath := fmt.Sprintf("custom/data/tx/%s", tx.SpenderTxs[position.OutputIndex]) - data, err := ctx.Query(queryPath, nil) - if err != nil { - return nil, err - } - - var spenderTx store.Transaction - if err := json.Unmarshal(data, &spenderTx); err != nil { - return nil, fmt.Errorf("unmarshaling json query response: %s", err) - } - for _, input := range spenderTx.Transaction.Inputs { - if input.Position.String() == position.String() { - for _, sig := range input.ConfirmSignatures { - sigs = append(sigs, sig[:]...) - } - } - } - } - - return sigs, nil -} +} \ No newline at end of file diff --git a/cmd/plasmacli/subcmd/eth/query/root.go b/cmd/plasmacli/subcmd/eth/query/root.go index 3b43e82..c358845 100644 --- a/cmd/plasmacli/subcmd/eth/query/root.go +++ b/cmd/plasmacli/subcmd/eth/query/root.go @@ -16,7 +16,6 @@ var ( indexF = "index" limitF = "limit" positionF = "position" - useNodeF = "use-node" ) // RootCmd returns the eth query command From 667b102c933ab7075e58108247a0f5bcc2387947 Mon Sep 17 00:00:00 2001 From: Wesley Graham Date: Fri, 6 Mar 2020 13:52:12 -0800 Subject: [PATCH 3/3] Polish confirm sig query --- client/query.go | 3 ++- cmd/plasmacli/subcmd/eth/query/confirmsig.go | 6 ++++-- cmd/plasmacli/subcmd/eth/root.go | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/client/query.go b/client/query.go index 77549ec..71490f5 100644 --- a/client/query.go +++ b/client/query.go @@ -158,7 +158,7 @@ func Blocks(ctx context.CLIContext, startingHeight *big.Int) ([]store.Block, err // Returns confirm sig results for given position // Trusts connected full node -func Signatures(ctx context.CLIContext, position plasma.Position) ([]byte, error) { +func ConfirmSignatures(ctx context.CLIContext, position plasma.Position) ([]byte, error) { key := store.GetOutputKey(position) hash, err := ctx.QueryStore(key, store.DataStoreName) if err != nil { @@ -188,6 +188,7 @@ func Signatures(ctx context.CLIContext, position plasma.Position) ([]byte, error if err := json.Unmarshal(data, &spenderTx); err != nil { return nil, fmt.Errorf("unmarshaling json query response: %s", err) } + for _, input := range spenderTx.Transaction.Inputs { if input.Position.String() == position.String() { for _, sig := range input.ConfirmSignatures { diff --git a/cmd/plasmacli/subcmd/eth/query/confirmsig.go b/cmd/plasmacli/subcmd/eth/query/confirmsig.go index bc37499..945ddb7 100644 --- a/cmd/plasmacli/subcmd/eth/query/confirmsig.go +++ b/cmd/plasmacli/subcmd/eth/query/confirmsig.go @@ -23,16 +23,18 @@ var sigCmd = &cobra.Command{ // parse position pos, err := plasma.FromPositionString(args[0]) if err != nil { - return err + return fmt.Errorf("error parsing position: %s", err) } var sigs []byte ctx := context.NewCLIContext() - sigs, err = client.Signatures(ctx, pos) + sigs, err = client.ConfirmSignatures(ctx, pos) if err != nil { return fmt.Errorf("failed to retrieve confirm signature information: %s", err) } + cmd.SilenceUsage = true + switch len(sigs) { case 0: fmt.Printf("No Confirm Signatures Found") diff --git a/cmd/plasmacli/subcmd/eth/root.go b/cmd/plasmacli/subcmd/eth/root.go index b53ef44..47a4a76 100644 --- a/cmd/plasmacli/subcmd/eth/root.go +++ b/cmd/plasmacli/subcmd/eth/root.go @@ -53,7 +53,7 @@ var ethCmd = &cobra.Command{ Short: "Interact with the plasma smart contract", Long: `Configurations for interacting with the rootchain contract can be specified in /plasma.toml. An eth node instance needs to be running for this command to work.`, - PreRunE: func(cmd *cobra.Command, args []string) error { + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { fmt.Println("setting up eth connection") plasma, err := config.GetContractConn() plasmaContract = plasma