BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
transaction.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
26 
27 namespace graphene { namespace protocol {
28  struct predicate_result;
29 
31  using rejected_predicate_map = map<custom_authority_id_type, rejected_predicate>;
32  using custom_authority_lookup = std::function<vector<authority>(account_id_type, const operation&,
34 
70  {
71  public:
72  virtual ~transaction() = default;
76  uint16_t ref_block_num = 0;
82  uint32_t ref_block_prefix = 0;
83 
88 
89  vector<operation> operations;
91 
93  digest_type digest()const;
94  virtual const transaction_id_type& id()const;
95  virtual void validate() const;
96 
97  void set_expiration( fc::time_point_sec expiration_time );
98  void set_reference_block( const block_id_type& reference_block );
99 
101  template<typename Visitor>
102  vector<typename Visitor::result_type> visit( Visitor&& visitor )
103  {
104  vector<typename Visitor::result_type> results;
105  for( auto& op : operations )
106  results.push_back(op.visit( std::forward<Visitor>( visitor ) ));
107  return results;
108  }
109  template<typename Visitor>
110  vector<typename Visitor::result_type> visit( Visitor&& visitor )const
111  {
112  vector<typename Visitor::result_type> results;
113  for( auto& op : operations )
114  results.push_back(op.visit( std::forward<Visitor>( visitor ) ));
115  return results;
116  }
117 
118  void get_required_authorities( flat_set<account_id_type>& active,
119  flat_set<account_id_type>& owner,
120  vector<authority>& other,
121  bool ignore_custom_operation_required_auths )const;
122 
123  virtual uint64_t get_packed_size()const;
124 
125  protected:
126  // Calculate the digest used for signature validation
127  digest_type sig_digest( const chain_id_type& chain_id )const;
129  };
130 
135  {
136  public:
138  : transaction(trx){}
139  virtual ~signed_transaction() = default;
140 
142  const signature_type& sign( const private_key_type& key, const chain_id_type& chain_id );
143 
145  signature_type sign( const private_key_type& key, const chain_id_type& chain_id )const;
146 
154  set<public_key_type> get_required_signatures(
155  const chain_id_type& chain_id,
156  const flat_set<public_key_type>& available_keys,
157  const std::function<const authority*(account_id_type)>& get_active,
158  const std::function<const authority*(account_id_type)>& get_owner,
159  bool allow_non_immediate_owner,
160  bool ignore_custom_operation_required_authorities,
161  uint32_t max_recursion = GRAPHENE_MAX_SIG_CHECK_DEPTH )const;
162 
178  void verify_authority(
179  const chain_id_type& chain_id,
180  const std::function<const authority*(account_id_type)>& get_active,
181  const std::function<const authority*(account_id_type)>& get_owner,
182  const custom_authority_lookup& get_custom,
183  bool allow_non_immediate_owner,
184  bool ignore_custom_operation_required_auths,
185  uint32_t max_recursion = GRAPHENE_MAX_SIG_CHECK_DEPTH )const;
186 
193  set<public_key_type> minimize_required_signatures(
194  const chain_id_type& chain_id,
195  const flat_set<public_key_type>& available_keys,
196  const std::function<const authority*(account_id_type)>& get_active,
197  const std::function<const authority*(account_id_type)>& get_owner,
198  const custom_authority_lookup& get_custom,
199  bool allow_non_immediate_owner,
200  bool ignore_custom_operation_required_auths,
201  uint32_t max_recursion = GRAPHENE_MAX_SIG_CHECK_DEPTH) const;
202 
214  virtual const flat_set<public_key_type>& get_signature_keys( const chain_id_type& chain_id )const;
215 
217  vector<signature_type> signatures;
218 
220  void clear() { operations.clear(); signatures.clear(); }
221 
223  void clear_signatures() { signatures.clear(); }
224  protected:
226  mutable flat_set<public_key_type> _signees;
227  };
228 
234  public:
238  virtual ~precomputable_transaction() = default;
239 
240  virtual const transaction_id_type& id()const override;
241  virtual void validate()const override;
242  virtual const flat_set<public_key_type>& get_signature_keys( const chain_id_type& chain_id )const override;
243  virtual uint64_t get_packed_size()const override;
244  protected:
245  mutable bool _validated = false;
246  mutable uint64_t _packed_size = 0;
247  };
248 
268  void verify_authority( const vector<operation>& ops, const flat_set<public_key_type>& sigs,
269  const std::function<const authority*(account_id_type)>& get_active,
270  const std::function<const authority*(account_id_type)>& get_owner,
271  const custom_authority_lookup& get_custom,
272  bool allow_non_immediate_owner,
273  bool ignore_custom_operation_required_auths,
274  uint32_t max_recursion = GRAPHENE_MAX_SIG_CHECK_DEPTH,
275  bool allow_committee = false,
276  const flat_set<account_id_type>& active_approvals = flat_set<account_id_type>(),
277  const flat_set<account_id_type>& owner_approvals = flat_set<account_id_type>() );
278 
293  {
296  virtual ~processed_transaction() = default;
297 
298  vector<operation_result> operation_results;
299 
300  digest_type merkle_digest()const;
301  };
302 
304 
305 } } // graphene::protocol
306 
307 FC_REFLECT( graphene::protocol::transaction, (ref_block_num)(ref_block_prefix)(expiration)(operations)(extensions) )
308 // Note: not reflecting _signees field for backward compatibility; in addition, it should not be in p2p messages
311 FC_REFLECT_DERIVED( graphene::protocol::processed_transaction, (graphene::protocol::precomputable_transaction), (operation_results) )
312 
313 GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::transaction)
314 GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::signed_transaction)
315 GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::precomputable_transaction)
316 GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::protocol::processed_transaction)
graphene::protocol::transaction::operations
vector< operation > operations
Definition: transaction.hpp:89
graphene::protocol::precomputable_transaction
Definition: transaction.hpp:233
graphene::protocol::precomputable_transaction::~precomputable_transaction
virtual ~precomputable_transaction()=default
graphene::protocol::precomputable_transaction::precomputable_transaction
precomputable_transaction()
Definition: transaction.hpp:235
graphene::protocol::transaction::visit
vector< typename Visitor::result_type > visit(Visitor &&visitor)
visit all operations
Definition: transaction.hpp:102
graphene::protocol::precomputable_transaction::id
virtual const transaction_id_type & id() const override
Definition: transaction.cpp:434
graphene::protocol::rejected_predicate_map
map< custom_authority_id_type, rejected_predicate > rejected_predicate_map
Definition: transaction.hpp:31
graphene::protocol::transaction::id
virtual const transaction_id_type & id() const
Definition: transaction.cpp:70
graphene::protocol::transaction::sig_digest
digest_type sig_digest(const chain_id_type &chain_id) const
Definition: transaction.cpp:50
graphene::protocol::signed_transaction::clear
void clear()
Definition: transaction.hpp:220
fc::static_variant
Definition: raw_fwd.hpp:27
graphene::protocol::signed_transaction::clear_signatures
void clear_signatures()
Definition: transaction.hpp:223
graphene::protocol::signed_transaction::sign
const signature_type & sign(const private_key_type &key, const chain_id_type &chain_id)
Definition: transaction.cpp:77
graphene::protocol::precomputable_transaction::precomputable_transaction
precomputable_transaction(signed_transaction &&tx)
Definition: transaction.hpp:237
graphene::protocol::precomputable_transaction::precomputable_transaction
precomputable_transaction(const signed_transaction &tx)
Definition: transaction.hpp:236
graphene::protocol::signed_transaction::minimize_required_signatures
set< public_key_type > minimize_required_signatures(const chain_id_type &chain_id, const flat_set< public_key_type > &available_keys, const std::function< const authority *(account_id_type)> &get_active, const std::function< const authority *(account_id_type)> &get_owner, const custom_authority_lookup &get_custom, bool allow_non_immediate_owner, bool ignore_custom_operation_required_auths, uint32_t max_recursion=GRAPHENE_MAX_SIG_CHECK_DEPTH) const
Definition: transaction.cpp:401
graphene::protocol::transaction::set_reference_block
void set_reference_block(const block_id_type &reference_block)
Definition: transaction.cpp:97
graphene::protocol::transaction::expiration
fc::time_point_sec expiration
Definition: transaction.hpp:87
graphene::protocol::precomputable_transaction::validate
virtual void validate() const override
Definition: transaction.cpp:441
graphene::protocol::precomputable_transaction::get_signature_keys
virtual const flat_set< public_key_type > & get_signature_keys(const chain_id_type &chain_id) const override
Extract public keys from signatures with given chain ID.
Definition: transaction.cpp:455
fc::sha256
Definition: sha256.hpp:10
graphene::protocol::transaction
groups operations that should be applied atomically
Definition: transaction.hpp:69
fc::zero_initialized_array
Definition: zeroed_array.hpp:30
graphene::protocol::processed_transaction::merkle_digest
digest_type merkle_digest() const
Definition: transaction.cpp:36
graphene::protocol::signed_transaction::~signed_transaction
virtual ~signed_transaction()=default
graphene::protocol::transaction::set_expiration
void set_expiration(fc::time_point_sec expiration_time)
Definition: transaction.cpp:92
graphene::protocol::transaction::~transaction
virtual ~transaction()=default
graphene::protocol::transaction::_tx_id_buffer
transaction_id_type _tx_id_buffer
Definition: transaction.hpp:128
GRAPHENE_MAX_SIG_CHECK_DEPTH
#define GRAPHENE_MAX_SIG_CHECK_DEPTH
Definition: config.hpp:43
graphene::protocol::verify_authority
void verify_authority(const vector< operation > &ops, const flat_set< public_key_type > &sigs, const std::function< const authority *(account_id_type)> &get_active, const std::function< const authority *(account_id_type)> &get_owner, const custom_authority_lookup &get_custom, bool allow_non_immediate_owner, bool ignore_custom_operation_required_auths, uint32_t max_recursion=GRAPHENE_MAX_SIG_CHECK_DEPTH, bool allow_committee=false, const flat_set< account_id_type > &active_approvals=flat_set< account_id_type >(), const flat_set< account_id_type > &owner_approvals=flat_set< account_id_type >())
Definition: transaction.cpp:269
fc::ecc::private_key
an elliptic curve private key.
Definition: elliptic.hpp:89
graphene::protocol::processed_transaction::operation_results
vector< operation_result > operation_results
Definition: transaction.hpp:298
graphene::protocol::signed_transaction
adds a signature to a transaction
Definition: transaction.hpp:134
graphene::protocol::processed_transaction::~processed_transaction
virtual ~processed_transaction()=default
graphene::protocol::signed_transaction::get_signature_keys
virtual const flat_set< public_key_type > & get_signature_keys(const chain_id_type &chain_id) const
Extract public keys from signatures with given chain ID.
Definition: transaction.cpp:350
operations.hpp
FC_REFLECT_DERIVED
#define FC_REFLECT_DERIVED(TYPE, INHERITS, MEMBERS)
Specializes fc::reflector for TYPE where type inherits other reflected classes.
Definition: reflect.hpp:305
graphene::protocol::signed_transaction::signed_transaction
signed_transaction(const transaction &trx=transaction())
Definition: transaction.hpp:137
graphene::protocol::transaction::get_required_authorities
void get_required_authorities(flat_set< account_id_type > &active, flat_set< account_id_type > &owner, vector< authority > &other, bool ignore_custom_operation_required_auths) const
Definition: transaction.cpp:103
fc::time_point_sec
Definition: time.hpp:74
graphene::protocol::precomputable_transaction::_validated
bool _validated
Definition: transaction.hpp:245
fc::ripemd160
Definition: ripemd160.hpp:11
graphene::protocol::transaction::get_packed_size
virtual uint64_t get_packed_size() const
Definition: transaction.cpp:65
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
graphene::protocol::transaction::ref_block_num
uint16_t ref_block_num
Definition: transaction.hpp:76
graphene::protocol::signed_transaction::get_required_signatures
set< public_key_type > get_required_signatures(const chain_id_type &chain_id, const flat_set< public_key_type > &available_keys, const std::function< const authority *(account_id_type)> &get_active, const std::function< const authority *(account_id_type)> &get_owner, bool allow_non_immediate_owner, bool ignore_custom_operation_required_authorities, uint32_t max_recursion=GRAPHENE_MAX_SIG_CHECK_DEPTH) const
Definition: transaction.cpp:366
graphene::protocol::transaction::extensions
extensions_type extensions
Definition: transaction.hpp:90
graphene::protocol::transaction::ref_block_prefix
uint32_t ref_block_prefix
Definition: transaction.hpp:82
graphene::protocol::signed_transaction::signatures
vector< signature_type > signatures
Definition: transaction.hpp:217
graphene::protocol::processed_transaction
captures the result of evaluating the operations contained in the transaction
Definition: transaction.hpp:292
graphene::protocol::transaction::digest
digest_type digest() const
Calculate the digest for a transaction.
Definition: transaction.cpp:43
graphene::protocol::transaction::validate
virtual void validate() const
Definition: transaction.cpp:58
graphene::protocol::signed_transaction::verify_authority
void verify_authority(const chain_id_type &chain_id, const std::function< const authority *(account_id_type)> &get_active, const std::function< const authority *(account_id_type)> &get_owner, const custom_authority_lookup &get_custom, bool allow_non_immediate_owner, bool ignore_custom_operation_required_auths, uint32_t max_recursion=GRAPHENE_MAX_SIG_CHECK_DEPTH) const
Definition: transaction.cpp:464
graphene::protocol::signed_transaction::_signees
flat_set< public_key_type > _signees
Definition: transaction.hpp:226
std
Definition: zeroed_array.hpp:76
graphene::protocol::precomputable_transaction::_packed_size
uint64_t _packed_size
Definition: transaction.hpp:246
FC_REFLECT
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
graphene::protocol::processed_transaction::processed_transaction
processed_transaction(const signed_transaction &trx=signed_transaction())
Definition: transaction.hpp:294
graphene::protocol::authority
Identifies a weighted set of keys and accounts that must approve operations.
Definition: authority.hpp:34
graphene::protocol::precomputable_transaction::get_packed_size
virtual uint64_t get_packed_size() const override
Definition: transaction.cpp:448
graphene::protocol::transaction::visit
vector< typename Visitor::result_type > visit(Visitor &&visitor) const
Definition: transaction.hpp:110
graphene
Definition: api.cpp:48
graphene::protocol::custom_authority_lookup
std::function< vector< authority >(account_id_type, const operation &, rejected_predicate_map *)> custom_authority_lookup
Definition: transaction.hpp:33
graphene::protocol::extensions_type
future_extensions::flat_set_type extensions_type
Definition: base.hpp:156