lotus/cmd/lotus-seed/seed/seed.go

274 lines
7.2 KiB
Go
Raw Normal View History

2019-11-27 14:18:51 +00:00
package seed
import (
2019-11-28 22:50:58 +00:00
"context"
2019-12-07 14:12:10 +00:00
"crypto/rand"
"encoding/hex"
2019-11-27 14:18:51 +00:00
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
2020-03-03 22:19:22 +00:00
"github.com/google/uuid"
logging "github.com/ipfs/go-log/v2"
2020-04-17 16:22:17 +00:00
ic "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/minio/blake2b-simd"
2019-11-27 14:18:51 +00:00
"golang.org/x/xerrors"
2020-07-03 01:29:06 +00:00
ffi "github.com/filecoin-project/filecoin-ffi"
"github.com/filecoin-project/go-address"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/extern/sector-storage/zerocomm"
2020-03-03 22:19:22 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/market"
2020-02-08 02:18:32 +00:00
2019-11-28 22:50:58 +00:00
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper/basicfs"
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
2020-09-30 17:32:19 +00:00
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
2019-11-27 14:18:51 +00:00
"github.com/filecoin-project/lotus/genesis"
)
2019-11-30 23:17:50 +00:00
var log = logging.Logger("preseal")
2020-07-03 01:29:06 +00:00
func PreSeal(maddr address.Address, spt abi.RegisteredSealProof, offset abi.SectorNumber, sectors int, sbroot string, preimage []byte, key *types.KeyInfo, fakeSectors bool) (*genesis.Miner, *types.KeyInfo, error) {
2020-03-17 20:19:52 +00:00
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, nil, err
}
2020-03-26 02:50:56 +00:00
cfg := &ffiwrapper.Config{
2020-03-03 22:19:22 +00:00
SealProofType: spt,
2019-11-27 14:18:51 +00:00
}
if err := os.MkdirAll(sbroot, 0775); err != nil { //nolint:gosec
return nil, nil, err
2019-11-27 14:18:51 +00:00
}
2020-03-06 05:30:47 +00:00
next := offset
2020-03-26 02:50:56 +00:00
sbfs := &basicfs.Provider{
2020-03-18 01:08:11 +00:00
Root: sbroot,
2020-03-03 22:19:22 +00:00
}
2020-03-26 02:50:56 +00:00
sb, err := ffiwrapper.New(sbfs, cfg)
2019-11-27 14:18:51 +00:00
if err != nil {
return nil, nil, err
2019-11-27 14:18:51 +00:00
}
2020-06-15 16:30:49 +00:00
ssize, err := spt.SectorSize()
2020-02-27 00:42:39 +00:00
if err != nil {
return nil, nil, err
}
2019-11-28 22:50:58 +00:00
var sealedSectors []*genesis.PreSeal
2019-11-30 23:17:50 +00:00
for i := 0; i < sectors; i++ {
2020-03-17 20:19:52 +00:00
sid := abi.SectorID{Miner: abi.ActorID(mid), Number: next}
2020-03-06 05:30:47 +00:00
next++
2019-11-27 14:18:51 +00:00
2020-07-03 01:29:06 +00:00
var preseal *genesis.PreSeal
if !fakeSectors {
preseal, err = presealSector(sb, sbfs, sid, spt, ssize, preimage)
if err != nil {
return nil, nil, err
}
} else {
preseal, err = presealSectorFake(sbfs, sid, spt, ssize)
if err != nil {
return nil, nil, err
}
2020-06-16 22:35:51 +00:00
}
sealedSectors = append(sealedSectors, preseal)
2019-11-27 14:18:51 +00:00
}
var minerAddr *wallet.Key
if key != nil {
minerAddr, err = wallet.NewKey(*key)
if err != nil {
return nil, nil, err
}
} else {
minerAddr, err = wallet.GenerateKey(types.KTBLS)
if err != nil {
return nil, nil, err
}
2019-11-28 22:50:58 +00:00
}
2020-04-17 16:22:17 +00:00
var pid peer.ID
{
log.Warn("PeerID not specified, generating dummy")
p, _, err := ic.GenerateEd25519Key(rand.Reader)
if err != nil {
return nil, nil, err
}
pid, err = peer.IDFromPrivateKey(p)
if err != nil {
return nil, nil, err
}
}
2020-02-12 00:58:55 +00:00
miner := &genesis.Miner{
ID: maddr,
2020-02-14 20:33:41 +00:00
Owner: minerAddr.Address,
Worker: minerAddr.Address,
MarketBalance: big.Zero(),
PowerBalance: big.Zero(),
SectorSize: ssize,
Sectors: sealedSectors,
2020-04-17 16:22:17 +00:00
PeerId: pid,
2019-11-28 22:50:58 +00:00
}
2020-02-12 00:58:55 +00:00
if err := createDeals(miner, minerAddr, maddr, ssize); err != nil {
return nil, nil, xerrors.Errorf("creating deals: %w", err)
2019-11-29 03:38:18 +00:00
}
2020-03-03 22:19:22 +00:00
{
2020-03-19 15:10:19 +00:00
b, err := json.MarshalIndent(&stores.LocalStorageMeta{
2020-03-13 11:59:19 +00:00
ID: stores.ID(uuid.New().String()),
Weight: 0, // read-only
CanSeal: false,
CanStore: false,
}, "", " ")
2020-03-03 22:19:22 +00:00
if err != nil {
return nil, nil, xerrors.Errorf("marshaling storage config: %w", err)
}
if err := ioutil.WriteFile(filepath.Join(sbroot, "sectorstore.json"), b, 0644); err != nil {
2020-03-03 22:19:22 +00:00
return nil, nil, xerrors.Errorf("persisting storage metadata (%s): %w", filepath.Join(sbroot, "storage.json"), err)
}
}
return miner, &minerAddr.KeyInfo, nil
2019-11-29 03:38:18 +00:00
}
func presealSector(sb *ffiwrapper.Sealer, sbfs *basicfs.Provider, sid abi.SectorID, spt abi.RegisteredSealProof, ssize abi.SectorSize, preimage []byte) (*genesis.PreSeal, error) {
pi, err := sb.AddPiece(context.TODO(), sid, nil, abi.PaddedPieceSize(ssize).Unpadded(), rand.Reader)
if err != nil {
return nil, err
}
trand := blake2b.Sum256(preimage)
ticket := abi.SealRandomness(trand[:])
fmt.Printf("sector-id: %d, piece info: %v\n", sid, pi)
in2, err := sb.SealPreCommit1(context.TODO(), sid, ticket, []abi.PieceInfo{pi})
if err != nil {
return nil, xerrors.Errorf("commit: %w", err)
}
cids, err := sb.SealPreCommit2(context.TODO(), sid, in2)
if err != nil {
return nil, xerrors.Errorf("commit: %w", err)
}
if err := sb.FinalizeSector(context.TODO(), sid, nil); err != nil {
return nil, xerrors.Errorf("trim cache: %w", err)
}
if err := cleanupUnsealed(sbfs, sid); err != nil {
return nil, xerrors.Errorf("remove unsealed file: %w", err)
}
log.Warn("PreCommitOutput: ", sid, cids.Sealed, cids.Unsealed)
return &genesis.PreSeal{
CommR: cids.Sealed,
CommD: cids.Unsealed,
SectorID: sid.Number,
ProofType: spt,
}, nil
}
2020-07-03 01:29:06 +00:00
func presealSectorFake(sbfs *basicfs.Provider, sid abi.SectorID, spt abi.RegisteredSealProof, ssize abi.SectorSize) (*genesis.PreSeal, error) {
2020-09-06 16:54:00 +00:00
paths, done, err := sbfs.AcquireSector(context.TODO(), sid, 0, storiface.FTSealed|storiface.FTCache, storiface.PathSealing)
2020-07-03 01:29:06 +00:00
if err != nil {
return nil, xerrors.Errorf("acquire unsealed sector: %w", err)
}
defer done()
if err := os.Mkdir(paths.Cache, 0755); err != nil {
return nil, xerrors.Errorf("mkdir cache: %w", err)
}
commr, err := ffi.FauxRep(spt, paths.Cache, paths.Sealed)
if err != nil {
return nil, xerrors.Errorf("fauxrep: %w", err)
}
return &genesis.PreSeal{
CommR: commr,
CommD: zerocomm.ZeroPieceCommitment(abi.PaddedPieceSize(ssize).Unpadded()),
SectorID: sid.Number,
ProofType: spt,
}, nil
}
2020-06-16 22:35:51 +00:00
func cleanupUnsealed(sbfs *basicfs.Provider, sid abi.SectorID) error {
2020-09-06 16:54:00 +00:00
paths, done, err := sbfs.AcquireSector(context.TODO(), sid, storiface.FTUnsealed, storiface.FTNone, storiface.PathSealing)
2020-06-16 22:35:51 +00:00
if err != nil {
return err
}
defer done()
return os.Remove(paths.Unsealed)
}
func WriteGenesisMiner(maddr address.Address, sbroot string, gm *genesis.Miner, key *types.KeyInfo) error {
2020-02-12 00:58:55 +00:00
output := map[string]genesis.Miner{
2019-11-29 03:38:18 +00:00
maddr.String(): *gm,
2019-11-27 14:18:51 +00:00
}
out, err := json.MarshalIndent(output, "", " ")
if err != nil {
return err
}
2020-02-21 20:56:30 +00:00
log.Infof("Writing preseal manifest to %s", filepath.Join(sbroot, "pre-seal-"+maddr.String()+".json"))
2019-11-27 14:18:51 +00:00
if err := ioutil.WriteFile(filepath.Join(sbroot, "pre-seal-"+maddr.String()+".json"), out, 0664); err != nil {
return err
}
if key != nil {
b, err := json.Marshal(key)
if err != nil {
return err
}
// TODO: allow providing key
if err := ioutil.WriteFile(filepath.Join(sbroot, "pre-seal-"+maddr.String()+".key"), []byte(hex.EncodeToString(b)), 0664); err != nil {
return err
}
}
2019-11-27 14:18:51 +00:00
return nil
}
2019-11-28 22:50:58 +00:00
2020-02-12 00:58:55 +00:00
func createDeals(m *genesis.Miner, k *wallet.Key, maddr address.Address, ssize abi.SectorSize) error {
2020-07-01 12:04:37 +00:00
for i, sector := range m.Sectors {
2020-02-08 02:18:32 +00:00
proposal := &market.DealProposal{
2020-02-27 00:42:39 +00:00
PieceCID: sector.CommD,
2020-02-08 02:18:32 +00:00
PieceSize: abi.PaddedPieceSize(ssize),
2019-11-28 22:50:58 +00:00
Client: k.Address,
Provider: maddr,
2020-07-01 12:04:37 +00:00
Label: fmt.Sprintf("%d", i),
2020-02-11 20:48:03 +00:00
StartEpoch: 0,
2020-02-08 02:18:32 +00:00
EndEpoch: 9001,
StoragePricePerEpoch: big.Zero(),
2020-02-08 02:18:37 +00:00
ProviderCollateral: big.Zero(),
ClientCollateral: big.Zero(),
2019-11-28 22:50:58 +00:00
}
2019-12-07 14:12:10 +00:00
sector.Deal = *proposal
2019-11-28 22:50:58 +00:00
}
return nil
}