Skip to content

Commit fd5b3b8

Browse files
Bengt Lofgrenbengtlofgren
authored andcommitted
from router passed to initialize fast market order
1 parent 2af2c6b commit fd5b3b8

File tree

8 files changed

+74
-42
lines changed

8 files changed

+74
-42
lines changed

solana/modules/matching-engine-testing/tests/shimful/fast_market_order_shim.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,18 @@ pub async fn initialize_fast_market_order_shimful(
8080
],
8181
program_id,
8282
);
83+
let from_endpoint = current_state
84+
.router_endpoints()
85+
.unwrap()
86+
.endpoints
87+
.get_from_and_to_endpoint_addresses(testing_context.transfer_direction)
88+
.0;
8389
let initialize_fast_market_order_ix = initialize_fast_market_order_shimful_instruction(
8490
&payer_signer,
8591
program_id,
8692
fast_market_order,
8793
&guardian_signature_info,
94+
&from_endpoint,
8895
);
8996
let transaction = testing_context
9097
.create_transaction(
@@ -142,6 +149,7 @@ pub fn initialize_fast_market_order_shimful_instruction(
142149
program_id: &Pubkey,
143150
fast_market_order: FastMarketOrderState,
144151
guardian_signature_info: &GuardianSignatureInfo,
152+
from_endpoint: &Pubkey,
145153
) -> solana_program::instruction::Instruction {
146154
let fast_market_order_account = Pubkey::find_program_address(
147155
&[
@@ -158,6 +166,7 @@ pub fn initialize_fast_market_order_shimful_instruction(
158166
fast_market_order_account: &fast_market_order_account,
159167
guardian_set: &guardian_signature_info.guardian_set_pubkey,
160168
guardian_set_signatures: &guardian_signature_info.guardian_signatures_pubkey,
169+
from_endpoint: from_endpoint,
161170
verify_vaa_shim_program: &WORMHOLE_VERIFY_VAA_SHIM_PID,
162171
system_program: &solana_program::system_program::ID,
163172
};
@@ -274,7 +283,6 @@ pub fn create_fast_market_order_state_from_vaa_data(
274283
close_account_refund_recipient,
275284
vaa_sequence: vaa_data.sequence,
276285
vaa_timestamp: vaa_data.vaa_time,
277-
vaa_nonce: vaa_data.nonce,
278286
vaa_emitter_chain: vaa_data.emitter_chain,
279287
vaa_consistency_level: vaa_data.consistency_level,
280288
vaa_emitter_address: vaa_data.emitter_address,

solana/modules/matching-engine-testing/tests/test_scenarios/create_and_close_fast_market_order.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ pub async fn test_initialize_fast_market_order_fallback() {
6969

7070
let instruction_triggers = vec![
7171
InstructionTrigger::InitializeProgram(InitializeInstructionConfig::default()),
72+
InstructionTrigger::CreateCctpRouterEndpoints(
73+
CreateCctpRouterEndpointsInstructionConfig::default(),
74+
),
7275
InstructionTrigger::InitializeFastMarketOrderShim(
7376
InitializeFastMarketOrderShimInstructionConfig::default(),
7477
),
@@ -96,6 +99,9 @@ pub async fn test_close_fast_market_order_fallback() {
9699
let testing_engine = TestingEngine::new(testing_context).await;
97100
let instruction_triggers = vec![
98101
InstructionTrigger::InitializeProgram(InitializeInstructionConfig::default()),
102+
InstructionTrigger::CreateCctpRouterEndpoints(
103+
CreateCctpRouterEndpointsInstructionConfig::default(),
104+
),
99105
InstructionTrigger::InitializeFastMarketOrderShim(
100106
InitializeFastMarketOrderShimInstructionConfig::default(),
101107
),
@@ -126,6 +132,9 @@ pub async fn test_close_fast_market_order_fallback_with_custom_refund_recipient(
126132
let testing_engine = TestingEngine::new(testing_context).await;
127133
let instruction_triggers = vec![
128134
InstructionTrigger::InitializeProgram(InitializeInstructionConfig::default()),
135+
InstructionTrigger::CreateCctpRouterEndpoints(
136+
CreateCctpRouterEndpointsInstructionConfig::default(),
137+
),
129138
InstructionTrigger::InitializeFastMarketOrderShim(
130139
InitializeFastMarketOrderShimInstructionConfig {
131140
close_account_refund_recipient: Some(solver_1.pubkey()),
@@ -238,6 +247,9 @@ pub async fn test_fast_market_order_cannot_be_closed_twice() {
238247
let testing_engine = TestingEngine::new(testing_context).await;
239248
let instruction_triggers = vec![
240249
InstructionTrigger::InitializeProgram(InitializeInstructionConfig::default()),
250+
InstructionTrigger::CreateCctpRouterEndpoints(
251+
CreateCctpRouterEndpointsInstructionConfig::default(),
252+
),
241253
InstructionTrigger::InitializeFastMarketOrderShim(
242254
InitializeFastMarketOrderShimInstructionConfig::default(),
243255
),
@@ -286,6 +298,9 @@ pub async fn test_fast_market_order_can_be_opened_after_being_closed_by_the_same
286298
let testing_engine = TestingEngine::new(testing_context).await;
287299
let instruction_triggers = vec![
288300
InstructionTrigger::InitializeProgram(InitializeInstructionConfig::default()),
301+
InstructionTrigger::CreateCctpRouterEndpoints(
302+
CreateCctpRouterEndpointsInstructionConfig::default(),
303+
),
289304
InstructionTrigger::InitializeFastMarketOrderShim(
290305
InitializeFastMarketOrderShimInstructionConfig::default(),
291306
),
@@ -321,6 +336,9 @@ pub async fn test_multiple_fast_market_orders_can_be_opened_and_closed_by_differ
321336
let testing_engine = TestingEngine::new(testing_context).await;
322337
let instruction_triggers = vec![
323338
InstructionTrigger::InitializeProgram(InitializeInstructionConfig::default()),
339+
InstructionTrigger::CreateCctpRouterEndpoints(
340+
CreateCctpRouterEndpointsInstructionConfig::default(),
341+
),
324342
InstructionTrigger::InitializeFastMarketOrderShim(
325343
InitializeFastMarketOrderShimInstructionConfig {
326344
fast_market_order_id: 0,

solana/modules/matching-engine-testing/tests/test_scenarios/execute_order.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,35 +1066,22 @@ pub async fn test_execute_order_shim_emitter_chain_mismatch() {
10661066
InitializeFastMarketOrderShimInstructionConfig {
10671067
fast_market_order_id: 1,
10681068
vaa_index: 1,
1069+
expected_error: Some(ExpectedError {
1070+
instruction_index: 2,
1071+
error_code: u32::from(MatchingEngineError::InvalidEndpoint),
1072+
error_string: "InvalidEndpoint".to_string(),
1073+
}),
10691074
..InitializeFastMarketOrderShimInstructionConfig::default()
10701075
},
10711076
),
10721077
];
1073-
let initialize_second_fast_market_order_state = testing_engine
1078+
testing_engine
10741079
.execute(
10751080
&mut test_context,
10761081
initialize_second_fast_market_order_instruction_triggers,
10771082
Some(initialize_first_fast_market_order_state),
10781083
)
10791084
.await;
1080-
let instruction_triggers = vec![InstructionTrigger::ExecuteOrderShim(
1081-
ExecuteOrderInstructionConfig {
1082-
vaa_index: 1,
1083-
expected_error: Some(ExpectedError {
1084-
instruction_index: 2,
1085-
error_code: u32::from(MatchingEngineError::VaaMismatch),
1086-
error_string: "AccountNotInitialized".to_string(),
1087-
}),
1088-
..ExecuteOrderInstructionConfig::default()
1089-
},
1090-
)];
1091-
testing_engine
1092-
.execute(
1093-
&mut test_context,
1094-
instruction_triggers,
1095-
Some(initialize_second_fast_market_order_state),
1096-
)
1097-
.await;
10981085
}
10991086

11001087
/// Cannot execute order shim before auction duration is over

solana/modules/matching-engine-testing/tests/testing_engine/engine.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,11 +886,18 @@ impl TestingEngine {
886886
],
887887
program_id,
888888
);
889+
let from_endpoint_pubkey = current_state
890+
.router_endpoints()
891+
.unwrap()
892+
.endpoints
893+
.get_from_and_to_endpoint_addresses(self.testing_context.transfer_direction)
894+
.0;
889895
let create_fast_market_order_instruction = initialize_fast_market_order_shimful_instruction(
890896
&create_fast_market_order_payer_signer,
891897
program_id,
892898
fast_market_order,
893899
&guardian_signature_info,
900+
&from_endpoint_pubkey,
894901
);
895902

896903
let place_initial_offer_instruction = place_initial_offer_shimful_instruction(

solana/modules/matching-engine-testing/tests/utils/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Deref for TestRouterEndpoints {
9898
}
9999

100100
impl TestRouterEndpoints {
101-
#[allow(dead_code)]
101+
102102
pub fn get_from_and_to_endpoint_addresses(
103103
&self,
104104
transfer_direction: TransferDirection,

solana/programs/matching-engine/src/fallback/processor/initialize_fast_market_order.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ use anchor_lang::{prelude::*, Discriminator};
22
use bytemuck::{Pod, Zeroable};
33
use solana_program::{instruction::Instruction, keccak};
44

5-
use crate::{state::FastMarketOrder, ID};
5+
use crate::{error::MatchingEngineError, state::FastMarketOrder, ID};
66

7-
const NUM_ACCOUNTS: usize = 6;
7+
const NUM_ACCOUNTS: usize = 7;
88

99
pub struct InitializeFastMarketOrderAccounts<'ix> {
1010
/// Lamports from this signer will be used to create the new fast market
1111
/// order account. This account will be the only authority allowed to
1212
/// close this account.
1313
// TODO: Rename to "payer".
1414
pub signer: &'ix Pubkey, // 0
15+
16+
/// The from router endpoint account for the hash of the fast market order
17+
pub from_endpoint: &'ix Pubkey,
1518
/// Wormhole guardian set account used to check recovered pubkeys using
1619
/// [Self::guardian_set_signatures].
1720
// TODO: Rename to "wormhole_guardian_set"
@@ -70,19 +73,22 @@ impl InitializeFastMarketOrder<'_> {
7073
let InitializeFastMarketOrderAccounts {
7174
signer: payer,
7275
fast_market_order_account: new_fast_market_order,
76+
from_endpoint,
77+
7378
guardian_set: wormhole_guardian_set,
7479
guardian_set_signatures: shim_guardian_signatures,
7580
verify_vaa_shim_program,
7681
system_program: _,
7782
} = self.accounts;
7883

7984
let accounts = vec![
80-
AccountMeta::new(*payer, true),
81-
AccountMeta::new_readonly(*verify_vaa_shim_program, false),
82-
AccountMeta::new_readonly(*wormhole_guardian_set, false),
83-
AccountMeta::new_readonly(*shim_guardian_signatures, false),
84-
AccountMeta::new(*new_fast_market_order, false),
85-
AccountMeta::new_readonly(solana_program::system_program::ID, false),
85+
AccountMeta::new(*payer, true), // 0
86+
AccountMeta::new(*new_fast_market_order, false), // 1
87+
AccountMeta::new_readonly(*from_endpoint, false), // 2
88+
AccountMeta::new_readonly(*wormhole_guardian_set, false), // 3
89+
AccountMeta::new_readonly(*shim_guardian_signatures, false), // 4
90+
AccountMeta::new_readonly(*verify_vaa_shim_program, false), // 5
91+
AccountMeta::new_readonly(solana_program::system_program::ID, false), // 6
8692
];
8793
debug_assert_eq!(accounts.len(), NUM_ACCOUNTS);
8894

@@ -115,18 +121,28 @@ pub(super) fn process(
115121

116122
// Verify the VAA digest with the Verify VAA shim program.
117123
super::helpers::invoke_verify_hash(
118-
1, // verify_vaa_shim_program_index
119-
2, // wormhole_guardian_set_index
120-
3, // shim_guardian_signatures_index
124+
5, // verify_vaa_shim_program_index
125+
3, // wormhole_guardian_set_index
126+
4, // shim_guardian_signatures_index
121127
data.guardian_set_bump,
122128
keccak::Hash(fast_market_order_vaa_digest),
123129
accounts,
124130
)?;
125131

132+
// These accounts will be used by the Verify VAA shim program.
133+
let from_endpoint = super::helpers::try_live_endpoint_account(&accounts[2], "from_endpoint")
134+
.map_err(|e: Error| e.with_account_name("from_endpoint"))?;
135+
136+
if fast_market_order.vaa_emitter_address != from_endpoint.address
137+
|| fast_market_order.vaa_emitter_chain != from_endpoint.chain
138+
{
139+
return Err(MatchingEngineError::InvalidEndpoint.into());
140+
}
141+
126142
// Create the new fast market order account and serialize the instruction
127143
// data into it.
128144

129-
let new_fast_market_order_info = &accounts[4];
145+
let new_fast_market_order_info = &accounts[1];
130146
let (expected_fast_market_order_key, fast_market_order_bump) = Pubkey::find_program_address(
131147
&[
132148
FastMarketOrder::SEED_PREFIX,
@@ -181,6 +197,7 @@ mod test {
181197
accounts: InitializeFastMarketOrderAccounts {
182198
signer: &Default::default(),
183199
fast_market_order_account: &Default::default(),
200+
from_endpoint: &Default::default(),
184201
verify_vaa_shim_program: &Default::default(),
185202
guardian_set: &Default::default(),
186203
guardian_set_signatures: &Default::default(),

solana/programs/matching-engine/src/fallback/processor/place_initial_offer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ mod tests {
359359
close_account_refund_recipient: Pubkey::default(),
360360
vaa_sequence: 0,
361361
vaa_timestamp: 0,
362-
vaa_nonce: 0,
363362
vaa_emitter_chain: 0,
364363
vaa_consistency_level: 0,
365364
vaa_emitter_address: [0_u8; 32],

solana/programs/matching-engine/src/state/fast_market_order.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,13 @@ pub struct FastMarketOrder {
4343
pub vaa_sequence: u64,
4444
/// The timestamp of the fast transfer VAA.
4545
pub vaa_timestamp: u32,
46-
/// The VAA nonce, which is not used and can be set to 0.
47-
// TODO: Can be taken out.
48-
pub vaa_nonce: u32,
4946
/// The source chain of the fast transfer VAA. (represented as a Wormhole
5047
/// chain ID).
5148
pub vaa_emitter_chain: u16,
5249
/// The consistency level of the fast transfer VAA.
5350
pub vaa_consistency_level: u8,
5451
/// Not used, but required for bytemuck serialization.
55-
_padding: [u8; 5],
52+
_padding: [u8; 1],
5653
}
5754

5855
pub struct FastMarketOrderParams {
@@ -70,7 +67,6 @@ pub struct FastMarketOrderParams {
7067
pub close_account_refund_recipient: Pubkey,
7168
pub vaa_sequence: u64,
7269
pub vaa_timestamp: u32,
73-
pub vaa_nonce: u32,
7470
pub vaa_emitter_chain: u16,
7571
pub vaa_consistency_level: u8,
7672
pub vaa_emitter_address: [u8; 32],
@@ -95,11 +91,10 @@ impl FastMarketOrder {
9591
close_account_refund_recipient: params.close_account_refund_recipient,
9692
vaa_sequence: params.vaa_sequence,
9793
vaa_timestamp: params.vaa_timestamp,
98-
vaa_nonce: params.vaa_nonce,
9994
vaa_emitter_chain: params.vaa_emitter_chain,
10095
vaa_consistency_level: params.vaa_consistency_level,
10196
vaa_emitter_address: params.vaa_emitter_address,
102-
_padding: [0_u8; 5],
97+
_padding: [0_u8; 1],
10398
}
10499
}
105100

@@ -132,7 +127,8 @@ impl FastMarketOrder {
132127
wormhole_svm_definitions::compute_keccak_digest(
133128
keccak::hashv(&[
134129
&self.vaa_timestamp.to_be_bytes(),
135-
&self.vaa_nonce.to_be_bytes(),
130+
// The nonce is 0
131+
&0_u32.to_be_bytes(),
136132
&self.vaa_emitter_chain.to_be_bytes(),
137133
&self.vaa_emitter_address,
138134
&self.vaa_sequence.to_be_bytes(),

0 commit comments

Comments
 (0)