From cdbf78f0f35bf7e88dece6409612d7ec44a9c1d4 Mon Sep 17 00:00:00 2001 From: Daniil Lashin Date: Wed, 4 Jul 2018 15:09:47 +0300 Subject: [PATCH] refactor api --- Gopkg.lock | 39 ++++++++++++++++++++++++----------- api/api.go | 11 ++++------ api/block.go | 6 +----- api/send_transaction.go | 6 +----- api/send_transaction_async.go | 6 +----- api/send_transaction_sync.go | 6 +----- api/status.go | 23 ++++++++++----------- api/transaction.go | 6 +----- api/transactions.go | 7 +------ 9 files changed, 48 insertions(+), 62 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 0beebf87e..450bf5c00 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -152,7 +152,10 @@ [[projects]] name = "github.com/prometheus/client_golang" - packages = ["prometheus"] + packages = [ + "prometheus", + "prometheus/promhttp" + ] revision = "c5b7fccd204277076155f10851dad72b76a49317" version = "v0.8.0" @@ -241,38 +244,49 @@ "abci/example/kvstore", "abci/server", "abci/types", + "blockchain", "config", + "consensus", + "consensus/types", "crypto", "crypto/merkle", "crypto/tmhash", + "evidence", + "libs/autofile", + "libs/clist", "libs/common", "libs/db", + "libs/events", "libs/flowrate", "libs/log", "libs/pubsub", "libs/pubsub/query", + "mempool", + "node", "p2p", "p2p/conn", + "p2p/pex", "p2p/upnp", + "privval", "proxy", + "rpc/client", + "rpc/core", "rpc/core/types", + "rpc/grpc", + "rpc/lib", "rpc/lib/client", + "rpc/lib/server", "rpc/lib/types", "state", - "types" + "state/txindex", + "state/txindex/kv", + "state/txindex/null", + "types", + "version" ] revision = "dfa9a9a30a666132425b29454e90a472aa579a48" version = "v0.22.0" -[[projects]] - name = "github.com/tendermint/tmlibs" - packages = [ - "common", - "log" - ] - revision = "692f1d86a6e2c0efa698fd1e4541b68c74ffaf38" - version = "v0.8.4" - [[projects]] branch = "master" name = "golang.org/x/crypto" @@ -298,6 +312,7 @@ "http2/hpack", "idna", "internal/timeseries", + "netutil", "trace" ] revision = "1e491301e022f8f977054da4c2d852decd59571f" @@ -374,6 +389,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "5dc565d960a37820eecf571bb5de7fd224da44e3fe11c277fdb65f1fa4fddc89" + inputs-digest = "b5767faba6c17d4970ec50b07e9c0f7ba70872b835c1fef51441a4101de8a9b1" solver-name = "gps-cdcl" solver-version = 1 diff --git a/api/api.go b/api/api.go index e91e8f116..a4c4135a6 100644 --- a/api/api.go +++ b/api/api.go @@ -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 @@ -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 } diff --git a/api/block.go b/api/block.go index 5996c6af3..be48e2173 100644 --- a/api/block.go +++ b/api/block.go @@ -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" @@ -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") diff --git a/api/send_transaction.go b/api/send_transaction.go index 7a0b953dc..83da7052c 100644 --- a/api/send_transaction.go +++ b/api/send_transaction.go @@ -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" ) @@ -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) diff --git a/api/send_transaction_async.go b/api/send_transaction_async.go index a0c3315cc..e88c9d957 100644 --- a/api/send_transaction_async.go +++ b/api/send_transaction_async.go @@ -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" ) @@ -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) diff --git a/api/send_transaction_sync.go b/api/send_transaction_sync.go index c2051d97f..15d90e2bc 100644 --- a/api/send_transaction_sync.go +++ b/api/send_transaction_sync.go @@ -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" ) @@ -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) diff --git a/api/status.go b/api/status.go index b3bc246c6..c2334b7d8 100644 --- a/api/status.go +++ b/api/status.go @@ -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" ) @@ -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), @@ -36,8 +39,4 @@ func Status(w http.ResponseWriter, r *http.Request) { LatestBlockTime: result.SyncInfo.LatestBlockTime, }, }) - - if err != nil { - panic(err) - } } diff --git a/api/transaction.go b/api/transaction.go index f54824347..f1ed0bedd 100644 --- a/api/transaction.go +++ b/api/transaction.go @@ -4,7 +4,6 @@ import ( "encoding/hex" "encoding/json" "github.com/gorilla/mux" - "github.com/tendermint/tendermint/types" "net/http" "strings" ) @@ -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") diff --git a/api/transactions.go b/api/transactions.go index 2a00620c2..b56ad60f3 100644 --- a/api/transactions.go +++ b/api/transactions.go @@ -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")