@@ -44,15 +44,8 @@ func (pool *Pool) get(index uint64) *model.WrappedIBTP {
4444}
4545
4646func (ex * Exchanger ) feedIBTP (wIbtp * model.WrappedIBTP ) {
47- var pool * Pool
48- ibtp := wIbtp .Ibtp
49- act , loaded := ex .ibtps .Load (ibtp .From )
50- if ! loaded {
51- pool = NewPool ()
52- ex .ibtps .Store (ibtp .From , pool )
53- } else {
54- pool = act .(* Pool )
55- }
47+ act , loaded := ex .ibtps .LoadOrStore (wIbtp .Ibtp .From , NewPool ())
48+ pool := act .(* Pool )
5649 pool .feed (wIbtp )
5750
5851 if ! loaded {
@@ -63,53 +56,47 @@ func (ex *Exchanger) feedIBTP(wIbtp *model.WrappedIBTP) {
6356 }
6457 }()
6558 inMeta := ex .exec .QueryInterchainMeta ()
59+ idx := inMeta [wIbtp .Ibtp .From ]
6660 for wIbtp := range pool .ch {
67- ibtp := wIbtp .Ibtp
68- idx := inMeta [ibtp .From ]
69- if ibtp .Index <= idx {
70- pool .delete (ibtp .Index )
71- ex .logger .Warnf ("ignore ibtp with invalid index: %d" , ibtp .Index )
61+ if wIbtp .Ibtp .Index <= idx {
62+ pool .delete (wIbtp .Ibtp .Index )
63+ ex .logger .Warnf ("ignore ibtp with invalid index: %d" , wIbtp .Ibtp .Index )
7264 continue
7365 }
74- if idx + 1 == ibtp .Index {
75- ex .processIBTP (wIbtp )
76- pool .delete (ibtp .Index )
77- index := ibtp .Index + 1
66+ if idx + 1 == wIbtp .Ibtp .Index {
67+ ex .processIBTP (wIbtp , pool )
68+ index := wIbtp .Ibtp .Index + 1
7869 wIbtp := pool .get (index )
7970 for wIbtp != nil {
80- ex .processIBTP (wIbtp )
81- pool .delete (wIbtp .Ibtp .Index )
71+ ex .processIBTP (wIbtp , pool )
8272 index ++
8373 wIbtp = pool .get (index )
8474 }
75+ idx = index - 1
8576 } else {
8677 pool .put (wIbtp )
8778 }
79+
8880 }
8981 }(pool )
9082 }
9183}
9284
93- func (ex * Exchanger ) processIBTP (wIbtp * model.WrappedIBTP ) {
85+ func (ex * Exchanger ) processIBTP (wIbtp * model.WrappedIBTP , pool * Pool ) {
9486 receipt , err := ex .exec .ExecuteIBTP (wIbtp )
9587 if err != nil {
9688 ex .logger .Errorf ("Execute ibtp error: %s" , err .Error ())
9789 return
9890 }
9991 ex .postHandleIBTP (wIbtp .Ibtp .From , receipt )
10092 ex .sendIBTPCounter .Inc ()
93+ pool .delete (wIbtp .Ibtp .Index )
10194}
10295
103- func (ex * Exchanger ) feedReceipt (receipt * pb.IBTP ) {
104- var pool * Pool
105- act , loaded := ex .ibtps .Load (receipt .To )
106- if ! loaded {
107- pool = NewPool ()
108- ex .ibtps .Store (receipt .To , pool )
109- } else {
110- pool = act .(* Pool )
111- }
112- pool .feed (& model.WrappedIBTP {Ibtp : receipt , IsValid : true })
96+ func (ex * Exchanger ) feedReceipt (ibtp * pb.IBTP ) {
97+ act , loaded := ex .ibtps .LoadOrStore (ibtp .From , NewPool ())
98+ pool := act .(* Pool )
99+ pool .feed (& model.WrappedIBTP {Ibtp : ibtp , IsValid : true })
113100
114101 if ! loaded {
115102 go func (pool * Pool ) {
@@ -119,26 +106,25 @@ func (ex *Exchanger) feedReceipt(receipt *pb.IBTP) {
119106 }
120107 }()
121108 callbackMeta := ex .exec .QueryCallbackMeta ()
109+ idx := callbackMeta [ibtp .To ]
122110 for wIbtp := range pool .ch {
123- ibtp := wIbtp .Ibtp
124- if ibtp .Index <= callbackMeta [ibtp .To ] {
125- pool .delete (ibtp .Index )
111+ if ibtp .Index <= idx {
112+ pool .delete (wIbtp .Ibtp .Index )
126113 ex .logger .Warn ("ignore ibtp with invalid index" )
127114 continue
128115 }
129- if callbackMeta [ibtp .To ]+ 1 == ibtp .Index {
130- ex .processIBTP (wIbtp )
131- pool .delete (ibtp .Index )
116+ if idx + 1 == ibtp .Index {
117+ ex .processIBTP (wIbtp , pool )
132118 index := ibtp .Index + 1
133119 wIbtp := pool .get (index )
134120 for wIbtp != nil {
135- ibtp := wIbtp .Ibtp
136121 receipt , _ := ex .exec .ExecuteIBTP (wIbtp )
137- ex .postHandleIBTP (ibtp .From , receipt )
138- pool .delete (ibtp .Index )
122+ ex .postHandleIBTP (wIbtp . Ibtp .From , receipt )
123+ pool .delete (wIbtp . Ibtp .Index )
139124 index ++
140125 wIbtp = pool .get (index )
141126 }
127+ idx = index - 1
142128 } else {
143129 pool .put (wIbtp )
144130 }
0 commit comments