6 #ifndef KAGOME_NETWORK_SYNCPROTOCOLIMPL 7 #define KAGOME_NETWORK_SYNCPROTOCOLIMPL 14 #include <unordered_map> 15 #include <unordered_set> 17 #include <boost/circular_buffer.hpp> 18 #include <libp2p/connection/stream.hpp> 19 #include <libp2p/host/host.hpp> 31 std::chrono::seconds(30);
48 std::chrono::seconds expiration_time);
68 std::chrono::time_point<std::chrono::system_clock>;
80 std::optional<CacheRecordIndex> target_slot = std::nullopt);
89 kMaxCacheEntriesPerPeer};
95 std::vector<std::optional<CacheRecord>>
storage_;
103 public std::enable_shared_from_this<SyncProtocolImpl>,
110 std::shared_ptr<SyncProtocolObserver> sync_observer,
111 std::shared_ptr<ReputationRepository> reputation_repository);
113 bool start()
override;
114 bool stop()
override;
117 return kSyncProtocolName;
120 void onIncomingStream(std::shared_ptr<Stream> stream)
override;
121 void newOutgoingStream(
123 std::function<
void(outcome::result<std::shared_ptr<Stream>>)> &&cb)
128 std::function<
void(outcome::result<BlocksResponse>)>
129 &&response_handler)
override;
131 void readRequest(std::shared_ptr<Stream> stream);
133 void writeResponse(std::shared_ptr<Stream> stream,
136 void writeRequest(std::shared_ptr<Stream> stream,
138 std::function<
void(outcome::result<void>)> &&cb);
140 void readResponse(std::shared_ptr<Stream> stream,
141 std::function<
void(outcome::result<BlocksResponse>)>
145 const static inline auto kSyncProtocolName =
"SyncProtocol"s;
154 #endif // KAGOME_NETWORK_SYNCPROTOCOLIMPL
std::unordered_map< PeerId, CacheRecordIndex > lookup_table_
Class for communication via /{chainType}/sync/2 according to sync protocol specification https://spec...
BlocksResponseCache(std::size_t capacity, std::chrono::seconds expiration_time)
void cache(const PeerId &peer_id, BlocksRequest::Fingerprint request_fingerprint, std::optional< CacheRecordIndex > target_slot=std::nullopt)
ExpirationTimepoint valid_till
std::size_t CacheRecordIndex
libp2p::peer::PeerInfo PeerInfo
bool isDuplicate(const PeerId &peer_id, BlocksRequest::Fingerprint request_fingerprint)
libp2p::peer::PeerId PeerId
std::vector< std::optional< CacheRecord > > storage_
std::shared_ptr< SyncProtocolObserver > sync_observer_
detail::BlocksResponseCache response_cache_
std::shared_ptr< ReputationRepository > reputation_repository_
const std::chrono::seconds expiration_time_
boost::circular_buffer< BlocksRequest::Fingerprint > fingerprints
const std::size_t capacity_
std::chrono::time_point< std::chrono::system_clock > ExpirationTimepoint
std::unordered_set< CacheRecordIndex > free_slots_
static constexpr auto kResponsesCacheExpirationTimeout
void purge()
removes all stale records
const std::string & protocolName() const override
static constexpr auto kMaxCacheEntriesPerPeer
static constexpr auto kResponsesCacheCapacity