Skip to content

Commit

Permalink
Improve CCIP Contract Reader Querying
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Nov 1, 2024
1 parent 86885ce commit 9d45bb2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
3 changes: 2 additions & 1 deletion pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ const (
const (
EventAttributeSequenceNumber = "SequenceNumber"
EventAttributeSourceChain = "SourceChain"
EventAttributeDestChain = "destChain"
EventAttributeDestChain = "DestChain"
EventAttributeState = "State"
)

// Dedicated filters
Expand Down
64 changes: 32 additions & 32 deletions pkg/reader/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/types/query"
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"

"github.com/smartcontractkit/chainlink-ccip/internal/plugincommon/consensus"

rmntypes "github.com/smartcontractkit/chainlink-ccip/commit/merkleroot/rmn/types"
typeconv "github.com/smartcontractkit/chainlink-ccip/internal/libs/typeconv"
"github.com/smartcontractkit/chainlink-ccip/internal/plugintypes"
Expand Down Expand Up @@ -127,6 +125,7 @@ func (r *ccipChainReader) CommitReportsGTETimestamp(
query.KeyFilter{
Key: consts.EventNameCommitReportAccepted,
Expressions: []query.Expression{
query.Timestamp(uint64(ts.Unix()), primitives.Gte),
query.Confidence(primitives.Finalized),
},
},
Expand All @@ -150,15 +149,6 @@ func (r *ccipChainReader) CommitReportsGTETimestamp(
return nil, fmt.Errorf("unexpected type %T while expecting a commit report", item)
}

valid := item.Timestamp >= uint64(ts.Unix())
if !valid {
r.lggr.Debugw("commit report too old, skipping", "report", ev, "item", item,
"destChain", dest,
"ts", ts,
"limit", limit)
continue
}

r.lggr.Debugw("processing commit report", "report", ev, "item", item)

merkleRoots := make([]cciptypes.MerkleRootChain, 0, len(ev.MerkleRoots))
Expand Down Expand Up @@ -245,6 +235,21 @@ func (r *ccipChainReader) ExecutedMessageRanges(
query.KeyFilter{
Key: consts.EventNameExecutionStateChanged,
Expressions: []query.Expression{
query.Comparator(consts.EventAttributeSourceChain, primitives.ValueComparator{
Value: source,
Operator: primitives.Eq,
}),
query.Comparator(consts.EventAttributeSequenceNumber, primitives.ValueComparator{
Value: seqNumRange.Start(),
Operator: primitives.Gte,
}, primitives.ValueComparator{
Value: seqNumRange.End(),
Operator: primitives.Lte,
}),
query.Comparator(consts.EventAttributeState, primitives.ValueComparator{
Value: 0,
Operator: primitives.Gt,
}),
query.Confidence(primitives.Finalized),
},
},
Expand All @@ -263,17 +268,6 @@ func (r *ccipChainReader) ExecutedMessageRanges(
if !ok {
return nil, fmt.Errorf("failed to cast %T to executionStateChangedEvent", item.Data)
}

// todo: filter via the query
valid := stateChange.SourceChainSelector == source &&
stateChange.SequenceNumber >= seqNumRange.Start() &&
stateChange.SequenceNumber <= seqNumRange.End() &&
stateChange.State > 0
if !valid {
r.lggr.Debugw("skipping invalid state change", "stateChange", stateChange)
continue
}

executed = append(executed, cciptypes.NewSeqNumRange(stateChange.SequenceNumber, stateChange.SequenceNumber))
}

Expand Down Expand Up @@ -353,6 +347,21 @@ func (r *ccipChainReader) MsgsBetweenSeqNums(
query.KeyFilter{
Key: consts.EventNameCCIPMessageSent,
Expressions: []query.Expression{
query.Comparator(consts.EventAttributeSourceChain, primitives.ValueComparator{
Value: sourceChainSelector,
Operator: primitives.Eq,
}),
query.Comparator(consts.EventAttributeDestChain, primitives.ValueComparator{
Value: r.destChain,
Operator: primitives.Eq,
}),
query.Comparator(consts.EventAttributeSequenceNumber, primitives.ValueComparator{
Value: seqNumRange.Start(),
Operator: primitives.Gte,
}, primitives.ValueComparator{
Value: seqNumRange.End(),
Operator: primitives.Lte,
}),
query.Confidence(primitives.Finalized),
},
},
Expand Down Expand Up @@ -380,17 +389,8 @@ func (r *ccipChainReader) MsgsBetweenSeqNums(
return nil, fmt.Errorf("failed to cast %v to Message", item.Data)
}

// todo: filter via the query
valid := msg.Message.Header.SourceChainSelector == sourceChainSelector &&
msg.Message.Header.DestChainSelector == r.destChain &&
msg.Message.Header.SequenceNumber >= seqNumRange.Start() &&
msg.Message.Header.SequenceNumber <= seqNumRange.End()

msg.Message.Header.OnRamp = onRampAddress

if valid {
msgs = append(msgs, msg.Message.ToMessage())
}
msgs = append(msgs, msg.Message.ToMessage())
}

r.lggr.Infow("decoded messages between sequence numbers", "msgs", msgs,
Expand Down

0 comments on commit 9d45bb2

Please sign in to comment.