|
1 | 1 | package deployment |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | 4 | "math/big" |
6 | | - "strconv" |
7 | | - "strings" |
8 | 5 | "sync" |
9 | 6 | "testing" |
10 | 7 |
|
@@ -574,130 +571,3 @@ func Test_toTypeAndVersionMap(t *testing.T) { |
574 | 571 | }) |
575 | 572 | } |
576 | 573 | } |
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