-
Notifications
You must be signed in to change notification settings - Fork 95
/
poller.go
289 lines (257 loc) · 8.83 KB
/
poller.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
package poller
import (
"github.com/iotaledger/iota.go/account"
"github.com/iotaledger/iota.go/account/event"
"github.com/iotaledger/iota.go/account/store"
"github.com/iotaledger/iota.go/account/util"
"github.com/iotaledger/iota.go/bundle"
. "github.com/iotaledger/iota.go/trinary"
"github.com/pkg/errors"
"sync"
"time"
)
// StringSet is a set of strings.
type StringSet map[string]struct{}
// ReceiveEventFilter filters and creates events given the incoming bundles, deposit addresses and spent addresses.
// It's the job of the ReceiveEventFilter to emit the appropriate events through the given EventMachine.
type ReceiveEventFilter func(eventMachine event.EventMachine, bndls bundle.Bundles, depAddrs StringSet, spentAddrs StringSet)
const (
// emitted when a broadcasted transfer got confirmed
EventTransferConfirmed event.Event = 100
// emitted when a deposit is being received
EventReceivingDeposit event.Event = 101
// emitted when a deposit is confirmed
EventReceivedDeposit event.Event = 102
// emitted when a zero value transaction is received
EventReceivedMessage event.Event = 103
)
// NewTransferPoller creates a new TransferPoller. If the interval is set to 0, the TransferPoller will only
// poll through Poll().
func NewTransferPoller(setts *account.Settings, filter ReceiveEventFilter, interval time.Duration) *TransferPoller {
if setts.EventMachine == nil {
setts.EventMachine = &event.DiscardEventMachine{}
}
if filter == nil {
filter = NewPerTailReceiveEventFilter(true)
}
poller := &TransferPoller{
setts: setts, receiveEventFilter: filter,
}
poller.syncTimer = util.NewSyncIntervalTimer(interval, poller.pollTransfers, func(err error) {
poller.setts.EventMachine.Emit(err, event.EventError)
})
return poller
}
// TransferPoller is an account plugin which takes care of checking pending transfers for confirmation
// and checking incoming transfers.
type TransferPoller struct {
setts *account.Settings
receiveEventFilter ReceiveEventFilter
acc account.Account
syncTimer *util.SyncIntervalTimer
}
func (tp *TransferPoller) Name() string {
return "transfer-poller"
}
func (tp *TransferPoller) Start(acc account.Account) error {
tp.acc = acc
go tp.syncTimer.Start()
return nil
}
// Poll awaits the current transfer polling task to finish (in case it's ongoing),
// pauses the repeated task, does a manual polling, resumes the repeated task and then returns.
// Poll will block infinitely if called after the account has been shutdown.
func (tp *TransferPoller) Poll() error {
tp.syncTimer.Pause()
err := tp.pollTransfers()
tp.syncTimer.Resume()
return err
}
func (tp *TransferPoller) Shutdown() error {
tp.syncTimer.Stop()
return nil
}
func (tp *TransferPoller) pollTransfers() error {
pendingTransfers, err := tp.setts.Store.GetPendingTransfers(tp.acc.ID())
if err != nil {
return errors.Wrap(err, "unable to load pending transfers for polling transfers")
}
depositAddresses, err := tp.setts.Store.GetDepositAddresses(tp.acc.ID())
if err != nil {
return errors.Wrap(err, "unable to load deposit addresses for polling transfers")
}
// nothing to do
if len(pendingTransfers) == 0 && len(depositAddresses) == 0 {
return nil
}
// poll incoming/outgoing in parallel
var outErr, inErr error
if len(pendingTransfers) > 0 {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
outErr = tp.checkOutgoingTransfers(pendingTransfers)
}()
inErr = tp.checkIncomingTransfers(depositAddresses, pendingTransfers)
wg.Wait()
} else {
inErr = tp.checkIncomingTransfers(depositAddresses, pendingTransfers)
}
if outErr != nil {
return outErr
}
return inErr
}
func (tp *TransferPoller) checkOutgoingTransfers(pendingTransfers map[string]*store.PendingTransfer) error {
for tailTx, pendingTransfer := range pendingTransfers {
if len(pendingTransfer.Tails) == 0 {
continue
}
states, err := tp.setts.API.GetInclusionStates(pendingTransfer.Tails)
if err != nil {
return errors.Wrapf(err, "unable to check latest inclusion state in outgoing transfers op. (first tail tx of bundle: %s)", tailTx)
}
// if any state is true we can remove the transfer as it got confirmed
for i, state := range states {
if !state {
continue
}
// fetch bundle to emit it in the event
bndl, err := tp.setts.API.GetBundle(pendingTransfer.Tails[i])
if err != nil {
return errors.Wrapf(err, "unable to get bundle in outgoing transfers op. (first tail tx of bundle: %s) of tail %s", tailTx, pendingTransfer.Tails[i])
}
tp.setts.EventMachine.Emit(bndl, EventTransferConfirmed)
if err := tp.setts.Store.RemovePendingTransfer(tp.acc.ID(), tailTx); err != nil {
return errors.Wrap(err, "unable to remove confirmed transfer from store in outgoing transfers op.")
}
break
}
}
return nil
}
func (tp *TransferPoller) checkIncomingTransfers(storedDepositAddresses map[uint64]*store.StoredDepositAddress, pendingTransfers map[string]*store.PendingTransfer) error {
if len(storedDepositAddresses) == 0 {
return nil
}
depositAddresses := make(StringSet)
depAddrs := make(Hashes, 0)
for keyIndex, depositAddress := range storedDepositAddresses {
// filter remainder address bundles
if depositAddress.TimeoutAt == nil {
continue
}
addr, err := tp.setts.AddrGen(keyIndex, depositAddress.SecurityLevel, true)
if err != nil {
return errors.Wrap(err, "unable to compute deposit address in incoming transfers op.")
}
depAddrs = append(depAddrs, addr)
depositAddresses[addr] = struct{}{}
}
if len(depositAddresses) == 0 {
return nil
}
spentAddresses := make(StringSet)
for _, transfer := range pendingTransfers {
bndl, err := store.PendingTransferToBundle(transfer)
if err != nil {
panic(err)
}
for j := range bndl {
if bndl[j].Value < 0 {
spentAddresses[bndl[j].Address] = struct{}{}
}
}
}
// get all bundles which operated on the current deposit addresses
bndls, err := tp.setts.API.GetBundlesFromAddresses(depAddrs, true)
if err != nil {
return errors.Wrap(err, "unable to fetch bundles from deposit addresses in incoming transfers op.")
}
// create the events to emit in the event system
tp.receiveEventFilter(tp.setts.EventMachine, bndls, depositAddresses, spentAddresses)
return nil
}
type ReceiveEventTuple struct {
Event event.Event
Bundle bundle.Bundle
}
// PerTailFilter filters receiving/received bundles by the bundle's tail transaction hash.
// Optionally takes in a bool flag indicating whether the first pass of the event filter
// should not emit any events.
func NewPerTailReceiveEventFilter(skipFirst ...bool) ReceiveEventFilter {
var skipFirstEmitting bool
if len(skipFirst) > 0 && skipFirst[0] {
skipFirstEmitting = true
}
receivedFilter := map[string]struct{}{}
receivingFilter := map[string]struct{}{}
return func(em event.EventMachine, bndls bundle.Bundles, ownDepAddrs StringSet, ownSpentAddrs StringSet) {
events := []ReceiveEventTuple{}
receivingBundles := make(map[string]bundle.Bundle)
receivedBundles := make(map[string]bundle.Bundle)
// filter out transfers to own remainder addresses or where
// a deposit is an input address (a spend from our own address)
for _, bndl := range bndls {
if err := bundle.ValidBundle(bndl); err != nil {
continue
}
tailTx := bundle.TailTransactionHash(bndl)
if *bndl[0].Persistence {
receivedBundles[tailTx] = bndl
} else {
receivingBundles[tailTx] = bndl
}
}
isValueTransfer := func(bndl bundle.Bundle) bool {
isValue := false
for _, tx := range bndl {
if tx.Value > 0 || tx.Value < 0 {
isValue = true
break
}
}
return isValue
}
// filter out bundles for which a previous event was emitted
// and emit new events for the new bundles
for tailTx, bndl := range receivingBundles {
if _, has := receivingFilter[tailTx]; has {
continue
}
receivingFilter[tailTx] = struct{}{}
// determine whether the bundle is a value transfer.
if isValueTransfer(bndl) {
events = append(events, ReceiveEventTuple{EventReceivingDeposit, bndl})
continue
}
events = append(events, ReceiveEventTuple{EventReceivedMessage, bndl})
}
for tailTx, bndl := range receivedBundles {
if _, has := receivedFilter[tailTx]; has {
continue
}
receivedFilter[tailTx] = struct{}{}
if isValueTransfer(bndl) {
events = append(events, ReceiveEventTuple{EventReceivedDeposit, bndl})
continue
}
// if a message bundle got confirmed but an event was already emitted
// during its receiving, we don't emit another event
if _, has := receivingFilter[tailTx]; has {
continue
}
events = append(events, ReceiveEventTuple{EventReceivedMessage, bndl})
}
// skip first emitting of events as multiple restarts of the same account
// would yield the same events.
if skipFirstEmitting {
skipFirstEmitting = false
return
}
for _, ev := range events {
em.Emit(ev.Bundle, ev.Event)
}
}
}