notifications.cpp raw

   1  // Copyright (c) 2021-present The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <addresstype.h>
   6  #include <consensus/amount.h>
   7  #include <interfaces/chain.h>
   8  #include <kernel/chain.h>
   9  #include <outputtype.h>
  10  #include <policy/feerate.h>
  11  #include <policy/policy.h>
  12  #include <primitives/block.h>
  13  #include <primitives/transaction.h>
  14  #include <script/descriptor.h>
  15  #include <script/script.h>
  16  #include <script/signingprovider.h>
  17  #include <sync.h>
  18  #include <test/fuzz/FuzzedDataProvider.h>
  19  #include <test/fuzz/fuzz.h>
  20  #include <test/fuzz/util.h>
  21  #include <test/fuzz/util/wallet.h>
  22  #include <test/util/transaction_utils.h>
  23  #include <test/util/setup_common.h>
  24  #include <tinyformat.h>
  25  #include <uint256.h>
  26  #include <util/check.h>
  27  #include <util/result.h>
  28  #include <util/time.h>
  29  #include <util/translation.h>
  30  #include <wallet/coincontrol.h>
  31  #include <wallet/context.h>
  32  #include <wallet/fees.h>
  33  #include <wallet/receive.h>
  34  #include <wallet/spend.h>
  35  #include <wallet/test/util.h>
  36  #include <wallet/wallet.h>
  37  #include <wallet/walletutil.h>
  38  
  39  #include <cstddef>
  40  #include <cstdint>
  41  #include <limits>
  42  #include <numeric>
  43  #include <set>
  44  #include <string>
  45  #include <tuple>
  46  #include <utility>
  47  #include <vector>
  48  
  49  namespace wallet {
  50  namespace {
  51  const TestingSetup* g_setup;
  52  
  53  void initialize_setup()
  54  {
  55      static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
  56      g_setup = testing_setup.get();
  57  }
  58  
  59  FUZZ_TARGET(wallet_notifications, .init = initialize_setup)
  60  {
  61      SeedRandomStateForTest(SeedRand::ZEROS);
  62      FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
  63      SetMockTime(ConsumeTime(fuzzed_data_provider));
  64      // The total amount, to be distributed to the wallets a and b in txs
  65      // without fee. Thus, the balance of the wallets should always equal the
  66      // total amount.
  67      const auto total_amount{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY / 100000)};
  68      FuzzedWallet a{
  69          *g_setup->m_node.chain,
  70          "fuzzed_wallet_a",
  71          "tprv8ZgxMBicQKsPd1QwsGgzfu2pcPYbBosZhJknqreRHgsWx32nNEhMjGQX2cgFL8n6wz9xdDYwLcs78N4nsCo32cxEX8RBtwGsEGgybLiQJfk",
  72      };
  73      FuzzedWallet b{
  74          *g_setup->m_node.chain,
  75          "fuzzed_wallet_b",
  76          "tprv8ZgxMBicQKsPfCunYTF18sEmEyjz8TfhGnZ3BoVAhkqLv7PLkQgmoG2Ecsp4JuqciWnkopuEwShit7st743fdmB9cMD4tznUkcs33vK51K9",
  77      };
  78  
  79      // Keep track of all coins in this test.
  80      // Each tuple in the chain represents the coins and the block created with
  81      // those coins. Once the block is mined, the next tuple will have an empty
  82      // block and the freshly mined coins.
  83      using Coins = std::set<std::tuple<CAmount, COutPoint>>;
  84      std::vector<std::tuple<Coins, CBlock>> chain;
  85      {
  86          // Add the initial entry
  87          chain.emplace_back();
  88          auto& [coins, block]{chain.back()};
  89          coins.emplace(total_amount, COutPoint{Txid::FromUint256(uint256::ONE), 1});
  90      }
  91      LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 20)
  92      {
  93          CallOneOf(
  94              fuzzed_data_provider,
  95              [&] {
  96                  auto& [coins_orig, block]{chain.back()};
  97                  // Copy the coins for this block and consume all of them
  98                  Coins coins = coins_orig;
  99                  while (!coins.empty()) {
 100                      // Create a new tx
 101                      CMutableTransaction tx{};
 102                      // Add some coins as inputs to it
 103                      auto num_inputs{fuzzed_data_provider.ConsumeIntegralInRange<int>(1, coins.size())};
 104                      CAmount in{0};
 105                      while (num_inputs-- > 0) {
 106                          const auto& [coin_amt, coin_outpoint]{*coins.begin()};
 107                          in += coin_amt;
 108                          tx.vin.emplace_back(coin_outpoint);
 109                          coins.erase(coins.begin());
 110                      }
 111                      // Create some outputs spending all inputs, without fee
 112                      LIMITED_WHILE(in > 0 && fuzzed_data_provider.ConsumeBool(), 10)
 113                      {
 114                          const auto out_value{ConsumeMoney(fuzzed_data_provider, in)};
 115                          in -= out_value;
 116                          auto& wallet{fuzzed_data_provider.ConsumeBool() ? a : b};
 117                          tx.vout.emplace_back(out_value, wallet.GetScriptPubKey(fuzzed_data_provider));
 118                      }
 119                      // Spend the remaining input value, if any
 120                      auto& wallet{fuzzed_data_provider.ConsumeBool() ? a : b};
 121                      tx.vout.emplace_back(in, wallet.GetScriptPubKey(fuzzed_data_provider));
 122                      // Add tx to block
 123                      block.vtx.emplace_back(MakeTransactionRef(tx));
 124                      // Check that funding the tx doesn't crash the wallet
 125                      a.FundTx(fuzzed_data_provider, tx);
 126                      b.FundTx(fuzzed_data_provider, tx);
 127                  }
 128                  // Mine block
 129                  const uint256& hash = block.GetHash();
 130                  interfaces::BlockInfo info{hash};
 131                  info.prev_hash = &block.hashPrevBlock;
 132                  info.height = chain.size();
 133                  info.data = &block;
 134                  // Ensure that no blocks are skipped by the wallet by setting the chain's accumulated
 135                  // time to the maximum value. This ensures that the wallet's birth time is always
 136                  // earlier than this maximum time.
 137                  info.chain_time_max = std::numeric_limits<unsigned int>::max();
 138                  a.wallet->blockConnected(ChainstateRole::NORMAL, info);
 139                  b.wallet->blockConnected(ChainstateRole::NORMAL, info);
 140                  // Store the coins for the next block
 141                  Coins coins_new;
 142                  for (const auto& tx : block.vtx) {
 143                      uint32_t i{0};
 144                      for (const auto& out : tx->vout) {
 145                          coins_new.emplace(out.nValue, COutPoint{tx->GetHash(), i++});
 146                      }
 147                  }
 148                  chain.emplace_back(coins_new, CBlock{});
 149              },
 150              [&] {
 151                  if (chain.size() <= 1) return; // The first entry can't be removed
 152                  auto& [coins, block]{chain.back()};
 153                  if (block.vtx.empty()) return; // Can only disconnect if the block was submitted first
 154                  // Disconnect block
 155                  const uint256& hash = block.GetHash();
 156                  interfaces::BlockInfo info{hash};
 157                  info.prev_hash = &block.hashPrevBlock;
 158                  info.height = chain.size() - 1;
 159                  info.data = &block;
 160                  a.wallet->blockDisconnected(info);
 161                  b.wallet->blockDisconnected(info);
 162                  chain.pop_back();
 163              });
 164          auto& [coins, first_block]{chain.front()};
 165          if (!first_block.vtx.empty()) {
 166              // Only check balance when at least one block was submitted
 167              const auto bal_a{GetBalance(*a.wallet).m_mine_trusted};
 168              const auto bal_b{GetBalance(*b.wallet).m_mine_trusted};
 169              assert(total_amount == bal_a + bal_b);
 170          }
 171      }
 172  }
 173  } // namespace
 174  } // namespace wallet
 175