Skip to content

Commit 92d77e3

Browse files
authored
feat: revert "feat: use snappy-wasm (#6483)" (#7451)
This reverts commit 0a8e6e1.
1 parent 277437e commit 92d77e3

File tree

4 files changed

+8
-213
lines changed

4 files changed

+8
-213
lines changed

packages/beacon-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
"multiformats": "^11.0.1",
147147
"prom-client": "^15.1.0",
148148
"qs": "^6.11.1",
149-
"@chainsafe/snappy-wasm": "^0.5.0",
149+
"snappyjs": "^0.7.0",
150150
"strict-event-emitter-types": "^2.0.0",
151151
"systeminformation": "^5.22.9",
152152
"uint8arraylist": "^2.4.7",

packages/beacon-node/src/network/gossip/encoding.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {digest} from "@chainsafe/as-sha256";
22
import {RPC} from "@chainsafe/libp2p-gossipsub/message";
33
import {DataTransform} from "@chainsafe/libp2p-gossipsub/types";
4-
import snappyWasm from "@chainsafe/snappy-wasm";
54
import {Message} from "@libp2p/interface";
65
import {ForkName} from "@lodestar/params";
76
import {intToBytes} from "@lodestar/utils";
7+
import {compress, uncompress} from "snappyjs";
88
import xxhashFactory from "xxhash-wasm";
99
import {MESSAGE_DOMAIN_VALID_SNAPPY} from "./constants.js";
1010
import {GossipTopicCache, getGossipSSZType} from "./topic.js";
@@ -15,10 +15,6 @@ const xxhash = await xxhashFactory();
1515
// Use salt to prevent msgId from being mined for collisions
1616
const h64Seed = BigInt(Math.floor(Math.random() * 1e9));
1717

18-
// create singleton snappy encoder + decoder
19-
const encoder = new snappyWasm.Encoder();
20-
const decoder = new snappyWasm.Decoder();
21-
2218
// Shared buffer to convert msgId to string
2319
const sharedMsgIdBuf = Buffer.alloc(20);
2420

@@ -83,12 +79,11 @@ export class DataTransformSnappy implements DataTransform {
8379
* - `outboundTransform()`: compress snappy payload
8480
*/
8581
inboundTransform(topicStr: string, data: Uint8Array): Uint8Array {
86-
// check uncompressed data length before we actually decompress
87-
const uncompressedDataLength = snappyWasm.decompress_len(data);
88-
if (uncompressedDataLength > this.maxSizePerMessage) {
89-
throw Error(`ssz_snappy decoded data length ${uncompressedDataLength} > ${this.maxSizePerMessage}`);
90-
}
82+
const uncompressedData = uncompress(data, this.maxSizePerMessage);
9183

84+
// check uncompressed data length before we extract beacon block root, slot or
85+
// attestation data at later steps
86+
const uncompressedDataLength = uncompressedData.length;
9287
const topic = this.gossipTopicCache.getTopic(topicStr);
9388
const sszType = getGossipSSZType(topic);
9489

@@ -99,24 +94,18 @@ export class DataTransformSnappy implements DataTransform {
9994
throw Error(`ssz_snappy decoded data length ${uncompressedDataLength} > ${sszType.maxSize}`);
10095
}
10196

102-
// Only after saniy length checks, we can decompress the data
103-
const uncompressedData = Buffer.allocUnsafe(uncompressedDataLength);
104-
decoder.decompress_into(data, uncompressedData);
10597
return uncompressedData;
10698
}
10799

108100
/**
109101
* Takes the data to be published (a topic and associated data) transforms the data. The
110102
* transformed data will then be used to create a `RawGossipsubMessage` to be sent to peers.
111103
*/
112-
// No need to parse topic, everything is snappy compressed
113104
outboundTransform(_topicStr: string, data: Uint8Array): Uint8Array {
114105
if (data.length > this.maxSizePerMessage) {
115106
throw Error(`ssz_snappy encoded data length ${data.length} > ${this.maxSizePerMessage}`);
116107
}
117-
118-
const compressedData = Buffer.allocUnsafe(snappyWasm.max_compress_len(data.length));
119-
const compressedLen = encoder.compress_into(data, compressedData);
120-
return compressedData.subarray(0, compressedLen);
108+
// No need to parse topic, everything is snappy compressed
109+
return compress(data);
121110
}
122111
}

packages/beacon-node/test/perf/network/gossip/snappy.test.ts

Lines changed: 0 additions & 189 deletions
This file was deleted.

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,11 +750,6 @@
750750
"@chainsafe/pubkey-index-map-linux-x64-gnu" "2.0.0"
751751
"@chainsafe/pubkey-index-map-win32-x64-msvc" "2.0.0"
752752

753-
"@chainsafe/snappy-wasm@^0.5.0":
754-
version "0.5.0"
755-
resolved "https://registry.yarnpkg.com/@chainsafe/snappy-wasm/-/snappy-wasm-0.5.0.tgz#067e534341ef746706e2dbf255bd7604c849be73"
756-
integrity sha512-ydXvhr9p+JjvzSSEyi6XExq8pHugFnrk70mk17T6mhDsklPvaXc+8K90G7TJF+u51lxI/fpv7MahrA5ayjFcSA==
757-
758753
"@chainsafe/ssz@^0.11.1":
759754
version "0.11.1"
760755
resolved "https://registry.yarnpkg.com/@chainsafe/ssz/-/ssz-0.11.1.tgz#d4aec883af2ec5196ae67b96242c467da20b2476"

0 commit comments

Comments
 (0)