Skip to content

Commit fb0c82e

Browse files
revert PR 156 (#161)
1 parent c9dc0ed commit fb0c82e

File tree

3 files changed

+5
-230
lines changed

3 files changed

+5
-230
lines changed

.changeset/fancy-ducks-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"chainlink-deployments-framework": patch
3+
---
4+
5+
Revert PR 156 as not needed in CLDF

deployment/address_book.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package deployment
22

33
import (
4-
"encoding/json"
54
"errors"
65
"fmt"
7-
"sort"
86
"strings"
97
"sync"
108

@@ -128,80 +126,6 @@ type OrderedAddressEntry struct {
128126
TypeAndVersion TypeAndVersion `json:"typeAndVersion"`
129127
}
130128

131-
// MarshalJSON implements custom JSON marshaling for AddressesByChain
132-
// to ensure deterministic ordering: chain selectors numerically, addresses alphabetically (case-insensitive)
133-
func (abc AddressesByChain) MarshalJSON() ([]byte, error) {
134-
// Get all chain selectors and sort them numerically
135-
chainSelectors := make([]uint64, 0, len(abc))
136-
for chainSelector := range abc {
137-
chainSelectors = append(chainSelectors, chainSelector)
138-
}
139-
sort.Slice(chainSelectors, func(i, j int) bool {
140-
return chainSelectors[i] < chainSelectors[j]
141-
})
142-
143-
// Build ordered result
144-
result := make([]OrderedChain, 0, len(chainSelectors))
145-
146-
for _, chainSelector := range chainSelectors {
147-
chainAddresses := abc[chainSelector]
148-
149-
// Get all addresses and sort them alphabetically
150-
addresses := make([]string, 0, len(chainAddresses))
151-
for address := range chainAddresses {
152-
addresses = append(addresses, address)
153-
}
154-
sort.Slice(addresses, func(i, j int) bool {
155-
return strings.ToLower(addresses[i]) < strings.ToLower(addresses[j]) // Using ToLower to ensure case-insensitive sorting
156-
})
157-
158-
// Build ordered address entries
159-
orderedAddresses := make([]OrderedAddressEntry, 0, len(addresses))
160-
for _, address := range addresses {
161-
orderedAddresses = append(orderedAddresses, OrderedAddressEntry{
162-
Address: address,
163-
TypeAndVersion: chainAddresses[address],
164-
})
165-
}
166-
167-
result = append(result, OrderedChain{
168-
ChainSelector: chainSelector,
169-
Addresses: orderedAddresses,
170-
})
171-
}
172-
173-
return json.Marshal(result)
174-
}
175-
176-
// UnmarshalJSON implements custom JSON unmarshaling for AddressesByChain
177-
func (abc *AddressesByChain) UnmarshalJSON(data []byte) error {
178-
var orderedChains []OrderedChain
179-
if err := json.Unmarshal(data, &orderedChains); err != nil {
180-
return err
181-
}
182-
183-
// Initialize the map if it's nil
184-
if *abc == nil {
185-
*abc = make(AddressesByChain)
186-
}
187-
188-
// Convert back to map structure
189-
for _, chain := range orderedChains {
190-
chainAddresses := make(map[string]TypeAndVersion)
191-
for _, addrEntry := range chain.Addresses {
192-
tv := addrEntry.TypeAndVersion
193-
// Ensure LabelSet is properly initialized if nil
194-
if tv.Labels == nil {
195-
tv.Labels = make(LabelSet)
196-
}
197-
chainAddresses[addrEntry.Address] = tv
198-
}
199-
(*abc)[chain.ChainSelector] = chainAddresses
200-
}
201-
202-
return nil
203-
}
204-
205129
type AddressBookMap struct {
206130
addressesByChain AddressesByChain
207131
mtx sync.RWMutex
@@ -445,27 +369,3 @@ func (tv *TypeAndVersion) AddLabel(label string) {
445369
}
446370
tv.Labels.Add(label)
447371
}
448-
449-
// MarshalJSON implements custom JSON marshaling for AddressBookMap
450-
// to ensure deterministic ordering via the AddressesByChain marshaling
451-
func (m *AddressBookMap) MarshalJSON() ([]byte, error) {
452-
m.mtx.RLock()
453-
defer m.mtx.RUnlock()
454-
455-
// Use the custom marshaling of AddressesByChain
456-
return json.Marshal(m.addressesByChain)
457-
}
458-
459-
// UnmarshalJSON implements custom JSON unmarshaling for AddressBookMap
460-
func (m *AddressBookMap) UnmarshalJSON(data []byte) error {
461-
m.mtx.Lock()
462-
defer m.mtx.Unlock()
463-
464-
// Initialize if needed
465-
if m.addressesByChain == nil {
466-
m.addressesByChain = make(AddressesByChain)
467-
}
468-
469-
// Use the custom unmarshaling of AddressesByChain
470-
return json.Unmarshal(data, &m.addressesByChain)
471-
}

deployment/address_book_test.go

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package deployment
22

33
import (
4-
"encoding/json"
54
"math/big"
6-
"strconv"
7-
"strings"
85
"sync"
96
"testing"
107

@@ -574,130 +571,3 @@ func Test_toTypeAndVersionMap(t *testing.T) {
574571
})
575572
}
576573
}
577-
578-
func TestAddressBookMap_JSONMarshaling(t *testing.T) {
579-
t.Parallel()
580-
581-
// Create test data with multiple chains and addresses
582-
onRamp100 := NewTypeAndVersion("OnRamp", Version1_0_0)
583-
onRamp110 := NewTypeAndVersion("OnRamp", Version1_1_0)
584-
585-
// Create addresses in non-alphabetical order
586-
addr1 := common.HexToAddress("0xC").String() // Will be third
587-
addr2 := common.HexToAddress("0xA").String() // Will be first
588-
addr3 := common.HexToAddress("0xB").String() // Will be second
589-
590-
// Create address book with chains in non-numerical order
591-
originalAB := NewMemoryAddressBookFromMap(map[uint64]map[string]TypeAndVersion{
592-
chainsel.TEST_90000002.Selector: { // Higher chain selector
593-
addr1: onRamp100,
594-
addr2: onRamp110,
595-
},
596-
chainsel.TEST_90000001.Selector: { // Lower chain selector
597-
addr3: onRamp100,
598-
addr1: onRamp110,
599-
},
600-
})
601-
602-
// Marshal to JSON twice
603-
jsonData1, err := json.Marshal(originalAB)
604-
require.NoError(t, err)
605-
606-
jsonData2, err := json.Marshal(originalAB)
607-
require.NoError(t, err)
608-
609-
// Convert to strings for comparison
610-
jsonStr1 := string(jsonData1)
611-
jsonStr2 := string(jsonData2)
612-
613-
// All JSON strings should be exactly identical
614-
assert.JSONEq(t, jsonStr1, jsonStr2, "First and second marshal should produce identical JSON strings")
615-
616-
// Test with pretty-printed JSON to show structure more clearly
617-
prettyJSON1, err := json.MarshalIndent(originalAB, "", " ")
618-
require.NoError(t, err)
619-
620-
prettyJSON2, err := json.MarshalIndent(originalAB, "", " ")
621-
require.NoError(t, err)
622-
623-
assert.Equal(t, string(prettyJSON1), string(prettyJSON2), "Pretty-printed JSON should also be deterministic") //nolint:testifylint
624-
625-
// Unmarshal back to verify that works
626-
var unmarshaledAB AddressBookMap
627-
err = json.Unmarshal(jsonData1, &unmarshaledAB)
628-
require.NoError(t, err)
629-
630-
// Verify the data is there
631-
addresses, err := unmarshaledAB.Addresses()
632-
require.NoError(t, err)
633-
634-
originalAddresses, err := originalAB.Addresses()
635-
require.NoError(t, err)
636-
637-
assert.Equal(t, originalAddresses, addresses)
638-
639-
// Verify the ordering is correct by checking chain selector positions
640-
chain1Str := strconv.FormatUint(chainsel.TEST_90000001.Selector, 10)
641-
chain2Str := strconv.FormatUint(chainsel.TEST_90000002.Selector, 10)
642-
643-
chain1Pos := strings.Index(jsonStr1, chain1Str)
644-
chain2Pos := strings.Index(jsonStr1, chain2Str)
645-
646-
// Verify the smaller chain selector comes first
647-
if chainsel.TEST_90000001.Selector < chainsel.TEST_90000002.Selector {
648-
assert.Greater(t, chain2Pos, chain1Pos, "Chain selectors should be ordered numerically")
649-
} else {
650-
assert.Greater(t, chain1Pos, chain2Pos, "Chain selectors should be ordered numerically")
651-
}
652-
653-
// Test exact expected output string
654-
expectedJSON := `[{"chainSelector":909606746561742123,"addresses":[{"address":"0x000000000000000000000000000000000000000b","typeAndVersion":{"Type":"OnRamp","Version":"1.0.0"}},{"address":"0x000000000000000000000000000000000000000C","typeAndVersion":{"Type":"OnRamp","Version":"1.1.0"}}]},{"chainSelector":5548718428018410741,"addresses":[{"address":"0x000000000000000000000000000000000000000A","typeAndVersion":{"Type":"OnRamp","Version":"1.1.0"}},{"address":"0x000000000000000000000000000000000000000C","typeAndVersion":{"Type":"OnRamp","Version":"1.0.0"}}]}]`
655-
656-
assert.Equal(t, expectedJSON, jsonStr1, "JSON should match exact expected deterministic output") //nolint:testifylint
657-
}
658-
659-
func TestAddressesByChain_JSONMarshaling(t *testing.T) {
660-
t.Parallel()
661-
662-
// Test the AddressesByChain type directly
663-
onRamp100 := NewTypeAndVersion("OnRamp", Version1_0_0)
664-
addr1 := common.HexToAddress("0x3a").String()
665-
addr2 := common.HexToAddress("0x1b").String()
666-
667-
abc := AddressesByChain{
668-
chainsel.TEST_90000002.Selector: {
669-
addr1: onRamp100,
670-
addr2: onRamp100,
671-
},
672-
chainsel.TEST_90000001.Selector: {
673-
addr1: onRamp100,
674-
},
675-
}
676-
677-
// Marshal multiple times
678-
jsonData1, err := json.Marshal(abc)
679-
require.NoError(t, err)
680-
681-
jsonData2, err := json.Marshal(abc)
682-
require.NoError(t, err)
683-
684-
// Convert to strings for exact comparison
685-
jsonStr1 := string(jsonData1)
686-
jsonStr2 := string(jsonData2)
687-
688-
// All should be exactly equal
689-
assert.Equal(t, jsonStr1, jsonStr2, "Multiple marshal operations should produce identical strings") //nolint:testifylint
690-
691-
// Unmarshal and verify
692-
var unmarshaled AddressesByChain
693-
err = json.Unmarshal(jsonData1, &unmarshaled)
694-
require.NoError(t, err)
695-
696-
// Should be equal to original
697-
assert.Equal(t, abc, unmarshaled)
698-
699-
// Test exact expected output string
700-
expectedJSON := `[{"chainSelector":909606746561742123,"addresses":[{"address":"0x000000000000000000000000000000000000003a","typeAndVersion":{"Type":"OnRamp","Version":"1.0.0"}}]},{"chainSelector":5548718428018410741,"addresses":[{"address":"0x000000000000000000000000000000000000001B","typeAndVersion":{"Type":"OnRamp","Version":"1.0.0"}},{"address":"0x000000000000000000000000000000000000003a","typeAndVersion":{"Type":"OnRamp","Version":"1.0.0"}}]}]`
701-
702-
assert.Equal(t, expectedJSON, jsonStr1, "JSON should match exact expected deterministic output") //nolint:testifylint
703-
}

0 commit comments

Comments
 (0)