Skip to content

Commit 9aaa027

Browse files
committed
smplx_test: add possibility to run network utils on tests
- add generate_blocks endpoint to ProviderTrait - add function to generate desired block height in test - add test for it
1 parent 6a6bef4 commit 9aaa027

6 files changed

Lines changed: 95 additions & 2 deletions

File tree

crates/sdk/src/provider/rpc/elements.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl ElementsRpc {
8383
Ok(())
8484
}
8585

86-
pub fn generate_blocks(&self, block_num: u32) -> Result<(), RpcError> {
86+
pub fn generate_blocks(&self, block_num: u64) -> Result<(), RpcError> {
8787
const METHOD: &str = "generatetoaddress";
8888

8989
let address = self.get_new_address("")?.to_string();
@@ -109,4 +109,12 @@ impl ElementsRpc {
109109

110110
Ok(())
111111
}
112+
113+
pub fn height(&self) -> Result<u64, RpcError> {
114+
const METHOD: &str = "getblockcount";
115+
self.inner
116+
.call::<serde_json::Value>(METHOD, &[])?
117+
.as_u64()
118+
.ok_or_else(|| RpcError::ElementsRpcUnexpectedReturn(METHOD.into()))
119+
}
112120
}

crates/test/src/context.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ use smplx_regtest::Regtest;
66
use smplx_regtest::client::RegtestClient;
77

88
use smplx_sdk::global::set_global_config;
9-
use smplx_sdk::provider::{EsploraProvider, ProviderInfo, ProviderTrait, SimplexProvider, SimplicityNetwork};
9+
use smplx_sdk::provider::{
10+
ElementsRpc, EsploraProvider, ProviderInfo, ProviderTrait, SimplexProvider, SimplicityNetwork,
11+
};
1012
use smplx_sdk::signer::Signer;
1113
use smplx_sdk::utils::random_mnemonic;
1214

1315
use crate::config::TestConfig;
1416
use crate::error::TestError;
17+
use crate::network_utils::NetworkUtils;
1518

1619
#[allow(dead_code)]
1720
pub struct TestContext {
@@ -85,6 +88,22 @@ impl TestContext {
8588
self.signer.get_provider().get_network()
8689
}
8790

91+
pub fn get_network_utils(&self) -> NetworkUtils {
92+
let network = self.get_network();
93+
assert!(
94+
matches!(network, SimplicityNetwork::ElementsRegtest { policy_asset: _ }),
95+
"Network utils only available in Regtest network"
96+
);
97+
98+
let regtest_rpc = ElementsRpc::new(
99+
self._provider_info.elements_url.clone().unwrap(),
100+
self._provider_info.auth.clone().unwrap(),
101+
)
102+
.expect("Failed to create rpc client for network utils");
103+
let esplora = EsploraProvider::new(self._provider_info.esplora_url.clone(), *network);
104+
NetworkUtils::from_context(regtest_rpc, esplora)
105+
}
106+
88107
fn setup(config: &TestConfig) -> Result<(Signer, ProviderInfo, Option<RegtestClient>), TestError> {
89108
let client: Option<RegtestClient>;
90109
let provider_info: ProviderInfo;

crates/test/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use smplx_sdk::provider::ProviderError;
44

55
use smplx_regtest::error::RegtestError;
66

7+
use crate::network_utils::NetworkUtilsError;
8+
79
#[derive(thiserror::Error, Debug)]
810
pub enum TestError {
911
#[error(transparent)]
@@ -20,4 +22,7 @@ pub enum TestError {
2022

2123
#[error("Network name should either be `Liquid`, `LiquidTestnet` or `ElementsRegtest`, got: {0}")]
2224
BadNetworkName(String),
25+
26+
#[error("Occurred a network utils execution error: '{0}'")]
27+
NetworkUtilsExecution(#[from] NetworkUtilsError),
2328
}

crates/test/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ pub mod config;
22
pub mod context;
33
pub mod error;
44
pub mod macros;
5+
pub mod network_utils;
56

67
pub use config::{RpcConfig, TEST_ENV_NAME, TestConfig};
78
pub use macros::core::SMPLX_TEST_MARKER;
9+
pub use network_utils::NetworkUtils;

crates/test/src/network_utils.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use smplx_sdk::provider::{ElementsRpc, EsploraProvider, ProviderError, ProviderTrait};
2+
use smplx_sdk::signer::SignerError;
3+
4+
pub struct NetworkUtils {
5+
rpc: ElementsRpc,
6+
esplora: EsploraProvider,
7+
}
8+
9+
#[derive(thiserror::Error, Debug)]
10+
pub enum NetworkUtilsError {
11+
#[error(transparent)]
12+
Provider(#[from] ProviderError),
13+
#[error(transparent)]
14+
Signer(#[from] SignerError),
15+
}
16+
17+
impl NetworkUtils {
18+
pub fn from_context(rpc: ElementsRpc, esplora: EsploraProvider) -> Self {
19+
Self { rpc, esplora }
20+
}
21+
22+
pub fn mine_until_height(&self, target_height: u64) -> Result<(), NetworkUtilsError> {
23+
self._mine_until_height(target_height)
24+
}
25+
}
26+
27+
impl NetworkUtils {
28+
fn _mine_until_height(&self, target_height: u64) -> Result<(), NetworkUtilsError> {
29+
let current_height = self.rpc.height().map_err(ProviderError::from)?;
30+
if current_height < target_height {
31+
let blocks_to_mine = target_height - current_height;
32+
self.rpc.generate_blocks(blocks_to_mine).map_err(ProviderError::from)?;
33+
34+
for _ in 0..50 {
35+
let h = self.esplora.fetch_tip_height()? as u64;
36+
if h >= target_height {
37+
break;
38+
}
39+
std::thread::sleep(std::time::Duration::from_millis(100));
40+
}
41+
}
42+
43+
Ok(())
44+
}
45+
}

fixtures/tests/hack_tests.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[simplex::test(regtest)]
2+
fn test_blocks_mining(context: simplex::TestContext) -> anyhow::Result<()> {
3+
const DESIRED_HEIGHT: u64 = 1_234;
4+
5+
let network_utils = context.get_network_utils();
6+
network_utils.mine_until_height(DESIRED_HEIGHT)?;
7+
8+
assert_eq!(
9+
DESIRED_HEIGHT,
10+
context.get_default_provider().fetch_tip_height()? as u64
11+
);
12+
13+
Ok(())
14+
}

0 commit comments

Comments
 (0)