Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide partial test suite for header validation #3

Merged
Merged
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
2 changes: 2 additions & 0 deletions ouroboros-praos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mockall = "0.13"
pallas-traverse = { git = "https://github.com/txpipe/pallas", rev = "4871342a8deffaceafb5e0f771e1623dfe57e25f" }
tracing-subscriber = "0.3"
criterion = "0.5.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.128"

[[bench]]
harness = false
Expand Down
3 changes: 1 addition & 2 deletions ouroboros-praos/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ fn validate_conway_block() {

let active_slots_coeff: FixedDecimal =
FixedDecimal::from(5u64) / FixedDecimal::from(100u64);
let c = (FixedDecimal::from(1u64) - active_slots_coeff).ln();
let conway_block_tag: u8 = 6;
let multi_era_header = MultiEraHeader::decode(conway_block_tag, None, test_block).unwrap();
let babbage_header = multi_era_header.as_babbage().expect("Infallible");
Expand Down Expand Up @@ -57,7 +56,7 @@ fn validate_conway_block() {
.expect_latest_opcert_sequence_number()
.returning(|_| None);

let block_validator = BlockValidator::new(babbage_header, &ledger_state, &epoch_nonce, &c);
let block_validator = BlockValidator::new(babbage_header, &ledger_state, &epoch_nonce, &active_slots_coeff);
assert_eq!(block_validator.validate().is_ok(), expected);
}
}
Expand Down
49 changes: 27 additions & 22 deletions ouroboros-praos/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@ pub struct BlockValidator<'b> {
header: &'b babbage::MintedHeader<'b>,
ledger_state: &'b dyn LedgerState,
epoch_nonce: &'b Hash<32>,
// c is the ln(1-active_slots_coeff). Usually ln(1-0.05)
c: &'b FixedDecimal,
active_slots_coeff: &'b FixedDecimal,
}

impl<'b> BlockValidator<'b> {
pub fn new(
header: &'b babbage::MintedHeader,
ledger_state: &'b dyn LedgerState,
epoch_nonce: &'b Hash<32>,
c: &'b FixedDecimal,
active_slots_coeff: &'b FixedDecimal,
) -> Self {
Self {
header,
ledger_state,
epoch_nonce,
c,
active_slots_coeff,
}
}

Expand Down Expand Up @@ -103,7 +102,7 @@ impl<'b> BlockValidator<'b> {
&block_vrf_proof,
)
}),
Box::new(|| self.validate_operational_certificate(issuer_vkey.as_slice())),
Box::new(|| self.validate_operational_certificate(issuer_vkey.as_slice(), &pool_id)),
Box::new(|| self.validate_kes_signature(absolute_slot, kes_signature)),
];

