fix rng and get_winner_numbers implemented

This commit is contained in:
kstdl 2017-11-03 17:52:41 +03:00
parent fc18c4812c
commit d7c80b4b6a
13 changed files with 99 additions and 48 deletions

2
.gitignore vendored
View file

@ -9,6 +9,8 @@ compile_commands.json
moc_*
*.moc
hardfork.hpp
build_xc
data
libraries/utilities/git_revision.cpp

View file

@ -163,23 +163,23 @@ namespace detail {
{
// t.me/peerplays #seednodes
vector<string> seeds = {
"seed.ppy.blckchnd.com:6112", // blckchnd
"ppy.esteem.ws:7777", // good-karma
"peerplays.bitcoiner.me:9777", // bitcoiner
"peerplays.roelandp.nl:9777", // roelandp
"78.46.95.153:7777", // theprophet0
"ppyseed.bacchist.me:42420", // bacchist-witness
"5.9.18.213:18828", // pfunk
"31.171.244.121:7777", // taconator
"seed.peerplaysdb.com:9777", // jesta
"ppy-seed.xeldal.com:19777", // xeldal
"peerplays-seed.altcap.io:61388", // winner.winner.chicken.dinner
"seed.peerplaysnodes.com:9777", // wackou
"peerplays-seed.privex.io:7777", // someguy123/privex
"51.15.78.16:9777", // agoric.systems
"212.71.253.163:9777", // xtar
"51.15.35.96:9777", // lafona
"anyx.ca:9777" // anyx
// "seed.ppy.blckchnd.com:6112", // blckchnd
// "ppy.esteem.ws:7777", // good-karma
// "peerplays.bitcoiner.me:9777", // bitcoiner
// "peerplays.roelandp.nl:9777", // roelandp
// "78.46.95.153:7777", // theprophet0
// "ppyseed.bacchist.me:42420", // bacchist-witness
// "5.9.18.213:18828", // pfunk
// "31.171.244.121:7777", // taconator
// "seed.peerplaysdb.com:9777", // jesta
// "ppy-seed.xeldal.com:19777", // xeldal
// "peerplays-seed.altcap.io:61388", // winner.winner.chicken.dinner
// "seed.peerplaysnodes.com:9777", // wackou
// "peerplays-seed.privex.io:7777", // someguy123/privex
// "51.15.78.16:9777", // agoric.systems
// "212.71.253.163:9777", // xtar
// "51.15.35.96:9777", // lafona
// "anyx.ca:9777" // anyx
};
for( const string& endpoint_string : seeds )
{

View file

@ -397,14 +397,13 @@ signed_block database::_generate_block(
pending_block.witness = witness_id;
// Genesis witnesses start with a default initial secret
if( witness_obj.next_secret_hash == secret_hash_type::hash( secret_hash_type() ) )
pending_block.previous_secret = secret_hash_type();
else
{
secret_hash_type::encoder last_enc;
fc::raw::pack( last_enc, block_signing_private_key );
fc::raw::pack( last_enc, witness_obj.previous_secret );
pending_block.previous_secret = last_enc.result();
if( secret_hash_type::hash( witness_obj.previous_secret ) == witness_obj.next_secret_hash ) {
pending_block.previous_secret = witness_obj.previous_secret;
} else {
secret_hash_type::encoder last_enc;
fc::raw::pack( last_enc, block_signing_private_key );
fc::raw::pack( last_enc, witness_obj.previous_secret );
pending_block.previous_secret = last_enc.result();
}
secret_hash_type::encoder next_enc;
@ -422,7 +421,7 @@ signed_block database::_generate_block(
}
push_block( pending_block, skip );
idump(( get_winner_numbers(asset_id_type(3), 253, 64) ));
return pending_block;
} FC_CAPTURE_AND_RETHROW( (witness_id) ) }
@ -688,8 +687,8 @@ const witness_object& database::validate_block_header( uint32_t skip, const sign
FC_ASSERT( head_block_time() < next_block.timestamp, "", ("head_block_time",head_block_time())("next",next_block.timestamp)("blocknum",next_block.block_num()) );
const witness_object& witness = next_block.witness(*this);
//DLN: TODO: Temporarily commented out to test shuffle vs RNG scheduling algorithm for witnesses, this was causing shuffle agorithm to fail during create_witness test. This should be re-enabled for RNG, and maybe for shuffle too, don't really know for sure.
// FC_ASSERT( secret_hash_type::hash( next_block.previous_secret ) == witness.next_secret_hash, "",
// ("previous_secret", next_block.previous_secret)("next_secret_hash", witness.next_secret_hash)("null_secret_hash", secret_hash_type::hash( secret_hash_type())));
FC_ASSERT( secret_hash_type::hash( next_block.previous_secret ) == witness.next_secret_hash, "",
("previous_secret", next_block.previous_secret)("next_secret_hash", witness.next_secret_hash));
if( !(skip&skip_witness_signature) )
FC_ASSERT( next_block.validate_signee( witness.signing_key ) );

View file

@ -30,6 +30,8 @@
#include <fc/smart_ref_impl.hpp>
#include <ctime>
namespace graphene { namespace chain {
const asset_object& database::get_core_asset() const
@ -97,5 +99,44 @@ uint32_t database::last_non_undoable_block_num() const
return head_block_num() - _undo_db.size();
}
std::vector<uint32_t> database::get_seeds(asset_id_type for_asset, uint32_t count_winners) const
{
FC_ASSERT( count_winners <= 64 );
std::string salted_string = std::string(_random_number_generator._seed) + std::to_string(for_asset.instance.value);
uint32_t* seeds = (uint32_t*)(fc::sha256::hash(salted_string)._hash);
std::vector<uint32_t> result;
result.reserve(64);
for( int s = 0; s < 8; ++s ) {
uint32_t* sub_seeds = ( uint32_t* ) fc::sha256::hash( std::to_string( seeds[s] ) + std::to_string( for_asset.instance.value ) )._hash;
for( int ss = 0; ss < 8; ++ss ) {
result.push_back(sub_seeds[ss]);
}
}
return result;
}
const std::unordered_set<uint8_t> database::get_winner_numbers( asset_id_type for_asset, uint8_t count_members, uint32_t count_winners ) const
{
std::unordered_set<uint8_t> result;
result.reserve(count_winners);
auto seeds = get_seeds(for_asset, count_winners);
for (auto current_seed = seeds.begin(); current_seed != seeds.end(); ++current_seed) {
uint8_t winner_num = *current_seed % count_members;
int iter_count = 0;
while (result.count(winner_num)) {
*current_seed = (*current_seed * 1103515245 + 12345) / 65536; //using gcc's consts for pseudorandom
winner_num = *current_seed % count_members;
}
result.emplace(winner_num);
if (result.size() >= count_winners) break;
}
FC_ASSERT(result.size() == count_winners);
return result;
}
} }

View file

@ -781,7 +781,7 @@ void database::init_genesis(const genesis_state_type& genesis_state)
std::for_each(genesis_state.initial_witness_candidates.begin(), genesis_state.initial_witness_candidates.end(),
[&](const genesis_state_type::initial_witness_type& witness) {
witness_create_operation op;
op.initial_secret = secret_hash_type::hash(secret_hash_type());
op.initial_secret = secret_hash_type::hash(witness.owner_name);
op.witness_account = get_account_id(witness.owner_name);
op.block_signing_key = witness.block_signing_key;
apply_operation(genesis_eval_state, op);

View file

@ -775,8 +775,8 @@ void schedule_pending_dividend_balances(database& db,
auto current_distribution_account_balance_iter = current_distribution_account_balance_range.first;
auto previous_distribution_account_balance_iter = previous_distribution_account_balance_range.first;
dlog("Current balances in distribution account: ${current}, Previous balances: ${previous}",
("current", std::distance(current_distribution_account_balance_range.first, current_distribution_account_balance_range.second))
("previous", std::distance(previous_distribution_account_balance_range.first, previous_distribution_account_balance_range.second)));
("current", (int64_t)std::distance(current_distribution_account_balance_range.first, current_distribution_account_balance_range.second))
("previous", (int64_t)std::distance(previous_distribution_account_balance_range.first, previous_distribution_account_balance_range.second)));
// when we pay out the dividends to the holders, we need to know the total balance of the dividend asset in all
// accounts other than the distribution account (it would be silly to distribute dividends back to

View file

@ -253,7 +253,8 @@ namespace graphene { namespace chain {
const dynamic_global_property_object& get_dynamic_global_properties()const;
const node_property_object& get_node_properties()const;
const fee_schedule& current_fee_schedule()const;
const std::unordered_set<uint8_t> get_winner_numbers(asset_id_type for_asset, uint8_t count_members, uint32_t count_winners)const;
std::vector<uint32_t> get_seeds(asset_id_type for_asset, uint32_t count_winners)const;
uint64_t get_random_bits( uint64_t bound );
time_point_sec head_block_time()const;

View file

@ -145,6 +145,10 @@ namespace graphene { namespace chain {
vesting_balance_object() {}
asset_id_type get_asset_id() const { return balance.asset_id; }
share_type get_asset_amount() const { return balance.amount; }
///@brief Deposit amount into vesting balance, requiring it to vest before withdrawal
void deposit(const fc::time_point_sec& now, const asset& amount);
bool is_deposit_allowed(const fc::time_point_sec& now, const asset& amount)const;
@ -184,8 +188,8 @@ namespace graphene { namespace chain {
ordered_non_unique< tag<by_asset_balance>,
composite_key<
vesting_balance_object,
member_offset<vesting_balance_object, asset_id_type, (size_t) (offset_s(vesting_balance_object,balance) + offset_s(asset,asset_id))>,
member_offset<vesting_balance_object, share_type, (size_t) (offset_s(vesting_balance_object,balance) + offset_s(asset,amount))>
const_mem_fun<vesting_balance_object, asset_id_type, &vesting_balance_object::get_asset_id>,
const_mem_fun<vesting_balance_object, share_type, &vesting_balance_object::get_asset_amount>
//member<vesting_balance_object, account_id_type, &vesting_balance_object::owner>
//member_offset<vesting_balance_object, account_id_type, (size_t) (offset_s(vesting_balance_object,owner))>
>,

View file

@ -43,10 +43,11 @@ object_id_type witness_create_evaluator::do_apply( const witness_create_operatio
vote_id = get_next_vote_id(p, vote_id_type::witness);
});
const auto& new_witness_object = db().create<witness_object>( [&]( witness_object& obj ){
const auto& new_witness_object = db().create<witness_object>( [&]( witness_object& obj ) {
obj.witness_account = op.witness_account;
obj.signing_key = op.block_signing_key;
obj.next_secret_hash = op.initial_secret;
obj.previous_secret = op.initial_secret;
obj.next_secret_hash = secret_hash_type::hash( op.initial_secret );
obj.vote_id = vote_id;
obj.url = op.url;
});

View file

@ -113,7 +113,7 @@ std::string modify_account_name(const std::string& name)
bool is_special_account(const graphene::chain::account_id_type& account_id)
{
return account_id.instance < 100;
return account_id.instance.value < 100;
}
bool is_scam(const std::string& account_name)

View file

@ -122,7 +122,7 @@ namespace
bool is_special_account(const graphene::chain::account_id_type& account_id)
{
return account_id.instance < 100;
return account_id.instance.value < 100;
}
bool is_scam(const std::string& account_name)

View file

@ -1762,10 +1762,11 @@ public:
witness_create_op.witness_account = witness_account.id;
witness_create_op.block_signing_key = witness_public_key;
witness_create_op.url = url;
secret_hash_type::encoder enc;
fc::raw::pack(enc, witness_private_key);
fc::raw::pack(enc, secret_hash_type());
witness_create_op.initial_secret = secret_hash_type::hash(enc.result());
// secret_hash_type::encoder enc;
// fc::raw::pack(enc, witness_private_key);
// fc::raw::pack(enc, secret_hash_type());
witness_create_op.initial_secret = secret_hash_type::hash(owner_account);
if (_remote_db->get_witness_by_account(witness_create_op.witness_account))

View file

@ -665,10 +665,12 @@ const witness_object& database_fixture::create_witness( const account_object& ow
witness_create_operation op;
op.witness_account = owner.id;
op.block_signing_key = signing_private_key.get_public_key();
secret_hash_type::encoder enc;
fc::raw::pack(enc, signing_private_key);
fc::raw::pack(enc, secret_hash_type());
op.initial_secret = secret_hash_type::hash(enc.result());
// secret_hash_type::encoder enc;
// fc::raw::pack(enc, signing_private_key);
// fc::raw::pack(enc, owner.name);
op.initial_secret = secret_hash_type::hash(owner.name);
trx.operations.push_back(op);
trx.validate();
processed_transaction ptx = db.push_transaction(trx, ~0);