2020-10-07 09:26:15 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.uber.org/fx"
|
2020-10-23 00:24:42 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-10-07 09:26:15 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/node/impl/full"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/messagesigner"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
StateAPI full.StateAPI
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:24:42 +00:00
|
|
|
// GetNonce gets the nonce from current chain head.
|
2021-05-18 18:56:42 +00:00
|
|
|
func (a *MpoolNonceAPI) GetNonce(ctx context.Context, addr address.Address) (uint64, error) {
|
2020-10-23 00:24:42 +00:00
|
|
|
ts := a.StateAPI.Chain.GetHeaviestTipSet()
|
|
|
|
|
|
|
|
// make sure we have a key address so we can compare with messages
|
|
|
|
keyAddr, err := a.StateAPI.StateManager.ResolveToKeyAddress(context.TODO(), addr, ts)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the last nonce from the state, if it exists.
|
|
|
|
highestNonce := uint64(0)
|
|
|
|
if baseActor, err := a.StateAPI.StateManager.LoadActorRaw(context.TODO(), addr, ts.ParentState()); err != nil {
|
|
|
|
if !xerrors.Is(err, types.ErrActorNotFound) {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
highestNonce = baseActor.Nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, find the highest nonce in the tipset.
|
|
|
|
msgs, err := a.StateAPI.Chain.MessagesForTipset(ts)
|
2020-10-07 09:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-23 00:24:42 +00:00
|
|
|
for _, msg := range msgs {
|
|
|
|
vmmsg := msg.VMMessage()
|
|
|
|
if vmmsg.From != keyAddr {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-30 18:18:51 +00:00
|
|
|
if vmmsg.Nonce >= highestNonce {
|
|
|
|
highestNonce = vmmsg.Nonce + 1
|
2020-10-23 00:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return highestNonce, nil
|
2020-10-07 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ messagesigner.MpoolNonceAPI = (*MpoolNonceAPI)(nil)
|