-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Snappy frame encode big payloads as chunks as per the standard (#3912)
* Snappy frame encode big payloads as chunks as per standard * cleanup comments * moving the chunking upstream * upgrade to the new snappy steam version supporting stream chunking * switch to async version to match the chunks * fixing the uncompress test * update the package fix * shift to sync compress version as it is almost 2x faster * update comment * lint * flip async and sync chunks
- Loading branch information
Showing
11 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...es/lodestar/test/unit-mainnet/network/reqresp/encodingStrategies/sszSnappy/decode.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import chai, {expect} from "chai"; | ||
import chaiAsPromised from "chai-as-promised"; | ||
import varint from "varint"; | ||
import {BufferedSource} from "../../../../../../src/network/reqresp/utils"; | ||
import {readSszSnappyPayload} from "../../../../../../src/network/reqresp/encodingStrategies/sszSnappy"; | ||
import {isEqualSszType} from "../../../../../utils/ssz"; | ||
import {arrToSource} from "../../../../../../test/unit/network/reqresp/utils"; | ||
import {goerliShadowForkBlock13249} from "./testData"; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
describe("network / reqresp / sszSnappy / decode", () => { | ||
describe("Test data vectors (generated in a previous version)", () => { | ||
const testCases = [goerliShadowForkBlock13249]; | ||
|
||
for (const {id, type, bytes, streamedBody, body} of testCases) { | ||
const deserializedBody = body ?? type.deserialize(Buffer.from(bytes)); | ||
const streamedBytes = Buffer.concat([Buffer.from(varint.encode(bytes.length)), streamedBody]); | ||
|
||
it(id, async () => { | ||
const bufferedSource = new BufferedSource(arrToSource([streamedBytes])); | ||
const bodyResult = await readSszSnappyPayload(bufferedSource, type); | ||
expect(isEqualSszType(type, bodyResult, deserializedBody)).to.equal(true, "Wrong decoded body"); | ||
}); | ||
} | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
...es/lodestar/test/unit-mainnet/network/reqresp/encodingStrategies/sszSnappy/encode.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import all from "it-all"; | ||
import pipe from "it-pipe"; | ||
import {expect} from "chai"; | ||
import varint from "varint"; | ||
|
||
import {allForks, ssz} from "@chainsafe/lodestar-types"; | ||
|
||
import {reqRespBlockResponseSerializer} from "../../../../../../src/network/reqresp/types"; | ||
import {writeSszSnappyPayload} from "../../../../../../src/network/reqresp/encodingStrategies/sszSnappy"; | ||
import {goerliShadowForkBlock13249} from "./testData"; | ||
import {RequestOrOutgoingResponseBody} from "../../../../../../src/network/reqresp/types"; | ||
|
||
describe("network / reqresp / sszSnappy / encode", () => { | ||
describe("Test data vectors (generated in a previous version)", () => { | ||
const testCases = [goerliShadowForkBlock13249]; | ||
|
||
for (const testCase of testCases) { | ||
const {id, type, bytes, streamedBody, body} = testCase; | ||
const deserializedBody = body ?? type.deserialize(Buffer.from(bytes)); | ||
const reqrespBody = | ||
body ?? | ||
(type === ssz.bellatrix.SignedBeaconBlock | ||
? {slot: (deserializedBody as allForks.SignedBeaconBlock).message.slot, bytes} | ||
: deserializedBody); | ||
|
||
it(id, async () => { | ||
const encodedChunks = await pipe( | ||
writeSszSnappyPayload(reqrespBody as RequestOrOutgoingResponseBody, reqRespBlockResponseSerializer), | ||
all | ||
); | ||
const encodedStream = Buffer.concat(encodedChunks); | ||
const expectedStreamed = Buffer.concat([Buffer.from(varint.encode(bytes.length)), streamedBody]); | ||
expect(encodedStream).to.be.deep.equal(expectedStreamed); | ||
}); | ||
} | ||
}); | ||
}); |
Binary file added
BIN
+136 KB
...t/network/reqresp/encodingStrategies/sszSnappy/goerliShadowForkBlock.13249/serialized.ssz
Binary file not shown.
Binary file added
BIN
+51.1 KB
.../network/reqresp/encodingStrategies/sszSnappy/goerliShadowForkBlock.13249/streamed.snappy
Binary file not shown.
25 changes: 25 additions & 0 deletions
25
packages/lodestar/test/unit-mainnet/network/reqresp/encodingStrategies/sszSnappy/testData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
import {bellatrix, ssz} from "@chainsafe/lodestar-types"; | ||
import {RequestOrIncomingResponseBody, RequestOrResponseType} from "../../../../../../src/network/reqresp/types"; | ||
|
||
export interface ISszSnappyTestBlockData<T extends RequestOrIncomingResponseBody> { | ||
id: string; | ||
type: RequestOrResponseType; | ||
bytes: Buffer; | ||
streamedBody: Buffer; | ||
body?: T; | ||
} | ||
|
||
/** | ||
* A real big bellatrix block from goerli-shadow-fork-2 devnet, which is expected to be | ||
* encoded in multiple chunks. | ||
*/ | ||
|
||
export const goerliShadowForkBlock13249: ISszSnappyTestBlockData<bellatrix.SignedBeaconBlock> = { | ||
id: "goerli-shadow-fork-block-13249", | ||
type: ssz.bellatrix.SignedBeaconBlock, | ||
bytes: fs.readFileSync(path.join(__dirname, "/goerliShadowForkBlock.13249/serialized.ssz")), | ||
streamedBody: fs.readFileSync(path.join(__dirname, "/goerliShadowForkBlock.13249/streamed.snappy")), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters