Skip to content

Commit ae186a6

Browse files
authored
feat: add some mocks for solana api and unit test (#3558)
Co-authored-by: wli-pro <wli-pro>
1 parent e20fa42 commit ae186a6

File tree

9 files changed

+318
-35
lines changed

9 files changed

+318
-35
lines changed

tee-worker/omni-executor/.env.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ OE_SENDGRID_FROM_NAME='Heima Dev Verify'
66
OE_PUMPX_API_BASE_URL=http://127.0.0.1:3456/pumpx
77
OE_BINANCE_API_KEY=binance_api_key
88
OE_BINANCE_API_SECRET=binance_api_secret
9-
OE_BINANCE_API_BASE_URL=http://127.0.0.1:3456/binance
9+
OE_BINANCE_API_BASE_URL=http://127.0.0.1:3456/binance
10+
OE_SOLANA_URL=http://127.0.0.1:3456/solana

tee-worker/omni-executor/Cargo.lock

Lines changed: 38 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tee-worker/omni-executor/accounting-contract-client/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ tracing = { workspace = true }
1717
[lints]
1818
workspace = true
1919

20+
[dev-dependencies]
21+
mock-server = { workspace = true }
22+
2023
[features]
2124
mocks = []

tee-worker/omni-executor/accounting-contract-client/src/solana.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,69 @@ pub mod mocks {
229229
}
230230
}
231231
}
232+
233+
#[cfg(test)]
234+
mod tests {
235+
use super::*;
236+
use sp_core::{ed25519, Pair};
237+
238+
#[tokio::test]
239+
async fn test_mock_server_execute_pay_out_request() {
240+
// Start mock server and get dynamic URL
241+
let mock_url = mock_server::async_run_test_only().await;
242+
let param_id = "D3S1ZTrFNkfeoHaLSTAjMXZVXnRJvsNnbwh9k5mRYqqV";
243+
244+
// has balance
245+
// address: AKnL4NNf3DGWZJS6cPknBuEGnVsV4A4m5tgebLHaRSZ9
246+
let seed = [1u8; 32];
247+
let pair = ed25519::Pair::from_seed(&seed);
248+
let client = AccountingContractClient::new(
249+
pair,
250+
format!("{}/solana", mock_url),
251+
param_id.to_string(),
252+
);
253+
254+
// Test execute_pay_out_request
255+
// base64 encoded tx string (sendTransaction):
256+
// AaV8xKPN0GfY3XVuTiQsxFPDOFlZhXhWwbcVMBEGd2kUTgid2Ss9GUNqJ1S01iTyvDKCWea8sRMwlmlZ1A8zDAEBAAMIiojj3XQJ8ZX9UtstPLpdcspnCb8dlBIb83SIAbQPb1zG8DcMUx1vBnO2Al4wQ7nCKjwgl/c5iVbUF2HhIwZJ3ez0N0UQ/aETrzeokAeTDaVjdVNHLlFhPu0cKQjhxBso7UkoxijRwsbq6QM4kFmVYSlZJzpcY/k2NsFGFKyHN9Hx9pMmCdAce72TK7SkfatcvRdHPxk+YN0BruCENR+3BgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAsuuu6Iwn1QSzfnYrRIa2f/i5FCzjnwDv32SN1j4fahTyq0V7Xk41kUInl1rH0tI9eWEbcpDkAbgz89/ZTaRNLMxJDpKM0uOHO7ND/JXaMxecpg9Nv0bCw26RKZ1V1Oa5AQYHAgEABwQDBRiE8aphpOG6LgEAAAAAAAAAAMqaOwAAAAA=
257+
let beneficiary_seed = [3u8; 32];
258+
let beneficiary_keypair =
259+
Keypair::from_seed(&beneficiary_seed).expect("Failed to create beneficiary keypair");
260+
let beneficiary = beneficiary_keypair.pubkey();
261+
let nonce = 1u64;
262+
let amount = U256::from(1000000000u64); // 1 SOL
263+
let result = client.execute_pay_out_request(beneficiary, nonce, amount).await;
264+
assert_eq!(result.is_ok(), true);
265+
}
266+
267+
#[tokio::test]
268+
async fn test_mock_server_get_balance() {
269+
// Start mock server and get dynamic URL
270+
let mock_url = mock_server::async_run_test_only().await;
271+
let param_id = "D3S1ZTrFNkfeoHaLSTAjMXZVXnRJvsNnbwh9k5mRYqqV";
272+
273+
// has balance
274+
// address: AKnL4NNf3DGWZJS6cPknBuEGnVsV4A4m5tgebLHaRSZ9
275+
let seed = [1u8; 32];
276+
let pair = ed25519::Pair::from_seed(&seed);
277+
let client = AccountingContractClient::new(
278+
pair,
279+
format!("{}/solana", mock_url),
280+
param_id.to_string(),
281+
);
282+
let balance = client.get_balance().await.expect("Fail to get balance");
283+
assert_eq!(balance, U256::from(1000000000u64));
284+
285+
// has no balance
286+
// address: 9hSR6S7WPtxmTojgo6GG3k4yDPecgJY292j7xrsUGWBu
287+
let seed = [2u8; 32];
288+
let pair = ed25519::Pair::from_seed(&seed);
289+
let client = AccountingContractClient::new(
290+
pair,
291+
format!("{}/solana", mock_url),
292+
param_id.to_string(),
293+
);
294+
let balance = client.get_balance().await.expect("Fail to get balance");
295+
assert_eq!(balance, U256::from(0));
296+
}
297+
}

