Skip to content

Commit a61de47

Browse files
author
Sandy Zhou
authored
Merge pull request #219 from meshplus/feat/handle-intp-2.0
fix(*): handle invalid/unavailable service error
2 parents 64e27dc + 22fb4cc commit a61de47

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

internal/syncer/agent.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ import (
2222
)
2323

2424
const (
25-
srcchainNotAvailable = "current appchain not available"
26-
dstchainNotAvailable = "target appchain not available"
27-
invalidIBTP = "invalid ibtp"
28-
ibtpIndexExist = "index already exists"
29-
ibtpIndexWrong = "wrong index"
30-
noBindRule = "appchain didn't register rule"
25+
srcchainNotAvailable = "current appchain not available"
26+
dstchainNotAvailable = "target appchain not available"
27+
invalidIBTP = "invalid ibtp"
28+
ibtpIndexExist = "index already exists"
29+
ibtpIndexWrong = "wrong index"
30+
noBindRule = "appchain didn't register rule"
31+
InvalidSourceService = "invalid source service"
32+
InvalidTargetService = "invalid target service"
33+
TargetServiceNotAvailable = "target service not available"
3134
)
3235

3336
var (
@@ -166,18 +169,21 @@ func (syncer *WrapperSyncer) SendIBTP(ibtp *pb.IBTP) error {
166169
tx, _ := syncer.client.GenerateIBTPTx(ibtp)
167170
tx.Extra = proof
168171

169-
var receipt *pb.Receipt
170-
strategies := []strategy.Strategy{strategy.Wait(2 * time.Second)}
171-
if ibtp.Type != pb.IBTP_ROLLBACK {
172-
strategies = append(strategies, strategy.Limit(5))
173-
}
174-
175-
var retErr error
172+
var (
173+
receipt *pb.Receipt
174+
retErr error
175+
count = uint64(0)
176+
)
176177
if err := retry.Retry(func(attempt uint) error {
177178
hash, err := syncer.client.SendTransaction(tx, nil)
178179
if err != nil {
179180
syncer.logger.Errorf("Send ibtp error: %s", err.Error())
180181
if errors.Is(err, rpcx.ErrRecoverable) {
182+
count++
183+
if count == 5 && ibtp.Type == pb.IBTP_INTERCHAIN {
184+
retErr = fmt.Errorf("rollback ibtp %s: %v", ibtp.ID(), err)
185+
return nil
186+
}
181187
return err
182188
}
183189
if errors.Is(err, rpcx.ErrReconstruct) {
@@ -200,12 +206,19 @@ func (syncer *WrapperSyncer) SendIBTP(ibtp *pb.IBTP) error {
200206
if strings.Contains(errMsg, noBindRule) || strings.Contains(errMsg, srcchainNotAvailable) {
201207
return fmt.Errorf("appchain not valid: %s", errMsg)
202208
}
209+
203210
// if target chain is not available, this ibtp should be rollback
204-
if strings.Contains(errMsg, dstchainNotAvailable) {
205-
syncer.logger.Errorf("Destination appchain is not available, try to rollback in source appchain...")
211+
if strings.Contains(errMsg, InvalidTargetService) {
212+
syncer.logger.Errorf("%s, try to rollback in source appchain...", errMsg)
206213
syncer.rollbackHandler(ibtp, "")
207214
return nil
208215
}
216+
217+
// if target chain is not available, this ibtp should be rollback
218+
if strings.Contains(errMsg, dstchainNotAvailable) || strings.Contains(errMsg, TargetServiceNotAvailable) {
219+
retErr = fmt.Errorf("rollback ibtp %s: %s", ibtp.ID(), errMsg)
220+
return nil
221+
}
209222
if strings.Contains(errMsg, ibtpIndexExist) {
210223
// if ibtp index is lower than index recorded on bitxhub, then ignore this ibtp
211224
return nil
@@ -229,7 +242,7 @@ func (syncer *WrapperSyncer) SendIBTP(ibtp *pb.IBTP) error {
229242
}
230243

231244
return nil
232-
}, strategies...); err != nil {
245+
}, strategy.Wait(2*time.Second)); err != nil {
233246
return err
234247
}
235248
return retErr

internal/syncer/syncer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ func TestSendIBTP(t *testing.T) {
516516
Status: pb.Receipt_FAILED,
517517
}
518518
client.EXPECT().GetReceipt(hash).Return(failReceipt, nil).AnyTimes()
519-
require.Nil(t, syncer.SendIBTP(&pb.IBTP{}))
519+
require.NotNil(t, syncer.SendIBTP(&pb.IBTP{}))
520520

521521
//failReceipt.Ret = []byte(errMsg2)
522522
//require.Nil(t, syncer.SendIBTP(&pb.IBTP{}))

0 commit comments

Comments
 (0)