Skip to content

Commit

Permalink
Add Extended versions of QueryKey and GetLatestValue.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Sep 19, 2024
1 parent 373d64f commit b726b6d
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 53 deletions.
113 changes: 113 additions & 0 deletions mocks/pkg/contractreader/extended.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions pkg/contractreader/extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink-common/pkg/types/query"
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"

"github.com/smartcontractkit/chainlink-ccip/internal/libs/slicelib"
"github.com/smartcontractkit/chainlink-ccip/internal/reader/contractreader"
Expand All @@ -16,6 +18,26 @@ import (
type Extended interface {
contractreader.ContractReaderFacade
GetBindings(contractName string) []ExtendedBoundContract

// ExtendedQueryKey performs automatic binding from contractName to the first bound contract.
// An error is generated if there are more than one bound contract for the contractName.
ExtendedQueryKey(
ctx context.Context,
contractName string,
filter query.KeyFilter,
limitAndSort query.LimitAndSort,
sequenceDataType any,
) ([]types.Sequence, error)

// ExtendedGetLatestValue performs automatic binding from contractName to the first bound contract, and
// constructs a read identifier for a given method name. An error is generated if there are more than one
// bound contract for the contractName.
ExtendedGetLatestValue(
ctx context.Context,
contractName, methodName string,
confidenceLevel primitives.ConfidenceLevel,
params, returnVal any,
) error
}

type ExtendedBoundContract struct {
Expand All @@ -38,6 +60,51 @@ func NewExtendedContractReader(baseContractReader contractreader.ContractReaderF
}
}

func (e *extendedContractReader) ExtendedQueryKey(
ctx context.Context,
contractName string,
filter query.KeyFilter,
limitAndSort query.LimitAndSort,
sequenceDataType any,
) ([]types.Sequence, error) {
extendedBindings := e.GetBindings(contractName)
if len(extendedBindings) != 1 {
return nil, fmt.Errorf(
"ExtendedQueryKey: expected one binding for %s contract, got %d", contractName, len(extendedBindings))
}
contractBinding := extendedBindings[0].Binding
return e.QueryKey(
ctx,
contractBinding,
filter,
limitAndSort,
sequenceDataType,
)
}

func (e *extendedContractReader) ExtendedGetLatestValue(
ctx context.Context,
contractName, methodName string,
confidenceLevel primitives.ConfidenceLevel,
params, returnVal any,
) error {
extendedBindings := e.GetBindings(contractName)
if len(extendedBindings) != 1 {
return fmt.Errorf(
"ExtendedGetLatestValue: expected one binding for the %s contract, got %d", contractName, len(extendedBindings))
}
contractBinding := extendedBindings[0].Binding
readIdentifier := contractBinding.ReadIdentifier(methodName)

return e.GetLatestValue(
ctx,
readIdentifier,
confidenceLevel,
params,
returnVal,
)
}

func (e *extendedContractReader) Bind(ctx context.Context, allBindings []types.BoundContract) error {
validBindings := slicelib.Filter(allBindings, func(b types.BoundContract) bool { return !e.bindingExists(b) })
if len(validBindings) == 0 {
Expand Down
71 changes: 18 additions & 53 deletions pkg/reader/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,9 @@ func (r *ccipChainReader) CommitReportsGTETimestamp(

ev := CommitReportAcceptedEvent{}

extendedBindings := r.contractReaders[dest].GetBindings(consts.ContractNameOffRamp)
if len(extendedBindings) != 1 {
return nil, fmt.Errorf("expected one binding for offRamp contract, got %d", len(extendedBindings))
}
contractBinding := extendedBindings[0].Binding
iter, err := r.contractReaders[dest].QueryKey(
iter, err := r.contractReaders[dest].ExtendedQueryKey(
ctx,
contractBinding,
consts.ContractNameOffRamp,
query.KeyFilter{
Key: consts.EventNameCommitReportAccepted,
Expressions: []query.Expression{
Expand Down Expand Up @@ -232,14 +227,9 @@ func (r *ccipChainReader) ExecutedMessageRanges(

dataTyp := ExecutionStateChangedEvent{}

extendedBindings := r.contractReaders[dest].GetBindings(consts.ContractNameOffRamp)
if len(extendedBindings) != 1 {
return nil, fmt.Errorf("expected one binding for offRamp contract, got %d", len(extendedBindings))
}
contractBinding := extendedBindings[0].Binding
iter, err := r.contractReaders[dest].QueryKey(
iter, err := r.contractReaders[dest].ExtendedQueryKey(
ctx,
contractBinding,
consts.ContractNameOffRamp,
query.KeyFilter{
Key: consts.EventNameExecutionStateChanged,
Expressions: []query.Expression{
Expand Down Expand Up @@ -295,14 +285,9 @@ func (r *ccipChainReader) MsgsBetweenSeqNums(
Message cciptypes.Message
}

bindings := r.contractReaders[sourceChainSelector].GetBindings(consts.ContractNameOnRamp)
if len(bindings) != 1 {
return nil, fmt.Errorf("expected one binding for the OnRamp contract, got %d", len(bindings))
}

seq, err := r.contractReaders[sourceChainSelector].QueryKey(
seq, err := r.contractReaders[sourceChainSelector].ExtendedQueryKey(
ctx,
bindings[0].Binding,
consts.ContractNameOnRamp,
query.KeyFilter{
Key: consts.EventNameCCIPMessageSent,
Expressions: []query.Expression{
Expand Down Expand Up @@ -365,16 +350,11 @@ func (r *ccipChainReader) GetExpectedNextSequenceNumber(
return 0, err
}

extendedBindings := r.contractReaders[sourceChainSelector].GetBindings(consts.ContractNameOnRamp)
if len(extendedBindings) != 1 {
return 0, fmt.Errorf("expected one binding for the OnRamp contract, got %d", len(extendedBindings))
}
contractBinding := extendedBindings[0].Binding

var expectedNextSequenceNumber uint64
err := r.contractReaders[sourceChainSelector].GetLatestValue(
err := r.contractReaders[sourceChainSelector].ExtendedGetLatestValue(
ctx,
contractBinding.ReadIdentifier(consts.MethodNameGetExpectedNextSequenceNumber),
consts.ContractNameOnRamp,
consts.MethodNameGetExpectedNextSequenceNumber,
primitives.Unconfirmed,
map[string]any{
"destChainSelector": destChainSelector,
Expand Down Expand Up @@ -432,16 +412,11 @@ func (r *ccipChainReader) Nonces(
return fmt.Errorf("failed to convert address %s to bytes: %w", address, err)
}

extendedBindings := r.contractReaders[destChainSelector].GetBindings(consts.ContractNameNonceManager)
if len(extendedBindings) != 1 {
return fmt.Errorf("expected one binding for the NonceManager contract, got %d", len(extendedBindings))
}
contractBinding := extendedBindings[0].Binding

var resp uint64
err = r.contractReaders[destChainSelector].GetLatestValue(
err = r.contractReaders[destChainSelector].ExtendedGetLatestValue(
ctx,
contractBinding.ReadIdentifier(consts.MethodNameGetInboundNonce),
consts.ContractNameNonceManager,
consts.MethodNameGetInboundNonce,
primitives.Unconfirmed,
map[string]any{
"sourceChainSelector": sourceChainSelector,
Expand Down Expand Up @@ -628,14 +603,10 @@ func (r *ccipChainReader) getSourceChainsConfig(
chainSel := chainSel
eg.Go(func() error {
resp := sourceChainConfig{}
extendedBindings := r.contractReaders[r.destChain].GetBindings(consts.ContractNameOffRamp)
if len(extendedBindings) != 1 {
return fmt.Errorf("expected one binding for offRamp contract, got %d", len(extendedBindings))
}
contractBinding := extendedBindings[0].Binding
err := r.contractReaders[r.destChain].GetLatestValue(
err := r.contractReaders[r.destChain].ExtendedGetLatestValue(
ctx,
contractBinding.ReadIdentifier(consts.MethodNameGetSourceChainConfig),
consts.ContractNameOffRamp,
consts.MethodNameGetSourceChainConfig,
primitives.Unconfirmed,
map[string]any{
"sourceChainSelector": chainSel,
Expand Down Expand Up @@ -697,17 +668,11 @@ func (r *ccipChainReader) getOfframpStaticConfig(
return offrampStaticChainConfig{}, err
}

extendedBindings := r.contractReaders[chain].GetBindings(consts.ContractNameOffRamp)
if len(extendedBindings) != 1 {
return offrampStaticChainConfig{},
fmt.Errorf("expected one binding for Offramp contract, got %d", len(extendedBindings))
}
contractBinding := extendedBindings[0].Binding

resp := offrampStaticChainConfig{}
err := r.contractReaders[chain].GetLatestValue(
err := r.contractReaders[chain].ExtendedGetLatestValue(
ctx,
contractBinding.ReadIdentifier(consts.MethodNameOfframpGetStaticConfig),
consts.ContractNameOffRamp,
consts.MethodNameOfframpGetStaticConfig,
primitives.Unconfirmed,
map[string]any{},
&resp,
Expand Down

0 comments on commit b726b6d

Please sign in to comment.