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

Commit dd50068

Browse files
feat(proposer): introduce zlib for transactions list bytes compression (#649)
1 parent f561847 commit dd50068

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

driver/chain_syncer/calldata/syncer.go

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/taikoxyz/taiko-client/driver/state"
2323
txlistfetcher "github.com/taikoxyz/taiko-client/driver/txlist_fetcher"
2424
"github.com/taikoxyz/taiko-client/internal/metrics"
25+
"github.com/taikoxyz/taiko-client/internal/utils"
2526
eventIterator "github.com/taikoxyz/taiko-client/pkg/chain_iterator/event_iterator"
2627
"github.com/taikoxyz/taiko-client/pkg/rpc"
2728
txListValidator "github.com/taikoxyz/taiko-client/pkg/txlist_validator"
@@ -247,6 +248,10 @@ func (s *Syncer) onBlockProposed(
247248
}
248249
}
249250

251+
if txListBytes, err = utils.Decompress(txListBytes); err != nil {
252+
return fmt.Errorf("failed to decompress tx list bytes: %w", err)
253+
}
254+
250255
// If the transactions list is invalid, we simply insert an empty L2 block.
251256
if !s.txListValidator.ValidateTxList(event.BlockId, txListBytes, event.Meta.BlobUsed) {
252257
log.Info("Invalid transactions list, insert an empty L2 block instead", "blockID", event.BlockId)

internal/utils/util_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package utils_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"github.com/taikoxyz/taiko-client/internal/testutils"
8+
"github.com/taikoxyz/taiko-client/internal/utils"
9+
)
10+
11+
func TestEncodeDecodeBytes(t *testing.T) {
12+
b := testutils.RandomBytes(1024)
13+
14+
compressed, err := utils.Compress(b)
15+
require.Nil(t, err)
16+
require.NotEmpty(t, compressed)
17+
18+
decompressed, err := utils.Decompress(compressed)
19+
require.Nil(t, err)
20+
21+
require.Equal(t, b, decompressed)
22+
}

internal/utils/utils.go

+46-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package utils
22

33
import (
4+
"bytes"
5+
"compress/zlib"
46
"crypto/rand"
7+
"errors"
58
"fmt"
9+
"io"
610
"math/big"
711
"os"
812
"strings"
@@ -14,22 +18,22 @@ import (
1418
"golang.org/x/exp/constraints"
1519
)
1620

21+
// LoadEnv loads all the test environment variables.
1722
func LoadEnv() {
18-
// load test environment variables.
1923
currentPath, err := os.Getwd()
2024
if err != nil {
21-
log.Debug("get current path failed", "err", err)
25+
log.Debug("Failed to get current path", "err", err)
2226
}
2327
path := strings.Split(currentPath, "/taiko-client")
2428
if len(path) == 0 {
25-
log.Debug("not a taiko-client repo")
29+
log.Debug("Not a taiko-client repo")
2630
}
27-
err = godotenv.Load(fmt.Sprintf("%s/taiko-client/integration_test/.env", path[0]))
28-
if err != nil {
29-
log.Debug("failed to load test env", "current path", currentPath, "err", err)
31+
if godotenv.Load(fmt.Sprintf("%s/taiko-client/integration_test/.env", path[0])) != nil {
32+
log.Debug("Failed to load test env", "current path", currentPath, "err", err)
3033
}
3134
}
3235

36+
// RandUint64 returns a random uint64 number.
3337
func RandUint64(max *big.Int) uint64 {
3438
if max == nil {
3539
max = new(big.Int)
@@ -40,6 +44,7 @@ func RandUint64(max *big.Int) uint64 {
4044
return num.Uint64()
4145
}
4246

47+
// RandUint32 returns a random uint32 number.
4348
func RandUint32(max *big.Int) uint32 {
4449
if max == nil {
4550
max = new(big.Int)
@@ -69,3 +74,38 @@ func Max[T constraints.Integer](a, b T) T {
6974
}
7075
return b
7176
}
77+
78+
// Compress compresses the given txList bytes using zlib.
79+
func Compress(txList []byte) ([]byte, error) {
80+
var b bytes.Buffer
81+
w := zlib.NewWriter(&b)
82+
defer w.Close()
83+
84+
if _, err := w.Write(txList); err != nil {
85+
return nil, err
86+
}
87+
88+
if err := w.Flush(); err != nil {
89+
return nil, err
90+
}
91+
92+
return b.Bytes(), nil
93+
}
94+
95+
// Decompress decompresses the given txList bytes using zlib.
96+
func Decompress(compressedTxList []byte) ([]byte, error) {
97+
r, err := zlib.NewReader(bytes.NewBuffer(compressedTxList))
98+
if err != nil {
99+
return nil, err
100+
}
101+
defer r.Close()
102+
103+
b, err := io.ReadAll(r)
104+
if err != nil {
105+
if !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
106+
return nil, err
107+
}
108+
}
109+
110+
return b, nil
111+
}

proposer/proposer.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/taikoxyz/taiko-client/bindings"
2020
"github.com/taikoxyz/taiko-client/bindings/encoding"
2121
"github.com/taikoxyz/taiko-client/internal/metrics"
22+
"github.com/taikoxyz/taiko-client/internal/utils"
2223
"github.com/taikoxyz/taiko-client/pkg/rpc"
2324
"github.com/taikoxyz/taiko-client/pkg/sender"
2425
selector "github.com/taikoxyz/taiko-client/proposer/prover_selector"
@@ -319,12 +320,17 @@ func (p *Proposer) ProposeTxList(
319320
txListBytes []byte,
320321
txNum uint,
321322
) error {
323+
compressedTxListBytes, err := utils.Compress(txListBytes)
324+
if err != nil {
325+
return err
326+
}
327+
322328
tx, err := p.txBuilder.Build(
323329
ctx,
324330
p.tierFees,
325331
p.sender.GetOpts(p.ctx),
326332
p.IncludeParentMetaHash,
327-
txListBytes,
333+
compressedTxListBytes,
328334
)
329335
if err != nil {
330336
log.Warn("Failed to build TaikoL1.proposeBlock transaction", "error", encoding.TryParsingCustomError(err))

0 commit comments

Comments
 (0)