Expand Down Expand Up @@ -138,10 +137,10 @@ impl<'b> BlockValidator<'b> {
.operational_cert_kes_period;

if opcert_kes_period > slot_kes_period {
return Err(ValidationError::KesVerificationError(
"Operational Certificate KES period is greater than the block slot KES period!"
.to_string(),
));
return Err(ValidationError::OpCertKesPeriodTooLarge {
opcert_kes_period,
slot_kes_period,
});
}
if slot_kes_period >= opcert_kes_period + self.ledger_state.max_kes_evolutions() {
return Err(ValidationError::KesVerificationError(
Expand All @@ -168,7 +167,7 @@ impl<'b> BlockValidator<'b> {
})
}

fn validate_operational_certificate(&self, issuer_vkey: &[u8]) -> Result<(), ValidationError> {
fn validate_operational_certificate(&self, issuer_vkey: &[u8], pool_id: &PoolId) -> Result<(), ValidationError> {
// Verify the Operational Certificate signature
let opcert_signature = Signature::try_from(
self.header
Expand All @@ -191,12 +190,12 @@ impl<'b> BlockValidator<'b> {

// Check the sequence number of the operational certificate. It should either be the same
// as the latest known sequence number for the issuer_vkey or one greater.
match self.ledger_state.latest_opcert_sequence_number(issuer_vkey) {
match self.ledger_state.latest_opcert_sequence_number(pool_id) {
Some(latest_opcert_sequence_number) => {
if (opcert_sequence_number - latest_opcert_sequence_number) > 1 {
return Err(ValidationError::InvalidOpcertSequenceNumber("Operational Certificate sequence number is too far ahead of the latest known sequence number!".to_string()));
} else if opcert_sequence_number < latest_opcert_sequence_number {
if opcert_sequence_number < latest_opcert_sequence_number {
return Err(ValidationError::InvalidOpcertSequenceNumber("Operational Certificate sequence number is less than the latest known sequence number!".to_string()));
} else if (opcert_sequence_number - latest_opcert_sequence_number) > 1 {
return Err(ValidationError::InvalidOpcertSequenceNumber("Operational Certificate sequence number is too far ahead of the latest known sequence number!".to_string()));
}
trace!("Operational Certificate sequence number is ok.")
}
Expand Down Expand Up @@ -289,15 +288,22 @@ impl<'b> BlockValidator<'b> {
absolute_slot: u64,
leader_vrf_output: &[u8],
) -> Result<(), ValidationError> {
// special case for testing purposes
if self.active_slots_coeff == &FixedDecimal::from(1u64) {
return Ok(());
}

let certified_leader_vrf: FixedDecimal = leader_vrf_output.into();
let denominator = CERTIFIED_NATURAL_MAX.deref() - &certified_leader_vrf;
let recip_q = CERTIFIED_NATURAL_MAX.deref() / &denominator;
let x = -(sigma * self.c);
let c = (FixedDecimal::from(1u64) - self.active_slots_coeff.clone()).ln();
let x = -(sigma * &c);

trace!("leader_vrf_output: {}", hex::encode(leader_vrf_output));
trace!("certified_leader_vrf: {}", certified_leader_vrf);
trace!("denominator: {}", denominator);
trace!("recip_q: {}", recip_q);
trace!("c: {}", self.c);
trace!("active_slots_coeff: {}", self.active_slots_coeff);
trace!("x: {}", x);

let ordering = x.exp_cmp(1000, 3, &recip_q);
Expand Down Expand Up @@ -333,10 +339,10 @@ impl<'b> BlockValidator<'b> {
trace!("block vrf_vkey_hash: {}", hex::encode(vrf_vkey_hash));
let ledger_vrf_vkey_hash = self.ledger_state.vrf_vkey_hash(pool_id)?;
if vrf_vkey_hash != ledger_vrf_vkey_hash {
return Err(ValidationError::InvalidVrfKeyForPool(
hex::encode(ledger_vrf_vkey_hash),
hex::encode(vrf_vkey),
));
return Err(ValidationError::InvalidVrfKeyForPool {
key_hash_from_ledger: hex::encode(ledger_vrf_vkey_hash),
key_hash_from_block: hex::encode(vrf_vkey),
});
}
Ok(())
}
Expand Down Expand Up @@ -409,7 +415,6 @@ mod tests {

let active_slots_coeff: FixedDecimal =
FixedDecimal::from(5u64) / FixedDecimal::from(100u64);
let c = (FixedDecimal::from(1u64) - active_slots_coeff).ln();
let conway_block_tag: u8 = 6;
let multi_era_header =
MultiEraHeader::decode(conway_block_tag, None, test_block).unwrap();
Expand Down Expand Up @@ -441,7 +446,7 @@ mod tests {
.returning(|_| None);

let block_validator =
BlockValidator::new(babbage_header, &ledger_state, &epoch_nonce, &c);
BlockValidator::new(babbage_header, &ledger_state, &epoch_nonce, &active_slots_coeff);
assert_eq!(block_validator.validate().is_ok(), expected);
}
}
Expand Down
1 change: 1 addition & 0 deletions ouroboros-praos/tests/data/test-vector.json

Large diffs are not rendered by default.

Loading