Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

make wait local to avoid long time connection #67

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions api/messager-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down