Skip to content

Commit 90fdd99

Browse files
authored
feat: fip-0098 types, methods etc. (#376)
1 parent b20c4dc commit 90fdd99

File tree

8 files changed

+373
-21
lines changed

8 files changed

+373
-21
lines changed

builtin/methods.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ var MethodsPower = struct {
175175
CurrentTotalPowerMinerRawPowerExported abi.MethodNum
176176
CurrentTotalPowerMinerCountExported abi.MethodNum
177177
CurrentTotalPowerMinerConsensusCountExported abi.MethodNum
178+
MinerPowerExported abi.MethodNum
178179
}{
179180
MethodConstructor,
180181
2,
@@ -190,6 +191,7 @@ var MethodsPower = struct {
190191
MustGenerateFRCMethodNum("MinerRawPower"),
191192
MustGenerateFRCMethodNum("MinerCount"),
192193
MustGenerateFRCMethodNum("MinerConsensusCount"),
194+
MustGenerateFRCMethodNum("MinerPower"),
193195
}
194196

195197
var MethodsMiner = struct {
@@ -241,9 +243,11 @@ var MethodsMiner = struct {
241243
GetPeerIDExported abi.MethodNum
242244
GetMultiaddrsExported abi.MethodNum
243245
// MovePartitionsExported abi.MethodNum
244-
ProveCommitSectors3 abi.MethodNum
245-
ProveReplicaUpdates3 abi.MethodNum
246-
ProveCommitSectorsNI abi.MethodNum
246+
ProveCommitSectors3 abi.MethodNum
247+
ProveReplicaUpdates3 abi.MethodNum
248+
ProveCommitSectorsNI abi.MethodNum
249+
MaxTerminationFeeExported abi.MethodNum
250+
InitialPledgeExported abi.MethodNum
247251
}{
248252
MethodConstructor,
249253
2,
@@ -296,6 +300,8 @@ var MethodsMiner = struct {
296300
34,
297301
35,
298302
36,
303+
MustGenerateFRCMethodNum("MaxTerminationFee"),
304+
MustGenerateFRCMethodNum("InitialPledge"),
299305
}
300306

301307
var MethodsVerifiedRegistry = struct {

builtin/v16/gen/gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func main() {
105105
power.MinerRawPowerReturn{},
106106
// other types
107107
power.CronEvent{},
108+
power.MinerPowerReturn{},
108109
); err != nil {
109110
panic(err)
110111
}
@@ -217,6 +218,7 @@ func main() {
217218
miner.SectorClaim{},
218219
miner.SectorNIActivationInfo{},
219220
miner.ProveCommitSectorsNIParams{},
221+
miner.MaxTerminationFeeParams{},
220222
); err != nil {
221223
panic(err)
222224
}

builtin/v16/miner/cbor_gen.go

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builtin/v16/miner/miner_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,12 @@ type ProveCommitSectorsNIParams struct {
516516
}
517517

518518
type ProveCommitSectorsNIReturn = batch.BatchReturn
519+
520+
type MaxTerminationFeeParams struct {
521+
Power abi.StoragePower
522+
InitialPledge abi.TokenAmount
523+
}
524+
525+
type MaxTerminationFeeReturn = abi.TokenAmount
526+
527+
type InitialPledgeReturn = abi.TokenAmount

builtin/v16/miner/monies.go

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010

1111
// Projection period of expected sector block reward for deposit required to pre-commit a sector.
1212
// This deposit is lost if the pre-commitment is not timely followed up by a commitment proof.
13-
var PreCommitDepositFactor = 20 // PARAM_SPEC
13+
var PreCommitDepositFactor = 20
1414
var PreCommitDepositProjectionPeriod = abi.ChainEpoch(PreCommitDepositFactor) * builtin.EpochsInDay
1515

1616
// Projection period of expected sector block rewards for storage pledge required to commit a sector.
1717
// This pledge is lost if a sector is terminated before its full committed lifetime.
18-
var InitialPledgeFactor = 20 // PARAM_SPEC
18+
var InitialPledgeFactor = 20
1919
var InitialPledgeProjectionPeriod = abi.ChainEpoch(InitialPledgeFactor) * builtin.EpochsInDay
2020

2121
// Cap on initial pledge requirement for sectors.
@@ -26,7 +26,7 @@ var InitialPledgeMaxPerByte = big.Div(big.NewInt(1e18), big.NewInt(32<<30))
2626
// Multiplier of share of circulating money supply for consensus pledge required to commit a sector.
2727
// This pledge is lost if a sector is terminated before its full committed lifetime.
2828
var InitialPledgeLockTarget = builtin.BigFrac{
29-
Numerator: big.NewInt(3), // PARAM_SPEC
29+
Numerator: big.NewInt(3),
3030
Denominator: big.NewInt(10),
3131
}
3232

@@ -133,25 +133,60 @@ func InitialPledgeForPower(
133133
return big.Min(nominalPledge, pledgeCap)
134134
}
135135

136-
var EstimatedSingleProveCommitGasUsage = big.NewInt(49299973) // PARAM_SPEC
137-
var EstimatedSinglePreCommitGasUsage = big.NewInt(16433324) // PARAM_SPEC
138-
var BatchDiscount = builtin.BigFrac{ // PARAM_SPEC
139-
Numerator: big.NewInt(1),
140-
Denominator: big.NewInt(20),
136+
// Maximum number of lifetime days penalized when a sector is terminated.
137+
const TerminationLifetimeCap abi.ChainEpoch = 140
138+
139+
// Used to compute termination fees in the base case by multiplying against initial pledge.
140+
var TermFeePledgeMultiple = builtin.BigFrac{
141+
Numerator: big.NewInt(85),
142+
Denominator: big.NewInt(1000),
141143
}
142-
var BatchBalancer = big.Mul(big.NewInt(5), builtin.OneNanoFIL) // PARAM_SPEC
143144

144-
func AggregateProveCommitNetworkFee(aggregateSize int, baseFee abi.TokenAmount) abi.TokenAmount {
145-
return aggregateNetworkFee(aggregateSize, EstimatedSingleProveCommitGasUsage, baseFee)
145+
// Used to ensure the termination fee for young sectors is not arbitrarily low.
146+
var TermFeeMinPledgeMultiple = builtin.BigFrac{
147+
Numerator: big.NewInt(2),
148+
Denominator: big.NewInt(100),
146149
}
147150

148-
func AggregatePreCommitNetworkFee(aggregateSize int, baseFee abi.TokenAmount) abi.TokenAmount {
149-
return aggregateNetworkFee(aggregateSize, EstimatedSinglePreCommitGasUsage, baseFee)
151+
// Used to compute termination fees when the termination fee of a sector is less than the fault fee for the same sector.
152+
var TermFeeMaxFaultFeeMultiple = builtin.BigFrac{
153+
Numerator: big.NewInt(105),
154+
Denominator: big.NewInt(100),
150155
}
151156

152-
func aggregateNetworkFee(aggregateSize int, gasUsage big.Int, baseFee abi.TokenAmount) abi.TokenAmount {
153-
effectiveGasFee := big.Max(baseFee, BatchBalancer)
154-
networkFeeNum := big.Product(effectiveGasFee, gasUsage, big.NewInt(int64(aggregateSize)), BatchDiscount.Numerator)
155-
networkFee := big.Div(networkFeeNum, BatchDiscount.Denominator)
156-
return networkFee
157+
const ContinuedFaultFactorNum = 351
158+
const ContinuedFaultFactorDenom = 100
159+
const ContinuedFaultProjectionPeriod abi.ChainEpoch = (builtin.EpochsInDay * ContinuedFaultFactorNum) / ContinuedFaultFactorDenom
160+
161+
// PledgePenaltyForContinuedFault calculates the penalty for a sector continuing faulty for another
162+
// proving period.
163+
// It is a projection of the expected reward earned by the sector. Also known as "FF(t)"
164+
func PledgePenaltyForContinuedFault(rewardEstimate smoothing.FilterEstimate, networkQaPowerEstimate smoothing.FilterEstimate, qaSectorPower abi.StoragePower) abi.TokenAmount {
165+
return ExpectedRewardForPower(rewardEstimate, networkQaPowerEstimate, qaSectorPower, ContinuedFaultProjectionPeriod)
166+
}
167+
168+
// PledgePenaltyForTermination Calculates termination fee for a given sector. Normally, it's
169+
// calculated as a fixed percentage of the initial pledge. However, there are some special cases
170+
// outlined in [FIP-0098](https://github.com/filecoin-project/FIPs/blob/master/FIPS/fip-0098.md).
171+
func PledgePenaltyForTermination(
172+
initialPledge abi.TokenAmount,
173+
sectorAge abi.ChainEpoch,
174+
faultFee abi.TokenAmount,
175+
) abi.TokenAmount {
176+
// Use the Percentage of the initial pledge strategy to determine the termination fee.
177+
simpleTerminationFee :=
178+
big.Div(big.Mul(initialPledge, TermFeePledgeMultiple.Numerator), TermFeePledgeMultiple.Denominator)
179+
180+
durationTerminationFee :=
181+
big.Div(big.Mul(big.NewInt(int64(sectorAge)), simpleTerminationFee), big.NewInt(int64(TerminationLifetimeCap*builtin.EpochsInDay)))
182+
183+
// Apply the age adjustment for young sectors to arrive at the base termination fee.
184+
baseTerminationFee := big.Min(simpleTerminationFee, durationTerminationFee)
185+
186+
// Calculate the minimum allowed fee (a lower bound on the termination fee) by comparing the absolute minimum termination fee value against the fault fee. Whatever result is Larger sets the lower bound for the termination fee.
187+
minimumFeeAbs := big.Div(big.Mul(initialPledge, TermFeeMinPledgeMultiple.Numerator), TermFeeMinPledgeMultiple.Denominator)
188+
minimumFeeFf := big.Div(big.Mul(faultFee, TermFeeMaxFaultFeeMultiple.Numerator), TermFeeMaxFaultFeeMultiple.Denominator)
189+
minimumFee := big.Max(minimumFeeAbs, minimumFeeFf)
190+
191+
return big.Max(baseTerminationFee, minimumFee)
157192
}

0 commit comments

Comments
 (0)