-
Notifications
You must be signed in to change notification settings - Fork 33
/
ServerHooks.h
260 lines (208 loc) · 9.81 KB
/
ServerHooks.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
*** Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp.
*** Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie.
*** All rights reserved.
***
*** This file is part of Catapult.
***
*** Catapult is free software: you can redistribute it and/or modify
*** it under the terms of the GNU Lesser General Public License as published by
*** the Free Software Foundation, either version 3 of the License, or
*** (at your option) any later version.
***
*** Catapult is distributed in the hope that it will be useful,
*** but WITHOUT ANY WARRANTY; without even the implied warranty of
*** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*** GNU Lesser General Public License for more details.
***
*** You should have received a copy of the GNU Lesser General Public License
*** along with Catapult. If not, see <http://www.gnu.org/licenses/>.
**/
#pragma once
#include "BasicServerHooks.h"
#include "TransactionEvent.h"
#include "catapult/cache_tx/MemoryUtCache.h"
#include "catapult/chain/ChainFunctions.h"
#include "catapult/chain/ChainSynchronizer.h"
#include "catapult/consumers/BlockConsumers.h"
#include "catapult/consumers/InputUtils.h"
#include "catapult/disruptor/DisruptorElement.h"
#include "catapult/handlers/HandlerTypes.h"
#include "catapult/ionet/PacketPayload.h"
namespace catapult { namespace extensions {
// region function aliases
/// New block sink prototype.
using NewBlockSink = consumers::NewBlockSink;
/// New transactions sink prototype that does not take ownership of new infos.
using SharedNewTransactionsSink = consumer<const consumers::TransactionInfos&>;
/// Packet payload sink.
using PacketPayloadSink = consumer<const ionet::PacketPayload&>;
/// Banned node identity sink.
using BannedNodeIdentitySink = consumer<const model::NodeIdentity&>;
/// Handler that is called when the confirmed state of transactions changes.
using TransactionsChangeHandler = consumers::BlockchainSyncHandlers::TransactionsChangeFunc;
/// Function signature for delivering a block range to a consumer.
using BlockRangeConsumerFunc = handlers::BlockRangeHandler;
/// Function signature for delivering a transaction range to a consumer.
using TransactionRangeConsumerFunc = handlers::TransactionRangeHandler;
/// Factory for creating a range consumer function bound to an input source.
template<typename TConsumer>
using RangeConsumerFactoryFunc = std::function<TConsumer (disruptor::InputSource)>;
/// Factory for creating a BlockRangeConsumerFunc bound to an input source.
using BlockRangeConsumerFactoryFunc = RangeConsumerFactoryFunc<BlockRangeConsumerFunc>;
/// Factory for creating a CompletionAwareBlockRangeConsumerFunc bound to an input source.
using CompletionAwareBlockRangeConsumerFactoryFunc = RangeConsumerFactoryFunc<chain::CompletionAwareBlockRangeConsumerFunc>;
/// Factory for creating a TransactionRangeConsumerFunc bound to an input source.
using TransactionRangeConsumerFactoryFunc = RangeConsumerFactoryFunc<TransactionRangeConsumerFunc>;
/// Retriever that returns the network chain heights for a number of peers.
using RemoteChainHeightsRetriever = std::function<thread::future<std::vector<Height>> (size_t)>;
/// Supplier for retrieving a finalized height hash pair.
using FinalizedHeightHashPairSupplier = supplier<model::HeightHashPair>;
/// Predicate for determining if a chain is synced.
using ChainSyncedPredicate = predicate<>;
/// Predicate for determining if a hash is known.
using KnownHashPredicate = chain::KnownHashPredicate;
// endregion
/// Hooks that can be used to configure server behavior.
class ServerHooks {
public:
/// Adds a new block \a sink.
void addNewBlockSink(const NewBlockSink& sink) {
m_newBlockSinks.push_back(sink);
}
/// Adds a new transactions \a sink.
void addNewTransactionsSink(const SharedNewTransactionsSink& sink) {
m_newTransactionsSinks.push_back(sink);
}
/// Adds a packet payload \a sink.
void addPacketPayloadSink(const PacketPayloadSink& sink) {
m_packetPayloadSinks.push_back(sink);
}
/// Adds a banned node identity \a sink.
void addBannedNodeIdentitySink(const BannedNodeIdentitySink& sink) {
m_bannedNodeIdentitySinks.push_back(sink);
}
/// Adds a transactions change \a handler.
void addTransactionsChangeHandler(const TransactionsChangeHandler& handler) {
m_transactionsChangeHandlers.push_back(handler);
}
/// Adds a transaction event \a handler.
void addTransactionEventHandler(const TransactionEventHandler& handler) {
m_transactionEventHandlers.push_back(handler);
}
/// Sets the \a factory for creating a BlockRangeConsumerFunc bound to an input source.
void setBlockRangeConsumerFactory(const BlockRangeConsumerFactoryFunc& factory) {
SetOnce(m_blockRangeConsumerFactory, factory);
}
/// Sets the \a factory for creating a CompletionAwareBlockRangeConsumerFunc bound to an input source.
void setCompletionAwareBlockRangeConsumerFactory(const CompletionAwareBlockRangeConsumerFactoryFunc& factory) {
SetOnce(m_completionAwareBlockRangeConsumerFactory, factory);
}
/// Sets the \a factory for creating a TransactionRangeConsumerFunc bound to an input source.
void setTransactionRangeConsumerFactory(const TransactionRangeConsumerFactoryFunc& factory) {
SetOnce(m_transactionRangeConsumerFactory, factory);
}
/// Sets the remote chain heights \a retriever.
void setRemoteChainHeightsRetriever(const RemoteChainHeightsRetriever& retriever) {
SetOnce(m_remoteChainHeightsRetriever, retriever);
}
/// Sets the local finalized height hash pair \a supplier.
void setLocalFinalizedHeightHashPairSupplier(const FinalizedHeightHashPairSupplier& supplier) {
SetOnce(m_localFinalizedHeightHashPairSupplier, supplier);
}
/// Sets the network finalized height hash pair \a supplier.
void setNetworkFinalizedHeightHashPairSupplier(const FinalizedHeightHashPairSupplier& supplier) {
SetOnce(m_networkFinalizedHeightHashPairSupplier, supplier);
}
/// Sets the chain synced \a predicate.
void setChainSyncedPredicate(const ChainSyncedPredicate& predicate) {
SetOnce(m_chainSyncedPredicate, predicate);
}
/// Adds a known hash \a predicate.
void addKnownHashPredicate(const KnownHashPredicate& predicate) {
m_knownHashPredicates.push_back(predicate);
}
public:
/// Gets the new block sink.
auto newBlockSink() const {
return AggregateConsumers(m_newBlockSinks);
}
/// Gets the new transactions sink.
auto newTransactionsSink() const {
return AggregateConsumers(m_newTransactionsSinks);
}
/// Gets the packet payload sink.
auto packetPayloadSink() const {
return AggregateConsumers(m_packetPayloadSinks);
}
/// Gets the banned node identity sink.
auto bannedNodeIdentitySink() const {
return AggregateConsumers(m_bannedNodeIdentitySinks);
}
/// Gets the transactions change handler.
auto transactionsChangeHandler() const {
return AggregateConsumers(m_transactionsChangeHandlers);
}
/// Gets the transaction event handler.
auto transactionEventHandler() const {
return AggregateConsumers(m_transactionEventHandlers);
}
/// Gets the factory for creating a BlockRangeConsumerFunc bound to an input source.
const auto& blockRangeConsumerFactory() const {
return Require(m_blockRangeConsumerFactory);
}
/// Gets the factory for creating a CompletionAwareBlockRangeConsumerFunc bound to an input source.
const auto& completionAwareBlockRangeConsumerFactory() const {
return Require(m_completionAwareBlockRangeConsumerFactory);
}
/// Gets the factory for creating a TransactionRangeConsumerFunc bound to an input source.
const auto& transactionRangeConsumerFactory() const {
return Require(m_transactionRangeConsumerFactory);
}
/// Gets the remote chain heights retriever.
const auto& remoteChainHeightsRetriever() const {
return Require(m_remoteChainHeightsRetriever);
}
/// Gets the local finalized height hash pair supplier.
/// \note This height hash pair must be in the local blockchain.
auto localFinalizedHeightHashPairSupplier() const {
return Require(m_localFinalizedHeightHashPairSupplier);
}
/// Gets the network finalized height hash pair supplier.
/// \note This height hash pair might not be in the local blockchain.
auto networkFinalizedHeightHashPairSupplier() const {
return Require(m_networkFinalizedHeightHashPairSupplier);
}
/// Gets the chain synced predicate.
auto chainSyncedPredicate() const {
return m_chainSyncedPredicate ? m_chainSyncedPredicate : []() { return true; };
}
/// Gets the known hash predicate augmented with a check in \a utCache.
KnownHashPredicate knownHashPredicate(const cache::ReadWriteUtCache& utCache) const {
return [&utCache, knownHashPredicates = m_knownHashPredicates](auto timestamp, const auto& hash) {
if (utCache.view().contains(hash))
return true;
for (const auto& knownHashPredicate : knownHashPredicates) {
if (knownHashPredicate(timestamp, hash))
return true;
}
return false;
};
}
private:
std::vector<NewBlockSink> m_newBlockSinks;
std::vector<SharedNewTransactionsSink> m_newTransactionsSinks;
std::vector<PacketPayloadSink> m_packetPayloadSinks;
std::vector<BannedNodeIdentitySink> m_bannedNodeIdentitySinks;
std::vector<TransactionsChangeHandler> m_transactionsChangeHandlers;
std::vector<TransactionEventHandler> m_transactionEventHandlers;
BlockRangeConsumerFactoryFunc m_blockRangeConsumerFactory;
CompletionAwareBlockRangeConsumerFactoryFunc m_completionAwareBlockRangeConsumerFactory;
TransactionRangeConsumerFactoryFunc m_transactionRangeConsumerFactory;
RemoteChainHeightsRetriever m_remoteChainHeightsRetriever;
FinalizedHeightHashPairSupplier m_localFinalizedHeightHashPairSupplier;
FinalizedHeightHashPairSupplier m_networkFinalizedHeightHashPairSupplier;
ChainSyncedPredicate m_chainSyncedPredicate;
std::vector<KnownHashPredicate> m_knownHashPredicates;
};
}}