/
export.go
154 lines (131 loc) · 4.04 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (C) 2022 FrogHub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package export
import (
"encoding/json"
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/chain/types"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"io/ioutil"
"strconv"
"time"
)
var log = logging.Logger("export")
var ExportsCmd = &cli.Command{
Name: "export",
Usage: "Export sector metadata",
ArgsUsage: "[sectorNum1 sectorNum2 ...]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "miner",
Usage: "Filecoin Miner. Such as: f01000",
Required: true,
},
},
Action: func(cctx *cli.Context) error {
ctx := cliutil.ReqContext(cctx)
start := time.Now()
if cctx.Args().Len() < 1 {
return fmt.Errorf("at least one sector must be specified")
}
runSectors := make([]uint64, 0)
for _, sn := range cctx.Args().Slice() {
sectorNum, err := strconv.ParseUint(sn, 10, 64)
if err != nil {
return fmt.Errorf("could not parse sector number: %w", err)
}
runSectors = append(runSectors, sectorNum)
}
maddr, err := address.NewFromString(cctx.String("miner"))
if err != nil {
return xerrors.Errorf("Getting NewFromString err:", err)
}
fullNodeApi, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return xerrors.Errorf("Getting FullNodeAPI err:", err)
}
defer closer()
//Sector size
mi, err := fullNodeApi.StateMinerInfo(ctx, maddr, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("Getting StateMinerInfo err:", err)
}
output := &RecoveryParams{
Miner: maddr,
SectorSize: mi.SectorSize,
}
sectorInfos := make(SectorInfos, 0)
failtSectors := make([]uint64, 0)
for _, sector := range runSectors {
ts, sectorPreCommitOnChainInfo, err := GetSectorCommitInfoOnChain(ctx, fullNodeApi, maddr, abi.SectorNumber(sector))
if err != nil {
log.Errorf("Getting sector (%d) precommit info error: %v ", sector, err)
continue
}
si := &SectorInfo{
SectorNumber: abi.SectorNumber(sector),
SealProof: sectorPreCommitOnChainInfo.Info.SealProof,
SealedCID: sectorPreCommitOnChainInfo.Info.SealedCID,
}
ticket, err := GetSectorTicketOnChain(ctx, fullNodeApi, maddr, ts, sectorPreCommitOnChainInfo)
if err != nil {
log.Errorf("Getting sector (%d) ticket error: %v ", sector, err)
continue
}
si.Ticket = ticket
sectorInfos = append(sectorInfos, si)
output.SectorInfos = sectorInfos
out, err := json.MarshalIndent(output, "", "\t")
if err != nil {
return err
}
of, err := homedir.Expand("sectors-recovery-" + maddr.String() + ".json")
if err != nil {
return err
}
if err := ioutil.WriteFile(of, out, 0644); err != nil {
return err
}
}
end := time.Now()
fmt.Println("export", len(sectorInfos), "sectors, failt sectors:", failtSectors, ", elapsed:", end.Sub(start))
return nil
},
}
type RecoveryParams struct {
Miner address.Address
SectorSize abi.SectorSize
SectorInfos SectorInfos
}
type SectorInfo struct {
SectorNumber abi.SectorNumber
Ticket abi.Randomness
SealProof abi.RegisteredSealProof
SealedCID cid.Cid
}
type SectorInfos []*SectorInfo
func (t SectorInfos) Len() int { return len(t) }
func (t SectorInfos) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t SectorInfos) Less(i, j int) bool {
return t[i].SectorNumber < t[j].SectorNumber
}