Skip to content

Commit

Permalink
Update yoda package for supporting network mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongseup committed Jan 10, 2025
1 parent 719d429 commit 67b68b7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 45 deletions.
2 changes: 1 addition & 1 deletion internal/app/exporter/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func selectPackage(
}
return eventnonce.Start(*p)
case pkg == "yoda":
endpoints := common.Endpoints{APIs: validAPIs, CheckAPI: true}
endpoints := common.Endpoints{RPCs: validRPCs, CheckRPC: true, APIs: validAPIs, CheckAPI: true}
p, err := common.NewPackager(m, f, l, mainnet, chainID, chainName, pkg, protocolType, cc, endpoints, monikers...)
if err != nil {
return errors.Wrap(err, common.ErrFailedToBuildPackager)
Expand Down
38 changes: 7 additions & 31 deletions internal/packages/duty/yoda/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,11 @@ import (
"time"

"github.com/cosmostation/cvms/internal/common"
commonapi "github.com/cosmostation/cvms/internal/common/api"
"github.com/cosmostation/cvms/internal/helper"
"github.com/cosmostation/cvms/internal/packages/duty/yoda/types"
)

func GetBlockHeight(
c *common.Exporter,
CommonYodaBlockHeightRequestPath string,
CommonYodaBlockHeightResponseParser func([]byte) (blockHeight int64, err error),
) (int64, error) {
// init context
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()

// create requester
requester := c.APIClient.R().SetContext(ctx)
// get current blockheight
resp, err := requester.Get(CommonYodaBlockHeightRequestPath)
if err != nil {
c.Errorf("api error: %s", err)
return 0, common.ErrFailedHttpRequest
}
if resp.StatusCode() != http.StatusOK {
c.Errorf("api error: [%d] %s", resp.StatusCode(), err)
return 0, common.ErrGotStrangeStatusCode
}

blockHeight, err := CommonYodaBlockHeightResponseParser(resp.Body())
if err != nil {
c.Errorf("api error: %s", err)
return 0, common.ErrFailedJsonUnmarshal
}
return blockHeight, nil
}

func GetYodaStatus(
c *common.Exporter,
CommonYodaQueryPath string,
Expand All @@ -56,7 +27,6 @@ func GetYodaStatus(
CommonYodaRequestCountsParser func([]byte) (requestCount float64, err error),
CommonYodaRequestPath string,
CommonYodaRequestParser func([]byte) (requestBlock int64, validatorsFailedToRespond []string, status string, err error),
currentBlockHeight int64,
) (types.CommonYodaStatus, error) {
// init context
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
Expand Down Expand Up @@ -119,6 +89,12 @@ func GetYodaStatus(
return types.CommonYodaStatus{}, common.ErrFailedJsonUnmarshal
}

currentBlockHeight, _, err := commonapi.GetStatus(c.CommonClient)
if err != nil {
c.Errorf("api error: %s", err)
return types.CommonYodaStatus{}, common.ErrFailedHttpRequest
}

//
// Get non expired requests
//
Expand Down
18 changes: 15 additions & 3 deletions internal/packages/duty/yoda/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ const (

func Start(p common.Packager) error {
if ok := helper.Contains(types.SupportedChains, p.ChainName); ok {
exporter := common.NewExporter(p)
packageMonikers = p.Monikers
for _, rpc := range p.RPCs {
exporter.SetRPCEndPoint(rpc)
break
}
for _, api := range p.APIs {
client := common.NewExporter(p)
client.SetAPIEndPoint(api)
go loop(client, p)
exporter.SetAPIEndPoint(api)
break
}
go loop(exporter, p)
return nil
}
return errors.Errorf("unsupported chain: %s", p.ChainName)
Expand Down Expand Up @@ -195,6 +199,14 @@ func loop(c *common.Exporter, p common.Packager) {
Set(item.MaxMisses)
}

for _, req := range reqsFinished {
YodaValidatorMissSummary.
With(prometheus.Labels{
common.ValidatorAddressLabel: req.Validator.ValidatorOperatorAddress,
common.MonikerLabel: req.Validator.Moniker,
}).Observe(float64(req.Request.BlocksPassed))
}

} else {
// filter metrics for only specific validator
for _, item := range status.Validators {
Expand Down
11 changes: 1 addition & 10 deletions internal/packages/duty/yoda/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func GetStatus(client *common.Exporter, chainName string) (types.CommonYodaStatu
commonYodaRequestCountsParser func(resp []byte) (requestCount float64, err error)
commonYodaRequestPath string
commonYodaRequestParser func(resp []byte) (requestBlock int64, validatorsFailedToRespond []string, status string, err error)
commonYodaLatestBlockPath string
commonYodaLatestBlockParser func(resp []byte) (latestBlock int64, err error)
)

switch chainName {
Expand All @@ -31,13 +29,6 @@ func GetStatus(client *common.Exporter, chainName string) (types.CommonYodaStatu
commonYodaRequestCountsParser = parser.BandYodaRequestCountParser
commonYodaRequestPath = types.BandYodaRequestsPath
commonYodaRequestParser = parser.BandYodaRequestParser
commonYodaLatestBlockPath = types.BandLatestBlockHeightRequestPath
commonYodaLatestBlockParser = parser.BandLatestBlockParser

lastBlock, _ := api.GetBlockHeight(
client,
commonYodaLatestBlockPath,
commonYodaLatestBlockParser)

return api.GetYodaStatus(
client,
Expand All @@ -49,7 +40,7 @@ func GetStatus(client *common.Exporter, chainName string) (types.CommonYodaStatu
commonYodaRequestCountsParser,
commonYodaRequestPath,
commonYodaRequestParser,
lastBlock)
)
default:
return types.CommonYodaStatus{}, common.ErrOutOfSwitchCases
}
Expand Down

0 comments on commit 67b68b7

Please sign in to comment.