Skip to content

Commit

Permalink
Try #5061:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Oct 5, 2023
2 parents c0c5967 + 582b03c commit ada8b8c
Show file tree
Hide file tree
Showing 30 changed files with 1,032 additions and 820 deletions.
41 changes: 39 additions & 2 deletions activation/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"sync"
"time"

"github.com/spacemeshos/post/proving"
"github.com/spacemeshos/post/shared"
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -88,6 +87,9 @@ type Builder struct {
initialPost *types.Post
validator nipostValidator

postMux sync.Mutex
postClient PostClient

// smeshingMutex protects `StartSmeshing` and `StopSmeshing` from concurrent access
smeshingMutex sync.Mutex

Expand Down Expand Up @@ -183,6 +185,41 @@ func NewBuilder(
return b
}

func (b *Builder) Connected(client PostClient) {
b.postMux.Lock()
defer b.postMux.Unlock()

if b.postClient != nil {
b.log.With().Error("post service already connected")
return
}

b.postClient = client
}

func (b *Builder) Disconnected(client PostClient) {
b.postMux.Lock()
defer b.postMux.Unlock()

if b.postClient != client {
b.log.With().Debug("post service not connected")
return
}

b.postClient = nil
}

func (b *Builder) proof(ctx context.Context, challenge []byte) (*types.Post, *types.PostMetadata, error) {
b.postMux.Lock()
defer b.postMux.Unlock()

if b.postClient == nil {
return nil, nil, errors.New("post service not connected")
}

return b.postClient.Proof(ctx, challenge)
}

// Smeshing returns true iff atx builder is smeshing.
func (b *Builder) Smeshing() bool {
return b.started.Load()
Expand Down Expand Up @@ -333,7 +370,7 @@ func (b *Builder) generateInitialPost(ctx context.Context) error {
startTime := time.Now()
var err error
events.EmitPostStart(shared.ZeroChallenge)
post, metadata, err := b.postSetupProvider.GenerateProof(ctx, shared.ZeroChallenge, proving.WithPowCreator(b.nodeID.Bytes()))
post, metadata, err := b.proof(ctx, shared.ZeroChallenge)
if err != nil {
events.EmitPostFailure()
return fmt.Errorf("post execution: %w", err)
Expand Down
31 changes: 17 additions & 14 deletions activation/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ type testAtxBuilder struct {
coinbase types.Address
goldenATXID types.ATXID

mpub *mocks.MockPublisher
mnipost *MocknipostBuilder
mpost *MockpostSetupProvider
mclock *MocklayerClock
msync *Mocksyncer
mValidator *MocknipostValidator
mpub *mocks.MockPublisher
mnipost *MocknipostBuilder
mpost *MockpostSetupProvider
mpostClient *MockPostClient
mclock *MocklayerClock
msync *Mocksyncer
mValidator *MocknipostValidator
}

func newTestBuilder(tb testing.TB, opts ...BuilderOption) *testAtxBuilder {
Expand All @@ -118,6 +119,7 @@ func newTestBuilder(tb testing.TB, opts ...BuilderOption) *testAtxBuilder {
mpub: mocks.NewMockPublisher(ctrl),
mnipost: NewMocknipostBuilder(ctrl),
mpost: NewMockpostSetupProvider(ctrl),
mpostClient: NewMockPostClient(ctrl),
mclock: NewMocklayerClock(ctrl),
msync: NewMocksyncer(ctrl),
mValidator: NewMocknipostValidator(ctrl),
Expand All @@ -143,6 +145,7 @@ func newTestBuilder(tb testing.TB, opts ...BuilderOption) *testAtxBuilder {
Nonce: 0,
Indices: make([]byte, 10),
}
b.Connected(tab.mpostClient)
tab.Builder = b
dir := tb.TempDir()
tab.mnipost.EXPECT().DataDir().Return(dir).AnyTimes()
Expand Down Expand Up @@ -250,7 +253,7 @@ func TestBuilder_StartSmeshingCoinbase(t *testing.T) {
tab.mpost.EXPECT().StartSession(gomock.Any()).AnyTimes()
tab.mpost.EXPECT().LastOpts().Return(&PostSetupOpts{}).AnyTimes()
tab.mpost.EXPECT().CommitmentAtx().Return(tab.goldenATXID, nil).AnyTimes()
tab.mpost.EXPECT().GenerateProof(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mpostClient.EXPECT().Proof(gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mValidator.EXPECT().Post(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
tab.mclock.EXPECT().AwaitLayer(gomock.Any()).Return(make(chan struct{})).AnyTimes()
require.NoError(t, tab.StartSmeshing(coinbase, postSetupOpts))
Expand All @@ -271,7 +274,7 @@ func TestBuilder_RestartSmeshing(t *testing.T) {
tab.mpost.EXPECT().CommitmentAtx().Return(types.EmptyATXID, nil).AnyTimes()
tab.mpost.EXPECT().LastOpts().Return(&PostSetupOpts{}).AnyTimes()
tab.mpost.EXPECT().StartSession(gomock.Any()).AnyTimes()
tab.mpost.EXPECT().GenerateProof(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{
tab.mpostClient.EXPECT().Proof(gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{
Challenge: shared.ZeroChallenge,
}, nil)
tab.mpost.EXPECT().Reset().AnyTimes()
Expand Down Expand Up @@ -382,7 +385,7 @@ func TestBuilder_StartSmeshing_PanicsOnErrInStartSession(t *testing.T) {
tab.log = l

// Stub these methods in case they get called
tab.mpost.EXPECT().GenerateProof(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mpostClient.EXPECT().Proof(gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mclock.EXPECT().AwaitLayer(gomock.Any()).AnyTimes()

// Set expectations
Expand All @@ -407,7 +410,7 @@ func TestBuilder_StartSmeshing_SessionNotStartedOnFailPrepare(t *testing.T) {
tab.log = l

// Stub these methods in case they get called
tab.mpost.EXPECT().GenerateProof(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mpostClient.EXPECT().Proof(gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mclock.EXPECT().AwaitLayer(gomock.Any()).AnyTimes()

// Set PrepareInitializer to fail
Expand All @@ -430,7 +433,7 @@ func TestBuilder_StopSmeshing_OnPoSTError(t *testing.T) {
tab.mpost.EXPECT().StartSession(gomock.Any()).Return(nil).AnyTimes()
tab.mpost.EXPECT().CommitmentAtx().Return(types.EmptyATXID, nil).AnyTimes()
tab.mpost.EXPECT().LastOpts().Return(&PostSetupOpts{}).AnyTimes()
tab.mpost.EXPECT().GenerateProof(gomock.Any(), gomock.Any(), gomock.Any()).Return(&types.Post{}, &types.PostMetadata{}, nil).AnyTimes()
tab.mpostClient.EXPECT().Proof(gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mValidator.EXPECT().Post(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
ch := make(chan struct{})
close(ch)
Expand Down Expand Up @@ -1089,7 +1092,7 @@ func TestBuilder_RetryPublishActivationTx(t *testing.T) {

func TestBuilder_InitialProofGeneratedOnce(t *testing.T) {
tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4}))
tab.mpost.EXPECT().GenerateProof(gomock.Any(), shared.ZeroChallenge, gomock.Any()).Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mpostClient.EXPECT().Proof(gomock.Any(), shared.ZeroChallenge).Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mpost.EXPECT().LastOpts().Return(&PostSetupOpts{})
tab.mpost.EXPECT().CommitmentAtx().Return(tab.goldenATXID, nil)
tab.mValidator.EXPECT().Post(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
Expand Down Expand Up @@ -1121,7 +1124,7 @@ func TestBuilder_InitialPostIsPersisted(t *testing.T) {
tab.mpost.EXPECT().Config().AnyTimes().Return(PostConfig{})
tab.mpost.EXPECT().LastOpts().Return(&PostSetupOpts{}).AnyTimes()
tab.mpost.EXPECT().CommitmentAtx().Return(tab.goldenATXID, nil).Times(3)
tab.mpost.EXPECT().GenerateProof(gomock.Any(), shared.ZeroChallenge, gomock.Any()).Return(&types.Post{}, &types.PostMetadata{
tab.mpostClient.EXPECT().Proof(gomock.Any(), shared.ZeroChallenge).Return(&types.Post{}, &types.PostMetadata{
Challenge: shared.ZeroChallenge,
}, nil)
tab.mValidator.EXPECT().Post(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
Expand All @@ -1132,7 +1135,7 @@ func TestBuilder_InitialPostIsPersisted(t *testing.T) {

// Remove the persisted post file and try again
require.NoError(t, os.Remove(filepath.Join(tab.nipostBuilder.DataDir(), postFilename)))
tab.mpost.EXPECT().GenerateProof(gomock.Any(), shared.ZeroChallenge, gomock.Any()).Return(&types.Post{}, &types.PostMetadata{}, nil)
tab.mpostClient.EXPECT().Proof(gomock.Any(), shared.ZeroChallenge).Return(&types.Post{}, &types.PostMetadata{}, nil)
require.NoError(t, tab.generateInitialPost(context.Background()))
}

Expand Down
15 changes: 10 additions & 5 deletions activation/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"time"

"github.com/spacemeshos/post/proving"
"github.com/spacemeshos/post/shared"
"github.com/spacemeshos/post/verifying"

Expand Down Expand Up @@ -56,20 +55,26 @@ type atxProvider interface {
}

// PostSetupProvider defines the functionality required for Post setup.
// This interface is used by the atx builder and currently implemented by the PostSetupManager.
// Eventually most of the functionality will be moved to the PoSTClient.
type postSetupProvider interface {
Status() *PostSetupStatus
Providers() ([]PostSetupProvider, error)
Benchmark(p PostSetupProvider) (int, error)
PrepareInitializer(ctx context.Context, opts PostSetupOpts) error
StartSession(context context.Context) error
Reset() error
GenerateProof(ctx context.Context, challenge []byte, options ...proving.OptionFunc) (*types.Post, *types.PostMetadata, error)
CommitmentAtx() (types.ATXID, error)
VRFNonce() (*types.VRFPostIndex, error)
LastOpts() *PostSetupOpts
Config() PostConfig
}

// nipostClient is a temporary interface for the NIPostBuilder.
// it is implemented by the PostSetupManager but will eventually merge with the PoSTClient.
type nipostClient interface {
Status() *PostSetupStatus
CommitmentAtx() (types.ATXID, error)
LastOpts() *PostSetupOpts
}

// SmeshingProvider defines the functionality required for the node's Smesher API.
type SmeshingProvider interface {
Smeshing() bool
Expand Down
Loading

0 comments on commit ada8b8c

Please sign in to comment.