2022-09-05 13:28:36 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-09-09 08:32:27 +00:00
|
|
|
"encoding/json"
|
2022-09-05 13:28:36 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
lminer "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
|
|
|
spaths "github.com/filecoin-project/lotus/storage/paths"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/ffiwrapper"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/ffiwrapper/basicfs"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/tarutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSectorImportAfterPC2(t *testing.T) {
|
|
|
|
kit.QuietMiningLogs()
|
|
|
|
|
|
|
|
var blockTime = 50 * time.Millisecond
|
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
////////
|
|
|
|
// Start a miner node
|
|
|
|
|
2022-09-05 13:28:36 +00:00
|
|
|
client, miner, ens := kit.EnsembleMinimal(t, kit.ThroughRPC())
|
|
|
|
ens.InterconnectAll().BeginMining(blockTime)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
////////
|
|
|
|
// Reserve some sector numbers on the miner node; We'll use one of those when creating the sector "remotely"
|
2022-09-05 13:28:36 +00:00
|
|
|
snums, err := miner.SectorNumReserveCount(ctx, "test-reservation-0001", 16)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sectorDir := t.TempDir()
|
|
|
|
|
|
|
|
maddr, err := miner.ActorAddress(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mid, err := address.IDFromAddress(maddr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-09 11:42:04 +00:00
|
|
|
mi, err := client.StateMinerInfo(ctx, maddr, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
ver, err := client.StateNetworkVersion(ctx, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
spt, err := lminer.PreferredSealProofTypeFromWindowPoStType(ver, mi.WindowPoStProofType)
|
2022-09-05 13:28:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ssize, err := spt.SectorSize()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pieceSize := abi.PaddedPieceSize(ssize)
|
|
|
|
|
|
|
|
////////
|
2022-09-09 09:11:47 +00:00
|
|
|
// Create/Seal a sector up to pc2 outside of the pipeline
|
2022-09-05 13:28:36 +00:00
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
// get one sector number from the reservation done on the miner above
|
2022-09-05 13:28:36 +00:00
|
|
|
sn, err := snums.First()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
// create all the sector identifiers
|
|
|
|
snum := abi.SectorNumber(sn)
|
|
|
|
sid := abi.SectorID{Miner: abi.ActorID(mid), Number: snum}
|
|
|
|
sref := storiface.SectorRef{ID: sid, ProofType: spt}
|
2022-09-05 13:28:36 +00:00
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
// create a low-level sealer instance
|
2022-09-05 13:28:36 +00:00
|
|
|
sealer, err := ffiwrapper.New(&basicfs.Provider{
|
|
|
|
Root: sectorDir,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-16 21:45:23 +00:00
|
|
|
// CREATE THE UNSEALED FILE
|
2022-09-09 09:11:47 +00:00
|
|
|
|
|
|
|
// create a reader for all-zero (CC) data
|
2022-09-05 13:28:36 +00:00
|
|
|
dataReader := bytes.NewReader(bytes.Repeat([]byte{0}, int(pieceSize.Unpadded())))
|
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
// create the unsealed CC sector file
|
2022-09-05 13:28:36 +00:00
|
|
|
pieceInfo, err := sealer.AddPiece(ctx, sref, nil, pieceSize.Unpadded(), dataReader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
// GENERATE THE TICKET
|
|
|
|
|
2022-09-05 13:28:36 +00:00
|
|
|
// get most recent valid ticket epoch
|
|
|
|
ts, err := client.ChainHead(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
ticketEpoch := ts.Height() - policy.SealRandomnessLookback
|
|
|
|
|
|
|
|
// ticket entropy is cbor-seriasized miner addr
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
require.NoError(t, maddr.MarshalCBOR(buf))
|
|
|
|
|
|
|
|
// generate ticket randomness
|
|
|
|
rand, err := client.StateGetRandomnessFromTickets(ctx, crypto.DomainSeparationTag_SealRandomness, ticketEpoch, buf.Bytes(), ts.Key())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
// EXECUTE PRECOMMIT 1 / 2
|
|
|
|
|
2022-09-05 13:28:36 +00:00
|
|
|
// run PC1
|
|
|
|
pc1out, err := sealer.SealPreCommit1(ctx, sref, abi.SealRandomness(rand), []abi.PieceInfo{pieceInfo})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// run pc2
|
|
|
|
scids, err := sealer.SealPreCommit2(ctx, sref, pc1out)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
// make finalized cache, put it in [sectorDir]/fin-cache while keeping the large cache for remote C1
|
2022-09-06 15:09:27 +00:00
|
|
|
finDst := filepath.Join(sectorDir, "fin-cache", fmt.Sprintf("s-t01000-%d", snum))
|
|
|
|
require.NoError(t, os.MkdirAll(finDst, 0777))
|
|
|
|
require.NoError(t, sealer.FinalizeSectorInto(ctx, sref, finDst))
|
2022-09-05 13:28:36 +00:00
|
|
|
|
|
|
|
////////
|
|
|
|
// start http server serving sector data
|
|
|
|
|
2022-09-09 12:38:23 +00:00
|
|
|
doneResp := new(*api.RemoteSealingDoneParams)
|
|
|
|
|
2022-09-05 13:28:36 +00:00
|
|
|
m := mux.NewRouter()
|
|
|
|
m.HandleFunc("/sectors/{type}/{id}", remoteGetSector(sectorDir)).Methods("GET")
|
2022-09-09 08:32:27 +00:00
|
|
|
m.HandleFunc("/sectors/{id}/commit1", remoteCommit1(sealer)).Methods("POST")
|
2022-09-09 12:38:23 +00:00
|
|
|
m.HandleFunc("/sectors/{id}/sealed", remoteDone(doneResp)).Methods("POST")
|
2022-09-09 10:54:48 +00:00
|
|
|
m.HandleFunc("/commit2", remoteCommit2(sealer)).Methods("POST")
|
2022-09-09 12:38:23 +00:00
|
|
|
|
2022-09-05 13:28:36 +00:00
|
|
|
srv := httptest.NewServer(m)
|
|
|
|
|
2022-09-09 09:11:47 +00:00
|
|
|
unsealedURL := fmt.Sprintf("%s/sectors/unsealed/s-t0%d-%d", srv.URL, mid, snum)
|
|
|
|
sealedURL := fmt.Sprintf("%s/sectors/sealed/s-t0%d-%d", srv.URL, mid, snum)
|
|
|
|
cacheURL := fmt.Sprintf("%s/sectors/cache/s-t0%d-%d", srv.URL, mid, snum)
|
|
|
|
remoteC1URL := fmt.Sprintf("%s/sectors/s-t0%d-%d/commit1", srv.URL, mid, snum)
|
2022-09-09 10:54:48 +00:00
|
|
|
remoteC2URL := fmt.Sprintf("%s/commit2", srv.URL)
|
2022-09-09 12:38:23 +00:00
|
|
|
doneURL := fmt.Sprintf("%s/sectors/s-t0%d-%d/sealed", srv.URL, mid, snum)
|
2022-09-09 09:11:47 +00:00
|
|
|
|
2022-09-05 13:28:36 +00:00
|
|
|
////////
|
|
|
|
// import the sector and continue sealing
|
|
|
|
|
|
|
|
err = miner.SectorReceive(ctx, api.RemoteSectorMeta{
|
|
|
|
State: "PreCommitting",
|
|
|
|
Sector: sid,
|
|
|
|
Type: spt,
|
|
|
|
|
|
|
|
Pieces: []api.SectorPiece{
|
|
|
|
{
|
|
|
|
Piece: pieceInfo,
|
|
|
|
DealInfo: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
TicketValue: abi.SealRandomness(rand),
|
|
|
|
TicketEpoch: ticketEpoch,
|
|
|
|
|
|
|
|
PreCommit1Out: pc1out,
|
|
|
|
|
|
|
|
CommD: &scids.Unsealed,
|
|
|
|
CommR: &scids.Sealed,
|
|
|
|
|
2022-09-16 21:45:23 +00:00
|
|
|
DataUnsealed: &storiface.SectorLocation{
|
2022-09-05 13:28:36 +00:00
|
|
|
Local: false,
|
2022-09-09 09:11:47 +00:00
|
|
|
URL: unsealedURL,
|
2022-09-05 13:28:36 +00:00
|
|
|
},
|
2022-09-16 21:45:23 +00:00
|
|
|
DataSealed: &storiface.SectorLocation{
|
2022-09-05 13:28:36 +00:00
|
|
|
Local: false,
|
2022-09-09 09:11:47 +00:00
|
|
|
URL: sealedURL,
|
2022-09-05 13:28:36 +00:00
|
|
|
},
|
2022-09-16 21:45:23 +00:00
|
|
|
DataCache: &storiface.SectorLocation{
|
2022-09-05 13:28:36 +00:00
|
|
|
Local: false,
|
2022-09-09 09:11:47 +00:00
|
|
|
URL: cacheURL,
|
2022-09-05 13:28:36 +00:00
|
|
|
},
|
2022-09-09 08:32:27 +00:00
|
|
|
|
2022-09-09 12:38:23 +00:00
|
|
|
RemoteCommit1Endpoint: remoteC1URL,
|
|
|
|
RemoteCommit2Endpoint: remoteC2URL,
|
|
|
|
RemoteSealingDoneEndpoint: doneURL,
|
2022-09-05 13:28:36 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check that we see the imported sector
|
|
|
|
ng, err := miner.SectorsListNonGenesis(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, ng, 1)
|
|
|
|
require.Equal(t, snum, ng[0])
|
|
|
|
|
2022-09-06 09:06:30 +00:00
|
|
|
miner.WaitSectorsProving(ctx, map[abi.SectorNumber]struct{}{snum: {}})
|
2022-09-09 12:38:23 +00:00
|
|
|
|
|
|
|
require.NotNil(t, *doneResp)
|
|
|
|
require.True(t, (*doneResp).Successful)
|
|
|
|
require.Equal(t, "Proving", (*doneResp).State)
|
|
|
|
require.NotNil(t, (*doneResp).CommitMessage)
|
2022-09-05 13:28:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 08:32:27 +00:00
|
|
|
func remoteCommit1(s *ffiwrapper.Sealer) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
// validate sector id
|
|
|
|
id, err := storiface.ParseSectorID(vars["id"])
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var params api.RemoteCommit1Params
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sref := storiface.SectorRef{
|
|
|
|
ID: id,
|
|
|
|
ProofType: params.ProofType,
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize, err := params.ProofType.SectorSize()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := s.SealCommit1(r.Context(), sref, params.Ticket, params.Seed, []abi.PieceInfo{
|
|
|
|
{
|
|
|
|
Size: abi.PaddedPieceSize(ssize),
|
|
|
|
PieceCID: params.Unsealed,
|
|
|
|
},
|
|
|
|
}, storiface.SectorCids{
|
|
|
|
Unsealed: params.Unsealed,
|
|
|
|
Sealed: params.Sealed,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(p); err != nil {
|
|
|
|
fmt.Println("c1 write error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 12:38:23 +00:00
|
|
|
func remoteDone(rs **api.RemoteSealingDoneParams) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
*rs = new(api.RemoteSealingDoneParams)
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(*rs); err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:54:48 +00:00
|
|
|
func remoteCommit2(s *ffiwrapper.Sealer) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var params api.RemoteCommit2Params
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sref := storiface.SectorRef{
|
|
|
|
ID: params.Sector,
|
|
|
|
ProofType: params.ProofType,
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := s.SealCommit2(r.Context(), sref, params.Commit1Out)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("c2 error: ", err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(p); err != nil {
|
|
|
|
fmt.Println("c2 write error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 13:28:36 +00:00
|
|
|
func remoteGetSector(sectorRoot string) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
// validate sector id
|
|
|
|
id, err := storiface.ParseSectorID(vars["id"])
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate type
|
|
|
|
_, err = spaths.FileTypeFromString(vars["type"])
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:09:27 +00:00
|
|
|
typ := vars["type"]
|
|
|
|
if typ == "cache" {
|
|
|
|
// if cache is requested, send the finalized cache we've created above
|
|
|
|
typ = "fin-cache"
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(sectorRoot, typ, vars["id"])
|
2022-09-05 13:28:36 +00:00
|
|
|
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if stat.IsDir() {
|
|
|
|
if _, has := r.Header["Range"]; has {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/x-tar")
|
|
|
|
w.WriteHeader(200)
|
|
|
|
|
|
|
|
err := tarutil.TarDirectory(path, w, make([]byte, 1<<20))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
// will do a ranged read over the file at the given path if the caller has asked for a ranged read in the request headers.
|
|
|
|
http.ServeFile(w, r, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("served sector file/dir, sectorID=%+v, fileType=%s, path=%s\n", id, vars["type"], path)
|
|
|
|
}
|
|
|
|
}
|