Skip to content

Commit 15317d7

Browse files
authored
Flush query should include begin and end block events (#1125)
* Include begin and end block events * disable flushing when termination condition is set * Still flush for FlushLifecycle * Add sort for flush logging to avoid confusion
1 parent 72d1ad2 commit 15317d7

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

relayer/chains/cosmos/query.go

+49-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"strconv"
1010
"strings"
11+
"sync"
1112
"time"
1213

1314
abci "github.com/cometbft/cometbft/abci/types"
@@ -52,14 +53,55 @@ func (cc *CosmosProvider) queryIBCMessages(ctx context.Context, log *zap.Logger,
5253
return nil, errors.New("limit must greater than 0")
5354
}
5455

55-
res, err := cc.RPCClient.TxSearch(ctx, query, true, &page, &limit, "")
56-
if err != nil {
57-
return nil, err
58-
}
59-
var ibcMsgs []ibcMessage
56+
var eg errgroup.Group
6057
chainID := cc.ChainId()
61-
for _, tx := range res.Txs {
62-
ibcMsgs = append(ibcMsgs, ibcMessagesFromEvents(log, tx.TxResult.Events, chainID, 0, base64Encoded)...)
58+
var ibcMsgs []ibcMessage
59+
var mu sync.Mutex
60+
61+
eg.Go(func() error {
62+
res, err := cc.RPCClient.BlockSearch(ctx, query, &page, &limit, "")
63+
if err != nil {
64+
return err
65+
}
66+
67+
var nestedEg errgroup.Group
68+
69+
for _, b := range res.Blocks {
70+
b := b
71+
nestedEg.Go(func() error {
72+
block, err := cc.RPCClient.BlockResults(ctx, &b.Block.Height)
73+
if err != nil {
74+
return err
75+
}
76+
77+
mu.Lock()
78+
defer mu.Unlock()
79+
ibcMsgs = append(ibcMsgs, ibcMessagesFromEvents(log, block.BeginBlockEvents, chainID, 0, base64Encoded)...)
80+
ibcMsgs = append(ibcMsgs, ibcMessagesFromEvents(log, block.EndBlockEvents, chainID, 0, base64Encoded)...)
81+
82+
return nil
83+
})
84+
}
85+
return nestedEg.Wait()
86+
})
87+
88+
eg.Go(func() error {
89+
res, err := cc.RPCClient.TxSearch(ctx, query, true, &page, &limit, "")
90+
if err != nil {
91+
return err
92+
}
93+
94+
mu.Lock()
95+
defer mu.Unlock()
96+
for _, tx := range res.Txs {
97+
ibcMsgs = append(ibcMsgs, ibcMessagesFromEvents(log, tx.TxResult.Events, chainID, 0, base64Encoded)...)
98+
}
99+
100+
return nil
101+
})
102+
103+
if err := eg.Wait(); err != nil {
104+
return nil, err
63105
}
64106

65107
return ibcMsgs, nil

relayer/processor/path_processor.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,7 @@ func NewPathProcessor(
9595
clientUpdateThresholdTime time.Duration,
9696
flushInterval time.Duration,
9797
) *PathProcessor {
98-
if flushInterval == 0 {
99-
// "disable" periodic flushing by using a large value.
100-
flushInterval = 200 * 24 * 365 * time.Hour
101-
}
102-
return &PathProcessor{
98+
pp := &PathProcessor{
10399
log: log,
104100
pathEnd1: newPathEndRuntime(log, pathEnd1, metrics),
105101
pathEnd2: newPathEndRuntime(log, pathEnd2, metrics),
@@ -109,10 +105,33 @@ func NewPathProcessor(
109105
flushInterval: flushInterval,
110106
metrics: metrics,
111107
}
108+
if flushInterval == 0 {
109+
pp.disablePeriodicFlush()
110+
}
111+
return pp
112+
}
113+
114+
// disablePeriodicFlush will "disable" periodic flushing by using a large value.
115+
func (pp *PathProcessor) disablePeriodicFlush() {
116+
pp.flushInterval = 200 * 24 * 365 * time.Hour
112117
}
113118

114119
func (pp *PathProcessor) SetMessageLifecycle(messageLifecycle MessageLifecycle) {
115120
pp.messageLifecycle = messageLifecycle
121+
if !pp.shouldFlush() {
122+
// disable flushing when termination conditions are set, e.g. connection/channel handshakes
123+
pp.disablePeriodicFlush()
124+
}
125+
}
126+
127+
func (pp *PathProcessor) shouldFlush() bool {
128+
if pp.messageLifecycle == nil {
129+
return true
130+
}
131+
if _, ok := pp.messageLifecycle.(*FlushLifecycle); ok {
132+
return true
133+
}
134+
return false
116135
}
117136

118137
// TEST USE ONLY
@@ -299,7 +318,7 @@ func (pp *PathProcessor) Run(ctx context.Context, cancel func()) {
299318
continue
300319
}
301320

302-
if !pp.initialFlushComplete {
321+
if pp.shouldFlush() && !pp.initialFlushComplete {
303322
pp.flush(ctx)
304323
pp.initialFlushComplete = true
305324
} else if pp.shouldTerminateForFlushComplete(ctx, cancel) {

relayer/processor/path_processor_internal.go

+3
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,9 @@ func queryPacketCommitments(
781781
for i, p := range c.Commitments {
782782
commitments[k][i] = p.Sequence
783783
}
784+
sort.SliceStable(commitments[k], func(i, j int) bool {
785+
return commitments[k][i] < commitments[k][j]
786+
})
784787
return nil
785788
}
786789
}

0 commit comments

Comments
 (0)