diff --git a/cmd/lotus-storage-miner/init.go b/cmd/lotus-storage-miner/init.go index a14090f59..3408bce17 100644 --- a/cmd/lotus-storage-miner/init.go +++ b/cmd/lotus-storage-miner/init.go @@ -7,6 +7,7 @@ import ( "path/filepath" "github.com/ipfs/go-datastore" + badger "github.com/ipfs/go-ds-badger" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/peer" "github.com/mitchellh/go-homedir" @@ -22,6 +23,7 @@ import ( "github.com/filecoin-project/lotus/lib/sectorbuilder" "github.com/filecoin-project/lotus/miner" "github.com/filecoin-project/lotus/node/modules" + "github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/storage" ) @@ -115,9 +117,21 @@ var initCmd = &cli.Command{ if pssb := cctx.String("pre-sealed-sectors"); pssb != "" { log.Infof("moving pre-sealed-sectors from %s into newly created storage miner repo", pssb) - if err := migratePreSealedSectors(pssb, repoPath); err != nil { + lr, err := r.Lock(repo.StorageMiner) + if err != nil { return err } + mds, err := lr.Datastore("/metadata") + if err != nil { + return err + } + + if err := migratePreSealedSectors(pssb, repoPath, mds); err != nil { + return err + } + if err := lr.Close(); err != nil { + return xerrors.Errorf("unlocking repo after preseal migration: %w", err) + } } if err := storageMinerInit(ctx, cctx, api, r); err != nil { @@ -142,12 +156,18 @@ var initCmd = &cli.Command{ // TODO: this method should be a lot more robust for mainnet. For testnet, its // fine if we mess things up a few times -func migratePreSealedSectors(presealsb string, repoPath string) error { +// Also probably makes sense for this method to be in the sectorbuilder package +func migratePreSealedSectors(presealsb string, repoPath string, mds dtypes.MetadataDS) error { pspath, err := homedir.Expand(presealsb) if err != nil { return err } + srcds, err := badger.NewDatastore(filepath.Join(pspath, "badger"), nil) + if err != nil { + return err + } + expRepo, err := homedir.Expand(repoPath) if err != nil { return err @@ -166,6 +186,16 @@ func migratePreSealedSectors(presealsb string, repoPath string) error { return err } } + + val, err := srcds.Get(sectorbuilder.LastSectorIdKey) + if err != nil { + return xerrors.Errorf("getting source last sector ID: %w", err) + } + + if err := mds.Put(sectorbuilder.LastSectorIdKey, val); err != nil { + return xerrors.Errorf("failed to write last sector ID key to target datastore: %w", err) + } + return nil } diff --git a/lib/sectorbuilder/sectorbuilder.go b/lib/sectorbuilder/sectorbuilder.go index a08df7ea8..1b43cc974 100644 --- a/lib/sectorbuilder/sectorbuilder.go +++ b/lib/sectorbuilder/sectorbuilder.go @@ -3,15 +3,16 @@ package sectorbuilder import ( "context" "fmt" + "io" + "os" + "strconv" + "sync" + sectorbuilder "github.com/filecoin-project/go-sectorbuilder" "github.com/ipfs/go-datastore" logging "github.com/ipfs/go-log" "go.opencensus.io/trace" "golang.org/x/xerrors" - "io" - "os" - "strconv" - "sync" "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/address" @@ -21,7 +22,7 @@ import ( const PoStReservedWorkers = 1 const PoRepProofPartitions = 2 -var lastSectorIdKey = datastore.NewKey("/sectorbuilder/last") +var LastSectorIdKey = datastore.NewKey("/sectorbuilder/last") var log = logging.Logger("sectorbuilder") @@ -94,7 +95,7 @@ func New(cfg *Config, ds dtypes.MetadataDS) (*SectorBuilder, error) { } var lastUsedID uint64 - b, err := ds.Get(lastSectorIdKey) + b, err := ds.Get(LastSectorIdKey) switch err { case nil: i, err := strconv.ParseInt(string(b), 10, 64) @@ -152,7 +153,7 @@ func (sb *SectorBuilder) AcquireSectorId() (uint64, error) { sb.lastID++ id := sb.lastID - err := sb.ds.Put(lastSectorIdKey, []byte(fmt.Sprint(id))) + err := sb.ds.Put(LastSectorIdKey, []byte(fmt.Sprint(id))) if err != nil { return 0, err }