Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Clean up vesting reward use in miner tests. (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
anorth authored Oct 14, 2020
1 parent 6c71cf5 commit 004c444
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions actors/builtin/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var testMultiaddrs []abi.Multiaddrs

// A balance for use in tests where the miner's low balance is not interesting.
var bigBalance = big.Mul(big.NewInt(1_000_000), big.NewInt(1e18))
var onePercentBigBalance = big.Div(bigBalance, big.NewInt(100))
// A reward amount for use in tests where the vesting amount wants to be large enough to cover penalties.
var bigRewards = big.Mul(big.NewInt(1_000), big.NewInt(1e18))

// an expriration 1 greater than min expiration
const defaultSectorExpiration = 190
Expand Down Expand Up @@ -1306,7 +1307,7 @@ func TestCCUpgrade(t *testing.T) {
oldPower := miner.QAPowerForSector(actor.sectorSize, oldSector)
expectedFee := miner.PledgePenaltyForContinuedFault(actor.epochRewardSmooth, actor.epochQAPowerSmooth, oldPower)
expectedPowerDelta := miner.NewPowerPairZero()
actor.applyRewards(rt, expectedFee, big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())
actor.onDeadlineCron(rt, &cronConfig{
continuedFaultsPenalty: expectedFee,
expiredSectorsPledgeDelta: oldSector.InitialPledge.Neg(),
Expand All @@ -1322,7 +1323,7 @@ func TestCCUpgrade(t *testing.T) {
WithBalance(bigBalance, big.Zero()).
Build(t)
actor.constructAndVerify(rt)
actor.applyRewards(rt, big.NewInt(1e18), big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())

oldSector, newSector := actor.commitProveAndUpgradeSector(rt, 100, 200, defaultSectorExpiration, []abi.DealID{1})

Expand Down Expand Up @@ -1421,7 +1422,7 @@ func TestCCUpgrade(t *testing.T) {
expectedFee := miner.PledgePenaltyForTermination(oldSector.ExpectedDayReward, rt.Epoch()-oldSector.Activation,
oldSector.ExpectedStoragePledge, actor.epochQAPowerSmooth, sectorPower, actor.epochRewardSmooth,
oldSector.ReplacedDayReward, oldSector.ReplacedSectorAge)
actor.applyRewards(rt, expectedFee, big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())
powerDelta, pledgeDelta := actor.terminateSectors(rt, bf(uint64(oldSector.SectorNumber)), expectedFee)

// power and pledge should have been removed
Expand Down Expand Up @@ -1831,9 +1832,8 @@ func TestWindowPost(t *testing.T) {
infos := actor.commitAndProveSectors(rt, 1, defaultSectorExpiration, nil)
pwr := miner.PowerForSectors(actor.sectorSize, infos)

// add lots of funds so we can pay penalties without going into debt
initialLocked := big.Mul(big.NewInt(200), big.NewInt(1e18))
actor.applyRewards(rt, initialLocked, big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())
initialLocked := actor.getLockedFunds(rt)

// Submit first PoSt to ensure we are sufficiently early to add a fault
// advance to next proving period
Expand Down Expand Up @@ -1888,8 +1888,7 @@ func TestWindowPost(t *testing.T) {
actor.constructAndVerify(rt)
infos := actor.commitAndProveSectors(rt, 2, defaultSectorExpiration, nil)

// add lots of funds so we can pay penalties without going into debt
actor.applyRewards(rt, big.Mul(big.NewInt(200), big.NewInt(1e18)), big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())

// advance to epoch when submitPoSt is due
st := getState(rt)
Expand Down Expand Up @@ -1956,8 +1955,7 @@ func TestWindowPost(t *testing.T) {
actor.constructAndVerify(rt)
infos := actor.commitAndProveSectors(rt, 2, defaultSectorExpiration, nil)

// add lots of funds so we can pay penalties without going into debt
actor.applyRewards(rt, big.Mul(big.NewInt(200), big.NewInt(1e18)), big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())

// advance to epoch when submitPoSt is due
st := getState(rt)
Expand Down Expand Up @@ -1993,9 +1991,7 @@ func TestWindowPost(t *testing.T) {
actor.constructAndVerify(rt)
infos := actor.commitAndProveSectors(rt, 2, defaultSectorExpiration, nil)

// add lots of funds so we can pay penalties without going into debt
initialLocked := big.Mul(big.NewInt(200), big.NewInt(1e18))
actor.applyRewards(rt, initialLocked, big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())

// Submit first PoSt to ensure we are sufficiently early to add a fault
// advance to next proving period
Expand Down Expand Up @@ -2279,8 +2275,7 @@ func TestDeadlineCron(t *testing.T) {
allSectors := append(activeSectors, unprovenSectors...)

// add lots of funds so penalties come from vesting funds
initialLocked := big.Mul(big.NewInt(400), big.NewInt(1e18))
actor.applyRewards(rt, initialLocked, big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())

st := getState(rt)
dlIdx, pIdx, err := st.FindSector(rt.AdtStore(), activeSectors[0].SectorNumber)
Expand Down Expand Up @@ -2334,8 +2329,7 @@ func TestDeadlineCron(t *testing.T) {
actor.constructAndVerify(rt)

// add lots of funds so we can pay penalties without going into debt
initialLocked := big.Mul(big.NewInt(200), big.NewInt(1e18))
actor.applyRewards(rt, initialLocked, big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())

// create enough sectors that one will be in a different partition
allSectors := actor.commitAndProveSectors(rt, 1, defaultSectorExpiration, nil)
Expand Down Expand Up @@ -2387,8 +2381,7 @@ func TestDeclareFaults(t *testing.T) {
pwr := miner.PowerForSectors(actor.sectorSize, allSectors)

// add lots of funds so penalties come from vesting funds
initialLocked := big.Mul(big.NewInt(200), big.NewInt(1e18))
actor.applyRewards(rt, initialLocked, big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())

// find deadline for sector
st := getState(rt)
Expand Down Expand Up @@ -2877,7 +2870,7 @@ func TestTerminateSectors(t *testing.T) {

// A miner will pay the minimum of termination fee and locked funds. Add some locked funds to ensure
// correct fee calculation is used.
actor.applyRewards(rt, big.Mul(big.NewInt(1e18), big.NewInt(20000)), big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())
st := getState(rt)
initialLockedFunds := st.LockedFunds

Expand Down Expand Up @@ -2920,9 +2913,6 @@ func TestTerminateSectors(t *testing.T) {
Build(t)
actor.constructAndVerify(rt)

// Add some locked funds to ensure full termination fee appears as pledge change.
actor.applyRewards(rt, big.Mul(big.NewInt(1e18), big.NewInt(20000)), big.Zero())

// Move the current epoch forward so that the first deadline is a stable candidate for both sectors
rt.SetEpoch(periodOffset + miner.WPoStChallengeWindow)

Expand Down Expand Up @@ -2957,6 +2947,10 @@ func TestTerminateSectors(t *testing.T) {

// advance clock a little and terminate new sector
rt.SetEpoch(rt.Epoch() + 5_000)

// Add some locked funds to ensure full termination fee appears as pledge change.
actor.applyRewards(rt, bigRewards, big.Zero())

sectorPower := miner.QAPowerForSector(actor.sectorSize, newSector)
twentyDayReward := miner.ExpectedRewardForPower(actor.epochRewardSmooth, actor.epochQAPowerSmooth, sectorPower, miner.InitialPledgeProjectionPeriod)
newSectorAge := rt.Epoch() - newSector.Activation
Expand All @@ -2976,12 +2970,14 @@ func TestWithdrawBalance(t *testing.T) {
builder := builderForHarness(actor).
WithBalance(bigBalance, big.Zero())

onePercentBalance := big.Div(bigBalance, big.NewInt(100))

t.Run("happy path withdraws funds", func(t *testing.T) {
rt := builder.Build(t)
actor.constructAndVerify(rt)

// withdraw 1% of balance
actor.withdrawFunds(rt, onePercentBigBalance, onePercentBigBalance, big.Zero())
actor.withdrawFunds(rt, onePercentBalance, onePercentBalance, big.Zero())
actor.checkState(rt)
})

Expand All @@ -2993,7 +2989,7 @@ func TestWithdrawBalance(t *testing.T) {
st.FeeDebt = big.Add(rt.Balance(), abi.NewTokenAmount(1e18))
rt.ReplaceState(st)
rt.ExpectAbortContainsMessage(exitcode.ErrInsufficientFunds, "unlocked balance can not repay fee debt", func() {
actor.withdrawFunds(rt, onePercentBigBalance, onePercentBigBalance, big.Zero())
actor.withdrawFunds(rt, onePercentBalance, onePercentBalance, big.Zero())
})
actor.checkState(rt)
})
Expand All @@ -3003,7 +2999,7 @@ func TestWithdrawBalance(t *testing.T) {
actor.constructAndVerify(rt)

st := getState(rt)
feeDebt := big.Sub(bigBalance, onePercentBigBalance)
feeDebt := big.Sub(bigBalance, onePercentBalance)
st.FeeDebt = feeDebt
rt.ReplaceState(st)

Expand Down Expand Up @@ -3158,7 +3154,7 @@ func TestCompactPartitions(t *testing.T) {

// terminate sector1
rt.SetEpoch(rt.Epoch() + 100)
actor.applyRewards(rt, big.Mul(big.NewInt(1e18), big.NewInt(20000)), big.Zero())
actor.applyRewards(rt, bigRewards, big.Zero())
tsector := info[0]
sectorSize, err := tsector.SealProof.SectorSize()
require.NoError(t, err)
Expand Down Expand Up @@ -5088,12 +5084,12 @@ func (h *actorHarness) applyRewards(rt *mock.Runtime, amt, penalty abi.TokenAmou

type cronConfig struct {
expectedEnrollment abi.ChainEpoch
vestingPledgeDelta abi.TokenAmount // nolint:structcheck,unused
detectedFaultsPowerDelta *miner.PowerPair
expiredSectorsPowerDelta *miner.PowerPair
expiredSectorsPledgeDelta abi.TokenAmount
continuedFaultsPenalty abi.TokenAmount
repaidFeeDebt abi.TokenAmount
continuedFaultsPenalty abi.TokenAmount // Expected amount burnt to pay continued fault penalties.
repaidFeeDebt abi.TokenAmount // Expected amount burnt to repay fee debt.
penaltyFromUnlocked abi.TokenAmount // Expected reduction in unlocked balance from penalties exceeding vesting funds.
}

func (h *actorHarness) onDeadlineCron(rt *mock.Runtime, config *cronConfig) {
Expand Down Expand Up @@ -5143,13 +5139,16 @@ func (h *actorHarness) onDeadlineCron(rt *mock.Runtime, config *cronConfig) {
}
if !penaltyTotal.IsZero() {
rt.ExpectSend(builtin.BurntFundsActorAddr, builtin.MethodSend, nil, penaltyTotal, nil, exitcode.Ok)
// TODO this forces tests to take funds from locked funds instead of balance.
// We should make other cases possible by pushing complexity to the config
penaltyFromUnlocked := penaltyTotal
penaltyFromVesting := penaltyTotal
// Outstanding fee debt is only repaid from unlocked balance, not vesting funds.
if !config.repaidFeeDebt.NilOrZero() {
penaltyFromUnlocked = big.Sub(penaltyFromUnlocked, config.repaidFeeDebt)
penaltyFromVesting = big.Sub(penaltyFromVesting, config.repaidFeeDebt)
}
// New penalties are paid first from vesting funds but, if exhausted, overflow to unlocked balance.
if !config.penaltyFromUnlocked.NilOrZero() {
penaltyFromVesting = big.Sub(penaltyFromVesting, config.penaltyFromUnlocked)
}
pledgeDelta = big.Sub(pledgeDelta, penaltyFromUnlocked)
pledgeDelta = big.Sub(pledgeDelta, penaltyFromVesting)
}

if !config.expiredSectorsPledgeDelta.NilOrZero() {
Expand Down

0 comments on commit 004c444

Please sign in to comment.