Skip to content

Commit 83a6249

Browse files
committed
refactor!(test, db): move unit trait definitions into test module
As a side-effect, all integrations now accept a tempdir.
1 parent 6c3e1df commit 83a6249

File tree

3 files changed

+108
-105
lines changed

3 files changed

+108
-105
lines changed

src/db/error.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,3 @@ impl From<rusqlite::Error> for SqlHeaderStoreError {
154154
Self::SQL(value)
155155
}
156156
}
157-
158-
/// Errors for the [`PeerStore`](crate) of unit type.
159-
#[derive(Debug)]
160-
pub enum UnitPeerStoreError {
161-
/// There were no peers found.
162-
NoPeers,
163-
}
164-
165-
impl core::fmt::Display for UnitPeerStoreError {
166-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167-
match self {
168-
UnitPeerStoreError::NoPeers => write!(f, "no peers in unit database."),
169-
}
170-
}
171-
}

src/db/traits.rs

Lines changed: 94 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::fmt::Debug;
22
use std::ops::RangeBounds;
3-
use std::{collections::BTreeMap, convert::Infallible, fmt::Display};
3+
use std::{collections::BTreeMap, fmt::Display};
44

55
use bitcoin::{block::Header, BlockHash};
66

77
use crate::prelude::FutureResult;
88

9-
use super::error::UnitPeerStoreError;
109
use super::PersistedPeer;
1110

1211
/// Methods required to persist the chain of block headers.
@@ -45,65 +44,6 @@ pub trait HeaderStore: Debug + Send + Sync {
4544
fn header_at(&mut self, height: u32) -> FutureResult<Option<Header>, Self::Error>;
4645
}
4746

