forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
190 lines (164 loc) · 4.74 KB
/
module.go
File metadata and controls
190 lines (164 loc) · 4.74 KB
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
package lotus
import (
"context"
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/apistruct"
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
"github.com/ipfs/go-cid"
"github.com/textileio/powergate/ffs"
"github.com/textileio/powergate/lotus"
)
// Module provides access to the paych api.
type Module struct {
clientBuilder lotus.ClientBuilder
}
var _ ffs.PaychManager = (*Module)(nil)
// New creates a new paych module.
func New(clientBuilder lotus.ClientBuilder) *Module {
return &Module{
clientBuilder: clientBuilder,
}
}
// List lists all payment channels involving the specified addresses.
func (m *Module) List(ctx context.Context, addrs ...string) ([]ffs.PaychInfo, error) {
filter := make(map[string]struct{}, len(addrs))
for _, addr := range addrs {
filter[addr] = struct{}{}
}
client, cls, err := m.clientBuilder()
if err != nil {
return nil, fmt.Errorf("creating lotus client: %s", err)
}
defer cls()
allAddrs, err := client.PaychList(ctx)
if err != nil {
return nil, err
}
chans := make([]<-chan statusResult, len(allAddrs))
for i, addr := range allAddrs {
chans[i] = m.paychStatus(ctx, client, addr)
}
resultsCh := make(chan statusResult, len(chans))
for _, c := range chans {
go func(ch <-chan statusResult) {
res := <-ch
resultsCh <- res
}(c)
}
results := make([]statusResult, len(chans))
for i := 0; i < len(chans); i++ {
results[i] = <-resultsCh
}
var final []ffs.PaychInfo
for _, result := range results {
if result.err != nil {
// ToDo: do we want to fail if there was an error checking
// even one status that may not even be in our filter?
continue
}
_, addrInFilter := filter[result.addr.String()]
_, ctlAddrInFilter := filter[result.status.ControlAddr.String()]
if addrInFilter || ctlAddrInFilter {
var dir ffs.PaychDir
switch result.status.Direction {
case api.PCHUndef:
dir = ffs.PaychDirUnspecified
case api.PCHInbound:
dir = ffs.PaychDirInbound
case api.PCHOutbound:
dir = ffs.PaychDirOutbound
default:
return nil, fmt.Errorf("unknown pay channel direction %v", result.status.Direction)
}
info := ffs.PaychInfo{
CtlAddr: result.status.ControlAddr.String(),
Addr: result.addr.String(),
Direction: dir,
}
final = append(final, info)
}
}
return final, nil
}
// Create creates a new payment channel.
func (m *Module) Create(ctx context.Context, from string, to string, amount uint64) (ffs.PaychInfo, cid.Cid, error) {
return ffs.PaychInfo{}, cid.Undef, fmt.Errorf("unimplemeted for now, blocked by lotus issue #1611")
// fAddr, err := address.NewFromString(from)
// if err != nil {
// return ffs.PaychInfo{}, cid.Undef, err
// }
// tAddr, err := address.NewFromString(from)
// if err != nil {
// return ffs.PaychInfo{}, cid.Undef, err
// }
// a := types.NewInt(amount)
// info, err := m.api.PaychGet(ctx, fAddr, tAddr, a)
// if err != nil {
// return ffs.PaychInfo{}, cid.Undef, err
// }
// // ToDo: verify these addresses and direction make sense
// res := ffs.PaychInfo{
// CtlAddr: from,
// Addr: info.Channel.String(),
// Direction: ffs.PaychDirOutbound,
// }
// return res, info.ChannelMessage, nil
}
// Redeem redeems a payment channel.
func (m *Module) Redeem(ctx context.Context, ch string) error {
chAddr, err := address.NewFromString(ch)
if err != nil {
return err
}
client, cls, err := m.clientBuilder()
if err != nil {
return fmt.Errorf("creating lotus client: %s", err)
}
defer cls()
vouchers, err := client.PaychVoucherList(ctx, chAddr)
if err != nil {
return err
}
var best *paych.SignedVoucher
for _, v := range vouchers {
spendable, err := client.PaychVoucherCheckSpendable(ctx, chAddr, v, nil, nil)
if err != nil {
return err
}
if spendable && (best == nil || v.Amount.GreaterThan(best.Amount)) {
best = v
}
}
if best == nil {
return fmt.Errorf("No spendable vouchers for that channel")
}
// ToDo: fix last two params since API changed.
mcid, err := client.PaychVoucherSubmit(ctx, chAddr, best, nil, nil)
if err != nil {
return err
}
mwait, err := client.StateWaitMsg(ctx, mcid, 3)
if err != nil {
return err
}
if mwait.Receipt.ExitCode != 0 {
return fmt.Errorf("submit voucher message execution failed (exit code %d)", mwait.Receipt.ExitCode)
}
return nil
}
type statusResult struct {
addr address.Address
status *api.PaychStatus
err error
}
func (m *Module) paychStatus(ctx context.Context, client *apistruct.FullNodeStruct, addr address.Address) <-chan statusResult {
c := make(chan statusResult)
go func() {
defer close(c)
status, err := client.PaychStatus(ctx, addr)
c <- statusResult{addr: addr, status: status, err: err}
}()
return c
}