-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
main.go
212 lines (185 loc) · 4.67 KB
/
main.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"github.com/docker/go-units"
logging "github.com/ipfs/go-log/v2"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/cmd/lotus-seed/seed"
"github.com/filecoin-project/lotus/genesis"
)
var log = logging.Logger("lotus-seed")
func main() {
logging.SetLogLevel("*", "INFO")
local := []*cli.Command{
genesisCmd,
preSealCmd,
aggregateManifestsCmd,
}
app := &cli.App{
Name: "lotus-seed",
Usage: "Seal sectors for genesis miner",
Version: build.UserVersion(),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "sector-dir",
Value: "~/.genesis-sectors",
},
},
Commands: local,
}
if err := app.Run(os.Args); err != nil {
log.Warn(err)
os.Exit(1)
}
}
var preSealCmd = &cli.Command{
Name: "pre-seal",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "miner-addr",
Value: "t01000",
Usage: "specify the future address of your miner",
},
&cli.StringFlag{
Name: "sector-size",
Value: "2KiB",
Usage: "specify size of sectors to pre-seal",
},
&cli.StringFlag{
Name: "ticket-preimage",
Value: "lotus is fire",
Usage: "set the ticket preimage for sealing randomness",
},
&cli.IntFlag{
Name: "num-sectors",
Value: 1,
Usage: "select number of sectors to pre-seal",
},
&cli.Uint64Flag{
Name: "sector-offset",
Value: 0,
Usage: "how many sector ids to skip when starting to seal",
},
&cli.StringFlag{
Name: "key",
Value: "",
Usage: "(optional) Key to use for signing / owner/worker addresses",
},
&cli.BoolFlag{
Name: "fake-sectors",
Value: false,
},
&cli.UintFlag{
Name: "network-version",
Usage: "specify network version",
Value: uint(build.GenesisNetworkVersion),
},
},
Action: func(c *cli.Context) error {
sdir := c.String("sector-dir")
sbroot, err := homedir.Expand(sdir)
if err != nil {
return err
}
maddr, err := address.NewFromString(c.String("miner-addr"))
if err != nil {
return err
}
var k *types.KeyInfo
if c.String("key") != "" {
k = new(types.KeyInfo)
kh, err := os.ReadFile(c.String("key"))
if err != nil {
return err
}
kb, err := hex.DecodeString(string(kh))
if err != nil {
return err
}
if err := json.Unmarshal(kb, k); err != nil {
return err
}
}
sectorSizeInt, err := units.RAMInBytes(c.String("sector-size"))
if err != nil {
return err
}
sectorSize := abi.SectorSize(sectorSizeInt)
nv := build.GenesisNetworkVersion
if c.IsSet("network-version") {
nv = network.Version(c.Uint64("network-version"))
}
var synthetic = false // there's little reason to have this for a seed.
spt, err := miner.SealProofTypeFromSectorSize(sectorSize, nv, synthetic)
if err != nil {
return err
}
gm, key, err := seed.PreSeal(maddr, spt, abi.SectorNumber(c.Uint64("sector-offset")), c.Int("num-sectors"), sbroot, []byte(c.String("ticket-preimage")), k, c.Bool("fake-sectors"))
if err != nil {
return err
}
return seed.WriteGenesisMiner(maddr, sbroot, gm, key)
},
}
var aggregateManifestsCmd = &cli.Command{
Name: "aggregate-manifests",
Usage: "aggregate a set of preseal manifests into a single file",
Action: func(cctx *cli.Context) error {
var inputs []map[string]genesis.Miner
for _, infi := range cctx.Args().Slice() {
fi, err := os.Open(infi)
if err != nil {
return err
}
var val map[string]genesis.Miner
if err := json.NewDecoder(fi).Decode(&val); err != nil {
return err
}
inputs = append(inputs, val)
if err := fi.Close(); err != nil {
return err
}
}
output := make(map[string]genesis.Miner)
for _, in := range inputs {
for maddr, val := range in {
if gm, ok := output[maddr]; ok {
output[maddr] = mergeGenMiners(gm, val)
} else {
output[maddr] = val
}
}
}
blob, err := json.MarshalIndent(output, "", " ")
if err != nil {
return err
}
fmt.Println(string(blob))
return nil
},
}
func mergeGenMiners(a, b genesis.Miner) genesis.Miner {
if a.SectorSize != b.SectorSize {
panic("sector sizes mismatch")
}
return genesis.Miner{
Owner: a.Owner,
Worker: a.Worker,
PeerId: a.PeerId,
MarketBalance: big.Zero(),
PowerBalance: big.Zero(),
SectorSize: a.SectorSize,
Sectors: append(a.Sectors, b.Sectors...),
}
}