Commit ec8d6b7c authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

indexer: clean up duplicated code (#3225)

* 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: default avatarJaved Khan <javed@optimism.io>
Co-authored-by: default avatarJaved Khan <javed@optimism.io>
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 74babaa4
---
'@eth-optimism/indexer': patch
---
Remove some duplicated code
...@@ -3,24 +3,14 @@ package indexer ...@@ -3,24 +3,14 @@ package indexer
import ( import (
"fmt" "fmt"
l2common "github.com/ethereum-optimism/optimism/l2geth/common"
"github.com/ethereum/go-ethereum/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. // 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) { if common.IsHexAddress(address) {
return common.HexToAddress(address), nil return common.HexToAddress(address), nil
} }
return common.Address{}, fmt.Errorf("invalid address: %v", address) 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)
}
...@@ -6,15 +6,14 @@ import ( ...@@ -6,15 +6,14 @@ import (
"testing" "testing"
indexer "github.com/ethereum-optimism/optimism/indexer" indexer "github.com/ethereum-optimism/optimism/indexer"
l2common "github.com/ethereum-optimism/optimism/l2geth/common"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require" "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 // 40-characater hexadecimal strings with optional 0x prefix into valid 20-byte
// addresses for the L1 chain. // addresses
func TestParseL1Address(t *testing.T) { func TestParseAddress(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
addr string addr string
...@@ -46,52 +45,7 @@ func TestParseL1Address(t *testing.T) { ...@@ -46,52 +45,7 @@ func TestParseL1Address(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
addr, err := indexer.ParseL1Address(test.addr) addr, err := indexer.ParseAddress(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)
require.Equal(t, err, test.expErr) require.Equal(t, err, test.expErr)
if test.expErr != nil { if test.expErr != nil {
return return
......
...@@ -159,7 +159,7 @@ func NewIndexer(cfg Config, gitVersion string) (*Indexer, error) { ...@@ -159,7 +159,7 @@ func NewIndexer(cfg Config, gitVersion string) (*Indexer, error) {
return nil, err return nil, err
} }
l1AddressManagerAddress, err := ParseL1Address(cfg.L1AddressManagerAddress) l1AddressManagerAddress, err := ParseAddress(cfg.L1AddressManagerAddress)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment