-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mpoolnonceapi.go
107 lines (92 loc) · 2.78 KB
/
mpoolnonceapi.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
package modules
import (
"context"
"strings"
"go.uber.org/fx"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/messagepool"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/impl/full"
)
// MpoolNonceAPI substitutes the mpool nonce with an implementation that
// doesn't rely on the mpool - it just gets the nonce from actor state
type MpoolNonceAPI struct {
fx.In
ChainModule full.ChainModuleAPI
StateModule full.StateModuleAPI
}
// GetNonce gets the nonce from current chain head.
func (a *MpoolNonceAPI) GetNonce(ctx context.Context, addr address.Address, tsk types.TipSetKey) (uint64, error) {
var err error
var ts *types.TipSet
if tsk == types.EmptyTSK {
// we need consistent tsk
ts, err = a.ChainModule.ChainHead(ctx)
if err != nil {
return 0, xerrors.Errorf("getting head: %w", err)
}
tsk = ts.Key()
} else {
ts, err = a.ChainModule.ChainGetTipSet(ctx, tsk)
if err != nil {
return 0, xerrors.Errorf("getting tipset: %w", err)
}
}
keyAddr := addr
if addr.Protocol() == address.ID {
// make sure we have a key address so we can compare with messages
keyAddr, err = a.StateModule.StateAccountKey(ctx, addr, tsk)
if err != nil {
return 0, xerrors.Errorf("getting account key: %w", err)
}
} else {
addr, err = a.StateModule.StateLookupID(ctx, addr, types.EmptyTSK)
if err != nil {
log.Infof("failed to look up id addr for %s: %w", addr, err)
addr = address.Undef
}
}
// Load the last nonce from the state, if it exists.
highestNonce := uint64(0)
act, err := a.StateModule.StateGetActor(ctx, keyAddr, ts.Key())
if err != nil {
if strings.Contains(err.Error(), types.ErrActorNotFound.Error()) {
return 0, xerrors.Errorf("getting actor converted: %w", types.ErrActorNotFound)
}
return 0, xerrors.Errorf("getting actor: %w", err)
}
highestNonce = act.Nonce
apply := func(msg *types.Message) {
if msg.From != addr && msg.From != keyAddr {
return
}
if msg.Nonce == highestNonce {
highestNonce = msg.Nonce + 1
}
}
for _, b := range ts.Blocks() {
msgs, err := a.ChainModule.ChainGetBlockMessages(ctx, b.Cid())
if err != nil {
return 0, xerrors.Errorf("getting block messages: %w", err)
}
if keyAddr.Protocol() == address.BLS {
for _, m := range msgs.BlsMessages {
apply(m)
}
} else {
for _, sm := range msgs.SecpkMessages {
apply(&sm.Message)
}
}
}
return highestNonce, nil
}
func (a *MpoolNonceAPI) GetActor(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) {
act, err := a.StateModule.StateGetActor(ctx, addr, tsk)
if err != nil {
return nil, xerrors.Errorf("calling StateGetActor: %w", err)
}
return act, nil
}
var _ messagepool.MpoolNonceAPI = (*MpoolNonceAPI)(nil)