Skip to content

Commit

Permalink
Deploying to gh-pages from @ 8c51137 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Oct 27, 2024
1 parent 52b2b12 commit d222abc
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 169 deletions.
2 changes: 1 addition & 1 deletion src/subcoin_network/block_downloader.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@
<span class="kw">_ </span>=&gt; <span class="number">512</span>,
};

<span class="kw">let </span>queued_blocks = <span class="self">self</span>.best_queued_number - best_number;
<span class="kw">let </span>queued_blocks = <span class="self">self</span>.best_queued_number.saturating_sub(best_number);

<span class="kw">if </span>queued_blocks &gt; max_queued_blocks {
<span class="self">self</span>.queue_status = ImportQueueStatus::Overloaded;
Expand Down
14 changes: 3 additions & 11 deletions src/subcoin_network/block_downloader/blocks_first.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,6 @@
<a href="#513" id="513">513</a>
<a href="#514" id="514">514</a>
<a href="#515" id="515">515</a>
<a href="#516" id="516">516</a>
<a href="#517" id="517">517</a>
<a href="#518" id="518">518</a>
<a href="#519" id="519">519</a>
</pre></div><pre class="rust"><code><span class="kw">use </span><span class="kw">crate</span>::block_downloader::BlockDownloadManager;
<span class="kw">use </span><span class="kw">crate</span>::peer_store::PeerStore;
<span class="kw">use </span><span class="kw">crate</span>::sync::{LocatorRequest, SyncAction, SyncRequest};
Expand Down Expand Up @@ -620,11 +616,7 @@
}

<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>replaceable_sync_peer(<span class="kw-2">&amp;</span><span class="self">self</span>) -&gt; <span class="prelude-ty">Option</span>&lt;PeerId&gt; {
<span class="kw">if </span><span class="self">self</span>.downloaded_blocks_count &gt; <span class="number">0 </span>{
<span class="prelude-val">None
</span>} <span class="kw">else </span>{
<span class="prelude-val">Some</span>(<span class="self">self</span>.peer_id)
}
(<span class="self">self</span>.downloaded_blocks_count == <span class="number">0</span>).then_some(<span class="self">self</span>.peer_id)
}

<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>replace_sync_peer(<span class="kw-2">&amp;mut </span><span class="self">self</span>, peer_id: PeerId, target_block_number: u32) {
Expand Down Expand Up @@ -663,7 +655,7 @@

<span class="kw">if </span>best_number == <span class="self">self</span>.target_block_number {
<span class="self">self</span>.state = State::Completed;
<span class="kw">return </span>SyncAction::SwitchToIdle;
<span class="kw">return </span>SyncAction::SetIdle;
}

<span class="kw">if </span><span class="self">self</span>.download_manager.is_stalled(<span class="self">self</span>.peer_id) {
Expand Down Expand Up @@ -818,7 +810,7 @@
<span class="string">"Received block #{block_number},{block_hash} higher than the target block"
</span>);
<span class="self">self</span>.state = State::Completed;
SyncAction::SwitchToIdle
SyncAction::SetIdle
} <span class="kw">else </span>{
<span class="self">self</span>.state = State::Disconnecting;
SyncAction::Disconnect(
Expand Down
16 changes: 2 additions & 14 deletions src/subcoin_network/block_downloader/headers_first.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,6 @@
<a href="#688" id="688">688</a>
<a href="#689" id="689">689</a>
<a href="#690" id="690">690</a>
<a href="#691" id="691">691</a>
<a href="#692" id="692">692</a>
<a href="#693" id="693">693</a>
<a href="#694" id="694">694</a>
<a href="#695" id="695">695</a>
<a href="#696" id="696">696</a>
</pre></div><pre class="rust"><code><span class="kw">use </span><span class="kw">crate</span>::block_downloader::BlockDownloadManager;
<span class="kw">use </span><span class="kw">crate</span>::peer_store::PeerStore;
<span class="kw">use </span><span class="kw">crate</span>::sync::{LocatorRequest, SyncAction, SyncRequest};
Expand Down Expand Up @@ -824,9 +818,7 @@
downloaded_headers: DownloadedHeaders,
downloaded_blocks_count: usize,
last_locator_start: u32,
<span class="comment">// TODO: Now it's solely used for the purpose of displaying the sync state.
// refactor it later.
</span>target_block_number: u32,
target_block_number: u32,
_phantom: PhantomData&lt;Block&gt;,
}

Expand Down Expand Up @@ -876,11 +868,7 @@
}

