Skip to content

Commit

Permalink
Update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed Apr 29, 2024
1 parent 0096c9a commit 3624d7f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/beacon-node/src/chain/validation/attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ async function validateGossipAttestationNoSignatureCheck(
// > TODO: Do this check **before** getting the target state but don't recompute zipIndexes
const aggregationBits = attestationOrCache.attestation
? attestationOrCache.attestation.aggregationBits
: getAggregationBitsFromAttestationSerialized(attestationOrCache.serializedData);
: getAggregationBitsFromAttestationSerialized(fork, attestationOrCache.serializedData);
if (aggregationBits === null) {
throw new AttestationError(GossipAction.REJECT, {
code: AttestationErrorCode.INVALID_SERIALIZED_BYTES,
Expand Down Expand Up @@ -414,7 +414,7 @@ async function validateGossipAttestationNoSignatureCheck(
let attDataRootHex: RootHex;
const signature = attestationOrCache.attestation
? attestationOrCache.attestation.signature
: getSignatureFromAttestationSerialized(attestationOrCache.serializedData);
: getSignatureFromAttestationSerialized(fork, attestationOrCache.serializedData);
if (signature === null) {
throw new AttestationError(GossipAction.REJECT, {
code: AttestationErrorCode.INVALID_SERIALIZED_BYTES,
Expand Down
14 changes: 10 additions & 4 deletions packages/beacon-node/src/util/sszBytes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import {BitArray, deserializeUint8ArrayBitListFromBytes} from "@chainsafe/ssz";
import {BLSSignature, RootHex, Slot} from "@lodestar/types";
import {toHex} from "@lodestar/utils";
import {BYTES_PER_FIELD_ELEMENT, FIELD_ELEMENTS_PER_BLOB, ForkName, ForkSeq} from "@lodestar/params";
import {
BYTES_PER_FIELD_ELEMENT,
FIELD_ELEMENTS_PER_BLOB,
ForkName,
ForkSeq,
MAX_COMMITTEES_PER_SLOT,
} from "@lodestar/params";

export type BlockRootHex = RootHex;
export type AttDataBase64 = string;

// class Attestation(Container):
// aggregation_bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE] - offset 4
// aggregation_bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE] (BitList[MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT]) - offset 4
// data: AttestationData - target data - 128
// committee_bits: BitVector[MAX_COMMITTEES_PER_SLOT] - Electra only: 8
// committee_bits: BitVector[MAX_COMMITTEES_PER_SLOT] - Electra only: 8 (mainnet)
// signature: BLSSignature - 96
//
// class AttestationData(Container): 128 bytes fixed size
Expand All @@ -24,7 +30,7 @@ const ATTESTATION_BEACON_BLOCK_ROOT_OFFSET = VARIABLE_FIELD_OFFSET + 8 + 8;
const ROOT_SIZE = 32;
const SLOT_SIZE = 8;
const ATTESTATION_DATA_SIZE = 128;
const COMMITTEE_BITS_SIZE = 8;
const COMMITTEE_BITS_SIZE = Math.max(Math.floor(MAX_COMMITTEES_PER_SLOT / 8), 1);
const SIGNATURE_SIZE = 96;

/**
Expand Down
34 changes: 25 additions & 9 deletions packages/beacon-node/test/unit/util/sszBytes.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {describe, it, expect} from "vitest";
import {deneb, Epoch, phase0, RootHex, Slot, ssz} from "@lodestar/types";
import {allForks, deneb, Epoch, phase0, RootHex, Slot, ssz} from "@lodestar/types";
import {fromHex, toHex} from "@lodestar/utils";
import {ForkName} from "@lodestar/params";
import {
getAttDataBase64FromAttestationSerialized,
getAttDataBase64FromSignedAggregateAndProofSerialized,
Expand All @@ -15,26 +16,38 @@ import {
} from "../../../src/util/sszBytes.js";

describe("attestation SSZ serialized picking", () => {
const testCases: phase0.Attestation[] = [
const testCases: allForks.Attestation[] = [
ssz.phase0.Attestation.defaultValue(),
attestationFromValues(
4_000_000,
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
200_00,
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffff"
),
ssz.electra.Attestation.defaultValue(),
];

for (const [i, attestation] of testCases.entries()) {
it(`attestation ${i}`, () => {
const bytes = ssz.phase0.Attestation.serialize(attestation);
const isElectra = "committeeBits" in attestation;
const bytes = isElectra
? ssz.electra.Attestation.serialize(attestation)
: ssz.phase0.Attestation.serialize(attestation);

expect(getSlotFromAttestationSerialized(bytes)).toBe(attestation.data.slot);
expect(getBlockRootFromAttestationSerialized(bytes)).toBe(toHex(attestation.data.beaconBlockRoot));
expect(getAggregationBitsFromAttestationSerialized(bytes)?.toBoolArray()).toEqual(
attestation.aggregationBits.toBoolArray()
);
expect(getSignatureFromAttestationSerialized(bytes)).toEqual(attestation.signature);

if (isElectra) {
expect(getAggregationBitsFromAttestationSerialized(ForkName.electra, bytes)?.toBoolArray()).toEqual(
attestation.aggregationBits.toBoolArray()
);
expect(getSignatureFromAttestationSerialized(ForkName.electra, bytes)).toEqual(attestation.signature);
} else {
expect(getAggregationBitsFromAttestationSerialized(ForkName.phase0, bytes)?.toBoolArray()).toEqual(
attestation.aggregationBits.toBoolArray()
);
expect(getSignatureFromAttestationSerialized(ForkName.phase0, bytes)).toEqual(attestation.signature);
}

const attDataBase64 = ssz.phase0.AttestationData.serialize(attestation.data);
expect(getAttDataBase64FromAttestationSerialized(bytes)).toBe(Buffer.from(attDataBase64).toString("base64"));
Expand Down Expand Up @@ -65,14 +78,16 @@ describe("attestation SSZ serialized picking", () => {
it("getAggregateionBitsFromAttestationSerialized - invalid data", () => {
const invalidAggregationBitsDataSizes = [0, 4, 100, 128, 227];
for (const size of invalidAggregationBitsDataSizes) {
expect(getAggregationBitsFromAttestationSerialized(Buffer.alloc(size))).toBeNull();
expect(getAggregationBitsFromAttestationSerialized(ForkName.phase0, Buffer.alloc(size))).toBeNull();
expect(getAggregationBitsFromAttestationSerialized(ForkName.electra, Buffer.alloc(size))).toBeNull();
}
});

it("getSignatureFromAttestationSerialized - invalid data", () => {
const invalidSignatureDataSizes = [0, 4, 100, 128, 227];
for (const size of invalidSignatureDataSizes) {
expect(getSignatureFromAttestationSerialized(Buffer.alloc(size))).toBeNull();
expect(getSignatureFromAttestationSerialized(ForkName.phase0, Buffer.alloc(size))).toBeNull();
expect(getSignatureFromAttestationSerialized(ForkName.electra, Buffer.alloc(size))).toBeNull();
}
});
});
Expand All @@ -86,6 +101,7 @@ describe("aggregateAndProof SSZ serialized picking", () => {
200_00,
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffffffffff"
),
ssz.electra.SignedAggregateAndProof.defaultValue(),
];

for (const [i, signedAggregateAndProof] of testCases.entries()) {
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/electra/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
MAX_COMMITTEES_PER_SLOT,
MAX_ATTESTATIONS_ELECTRA,
MAX_ATTESTER_SLASHINGS_ELECTRA,
MAX_EXECUTION_LAYER_EXITS,
} from "@lodestar/params";
import {ssz as primitiveSsz} from "../primitive/index.js";
import {ssz as phase0Ssz} from "../phase0/index.js";
Expand Down
2 changes: 2 additions & 0 deletions packages/validator/src/util/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,7 @@ function getSpecCriticalParams(localConfig: ChainConfig): Record<keyof ConfigWit
// ELECTRA
MAX_DEPOSIT_RECEIPTS_PER_PAYLOAD: electraForkRelevant,
MAX_EXECUTION_LAYER_EXITS: electraForkRelevant,
MAX_ATTESTER_SLASHINGS_ELECTRA: electraForkRelevant,
MAX_ATTESTATIONS_ELECTRA: electraForkRelevant,
};
}

0 comments on commit 3624d7f

Please sign in to comment.