diff --git a/client/http/http.go b/client/http/http.go index d3cbd19..2970466 100644 --- a/client/http/http.go +++ b/client/http/http.go @@ -59,7 +59,7 @@ func New(ctx context.Context, l log.Logger, url string, chainHash []byte, transp chainInfo, err := c.FetchChainInfo(ctx, chainHash) if err != nil { - return nil, err + return nil, fmt.Errorf("FetchChainInfo err: %w", err) } c.chainInfo = chainInfo @@ -243,7 +243,7 @@ func (h *httpClient) FetchChainInfo(ctx context.Context, chainHash []byte) (*cha chainInfo, err := chain2.InfoFromJSON(infoBody.Body) if err != nil { - resC <- httpInfoResponse{nil, fmt.Errorf("decoding response [chain2.InfoFromJSON]: %w", err)} + resC <- httpInfoResponse{nil, fmt.Errorf("decoding response [InfoFromJSON]: %w", err)} return } diff --git a/client/test/http/mock/httpserver.go b/client/test/http/mock/httpserver.go index c2618b7..dd00c65 100644 --- a/client/test/http/mock/httpserver.go +++ b/client/test/http/mock/httpserver.go @@ -40,21 +40,26 @@ func NewMockHTTPPublicServer(t *testing.T, badSecondRound bool, sch *crypto.Sche for i := 0; i < 3; i++ { protoInfo, err := server.ChainInfo(ctx, &drand.ChainInfoRequest{}) if err != nil { + t.Error("MockServer.ChainInfo error:", err) time.Sleep(10 * time.Millisecond) continue } chainInfo, err = chain.InfoFromProto(protoInfo) if err != nil { + t.Error("MockServer.InfoFromProto error:", err) time.Sleep(10 * time.Millisecond) continue } + break } if chainInfo == nil { t.Fatal("could not use server after 3 attempts.") } - handler.RegisterNewBeaconHandler(client, chainInfo.HashString()) + t.Log("MockServer.ChainInfo:", chainInfo) + + handler.RegisterDefaultBeaconHandler(handler.RegisterNewBeaconHandler(client, chainInfo.HashString())) listener, err := net.Listen("tcp", ":0") if err != nil { diff --git a/internal/lib/cli.go b/internal/lib/cli.go index 5af2569..059b182 100644 --- a/internal/lib/cli.go +++ b/internal/lib/cli.go @@ -132,6 +132,7 @@ func Create(c *cli.Context, withInstrumentation bool, opts ...pubClient.Option) var err error var hash []byte if groupPath := c.Path(GroupConfFlag.Name); groupPath != "" { + l.Debugw("parsing group-conf file") info, err = chainInfoFromGroupTOML(groupPath) if err != nil { l.Infow("Got a group conf file that is not a toml file. Trying it as a ChainInfo json file.", "path", groupPath) @@ -140,6 +141,8 @@ func Create(c *cli.Context, withInstrumentation bool, opts ...pubClient.Option) return nil, fmt.Errorf("failed to decode group (%s) : %w", groupPath, err) } } + l.Debugw("parsing group-conf file, successful") + opts = append(opts, pubClient.WithChainInfo(info)) } @@ -154,6 +157,7 @@ func Create(c *cli.Context, withInstrumentation bool, opts ...pubClient.Option) if len(grc) > 0 { clients = append(clients, grc...) } + l.Debugw("built GRPC Client", "successful", len(grc)) if c.String(HashFlag.Name) != "" { hash, err = hex.DecodeString(c.String(HashFlag.Name)) @@ -183,6 +187,8 @@ func Create(c *cli.Context, withInstrumentation bool, opts ...pubClient.Option) if len(gc) > 0 { clients = append(clients, gc...) } + l.Debugw("built HTTP Client", "successful", len(gc)) + if info != nil && hash != nil && !bytes.Equal(hash, info.Hash()) { return nil, fmt.Errorf( "%w for beacon %s : expected %v != info %v", @@ -244,9 +250,17 @@ func buildHTTPClients(c *cli.Context, l log.Logger, hash []byte, withInstrumenta var hc client.Client var info *chainCommon.Info - l.Infow("Building HTTP clients", "hash", len(hash), "urls", c.StringSlice(URLFlag.Name)) + urls := c.StringSlice(URLFlag.Name) + + l.Infow("Building HTTP clients", "hash", len(hash), "urls", len(urls)) + + // we return an empty list if no URLs were provided + if len(urls) == 0 { + return clients, nil, nil + } - for _, url := range c.StringSlice(URLFlag.Name) { + for _, url := range urls { + l.Debugw("trying to instantiate http client", "url", url) hc, err = http2.New(ctx, l, url, hash, nhttp.DefaultTransport) if err != nil { l.Warnw("", "client", "failed to load URL", "url", url, "err", err) @@ -262,6 +276,7 @@ func buildHTTPClients(c *cli.Context, l log.Logger, hash []byte, withInstrumenta clients = append(clients, hc) } + // do we want to error out or not if all provided URL failed to instantiate a client? if len(skipped) == len(c.StringSlice(URLFlag.Name)) { return nil, nil, errors.New("all URLs failed to be used for creating a http client") } @@ -272,10 +287,12 @@ func buildHTTPClients(c *cli.Context, l log.Logger, hash []byte, withInstrumenta return nil, nil, errors.New("mismatch between retrieved chain info and provided hash") } + // we re-try dialing the skipped remotes, just in case, but that's the last time, we won't be dialing these again + // later in case they fail. for _, url := range skipped { hc, err = http2.NewWithInfo(l, url, info, nhttp.DefaultTransport) if err != nil { - l.Warnw("", "client", "failed to load URL", "url", url, "err", err) + l.Warnw("", "client", "failed to load URL again", "url", url, "err", err) continue } clients = append(clients, hc) diff --git a/internal/lib/cli_test.go b/internal/lib/cli_test.go index e2bfdbe..9515cdd 100644 --- a/internal/lib/cli_test.go +++ b/internal/lib/cli_test.go @@ -2,7 +2,6 @@ package lib import ( "bytes" - "context" "encoding/hex" "errors" "os" @@ -11,8 +10,6 @@ import ( "testing" "time" - "github.com/drand/drand/v2/test/mock" - clock "github.com/jonboulle/clockwork" "github.com/stretchr/testify/require" "github.com/urfave/cli/v2" @@ -64,11 +61,7 @@ func TestClientLib(t *testing.T) { addr, info, cancel, _ := httpmock.NewMockHTTPPublicServer(t, false, sch, clk) defer cancel() - time.Sleep(time.Second) - - grpcLis, _ := mock.NewMockGRPCPublicServer(t, lg, ":0", false, sch, clk) - go grpcLis.Start() - defer grpcLis.Stop(context.Background()) + t.Log("Started mockserver at", addr) args := []string{"mock-client", "--url", "http://" + addr, "--insecure"} err = run(lg, args)