Skip to content

Commit

Permalink
Review comments, fix SlotCommitmentId length, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Oct 26, 2023
1 parent b9952af commit d8b2329
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
30 changes: 5 additions & 25 deletions sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use alloc::collections::BTreeSet;
use core::ops::Deref;

use packable::{
error::{UnpackError, UnpackErrorExt},
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions sdk/src/types/block/slot/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EpochIndex> for u32 {
Expand Down
12 changes: 6 additions & 6 deletions sdk/tests/client/signing/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:#?}");
Expand Down Expand Up @@ -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,
Expand All @@ -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:#?}");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:#?}");
Expand Down
12 changes: 6 additions & 6 deletions sdk/tests/client/signing/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:#?}");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:#?}");
Expand Down Expand Up @@ -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,
Expand All @@ -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:#?}");
Expand Down
4 changes: 2 additions & 2 deletions sdk/tests/client/signing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:#?}");
Expand Down
4 changes: 2 additions & 2 deletions sdk/tests/client/signing/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:#?}");
Expand Down

0 comments on commit d8b2329

Please sign in to comment.