/
mpoolmonitor.go
186 lines (154 loc) · 4.24 KB
/
mpoolmonitor.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
package mpoolmonitor
import (
"context"
"fmt"
"sync"
"time"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api/v1api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
)
var log = logging.Logger("mpoolmonitor")
// TimeStampedMsg wraps the pending msg with a chainEpoch
type TimeStampedMsg struct {
SignedMessage types.SignedMessage
Added abi.ChainEpoch // Epoch when message was first noticed in mpool
AddedTime time.Time
}
type MpoolMonitor struct {
ctx context.Context
cancel context.CancelFunc
fullNode v1api.FullNode
lk sync.Mutex
mpoolAlertEpochs abi.ChainEpoch
msgs map[cid.Cid]*TimeStampedMsg
}
func NewMonitor(fullNode v1api.FullNode, mpoolAlertEpochs int64) *MpoolMonitor {
return &MpoolMonitor{
fullNode: fullNode,
mpoolAlertEpochs: abi.ChainEpoch(mpoolAlertEpochs),
msgs: make(map[cid.Cid]*TimeStampedMsg),
}
}
func (mm *MpoolMonitor) Start(ctx context.Context) error {
log.Infow("Mpool monitor: starting")
mmctx, cancel := context.WithCancel(ctx)
mm.ctx = mmctx
mm.cancel = cancel
go mm.startMonitoring(mmctx)
return nil
}
func (mm *MpoolMonitor) startMonitoring(ctx context.Context) {
err := mm.update(ctx)
if err != nil {
log.Errorf("failed to start mpool monitor: %s", err)
}
ticker := time.NewTicker(time.Duration(build.BlockDelaySecs) * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := mm.update(ctx)
if err != nil {
log.Errorf("failed to get messages from mpool: %s", err)
}
}
}
}
// update gets the current pending messages from mpool. It updated the local
// copy of a message(not TimeStampedMsg.added) if CID is already present in MpoolMonitor.msgs
// Otherwise, it inserts a new key value pair for the message. It removed any msgs not found in the
// latest output of MpoolPending
func (mm *MpoolMonitor) update(ctx context.Context) error {
t := time.Now()
ts, err := mm.fullNode.ChainHead(ctx)
if err != nil {
return fmt.Errorf("failed to get chain head: %w", err)
}
msgs, err := mm.fullNode.MpoolPending(ctx, types.EmptyTSK)
if err != nil {
return fmt.Errorf("getting mpool messages: %w", err)
}
newMsgs := make(map[cid.Cid]*TimeStampedMsg)
for _, pm := range msgs {
newMsgs[pm.Cid()] = &TimeStampedMsg{
SignedMessage: *pm,
Added: ts.Height(),
AddedTime: t,
}
}
mm.lk.Lock()
defer mm.lk.Unlock()
// Add new msgs, update older msgs with any changes in values
for k, v := range newMsgs {
_, ok := mm.msgs[k]
if ok {
mm.msgs[k].SignedMessage = v.SignedMessage
} else {
mm.msgs[k] = v
}
}
// Remove msgs from mm.msgs if they are not found in updated list
for k := range mm.msgs {
_, ok := newMsgs[k]
if !ok {
delete(mm.msgs, k)
}
}
return nil
}
func (mm *MpoolMonitor) PendingLocal(ctx context.Context) ([]*TimeStampedMsg, error) {
localAddr := make(map[string]struct{})
var ret []*TimeStampedMsg
addrs, err := mm.fullNode.WalletList(ctx)
if err != nil {
return nil, fmt.Errorf("getting local addresses: %w", err)
}
for _, a := range addrs {
localAddr[a.String()] = struct{}{}
}
mm.lk.Lock()
defer mm.lk.Unlock()
for _, msg := range mm.msgs {
if _, has := localAddr[msg.SignedMessage.Message.From.String()]; !has {
continue
}
ret = append(ret, msg)
}
return ret, nil
}
func (mm *MpoolMonitor) PendingAll() ([]*TimeStampedMsg, error) {
var ret []*TimeStampedMsg
mm.lk.Lock()
defer mm.lk.Unlock()
for _, msg := range mm.msgs {
ret = append(ret, msg)
}
return ret, nil
}
func (mm *MpoolMonitor) Alerts(ctx context.Context) ([]cid.Cid, error) {
var ret []cid.Cid
ts, err := mm.fullNode.ChainHead(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get chain head: %w", err)
}
msgs, err := mm.PendingLocal(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get local messages: %w", err)
}
for _, msg := range msgs {
if msg.Added+mm.mpoolAlertEpochs <= ts.Height() {
ret = append(ret, msg.SignedMessage.Cid())
}
}
return ret, nil
}
func (mm *MpoolMonitor) Stop(ctx context.Context) error {
mm.cancel()
return nil
}