Skip to content

Commit

Permalink
feat: Bitcoin support in PocketIC (#3989)
Browse files Browse the repository at this point in the history
* feat: Bitcoin support in PocketIC

* windows

---------

Co-authored-by: Linwei Shang <linwei.shang@dfinity.org>
  • Loading branch information
mraszyk and lwshang authored Nov 14, 2024
1 parent bc91ffe commit 87a27ec
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# UNRELEASED

### feat: Bitcoin support in PocketIC

`dfx start --pocketic` is now compatible with `--bitcoin-node` and `--enable-bitcoin`.

### feat: all commands will use the DFX_NETWORK from the environment

If `DFX_NETWORK` is set in the environment, all commands will use that network by default.
Expand Down
7 changes: 7 additions & 0 deletions e2e/tests-dfx/bitcoin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set_local_network_bitcoin_enabled() {
}

@test "dfx restarts replica when ic-btc-adapter restarts" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: PocketIC does not expose bitcoin adapter"
dfx_new_assets hello
dfx_start --enable-bitcoin

Expand Down Expand Up @@ -86,13 +87,15 @@ set_local_network_bitcoin_enabled() {
}

@test "dfx start --bitcoin-node <node> implies --enable-bitcoin" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: PocketIC does not expose bitcoin adapter"
dfx_new hello
dfx_start "--bitcoin-node" "127.0.0.1:18444"

assert_file_not_empty "$E2E_SHARED_LOCAL_NETWORK_DATA_DIRECTORY/ic-btc-adapter-pid"
}

@test "dfx start --enable-bitcoin with no other configuration succeeds" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: PocketIC does not expose bitcoin adapter"
dfx_new hello

dfx_start --enable-bitcoin
Expand All @@ -111,6 +114,7 @@ set_local_network_bitcoin_enabled() {
}

@test "can enable bitcoin through default configuration - dfx start" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: PocketIC does not expose bitcoin adapter"
dfx_new hello
define_project_network
set_project_default_bitcoin_enabled
Expand All @@ -121,6 +125,7 @@ set_local_network_bitcoin_enabled() {
}

@test "can enable bitcoin through shared local network - dfx start" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: PocketIC does not expose bitcoin adapter"
dfx_new hello
set_shared_local_network_bitcoin_enabled

Expand All @@ -130,6 +135,7 @@ set_local_network_bitcoin_enabled() {
}

@test "can enable bitcoin through local network configuration - dfx start" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: PocketIC does not expose bitcoin adapter"
dfx_new hello
set_local_network_bitcoin_enabled

Expand All @@ -139,6 +145,7 @@ set_local_network_bitcoin_enabled() {
}

@test "dfx start with both bitcoin and canister http enabled" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: PocketIC does not expose bitcoin adapter"
dfx_new hello

dfx_start --enable-bitcoin --enable-canister-http
Expand Down
4 changes: 0 additions & 4 deletions scripts/workflows/e2e-matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ def test_scripts(prefix):
"backend": ["pocketic", "replica"],
"os": ["macos-13-large", "ubuntu-20.04"],
"exclude": [
{
"backend": "pocketic",
"test": "dfx/bitcoin"
},
{
"backend": "pocketic",
"test": "dfx/canister_http_adapter"
Expand Down
9 changes: 9 additions & 0 deletions src/dfx/src/actors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,19 @@ pub fn start_pocketic_actor(
)
})?;

