-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmod.rs
274 lines (227 loc) · 6.99 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use anchor_lang::prelude::*;
use ntt_messages::chain_id::ChainId;
use crate::{
config::Config,
error::NTTError,
peer::NttManagerPeer,
queue::{inbox::InboxRateLimit, outbox::OutboxRateLimit, rate_limit::RateLimitState},
registered_transceiver::RegisteredTransceiver,
};
pub mod transfer_ownership;
pub mod transfer_token_authority;
pub use transfer_ownership::*;
pub use transfer_token_authority::*;
// * Set peers
#[derive(Accounts)]
#[instruction(args: SetPeerArgs)]
pub struct SetPeer<'info> {
#[account(mut)]
pub payer: Signer<'info>,
pub owner: Signer<'info>,
#[account(
has_one = owner,
)]
pub config: Account<'info, Config>,
#[account(
init_if_needed,
space = 8 + NttManagerPeer::INIT_SPACE,
payer = payer,
seeds = [NttManagerPeer::SEED_PREFIX, args.chain_id.id.to_be_bytes().as_ref()],
bump
)]
pub peer: Account<'info, NttManagerPeer>,
#[account(
init_if_needed,
space = 8 + InboxRateLimit::INIT_SPACE,
payer = payer,
seeds = [
InboxRateLimit::SEED_PREFIX,
args.chain_id.id.to_be_bytes().as_ref()
],
bump,
)]
pub inbox_rate_limit: Account<'info, InboxRateLimit>,
pub system_program: Program<'info, System>,
}
#[derive(AnchorDeserialize, AnchorSerialize)]
pub struct SetPeerArgs {
pub chain_id: ChainId,
pub address: [u8; 32],
pub limit: u64,
/// The token decimals on the peer chain.
pub token_decimals: u8,
}
pub fn set_peer(ctx: Context<SetPeer>, args: SetPeerArgs) -> Result<()> {
ctx.accounts.peer.set_inner(NttManagerPeer {
bump: ctx.bumps.peer,
address: args.address,
token_decimals: args.token_decimals,
});
ctx.accounts.inbox_rate_limit.set_inner(InboxRateLimit {
bump: ctx.bumps.inbox_rate_limit,
rate_limit: RateLimitState::new(args.limit),
});
Ok(())
}
// * Transceiver registration
#[derive(Accounts)]
pub struct RegisterTransceiver<'info> {
#[account(
mut,
has_one = owner,
)]
pub config: Account<'info, Config>,
pub owner: Signer<'info>,
#[account(mut)]
pub payer: Signer<'info>,
#[account(
executable,
constraint = transceiver.key() != Pubkey::default() @ NTTError::InvalidTransceiverProgram
)]
/// CHECK: transceiver is meant to be a transceiver program. Arguably a `Program` constraint could be
/// used here that wraps the Transceiver account type.
pub transceiver: UncheckedAccount<'info>,
#[account(
init_if_needed,
space = 8 + RegisteredTransceiver::INIT_SPACE,
payer = payer,
seeds = [RegisteredTransceiver::SEED_PREFIX, transceiver.key().as_ref()],
bump
)]
pub registered_transceiver: Account<'info, RegisteredTransceiver>,
pub system_program: Program<'info, System>,
}
pub fn register_transceiver(ctx: Context<RegisterTransceiver>) -> Result<()> {
// initialize registered transceiver with new id on init
if ctx.accounts.registered_transceiver.transceiver_address == Pubkey::default() {
let id = ctx.accounts.config.next_transceiver_id;
ctx.accounts.config.next_transceiver_id += 1;
ctx.accounts
.registered_transceiver
.set_inner(RegisteredTransceiver {
bump: ctx.bumps.registered_transceiver,
id,
transceiver_address: ctx.accounts.transceiver.key(),
});
}
ctx.accounts
.config
.enabled_transceivers
.set(ctx.accounts.registered_transceiver.id, true)?;
Ok(())
}
#[derive(Accounts)]
pub struct DeregisterTransceiver<'info> {
#[account(
mut,
has_one = owner,
)]
pub config: Account<'info, Config>,
#[account(mut)]
pub owner: Signer<'info>,
#[account(executable)]
/// CHECK: transceiver is meant to be a transceiver program. Arguably a `Program` constraint could be
/// used here that wraps the Transceiver account type.
pub transceiver: UncheckedAccount<'info>,
#[account(
seeds = [RegisteredTransceiver::SEED_PREFIX, transceiver.key().as_ref()],
bump,
constraint = config.enabled_transceivers.get(registered_transceiver.id)? @ NTTError::DisabledTransceiver,
)]
pub registered_transceiver: Account<'info, RegisteredTransceiver>,
}
pub fn deregister_transceiver(ctx: Context<DeregisterTransceiver>) -> Result<()> {
ctx.accounts
.config
.enabled_transceivers
.set(ctx.accounts.registered_transceiver.id, false)?;
// decrement threshold if too high
let num_enabled_transceivers = ctx.accounts.config.enabled_transceivers.len();
if num_enabled_transceivers < ctx.accounts.config.threshold {
// threshold should be at least 1
ctx.accounts.config.threshold = num_enabled_transceivers.max(1);
}
Ok(())
}
// * Limit rate adjustment
#[derive(Accounts)]
pub struct SetOutboundLimit<'info> {
#[account(
has_one = owner,
)]
pub config: Account<'info, Config>,
pub owner: Signer<'info>,
#[account(mut)]
pub rate_limit: Account<'info, OutboxRateLimit>,
}
#[derive(AnchorDeserialize, AnchorSerialize)]
pub struct SetOutboundLimitArgs {
pub limit: u64,
}
pub fn set_outbound_limit(
ctx: Context<SetOutboundLimit>,
args: SetOutboundLimitArgs,
) -> Result<()> {
ctx.accounts.rate_limit.set_limit(args.limit);
Ok(())
}
#[derive(Accounts)]
#[instruction(args: SetInboundLimitArgs)]
pub struct SetInboundLimit<'info> {
#[account(
has_one = owner,
)]
pub config: Account<'info, Config>,
pub owner: Signer<'info>,
#[account(
mut,
seeds = [
InboxRateLimit::SEED_PREFIX,
args.chain_id.id.to_be_bytes().as_ref()
],
bump = rate_limit.bump
)]
pub rate_limit: Account<'info, InboxRateLimit>,
}
#[derive(AnchorDeserialize, AnchorSerialize)]
pub struct SetInboundLimitArgs {
pub limit: u64,
pub chain_id: ChainId,
}
pub fn set_inbound_limit(ctx: Context<SetInboundLimit>, args: SetInboundLimitArgs) -> Result<()> {
ctx.accounts.rate_limit.set_limit(args.limit);
Ok(())
}
// * Pausing
#[derive(Accounts)]
pub struct SetPaused<'info> {
pub owner: Signer<'info>,
#[account(
mut,
has_one = owner,
)]
pub config: Account<'info, Config>,
}
pub fn set_paused(ctx: Context<SetPaused>, paused: bool) -> Result<()> {
ctx.accounts.config.paused = paused;
Ok(())
}
// * Set Threshold
#[derive(Accounts)]
#[instruction(threshold: u8)]
pub struct SetThreshold<'info> {
pub owner: Signer<'info>,
#[account(
mut,
has_one = owner,
constraint = threshold <= config.enabled_transceivers.len() @ NTTError::ThresholdTooHigh
)]
pub config: Account<'info, Config>,
}
pub fn set_threshold(ctx: Context<SetThreshold>, threshold: u8) -> Result<()> {
if threshold == 0 {
return Err(NTTError::ZeroThreshold.into());
}
ctx.accounts.config.threshold = threshold;
Ok(())
}