Skip to content

Commit

Permalink
Improve visibility around poet registrations in events
Browse files Browse the repository at this point in the history
Closes #5565
  • Loading branch information
jellonek committed Oct 10, 2024
1 parent 118ee7f commit 05ce6c4
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 25 deletions.
2 changes: 1 addition & 1 deletion activation/e2e/atx_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func Test_MarryAndMerge(t *testing.T) {
}

client := ae2e.NewTestPoetClient(2, poetCfg)
poetSvc := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetSvc := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)

clock, err := timesync.NewClock(
timesync.WithGenesisTime(genesis),
Expand Down
2 changes: 1 addition & 1 deletion activation/e2e/builds_atx_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestBuilder_SwitchesToBuildV2(t *testing.T) {
t.Cleanup(clock.Close)

client := ae2e.NewTestPoetClient(1, poetCfg)
poetClient := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetClient := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)

localDB := localsql.InMemory()
nb, err := activation.NewNIPostBuilder(
Expand Down
4 changes: 2 additions & 2 deletions activation/e2e/checkpoint_merged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Test_CheckpointAfterMerge(t *testing.T) {
}

client := ae2e.NewTestPoetClient(2, poetCfg)
poetSvc := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetSvc := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)

clock, err := timesync.NewClock(
timesync.WithGenesisTime(genesis),
Expand Down Expand Up @@ -285,7 +285,7 @@ func Test_CheckpointAfterMerge(t *testing.T) {
require.NoError(t, err)
cdb = datastore.NewCachedDB(newDB, logger)

poetSvc = activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetSvc = activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)
validator = activation.NewValidator(newDB, poetDb, cfg, opts.Scrypt, verifier)
require.NoError(t, err)
atxHdlr = activation.NewHandler(
Expand Down
4 changes: 2 additions & 2 deletions activation/e2e/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestCheckpoint_PublishingSoloATXs(t *testing.T) {
GracePeriod: epoch / 4,
}
client := ae2e.NewTestPoetClient(1, poetCfg)
poetService := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetService := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)

// ensure that genesis aligns with layer timings
genesis := time.Now().Add(layerDuration).Round(layerDuration)
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestCheckpoint_PublishingSoloATXs(t *testing.T) {
require.NoError(t, err)
cdb = datastore.NewCachedDB(newDB, logger)
atxdata, err = atxsdata.Warm(newDB, 1, logger)
poetService = activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetService = activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)
validator = activation.NewValidator(newDB, poetDb, cfg, opts.Scrypt, verifier)
require.NoError(t, err)
atxHdlr = activation.NewHandler(
Expand Down
4 changes: 2 additions & 2 deletions 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)

client := ae2e.NewTestPoetClient(1, poetCfg)
poetService := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetService := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)

localDB := localsql.InMemory()
nb, err := activation.NewNIPostBuilder(
Expand Down Expand Up @@ -275,7 +275,7 @@ func Test_NIPostBuilderWithMultipleClients(t *testing.T) {
poetDb, err := activation.NewPoetDb(db, logger.Named("poetDb"))
require.NoError(t, err)
client := ae2e.NewTestPoetClient(len(signers), poetCfg)
poetService := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetService := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)

mclock := activation.NewMocklayerClock(ctrl)
mclock.EXPECT().LayerToTime(gomock.Any()).AnyTimes().DoAndReturn(
Expand Down
1 change: 1 addition & 0 deletions activation/e2e/poet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (h *HTTPPoetTestHarness) Client(
h.ServerCfg(),
cfg,
logger,
1,
opts...,
)
}
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 @@ -53,7 +53,7 @@ func TestValidator_Validate(t *testing.T) {
poetDb, err := activation.NewPoetDb(statesql.InMemory(), logger.Named("poetDb"))
require.NoError(t, err)
client := ae2e.NewTestPoetClient(1, poetCfg)
poetService := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger)
poetService := activation.NewPoetServiceWithClient(poetDb, client, poetCfg, logger, 1)

mclock := activation.NewMocklayerClock(ctrl)
mclock.EXPECT().LayerToTime(gomock.Any()).AnyTimes().DoAndReturn(
Expand Down
3 changes: 3 additions & 0 deletions activation/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type PoetService interface {

// Proof returns the proof for the given round ID.
Proof(ctx context.Context, roundID string) (*types.PoetProof, []types.Hash32, error)

// GetTickSize returns tickSize configured for particular PoET service
GetTickSize() uint64
}

// A certifier client that the certifierService uses to obtain certificates
Expand Down
9 changes: 9 additions & 0 deletions activation/nipost.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ func (nb *NIPostBuilder) getBestProof(
type poetProof struct {
poet *types.PoetProof
membership *types.MerkleProof
client PoetService
round string
}
proofs := make(chan *poetProof, len(registrations))

Expand Down Expand Up @@ -568,6 +570,8 @@ func (nb *NIPostBuilder) getBestProof(
proofs <- &poetProof{
poet: proof,
membership: membership,
client: client,
round: round,
}
return nil
})
Expand Down Expand Up @@ -600,6 +604,11 @@ func (nb *NIPostBuilder) getBestProof(
zap.Binary("ref", ref[:]),
log.ZShortStringer("smesherID", nodeID),
)
events.EmitBestPoetProofSelected(
bestProof.client.Address(),
bestProof.round,
bestProof.poet.LeafCount/bestProof.client.GetTickSize(),
)
return ref, bestProof.membership, nil
}

Expand Down
20 changes: 19 additions & 1 deletion activation/poet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/spacemeshos/go-spacemesh/activation/metrics"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/localsql/certifier"
Expand Down Expand Up @@ -245,7 +246,10 @@ func (c *HTTPPoetClient) Submit(
roundEnd = time.Now().Add(resBody.RoundEnd.AsDuration())
}

return &types.PoetRound{ID: resBody.RoundId, End: roundEnd}, nil
round := types.PoetRound{ID: resBody.RoundId, End: roundEnd}
events.EmitRegisteredInPoet(c.Address(), challenge, round.ID)

return &round, nil
}

func (c *HTTPPoetClient) Info(ctx context.Context) (*types.PoetInfo, error) {
Expand Down Expand Up @@ -400,6 +404,9 @@ type poetService struct {
expectedPhaseShift time.Duration
infoCache cachedData[*types.PoetInfo]
powParamsCache cachedData[*PoetPowParams]

// Used to calculate ticks from PoetProof.LeafCount.
tickSize uint64
}

type PoetServiceOpt func(*poetService)
Expand All @@ -415,17 +422,20 @@ func NewPoetService(
server types.PoetServer,
cfg PoetConfig,
logger *zap.Logger,
tickSize uint64,
opts ...PoetServiceOpt,
) (*poetService, error) {
client, err := NewHTTPPoetClient(server, cfg, WithLogger(logger))
if err != nil {
return nil, err
}
events.EmitPoetClientCreated(client.Address())
return NewPoetServiceWithClient(
db,
client,
cfg,
logger,
tickSize,
opts...,
), nil
}
Expand All @@ -435,6 +445,7 @@ func NewPoetServiceWithClient(
client PoetClient,
cfg PoetConfig,
logger *zap.Logger,
tickSize uint64,
opts ...PoetServiceOpt,
) *poetService {
service := &poetService{
Expand All @@ -446,6 +457,7 @@ func NewPoetServiceWithClient(
powParamsCache: cachedData[*PoetPowParams]{ttl: cfg.PowParamsCacheTTL},
proofMembers: make(map[string][]types.Hash32, 1),
expectedPhaseShift: cfg.PhaseShift,
tickSize: tickSize,
}
for _, opt := range opts {
opt(service)
Expand All @@ -465,6 +477,10 @@ func NewPoetServiceWithClient(
return service
}

func (c *poetService) GetTickSize() uint64 {
return c.tickSize
}

func (c *poetService) verifyPhaseShiftConfiguration(ctx context.Context) error {
info, err := c.getInfo(ctx)
if err != nil {
Expand Down Expand Up @@ -617,6 +633,8 @@ func (c *poetService) Proof(ctx context.Context, roundID string) (*types.PoetPro
clear(c.proofMembers)
c.proofMembers[roundID] = members

events.EmitPoetProofReceived(c.Address(), proof.RoundID, proof.LeafCount/c.tickSize)

return &proof.PoetProof, members, nil
}

Expand Down
30 changes: 15 additions & 15 deletions activation/poet_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestPoetClient_CachesProof(t *testing.T) {

client, err := NewHTTPPoetClient(server, DefaultPoetConfig(), withCustomHttpClient(ts.Client()))
require.NoError(t, err)
poet := NewPoetServiceWithClient(db, client, DefaultPoetConfig(), zaptest.NewLogger(t))
poet := NewPoetServiceWithClient(db, client, DefaultPoetConfig(), zaptest.NewLogger(t), 1)

eg := errgroup.Group{}
for range 20 {
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestPoetClient_QueryProofTimeout(t *testing.T) {
client.EXPECT().Info(gomock.Any()).Return(&types.PoetInfo{
PhaseShift: cfg.PhaseShift,
}, nil)
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1)

// any additional call on Info will block
client.EXPECT().Proof(gomock.Any(), "1").DoAndReturn(
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestPoetClient_Certify(t *testing.T) {

client, err := NewHTTPPoetClient(server, cfg, withCustomHttpClient(ts.Client()))
require.NoError(t, err)
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), WithCertifier(mCertifier))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1, WithCertifier(mCertifier))

got, err := poet.Certify(context.Background(), sig.NodeID())
require.NoError(t, err)
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestPoetClient_Certify(t *testing.T) {
client, err := NewHTTPPoetClient(server, cfg, withCustomHttpClient(ts.Client()))
require.NoError(t, err)

poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), WithCertifier(mCertifier))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1, WithCertifier(mCertifier))
_, err = poet.Certify(context.Background(), sig.NodeID())
require.ErrorIs(t, err, ErrCertificatesNotSupported)
})
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestPoetClient_ObtainsCertOnSubmit(t *testing.T) {

client, err := NewHTTPPoetClient(server, cfg, withCustomHttpClient(ts.Client()))
require.NoError(t, err)
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), WithCertifier(mCertifier))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1, WithCertifier(mCertifier))

_, err = poet.Submit(context.Background(), time.Time{}, nil, nil, types.RandomEdSignature(), sig.NodeID())
require.NoError(t, err)
Expand Down Expand Up @@ -434,7 +434,7 @@ func TestPoetClient_RecertifiesOnAuthFailure(t *testing.T) {

client, err := NewHTTPPoetClient(server, cfg, withCustomHttpClient(ts.Client()))
require.NoError(t, err)
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), WithCertifier(mCertifier))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1, WithCertifier(mCertifier))

