Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pluto-featureset.workspace = true
prost.workspace = true
prost-types.workspace = true
regex.workspace = true
reqwest = { workspace = true, features = ["stream"] }
serde.workspace = true
serde_json.workspace = true
base64.workspace = true
Expand All @@ -34,6 +35,7 @@ pluto-eth2util.workspace = true
pluto-ssz.workspace = true
ssz.workspace = true
tree_hash.workspace = true
url.workspace = true

[dev-dependencies]
anyhow.workspace = true
Expand Down
129 changes: 129 additions & 0 deletions crates/core/src/ssz_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use pluto_eth2api::{
spec::{altair, bellatrix, capella, deneb, electra, fulu, phase0},
v1,
versioned::{self, AttestationPayload, DataVersion, SignedAggregateAndProofPayload},
};
use pluto_ssz::{
Expand Down Expand Up @@ -41,6 +42,10 @@ pub enum SszCodecError {
/// Unknown or unsupported data version.
#[error("ssz unknown version: {0}")]
UnknownVersion(u64),
/// A fixed-size array body length is not a whole multiple of the element
/// size.
#[error("invalid buffer size")]
InvalidBufferSize,
/// Inner SSZ binary decoding failed.
#[error("ssz decode: {0}")]
Decode(String),
Expand Down Expand Up @@ -144,6 +149,35 @@ pub fn decode_signed_contribution_and_proof(
Ok(altair::SignedContributionAndProof::from_ssz_bytes(bytes)?)
}

/// SSZ-serialized byte length of a single `v1::SignedValidatorRegistration`:
/// `fee_recipient`(20) + `gas_limit`(8) + `timestamp`(8) + `pubkey`(48) +
/// `signature`(96). The register-validator endpoint accepts a bare
/// concatenation of these fixed-size objects (no length prefix), so the body
/// must be a whole multiple of this size.
const SIGNED_VALIDATOR_REGISTRATION_SSZ_SIZE: usize = 180;

/// Decodes an array of `v1::SignedValidatorRegistration` from a bare SSZ
/// concatenation. The body must be a whole multiple of
/// [`SIGNED_VALIDATOR_REGISTRATION_SSZ_SIZE`]; otherwise
/// [`SszCodecError::InvalidBufferSize`] is returned.
pub fn decode_signed_validator_registrations(
bytes: &[u8],
) -> Result<Vec<v1::SignedValidatorRegistration>, SszCodecError> {
if !bytes
.len()
.is_multiple_of(SIGNED_VALIDATOR_REGISTRATION_SSZ_SIZE)
{
return Err(SszCodecError::InvalidBufferSize);
}

let mut out = Vec::with_capacity(bytes.len() / SIGNED_VALIDATOR_REGISTRATION_SSZ_SIZE);
for chunk in bytes.chunks_exact(SIGNED_VALIDATOR_REGISTRATION_SSZ_SIZE) {
out.push(v1::SignedValidatorRegistration::from_ssz_bytes(chunk)?);
}

Ok(out)
}

// ===========================================================================
// Versioned type helpers
// ===========================================================================
Expand Down Expand Up @@ -449,6 +483,57 @@ fn encode_proposal_block(block: &versioned::SignedProposalBlock) -> Result<Vec<u
})
}

/// Decodes a bare per-fork full (non-blinded) signed proposal block body from
/// SSZ binary, selecting the variant by `version`.
///
/// Unlike [`decode_versioned_signed_proposal`], this expects the raw
/// beacon-API SSZ block body with no Charon versioned header — the format a
/// validator client posts to `/eth/v{1,2}/beacon/blocks`. The fork is taken
/// from the `Eth-Consensus-Version` request header, not from the bytes. The
/// blinded endpoint uses [`decode_signed_blinded_proposal_block_body`].
pub fn decode_signed_proposal_block_body(
version: DataVersion,
bytes: &[u8],
) -> Result<versioned::SignedProposalBlock, SszCodecError> {
decode_proposal_block(version, false, bytes)
}

/// Decodes a bare per-fork blinded signed proposal block body from SSZ binary,
/// selecting the variant by `version`.
///
/// The raw beacon-API SSZ block body posted to
/// `/eth/v{1,2}/beacon/blinded_blocks`; the fork is taken from the
/// `Eth-Consensus-Version` request header.
pub fn decode_signed_blinded_proposal_block_body(
version: DataVersion,
bytes: &[u8],
) -> Result<versioned::SignedBlindedProposalBlock, SszCodecError> {
use versioned::SignedBlindedProposalBlock;
Ok(match version {
DataVersion::Bellatrix => SignedBlindedProposalBlock::Bellatrix(
bellatrix::SignedBlindedBeaconBlock::from_ssz_bytes(bytes)?,
),
DataVersion::Capella => SignedBlindedProposalBlock::Capella(
capella::SignedBlindedBeaconBlock::from_ssz_bytes(bytes)?,
),
DataVersion::Deneb => SignedBlindedProposalBlock::Deneb(
deneb::SignedBlindedBeaconBlock::from_ssz_bytes(bytes)?,
),
DataVersion::Electra => SignedBlindedProposalBlock::Electra(
electra::SignedBlindedBeaconBlock::from_ssz_bytes(bytes)?,
),
// Fulu blinded blocks share the Electra layout.
DataVersion::Fulu => SignedBlindedProposalBlock::Fulu(
electra::SignedBlindedBeaconBlock::from_ssz_bytes(bytes)?,
),
DataVersion::Phase0 | DataVersion::Altair | DataVersion::Unknown => {
return Err(SszCodecError::UnknownVersion(
version.to_legacy_u64().unwrap_or(u64::MAX),
));
}
})
}

fn decode_proposal_block(
version: DataVersion,
blinded: bool,
Expand Down Expand Up @@ -521,6 +606,50 @@ mod tests {
}
}

fn sample_signed_registration(byte: u8) -> v1::SignedValidatorRegistration {
v1::SignedValidatorRegistration {
message: v1::ValidatorRegistration {
fee_recipient: [byte; 20],
gas_limit: 30_000_000,
timestamp: 1_700_000_000,
pubkey: [byte; 48],
},
signature: [byte; 96],
}
}

#[test]
fn roundtrip_signed_validator_registrations() {
use ssz::Encode;

let regs = vec![
sample_signed_registration(0x1A),
sample_signed_registration(0x2B),
];
let mut body = Vec::new();
for reg in &regs {
body.extend_from_slice(&reg.as_ssz_bytes());
}
// Each object is exactly 180 bytes.
assert_eq!(body.len(), 2 * SIGNED_VALIDATOR_REGISTRATION_SSZ_SIZE);

let decoded = decode_signed_validator_registrations(&body).unwrap();
assert_eq!(decoded, regs);
}

#[test]
fn decode_signed_validator_registrations_rejects_misaligned_buffer() {
let err = decode_signed_validator_registrations(&[0u8; 181]).unwrap_err();
assert!(matches!(err, SszCodecError::InvalidBufferSize));
assert_eq!(err.to_string(), "invalid buffer size");
}

#[test]
fn decode_signed_validator_registrations_empty_is_empty() {
let decoded = decode_signed_validator_registrations(&[]).unwrap();
assert!(decoded.is_empty());
}

#[test]
fn roundtrip_phase0_attestation() {
let att = phase0::Attestation {
Expand Down
Loading
Loading