Skip to content

Commit

Permalink
restore compression headers
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy committed Jul 29, 2024
1 parent bb4eb38 commit 3ce1bef
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
12 changes: 8 additions & 4 deletions network/wsNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ func (wn *WebsocketNetwork) ServeHTTP(response http.ResponseWriter, request *htt
responseHeader.Set(ProtocolVersionHeader, matchingVersion)
responseHeader.Set(GenesisHeader, wn.GenesisID)
// set the features we support, for example
// responseHeader.Set(PeerFeaturesHeader, "ppzstd")
responseHeader.Set(PeerFeaturesHeader, PeerFeatureProposalCompression)
var challenge string
if wn.prioScheme != nil {
challenge = wn.prioScheme.NewPrioChallenge()
Expand Down Expand Up @@ -1387,7 +1387,7 @@ func (wn *WebsocketNetwork) getPeersChangeCounter() int32 {
}

// preparePeerData prepares batches of data for sending.
// It performs optional zstd compression for proposal massages
// It performs zstd compression for proposal massages if they this is a prio request and has proposal.
func (wn *msgBroadcaster) preparePeerData(request broadcastRequest, prio bool) ([][]byte, []crypto.Digest) {
// determine if there is a payload proposal and peers supporting compressed payloads
shouldCompress := false
Expand Down Expand Up @@ -1954,6 +1954,10 @@ const UserAgentHeader = "User-Agent"
// PeerFeaturesHeader is the HTTP header listing features
const PeerFeaturesHeader = "X-Algorand-Peer-Features"

// PeerFeatureProposalCompression is a value for PeerFeaturesHeader indicating peer
// supports proposal payload compression with zstd
const PeerFeatureProposalCompression = "ppzstd"

var websocketsScheme = map[string]string{"http": "ws", "https": "wss"}

var errBadAddr = errors.New("bad address")
Expand Down Expand Up @@ -2075,8 +2079,8 @@ func (wn *WebsocketNetwork) tryConnect(netAddr, gossipAddr string) {

// for backward compatibility, include the ProtocolVersion header as well.
requestHeader.Set(ProtocolVersionHeader, wn.protocolVersion)
// set the features header (comma-separated list), for example
requestHeader.Set(PeerFeaturesHeader, "ppzstd")
// set the features header (comma-separated list)
requestHeader.Set(PeerFeaturesHeader, PeerFeatureProposalCompression)
SetUserAgentHeader(requestHeader)
myInstanceName := wn.log.GetInstanceName()
requestHeader.Set(InstanceNameHeader, myInstanceName)
Expand Down
1 change: 1 addition & 0 deletions network/wsNetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ func TestWebsocketProposalPayloadCompression(t *testing.T) {

// old node + new node
{[]string{"2.1"}, "2.1", []string{"2.2", "2.1"}, "2.2"},
{[]string{"2.2", "2.1"}, "2.1", []string{"2.2"}, "2.2"},

// combinations
{[]string{"2.2", "2.1"}, "2.1", []string{"2.2", "2.1"}, "2.1"},
Expand Down
13 changes: 8 additions & 5 deletions network/wsPeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,10 @@ func (wp *wsPeer) OnClose(f func()) {
//msgp:ignore peerFeatureFlag
type peerFeatureFlag int

const (
pfCompressedProposal peerFeatureFlag = 1 << iota
)

// versionPeerFeatures defines protocol version when peer features were introduced
const versionPeerFeatures = "2.2"

Expand Down Expand Up @@ -1214,11 +1218,10 @@ func decodePeerFeatures(version string, announcedFeatures string) peerFeatureFla
var features peerFeatureFlag
parts := strings.Split(announcedFeatures, ",")
for _, part := range parts {
// check features here, for example
_ = strings.TrimSpace(part)
// if part == "ppzstd" {
// features |= pfCompressedProposal
// }
part = strings.TrimSpace(part)
if part == PeerFeatureProposalCompression {
features |= pfCompressedProposal
}
}
return features
}
6 changes: 5 additions & 1 deletion network/wsPeer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ func TestVersionToFeature(t *testing.T) {
{"1.2.3", "", peerFeatureFlag(0)},
{"a.b", "", peerFeatureFlag(0)},
{"2.1", "", peerFeatureFlag(0)},
{"2.1", "", peerFeatureFlag(0)},
{"2.1", PeerFeatureProposalCompression, peerFeatureFlag(0)},
{"2.2", "", peerFeatureFlag(0)},
{"2.2", "test", peerFeatureFlag(0)},
{"2.2", strings.Join([]string{"a", "b"}, ","), peerFeatureFlag(0)},
{"2.2", PeerFeatureProposalCompression, pfCompressedProposal},
{"2.2", strings.Join([]string{PeerFeatureProposalCompression, "test"}, ","), pfCompressedProposal},
{"2.2", strings.Join([]string{PeerFeatureProposalCompression, "test"}, ", "), pfCompressedProposal},
{"2.3", PeerFeatureProposalCompression, pfCompressedProposal},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
Expand Down

0 comments on commit 3ce1bef

Please sign in to comment.