-
Notifications
You must be signed in to change notification settings - Fork 18
/
forwards_sync.go
203 lines (177 loc) · 5.44 KB
/
forwards_sync.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
package cln
import (
"context"
"fmt"
"log"
"math"
"strconv"
"time"
"github.com/breez/lspd/cln/rpc"
"github.com/breez/lspd/history"
"github.com/breez/lspd/lightning"
)
type ForwardSync struct {
createdOffset uint64
updatedOffset uint64
nodeid []byte
client *ClnClient
store history.Store
}
func NewForwardSync(nodeid []byte, client *ClnClient, store history.Store) *ForwardSync {
return &ForwardSync{
nodeid: nodeid,
client: client,
store: store,
}
}
var forwardSyncInterval time.Duration = time.Minute * 5
func (s *ForwardSync) ForwardsSynchronize(ctx context.Context) {
s.createdOffset, s.updatedOffset, _ = s.store.FetchClnForwardOffsets(ctx, s.nodeid)
s.forwardsSynchronizeOnce(ctx)
for {
select {
case <-ctx.Done():
return
case <-time.After(forwardSyncInterval):
}
s.forwardsSynchronizeOnce(ctx)
}
}
func (s *ForwardSync) forwardsSynchronizeOnce(ctx context.Context) {
s.forwardCreatedSynchronizeOnce(ctx)
s.forwardUpdatedSynchronizeOnce(ctx)
}
func (s *ForwardSync) forwardCreatedSynchronizeOnce(ctx context.Context) {
log.Printf("forwardCreatedSynchronizeOnce(%x) - Begin", s.nodeid)
var limit uint32 = 10000
endReached := false
round := 0
for !endReached {
log.Printf("forwardCreatedSynchronizeOnce(%x) - round %v, offset %v", s.nodeid, round, s.createdOffset)
var forwards []*history.Forward
var newCreatedOffset uint64
var err error
forwards, newCreatedOffset, endReached, err = s.listForwards(
rpc.ListforwardsRequest_CREATED, s.createdOffset, limit)
if err != nil {
log.Printf("forwardCreatedSynchronizeOnce(%x)- ListForwards error: %v", s.nodeid, err)
return
}
log.Printf("forwardCreatedSynchronizeOnce(%x) - round %v, offset %v yielded %v forwards", s.nodeid, round, s.createdOffset, len(forwards))
if len(forwards) == 0 {
break
}
err = s.store.InsertForwards(ctx, forwards, s.nodeid)
if err != nil {
log.Printf("forwardCreatedSynchronizeOnce(%x) - store.InsertForwards error: %v", s.nodeid, err)
return
}
err = s.store.SetClnForwardOffsets(ctx, s.nodeid, newCreatedOffset, s.updatedOffset)
if err != nil {
log.Printf("forwardCreatedSynchronizeOnce(%x) - store.SetClnForwardOffsets error: %v", s.nodeid, err)
return
}
s.createdOffset = newCreatedOffset
}
log.Printf("forwardCreatedSynchronizeOnce(%x) - Done", s.nodeid)
}
func (s *ForwardSync) forwardUpdatedSynchronizeOnce(ctx context.Context) {
log.Printf("forwardUpdatedSynchronizeOnce(%x) - Begin", s.nodeid)
var limit uint32 = 10000
endReached := false
round := 0
for !endReached {
log.Printf("forwardUpdatedSynchronizeOnce(%x) - round %v, offset %v", s.nodeid, round, s.updatedOffset)
var forwards []*history.Forward
var newUpdatedOffset uint64
var err error
forwards, newUpdatedOffset, endReached, err = s.listForwards(
rpc.ListforwardsRequest_UPDATED, s.updatedOffset, limit)
if err != nil {
log.Printf("forwardUpdatedSynchronizeOnce(%x)- ListForwards error: %v", s.nodeid, err)
return
}
log.Printf("forwardUpdatedSynchronizeOnce(%x) - round %v, offset %v yielded %v forwards", s.nodeid, round, s.updatedOffset, len(forwards))
if len(forwards) == 0 {
break
}
err = s.store.UpdateForwards(ctx, forwards, s.nodeid)
if err != nil {
log.Printf("forwardUpdatedSynchronizeOnce(%x) - store.InsertForwards error: %v", s.nodeid, err)
return
}
err = s.store.SetClnForwardOffsets(ctx, s.nodeid, s.createdOffset, newUpdatedOffset)
if err != nil {
log.Printf("forwardUpdatedSynchronizeOnce(%x) - store.SetClnForwardOffsets error: %v", s.nodeid, err)
return
}
s.updatedOffset = newUpdatedOffset
}
log.Printf("forwardUpdatedSynchronizeOnce(%x) - Done", s.nodeid)
}
func (s *ForwardSync) listForwards(
index rpc.ListforwardsRequest_ListforwardsIndex,
offset uint64,
limit uint32,
) ([]*history.Forward, uint64, bool, error) {
rLimit := limit * 2
status := rpc.ListforwardsRequest_SETTLED
response, err := s.client.client.ListForwards(
context.Background(),
&rpc.ListforwardsRequest{
Status: &status,
Index: &index,
Start: &offset,
Limit: &rLimit,
},
)
if err != nil {
return nil, 0, false, err
}
var result []*history.Forward
endReached := len(response.Forwards) < int(limit)
var lastIndex *uint64
for _, forward := range response.Forwards {
if forward.OutChannel == nil {
continue
}
if forward.CreatedIndex == nil {
continue
}
if forward.InMsat == nil {
continue
}
if forward.OutMsat == nil {
continue
}
in, err := lightning.NewShortChannelIDFromString(forward.InChannel)
if err != nil {
return nil, 0, false, fmt.Errorf("NewShortChannelIDFromString(%s) error: %w", forward.InChannel, err)
}
out, err := lightning.NewShortChannelIDFromString(*forward.OutChannel)
if err != nil {
return nil, 0, false, fmt.Errorf("NewShortChannelIDFromString(%s) error: %w", *forward.OutChannel, err)
}
sec, dec := math.Modf(forward.ReceivedTime)
result = append(result, &history.Forward{
Identifier: strconv.FormatUint(*forward.CreatedIndex, 10),
InChannel: *in,
OutChannel: *out,
InMsat: forward.InMsat.Msat,
OutMsat: forward.OutMsat.Msat,
ResolvedTime: time.Unix(int64(sec), int64(dec*(1e9))),
})
if index == rpc.ListforwardsRequest_CREATED {
lastIndex = forward.CreatedIndex
} else {
lastIndex = forward.UpdatedIndex
}
}
var newOffset uint64
if lastIndex == nil {
newOffset = 0
} else {
newOffset = *lastIndex + 1
}
return result, newOffset, endReached, nil
}