Skip to content

Instantly share code, notes, and snippets.

@dapplion
Created July 8, 2023 09:18
Show Gist options
  • Save dapplion/98be2aaf9b7bb2f1252262abcbe3361d to your computer and use it in GitHub Desktop.
Save dapplion/98be2aaf9b7bb2f1252262abcbe3361d to your computer and use it in GitHub Desktop.
import {randomBytes} from "node:crypto";
import {compress} from "snappyjs";
import {phase0, ssz} from "@lodestar/types";
import {BitArray} from "@chainsafe/ssz";
// To run:
// $ ../../node_modules/.bin/ts-node --esm test/unit/network/snappy.test.ts
/* eslint-disable no-console */
const aggregate: phase0.SignedAggregateAndProof = {
message: {
aggregatorIndex: 712352,
aggregate: {
aggregationBits: BitArray.fromBoolArray(Array.from({length: 230}, () => Math.random() < 0.95)),
data: {
slot: 6174623,
index: 13,
beaconBlockRoot: randomBytes(32),
source: {epoch: 507321, root: randomBytes(32)},
target: {epoch: 507319, root: randomBytes(32)},
},
signature: randomBytes(48),
},
selectionProof: randomBytes(48),
},
signature: randomBytes(48),
};
{
const bytes = ssz.phase0.SignedAggregateAndProof.serialize(aggregate);
const bytesComp = compress(bytes);
console.log("SignedAggregateAndProof", {
uncompressed: bytes.length,
compressed: bytesComp.length,
ratio: bytesComp.length / bytes.length,
json: JSON.stringify(ssz.phase0.SignedAggregateAndProof.toJson(aggregate)),
});
}
const attestation: phase0.Attestation = {
aggregationBits: BitArray.fromSingleBit(230, 120),
data: {
slot: 6174623,
index: 13,
beaconBlockRoot: randomBytes(32),
source: {epoch: 507321, root: randomBytes(32)},
target: {epoch: 507319, root: randomBytes(32)},
},
signature: randomBytes(48),
};
{
const bytes = ssz.phase0.Attestation.serialize(attestation);
const bytesComp = compress(bytes);
console.log("Attestation", {
uncompressed: bytes.length,
compressed: bytesComp.length,
ratio: bytesComp.length / bytes.length,
json: JSON.stringify(ssz.phase0.Attestation.toJson(attestation)),
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment