Skip to content

Commit

Permalink
test(f3): test participant polling
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Oct 25, 2024
1 parent 3d6935b commit e793897
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
3 changes: 2 additions & 1 deletion chain/lf3/participation_lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 74 additions & 5 deletions chain/lf3/participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()))
}

0 comments on commit e793897

Please sign in to comment.