From ec8d6b7c1fe66ffe5f371da432122ba86c1c1e91 Mon Sep 17 00:00:00 2001 From: Mark Tyneway Date: Mon, 15 Aug 2022 11:09:25 -0700 Subject: [PATCH] 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: Javed Khan Co-authored-by: Javed Khan Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- .changeset/wet-monkeys-tap.md | 5 ++++ indexer/crypto.go | 14 ++------- indexer/crypto_test.go | 54 +++-------------------------------- indexer/indexer.go | 2 +- 4 files changed, 12 insertions(+), 63 deletions(-) create mode 100644 .changeset/wet-monkeys-tap.md diff --git a/.changeset/wet-monkeys-tap.md b/.changeset/wet-monkeys-tap.md new file mode 100644 index 000000000000..daad00a6efe9 --- /dev/null +++ b/.changeset/wet-monkeys-tap.md @@ -0,0 +1,5 @@ +--- +'@eth-optimism/indexer': patch +--- + +Remove some duplicated code diff --git a/indexer/crypto.go b/indexer/crypto.go index 6f0aa0c67a36..e62bb231f938 100644 --- a/indexer/crypto.go +++ b/indexer/crypto.go @@ -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) -} diff --git a/indexer/crypto_test.go b/indexer/crypto_test.go index 37edb52d88a0..2532c6b5cae3 100644 --- a/indexer/crypto_test.go +++ b/indexer/crypto_test.go @@ -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 @@ -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 diff --git a/indexer/indexer.go b/indexer/indexer.go index 819d676c8806..1b7d14182346 100644 --- a/indexer/indexer.go +++ b/indexer/indexer.go @@ -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 }