diff --git a/driver/chain_syncer/calldata/syncer.go b/driver/chain_syncer/calldata/syncer.go index 25224194e..0610db44c 100644 --- a/driver/chain_syncer/calldata/syncer.go +++ b/driver/chain_syncer/calldata/syncer.go @@ -24,7 +24,7 @@ import ( "github.com/taikoxyz/taiko-client/internal/metrics" eventIterator "github.com/taikoxyz/taiko-client/pkg/chain_iterator/event_iterator" "github.com/taikoxyz/taiko-client/pkg/rpc" - txListValidator "github.com/taikoxyz/taiko-client/pkg/txlistvalidator" + txListValidator "github.com/taikoxyz/taiko-client/pkg/txlist_validator" ) var ( @@ -248,20 +248,6 @@ func (s *Syncer) onBlockProposed( return fmt.Errorf("failed to decode tx list: %w", err) } - // Check whether the transactions list is valid. - hint, invalidTxIndex, err := s.txListValidator.ValidateTxList(event.BlockId, txListBytes, event.Meta.BlobUsed) - if err != nil { - return fmt.Errorf("failed to validate transactions list: %w", err) - } - - log.Info( - "Validate transactions list", - "blockID", event.BlockId, - "hint", hint, - "invalidTxIndex", invalidTxIndex, - "bytes", len(txListBytes), - ) - l1Origin := &rawdb.L1Origin{ BlockID: event.BlockId, L2BlockHash: common.Hash{}, // Will be set by taiko-geth. @@ -275,7 +261,7 @@ func (s *Syncer) onBlockProposed( } // If the transactions list is invalid, we simply insert an empty L2 block. - if hint != txListValidator.HintOK { + if !s.txListValidator.ValidateTxList(event.BlockId, txListBytes, event.Meta.BlobUsed) { log.Info("Invalid transactions list, insert an empty L2 block instead", "blockID", event.BlockId) txListBytes = []byte{} } diff --git a/pkg/txlistvalidator/tx_list_validator.go b/pkg/txlist_validator/tx_list_validator.go similarity index 71% rename from pkg/txlistvalidator/tx_list_validator.go rename to pkg/txlist_validator/tx_list_validator.go index c3b9ee42f..2a65863cb 100644 --- a/pkg/txlistvalidator/tx_list_validator.go +++ b/pkg/txlist_validator/tx_list_validator.go @@ -8,15 +8,7 @@ import ( "github.com/ethereum/go-ethereum/rlp" ) -// InvalidTxListReason represents a reason why a transactions list is invalid. -type InvalidTxListReason uint8 - -// All invalid transactions list reasons. -const ( - HintNone InvalidTxListReason = iota - HintOK -) - +// TxListValidator is responsible for validating the transactions list in a TaikoL1.proposeBlock transaction. type TxListValidator struct { blockMaxGasLimit uint64 maxTransactionsPerBlock uint64 @@ -45,40 +37,30 @@ func (v *TxListValidator) ValidateTxList( blockID *big.Int, txListBytes []byte, blobUsed bool, -) (hint InvalidTxListReason, txIdx int, err error) { +) (isValid bool) { + // If the transaction list is empty, it's valid. if len(txListBytes) == 0 { - return HintOK, 0, nil + return true } - hint, txIdx = v.isTxListValid(blockID, txListBytes, blobUsed) - - return hint, txIdx, nil -} - -// isTxListValid checks whether the transaction list is valid. -func (v *TxListValidator) isTxListValid( - blockID *big.Int, - txListBytes []byte, - blobUsed bool, -) (hint InvalidTxListReason, txIdx int) { if !blobUsed && (len(txListBytes) > int(v.maxBytesPerTxList)) { log.Info("Transactions list binary too large", "length", len(txListBytes), "blockID", blockID) - return HintNone, 0 + return false } var txs types.Transactions if err := rlp.DecodeBytes(txListBytes, &txs); err != nil { log.Info("Failed to decode transactions list bytes", "blockID", blockID, "error", err) - return HintNone, 0 + return false } log.Debug("Transactions list decoded", "blockID", blockID, "length", len(txs)) if txs.Len() > int(v.maxTransactionsPerBlock) { log.Info("Too many transactions", "blockID", blockID, "count", txs.Len()) - return HintNone, 0 + return false } log.Info("Transaction list is valid", "blockID", blockID) - return HintOK, 0 + return true } diff --git a/pkg/txlistvalidator/tx_list_validator_test.go b/pkg/txlist_validator/tx_list_validator_test.go similarity index 88% rename from pkg/txlistvalidator/tx_list_validator_test.go rename to pkg/txlist_validator/tx_list_validator_test.go index 12d3e29ee..4cf0fd018 100644 --- a/pkg/txlistvalidator/tx_list_validator_test.go +++ b/pkg/txlist_validator/tx_list_validator_test.go @@ -42,51 +42,44 @@ func TestIsTxListValid(t *testing.T) { name string blockID *big.Int txListBytes []byte - wantReason InvalidTxListReason - wantTxIdx int + isValid bool }{ { "txListBytes binary too large", chainID, randBytes(maxTxlistBytes + 1), - HintNone, - 0, + false, }, { "txListBytes not decodable to rlp", chainID, - randBytes(0), - HintNone, - 0, + randBytes(0x1), + false, }, { "txListBytes too many transactions", chainID, rlpEncodedTransactionBytes(int(maxBlockNumTxs)+1, true), - HintNone, - 0, + false, }, { "success empty tx list", chainID, rlpEncodedTransactionBytes(0, true), - HintOK, - 0, + true, }, { "success non-empty tx list", chainID, rlpEncodedTransactionBytes(1, true), - HintOK, - 0, + true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - reason, txIdx := v.isTxListValid(tt.blockID, tt.txListBytes, false) - require.Equal(t, tt.wantReason, reason) - require.Equal(t, tt.wantTxIdx, txIdx) + isValid := v.ValidateTxList(tt.blockID, tt.txListBytes, false) + require.Equal(t, tt.isValid, isValid) }) } }