Skip to content

Commit

Permalink
PreCommitPolicy: Fix an edge-case when deal duration is close to the …
Browse files Browse the repository at this point in the history
…MaxSectorExpirationExtension
  • Loading branch information
arajasek committed Jul 28, 2021
1 parent ef94509 commit ef67e38
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion chain/actors/builtin/miner/actor.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {

var Methods = builtin{{.latestVersion}}.MethodsMiner

// Unchanged between v0, v2, v3, and v4 actors
// Unchanged between v0, v2, v3, v4, and v5 actors
var WPoStProvingPeriod = miner0.WPoStProvingPeriod
var WPoStPeriodDeadlines = miner0.WPoStPeriodDeadlines
var WPoStChallengeWindow = miner0.WPoStChallengeWindow
Expand Down
2 changes: 1 addition & 1 deletion chain/actors/builtin/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func init() {

var Methods = builtin5.MethodsMiner

// Unchanged between v0, v2, v3, and v4 actors
// Unchanged between v0, v2, v3, v4, and v5 actors
var WPoStProvingPeriod = miner0.WPoStProvingPeriod
var WPoStPeriodDeadlines = miner0.WPoStPeriodDeadlines
var WPoStChallengeWindow = miner0.WPoStChallengeWindow
Expand Down
4 changes: 4 additions & 0 deletions chain/actors/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ func GetMaxSectorExpirationExtension() abi.ChainEpoch {
return miner5.MaxSectorExpirationExtension
}

func GetMinSectorExpiration() abi.ChainEpoch {
return miner5.MinSectorExpiration
}

func GetMaxPoStPartitions(nv network.Version, p abi.RegisteredPoStProof) (int, error) {
sectorsPerPart, err := builtin5.PoStProofWindowPoStPartitionSectors(p)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions chain/actors/policy/policy.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ func GetMaxSectorExpirationExtension() abi.ChainEpoch {
return miner{{.latestVersion}}.MaxSectorExpirationExtension
}

func GetMinSectorExpiration() abi.ChainEpoch {
return miner{{.latestVersion}}.MinSectorExpiration
}

func GetMaxPoStPartitions(nv network.Version, p abi.RegisteredPoStProof) (int, error) {
sectorsPerPart, err := builtin{{.latestVersion}}.PoStProofWindowPoStPartitionSectors(p)
if err != nil {
Expand Down
41 changes: 33 additions & 8 deletions extern/storage-sealing/precommit_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sealing
import (
"context"

"github.com/filecoin-project/lotus/chain/actors/policy"

"github.com/filecoin-project/lotus/chain/actors/builtin/miner"

"github.com/filecoin-project/go-state-types/network"
Expand Down Expand Up @@ -60,7 +62,7 @@ func (p *BasicPreCommitPolicy) Expiration(ctx context.Context, ps ...Piece) (abi
return 0, err
}

var end *abi.ChainEpoch
var hardMinimum *abi.ChainEpoch

for _, p := range ps {
if p.DealInfo == nil {
Expand All @@ -72,18 +74,41 @@ func (p *BasicPreCommitPolicy) Expiration(ctx context.Context, ps ...Piece) (abi
continue
}

if end == nil || *end < p.DealInfo.DealSchedule.EndEpoch {
if hardMinimum == nil || *hardMinimum < p.DealInfo.DealSchedule.EndEpoch {
tmp := p.DealInfo.DealSchedule.EndEpoch
end = &tmp
hardMinimum = &tmp
}
}

if end == nil {
tmp := epoch + p.duration
end = &tmp
exp := epoch + p.duration
if hardMinimum != nil {
exp = *hardMinimum
}

// TODO: Is this still needed?
// align on proving period boundary
exp += miner.WPoStProvingPeriod - (exp % miner.WPoStProvingPeriod) + p.provingBoundary - 1

// add / subtract out miner.WPoStProvingPeriod to allow 24h for the PC message to land, should probably be a config
maxEnd := epoch + policy.GetMaxSectorExpirationExtension() - miner.WPoStProvingPeriod
minEnd := epoch + policy.GetMinSectorExpiration() + miner.WPoStProvingPeriod

if exp > maxEnd {
// we need to subtract out ceil(end - maxEnd) days
daysToSubtract := (exp-maxEnd)/miner.WPoStProvingPeriod + 1
exp -= daysToSubtract * miner.WPoStProvingPeriod
}

if exp < minEnd {
// we need to add ceil(end - maxEnd) days
daysToAdd := (minEnd-exp)/miner.WPoStProvingPeriod + 1
exp += daysToAdd * miner.WPoStProvingPeriod
}

*end += miner.WPoStProvingPeriod - (*end % miner.WPoStProvingPeriod) + p.provingBoundary - 1
if hardMinimum != nil && exp < *hardMinimum {
// i guess the best thing to do here is to just set it to the deal-enforced limit?
exp = *hardMinimum
}

return *end, nil
return exp, nil
}
8 changes: 4 additions & 4 deletions extern/storage-sealing/precommit_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestBasicPolicyEmptySector(t *testing.T) {
exp, err := policy.Expiration(context.Background())
require.NoError(t, err)

assert.Equal(t, 2879, int(exp))
assert.Equal(t, 524159, int(exp))
}

func TestBasicPolicyMostConstrictiveSchedule(t *testing.T) {
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestBasicPolicyMostConstrictiveSchedule(t *testing.T) {
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)

assert.Equal(t, 2890, int(exp))
assert.Equal(t, 524170, int(exp))
}

func TestBasicPolicyIgnoresExistingScheduleIfExpired(t *testing.T) {
Expand All @@ -112,7 +112,7 @@ func TestBasicPolicyIgnoresExistingScheduleIfExpired(t *testing.T) {
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)

assert.Equal(t, 2879, int(exp))
assert.Equal(t, 524159, int(exp))
}

func TestMissingDealIsIgnored(t *testing.T) {
Expand Down Expand Up @@ -146,5 +146,5 @@ func TestMissingDealIsIgnored(t *testing.T) {
exp, err := policy.Expiration(context.Background(), pieces...)
require.NoError(t, err)

assert.Equal(t, 2890, int(exp))
assert.Equal(t, 524170, int(exp))
}

0 comments on commit ef67e38

Please sign in to comment.