Skip to content

Commit

Permalink
Merge pull request #54 from MinterTeam/dev
Browse files Browse the repository at this point in the history
refactor api
  • Loading branch information
danil-lashin committed Jul 4, 2018
2 parents ebe8897 + cdbf78f commit 5c87d36
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 62 deletions.
39 changes: 27 additions & 12 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ import (
"github.com/MinterTeam/minter-go-node/cmd/utils"
"github.com/MinterTeam/minter-go-node/core/minter"
"github.com/MinterTeam/minter-go-node/core/state"
"github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/rpc/lib/client"
rpc "github.com/tendermint/tendermint/rpc/client"
"strconv"
"time"
)

var (
blockchain *minter.Blockchain
client *rpcclient.JSONRPCClient
client *rpc.HTTP
)

func RunApi(b *minter.Blockchain) {
client = rpcclient.NewJSONRPCClient(*utils.TendermintRpcAddrFlag)
core_types.RegisterAmino(client.Codec())
client = rpc.NewHTTP(*utils.TendermintRpcAddrFlag, "/websocket")

blockchain = b

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

// wait for tendermint to start
for true {
result := new(core_types.ResultHealth)
_, err := client.Call("health", map[string]interface{}{}, result)
_, err := client.Health()
if err == nil {
break
}
Expand Down
6 changes: 1 addition & 5 deletions api/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/gorilla/mux"
"github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/types"
"math/big"
"net/http"
Expand Down Expand Up @@ -41,10 +40,7 @@ func Block(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
height, _ := strconv.ParseInt(vars["height"], 10, 64)

result := new(core_types.ResultBlock)
_, err := client.Call("block", map[string]interface{}{
"height": height,
}, result)
result, err := client.Block(&height)

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

Expand Down
6 changes: 1 addition & 5 deletions api/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

result := new(core_types.ResultBroadcastTxCommit)
_, err := client.Call("broadcast_tx_commit", map[string]interface{}{
"tx": types.Hex2Bytes(req.Transaction),
}, result)
result, err := client.BroadcastTxCommit(types.Hex2Bytes(req.Transaction))

if err != nil {
panic(err)
Expand Down
6 changes: 1 addition & 5 deletions api/send_transaction_async.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

result := new(core_types.ResultBroadcastTx)
_, err := client.Call("broadcast_tx_async", map[string]interface{}{
"tx": types.Hex2Bytes(req.Transaction),
}, result)
result, err := client.BroadcastTxAsync(types.Hex2Bytes(req.Transaction))

if err != nil {
panic(err)
Expand Down
6 changes: 1 addition & 5 deletions api/send_transaction_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

result := new(core_types.ResultBroadcastTx)
_, err := client.Call("broadcast_tx_sync", map[string]interface{}{
"tx": types.Hex2Bytes(req.Transaction),
}, result)
result, err := client.BroadcastTxSync(types.Hex2Bytes(req.Transaction))

if err != nil {
panic(err)
Expand Down
23 changes: 11 additions & 12 deletions api/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"encoding/json"
"github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/rpc/core/types"
"net/http"
"time"
)
Expand All @@ -17,17 +16,21 @@ type StatusResponse struct {

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

result := new(core_types.ResultStatus)
_, err := client.Call("status", map[string]interface{}{}, result)
result, err := client.Status()

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

if err != nil {
panic(err)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Response{
Code: 500,
Result: nil,
Log: err.Error(),
})
return
}

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)

err = json.NewEncoder(w).Encode(Response{
json.NewEncoder(w).Encode(Response{
Code: 0,
Result: StatusResponse{
LatestBlockHash: common.HexBytes(result.SyncInfo.LatestBlockHash),
Expand All @@ -36,8 +39,4 @@ func Status(w http.ResponseWriter, r *http.Request) {
LatestBlockTime: result.SyncInfo.LatestBlockTime,
},
})

if err != nil {
panic(err)
}
}
6 changes: 1 addition & 5 deletions api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/hex"
"encoding/json"
"github.com/gorilla/mux"
"github.com/tendermint/tendermint/types"
"net/http"
"strings"
)
Expand All @@ -15,10 +14,7 @@ func Transaction(w http.ResponseWriter, r *http.Request) {
hash := strings.TrimLeft(vars["hash"], "Mt")
decoded, err := hex.DecodeString(hash)

result := new(types.TxResult)
_, err = client.Call("tx", map[string]interface{}{
"hash": decoded,
}, result)
result, err := client.Tx(decoded, false)

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

Expand Down
7 changes: 1 addition & 6 deletions api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ func Transactions(w http.ResponseWriter, r *http.Request) {

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

rpcResult := new(ResultTxSearch)
_, err := client.Call("tx_search", map[string]interface{}{
"query": query,
"page": 1,
"per_page": 100,
}, rpcResult)
rpcResult, err := client.TxSearch(query, false, 1, 100)

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

Expand Down

0 comments on commit 5c87d36

Please sign in to comment.