Skip to content

Commit

Permalink
Use the right post challenge to certify
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Nov 8, 2023
1 parent c7e45b6 commit 7c1cd15
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
16 changes: 8 additions & 8 deletions activation/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,41 +323,41 @@ 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
)

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
}
Expand All @@ -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) {
Expand Down
24 changes: 15 additions & 9 deletions activation/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,16 +1140,17 @@ 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) {
tab := newTestBuilder(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)
Expand All @@ -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())
Expand All @@ -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)
})
}
Expand Down
7 changes: 4 additions & 3 deletions activation/certifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand All @@ -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,
},
}

Expand Down
2 changes: 1 addition & 1 deletion activation/e2e/certifier_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion activation/e2e/nipost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
2 changes: 1 addition & 1 deletion activation/e2e/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7c1cd15

Please sign in to comment.