Skip to content

Commit 6b36b1a

Browse files
committed
update braodcast_message.go to new tx handler
1 parent 1f72e5b commit 6b36b1a

1 file changed

Lines changed: 40 additions & 22 deletions

File tree

reporter/client/broadcast_message.go

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (c *Client) GenerateAndBroadcastSpotPriceReport(ctx context.Context, qd []b
145145
}
146146

147147
func (c *Client) HandleBridgeDepositTxInChannel(ctx context.Context, data TxChannelInfo) {
148-
resp, err := c.sendTx(ctx, 0, data.Msg) // 0 = no queryMeta tracking for bridge transactions
148+
resp, err := c.sendTx(ctx, 0, true, data.Msg) // 0 = no queryMeta tracking for bridge transactions
149149
if err != nil {
150150
c.logger.Error("submitting deposit report transaction",
151151
"error", err,
@@ -163,7 +163,7 @@ func (c *Client) HandleBridgeDepositTxInChannel(ctx context.Context, data TxChan
163163

164164
// For unordered transactions, we don't need to handle concurrent transaction limits
165165

166-
c.txChan <- data
166+
c.trySend(ctx, data)
167167

168168
return
169169
}
@@ -194,28 +194,46 @@ func (c *Client) HandleBridgeDepositTxInChannel(ctx context.Context, data TxChan
194194
c.logger.Info(fmt.Sprintf("Response from bridge tx report: %v", resp.TxResult))
195195
}
196196

197-
func (c *Client) BroadcastTxMsgToChain() {
197+
func (c *Client) BroadcastTxMsgToChain(ctx context.Context) {
198198
semaphore := make(chan struct{}, maxConcurrentTxs)
199-
for obj := range c.txChan {
200-
// submit transaction in goroutine without waiting for completion
201-
go func(txInfo TxChannelInfo) {
202-
semaphore <- struct{}{}
203-
defer func() { <-semaphore }()
204-
205-
ctx, cancel := context.WithTimeout(context.Background(), txBroadcastTimeout)
206-
defer cancel()
207-
208-
if !txInfo.isBridge {
209-
_, err := c.sendTx(ctx, txInfo.QueryMetaId, txInfo.Msg)
210-
if err != nil {
211-
c.logger.Error(fmt.Sprintf("Error sending tx: %v", err))
212-
}
213-
} else {
214-
c.HandleBridgeDepositTxInChannel(ctx, txInfo)
199+
for {
200+
select {
201+
case <-ctx.Done():
202+
c.logger.Debug("BroadcastTxMsgToChain: context canceled")
203+
return
204+
case obj, ok := <-c.txChan:
205+
if !ok {
206+
c.logger.Debug("BroadcastTxMsgToChain: tx channel closed")
207+
return
215208
}
216-
}(obj)
217209

218-
// log channel status and immediately continue to next transaction
219-
c.logger.Info(fmt.Sprintf("Tx in Channel: %d", len(c.txChan)))
210+
c.broadcastWg.Add(1)
211+
// submit transaction in goroutine without waiting for completion
212+
go func(txInfo TxChannelInfo) {
213+
defer c.broadcastWg.Done()
214+
215+
select {
216+
case semaphore <- struct{}{}:
217+
defer func() { <-semaphore }()
218+
case <-ctx.Done():
219+
return
220+
}
221+
222+
txCtx, cancel := context.WithTimeout(ctx, txBroadcastTimeout)
223+
defer cancel()
224+
225+
if !txInfo.isBridge {
226+
_, err := c.sendTx(txCtx, txInfo.QueryMetaId, false, txInfo.Msg)
227+
if err != nil {
228+
c.logger.Error(fmt.Sprintf("Error sending tx: %v", err))
229+
}
230+
} else {
231+
c.HandleBridgeDepositTxInChannel(txCtx, txInfo)
232+
}
233+
}(obj)
234+
235+
// log channel status and immediately continue to next transaction
236+
c.logger.Info(fmt.Sprintf("Tx in Channel: %d", len(c.txChan)))
237+
}
220238
}
221239
}

0 commit comments

Comments
 (0)