14
14
15
15
use chrono:: prelude:: { DateTime , Utc } ;
16
16
use chrono:: Duration ;
17
+ use p2p:: Capabilities ;
17
18
use rand:: prelude:: * ;
18
19
use std:: cmp;
19
20
use std:: sync:: Arc ;
@@ -71,6 +72,11 @@ impl BodySync {
71
72
Ok ( false )
72
73
}
73
74
75
+ /// Is our local node running in archive_mode?
76
+ fn archive_mode ( & self ) -> bool {
77
+ self . chain . archive_mode ( )
78
+ }
79
+
74
80
/// Return true if txhashset download is needed (when requested block is under the horizon).
75
81
/// Otherwise go request some missing blocks and return false.
76
82
fn body_sync ( & mut self ) -> Result < bool , chain:: Error > {
@@ -85,26 +91,39 @@ impl BodySync {
85
91
return Ok ( true ) ;
86
92
}
87
93
88
- // Find connected peers with strictly greater difficulty than us.
89
- let peers_iter = || {
90
- self . peers
91
- . iter ( )
92
- . with_difficulty ( |x| x > head. total_difficulty )
93
- . connected ( )
94
- } ;
94
+ let peers = {
95
+ // Find connected peers with strictly greater difficulty than us.
96
+ let peers_iter = || {
97
+ // If we are running with archive mode enabled we only want to sync
98
+ // from other archive nodes.
99
+ let cap = if self . archive_mode ( ) {
100
+ Capabilities :: BLOCK_HIST
101
+ } else {
102
+ Capabilities :: UNKNOWN
103
+ } ;
104
+
105
+ self . peers
106
+ . iter ( )
107
+ . with_capabilities ( cap)
108
+ . with_difficulty ( |x| x > head. total_difficulty )
109
+ . connected ( )
110
+ } ;
111
+
112
+ // We prefer outbound peers with greater difficulty.
113
+ let mut peers: Vec < _ > = peers_iter ( ) . outbound ( ) . into_iter ( ) . collect ( ) ;
114
+ if peers. is_empty ( ) {
115
+ debug ! ( "no outbound peers with more work, considering inbound" ) ;
116
+ peers = peers_iter ( ) . inbound ( ) . into_iter ( ) . collect ( ) ;
117
+ }
95
118
96
- // We prefer outbound peers with greater difficulty.
97
- let mut peers: Vec < _ > = peers_iter ( ) . outbound ( ) . into_iter ( ) . collect ( ) ;
98
- if peers. is_empty ( ) {
99
- debug ! ( "no outbound peers with more work, considering inbound" ) ;
100
- peers = peers_iter ( ) . inbound ( ) . into_iter ( ) . collect ( ) ;
101
- }
119
+ // If we have no peers (outbound or inbound) then we are done for now.
120
+ if peers. is_empty ( ) {
121
+ debug ! ( "no peers (inbound or outbound) with more work" ) ;
122
+ return Ok ( false ) ;
123
+ }
102
124
103
- // If we have no peers (outbound or inbound) then we are done for now.
104
- if peers. is_empty ( ) {
105
- debug ! ( "no peers (inbound or outbound) with more work" ) ;
106
- return Ok ( false ) ;
107
- }
125
+ peers
126
+ } ;
108
127
109
128
// if we have 5 peers to sync from then ask for 50 blocks total (peer_count *
110
129
// 10) max will be 80 if all 8 peers are advertising more work
0 commit comments