Skip to content

Commit

Permalink
feat(logging): integrate log creation functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
golnar-boosty committed Nov 19, 2024
1 parent 6e624ad commit 7f952d5
Show file tree
Hide file tree
Showing 60 changed files with 944 additions and 376 deletions.
92 changes: 46 additions & 46 deletions common/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/KiraCore/interx/config"
"github.com/KiraCore/interx/database"
"github.com/KiraCore/interx/global"
"github.com/KiraCore/interx/log"
"github.com/KiraCore/interx/types"
tmjson "github.com/cometbft/cometbft/libs/json"
tmTypes "github.com/cometbft/cometbft/rpc/core/types"
Expand All @@ -26,19 +27,18 @@ import (
// MakeTendermintRPCRequest is a function to make GET request
func MakeTendermintRPCRequest(rpcAddr string, url string, query string) (interface{}, interface{}, int) {
endpoint := fmt.Sprintf("%s%s?%s", rpcAddr, url, query)
// GetLogger().Info("[rpc-call] Entering tendermint rpc call: ", endpoint)

resp, err := http.Get(endpoint)
if err != nil {
GetLogger().Error("[rpc-call] Unable to connect to ", endpoint)
log.CustomLogger().Error("[rpc-call] Unable to connect to ", endpoint)
return ServeError(0, "", err.Error(), http.StatusInternalServerError)
}
defer resp.Body.Close()

response := new(types.RPCResponse)
err = json.NewDecoder(resp.Body).Decode(response)
if err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: : ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: : ", err)
return nil, err.Error(), resp.StatusCode
}

Expand All @@ -48,11 +48,11 @@ func MakeTendermintRPCRequest(rpcAddr string, url string, query string) (interfa
// MakeGetRequest is a function to make GET request
func MakeGetRequest(rpcAddr string, url string, query string) (Result interface{}, Error interface{}, StatusCode int) {
endpoint := fmt.Sprintf("%s%s?%s", rpcAddr, url, query)
// GetLogger().Info("[rpc-call] Entering rpc call: ", endpoint)
// log.CustomLogger().Info("[rpc-call] Entering rpc call: ", endpoint)

resp, err := http.Get(endpoint)
if err != nil {
GetLogger().Error("[rpc-call] Unable to connect to ", endpoint)
log.CustomLogger().Error("[rpc-call] Unable to connect to ", endpoint)
return ServeError(0, "", err.Error(), http.StatusInternalServerError)
}
defer resp.Body.Close()
Expand All @@ -61,7 +61,7 @@ func MakeGetRequest(rpcAddr string, url string, query string) (Result interface{

err = json.NewDecoder(resp.Body).Decode(&Result)
if err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: : ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: : ", err)
Error = err.Error()
}

Expand All @@ -71,11 +71,11 @@ func MakeGetRequest(rpcAddr string, url string, query string) (Result interface{
// DownloadResponseToFile is a function to save GET response as a file
func DownloadResponseToFile(rpcAddr string, url string, query string, filepath string) error {
endpoint := fmt.Sprintf("%s%s?%s", rpcAddr, url, query)
// GetLogger().Info("[rpc-call] Entering rpc call: ", endpoint)
// log.CustomLogger().Info("[rpc-call] Entering rpc call: ", endpoint)

resp, err := http.Get(endpoint)
if err != nil {
GetLogger().Error("[rpc-call] Unable to connect to ", endpoint)
log.CustomLogger().Error("[rpc-call] Unable to connect to ", endpoint)
return err
}
defer resp.Body.Close()
Expand All @@ -86,7 +86,7 @@ func DownloadResponseToFile(rpcAddr string, url string, query string, filepath s
global.Mutex.Lock()
_, err = io.Copy(fileout, resp.Body)
if err != nil {
GetLogger().Error("[rpc-call] Unable to save response")
log.CustomLogger().Error("[rpc-call] Unable to save response")
}

global.Mutex.Unlock()
Expand All @@ -98,15 +98,15 @@ func DownloadResponseToFile(rpcAddr string, url string, query string, filepath s
func GetAccountBalances(gwCosmosmux *runtime.ServeMux, r *http.Request, bech32addr string) []types.Coin {
_, err := sdk.AccAddressFromBech32(bech32addr)
if err != nil {
GetLogger().Error("[grpc-call] Invalid bech32addr: ", bech32addr)
log.CustomLogger().Error("[grpc-call] Invalid bech32addr: ", bech32addr)
return nil
}

r.URL.Path = fmt.Sprintf("/cosmos/bank/v1beta1/balances/%s", bech32addr)
r.URL.RawQuery = ""
r.Method = "GET"

// GetLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)
// log.CustomLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)

recorder := httptest.NewRecorder()
gwCosmosmux.ServeHTTP(recorder, r)
Expand All @@ -119,7 +119,7 @@ func GetAccountBalances(gwCosmosmux *runtime.ServeMux, r *http.Request, bech32ad
result := BalancesResponse{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
GetLogger().Error("[grpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[grpc-call] Unable to decode response: ", err)
}

return result.Balances
Expand All @@ -129,15 +129,15 @@ func GetAccountBalances(gwCosmosmux *runtime.ServeMux, r *http.Request, bech32ad
func GetAccountNumberSequence(gwCosmosmux *runtime.ServeMux, r *http.Request, bech32addr string) (uint64, uint64) {
_, err := sdk.AccAddressFromBech32(bech32addr)
if err != nil {
GetLogger().Error("[grpc-call] Invalid bech32addr: ", bech32addr)
log.CustomLogger().Error("[grpc-call] Invalid bech32addr: ", bech32addr)
return 0, 0
}

r.URL.Path = fmt.Sprintf("/cosmos/auth/v1beta1/accounts/%s", bech32addr)
r.URL.RawQuery = ""
r.Method = "GET"

// GetLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)
// log.CustomLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)

recorder := httptest.NewRecorder()
gwCosmosmux.ServeHTTP(recorder, r)
Expand All @@ -154,7 +154,7 @@ func GetAccountNumberSequence(gwCosmosmux *runtime.ServeMux, r *http.Request, be
result := QueryAccountResponse{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
GetLogger().Error("[grpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[grpc-call] Unable to decode response: ", err)
}

accountNumber, _ := strconv.ParseInt(result.Account.AccountNumber, 10, 64)
Expand All @@ -166,11 +166,11 @@ func GetAccountNumberSequence(gwCosmosmux *runtime.ServeMux, r *http.Request, be
// BroadcastTransaction is a function to post transaction, returns txHash
func BroadcastTransaction(rpcAddr string, txBytes []byte) (string, error) {
endpoint := fmt.Sprintf("%s/broadcast_tx_async?tx=0x%X", rpcAddr, txBytes)
GetLogger().Info("[rpc-call] Entering rpc call: ", endpoint)
log.CustomLogger().Info("[rpc-call] Entering rpc call: ", endpoint)

resp, err := http.Get(endpoint)
if err != nil {
GetLogger().Error("[rpc-call] Unable to connect to ", endpoint)
log.CustomLogger().Error("[rpc-call] Unable to connect to ", endpoint)
return "", err
}
defer resp.Body.Close()
Expand All @@ -190,12 +190,12 @@ func BroadcastTransaction(rpcAddr string, txBytes []byte) (string, error) {
result := new(RPCTempResponse)
err = json.NewDecoder(resp.Body).Decode(result)
if err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: ", err)
return "", err
}

if resp.StatusCode != http.StatusOK {
GetLogger().Error("[rpc-call] Unable to broadcast transaction: ", result.Error.Message)
log.CustomLogger().Error("[rpc-call] Unable to broadcast transaction: ", result.Error.Message)
return "", errors.New(result.Error.Message)
}

Expand All @@ -218,11 +218,11 @@ func GetBlockTime(rpcAddr string, height int64) (int64, error) {
}

endpoint := fmt.Sprintf("%s/block?height=%d", rpcAddr, height)
// GetLogger().Info("[rpc-call] Entering rpc call: ", endpoint)
// log.CustomLogger().Info("[rpc-call] Entering rpc call: ", endpoint)

resp, err := http.Get(endpoint)
if err != nil {
GetLogger().Error("[rpc-call] Unable to connect to ", endpoint)
log.CustomLogger().Error("[rpc-call] Unable to connect to ", endpoint)
return 0, fmt.Errorf("block not found: %d", height)
}
defer resp.Body.Close()
Expand All @@ -232,18 +232,18 @@ func GetBlockTime(rpcAddr string, height int64) (int64, error) {
response := new(tmJsonRPCTypes.RPCResponse)

if err := json.Unmarshal(respBody, response); err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: ", err)
return 0, err
}

if response.Error != nil {
GetLogger().Error("[rpc-call] Block not found: ", height)
log.CustomLogger().Error("[rpc-call] Block not found: ", height)
return 0, fmt.Errorf("block not found: %d", height)
}

result := new(tmTypes.ResultBlock)
if err := tmjson.Unmarshal(response.Result, result); err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: ", err)
return 0, err
}

Expand All @@ -267,11 +267,11 @@ func GetBlockNanoTime(rpcAddr string, height int64) (int64, error) {
}

endpoint := fmt.Sprintf("%s/block?height=%d", rpcAddr, height)
// GetLogger().Info("[rpc-call] Entering rpc call: ", endpoint)
// log.CustomLogger().Info("[rpc-call] Entering rpc call: ", endpoint)

resp, err := http.Get(endpoint)
if err != nil {
GetLogger().Error("[rpc-call] Unable to connect to ", endpoint)
log.CustomLogger().Error("[rpc-call] Unable to connect to ", endpoint)
return 0, fmt.Errorf("block not found: %d", height)
}
defer resp.Body.Close()
Expand All @@ -281,18 +281,18 @@ func GetBlockNanoTime(rpcAddr string, height int64) (int64, error) {
response := new(tmJsonRPCTypes.RPCResponse)

if err := json.Unmarshal(respBody, response); err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: ", err)
return 0, err
}

if response.Error != nil {
GetLogger().Error("[rpc-call] Block not found: ", height)
log.CustomLogger().Error("[rpc-call] Block not found: ", height)
return 0, fmt.Errorf("block not found: %d", height)
}

result := new(tmTypes.ResultBlock)
if err := tmjson.Unmarshal(response.Result, result); err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: ", err)
return 0, err
}

Expand All @@ -319,7 +319,7 @@ func GetTokenAliases(gwCosmosmux *runtime.ServeMux, r *http.Request) ([]types.To
r.URL.RawQuery = ""
r.Method = "GET"

// GetLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)
// log.CustomLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)
recorder := httptest.NewRecorder()
gwCosmosmux.ServeHTTP(recorder, r)
resp := recorder.Result()
Expand All @@ -334,13 +334,13 @@ func GetTokenAliases(gwCosmosmux *runtime.ServeMux, r *http.Request) ([]types.To

err := json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
GetLogger().Error("[grpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[grpc-call] Unable to decode response: ", err)
}

// save block time
err = database.AddTokenAliases(result.Data)
if err != nil {
GetLogger().Error("[grpc-call] Unable to save response")
log.CustomLogger().Error("[grpc-call] Unable to save response")
}

return result.Data, result.DefaultDenom, result.Bech32Prefix
Expand All @@ -352,7 +352,7 @@ func GetAllBalances(gwCosmosmux *runtime.ServeMux, r *http.Request, bech32Addr s
r.URL.RawQuery = "pagination.limit=100000"
r.Method = "GET"

// GetLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)
// log.CustomLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)

recorder := httptest.NewRecorder()
gwCosmosmux.ServeHTTP(recorder, r)
Expand All @@ -365,7 +365,7 @@ func GetAllBalances(gwCosmosmux *runtime.ServeMux, r *http.Request, bech32Addr s
result := AllBalancesResponse{}
err := json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
GetLogger().Error("[grpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[grpc-call] Unable to decode response: ", err)
}

return result.Balances
Expand All @@ -377,7 +377,7 @@ func GetTokenSupply(gwCosmosmux *runtime.ServeMux, r *http.Request) []types.Toke
r.URL.RawQuery = ""
r.Method = "GET"

// GetLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)
// log.CustomLogger().Info("[grpc-call] Entering grpc call: ", r.URL.Path)

recorder := httptest.NewRecorder()
gwCosmosmux.ServeHTTP(recorder, r)
Expand All @@ -390,7 +390,7 @@ func GetTokenSupply(gwCosmosmux *runtime.ServeMux, r *http.Request) []types.Toke
result := TokenAliasesResponse{}
err := json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
GetLogger().Error("[grpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[grpc-call] Unable to decode response: ", err)
}

return result.Supply
Expand All @@ -404,12 +404,12 @@ func GetKiraStatus(rpcAddr string) *types.KiraStatus {

byteData, err := json.Marshal(success)
if err != nil {
GetLogger().Error("[kira-status] Invalid response format", err)
log.CustomLogger().Error("[kira-status] Invalid response format", err)
}

err = json.Unmarshal(byteData, &result)
if err != nil {
GetLogger().Error("[kira-status] Invalid response format", err)
log.CustomLogger().Error("[kira-status] Invalid response format", err)
}

return &result
Expand All @@ -426,13 +426,13 @@ func GetInterxStatus(interxAddr string) *types.InterxStatus {

byteData, err := json.Marshal(success)
if err != nil {
GetLogger().Error("[interx-status] Invalid response format", err)
log.CustomLogger().Error("[interx-status] Invalid response format", err)
return nil
}

err = json.Unmarshal(byteData, &result)
if err != nil {
GetLogger().Error("[interx-status] Invalid response format", err)
log.CustomLogger().Error("[interx-status] Invalid response format", err)
return nil
}

Expand All @@ -450,13 +450,13 @@ func GetSnapshotInfo(interxAddr string) *types.SnapShotChecksumResponse {

byteData, err := json.Marshal(success)
if err != nil {
GetLogger().Error("[interx-snapshot_info] Invalid response format", err)
log.CustomLogger().Error("[interx-snapshot_info] Invalid response format", err)
return nil
}

err = json.Unmarshal(byteData, &result)
if err != nil {
GetLogger().Error("[interx-snapshot_info] Invalid response format", err)
log.CustomLogger().Error("[interx-snapshot_info] Invalid response format", err)
return nil
}

Expand All @@ -475,7 +475,7 @@ func GetBlockchain(rpcAddr string) (*tmTypes.ResultBlockchainInfo, error) {
endpoint := fmt.Sprintf("%s/blockchain", rpcAddr)
resp, err := http.Get(endpoint)
if err != nil {
GetLogger().Error("[rpc-call] Unable to connect to ", endpoint)
log.CustomLogger().Error("[rpc-call] Unable to connect to ", endpoint)
return nil, fmt.Errorf("blockchain query error")
}
defer resp.Body.Close()
Expand All @@ -485,18 +485,18 @@ func GetBlockchain(rpcAddr string) (*tmTypes.ResultBlockchainInfo, error) {
response := new(tmJsonRPCTypes.RPCResponse)

if err := json.Unmarshal(respBody, response); err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: ", err)
return nil, err
}

if response.Error != nil {
GetLogger().Error("[rpc-call] Blockchain query fail ")
log.CustomLogger().Error("[rpc-call] Blockchain query fail ")
return nil, fmt.Errorf("blockchain query error")
}

result := new(tmTypes.ResultBlockchainInfo)
if err := tmjson.Unmarshal(response.Result, result); err != nil {
GetLogger().Error("[rpc-call] Unable to decode response: ", err)
log.CustomLogger().Error("[rpc-call] Unable to decode response: ", err)
return nil, err
}

Expand Down
Loading

0 comments on commit 7f952d5

Please sign in to comment.