1774 {
1775
1776 if( c->is_transactions_only_connection() ) return;
1777
1779 uint32_t peer_lib = msg.last_irreversible_block_num;
1782 std::tie( lib_num, std::ignore, head,
1784
1786
1787 auto current_time_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
1788 int64_t network_latency_ns = current_time_ns - msg.time;
1789 if( network_latency_ns < 0 ) {
1790 peer_wlog(c,
"Peer sent a handshake with a timestamp skewed by at least ${t}ms", (
"t", network_latency_ns/1000000));
1791 network_latency_ns = 0;
1792 }
1793
1794 uint32_t nblk_behind_by_net_latency =
static_cast<uint32_t>(network_latency_ns / block_interval_ns);
1795
1796 uint32_t nblk_combined_latency = 2 * nblk_behind_by_net_latency + 1;
1797
1798 peer_dlog(c,
"Network latency is ${lat}ms, ${num} blocks discrepancy by network latency, ${tot_num} blocks discrepancy expected once message received",
1799 ("lat", network_latency_ns/1000000)("num", nblk_behind_by_net_latency)("tot_num", nblk_combined_latency));
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815 if (head_id == msg.head_id) {
1816 peer_ilog( c,
"handshake lib ${lib}, head ${head}, head id ${id}.. sync 0",
1817 ("lib", msg.last_irreversible_block_num)("head", msg.head_num)("id", msg.head_id.str().substr(8,16)) );
1818 c->syncing = false;
1819 notice_message note;
1820 note.known_blocks.mode =
none;
1822 note.known_trx.pending = 0;
1823 c->enqueue( note );
1824 return;
1825 }
1826 if (head < peer_lib) {
1827 peer_ilog( c,
"handshake lib ${lib}, head ${head}, head id ${id}.. sync 1",
1828 ("lib", msg.last_irreversible_block_num)("head", msg.head_num)("id", msg.head_id.str().substr(8,16)) );
1829 c->syncing = false;
1830 if (c->sent_handshake_count > 0) {
1831 c->send_handshake();
1832 }
1833 return;
1834 }
1835 if (lib_num > msg.head_num + nblk_combined_latency) {
1836 peer_ilog( c,
"handshake lib ${lib}, head ${head}, head id ${id}.. sync 2",
1837 ("lib", msg.last_irreversible_block_num)("head", msg.head_num)("id", msg.head_id.str().substr(8,16)) );
1838 if (msg.generation > 1 || c->protocol_version >
proto_base) {
1839 notice_message note;
1840 note.known_trx.pending = lib_num;
1843 note.known_blocks.pending = head;
1844 c->enqueue( note );
1845 }
1846 c->syncing = true;
1847 return;
1848 }
1849
1850 if (head + nblk_combined_latency < msg.head_num ) {
1851 peer_ilog( c,
"handshake lib ${lib}, head ${head}, head id ${id}.. sync 3",
1852 ("lib", msg.last_irreversible_block_num)("head", msg.head_num)("id", msg.head_id.str().substr(8,16)) );
1853 c->syncing = false;
1854 verify_catchup(c, msg.head_num, msg.head_id);
1855 return;
1856 } else if(head >= msg.head_num + nblk_combined_latency) {
1857 peer_ilog( c,
"handshake lib ${lib}, head ${head}, head id ${id}.. sync 4",
1858 ("lib", msg.last_irreversible_block_num)("head", msg.head_num)("id", msg.head_id.str().substr(8,16)) );
1859 if (msg.generation > 1 || c->protocol_version >
proto_base) {
1860 notice_message note;
1861 note.known_trx.mode =
none;
1863 note.known_blocks.pending = head;
1864 note.known_blocks.ids.push_back(head_id);
1865 c->enqueue( note );
1866 }
1867 c->syncing = false;
1869 msg_head_num = msg.head_num, msg_head_id = msg.head_id]() {
1870 bool on_fork = true;
1871 try {
1872 controller& cc = chain_plug->chain();
1873 on_fork = cc.get_block_id_for_num( msg_head_num ) != msg_head_id;
1874 } catch( ... ) {}
1875 if( on_fork ) {
1876 c->strand.post( [c]() {
1877 request_message req;
1878 req.req_blocks.mode = catch_up;
1879 req.req_trx.mode = none;
1880 c->enqueue( req );
1881 } );
1882 }
1883 } );
1884 return;
1885 } else {
1886 peer_dlog( c,
"Block discrepancy is within network latency range.");
1887 }
1888 }
auto post(int priority, Func &&func)
chain_plugin * chain_plug
std::tuple< uint32_t, uint32_t, uint32_t, block_id_type, block_id_type, block_id_type > get_chain_info() const
void sync_reset_lib_num(const connection_ptr &conn, bool closing)
constexpr uint16_t proto_base
#define peer_dlog(PEER, FORMAT,...)
#define peer_ilog(PEER, FORMAT,...)
#define peer_wlog(PEER, FORMAT,...)
static constexpr int medium