Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add logging mechanism #239

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
303 changes: 156 additions & 147 deletions README.md
100644 → 100755

Large diffs are not rendered by default.

220 changes: 171 additions & 49 deletions common/api.go
100644 → 100755

Large diffs are not rendered by default.

35 changes: 28 additions & 7 deletions common/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/KiraCore/interx/config"
"github.com/KiraCore/interx/database"
"github.com/KiraCore/interx/log"
)

type BlockHeightTime struct {
Expand All @@ -28,7 +29,9 @@ func GetAverageBlockTime() float64 {
return 0
}

// GetLogger().Infof("[GetAverageBlockTime] %v", LatestNBlockTimes)
log.CustomLogger().Info(" `GetAverageBlockTime` Finished request.",
"LatestNBlockTimes", LatestNBlockTimes,
)

return total / float64(len(LatestNBlockTimes))
}
Expand All @@ -43,9 +46,12 @@ func LoadAllBlocks() {
}

func AddNewBlock(height int64, timestamp int64) {

if len(LatestNBlockTimes) > 0 && LatestNBlockTimes[len(LatestNBlockTimes)-1].Height >= height {
// not a new block
GetLogger().Errorf("[AddNewBlock] not a new block: %d", height)
log.CustomLogger().Error("[AddNewBlock] Failed to fetch new block.",
"height", height,
)
return
}

Expand All @@ -54,15 +60,22 @@ func AddNewBlock(height int64, timestamp int64) {

prevBlockTimestamp, err := GetBlockNanoTime(config.Config.RPC, height-1)
if err != nil {
GetLogger().Errorf("[AddNewBlock] Can't get block: %d", height-1)
log.CustomLogger().Error("[AddNewBlock][GetBlockNanoTime] Failed to fetch a block.",
"height", height-1,
)
return
}

timespan = (float64(timestamp) - float64(prevBlockTimestamp)) / 1e9

if len(LatestNBlockTimes) > 0 && timespan >= GetAverageBlockTime()*float64(config.Config.Block.HaltedAvgBlockTimes) {
// a block just after a halt
GetLogger().Errorf("[AddNewBlock] block just after a halt: %d, timestamp: %f, average: %f", height, timespan, GetAverageBlockTime())
log.CustomLogger().Error("`AddNewBlock` Block added just after a halt.",
"height", height,
"timestamp", timespan,
"average_block_time", GetAverageBlockTime(),
"halted_threshold", GetAverageBlockTime()*float64(config.Config.Block.HaltedAvgBlockTimes),
)
return
}
}
Expand All @@ -76,6 +89,8 @@ func AddNewBlock(height int64, timestamp int64) {
if len(LatestNBlockTimes) > N {
LatestNBlockTimes = LatestNBlockTimes[len(LatestNBlockTimes)-N:]
}

log.CustomLogger().Info("Finished 'AddNewBlock' request.")
}

func UpdateN(_N int) {
Expand All @@ -94,13 +109,17 @@ func UpdateN(_N int) {

currentBlockTimestamp, err := GetBlockNanoTime(config.Config.RPC, current)
if err != nil {
GetLogger().Errorf("[UpdateN] Can't get block: %d", current)
log.CustomLogger().Error("[UpdateN][GetBlockNanoTime] Failed to fetch a block.",
"height", current,
)
return
}

prevBlockTimestamp, err := GetBlockNanoTime(config.Config.RPC, current-1)
if err != nil {
GetLogger().Errorf("[UpdateN] Can't get block: %d", current-1)
log.CustomLogger().Error("[UpdateN][GetBlockNanoTime] Failed to fetch a block.",
"height", current-1,
)
return
}

Expand All @@ -125,7 +144,9 @@ func IsConsensusStopped(validatorCount int) bool {
blockTime, _ := time.Parse(time.RFC3339, NodeStatus.Blocktime)

if blockHeight <= 1 {
GetLogger().Errorf("[UpdateN] block <= 1: %d", blockHeight)
log.CustomLogger().Error("[IsConsensusStopped] Failed to UpdateN block <= 1.",
"height", blockHeight,
)
return false
}

Expand Down
32 changes: 29 additions & 3 deletions common/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ import (

"github.com/KiraCore/interx/config"
"github.com/KiraCore/interx/global"
"github.com/KiraCore/interx/log"
"github.com/KiraCore/interx/types"
)

// PutCache is a function to save value to cache
func PutCache(chainIDHash string, endpointHash string, requestHash string, value types.InterxResponse) error {
// GetLogger().Info("[cache] Saving interx response")

log.CustomLogger().Info("Starting 'PutCache' request...",
"endpoint", endpointHash,
)

data, err := json.Marshal(value)
if err != nil {
log.CustomLogger().Error("[PutCache] Failed to marshal the response.",
"error", err,
)
return err
}

Expand All @@ -25,31 +32,50 @@ func PutCache(chainIDHash string, endpointHash string, requestHash string, value
global.Mutex.Lock()
err = os.MkdirAll(folderPath, os.ModePerm)
if err != nil {

log.CustomLogger().Error("[PutCache] Failed to create a folder.",
"error", err,
"folder_Path", folderPath,
)

global.Mutex.Unlock()

GetLogger().Error("[cache] Unable to create a folder: ", folderPath)
return err
}

err = os.WriteFile(filePath, data, 0644)
global.Mutex.Unlock()

if err != nil {
GetLogger().Error("[cache] Unable to save response: ", filePath)
log.CustomLogger().Error("[PutCache] Failed to write data to the named file.",
"error", err,
"file_Path", filePath,
)
}

log.CustomLogger().Info("Finished 'PutCache' request.")

return err
}

// GetCache is a function to get value from cache
func GetCache(chainIDHash string, endpointHash string, requestHash string) (types.InterxResponse, error) {

log.CustomLogger().Info("Starting 'GetCache' request...",
"endpoint", endpointHash,
)

filePath := fmt.Sprintf("%s/%s/%s/%s", config.GetResponseCacheDir(), chainIDHash, endpointHash, requestHash)

response := types.InterxResponse{}

data, err := os.ReadFile(filePath)

if err != nil {
log.CustomLogger().Error("[GetCache] Failed to read data from the named file.",
"error", err,
"file_Path", filePath,
)
return response, err
}

Expand Down
53 changes: 45 additions & 8 deletions common/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/KiraCore/interx/config"
"github.com/KiraCore/interx/database"
"github.com/KiraCore/interx/log"
"github.com/KiraCore/interx/types"
"github.com/KiraCore/interx/types/rosetta"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
Expand Down Expand Up @@ -161,12 +162,20 @@ func (c conventionalMarshaller) MarshalAndConvert(endpoint string) ([]byte, erro

// GetInterxRequest is a function to get Interx Request
func GetInterxRequest(r *http.Request) types.InterxRequest {

log.CustomLogger().Info("Starting 'GetInterxRequest' request...",
"method", r.Method,
"endpoint", r.URL.String(),
)

request := types.InterxRequest{}

request.Method = r.Method
request.Endpoint = r.URL.String()
request.Params, _ = ioutil.ReadAll(r.Body)

log.CustomLogger().Info("Finished 'GetInterxRequest' request.")

return request
}

Expand All @@ -184,6 +193,9 @@ func GetResponseFormat(request types.InterxRequest, rpcAddr string) *types.Proxy

// GetResponseSignature is a function to get response signature
func GetResponseSignature(response types.ProxyResponse) (string, string) {

log.CustomLogger().Info("Starting 'GetResponseSignature' request...")

// Get Response Hash
responseHash := GetBlake2bHash(response.Response)

Expand All @@ -196,36 +208,54 @@ func GetResponseSignature(response types.ProxyResponse) (string, string) {
sign.Response = responseHash
signBytes, err := json.Marshal(sign)
if err != nil {
log.CustomLogger().Error("[GetResponseSignature] Failed to create signature.",
"error", err,
)
return "", responseHash
}

// Get Signature
signature, err := config.Config.PrivKey.Sign(signBytes)
if err != nil {
log.CustomLogger().Error("[GetResponseSignature] Failed to fetch signature.",
"error", err,
)
return "", responseHash
}

log.CustomLogger().Info("Finished 'GetResponseSignature' request.")

return base64.StdEncoding.EncodeToString([]byte(signature)), responseHash
}

// SearchCache is a function to search response in cache
func SearchCache(request types.InterxRequest, response *types.ProxyResponse) (bool, interface{}, interface{}, int) {

log.CustomLogger().Info("Starting SearchCache")

chainIDHash := GetBlake2bHash(response.Chainid)
endpointHash := GetBlake2bHash(request.Endpoint)
requestHash := GetBlake2bHash(request)

// GetLogger().Info(chainIDHash, endpointHash, requestHash)
log.CustomLogger().Info("`SearchCache` Config Path", "chainIDHash", chainIDHash,
"endpointHash", endpointHash, "requestHash", requestHash,
)

result, err := GetCache(chainIDHash, endpointHash, requestHash)
// GetLogger().Info(result)

if err != nil {
log.CustomLogger().Error("[SearchCache] Failed to run GetCache function.",
"error", err,
)
return false, nil, nil, -1
}

if IsCacheExpired(result) {
return false, nil, nil, -1
}

log.CustomLogger().Info("Finished SearchCache")

return true, result.Response.Response, result.Response.Error, result.Status
}

Expand All @@ -235,7 +265,7 @@ func WrapResponse(w http.ResponseWriter, request types.InterxRequest, response t
statusCode = 503 // Service Unavailable Error
}
if saveToCache {
// GetLogger().Info("[gateway] Saving in the cache")
log.CustomLogger().Info("Starting Wrap Response...")

chainIDHash := GetBlake2bHash(response.Chainid)
endpointHash := GetBlake2bHash(request.Endpoint)
Expand All @@ -249,9 +279,10 @@ func WrapResponse(w http.ResponseWriter, request types.InterxRequest, response t
CachingBlockDuration: conf.CachingBlockDuration,
})
if err != nil {
GetLogger().Error("[gateway] Failed to save in the cache: ", err.Error())
log.CustomLogger().Error("[WrapResponse] Failed to save in the cache.",
"error", err,
)
}
// GetLogger().Info("[gateway] Save finished")
}
}

Expand Down Expand Up @@ -279,15 +310,19 @@ func WrapResponse(w http.ResponseWriter, request types.InterxRequest, response t
case string:
_, err := w.Write([]byte(v))
if err != nil {
GetLogger().Error("[gateway] Failed to make a response", err.Error())
log.CustomLogger().Error("[WrapResponse] Failed to make a response.",
"error", err,
)
}
return
}

encoded, _ := conventionalMarshaller{response.Response}.MarshalAndConvert(request.Endpoint)
_, err := w.Write(encoded)
if err != nil {
GetLogger().Error("[gateway] Failed to make a response", err.Error())
log.CustomLogger().Error("[WrapResponse] Failed to make a response.",
"error", err,
)
}
} else {
w.WriteHeader(statusCode)
Expand All @@ -299,7 +334,9 @@ func WrapResponse(w http.ResponseWriter, request types.InterxRequest, response t
encoded, _ := conventionalMarshaller{response.Error}.MarshalAndConvert(request.Endpoint)
_, err := w.Write(encoded)
if err != nil {
GetLogger().Error("[gateway] Failed to make a response", err.Error())
log.CustomLogger().Error("[WrapResponse] Failed to make a response.",
"error", err,
)
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions common/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"encoding/json"

"github.com/KiraCore/interx/log"
govTypes "github.com/KiraCore/interx/types/kira/gov"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -79,13 +80,17 @@ func QueryVotersFromGrpcResult(success interface{}) ([]govTypes.Voter, error) {

byteData, err := json.Marshal(success)
if err != nil {
GetLogger().Error("[query-voters] Invalid response format: ", err)
log.CustomLogger().Error("[QueryVotersFromGrpcResult] Failed to marshal response.",
"error", err,
)
return nil, err
}

err = json.Unmarshal(byteData, &result)
if err != nil {
GetLogger().Error("[query-voters] Invalid response format: ", err)
log.CustomLogger().Error("[QueryVotersFromGrpcResult] Failed to unmarshal response.",
"error", err,
)
return nil, err
}

Expand Down Expand Up @@ -126,13 +131,17 @@ func QueryVotesFromGrpcResult(success interface{}) ([]govTypes.Vote, error) {

byteData, err := json.Marshal(success)
if err != nil {
GetLogger().Error("[query-votes] Invalid response format: ", err)
log.CustomLogger().Error("[QueryVotesFromGrpcResult] Failed to marshal response.",
"error", err,
)
return nil, err
}

err = json.Unmarshal(byteData, &result)
if err != nil {
GetLogger().Error("[query-votes] Invalid response format: ", err)
log.CustomLogger().Error("[QueryVotesFromGrpcResult] Failed to unmarshal response.",
"error", err,
)
return nil, err
}

Expand All @@ -155,12 +164,16 @@ func QueryNetworkPropertiesFromGrpcResult(success interface{}) (NetworkPropertie
result := NetworkPropertiesResponse{}
byteData, err := json.Marshal(success)
if err != nil {
GetLogger().Error("[query-network-properties] Invalid response format", err)
log.CustomLogger().Error("[QueryNetworkPropertiesFromGrpcResult] Failed to marshal response.",
"error", err,
)
return NetworkPropertiesResponse{}, err
}
err = json.Unmarshal(byteData, &result)
if err != nil {
GetLogger().Error("[query-network-properties] Invalid response format", err)
log.CustomLogger().Error("[QueryNetworkPropertiesFromGrpcResult] Failed to unmarshal response.",
"error", err,
)
return NetworkPropertiesResponse{}, err
}

Expand Down
Loading
Loading