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"
|
2019-11-27 14:18:51 +00:00
|
|
|
"crypto/sha256"
|
2020-02-12 21:41:59 +00:00
|
|
|
"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"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-11-27 14:18:51 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-03-03 22:19:22 +00:00
|
|
|
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
|
|
|
|
"github.com/filecoin-project/go-sectorbuilder/fs"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/market"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
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"
|
2019-11-27 14:18:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/genesis"
|
2020-03-03 22:19:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2019-11-27 14:18:51 +00:00
|
|
|
)
|
|
|
|
|
2019-11-30 23:17:50 +00:00
|
|
|
var log = logging.Logger("preseal")
|
|
|
|
|
2020-02-27 00:42:39 +00:00
|
|
|
func PreSeal(maddr address.Address, pt abi.RegisteredProof, offset abi.SectorNumber, sectors int, sbroot string, preimage []byte, key *types.KeyInfo) (*genesis.Miner, *types.KeyInfo, error) {
|
|
|
|
ppt, err := pt.RegisteredPoStProof()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
spt, err := pt.RegisteredSealProof()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:18:51 +00:00
|
|
|
cfg := §orbuilder.Config{
|
2020-03-03 22:19:22 +00:00
|
|
|
Miner: maddr,
|
|
|
|
SealProofType: spt,
|
|
|
|
PoStProofType: ppt,
|
2019-11-27 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 13:49:58 +00:00
|
|
|
if err := os.MkdirAll(sbroot, 0775); err != nil {
|
2020-02-12 21:41:59 +00:00
|
|
|
return nil, nil, err
|
2019-11-27 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 22:19:22 +00:00
|
|
|
sbfs := &fs.Basic{
|
|
|
|
Miner: maddr,
|
|
|
|
NextID: offset,
|
|
|
|
Root: sbroot,
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := sectorbuilder.New(sbfs, cfg)
|
2019-11-27 14:18:51 +00:00
|
|
|
if err != nil {
|
2020-02-12 21:41:59 +00:00
|
|
|
return nil, nil, err
|
2019-11-27 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 00:42:39 +00:00
|
|
|
ssize, err := pt.SectorSize()
|
|
|
|
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-03 22:19:22 +00:00
|
|
|
sid, err := sbfs.AcquireSectorNumber()
|
2019-11-27 14:18:51 +00:00
|
|
|
if err != nil {
|
2020-02-12 21:41:59 +00:00
|
|
|
return nil, nil, err
|
2019-11-27 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
pi, err := sb.AddPiece(context.TODO(), abi.PaddedPieceSize(ssize).Unpadded(), sid, rand.Reader, nil)
|
2019-11-27 14:18:51 +00:00
|
|
|
if err != nil {
|
2020-02-12 21:41:59 +00:00
|
|
|
return nil, nil, err
|
2019-11-27 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trand := sha256.Sum256(preimage)
|
2020-02-27 00:42:39 +00:00
|
|
|
ticket := abi.SealRandomness(trand[:])
|
2019-11-27 14:18:51 +00:00
|
|
|
|
2020-02-21 16:57:40 +00:00
|
|
|
fmt.Printf("sector-id: %d, piece info: %v\n", sid, pi)
|
2019-11-27 14:18:51 +00:00
|
|
|
|
2020-03-03 22:19:22 +00:00
|
|
|
in2, err := sb.SealPreCommit1(context.TODO(), sid, ticket, []abi.PieceInfo{pi})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("commit: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scid, ucid, err := sb.SealPreCommit2(context.TODO(), sid, in2)
|
2019-11-27 14:18:51 +00:00
|
|
|
if err != nil {
|
2020-02-12 21:41:59 +00:00
|
|
|
return nil, nil, xerrors.Errorf("commit: %w", err)
|
2019-11-27 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 22:19:22 +00:00
|
|
|
if err := sb.FinalizeSector(context.TODO(), sid); err != nil {
|
2020-02-12 21:41:59 +00:00
|
|
|
return nil, nil, xerrors.Errorf("trim cache: %w", err)
|
2019-12-06 21:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 00:42:39 +00:00
|
|
|
log.Warn("PreCommitOutput: ", sid, scid, ucid)
|
2019-11-28 22:50:58 +00:00
|
|
|
sealedSectors = append(sealedSectors, &genesis.PreSeal{
|
2020-02-28 18:01:43 +00:00
|
|
|
CommR: scid,
|
|
|
|
CommD: ucid,
|
|
|
|
SectorID: sid,
|
|
|
|
ProofType: pt,
|
2019-11-27 14:18:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
var minerAddr *wallet.Key
|
|
|
|
if key != nil {
|
|
|
|
minerAddr, err = wallet.NewKey(*key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-12 23:52:36 +00:00
|
|
|
minerAddr, err = wallet.GenerateKey(crypto.SigTypeBLS)
|
2020-02-12 21:41:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-28 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 00:58:55 +00:00
|
|
|
miner := &genesis.Miner{
|
2020-02-14 20:33:41 +00:00
|
|
|
Owner: minerAddr.Address,
|
|
|
|
Worker: minerAddr.Address,
|
|
|
|
MarketBalance: big.Zero(),
|
|
|
|
PowerBalance: big.Zero(),
|
|
|
|
SectorSize: ssize,
|
|
|
|
Sectors: sealedSectors,
|
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 {
|
2020-02-12 21:41:59 +00:00
|
|
|
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-04 02:24:08 +00:00
|
|
|
b, err := json.MarshalIndent(&config.StorageMeta{
|
2020-03-03 22:19:22 +00:00
|
|
|
ID: uuid.New().String(),
|
|
|
|
Weight: 0, // read-only
|
|
|
|
CanCommit: false,
|
|
|
|
CanStore: false,
|
2020-03-04 02:24:08 +00:00
|
|
|
}, "", " ")
|
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, "storage.json"), b, 0644); err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("persisting storage metadata (%s): %w", filepath.Join(sbroot, "storage.json"), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
return miner, &minerAddr.KeyInfo, nil
|
2019-11-29 03:38:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
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 {
|
2019-11-28 22:50:58 +00:00
|
|
|
for _, 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,
|
2019-12-01 20:07:42 +00:00
|
|
|
Provider: maddr,
|
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
|
|
|
|
}
|