-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[BCFR-147] - Add DecodeHook to convert between string and common.Address types #14508
Draft
Farber98
wants to merge
11
commits into
develop
Choose a base branch
from
BCI-3969-add-codec-modifier-address-to-string
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0ed1ac0
add hook to parse addresses as string
Farber98 7b76566
add changesets
Farber98 18095de
add pointers types and test cases
Farber98 368ba12
Merge branch 'develop' into BCI-3969-add-codec-modifier-address-to-st…
Farber98 93b4cfa
fix lint
Farber98 0131a82
improved readability
Farber98 c1f808d
simplifications
Farber98 e6bb94a
fix comment
Farber98 805bbd3
draft abi
Farber98 3e9e362
[Bot] Update changeset file with jira issue
app-token-issuer-infra-releng[bot] 2be1905
using modifier
Farber98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"chainlink": minor | ||
--- | ||
|
||
#internal Add DecoderHook to parse address as string |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@chainlink/contracts': minor | ||
--- | ||
|
||
#internal Add event inside Chain Reader testing contract to check the new address to string decoder hook | ||
|
||
|
||
BCFR-147 |
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
204 changes: 180 additions & 24 deletions
204
core/gethwrappers/generated/chain_reader_tester/chain_reader_tester.go
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package codec | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/types" | ||
) | ||
|
||
func TestAddressStringDecodeHook(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Helper vars | ||
var nilString *string | ||
var nilAddress *common.Address | ||
hexString := "0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF" | ||
address := common.HexToAddress(hexString) | ||
addressToString := address.Hex() | ||
emptyAddress := common.Address{} | ||
emptyString := "" | ||
stringType, stringPtrType := reflect.TypeOf(""), reflect.PointerTo(reflect.TypeOf("")) | ||
addressType, addressPtrType := reflect.TypeOf(common.Address{}), reflect.TypeOf(&common.Address{}) | ||
|
||
t.Run("Converts from string to common.Address", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(stringType, addressType, hexString) | ||
require.NoError(t, err) | ||
require.IsType(t, common.Address{}, result) | ||
assert.Equal(t, address, result) | ||
}) | ||
|
||
t.Run("Converts from string to *common.Address", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(stringType, addressPtrType, hexString) | ||
require.NoError(t, err) | ||
assert.Equal(t, &address, result) | ||
}) | ||
|
||
t.Run("Converts from *string to common.Address", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(stringPtrType, addressType, &hexString) | ||
require.NoError(t, err) | ||
require.IsType(t, common.Address{}, result) | ||
assert.Equal(t, address, result) | ||
}) | ||
|
||
t.Run("Converts from *string to *common.Address", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(stringPtrType, addressPtrType, &hexString) | ||
require.NoError(t, err) | ||
assert.Equal(t, &address, result) | ||
}) | ||
|
||
t.Run("Converts from common.Address to string", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(addressType, stringType, address) | ||
require.NoError(t, err) | ||
require.IsType(t, "", result) | ||
assert.Equal(t, addressToString, result) | ||
}) | ||
|
||
t.Run("Converts from common.Address to *string", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(addressType, stringPtrType, address) | ||
require.NoError(t, err) | ||
assert.Equal(t, &addressToString, result) | ||
}) | ||
|
||
t.Run("Converts from *common.Address to string", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(addressPtrType, stringType, &address) | ||
require.NoError(t, err) | ||
assert.Equal(t, addressToString, result) | ||
}) | ||
|
||
t.Run("Converts from *common.Address to *string", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(addressPtrType, stringPtrType, &address) | ||
require.NoError(t, err) | ||
assert.Equal(t, &addressToString, result) | ||
}) | ||
|
||
t.Run("Returns error on invalid hex string", func(t *testing.T) { | ||
_, err := addressStringDecodeHook(stringType, addressType, "NotAHexString") | ||
assert.True(t, errors.Is(err, types.ErrInvalidType)) | ||
_, err = addressStringDecodeHook(stringType, addressPtrType, "NotAHexString") | ||
assert.True(t, errors.Is(err, types.ErrInvalidType)) | ||
}) | ||
|
||
t.Run("Returns error on empty string and empty *string", func(t *testing.T) { | ||
_, err := addressStringDecodeHook(stringType, addressType, emptyString) | ||
assert.True(t, errors.Is(err, types.ErrInvalidType), "Expected an error for empty string") | ||
_, err = addressStringDecodeHook(stringType, addressPtrType, emptyString) | ||
assert.True(t, errors.Is(err, types.ErrInvalidType), "Expected an error for empty string") | ||
_, err = addressStringDecodeHook(stringPtrType, addressType, &emptyString) | ||
assert.True(t, errors.Is(err, types.ErrInvalidType), "Expected an error for empty string") | ||
_, err = addressStringDecodeHook(stringPtrType, addressPtrType, &emptyString) | ||
assert.True(t, errors.Is(err, types.ErrInvalidType), "Expected an error for empty string") | ||
}) | ||
|
||
t.Run("Returns error for empty common.Address and empty *common.Address", func(t *testing.T) { | ||
_, err := addressStringDecodeHook(addressType, stringType, emptyAddress) | ||
assert.True(t, errors.Is(err, types.ErrInvalidType), "Expected error for empty common.Address") | ||
_, err = addressStringDecodeHook(addressType, stringPtrType, emptyAddress) | ||
assert.True(t, errors.Is(err, types.ErrInvalidType), "Expected error for empty common.Address") | ||
_, err = addressStringDecodeHook(addressPtrType, stringType, &emptyAddress) | ||
assert.True(t, errors.Is(err, types.ErrInvalidType), "Expected error for empty *common.Address") | ||
_, err = addressStringDecodeHook(addressPtrType, stringPtrType, &emptyAddress) | ||
assert.True(t, errors.Is(err, types.ErrInvalidType), "Expected error for empty *common.Address") | ||
}) | ||
t.Run("Returns error for nil *string", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(stringPtrType, addressType, nilString) | ||
require.Error(t, err, "Expected error for nil *string input") | ||
assert.Contains(t, err.Error(), "nil *string value") | ||
assert.Nil(t, result, "Expected result to be nil for nil *string input") | ||
|
||
result, err = addressStringDecodeHook(stringPtrType, addressPtrType, nilString) | ||
require.Error(t, err, "Expected error for nil *string input") | ||
assert.Contains(t, err.Error(), "nil *string value") | ||
assert.Nil(t, result, "Expected result to be nil for nil *string input") | ||
}) | ||
|
||
t.Run("Returns error for nil *common.Address", func(t *testing.T) { | ||
result, err := addressStringDecodeHook(addressPtrType, stringType, nilAddress) | ||
require.Error(t, err, "Expected error for nil *common.Address input") | ||
assert.Contains(t, err.Error(), "nil *common.Address value") | ||
assert.Nil(t, result, "Expected result to be nil for nil *common.Address input") | ||
|
||
result, err = addressStringDecodeHook(addressPtrType, stringPtrType, nilAddress) | ||
require.Error(t, err, "Expected error for nil *common.Address input") | ||
assert.Contains(t, err.Error(), "nil *common.Address value") | ||
assert.Nil(t, result, "Expected result to be nil for nil *common.Address input") | ||
}) | ||
|
||
t.Run("Returns input unchanged for unsupported conversion", func(t *testing.T) { | ||
unsupportedCases := []struct { | ||
fromType reflect.Type | ||
toType reflect.Type | ||
input interface{} | ||
}{ | ||
{fromType: reflect.TypeOf(12345), toType: addressType, input: 12345}, | ||
{fromType: reflect.TypeOf(12345), toType: stringType, input: 12345}, | ||
{fromType: reflect.TypeOf([]byte{}), toType: addressType, input: []byte{0x01, 0x02, 0x03}}, | ||
} | ||
|
||
for _, tc := range unsupportedCases { | ||
result, err := addressStringDecodeHook(tc.fromType, tc.toType, tc.input) | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.input, result, "Expected original value to be returned for unsupported conversion") | ||
} | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can be constants outside the function scope. This would save them being declared every time the function runs.