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

[Merged by Bors] - Allow any scheme for http poet client and default to http #4116

Closed
wants to merge 12 commits into from
Closed
9 changes: 6 additions & 3 deletions activation/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func WithPoetRetryInterval(interval time.Duration) BuilderOption {
}

// PoETClientInitializer interfaces for creating PoetProvingServiceClient.
type PoETClientInitializer func(string, PoetConfig) PoetProvingServiceClient
type PoETClientInitializer func(string, PoetConfig) (PoetProvingServiceClient, error)

// WithPoETClientInitializer modifies initialization logic for PoET client. Used during client update.
func WithPoETClientInitializer(initializer PoETClientInitializer) BuilderOption {
Expand Down Expand Up @@ -477,14 +477,17 @@ func (b *Builder) UpdatePoETServers(ctx context.Context, endpoints []string) err

clients := make([]PoetProvingServiceClient, 0, len(endpoints))
for _, endpoint := range endpoints {
client := b.poetClientInitializer(endpoint, b.poetCfg)
client, err := b.poetClientInitializer(endpoint, b.poetCfg)
if err != nil {
return &PoetSvcUnstableError{source: fmt.Errorf("failed to initialize poet client for '%s': %w", endpoint, err)}
fasmat marked this conversation as resolved.
Show resolved Hide resolved
}
// TODO(dshulyak) not enough information to verify that PoetServiceID matches with an expected one.
// Maybe it should be provided during update.
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
sid, err := client.PoetServiceID(ctx)
if err != nil {
return &PoetSvcUnstableError{source: fmt.Errorf("failed to query poet '%s' for ID (%w)", endpoint, err)}
return &PoetSvcUnstableError{source: fmt.Errorf("failed to query poet '%s' for ID: %w", endpoint, err)}
}
b.log.WithContext(ctx).With().Debug("preparing to update poet service", log.String("poet_id", hex.EncodeToString(sid)))
clients = append(clients, client)
Expand Down
8 changes: 4 additions & 4 deletions activation/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,10 +1159,10 @@ func TestBuilder_InitialProofGeneratedOnce(t *testing.T) {
func TestBuilder_UpdatePoets(t *testing.T) {
r := require.New(t)

tab := newTestBuilder(t, WithPoETClientInitializer(func(string, PoetConfig) PoetProvingServiceClient {
tab := newTestBuilder(t, WithPoETClientInitializer(func(string, PoetConfig) (PoetProvingServiceClient, error) {
poet := NewMockPoetProvingServiceClient(gomock.NewController(t))
poet.EXPECT().PoetServiceID(gomock.Any()).AnyTimes().Return([]byte("poetid"), nil)
return poet
return poet, nil
}))

r.Nil(tab.Builder.receivePendingPoetClients())
Expand All @@ -1179,10 +1179,10 @@ func TestBuilder_UpdatePoets(t *testing.T) {
func TestBuilder_UpdatePoetsUnstable(t *testing.T) {
r := require.New(t)

tab := newTestBuilder(t, WithPoETClientInitializer(func(string, PoetConfig) PoetProvingServiceClient {
tab := newTestBuilder(t, WithPoETClientInitializer(func(string, PoetConfig) (PoetProvingServiceClient, error) {
poet := NewMockPoetProvingServiceClient(gomock.NewController(t))
poet.EXPECT().PoetServiceID(gomock.Any()).AnyTimes().Return([]byte("poetid"), errors.New("ERROR"))
return poet
return poet, nil
}))

err := tab.Builder.UpdatePoETServers(context.Background(), []string{"poet0", "poet1"})
Expand Down
5 changes: 3 additions & 2 deletions activation/nipost.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type PoetProvingServiceClient interface {
// PoetServiceID returns the public key of the PoET proving service.
PoetServiceID(context.Context) (types.PoetServiceID, error)

GetProof(ctx context.Context, roundID string) (*types.PoetProofMessage, error)
// Proof returns the proof for the given round ID.
Proof(ctx context.Context, roundID string) (*types.PoetProofMessage, error)
}

func (nb *NIPostBuilder) load(challenge types.Hash32) {
Expand Down Expand Up @@ -320,7 +321,7 @@ func (nb *NIPostBuilder) getBestProof(ctx context.Context, challenge *types.Hash
case <-time.After(waitTime):
}

proof, err := client.GetProof(ctx, round)
proof, err := client.Proof(ctx, round)
switch {
case errors.Is(err, context.Canceled):
return fmt.Errorf("querying proof: %w", ctx.Err())
Expand Down
28 changes: 14 additions & 14 deletions activation/nipost_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions activation/nipost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestNIPostBuilderWithMocks(t *testing.T) {

postProvider := &postSetupProviderMock{}
poetProvider := defaultPoetServiceMock(t, []byte("poet"))
poetProvider.EXPECT().GetProof(gomock.Any(), "").Return(&types.PoetProofMessage{
poetProvider.EXPECT().Proof(gomock.Any(), "").Return(&types.PoetProofMessage{
PoetProof: types.PoetProof{Members: [][]byte{hash.Bytes()}},
}, nil)

Expand Down Expand Up @@ -162,7 +162,7 @@ func TestPostSetup(t *testing.T) {
hash := challenge.Hash()

poetProvider := defaultPoetServiceMock(t, []byte("poet"))
poetProvider.EXPECT().GetProof(gomock.Any(), "").Return(&types.PoetProofMessage{
poetProvider.EXPECT().Proof(gomock.Any(), "").Return(&types.PoetProofMessage{
PoetProof: types.PoetProof{Members: [][]byte{hash.Bytes()}},
}, nil)

Expand Down Expand Up @@ -349,7 +349,7 @@ func TestNIPostBuilder_BuildNIPost(t *testing.T) {
challenge2Hash := challenge2.Hash()

poetProver := defaultPoetServiceMock(t, []byte("poet"))
poetProver.EXPECT().GetProof(gomock.Any(), "").AnyTimes().Return(&types.PoetProofMessage{
poetProver.EXPECT().Proof(gomock.Any(), "").AnyTimes().Return(&types.PoetProofMessage{
PoetProof: types.PoetProof{Members: [][]byte{challengeHash.Bytes(), challenge2Hash.Bytes()}},
}, nil)

Expand Down Expand Up @@ -439,7 +439,7 @@ func TestNIPostBuilder_ManyPoETs_SubmittingChallenge_DeadlineReached(t *testing.
ChallengeHash: challengeHash,
}, nil
})
poet.EXPECT().GetProof(gomock.Any(), gomock.Any()).Return(&types.PoetProofMessage{
poet.EXPECT().Proof(gomock.Any(), gomock.Any()).Return(&types.PoetProofMessage{
PoetProof: types.PoetProof{Members: [][]byte{challengeHash.Bytes()}},
}, nil)
poets = append(poets, poet)
Expand Down Expand Up @@ -479,12 +479,12 @@ func TestNIPostBuilder_ManyPoETs_WaitingForProof_DeadlineReached(t *testing.T) {
poets := make([]PoetProvingServiceClient, 0, 2)
{
poet := defaultPoetServiceMock(t, []byte("poet0"))
poet.EXPECT().GetProof(gomock.Any(), gomock.Any()).Return(&proof, nil)
poet.EXPECT().Proof(gomock.Any(), gomock.Any()).Return(&proof, nil)
poets = append(poets, poet)
}
{
poet := defaultPoetServiceMock(t, []byte("poet1"))
poet.EXPECT().GetProof(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, _ string) (*types.PoetProofMessage, error) {
poet.EXPECT().Proof(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, _ string) (*types.PoetProofMessage, error) {
// Hang up after the context expired
<-ctx.Done()
return nil, ctx.Err()
Expand Down Expand Up @@ -534,12 +534,12 @@ func TestNIPostBuilder_ManyPoETs_AllFinished(t *testing.T) {
poets := make([]PoetProvingServiceClient, 0, 2)
{
poet := defaultPoetServiceMock(t, []byte("poet0"))
poet.EXPECT().GetProof(gomock.Any(), "").Return(&proofWorse, nil)
poet.EXPECT().Proof(gomock.Any(), "").Return(&proofWorse, nil)
poets = append(poets, poet)
}
{
poet := defaultPoetServiceMock(t, []byte("poet1"))
poet.EXPECT().GetProof(gomock.Any(), "").Return(&proofBetter, nil)
poet.EXPECT().Proof(gomock.Any(), "").Return(&proofBetter, nil)
poets = append(poets, poet)
}

Expand Down Expand Up @@ -656,7 +656,7 @@ func TestNIPSTBuilder_PoetUnstable(t *testing.T) {
poetDb := NewMockpoetDbAPI(gomock.NewController(t))
mclock := defaultLayerClockMock(t)
poetProver := defaultPoetServiceMock(t, []byte("poet"))
poetProver.EXPECT().GetProof(gomock.Any(), "").Return(nil, errors.New("failed"))
poetProver.EXPECT().Proof(gomock.Any(), "").Return(nil, errors.New("failed"))

nb := NewNIPostBuilder(nodeID, postProver, []PoetProvingServiceClient{poetProver},
poetDb, sql.InMemory(), logtest.New(t), sig, poetCfg, mclock)
Expand All @@ -670,7 +670,7 @@ func TestNIPSTBuilder_PoetUnstable(t *testing.T) {
poetDb.EXPECT().ValidateAndStore(gomock.Any(), gomock.Any()).Return(nil)
mclock := defaultLayerClockMock(t)
poetProver := defaultPoetServiceMock(t, []byte("poet"))
poetProver.EXPECT().GetProof(gomock.Any(), "").Return(&types.PoetProofMessage{PoetProof: types.PoetProof{}}, nil)
poetProver.EXPECT().Proof(gomock.Any(), "").Return(&types.PoetProofMessage{PoetProof: types.PoetProof{}}, nil)

nb := NewNIPostBuilder(nodeID, postProver, []PoetProvingServiceClient{poetProver},
poetDb, sql.InMemory(), logtest.New(t), sig, poetCfg, mclock)
Expand Down
Loading