From 7c1cd15302857c22356ac638ba65dc840fbe9cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Wed, 8 Nov 2023 19:36:31 +0700 Subject: [PATCH] Use the right post challenge to certify --- activation/activation.go | 16 ++++++++-------- activation/activation_test.go | 24 +++++++++++++++--------- activation/certifier.go | 7 ++++--- activation/e2e/certifier_client_test.go | 2 +- activation/e2e/nipost_test.go | 2 +- activation/e2e/validation_test.go | 2 +- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/activation/activation.go b/activation/activation.go index 0d8ccd3fa7..fc3a861969 100644 --- a/activation/activation.go +++ b/activation/activation.go @@ -323,17 +323,17 @@ func (b *Builder) generateInitialPost(ctx context.Context) error { // // New nodes should call it after the initial POST is created. func (b *Builder) certifyPost(ctx context.Context) { - post, meta, err := b.obtainPostForCertification() + post, meta, ch, err := b.obtainPostForCertification() if err != nil { b.log.With().Error("failed to obtain post for certification", log.Err(err)) } - client := NewCertifierClient(b.log.Zap(), post, meta) + client := NewCertifierClient(b.log.Zap(), post, meta, ch) b.certifier = NewCertifier(b.nipostBuilder.DataDir(), b.log.Zap(), client) b.certifier.CertifyAll(ctx, b.poets) } -func (b *Builder) obtainPostForCertification() (*types.Post, *types.PostInfo, error) { +func (b *Builder) obtainPostForCertification() (*types.Post, *types.PostInfo, []byte, error) { var ( post *types.Post meta *types.PostInfo @@ -341,23 +341,23 @@ func (b *Builder) obtainPostForCertification() (*types.Post, *types.PostInfo, er if b.initialPost != nil { b.log.Info("certifying using the initial post") - return b.initialPost, b.initialPostInfo, nil + return b.initialPost, b.initialPostInfo, shared.ZeroChallenge, nil } b.log.Info("certifying using an existing ATX") atxid, err := atxs.GetFirstIDByNodeID(b.cdb, b.SmesherID()) if err != nil { - return nil, nil, fmt.Errorf("cannot certify - no existing ATX found: %w", err) + return nil, nil, nil, fmt.Errorf("cannot certify - no existing ATX found: %w", err) } atx, err := b.cdb.GetFullAtx(atxid) if err != nil { - return nil, nil, fmt.Errorf("cannot certify - failed to retrieve ATX: %w", err) + return nil, nil, nil, fmt.Errorf("cannot certify - failed to retrieve ATX: %w", err) } var commitmentAtx *types.ATXID if commitmentAtx = atx.CommitmentATX; commitmentAtx == nil { atx, err := atxs.CommitmentATX(b.cdb, b.SmesherID()) if err != nil { - return nil, nil, fmt.Errorf("cannot determine own commitment ATX: %w", err) + return nil, nil, nil, fmt.Errorf("cannot determine own commitment ATX: %w", err) } commitmentAtx = &atx } @@ -370,7 +370,7 @@ func (b *Builder) obtainPostForCertification() (*types.Post, *types.PostInfo, er LabelsPerUnit: atx.NIPost.PostMetadata.LabelsPerUnit, } - return post, meta, nil + return post, meta, atx.NIPost.PostMetadata.Challenge, nil } func (b *Builder) run(ctx context.Context) { diff --git a/activation/activation_test.go b/activation/activation_test.go index ff8ccdb6f5..e1fa00d9ed 100644 --- a/activation/activation_test.go +++ b/activation/activation_test.go @@ -1140,7 +1140,7 @@ func TestBuilder_InitialProofGeneratedOnce(t *testing.T) { func TestBuilder_ObtainPostForCertification(t *testing.T) { t.Run("no POST or ATX - fail", func(t *testing.T) { tab := newTestBuilder(t) - _, _, err := tab.obtainPostForCertification() + _, _, _, err := tab.obtainPostForCertification() require.Error(t, err) }) t.Run("initial POST available", func(t *testing.T) { @@ -1148,8 +1148,9 @@ func TestBuilder_ObtainPostForCertification(t *testing.T) { tab.mpostClient.EXPECT().Proof(gomock.Any(), shared.ZeroChallenge).Return(&types.Post{}, &types.PostInfo{}, nil) require.NoError(t, tab.generateInitialPost(context.Background())) - _, _, err := tab.obtainPostForCertification() + _, _, ch, err := tab.obtainPostForCertification() require.NoError(t, err) + require.EqualValues(t, shared.ZeroChallenge, ch) }) t.Run("initial POST unavailable but ATX exists", func(t *testing.T) { tab := newTestBuilder(t) @@ -1162,13 +1163,17 @@ func TestBuilder_ObtainPostForCertification(t *testing.T) { CommitmentATX: &commitmentAtxId, InitialPost: &types.Post{}, } - nipost := newNIPostWithChallenge(t, types.HexToHash32("55555"), []byte("66666")) - nipost.Post = &types.Post{ - Nonce: 5, - Indices: []byte{1, 2, 3, 4}, - Pow: 7, + nipost := &types.NIPost{ + Post: &types.Post{ + Nonce: 5, + Indices: []byte{1, 2, 3, 4}, + Pow: 7, + }, + PostMetadata: &types.PostMetadata{ + Challenge: []byte("66666"), + LabelsPerUnit: 777, + }, } - nipost.PostMetadata.LabelsPerUnit = 777 atx := types.NewActivationTx(challenge, types.Address{}, nipost, 2, nil) atx.SetEffectiveNumUnits(2) atx.SetReceived(time.Now()) @@ -1177,12 +1182,13 @@ func TestBuilder_ObtainPostForCertification(t *testing.T) { require.NoError(t, err) require.NoError(t, atxs.Add(tab.cdb, vAtx)) - post, meta, err := tab.obtainPostForCertification() + post, meta, ch, err := tab.obtainPostForCertification() require.NoError(t, err) require.Equal(t, commitmentAtxId, meta.CommitmentATX) require.Equal(t, uint32(2), meta.NumUnits) require.Equal(t, tab.nodeID, meta.NodeID) require.Equal(t, uint64(777), meta.LabelsPerUnit) + require.Equal(t, nipost.PostMetadata.Challenge, ch) require.Equal(t, nipost.Post, post) }) } diff --git a/activation/certifier.go b/activation/certifier.go index 1014c9f1f8..a8a87a9a89 100644 --- a/activation/certifier.go +++ b/activation/certifier.go @@ -17,7 +17,6 @@ import ( "github.com/hashicorp/go-retryablehttp" "github.com/natefinch/atomic" "github.com/sourcegraph/conc/iter" - "github.com/spacemeshos/post/shared" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -178,15 +177,17 @@ type CertifierClient struct { client *retryablehttp.Client post *types.Post postInfo *types.PostInfo + postCh []byte logger *zap.Logger } -func NewCertifierClient(logger *zap.Logger, post *types.Post, postInfo *types.PostInfo) *CertifierClient { +func NewCertifierClient(logger *zap.Logger, post *types.Post, postInfo *types.PostInfo, postCh []byte) *CertifierClient { c := &CertifierClient{ client: retryablehttp.NewClient(), logger: logger, post: post, postInfo: postInfo, + postCh: postCh, } c.client.Logger = &retryableHttpLogger{logger} c.client.ResponseLogHook = func(logger retryablehttp.Logger, resp *http.Response) { @@ -208,7 +209,7 @@ func (c *CertifierClient) Certify(ctx context.Context, url *url.URL, pubkey []by CommitmentAtxId: c.postInfo.CommitmentATX[:], LabelsPerUnit: c.postInfo.LabelsPerUnit, NumUnits: c.postInfo.NumUnits, - Challenge: shared.ZeroChallenge, + Challenge: c.postCh, }, } diff --git a/activation/e2e/certifier_client_test.go b/activation/e2e/certifier_client_test.go index ca3872b01f..284d896c12 100644 --- a/activation/e2e/certifier_client_test.go +++ b/activation/e2e/certifier_client_test.go @@ -92,7 +92,7 @@ func TestCertification(t *testing.T) { require.NoError(t, err) poets = append(poets, client) - certifierClient := activation.NewCertifierClient(zaptest.NewLogger(t), post, info) + certifierClient := activation.NewCertifierClient(zaptest.NewLogger(t), post, info, shared.ZeroChallenge) certifier := activation.NewCertifier(t.TempDir(), zaptest.NewLogger(t), certifierClient) certs := certifier.CertifyAll(context.Background(), poets) require.Len(t, certs, 3) diff --git a/activation/e2e/nipost_test.go b/activation/e2e/nipost_test.go index 2d4a2ffce5..4a018d8c14 100644 --- a/activation/e2e/nipost_test.go +++ b/activation/e2e/nipost_test.go @@ -200,7 +200,7 @@ func TestNIPostBuilderWithClients(t *testing.T) { ) require.NoError(t, err) - certifierClient := activation.NewCertifierClient(zaptest.NewLogger(t), post, info) + certifierClient := activation.NewCertifierClient(zaptest.NewLogger(t), post, info, shared.ZeroChallenge) certifier := activation.NewCertifier(t.TempDir(), logger, certifierClient) certifier.CertifyAll(context.Background(), []activation.PoetClient{client}) diff --git a/activation/e2e/validation_test.go b/activation/e2e/validation_test.go index fe2790f08c..11464f5266 100644 --- a/activation/e2e/validation_test.go +++ b/activation/e2e/validation_test.go @@ -111,7 +111,7 @@ func TestValidator_Validate(t *testing.T) { } challengeHash := challenge.Hash() - certifierClient := activation.NewCertifierClient(zaptest.NewLogger(t), post, info) + certifierClient := activation.NewCertifierClient(zaptest.NewLogger(t), post, info, shared.ZeroChallenge) certifier := activation.NewCertifier(t.TempDir(), logger, certifierClient) nipost, err := nb.BuildNIPost(context.Background(), &challenge, certifier) require.NoError(t, err)