-
Notifications
You must be signed in to change notification settings - Fork 4
/
lotus.go
319 lines (266 loc) · 7.35 KB
/
lotus.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"bytes"
"context"
"strings"
"time"
"github.com/glifio/go-logger"
"github.com/pkg/errors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client"
"github.com/filecoin-project/lotus/api/v0api"
apibstore "github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/types"
cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-hamt-ipld"
cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
)
func lotusVerifyAccount(ctx context.Context, targetAddr string, allowance types.BigInt) (cid.Cid, error) {
target, err := address.NewFromString(targetAddr)
if err != nil {
return cid.Cid{}, err
}
params, err := actors.SerializeParams(&verifreg.AddVerifiedClientParams{Address: target, Allowance: allowance})
if err != nil {
return cid.Cid{}, err
}
lapi, closer, err := lotusGetFullNodeAPI(ctx)
if err != nil {
return cid.Cid{}, err
}
defer closer()
nonce, err := lapi.MpoolGetNonce(ctx, VerifierAddr)
msg := &types.Message{
To: builtin.VerifiedRegistryActorAddr,
From: VerifierAddr,
Method: builtin.MethodsVerifiedRegistry.AddVerifiedClient,
Params: params,
Nonce: nonce,
}
sendSpec := &api.MessageSendSpec{
MaxFee: types.BigInt(env.MaxFee),
}
msgWithGas, err := lapi.GasEstimateMessageGas(ctx, msg, sendSpec, types.EmptyTSK)
if err != nil {
return cid.Cid{}, err
}
sig, err := walletSignMessage(ctx, VerifierAddr, msgWithGas.Cid().Bytes(), api.MsgMeta{Type: api.MTUnknown})
if err != nil {
return cid.Cid{}, err
}
mCid, err := lapi.MpoolPush(ctx, &types.SignedMessage{Signature: *sig, Message: *msgWithGas})
if err != nil {
return cid.Cid{}, err
}
return mCid, nil
}
type addrAndDataCap struct {
Address address.Address
DataCap verifreg.DataCap
}
func lotusListVerifiers(ctx context.Context) ([]addrAndDataCap, error) {
api, closer, err := lotusGetFullNodeAPI(ctx)
if err != nil {
return nil, err
}
defer closer()
act, err := api.StateGetActor(ctx, builtin.VerifiedRegistryActorAddr, types.EmptyTSK)
if err != nil {
return nil, err
}
apibs := apibstore.NewAPIBlockstore(api)
cst := cbor.NewCborStore(apibs)
var st verifreg.State
if err := cst.Get(ctx, act.Head, &st); err != nil {
return nil, err
}
vh, err := hamt.LoadNode(ctx, cst, st.Verifiers, hamt.UseTreeBitWidth(5))
if err != nil {
return nil, err
}
var resp []addrAndDataCap
err = vh.ForEach(ctx, func(k string, val interface{}) error {
addr, err := address.NewFromBytes([]byte(k))
if err != nil {
return err
}
var dcap verifreg.DataCap
if err := dcap.UnmarshalCBOR(bytes.NewReader(val.(*cbg.Deferred).Raw)); err != nil {
return err
}
resp = append(resp, addrAndDataCap{addr, dcap})
return nil
})
return resp, err
}
func lotusListVerifiedClients(ctx context.Context) ([]addrAndDataCap, error) {
api, closer, err := lotusGetFullNodeAPI(ctx)
if err != nil {
return nil, err
}
defer closer()
act, err := api.StateGetActor(ctx, builtin.VerifiedRegistryActorAddr, types.EmptyTSK)
if err != nil {
return nil, err
}
apibs := apibstore.NewAPIBlockstore(api)
cst := cbor.NewCborStore(apibs)
var st verifreg.State
if err := cst.Get(ctx, act.Head, &st); err != nil {
return nil, err
}
vh, err := hamt.LoadNode(ctx, cst, st.VerifiedClients, hamt.UseTreeBitWidth(5))
if err != nil {
return nil, err
}
var resp []addrAndDataCap
err = vh.ForEach(ctx, func(k string, val interface{}) error {
addr, err := address.NewFromBytes([]byte(k))
if err != nil {
return err
}
var dcap verifreg.DataCap
if err := dcap.UnmarshalCBOR(bytes.NewReader(val.(*cbg.Deferred).Raw)); err != nil {
return err
}
resp = append(resp, addrAndDataCap{addr, dcap})
return nil
})
return resp, err
}
func ignoreNotFound(err error) error {
if err != nil && strings.Contains(err.Error(), "not found") {
return nil
}
return err
}
func lotusCheckAccountRemainingBytes(ctx context.Context, targetAddr string) (big.Int, error) {
caddr, err := address.NewFromString(targetAddr)
if err != nil {
return big.Int{}, err
}
api, closer, err := lotusGetFullNodeAPI(ctx)
if err != nil {
return big.Int{}, err
}
defer closer()
dcap, err := api.StateVerifiedClientStatus(ctx, caddr, types.EmptyTSK)
err = ignoreNotFound(err)
if err != nil {
return big.Int{}, err
}
if dcap == nil || dcap.Int == nil {
return big.NewInt(0), nil
}
return *dcap, nil
}
func lotusCheckVerifierRemainingBytes(ctx context.Context, targetAddr string) (big.Int, error) {
vaddr, err := address.NewFromString(targetAddr)
if err != nil {
return big.Int{}, err
}
api, closer, err := lotusGetFullNodeAPI(ctx)
if err != nil {
return big.Int{}, err
}
defer closer()
dcap, err := api.StateVerifierStatus(ctx, vaddr, types.EmptyTSK)
err = ignoreNotFound(err)
if err != nil {
return big.Int{}, err
}
if dcap == nil || dcap.Int == nil {
return big.NewInt(0), nil
}
return *dcap, nil
}
func lotusGetFullNodeAPI(ctx context.Context) (apiClient v0api.FullNode, closer jsonrpc.ClientCloser, err error) {
err = retry(ctx, func() error {
ainfo := cliutil.APIInfo{Token: []byte(env.LotusAPIToken)}
var innerErr error
apiClient, closer, innerErr = client.NewFullNodeRPCV0(ctx, env.LotusAPIDialAddr, ainfo.AuthHeader())
return innerErr
})
return
}
func lotusSendFIL(ctx context.Context, lapi v0api.FullNode, fromAddr, toAddr address.Address, filAmount types.FIL) (cid.Cid, error) {
nonce, err := lapi.MpoolGetNonce(ctx, fromAddr)
if err != nil {
return cid.Cid{}, err
}
msg := &types.Message{
From: fromAddr,
To: toAddr,
Value: types.BigInt(filAmount),
Nonce: nonce,
}
sendSpec := &api.MessageSendSpec{
MaxFee: types.BigInt(env.MaxFee),
}
msgWithGas, err := lapi.GasEstimateMessageGas(ctx, msg, sendSpec, types.EmptyTSK)
if err != nil {
return cid.Cid{}, err
}
sig, err := walletSignMessage(ctx, fromAddr, msgWithGas.Cid().Bytes(), api.MsgMeta{Type: api.MTUnknown})
if err != nil {
return cid.Cid{}, err
}
mCid, err := lapi.MpoolPush(ctx, &types.SignedMessage{Signature: *sig, Message: *msgWithGas})
if err != nil {
return cid.Cid{}, err
}
return mCid, nil
}
var errNotMiner = errors.New("not a miner")
func lotusTranslateError(err *error) {
if *err == nil {
return
}
if strings.Contains((*err).Error(), "not found") {
*err = errNotMiner
}
}
func lotusSearchMessageResult(ctx context.Context, cid cid.Cid) (*api.MsgLookup, error) {
client, closer, err := lotusGetFullNodeAPI(ctx)
if err != nil {
logger.Errorf("error getting FullNodeAPI: %v", err)
return &api.MsgLookup{}, err
}
defer closer()
var mLookup *api.MsgLookup
mLookup, err = client.StateSearchMsg(ctx, cid)
if err != nil {
return &api.MsgLookup{}, err
}
return mLookup, nil
}
func retry(ctx context.Context, fn func() error) (err error) {
wait := 5 * time.Second
for {
select {
case <-ctx.Done():
return err
default:
}
err = fn()
if err != nil {
time.Sleep(wait)
wait += wait / 2
continue
}
return nil
}
}
func withStack(err *error) {
if *err != nil {
*err = errors.WithStack(*err)
}
}