Skip to content

Commit 07050cf

Browse files
Stebalienrjan90
authored andcommitted
fip-0100: add daily fee state invariants (#362)
1. Verify that the fees in the expiration queues match the fees associated with the sectors for each entry in each queue. 2. Verify that the sums of the fees in all the the expiration queues for each deadline match the fees in the deadlines. 3. Verify that the sum of the partition live power matches the live power in each deadline.
1 parent 0129d9b commit 07050cf

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

builtin/v16/miner/invariants.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func CheckDeadlineStateInvariants(deadline *Deadline, store adt.Store, quant bui
179179
allLivePower := NewPowerPairZero()
180180
allActivePower := NewPowerPairZero()
181181
allFaultyPower := NewPowerPairZero()
182+
allDailyFee := big.Zero()
182183

183184
// Check partitions.
184185
partitionsWithExpirations := map[abi.ChainEpoch][]uint64{}
@@ -220,6 +221,7 @@ func CheckDeadlineStateInvariants(deadline *Deadline, store adt.Store, quant bui
220221
allLivePower = allLivePower.Add(summary.LivePower)
221222
allActivePower = allActivePower.Add(summary.ActivePower)
222223
allFaultyPower = allFaultyPower.Add(summary.FaultyPower)
224+
allDailyFee = big.Add(allDailyFee, summary.DailyFee)
223225
return nil
224226
})
225227
acc.RequireNoError(err, "error iterating partitions")
@@ -318,6 +320,8 @@ func CheckDeadlineStateInvariants(deadline *Deadline, store adt.Store, quant bui
318320
}
319321

320322
acc.Require(deadline.FaultyPower.Equals(allFaultyPower), "deadline faulty power %v != partitions total %v", deadline.FaultyPower, allFaultyPower)
323+
acc.Require(deadline.LivePower.Equals(allLivePower), "deadline live power %v != partitions total %v", deadline.LivePower, allLivePower)
324+
acc.Require(deadline.DailyFee.Equals(allDailyFee), "deadline daily fee %v != partitions total %v", deadline.DailyFee, allDailyFee)
321325

322326
{
323327
// Validate partition expiration queue contains an entry for each partition and epoch with an expiration.
@@ -373,6 +377,7 @@ type PartitionStateSummary struct {
373377
ActivePower PowerPair
374378
FaultyPower PowerPair
375379
RecoveringPower PowerPair
380+
DailyFee abi.TokenAmount
376381
ExpirationEpochs []abi.ChainEpoch // Epochs at which some sector is scheduled to expire.
377382
EarlyTerminationCount int
378383
}
@@ -490,11 +495,13 @@ func CheckPartitionStateInvariants(
490495

491496
// Validate the expiration queue.
492497
var expirationEpochs []abi.ChainEpoch
498+
feeDeduction := big.Zero()
493499
if expQ, err := LoadExpirationQueue(store, partition.ExpirationsEpochs, quant, PartitionExpirationAmtBitwidth); err != nil {
494500
acc.Addf("error loading expiration queue: %v", err)
495501
} else if liveSectors != nil {
496502
qsummary := CheckExpirationQueue(expQ, liveSectors, partition.Faults, quant, sectorSize, acc)
497503
expirationEpochs = qsummary.ExpirationEpochs
504+
feeDeduction = qsummary.FeeDeduction
498505

499506
// Check the queue is compatible with partition fields
500507
if qSectors, err := bitfield.MergeBitFields(qsummary.OnTimeSectors, qsummary.EarlySectors); err != nil {
@@ -525,6 +532,7 @@ func CheckPartitionStateInvariants(
525532
RecoveringPower: partition.RecoveringPower,
526533
ExpirationEpochs: expirationEpochs,
527534
EarlyTerminationCount: earlyTerminationCount,
535+
DailyFee: feeDeduction,
528536
}
529537
}
530538

@@ -534,6 +542,7 @@ type ExpirationQueueStateSummary struct {
534542
ActivePower PowerPair
535543
FaultyPower PowerPair
536544
OnTimePledge abi.TokenAmount
545+
FeeDeduction abi.TokenAmount
537546
ExpirationEpochs []abi.ChainEpoch
538547
}
539548

@@ -553,6 +562,7 @@ func CheckExpirationQueue(expQ ExpirationQueue, liveSectors map[abi.SectorNumber
553562
allActivePower := NewPowerPairZero()
554563
allFaultyPower := NewPowerPairZero()
555564
allOnTimePledge := big.Zero()
565+
allFeeDeduction := big.Zero()
556566
firstQueueEpoch := abi.ChainEpoch(-1)
557567
var exp ExpirationSet
558568
err = expQ.ForEach(&exp, func(e int64) error {
@@ -563,6 +573,7 @@ func CheckExpirationQueue(expQ ExpirationQueue, liveSectors map[abi.SectorNumber
563573
}
564574
expirationEpochs = append(expirationEpochs, epoch)
565575

576+
dailyFee := big.Zero()
566577
onTimeSectorsPledge := big.Zero()
567578
err := exp.OnTimeSectors.ForEach(func(n uint64) error {
568579
sno := abi.SectorNumber(n)
@@ -579,6 +590,7 @@ func CheckExpirationQueue(expQ ExpirationQueue, liveSectors map[abi.SectorNumber
579590
epoch, sector.SectorNumber, firstQueueEpoch, target)
580591

581592
onTimeSectorsPledge = big.Add(onTimeSectorsPledge, sector.InitialPledge)
593+
dailyFee = big.Add(dailyFee, sector.DailyFee)
582594
} else {
583595
acc.Addf("on-time expiration sector %d isn't live", n)
584596
}
@@ -600,6 +612,7 @@ func CheckExpirationQueue(expQ ExpirationQueue, liveSectors map[abi.SectorNumber
600612
target := quant.QuantizeUp(sector.Expiration)
601613
acc.Require(epoch < target, "invalid early expiration %d for sector %d, expected < %d",
602614
epoch, sector.SectorNumber, target)
615+
dailyFee = big.Add(dailyFee, sector.DailyFee)
603616
} else {
604617
acc.Addf("on-time expiration sector %d isn't live", n)
605618
}
@@ -649,12 +662,14 @@ func CheckExpirationQueue(expQ ExpirationQueue, liveSectors map[abi.SectorNumber
649662
}
650663

651664
acc.Require(exp.OnTimePledge.Equals(onTimeSectorsPledge), "on time pledge recorded %v doesn't match computed %v", exp.OnTimePledge, onTimeSectorsPledge)
665+
acc.Require(exp.FeeDeduction.Equals(dailyFee), "daily fee recorded %v doesn't match computed %v", exp.FeeDeduction, dailyFee)
652666

653667
allOnTime = append(allOnTime, exp.OnTimeSectors)
654668
allEarly = append(allEarly, exp.EarlySectors)
655669
allActivePower = allActivePower.Add(exp.ActivePower)
656670
allFaultyPower = allFaultyPower.Add(exp.FaultyPower)
657671
allOnTimePledge = big.Add(allOnTimePledge, exp.OnTimePledge)
672+
allFeeDeduction = big.Add(allFeeDeduction, exp.FeeDeduction)
658673
return nil
659674
})
660675
acc.RequireNoError(err, "error iterating expiration queue")
@@ -675,6 +690,7 @@ func CheckExpirationQueue(expQ ExpirationQueue, liveSectors map[abi.SectorNumber
675690
ActivePower: allActivePower,
676691
FaultyPower: allFaultyPower,
677692
OnTimePledge: allOnTimePledge,
693+
FeeDeduction: allFeeDeduction,
678694
ExpirationEpochs: expirationEpochs,
679695
}
680696
}

0 commit comments

Comments
 (0)