48-
/// This is a simple wrapper for the unit type, signifying that no headers will be stored between sessions.
49-
impl HeaderStore for () {
50-
type Error = Infallible;
51-
fn load<'a>(
52-
&'a mut self,
53-
_range: impl RangeBounds<u32> + Send + Sync + 'a,
54-
) -> FutureResult<'a, BTreeMap<u32, Header>, Self::Error> {
55-
async fn do_load() -> Result<BTreeMap<u32, Header>, Infallible> {
56-
Ok(BTreeMap::new())
57-
}
58-
Box::pin(do_load())
59-
}
60-
61-
fn write<'a>(
62-
&'a mut self,
63-
_header_chain: &'a BTreeMap<u32, Header>,
64-
) -> FutureResult<'a, (), Self::Error> {
65-
async fn do_write() -> Result<(), Infallible> {
66-
Ok(())
67-
}
68-
Box::pin(do_write())
69-
}
70-
71-
fn write_over<'a>(
72-
&'a mut self,
73-
_header_chain: &'a BTreeMap<u32, Header>,
74-
_height: u32,
75-
) -> FutureResult<'a, (), Self::Error> {
76-
async fn do_write_over() -> Result<(), Infallible> {
77-
Ok(())
78-
}
79-
Box::pin(do_write_over())
80-
}
81-
82-
fn height_of<'a>(
83-
&'a mut self,
84-
_block_hash: &'a BlockHash,
85-
) -> FutureResult<'a, Option<u32>, Self::Error> {
86-
async fn do_height_of() -> Result<Option<u32>, Infallible> {
87-
Ok(None)
88-
}
89-
Box::pin(do_height_of())
90-
}
91-
92-
fn hash_at(&mut self, _height: u32) -> FutureResult<Option<BlockHash>, Self::Error> {
93-
async fn do_hast_at() -> Result<Option<BlockHash>, Infallible> {
94-
Ok(None)
95-
}
96-
Box::pin(do_hast_at())
97-
}
98-
99-
fn header_at(&mut self, _height: u32) -> FutureResult<Option<Header>, Self::Error> {
100-
async fn do_header_at() -> Result<Option<Header>, Infallible> {
101-
Ok(None)
102-
}
103-
Box::pin(do_header_at())
104-
}
105-
}
106-
10747
/// Methods that define a list of peers on the Bitcoin P2P network.
10848
pub trait PeerStore: Debug + Send + Sync {
10949
/// Errors that may occur within a [`PeerStore`].
@@ -118,26 +58,105 @@ pub trait PeerStore: Debug + Send + Sync {
11858
fn num_unbanned(&mut self) -> FutureResult<u32, Self::Error>;
11959
}
12060

121-
impl PeerStore for () {
122-
type Error = UnitPeerStoreError;
123-
fn update(&mut self, _peer: PersistedPeer) -> FutureResult<(), Self::Error> {
124-
async fn do_update() -> Result<(), UnitPeerStoreError> {
125-
Ok(())
61+
#[cfg(test)]
62+
mod test {
63+
use super::*;
64+
use std::convert::Infallible;
65+
66+
/// Errors for the [`PeerStore`](crate) of unit type.
67+
#[derive(Debug)]
68+
pub enum UnitPeerStoreError {
69+
/// There were no peers found.
70+
NoPeers,
71+
}
72+
73+
impl core::fmt::Display for UnitPeerStoreError {
74+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75+
match self {
76+
UnitPeerStoreError::NoPeers => write!(f, "no peers in unit database."),
77+
}
12678
}
127-
Box::pin(do_update())
12879
}
12980

130-
fn random(&mut self) -> FutureResult<PersistedPeer, Self::Error> {
131-
async fn do_random() -> Result<PersistedPeer, UnitPeerStoreError> {
132-
Err(UnitPeerStoreError::NoPeers)
81+
impl PeerStore for () {
82+
type Error = UnitPeerStoreError;
83+
fn update(&mut self, _peer: PersistedPeer) -> FutureResult<(), Self::Error> {
84+
async fn do_update() -> Result<(), UnitPeerStoreError> {
85+
Ok(())
86+
}
87+
Box::pin(do_update())
88+
}
89+
90+
fn random(&mut self) -> FutureResult<PersistedPeer, Self::Error> {
91+
async fn do_random() -> Result<PersistedPeer, UnitPeerStoreError> {
92+
Err(UnitPeerStoreError::NoPeers)
93+
}
94+
Box::pin(do_random())
95+
}
96+
97+
fn num_unbanned(&mut self) -> FutureResult<u32, Self::Error> {
98+
async fn do_num_unbanned() -> Result<u32, UnitPeerStoreError> {
99+
Ok(0)
100+
}
101+
Box::pin(do_num_unbanned())
133102
}
134-
Box::pin(do_random())
135103
}
136104

137-
fn num_unbanned(&mut self) -> FutureResult<u32, Self::Error> {
138-
async fn do_num_unbanned() -> Result<u32, UnitPeerStoreError> {
139-
Ok(0)
105+
impl HeaderStore for () {
106+
type Error = Infallible;
107+
fn load<'a>(
108+
&'a mut self,
109+
_range: impl RangeBounds<u32> + Send + Sync + 'a,
110+
) -> FutureResult<'a, BTreeMap<u32, Header>, Self::Error> {
111+
async fn do_load() -> Result<BTreeMap<u32, Header>, Infallible> {
112+
Ok(BTreeMap::new())
113+
}
114+
Box::pin(do_load())
115+
}
116+
117+
fn write<'a>(
118+
&'a mut self,
119+
_header_chain: &'a BTreeMap<u32, Header>,
120+
) -> FutureResult<'a, (), Self::Error> {
121+
async fn do_write() -> Result<(), Infallible> {
122+
Ok(())
123+
}
124+
Box::pin(do_write())
125+
}
126+
127+
fn write_over<'a>(
128+
&'a mut self,
129+
_header_chain: &'a BTreeMap<u32, Header>,
130+
_height: u32,
131+
) -> FutureResult<'a, (), Self::Error> {
132+
async fn do_write_over() -> Result<(), Infallible> {
133+
Ok(())
134+
}
135+
Box::pin(do_write_over())
136+
}
137+
138+
fn height_of<'a>(
139+
&'a mut self,
140+
_block_hash: &'a BlockHash,
141+
) -> FutureResult<'a, Option<u32>, Self::Error> {
142+
async fn do_height_of() -> Result<Option<u32>, Infallible> {
143+
Ok(None)
144+
}
145+
Box::pin(do_height_of())
146+
}
147+
148+
fn hash_at(&mut self, _height: u32) -> FutureResult<Option<BlockHash>, Self::Error> {
149+
async fn do_hast_at() -> Result<Option<BlockHash>, Infallible> {
150+
Ok(None)
151+
}
152+
Box::pin(do_hast_at())
153+
}
154+
155+
fn header_at(&mut self, _height: u32) -> FutureResult<Option<Header>, Self::Error> {
156+
async fn do_header_at() -> Result<Option<Header>, Infallible> {
157+
Ok(None)
158+
}
159+
Box::pin(do_header_at())
140160
}
141-
Box::pin(do_num_unbanned())
142161
}
143162
}

tests/core.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ fn start_bitcoind(with_v2_transport: bool) -> anyhow::Result<(corepc_node::Node,
4040
Ok((bitcoind, socket_addr))
4141
}
4242

