Skip to content

Commit 1ed291a

Browse files
Mariusz-Trelavogel76
authored andcommitted
Encapsulate recurrent_transfer_extension_visitor
1 parent 23a4500 commit 1ed291a

File tree

3 files changed

+53
-39
lines changed

3 files changed

+53
-39
lines changed

libraries/chain/hive_evaluator.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,12 +3315,10 @@ void recurrent_transfer_evaluator::do_apply( const recurrent_transfer_operation&
33153315
asset available = _db.get_balance( from_account, op.amount.symbol );
33163316
FC_ASSERT( available >= op.amount, "Account does not have enough tokens for the first transfer, has ${has} needs ${needs}", ("has", available)("needs", op.amount) );
33173317

3318-
recurrent_transfer_operation::recurrent_transfer_extension_visitor vtor;
3319-
for( const auto& e : op.extensions )
3320-
e.visit( vtor );
3321-
uint8_t rtp_id = vtor.pair_id;
3318+
bool was_pair_id = false;
3319+
uint8_t rtp_id = op.get_pair_id( &was_pair_id );
33223320

3323-
FC_ASSERT( !vtor.was_pair_id || ( vtor.was_pair_id && _db.has_hardfork( HIVE_HARDFORK_1_28 ) ),
3321+
FC_ASSERT( !was_pair_id || ( was_pair_id && _db.has_hardfork( HIVE_HARDFORK_1_28 ) ),
33243322
"recurrent_transfer_pair_id extension requires hardfork ${hf}", ( "hf", HIVE_HARDFORK_1_28 ) );
33253323

33263324
const auto& rt_idx = _db.get_index< recurrent_transfer_index, by_from_to_id >();

libraries/protocol/include/hive/protocol/hive_operations.hpp

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,40 +1075,62 @@ namespace hive { namespace protocol {
10751075
*/
10761076
struct recurrent_transfer_operation : public base_operation
10771077
{
1078-
struct recurrent_transfer_extension_visitor
1079-
{
1080-
uint8_t pair_id = 0; // default recurrent transfer id is 0
1081-
bool was_pair_id = false;
1078+
private:
1079+
struct recurrent_transfer_extension_visitor
1080+
{
1081+
uint8_t pair_id = 0; // default recurrent transfer id is 0
1082+
bool was_pair_id = false;
10821083

1083-
typedef void result_type;
1084+
typedef void result_type;
10841085

1085-
void operator()( const recurrent_transfer_pair_id& recurrent_transfer_pair_id )
1086-
{
1087-
was_pair_id = true;
1088-
pair_id = recurrent_transfer_pair_id.pair_id;
1089-
}
1086+
void operator()( const recurrent_transfer_pair_id& recurrent_transfer_pair_id )
1087+
{
1088+
was_pair_id = true;
1089+
pair_id = recurrent_transfer_pair_id.pair_id;
1090+
}
10901091

1091-
void operator()( const hive::void_t& ) {}
1092-
};
1092+
void operator()( const hive::void_t& ) {}
1093+
};
10931094

1094-
account_name_type from;
1095-
/// Account to transfer asset to
1096-
account_name_type to;
1097-
/// The amount of asset to transfer from @ref from to @ref to
1098-
asset amount;
1095+
public:
10991096

1100-
string memo;
1101-
/// How often will the payment be triggered, unit: hours
1102-
uint16_t recurrence = 0;
1097+
account_name_type from;
1098+
/// Account to transfer asset to
1099+
account_name_type to;
1100+
/// The amount of asset to transfer from @ref from to @ref to
1101+
asset amount;
11031102

1104-
// How many times the recurrent payment will be executed
1105-
uint16_t executions = 0;
1103+
string memo;
1104+
/// How often will the payment be triggered, unit: hours
1105+
uint16_t recurrence = 0;
11061106

1107-
/// Extensions, only recurrent_transfer_pair_id is supported so far
1108-
recurrent_transfer_extensions_type extensions;
1107+
// How many times the recurrent payment will be executed
1108+
uint16_t executions = 0;
11091109

1110-
void validate()const;
1111-
void get_required_active_authorities( flat_set<account_name_type>& a )const{ a.insert(from); }
1110+
/// Extensions, only recurrent_transfer_pair_id is supported so far
1111+
recurrent_transfer_extensions_type extensions;
1112+
1113+
void validate()const;
1114+
void get_required_active_authorities( flat_set<account_name_type>& a )const{ a.insert(from); }
1115+
1116+
/// Returns pair id specific to given operation. explicitValue if not null will be set to true if pair_id was explicitly specified in the operation.
1117+
uint8_t get_pair_id( const recurrent_transfer_extensions_type& _extensions, bool* explicitValue = nullptr ) const
1118+
{
1119+
recurrent_transfer_extension_visitor _vtor;
1120+
1121+
for( const auto& e : _extensions )
1122+
e.visit( _vtor );
1123+
1124+
if( explicitValue )
1125+
*explicitValue = _vtor.was_pair_id;
1126+
1127+
return _vtor.pair_id;
1128+
}
1129+
1130+
uint8_t get_pair_id( bool* explicitValue = nullptr ) const
1131+
{
1132+
return get_pair_id( extensions, explicitValue );
1133+
}
11121134
};
11131135

11141136
struct witness_block_approve_operation : public base_operation

tests/unit/tests/operation_tests.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10320,10 +10320,7 @@ BOOST_AUTO_TEST_CASE( extensions_in_virtual_operations_generated_after_recurrent
1032010320
auto _recent_ops = get_last_operations( 1 );
1032110321
auto _last_op = _recent_ops.back().get< fill_recurrent_transfer_operation >();
1032210322

10323-
recurrent_transfer_operation::recurrent_transfer_extension_visitor _vtor;
10324-
for( const auto& e : _last_op.extensions )
10325-
e.visit( _vtor );
10326-
BOOST_REQUIRE( _vtor.pair_id == _pair_id_value );
10323+
BOOST_REQUIRE( op.get_pair_id( _last_op.extensions ) == _pair_id_value );
1032710324
}
1032810325

1032910326
generate_blocks( db->head_block_time() + fc::hours( op.recurrence ) );
@@ -10332,10 +10329,7 @@ BOOST_AUTO_TEST_CASE( extensions_in_virtual_operations_generated_after_recurrent
1033210329
auto _recent_ops = get_last_operations( 1 );
1033310330
auto _last_op = _recent_ops.back().get< failed_recurrent_transfer_operation >();
1033410331

10335-
recurrent_transfer_operation::recurrent_transfer_extension_visitor _vtor;
10336-
for( const auto& e : _last_op.extensions )
10337-
e.visit( _vtor );
10338-
BOOST_REQUIRE( _vtor.pair_id == _pair_id_value );
10332+
BOOST_REQUIRE( op.get_pair_id( _last_op.extensions ) == _pair_id_value );
1033910333
}
1034010334
}
1034110335
FC_LOG_AND_RETHROW()

0 commit comments

Comments
 (0)