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

Add check spec for network list #324

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 61 additions & 10 deletions cmd/check_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
// type checkSpecer interface {
// NetworkList() error
// NetworkOptions() error
// NetworkStatus(ctx context.Context) error
// NetworkStatus(ctx context.Context) error

// AccountBalance() error
// AccountCoins() error
Expand All @@ -59,45 +59,71 @@ var (
// MultipleModes() error
// }
type checkSpec struct {
fetcher *fetcher.Fetcher
onlineFetcher *fetcher.Fetcher
offlineFetcher *fetcher.Fetcher
}

func newCheckSpec(ctx context.Context) (*checkSpec, error) {
fetcherOpts := []fetcher.Option{
onlineFetcherOpts := []fetcher.Option{
fetcher.WithMaxConnections(Config.MaxOnlineConnections),
fetcher.WithRetryElapsedTime(time.Duration(Config.RetryElapsedTime) * time.Second),
fetcher.WithTimeout(time.Duration(Config.HTTPTimeout) * time.Second),
fetcher.WithMaxRetries(Config.MaxRetries),
}

offlineFetcherOpts := []fetcher.Option{
fetcher.WithMaxConnections(Config.Construction.MaxOfflineConnections),
fetcher.WithRetryElapsedTime(time.Duration(Config.RetryElapsedTime) * time.Second),
fetcher.WithTimeout(time.Duration(Config.HTTPTimeout) * time.Second),
fetcher.WithMaxRetries(Config.MaxRetries),
}

if Config.ForceRetry {
fetcherOpts = append(fetcherOpts, fetcher.WithForceRetry())
onlineFetcherOpts = append(onlineFetcherOpts, fetcher.WithForceRetry())
offlineFetcherOpts = append(offlineFetcherOpts, fetcher.WithForceRetry())
}

fetcher := fetcher.New(
onlineFetcher := fetcher.New(
Config.OnlineURL,
fetcherOpts...,
onlineFetcherOpts...,
)
offlineFetcher := fetcher.New(
Config.Construction.OfflineURL,
offlineFetcherOpts...,
)

_, _, fetchErr := fetcher.InitializeAsserter(ctx, Config.Network, Config.ValidationFile)
_, _, fetchErr := onlineFetcher.InitializeAsserter(ctx, Config.Network, Config.ValidationFile)
if fetchErr != nil {
return nil, results.ExitData(
Config,
nil,
nil,
fmt.Errorf("%w: unable to initialize asserter for online node fetcher", fetchErr.Err),
"",
"",
)
}

_, _, fetchErr = offlineFetcher.InitializeAsserter(ctx, Config.Network, Config.ValidationFile)
if fetchErr != nil {
return nil, results.ExitData(
Config,
nil,
nil,
fmt.Errorf("%w: unable to initialize asserter", fetchErr.Err),
fmt.Errorf("%w: unable to initialize asserter for offline node fetcher", fetchErr.Err),
"",
"",
)
}

return &checkSpec{
fetcher: fetcher,
onlineFetcher: onlineFetcher,
offlineFetcher: offlineFetcher,
}, nil
}

func (cs *checkSpec) NetworkStatus(ctx context.Context) error {
res, err := cs.fetcher.NetworkStatusRetry(ctx, Config.Network, nil)
res, err := cs.onlineFetcher.NetworkStatusRetry(ctx, Config.Network, nil)
if err != nil {
return fmt.Errorf("%w: unable to fetch network status", err.Err)
}
Expand All @@ -110,6 +136,23 @@ func (cs *checkSpec) NetworkStatus(ctx context.Context) error {
return nil
}

func (cs *checkSpec) NetworkList(ctx context.Context, fetcher *fetcher.Fetcher) error {
networks, err := fetcher.NetworkList(ctx, nil)
if err != nil {
return fmt.Errorf("%w: unable to fetch network list", err.Err)
}
if len(networks.NetworkIdentifiers) == 0 {
return fmt.Errorf("network_identifiers are required")
}
for _, network := range networks.NetworkIdentifiers {
if network.Network == Config.Network.Network &&
network.Blockchain == Config.Network.Blockchain {
return nil
}
}
return fmt.Errorf("network identifier in configuration file is not returned by /network/list")
}

func runCheckSpecCmd(_ *cobra.Command, _ []string) error {
ctx := context.Background()
cs, err := newCheckSpec(ctx)
Expand All @@ -121,6 +164,14 @@ func runCheckSpecCmd(_ *cobra.Command, _ []string) error {
return fmt.Errorf("%w: network status verification failed", err)
}

if err = cs.NetworkList(ctx, cs.onlineFetcher); err != nil {
return fmt.Errorf("%w: online network list verification failed", err)
}

if err = cs.NetworkList(ctx, cs.offlineFetcher); err != nil {
return fmt.Errorf("%w: offline network list verification failed", err)
}

// TODO: more checks

fmt.Println("Successfully validated check:spec")
Expand Down