<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>replaceable_sync_peer(<span class="kw-2">&amp;</span><span class="self">self</span>) -&gt; <span class="prelude-ty">Option</span>&lt;PeerId&gt; {
<span class="kw">if </span><span class="self">self</span>.downloaded_blocks_count &gt; <span class="number">0 </span>{
<span class="prelude-val">None
</span>} <span class="kw">else </span>{
<span class="prelude-val">Some</span>(<span class="self">self</span>.peer_id)
}
(<span class="self">self</span>.downloaded_blocks_count == <span class="number">0</span>).then_some(<span class="self">self</span>.peer_id)
}

<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>replace_sync_peer(<span class="kw-2">&amp;mut </span><span class="self">self</span>, peer_id: PeerId, target_block_number: u32) {
Expand Down
70 changes: 59 additions & 11 deletions src/subcoin_network/peer_manager.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,30 @@
<a href="#771" id="771">771</a>
<a href="#772" id="772">772</a>
<a href="#773" id="773">773</a>
<a href="#774" id="774">774</a>
<a href="#775" id="775">775</a>
<a href="#776" id="776">776</a>
<a href="#777" id="777">777</a>
<a href="#778" id="778">778</a>
<a href="#779" id="779">779</a>
<a href="#780" id="780">780</a>
<a href="#781" id="781">781</a>
<a href="#782" id="782">782</a>
<a href="#783" id="783">783</a>
<a href="#784" id="784">784</a>
<a href="#785" id="785">785</a>
<a href="#786" id="786">786</a>
<a href="#787" id="787">787</a>
<a href="#788" id="788">788</a>
<a href="#789" id="789">789</a>
<a href="#790" id="790">790</a>
<a href="#791" id="791">791</a>
<a href="#792" id="792">792</a>
<a href="#793" id="793">793</a>
<a href="#794" id="794">794</a>
<a href="#795" id="795">795</a>
<a href="#796" id="796">796</a>
<a href="#797" id="797">797</a>
</pre></div><pre class="rust"><code><span class="kw">use </span><span class="kw">crate</span>::address_book::AddressBook;
<span class="kw">use </span><span class="kw">crate</span>::connection::{ConnectionInitiator, ConnectionWriter, Direction, NewConnection};
<span class="kw">use </span><span class="kw">crate</span>::metrics::Metrics;
Expand Down Expand Up @@ -1088,7 +1112,7 @@
}
}

<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>on_tick(<span class="kw-2">&amp;mut </span><span class="self">self</span>) -&gt; <span class="prelude-ty">Option</span>&lt;SlowPeer&gt; {
<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>on_tick(<span class="kw-2">&amp;mut </span><span class="self">self</span>) -&gt; (Vec&lt;PeerId&gt;, <span class="prelude-ty">Option</span>&lt;SlowPeer&gt;) {
<span class="kw">let </span><span class="kw-2">mut </span>timeout_peers = <span class="macro">vec!</span>[];
<span class="kw">let </span><span class="kw-2">mut </span>should_ping_peers = <span class="macro">vec!</span>[];

Expand All @@ -1107,10 +1131,6 @@
<span class="self">self</span>.send_pings(should_ping_peers);
}

<span class="kw">for </span>peer_id <span class="kw">in </span>timeout_peers {
<span class="self">self</span>.disconnect(peer_id, Error::PingTimeout);
}

<span class="kw">let </span>outbound_peers_count = <span class="self">self
</span>.connected_peers
.values()
Expand All @@ -1133,6 +1153,13 @@
.set(<span class="self">self</span>.address_book.available_addresses_count() <span class="kw">as </span>u64);
}

