CatapultServer  v0.5.0.1 (Elephant)
TransactionPluginFactory.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "Transaction.h"
23 #include "TransactionPlugin.h"
24 #include "catapult/functions.h"
25 
26 namespace catapult { namespace model {
27 
31  Default,
32 
35  };
36 
38  template<TransactionPluginFactoryOptions Options>
40  public:
42  template<typename TEmbeddedTransaction, typename TPublishEmbeddedFunc>
43  static std::unique_ptr<EmbeddedTransactionPlugin> CreateEmbedded(TPublishEmbeddedFunc publishEmbeddedFunc) {
44  return std::make_unique<EmbeddedTransactionPluginT<TEmbeddedTransaction>>(publishEmbeddedFunc);
45  }
46 
48  template<typename TTransaction, typename TEmbeddedTransaction, typename TPublishFunc, typename TPublishEmbeddedFunc>
49  static std::unique_ptr<TransactionPlugin> Create(TPublishFunc publishFunc, TPublishEmbeddedFunc publishEmbeddedFunc) {
50  return std::make_unique<TransactionPluginT<TTransaction, TEmbeddedTransaction>>(publishFunc, publishEmbeddedFunc);
51  }
52 
53  private:
54  template<typename TTransaction, typename TDerivedTransaction, typename TPlugin>
55  class BasicTransactionPluginT : public TPlugin {
56  private:
58 
59  public:
60  explicit BasicTransactionPluginT(const PublishFunc& publishFunc) : m_publishFunc(publishFunc)
61  {}
62 
63  public:
64  EntityType type() const override {
65  return TDerivedTransaction::Entity_Type;
66  }
67 
68  TransactionAttributes attributes() const override {
69  auto version = TDerivedTransaction::Current_Version;
70  return { version, version, utils::TimeSpan() };
71  }
72 
73  uint64_t calculateRealSize(const TTransaction& transaction) const override {
74  return TDerivedTransaction::CalculateRealSize(static_cast<const TDerivedTransaction&>(transaction));
75  }
76 
77  protected:
78  void publishImpl(const TTransaction& transaction, NotificationSubscriber& sub) const {
79  m_publishFunc(static_cast<const TDerivedTransaction&>(transaction), sub);
80  }
81 
82  private:
84  };
85 
86  template<typename TEmbeddedTransaction>
88  : public BasicTransactionPluginT<EmbeddedTransaction, TEmbeddedTransaction, EmbeddedTransactionPlugin> {
89  private:
91 
92  public:
93  template<typename TPublishEmbeddedFunc>
94  explicit EmbeddedTransactionPluginT(TPublishEmbeddedFunc publishEmbeddedFunc) : BaseType(publishEmbeddedFunc)
95  {}
96 
97  public:
98  void publish(const EmbeddedTransaction& transaction, NotificationSubscriber& sub) const override {
99  BaseType::publishImpl(transaction, sub);
100  }
101  };
102 
103  template<typename TTransaction, typename TEmbeddedTransaction>
104  class TransactionPluginT : public BasicTransactionPluginT<Transaction, TTransaction, TransactionPlugin> {
105  private:
107 
108  public:
109  template<typename TPublishFunc, typename TPublishEmbeddedFunc>
110  explicit TransactionPluginT(TPublishFunc publishFunc, TPublishEmbeddedFunc publishEmbeddedFunc)
111  : BaseType(publishFunc)
112  , m_pEmbeddedTransactionPlugin(CreateEmbedded<TEmbeddedTransaction>(publishEmbeddedFunc))
113  {}
114 
115  public:
116  void publish(const WeakEntityInfoT<Transaction>& transactionInfo, NotificationSubscriber& sub) const override {
117  BaseType::publishImpl(transactionInfo.entity(), sub);
118  }
119 
120  RawBuffer dataBuffer(const Transaction& transaction) const override {
121  auto headerSize = VerifiableEntity::Header_Size;
122  return { reinterpret_cast<const uint8_t*>(&transaction) + headerSize, transaction.Size - headerSize };
123  }
124 
125  std::vector<RawBuffer> merkleSupplementaryBuffers(const Transaction&) const override {
126  return {};
127  }
128 
129  bool supportsTopLevel() const override {
131  }
132 
133  bool supportsEmbedding() const override {
134  return true;
135  }
136 
137  const EmbeddedTransactionPlugin& embeddedPlugin() const override {
139  }
140 
141  private:
142  std::unique_ptr<EmbeddedTransactionPlugin> m_pEmbeddedTransactionPlugin;
143  };
144  };
145 
147 #define DEFINE_TRANSACTION_PLUGIN_FACTORY(NAME, OPTIONS, PUBLISH) \
148  std::unique_ptr<TransactionPlugin> Create##NAME##TransactionPlugin() { \
149  using Factory = TransactionPluginFactory<TransactionPluginFactoryOptions::OPTIONS>; \
150  return Factory::Create<NAME##Transaction, Embedded##NAME##Transaction>( \
151  PUBLISH<NAME##Transaction>, \
152  PUBLISH<Embedded##NAME##Transaction>); \
153  }
154 
156 #define DEFINE_TRANSACTION_PLUGIN_FACTORY_WITH_CONFIG(NAME, OPTIONS, PUBLISH, CONFIG_TYPE) \
157  std::unique_ptr<TransactionPlugin> Create##NAME##TransactionPlugin(const CONFIG_TYPE& config) { \
158  using Factory = TransactionPluginFactory<TransactionPluginFactoryOptions::OPTIONS>; \
159  return Factory::Create<NAME##Transaction, Embedded##NAME##Transaction>( \
160  PUBLISH<NAME##Transaction>(config), \
161  PUBLISH<Embedded##NAME##Transaction>(config)); \
162  }
163 }}
catapult::model::TransactionPluginFactoryOptions::Default
Transaction supports both top-level and embedding.
catapult::model::TransactionPluginFactory::TransactionPluginT
Definition: TransactionPluginFactory.h:104
catapult::model::TransactionPluginFactory::TransactionPluginT::publish
void publish(const WeakEntityInfoT< Transaction > &transactionInfo, NotificationSubscriber &sub) const override
Sends all notifications from transactionInfo to sub.
Definition: TransactionPluginFactory.h:116
catapult::model::TransactionPluginFactory::TransactionPluginT::embeddedPlugin
const EmbeddedTransactionPlugin & embeddedPlugin() const override
Gets the corresponding embedded plugin if supportsEmbedding() is true.
Definition: TransactionPluginFactory.h:137
Transaction.h
catapult::model::NotificationSubscriber
A notification subscriber.
Definition: NotificationSubscriber.h:27
catapult::model::TransactionPluginFactory::TransactionPluginT::dataBuffer
RawBuffer dataBuffer(const Transaction &transaction) const override
Extracts the primary data buffer from transaction that is used for signing and basic hashing.
Definition: TransactionPluginFactory.h:120
catapult::tools::Options
boost::program_options::variables_map Options
Provides access to parsed options.
Definition: Options.h:34
catapult::model::EntityType
EntityType
Enumeration of entity types.
Definition: EntityType.h:43
catapult::model::TransactionPluginFactory::TransactionPluginT::supportsTopLevel
bool supportsTopLevel() const override
true if this transaction type supports being embedded directly in blocks.
Definition: TransactionPluginFactory.h:129
catapult::model::TransactionPluginFactory::BasicTransactionPluginT< Transaction, TTransaction, TransactionPlugin >::PublishFunc
consumer< const TTransaction &, NotificationSubscriber & > PublishFunc
Definition: TransactionPluginFactory.h:57
catapult::model::VerifiableEntityHeader::Header_Size
static constexpr size_t Header_Size
Size of the VerifiableEntity part that can be skipped when signing/verifying.
Definition: VerifiableEntity.h:38
functions.h
catapult::utils::TimeSpan
Represents a time duration.
Definition: TimeSpan.h:30
catapult::model::TransactionPluginFactory::BasicTransactionPluginT::type
EntityType type() const override
Definition: TransactionPluginFactory.h:64
catapult::model::TransactionPluginFactory::TransactionPluginT::supportsEmbedding
bool supportsEmbedding() const override
true if this transaction type supports being embedded in other transactions.
Definition: TransactionPluginFactory.h:133
catapult::model::WeakEntityInfoT
Wrapper around a strongly typed entity and its associated metadata.
Definition: WeakEntityInfo.h:33
catapult::model::EmbeddedTransactionPlugin
An embedded transaction plugin.
Definition: TransactionPlugin.h:68
catapult::model::TransactionPluginFactory::TransactionPluginT::TransactionPluginT
TransactionPluginT(TPublishFunc publishFunc, TPublishEmbeddedFunc publishEmbeddedFunc)
Definition: TransactionPluginFactory.h:110
catapult::model::TransactionPluginFactory::EmbeddedTransactionPluginT
Definition: TransactionPluginFactory.h:87
catapult::model::TransactionPluginFactory::CreateEmbedded
static std::unique_ptr< EmbeddedTransactionPlugin > CreateEmbedded(TPublishEmbeddedFunc publishEmbeddedFunc)
Creates an embedded transaction plugin around publishEmbeddedFunc.
Definition: TransactionPluginFactory.h:43
catapult::model::TransactionPluginFactory::Create
static std::unique_ptr< TransactionPlugin > Create(TPublishFunc publishFunc, TPublishEmbeddedFunc publishEmbeddedFunc)
Creates a transaction plugin that supports embedding around publishFunc and publishEmbeddedFunc.
Definition: TransactionPluginFactory.h:49
catapult::model::WeakEntityInfoT::entity
constexpr const TEntity & entity() const
Gets the entity.
Definition: WeakEntityInfo.h:82
catapult::model::TransactionPluginFactory::EmbeddedTransactionPluginT::publish
void publish(const EmbeddedTransaction &transaction, NotificationSubscriber &sub) const override
Sends all notifications from transaction to sub.
Definition: TransactionPluginFactory.h:98
catapult::model::TransactionPluginFactory::TransactionPluginT::m_pEmbeddedTransactionPlugin
std::unique_ptr< EmbeddedTransactionPlugin > m_pEmbeddedTransactionPlugin
Definition: TransactionPluginFactory.h:142
TransactionPlugin.h
catapult::model::TransactionPluginFactory::TransactionPluginT::merkleSupplementaryBuffers
std::vector< RawBuffer > merkleSupplementaryBuffers(const Transaction &) const override
Definition: TransactionPluginFactory.h:125
catapult::model::TransactionPluginFactory
Factory for creating transaction plugins.
Definition: TransactionPluginFactory.h:39
catapult::model::TransactionPluginFactoryOptions::Only_Embeddable
Transaction only supports embedding.
catapult::model::EmbeddedTransaction
Binary layout for an embedded transaction (non-verifiable).
Definition: EmbeddedTransaction.h:38
catapult::model::TransactionAttributes
Transaction dependent attributes.
Definition: TransactionPlugin.h:38
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::model::TransactionPluginFactory::BasicTransactionPluginT::attributes
TransactionAttributes attributes() const override
Definition: TransactionPluginFactory.h:68
catapult::model::TransactionPluginFactory::BasicTransactionPluginT::BasicTransactionPluginT
BasicTransactionPluginT(const PublishFunc &publishFunc)
Definition: TransactionPluginFactory.h:60
catapult::model::TransactionPluginFactory::BasicTransactionPluginT
Definition: TransactionPluginFactory.h:55
catapult::model::TransactionPluginFactory::BasicTransactionPluginT::m_publishFunc
PublishFunc m_publishFunc
Definition: TransactionPluginFactory.h:83
catapult::model::TransactionPluginFactory::BasicTransactionPluginT::publishImpl
void publishImpl(const TTransaction &transaction, NotificationSubscriber &sub) const
Definition: TransactionPluginFactory.h:78
catapult::model::TransactionPluginFactory::BasicTransactionPluginT::calculateRealSize
uint64_t calculateRealSize(const TTransaction &transaction) const override
Definition: TransactionPluginFactory.h:73
catapult::model::TransactionPluginFactory::EmbeddedTransactionPluginT::EmbeddedTransactionPluginT
EmbeddedTransactionPluginT(TPublishEmbeddedFunc publishEmbeddedFunc)
Definition: TransactionPluginFactory.h:94
catapult::model::TransactionPluginFactoryOptions
TransactionPluginFactoryOptions
Transaction plugin factory options.
Definition: TransactionPluginFactory.h:29
catapult::consumer
std::function< void(TArgs...)> consumer
A consumer function.
Definition: functions.h:35
catapult::model::SizePrefixedEntity::Size
uint32_t Size
Entity size.
Definition: SizePrefixedEntity.h:33
catapult::utils::RawBuffer
BasicRawBuffer< const uint8_t > RawBuffer
A const binary buffer.
Definition: RawBuffer.h:62
catapult::model::Transaction
Binary layout for a transaction.
Definition: Transaction.h:32