Skip to content

Commit

Permalink
indexer: clean up duplicated code (ethereum-optimism#3225)
Browse files Browse the repository at this point in the history
* indexer: clean up duplicated code

Remove `ParseL1Address` and `ParseL2Address` and
instead use `ParseAddress`. Addresses on L1 and L2
are the same and we can use as much upstream code
as possible instead of code from `l2geth`.

* Update indexer/crypto.go

Co-authored-by: Javed Khan <[email protected]>

Co-authored-by: Javed Khan <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 15, 2022
1 parent 74babaa commit ec8d6b7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 63 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-monkeys-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/indexer': patch
---

Remove some duplicated code
14 changes: 2 additions & 12 deletions indexer/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@ package indexer
import (
"fmt"

l2common "github.com/ethereum-optimism/optimism/l2geth/common"
"github.com/ethereum/go-ethereum/common"
)

// ParseL1Address parses a L1 ETH address from a hex string. This method will
// ParseAddress parses a ETH address from a hex string. This method will
// fail if the address is not a valid hexadecimal address.
func ParseL1Address(address string) (common.Address, error) {
func ParseAddress(address string) (common.Address, error) {
if common.IsHexAddress(address) {
return common.HexToAddress(address), nil
}
return common.Address{}, fmt.Errorf("invalid address: %v", address)
}

// ParseL2Address parses a L2 ETH address from a hex string. This method will
// fail if the address is not a valid hexadecimal address.
func ParseL2Address(address string) (l2common.Address, error) {
if l2common.IsHexAddress(address) {
return l2common.HexToAddress(address), nil
}
return l2common.Address{}, fmt.Errorf("invalid address: %v", address)
}
54 changes: 4 additions & 50 deletions indexer/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"testing"

indexer "github.com/ethereum-optimism/optimism/indexer"
l2common "github.com/ethereum-optimism/optimism/l2geth/common"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

// TestParseL1Address asserts that ParseL1Address correctly parses
// TestParseAddress asserts that ParseAddress correctly parses
// 40-characater hexadecimal strings with optional 0x prefix into valid 20-byte
// addresses for the L1 chain.
func TestParseL1Address(t *testing.T) {
// addresses
func TestParseAddress(t *testing.T) {
tests := []struct {
name string
addr string
Expand Down Expand Up @@ -46,52 +45,7 @@ func TestParseL1Address(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
addr, err := indexer.ParseL1Address(test.addr)
require.Equal(t, err, test.expErr)
if test.expErr != nil {
return
}
require.Equal(t, addr, test.expAddr)
})
}
}

// TestParseL2Address asserts that ParseL2Address correctly parses
// 40-characater hexadecimal strings with optional 0x prefix into valid 20-byte
// addresses for the L2 chain.
func TestParseL2Address(t *testing.T) {
tests := []struct {
name string
addr string
expErr error
expAddr l2common.Address
}{
{
name: "empty address",
addr: "",
expErr: errors.New("invalid address: "),
},
{
name: "only 0x",
addr: "0x",
expErr: errors.New("invalid address: 0x"),
},
{
name: "non hex character",
addr: "0xaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
expErr: errors.New("invalid address: 0xaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
},
{
name: "valid address",
addr: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
expErr: nil,
expAddr: l2common.BytesToAddress(bytes.Repeat([]byte{170}, 20)),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
addr, err := indexer.ParseL2Address(test.addr)
addr, err := indexer.ParseAddress(test.addr)
require.Equal(t, err, test.expErr)
if test.expErr != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func NewIndexer(cfg Config, gitVersion string) (*Indexer, error) {
return nil, err
}

l1AddressManagerAddress, err := ParseL1Address(cfg.L1AddressManagerAddress)
l1AddressManagerAddress, err := ParseAddress(cfg.L1AddressManagerAddress)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ec8d6b7

Please sign in to comment.