Skip to content

Commit

Permalink
test(taiko-client): update tests for TxListDecompressor (#17032)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha authored May 8, 2024
1 parent 85021ba commit dbdbeb8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 74 deletions.
8 changes: 4 additions & 4 deletions packages/taiko-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ lint:
&& goimports -local "github.com/taikoxyz/taiko-mono/packages/taiko-client" -w ./ \
&& golangci-lint run

test:
PACKAGE=${PACKAGE} \
test: lint
@PACKAGE=${PACKAGE} \
RUN_TESTS=true \
./integration_test/entrypoint.sh

dev_net:
COMPILE_PROTOCOL=${COMPILE_PROTOCOL} \
@COMPILE_PROTOCOL=${COMPILE_PROTOCOL} \
./integration_test/entrypoint.sh

gen_bindings:
TAIKO_GETH_DIR=${TAIKO_GETH_DIR} \
@TAIKO_GETH_DIR=${TAIKO_GETH_DIR} \
./scripts/gen_bindings.sh

.PHONY: build \
Expand Down
2 changes: 1 addition & 1 deletion packages/taiko-client/bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8532b7750513b87732340030139513d2b2ee203b
d4b62d23b4819bd932ad52b040a01f2cbbbeb9f2
Original file line number Diff line number Diff line change
Expand Up @@ -6,101 +6,86 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/internal/testutils"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/internal/utils"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/pkg/rpc"
)

var (
maxBlocksGasLimit = uint64(50)
maxTxlistBytes = uint64(10000)
chainID = genesis.Config.ChainID
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
genesis = &core.Genesis{
Config: params.AllEthashProtocolChanges,
Alloc: types.GenesisAlloc{testAddr: {Balance: big.NewInt(2e15)}},
ExtraData: []byte("test genesis"),
Timestamp: 9000,
BaseFee: big.NewInt(params.InitialBaseFee),
}
chainID = new(big.Int).SetUint64(167001)
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
)

func TestDecomporess(t *testing.T) {
d := NewTxListDecompressor(
maxBlocksGasLimit,
maxTxlistBytes,
type TxListDecompressorTestSuite struct {
testutils.ClientTestSuite
d *TxListDecompressor
}

func (s *TxListDecompressorTestSuite) SetupTest() {
s.ClientTestSuite.SetupTest()
s.d = NewTxListDecompressor(
params.MaxGasLimit,
rpc.BlockMaxTxListBytes,
chainID,
)
}

func (s *TxListDecompressorTestSuite) TestZeroBytes() {
s.Empty(s.d.TryDecompress(chainID, []byte{}, false))
}

func (s *TxListDecompressorTestSuite) TestCalldataSize() {
s.Empty(s.d.TryDecompress(chainID, randBytes(rpc.BlockMaxTxListBytes+1), false))
s.Empty(s.d.TryDecompress(chainID, randBytes(rpc.BlockMaxTxListBytes-1), false))
}

func (s *TxListDecompressorTestSuite) TestValidTxList() {
compressed, err := utils.Compress(rlpEncodedTransactionBytes(1, true))
require.NoError(t, err)

tests := []struct {
name string
blockID *big.Int
txListBytes []byte
decompressed []byte
}{
{
"txListBytes binary too large",
chainID,
randBytes(maxTxlistBytes + 1),
[]byte{},
},
{
"txListBytes not decodable to rlp",
chainID,
randBytes(0x1),
[]byte{},
},
{
"success empty tx list",
chainID,
rlpEncodedTransactionBytes(0, true),
[]byte{},
},
{
"success non-empty tx list",
chainID,
compressed,
rlpEncodedTransactionBytes(1, true),
},
}
s.Nil(err)
decompressed, err := utils.Decompress(compressed)
s.Nil(err)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.decompressed, d.TryDecompress(tt.blockID, tt.txListBytes, false))
})
}
s.Equal(s.d.TryDecompress(chainID, compressed, true), decompressed)
s.Equal(s.d.TryDecompress(chainID, compressed, false), decompressed)
}

func (s *TxListDecompressorTestSuite) TestInvalidTxList() {
compressed, err := utils.Compress(randBytes(1024))
s.Nil(err)

s.Zero(len(s.d.TryDecompress(chainID, compressed, true)))
s.Zero(len(s.d.TryDecompress(chainID, compressed, false)))
}

func (s *TxListDecompressorTestSuite) TestInvalidZlibBytes() {
s.Zero(len(s.d.TryDecompress(chainID, randBytes(1024), true)))
s.Zero(len(s.d.TryDecompress(chainID, randBytes(1024), false)))
}

func TestDriverTestSuite(t *testing.T) {
suite.Run(t, new(TxListDecompressorTestSuite))
}

func rlpEncodedTransactionBytes(l int, signed bool) []byte {
txs := make(types.Transactions, 0)
for i := 0; i < l; i++ {
var tx *types.Transaction
if signed {
txData := &types.LegacyTx{
Nonce: 1,
To: &testAddr,
GasPrice: common.Big256,
Value: common.Big1,
Gas: 10,
}

tx = types.MustSignNewTx(testKey, types.LatestSigner(genesis.Config), txData)
txData := &types.LegacyTx{Nonce: 1, To: &testAddr, GasPrice: common.Big256, Value: common.Big1, Gas: 10}

tx = types.MustSignNewTx(testKey, types.LatestSigner(&params.ChainConfig{ChainID: chainID}), txData)
} else {
tx = types.NewTransaction(1, testAddr, common.Big1, 10, new(big.Int).SetUint64(10*params.GWei), nil)
}
txs = append(
txs,
tx,
)
txs = append(txs, tx)
}
b, _ := rlp.EncodeToBytes(txs)
return b
Expand Down

0 comments on commit dbdbeb8

Please sign in to comment.