Skip to content

Commit

Permalink
feat: enable consumer chains basic support
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Oct 30, 2023
1 parent 923e15f commit f181e2a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cmd/tmtop.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func main() {
}

rootCmd.PersistentFlags().StringVar(&config.RPCHost, "rpc-host", "http://localhost:26657", "RPC host URL")
rootCmd.PersistentFlags().StringVar(&config.ProviderRPCHost, "provider-rpc-host", "", "Provider chain RPC host URL")
rootCmd.PersistentFlags().DurationVar(&config.RefreshRate, "refresh-rate", time.Second, "Refresh rate")
rootCmd.PersistentFlags().BoolVar(&config.QueryValidators, "query-validators", false, "Whether to query validators from cosmos-sdk")
rootCmd.PersistentFlags().BoolVar(&config.QueryValidators, "query-validators", true, "Whether to query validators from cosmos-sdk")
rootCmd.PersistentFlags().DurationVar(&config.ValidatorsRefreshRate, "validators-refresh-rate", time.Minute, "Validators refresh rate")
rootCmd.PersistentFlags().DurationVar(&config.ChainInfoRefreshRate, "chain-info-refresh-rate", 5*time.Minute, "Chain info refresh rate")

Expand Down
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ import "time"

type Config struct {
RPCHost string
ProviderRPCHost string
RefreshRate time.Duration
ValidatorsRefreshRate time.Duration
ChainInfoRefreshRate time.Duration
QueryValidators bool
}

func (c Config) GetProviderOrConsumerHost() string {
if c.ProviderRPCHost != "" {
return c.ProviderRPCHost
}

return c.RPCHost
}
16 changes: 9 additions & 7 deletions pkg/fetcher/cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (f *CosmosDataFetcher) AbciQuery(
method string,
message codec.ProtoMarshaler,
output codec.ProtoMarshaler,
host string,
) error {
dataBytes, err := message.Marshal()
if err != nil {
Expand All @@ -57,7 +58,7 @@ func (f *CosmosDataFetcher) AbciQuery(
)

var response types.AbciQueryResponse
if err := f.Get(queryURL, &response); err != nil {
if err := f.Get(queryURL, &response, host); err != nil {
return err
}

Expand All @@ -84,6 +85,7 @@ func (f *CosmosDataFetcher) GetValidators() (*types.ChainValidators, error) {
"/cosmos.staking.v1beta1.Query/Validators",
&query,
&validatorsResponse,
f.Config.GetProviderOrConsumerHost(),
); err != nil {
return nil, err
}
Expand All @@ -109,29 +111,29 @@ func (f *CosmosDataFetcher) GetValidators() (*types.ChainValidators, error) {
return &validators, nil
}

func (f *CosmosDataFetcher) Get(relativeURL string, target interface{}) error {
func (f *CosmosDataFetcher) Get(relativeURL string, target interface{}, host string) error {
client := &http.Client{Timeout: 300 * time.Second}
start := time.Now()

url := fmt.Sprintf("%s%s", f.Config.RPCHost, relativeURL)
fullURL := fmt.Sprintf("%s%s", host, relativeURL)

req, err := http.NewRequest(http.MethodGet, url, nil)
req, err := http.NewRequest(http.MethodGet, fullURL, nil)
if err != nil {
return err
}

req.Header.Set("User-Agent", "tmtop")

f.Logger.Debug().Str("url", url).Msg("Doing a query...")
f.Logger.Debug().Str("url", fullURL).Msg("Doing a query...")

res, err := client.Do(req)
if err != nil {
f.Logger.Warn().Str("url", url).Err(err).Msg("Query failed")
f.Logger.Warn().Str("url", fullURL).Err(err).Msg("Query failed")
return err
}
defer res.Body.Close()

f.Logger.Debug().Str("url", url).Dur("duration", time.Since(start)).Msg("Query is finished")
f.Logger.Debug().Str("url", fullURL).Dur("duration", time.Since(start)).Msg("Query is finished")

return json.NewDecoder(res.Body).Decode(target)
}

0 comments on commit f181e2a

Please sign in to comment.