_, err = poet.Submit(context.Background(), time.Time{}, nil, nil, types.RandomEdSignature(), sig.NodeID())
require.NoError(t, err)
Expand Down Expand Up @@ -509,7 +509,7 @@ func TestPoetClient_FallbacksToPowWhenCannotRecertify(t *testing.T) {
client, err := NewHTTPPoetClient(server, cfg, withCustomHttpClient(ts.Client()))
require.NoError(t, err)

poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), WithCertifier(mCertifier))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1, WithCertifier(mCertifier))

_, err = poet.Submit(context.Background(), time.Time{}, nil, nil, types.RandomEdSignature(), sig.NodeID())
require.NoError(t, err)
Expand Down Expand Up @@ -541,7 +541,7 @@ func TestPoetService_CachesCertifierInfo(t *testing.T) {
client.EXPECT().Address().Return("some_addr").AnyTimes()
client.EXPECT().Info(gomock.Any()).Return(poetInfoResp, nil)

poet := NewPoetServiceWithClient(db, client, cfg, zaptest.NewLogger(t))
poet := NewPoetServiceWithClient(db, client, cfg, zaptest.NewLogger(t), 1)

if tc.ttl == 0 {
client.EXPECT().Info(gomock.Any()).Times(5).Return(poetInfoResp, nil)
Expand Down Expand Up @@ -575,7 +575,7 @@ func TestPoetService_CachesPowParams(t *testing.T) {
client.EXPECT().Info(gomock.Any()).Return(&types.PoetInfo{}, nil)
client.EXPECT().Address().Return("some_address").AnyTimes()

poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1)

params := PoetPowParams{
Challenge: types.RandomBytes(10),
Expand Down Expand Up @@ -609,7 +609,7 @@ func TestPoetService_FetchPoetPhaseShift(t *testing.T) {
PhaseShift: phaseShift,
}, nil)

NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t))
NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1)
})

