diff --git a/rai/core_test/wallet.cpp b/rai/core_test/wallet.cpp index ee4e84d2..d9d11108 100644 --- a/rai/core_test/wallet.cpp +++ b/rai/core_test/wallet.cpp @@ -978,3 +978,26 @@ TEST (wallet, password_race_corrupt_seed) } } } + +TEST (wallet, utx_implicit_generate) +{ + rai::system system (24000, 1); + system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv); + rai::genesis genesis; + system.nodes [0]->ledger.utx_parse_canary = genesis.hash (); + { + rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true); + ASSERT_FALSE (system.wallet (0)->should_generate_utx (transaction, genesis.hash ())); + rai::utx_block block (rai::test_genesis_key.pub, genesis.hash (), rai::test_genesis_key.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0); + ASSERT_EQ (rai::process_result::progress, system.nodes [0]->ledger.process (transaction, block).code); + ASSERT_TRUE (system.wallet (0)->should_generate_utx (transaction, block.hash ())); + } + ASSERT_FALSE (system.wallet (0)->search_pending ()); + auto iterations (0); + while (system.nodes [0]->balance (rai::test_genesis_key.pub) != rai::genesis_amount) + { + system.poll (); + ++iterations; + ASSERT_LT (iterations, 200); + } +} diff --git a/rai/node/wallet.cpp b/rai/node/wallet.cpp index 04d62957..59e72aba 100644 --- a/rai/node/wallet.cpp +++ b/rai/node/wallet.cpp @@ -865,7 +865,7 @@ std::shared_ptr rai::wallet::receive_action (rai::block const & send { std::shared_ptr rep_block = node.ledger.store.block_get (transaction, info.rep_block); assert (rep_block != nullptr); - if (node.ledger.utx_generation_enabled (transaction)) + if (should_generate_utx (transaction, info.head)) { block.reset (new rai::utx_block (account, info.head, rep_block->representative (), info.balance.number () + pending_info.amount.number (), hash, prv, account, generate_work_a ? work_fetch (transaction, account, info.head) : 0)); } @@ -878,7 +878,7 @@ std::shared_ptr rai::wallet::receive_action (rai::block const & send { if (node.ledger.utx_generation_enabled (transaction)) { - block.reset (new rai::utx_block (account, info.head, representative_a, pending_info.amount, hash, prv, account, generate_work_a ? work_fetch (transaction, account, account) : 0)); + block.reset (new rai::utx_block (account, 0, representative_a, pending_info.amount, hash, prv, account, generate_work_a ? work_fetch (transaction, account, account) : 0)); } else { @@ -940,7 +940,7 @@ std::shared_ptr rai::wallet::change_action (rai::account const & sou rai::raw_key prv; auto error2 (store.fetch (transaction, source_a, prv)); assert (!error2); - if (node.ledger.utx_generation_enabled (transaction)) + if (should_generate_utx (transaction, info.head)) { block.reset (new rai::utx_block (source_a, info.head, representative_a, info.balance, 0, prv, source_a, generate_work_a ? work_fetch (transaction, source_a, info.head) : 0)); } @@ -1017,7 +1017,7 @@ std::shared_ptr rai::wallet::send_action (rai::account const & sourc assert (!error2); std::shared_ptr rep_block = node.ledger.store.block_get (transaction, info.rep_block); assert (rep_block != nullptr); - if (node.ledger.utx_generation_enabled (transaction)) + if (should_generate_utx (transaction, info.head)) { block.reset (new rai::utx_block (source_a, info.head, rep_block->representative (), balance - amount_a, account_a, prv, source_a, generate_work_a ? work_fetch (transaction, source_a, info.head) : 0)); } @@ -1052,6 +1052,14 @@ std::shared_ptr rai::wallet::send_action (rai::account const & sourc return block; } +bool rai::wallet::should_generate_utx (MDB_txn * transaction_a, rai::block_hash const & hash_a) +{ + auto head (node.store.block_get(transaction_a, hash_a)); + assert (head != nullptr); + auto is_utx (dynamic_cast (head.get ()) != nullptr); + return is_utx || node.ledger.utx_generation_enabled (transaction_a); +} + bool rai::wallet::change_sync (rai::account const & source_a, rai::account const & representative_a) { std::promise result; diff --git a/rai/node/wallet.hpp b/rai/node/wallet.hpp index 836aedfa..198d552e 100644 --- a/rai/node/wallet.hpp +++ b/rai/node/wallet.hpp @@ -151,6 +151,7 @@ public: void work_ensure (MDB_txn *, rai::account const &); bool search_pending (); void init_free_accounts (MDB_txn *); + bool should_generate_utx (MDB_txn *, rai::block_hash const &); /** Changes the wallet seed and returns the first account */ rai::public_key change_seed (MDB_txn * transaction_a, rai::raw_key const & prv_a); std::unordered_set free_accounts;