From d8b232993d129820cf3cb7222dac474ab595a48d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Thu, 12 Oct 2023 15:39:38 +0200 Subject: [PATCH] Review comments, fix SlotCommitmentId length, update tests --- sdk/src/types/block/output/delegation.rs | 30 ++++-------------------- sdk/src/types/block/slot/epoch.rs | 19 +++++++++++++++ sdk/tests/client/signing/account.rs | 12 +++++----- sdk/tests/client/signing/basic.rs | 12 +++++----- sdk/tests/client/signing/mod.rs | 4 ++-- sdk/tests/client/signing/nft.rs | 4 ++-- 6 files changed, 40 insertions(+), 41 deletions(-) diff --git a/sdk/src/types/block/output/delegation.rs b/sdk/src/types/block/output/delegation.rs index 76476f73b2..089bd89998 100644 --- a/sdk/src/types/block/output/delegation.rs +++ b/sdk/src/types/block/output/delegation.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 use alloc::collections::BTreeSet; -use core::ops::Deref; use packable::{ error::{UnpackError, UnpackErrorExt}, @@ -391,36 +390,17 @@ impl DelegationOutput { .map(|s| s.as_commitment().commitment_id()) .ok_or(StateTransitionError::MissingCommitmentContextInput)?; - let slot_commitment_index = crate::types::block::slot::SlotIndex::new(u32::from_le_bytes( - slot_commitment_id.deref()[32..36].try_into().unwrap(), - )); - let future_bounded_slot = slot_commitment_index + protocol_parameters.min_committable_age; + let future_bounded_slot: SlotIndex = slot_commitment_id.slot_index() + protocol_parameters.min_committable_age; let future_bounded_epoch = EpochIndex::from_slot_index( future_bounded_slot, core::iter::once((EpochIndex::new(0), protocol_parameters.slots_per_epoch_exponent)), ) .unwrap(); - // Returns the slot for which to be eligible for the committee selection of epoch n, a potential validator must - // issue at least one block between Registration Slot(n)+1. and this slot. - fn activity_window_slot(epoch: EpochIndex, protocol_parameters: &ProtocolParameters) -> SlotIndex { - epoch_start(epoch, protocol_parameters) - protocol_parameters.epoch_nearing_threshold - 1 - } - - // Returns the slot at which delegation can happen for the following epoch. - fn registration_slot(epoch: EpochIndex, protocol_parameters: &ProtocolParameters) -> SlotIndex { - epoch_start(epoch, protocol_parameters) - - protocol_parameters.epoch_nearing_threshold - - activity_window_slot(epoch, protocol_parameters) - - 1 - } - - // Calculates the start slot of the given epoch. - fn epoch_start(epoch_index: EpochIndex, protocol_parameters: &ProtocolParameters) -> SlotIndex { - SlotIndex::new(*epoch_index << protocol_parameters.slots_per_epoch_exponent) - } - - let registration_slot = registration_slot(future_bounded_epoch, protocol_parameters); + let registration_slot = future_bounded_epoch.registration_slot( + protocol_parameters.slots_per_epoch_exponent, + protocol_parameters.epoch_nearing_threshold, + ); let expected_end_epoch = if future_bounded_slot <= registration_slot { future_bounded_epoch diff --git a/sdk/src/types/block/slot/epoch.rs b/sdk/src/types/block/slot/epoch.rs index a26ccd49ce..dbbaf98e88 100644 --- a/sdk/src/types/block/slot/epoch.rs +++ b/sdk/src/types/block/slot/epoch.rs @@ -74,6 +74,25 @@ impl EpochIndex { pub fn last_slot_index(self, slots_per_epoch_exponent: u8) -> SlotIndex { SlotIndex::from_epoch_index(self + 1, slots_per_epoch_exponent) - 1 } + + /// Calculates the start slot of the given epoch. + pub fn epoch_start(&self, slots_per_epoch_exponent: u8) -> SlotIndex { + SlotIndex::new(self.0 << slots_per_epoch_exponent) + } + + /// Returns the slot for which to be eligible for the committee selection of epoch n, a potential validator must + /// issue at least one block between Registration Slot(n)+1. and this slot. + pub fn activity_window_slot(&self, slots_per_epoch_exponent: u8, epoch_nearing_threshold: u32) -> SlotIndex { + self.epoch_start(slots_per_epoch_exponent) - epoch_nearing_threshold - 1 + } + + /// Returns the slot at which delegation can happen for the following epoch. + pub fn registration_slot(&self, slots_per_epoch_exponent: u8, epoch_nearing_threshold: u32) -> SlotIndex { + self.epoch_start(slots_per_epoch_exponent) + - epoch_nearing_threshold + - self.activity_window_slot(slots_per_epoch_exponent, epoch_nearing_threshold) + - 1 + } } impl From for u32 { diff --git a/sdk/tests/client/signing/account.rs b/sdk/tests/client/signing/account.rs index 711bd280ca..6574d3d1c7 100644 --- a/sdk/tests/client/signing/account.rs +++ b/sdk/tests/client/signing/account.rs @@ -91,7 +91,7 @@ async fn sign_account_state_transition() -> Result<()> { ) .with_outputs(outputs) .add_mana_allotment(rand_mana_allotment(&protocol_parameters)) - .finish_with_params(protocol_parameters)?; + .finish_with_params(protocol_parameters.clone())?; let prepared_transaction_data = PreparedTransactionData { transaction, @@ -108,7 +108,7 @@ async fn sign_account_state_transition() -> Result<()> { validate_signed_transaction_payload_length(&tx_payload)?; - let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload)?; + let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload, protocol_parameters)?; if let Some(conflict) = conflict { panic!("{conflict:?}, with {tx_payload:#?}"); @@ -176,7 +176,7 @@ async fn sign_account_governance_transition() -> Result<()> { ) .with_outputs(outputs) .add_mana_allotment(rand_mana_allotment(&protocol_parameters)) - .finish_with_params(protocol_parameters)?; + .finish_with_params(protocol_parameters.clone())?; let prepared_transaction_data = PreparedTransactionData { transaction, @@ -193,7 +193,7 @@ async fn sign_account_governance_transition() -> Result<()> { validate_signed_transaction_payload_length(&tx_payload)?; - let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload)?; + let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload, protocol_parameters)?; if let Some(conflict) = conflict { panic!("{conflict:?}, with {tx_payload:#?}"); @@ -297,7 +297,7 @@ async fn account_reference_unlocks() -> Result<()> { ) .with_outputs(outputs) .add_mana_allotment(rand_mana_allotment(&protocol_parameters)) - .finish_with_params(protocol_parameters)?; + .finish_with_params(protocol_parameters.clone())?; let prepared_transaction_data = PreparedTransactionData { transaction, @@ -326,7 +326,7 @@ async fn account_reference_unlocks() -> Result<()> { validate_signed_transaction_payload_length(&tx_payload)?; - let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload)?; + let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload, protocol_parameters)?; if let Some(conflict) = conflict { panic!("{conflict:?}, with {tx_payload:#?}"); diff --git a/sdk/tests/client/signing/basic.rs b/sdk/tests/client/signing/basic.rs index d7390f0d30..5f04625d37 100644 --- a/sdk/tests/client/signing/basic.rs +++ b/sdk/tests/client/signing/basic.rs @@ -72,7 +72,7 @@ async fn single_ed25519_unlock() -> Result<()> { ) .with_outputs(outputs) .add_mana_allotment(rand_mana_allotment(&protocol_parameters)) - .finish_with_params(protocol_parameters)?; + .finish_with_params(protocol_parameters.clone())?; let prepared_transaction_data = PreparedTransactionData { transaction, @@ -89,7 +89,7 @@ async fn single_ed25519_unlock() -> Result<()> { validate_signed_transaction_payload_length(&tx_payload)?; - let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload)?; + let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload, protocol_parameters)?; if let Some(conflict) = conflict { panic!("{conflict:?}, with {tx_payload:#?}"); @@ -167,7 +167,7 @@ async fn ed25519_reference_unlocks() -> Result<()> { ) .with_outputs(outputs) .add_mana_allotment(rand_mana_allotment(&protocol_parameters)) - .finish_with_params(protocol_parameters)?; + .finish_with_params(protocol_parameters.clone())?; let prepared_transaction_data = PreparedTransactionData { transaction, @@ -196,7 +196,7 @@ async fn ed25519_reference_unlocks() -> Result<()> { validate_signed_transaction_payload_length(&tx_payload)?; - let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload)?; + let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload, protocol_parameters)?; if let Some(conflict) = conflict { panic!("{conflict:?}, with {tx_payload:#?}"); @@ -273,7 +273,7 @@ async fn two_signature_unlocks() -> Result<()> { ) .with_outputs(outputs) .add_mana_allotment(rand_mana_allotment(&protocol_parameters)) - .finish_with_params(protocol_parameters)?; + .finish_with_params(protocol_parameters.clone())?; let prepared_transaction_data = PreparedTransactionData { transaction, @@ -291,7 +291,7 @@ async fn two_signature_unlocks() -> Result<()> { validate_signed_transaction_payload_length(&tx_payload)?; - let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload)?; + let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload, protocol_parameters)?; if let Some(conflict) = conflict { panic!("{conflict:?}, with {tx_payload:#?}"); diff --git a/sdk/tests/client/signing/mod.rs b/sdk/tests/client/signing/mod.rs index 8ee36fc9a9..3df63fba21 100644 --- a/sdk/tests/client/signing/mod.rs +++ b/sdk/tests/client/signing/mod.rs @@ -385,7 +385,7 @@ async fn all_combined() -> Result<()> { .with_outputs(outputs) .with_creation_slot(slot_index) .add_mana_allotment(rand_mana_allotment(&protocol_parameters)) - .finish_with_params(protocol_parameters)?; + .finish_with_params(protocol_parameters.clone())?; let prepared_transaction_data = PreparedTransactionData { transaction, @@ -477,7 +477,7 @@ async fn all_combined() -> Result<()> { validate_signed_transaction_payload_length(&tx_payload)?; - let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload)?; + let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload, protocol_parameters)?; if let Some(conflict) = conflict { panic!("{conflict:?}, with {tx_payload:#?}"); diff --git a/sdk/tests/client/signing/nft.rs b/sdk/tests/client/signing/nft.rs index 0508bc5bcd..bb999d16e8 100644 --- a/sdk/tests/client/signing/nft.rs +++ b/sdk/tests/client/signing/nft.rs @@ -117,7 +117,7 @@ async fn nft_reference_unlocks() -> Result<()> { ) .with_outputs(outputs) .add_mana_allotment(rand_mana_allotment(&protocol_parameters)) - .finish_with_params(protocol_parameters)?; + .finish_with_params(protocol_parameters.clone())?; let prepared_transaction_data = PreparedTransactionData { transaction, @@ -146,7 +146,7 @@ async fn nft_reference_unlocks() -> Result<()> { validate_signed_transaction_payload_length(&tx_payload)?; - let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload)?; + let conflict = verify_semantic(&prepared_transaction_data.inputs_data, &tx_payload, protocol_parameters)?; if let Some(conflict) = conflict { panic!("{conflict:?}, with {tx_payload:#?}");