t.Run("poet service created: phase shift is not fetched",
Expand All @@ -621,7 +621,7 @@ func TestPoetService_FetchPoetPhaseShift(t *testing.T) {
client.EXPECT().Address().Return("some_addr").AnyTimes()
client.EXPECT().Info(gomock.Any()).Return(nil, errors.New("some error"))

NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t))
NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1)
})

t.Run("poet service creation failed: expected and fetched phase shift are not matching",
Expand All @@ -636,7 +636,7 @@ func TestPoetService_FetchPoetPhaseShift(t *testing.T) {
}, nil)

log := zaptest.NewLogger(t).WithOptions(zap.WithFatalHook(calledFatal(t)))
NewPoetServiceWithClient(nil, client, cfg, log)
NewPoetServiceWithClient(nil, client, cfg, log, 1)
})

t.Run("fetch phase shift before submitting challenge: success",
Expand All @@ -648,7 +648,7 @@ func TestPoetService_FetchPoetPhaseShift(t *testing.T) {
client.EXPECT().Address().Return("some_addr").AnyTimes()
client.EXPECT().Info(gomock.Any()).Return(nil, errors.New("some error"))

poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1)
sig, err := signing.NewEdSigner()
require.NoError(t, err)

Expand All @@ -673,7 +673,7 @@ func TestPoetService_FetchPoetPhaseShift(t *testing.T) {
client.EXPECT().Address().Return("some_addr").AnyTimes()
client.EXPECT().Info(gomock.Any()).Return(nil, errors.New("some error"))

poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t))
poet := NewPoetServiceWithClient(nil, client, cfg, zaptest.NewLogger(t), 1)
sig, err := signing.NewEdSigner()
require.NoError(t, err)

Expand All @@ -694,7 +694,7 @@ func TestPoetService_FetchPoetPhaseShift(t *testing.T) {
client.EXPECT().Info(gomock.Any()).Return(nil, errors.New("some error"))

log := zaptest.NewLogger(t).WithOptions(zap.WithFatalHook(calledFatal(t)))
poet := NewPoetServiceWithClient(nil, client, cfg, log)
poet := NewPoetServiceWithClient(nil, client, cfg, log, 1)
sig, err := signing.NewEdSigner()
require.NoError(t, err)

Expand Down
Loading

0 comments on commit 05ce6c4

Please sign in to comment.