Skip to content

Commit f1c5807

Browse files
committed
[CAL][2] - implement metadata() and GetContractAddress()
1 parent 25cef37 commit f1c5807

File tree

7 files changed

+126
-12
lines changed

7 files changed

+126
-12
lines changed

mocks/pkg/contractreader/extended.go

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/chainaccessor/legacy_accessor.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package chainaccessor
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
"github.com/smartcontractkit/chainlink-common/pkg/logger"
@@ -40,13 +41,35 @@ func NewLegacyAccessor(
4041
}
4142

4243
func (l *LegacyAccessor) Metadata() cciptypes.AccessorMetadata {
43-
// TODO(NONEVM-1865): implement
44-
panic("implement me")
44+
allBindings := l.contractReader.GetAllBindings()
45+
contracts := make(map[string]cciptypes.UnknownAddress, len(allBindings))
46+
for contractName, binding := range allBindings {
47+
addressBytes, err := l.addrCodec.AddressStringToBytes(binding[0].Binding.Address, l.chainSelector)
48+
if err != nil {
49+
l.lggr.Errorf("failed to convert address to bytes : %v", err)
50+
continue
51+
}
52+
contracts[contractName] = addressBytes
53+
}
54+
55+
return cciptypes.AccessorMetadata{
56+
ChainSelector: l.chainSelector,
57+
Contracts: contracts,
58+
}
4559
}
4660

4761
func (l *LegacyAccessor) GetContractAddress(contractName string) ([]byte, error) {
48-
// TODO(NONEVM-1865): implement
49-
panic("implement me")
62+
bindings := l.contractReader.GetBindings(contractName)
63+
if len(bindings) != 1 {
64+
return nil, fmt.Errorf("expected one binding for the %s contract, got %d", contractName, len(bindings))
65+
}
66+
67+
addressBytes, err := l.addrCodec.AddressStringToBytes(bindings[0].Binding.Address, l.chainSelector)
68+
if err != nil {
69+
return nil, fmt.Errorf("convert address %s to bytes: %w", bindings[0].Binding.Address, err)
70+
}
71+
72+
return addressBytes, nil
5073
}
5174