43-
fn new_node(addrs: HashSet<ScriptBuf>, socket_addr: SocketAddrV4) -> (Node<(), ()>, Client) {
44-
let host = (IpAddr::V4(*socket_addr.ip()), Some(socket_addr.port()));
45-
let builder = kyoto::core::builder::NodeBuilder::new(bitcoin::Network::Regtest);
46-
let (node, client) = builder
47-
.add_peer(host)
48-
.add_scripts(addrs)
49-
.build_with_databases((), ());
50-
(node, client)
51-
}
52-
5343
fn new_node_sql(
5444
addrs: HashSet<ScriptBuf>,
5545
socket_addr: SocketAddrV4,
@@ -153,6 +143,7 @@ async fn live_reorg() {
153143
}
154144
let (bitcoind, socket_addr) = rpc_result.unwrap();
155145
let rpc = &bitcoind.client;
146+
let tempdir = tempfile::TempDir::new().unwrap().into_path();
156147
// Mine some blocks
157148
let miner = rpc.new_address().unwrap();
158149
mine_blocks(rpc, &miner, 10, 1).await;
@@ -161,7 +152,7 @@ async fn live_reorg() {
161152
let mut scripts = HashSet::new();
162153
let other = rpc.new_address().unwrap();
163154
scripts.insert(other.into());
164-
let (node, client) = new_node(scripts.clone(), socket_addr);
155+
let (node, client) = new_node_sql(scripts.clone(), socket_addr, tempdir);
165156
tokio::task::spawn(async move { node.run().await });
166157
let Client {
167158
requester,
@@ -207,6 +198,7 @@ async fn live_reorg_additional_sync() {
207198
}
208199
let (bitcoind, socket_addr) = rpc_result.unwrap();
209200
let rpc = &bitcoind.client;
201+
let tempdir = tempfile::TempDir::new().unwrap().into_path();
210202
// Mine some blocks
211203
let miner = rpc.new_address().unwrap();
212204
mine_blocks(rpc, &miner, 10, 1).await;
@@ -215,7 +207,7 @@ async fn live_reorg_additional_sync() {
215207
let mut scripts = HashSet::new();
216208
let other = rpc.new_address().unwrap();
217209
scripts.insert(other.into());
218-
let (node, client) = new_node(scripts.clone(), socket_addr);
210+
let (node, client) = new_node_sql(scripts.clone(), socket_addr, tempdir);
219211
tokio::task::spawn(async move { node.run().await });
220212
let Client {
221213
requester,
@@ -265,14 +257,15 @@ async fn various_client_methods() {
265257
}
266258
let (bitcoind, socket_addr) = rpc_result.unwrap();
267259
let rpc = &bitcoind.client;
260+
let tempdir = tempfile::TempDir::new().unwrap().into_path();
268261
// Mine a lot of blocks
269262
let miner = rpc.new_address().unwrap();
270263
mine_blocks(rpc, &miner, 500, 15).await;
271264
let best = best_hash(rpc);
272265
let mut scripts = HashSet::new();
273266
let other = rpc.new_address().unwrap();
274267
scripts.insert(other.into());
275-
let (node, client) = new_node(scripts.clone(), socket_addr);
268+
let (node, client) = new_node_sql(scripts.clone(), socket_addr, tempdir);
276269
tokio::task::spawn(async move { node.run().await });
277270
let Client {
278271
requester,
@@ -589,6 +582,7 @@ async fn halting_download_works() {
589582
}
590583
let (bitcoind, socket_addr) = rpc_result.unwrap();
591584
let rpc = &bitcoind.client;
585+
let tempdir = tempfile::TempDir::new().unwrap().into_path();
592586

593587
let miner = rpc.new_address().unwrap();
594588
mine_blocks(rpc, &miner, 10, 1).await;
@@ -602,8 +596,10 @@ async fn halting_download_works() {
602596
let (node, client) = builder
603597
.add_peers(vec![host.into()])
604598
.add_scripts(scripts)
599+
.add_data_dir(tempdir)
605600
.halt_filter_download()
606-
.build_with_databases((), ());
601+
.build_node()
602+
.unwrap();
607603

608604
tokio::task::spawn(async move { node.run().await });
609605
let Client {
@@ -640,6 +636,7 @@ async fn halting_download_works() {
640636

641637
#[tokio::test]
642638
async fn signet_syncs() {
639+
let tempdir = tempfile::TempDir::new().unwrap().into_path();
643640
let address = bitcoin::Address::from_str("tb1q9pvjqz5u5sdgpatg3wn0ce438u5cyv85lly0pc")
644641
.unwrap()
645642
.require_network(bitcoin::Network::Signet)
@@ -652,7 +649,9 @@ async fn signet_syncs() {
652649
let (node, client) = builder
653650
.add_peer(host)
654651
.add_scripts(set)
655-
.build_with_databases((), ());
652+
.add_data_dir(tempdir)
653+
.build_node()
654+
.unwrap();
656655
tokio::task::spawn(async move { node.run().await });
657656
async fn print_and_sync(mut client: Client) {
658657
loop {

0 commit comments

Comments
 (0)