Skip to content

Commit

Permalink
fix overflow and unexpected behavior of deposit nonce set/get
Browse files Browse the repository at this point in the history
  • Loading branch information
tolak committed Sep 9, 2023
1 parent c0aaeda commit 0046451
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,14 +848,14 @@ pub mod pallet {

/// Return true if deposit nonce has been used
pub fn is_proposal_executed(nonce: DepositNonce, domain_id: DomainID) -> bool {
(UsedNonces::<T>::get(domain_id, nonce / 256) & (1 << (nonce % 256))) != 0
(UsedNonces::<T>::get(domain_id, nonce / 64) & (1 << (nonce % 64))) != 0
}

/// Set bit mask for specific nonce as used
fn set_proposal_executed(nonce: DepositNonce, domain_id: DomainID) {
let mut current_nonces = UsedNonces::<T>::get(domain_id, nonce / 256);
current_nonces |= 1 << (nonce % 256);
UsedNonces::<T>::insert(domain_id, nonce / 256, current_nonces);
let mut current_nonces = UsedNonces::<T>::get(domain_id, nonce / 64);
current_nonces |= 1 << (nonce % 64);
UsedNonces::<T>::insert(domain_id, nonce / 64, current_nonces);
}

/// Execute a single proposal
Expand Down Expand Up @@ -3171,5 +3171,42 @@ pub mod pallet {
)]);
})
}

fn deposit_nonce_fix_should_work() {
new_test_ext().execute_with(|| {
// Nonce from source chain start from 1, set first batch of nonce under [1, 63]
for nonce in 1..64u64 {
SygmaBridge::set_proposal_executed(nonce, 0);
}
// Nonce 0 should not be set
assert_eq!(SygmaBridge::is_proposal_executed(0, 0), false);
// Nonce 1 should be set
assert_eq!(SygmaBridge::is_proposal_executed(1, 0), true);
// Nonce 63 should be set
assert_eq!(SygmaBridge::is_proposal_executed(63, 0), true);

// set second batch of nonce under [64, 127]
for nonce in 64..128u64 {
SygmaBridge::set_proposal_executed(nonce, 0);
}
// Nonce 64 should be set
assert_eq!(SygmaBridge::is_proposal_executed(64, 0), true);
// Nonce 127 should be set
assert_eq!(SygmaBridge::is_proposal_executed(127, 0), true);
// Nonce 128 should not be set
assert_eq!(SygmaBridge::is_proposal_executed(128, 0), false);

// set future batch of nonce under [256, 300]
for nonce in 256..301u64 {
SygmaBridge::set_proposal_executed(nonce, 0);
}
// Nonce 256 should be set
assert_eq!(SygmaBridge::is_proposal_executed(256, 0), true);
// Nonce 300 should be set
assert_eq!(SygmaBridge::is_proposal_executed(300, 0), true);
// Nonce 301 should not be set
assert_eq!(SygmaBridge::is_proposal_executed(301, 0), false);
})
}
}
}

0 comments on commit 0046451

Please sign in to comment.