Skip to content

Commit

Permalink
packages refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
facs95 committed Jun 25, 2023
1 parent 71151eb commit d54307f
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 75 deletions.
14 changes: 14 additions & 0 deletions internal/encoding/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package encoding

import (
"github.com/cosmos/cosmos-sdk/simapp/params"

evmosapp "github.com/evmos/evmos/v12/app"
"github.com/evmos/evmos/v12/encoding"
)

// MakeConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig {
mb := evmosapp.ModuleBasics
return encoding.MakeConfig(mb)
}
6 changes: 1 addition & 5 deletions internal/handler/v2/delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ func (h *Handler) DelegationsByAddress(ctx *fasthttp.RequestCtx) {
address := ctx.UserValue("address").(string)
// TODO - validate address before querying numia
if address == "" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
errorResponse := &ErrorResponse{
Error: "Missing address",
}
sendJSONResponse(ctx, errorResponse)
sendBadRequestResponse(ctx, "Missing address in request")
return
}

Expand Down
32 changes: 0 additions & 32 deletions internal/handler/v2/encoding.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/handler/v2/height.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/backend/blob/main/LICENSE)

package v2

import (
Expand Down
19 changes: 19 additions & 0 deletions internal/handler/v2/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"encoding/json"
"net/http"

"github.com/tharsis/dashboard-backend/internal/encoding"

"github.com/gogo/protobuf/proto"
"github.com/valyala/fasthttp"
)

Expand All @@ -23,6 +26,22 @@ func sendSuccessfulJSONResponse(ctx *fasthttp.RequestCtx, response interface{})
sendJSONResponse(ctx, response)
}

// sendSuccesfulProtoJSONResponse sends a successful JSON response to the client.
// It encodes
// It sets the status code to 200.
func sendSuccesfulProtoJSONResponse(ctx *fasthttp.RequestCtx, response proto.Message) {
encConfig := encoding.MakeEncodingConfig()
jsonResponse, err := encConfig.Codec.MarshalJSON(response)
if err != nil {
ctx.Logger().Printf("Error encoding response: %s", err.Error())
ctx.SetStatusCode(http.StatusInternalServerError)
return
}
ctx.SetStatusCode(http.StatusOK)
ctx.Response.Header.SetContentType("application/json")
ctx.SetBody(jsonResponse)
}

// --- Error Responses ---
// Whenever the response is an error, any HTTP code that is not 200,
// we send a JSON response with the following format:
Expand Down
6 changes: 1 addition & 5 deletions internal/handler/v2/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ func (h *Handler) RewardsByAddress(ctx *fasthttp.RequestCtx) {
address := ctx.UserValue("address").(string)
// TODO - validate address before querying numia
if address == "" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
errorResponse := &ErrorResponse{
Error: "Missing address",
}
sendJSONResponse(ctx, errorResponse)
sendBadRequestResponse(ctx, "Missing address in request")
return
}

Expand Down
17 changes: 10 additions & 7 deletions internal/handler/v2/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ package v2
import (
"encoding/json"

"github.com/tharsis/dashboard-backend/internal/node/rest"

"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/valyala/fasthttp"
)

