lotus/api/test/window_post.go

242 lines
5.5 KiB
Go
Raw Normal View History

2020-06-01 12:49:48 +00:00
package test
import (
"context"
"fmt"
2020-06-01 12:59:51 +00:00
"github.com/filecoin-project/lotus/api"
2020-07-10 14:43:14 +00:00
"github.com/filecoin-project/lotus/build"
2020-06-01 12:49:48 +00:00
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/specs-actors/actors/abi"
miner2 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
sealing "github.com/filecoin-project/storage-fsm"
"github.com/filecoin-project/lotus/chain/types"
2020-07-28 16:16:46 +00:00
bminer "github.com/filecoin-project/lotus/miner"
2020-06-01 12:49:48 +00:00
"github.com/filecoin-project/lotus/node/impl"
)
func TestPledgeSector(t *testing.T, b APIBuilder, blocktime time.Duration, nSectors int) {
os.Setenv("BELLMAN_NO_GPU", "1")
ctx := context.Background()
n, sn := b(t, 1, oneMiner)
client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
addrinfo, err := client.NetAddrsListen(ctx)
if err != nil {
t.Fatal(err)
}
if err := miner.NetConnect(ctx, addrinfo); err != nil {
t.Fatal(err)
}
2020-07-10 14:43:14 +00:00
build.Clock.Sleep(time.Second)
2020-06-01 12:49:48 +00:00
mine := true
done := make(chan struct{})
blockNotif := make(chan struct{}, 1)
2020-06-01 12:49:48 +00:00
go func() {
defer close(done)
for mine {
2020-07-10 14:43:14 +00:00
build.Clock.Sleep(blocktime)
2020-07-28 16:16:46 +00:00
if err := sn[0].MineOne(ctx, bminer.MineReq{Done: func(bool, error) {
select {
case blockNotif <- struct{}{}:
default:
}
2020-07-28 16:16:46 +00:00
}}); err != nil {
2020-06-01 12:49:48 +00:00
t.Error(err)
}
}
}()
pledgeSectors(t, ctx, miner, nSectors, blockNotif)
2020-06-01 12:49:48 +00:00
mine = false
<-done
}
func pledgeSectors(t *testing.T, ctx context.Context, miner TestStorageNode, n int, blockNotif <-chan struct{}) {
2020-06-01 12:49:48 +00:00
for i := 0; i < n; i++ {
err := miner.PledgeSector(ctx)
require.NoError(t, err)
if i%3 == 0 && blockNotif != nil {
<-blockNotif
}
2020-06-01 12:49:48 +00:00
}
for {
s, err := miner.SectorsList(ctx) // Note - the test builder doesn't import genesis sectors into FSM
require.NoError(t, err)
fmt.Printf("Sectors: %d\n", len(s))
if len(s) >= n {
break
}
2020-07-10 14:43:14 +00:00
build.Clock.Sleep(100 * time.Millisecond)
2020-06-01 12:49:48 +00:00
}
fmt.Printf("All sectors is fsm\n")
s, err := miner.SectorsList(ctx)
require.NoError(t, err)
toCheck := map[abi.SectorNumber]struct{}{}
for _, number := range s {
toCheck[number] = struct{}{}
}
for len(toCheck) > 0 {
for n := range toCheck {
st, err := miner.SectorsStatus(ctx, n, false)
2020-06-01 12:49:48 +00:00
require.NoError(t, err)
2020-06-01 12:59:51 +00:00
if st.State == api.SectorState(sealing.Proving) {
2020-06-01 12:49:48 +00:00
delete(toCheck, n)
}
if strings.Contains(string(st.State), "Fail") {
t.Fatal("sector in a failed state", st.State)
}
}
2020-07-10 14:43:14 +00:00
build.Clock.Sleep(100 * time.Millisecond)
2020-06-01 12:49:48 +00:00
fmt.Printf("WaitSeal: %d\n", len(s))
}
}
func TestWindowPost(t *testing.T, b APIBuilder, blocktime time.Duration, nSectors int) {
os.Setenv("BELLMAN_NO_GPU", "1")
ctx := context.Background()
n, sn := b(t, 1, oneMiner)
client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
addrinfo, err := client.NetAddrsListen(ctx)
if err != nil {
t.Fatal(err)
}
if err := miner.NetConnect(ctx, addrinfo); err != nil {
t.Fatal(err)
}
2020-07-10 14:43:14 +00:00
build.Clock.Sleep(time.Second)
2020-06-01 12:49:48 +00:00
mine := true
done := make(chan struct{})
go func() {
defer close(done)
for mine {
2020-07-10 14:43:14 +00:00
build.Clock.Sleep(blocktime)
2020-07-28 16:16:46 +00:00
if err := sn[0].MineOne(ctx, MineNext); err != nil {
2020-06-01 12:49:48 +00:00
t.Error(err)
}
}
}()
pledgeSectors(t, ctx, miner, nSectors, nil)
2020-06-01 12:49:48 +00:00
maddr, err := miner.ActorAddress(ctx)
require.NoError(t, err)
di, err := client.StateMinerProvingDeadline(ctx, maddr, types.EmptyTSK)
require.NoError(t, err)
fmt.Printf("Running one proving periods\n")
for {
head, err := client.ChainHead(ctx)
require.NoError(t, err)
if head.Height() > di.PeriodStart+(miner2.WPoStProvingPeriod)+2 {
2020-06-01 12:49:48 +00:00
break
}
if head.Height()%100 == 0 {
2020-06-01 12:49:48 +00:00
fmt.Printf("@%d\n", head.Height())
}
2020-07-10 14:43:14 +00:00
build.Clock.Sleep(blocktime)
2020-06-01 12:49:48 +00:00
}
p, err := client.StateMinerPower(ctx, maddr, types.EmptyTSK)
require.NoError(t, err)
ssz, err := miner.ActorSectorSize(ctx, maddr)
require.NoError(t, err)
require.Equal(t, p.MinerPower, p.TotalPower)
require.Equal(t, p.MinerPower.RawBytePower, types.NewInt(uint64(ssz)*uint64(nSectors+GenesisPreseals)))
2020-06-01 12:49:48 +00:00
2020-07-23 17:23:21 +00:00
// Drop 2 sectors from deadline 2 partition 0 (full partition / deadline)
{
parts, err := client.StateMinerPartitions(ctx, maddr, 2, types.EmptyTSK)
require.NoError(t, err)
require.Greater(t, len(parts), 0)
n, err := parts[0].Sectors.Count()
require.NoError(t, err)
require.Equal(t, uint64(2), n)
// Drop the partition
err = parts[0].Sectors.ForEach(func(sid uint64) error {
return miner.SectorRemove(ctx, abi.SectorNumber(sid))
})
require.NoError(t, err)
}
// Drop 1 sectors from deadline 3 partition 0
{
parts, err := client.StateMinerPartitions(ctx, maddr, 3, types.EmptyTSK)
require.NoError(t, err)
require.Greater(t, len(parts), 0)
n, err := parts[0].Sectors.Count()
require.NoError(t, err)
require.Equal(t, uint64(2), n)
// Drop the sector
s, err := parts[0].Sectors.First()
require.NoError(t, err)
err = miner.SectorRemove(ctx, abi.SectorNumber(s))
require.NoError(t, err)
}
di, err = client.StateMinerProvingDeadline(ctx, maddr, types.EmptyTSK)
require.NoError(t, err)
for {
head, err := client.ChainHead(ctx)
require.NoError(t, err)
if head.Height() > di.PeriodStart+(miner2.WPoStProvingPeriod)+2 {
break
}
if head.Height()%100 == 0 {
fmt.Printf("@%d\n", head.Height())
}
build.Clock.Sleep(blocktime)
}
p, err = client.StateMinerPower(ctx, maddr, types.EmptyTSK)
require.NoError(t, err)
require.Equal(t, p.MinerPower, p.TotalPower)
sectors := p.MinerPower.RawBytePower.Uint64() / uint64(ssz)
2020-07-27 11:23:55 +00:00
require.Equal(t, nSectors+GenesisPreseals-3, int(sectors)) // -3 just removed sectors
2020-06-01 12:49:48 +00:00
mine = false
<-done
}