tee-worker/omni-executor/mock-server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name = "mock-server"
44
version = "0.1.0"
55

66
[dependencies]
7-
env_logger = "0.10"
7+
env_logger = { workspace = true }
88
hex = { workspace = true, features = ["std"] }
99
log = { workspace = true, features = ["std"] }
1010
serde_json = { workspace = true, features = ["std"] }

tee-worker/omni-executor/mock-server/src/lib.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use warp::Filter;
1414
mod binance;
1515
mod pumpx;
1616
mod sendgrid;
17+
mod solana;
1718

1819
// It should only works on UNIX.
1920
async fn shutdown_signal() {
@@ -32,19 +33,31 @@ async fn shutdown_signal() {
3233
log::info!("Shutdown signal received, stopping server...");
3334
}
3435

35-
pub fn run(port: u16) -> Result<String, Box<dyn std::error::Error>> {
36+
pub fn run(port: u16) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
37+
run_with_shutdown_control(port, true)
38+
}
39+
40+
pub fn run_with_shutdown_control(
41+
port: u16,
42+
wait_for_shutdown: bool,
43+
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
3644
let (result_in, result_out) = channel();
3745
let (shutdown_in, shutdown_out) = channel();
3846

3947
thread::spawn(move || {
4048
let runtime = Builder::new_current_thread().enable_all().build().unwrap();
4149
LocalSet::new().block_on(&runtime, async {
42-
let (addr, srv) =
43-
warp::serve(binance::handle().or(pumpx::handle()).or(sendgrid::handle()).boxed())
44-
.bind_with_graceful_shutdown(([0, 0, 0, 0], port), async {
45-
shutdown_signal().await;
46-
let _ = shutdown_in.send(());
47-
});
50+
let (addr, srv) = warp::serve(
51+
binance::handle()
52+
.or(pumpx::handle())
53+
.or(sendgrid::handle())
54+
.or(solana::handle())
55+
.boxed(),
56+
)
57+
.bind_with_graceful_shutdown(([0, 0, 0, 0], port), async {
58+
shutdown_signal().await;
59+
let _ = shutdown_in.send(());
60+
});
4861

4962
log::info!("mock-server listen on addr:{:?}", addr);
5063
let _ = result_in.send(format!("http://{:?}", addr));
@@ -57,7 +70,19 @@ pub fn run(port: u16) -> Result<String, Box<dyn std::error::Error>> {
5770

5871
let url = result_out.blocking_recv()?;
5972

60-
let _ = shutdown_out.blocking_recv();
73+
if wait_for_shutdown {
74+
let _ = shutdown_out.blocking_recv();
75+
}
6176

6277
Ok(url)
6378
}
79+
80+
/// Helper function to start a mock server for testing purposes.
81+
/// Returns the server URL as a String.
82+
/// The server will be started on a random available port.
83+
pub async fn async_run_test_only() -> String {
84+
tokio::task::spawn_blocking(move || run_with_shutdown_control(0, false))
85+
.await
86+
.expect("Fail to start mock server")
87+
.expect("Failed to get server URL")
88+
}

0 commit comments

Comments
 (0)