forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
codec.go
252 lines (220 loc) · 7.27 KB
/
codec.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"errors"
"fmt"
"math"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils/compression"
"github.com/MetalBlockchain/metalgo/utils/constants"
"github.com/MetalBlockchain/metalgo/utils/metric"
"github.com/MetalBlockchain/metalgo/utils/timer/mockable"
"github.com/MetalBlockchain/metalgo/utils/wrappers"
)
var (
errMissingField = errors.New("message missing field")
errBadOp = errors.New("input field has invalid operation")
_ Codec = &codec{}
)
type Packer interface {
Pack(
op Op,
fieldValues map[Field]interface{},
compress bool,
bypassThrottling bool,
) (OutboundMessage, error)
}
type Parser interface {
SetTime(t time.Time) // useful in UTs
// Parse reads given bytes as InboundMessage
// Overrides client specified deadline in a message to maxDeadlineDuration
Parse(bytes []byte, nodeID ids.ShortID, onFinishedHandling func()) (InboundMessage, error)
}
type Codec interface {
Packer
Parser
}
// codec defines the serialization and deserialization of network messages.
// It's safe for multiple goroutines to call Pack and Parse concurrently.
type codec struct {
// Contains []byte. Used as an optimization.
// Can be accessed by multiple goroutines concurrently.
byteSlicePool sync.Pool
clock mockable.Clock
compressTimeMetrics map[Op]metric.Averager
decompressTimeMetrics map[Op]metric.Averager
compressor compression.Compressor
maxMessageTimeout time.Duration
}
func NewCodecWithMemoryPool(namespace string, metrics prometheus.Registerer, maxMessageSize int64, maxMessageTimeout time.Duration) (Codec, error) {
c := &codec{
byteSlicePool: sync.Pool{
New: func() interface{} {
return make([]byte, 0, constants.DefaultByteSliceCap)
},
},
compressTimeMetrics: make(map[Op]metric.Averager, len(ExternalOps)),
decompressTimeMetrics: make(map[Op]metric.Averager, len(ExternalOps)),
compressor: compression.NewGzipCompressor(maxMessageSize),
maxMessageTimeout: maxMessageTimeout,
}
errs := wrappers.Errs{}
for _, op := range ExternalOps {
if !op.Compressible() {
continue
}
c.compressTimeMetrics[op] = metric.NewAveragerWithErrs(
namespace,
fmt.Sprintf("%s_compress_time", op),
fmt.Sprintf("time (in ns) to compress %s messages", op),
metrics,
&errs,
)
c.decompressTimeMetrics[op] = metric.NewAveragerWithErrs(
namespace,
fmt.Sprintf("%s_decompress_time", op),
fmt.Sprintf("time (in ns) to decompress %s messages", op),
metrics,
&errs,
)
}
return c, errs.Err
}
func (c *codec) SetTime(t time.Time) {
c.clock.Set(t)
}
// Pack attempts to pack a map of fields into a message.
// The first byte of the message is the opcode of the message.
// Uses [buffer] to hold the message's byte repr.
// [buffer]'s contents may be overwritten by this method.
// [buffer] may be nil.
// If [compress], compress the payload.
// If [bypassThrottling], mark the message to avoid outbound throttling checks.
func (c *codec) Pack(
op Op,
fieldValues map[Field]interface{},
compress bool,
bypassThrottling bool,
) (OutboundMessage, error) {
msgFields, ok := messages[op]
if !ok {
return nil, errBadOp
}
buffer := c.byteSlicePool.Get().([]byte)
p := wrappers.Packer{
MaxSize: math.MaxInt32,
Bytes: buffer[:0],
}
// Pack the op code (message type)
p.PackByte(byte(op))
// Optionally, pack whether the payload is compressed
if op.Compressible() {
p.PackBool(compress)
}
// Pack the uncompressed payload
for _, field := range msgFields {
data, ok := fieldValues[field]
if !ok {
return nil, errMissingField
}
field.Packer()(&p, data)
}
if p.Err != nil {
return nil, p.Err
}
msg := &outboundMessage{
op: op,
bytes: p.Bytes,
refs: 1,
c: c,
bypassThrottling: bypassThrottling,
}
if !compress {
return msg, nil
}
// If [compress], compress the payload (not the op code, not isCompressed).
// The slice below is guaranteed to be in-bounds because [p.Err] == nil
// implies that len(msg.bytes) >= 2
payloadBytes := msg.bytes[wrappers.BoolLen+wrappers.ByteLen:]
startTime := time.Now()
compressedPayloadBytes, err := c.compressor.Compress(payloadBytes)
if err != nil {
return nil, fmt.Errorf("couldn't compress payload of %s message: %w", op, err)
}
c.compressTimeMetrics[op].Observe(float64(time.Since(startTime)))
msg.bytesSavedCompression = len(payloadBytes) - len(compressedPayloadBytes) // may be negative
// Remove the uncompressed payload (keep just the message type and isCompressed)
msg.bytes = msg.bytes[:wrappers.BoolLen+wrappers.ByteLen]
// Attach the compressed payload
msg.bytes = append(msg.bytes, compressedPayloadBytes...)
return msg, nil
}
// Parse attempts to convert bytes into a message.
// The first byte of the message is the opcode of the message.
// Overrides client specified deadline in a message to maxDeadlineDuration
func (c *codec) Parse(bytes []byte, nodeID ids.ShortID, onFinishedHandling func()) (InboundMessage, error) {
p := wrappers.Packer{Bytes: bytes}
// Unpack the op code (message type)
op := Op(p.UnpackByte())
msgFields, ok := messages[op]
if !ok { // Unknown message type
return nil, errBadOp
}
// See if messages of this type may be compressed
compressed := false
if op.Compressible() {
compressed = p.UnpackBool()
}
if p.Err != nil {
return nil, p.Err
}
bytesSaved := 0
// If the payload is compressed, decompress it
if compressed {
// The slice below is guaranteed to be in-bounds because [p.Err] == nil
compressedPayloadBytes := p.Bytes[wrappers.ByteLen+wrappers.BoolLen:]
startTime := time.Now()
payloadBytes, err := c.compressor.Decompress(compressedPayloadBytes)
if err != nil {
return nil, fmt.Errorf("couldn't decompress payload of %s message: %w", op, err)
}
c.decompressTimeMetrics[op].Observe(float64(time.Since(startTime)))
// Replace the compressed payload with the decompressed payload.
// Remove the compressed payload and isCompressed; keep just the message type
p.Bytes = p.Bytes[:wrappers.ByteLen]
// Rewind offset by 1 because we removed the bool flag
// since the data now is uncompressed
p.Offset -= wrappers.BoolLen
// Attach the decompressed payload.
p.Bytes = append(p.Bytes, payloadBytes...)
bytesSaved = len(payloadBytes) - len(compressedPayloadBytes)
}
// Parse each field of the payload
fieldValues := make(map[Field]interface{}, len(msgFields))
for _, field := range msgFields {
fieldValues[field] = field.Unpacker()(&p)
}
if p.Offset != len(p.Bytes) {
return nil, fmt.Errorf("expected length %d but got %d", len(p.Bytes), p.Offset)
}
var expirationTime time.Time
if deadline, hasDeadline := fieldValues[Deadline]; hasDeadline {
deadlineDuration := time.Duration(deadline.(uint64))
if deadlineDuration > c.maxMessageTimeout {
deadlineDuration = c.maxMessageTimeout
}
expirationTime = c.clock.Time().Add(deadlineDuration)
}
return &inboundMessage{
op: op,
fields: fieldValues,
bytesSavedCompression: bytesSaved,
nodeID: nodeID,
expirationTime: expirationTime,
onFinishedHandling: onFinishedHandling,
}, p.Err
}