11import { digest } from "@chainsafe/as-sha256" ;
22import { RPC } from "@chainsafe/libp2p-gossipsub/message" ;
33import { DataTransform } from "@chainsafe/libp2p-gossipsub/types" ;
4- import snappyWasm from "@chainsafe/snappy-wasm" ;
54import { Message } from "@libp2p/interface" ;
65import { ForkName } from "@lodestar/params" ;
76import { intToBytes } from "@lodestar/utils" ;
7+ import { compress , uncompress } from "snappyjs" ;
88import xxhashFactory from "xxhash-wasm" ;
99import { MESSAGE_DOMAIN_VALID_SNAPPY } from "./constants.js" ;
1010import { GossipTopicCache , getGossipSSZType } from "./topic.js" ;
@@ -15,10 +15,6 @@ const xxhash = await xxhashFactory();
1515// Use salt to prevent msgId from being mined for collisions
1616const 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
2319const 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}
0 commit comments