let bitcoin_integration_config = if local_server_descriptor.bitcoin.enabled {
Some(BitcoinIntegrationConfig {
canister_init_arg: local_server_descriptor.bitcoin.canister_init_arg.clone(),
})
} else {
None
};
let actor_config = pocketic::Config {
pocketic_path,
effective_config_path: local_server_descriptor.effective_config_path(),
replica_config,
bitcoind_addr: local_server_descriptor.bitcoin.nodes.clone(),
bitcoin_integration_config,
port: local_server_descriptor.replica.port,
port_file: pocketic_port_path,
pid_file: local_server_descriptor.pocketic_pid_path(),
Expand Down
36 changes: 33 additions & 3 deletions src/dfx/src/actors/pocketic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ use crate::actors::shutdown::{wait_for_child_or_receiver, ChildOrReceiver};
use crate::actors::shutdown_controller::signals::outbound::Shutdown;
use crate::actors::shutdown_controller::signals::ShutdownSubscribe;
use crate::actors::shutdown_controller::ShutdownController;
use crate::actors::BitcoinIntegrationConfig;
use crate::lib::error::{DfxError, DfxResult};
#[cfg(unix)]
use crate::lib::info::replica_rev;
#[cfg(unix)]
use crate::lib::integrations::bitcoin::initialize_bitcoin_canister;
#[cfg(unix)]
use crate::lib::integrations::create_integrations_agent;
use actix::{
Actor, ActorContext, ActorFutureExt, Addr, AsyncContext, Context, Handler, Recipient,
ResponseActFuture, Running, WrapFuture,
Expand All @@ -20,6 +25,7 @@ use dfx_core::config::model::replica_config::ReplicaConfig;
#[cfg(unix)]
use dfx_core::json::save_json_file;
use slog::{debug, error, info, warn, Logger};
use std::net::SocketAddr;
use std::ops::ControlFlow::{self, *};
use std::path::{Path, PathBuf};
use std::thread::JoinHandle;
Expand All @@ -43,6 +49,8 @@ pub struct Config {
pub pocketic_path: PathBuf,
pub effective_config_path: PathBuf,
pub replica_config: ReplicaConfig,
pub bitcoind_addr: Option<Vec<SocketAddr>>,
pub bitcoin_integration_config: Option<BitcoinIntegrationConfig>,
pub port: Option<u16>,
pub port_file: PathBuf,
pub pid_file: PathBuf,
Expand Down Expand Up @@ -265,6 +273,8 @@ fn pocketic_start_thread(
let instance = match initialize_pocketic(
port,
&config.effective_config_path,
&config.bitcoind_addr,
&config.bitcoin_integration_config,
&config.replica_config,
logger.clone(),
) {
Expand Down Expand Up @@ -322,6 +332,8 @@ fn pocketic_start_thread(
async fn initialize_pocketic(
port: u16,
effective_config_path: &Path,
bitcoind_addr: &Option<Vec<SocketAddr>>,
bitcoin_integration_config: &Option<BitcoinIntegrationConfig>,
replica_config: &ReplicaConfig,
logger: Logger,
) -> DfxResult<usize> {
Expand All @@ -339,7 +351,7 @@ async fn initialize_pocketic(
sns: Some(SubnetSpec::default()),
ii: Some(SubnetSpec::default()),
fiduciary: None,
bitcoin: None,
bitcoin: Some(SubnetSpec::default()),
system: vec![],
verified_application: vec![],
application: vec![],
Expand All @@ -358,7 +370,7 @@ async fn initialize_pocketic(
state_dir: Some(replica_config.state_manager.state_root.clone()),
nonmainnet_features: true,
log_level: Some(replica_config.log_level.to_ic_starter_string()),
bitcoind_addr: None,
bitcoind_addr: bitcoind_addr.clone(),
})
.send()
.await?
Expand Down Expand Up @@ -420,12 +432,30 @@ async fn initialize_pocketic(
.send()
.await?
.error_for_status()?;

let agent_url = format!("http://localhost:{port}/instances/{instance}/");

debug!(logger, "Waiting for replica to report healthy status");
crate::lib::replica::status::ping_and_wait(&agent_url).await?;

if let Some(bitcoin_integration_config) = bitcoin_integration_config {
let agent = create_integrations_agent(&agent_url, &logger).await?;
initialize_bitcoin_canister(&agent, &logger, bitcoin_integration_config.clone()).await?;
}

info!(logger, "Initialized PocketIC.");
Ok(instance)
}

#[cfg(not(unix))]
fn initialize_pocketic(_: u16, _: &Path, _: &ReplicaConfig, _: Logger) -> DfxResult<usize> {
fn initialize_pocketic(
_: u16,
_: &Path,
_: &Option<Vec<SocketAddr>>,
_: &Option<BitcoinIntegrationConfig>,
_: &ReplicaConfig,
_: Logger,
) -> DfxResult<usize> {
bail!("PocketIC not supported on this platform")
}

Expand Down
4 changes: 2 additions & 2 deletions src/dfx/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ pub struct StartOpts {
clean: bool,

/// Address of bitcoind node. Implies --enable-bitcoin.
#[arg(long, action = ArgAction::Append, conflicts_with = "pocketic")]
#[arg(long, action = ArgAction::Append)]
bitcoin_node: Vec<SocketAddr>,

/// enable bitcoin integration
#[arg(long, conflicts_with = "pocketic")]
#[arg(long)]
enable_bitcoin: bool,

/// enable canister http requests (on by default for --pocketic)
Expand Down

0 comments on commit 87a27ec

Please sign in to comment.