Skip to content

Commit e224461

Browse files
authored
fix: address post-merge code review comments for PR #1183 (#1189)
1 parent 7f52f01 commit e224461

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

crates/contract/tests/inprocess/expired_attestation.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use near_sdk::{
1919
test_utils::VMContextBuilder, testing_env, AccountId, CurveType, NearToken, PublicKey,
2020
VMContext,
2121
};
22-
use std::{collections::HashSet, time::Duration};
22+
use std::time::Duration;
2323

2424
const SECOND: Duration = Duration::from_secs(1);
2525
const NANOS_IN_SECOND: u64 = SECOND.as_nanos() as u64;
@@ -65,7 +65,6 @@ impl TestSetup {
6565
.unwrap();
6666
}
6767

68-
/// Try to submit attestation and return Result for testing failures
6968
fn try_submit_attestation_for_node(
7069
&mut self,
7170
node_id: &NodeId,
@@ -94,7 +93,6 @@ impl TestSetup {
9493
}
9594
}
9695

97-
/// Get NodeIds created from the existing participants
9896
fn get_participant_node_ids(&self) -> Vec<NodeId> {
9997
self.participants_list
10098
.iter()
@@ -105,8 +103,7 @@ impl TestSetup {
105103
.collect()
106104
}
107105

108-
/// Helper to create attestation with hash constraints
109-
fn create_hash_attestation(hash: [u8; 32]) -> Attestation {
106+
fn create_attestation_with_hash_constraint(hash: [u8; 32]) -> Attestation {
110107
Attestation::Mock(MockAttestation::WithConstraints {
111108
mpc_docker_image_hash: Some(hash),
112109
launcher_docker_compose_hash: None,
@@ -535,23 +532,22 @@ fn nodes_can_start_with_old_valid_hashes_during_grace_period() {
535532
let test_time_1 = deployment_times[2] + 3 * NANOS_IN_SECOND;
536533
set_system_time(test_time_1);
537534

538-
let allowed_set: HashSet<_> = test_setup
535+
let allowed_hashes: [_; 3] = test_setup
539536
.contract
540537
.allowed_code_hashes()
541-
.iter()
542-
.map(|h| **h)
543-
.collect();
544-
let expected_set: HashSet<_> = hashes.into_iter().collect();
538+
.try_into()
539+
.unwrap();
540+
let expected_hashes = [hash_v1.into(), hash_v2.into(), hash_v3.into()];
545541

546-
assert_eq!(allowed_set, expected_set);
542+
assert_eq!(allowed_hashes, expected_hashes);
547543

548544
// Use existing participant nodes for testing different hash versions
549545
let node_ids = test_setup.get_participant_node_ids();
550546

551547
// Test that nodes can submit attestations with all hash versions at T=10s
552548
// All attestations should succeed during grace period (current time: T=10s)
553549
for (node, &hash) in node_ids.iter().zip(hashes.iter()) {
554-
let attestation = TestSetup::create_hash_attestation(hash);
550+
let attestation = TestSetup::create_attestation_with_hash_constraint(hash);
555551
test_setup.submit_attestation_for_node(node, attestation);
556552
}
557553

@@ -563,20 +559,21 @@ fn nodes_can_start_with_old_valid_hashes_during_grace_period() {
563559
// but at T=20s it has expired and should be filtered out by allowed_code_hashes()
564560
set_system_time(v1_expiry_time + NANOS_IN_SECOND); // T=20s
565561

566-
// Verify that only non-expired hashes remain valid at T=20s
567-
// hash_v1 should have already expired, so only hash_v2 and hash_v3 should be allowed
568-
let allowed_after_v1_expiry = test_setup.contract.allowed_code_hashes();
569-
let allowed_set_after_v1_expiry: HashSet<_> =
570-
allowed_after_v1_expiry.iter().map(|h| **h).collect();
571-
let expected_set_after_v1_expiry: HashSet<_> = [hash_v2, hash_v3].into_iter().collect();
562+
// T=20s: hash_v1 is expired. Verify that only hash_v2 and hash_v3 are allowed.
563+
let allowed_after_v1_expiry: [_; 2] = test_setup
564+
.contract
565+
.allowed_code_hashes()
566+
.try_into()
567+
.unwrap();
568+
let expected_after_v1_expiry = [hash_v2.into(), hash_v3.into()];
572569

573570
assert_eq!(
574-
allowed_set_after_v1_expiry, expected_set_after_v1_expiry,
571+
allowed_after_v1_expiry, expected_after_v1_expiry,
575572
"Only hash_v2 and hash_v3 should remain valid at T=20s (hash_v1 expired)"
576573
);
577574

578575
// Verify that submitting attestation with expired hash_v1 now fails
579-
let expired_attestation = TestSetup::create_hash_attestation(hash_v1);
576+
let expired_attestation = TestSetup::create_attestation_with_hash_constraint(hash_v1);
580577
let result = test_setup.try_submit_attestation_for_node(&node_ids[0], expired_attestation);
581578
assert!(
582579
result.is_err(),
@@ -586,11 +583,8 @@ fn nodes_can_start_with_old_valid_hashes_during_grace_period() {
586583
// Test late-joining nodes at current time T=20s (after hash_v1 expired)
587584
// Only hash_v2 and hash_v3 should be valid for new nodes
588585
// Reuse existing node_ids (nodes 2 and 3 since hash_v1 expired)
589-
for (node, &hash) in node_ids[1..]
590-
.iter()
591-
.zip(expected_set_after_v1_expiry.iter())
592-
{
593-
let late_attestation = TestSetup::create_hash_attestation(hash);
586+
for (node, hash) in node_ids[1..].iter().zip(expected_after_v1_expiry.iter()) {
587+
let late_attestation = TestSetup::create_attestation_with_hash_constraint(**hash);
594588
test_setup.submit_attestation_for_node(node, late_attestation);
595589
}
596590

@@ -604,7 +598,7 @@ fn nodes_can_start_with_old_valid_hashes_during_grace_period() {
604598

605599
// Verify that only the latest hash is now accepted
606600
// Reuse the third node (index 2) for final validation
607-
let final_attestation = TestSetup::create_hash_attestation(hash_v3);
601+
let final_attestation = TestSetup::create_attestation_with_hash_constraint(hash_v3);
608602
// This should succeed since hash_v3 is the only remaining valid hash
609603
test_setup.submit_attestation_for_node(&node_ids[2], final_attestation);
610604
}

crates/contract/tests/sandbox/tee.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ async fn test_vote_code_hash_approved_hashes_persist_after_vote_changes() -> Res
140140
assert!(allowed_hashes.contains(&first_hash));
141141
assert!(allowed_hashes.contains(&second_hash));
142142
// Latest should still be the second hash
143-
assert_eq!(
144-
get_latest_code_hash(&contract).await?,
145-
Some(second_hash.clone())
146-
);
143+
assert_eq!(get_latest_code_hash(&contract).await?, Some(second_hash));
147144

148145
Ok(())
149146
}

0 commit comments

Comments
 (0)