Skip to content

Commit a6878c8

Browse files
committed
Compatibility with Bitcoin Core v0.21
- Uses the new `connections_in` field to determine if we have inbound connections, instead of checking the list of peers via `listpeers`. This also avoids using the `listpeers` command for v0.21+, which isn't yet compatible in rust-bitcoincore-rpc, pending the acceptance of rust-bitcoin/rust-bitcoincore-rpc#158. - Identify the updated message for "wallet already exists" errors. - Update CI to run tests against v0.21
1 parent c61e3f2 commit a6878c8

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Compatibility with Bitcoin Core v0.21
6+
57
- New `create_wallet_if_missing` option to ease the creation of a designated bitcoind wallet (#76)
68

79
## 0.2.1 - 2021-01-14

scripts/ci-test-deps.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/bin/bash
22
set -eo pipefail
33

4-
BITCOIN_VERSION=0.20.0
4+
BITCOIN_VERSION=0.21.0
55
BITCOIN_FILENAME=bitcoin-$BITCOIN_VERSION-x86_64-linux-gnu.tar.gz
66
BITCOIN_URL=https://bitcoincore.org/bin/bitcoin-core-$BITCOIN_VERSION/$BITCOIN_FILENAME
7-
BITCOIN_SHA256=35ec10f87b6bc1e44fd9cd1157e5dfa483eaf14d7d9a9c274774539e7824c427
7+
BITCOIN_SHA256=da7766775e3f9c98d7a9145429f2be8297c2672fe5b118fd3dc2411fb48e0032
88

99
ELECTRUM_URL=https://download.electrum.org/4.0.3/electrum-4.0.3-x86_64.AppImage
1010
ELECTRUM_SHA256=512b58c437847048a9629cb6bf2eb786b8969a1e17b7b51510b11672c9b29fc7

scripts/setup-env.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ walletnotify=nc -U $BWT_SOCKET > /dev/null 2>&1
7272
7373
[regtest]
7474
rpcport=$BTC_RPC_PORT
75-
wallet=internal
7675
EOL
7776

7877
bitcoind -datadir=$BTC_DIR $BTC_OPTS &
7978

8079
echo - Waiting for bitcoind to warm up...
8180
btc -rpcwait getblockchaininfo > /dev/null
82-
echo - Creating watch-only wallet...
83-
btc createwallet bwt true > /dev/null
81+
82+
echo - Creating wallets...
83+
btc createwallet internal > /dev/null
84+
btc createwallet bwt true true > /dev/null
8485

8586
echo - Generating some blocks...
8687
btc generatetoaddress 110 `btc getnewaddress` > /dev/null

src/app.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,18 @@ impl App {
220220
// and create the wallet if the `create_wallet_if_missing` option was set
221221
fn load_wallet(rpc: &RpcClient, name: &str, create_if_missing: bool) -> Result<()> {
222222
use crate::util::bitcoincore_ext::{RPC_WALLET_ERROR, RPC_WALLET_NOT_FOUND};
223-
// needs to be identified by the error message string because it uses a generic wallet error code
224-
const MSG_ALREADY_LOADED_SUFF: &str = "Duplicate -wallet filename specified.";
223+
// Needs to be identified by the error message string because it uses a generic wallet error code.
224+
// The error message changed in Bitcoin Core v0.21.
225+
const MSG_ALREADY_LOADED_OLD: &str = "Duplicate -wallet filename specified.";
226+
const MSG_ALREADY_LOADED_NEW: &str = "is already loaded.";
225227

226228
match rpc.load_wallet(name) {
227229
Ok(_) => Ok(()),
228230
// Wallet already loaded
229231
Err(rpc::Error::JsonRpc(rpc::jsonrpc::Error::Rpc(ref e)))
230-
if e.code == RPC_WALLET_ERROR && e.message.ends_with(MSG_ALREADY_LOADED_SUFF) =>
232+
if e.code == RPC_WALLET_ERROR
233+
&& (e.message.ends_with(MSG_ALREADY_LOADED_OLD)
234+
|| e.message.ends_with(MSG_ALREADY_LOADED_NEW)) =>
231235
{
232236
Ok(())
233237
}

src/util/banner.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ const HALVING_INTERVAL: u64 = 210_000;
1616
pub fn get_welcome_banner(query: &Query, omit_donation: bool) -> Result<String> {
1717
let rpc = query.rpc();
1818

19-
let net_info = rpc.get_network_info()?;
19+
let net_info = rpc.get_network_info_()?;
2020
let chain_info = rpc.get_blockchain_info()?;
2121
let mempool_info = rpc.get_mempool_info()?;
2222
let net_totals = rpc.get_net_totals()?;
23-
let peers = rpc.get_peer_info()?;
2423
let hash_rate_7d = rpc.get_network_hash_ps(Some(1008), None)?;
2524
let uptime = dur_from_secs(rpc.uptime()?);
2625
let tip = rpc.get_block_stats(&rpc.get_best_block_hash()?)?;
2726

27+
// Bitcoin Core v0.21 has a `connections_in` field. For older version,
28+
// retreive the list of peers and check if there are any inbound ones.
29+
let has_inbound = net_info.connections_in.map_or_else(
30+
|| -> Result<bool> { Ok(rpc.get_peer_info()?.iter().any(|p| p.inbound)) },
31+
|connections_in| Ok(connections_in > 0),
32+
)?;
33+
2834
let est_fee = |target| {
2935
query
3036
.estimate_fee(target)
@@ -65,8 +71,6 @@ pub fn get_welcome_banner(query: &Query, omit_donation: bool) -> Result<String>
6571
// sat/kb -> sat/vB
6672
let mempool_min_fee = mempool_info.mempool_min_fee.as_sat() as f64 / 1000f64;
6773

68-
let has_inbound = peers.iter().any(|p| p.inbound);
69-
7074
let modes = [
7175
if chain_info.pruned {
7276
"✂️ ᴘʀᴜɴᴇᴅ"

src/util/bitcoincore_ext.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ pub const RPC_IN_WARMUP: i32 = -28;
1616
pub const RPC_METHOD_NOT_FOUND: i32 = -32601;
1717

1818
pub trait RpcApiExt: RpcApi {
19+
// Pending https://github.com/rust-bitcoin/rust-bitcoincore-rpc/pull/158
20+
fn get_network_info_(&self) -> RpcResult<GetNetworkInfoResult> {
21+
self.call("getnetworkinfo", &[])
22+
}
23+
1924
fn list_labels(&self) -> RpcResult<Vec<String>> {
2025
self.call("listlabels", &[])
2126
}
@@ -86,6 +91,41 @@ pub struct GetMempoolInfoResult {
8691
pub mempool_min_fee: bitcoin::Amount,
8792
}
8893

94+
// Pending https://github.com/rust-bitcoin/rust-bitcoincore-rpc/pull/158
95+
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
96+
pub struct GetNetworkInfoResult {
97+
pub version: usize,
98+
pub subversion: String,
99+
#[serde(rename = "protocolversion")]
100+
pub protocol_version: usize,
101+
#[serde(rename = "localservices")]
102+
pub local_services: String,
103+
#[serde(rename = "localrelay")]
104+
pub local_relay: bool,
105+
#[serde(rename = "timeoffset")]
106+
pub time_offset: isize,
107+
pub connections: usize,
108+
/// The number of inbound connections
109+
/// Added in Bitcoin Core v0.21
110+
pub connections_in: Option<usize>,
111+
/// The number of outbound connections
112+
/// Added in Bitcoin Core v0.21
113+
pub connectiosn_out: Option<usize>,
114+
#[serde(rename = "networkactive")]
115+
pub network_active: bool,
116+
pub networks: Vec<json::GetNetworkInfoResultNetwork>,
117+
#[serde(rename = "relayfee", with = "bitcoin::util::amount::serde::as_btc")]
118+
pub relay_fee: bitcoin::Amount,
119+
#[serde(
120+
rename = "incrementalfee",
121+
with = "bitcoin::util::amount::serde::as_btc"
122+
)]
123+
pub incremental_fee: bitcoin::Amount,
124+
#[serde(rename = "localaddresses")]
125+
pub local_addresses: Vec<json::GetNetworkInfoResultAddress>,
126+
pub warnings: String,
127+
}
128+
89129
// Wrap rust-bitcoincore-rpc's RescanSince to enable deserialization
90130
// Pending https://github.com/rust-bitcoin/rust-bitcoincore-rpc/pull/150
91131
// XXX The PR does not include null handling

0 commit comments

Comments
 (0)