Skip to content

Commit

Permalink
reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Jun 19, 2024
1 parent 88b512b commit 6ca088b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 45 deletions.
18 changes: 4 additions & 14 deletions etherman/sequence_batches_decode_elderberry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package etherman

import (
"bytes"
"encoding/json"
"strings"

Expand All @@ -11,22 +10,13 @@ import (
)

type SequenceBatchesDecodeElderberry struct {
SequenceBatchesBase
}

func NewDecodeSequenceBatchesElderberry() *SequenceBatchesDecodeElderberry {
return &SequenceBatchesDecodeElderberry{}
}

// MatchMethodId returns true if the methodId is the one for the sequenceBatchesEtrog method
func (s *SequenceBatchesDecodeElderberry) MatchMethodId(methodId []byte) bool {
return bytes.Equal(methodId, methodIDSequenceBatchesElderberry)
}

func (s *SequenceBatchesDecodeElderberry) NameMethodID(methodId []byte) string {
if s.MatchMethodId(methodId) {
return "sequenceBatchesElderberry"
return &SequenceBatchesDecodeElderberry{
NewSequenceBatchesBase(methodIDSequenceBatchesElderberry, "sequenceBatchesElderberry"),
}
return ""
}

func (s *SequenceBatchesDecodeElderberry) DecodeSequenceBatches(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64, l1InfoRoot common.Hash) ([]SequencedBatch, error) {
Expand Down Expand Up @@ -63,7 +53,7 @@ func (s *SequenceBatchesDecodeElderberry) DecodeSequenceBatches(txData []byte, l
sequencedBatches := make([]SequencedBatch, len(sequences))

SequencedBatchMetadata := &SequencedBatchMetadata{
CallFunctionName: "sequenceBatchesElderberry",
CallFunctionName: s.NameMethodID(txData[:4]),
RollupFlavor: RollupFlavorZkEVM,
ForkName: "elderberry",
}
Expand Down
21 changes: 6 additions & 15 deletions etherman/sequence_batches_decode_elderberry_validium.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package etherman

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -29,10 +28,12 @@ var (
// uint64 forcedTimestamp;
// bytes32 forcedBlockHashL1;
// }
methodIDSequenceBatchesValidiumElderberry = []byte{0xdb, 0x5b, 0x0e, 0xd7} // 0xdb5b0ed7 sequenceBatchesValidium((bytes32,bytes32,uint64,bytes32)[],uint64,uint64,address,bytes)
methodIDSequenceBatchesValidiumElderberry = []byte{0xdb, 0x5b, 0x0e, 0xd7} // 0xdb5b0ed7 sequenceBatchesValidium((bytes32,bytes32,uint64,bytes32)[],uint64,uint64,address,bytes)
methodIDSequenceBatchesValidiumElderberryName = "sequenceBatchesElderberryValidium"
)

type SequenceBatchesDecodeElderberryValidium struct {
SequenceBatchesBase
da dataavailability.BatchDataProvider
SmcABI abi.ABI
}
Expand All @@ -42,19 +43,9 @@ func NewDecodeSequenceBatchesElderberryValidium(da dataavailability.BatchDataPro
if err != nil {
return nil, err
}
return &SequenceBatchesDecodeElderberryValidium{da, smcAbi}, nil
}

// MatchMethodId returns true if the methodId is the one for the sequenceBatchesEtrog method
func (s *SequenceBatchesDecodeElderberryValidium) MatchMethodId(methodId []byte) bool {
return bytes.Equal(methodId, methodIDSequenceBatchesValidiumElderberry)
}

func (s *SequenceBatchesDecodeElderberryValidium) NameMethodID(methodId []byte) string {
if s.MatchMethodId(methodId) {
return "sequenceBatchesElderberryValidium"
}
return ""
return &SequenceBatchesDecodeElderberryValidium{
NewSequenceBatchesBase(methodIDSequenceBatchesValidiumElderberry, methodIDSequenceBatchesValidiumElderberryName),
da, smcAbi}, nil
}

func (s *SequenceBatchesDecodeElderberryValidium) DecodeSequenceBatches(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64, l1InfoRoot common.Hash) ([]SequencedBatch, error) {
Expand Down
18 changes: 4 additions & 14 deletions etherman/sequence_batches_decode_etrog.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package etherman

import (
"bytes"
"encoding/json"
"strings"

Expand All @@ -12,22 +11,13 @@ import (
)

type SequenceBatchesDecodeEtrog struct {
SequenceBatchesBase
}

func NewDecodeSequenceBatchesEtrog() *SequenceBatchesDecodeEtrog {
return &SequenceBatchesDecodeEtrog{}
}

// MatchMethodId returns true if the methodId is the one for the sequenceBatchesEtrog method
func (s *SequenceBatchesDecodeEtrog) MatchMethodId(methodId []byte) bool {
return bytes.Equal(methodId, methodIDSequenceBatchesEtrog)
}

func (s *SequenceBatchesDecodeEtrog) NameMethodID(methodId []byte) string {
if s.MatchMethodId(methodId) {
return "sequenceBatchesEtrog"
return &SequenceBatchesDecodeEtrog{
NewSequenceBatchesBase(methodIDSequenceBatchesEtrog, "sequenceBatchesEtrog"),
}
return ""
}

func (s *SequenceBatchesDecodeEtrog) DecodeSequenceBatches(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64, l1InfoRoot common.Hash) ([]SequencedBatch, error) {
Expand Down Expand Up @@ -60,7 +50,7 @@ func (s *SequenceBatchesDecodeEtrog) DecodeSequenceBatches(txData []byte, lastBa
}

SequencedBatchMetadata := &SequencedBatchMetadata{
CallFunctionName: "sequenceBatchesEtrog",
CallFunctionName: s.NameMethodID(txData[:4]),
RollupFlavor: RollupFlavorZkEVM,
ForkName: "etrog",
}
Expand Down
7 changes: 5 additions & 2 deletions etherman/sequence_batches_decode_etrog_validium.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
)

type SequenceBatchesDecodeEtrogValidium struct {
SequenceBatchesBase
da dataavailability.BatchDataProvider
SmcABI abi.ABI
}
Expand All @@ -28,7 +29,9 @@ func NewDecodeSequenceBatchesEtrogValidium(da dataavailability.BatchDataProvider
if err != nil {
return nil, err
}
return &SequenceBatchesDecodeEtrogValidium{da, smcAbi}, nil
return &SequenceBatchesDecodeEtrogValidium{
NewSequenceBatchesBase(methodIDSequenceBatchesValidiumEtrog, "sequenceBatchesEtrogValidium"),
da, smcAbi}, nil
}

// MatchMethodId returns true if the methodId is the one for the sequenceBatchesEtrog method
Expand Down Expand Up @@ -76,7 +79,7 @@ func (s *SequenceBatchesDecodeEtrogValidium) DecodeSequenceBatches(txData []byte
}

SequencedBatchMetadata := &SequencedBatchMetadata{
CallFunctionName: "sequenceBatchesEtrogValidium",
CallFunctionName: s.NameMethodID(txData[:4]),
RollupFlavor: RollupFlavorValidium,
ForkName: "etrog",
}
Expand Down
23 changes: 23 additions & 0 deletions etherman/sequence_batches_decode_shared.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package etherman

import (
"bytes"
"encoding/json"
"fmt"

Expand All @@ -21,6 +22,28 @@ type DecodedSequenceBatchesCallData struct {
Data []interface{}
}

type SequenceBatchesBase struct {
Signature []byte
Name string
}

func NewSequenceBatchesBase(signature []byte, name string) SequenceBatchesBase {
return SequenceBatchesBase{Signature: signature, Name: name}
}

// MatchMethodId returns true if the methodId is the one for the sequenceBatchesEtrog method
func (s *SequenceBatchesBase) MatchMethodId(methodId []byte) bool {
return bytes.Equal(methodId, s.Signature)
}

// NameMethodID returns name
func (s *SequenceBatchesBase) NameMethodID(methodId []byte) string {
if s.MatchMethodId(methodId) {
return s.Name
}
return ""
}

func decodeSequenceCallData(smcAbi abi.ABI, txData []byte) (*DecodedSequenceBatchesCallData, error) {
// Extract coded txs.
// Recover Method from signature and ABI
Expand Down

0 comments on commit 6ca088b

Please sign in to comment.