2022-08-29 14:25:30 +00:00
|
|
|
// stm: #integration
|
2021-05-18 20:32:10 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
2024-03-04 09:55:12 +00:00
|
|
|
"bytes"
|
2021-05-18 20:32:10 +00:00
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-03-04 09:55:12 +00:00
|
|
|
"github.com/ipld/go-ipld-prime"
|
|
|
|
"github.com/ipld/go-ipld-prime/codec/dagcbor"
|
|
|
|
"github.com/ipld/go-ipld-prime/node/basicnode"
|
|
|
|
"github.com/multiformats/go-multicodec"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-05-18 20:32:10 +00:00
|
|
|
"github.com/filecoin-project/go-bitfield"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2021-05-18 20:32:10 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-06-18 18:45:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
2024-03-04 09:55:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/must"
|
2022-06-14 17:41:59 +00:00
|
|
|
sealing "github.com/filecoin-project/lotus/storage/pipeline"
|
2021-05-18 20:32:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTerminate(t *testing.T) {
|
2021-12-13 12:41:04 +00:00
|
|
|
//stm: @CHAIN_SYNCER_LOAD_GENESIS_001, @CHAIN_SYNCER_FETCH_TIPSET_001,
|
|
|
|
//stm: @CHAIN_SYNCER_START_001, @CHAIN_SYNCER_SYNC_001, @BLOCKCHAIN_BEACON_VALIDATE_BLOCK_VALUES_01
|
|
|
|
//stm: @CHAIN_SYNCER_COLLECT_CHAIN_001, @CHAIN_SYNCER_COLLECT_HEADERS_001, @CHAIN_SYNCER_VALIDATE_TIPSET_001
|
|
|
|
//stm: @CHAIN_SYNCER_NEW_PEER_HEAD_001, @CHAIN_SYNCER_VALIDATE_MESSAGE_META_001, @CHAIN_SYNCER_STOP_001
|
2021-12-14 10:33:33 +00:00
|
|
|
|
|
|
|
//stm: @CHAIN_INCOMING_HANDLE_INCOMING_BLOCKS_001, @CHAIN_INCOMING_VALIDATE_BLOCK_PUBSUB_001, @CHAIN_INCOMING_VALIDATE_MESSAGE_PUBSUB_001
|
2021-06-23 15:37:43 +00:00
|
|
|
kit.Expensive(t)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
kit.QuietMiningLogs()
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-06-22 15:34:30 +00:00
|
|
|
var (
|
|
|
|
blocktime = 2 * time.Millisecond
|
|
|
|
nSectors = 2
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-08-06 00:46:05 +00:00
|
|
|
client, miner, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.PresealSectors(nSectors))
|
2021-06-17 08:40:41 +00:00
|
|
|
ens.InterconnectAll().BeginMining(blocktime)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
maddr, err := miner.ActorAddress(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ssz, err := miner.ActorSectorSize(ctx, maddr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_MINER_POWER_001
|
2021-05-18 20:32:10 +00:00
|
|
|
p, err := client.StateMinerPower(ctx, maddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, p.MinerPower, p.TotalPower)
|
2021-06-22 15:34:30 +00:00
|
|
|
require.Equal(t, p.MinerPower.RawBytePower, types.NewInt(uint64(ssz)*uint64(nSectors)))
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-06-17 08:40:41 +00:00
|
|
|
t.Log("Seal a sector")
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-06-17 08:40:41 +00:00
|
|
|
miner.PledgeSectors(ctx, 1, 0, nil)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-06-17 08:40:41 +00:00
|
|
|
t.Log("wait for power")
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
{
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_MINER_CALCULATE_DEADLINE_001
|
2021-05-18 20:32:10 +00:00
|
|
|
// Wait until proven.
|
|
|
|
di, err := client.StateMinerProvingDeadline(ctx, maddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-06 00:46:05 +00:00
|
|
|
waitUntil := di.Open + di.WPoStProvingPeriod
|
2021-06-17 08:40:41 +00:00
|
|
|
t.Logf("End for head.Height > %d", waitUntil)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
ts := client.WaitTillChain(ctx, kit.HeightAtLeast(waitUntil))
|
2021-06-17 20:58:29 +00:00
|
|
|
t.Logf("Now head.Height = %d", ts.Height())
|
2021-05-18 20:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nSectors++
|
|
|
|
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_MINER_POWER_001
|
2021-05-18 20:32:10 +00:00
|
|
|
p, err = client.StateMinerPower(ctx, maddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, p.MinerPower, p.TotalPower)
|
2021-08-06 00:46:05 +00:00
|
|
|
require.Equal(t, types.NewInt(uint64(ssz)*uint64(nSectors)), p.MinerPower.RawBytePower)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-06-17 08:40:41 +00:00
|
|
|
t.Log("Terminate a sector")
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
toTerminate := abi.SectorNumber(3)
|
|
|
|
|
2021-12-15 14:30:42 +00:00
|
|
|
//stm: @SECTOR_TERMINATE_001
|
2021-05-18 20:32:10 +00:00
|
|
|
err = miner.SectorTerminate(ctx, toTerminate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
msgTriggerred := false
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
si, err := miner.SectorsStatus(ctx, toTerminate, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-17 08:40:41 +00:00
|
|
|
t.Log("state: ", si.State, msgTriggerred)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
switch sealing.SectorState(si.State) {
|
2021-12-15 14:30:42 +00:00
|
|
|
//stm: @SECTOR_TERMINATE_PENDING_001
|
2021-05-18 20:32:10 +00:00
|
|
|
case sealing.Terminating:
|
|
|
|
if !msgTriggerred {
|
|
|
|
{
|
|
|
|
p, err := miner.SectorTerminatePending(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, p, 1)
|
|
|
|
require.Equal(t, abi.SectorNumber(3), p[0].Number)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := miner.SectorTerminateFlush(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if c != nil {
|
|
|
|
msgTriggerred = true
|
2021-06-17 08:40:41 +00:00
|
|
|
t.Log("terminate message:", c)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
p, err := miner.SectorTerminatePending(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, p, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case sealing.TerminateWait, sealing.TerminateFinality, sealing.Removed:
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2021-06-22 15:34:30 +00:00
|
|
|
// need to wait for message to be mined and applied.
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_MINER_POWER_001
|
2021-05-18 20:32:10 +00:00
|
|
|
// check power decreased
|
|
|
|
p, err = client.StateMinerPower(ctx, maddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, p.MinerPower, p.TotalPower)
|
2021-06-22 15:34:30 +00:00
|
|
|
require.Equal(t, types.NewInt(uint64(ssz)*uint64(nSectors-1)), p.MinerPower.RawBytePower)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
// check in terminated set
|
|
|
|
{
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_MINER_GET_PARTITIONS_001
|
2021-05-18 20:32:10 +00:00
|
|
|
parts, err := client.StateMinerPartitions(ctx, maddr, 1, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Greater(t, len(parts), 0)
|
|
|
|
|
|
|
|
bflen := func(b bitfield.BitField) uint64 {
|
|
|
|
l, err := b.Count()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, uint64(1), bflen(parts[0].AllSectors))
|
|
|
|
require.Equal(t, uint64(0), bflen(parts[0].LiveSectors))
|
|
|
|
}
|
|
|
|
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_MINER_CALCULATE_DEADLINE_001
|
2021-05-18 20:32:10 +00:00
|
|
|
di, err := client.StateMinerProvingDeadline(ctx, maddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-22 15:34:30 +00:00
|
|
|
waitUntil := di.PeriodStart + di.WPoStProvingPeriod + 20 // slack like above
|
2021-06-17 08:40:41 +00:00
|
|
|
t.Logf("End for head.Height > %d", waitUntil)
|
2021-06-18 18:45:29 +00:00
|
|
|
ts := client.WaitTillChain(ctx, kit.HeightAtLeast(waitUntil))
|
2021-06-17 20:58:29 +00:00
|
|
|
t.Logf("Now head.Height = %d", ts.Height())
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_MINER_POWER_001
|
2021-05-18 20:32:10 +00:00
|
|
|
p, err = client.StateMinerPower(ctx, maddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, p.MinerPower, p.TotalPower)
|
2021-06-22 15:34:30 +00:00
|
|
|
require.Equal(t, types.NewInt(uint64(ssz)*uint64(nSectors-1)), p.MinerPower.RawBytePower)
|
2024-03-04 09:55:12 +00:00
|
|
|
|
|
|
|
// check "sector-terminated" actor event
|
|
|
|
var epochZero abi.ChainEpoch
|
2024-03-19 00:37:46 +00:00
|
|
|
allEvents, err := miner.FullNode.GetActorEventsRaw(ctx, &types.ActorEventFilter{
|
2024-03-04 09:55:12 +00:00
|
|
|
FromHeight: &epochZero,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, key := range []string{"sector-precommitted", "sector-activated", "sector-terminated"} {
|
|
|
|
var found bool
|
|
|
|
keyBytes := must.One(ipld.Encode(basicnode.NewString(key), dagcbor.Encode))
|
|
|
|
for _, event := range allEvents {
|
|
|
|
for _, e := range event.Entries {
|
|
|
|
if e.Key == "$type" && bytes.Equal(e.Value, keyBytes) {
|
|
|
|
found = true
|
|
|
|
if key == "sector-terminated" {
|
|
|
|
expectedEntries := []types.EventEntry{
|
|
|
|
{Flags: 0x03, Codec: uint64(multicodec.Cbor), Key: "$type", Value: keyBytes},
|
|
|
|
{Flags: 0x03, Codec: uint64(multicodec.Cbor), Key: "sector", Value: must.One(ipld.Encode(basicnode.NewInt(int64(toTerminate)), dagcbor.Encode))},
|
|
|
|
}
|
|
|
|
require.ElementsMatch(t, expectedEntries, event.Entries)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.True(t, found, "expected to find event %s", key)
|
|
|
|
}
|
2021-05-18 20:32:10 +00:00
|
|
|
}
|