-
Notifications
You must be signed in to change notification settings - Fork 2
/
bench.go
181 lines (153 loc) · 3.67 KB
/
bench.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bufio"
"context"
"crypto/rand"
"fmt"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper/basicfs"
proof2 "github.com/filecoin-project/specs-actors/v2/actors/runtime/proof"
"golang.org/x/xerrors"
"io"
"os"
"strconv"
"strings"
"github.com/urfave/cli/v2"
addr "github.com/filecoin-project/go-address"
)
func ReadSids(fileName string) ([]int, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
buf := bufio.NewReader(f)
var sids []int
for {
line, err := buf.ReadString('\n')
if err == io.EOF {
break
}
line = strings.TrimSpace(line)
sid, err := strconv.Atoi(line)
if err != nil {
return nil, err
}
sids = append(sids, sid)
}
return sids, nil
}
func allSectors(ctx context.Context, api api.FullNode, maddr addr.Address, allSectors bitfield.BitField) ([]proof2.SectorInfo, error) {
sset, err := api.StateMinerSectors(ctx, maddr, &allSectors, types.EmptyTSK)
if err != nil {
return nil, err
}
if len(sset) == 0 {
return nil, nil
}
substitute := proof2.SectorInfo{
SectorNumber: sset[0].SectorNumber,
SealedCID: sset[0].SealedCID,
SealProof: sset[0].SealProof,
}
sectorByID := make(map[uint64]proof2.SectorInfo, len(sset))
for _, sector := range sset {
sectorByID[uint64(sector.SectorNumber)] = proof2.SectorInfo{
SectorNumber: sector.SectorNumber,
SealedCID: sector.SealedCID,
SealProof: sector.SealProof,
}
}
proofSectors := make([]proof2.SectorInfo, 0, len(sset))
if err := allSectors.ForEach(func(sectorNo uint64) error {
if info, found := sectorByID[sectorNo]; found {
proofSectors = append(proofSectors, info)
} else {
proofSectors = append(proofSectors, substitute)
}
return nil
}); err != nil {
return nil, xerrors.Errorf("iterating partition sector bitmap: %w", err)
}
return proofSectors, nil
}
func benchPost(cctx *cli.Context, sids[]int, dir string) error {
fmt.Printf("bench %v sectors\n", len(sids))
sectors := bitfield.New()
for _, id := range sids {
sectors.Set(uint64(id))
}
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 := getActorAddress(ctx, nodeApi, cctx.String("actor"))
if err != nil {
return err
}
amid, err := addr.IDFromAddress(maddr)
if err != nil {
return err
}
mid := abi.ActorID(amid)
sis, err := allSectors(ctx, api, maddr, sectors)
if err != nil {
return err
}
sbfs := &basicfs.Provider{
Root: dir,
}
sb, err := ffiwrapper.New(sbfs)
if err != nil {
return err
}
var challenge [32]byte
rand.Read(challenge[:])
_, _, err1 := sb.GenerateWindowPoSt(ctx, mid, sis, challenge[:])
if err1 != nil {
return err1
}
return nil
}
var benchCmd = &cli.Command{
Name: "bench",
Usage: "bench post",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "file",
Usage: "bench sector ids",
Value: "",
},
&cli.StringFlag{
Name: "db",
Usage: "bench home",
Value: "",
},
},
Action: func(cctx *cli.Context) error {
fi := cctx.String("file")
if fi == "" {
return fmt.Errorf("input file")
}
if cctx.String("db") == ""{
return fmt.Errorf("input db")
}
sids, err := ReadSids(fi)
if err != nil {
return err
}
return benchPost(cctx, sids, cctx.String("db"))
},
}