Skip to content

Commit

Permalink
Fix state view (#14532)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorwstein authored Sep 23, 2024
1 parent 6a39c12 commit 6b30027
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
15 changes: 10 additions & 5 deletions integration-tests/deployment/address_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var (
ErrInvalidChainSelector = fmt.Errorf("invalid chain selector")
ErrInvalidAddress = fmt.Errorf("invalid address")
ErrChainNotFound = fmt.Errorf("chain not found")
)

// ContractType is a simple string type for identifying contract types.
Expand Down Expand Up @@ -90,7 +91,7 @@ type AddressBookMap struct {
func (m *AddressBookMap) Save(chainSelector uint64, address string, typeAndVersion TypeAndVersion) error {
_, exists := chainsel.ChainBySelector(chainSelector)
if !exists {
return errors.Wrapf(ErrInvalidChainSelector, "chain selector %d not found", chainSelector)
return errors.Wrapf(ErrInvalidChainSelector, "chain selector %d", chainSelector)
}
if address == "" || address == common.HexToAddress("0x0").Hex() {
return errors.Wrap(ErrInvalidAddress, "address cannot be empty")
Expand Down Expand Up @@ -119,11 +120,15 @@ func (m *AddressBookMap) Addresses() (map[uint64]map[string]TypeAndVersion, erro
return m.AddressesByChain, nil
}

func (m *AddressBookMap) AddressesForChain(chain uint64) (map[string]TypeAndVersion, error) {
if _, exists := m.AddressesByChain[chain]; !exists {
return nil, fmt.Errorf("chain %d not found", chain)
func (m *AddressBookMap) AddressesForChain(chainSelector uint64) (map[string]TypeAndVersion, error) {
_, exists := chainsel.ChainBySelector(chainSelector)
if !exists {
return nil, errors.Wrapf(ErrInvalidChainSelector, "chain selector %d", chainSelector)
}
if _, exists := m.AddressesByChain[chainSelector]; !exists {
return nil, errors.Wrapf(ErrChainNotFound, "chain selector %d", chainSelector)
}
return m.AddressesByChain[chain], nil
return m.AddressesByChain[chainSelector], nil
}

// Attention this will mutate existing book
Expand Down
10 changes: 9 additions & 1 deletion integration-tests/deployment/address_book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,24 @@ func TestAddressBook_Save(t *testing.T) {
err := ab.Save(chainsel.TEST_90000001.Selector, addr1, onRamp100)
require.NoError(t, err)

// Check input validation
// Invalid address
err = ab.Save(chainsel.TEST_90000001.Selector, "asdlfkj", onRamp100)
require.Error(t, err)
assert.Equal(t, errors.Is(err, ErrInvalidAddress), true, "err %s", err)

// Valid chain but not present.
_, err = ab.AddressesForChain(chainsel.TEST_90000002.Selector)
assert.Equal(t, errors.Is(err, ErrChainNotFound), true, "err %s", err)

// Invalid selector
err = ab.Save(0, addr1, onRamp100)
require.Error(t, err)
assert.Equal(t, errors.Is(err, ErrInvalidChainSelector), true)

// Duplicate
err = ab.Save(chainsel.TEST_90000001.Selector, addr1, onRamp100)
require.Error(t, err)

// Zero address
err = ab.Save(chainsel.TEST_90000001.Selector, common.HexToAddress("0x0").Hex(), onRamp100)
require.Error(t, err)
Expand Down
20 changes: 13 additions & 7 deletions integration-tests/deployment/ccip/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"

chainsel "github.com/smartcontractkit/chain-selectors"

"github.com/smartcontractkit/chainlink/integration-tests/deployment"
Expand Down Expand Up @@ -35,6 +34,8 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/aggregator_v3_interface"
)

// CCIPChainState holds a Go binding for all the currently deployed CCIP contracts
// on a chain. If a binding is nil, it means here is no such contract on the chain.
type CCIPChainState struct {
OnRamp *onramp.OnRamp
OffRamp *offramp.OffRamp
Expand Down Expand Up @@ -177,12 +178,17 @@ func LoadOnchainState(e deployment.Environment, ab deployment.AddressBook) (CCIP
state := CCIPOnChainState{
Chains: make(map[uint64]CCIPChainState),
}
addresses, err := ab.Addresses()
if err != nil {
return state, errors.Wrap(err, "could not get addresses")
}
for chainSelector, addresses := range addresses {
chainState, err := LoadChainState(e.Chains[chainSelector], addresses)
for chainSelector, chain := range e.Chains {
addresses, err := ab.AddressesForChain(chainSelector)
if err != nil {
// Chain not found in address book, initialize empty
if errors.Is(err, deployment.ErrChainNotFound) {
addresses = make(map[string]deployment.TypeAndVersion)
} else {
return state, err
}
}
chainState, err := LoadChainState(chain, addresses)
if err != nil {
return state, err
}
Expand Down

0 comments on commit 6b30027

Please sign in to comment.