-
Notifications
You must be signed in to change notification settings - Fork 459
/
mpool_api.go
327 lines (277 loc) · 10.8 KB
/
mpool_api.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package mpool
import (
"context"
"encoding/json"
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/venus/pkg/messagepool"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/ipfs/go-cid"
)
var _ v1api.IMessagePool = &MessagePoolAPI{}
// MessagePoolAPI messsage pool api implement
type MessagePoolAPI struct {
pushLocks *messagepool.MpoolLocker
mp *MessagePoolSubmodule
}
// MpoolDeleteByAdress delete msg in mpool of addr
func (a *MessagePoolAPI) MpoolDeleteByAdress(ctx context.Context, addr address.Address) error {
return a.mp.MPool.DeleteByAdress(addr)
}
// MpoolPublish publish message of address
func (a *MessagePoolAPI) MpoolPublishByAddr(ctx context.Context, addr address.Address) error {
return a.mp.MPool.PublishMsgForWallet(ctx, addr)
}
func (a *MessagePoolAPI) MpoolPublishMessage(ctx context.Context, smsg *types.SignedMessage) error {
return a.mp.MPool.PublishMsg(ctx, smsg)
}
// MpoolPush pushes a signed message to mempool.
func (a *MessagePoolAPI) MpoolPush(ctx context.Context, smsg *types.SignedMessage) (cid.Cid, error) {
return a.mp.MPool.Push(ctx, smsg)
}
// MpoolGetConfig returns (a copy of) the current mpool config
func (a *MessagePoolAPI) MpoolGetConfig(context.Context) (*types.MpoolConfig, error) {
cfg := a.mp.MPool.GetConfig()
return &types.MpoolConfig{
PriorityAddrs: cfg.PriorityAddrs,
SizeLimitHigh: cfg.SizeLimitHigh,
SizeLimitLow: cfg.SizeLimitLow,
ReplaceByFeeRatio: cfg.ReplaceByFeeRatio,
PruneCooldown: cfg.PruneCooldown,
GasLimitOverestimation: cfg.GasLimitOverestimation,
}, nil
}
// MpoolSetConfig sets the mpool config to (a copy of) the supplied config
func (a *MessagePoolAPI) MpoolSetConfig(ctx context.Context, cfg *types.MpoolConfig) error {
return a.mp.MPool.SetConfig(ctx, &messagepool.MpoolConfig{
PriorityAddrs: cfg.PriorityAddrs,
SizeLimitHigh: cfg.SizeLimitHigh,
SizeLimitLow: cfg.SizeLimitLow,
ReplaceByFeeRatio: cfg.ReplaceByFeeRatio,
PruneCooldown: cfg.PruneCooldown,
GasLimitOverestimation: cfg.GasLimitOverestimation,
})
}
// MpoolSelect returns a list of pending messages for inclusion in the next block
func (a *MessagePoolAPI) MpoolSelect(ctx context.Context, tsk types.TipSetKey, ticketQuality float64) ([]*types.SignedMessage, error) {
ts, err := a.mp.chain.API().ChainGetTipSet(ctx, tsk)
if err != nil {
return nil, fmt.Errorf("loading tipset %s: %w", tsk, err)
}
return a.mp.MPool.SelectMessages(ctx, ts, ticketQuality)
}
// MpoolSelects The batch selection message is used when multiple blocks need to select messages at the same time
func (a *MessagePoolAPI) MpoolSelects(ctx context.Context, tsk types.TipSetKey, ticketQualitys []float64) ([][]*types.SignedMessage, error) {
ts, err := a.mp.chain.API().ChainGetTipSet(ctx, tsk)
if err != nil {
return nil, fmt.Errorf("loading tipset %s: %w", tsk, err)
}
return a.mp.MPool.MultipleSelectMessages(ctx, ts, ticketQualitys)
}
// MpoolPending returns pending mempool messages.
func (a *MessagePoolAPI) MpoolPending(ctx context.Context, tsk types.TipSetKey) ([]*types.SignedMessage, error) {
var ts *types.TipSet
var err error
if tsk.IsEmpty() {
ts, err = a.mp.chain.API().ChainHead(ctx)
if err != nil {
return nil, fmt.Errorf("loading tipset %s: %w", tsk, err)
}
} else {
ts, err = a.mp.chain.API().ChainGetTipSet(ctx, tsk)
if err != nil {
return nil, fmt.Errorf("loading tipset %s: %w", tsk, err)
}
}
pending, mpts := a.mp.MPool.Pending(ctx)
haveCids := map[cid.Cid]struct{}{}
for _, m := range pending {
haveCids[m.Cid()] = struct{}{}
}
mptsH := mpts.Height()
tsH := ts.Height()
if ts == nil || mptsH > tsH {
return pending, nil
}
for {
mptsH = mpts.Height()
tsH = ts.Height()
if mptsH == tsH {
if mpts.Equals(ts) {
return pending, nil
}
// different blocks in tipsets
have, err := a.mp.MPool.MessagesForBlocks(ctx, ts.Blocks())
if err != nil {
return nil, fmt.Errorf("getting messages for base ts: %w", err)
}
for _, m := range have {
haveCids[m.Cid()] = struct{}{}
}
}
msgs, err := a.mp.MPool.MessagesForBlocks(ctx, ts.Blocks())
if err != nil {
return nil, fmt.Errorf(": %w", err)
}
for _, m := range msgs {
mc := m.Cid()
if _, ok := haveCids[mc]; ok {
continue
}
haveCids[mc] = struct{}{}
pending = append(pending, m)
}
mptsH = mpts.Height()
tsH = ts.Height()
if mptsH >= tsH {
return pending, nil
}
ts, err = a.mp.chain.API().ChainGetTipSet(ctx, ts.Parents())
if err != nil {
return nil, fmt.Errorf("loading parent tipset: %w", err)
}
}
}
// MpoolClear clears pending messages from the mpool
func (a *MessagePoolAPI) MpoolClear(ctx context.Context, local bool) error {
a.mp.MPool.Clear(ctx, local)
return nil
}
// MpoolPushUntrusted pushes a signed message to mempool from untrusted sources.
func (a *MessagePoolAPI) MpoolPushUntrusted(ctx context.Context, smsg *types.SignedMessage) (cid.Cid, error) {
return a.mp.MPool.PushUntrusted(ctx, smsg)
}
// MpoolPushMessage atomically assigns a nonce, signs, and pushes a message
// to mempool.
// maxFee is only used when GasFeeCap/GasPremium fields aren't specified
//
// When maxFee is set to 0, MpoolPushMessage will guess appropriate fee
// based on current chain conditions
func (a *MessagePoolAPI) MpoolPushMessage(ctx context.Context, msg *types.Message, spec *types.MessageSendSpec) (*types.SignedMessage, error) {
cp := *msg
msg = &cp
inMsg := *msg
fromA, err := a.mp.chain.API().StateAccountKey(ctx, msg.From, types.EmptyTSK)
if err != nil {
return nil, fmt.Errorf("getting key address: %w", err)
}
{
done, err := a.pushLocks.TakeLock(ctx, fromA)
if err != nil {
return nil, fmt.Errorf("taking lock: %w", err)
}
defer done()
}
if msg.Nonce != 0 {
return nil, fmt.Errorf("MpoolPushMessage expects message nonce to be 0, was %d", msg.Nonce)
}
msg, err = a.GasEstimateMessageGas(ctx, msg, spec, types.TipSetKey{})
if err != nil {
return nil, fmt.Errorf("GasEstimateMessageGas error: %w", err)
}
if msg.GasPremium.GreaterThan(msg.GasFeeCap) {
inJSON, err := json.Marshal(inMsg)
if err != nil {
return nil, err
}
outJSON, err := json.Marshal(msg)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("after estimation, GasPremium is greater than GasFeeCap, inmsg: %s, outmsg: %s",
inJSON, outJSON)
}
if msg.From.Protocol() == address.ID {
log.Warnf("Push from ID address (%s), adjusting to %s", msg.From, fromA)
msg.From = fromA
}
b, err := a.mp.walletAPI.WalletBalance(ctx, msg.From)
if err != nil {
return nil, fmt.Errorf("mpool push: getting origin balance: %w", err)
}
requiredFunds := big.Add(msg.Value, msg.RequiredFunds())
if b.LessThan(requiredFunds) {
return nil, fmt.Errorf("mpool push: not enough funds: %s < %s", b, requiredFunds)
}
// Sign and push the message
return a.mp.msgSigner.SignMessage(ctx, msg, func(smsg *types.SignedMessage) error {
if _, err := a.MpoolPush(ctx, smsg); err != nil {
return fmt.Errorf("mpool push: failed to push message: %w", err)
}
return nil
})
}
// MpoolBatchPush batch pushes a unsigned message to mempool.
func (a *MessagePoolAPI) MpoolBatchPush(ctx context.Context, smsgs []*types.SignedMessage) ([]cid.Cid, error) {
var messageCids []cid.Cid
for _, smsg := range smsgs {
smsgCid, err := a.mp.MPool.Push(ctx, smsg)
if err != nil {
return messageCids, err
}
messageCids = append(messageCids, smsgCid)
}
return messageCids, nil
}
// MpoolBatchPushUntrusted batch pushes a signed message to mempool from untrusted sources.
func (a *MessagePoolAPI) MpoolBatchPushUntrusted(ctx context.Context, smsgs []*types.SignedMessage) ([]cid.Cid, error) {
var messageCids []cid.Cid
for _, smsg := range smsgs {
smsgCid, err := a.mp.MPool.PushUntrusted(ctx, smsg)
if err != nil {
return messageCids, err
}
messageCids = append(messageCids, smsgCid)
}
return messageCids, nil
}
// MpoolBatchPushMessage batch pushes a unsigned message to mempool.
func (a *MessagePoolAPI) MpoolBatchPushMessage(ctx context.Context, msgs []*types.Message, spec *types.MessageSendSpec) ([]*types.SignedMessage, error) {
var smsgs []*types.SignedMessage
for _, msg := range msgs {
smsg, err := a.MpoolPushMessage(ctx, msg, spec)
if err != nil {
return smsgs, err
}
smsgs = append(smsgs, smsg)
}
return smsgs, nil
}
// MpoolGetNonce gets next nonce for the specified sender.
// Note that this method may not be atomic. Use MpoolPushMessage instead.
func (a *MessagePoolAPI) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) {
return a.mp.MPool.GetNonce(ctx, addr, types.EmptyTSK)
}
func (a *MessagePoolAPI) MpoolSub(ctx context.Context) (<-chan types.MpoolUpdate, error) {
return a.mp.MPool.Updates(ctx)
}
// GasEstimateMessageGas estimates gas values for unset message gas fields
func (a *MessagePoolAPI) GasEstimateMessageGas(ctx context.Context, msg *types.Message, spec *types.MessageSendSpec, tsk types.TipSetKey) (*types.Message, error) {
return a.mp.MPool.GasEstimateMessageGas(ctx, &types.EstimateMessage{Msg: msg, Spec: spec}, tsk)
}
func (a *MessagePoolAPI) GasBatchEstimateMessageGas(ctx context.Context, estimateMessages []*types.EstimateMessage, fromNonce uint64, tsk types.TipSetKey) ([]*types.EstimateResult, error) {
return a.mp.MPool.GasBatchEstimateMessageGas(ctx, estimateMessages, fromNonce, tsk)
}
// GasEstimateFeeCap estimates gas fee cap
func (a *MessagePoolAPI) GasEstimateFeeCap(ctx context.Context, msg *types.Message, maxqueueblks int64, tsk types.TipSetKey) (big.Int, error) {
return a.mp.MPool.GasEstimateFeeCap(ctx, msg, maxqueueblks, tsk)
}
func (a *MessagePoolAPI) GasEstimateGasLimit(ctx context.Context, msgIn *types.Message, tsk types.TipSetKey) (int64, error) {
return a.mp.MPool.GasEstimateGasLimit(ctx, msgIn, tsk)
}
// GasEstimateGasPremium estimates what gas price should be used for a
// message to have high likelihood of inclusion in `nblocksincl` epochs.
func (a *MessagePoolAPI) GasEstimateGasPremium(ctx context.Context, nblocksincl uint64, sender address.Address, gaslimit int64, tsk types.TipSetKey) (big.Int, error) {
return a.mp.MPool.GasEstimateGasPremium(ctx, nblocksincl, sender, gaslimit, tsk, a.mp.MPool.PriceCache)
}
func (a *MessagePoolAPI) MpoolCheckMessages(ctx context.Context, protos []*types.MessagePrototype) ([][]types.MessageCheckStatus, error) {
return a.mp.MPool.CheckMessages(ctx, protos)
}
func (a *MessagePoolAPI) MpoolCheckPendingMessages(ctx context.Context, addr address.Address) ([][]types.MessageCheckStatus, error) {
return a.mp.MPool.CheckPendingMessages(ctx, addr)
}
func (a *MessagePoolAPI) MpoolCheckReplaceMessages(ctx context.Context, msg []*types.Message) ([][]types.MessageCheckStatus, error) {
return a.mp.MPool.CheckReplaceMessages(ctx, msg)
}