11use std:: fmt:: Debug ;
22use std:: ops:: RangeBounds ;
3- use std:: { collections:: BTreeMap , convert :: Infallible , fmt:: Display } ;
3+ use std:: { collections:: BTreeMap , fmt:: Display } ;
44
55use bitcoin:: { block:: Header , BlockHash } ;
66
77use crate :: prelude:: FutureResult ;
88
9- use super :: error:: UnitPeerStoreError ;
109use 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.
10848pub 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}
0 commit comments