Skip to content

Commit 1c564b6

Browse files
rvaggrjan90
authored andcommitted
fip-0100: constants and function to calculate daily fee (#363)
1 parent 07050cf commit 1c564b6

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

builtin/v16/miner/policy.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ const MinSectorExpiration = 180 * builtin.EpochsInDay // PARAM_SPEC
141141
// the associated seal proof's maximum lifetime.
142142
const MaxSectorExpirationExtension = 1278 * builtin.EpochsInDay // PARAM_SPEC
143143

144+
// Numerator of the fraction of circulating supply that will be used to calculate
145+
// the daily fee for new sectors.
146+
const DailyFeeCirculatingSupplyQAPMultiplierNum = 21536
147+
148+
// Denominator of the fraction of circulating supply that will be used to calculate
149+
// the daily fee for new sectors.
150+
var DailyFeeCirculatingSupplyQAPMultiplierDenom = (func() big.Int {
151+
bi, err := big.FromString("100000000000000000000000000000")
152+
if err != nil {
153+
panic(err)
154+
}
155+
return bi
156+
})()
157+
158+
// Denominator for the fraction of estimated daily block reward for the sector(s)
159+
// attracting a fee, to be used as a cap for the fees when payable.
160+
// No numerator is provided as the fee is calculated as a fraction of the estimated
161+
// daily block reward.
162+
const DailyFeeBlockRewardCapDenom = 2
163+
144164
// QualityForWeight calculates the quality of a sector with the given size, duration, and verified weight.
145165
// VerifiedDealWeight is spacetime occupied by verified pieces in a sector.
146166
// VerifiedDealWeight should be less than or equal to total SpaceTime of a sector.
@@ -196,3 +216,11 @@ func QAPowerMax(size abi.SectorSize) abi.StoragePower {
196216
big.Mul(big.NewInt(int64(size)), builtin.VerifiedDealWeightMultiplier),
197217
builtin.QualityBaseMultiplier)
198218
}
219+
220+
// DailyProofFee calculates the daily fee for a sector's quality-adjusted power based on the current
221+
// circulating supply.
222+
func DailyProofFee(circulatingSupply abi.TokenAmount, qaPower abi.StoragePower) abi.TokenAmount {
223+
numerator := big.NewInt(DailyFeeCirculatingSupplyQAPMultiplierNum)
224+
result := big.Mul(big.Mul(numerator, qaPower), circulatingSupply)
225+
return big.Div(result, DailyFeeCirculatingSupplyQAPMultiplierDenom)
226+
}

builtin/v16/miner/policy_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,33 @@ func TestQualityForWeight(t *testing.T) {
5050
}
5151
}
5252
}
53+
54+
// matches builtin-actors/actors/miner/tests/policy_test.rs
55+
func TestDailyProofFeeCalc(t *testing.T) {
56+
// Given a CS of 680M FIL, 32GiB QAP, a fee multiplier of 7.4e-15 per 32GiB QAP, the daily proof
57+
// fee should be 5032 nanoFIL.
58+
// 680M * 7.4e-15 = 0.000005032 FIL
59+
// 0.000005032 * 1e9 = 5032 nanoFIL
60+
// 0.000005032 * 1e18 = 5032000000000 attoFIL
61+
// As a per-byte multiplier we use 2.1536e-25, a close approximation of 7.4e-15 / 32GiB.
62+
// 680M * 32GiB * 2.1536e-25 = 0.000005031805013354 FIL
63+
// 0.000005031805013354 * 1e18 = 5031805013354 attoFIL
64+
circulatingSupply := big.Mul(big.NewInt(680_000_000), builtin.TokenPrecision)
65+
ref32GibFee := big.NewInt(5031805013354)
66+
67+
for _, tc := range []struct {
68+
size uint64
69+
expected big.Int
70+
}{
71+
{32, ref32GibFee},
72+
{64, big.Mul(ref32GibFee, big.NewInt(2))},
73+
{32 * 10, big.Mul(ref32GibFee, big.NewInt(10))},
74+
{32 * 5, big.Mul(ref32GibFee, big.NewInt(5))},
75+
{64 * 10, big.Mul(ref32GibFee, big.NewInt(20))},
76+
} {
77+
power := big.NewInt(int64(tc.size << 30)) // 32GiB raw QAP
78+
fee := miner.DailyProofFee(circulatingSupply, power)
79+
delta := big.Sub(fee, tc.expected).Abs()
80+
require.LessOrEqual(t, delta.Uint64(), uint64(2), "fee: %s, expected: %s", fee, tc.expected)
81+
}
82+
}

0 commit comments

Comments
 (0)