-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement broadcast command/REST endpoint to submit transactions generated offline with --generated-only and the sign command. Closes: #1954
- Loading branch information
Alessio Treglia
committed
Sep 8, 2018
1 parent
88a2dde
commit 4c8cd38
Showing
10 changed files
with
203 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cli | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/context" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
"github.com/spf13/cobra" | ||
amino "github.com/tendermint/go-amino" | ||
) | ||
|
||
// GetSignCommand returns the sign command | ||
func GetBroadcastCommand(codec *amino.Codec) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "broadcast <file>", | ||
Short: "Broadcast transactions generated offline", | ||
Long: `Broadcast transactions created with the --generate-only flag and signed with the sign command. | ||
Read a transaction from <file> and broadcast it to a node.`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
cliCtx := context.NewCLIContext().WithCodec(codec).WithLogger(os.Stdout) | ||
stdTx, err := readAndUnmarshalStdTx(cliCtx.Codec, args[0]) | ||
if err != nil { | ||
return | ||
} | ||
txBytes, err := cliCtx.Codec.MarshalBinary(stdTx) | ||
if err != nil { | ||
return | ||
} | ||
return cliCtx.EnsureBroadcastTx(txBytes) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func readAndUnmarshalStdTx(cdc *amino.Codec, filename string) (stdTx auth.StdTx, err error) { | ||
var bytes []byte | ||
if bytes, err = ioutil.ReadFile(filename); err != nil { | ||
return | ||
} | ||
if err = cdc.UnmarshalJSON(bytes, &stdTx); err != nil { | ||
return | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package rest | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/context" | ||
"github.com/cosmos/cosmos-sdk/client/utils" | ||
"github.com/cosmos/cosmos-sdk/wire" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
) | ||
|
||
type broadcastBody struct { | ||
Tx auth.StdTx `json:"tx"` | ||
} | ||
|
||
// BroadcastTxRequestHandlerFn returns the broadcast tx REST handler | ||
func BroadcastTxRequestHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var m broadcastBody | ||
if ok := unmarshalBodyOrReturnBadRequest(cliCtx, w, r, &m); !ok { | ||
return | ||
} | ||
|
||
txBytes, err := cliCtx.Codec.MarshalBinary(m.Tx) | ||
if err != nil { | ||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
res, err := cliCtx.BroadcastTx(txBytes) | ||
if err != nil { | ||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
output, err := wire.MarshalJSONIndent(cdc, res) | ||
if err != nil { | ||
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
w.Write(output) | ||
} | ||
} | ||
|
||
func unmarshalBodyOrReturnBadRequest(cliCtx context.CLIContext, w http.ResponseWriter, r *http.Request, m *broadcastBody) bool { | ||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return false | ||
} | ||
err = cliCtx.Codec.UnmarshalJSON(body, m) | ||
if err != nil { | ||
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters