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

Commit

Permalink
Test cron calls too
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenGround0 committed Oct 8, 2021
1 parent e71aa01 commit 1e2e575
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 147 deletions.
45 changes: 5 additions & 40 deletions actors/migration/nv14/test/fault_age_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
"github.com/filecoin-project/specs-actors/v6/actors/builtin/exported"
"github.com/filecoin-project/specs-actors/v6/actors/builtin/miner"
"github.com/filecoin-project/specs-actors/v6/actors/builtin/power"
"github.com/filecoin-project/specs-actors/v6/actors/builtin/reward"
"github.com/filecoin-project/specs-actors/v6/actors/migration/nv14"
"github.com/filecoin-project/specs-actors/v6/actors/runtime/proof"
adt5 "github.com/filecoin-project/specs-actors/v6/actors/util/adt"
"github.com/filecoin-project/specs-actors/v6/actors/util/smoothing"
tutil "github.com/filecoin-project/specs-actors/v6/support/testing"
"github.com/filecoin-project/specs-actors/v6/support/vm"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -53,7 +51,6 @@ func TestEarlyTerminationInCronBeforeAndAfterMigration(t *testing.T) {
sector1Num := sectorNumbers[0]
sector2Num := sectorNumbers[1]
sector1 := sectors[0]
sector2 := sectors[1]

// Gather initial IP. This will only change when sectors are terminated
var minerState miner.State
Expand Down Expand Up @@ -105,29 +102,21 @@ func TestEarlyTerminationInCronBeforeAndAfterMigration(t *testing.T) {
vm.ApplyOk(t, v6, worker, minerAddrs.IDAddress, big.Zero(), builtin5.MethodsMiner.DeclareFaults, dfParams)

// Assemble different cron call signatures
rewardEst, qaPowerEst := getThisEpochStats(t, v6)
twoFaultedInvocs := []vm.ExpectInvocation{
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend, Value: feeForFaultedSectors(rewardEst, qaPowerEst, sectorSize, sector1, sector2)},
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.EnrollCronEvent},
}
oneFaultedInvocs := []vm.ExpectInvocation{
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend, Value: feeForFaultedSectors(rewardEst, qaPowerEst, sectorSize, sector2)},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.EnrollCronEvent},
}
firstInvocs := []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdateClaimedPower},
twoFaultedInvocs[0],
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.EnrollCronEvent},
}
sectorOneTermInvocs := []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdateClaimedPower},
twoFaultedInvocs[0],
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.EnrollCronEvent},
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend},
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdatePledgeTotal},
}
sectorTwoTermInvocs := []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.UpdateClaimedPower},
oneFaultedInvocs[0],
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.EnrollCronEvent},
{To: builtin.BurntFundsActorAddr, Method: builtin.MethodSend},
Expand All @@ -138,9 +127,7 @@ func TestEarlyTerminationInCronBeforeAndAfterMigration(t *testing.T) {
// since both sectors are faulty no posts need to be submitted
for i := 0; i < int(miner5.FaultMaxAge/miner5.WPoStProvingPeriod); i++ {
invocs := twoFaultedInvocs
if i == 0 {
invocs = firstInvocs
} else if i == int(miner5.FaultMaxAge/miner5.WPoStProvingPeriod)-1 {
if i == int(miner5.FaultMaxAge/miner5.WPoStProvingPeriod)-1 {
invocs = sectorOneTermInvocs
}
v6 = checkAndAdvanceDeadline(t, v6, minerAddrs.IDAddress, sector1Num, ip, invocs)
Expand All @@ -166,28 +153,6 @@ func TestEarlyTerminationInCronBeforeAndAfterMigration(t *testing.T) {

}

func feeForFaultedSectors(rewardEst *smoothing.FilterEstimate, qaPowerEst *smoothing.FilterEstimate, sectorSize abi.SectorSize, sectors ...*miner.SectorOnChainInfo) *big.Int {
powerForSectors := miner.NewPowerPairZero()
for _, sector := range sectors {
powerForSectors.Add(miner.PowerForSector(sectorSize, sector))
}
fee := miner.PledgePenaltyForContinuedFault(*rewardEst, *qaPowerEst, powerForSectors.QA)
return &fee
}

func getThisEpochStats(t *testing.T, v *vm.VM) (rewardEst *smoothing.FilterEstimate, qaPowerEst *smoothing.FilterEstimate) {
ret := vm.ApplyOk(t, v, builtin.SystemActorAddr, builtin.RewardActorAddr, big.Zero(), builtin.MethodsReward.ThisEpochReward, nil)
rewardRaw, ok := ret.(*reward.ThisEpochRewardReturn)
require.True(t, ok)
rewardEst = &rewardRaw.ThisEpochRewardSmoothed

ret = vm.ApplyOk(t, v, builtin.SystemActorAddr, builtin.StoragePowerActorAddr, big.Zero(), builtin.MethodsPower.CurrentTotalPower, nil)
powerRaw, ok := ret.(*power.CurrentTotalPowerReturn)
require.True(t, ok)
qaPowerEst = &powerRaw.QualityAdjPowerSmoothed
return
}

func checkAndAdvanceDeadline(t *testing.T, v *vm.VM, mAddr address.Address, sectorNum abi.SectorNumber, ipExpected abi.TokenAmount, cronSubInvocs []vm.ExpectInvocation) *vm.VM {
var minerState miner.State
err := v.GetState(mAddr, &minerState)
Expand All @@ -204,12 +169,12 @@ func checkAndAdvanceDeadline(t *testing.T, v *vm.VM, mAddr address.Address, sect
SubInvocations: []vm.ExpectInvocation{
{To: builtin.StoragePowerActorAddr, Method: builtin.MethodsPower.OnEpochTickEnd, SubInvocations: []vm.ExpectInvocation{
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.ThisEpochReward},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.UpdateNetworkKPI},
{To: mAddr, Method: builtin.MethodsMiner.OnDeferredCronEvent, SubInvocations: cronSubInvocs},
{To: builtin.RewardActorAddr, Method: builtin.MethodsReward.UpdateNetworkKPI},
}},
{To: builtin.StorageMarketActorAddr, Method: builtin.MethodsMarket.CronTick},
},
}.Matches(t, v.Invocations()[1])
}.Matches(t, v.Invocations()[0])

// get out of this deadline to advance to the next one in the next iteration
v, err = v.WithEpoch(dlInfo.Close)
Expand Down
107 changes: 0 additions & 107 deletions actors/migration/nv14/test/wip_test.go
Original file line number Diff line number Diff line change
@@ -1,116 +1,9 @@
package test_test

import (
"context"
"testing"

"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/rt"
miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
ipld2 "github.com/filecoin-project/specs-actors/v2/support/ipld"
builtin5 "github.com/filecoin-project/specs-actors/v5/actors/builtin"
miner5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/miner"
vm5 "github.com/filecoin-project/specs-actors/v5/support/vm"
"github.com/filecoin-project/specs-actors/v6/actors/builtin"
"github.com/filecoin-project/specs-actors/v6/actors/builtin/exported"
"github.com/filecoin-project/specs-actors/v6/actors/builtin/miner"
"github.com/filecoin-project/specs-actors/v6/actors/migration/nv14"
adt5 "github.com/filecoin-project/specs-actors/v6/actors/util/adt"
"github.com/filecoin-project/specs-actors/v6/support/vm"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRecoverFaultsFromBeforeAfterMigration(t *testing.T) {

ctx := context.Background()
bs := ipld2.NewSyncBlockStoreInMemory()
v := vm5.NewVMWithSingletons(ctx, t, bs)

sealProof := abi.RegisteredSealProof_StackedDrg32GiBV1_1
wPoStProof, err := sealProof.RegisteredWindowPoStProof()
require.NoError(t, err)

require.NoError(t, err)
addrs := vm5.CreateAccounts(ctx, t, v, 1, big.Mul(big.NewInt(10_000), builtin.TokenPrecision), 93837778)
owner, worker := addrs[0], addrs[0]
minerAddrs := CreateMinerV5(t, v, owner, worker, wPoStProof, big.Mul(big.NewInt(10_000), vm.FIL))

v, sectorNumbers, sectors := commitProveCronNSectorsV5(t, v, 2, worker, minerAddrs.IDAddress, sealProof)
sector1Num := sectorNumbers[0]
sector2Num := sectorNumbers[1]
sector1 := sectors[0]

// Gather initial IP. This will only change when sectors are terminated
var minerState miner.State
err = v.GetState(minerAddrs.IDAddress, &minerState)
require.NoError(t, err)
ip := minerState.InitialPledge

// Fault sector 100 by skipping in post in the next proving period
dlInfo, pIdx, v := vm5.AdvanceTillProvingDeadline(t, v, minerAddrs.IDAddress, sector1Num)
partitions := []miner.PoStPartition{{
Index: pIdx,
Skipped: bitfield.NewFromSet([]uint64{uint64(sector1Num)}),
}}
v, err = v.WithEpoch(dlInfo.Last()) // run cron on deadline end to fault
require.NoError(t, err)
sectorSize, err := sealProof.SectorSize()
require.NoError(t, err)

SubmitWindowPoStV5(t, v, worker, minerAddrs.IDAddress, dlInfo, partitions, miner.PowerForSector(sectorSize, sector1).Neg())
vm5.ApplyOk(t, v, builtin.SystemActorAddr, builtin.CronActorAddr, big.Zero(), builtin.MethodsCron.EpochTick, nil)

// Migrate
log := nv14.TestLogger{TB: t}
adtStore := adt5.WrapStore(ctx, cbor.NewCborStore(bs))
startRoot := v.StateRoot()
nextRoot, err := nv14.MigrateStateTree(ctx, adtStore, startRoot, abi.ChainEpoch(0), nv14.Config{MaxWorkers: 1}, log, nv14.NewMemMigrationCache())
require.NoError(t, err)

lookup := map[cid.Cid]rt.VMActor{}
for _, ba := range exported.BuiltinActors() {
lookup[ba.Code()] = ba
}

v6, err := vm.NewVMAtEpoch(ctx, lookup, v.Store(), nextRoot, v.GetEpoch()+1)
require.NoError(t, err)

// Fault sector 101
d, p := vm.SectorDeadline(t, v6, minerAddrs.IDAddress, sector2Num)
v6, _ = vm.AdvanceByDeadlineTillIndex(t, v6, minerAddrs.IDAddress, d+2) // move out of deadline so fault can go through
dfParams := &miner5.DeclareFaultsParams{
Faults: []miner0.FaultDeclaration{
{
Deadline: d,
Partition: p,
Sectors: bitfield.NewFromSet([]uint64{uint64(sector2Num)}),
},
},
}
vm.ApplyOk(t, v6, worker, minerAddrs.IDAddress, big.Zero(), builtin5.MethodsMiner.DeclareFaults, dfParams)

// Run for 3 epochs as faulty
for i := 0; i < 3; i++ {
v6 = checkAndAdvanceDeadline(t, v6, minerAddrs.IDAddress, sector1Num, ip)
}
err = v6.GetState(minerAddrs.IDAddress, &minerState)
require.NoError(t, err)
// one sector has been terminated
oneSectorIP := big.Div(ip, big.NewInt(2))
assert.Equal(t, oneSectorIP, minerState.InitialPledge)

secondSectorTerminationPP := int(miner.FaultMaxAge/miner.WPoStProvingPeriod) + 1 // +1 because second sector faults 1 PP after first
for i := int(miner5.FaultMaxAge / miner5.WPoStProvingPeriod); i < secondSectorTerminationPP; i++ {
v6 = checkAndAdvanceDeadline(t, v6, minerAddrs.IDAddress, sector2Num, oneSectorIP)
}
err = v6.GetState(minerAddrs.IDAddress, &minerState)
require.NoError(t, err)
// both sectors terminated
assert.Equal(t, big.Zero(), minerState.InitialPledge)

}

0 comments on commit 1e2e575

Please sign in to comment.