Skip to content

Commit

Permalink
WIP: add a command for compacting sector numbers bitfield
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Oct 29, 2020
1 parent 32ea060 commit 77ec7ca
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions chain/actors/builtin/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type State interface {
LoadSectors(sectorNos *bitfield.BitField) ([]*SectorOnChainInfo, error)
NumLiveSectors() (uint64, error)
IsAllocated(abi.SectorNumber) (bool, error)
GetAllocatedSectors() (*bitfield.BitField, error)

LoadDeadline(idx uint64) (Deadline, error)
ForEachDeadline(cb func(idx uint64, dl Deadline) error) error
Expand Down
9 changes: 9 additions & 0 deletions chain/actors/builtin/miner/v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ func (s *state0) IsAllocated(num abi.SectorNumber) (bool, error) {
return allocatedSectors.IsSet(uint64(num))
}

func (s *state0) GetAllocatedSectors() (*bitfield.BitField, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
return nil, err
}

return &allocatedSectors, nil
}

func (s *state0) LoadDeadline(idx uint64) (Deadline, error) {
dls, err := s.State.LoadDeadlines(s.store)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions chain/actors/builtin/miner/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ func (s *state2) LoadSectors(snos *bitfield.BitField) ([]*SectorOnChainInfo, err
return infos, nil
}

func (s *state2) GetAllocatedSectors() (*bitfield.BitField, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
return nil, err
}

return &allocatedSectors, nil
}

func (s *state2) IsAllocated(num abi.SectorNumber) (bool, error) {
var allocatedSectors bitfield.BitField
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
Expand Down
111 changes: 111 additions & 0 deletions cmd/lotus-storage-miner/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"

Expand Down Expand Up @@ -918,3 +919,113 @@ var actorConfirmChangeWorker = &cli.Command{
return nil
},
}

var actorCompactAllocatedCmd = &cli.Command{
Name: "compact-allocated",
Usage: "compact allocated sectors bitfield",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "auto",
Usage: "fill out all empty gaps in allocation bitfield",
Value: false,
},
&cli.BoolFlag{
Name: "really-do-it",
Usage: "Actually send transaction performing the action",
Value: false,
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Bool("really-do-it") {
fmt.Println("Pass --really-do-it to actually execute this action")
return nil
}

if !cctx.Args().Present() {
return fmt.Errorf("must pass address of new owner address")
}

nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()

api, acloser, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer acloser()

ctx := lcli.ReqContext(cctx)

maddr, err := nodeApi.ActorAddress(ctx)
if err != nil {
return err
}

mact, err := api.StateGetActor(ctx, maddr, types.EmptyTSK)
if err != nil {
return err
}

store := adt.WrapStore(ctx, cbor.NewCborStore(apibstore.NewAPIBlockstore(api)))

mst, err := miner.Load(store, mact)
if err != nil {
return err
}

allocs, err := mst.GetAllocatedSectors()
if err != nil {
return err
}

last, err := allocs.Last()
if err != nil {
return err
}

// ??? TODO
bitfield.NewFromIter()

mi, err := api.StateMinerInfo(ctx, maddr, types.EmptyTSK)
if err != nil {
return err
}

params := &miner2.CompactSectorNumbersParams{}

sp, err := actors.SerializeParams(params)
if err != nil {
return xerrors.Errorf("serializing params: %w", err)
}

smsg, err := api.MpoolPushMessage(ctx, &types.Message{
From: mi.Worker,
To: maddr,
Method: miner.Methods.CompactSectorNumbers,
Value: big.Zero(),
Params: sp,
}, nil)
if err != nil {
return xerrors.Errorf("mpool push: %w", err)
}

fmt.Println("CompactSectorNumbers Message CID:", smsg.Cid())

// wait for it to get mined into a block
wait, err := api.StateWaitMsg(ctx, smsg.Cid(), build.MessageConfidence)
if err != nil {
return err
}

// check it executed successfully
if wait.Receipt.ExitCode != 0 {
fmt.Println("Propose owner change failed!")
return err
}

return nil
},
}

0 comments on commit 77ec7ca

Please sign in to comment.