-
Notifications
You must be signed in to change notification settings - Fork 22
/
msgpusher.go
81 lines (64 loc) · 1.81 KB
/
msgpusher.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
package filclient
import (
"context"
"fmt"
"sync"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
)
// a simple nonce tracking message pusher that assumes it is the only thing with access to the given key
type MsgPusher struct {
gapi api.Gateway
w *wallet.LocalWallet
nlk sync.Mutex
nonces map[address.Address]uint64
}
func NewMsgPusher(gapi api.Gateway, w *wallet.LocalWallet) *MsgPusher {
return &MsgPusher{
gapi: gapi,
w: w,
nonces: make(map[address.Address]uint64),
}
}
func (mp *MsgPusher) MpoolPushMessage(ctx context.Context, msg *types.Message, maxFee *api.MessageSendSpec) (*types.SignedMessage, error) {
mp.nlk.Lock()
defer mp.nlk.Unlock()
kaddr, err := mp.gapi.StateAccountKey(ctx, msg.From, types.EmptyTSK)
if err != nil {
return nil, err
}
n, ok := mp.nonces[kaddr]
if !ok {
act, err := mp.gapi.StateGetActor(ctx, kaddr, types.EmptyTSK)
if err != nil {
return nil, err
}
n = act.Nonce
mp.nonces[kaddr] = n
}
msg.Nonce = n
estim, err := mp.gapi.GasEstimateMessageGas(ctx, msg, &api.MessageSendSpec{}, types.EmptyTSK)
if err != nil {
return nil, fmt.Errorf("failed to estimate gas: %w", err)
}
estim.GasFeeCap = abi.NewTokenAmount(4000000000)
estim.GasPremium = big.Mul(estim.GasPremium, big.NewInt(2))
sig, err := mp.w.WalletSign(ctx, kaddr, estim.Cid().Bytes(), api.MsgMeta{Type: api.MTChainMsg})
if err != nil {
return nil, err
}
smsg := &types.SignedMessage{
Message: *estim,
Signature: *sig,
}
_, err = mp.gapi.MpoolPush(ctx, smsg)
if err != nil {
return nil, err
}
mp.nonces[kaddr]++
return smsg, nil
}