From 44f4ee0de1c628c330abc74a8328dedc9b1b7a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 29 Oct 2019 21:41:30 +0100 Subject: [PATCH] storageminer: cmd to list sector commitments --- api/api.go | 10 +++++ api/struct.go | 6 +++ cmd/lotus-storage-miner/commitments.go | 41 ++++++++++++++++++ cmd/lotus-storage-miner/main.go | 1 + node/impl/storminer.go | 6 +++ storage/commitment/tracker.go | 57 ++++++++++++++++++++++++++ storage/miner.go | 2 +- 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 cmd/lotus-storage-miner/commitments.go diff --git a/api/api.go b/api/api.go index 635901ecb..463e5e9cb 100644 --- a/api/api.go +++ b/api/api.go @@ -168,6 +168,8 @@ type StorageMiner interface { SectorsList(context.Context) ([]uint64, error) SectorsRefs(context.Context) (map[string][]SealedRef, error) + + CommitmentsList(context.Context) ([]SectorCommitment, error) } // Version provides various build-time information @@ -330,6 +332,14 @@ type SyncState struct { Height uint64 } +type SectorCommitment struct { + SectorID uint64 + Miner address.Address + + CommitMsg cid.Cid + DealIDs []uint64 +} + type SyncStateStage int const ( diff --git a/api/struct.go b/api/struct.go index 6eefe41dc..47d88aa69 100644 --- a/api/struct.go +++ b/api/struct.go @@ -133,6 +133,8 @@ type StorageMinerStruct struct { SectorsList func(context.Context) ([]uint64, error) `perm:"read"` SectorsRefs func(context.Context) (map[string][]SealedRef, error) `perm:"read"` + + CommitmentsList func(context.Context) ([]SectorCommitment, error) `perm:"read"` } } @@ -479,6 +481,10 @@ func (c *StorageMinerStruct) SectorsRefs(ctx context.Context) (map[string][]Seal return c.Internal.SectorsRefs(ctx) } +func (c *StorageMinerStruct) CommitmentsList(ctx context.Context) ([]SectorCommitment, error) { + return c.Internal.CommitmentsList(ctx) +} + var _ Common = &CommonStruct{} var _ FullNode = &FullNodeStruct{} var _ StorageMiner = &StorageMinerStruct{} diff --git a/cmd/lotus-storage-miner/commitments.go b/cmd/lotus-storage-miner/commitments.go new file mode 100644 index 000000000..a7825deb1 --- /dev/null +++ b/cmd/lotus-storage-miner/commitments.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + lcli "github.com/filecoin-project/lotus/cli" + + "gopkg.in/urfave/cli.v2" +) + +var commitmentsCmd = &cli.Command{ + Name: "commitments", + Usage: "interact with commitment tracker", + Subcommands: []*cli.Command{ + commitmentsListCmd, + }, +} + +var commitmentsListCmd = &cli.Command{ + Name: "list", + Usage: "List tracked sector commitments", + Action: func(cctx *cli.Context) error { + api, closer, err := lcli.GetStorageMinerAPI(cctx) + if err != nil { + return err + } + defer closer() + + ctx := lcli.ReqContext(cctx) + + comms, err := api.CommitmentsList(ctx) + if err != nil { + return err + } + + for _, comm := range comms { + fmt.Printf("%s:%d msg:%s, deals: %v\n", comm.Miner, comm.SectorID, comm.CommitMsg, comm.DealIDs) + } + + return nil + }, +} diff --git a/cmd/lotus-storage-miner/main.go b/cmd/lotus-storage-miner/main.go index c9a904695..c08cd21e6 100644 --- a/cmd/lotus-storage-miner/main.go +++ b/cmd/lotus-storage-miner/main.go @@ -24,6 +24,7 @@ func main() { infoCmd, storeGarbageCmd, sectorsCmd, + commitmentsCmd, } jaeger := tracing.SetupJaegerTracing("lotus") defer func() { diff --git a/node/impl/storminer.go b/node/impl/storminer.go index c763dab5e..8eb363fe2 100644 --- a/node/impl/storminer.go +++ b/node/impl/storminer.go @@ -10,6 +10,7 @@ import ( "github.com/filecoin-project/lotus/chain/address" "github.com/filecoin-project/lotus/lib/sectorbuilder" "github.com/filecoin-project/lotus/storage" + "github.com/filecoin-project/lotus/storage/commitment" "github.com/filecoin-project/lotus/storage/sector" "github.com/filecoin-project/lotus/storage/sectorblocks" @@ -23,6 +24,7 @@ type StorageMinerAPI struct { SectorBuilder *sectorbuilder.SectorBuilder Sectors *sector.Store SectorBlocks *sectorblocks.SectorBlocks + CommitmentTracker *commitment.Tracker Miner *storage.Miner } @@ -81,4 +83,8 @@ func (sm *StorageMinerAPI) SectorsRefs(context.Context) (map[string][]api.Sealed return out, nil } +func (sm *StorageMinerAPI) CommitmentsList(ctx context.Context) ([]api.SectorCommitment, error) { + return sm.CommitmentTracker.List() +} + var _ api.StorageMiner = &StorageMinerAPI{} diff --git a/storage/commitment/tracker.go b/storage/commitment/tracker.go index 10510fba0..a0fa57e80 100644 --- a/storage/commitment/tracker.go +++ b/storage/commitment/tracker.go @@ -3,6 +3,8 @@ package commitment import ( "context" "fmt" + "strconv" + "strings" "sync" "github.com/ipfs/go-cid" @@ -12,8 +14,10 @@ import ( logging "github.com/ipfs/go-log" "golang.org/x/xerrors" + "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/chain/address" "github.com/filecoin-project/lotus/node/modules/dtypes" + dsq "github.com/ipfs/go-datastore/query" ) var log = logging.Logger("commitment") @@ -145,3 +149,56 @@ func (ct *Tracker) CheckCommitment(miner address.Address, sectorId uint64) (bool return ct.commitments.Has(key) } + +func (ct *Tracker) List() ([]api.SectorCommitment, error) { + out := make([]api.SectorCommitment, 0) + + ct.lk.Lock() + defer ct.lk.Unlock() + + res, err := ct.commitments.Query(dsq.Query{}) + if err != nil { + return nil, err + } + defer res.Close() + + for { + res, ok := res.NextSync() + if !ok { + break + } + + if res.Error != nil { + return nil, xerrors.Errorf("iterating commitments: %w", err) + } + + parts := strings.Split(res.Key, "/") + if len(parts) != 4 { + return nil, xerrors.Errorf("expected commitment key to be 4 parts, Key %s", res.Key) + } + + miner, err := address.NewFromString(parts[2]) + if err != nil { + return nil, xerrors.Errorf("parsing miner address: %w", err) + } + + sectorID, err := strconv.ParseInt(parts[3], 10, 64) + if err != nil { + return nil, xerrors.Errorf("parsing sector id: %w", err) + } + + var comm commitment + if err := cbor.DecodeInto(res.Value, &comm); err != nil { + return nil, xerrors.Errorf("decoding commitment %s (`% X`): %w", res.Key, res.Value, err) + } + + out = append(out, api.SectorCommitment{ + SectorID: uint64(sectorID), + Miner: miner, + CommitMsg: comm.Msg, + DealIDs: comm.DealIDs, + }) + } + + return out, nil +} diff --git a/storage/miner.go b/storage/miner.go index 81880f86b..7e1757e31 100644 --- a/storage/miner.go +++ b/storage/miner.go @@ -2,9 +2,9 @@ package storage import ( "context" - "github.com/filecoin-project/go-sectorbuilder/sealing_state" "sync" + "github.com/filecoin-project/go-sectorbuilder/sealing_state" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" logging "github.com/ipfs/go-log"