database_fixture.cpp 6.3 KB
Newer Older
N
Nathan Hourt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * Copyright (c) 2017, Respective Authors.
 *
 * The MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <boost/test/unit_test.hpp>
#include <boost/program_options.hpp>

#include <eos/chain/account_object.hpp>
#include <eos/chain/producer_object.hpp>

#include <eos/utilities/tempdir.hpp>

#include <fc/crypto/digest.hpp>
#include <fc/smart_ref_impl.hpp>

#include <iostream>
#include <iomanip>
#include <sstream>

#include "database_fixture.hpp"

N
Nathan Hourt 已提交
41
uint32_t EOS_TESTING_GENESIS_TIMESTAMP = 1431700005;
N
Nathan Hourt 已提交
42 43 44

namespace eos { namespace chain {

N
Nathan Hourt 已提交
45 46
testing_fixture::testing_fixture() {
   default_genesis_state.initial_timestamp = fc::time_point_sec(EOS_TESTING_GENESIS_TIMESTAMP);
N
Nathan Hourt 已提交
47
   default_genesis_state.immutable_parameters.min_producer_count = config::ProducerCount;
N
Nathan Hourt 已提交
48
   for (int i = 0; i < default_genesis_state.immutable_parameters.min_producer_count; ++i) {
N
Nathan Hourt 已提交
49 50 51
      auto name = std::string("init") + fc::to_string(i);
      auto private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(name));
      public_key_type public_key = private_key.get_public_key();
52
      default_genesis_state.initial_accounts.emplace_back(name, 100000, public_key, public_key);
N
Nathan Hourt 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      key_ring[public_key] = private_key;

      private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(name + ".producer"));
      public_key = private_key.get_public_key();
      default_genesis_state.initial_producers.emplace_back(name, public_key);
      key_ring[public_key] = private_key;
   }
}

fc::path testing_fixture::get_temp_dir(std::string id) {
   if (id.empty()) {
      anonymous_temp_dirs.emplace_back();
      return anonymous_temp_dirs.back().path();
   }
   if (named_temp_dirs.count(id))
      return named_temp_dirs[id].path();
   return named_temp_dirs.emplace(std::make_pair(id, fc::temp_directory())).first->second.path();
}

const genesis_state_type&testing_fixture::genesis_state() const {
   return default_genesis_state;
}

genesis_state_type&testing_fixture::genesis_state() {
   return default_genesis_state;
}

private_key_type testing_fixture::get_private_key(const public_key_type& public_key) const {
   auto itr = key_ring.find(public_key);
N
Nathan Hourt 已提交
82
   EOS_ASSERT(itr != key_ring.end(), missing_key_exception,
N
Nathan Hourt 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
              "Private key corresponding to public key ${k} not known.", ("k", public_key));
   return itr->second;
}

testing_database::testing_database(testing_fixture& fixture, std::string id,
                                   fc::optional<genesis_state_type> override_genesis_state)
   : genesis_state(override_genesis_state? *override_genesis_state : fixture.genesis_state()),
     fixture(fixture) {
   data_dir = fixture.get_temp_dir(id);
}

void testing_database::open() {
   database::open(data_dir, TEST_DB_SIZE, [this]{return genesis_state;});
}

98 99
void testing_database::replay() {
   database::replay(data_dir, TEST_DB_SIZE, genesis_state);
N
Nathan Hourt 已提交
100 101 102 103 104 105
}

void testing_database::wipe(bool include_blocks) {
   database::wipe(data_dir, include_blocks);
}

N
Nathan Hourt 已提交
106 107 108 109
void testing_database::produce_blocks(uint32_t count, uint32_t blocks_to_miss) {
   if (count == 0)
      return;

N
Nathan Hourt 已提交
110 111
   BOOST_REQUIRE_MESSAGE(is_open(), "Producing blocks on closed db... Did you forget to open it?");

N
Nathan Hourt 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
   for (int i = 0; i < count; ++i) {
      auto slot = blocks_to_miss + 1;
      auto producer_id = get_scheduled_producer(slot);
      const auto& producer = get(producer_id);
      auto private_key = fixture.get_private_key(producer.signing_key);
      generate_block(get_slot_time(slot), producer_id, private_key, 0);
   }
}

void testing_database::sync_with(testing_database& other) {
   // Already in sync?
   if (head_block_id() == other.head_block_id())
      return;
   // If other has a longer chain than we do, sync it to us first
   if (head_block_num() < other.head_block_num())
      return other.sync_with(*this);

   auto sync_dbs = [](testing_database& a, testing_database& b) {
      for (int i = 1; i <= a.head_block_num(); ++i) {
         auto block = a.fetch_block_by_number(i);
         if (block && !b.is_known_block(block->id())) {
            b.push_block(*block);
         }
      }
   };

   sync_dbs(*this, other);
   sync_dbs(other, *this);
}

void testing_network::connect_database(testing_database& new_database) {
   if (databases.count(&new_database))
      return;

   // If the network isn't empty, sync the new database with one of the old ones. The old ones are already in sync with
   // eachother, so just grab one arbitrarily. The old databases are connected to the propagation signals, so when one
   // of them gets synced, it will propagate blocks to the others as well.
   if (!databases.empty()) {
      databases.begin()->first->sync_with(new_database);
   }

   // The new database is now in sync with any old ones; go ahead and connect the propagation signal.
   databases[&new_database] = new_database.applied_block.connect([this](const signed_block& block) {
      if (!currently_propagating_block)
         propagate_block(block);
   });
}

void testing_network::disconnect_database(testing_database& leaving_database) {
   databases.erase(&leaving_database);
}

void testing_network::disconnect_all() {
   databases.clear();
}

void testing_network::propagate_block(const signed_block& block) {
   currently_propagating_block = true;
   for (const auto& pair : databases)
      pair.first->push_block(block);
   currently_propagating_block = false;
}

N
Nathan Hourt 已提交
175
} } // eos::chain