diff --git a/Makefile b/Makefile index 9fe570606..3860005eb 100644 --- a/Makefile +++ b/Makefile @@ -220,3 +220,7 @@ split-test-packages:$(BUILDDIR)/packages.txt split -d -n l/$(NUM_SPLIT) $< $<. test-group-%:split-test-packages cat $(BUILDDIR)/packages.txt.$* | xargs go test -parallel 4 -mod=readonly -timeout=10m -race -coverprofile=$*.profile.out -covermode=atomic + +.PHONY: proto +proto: + ignite generate proto-go -y diff --git a/go.mod b/go.mod index 60712c418..1e0f11f57 100644 --- a/go.mod +++ b/go.mod @@ -126,6 +126,7 @@ require ( github.com/fzipp/gocyclo v0.5.1 // indirect github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect + github.com/ghodss/yaml v1.0.0 // indirect github.com/go-critic/go-critic v0.6.3 // indirect github.com/go-kit/kit v0.12.0 // indirect github.com/go-kit/log v0.2.1 // indirect @@ -173,6 +174,7 @@ require ( github.com/gostaticanalysis/nilerr v0.1.1 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-bexpr v0.1.10 // indirect diff --git a/go.sum b/go.sum index 324467253..2782003c8 100644 --- a/go.sum +++ b/go.sum @@ -457,6 +457,7 @@ github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 h1:BAIP2Gihuqh github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46/go.mod h1:QNpY22eby74jVhqH4WhDLDwxc/vqsern6pW+u2kbkpc= github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4= github.com/ghemawat/stream v0.0.0-20171120220530-696b145b53b9/go.mod h1:106OIgooyS7OzLDOpUGgm9fA3bQENb/cFSyyBmMoJDs= +github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -765,6 +766,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0= github.com/guptarohit/asciigraph v0.5.5/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag= diff --git a/proto/evm/compression.proto b/proto/evm/compression.proto new file mode 100644 index 000000000..d2131d505 --- /dev/null +++ b/proto/evm/compression.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; +package seiprotocol.seichain.evm; + +import "gogoproto/gogo.proto"; + +option go_package = "github.com/sei-protocol/sei-chain/x/evm/types"; + +// CompressedData is only used for receipts as the moment +// It is not meant to be used outside of storage and retrieval +message CompressedData { + enum Algorithm { + SNAPPY = 0; + } + + bytes data = 1; + Algorithm algorithm = 2; +} \ No newline at end of file diff --git a/utils/compression/compress.go b/utils/compression/compress.go new file mode 100644 index 000000000..039e3282f --- /dev/null +++ b/utils/compression/compress.go @@ -0,0 +1,52 @@ +package compression + +import ( + "fmt" + + "github.com/gogo/protobuf/proto" + "github.com/sei-protocol/sei-chain/x/evm/types" +) + +func CompressMessage(message proto.Message) ([]byte, error) { + b, err := proto.Marshal(message) + if err != nil { + return nil, err + } + + bCompressed, err := compressSnappy(b) + if err != nil { + return nil, err + } + + cd := &types.CompressedData{ + Data: bCompressed, + Algorithm: types.CompressedData_SNAPPY, + } + return cd.Marshal() +} + +func DecompressMessage(target proto.Message, compressed []byte) error { + // this will work if the type is CompressedData + // if not, then it will try to unmarshal it as a regular proto message + var cd types.CompressedData + if err := cd.Unmarshal(compressed); err != nil { + // fall back to non-compress unmarshal + return proto.Unmarshal(compressed, target) + } + + // unmarshal was successful, but no data, treat as non-compressed + if cd.Data == nil { + return proto.Unmarshal(compressed, target) + } + + // add other algorithms here if we need to change it + if cd.Algorithm == types.CompressedData_SNAPPY { + decompressed, err := decompressSnappy(cd.Data) + if err != nil { + return err + } + return proto.Unmarshal(decompressed, target) + } + + return fmt.Errorf("unsupported compression algorithm: %d (%s)", cd.Algorithm, cd.Algorithm.String()) +} diff --git a/utils/compression/compress_test.go b/utils/compression/compress_test.go new file mode 100644 index 000000000..094160422 --- /dev/null +++ b/utils/compression/compress_test.go @@ -0,0 +1,242 @@ +package compression + +import ( + "bytes" + "fmt" + "testing" + + "github.com/gogo/protobuf/jsonpb" + "github.com/sei-protocol/sei-chain/x/evm/types" + "github.com/stretchr/testify/require" +) + +type compressFunc func([]byte) ([]byte, error) +type uncompressFunc func([]byte) ([]byte, error) + +func printCompressionRatio(algo string, b, bCompressed []byte) { + originalSize := len(b) + compressedSize := len(bCompressed) + compressionRatio := float64(compressedSize) / float64(originalSize) * 100 + + fmt.Printf("[Debug] (%s) Original size: %d bytes\n", algo, originalSize) + fmt.Printf("[Debug] (%s), compressed size: %d bytes\n", algo, compressedSize) + fmt.Printf("[Debug] (%s), compression ratio: %.2f%%\n", algo, compressionRatio) +} + +const receiptJson = `{ + "tx_type": 0, + "cumulative_gas_used": 0, + "contract_address": null, + "tx_hash_hex": "50000603341192e9af2688fcd052dfbba333d3196d40df121ca8bb92a345a2b4", + "gas_used": 243714, + "effective_gas_price": 1000000000, + "block_number": 12345, + "transaction_index": 0, + "status": 1, + "from": "0x70f67735d4b4d9fcfb3014da2470e2f82a8744c7", + "to": "0x2880ab155794e7179c9ee2e38200202908c17b43", + "vm_error": "", + "logs": [ + { + "address": "0x2880ab155794e7179c9ee2e38200202908c17b43", + "topics": [ + "0xd06a6b7f4918494b3719217d1802786c1f5112a6c1d88fe2cfec00b4584f6aec", + "0x53614f1cb0c031d4af66c04cb9c756234adad0e1cee85303795091499a4084eb" + ], + "data": "00000000000000000000000000000000000000000000000000000000667072e700000000000000000000000000000000000000000000000000000000024c88ca000000000000000000000000000000000000000000000000000000000000d23a", + "index": 0 + } + ], + "logsBloom": "00000400000000000000002000000000000000000000000000000000000000000200000000000000000000000008000000000000000000000000000000000000000000000000000000400000000010000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010001400000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000100008000000000800000000000" +}` + +func TestBackwardsCompatibility(t *testing.T) { + var r types.Receipt + err := jsonpb.Unmarshal(bytes.NewReader([]byte(receiptJson)), &r) + require.NoError(t, err) + receiptBytes, err := r.Marshal() + require.NoError(t, err) + + type compressTest struct { + Name string + Stored func() []byte + Expected []byte + WantErr bool + } + + for _, test := range []compressTest{ + { + Name: "not-compressed", + Stored: func() []byte { + return receiptBytes + }, + Expected: receiptBytes, + }, + { + Name: "compressed", + Stored: func() []byte { + cb, err := CompressMessage(&r) + require.NoError(t, err) + return cb + }, + Expected: receiptBytes, + }, + { + Name: "bad data", + Stored: func() []byte { + // not a receipt + block := &types.Log{ + Address: "0x1230ab155794e7179c9ee2e38200202908c17b43", + } + b, err := block.Marshal() + require.NoError(t, err) + return b + }, + WantErr: true, + }, + } { + t.Run(test.Name, func(t *testing.T) { + var result types.Receipt + err := DecompressMessage(&result, test.Stored()) + if test.WantErr { + require.Error(t, err) + } else { + // should have same bytes as expected bytes + require.NoError(t, err) + resultBytes, err := result.Marshal() + require.NoError(t, err) + require.Equal(t, test.Expected, resultBytes) + } + }) + } +} + +func TestCompressMessage(t *testing.T) { + var r types.Receipt + err := jsonpb.Unmarshal(bytes.NewReader([]byte(receiptJson)), &r) + require.NoError(t, err) + + raw, err := r.Marshal() + require.NoError(t, err) + + compressed, err := CompressMessage(&r) + require.NoError(t, err) + + var r2 types.Receipt + err = DecompressMessage(&r2, compressed) + require.NoError(t, err) + + require.Equal(t, r, r2) + printCompressionRatio("zlib", raw, compressed) +} + +func TestCompressionRatio(t *testing.T) { + r := types.Receipt{} + err := jsonpb.Unmarshal(bytes.NewReader([]byte(receiptJson)), &r) + require.NoError(t, err) + + b, err := r.Marshal() + require.NoError(t, err) + + tests := []struct { + Name string + Compress compressFunc + Uncompress uncompressFunc + }{ + //{ + // Name: "brotli", + // Compress: compressBrotli, + // Uncompress: decompressBrotli, + //}, + //{ + // Name: "bzip2", + // Compress: compressBzip2, + // Uncompress: decompressBzip2, + //}, + //{ + // Name: "lzma", + // Compress: compressLzma, + // Uncompress: decompressLzma, + //}, + //{ + // Name: "lz4", + // Compress: compressLz4, + // Uncompress: decompressLz4, + //}, + { + Name: "zlib", + Compress: compressZLib, + Uncompress: decompressZLib, + }, + //{ + // Name: "zstd", + // Compress: compressZstd, + // Uncompress: decompressZstd, + //}, + { + Name: "snappy", + Compress: compressSnappy, + Uncompress: decompressSnappy, + }, + //{ + // Name: "gzip", + // Compress: compressGzip, + // Uncompress: decompressGzip, + //}, + } + + for _, test := range tests { + t.Run(test.Name, func(t *testing.T) { + bCompressed, err := test.Compress(b) + require.NoError(t, err) + + bUncompressed, err := test.Uncompress(bCompressed) + require.NoError(t, err) + + require.Equal(t, string(b), string(bUncompressed), "%s: expected %s but got %s", test.Name, b, bUncompressed) + printCompressionRatio(test.Name, b, bCompressed) + }) + } +} + +func benchmarkCompression(b *testing.B, name string, compress compressFunc, uncompress uncompressFunc) { + r := types.Receipt{} + err := jsonpb.Unmarshal(bytes.NewReader([]byte(receiptJson)), &r) + require.NoError(b, err) + + data, err := r.Marshal() + require.NoError(b, err) + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + compressedData, err := compress(data) + require.NoError(b, err) + + _, err = uncompress(compressedData) + require.NoError(b, err) + } +} + +func BenchmarkCompression(b *testing.B) { + benchmarks := []struct { + name string + compress compressFunc + uncompress uncompressFunc + }{ + //{"brotli", compressBrotli, decompressBrotli}, + //{"bzip2", compressBzip2, decompressBzip2}, + //{"lzma", compressLzma, decompressLzma}, + {"zlib", compressZLib, decompressZLib}, + //{"lz4", compressLz4, decompressLz4}, + //{"zstd", compressZstd, decompressZstd}, + {"snappy", compressSnappy, decompressSnappy}, + //{"gzip", compressGzip, decompressGzip}, + } + + for _, bm := range benchmarks { + b.Run(bm.name, func(b *testing.B) { + benchmarkCompression(b, bm.name, bm.compress, bm.uncompress) + }) + } +} diff --git a/utils/compression/snappy.go b/utils/compression/snappy.go new file mode 100644 index 000000000..da38be651 --- /dev/null +++ b/utils/compression/snappy.go @@ -0,0 +1,30 @@ +package compression + +import ( + "bytes" + + "github.com/golang/snappy" +) + +func compressSnappy(b []byte) ([]byte, error) { + var buf bytes.Buffer + writer := snappy.NewBufferedWriter(&buf) + _, err := writer.Write(b) + if err != nil { + return nil, err + } + writer.Close() + + return buf.Bytes(), nil +} + +func decompressSnappy(b []byte) ([]byte, error) { + r := snappy.NewReader(bytes.NewReader(b)) + + var buf bytes.Buffer + if _, err := buf.ReadFrom(r); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} diff --git a/utils/compression/zlib.go b/utils/compression/zlib.go new file mode 100644 index 000000000..f1a5371c4 --- /dev/null +++ b/utils/compression/zlib.go @@ -0,0 +1,34 @@ +package compression + +import ( + "bytes" + + "compress/zlib" +) + +func compressZLib(b []byte) ([]byte, error) { + var buf bytes.Buffer + writer := zlib.NewWriter(&buf) + _, err := writer.Write(b) + if err != nil { + return nil, err + } + writer.Close() + + return buf.Bytes(), nil +} + +func decompressZLib(b []byte) ([]byte, error) { + r, err := zlib.NewReader(bytes.NewReader(b)) + if err != nil { + return nil, err + } + defer r.Close() + + var buf bytes.Buffer + if _, err := buf.ReadFrom(r); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} diff --git a/x/evm/keeper/receipt.go b/x/evm/keeper/receipt.go index 43e6c9388..772c61bfc 100644 --- a/x/evm/keeper/receipt.go +++ b/x/evm/keeper/receipt.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" + "github.com/sei-protocol/sei-chain/utils/compression" "github.com/sei-protocol/sei-chain/x/evm/types" ) @@ -17,8 +18,9 @@ func (k *Keeper) GetReceipt(ctx sdk.Context, txHash common.Hash) (*types.Receipt if bz == nil { return nil, errors.New("not found") } - r := types.Receipt{} - if err := r.Unmarshal(bz); err != nil { + + var r types.Receipt + if err := compression.DecompressMessage(&r, bz); err != nil { return nil, err } return &r, nil @@ -26,7 +28,7 @@ func (k *Keeper) GetReceipt(ctx sdk.Context, txHash common.Hash) (*types.Receipt func (k *Keeper) SetReceipt(ctx sdk.Context, txHash common.Hash, receipt *types.Receipt) error { store := ctx.KVStore(k.storeKey) - bz, err := receipt.Marshal() + bz, err := compression.CompressMessage(receipt) if err != nil { return err } diff --git a/x/evm/types/compression.pb.go b/x/evm/types/compression.pb.go new file mode 100644 index 000000000..f5d09ac24 --- /dev/null +++ b/x/evm/types/compression.pb.go @@ -0,0 +1,382 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: evm/compression.proto + +package types + +import ( + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type CompressedData_Algorithm int32 + +const ( + CompressedData_SNAPPY CompressedData_Algorithm = 0 +) + +var CompressedData_Algorithm_name = map[int32]string{ + 0: "SNAPPY", +} + +var CompressedData_Algorithm_value = map[string]int32{ + "SNAPPY": 0, +} + +func (x CompressedData_Algorithm) String() string { + return proto.EnumName(CompressedData_Algorithm_name, int32(x)) +} + +func (CompressedData_Algorithm) EnumDescriptor() ([]byte, []int) { + return fileDescriptor_e7e00623403436c4, []int{0, 0} +} + +// CompressedData is only used for receipts as the moment +// It is not meant to be used outside of storage and retrieval +type CompressedData struct { + Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` + Algorithm CompressedData_Algorithm `protobuf:"varint,2,opt,name=algorithm,proto3,enum=seiprotocol.seichain.evm.CompressedData_Algorithm" json:"algorithm,omitempty"` +} + +func (m *CompressedData) Reset() { *m = CompressedData{} } +func (m *CompressedData) String() string { return proto.CompactTextString(m) } +func (*CompressedData) ProtoMessage() {} +func (*CompressedData) Descriptor() ([]byte, []int) { + return fileDescriptor_e7e00623403436c4, []int{0} +} +func (m *CompressedData) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *CompressedData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_CompressedData.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *CompressedData) XXX_Merge(src proto.Message) { + xxx_messageInfo_CompressedData.Merge(m, src) +} +func (m *CompressedData) XXX_Size() int { + return m.Size() +} +func (m *CompressedData) XXX_DiscardUnknown() { + xxx_messageInfo_CompressedData.DiscardUnknown(m) +} + +var xxx_messageInfo_CompressedData proto.InternalMessageInfo + +func (m *CompressedData) GetData() []byte { + if m != nil { + return m.Data + } + return nil +} + +func (m *CompressedData) GetAlgorithm() CompressedData_Algorithm { + if m != nil { + return m.Algorithm + } + return CompressedData_SNAPPY +} + +func init() { + proto.RegisterEnum("seiprotocol.seichain.evm.CompressedData_Algorithm", CompressedData_Algorithm_name, CompressedData_Algorithm_value) + proto.RegisterType((*CompressedData)(nil), "seiprotocol.seichain.evm.CompressedData") +} + +func init() { proto.RegisterFile("evm/compression.proto", fileDescriptor_e7e00623403436c4) } + +var fileDescriptor_e7e00623403436c4 = []byte{ + // 232 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4d, 0x2d, 0xcb, 0xd5, + 0x4f, 0xce, 0xcf, 0x2d, 0x28, 0x4a, 0x2d, 0x2e, 0xce, 0xcc, 0xcf, 0xd3, 0x2b, 0x28, 0xca, 0x2f, + 0xc9, 0x17, 0x92, 0x28, 0x4e, 0xcd, 0x04, 0xb3, 0x92, 0xf3, 0x73, 0xf4, 0x8a, 0x53, 0x33, 0x93, + 0x33, 0x12, 0x33, 0xf3, 0xf4, 0x52, 0xcb, 0x72, 0xa5, 0x44, 0xd2, 0xf3, 0xd3, 0xf3, 0xc1, 0x52, + 0xfa, 0x20, 0x16, 0x44, 0xbd, 0x52, 0x3f, 0x23, 0x17, 0x9f, 0x33, 0xd4, 0x94, 0xd4, 0x14, 0x97, + 0xc4, 0x92, 0x44, 0x21, 0x21, 0x2e, 0x96, 0x94, 0xc4, 0x92, 0x44, 0x09, 0x46, 0x05, 0x46, 0x0d, + 0x9e, 0x20, 0x30, 0x5b, 0x28, 0x80, 0x8b, 0x33, 0x31, 0x27, 0x3d, 0xbf, 0x28, 0xb3, 0x24, 0x23, + 0x57, 0x82, 0x49, 0x81, 0x51, 0x83, 0xcf, 0xc8, 0x48, 0x0f, 0x97, 0x55, 0x7a, 0xa8, 0x06, 0xea, + 0x39, 0xc2, 0x74, 0x06, 0x21, 0x0c, 0x51, 0x12, 0xe7, 0xe2, 0x84, 0x8b, 0x0b, 0x71, 0x71, 0xb1, + 0x05, 0xfb, 0x39, 0x06, 0x04, 0x44, 0x0a, 0x30, 0x38, 0xb9, 0x9f, 0x78, 0x24, 0xc7, 0x78, 0xe1, + 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, 0x2c, 0xc7, 0x70, + 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x6e, 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, 0xae, + 0x7e, 0x71, 0x6a, 0xa6, 0x2e, 0xcc, 0x72, 0x30, 0x07, 0x6c, 0xbb, 0x7e, 0x85, 0x3e, 0x28, 0x58, + 0x4a, 0x2a, 0x0b, 0x52, 0x8b, 0x93, 0xd8, 0xc0, 0xf2, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xdf, 0x44, 0xa1, 0xb6, 0x2a, 0x01, 0x00, 0x00, +} + +func (m *CompressedData) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *CompressedData) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *CompressedData) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Algorithm != 0 { + i = encodeVarintCompression(dAtA, i, uint64(m.Algorithm)) + i-- + dAtA[i] = 0x10 + } + if len(m.Data) > 0 { + i -= len(m.Data) + copy(dAtA[i:], m.Data) + i = encodeVarintCompression(dAtA, i, uint64(len(m.Data))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintCompression(dAtA []byte, offset int, v uint64) int { + offset -= sovCompression(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *CompressedData) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Data) + if l > 0 { + n += 1 + l + sovCompression(uint64(l)) + } + if m.Algorithm != 0 { + n += 1 + sovCompression(uint64(m.Algorithm)) + } + return n +} + +func sovCompression(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozCompression(x uint64) (n int) { + return sovCompression(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *CompressedData) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCompression + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: CompressedData: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: CompressedData: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Data", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCompression + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthCompression + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthCompression + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Data = append(m.Data[:0], dAtA[iNdEx:postIndex]...) + if m.Data == nil { + m.Data = []byte{} + } + iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Algorithm", wireType) + } + m.Algorithm = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCompression + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Algorithm |= CompressedData_Algorithm(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipCompression(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthCompression + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipCompression(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCompression + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCompression + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowCompression + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthCompression + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupCompression + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthCompression + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthCompression = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowCompression = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupCompression = fmt.Errorf("proto: unexpected end of group") +)