Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Resolve #10: Tests Fixed
Browse files Browse the repository at this point in the history
All tests now pass again. :)
  • Loading branch information
nathanielhourt committed Apr 19, 2017
1 parent 1209d32 commit 861cd06
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion libraries/chain/database.cpp
Expand Up @@ -894,7 +894,7 @@ void database::update_last_irreversible_block()
// 1 1 1 1 1 1 1 2 2 2 -> 1
// 3 3 3 3 3 3 3 3 3 3 -> 3

size_t offset = ((EOS_100_PERCENT - EOS_IRREVERSIBLE_THRESHOLD) * producer_objs.size() / EOS_100_PERCENT);
size_t offset = EOS_PERCENT(producer_objs.size(), EOS_100_PERCENT - EOS_IRREVERSIBLE_THRESHOLD);

std::nth_element( producer_objs.begin(), producer_objs.begin() + offset, producer_objs.end(),
[]( const producer_object* a, const producer_object* b )
Expand Down
9 changes: 8 additions & 1 deletion libraries/chain/include/eos/chain/config.hpp
Expand Up @@ -29,13 +29,20 @@
/** percentage fields are fixed point with a denominator of 10,000 */
#define EOS_100_PERCENT 10000
#define EOS_1_PERCENT (EOS_100_PERCENT/100)

#define EOS_DEFAULT_MAX_BLOCK_SIZE (256*1024)

#define EOS_DEFAULT_PRODUCER_PAY_PER_BLOCK (EOS_BLOCKCHAIN_PRECISION * int64_t(10))
#define EOS_DEFAULT_MAX_TIME_UNTIL_EXPIRATION (60*60)

#define EOS_IRREVERSIBLE_THRESHOLD (70 * EOS_1_PERCENT)

template<typename Number>
Number EOS_PERCENT(Number value, int percentage) {
return value * percentage / EOS_100_PERCENT;
}

namespace eos { namespace config {
const static int EOS_PRODUCER_COUNT = 21;
const static int ProducerCount = 21;
const static int IrreversibleThreshold = 70 * EOS_1_PERCENT;
} } // namespace eos::config
Expand Up @@ -50,7 +50,7 @@ namespace eos { namespace chain {
id_type id;
chain_parameters parameters;

std::array<producer_id_type, config::EOS_PRODUCER_COUNT> active_producers;
std::array<producer_id_type, config::ProducerCount> active_producers;
};


Expand Down
Expand Up @@ -33,7 +33,7 @@ namespace eos { namespace chain {

struct immutable_chain_parameters
{
uint16_t min_producer_count = config::EOS_PRODUCER_COUNT;
uint16_t min_producer_count = config::ProducerCount;
};

} } // eos::chain
Expand Down
2 changes: 1 addition & 1 deletion tests/common/database_fixture.cpp
Expand Up @@ -44,7 +44,7 @@ namespace eos { namespace chain {

testing_fixture::testing_fixture() {
default_genesis_state.initial_timestamp = fc::time_point_sec(EOS_TESTING_GENESIS_TIMESTAMP);
default_genesis_state.immutable_parameters.min_producer_count = 11;
default_genesis_state.immutable_parameters.min_producer_count = config::ProducerCount;
for (int i = 0; i < default_genesis_state.immutable_parameters.min_producer_count; ++i) {
auto name = std::string("init") + fc::to_string(i);
auto private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(name));
Expand Down
39 changes: 23 additions & 16 deletions tests/tests/block_tests.cpp
Expand Up @@ -34,7 +34,8 @@

#include "../common/database_fixture.hpp"

using namespace eos::chain;
using namespace eos;
using namespace chain;

BOOST_AUTO_TEST_SUITE(block_tests)

Expand Down Expand Up @@ -266,18 +267,20 @@ BOOST_FIXTURE_TEST_CASE( rsf_missed_blocks, testing_fixture )
BOOST_FIXTURE_TEST_CASE(restart_db, testing_fixture)
{ try {
MKDB(db)
db.produce_blocks(10);

BOOST_CHECK_EQUAL(db.head_block_num(), 10);
BOOST_CHECK_EQUAL(db.last_irreversible_block_num(), 3);
auto lag = EOS_PERCENT(config::ProducerCount, config::IrreversibleThreshold);
db.produce_blocks(20);

BOOST_CHECK_EQUAL(db.head_block_num(), 20);
BOOST_CHECK_EQUAL(db.last_irreversible_block_num(), 20 - lag);

db.close();
db.open();

// After restarting, we should have rewound to the last irreversible block.
BOOST_CHECK_EQUAL(db.head_block_num(), 3);
BOOST_CHECK_EQUAL(db.head_block_num(), 20 - lag);
db.produce_blocks(5);
BOOST_CHECK_EQUAL(db.head_block_num(), 8);
BOOST_CHECK_EQUAL(db.head_block_num(), 25 - lag);
} FC_LOG_AND_RETHROW() }

// Check that a db which is closed and reopened successfully syncs back with the network, including retrieving blocks
Expand All @@ -286,45 +289,49 @@ BOOST_FIXTURE_TEST_CASE(sleepy_db, testing_fixture)
{ try {
MKDB(producer)
MKNET(net, (producer))
// Produce 10 blocks on the chain
producer.produce_blocks(10);

auto lag = EOS_PERCENT(config::ProducerCount, config::IrreversibleThreshold);
producer.produce_blocks(20);

{
// The new node, sleepy, joins, syncs, disconnects
MKDB(sleepy, sleepy)
net.connect_database(sleepy);
BOOST_CHECK_EQUAL(producer.head_block_num(), 10);
BOOST_CHECK_EQUAL(sleepy.head_block_num(), 10);
BOOST_CHECK_EQUAL(producer.head_block_num(), 20);
BOOST_CHECK_EQUAL(sleepy.head_block_num(), 20);

net.disconnect_database(sleepy);
sleepy.close();
}

// 5 new blocks are produced
producer.produce_blocks(5);
BOOST_CHECK_EQUAL(producer.head_block_num(), 15);
BOOST_CHECK_EQUAL(producer.head_block_num(), 25);

// Sleepy is reborn! Check that it is now rewound to the LIB...
MKDB(sleepy, sleepy)
BOOST_CHECK_EQUAL(sleepy.head_block_num(), 3);
BOOST_CHECK_EQUAL(sleepy.head_block_num(), 20 - lag);

// Reconnect sleepy to the network and check that it syncs up to the present
net.connect_database(sleepy);
BOOST_CHECK_EQUAL(sleepy.head_block_num(), 15);
BOOST_CHECK_EQUAL(sleepy.head_block_num(), 25);
BOOST_CHECK_EQUAL(sleepy.head_block_id().str(), producer.head_block_id().str());
} FC_LOG_AND_RETHROW() }

// Test reindexing the blockchain
BOOST_FIXTURE_TEST_CASE(reindex, testing_fixture)
{ try {
MKDB(db)

auto lag = EOS_PERCENT(config::ProducerCount, config::IrreversibleThreshold);
db.produce_blocks(100);
BOOST_CHECK_EQUAL(db.last_irreversible_block_num(), 93);

BOOST_CHECK_EQUAL(db.last_irreversible_block_num(), 100 - lag);
db.close();
db.reindex();
BOOST_CHECK_EQUAL(db.head_block_num(), 93);
BOOST_CHECK_EQUAL(db.head_block_num(), 100 - lag);
db.produce_blocks(20);
BOOST_CHECK_EQUAL(db.head_block_num(), 113);
BOOST_CHECK_EQUAL(db.head_block_num(), 120 - lag);
} FC_LOG_AND_RETHROW() }

// Test wiping a database and resyncing with an ongoing network
Expand Down

0 comments on commit 861cd06

Please sign in to comment.