5275
func (l *LegacyAccessor) GetChainFeeComponents(

pkg/contractreader/extended.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type Extended interface {
3333
// GetBindings returns current bindings for a given contract reader.
3434
GetBindings(contractName string) []ExtendedBoundContract
3535

36+
// GetAllBindings returns all bindings for all contracts associated with this reader.
37+
GetAllBindings() map[string][]ExtendedBoundContract
38+
3639
// ExtendedQueryKey performs automatic binding from contractName to the first bound contract.
3740
// An error is generated if there are more than one bound contract for the contractName.
3841
ExtendedQueryKey(
@@ -298,6 +301,18 @@ func (e *extendedContractReader) GetBindings(contractName string) []ExtendedBoun
298301
return bindings
299302
}
300303

304+
func (e *extendedContractReader) GetAllBindings() map[string][]ExtendedBoundContract {
305+
e.mu.RLock()
306+
defer e.mu.RUnlock()
307+
308+
allBindings := make(map[string][]ExtendedBoundContract, len(e.contractBindingsByName))
309+
for key, bindings := range e.contractBindingsByName {
310+
allBindings[key] = bindings
311+
}
312+
313+
return allBindings
314+
}
315+
301316
func (e *extendedContractReader) bindingExists(b types.BoundContract) bool {
302317
e.mu.RLock()
303318
defer e.mu.RUnlock()

pkg/reader/ccip.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,18 @@ func (r *ccipChainReader) MsgsBetweenSeqNums(
510510
ctx context.Context, sourceChainSelector cciptypes.ChainSelector, seqNumRange cciptypes.SeqNumRange,
511511
) ([]cciptypes.Message, error) {
512512
lggr := logutil.WithContextValues(ctx, r.lggr)
513-
if err := validateReaderExistence(r.contractReaders, sourceChainSelector); err != nil {
513+
514+
if err := validateAccessorExistence(r.accessors, sourceChainSelector); err != nil {
514515
return nil, err
515516
}
516-
517-
onRampAddress, err := r.GetContractAddress(consts.ContractNameOnRamp, sourceChainSelector)
517+
onRampAddress, err := r.accessors[sourceChainSelector].GetContractAddress(consts.ContractNameOnRamp)
518518
if err != nil {
519519
return nil, fmt.Errorf("get onRamp address: %w", err)
520520
}
521521

522+
if err := validateReaderExistence(r.contractReaders, sourceChainSelector); err != nil {
523+
return nil, err
524+
}
522525
seq, err := r.contractReaders[sourceChainSelector].ExtendedQueryKey(
523526
ctx,
524527
consts.ContractNameOnRamp,
@@ -557,7 +560,7 @@ func (r *ccipChainReader) MsgsBetweenSeqNums(
557560
return nil, fmt.Errorf("failed to query onRamp: %w", err)
558561
}
559562

560-
onRampAddressAfterQuery, err := r.GetContractAddress(consts.ContractNameOnRamp, sourceChainSelector)
563+
onRampAddressAfterQuery, err := r.accessors[sourceChainSelector].GetContractAddress(consts.ContractNameOnRamp)
561564
if err != nil {
562565
return nil, fmt.Errorf("get onRamp address after query: %w", err)
563566
}
@@ -1126,7 +1129,11 @@ func (r *ccipChainReader) GetRMNRemoteConfig(ctx context.Context) (cciptypes.Rem
11261129

11271130
// RMNRemote address stored in the offramp static config is actually the proxy contract address.
11281131
// Here we will get the RMNRemote address from the proxy contract by calling the RMNProxy contract.
1129-
proxyContractAddress, err := r.GetContractAddress(consts.ContractNameRMNRemote, r.destChain)
1132+
destChainAccessor := r.accessors[r.destChain]
1133+
if destChainAccessor == nil {
1134+
return cciptypes.RemoteConfig{}, fmt.Errorf("chain accessor not found for dest chain %d", r.destChain)
1135+
}
1136+
proxyContractAddress, err := destChainAccessor.GetContractAddress(consts.ContractNameRMNRemote)
11301137
if err != nil {
11311138
return cciptypes.RemoteConfig{}, fmt.Errorf("get RMNRemote proxy contract address: %w", err)
11321139
}
@@ -1380,6 +1387,7 @@ func (r *ccipChainReader) Sync(ctx context.Context, contracts ContractAddresses)
13801387
return errors.Join(errs...)
13811388
}
13821389

1390+
// TODO(NONEVM-1865): remove this function once from CCIPReader interface once CAL migration is complete.
13831391
func (r *ccipChainReader) GetContractAddress(contractName string, chain cciptypes.ChainSelector) ([]byte, error) {
13841392
return r.donAddressBook.GetContractAddress(addressbook.ContractName(contractName), chain)
13851393
}

pkg/reader/ccip_interface.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/smartcontractkit/chainlink-common/pkg/types"
1111
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"
1212

13+
"github.com/smartcontractkit/chainlink-ccip/pkg/chainaccessor"
1314
"github.com/smartcontractkit/chainlink-ccip/pkg/contractreader"
1415
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
1516
)
@@ -149,7 +150,7 @@ func NewCCIPChainReader(
149150
func NewCCIPReaderWithExtendedContractReaders(
150151
ctx context.Context,
151152
lggr logger.Logger,
152-
contractReaders map[cciptypes.ChainSelector]contractreader.Extended,
153+
extendedContractReaders map[cciptypes.ChainSelector]contractreader.Extended,
153154
contractWriters map[cciptypes.ChainSelector]types.ContractWriter,
154155
destChain cciptypes.ChainSelector,
155156
offrampAddress []byte,
@@ -160,9 +161,19 @@ func NewCCIPReaderWithExtendedContractReaders(
160161
// Panic here since right now this is only called from tests in core
161162
panic(fmt.Errorf("failed to create CCIP reader: %w", err))
162163
}
163-
for ch, extendedCr := range contractReaders {
164+
var cas = make(map[cciptypes.ChainSelector]cciptypes.ChainAccessor)
165+
for ch, extendedCr := range extendedContractReaders {
164166
cr.WithExtendedContractReader(ch, extendedCr)
167+
cas[ch] = chainaccessor.NewLegacyAccessor(
168+
lggr,
169+
ch,
170+
extendedCr,
171+
contractWriters[ch],
172+
addrCodec,
173+
)
165174
}
175+
176+
cr.accessors = cas
166177
return cr
167178
}
168179

pkg/reader/helpers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,13 @@ func validateReaderExistence[T contractreader.ContractReaderFacade](
8080
}
8181
return nil
8282
}
83+
84+
func validateAccessorExistence(
85+
accessors map[cciptypes.ChainSelector]cciptypes.ChainAccessor,
86+
chainSelector cciptypes.ChainSelector,
87+
) error {
88+
if _, exists := accessors[chainSelector]; !exists {
89+
return fmt.Errorf("chain accessor not found for chain %d", chainSelector)
90+
}
91+
return nil
92+
}

pkg/types/ccipocr3/chain_accessor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type AllAccessors interface {
3636
// GetContractAddress returns the contract address that is registered for the provided contract name and chain.
3737
// WARNING: This function will fail if the oracle does not support the requested chain.
3838
//
39-
// Deprecated: use Metadata() instead.
39+
// TODO(NONEVM-1865): do we want to mark this as deprecated in favor of Metadata()?
4040
GetContractAddress(contractName string) ([]byte, error)
4141

4242
// GetAllConfig looks up all configurations available to the accessor. This

0 commit comments

Comments
 (0)