-
Notifications
You must be signed in to change notification settings - Fork 734
/
Copy pathpost_message.rs
245 lines (212 loc) · 6.81 KB
/
post_message.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
use crate::{
accounts::{
Bridge,
FeeCollector,
PostedMessage,
PostedMessageUnreliable,
Sequence,
SequenceDerivationData,
},
error::Error::{
EmitterChanged,
InsufficientFees,
InvalidPayloadLength,
MathOverflow,
},
types::ConsistencyLevel,
IsSigned::*,
MessageData,
CHAIN_ID_SOLANA,
};
use solana_program::{
msg,
pubkey::Pubkey,
sysvar::clock::Clock,
};
use solitaire::{
processors::seeded::Seeded,
trace,
CreationLamports::Exempt,
*,
};
pub type UninitializedMessage<'b> = PostedMessage<'b, { AccountState::Uninitialized }>;
#[derive(FromAccounts)]
pub struct PostMessage<'b> {
/// Bridge config needed for fee calculation.
pub bridge: Mut<Bridge<'b, { AccountState::Initialized }>>,
/// Account to store the posted message
pub message: Signer<Mut<UninitializedMessage<'b>>>,
/// Emitter of the VAA
pub emitter: Signer<MaybeMut<Info<'b>>>,
/// Tracker for the emitter sequence
pub sequence: Mut<Sequence<'b>>,
/// Payer for account creation
pub payer: Mut<Signer<Info<'b>>>,
/// Account to collect tx fee
pub fee_collector: Mut<FeeCollector<'b>>,
pub clock: Sysvar<'b, Clock>,
}
#[derive(FromAccounts)]
pub struct PostMessageUnreliable<'b> {
/// Bridge config needed for fee calculation.
pub bridge: Mut<Bridge<'b, { AccountState::Initialized }>>,
/// Account to store the posted message
pub message: Signer<Mut<PostedMessageUnreliable<'b, { AccountState::MaybeInitialized }>>>,
/// Emitter of the VAA
pub emitter: Signer<MaybeMut<Info<'b>>>,
/// Tracker for the emitter sequence
pub sequence: Mut<Sequence<'b>>,
/// Payer for account creation
pub payer: Mut<Signer<Info<'b>>>,
/// Account to collect tx fee
pub fee_collector: Mut<FeeCollector<'b>>,
pub clock: Sysvar<'b, Clock>,
}
#[derive(BorshDeserialize, BorshSerialize)]
pub struct PostMessageData {
/// Unique nonce for this message
pub nonce: u32,
/// Message payload
pub payload: Vec<u8>,
/// Commitment Level required for an attestation to be produced
pub consistency_level: ConsistencyLevel,
}
pub fn post_message(
ctx: &ExecutionContext,
accs: &mut PostMessage,
data: PostMessageData,
) -> Result<()> {
post_message_internal(
ctx,
&mut accs.bridge,
accs.message.info().key,
&mut accs.message,
&mut accs.emitter,
&mut accs.sequence,
&mut accs.payer,
&mut accs.fee_collector,
&mut accs.clock,
data,
)?;
// Create message account
let size = accs.message.size();
create_account(
ctx,
accs.message.info(),
accs.payer.key,
Exempt,
size,
ctx.program_id,
NotSigned,
)?;
Ok(())
}
/// Post a message while reusing the message account. This saves the rent that would be required for
/// allocating a new message account. When an account is reused and the guardians don't pick up the
/// message due to network instability or a bug there is NO way to recover the message if it has
/// been overwritten. This makes this instruction useful for use-cases that require high number of
/// messages to be published but don't require 100% delivery guarantee.
/// DO NOT USE THIS FOR USE-CASES THAT MOVE VALUE; MESSAGES MAY NOT BE DELIVERED
pub fn post_message_unreliable(
ctx: &ExecutionContext,
accs: &mut PostMessageUnreliable,
data: PostMessageData,
) -> Result<()> {
// Accounts can't be resized so the payload sizes need to match
if accs.message.is_initialized() && accs.message.payload.len() != data.payload.len() {
return Err(InvalidPayloadLength.into());
}
// The emitter must be identical
if accs.message.is_initialized() && accs.emitter.key.to_bytes() != accs.message.emitter_address
{
return Err(EmitterChanged.into());
}
post_message_internal(
ctx,
&mut accs.bridge,
accs.message.info().key,
&mut accs.message,
&mut accs.emitter,
&mut accs.sequence,
&mut accs.payer,
&mut accs.fee_collector,
&mut accs.clock,
data,
)?;
if !accs.message.is_initialized() {
// Create message account
let size = accs.message.size();
create_account(
ctx,
accs.message.info(),
accs.payer.key,
Exempt,
size,
ctx.program_id,
NotSigned,
)?;
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn post_message_internal<'b>(
ctx: &ExecutionContext,
bridge: &mut Mut<Bridge<'b, { AccountState::Initialized }>>,
#[cfg_attr(not(feature = "trace"), allow(unused_variables))] message_key: &Pubkey,
message: &mut MessageData,
emitter: &mut Signer<MaybeMut<Info<'b>>>,
sequence: &mut Mut<Sequence<'b>>,
payer: &mut Mut<Signer<Info<'b>>>,
fee_collector: &mut Mut<FeeCollector<'b>>,
clock: &mut Sysvar<'b, Clock>,
data: PostMessageData,
) -> Result<()> {
trace!("Message Address: {}", message_key);
trace!("Emitter Address: {}", emitter.info().key);
trace!("Nonce: {}", data.nonce);
let sequence_derivation = SequenceDerivationData {
emitter_key: emitter.key,
};
sequence.verify_derivation(ctx.program_id, &sequence_derivation)?;
let fee = bridge.config.fee;
// Fee handling, checking previously known balance allows us to not care who is the payer of
// this submission.
if fee_collector
.lamports()
.checked_sub(bridge.last_lamports)
.ok_or(MathOverflow)?
< fee
{
trace!(
"Expected fee not found: fee, last_lamports, collector: {} {} {}",
fee,
bridge.last_lamports,
fee_collector.lamports(),
);
return Err(InsufficientFees.into());
}
bridge.last_lamports = fee_collector.lamports();
// Init sequence tracker if it does not exist yet.
if !sequence.is_initialized() {
trace!("Initializing Sequence account to 0.");
sequence.create(&sequence_derivation, ctx, payer.key, Exempt)?;
}
// DO NOT REMOVE - CRITICAL OUTPUT
msg!("Sequence: {}", sequence.sequence);
// Initialize transfer
trace!("Setting Message Details");
message.submission_time = clock.unix_timestamp as u32;
message.emitter_chain = CHAIN_ID_SOLANA;
message.emitter_address = emitter.key.to_bytes();
message.nonce = data.nonce;
message.payload = data.payload;
message.sequence = sequence.sequence;
message.consistency_level = match data.consistency_level {
ConsistencyLevel::Confirmed => 1,
ConsistencyLevel::Finalized => 32,
};
// Bump sequence number
trace!("New Sequence: {}", sequence.sequence + 1);
sequence.sequence += 1;
Ok(())
}