2021-06-18 18:45:29 +00:00
|
|
|
package kit
|
2021-05-25 23:04:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-01 19:07:53 +00:00
|
|
|
"encoding/json"
|
2021-05-25 23:04:13 +00:00
|
|
|
"fmt"
|
2021-07-01 19:07:53 +00:00
|
|
|
"io/ioutil"
|
2021-06-18 16:40:33 +00:00
|
|
|
"net"
|
2021-07-01 19:07:53 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-08-02 13:29:38 +00:00
|
|
|
"sort"
|
2021-06-10 11:02:08 +00:00
|
|
|
"strings"
|
2021-05-25 23:04:13 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-07-01 19:07:53 +00:00
|
|
|
"github.com/google/uuid"
|
2022-08-25 18:20:41 +00:00
|
|
|
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2021-07-01 19:07:53 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2021-06-10 11:02:08 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-05-25 23:04:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2022-06-14 17:21:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet/key"
|
2021-05-25 23:04:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/miner"
|
2022-06-14 18:25:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/paths"
|
2022-06-14 17:41:59 +00:00
|
|
|
sealing "github.com/filecoin-project/lotus/storage/pipeline"
|
2022-06-14 18:03:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2021-05-25 23:04:13 +00:00
|
|
|
)
|
|
|
|
|
2021-06-18 16:40:33 +00:00
|
|
|
type MinerSubsystem int
|
|
|
|
|
|
|
|
const (
|
2021-07-12 10:12:29 +00:00
|
|
|
SMarkets MinerSubsystem = 1 << iota
|
2021-06-18 16:40:33 +00:00
|
|
|
SMining
|
|
|
|
SSealing
|
|
|
|
SSectorStorage
|
|
|
|
|
|
|
|
MinerSubsystems = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ms MinerSubsystem) Add(single MinerSubsystem) MinerSubsystem {
|
|
|
|
return ms | single
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms MinerSubsystem) Has(single MinerSubsystem) bool {
|
|
|
|
return ms&single == single
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms MinerSubsystem) All() [MinerSubsystems]bool {
|
|
|
|
var out [MinerSubsystems]bool
|
|
|
|
|
|
|
|
for i := range out {
|
|
|
|
out[i] = ms&(1<<i) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-06-11 17:26:25 +00:00
|
|
|
// TestMiner represents a miner enrolled in an Ensemble.
|
2021-05-25 23:04:13 +00:00
|
|
|
type TestMiner struct {
|
2021-06-10 11:02:08 +00:00
|
|
|
api.StorageMiner
|
2021-05-25 23:04:13 +00:00
|
|
|
|
2022-01-21 12:33:47 +00:00
|
|
|
BaseAPI api.StorageMiner
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
t *testing.T
|
|
|
|
|
|
|
|
// ListenAddr is the address on which an API server is listening, if an
|
|
|
|
// API server is created for this Node
|
|
|
|
ListenAddr multiaddr.Multiaddr
|
|
|
|
|
|
|
|
ActorAddr address.Address
|
2022-06-14 17:21:34 +00:00
|
|
|
OwnerKey *key.Key
|
2021-05-25 23:04:13 +00:00
|
|
|
MineOne func(context.Context, miner.MineReq) error
|
|
|
|
Stop func(context.Context) error
|
|
|
|
|
2022-08-02 14:05:29 +00:00
|
|
|
FullNode *TestFullNode
|
|
|
|
PresealDir string
|
|
|
|
PresealSectors int
|
2021-05-25 23:04:13 +00:00
|
|
|
|
|
|
|
Libp2p struct {
|
|
|
|
PeerID peer.ID
|
|
|
|
PrivKey libp2pcrypto.PrivKey
|
|
|
|
}
|
|
|
|
|
2021-06-18 16:40:33 +00:00
|
|
|
RemoteListener net.Listener
|
|
|
|
|
2021-06-11 17:26:25 +00:00
|
|
|
options nodeOpts
|
2021-05-25 23:04:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 11:02:08 +00:00
|
|
|
func (tm *TestMiner) PledgeSectors(ctx context.Context, n, existing int, blockNotif <-chan struct{}) {
|
|
|
|
toCheck := tm.StartPledge(ctx, n, existing, blockNotif)
|
2022-02-08 14:29:30 +00:00
|
|
|
tm.WaitSectorsProving(ctx, toCheck)
|
|
|
|
}
|
2021-06-10 11:02:08 +00:00
|
|
|
|
2022-02-08 14:29:30 +00:00
|
|
|
func (tm *TestMiner) WaitSectorsProving(ctx context.Context, toCheck map[abi.SectorNumber]struct{}) {
|
2021-06-10 11:02:08 +00:00
|
|
|
for len(toCheck) > 0 {
|
|
|
|
tm.FlushSealingBatches(ctx)
|
|
|
|
|
|
|
|
states := map[api.SectorState]int{}
|
|
|
|
for n := range toCheck {
|
|
|
|
st, err := tm.StorageMiner.SectorsStatus(ctx, n, false)
|
|
|
|
require.NoError(tm.t, err)
|
|
|
|
states[st.State]++
|
2022-03-17 20:13:18 +00:00
|
|
|
if st.State == api.SectorState(sealing.Proving) || st.State == api.SectorState(sealing.Available) {
|
2021-06-10 11:02:08 +00:00
|
|
|
delete(toCheck, n)
|
|
|
|
}
|
|
|
|
if strings.Contains(string(st.State), "Fail") {
|
|
|
|
tm.t.Fatal("sector in a failed state", st.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
build.Clock.Sleep(100 * time.Millisecond)
|
2022-02-08 14:29:30 +00:00
|
|
|
fmt.Printf("WaitSectorsProving: %d %+v\n", len(toCheck), states)
|
2021-06-10 11:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TestMiner) StartPledge(ctx context.Context, n, existing int, blockNotif <-chan struct{}) map[abi.SectorNumber]struct{} {
|
2021-05-25 23:04:13 +00:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if i%3 == 0 && blockNotif != nil {
|
|
|
|
<-blockNotif
|
|
|
|
tm.t.Log("WAIT")
|
|
|
|
}
|
|
|
|
tm.t.Logf("PLEDGING %d", i)
|
2021-06-10 11:02:08 +00:00
|
|
|
_, err := tm.StorageMiner.PledgeSector(ctx)
|
2021-05-25 23:04:13 +00:00
|
|
|
require.NoError(tm.t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2022-08-02 13:29:38 +00:00
|
|
|
s, err := tm.SectorsListNonGenesis(ctx)
|
2021-05-25 23:04:13 +00:00
|
|
|
require.NoError(tm.t, err)
|
2022-08-02 14:05:29 +00:00
|
|
|
fmt.Printf("Sectors: %d (n %d, ex %d)\n", len(s), n, existing)
|
2021-05-25 23:04:13 +00:00
|
|
|
if len(s) >= n+existing {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
build.Clock.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("All sectors is fsm\n")
|
|
|
|
|
2022-08-02 13:29:38 +00:00
|
|
|
s, err := tm.SectorsListNonGenesis(ctx)
|
2021-05-25 23:04:13 +00:00
|
|
|
require.NoError(tm.t, err)
|
|
|
|
|
|
|
|
toCheck := map[abi.SectorNumber]struct{}{}
|
|
|
|
for _, number := range s {
|
|
|
|
toCheck[number] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-06-10 11:02:08 +00:00
|
|
|
return toCheck
|
|
|
|
}
|
2021-05-25 23:04:13 +00:00
|
|
|
|
2021-06-10 11:02:08 +00:00
|
|
|
func (tm *TestMiner) FlushSealingBatches(ctx context.Context) {
|
|
|
|
pcb, err := tm.StorageMiner.SectorPreCommitFlush(ctx)
|
|
|
|
require.NoError(tm.t, err)
|
|
|
|
if pcb != nil {
|
|
|
|
fmt.Printf("PRECOMMIT BATCH: %+v\n", pcb)
|
|
|
|
}
|
|
|
|
|
|
|
|
cb, err := tm.StorageMiner.SectorCommitFlush(ctx)
|
|
|
|
require.NoError(tm.t, err)
|
|
|
|
if cb != nil {
|
|
|
|
fmt.Printf("COMMIT BATCH: %+v\n", cb)
|
2021-05-25 23:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-01 19:07:53 +00:00
|
|
|
|
|
|
|
const metaFile = "sectorstore.json"
|
|
|
|
|
2022-07-12 10:42:08 +00:00
|
|
|
func (tm *TestMiner) AddStorage(ctx context.Context, t *testing.T, conf func(*paths.LocalStorageMeta)) storiface.ID {
|
|
|
|
p := t.TempDir()
|
2021-07-01 19:07:53 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
|
|
if !os.IsExist(err) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:42:08 +00:00
|
|
|
_, err := os.Stat(filepath.Join(p, metaFile))
|
2021-07-01 19:07:53 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-06-14 18:25:52 +00:00
|
|
|
cfg := &paths.LocalStorageMeta{
|
2022-01-18 10:57:04 +00:00
|
|
|
ID: storiface.ID(uuid.New().String()),
|
2022-07-12 10:42:08 +00:00
|
|
|
Weight: 10,
|
|
|
|
CanSeal: false,
|
|
|
|
CanStore: false,
|
2021-07-01 19:07:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 10:42:08 +00:00
|
|
|
conf(cfg)
|
|
|
|
|
2021-07-01 19:07:53 +00:00
|
|
|
if !(cfg.CanStore || cfg.CanSeal) {
|
|
|
|
t.Fatal("must specify at least one of CanStore or cfg.CanSeal")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.MarshalIndent(cfg, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(p, metaFile), b, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = tm.StorageAddLocal(ctx, p)
|
|
|
|
require.NoError(t, err)
|
2022-07-12 10:42:08 +00:00
|
|
|
|
|
|
|
return cfg.ID
|
2021-07-01 19:07:53 +00:00
|
|
|
}
|
2022-08-02 13:29:38 +00:00
|
|
|
func (tm *TestMiner) SectorsListNonGenesis(ctx context.Context) ([]abi.SectorNumber, error) {
|
|
|
|
l, err := tm.SectorsList(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// sort just in case
|
|
|
|
sort.Slice(l, func(i, j int) bool {
|
|
|
|
return l[i] < l[j]
|
|
|
|
})
|
|
|
|
|
2022-08-02 14:05:29 +00:00
|
|
|
return l[tm.PresealSectors:], nil
|
2022-08-02 13:29:38 +00:00
|
|
|
}
|