diff --git a/api/messager-client.go b/api/messager-client.go index db5cf5fa..ff6dc870 100644 --- a/api/messager-client.go +++ b/api/messager-client.go @@ -10,6 +10,7 @@ import ( types2 "github.com/filecoin-project/venus/pkg/types" "github.com/ipfs-force-community/venus-common-utils/apiinfo" "golang.org/x/xerrors" + "time" ) var ErrFailMsg = xerrors.New("Message Fail") @@ -32,15 +33,52 @@ func NewMessager(in client.IMessager) *Messager { return &Messager{in: in} } -func (m *Messager) WaitMessage(ctx context.Context, id string, confidence uint64) (*types.Message, error) { - msg, err := m.in.WaitMessage(ctx, id, confidence) - if err != nil { - return nil, err - } - if msg.State == types.FailedMsg { - return nil, ErrFailMsg +func (message *Messager) WaitMessage(ctx context.Context, id string, confidence uint64) (*types.Message, error) { + tm := time.NewTicker(time.Second * 30) + defer tm.Stop() + + doneCh := make(chan struct{}, 1) + doneCh <- struct{}{} + + for { + select { + case <-doneCh: + msg, err := message.in.GetMessageByUid(ctx, id) + if err != nil { + return nil, xerrors.Errorf("get message fail while wait %w", ErrFailMsg) + } + + switch msg.State { + //OffChain + case types.FillMsg: + fallthrough + case types.UnFillMsg: + fallthrough + case types.UnKnown: + continue + //OnChain + case types.ReplacedMsg: + fallthrough + case types.OnChainMsg: + if msg.Confidence > int64(confidence) { + return msg, nil + } + continue + //Error + case types.FailedMsg: + var reason string + if msg.Receipt != nil { + reason = string(msg.Receipt.ReturnValue) + } + return nil, xerrors.Errorf("msg failed due to %s %w", reason, ErrFailMsg) + } + + case <-tm.C: + doneCh <- struct{}{} + case <-ctx.Done(): + return nil, xerrors.Errorf("get message fail while wait %w", ErrFailMsg) + } } - return msg, nil } func (m *Messager) WalletHas(ctx context.Context, addr address.Address) (bool, error) {