Skip to content

Commit

Permalink
[CCIP-3522] refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
valerii-kabisov-cll committed Nov 11, 2024
1 parent 2bc28d1 commit f12ffe8
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions deployment/address_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ type AddressBookMap struct {
mtx sync.RWMutex
}

// Save will save an address for a given chain selector. It will error if there is a conflicting existing address.
func (m *AddressBookMap) Save(chainSelector uint64, address string, typeAndVersion TypeAndVersion) error {
// save will save an address for a given chain selector. It will error if there is a conflicting existing address.
func (m *AddressBookMap) save(chainSelector uint64, address string, typeAndVersion TypeAndVersion) error {
_, exists := chainsel.ChainBySelector(chainSelector)
if !exists {
return errors.Wrapf(ErrInvalidChainSelector, "chain selector %d", chainSelector)
Expand All @@ -112,9 +112,6 @@ func (m *AddressBookMap) Save(chainSelector uint64, address string, typeAndVersi
return fmt.Errorf("type cannot be empty")
}

m.mtx.Lock()
defer m.mtx.Unlock()

if _, exists := m.AddressesByChain[chainSelector]; !exists {
// First time chain add, create map
m.AddressesByChain[chainSelector] = make(map[string]TypeAndVersion)
Expand All @@ -126,6 +123,14 @@ func (m *AddressBookMap) Save(chainSelector uint64, address string, typeAndVersi
return nil
}

// Save will save an address for a given chain selector. It will error if there is a conflicting existing address.
// thread safety version of the save method
func (m *AddressBookMap) Save(chainSelector uint64, address string, typeAndVersion TypeAndVersion) error {
m.mtx.Lock()
defer m.mtx.Unlock()
return m.save(chainSelector, address, typeAndVersion)
}

func (m *AddressBookMap) Addresses() (map[uint64]map[string]TypeAndVersion, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
Expand Down Expand Up @@ -168,13 +173,9 @@ func (m *AddressBookMap) Merge(ab AddressBook) error {

for chainSelector, chainAddresses := range addresses {
for address, typeAndVersion := range chainAddresses {
if _, exists := m.AddressesByChain[chainSelector]; !exists {
m.AddressesByChain[chainSelector] = make(map[string]TypeAndVersion)
}
if _, exists := m.AddressesByChain[chainSelector][address]; exists {
return fmt.Errorf("address %s already exists for chain %d", address, chainSelector)
if err := m.save(chainSelector, address, typeAndVersion); err != nil {
return err
}
m.AddressesByChain[chainSelector][address] = typeAndVersion
}
}
return nil
Expand Down

0 comments on commit f12ffe8

Please sign in to comment.