EIPs Explained

EIP-706 · Final · Networking · 2017-09-07

EIP-706: DEVp2p snappy compression

Added Snappy compression to every DEVp2p message after the handshake (protocol version 5), cutting sync bandwidth by roughly 60–80% for all sub-protocols at once.

What problem does it solve?

Ethereum nodes talk to each other over DEVp2p, and sub-protocols such as eth and les run on top of it. In 2017 none of that traffic was compressed, even though blocks and transactions compress very well. The spec measured a Geth fast sync of mainnet (to block 4,248,000) and found it downloaded about 33.6 GB. With compression, the same sync needed about 13.5 GB.

Adding compression at the DEVp2p layer helps every sub-protocol at once, so each one does not have to design its own scheme.

How it works

  • Nodes advertise DEVp2p version 5 instead of 4 in the handshake. If the peer only speaks version 4, nothing changes for that connection.
  • With a v5 peer, each outgoing message {Code, Size, Payload} has its payload compressed with Snappy just before encryption, and Size is updated to the compressed length.
  • Incoming messages are decrypted first, then decompressed, and Size is set back to the original length.
  • The handshake itself is never compressed, because it is how peers agree on the version.
  • Snappy's framing format is not used. DEVp2p is already message-based, so raw Snappy blocks are enough.

DoS protection: a small compressed message could expand to something huge. Snappy stores the decompressed length at the start of the stream, so a node can check it before decompressing. The spec sets the limit at 16 MB, the same as the existing 24-bit DEVp2p message size limit, and nodes discard anything larger.

Sample code

Compress and decompress a payload with the same rules (Go, github.com/golang/snappy):

package devp2p

import (
	"errors"

	"github.com/golang/snappy"
)

const maxUncompressed = 16 * 1024 * 1024 // 16 MB, per EIP-706

// Before encryption, for v5 peers.
func compress(payload []byte) []byte {
	return snappy.Encode(nil, payload) // raw block format, no framing
}

// After decryption, for v5 peers.
func decompress(payload []byte) ([]byte, error) {
	n, err := snappy.DecodedLen(payload) // read the length header only
	if err != nil {
		return nil, err
	}
	if n > maxUncompressed {
		return nil, errors.New("message too large")
	}
	return snappy.Decode(nil, payload)
}

Things to know

  • This proposal is Final, and DEVp2p v5 with Snappy is the norm between execution clients.
  • Snappy can encode the same input in more than one valid way, so implementations may produce different bytes. Any compliant decoder must still read them all.
  • This is a transport-layer change. It does not affect consensus, block contents or anything a smart contract can see.
  • Always check the decoded length before you decompress. Skipping the 16 MB check reopens the memory-exhaustion attack the spec warns about.

#networking #devp2p #compression #snappy #p2p

Details

Authors
Péter Szilágyi <[email protected]>
Status
Final
Type
Standards Track / Networking
Created
2017-09-07

Machine-readable: Markdown · JSON