Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCIP-3522] address book remove feature #15148

Merged
merged 11 commits into from
Nov 18, 2024
5 changes: 5 additions & 0 deletions .changeset/breezy-suits-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

#added address book remove feature
26 changes: 26 additions & 0 deletions deployment/address_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type AddressBook interface {
AddressesForChain(chain uint64) (map[string]TypeAndVersion, error)
// Allows for merging address books (e.g. new deployments with existing ones)
Merge(other AddressBook) error
Remove(ab AddressBook) error
}

type AddressBookMap struct {
valerii-kabisov-cll marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -149,6 +150,31 @@ func (m *AddressBookMap) Merge(ab AddressBook) error {
return nil
}

// Remove removes the address book addresses specified via the argument from the AddressBookMap.
// Errors if all the addresses in the given address book are not contained in the AddressBookMap.
func (m *AddressBookMap) Remove(ab AddressBook) error {
addresses, err := ab.Addresses()
if err != nil {
return err
}

addressNotFound := true
for chainSelector, chainAddresses := range addresses {
for address, _ := range chainAddresses {
if _, exists := m.AddressesByChain[chainSelector][address]; exists {
addressNotFound = false
delete(m.AddressesByChain[chainSelector], address)
}
}
}

if addressNotFound {
valerii-kabisov-cll marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("AddressBookMap does not contain any address from the given address book")
}

return nil
}

// TODO: Maybe could add an environment argument
// which would ensure only mainnet/testnet chain selectors are used
// for further safety?
Expand Down
44 changes: 44 additions & 0 deletions deployment/address_book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,47 @@ func TestAddressBook_Merge(t *testing.T) {
},
})
}

func TestAddressBook_Remove(t *testing.T) {
valerii-kabisov-cll marked this conversation as resolved.
Show resolved Hide resolved
onRamp100 := NewTypeAndVersion("OnRamp", Version1_0_0)
onRamp110 := NewTypeAndVersion("OnRamp", Version1_1_0)
addr1 := common.HexToAddress("0x1").String()
addr2 := common.HexToAddress("0x2").String()
addr3 := common.HexToAddress("0x3").String()

baseAB := NewMemoryAddressBookFromMap(map[uint64]map[string]TypeAndVersion{
chainsel.TEST_90000001.Selector: {
addr1: onRamp100,
addr2: onRamp100,
},
chainsel.TEST_90000002.Selector: {
addr1: onRamp110,
addr3: onRamp110,
},
})

testAB := NewMemoryAddressBookFromMap(map[uint64]map[string]TypeAndVersion{
chainsel.TEST_90000001.Selector: {
addr1: onRamp100, // should be removed
addr3: onRamp100, // doesn't exist in TEST_90000001.Selector
},
})

expectingAB := NewMemoryAddressBookFromMap(map[uint64]map[string]TypeAndVersion{
chainsel.TEST_90000001.Selector: {
addr2: onRamp100,
},
chainsel.TEST_90000002.Selector: {
addr1: onRamp110,
addr3: onRamp110,
},
})

// first attempt,
require.NoError(t, baseAB.Remove(testAB))
require.EqualValues(t, baseAB, expectingAB)

// Remove method should error in case no address was matched with the given address book.
require.Error(t, baseAB.Remove(testAB))
require.EqualValues(t, baseAB, expectingAB)
}
Loading