Skip to content

Commit 97d3e9a

Browse files
author
HaoyangLiu
committed
IRISHUB-359: Remove useless broadcast and add tx/send for iris rainbow
1 parent 22874bb commit 97d3e9a

File tree

3 files changed

+87
-48
lines changed

3 files changed

+87
-48
lines changed

client/tendermint/tx/broadcasttx.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

client/tendermint/tx/rest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import (
1010
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec) {
1111
r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(cdc, cliCtx)).Methods("GET")
1212
r.HandleFunc("/txs", SearchTxRequestHandlerFn(cliCtx, cdc)).Methods("GET")
13-
r.HandleFunc("/txs/broadcast", BroadcastTxRequestHandlerFn(cliCtx, cdc)).Methods("POST")
13+
r.HandleFunc("/txs/send", SendTxRequestHandlerFn(cliCtx, cdc)).Methods("POST")
1414
}

client/tendermint/tx/sendtx.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package tx
2+
3+
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
5+
"github.com/cosmos/cosmos-sdk/wire"
6+
"github.com/cosmos/cosmos-sdk/x/auth"
7+
"github.com/irisnet/irishub/client/context"
8+
"github.com/irisnet/irishub/client/utils"
9+
"github.com/tendermint/tendermint/crypto"
10+
"net/http"
11+
)
12+
13+
type sendTx struct {
14+
Msgs []string `json:"msgs"`
15+
Fee auth.StdFee `json:"fee"`
16+
Signatures []stdSignature `json:"signatures"`
17+
Memo string `json:"memo"`
18+
}
19+
20+
type stdSignature struct {
21+
PubKey []byte `json:"pub_key"` // optional
22+
Signature []byte `json:"signature"`
23+
AccountNumber int64 `json:"account_number"`
24+
Sequence int64 `json:"sequence"`
25+
}
26+
27+
func SendTxRequestHandlerFn(cliCtx context.CLIContext, cdc *wire.Codec) http.HandlerFunc {
28+
return func(w http.ResponseWriter, r *http.Request) {
29+
var sendTxBody sendTx
30+
err := utils.ReadPostBody(w, r, cdc, &sendTxBody)
31+
if err != nil {
32+
return
33+
}
34+
cliCtx.Async = utils.AsyncOnlyArg(r)
35+
36+
var sig = make([]auth.StdSignature, len(sendTxBody.Signatures))
37+
for index, s := range sendTxBody.Signatures {
38+
var pubkey crypto.PubKey
39+
if err := cdc.UnmarshalBinaryBare(s.PubKey, &pubkey); err != nil {
40+
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
41+
}
42+
sig[index].PubKey = pubkey
43+
sig[index].Signature = s.Signature
44+
sig[index].AccountNumber = s.AccountNumber
45+
sig[index].Sequence = s.Sequence
46+
}
47+
48+
var msgs = make([]sdk.Msg, len(sendTxBody.Msgs))
49+
for index, msgS := range sendTxBody.Msgs {
50+
var data = []byte(msgS)
51+
var msg sdk.Msg
52+
if err := cdc.UnmarshalJSON(data, &msg); err != nil {
53+
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
54+
return
55+
}
56+
msgs[index] = msg
57+
}
58+
59+
var stdTx = auth.StdTx{
60+
Msgs: msgs,
61+
Fee: sendTxBody.Fee,
62+
Signatures: sig,
63+
Memo: sendTxBody.Memo,
64+
}
65+
txBytes, err := cdc.MarshalBinary(stdTx)
66+
if err != nil {
67+
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
68+
return
69+
}
70+
71+
var res interface{}
72+
if cliCtx.Async {
73+
res, err = cliCtx.BroadcastTxAsync(txBytes)
74+
} else {
75+
res, err = cliCtx.BroadcastTx(txBytes)
76+
}
77+
78+
output, err := cdc.MarshalJSONIndent(res, "", " ")
79+
if err != nil {
80+
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
81+
return
82+
}
83+
84+
w.Write(output)
85+
}
86+
}

0 commit comments

Comments
 (0)