Struct snarkos_node_router::Sync
source · pub struct Sync<N: Network> { /* private fields */ }
Expand description
A struct that keeps track of the current sync state.
State
- When a request is inserted, the
requests
map andrequest_timestamps
map insert an entry for the request height. - When a response is inserted, the
requests
map inserts the entry for the request height. - When a request is completed, the
requests
map still has the entry, but itssync_ips
is empty; - the
request_timestamps
map remains unchanged. - When a response is removed/completed, the
requests
map andrequest_timestamps
map also remove the entry for the request height. - When a request is timed out, the
requests
,request_timestamps
, andresponses
map remove the entry for the request height;
Implementations§
source§impl<N: Network> Sync<N>
impl<N: Network> Sync<N>
sourcepub fn set_local_ip(&self, local_ip: SocketAddr)
pub fn set_local_ip(&self, local_ip: SocketAddr)
Returns a dummy listening address of the associated node.
sourcepub fn latest_canon_height(&self) -> u32
pub fn latest_canon_height(&self) -> u32
Returns the latest block height in the sync pool.
sourcepub fn get_canon_height(&self, hash: &N::BlockHash) -> Option<u32>
pub fn get_canon_height(&self, hash: &N::BlockHash) -> Option<u32>
Returns the canonical block height, if it exists.
sourcepub fn get_canon_hash(&self, height: u32) -> Option<N::BlockHash>
pub fn get_canon_hash(&self, height: u32) -> Option<N::BlockHash>
Returns the canonical block hash for the given block height, if it exists.
sourcepub fn get_peer_height(&self, peer_ip: &SocketAddr) -> Option<u32>
pub fn get_peer_height(&self, peer_ip: &SocketAddr) -> Option<u32>
Returns the latest block height of the given peer IP.
sourcepub fn get_peer_heights(&self) -> BTreeMap<u32, Vec<SocketAddr>>
pub fn get_peer_heights(&self) -> BTreeMap<u32, Vec<SocketAddr>>
Returns a map of peer height to peer IPs.
e.g. {{ 127 => \[peer1, peer2\], 128 => \[peer3\], 135 => \[peer4, peer5\] }}
sourcepub fn get_peers_by_height(&self) -> Vec<(SocketAddr, u32)>
pub fn get_peers_by_height(&self) -> Vec<(SocketAddr, u32)>
Returns the list of peers with their heights, sorted by height (descending).
sourcepub fn get_common_ancestor(
&self,
peer_a: SocketAddr,
peer_b: SocketAddr
) -> Option<u32>
pub fn get_common_ancestor( &self, peer_a: SocketAddr, peer_b: SocketAddr ) -> Option<u32>
Returns the common ancestor for the given peer pair, if it exists.
sourcepub fn get_block_request(&self, height: u32) -> Option<SyncRequest<N>>
pub fn get_block_request(&self, height: u32) -> Option<SyncRequest<N>>
Returns the block request for the given height, if it exists.
sourcepub fn get_block_request_timestamp(&self, height: u32) -> Option<Instant>
pub fn get_block_request_timestamp(&self, height: u32) -> Option<Instant>
Returns the timestamp of the last time the block was requested, if it exists.
sourcepub fn insert_canon_locator(&self, height: u32, hash: N::BlockHash)
pub fn insert_canon_locator(&self, height: u32, hash: N::BlockHash)
Inserts a canonical block hash for the given block height, overriding an existing entry if it exists.
sourcepub fn insert_canon_locators(&self, locators: BlockLocators<N>) -> Result<()>
pub fn insert_canon_locators(&self, locators: BlockLocators<N>) -> Result<()>
Inserts the block locators as canonical, overriding any existing entries.
sourcepub fn find_sync_peers(&self) -> Option<(IndexMap<SocketAddr, u32>, u32)>
pub fn find_sync_peers(&self) -> Option<(IndexMap<SocketAddr, u32>, u32)>
Returns the sync peers with their latest heights, and their minimum common ancestor, if the node can sync. This function returns peers that are consistent with each other, and have a block height that is greater than the canon height of this node.
sourcepub fn prepare_block_requests(&self) -> Vec<(u32, SyncRequest<N>)>
pub fn prepare_block_requests(&self) -> Vec<(u32, SyncRequest<N>)>
Returns a list of block requests, if the node needs to sync.
sourcepub fn insert_block_request(
&self,
height: u32,
(hash, previous_hash, sync_ips): SyncRequest<N>
) -> Result<()>
pub fn insert_block_request( &self, height: u32, (hash, previous_hash, sync_ips): SyncRequest<N> ) -> Result<()>
Inserts a block request for the given height.
sourcepub fn insert_block_response(
&self,
peer_ip: SocketAddr,
block: Block<N>
) -> Result<()>
pub fn insert_block_response( &self, peer_ip: SocketAddr, block: Block<N> ) -> Result<()>
Inserts the given block response, after checking that the request exists and the response is well-formed. On success, this function removes the peer IP from the requests map. On failure, this function removes all block requests from the given peer IP.
sourcepub fn update_peer_locators(
&self,
peer_ip: SocketAddr,
locators: BlockLocators<N>
) -> Result<()>
pub fn update_peer_locators( &self, peer_ip: SocketAddr, locators: BlockLocators<N> ) -> Result<()>
Updates the block locators and common ancestors for the given peer IP. This function checks that the given block locators are well-formed, however it does not check that the block locators are consistent the peer’s previous block locators or other peers’ block locators.
sourcepub fn remove_peer(&self, peer_ip: &SocketAddr)
pub fn remove_peer(&self, peer_ip: &SocketAddr)
Removes the peer from the sync pool, if they exist.
sourcepub fn remove_block_request_to_peer(&self, peer_ip: &SocketAddr, height: u32)
pub fn remove_block_request_to_peer(&self, peer_ip: &SocketAddr, height: u32)
Removes the block request for the given peer IP, if it exists.
sourcepub fn remove_block_requests_to_peer(&self, peer_ip: &SocketAddr)
pub fn remove_block_requests_to_peer(&self, peer_ip: &SocketAddr)
Removes all block requests for the given peer IP.
sourcepub fn remove_block_request(&self, height: u32)
pub fn remove_block_request(&self, height: u32)
Removes the entire block request for the given height, if it exists.
sourcepub fn remove_block_response(&self, height: u32) -> Option<Block<N>>
pub fn remove_block_response(&self, height: u32) -> Option<Block<N>>
Removes and returns the block response for the given height, if the request is complete.