Skip to content

Commit

Permalink
feat: add http and grpc API call logs (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
bap2pecs authored Nov 26, 2024
1 parent 1de35b8 commit 4ffe20d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
36 changes: 36 additions & 0 deletions server/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package server

import (
"context"
"fmt"

"github.com/babylonlabs-io/finality-gadget/proto"
"github.com/babylonlabs-io/finality-gadget/types"
"go.uber.org/zap"
)

// QueryIsBlockBabylonFinalized is an RPC method that returns the finality status of a block by querying the internal db.
func (s *Server) QueryIsBlockBabylonFinalized(ctx context.Context, req *proto.QueryIsBlockBabylonFinalizedRequest) (*proto.QueryIsBlockFinalizedResponse, error) {
s.logger.Debug(
"QueryIsBlockBabylonFinalized request",
zap.String("blockHash", req.Block.BlockHash),
zap.Uint64("blockHeight", req.Block.BlockHeight),
zap.Uint64("blockTimestamp", req.Block.BlockTimestamp),
)
isFinalized, err := s.fg.QueryIsBlockBabylonFinalized(&types.Block{
BlockHash: req.Block.BlockHash,
BlockHeight: req.Block.BlockHeight,
Expand All @@ -23,6 +31,12 @@ func (s *Server) QueryIsBlockBabylonFinalized(ctx context.Context, req *proto.Qu

// QueryIsBlockBabylonFinalizedFromBabylon is an RPC method that returns the finality status of a block by querying Babylon chain.
func (s *Server) QueryIsBlockBabylonFinalizedFromBabylon(ctx context.Context, req *proto.QueryIsBlockBabylonFinalizedRequest) (*proto.QueryIsBlockFinalizedResponse, error) {
s.logger.Debug(
"QueryIsBlockBabylonFinalizedFromBabylon request",
zap.String("blockHash", req.Block.BlockHash),
zap.Uint64("blockHeight", req.Block.BlockHeight),
zap.Uint64("blockTimestamp", req.Block.BlockTimestamp),
)
isFinalized, err := s.fg.QueryIsBlockBabylonFinalizedFromBabylon(&types.Block{
BlockHash: req.Block.BlockHash,
BlockHeight: req.Block.BlockHeight,
Expand All @@ -37,6 +51,18 @@ func (s *Server) QueryIsBlockBabylonFinalizedFromBabylon(ctx context.Context, re

// QueryBlockRangeBabylonFinalized is an RPC method that returns the latest Babylon finalized block in a range by querying Babylon chain.
func (s *Server) QueryBlockRangeBabylonFinalized(ctx context.Context, req *proto.QueryBlockRangeBabylonFinalizedRequest) (*proto.QueryBlockRangeBabylonFinalizedResponse, error) {
if len(req.Blocks) == 0 {
s.logger.Error("blocks array is empty")
return nil, fmt.Errorf("blocks array is empty")
}

s.logger.Debug(
"QueryBlockRangeBabylonFinalized request",
zap.Uint64("fromBlockHeight", req.Blocks[0].BlockHeight),
zap.String("fromBlockHash", req.Blocks[0].BlockHash),
zap.Uint64("toBlockHeight", req.Blocks[len(req.Blocks)-1].BlockHeight),
zap.String("toBlockHash", req.Blocks[len(req.Blocks)-1].BlockHash),
)
blocks := make([]*types.Block, 0, len(req.Blocks))

for _, block := range req.Blocks {
Expand All @@ -63,6 +89,7 @@ func (s *Server) QueryBlockRangeBabylonFinalized(ctx context.Context, req *proto

// QueryBtcStakingActivatedTimestamp is an RPC method that returns the timestamp when BTC staking was activated.
func (s *Server) QueryBtcStakingActivatedTimestamp(ctx context.Context, req *proto.QueryBtcStakingActivatedTimestampRequest) (*proto.QueryBtcStakingActivatedTimestampResponse, error) {
s.logger.Debug("QueryBtcStakingActivatedTimestamp request")
timestamp, err := s.fg.QueryBtcStakingActivatedTimestamp()
if err != nil {
return nil, err
Expand All @@ -73,6 +100,10 @@ func (s *Server) QueryBtcStakingActivatedTimestamp(ctx context.Context, req *pro

// QueryIsBlockFinalizedByHeight is an RPC method that returns the status of a block at a given height.
func (s *Server) QueryIsBlockFinalizedByHeight(ctx context.Context, req *proto.QueryIsBlockFinalizedByHeightRequest) (*proto.QueryIsBlockFinalizedResponse, error) {
s.logger.Debug(
"QueryIsBlockFinalizedByHeight request",
zap.Uint64("blockHeight", req.BlockHeight),
)
isFinalized, err := s.fg.QueryIsBlockFinalizedByHeight(req.BlockHeight)

if err != nil {
Expand All @@ -84,6 +115,10 @@ func (s *Server) QueryIsBlockFinalizedByHeight(ctx context.Context, req *proto.Q

// QueryIsBlockFinalizedByHeight is an RPC method that returns the status of a block at a given height.
func (s *Server) QueryIsBlockFinalizedByHash(ctx context.Context, req *proto.QueryIsBlockFinalizedByHashRequest) (*proto.QueryIsBlockFinalizedResponse, error) {
s.logger.Debug(
"QueryIsBlockFinalizedByHash request",
zap.String("blockHash", req.BlockHash),
)
isFinalized, err := s.fg.QueryIsBlockFinalizedByHash(req.BlockHash)

if err != nil {
Expand All @@ -95,6 +130,7 @@ func (s *Server) QueryIsBlockFinalizedByHash(ctx context.Context, req *proto.Que

// QueryLatestFinalizedBlock is an RPC method that returns the latest consecutively finalized block.
func (s *Server) QueryLatestFinalizedBlock(ctx context.Context, req *proto.QueryLatestFinalizedBlockRequest) (*proto.QueryBlockResponse, error) {
s.logger.Debug("QueryLatestFinalizedBlock request")
block, err := s.fg.QueryLatestFinalizedBlock()

if block == nil {
Expand Down
15 changes: 14 additions & 1 deletion server/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ func (s *Server) newHttpHandler() http.Handler {
func (s *Server) txStatusHandler(w http.ResponseWriter, r *http.Request) {
// Extract query parameters
txHash := r.URL.Query().Get("hash")
s.logger.Debug("Received transaction hash", zap.String("txHash", txHash))
s.logger.Debug("transaction status request",
zap.String("path", "/v1/transaction"),
zap.String("method", r.Method),
zap.String("txHash", txHash),
zap.String("remoteAddr", r.RemoteAddr),
)

// Get block from rpc.
txInfo, err := s.fg.QueryTransactionStatus(txHash)
Expand All @@ -42,6 +47,10 @@ func (s *Server) txStatusHandler(w http.ResponseWriter, r *http.Request) {
}

func (s *Server) chainSyncStatusHandler(w http.ResponseWriter, r *http.Request) {
s.logger.Debug(
"chainSyncStatus request",
zap.String("path", "/v1/chainSyncStatus"),
)
// Get block from rpc.
chainSyncStatus, err := s.fg.QueryChainSyncStatus()
if err != nil {
Expand All @@ -64,6 +73,10 @@ func (s *Server) chainSyncStatusHandler(w http.ResponseWriter, r *http.Request)
}

func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
s.logger.Debug(
"health request",
zap.String("path", "/health"),
)
response := "Finality gadget is healthy"
_, err := w.Write([]byte(response))
if err != nil {
Expand Down

0 comments on commit 4ffe20d

Please sign in to comment.