Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/sdk/src/provider/rpc/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ElementsRpc {
Ok(())
}

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

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

Ok(())
}

pub fn height(&self) -> Result<u64, RpcError> {
const METHOD: &str = "getblockcount";
self.inner
Comment thread
ikripaka marked this conversation as resolved.
.call::<serde_json::Value>(METHOD, &[])?
.as_u64()
.ok_or_else(|| RpcError::ElementsRpcUnexpectedReturn(METHOD.into()))
}
}
21 changes: 20 additions & 1 deletion crates/test/src/context.rs
Comment thread
Arvolear marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use smplx_regtest::Regtest;
use smplx_regtest::client::RegtestClient;

use smplx_sdk::global::set_global_config;
use smplx_sdk::provider::{EsploraProvider, ProviderInfo, ProviderTrait, SimplexProvider, SimplicityNetwork};
use smplx_sdk::provider::{
ElementsRpc, EsploraProvider, ProviderInfo, ProviderTrait, SimplexProvider, SimplicityNetwork,
};
use smplx_sdk::signer::Signer;
use smplx_sdk::utils::random_mnemonic;

use crate::config::TestConfig;
use crate::error::TestError;
use crate::network_utils::NetworkUtils;

#[allow(dead_code)]
pub struct TestContext {
Expand Down Expand Up @@ -85,6 +88,22 @@ impl TestContext {
self.signer.get_provider().get_network()
}

pub fn get_network_utils(&self) -> NetworkUtils {
let network = self.get_network();
assert!(
matches!(network, SimplicityNetwork::ElementsRegtest { policy_asset: _ }),
"Network utils only available in Regtest network"
);
Comment thread
ikripaka marked this conversation as resolved.
Outdated

let regtest_rpc = ElementsRpc::new(
self._provider_info.elements_url.clone().unwrap(),
self._provider_info.auth.clone().unwrap(),
)
.expect("Failed to create rpc client for network utils");
let esplora = EsploraProvider::new(self._provider_info.esplora_url.clone(), *network);
NetworkUtils::from_context(regtest_rpc, esplora)
Comment thread
ikripaka marked this conversation as resolved.
Outdated
}

fn setup(config: &TestConfig) -> Result<(Signer, ProviderInfo, Option<RegtestClient>), TestError> {
let client: Option<RegtestClient>;
let provider_info: ProviderInfo;
Expand Down
5 changes: 5 additions & 0 deletions crates/test/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use smplx_sdk::provider::ProviderError;

use smplx_regtest::error::RegtestError;

use crate::network_utils::NetworkUtilsError;

#[derive(thiserror::Error, Debug)]
pub enum TestError {
#[error(transparent)]
Expand All @@ -20,4 +22,7 @@ pub enum TestError {

#[error("Network name should either be `Liquid`, `LiquidTestnet` or `ElementsRegtest`, got: {0}")]
BadNetworkName(String),

#[error("Occurred a network utils execution error: '{0}'")]
NetworkUtilsExecution(#[from] NetworkUtilsError),
}
2 changes: 2 additions & 0 deletions crates/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod config;
pub mod context;
pub mod error;
pub mod macros;
pub mod network_utils;

pub use config::{RpcConfig, TEST_ENV_NAME, TestConfig};
pub use macros::core::SMPLX_TEST_MARKER;
pub use network_utils::NetworkUtils;
45 changes: 45 additions & 0 deletions crates/test/src/network_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use smplx_sdk::provider::{ElementsRpc, EsploraProvider, ProviderError, ProviderTrait};
use smplx_sdk::signer::SignerError;

pub struct NetworkUtils {
rpc: ElementsRpc,
esplora: EsploraProvider,
}

#[derive(thiserror::Error, Debug)]
pub enum NetworkUtilsError {
Comment thread
ikripaka marked this conversation as resolved.
Outdated
#[error(transparent)]
Provider(#[from] ProviderError),
#[error(transparent)]
Signer(#[from] SignerError),
Comment thread
ikripaka marked this conversation as resolved.
Outdated
}

impl NetworkUtils {
pub fn from_context(rpc: ElementsRpc, esplora: EsploraProvider) -> Self {
Comment thread
ikripaka marked this conversation as resolved.
Outdated
Self { rpc, esplora }
}

pub fn mine_until_height(&self, target_height: u64) -> Result<(), NetworkUtilsError> {
self._mine_until_height(target_height)
}
}

impl NetworkUtils {
fn _mine_until_height(&self, target_height: u64) -> Result<(), NetworkUtilsError> {
Comment thread
ikripaka marked this conversation as resolved.
Outdated
let current_height = self.rpc.height().map_err(ProviderError::from)?;
if current_height < target_height {
let blocks_to_mine = target_height - current_height;
self.rpc.generate_blocks(blocks_to_mine).map_err(ProviderError::from)?;

for _ in 0..50 {
let h = self.esplora.fetch_tip_height()? as u64;
if h >= target_height {
break;
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
}

Ok(())
}
Comment thread
ikripaka marked this conversation as resolved.
}
14 changes: 14 additions & 0 deletions fixtures/tests/hack_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[simplex::test(regtest)]
Comment thread
ikripaka marked this conversation as resolved.
Outdated
fn test_blocks_mining(context: simplex::TestContext) -> anyhow::Result<()> {
const DESIRED_HEIGHT: u64 = 1_234;

let network_utils = context.get_network_utils();
network_utils.mine_until_height(DESIRED_HEIGHT)?;

assert_eq!(
DESIRED_HEIGHT,
context.get_default_provider().fetch_tip_height()? as u64
);

Ok(())
}