From 241c738f114bcbb43e792d07d36f4ba306768a0d Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Tue, 4 Jul 2017 12:29:57 -0500 Subject: [PATCH] Fix setproducer regression --- .../include/eos/chain/chain_controller.hpp | 3 +- .../staked_balance_contract.cpp | 70 +++++++------------ tests/common/macro_support.hpp | 16 ++++- 3 files changed, 42 insertions(+), 47 deletions(-) diff --git a/libraries/chain/include/eos/chain/chain_controller.hpp b/libraries/chain/include/eos/chain/chain_controller.hpp index 2e8cc5d28..d3911dab0 100644 --- a/libraries/chain/include/eos/chain/chain_controller.hpp +++ b/libraries/chain/include/eos/chain/chain_controller.hpp @@ -156,7 +156,8 @@ namespace eos { namespace chain { auto on_exit = fc::make_scoped_exit( [&](){ for( const auto& t : old_pending ) { try { - push_transaction( t ); + if (!is_known_transaction(t.id())) + push_transaction( t ); } catch ( ... ){} } }); diff --git a/libraries/native_contract/staked_balance_contract.cpp b/libraries/native_contract/staked_balance_contract.cpp index b545ac55f..7183cbbba 100644 --- a/libraries/native_contract/staked_balance_contract.cpp +++ b/libraries/native_contract/staked_balance_contract.cpp @@ -81,58 +81,42 @@ void apply_staked_claim(apply_context& context) { } void validate_staked_setproducer(message_validate_context& context) { - auto create = context.msg.as(); - EOS_ASSERT(create.name.good(), message_validate_exception, "Producer owner name cannot be empty"); + auto update = context.msg.as(); + EOS_ASSERT(update.name.good(), message_validate_exception, "Producer owner name cannot be empty"); } void precondition_staked_setproducer(precondition_validate_context& context) { - auto create = context.msg.as(); + auto update = context.msg.as(); const auto& db = context.db; - auto producer = db.find(create.name); - EOS_ASSERT(producer == nullptr, message_precondition_exception, - "Account ${name} already has a block producer", ("name", create.name)); + auto producer = db.find(update.name); + if (producer) + EOS_ASSERT(producer->signing_key != update.key || producer->configuration != update.configuration, + message_validate_exception, "Producer's new settings may not be identical to old settings"); } void apply_staked_setproducer(apply_context& context) { - auto create = context.msg.as(); - auto& db = context.mutable_db; - db.create([&create](producer_object& p) { - p.owner = create.name; - p.signing_key = create.key; - p.configuration = create.configuration; - }); - auto raceTime = ProducerScheduleObject::get(db).currentRaceTime; - db.create([&create, &raceTime](ProducerVotesObject& pvo) { - pvo.ownerName = create.name; - pvo.startNewRaceLap(raceTime); - }); -} - -/* -void UpdateProducer::validate(message_validate_context& context) { - auto update = context.msg.as(); - EOS_ASSERT(update.name.good(), message_validate_exception, "Producer owner name cannot be empty"); -} - -void UpdateProducer::validate_preconditions(precondition_validate_context& context) { - const auto& db = context.db; - auto update = context.msg.as(); - const auto& producer = db.get(update.name); - EOS_ASSERT(producer.signing_key != update.newKey || producer.configuration != update.configuration, - message_validate_exception, "Producer's new settings may not be identical to old settings"); -} - -void UpdateProducer::apply(apply_context& context) { + auto update = context.msg.as(); auto& db = context.mutable_db; - auto update = context.msg.as(); - const auto& producer = db.get(update.name); - - db.modify(producer, [&update](producer_object& p) { - p.signing_key = update.newKey; - p.configuration = update.configuration; - }); + auto producer = db.find(update.name); + + if (producer) + db.modify(*producer, [&update](producer_object& p) { + p.signing_key = update.key; + p.configuration = update.configuration; + }); + else { + db.create([&update](producer_object& p) { + p.owner = update.name; + p.signing_key = update.key; + p.configuration = update.configuration; + }); + auto raceTime = ProducerScheduleObject::get(db).currentRaceTime; + db.create([name = update.name, raceTime](ProducerVotesObject& pvo) { + pvo.ownerName = name; + pvo.startNewRaceLap(raceTime); + }); + } } -*/ void validate_staked_okproducer(message_validate_context& context) { auto approve = context.msg.as(); diff --git a/tests/common/macro_support.hpp b/tests/common/macro_support.hpp index bb6b04ec7..8865f8694 100644 --- a/tests/common/macro_support.hpp +++ b/tests/common/macro_support.hpp @@ -8,16 +8,18 @@ block_log name ## _log(get_temp_dir() / "blocklog"); \ fork_database name ## _fdb; \ native_contract::native_contract_chain_initializer name ## _initializer(genesis_state()); \ - testing_blockchain name(name ## _db, name ## _fdb, name ## _log, name ## _initializer, *this); + testing_blockchain name(name ## _db, name ## _fdb, name ## _log, name ## _initializer, *this); \ + BOOST_TEST_CHECKPOINT("Created blockchain " << #name); #define MKCHAIN2(name, id) \ chainbase::database name ## _db(get_temp_dir(#id), chainbase::database::read_write, TEST_DB_SIZE); \ block_log name ## _log(get_temp_dir(#id) / "blocklog"); \ fork_database name ## _fdb; \ native_contract::native_contract_chain_initializer name ## _initializer(genesis_state()); \ - testing_blockchain name(name ## _db, name ## _fdb, name ## _log, name ## _initializer, *this); + testing_blockchain name(name ## _db, name ## _fdb, name ## _log, name ## _initializer, *this); \ + BOOST_TEST_CHECKPOINT("Created blockchain " << #name); #define MKCHAINS_MACRO(x, y, name) Make_Blockchain(name) -#define MKNET1(name) testing_network name; +#define MKNET1(name) testing_network name; BOOST_TEST_CHECKPOINT("Created testnet " << #name); #define MKNET2_MACRO(x, name, chain) name.connect_blockchain(chain); #define MKNET2(name, ...) MKNET1(name) BOOST_PP_SEQ_FOR_EACH(MKNET2_MACRO, name, __VA_ARGS__) @@ -31,6 +33,7 @@ trx.expiration = chain.head_block_time() + 100; \ trx.set_reference_block(chain.head_block_id()); \ chain.push_transaction(trx); \ + BOOST_TEST_CHECKPOINT("Created account " << #name); \ } #define MKACCT2(chain, name) \ Make_Key(name) \ @@ -60,6 +63,7 @@ trx.expiration = chain.head_block_time() + 100; \ trx.set_reference_block(chain.head_block_id()); \ chain.push_transaction(trx); \ + BOOST_TEST_CHECKPOINT("Transfered " << amount << " from " << #sender << " to " << #recipient); \ } #define XFER4(chain, sender, recipient, amount) XFER5(chain, sender, recipient, amount, "") @@ -75,6 +79,7 @@ trx.expiration = chain.head_block_time() + 100; \ trx.set_reference_block(chain.head_block_id()); \ chain.push_transaction(trx); \ + BOOST_TEST_CHECKPOINT("Staked " << amount << " to " << #recipient); \ } #define STAKE3(chain, account, amount) STAKE4(chain, account, account, amount) @@ -86,6 +91,7 @@ trx.expiration = chain.head_block_time() + 100; \ trx.set_reference_block(chain.head_block_id()); \ chain.push_transaction(trx); \ + BOOST_TEST_CHECKPOINT("Begin unstake " << amount << " to " << #account); \ } #define FINISH_UNSTAKE3(chain, account, amount) \ @@ -96,6 +102,7 @@ trx.expiration = chain.head_block_time() + 100; \ trx.set_reference_block(chain.head_block_id()); \ chain.push_transaction(trx); \ + BOOST_TEST_CHECKPOINT("Finish unstake " << amount << " to " << #account); \ } #define MKPDCR4(chain, owner, key, cfg) \ @@ -106,6 +113,7 @@ trx.expiration = chain.head_block_time() + 100; \ trx.set_reference_block(chain.head_block_id()); \ chain.push_transaction(trx); \ + BOOST_TEST_CHECKPOINT("Create producer " << #owner); \ } #define MKPDCR3(chain, owner, key) MKPDCR4(chain, owner, key, BlockchainConfiguration{}); #define MKPDCR2(chain, owner) \ @@ -120,6 +128,7 @@ trx.expiration = chain.head_block_time() + 100; \ trx.set_reference_block(chain.head_block_id()); \ chain.push_transaction(trx); \ + BOOST_TEST_CHECKPOINT("Set producer approval from " << #voter << " for " << #producer << " to " << approved); \ } #define UPPDCR4(chain, owner, key, cfg) \ @@ -130,5 +139,6 @@ trx.expiration = chain.head_block_time() + 100; \ trx.set_reference_block(chain.head_block_id()); \ chain.push_transaction(trx); \ + BOOST_TEST_CHECKPOINT("Update producer " << #owner); \ } #define UPPDCR3(chain, owner, key) UPPDCR4(chain, owner, key, chain.get_producer(owner).configuration) -- GitLab