Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(tx_list_validator): remove unused code in tx_list_validator pa…
Browse files Browse the repository at this point in the history
…ckage (#609)
  • Loading branch information
davidtaikocha authored Mar 6, 2024
1 parent 8159155 commit cc4e302
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 58 deletions.
18 changes: 2 additions & 16 deletions driver/chain_syncer/calldata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand All @@ -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{}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down

0 comments on commit cc4e302

Please sign in to comment.