-
Notifications
You must be signed in to change notification settings - Fork 193
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
feat(eth): Collections encoders for bytes, Ethereum addresses, and Ethereum hashes #1841
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,79 @@ | ||
package types | ||
|
||
import ( | ||
fmt "fmt" | ||
|
||
"github.com/NibiruChain/collections" | ||
ethcommon "github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
// BytesToHex converts a byte array to a hexadecimal string | ||
func BytesToHex(bz []byte) string { | ||
return fmt.Sprintf("%x", bz) | ||
} | ||
|
||
// EthAddr: (alias) 20 byte address of an Ethereum account. | ||
type EthAddr = ethcommon.Address | ||
|
||
// EthHash: (alias) 32 byte Keccak256 hash of arbitrary data. | ||
type EthHash = ethcommon.Hash | ||
|
||
var ( | ||
// Implements a `collections.ValueEncoder` for the `[]byte` type | ||
ValueEncoderBytes collections.ValueEncoder[[]byte] = veBytes{} | ||
KeyEncoderBytes collections.KeyEncoder[[]byte] = keBytes{} | ||
|
||
// Implements a `collections.ValueEncoder` for an Ethereum address. | ||
ValueEncoderEthAddr collections.ValueEncoder[EthAddr] = veEthAddr{} | ||
// keEthHash: Implements a `collections.KeyEncoder` for an Ethereum address. | ||
KeyEncoderEthAddr collections.KeyEncoder[EthAddr] = keEthAddr{} | ||
|
||
// keEthHash: Implements a `collections.KeyEncoder` for an Ethereum hash. | ||
KeyEncoderEthHash collections.KeyEncoder[EthHash] = keEthHash{} | ||
) | ||
|
||
// collections ValueEncoder[[]byte] | ||
type veBytes struct{} | ||
|
||
func (_ veBytes) Encode(value []byte) []byte { return value } | ||
func (_ veBytes) Decode(bz []byte) []byte { return bz } | ||
func (_ veBytes) Stringify(value []byte) string { return BytesToHex(value) } | ||
func (_ veBytes) Name() string { return "[]byte" } | ||
|
||
// veEthAddr: Implements a `collections.ValueEncoder` for an Ethereum address. | ||
type veEthAddr struct{} | ||
|
||
func (_ veEthAddr) Encode(value EthAddr) []byte { return value.Bytes() } | ||
func (_ veEthAddr) Decode(bz []byte) EthAddr { return ethcommon.BytesToAddress(bz) } | ||
func (_ veEthAddr) Stringify(value EthAddr) string { return value.Hex() } | ||
func (_ veEthAddr) Name() string { return "EthAddr" } | ||
|
||
type keBytes struct{} | ||
|
||
// Encode encodes the type T into bytes. | ||
func (_ keBytes) Encode(key []byte) []byte { return key } | ||
|
||
// Decode decodes the given bytes back into T. | ||
// And it also must return the bytes of the buffer which were read. | ||
func (_ keBytes) Decode(bz []byte) (int, []byte) { return len(bz), bz } | ||
|
||
// Stringify returns a string representation of T. | ||
func (_ keBytes) Stringify(key []byte) string { return BytesToHex(key) } | ||
|
||
// keEthAddr: Implements a `collections.KeyEncoder` for an Ethereum address. | ||
type keEthAddr struct{} | ||
|
||
func (_ keEthAddr) Encode(value EthAddr) []byte { return value.Bytes() } | ||
func (_ keEthAddr) Decode(bz []byte) (int, EthAddr) { | ||
return ethcommon.AddressLength, ethcommon.BytesToAddress(bz) | ||
} | ||
func (_ keEthAddr) Stringify(value EthAddr) string { return value.Hex() } | ||
|
||
// keEthHash: Implements a `collections.KeyEncoder` for an Ethereum hash. | ||
type keEthHash struct{} | ||
|
||
func (_ keEthHash) Encode(value EthHash) []byte { return value.Bytes() } | ||
func (_ keEthHash) Decode(bz []byte) (int, EthHash) { | ||
return ethcommon.HashLength, ethcommon.BytesToHash(bz) | ||
} | ||
func (_ keEthHash) Stringify(value EthHash) string { return value.Hex() } |
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,97 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/NibiruChain/collections" | ||
ethcommon "github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
|
||
ethtypes "github.com/NibiruChain/nibiru/eth/types" | ||
) | ||
|
||
func assertBijectiveKey[T any](t *testing.T, encoder collections.KeyEncoder[T], key T) { | ||
encodedKey := encoder.Encode(key) | ||
readLen, decodedKey := encoder.Decode(encodedKey) | ||
require.Equal(t, len(encodedKey), readLen, "encoded key and read bytes must have same size") | ||
require.Equal(t, key, decodedKey, "encoding and decoding produces different keys") | ||
wantStr := encoder.Stringify(key) | ||
gotStr := encoder.Stringify(decodedKey) | ||
require.Equal(t, wantStr, gotStr, | ||
"encoding and decoding produce different string representations") | ||
} | ||
|
||
func assertBijectiveValue[T any](t *testing.T, encoder collections.ValueEncoder[T], value T) { | ||
encodedValue := encoder.Encode(value) | ||
decodedValue := encoder.Decode(encodedValue) | ||
require.Equal(t, value, decodedValue, "encoding and decoding produces different values") | ||
|
||
wantStr := encoder.Stringify(value) | ||
gotStr := encoder.Stringify(decodedValue) | ||
require.Equal(t, wantStr, gotStr, | ||
"encoding and decoding produce different string representations") | ||
require.NotEmpty(t, encoder.Name()) | ||
} | ||
|
||
type SuiteEncoders struct { | ||
suite.Suite | ||
} | ||
|
||
func TestSuiteEncoders_RunAll(t *testing.T) { | ||
suite.Run(t, new(SuiteEncoders)) | ||
} | ||
|
||
func (s *SuiteEncoders) TestEncoderBytes() { | ||
testCases := []struct { | ||
name string | ||
value string | ||
}{ | ||
{"dec-like number", "-1000.5858"}, | ||
{"Nibiru bech32 addr", "nibi1rlvdjfmxkyfj4tzu73p8m4g2h4y89xccf9622l"}, | ||
{"Nibiru EVM addr", "0xA52c829E935C30F4C7dcD66739Cf91BF79dD9253"}, | ||
{"normal text with special symbols", "abc123日本語!!??foobar"}, | ||
} | ||
for _, tc := range testCases { | ||
s.Run("bijectivity: []byte encoders "+tc.name, func() { | ||
given := []byte(tc.value) | ||
assertBijectiveKey(s.T(), ethtypes.KeyEncoderBytes, given) | ||
assertBijectiveValue(s.T(), ethtypes.ValueEncoderBytes, given) | ||
}) | ||
} | ||
} | ||
|
||
func (s *SuiteEncoders) TestEncoderEthAddr() { | ||
testCases := []struct { | ||
name string | ||
given ethtypes.EthAddr | ||
wantPanic bool | ||
}{ | ||
{ | ||
name: "Nibiru EVM addr", | ||
given: ethcommon.BytesToAddress([]byte("0xA52c829E935C30F4C7dcD66739Cf91BF79dD9253")), | ||
}, | ||
{ | ||
name: "Nibiru EVM addr length above 20 bytes", | ||
given: ethcommon.BytesToAddress([]byte("0xA52c829E935C30F4C7dcD66739Cf91BF79dD92532456BF123421")), | ||
}, | ||
{ | ||
name: "Nibiru Bech 32 addr (hypothetically)", | ||
given: ethtypes.EthAddr([]byte("nibi1rlvdjfmxkyfj4tzu73p8m4g2h4y89xccf9622l")), | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
s.Run("bijectivity: []byte encoders "+tc.name, func() { | ||
given := tc.given | ||
runTest := func() { | ||
assertBijectiveKey(s.T(), ethtypes.KeyEncoderEthAddr, given) | ||
assertBijectiveValue(s.T(), ethtypes.ValueEncoderEthAddr, given) | ||
} | ||
if tc.wantPanic { | ||
s.Require().Panics(runTest) | ||
} else { | ||
s.Require().NotPanics(runTest) | ||
} | ||
}) | ||
} | ||
} |
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.
LGTM! The entry effectively documents the addition of collections encoders for bytes, Ethereum addresses, and Ethereum hashes. However, please remove the trailing space at the end of the line for cleanliness.
Committable suggestion