summaryrefslogtreecommitdiff
path: root/network/msgCompressor.go
blob: 28f835832fc1d0c28c41366caa89ff773926e725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package network

import (
	"bytes"
	"fmt"
	"io"

	"github.com/DataDog/zstd"

	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
)

var zstdCompressionMagic = [4]byte{0x28, 0xb5, 0x2f, 0xfd}

const zstdCompressionLevel = zstd.BestSpeed

// checkCanCompress checks if there is an proposal payload message and peers supporting compression
func checkCanCompress(request broadcastRequest, peers []*wsPeer) bool {
	canCompress := false
	hasPP := false
	for _, tag := range request.tags {
		if tag == protocol.ProposalPayloadTag {
			hasPP = true
			break
		}
	}
	// if have proposal payload check if there are any peers supporting compression
	if hasPP {
		for _, peer := range peers {
			if peer.pfProposalCompressionSupported() {
				canCompress = true
				break
			}
		}
	}
	return canCompress
}

// zstdCompressMsg returns a concatenation of a tag and compressed data
func zstdCompressMsg(tbytes []byte, d []byte) ([]byte, string) {
	bound := zstd.CompressBound(len(d))
	if bound < len(d) {
		// although CompressBound allocated more than the src size, this is an implementation detail.
		// increase the buffer size to always have enough space for the raw data if compression fails.
		bound = len(d)
	}
	mbytesComp := make([]byte, len(tbytes)+bound)
	copy(mbytesComp, tbytes)
	comp, err := zstd.CompressLevel(mbytesComp[len(tbytes):], d, zstdCompressionLevel)
	if err != nil {
		// fallback and reuse non-compressed original data
		logMsg := fmt.Sprintf("failed to compress into buffer of len %d: %v", len(d), err)
		copied := copy(mbytesComp[len(tbytes):], d)
		return mbytesComp[:len(tbytes)+copied], logMsg
	}
	mbytesComp = mbytesComp[:len(tbytes)+len(comp)]
	return mbytesComp, ""
}

// MaxDecompressedMessageSize defines a maximum decompressed data size
// to prevent zip bombs. This depends on MaxTxnBytesPerBlock consensus parameter
// and should be larger.
const MaxDecompressedMessageSize = 20 * 1024 * 1024 // some large enough value

// wsPeerMsgDataConverter performs optional incoming messages conversion.
// At the moment it only supports zstd decompression for payload proposal
type wsPeerMsgDataConverter struct {
	log    logging.Logger
	origin string

	// actual converter(s)
	ppdec zstdProposalDecompressor
}

type zstdProposalDecompressor struct {
	active bool
}

func (dec zstdProposalDecompressor) enabled() bool {
	return dec.active
}

func (dec zstdProposalDecompressor) accept(data []byte) bool {
	return len(data) > 4 && bytes.Equal(data[:4], zstdCompressionMagic[:])
}

func (dec zstdProposalDecompressor) convert(data []byte) ([]byte, error) {
	r := zstd.NewReader(bytes.NewReader(data))
	defer r.Close()
	b := make([]byte, 0, 3*len(data))
	for {
		if len(b) == cap(b) {
			// grow capacity, retain length
			b = append(b, 0)[:len(b)]
		}
		n, err := r.Read(b[len(b):cap(b)])
		b = b[:len(b)+n]
		if err != nil {
			if err == io.EOF {
				return b, nil
			}
			return nil, err
		}
		if len(b) > MaxDecompressedMessageSize {
			return nil, fmt.Errorf("proposal data is too large: %d", len(b))
		}
	}
}

func (c *wsPeerMsgDataConverter) convert(tag protocol.Tag, data []byte) ([]byte, error) {
	if tag == protocol.ProposalPayloadTag {
		if c.ppdec.enabled() {
			// sender might support compressed payload but fail to compress for whatever reason,
			// in this case it sends non-compressed payload - the receiver decompress only if it is compressed.
			if c.ppdec.accept(data) {
				res, err := c.ppdec.convert(data)
				if err != nil {
					return nil, fmt.Errorf("peer %s: %w", c.origin, err)
				}
				return res, nil
			}
			c.log.Warnf("peer %s supported zstd but sent non-compressed data", c.origin)
		}
	}
	return data, nil
}

func makeWsPeerMsgDataConverter(wp *wsPeer) *wsPeerMsgDataConverter {
	c := wsPeerMsgDataConverter{
		log:    wp.log,
		origin: wp.originAddress,
	}

	if wp.pfProposalCompressionSupported() {
		c.ppdec = zstdProposalDecompressor{
			active: true,
		}
	}

	return &c
}