Skip to content

Commit 1ba37bd

Browse files
committed
solana: Change transceiver guard to anchor constraint
Adds methods on the repo's custom Bitmap struct to expose length-related methods from the underlying Bitmap struct. This allows the Anchor code to check the length of the Bitmap which corresponds to the number of enabled transceivers.
1 parent dbbabcf commit 1ba37bd

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

solana/programs/example-native-token-transfers/src/bitmap.rs

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ impl Bitmap {
3535
let bm = BM::<128>::from_value(self.map) & BM::<128>::from_value(enabled.map);
3636
bm.len() as u8
3737
}
38+
39+
pub fn len(self) -> usize {
40+
BM::<128>::from_value(self.map).len()
41+
}
42+
43+
pub fn is_empty(self) -> bool {
44+
BM::<128>::from_value(self.map).is_empty()
45+
}
3846
}
3947

4048
#[cfg(test)]

solana/programs/example-native-token-transfers/src/instructions/transfer.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ pub struct Transfer<'info> {
2222
#[account(mut)]
2323
pub payer: Signer<'info>,
2424

25+
// Ensure that there exists at least one enabled transceiver
26+
#[account(
27+
constraint = config.next_transceiver_id != 0 @ NTTError::NoRegisteredTransceivers,
28+
constraint = config.enabled_transceivers.is_empty() @ NTTError::NoRegisteredTransceivers,
29+
)]
2530
pub config: NotPausedConfig<'info>,
2631

2732
#[account(
@@ -121,14 +126,6 @@ pub fn transfer_burn(ctx: Context<TransferBurn>, args: TransferArgs) -> Result<(
121126

122127
let accs = ctx.accounts;
123128

124-
// TODO If functionality is added to disable transceivers, this check should be modified to
125-
// ensure that, if all transceivers have been disabled, that an error is returned. Otherwise it
126-
// may be possible to permanently lose funds.
127-
// No transceivers have been registered yet
128-
if accs.common.config.next_transceiver_id == 0 {
129-
return Err(NTTError::NoRegisteredTransceivers.into());
130-
}
131-
132129
let TransferArgs {
133130
mut amount,
134131
recipient_chain,
@@ -223,14 +220,6 @@ pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<(
223220

224221
let accs = ctx.accounts;
225222

226-
// TODO If functionality is added to disable transceivers, this check should be modified to
227-
// ensure that, if all transceivers have been disabled, that an error is returned. Otherwise it
228-
// may be possible to permanently lose funds.
229-
// No transceivers have been registered yet
230-
if accs.common.config.next_transceiver_id == 0 {
231-
return Err(NTTError::NoRegisteredTransceivers.into());
232-
}
233-
234223
let TransferArgs {
235224
mut amount,
236225
recipient_chain,

0 commit comments

Comments
 (0)