From 6ca088b045033611f9ffe7a37723cea39a188e6d Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:24:46 +0200 Subject: [PATCH] reduce duplication --- .../sequence_batches_decode_elderberry.go | 18 ++++----------- ...ence_batches_decode_elderberry_validium.go | 21 +++++------------ etherman/sequence_batches_decode_etrog.go | 18 ++++----------- .../sequence_batches_decode_etrog_validium.go | 7 ++++-- etherman/sequence_batches_decode_shared.go | 23 +++++++++++++++++++ 5 files changed, 42 insertions(+), 45 deletions(-) diff --git a/etherman/sequence_batches_decode_elderberry.go b/etherman/sequence_batches_decode_elderberry.go index 9f00e68..0c4ede1 100644 --- a/etherman/sequence_batches_decode_elderberry.go +++ b/etherman/sequence_batches_decode_elderberry.go @@ -1,7 +1,6 @@ package etherman import ( - "bytes" "encoding/json" "strings" @@ -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) { @@ -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", } diff --git a/etherman/sequence_batches_decode_elderberry_validium.go b/etherman/sequence_batches_decode_elderberry_validium.go index 082aefc..d9d8683 100644 --- a/etherman/sequence_batches_decode_elderberry_validium.go +++ b/etherman/sequence_batches_decode_elderberry_validium.go @@ -1,7 +1,6 @@ package etherman import ( - "bytes" "encoding/json" "fmt" "strings" @@ -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 } @@ -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) { diff --git a/etherman/sequence_batches_decode_etrog.go b/etherman/sequence_batches_decode_etrog.go index 4e7ff3e..1363cea 100644 --- a/etherman/sequence_batches_decode_etrog.go +++ b/etherman/sequence_batches_decode_etrog.go @@ -1,7 +1,6 @@ package etherman import ( - "bytes" "encoding/json" "strings" @@ -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) { @@ -60,7 +50,7 @@ func (s *SequenceBatchesDecodeEtrog) DecodeSequenceBatches(txData []byte, lastBa } SequencedBatchMetadata := &SequencedBatchMetadata{ - CallFunctionName: "sequenceBatchesEtrog", + CallFunctionName: s.NameMethodID(txData[:4]), RollupFlavor: RollupFlavorZkEVM, ForkName: "etrog", } diff --git a/etherman/sequence_batches_decode_etrog_validium.go b/etherman/sequence_batches_decode_etrog_validium.go index 5c58ff6..28dd78f 100644 --- a/etherman/sequence_batches_decode_etrog_validium.go +++ b/etherman/sequence_batches_decode_etrog_validium.go @@ -19,6 +19,7 @@ var ( ) type SequenceBatchesDecodeEtrogValidium struct { + SequenceBatchesBase da dataavailability.BatchDataProvider SmcABI abi.ABI } @@ -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 @@ -76,7 +79,7 @@ func (s *SequenceBatchesDecodeEtrogValidium) DecodeSequenceBatches(txData []byte } SequencedBatchMetadata := &SequencedBatchMetadata{ - CallFunctionName: "sequenceBatchesEtrogValidium", + CallFunctionName: s.NameMethodID(txData[:4]), RollupFlavor: RollupFlavorValidium, ForkName: "etrog", } diff --git a/etherman/sequence_batches_decode_shared.go b/etherman/sequence_batches_decode_shared.go index f87c921..eab87aa 100644 --- a/etherman/sequence_batches_decode_shared.go +++ b/etherman/sequence_batches_decode_shared.go @@ -1,6 +1,7 @@ package etherman import ( + "bytes" "encoding/json" "fmt" @@ -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