Skip to content

Commit aaf1e43

Browse files
committed
solana: Add check for no registered transceivers
Add guard on transfer instructions to prevent a transfer from being processed when no transceivers are registered. This is done by checking the `next_transceiver_id` field in the Config. This field auto-increments when a transceiver is added. If this value is 0, then the program is in a state where no transceivers have been registered.
1 parent 55dce5f commit aaf1e43

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ impl Bitmap {
4949
.try_into()
5050
.expect("Bitmap length must not exceed the bounds of u8")
5151
}
52+
53+
pub fn len(self) -> usize {
54+
BM::<128>::from_value(self.map).len()
55+
}
56+
57+
pub fn is_empty(self) -> bool {
58+
BM::<128>::from_value(self.map).is_empty()
59+
}
5260
}
5361

5462
#[cfg(test)]

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

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub enum NTTError {
5151
OverflowScaledAmount,
5252
#[msg("BitmapIndexOutOfBounds")]
5353
BitmapIndexOutOfBounds,
54+
#[msg("NoRegisteredTransceivers")]
55+
NoRegisteredTransceivers,
5456
}
5557

5658
impl From<ScalingError> for NTTError {

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

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub struct Redeem<'info> {
2424
// NOTE: this works when the contract is paused
2525
#[account(
2626
constraint = config.threshold > 0 @ NTTError::ZeroThreshold,
27+
constraint = config.next_transceiver_id != 0 @ NTTError::NoRegisteredTransceivers,
28+
constraint = !config.enabled_transceivers.is_empty() @ NTTError::NoRegisteredTransceivers,
2729
)]
2830
pub config: Account<'info, Config>,
2931

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

+7
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(
@@ -120,6 +125,7 @@ pub fn transfer_burn(ctx: Context<TransferBurn>, args: TransferArgs) -> Result<(
120125
);
121126

122127
let accs = ctx.accounts;
128+
123129
let TransferArgs {
124130
mut amount,
125131
recipient_chain,
@@ -223,6 +229,7 @@ pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<(
223229
);
224230

225231
let accs = ctx.accounts;
232+
226233
let TransferArgs {
227234
mut amount,
228235
recipient_chain,

solana/tests/example-native-token-transfer.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,40 @@ describe("example-native-token-transfers", () => {
9595
mode: "locking",
9696
});
9797

98-
await ntt.registerTransceiver({
98+
const transceiver = await ntt.registerTransceiver({
9999
payer,
100100
owner: payer,
101101
transceiver: ntt.program.programId,
102102
});
103103

104-
await ntt.setWormholeTransceiverPeer({
104+
if (transceiver === null) {
105+
throw new Error('did not register transceiver')
106+
}
107+
108+
const transceiverPeer = await ntt.setWormholeTransceiverPeer({
105109
payer,
106110
owner: payer,
107111
chain: "ethereum",
108112
address: Buffer.from("transceiver".padStart(32, "\0")),
109113
});
110114

111-
await ntt.setPeer({
115+
if (transceiverPeer === null) {
116+
throw new Error('did not set transceiver peer')
117+
}
118+
119+
const peer = await ntt.setPeer({
112120
payer,
113121
owner: payer,
114122
chain: "ethereum",
115123
address: Buffer.from("nttManager".padStart(32, "\0")),
116124
limit: new BN(1000000),
117125
tokenDecimals: 18,
118126
});
127+
128+
if (peer === null) {
129+
throw new Error('did not set peer')
130+
}
131+
119132
});
120133

121134
it("Can send tokens", async () => {

0 commit comments

Comments
 (0)