Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ord env command #3146

Merged
merged 10 commits into from
Feb 20, 2024
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
Prev Previous commit
Next Next commit
Getting there!
  • Loading branch information
casey committed Feb 20, 2024
commit 9efad77a3ddc4fe382b237ad523cf5f200e3496f
21 changes: 15 additions & 6 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,21 @@ impl Options {
let client = Client::new(&rpc_url, auth)
.with_context(|| format!("failed to connect to Bitcoin Core RPC at {rpc_url}"))?;

let rpc_chain = match client.get_blockchain_info()?.chain.as_str() {
"main" => Chain::Mainnet,
"test" => Chain::Testnet,
"regtest" => Chain::Regtest,
"signet" => Chain::Signet,
other => bail!("Bitcoin RPC server on unknown chain: {other}"),
let rpc_chain = loop {
match client.get_blockchain_info() {
Ok(blockchain_info) => {
break match blockchain_info.chain.as_str() {
"main" => Chain::Mainnet,
"test" => Chain::Testnet,
"regtest" => Chain::Regtest,
"signet" => Chain::Signet,
other => bail!("Bitcoin RPC server on unknown chain: {other}"),
}
}
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err)))
if err.code == -28 => {}
Err(err) => bail!("Failed to connect to Bitcoin Core RPC at `{rpc_url}`: {err}"),
}
};

let ord_chain = self.chain();
Expand Down
27 changes: 20 additions & 7 deletions src/subcommand/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ rpcport={bitcoind_port}
.spawn()?,
);

eprintln!(
"example `bitcoin-cli` command: bitcoin-cli -datadir='{}' getblockchaininfo",
self.directory.display()
);

loop {
if env.join("regtest/.cookie").try_exists()? {
break;
Expand All @@ -78,6 +73,8 @@ rpcport={bitcoind_port}

let ord = std::env::current_exe()?;

let rpc_url = format!("http:https://localhost:{bitcoind_port}");

let _ord = KillOnDrop(
Command::new(&ord)
.arg("--regtest")
Expand All @@ -86,7 +83,7 @@ rpcport={bitcoind_port}
.arg("--data-dir")
.arg(&env)
.arg("--rpc-url")
.arg(format!("http:https://localhost:{bitcoind_port}"))
.arg(&rpc_url)
.arg("server")
.arg("--http-port")
.arg(ord_port.to_string())
Expand All @@ -101,14 +98,30 @@ rpcport={bitcoind_port}
.arg("--data-dir")
.arg(&env)
.arg("--rpc-url")
.arg(format!("http:https://localhost:{bitcoind_port}"))
.arg(&rpc_url)
.arg("wallet")
.arg("create")
.status()?;

ensure!(status.success(), "failed to create wallet: {status}");
}

eprintln!(
"==> env started in {}

example `bitcoin-cli` command:
bitcoin-cli -datadir='{}' getblockchaininfo

example `ord` command:
ord --regtest --bitcoin-data-dir '{}' --data-dir '{}' wallet --server-url '{}' balance
",
self.directory.display(),
self.directory.display(),
self.directory.display(),
self.directory.display(),
rpc_url,
);

loop {
if SHUTTING_DOWN.load(atomic::Ordering::Relaxed) {
break Ok(None);
Expand Down