From 566d9358b75ff7669aebd3def415574b59ee2718 Mon Sep 17 00:00:00 2001 From: Jennifer Echenim Date: Sat, 30 Dec 2023 00:33:29 +0400 Subject: [PATCH 1/2] feat: endpoint to fetch testnet health status --- tools/walletextension/api/routes.go | 28 +++++++++++++++++++++++ tools/walletextension/common/constants.go | 1 + tools/walletextension/wallet_extension.go | 6 +++++ 3 files changed, 35 insertions(+) diff --git a/tools/walletextension/api/routes.go b/tools/walletextension/api/routes.go index 2489511b12..8809f663aa 100644 --- a/tools/walletextension/api/routes.go +++ b/tools/walletextension/api/routes.go @@ -63,6 +63,10 @@ func NewHTTPRoutes(walletExt *walletextension.WalletExtension) []Route { Name: common.PathHealth, Func: httpHandler(walletExt, healthRequestHandler), }, + { + Name: common.PathNetworkHealth, + Func: httpHandler(walletExt, networkHealthRequestHandler), + }, { Name: common.PathVersion, Func: httpHandler(walletExt, versionRequestHandler), @@ -422,12 +426,36 @@ func healthRequestHandler(walletExt *walletextension.WalletExtension, conn userc return } + // TODO: connect to database and check if it is healthy err = conn.WriteResponse([]byte(common.SuccessMsg)) if err != nil { walletExt.Logger().Error("error writing success response", log.ErrKey, err) } } +// Handles request to /network-health endpoint. +func networkHealthRequestHandler(walletExt *walletextension.WalletExtension, userConn userconn.UserConn) { + // read the request + _, err := userConn.ReadRequest() + if err != nil { + walletExt.Logger().Error("error reading request", log.ErrKey, err) + return + } + + healthStatus, err := walletExt.GetTenNodeHealthStatus() + + data := map[string]interface{}{ + "result": healthStatus, + "error": err, + } + + err = userConn.WriteResponse([]byte(fmt.Sprintf("%v", data))) + + if err != nil { + walletExt.Logger().Error("error writing success response", log.ErrKey, err) + } +} + // Handles request to /version endpoint. func versionRequestHandler(walletExt *walletextension.WalletExtension, userConn userconn.UserConn) { // read the request diff --git a/tools/walletextension/common/constants.go b/tools/walletextension/common/constants.go index 6cca9175e0..1e35fb7628 100644 --- a/tools/walletextension/common/constants.go +++ b/tools/walletextension/common/constants.go @@ -35,6 +35,7 @@ const ( PathRevoke = "/revoke/" PathObscuroGateway = "/" PathHealth = "/health/" + PathNetworkHealth = "/network-health/" WSProtocol = "ws://" DefaultUser = "defaultUser" UserQueryParameter = "u" diff --git a/tools/walletextension/wallet_extension.go b/tools/walletextension/wallet_extension.go index 55ee12fe98..d166b6030a 100644 --- a/tools/walletextension/wallet_extension.go +++ b/tools/walletextension/wallet_extension.go @@ -11,6 +11,7 @@ import ( "github.com/ten-protocol/go-ten/tools/walletextension/config" "github.com/ten-protocol/go-ten/go/common/log" + "github.com/ten-protocol/go-ten/go/obsclient" "github.com/ten-protocol/go-ten/tools/walletextension/useraccountmanager" @@ -38,6 +39,7 @@ type WalletExtension struct { stopControl *stopcontrol.StopControl version string config *config.Config + obsClient *obsclient.ObsClient } func New( @@ -384,3 +386,7 @@ func (w *WalletExtension) checkParametersForInterceptedGetStorageAt(params []int func (w *WalletExtension) Version() string { return w.version } + +func (w *WalletExtension) GetTenNodeHealthStatus() (bool, error) { + return w.obsClient.Health() +} From 7f5cd3412de3174e8ff9f0ba2f36bcf61788ee9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Kokelj?= Date: Mon, 8 Jan 2024 13:32:10 +0100 Subject: [PATCH 2/2] create rpcClient and tenClinet to call health --- tools/walletextension/wallet_extension.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/walletextension/wallet_extension.go b/tools/walletextension/wallet_extension.go index d166b6030a..4bd539b523 100644 --- a/tools/walletextension/wallet_extension.go +++ b/tools/walletextension/wallet_extension.go @@ -39,7 +39,7 @@ type WalletExtension struct { stopControl *stopcontrol.StopControl version string config *config.Config - obsClient *obsclient.ObsClient + tenClient *obsclient.ObsClient } func New( @@ -51,6 +51,12 @@ func New( logger gethlog.Logger, config *config.Config, ) *WalletExtension { + rpcClient, err := rpc.NewNetworkClient(hostAddr) + if err != nil { + logger.Error(fmt.Errorf("could not create RPC client on %s. Cause: %w", hostAddr, err).Error()) + panic(err) + } + newTenClient := obsclient.NewObsClient(rpcClient) return &WalletExtension{ hostAddr: hostAddr, userAccountManager: userAccountManager, @@ -60,6 +66,7 @@ func New( stopControl: stopControl, version: version, config: config, + tenClient: newTenClient, } } @@ -388,5 +395,5 @@ func (w *WalletExtension) Version() string { } func (w *WalletExtension) GetTenNodeHealthStatus() (bool, error) { - return w.obsClient.Health() + return w.tenClient.Health() }