-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stronger address book typing (#1326)
## Motivation Want to be maximally defensive for the address book to prevent corruption. ## Solution - Type contract and version - Let products compose type and version at deploy/state gen time to avoid a combinatorial number of strings
- Loading branch information
1 parent
7200ebe
commit 0227b9f
Showing
8 changed files
with
244 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,93 @@ | ||
package deployment | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
chainsel "github.com/smartcontractkit/chain-selectors" | ||
"github.com/stretchr/testify/require" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestAddressBook(t *testing.T) { | ||
func TestAddressBook_Save(t *testing.T) { | ||
ab := NewMemoryAddressBook() | ||
err := ab.Save(1, "0x1", "OnRamp 1.0.0") | ||
onRamp100 := NewTypeAndVersion("OnRamp", Version1_0_0) | ||
onRamp110 := NewTypeAndVersion("OnRamp", Version1_1_0) | ||
offRamp100 := NewTypeAndVersion("OffRamp", Version1_0_0) | ||
addr1 := common.HexToAddress("0x1").String() | ||
addr2 := common.HexToAddress("0x2").String() | ||
addr3 := common.HexToAddress("0x3").String() | ||
|
||
err := ab.Save(chainsel.TEST_90000001.Selector, addr1, onRamp100) | ||
require.NoError(t, err) | ||
// Duplicate address will error | ||
err = ab.Save(1, "0x1", "OnRamp 1.0.0") | ||
|
||
// Check input validation | ||
err = ab.Save(chainsel.TEST_90000001.Selector, "asdlfkj", onRamp100) | ||
require.Error(t, err) | ||
assert.Equal(t, errors.Is(err, ErrInvalidAddress), true, "err %s", err) | ||
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) | ||
|
||
// Distinct address same TV will not | ||
err = ab.Save(1, "0x2", "OnRamp 1.0.0") | ||
err = ab.Save(chainsel.TEST_90000001.Selector, addr2, onRamp100) | ||
require.NoError(t, err) | ||
// Same address different chain will not error | ||
err = ab.Save(2, "0x1", "OnRamp 1.0.0") | ||
err = ab.Save(chainsel.TEST_90000002.Selector, addr1, onRamp100) | ||
require.NoError(t, err) | ||
// We can save different versions of the same contract | ||
err = ab.Save(2, "0x2", "OnRamp 1.2.0") | ||
err = ab.Save(chainsel.TEST_90000002.Selector, addr2, onRamp110) | ||
require.NoError(t, err) | ||
|
||
addresses, err := ab.Addresses() | ||
require.NoError(t, err) | ||
assert.DeepEqual(t, addresses, map[uint64]map[string]string{ | ||
1: { | ||
"0x1": "OnRamp 1.0.0", | ||
"0x2": "OnRamp 1.0.0", | ||
assert.DeepEqual(t, addresses, map[uint64]map[string]TypeAndVersion{ | ||
chainsel.TEST_90000001.Selector: { | ||
addr1: onRamp100, | ||
addr2: onRamp100, | ||
}, | ||
2: { | ||
"0x1": "OnRamp 1.0.0", | ||
"0x2": "OnRamp 1.2.0", | ||
chainsel.TEST_90000002.Selector: { | ||
addr1: onRamp100, | ||
addr2: onRamp110, | ||
}, | ||
}) | ||
|
||
// Test merge | ||
ab2 := NewMemoryAddressBook() | ||
require.NoError(t, ab2.Save(3, "0x3", "OnRamp 1.0.0")) | ||
require.NoError(t, ab2.Save(chainsel.TEST_90000003.Selector, addr3, onRamp100)) | ||
require.NoError(t, ab.Merge(ab2)) | ||
// Other address book should remain unchanged. | ||
addresses, err = ab2.Addresses() | ||
require.NoError(t, err) | ||
assert.DeepEqual(t, addresses, map[uint64]map[string]string{ | ||
3: { | ||
"0x3": "OnRamp 1.0.0", | ||
assert.DeepEqual(t, addresses, map[uint64]map[string]TypeAndVersion{ | ||
chainsel.TEST_90000003.Selector: { | ||
addr3: onRamp100, | ||
}, | ||
}) | ||
// Existing addressbook should contain the new elements. | ||
addresses, err = ab.Addresses() | ||
require.NoError(t, err) | ||
assert.DeepEqual(t, addresses, map[uint64]map[string]string{ | ||
1: { | ||
"0x1": "OnRamp 1.0.0", | ||
"0x2": "OnRamp 1.0.0", | ||
assert.DeepEqual(t, addresses, map[uint64]map[string]TypeAndVersion{ | ||
chainsel.TEST_90000001.Selector: { | ||
addr1: onRamp100, | ||
addr2: onRamp100, | ||
}, | ||
2: { | ||
"0x1": "OnRamp 1.0.0", | ||
"0x2": "OnRamp 1.2.0", | ||
chainsel.TEST_90000002.Selector: { | ||
addr1: onRamp100, | ||
addr2: onRamp110, | ||
}, | ||
3: { | ||
"0x3": "OnRamp 1.0.0", | ||
chainsel.TEST_90000003.Selector: { | ||
addr3: onRamp100, | ||
}, | ||
}) | ||
|
||
// Merge to an existing chain. | ||
require.NoError(t, ab2.Save(2, "0x3", "OffRamp 1.0.0")) | ||
require.NoError(t, ab2.Save(chainsel.TEST_90000002.Selector, addr3, offRamp100)) | ||
} |
Oops, something went wrong.