Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 4aff188

Browse files
committed
Implement into_model
For all `json` types implement an inherent `into_model` function and remove the `TryInto` implementations. Done to improve ergonomics. Patch includes a bunch of other cleanups and improvements on the way.
1 parent 458264b commit 4aff188

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1426
-1387
lines changed

client/src/client_sync/error.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use bitcoin::{hex, secp256k1};
88
#[derive(Debug)]
99
pub enum Error {
1010
JsonRpc(jsonrpc::error::Error),
11-
Hex(hex::HexToBytesError),
11+
HexToArray(hex::HexToArrayError),
12+
HexToBytes(hex::HexToBytesError),
1213
Json(serde_json::error::Error),
1314
BitcoinSerialization(bitcoin::consensus::encode::FromHexError),
1415
Secp256k1(secp256k1::Error),
@@ -29,8 +30,12 @@ impl From<jsonrpc::error::Error> for Error {
2930
fn from(e: jsonrpc::error::Error) -> Error { Error::JsonRpc(e) }
3031
}
3132

33+
impl From<hex::HexToArrayError> for Error {
34+
fn from(e: hex::HexToArrayError) -> Self { Self::HexToArray(e) }
35+
}
36+
3237
impl From<hex::HexToBytesError> for Error {
33-
fn from(e: hex::HexToBytesError) -> Error { Error::Hex(e) }
38+
fn from(e: hex::HexToBytesError) -> Self { Self::HexToBytes(e) }
3439
}
3540

3641
impl From<serde_json::error::Error> for Error {
@@ -59,7 +64,8 @@ impl fmt::Display for Error {
5964

6065
match *self {
6166
JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
62-
Hex(ref e) => write!(f, "hex decode error: {}", e),
67+
HexToArray(ref e) => write!(f, "hex to array decode error: {}", e),
68+
HexToBytes(ref e) => write!(f, "hex to bytes decode error: {}", e),
6369
Json(ref e) => write!(f, "JSON error: {}", e),
6470
BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
6571
Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e),
@@ -80,7 +86,8 @@ impl error::Error for Error {
8086

8187
match *self {
8288
JsonRpc(ref e) => Some(e),
83-
Hex(ref e) => Some(e),
89+
HexToArray(ref e) => Some(e),
90+
HexToBytes(ref e) => Some(e),
8491
Json(ref e) => Some(e),
8592
BitcoinSerialization(ref e) => Some(e),
8693
Secp256k1(ref e) => Some(e),

client/src/client_sync/v17/blockchain.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ macro_rules! impl_client_v17__getbestblockhash {
2929
/// Gets the blockhash of the current chain tip.
3030
pub fn best_block_hash(&self) -> Result<bitcoin::BlockHash> {
3131
let json = self.get_best_block_hash()?;
32-
let concrete: $crate::json::model::GetBestBlockHash = json.try_into().unwrap();
33-
Ok(concrete.0)
32+
Ok(json.block_hash()?)
3433
}
3534

3635
pub fn get_best_block_hash(&self) -> Result<GetBestBlockHash> {
@@ -48,8 +47,7 @@ macro_rules! impl_client_v17__getblock {
4847
/// Gets a block by blockhash.
4948
pub fn get_block(&self, hash: &BlockHash) -> Result<Block> {
5049
let json = self.get_block_verbosity_zero(hash)?;
51-
let concrete: $crate::json::model::GetBlockVerbosityZero = json.try_into()?;
52-
Ok(concrete.0)
50+
Ok(json.block()?)
5351
}
5452

5553
// FIXME(getblock): This handling of optional args is ugly as hell but because the returned json

client/src/client_sync/v17/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ crate::impl_client_v17__gettransaction!();
5353
#[serde(rename_all = "kebab-case")]
5454
pub enum AddressType {
5555
Legacy,
56-
P2ShSegwit,
56+
P2shSegwit,
5757
Bech32,
5858
}
5959

@@ -63,7 +63,7 @@ impl fmt::Display for AddressType {
6363

6464
let s = match *self {
6565
Legacy => "legacy",
66-
P2ShSegwit => "p2sh-segwit",
66+
P2shSegwit => "p2sh-segwit",
6767
Bech32 => "bech32",
6868
};
6969
fmt::Display::fmt(s, f)

client/src/client_sync/v17/wallet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ macro_rules! impl_client_v17__sendtoaddress {
102102
&self,
103103
address: &Address<NetworkChecked>,
104104
amount: Amount,
105-
) -> Result<bitcoin::Txid> {
105+
) -> Result<SendToAddress> {
106106
let mut args = [address.to_string().into(), into_json(amount.to_btc())?];
107107
self.call("sendtoaddress", handle_defaults(&mut args, &["".into(), "".into()]))
108108
}

client/src/client_sync/v23.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ crate::impl_client_v17__gettransaction!();
4747
#[serde(rename_all = "kebab-case")]
4848
pub enum AddressType {
4949
Legacy,
50-
P2ShSegwit,
50+
P2shSegwit,
5151
Bech32,
5252
Bech32m,
5353
}
@@ -58,7 +58,7 @@ impl fmt::Display for AddressType {
5858

5959
let s = match *self {
6060
Legacy => "legacy",
61-
P2ShSegwit => "p2sh-segwit",
61+
P2shSegwit => "p2sh-segwit",
6262
Bech32 => "bech32",
6363
Bech32m => "bech32m",
6464
};

integration_test/src/v17/blockchain.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ macro_rules! impl_test_v17__getblockchaininfo {
1212
#[test]
1313
fn get_blockchain_info() {
1414
let bitcoind = $crate::bitcoind_no_wallet();
15-
let _ = bitcoind.client.get_blockchain_info().expect("getblockchaininfo");
15+
let json = bitcoind.client.get_blockchain_info().expect("getblockchaininfo");
16+
assert!(json.into_model().is_ok());
1617
}
1718
};
1819
}
@@ -29,24 +30,53 @@ macro_rules! impl_test_v17__getbestblockhash {
2930
#[test]
3031
fn get_best_block_hash() {
3132
let bitcoind = $crate::bitcoind_no_wallet();
32-
let _ = bitcoind.client.get_best_block_hash().expect("getbestblockhash");
33+
let json = bitcoind.client.get_best_block_hash().expect("getbestblockhash");
34+
assert!(json.into_model().is_ok());
35+
}
36+
};
37+
}
38+
39+
/// Requires `Client` to be in scope and to implement `get_block 0`.
40+
#[macro_export]
41+
macro_rules! impl_test_v17__getblock_verbosity_0 {
42+
() => {
43+
#[test]
44+
fn get_block_verbosity_0() {
45+
let bitcoind = $crate::bitcoind_no_wallet();
46+
let block_hash = best_block_hash();
47+
48+
let json = bitcoind.client.get_block_verbosity_zero(&block_hash).expect("getblock 0");
49+
json.into_model().unwrap();
3350
}
3451
};
3552
}
3653

3754
/// Requires `Client` to be in scope and to implement `get_block`.
3855
#[macro_export]
39-
macro_rules! impl_test_v17__getblock {
56+
macro_rules! impl_test_v17__getblock_verbosity_1 {
57+
() => {
58+
#[test]
59+
fn get_block_verbosity_1() {
60+
let bitcoind = $crate::bitcoind_no_wallet();
61+
let block_hash = best_block_hash();
62+
63+
let json = bitcoind.client.get_block_verbosity_one(&block_hash).expect("getblock 1");
64+
json.into_model().unwrap();
65+
}
66+
};
67+
}
68+
69+
/// Requires `Client` to be in scope and to implement `get_block 2`.
70+
#[macro_export]
71+
macro_rules! impl_test_v17__getblock_verbosity_2 {
4072
() => {
4173
#[test]
42-
fn get_block() {
74+
fn get_block_verbosity_2() {
4375
let bitcoind = $crate::bitcoind_no_wallet();
4476
let block_hash = best_block_hash();
4577

46-
let _ = bitcoind.client.get_block_verbosity_zero(&block_hash).expect("getblock 0");
47-
let _ = bitcoind.client.get_block_verbosity_one(&block_hash).expect("getblock 1");
48-
// TODO: getblock 2
49-
// let json = client.get_block_verbosity_two(&block_hash).expect("getblock 2");
78+
let json = client.get_block_verbosity_two(&block_hash).expect("getblock 2");
79+
json.into_model().unwrap();
5080
}
5181
};
5282
}

integration_test/src/v17/control.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ macro_rules! impl_test_v17__stop {
1212
#[test]
1313
fn stop() {
1414
let bitcoind = $crate::bitcoind_no_wallet();
15+
// There is no json object for `stop`, we just return a string.
1516
let _ = bitcoind.client.stop().expect("stop");
1617
}
1718
};

integration_test/src/v17/generating.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ macro_rules! impl_test_v17__generatetoaddress {
1313
fn generate_to_address() {
1414
let bitcoind = $crate::bitcoind_with_default_wallet();
1515
let address = bitcoind.client.new_address().expect("failed to get new address");
16-
let _ = bitcoind.client.generate_to_address(1, &address).expect("generatetoaddress");
16+
let json = bitcoind.client.generate_to_address(1, &address).expect("generatetoaddress");
17+
json.into_model().unwrap();
1718
}
1819
};
1920
}

integration_test/src/v17/network.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ macro_rules! impl_test_v17__getnetworkinfo {
1212
#[test]
1313
fn get_network_info() {
1414
let bitcoind = $crate::bitcoind_no_wallet();
15-
let _ = bitcoind.client.get_network_info().expect("getnetworkinfo");
15+
let json = bitcoind.client.get_network_info().expect("getnetworkinfo");
16+
json.into_model().unwrap();
17+
1618
bitcoind.client.check_expected_server_version().expect("unexpected version");
1719
}
1820
};

integration_test/src/v17/wallet.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Macros for implementing test methods on a JSON-RPC client.
4+
//!
5+
//! Specifically this is methods found under the `== Wallet ==` section of the
6+
//! API docs of `bitcoind v0.17.1`.
7+
18
/// Requires `Client` to be in scope and to implement `createwallet`.
29
#[macro_export]
310
macro_rules! impl_test_v17__createwallet {
@@ -31,7 +38,8 @@ macro_rules! impl_test_v17__unloadwallet {
3138
let bitcoind = $crate::bitcoind_no_wallet();
3239
let wallet = format!("wallet-{}", rand::random::<u32>()).to_string();
3340
bitcoind.client.create_wallet(&wallet).expect("failed to create wallet");
34-
let _ = bitcoind.client.unload_wallet(&wallet).expect("unloadwallet");
41+
let json = bitcoind.client.unload_wallet(&wallet).expect("unloadwallet");
42+
assert!(json.into_model().is_ok())
3543
}
3644
};
3745
}
@@ -45,13 +53,26 @@ macro_rules! impl_test_v17__getnewaddress {
4553
use bitcoind::AddressType;
4654

4755
let bitcoind = $crate::bitcoind_with_default_wallet();
48-
let _ = bitcoind.client.get_new_address().expect("getnewaddress");
4956

50-
let addr = bitcoind
57+
let json = bitcoind.client.get_new_address().expect("getnewaddress");
58+
assert!(json.into_model().is_ok());
59+
60+
// Test the helper as well just for good measure.
61+
let _ = bitcoind.client.new_address().unwrap();
62+
63+
// Exhaustively test address types with helper.
64+
let _ = bitcoind
65+
.client
66+
.new_address_with_type(AddressType::Legacy)
67+
.unwrap();
68+
let _ = bitcoind
69+
.client
70+
.new_address_with_type(AddressType::P2shSegwit)
71+
.unwrap();
72+
let _ = bitcoind
5173
.client
5274
.new_address_with_type(AddressType::Bech32)
5375
.unwrap();
54-
5576
}
5677
};
5778
}
@@ -66,7 +87,7 @@ macro_rules! impl_test_v17__getbalance {
6687

6788
let bitcoind = $crate::bitcoind_with_default_wallet();
6889
let json = bitcoind.client.get_balance().expect("getbalance");
69-
let _: model::GetBalance = json.try_into().unwrap();
90+
assert!(json.into_model().is_ok())
7091
}
7192
};
7293
}
@@ -85,10 +106,11 @@ macro_rules! impl_test_v17__sendtoaddress {
85106
let address = bitcoind.client.new_address().expect("failed to create new address");
86107
let _ = bitcoind.client.generate_to_address(101, &address).expect("generatetoaddress");
87108

88-
let _ = bitcoind
109+
let json = bitcoind
89110
.client
90111
.send_to_address(&address, Amount::from_sat(10_000))
91-
.expect("sendtoaddress");
112+
.expect("sendtddress");
113+
json.into_model().unwrap();
92114
}
93115
};
94116
}
@@ -112,10 +134,12 @@ macro_rules! impl_test_v17__gettransaction {
112134
let txid = bitcoind
113135
.client
114136
.send_to_address(&address, Amount::from_sat(10_000))
115-
.expect("sendtoaddress");
137+
.expect("sendtoaddress")
138+
.txid()
139+
.unwrap();
116140

117141
let json = bitcoind.client.get_transaction(txid).expect("gettransaction");
118-
let _: model::GetTransaction = json.try_into().unwrap();
142+
json.into_model().unwrap();
119143
}
120144
};
121145
}

integration_test/src/v19/wallet.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ macro_rules! impl_test_v19__getbalances {
77
let bitcoind = $crate::bitcoind_with_default_wallet();
88
let address = bitcoind.client.new_address().expect("failed to get new address");
99
let _ = bitcoind.client.generate_to_address(101, &address).expect("generatetoaddress");
10-
let _ = bitcoind.client.get_balances().expect("getbalances");
10+
let json = bitcoind.client.get_balances().expect("getbalances");
11+
json.into_model().unwrap();
1112
}
1213
};
1314
}

integration_test/src/v22/blockchain.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

integration_test/src/v22/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
//! Macros for implementing test methods on a JSON-RPC client for `bitcoind v22.1`.
44
5-
pub mod blockchain;
65
pub mod wallet;

integration_test/src/v22/wallet.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Macros for implementing test methods on a JSON-RPC client.
4+
//!
5+
//! Specifically this is methods found under the `== Wallet ==` section of the
6+
//! API docs of `bitcoind v22.1`.
7+
18
/// Requires `Client` to be in scope and to implement `unloadwallet`.
29
#[macro_export]
310
macro_rules! impl_test_v22__unloadwallet {

integration_test/tests/v17_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ mod blockchain {
1010

1111
impl_test_v17__getblockchaininfo!();
1212
impl_test_v17__getbestblockhash!();
13-
impl_test_v17__getblock!();
14-
// impl_test_v22__gettxout!();
13+
impl_test_v17__getblock_verbosity_0!();
14+
impl_test_v17__getblock_verbosity_1!();
1515
}
1616

1717
// == Control ==

integration_test/tests/v18_api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ mod blockchain {
1010

1111
impl_test_v17__getblockchaininfo!();
1212
impl_test_v17__getbestblockhash!();
13-
impl_test_v17__getblock!();
13+
impl_test_v17__getblock_verbosity_0!();
14+
impl_test_v17__getblock_verbosity_1!();
1415
}
1516

1617
// == Control ==

integration_test/tests/v19_api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ mod blockchain {
1010

1111
impl_test_v17__getblockchaininfo!();
1212
impl_test_v17__getbestblockhash!();
13-
impl_test_v17__getblock!();
13+
impl_test_v17__getblock_verbosity_0!();
14+
impl_test_v17__getblock_verbosity_1!();
1415
}
1516

1617
// == Control ==

integration_test/tests/v20_api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ mod blockchain {
1010

1111
impl_test_v17__getblockchaininfo!();
1212
impl_test_v17__getbestblockhash!();
13-
impl_test_v17__getblock!();
13+
impl_test_v17__getblock_verbosity_0!();
14+
impl_test_v17__getblock_verbosity_1!();
1415
}
1516

1617
// == Control ==

0 commit comments

Comments
 (0)