Skip to content
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 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [#1837](https://github.com/NibiruChain/nibiru/pull/1837) - feat(eth): protos, eth types, and evm module types
- [#1838](https://github.com/NibiruChain/nibiru/pull/1838) - feat(eth): Go-ethereum, crypto, encoding, and unit tests for evm/types
- [#1841](https://github.com/NibiruChain/nibiru/pull/1841) - feat(eth): Collections encoders for bytes, Ethereum addresses, and Ethereum hashes
Copy link
Contributor

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.

- feat(eth): Collections encoders for bytes, Ethereum addresses, and Ethereum hashes 
+ feat(eth): Collections encoders for bytes, Ethereum addresses, and Ethereum hashes

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- [#1841](https://github.com/NibiruChain/nibiru/pull/1841) - feat(eth): Collections encoders for bytes, Ethereum addresses, and Ethereum hashes
- [#1841](https://github.com/NibiruChain/nibiru/pull/1841) - feat(eth): Collections encoders for bytes, Ethereum addresses, and Ethereum hashes


#### Dapp modules: perp, spot, etc

Expand Down
79 changes: 79 additions & 0 deletions eth/types/state_encoder.go
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() }
97 changes: 97 additions & 0 deletions eth/types/state_encoder_test.go
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)
}
})
}
}
Loading