This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
/
lotus.go
152 lines (128 loc) · 5.71 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
package filecoin
import (
"context"
"fmt"
"net/http"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/dline"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/specs-actors/v5/actors/builtin"
"github.com/ipfs/go-cid"
)
// LotusAPI tells the rpc client how to handle the different methods
type LotusAPI struct {
Methods struct {
ChainHead func(context.Context) (*TipSet, error)
GasEstimateMessageGas func(context.Context, *Message, *MessageSendSpec, TipSetKey) (*Message, error)
StateGetActor func(context.Context, address.Address, TipSetKey) (*Actor, error)
MpoolPush func(context.Context, *SignedMessage) (cid.Cid, error)
StateWaitMsg func(context.Context, cid.Cid, uint64) (*MsgLookup, error)
StateSearchMsg func(context.Context, cid.Cid) (*MsgLookup, error)
StateAccountKey func(context.Context, address.Address, TipSetKey) (address.Address, error)
StateLookupID func(context.Context, address.Address, TipSetKey) (address.Address, error)
StateReadState func(context.Context, address.Address, TipSetKey) (*ActorState, error)
StateNetworkVersion func(context.Context, TipSetKey) (network.Version, error)
StateMarketBalance func(context.Context, address.Address, TipSetKey) (MarketBalance, error)
StateDealProviderCollateralBounds func(context.Context, abi.PaddedPieceSize, bool, TipSetKey) (DealCollateralBounds, error)
StateMinerInfo func(context.Context, address.Address, TipSetKey) (MinerInfo, error)
StateMinerProvingDeadline func(context.Context, address.Address, TipSetKey) (*dline.Info, error)
StateCall func(context.Context, *Message, TipSetKey) (*InvocResult, error)
ChainReadObj func(context.Context, cid.Cid) ([]byte, error)
ChainGetMessage func(context.Context, cid.Cid) (*Message, error)
}
closer jsonrpc.ClientCloser
}
// NewLotusRPC starts a new lotus RPC client
func NewLotusRPC(ctx context.Context, addr string, header http.Header) (API, error) {
var res LotusAPI
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{
&res.Methods,
},
header,
)
if err != nil {
return nil, err
}
res.closer = closer
return &res, nil
}
func (a *LotusAPI) Close() {
a.closer()
}
func (a *LotusAPI) ChainHead(ctx context.Context) (*TipSet, error) {
return a.Methods.ChainHead(ctx)
}
func (a *LotusAPI) GasEstimateMessageGas(ctx context.Context, m *Message, mss *MessageSendSpec, tsk TipSetKey) (*Message, error) {
return a.Methods.GasEstimateMessageGas(ctx, m, mss, tsk)
}
func (a *LotusAPI) StateGetActor(ctx context.Context, addr address.Address, tsk TipSetKey) (*Actor, error) {
return a.Methods.StateGetActor(ctx, addr, tsk)
}
func (a *LotusAPI) MpoolPush(ctx context.Context, sm *SignedMessage) (cid.Cid, error) {
return a.Methods.MpoolPush(ctx, sm)
}
func (a *LotusAPI) StateWaitMsg(ctx context.Context, c cid.Cid, conf uint64) (*MsgLookup, error) {
// return a.Methods.StateWaitMsg(ctx, c, conf)
// just for testing
if conf == 0 {
return a.Methods.StateSearchMsg(ctx, c)
}
maxRetries := 10
retries := 0
for retries < maxRetries {
retries++
select {
case <-time.Tick(builtin.EpochDurationSeconds * time.Second * time.Duration(conf)):
lookup, err := a.Methods.StateSearchMsg(ctx, c)
if err != nil {
continue
}
if lookup != nil {
return lookup, nil
}
case <-ctx.Done():
return nil, ctx.Err()
}
}
return nil, fmt.Errorf("failed after %d retries", retries)
}
func (a *LotusAPI) StateSearchMsg(ctx context.Context, c cid.Cid) (*MsgLookup, error) {
return a.Methods.StateSearchMsg(ctx, c)
}
func (a *LotusAPI) StateAccountKey(ctx context.Context, addr address.Address, tsk TipSetKey) (address.Address, error) {
return a.Methods.StateAccountKey(ctx, addr, tsk)
}
func (a *LotusAPI) StateLookupID(ctx context.Context, addr address.Address, tsk TipSetKey) (address.Address, error) {
return a.Methods.StateLookupID(ctx, addr, tsk)
}
func (a *LotusAPI) StateReadState(ctx context.Context, addr address.Address, tsk TipSetKey) (*ActorState, error) {
return a.Methods.StateReadState(ctx, addr, tsk)
}
func (a *LotusAPI) StateNetworkVersion(ctx context.Context, tsk TipSetKey) (network.Version, error) {
return a.Methods.StateNetworkVersion(ctx, tsk)
}
func (a *LotusAPI) StateMarketBalance(ctx context.Context, addr address.Address, tsk TipSetKey) (MarketBalance, error) {
return a.Methods.StateMarketBalance(ctx, addr, tsk)
}
func (a *LotusAPI) StateDealProviderCollateralBounds(ctx context.Context, size abi.PaddedPieceSize, verified bool, tsk TipSetKey) (DealCollateralBounds, error) {
return a.Methods.StateDealProviderCollateralBounds(ctx, size, verified, tsk)
}
func (a *LotusAPI) StateMinerInfo(ctx context.Context, addr address.Address, tsk TipSetKey) (MinerInfo, error) {
return a.Methods.StateMinerInfo(ctx, addr, tsk)
}
func (a *LotusAPI) StateMinerProvingDeadline(ctx context.Context, addr address.Address, tsk TipSetKey) (*dline.Info, error) {
return a.Methods.StateMinerProvingDeadline(ctx, addr, tsk)
}
func (a *LotusAPI) StateCall(ctx context.Context, msg *Message, tsk TipSetKey) (*InvocResult, error) {
return a.Methods.StateCall(ctx, msg, tsk)
}
func (a *LotusAPI) ChainReadObj(ctx context.Context, c cid.Cid) ([]byte, error) {
return a.Methods.ChainReadObj(ctx, c)
}
func (a *LotusAPI) ChainGetMessage(ctx context.Context, c cid.Cid) (*Message, error) {
return a.Methods.ChainGetMessage(ctx, c)
}