Skip to content

Commit

Permalink
fix: Fix overflow and unexpected behavior of deposit nonce set/get (#127
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tolak committed Sep 11, 2023
1 parent 09ab02f commit 6652fe4
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,14 +769,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 @@ -2534,5 +2534,43 @@ pub mod pallet {
assert_eq!(Balances::free_balance(&BOB), ENDOWED_BALANCE + 200000000);
})
}

#[test]
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!(!SygmaBridge::is_proposal_executed(0, 0));
// Nonce 1 should be set
assert!(SygmaBridge::is_proposal_executed(1, 0));
// Nonce 63 should be set
assert!(SygmaBridge::is_proposal_executed(63, 0));

// 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!(SygmaBridge::is_proposal_executed(64, 0));
// Nonce 127 should be set
assert!(SygmaBridge::is_proposal_executed(127, 0));
// Nonce 128 should not be set
assert!(!SygmaBridge::is_proposal_executed(128, 0));

// 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!(SygmaBridge::is_proposal_executed(256, 0));
// Nonce 300 should be set
assert!(SygmaBridge::is_proposal_executed(300, 0));
// Nonce 301 should not be set
assert!(!SygmaBridge::is_proposal_executed(301, 0));
})
}
}
}

0 comments on commit 6652fe4

Please sign in to comment.