<span class="kw">let </span>maybe_slow_peer = <span class="self">self</span>.manage_outbound_connections(outbound_peers_count);

(timeout_peers, maybe_slow_peer)
}

<span class="doccomment">/// Manages outbound connections by initiating new connections or evicting slow peers.
</span><span class="kw">fn </span>manage_outbound_connections(<span class="kw-2">&amp;mut </span><span class="self">self</span>, outbound_peers_count: usize) -&gt; <span class="prelude-ty">Option</span>&lt;SlowPeer&gt; {
<span class="kw">if </span>outbound_peers_count &lt; <span class="self">self</span>.max_outbound_peers {
<span class="kw">if let </span><span class="prelude-val">Some</span>(addr) = <span class="self">self</span>.address_book.pop() {
<span class="kw">if </span>!<span class="self">self</span>.connections.contains_key(<span class="kw-2">&amp;</span>addr) {
Expand Down Expand Up @@ -1215,7 +1242,19 @@
}
}

<span class="doccomment">/// Disconnect from a peer with given reason, do nothing if the peer is persistent.
<span class="doccomment">/// Disconnects from a specified peer, unless it is designated as persistent, with a given reason.
///
/// # Important Notes
///
/// - **Syncing Components:** This function, as well as [`Self::evict`], should not be invoked
/// directly within the peer manager module without triggering a notification. For example,
/// chain sync might depend on receiving a disconnect notification to correctly update their
/// internal state, which helps maintain a consistent peer set between the peer manager and
/// other modules.
///
/// - **Potential for Inconsistent State:** Bypassing notifications may lead to inconsistency
/// between the peer manager and modules that rely on peer status, resulting in unexpected
/// issues in the peer set or other connected components.
</span><span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>disconnect(<span class="kw-2">&amp;mut </span><span class="self">self</span>, peer_id: PeerId, reason: Error) {
<span class="kw">if </span><span class="self">self</span>.config.persistent.contains(<span class="kw-2">&amp;</span>peer_id) {
<span class="kw">return</span>;
Expand All @@ -1238,6 +1277,20 @@
<span class="self">self</span>.connected_peers.remove(<span class="kw-2">&amp;</span>peer_id);
}

<span class="doccomment">/// Evicts a peer, disconnecting it with a specified reason and updating the eviction timestamp.
///
/// This function internally calls [`Self::disconnect`] to carry out the disconnection
/// process and subsequently records the current time as the `last_eviction` timestamp.
///
/// # Important Note
///
/// Just like with `disconnect`, any call to `evict` should be accompanied by necessary
/// notifications to avoid state inconsistencies.
</span><span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>evict(<span class="kw-2">&amp;mut </span><span class="self">self</span>, peer_id: PeerId, reason: Error) {
<span class="self">self</span>.disconnect(peer_id, reason);
<span class="self">self</span>.last_eviction = Instant::now();
}

<span class="doccomment">/// Sets the prefer addrv2 flag for a peer.
</span><span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>set_want_addrv2(<span class="kw-2">&amp;mut </span><span class="self">self</span>, peer_id: PeerId) {
<span class="self">self</span>.connected_peers.entry(peer_id).and_modify(|info| {
Expand All @@ -1252,11 +1305,6 @@
});
}

<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>evict(<span class="kw-2">&amp;mut </span><span class="self">self</span>, peer_id: PeerId, reason: Error) {
<span class="self">self</span>.disconnect(peer_id, reason);
<span class="self">self</span>.last_eviction = Instant::now();
}

<span class="doccomment">/// Checks if a peer is connected.
</span><span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>is_connected(<span class="kw-2">&amp;</span><span class="self">self</span>, peer_id: PeerId) -&gt; bool {
<span class="self">self</span>.connected_peers.contains_key(<span class="kw-2">&amp;</span>peer_id)
Expand Down
Loading

0 comments on commit d222abc

Please sign in to comment.