Skip to content

Commit 60b8856

Browse files
committed
process niporep sector commit info and push message on chain #1086
1 parent 93c829e commit 60b8856

File tree

2 files changed

+269
-1
lines changed

2 files changed

+269
-1
lines changed

damocles-manager/modules/impl/commitmgr/commit_processor.go

+137-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/filecoin-project/go-state-types/big"
1515
stbuiltin "github.com/filecoin-project/go-state-types/builtin"
1616
miner13 "github.com/filecoin-project/go-state-types/builtin/v13/miner"
17+
miner14 "github.com/filecoin-project/go-state-types/builtin/v14/miner"
1718

1819
"github.com/filecoin-project/venus/venus-shared/actors/builtin/miner"
1920

@@ -129,15 +130,21 @@ func (c CommitProcessor) Process(
129130

130131
ddoSectors := make([]core.SectorState, 0)
131132
builtinMarketSectors := make([]core.SectorState, 0)
133+
niporepSectors := make([]core.SectorState, 0)
132134
for i := range sectors {
133-
if sectors[i].HasBuiltinMarketDeal() {
135+
if sectors[i].SectorType.IsNonInteractive() {
136+
niporepSectors = append(niporepSectors, sectors[i])
137+
} else if sectors[i].HasBuiltinMarketDeal() {
134138
builtinMarketSectors = append(builtinMarketSectors, sectors[i])
135139
} else {
136140
ddoSectors = append(ddoSectors, sectors[i])
137141
}
138142
}
139143

140144
aggregate := c.ShouldBatch(mid) && len(sectors) >= core.MinAggregatedSectors
145+
if len(niporepSectors) > 0 {
146+
return c.ProcessNiPoRep(ctx, niporepSectors, mid, ctrlAddr, tok, nv, aggregate)
147+
}
141148
if nv >= MinDDONetworkVersion {
142149
if err := c.ProcessV2(ctx, ddoSectors, mid, ctrlAddr, tok, nv, aggregate); err != nil {
143150
return err
@@ -376,6 +383,135 @@ func (c CommitProcessor) ProcessV2(
376383
return nil
377384
}
378385

386+
func (c CommitProcessor) ProcessNiPoRep(
387+
ctx context.Context,
388+
sectors []core.SectorState,
389+
mid abi.ActorID,
390+
ctrlAddr address.Address,
391+
tok core.TipSetToken,
392+
nv network.Version,
393+
batch bool,
394+
) error {
395+
// Notice: If a sector in sectors has been sent, it's cid failed should be changed already.
396+
plog := log.With("proc", "prove", "miner", mid, "ctrl", ctrlAddr.String(), "len", len(sectors))
397+
398+
start := time.Now()
399+
defer func() {
400+
plog.Infof("finished process, elapsed %s", time.Since(start))
401+
}()
402+
403+
defer updateSector(ctx, c.smgr, sectors, plog)
404+
405+
mcfg, err := c.config.MinerConfig(mid)
406+
if err != nil {
407+
return fmt.Errorf("get miner config for %d: %w", mid, err)
408+
}
409+
410+
arp, err := c.aggregateProofType(nv)
411+
if err != nil {
412+
return fmt.Errorf("get aggregate proof type: %w", err)
413+
}
414+
415+
infos := []core.AggregateSealVerifyInfo{}
416+
sectorsMap := map[abi.SectorNumber]core.SectorState{}
417+
failed := map[abi.SectorID]struct{}{}
418+
actInfos := []miner14.SectorNIActivationInfo{}
419+
420+
collateral := big.Zero()
421+
for i, p := range sectors {
422+
sectorsMap[p.ID.Number] = sectors[i]
423+
expire, err := c.sectorExpiration(ctx, &p)
424+
if err != nil {
425+
plog.Errorf("get sector expiration for %d failed: %s\n", p.ID.Number, err)
426+
failed[sectors[i].ID] = struct{}{}
427+
continue
428+
}
429+
430+
if mcfg.Commitment.Prove.SendFund {
431+
sc, err := getSectorCollateral(ctx, c.api, mid, p.ID.Number, tok)
432+
if err != nil {
433+
plog.Errorf("get sector collateral for %d failed: %s\n", p.ID.Number, err)
434+
failed[sectors[i].ID] = struct{}{}
435+
continue
436+
}
437+
438+
collateral = big.Add(collateral, sc)
439+
}
440+
441+
infos = append(infos, core.AggregateSealVerifyInfo{
442+
Number: p.ID.Number,
443+
Randomness: abi.SealRandomness(p.Ticket.Ticket),
444+
InteractiveRandomness: abi.InteractiveSealRandomness(p.Seed.Seed),
445+
SealedCID: p.Pre.CommR,
446+
UnsealedCID: p.Pre.CommD,
447+
})
448+
449+
actInfos[i] = miner14.SectorNIActivationInfo{
450+
SealingNumber: p.ID.Number,
451+
SealerID: mid,
452+
SealedCID: p.Pre.CommR,
453+
SectorNumber: p.ID.Number,
454+
SealRandEpoch: p.Seed.Epoch,
455+
Expiration: expire,
456+
}
457+
}
458+
459+
if len(infos) == 0 {
460+
return nil
461+
}
462+
463+
sort.Slice(infos, func(i, j int) bool {
464+
return infos[i].Number < infos[j].Number
465+
})
466+
467+
sort.Slice(actInfos, func(i, j int) bool {
468+
return actInfos[i].SealingNumber < actInfos[j].SealingNumber
469+
})
470+
471+
params := &miner14.ProveCommitSectorsNIParams{
472+
Sectors: actInfos,
473+
SealProofType: sectorsMap[infos[0].Number].SectorType,
474+
AggregateProofType: arp,
475+
ProvingDeadline: 7,
476+
RequireActivationSuccess: true,
477+
}
478+
479+
proofs := make([][]byte, 0)
480+
for i := range infos {
481+
proofs = append(proofs, sectorsMap[infos[i].Number].Proof.Proof)
482+
}
483+
484+
params.AggregateProof, err = c.prover.AggregateSealProofs(ctx, core.AggregateSealVerifyProofAndInfos{
485+
Miner: mid,
486+
SealProof: sectorsMap[infos[0].Number].SectorType,
487+
AggregateProof: arp,
488+
Infos: infos,
489+
}, proofs)
490+
491+
if err != nil {
492+
return fmt.Errorf("aggregate sector failed: %w", err)
493+
}
494+
495+
enc := new(bytes.Buffer)
496+
if err := params.MarshalCBOR(enc); err != nil {
497+
return fmt.Errorf("couldn't serialize ProveCommitAggregateParams: %w", err)
498+
}
499+
500+
ccid, err := pushMessage(ctx, ctrlAddr, mid, collateral, stbuiltin.MethodsMiner.ProveCommitSectorsNI,
501+
c.msgClient, &mcfg.Commitment.Prove.Batch.FeeConfig, enc.Bytes(), plog)
502+
if err != nil {
503+
return fmt.Errorf("push aggregate prove message failed: %w", err)
504+
}
505+
506+
for i := range sectors {
507+
if _, ok := failed[sectors[i].ID]; !ok {
508+
sectors[i].MessageInfo.CommitCid = &ccid
509+
}
510+
}
511+
512+
return nil
513+
}
514+
379515
func (CommitProcessor) aggregateProofType(nv network.Version) (abi.RegisteredAggregationProof, error) {
380516
if nv < network.Version16 {
381517
return abi.RegisteredAggregationProof_SnarkPackV1, nil

damocles-manager/modules/impl/commitmgr/policy.go

+132
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,135 @@ func (PreCommitProcessor) ccSectorLifetime(
144144

145145
return ccLifetimeEpochs - provingPeriod*2, nil
146146
}
147+
148+
func (pp CommitProcessor) sectorExpiration(ctx context.Context, state *core.SectorState) (abi.ChainEpoch, error) {
149+
mcfg, err := pp.config.MinerConfig(state.ID.Miner)
150+
if err != nil {
151+
return 0, fmt.Errorf("get miner config: %w", err)
152+
}
153+
154+
tok, height, err := pp.api.ChainHead(ctx)
155+
if err != nil {
156+
return 0, fmt.Errorf("get chain head: %w", err)
157+
}
158+
159+
nv, err := pp.api.StateNetworkVersion(ctx, tok)
160+
if err != nil {
161+
return 0, fmt.Errorf("failed to get network version: %w", err)
162+
}
163+
164+
av, err := actors.VersionForNetwork(nv)
165+
if err != nil {
166+
return 0, fmt.Errorf("unsupported network version: %w", err)
167+
}
168+
169+
mpcd, err := policy.GetMaxProveCommitDuration(av, state.SectorType)
170+
if err != nil {
171+
return 0, fmt.Errorf("getting max prove commit duration: %w", err)
172+
}
173+
174+
expiration, err := pp.sectorEnd(height, nv, state, mcfg.Sector.LifetimeDays, miner.WPoStProvingPeriod)
175+
if err != nil {
176+
return 0, fmt.Errorf("calculate sector end: %w", err)
177+
}
178+
179+
//revive:disable-next-line:line-length-limit
180+
if minExpiration := state.Ticket.Epoch + policy.MaxPreCommitRandomnessLookback + mpcd + policy.MinSectorExpiration; expiration < minExpiration {
181+
expiration = minExpiration
182+
}
183+
184+
// Assume: both precommit msg & commit msg land on chain as early as possible
185+
maxLifetime, err := policy.GetMaxSectorExpirationExtension(nv)
186+
if err != nil {
187+
return 0, fmt.Errorf("get max sector expiration extension: %w", err)
188+
}
189+
maxExpiration := height + policy.GetPreCommitChallengeDelay() + maxLifetime
190+
if expiration > maxExpiration {
191+
expiration = maxExpiration
192+
}
193+
194+
return expiration, nil
195+
}
196+
197+
func (pp CommitProcessor) sectorEnd(
198+
height abi.ChainEpoch,
199+
nv network.Version,
200+
state *core.SectorState,
201+
lifetimeDays uint64,
202+
provingPeriod abi.ChainEpoch,
203+
) (abi.ChainEpoch, error) {
204+
var end *abi.ChainEpoch
205+
206+
for _, p := range state.SectorPiece() {
207+
if !p.HasDealInfo() {
208+
continue
209+
}
210+
211+
endEpoch := p.EndEpoch()
212+
if endEpoch < height {
213+
log.Warnf("piece schedule %+v ended before current epoch %d", p, height)
214+
continue
215+
}
216+
217+
if end == nil || *end < endEpoch {
218+
tmp := endEpoch
219+
end = &tmp
220+
}
221+
}
222+
223+
if end == nil {
224+
// no deal pieces, get expiration for committed capacity sector
225+
expirationDuration, err := pp.ccSectorLifetime(
226+
nv,
227+
abi.ChainEpoch(lifetimeDays*policy.EpochsInDay),
228+
provingPeriod,
229+
)
230+
if err != nil {
231+
return 0, fmt.Errorf("failed to get cc sector lifetime: %w", err)
232+
}
233+
234+
tmp := height + expirationDuration
235+
end = &tmp
236+
}
237+
238+
// Ensure there is at least one day for the PC message to land without falling below min sector lifetime
239+
// TODO: The "one day" should probably be a config, though it doesn't matter too much
240+
minExp := height + policy.GetMinSectorExpiration() + miner.WPoStProvingPeriod
241+
if *end < minExp {
242+
end = &minExp
243+
}
244+
245+
return *end, nil
246+
}
247+
248+
func (CommitProcessor) ccSectorLifetime(
249+
nv network.Version,
250+
ccLifetimeEpochs abi.ChainEpoch,
251+
provingPeriod abi.ChainEpoch,
252+
) (abi.ChainEpoch, error) {
253+
maxExpiration, err := policy.GetMaxSectorExpirationExtension(nv)
254+
if err != nil {
255+
return 0, fmt.Errorf("get max sector expiration extension: %w", err)
256+
}
257+
258+
if ccLifetimeEpochs == 0 {
259+
ccLifetimeEpochs = maxExpiration
260+
}
261+
262+
if minExpiration := abi.ChainEpoch(policy.MinSectorExpiration); ccLifetimeEpochs < minExpiration {
263+
log.Warnf(
264+
"value for CommittedCapacitySectorLifetime is too short, using default minimum (%d epochs)",
265+
minExpiration,
266+
)
267+
return minExpiration, nil
268+
}
269+
if ccLifetimeEpochs > maxExpiration {
270+
log.Warnf(
271+
"value for CommittedCapacitySectorLifetime is too long, using default maximum (%d epochs)",
272+
maxExpiration,
273+
)
274+
return maxExpiration, nil
275+
}
276+
277+
return ccLifetimeEpochs - provingPeriod*2, nil
278+
}

0 commit comments

Comments
 (0)