Added resend flag into info_for_vin, changed comparer, made test for comparer

This commit is contained in:
Alexander Suslikov 2019-02-08 18:18:41 +03:00 committed by Anzhy Cherrnyavski
parent 774e7b34cb
commit 7428c10414
6 changed files with 62 additions and 21 deletions

View file

@ -257,13 +257,11 @@ void_result bitcoin_transaction_revert_evaluator::do_apply( const bitcoin_transa
for( const auto& vin_id : btc_trx_obj.vins ) {
const auto& vin_obj = *vins_info_idx.find( vin_id );
if( trx_info.valid_vins.count( fc::sha256 ( vin_obj.out.hash_tx ) ) ) {
d.modify( vin_obj, [&]( info_for_used_vin_object& obj ){
obj.resend = true;
});
} else {
d.remove( vin_obj );
d.i_w_info.insert_info_for_vin( vin_obj.out, vin_obj.address, vin_obj.script, true );
}
d.remove( vin_obj );
}
d.bitcoin_confirmations.remove<sidechain::by_hash>( trx_info.transaction_id );
d.remove( btc_trx_obj );

View file

@ -20,8 +20,6 @@ class info_for_used_vin_object : public abstract_object<info_for_used_vin_object
sidechain::prev_out out;
std::string address;
sidechain::bytes script;
bool resend = false;
};
struct by_id;
@ -38,4 +36,4 @@ typedef generic_index<info_for_used_vin_object, info_for_used_vin_multi_index_co
} } // graphene::chain
FC_REFLECT_DERIVED( graphene::chain::info_for_used_vin_object, (graphene::chain::object), (identifier)(out)(address)(script)(resend) )
FC_REFLECT_DERIVED( graphene::chain::info_for_used_vin_object, (graphene::chain::object), (identifier)(out)(address)(script) )

View file

@ -22,7 +22,7 @@ struct info_for_vin
{
info_for_vin() = default;
info_for_vin( const prev_out& _out, const std::string& _address, bytes _script = bytes() );
info_for_vin( const prev_out& _out, const std::string& _address, bytes _script = bytes(), bool _resend = false );
bool operator!=( const info_for_vin& obj ) const;
@ -40,17 +40,18 @@ struct info_for_vin
bytes script;
bool used = false;
bool resend = false;
};
struct by_id;
struct by_identifier;
struct by_id_and_not_used;
struct by_id_resend_not_used;
using info_for_vin_index = boost::multi_index_container<info_for_vin,
indexed_by<
ordered_unique<tag<by_id>, member<info_for_vin, uint64_t, &info_for_vin::id>>,
ordered_unique<tag<by_identifier>, member<info_for_vin, fc::sha256, &info_for_vin::identifier>>,
ordered_non_unique<tag<by_id_and_not_used>, identity< info_for_vin >, info_for_vin::comparer >
ordered_non_unique<tag<by_id_resend_not_used>, identity< info_for_vin >, info_for_vin::comparer >
>
>;
@ -69,7 +70,7 @@ public:
fc::optional<info_for_vin> get_info_for_pw_vin();
void insert_info_for_vin( const prev_out& out, const std::string& address, bytes script = bytes() );
void insert_info_for_vin( const prev_out& out, const std::string& address, bytes script = bytes(), bool resend = false );
void modify_info_for_vin( const info_for_vin& obj, const std::function<void( info_for_vin& e )>& func );
@ -110,4 +111,4 @@ private:
}
FC_REFLECT( sidechain::info_for_vin, (identifier)(out)(address)(script)(used) )
FC_REFLECT( sidechain::info_for_vin, (identifier)(out)(address)(script)(used)(resend) )

View file

