From e793897905640a1fc5ba73c5388934cbf08586e5 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 25 Oct 2024 09:13:04 -0700 Subject: [PATCH] test(f3): test participant polling --- chain/lf3/participation_lease_test.go | 3 +- chain/lf3/participation_test.go | 79 +++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/chain/lf3/participation_lease_test.go b/chain/lf3/participation_lease_test.go index 5c045768e86..c9dc11ac29e 100644 --- a/chain/lf3/participation_lease_test.go +++ b/chain/lf3/participation_lease_test.go @@ -32,7 +32,8 @@ func TestLeaser(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(123), lease.MinerID) require.Equal(t, issuer.String(), lease.Issuer) - require.Equal(t, uint64(5), lease.ValidityTerm) // Current instance (10) + offset (5) + require.Equal(t, uint64(10), lease.FromInstance) // Current instance (10) + offset (5) + require.Equal(t, uint64(5), lease.ValidityTerm) // Current instance (10) + offset (5) }) t.Run("get participants", func(t *testing.T) { progress.currentInstance = 11 diff --git a/chain/lf3/participation_test.go b/chain/lf3/participation_test.go index 79564d467eb..cfc2276c6fa 100644 --- a/chain/lf3/participation_test.go +++ b/chain/lf3/participation_test.go @@ -7,10 +7,10 @@ import ( "time" "github.com/jpillora/backoff" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/filecoin-project/go-address" - "github.com/filecoin-project/go-f3/gpbft" "github.com/filecoin-project/go-f3/manifest" "github.com/filecoin-project/lotus/api" @@ -41,10 +41,6 @@ func (m *manifestFailAPI) F3GetOrRenewParticipationTicket(ctx context.Context, m } } -func (m *manifestFailAPI) F3GetProgress(ctx context.Context) (gpbft.Instant, error) { - return gpbft.Instant{}, nil -} - func (m *manifestFailAPI) F3Participate(ctx context.Context, ticket api.F3ParticipationTicket) (api.F3ParticipationLease, error) { return api.F3ParticipationLease{ Network: "test", @@ -73,3 +69,76 @@ func TestParticipantManifestFailure(t *testing.T) { <-api.manifestRequested require.NoError(t, p.Stop(context.Background())) } + +type repeatedParticipateAPI struct { + secondTicket chan struct{} + instance uint64 + t *testing.T +} + +func (m *repeatedParticipateAPI) F3GetManifest(ctx context.Context) (*manifest.Manifest, error) { + return &manifest.Manifest{ + NetworkName: "test", + CatchUpAlignment: time.Millisecond, + }, nil +} + +func (m *repeatedParticipateAPI) F3GetOrRenewParticipationTicket(ctx context.Context, minerID address.Address, previous api.F3ParticipationTicket, instances uint64) (api.F3ParticipationTicket, error) { + switch string(previous) { + case "first ticket": + return api.F3ParticipationTicket("second ticket"), nil + case "": + return api.F3ParticipationTicket("first ticket"), nil + default: + panic("unexpected ticket") + } +} + +func (m *repeatedParticipateAPI) F3Participate(ctx context.Context, ticket api.F3ParticipationTicket) (api.F3ParticipationLease, error) { + switch string(ticket) { + case "first ticket": + case "second ticket": + // This is 6, not 5, because we expect one final call to participate before getting + // a new ticket. + assert.Equal(m.t, uint64(6), m.instance) + close(m.secondTicket) + return api.F3ParticipationLease{}, api.ErrF3ParticipationIssuerMismatch + default: + m.t.Errorf("unexpected f3 ticket: %s", string(ticket)) + return api.F3ParticipationLease{}, api.ErrF3Disabled + } + + if m.instance >= 10 { + m.t.Error("did not expect the participant to continue past the half-way point") + return api.F3ParticipationLease{}, api.ErrF3Disabled + } + + lease := api.F3ParticipationLease{ + Network: "test", + Issuer: "foobar", + MinerID: 0, + FromInstance: m.instance, + ValidityTerm: 10 - m.instance, + } + m.instance++ + + return lease, nil +} + +// Make sure we keep calling participate until our validity term drops to half (5) of the initial +// term (10). At that point, the participant should request a new ticket. +func TestParticipantRepeat(t *testing.T) { + api := &repeatedParticipateAPI{secondTicket: make(chan struct{}), t: t} + addr, err := address.NewIDAddress(1000) + require.NoError(t, err) + + p := lf3.NewParticipant(context.Background(), api, dtypes.MinerAddress(addr), + &backoff.Backoff{ + Min: 1 * time.Second, + Max: 1 * time.Minute, + Factor: 1.5, + }, 13, 10) + require.NoError(t, p.Start(context.Background())) + <-api.secondTicket + require.NoError(t, p.Stop(context.Background())) +}