Skip to content

Commit 5c87d36

Browse files
authored
Merge pull request #54 from MinterTeam/dev
refactor api
2 parents ebe8897 + cdbf78f commit 5c87d36

9 files changed

Lines changed: 48 additions & 62 deletions

Gopkg.lock

Lines changed: 27 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/api.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@ import (
1010
"github.com/MinterTeam/minter-go-node/cmd/utils"
1111
"github.com/MinterTeam/minter-go-node/core/minter"
1212
"github.com/MinterTeam/minter-go-node/core/state"
13-
"github.com/tendermint/tendermint/rpc/core/types"
14-
"github.com/tendermint/tendermint/rpc/lib/client"
13+
rpc "github.com/tendermint/tendermint/rpc/client"
1514
"strconv"
1615
"time"
1716
)
1817

1918
var (
2019
blockchain *minter.Blockchain
21-
client *rpcclient.JSONRPCClient
20+
client *rpc.HTTP
2221
)
2322

2423
func RunApi(b *minter.Blockchain) {
25-
client = rpcclient.NewJSONRPCClient(*utils.TendermintRpcAddrFlag)
26-
core_types.RegisterAmino(client.Codec())
24+
client = rpc.NewHTTP(*utils.TendermintRpcAddrFlag, "/websocket")
2725

2826
blockchain = b
2927

@@ -55,8 +53,7 @@ func RunApi(b *minter.Blockchain) {
5553

5654
// wait for tendermint to start
5755
for true {
58-
result := new(core_types.ResultHealth)
59-
_, err := client.Call("health", map[string]interface{}{}, result)
56+
_, err := client.Health()
6057
if err == nil {
6158
break
6259
}

api/block.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/MinterTeam/minter-go-node/core/transaction"
77
"github.com/gorilla/mux"
88
"github.com/tendermint/tendermint/libs/common"
9-
"github.com/tendermint/tendermint/rpc/core/types"
109
"github.com/tendermint/tendermint/types"
1110
"math/big"
1211
"net/http"
@@ -41,10 +40,7 @@ func Block(w http.ResponseWriter, r *http.Request) {
4140
vars := mux.Vars(r)
4241
height, _ := strconv.ParseInt(vars["height"], 10, 64)
4342

44-
result := new(core_types.ResultBlock)
45-
_, err := client.Call("block", map[string]interface{}{
46-
"height": height,
47-
}, result)
43+
result, err := client.Block(&height)
4844

4945
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
5046

api/send_transaction.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/MinterTeam/minter-go-node/core/code"
99
"github.com/MinterTeam/minter-go-node/core/types"
10-
"github.com/tendermint/tendermint/rpc/core/types"
1110
"net/http"
1211
"strings"
1312
)
@@ -22,10 +21,7 @@ func SendTransaction(w http.ResponseWriter, r *http.Request) {
2221
body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
2322
json.Unmarshal(body, &req)
2423

25-
result := new(core_types.ResultBroadcastTxCommit)
26-
_, err := client.Call("broadcast_tx_commit", map[string]interface{}{
27-
"tx": types.Hex2Bytes(req.Transaction),
28-
}, result)
24+
result, err := client.BroadcastTxCommit(types.Hex2Bytes(req.Transaction))
2925

3026
if err != nil {
3127
panic(err)

api/send_transaction_async.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/MinterTeam/minter-go-node/core/code"
99
"github.com/MinterTeam/minter-go-node/core/types"
10-
"github.com/tendermint/tendermint/rpc/core/types"
1110
"net/http"
1211
"strings"
1312
)
@@ -18,10 +17,7 @@ func SendTransactionAsync(w http.ResponseWriter, r *http.Request) {
1817
body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
1918
json.Unmarshal(body, &req)
2019

21-
result := new(core_types.ResultBroadcastTx)
22-
_, err := client.Call("broadcast_tx_async", map[string]interface{}{
23-
"tx": types.Hex2Bytes(req.Transaction),
24-
}, result)
20+
result, err := client.BroadcastTxAsync(types.Hex2Bytes(req.Transaction))
2521

2622
if err != nil {
2723
panic(err)

api/send_transaction_sync.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/MinterTeam/minter-go-node/core/code"
99
"github.com/MinterTeam/minter-go-node/core/types"
10-
"github.com/tendermint/tendermint/rpc/core/types"
1110
"net/http"
1211
"strings"
1312
)
@@ -18,10 +17,7 @@ func SendTransactionSync(w http.ResponseWriter, r *http.Request) {
1817
body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
1918
json.Unmarshal(body, &req)
2019

21-
result := new(core_types.ResultBroadcastTx)
22-
_, err := client.Call("broadcast_tx_sync", map[string]interface{}{
23-
"tx": types.Hex2Bytes(req.Transaction),
24-
}, result)
20+
result, err := client.BroadcastTxSync(types.Hex2Bytes(req.Transaction))
2521

2622
if err != nil {
2723
panic(err)

api/status.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package api
33
import (
44
"encoding/json"
55
"github.com/tendermint/tendermint/libs/common"
6-
"github.com/tendermint/tendermint/rpc/core/types"
76
"net/http"
87
"time"
98
)
@@ -17,17 +16,21 @@ type StatusResponse struct {
1716

1817
func Status(w http.ResponseWriter, r *http.Request) {
1918

20-
result := new(core_types.ResultStatus)
21-
_, err := client.Call("status", map[string]interface{}{}, result)
19+
result, err := client.Status()
20+
21+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
2222

2323
if err != nil {
24-
panic(err)
24+
w.WriteHeader(http.StatusOK)
25+
json.NewEncoder(w).Encode(Response{
26+
Code: 500,
27+
Result: nil,
28+
Log: err.Error(),
29+
})
30+
return
2531
}
2632

27-
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
28-
w.WriteHeader(http.StatusOK)
29-
30-
err = json.NewEncoder(w).Encode(Response{
33+
json.NewEncoder(w).Encode(Response{
3134
Code: 0,
3235
Result: StatusResponse{
3336
LatestBlockHash: common.HexBytes(result.SyncInfo.LatestBlockHash),
@@ -36,8 +39,4 @@ func Status(w http.ResponseWriter, r *http.Request) {
3639
LatestBlockTime: result.SyncInfo.LatestBlockTime,
3740
},
3841
})
39-
40-
if err != nil {
41-
panic(err)
42-
}
4342
}

api/transaction.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/hex"
55
"encoding/json"
66
"github.com/gorilla/mux"
7-
"github.com/tendermint/tendermint/types"
87
"net/http"
98
"strings"
109
)
@@ -15,10 +14,7 @@ func Transaction(w http.ResponseWriter, r *http.Request) {
1514
hash := strings.TrimLeft(vars["hash"], "Mt")
1615
decoded, err := hex.DecodeString(hash)
1716

18-
result := new(types.TxResult)
19-
_, err = client.Call("tx", map[string]interface{}{
20-
"hash": decoded,
21-
}, result)
17+
result, err := client.Tx(decoded, false)
2218

2319
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
2420

api/transactions.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ func Transactions(w http.ResponseWriter, r *http.Request) {
4242

4343
query := r.URL.Query().Get("query")
4444

45-
rpcResult := new(ResultTxSearch)
46-
_, err := client.Call("tx_search", map[string]interface{}{
47-
"query": query,
48-
"page": 1,
49-
"per_page": 100,
50-
}, rpcResult)
45+
rpcResult, err := client.TxSearch(query, false, 1, 100)
5146

5247
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
5348

0 commit comments

Comments
 (0)