// BroadcastParams represents the parameters for the POST /v2/tx/broadcast endpoint.
type BroadcastParams struct {
// BroadcastTxParams represents the parameters for the POST /v2/tx/broadcast endpoint.
type BroadcastTxParams struct {
// which network should the transaction be broadcasted to
Network string `json:"network"`
// the signed transaction to be broadcasted
TxBytes []byte `json:"tx_bytes"`
}

type BroadcastTxResponse struct {
tx.BroadcastTxResponse
TxResponse types.TxResponse `json:"tx_response"`
}

// BroadcastTx handles POST /tx/broadcast.
// It broadcasts a signed transaction to the specified network.
// It broadcasts a signed transaction synchronously to the specified network.
// Returns:
//
// {
Expand All @@ -41,7 +44,7 @@ type BroadcastTxResponse struct {
// }
// }
func (h *Handler) BroadcastTx(ctx *fasthttp.RequestCtx) {
reqParams := BroadcastParams{}
reqParams := BroadcastTxParams{}
if err := json.Unmarshal(ctx.PostBody(), &reqParams); err != nil {
ctx.Logger().Printf("Error decoding request body: %s", err.Error())
sendBadRequestResponse(ctx, "Invalid request body")
Expand All @@ -60,7 +63,7 @@ func (h *Handler) BroadcastTx(ctx *fasthttp.RequestCtx) {
return
}

restClient, err := NewRestClient(reqParams.Network)
restClient, err := rest.NewClient(reqParams.Network)
if err != nil {
ctx.Logger().Printf("Error creating rest client: %s", err.Error())
sendInternalErrorResponse(ctx)
Expand All @@ -74,5 +77,5 @@ func (h *Handler) BroadcastTx(ctx *fasthttp.RequestCtx) {
return
}

sendSuccessfulJSONResponse(ctx, txResponse)
sendSuccesfulProtoJSONResponse(ctx, &txResponse)
}
31 changes: 6 additions & 25 deletions internal/handler/v2/http.go → internal/node/rest/client.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package v2
package rest

import (
"bytes"
"fmt"
"io"
"net/http"

"github.com/cosmos/cosmos-sdk/types/tx"
)

type RestClient struct {
type Client struct {
apiAddress string
}

func NewRestClient(network string) (*RestClient, error) {
// NewClient returns a new instance of a RestClient.
func NewClient(network string) (*Client, error) {
apiAddress, err := getAPIAddress(network)
if err != nil {
return nil, fmt.Errorf("error while getting api address from redis: %w", err)
}
return &RestClient{
return &Client{
apiAddress: apiAddress,
}, nil
}
Expand All @@ -30,7 +29,7 @@ func getAPIAddress(_ string) (string, error) {

// PostRequest defines a wrapper around an HTTP POST request with a provided URL and data.
// An error is returned if the request or reading the body fails.
func (c *RestClient) post(url string, body []byte) ([]byte, error) {
func (c *Client) postRequest(url string, body []byte) ([]byte, error) {
// join the node's address with the endpoint's path
queryURL := fmt.Sprintf("%s/%s", c.apiAddress, url)

Expand All @@ -48,21 +47,3 @@ func (c *RestClient) post(url string, body []byte) ([]byte, error) {
}
return bz, nil
}

// BroadcastTx broadcasts transaction bytes to a Tendermint node
// synchronously through the REST API.
func (c *RestClient) BroadcastTx(txBytes []byte) (tx.BroadcastTxResponse, error) {
broadcastTxEndpoint := "cosmos/tx/v1beta1/txs"
postResponse, err := c.post(broadcastTxEndpoint, txBytes)
if err != nil {
return tx.BroadcastTxResponse{}, err
}
encConfig := MakeEncodingConfig()

jsonResponse := tx.BroadcastTxResponse{}
err = encConfig.Codec.UnmarshalJSON(postResponse, &jsonResponse)
if err != nil {
return tx.BroadcastTxResponse{}, fmt.Errorf("error while unmarshalling response body: %w", err)
}
return jsonResponse, nil
}
29 changes: 29 additions & 0 deletions internal/node/rest/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package rest

import (
"fmt"

"github.com/tharsis/dashboard-backend/internal/encoding"

"github.com/cosmos/cosmos-sdk/types/tx"
)

// All endpoints under /cosmos/tx/ path should be defined in this file

// BroadcastTx broadcasts transaction bytes to a Tendermint node
// through its REST API.
func (c *Client) BroadcastTx(txBytes []byte) (tx.BroadcastTxResponse, error) {
broadcastTxEndpoint := "cosmos/tx/v1beta1/txs"
postResponse, err := c.postRequest(broadcastTxEndpoint, txBytes)
if err != nil {
return tx.BroadcastTxResponse{}, err
}

encConfig := encoding.MakeEncodingConfig()
jsonResponse := tx.BroadcastTxResponse{}
err = encConfig.Codec.UnmarshalJSON(postResponse, &jsonResponse)
if err != nil {
return tx.BroadcastTxResponse{}, fmt.Errorf("error while unmarshalling response body: %w", err)
}
return jsonResponse, nil
}

0 comments on commit d54307f

Please sign in to comment.