Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion adapters/core2sn/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/compression"
)

func AdaptSegmentLengths(l core.SegmentLengths) starknet.SegmentLengths {
Expand Down Expand Up @@ -115,7 +116,7 @@ func AdaptDeprecatedEntryPoint(ep *core.DeprecatedEntryPoint) starknet.EntryPoin
func AdaptDeprecatedCairoClass(
class *core.DeprecatedCairoClass,
) (starknet.DeprecatedCairoClass, error) {
decompressedProgram, err := utils.Gzip64Decode(class.Program)
decompressedProgram, err := compression.Gzip64Decode(class.Program)
if err != nil {
return starknet.DeprecatedCairoClass{}, err
}
Expand Down
3 changes: 2 additions & 1 deletion adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/NethermindEth/juno/l1/eth"
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/compression"
)

// ErrPreConfirmedIdentifierMismatch is returned by AdaptPreConfirmedWithDelta
Expand Down Expand Up @@ -430,7 +431,7 @@ func AdaptDeprecatedCairoClass(
}

var err error
class.Program, err = utils.Gzip64Encode(response.Program)
class.Program, err = compression.Gzip64Encode(response.Program)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions clients/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gateway

import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
Expand All @@ -12,6 +11,7 @@ import (
"net/url"
"time"

"github.com/NethermindEth/juno/utils/compression"
"github.com/NethermindEth/juno/utils/log"
)

Expand Down Expand Up @@ -146,7 +146,8 @@ func prepareRequestBody(jsonBody []byte) (io.Reader, bool, error) {
}

var buf bytes.Buffer
gzWriter := gzip.NewWriter(&buf)
gzWriter := compression.GzipWriter(&buf)
defer gzWriter.Release()
if _, err := gzWriter.Write(jsonBody); err != nil {
return nil, false, fmt.Errorf("writing gzip content: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/class_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/compression"
)

func deprecatedCairoClassHash(class *DeprecatedCairoClass) (felt.Felt, error) {
decompressedProgram, err := utils.Gzip64Decode(class.Program)
decompressedProgram, err := compression.Gzip64Decode(class.Program)
if err != nil {
return felt.Felt{}, err
}
Expand Down
14 changes: 3 additions & 11 deletions jsonrpc/http.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
package jsonrpc

import (
"compress/gzip"
"context"
"errors"
"io"
"maps"
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/utils/compression"
"github.com/NethermindEth/juno/utils/log"
"go.uber.org/zap"
)

// gzipWriterPool holds gzip writers for reuse;
// callers must Reset a writer onto their destination before writing to it.
var gzipWriterPool = sync.Pool{
New: func() any { return gzip.NewWriter(io.Discard) },
}

type HTTP struct {
rpc *Server
logger log.StructuredLogger
Expand Down Expand Up @@ -131,11 +124,10 @@ func (h *HTTP) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
var ioWriter io.Writer = writer
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
writer.Header().Set("Content-Encoding", "gzip")
gw := gzipWriterPool.Get().(*gzip.Writer)
gw.Reset(writer)
gw := compression.GzipWriter(writer)
defer func() {
closeErr := gw.Close()
gzipWriterPool.Put(gw)
gw.Release()
if closeErr != nil {
http.Error(writer, "gzip close error", http.StatusInternalServerError)
return
Expand Down
21 changes: 6 additions & 15 deletions rpc/v10/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"sync"

"github.com/NethermindEth/juno/adapters/sn2core"
Expand All @@ -21,21 +20,14 @@ import (
"github.com/NethermindEth/juno/rpc/rpccore"
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/starknet/compiler"
"github.com/NethermindEth/juno/utils/compression"
"github.com/NethermindEth/juno/utils/throttler"
"go.uber.org/zap"
)

var (
gzPool = sync.Pool{
New: func() any {
w, _ := gzip.NewWriterLevel(io.Discard, gzip.BestSpeed)
return w
},
}
bufPool = sync.Pool{
New: func() any { return new(bytes.Buffer) },
}
)
var bufPool = sync.Pool{
New: func() any { return new(bytes.Buffer) },
}

// AdaptTransaction adapts a core.Transaction to a local Transaction.
// It's a wrapper around AdaptCoreTransaction that allows to exclude proof facts
Expand Down Expand Up @@ -244,9 +236,8 @@ func ContractClassToGatewayPayload(class *ContractClass) ([]byte, error) {
defer bufPool.Put(sierraBuf)

b64 := base64.NewEncoder(base64.StdEncoding, sierraBuf)
gz := gzPool.Get().(*gzip.Writer)
gz.Reset(b64)
defer gzPool.Put(gz)
gz := compression.GzipWriterLevel(b64, gzip.BestSpeed)
defer gz.Release()

enc := json.NewEncoder(gz)
enc.SetEscapeHTML(false)
Expand Down
4 changes: 2 additions & 2 deletions rpc/v10/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/NethermindEth/juno/starknet"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/sync/preconfirmed"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/compression"
"github.com/NethermindEth/juno/utils/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -2285,7 +2285,7 @@ func TestContractClassToGatewayPayload(t *testing.T) {
require.Equal(t, class.EntryPoints, decoded.EntryPoints)
require.Equal(t, class.ABI, decoded.ABI)

sierraJSON, err := utils.Gzip64Decode(decoded.SierraProgram)
sierraJSON, err := compression.Gzip64Decode(decoded.SierraProgram)
require.NoError(t, err, "sierra_program must be gzip+base64 encoded")

var roundTripped []felt.Felt
Expand Down
3 changes: 2 additions & 1 deletion rpc/v8/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/starknet/compiler"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/compression"
)

// https://github.com/starkware-libs/starknet-specs/blob/v0.8.1/api/starknet_api_openrpc.json#L3159
Expand Down Expand Up @@ -66,7 +67,7 @@
}
base64Program := string(program[1 : len(program)-1])

feederClass.DeprecatedCairo.Program, err = utils.Gzip64Decode(base64Program)
feederClass.DeprecatedCairo.Program, err = compression.Gzip64Decode(base64Program)

Check warning on line 70 in rpc/v8/class.go

View check run for this annotation

Codecov / codecov/patch

rpc/v8/class.go#L70

Added line #L70 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/v8/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/NethermindEth/juno/rpc/rpccore"
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/starknet/compiler"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/compression"
"github.com/NethermindEth/juno/utils/throttler"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -711,7 +711,7 @@ func (h *Handler) pushToFeederGateway(ctx context.Context, tx *BroadcastedTransa
return AddTxResponse{}, jsonrpc.Err(jsonrpc.InternalError, errIn.Error())
}

gwSierraProg, errIn := utils.Gzip64Encode(sierraProgBytes)
gwSierraProg, errIn := compression.Gzip64Encode(sierraProgBytes)
if errIn != nil {
return AddTxResponse{}, jsonrpc.Err(jsonrpc.InternalError, errIn.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/v9/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"github.com/NethermindEth/juno/rpc/rpccore"
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/starknet/compiler"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/compression"
)

type CalldataInputs = rpccore.LimitSlice[felt.Felt, rpccore.FunctionCalldataLimit]
Expand Down Expand Up @@ -73,7 +73,7 @@
}
base64Program := string(program[1 : len(program)-1])

feederClass.DeprecatedCairo.Program, err = utils.Gzip64Decode(base64Program)
feederClass.DeprecatedCairo.Program, err = compression.Gzip64Decode(base64Program)

Check warning on line 76 in rpc/v9/class.go

View check run for this annotation

Codecov / codecov/patch

rpc/v9/class.go#L76

Added line #L76 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/v9/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/NethermindEth/juno/rpc/rpccore"
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/starknet/compiler"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/utils/compression"
"github.com/NethermindEth/juno/utils/throttler"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -789,7 +789,7 @@ func (h *Handler) pushToFeederGateway(
return AddTxResponse{}, jsonrpc.Err(jsonrpc.InternalError, errIn.Error())
}

gwSierraProg, errIn := utils.Gzip64Encode(sierraProgBytes)
gwSierraProg, errIn := compression.Gzip64Encode(sierraProgBytes)
if errIn != nil {
return AddTxResponse{}, jsonrpc.Err(jsonrpc.InternalError, errIn.Error())
}
Expand Down
41 changes: 0 additions & 41 deletions utils/compression.go

This file was deleted.

Loading
Loading