@ -9,8 +9,8 @@ namespace sidechain {
uint64_t info_for_vin::count_id_info_for_vin = 0;
uint64_t bitcoin_transaction_confirmations::count_id_tx_conf = 0;
info_for_vin::info_for_vin( const prev_out& _out, const std::string& _address, bytes _script ) :
id( count_id_info_for_vin++ ), out( _out ), address( _address ), script( _script )
info_for_vin::info_for_vin( const prev_out& _out, const std::string& _address, bytes _script, bool _resend ) :
id( count_id_info_for_vin++ ), out( _out ), address( _address ), script( _script ), resend( _resend )
{
identifier = fc::sha256::hash( out.hash_tx + std::to_string( out.n_vout ) );
}
@ -33,6 +33,8 @@ bool info_for_vin::comparer::operator() ( const info_for_vin& lhs, const info_fo
{
if( lhs.used != rhs.used ) {
return lhs.used < rhs.used;
} else if ( lhs.resend != rhs.resend ) {
return lhs.resend > rhs.resend;
}
return lhs.id < rhs.id;
}
@ -75,9 +77,9 @@ fc::optional<info_for_vin> input_withdrawal_info::get_info_for_pw_vin()
return vin;
}
void input_withdrawal_info::insert_info_for_vin( const prev_out& out, const std::string& address, bytes script )
void input_withdrawal_info::insert_info_for_vin( const prev_out& out, const std::string& address, bytes script, bool resend )
{
info_for_vins.insert( info_for_vin( out, address, script ) );
info_for_vins.insert( info_for_vin( out, address, script, resend ) );
}
void input_withdrawal_info::modify_info_for_vin( const info_for_vin& obj, const std::function<void( info_for_vin& e )>& func )
@ -114,8 +116,8 @@ std::vector<info_for_vin> input_withdrawal_info::get_info_for_vins()
std::vector<info_for_vin> result;
const auto max_vins = db.get_sidechain_params().maximum_condensing_tx_vins;
info_for_vins.safe_for<by_id_and_not_used>( [&]( info_for_vin_index::index<by_id_and_not_used>::type::iterator itr_b,
info_for_vin_index::index<by_id_and_not_used>::type::iterator itr_e )
info_for_vins.safe_for<by_id_resend_not_used>( [&]( info_for_vin_index::index<by_id_resend_not_used>::type::iterator itr_b,
info_for_vin_index::index<by_id_resend_not_used>::type::iterator itr_e )
{
for( size_t i = 0; itr_b != itr_e && i < max_vins && !itr_b->used; i++ ) {
info_for_vin vin;

View file

@ -227,4 +227,48 @@ BOOST_AUTO_TEST_CASE( input_withdrawal_info_get_info_for_vouts_test )
BOOST_CHECK( vouts2.size() == 3 );
}
BOOST_AUTO_TEST_CASE( info_for_vin_comparer_tests )
{
input_withdrawal_info infos( db );
auto vins_checker = [&]( std::vector<uint32_t> vins_id ) {
auto vins = infos.get_info_for_vins();
auto i = 0;
for( uint32_t j : vins_id ) {
BOOST_CHECK( vins[i].out.hash_tx == std::to_string( j ) );
BOOST_CHECK( vins[i].out.n_vout == j );
BOOST_CHECK( vins[i].out.amount == j );
BOOST_CHECK( vins[i].address == "addr" + std::to_string( j ) );
i++;
}
};
for( size_t i = 0; i < 10; i++ ) {
prev_out out = { std::to_string( i ), static_cast<uint32_t>( i ), static_cast< uint64_t >( i ) };
bool resend = ( i % 2 == 0 ) ? true : false;
infos.insert_info_for_vin( out, "addr" + std::to_string( i ), { 0x01, 0x02, 0x03 }, resend );
}
vins_checker( { 0, 2, 4, 6, 8 } );
for( size_t i = 0; i < 5; i++ ) {
auto identifier = fc::sha256::hash( std::to_string( i ) + std::to_string( i ) );
auto iter = infos.find_info_for_vin( identifier );
infos.mark_as_used_vin( *iter );
}
vins_checker( { 6, 8, 5, 7, 9 } );
for( auto i : { 6, 8 } ) {
auto identifier = fc::sha256::hash( std::to_string( i ) + std::to_string( i ) );
auto iter = infos.find_info_for_vin( identifier );
infos.mark_as_used_vin( *iter );
}
vins_checker( { 5, 7, 9 } );
for( size_t i = 10; i < 14; i++ ) {
prev_out out = { std::to_string( i ), static_cast<uint32_t>( i ), static_cast< uint64_t >( i ) };
infos.insert_info_for_vin( out, "addr" + std::to_string( i ), { 0x01, 0x02, 0x03 }, true );
}
vins_checker( { 10, 11, 12, 13, 5 } );
}
BOOST_AUTO_TEST_SUITE_END()

View file

@ -216,8 +216,6 @@ BOOST_AUTO_TEST_CASE( bitcoin_transaction_revert_operation_test )
auto vin_itr = vins_info_idx.find( fc::sha256( std::string( 64, '1' ) ) );
BOOST_CHECK( vin_itr != vins_info_idx.end() );
BOOST_CHECK( vin_itr->resend );
BOOST_CHECK( vouts_info_idx.size() == 3 );
auto vout_itr = vouts_info_idx.begin();