void client_impl::trustee_loop() { _last_block = _chain_db->get_head_block().timestamp; while (!_trustee_loop_complete.canceled()) { signed_transactions pending_trxs; pending_trxs = get_pending_transactions(); if (pending_trxs.size() && (fc::time_point::now() - _last_block) > fc::seconds(30)) { try { bts::blockchain::trx_block blk = _wallet->generate_next_block(*_chain_db, pending_trxs); blk.sign(_trustee_key); // _chain_db->push_block( blk ); if (_chain_client) _chain_client->broadcast_block(blk); else { _p2p_node->broadcast(block_message(blk.id(), blk, blk.trustee_signature)); // with the p2p code, if you broadcast something to the network, it will not // immediately send it back to you on_new_block(blk); } _last_block = fc::time_point::now(); } catch (const fc::exception& e) { elog("error producing block?: ${e}", ("e", e.to_detail_string())); } } fc::usleep(fc::seconds(1)); } }
void node::broadcast_block( const full_block& block_to_broadcast ) { ilog( "broadcasting block ${b}", ("b",block_to_broadcast.block_num) ); vector<peer_connection_ptr> broadcast_list( _peers.begin(), _peers.end() ); for( auto peer : broadcast_list ) { fc::async( [peer, block_to_broadcast](){ peer->send_message(block_message(block_to_broadcast) ); } ); } }
/** * Given the hash of the requested data, fetch the body. */ virtual message get_item(const item_id& id) override { try { // ilog("Request for item ${id}", ("id", id)); if( id.item_type == graphene::net::block_message_type ) { auto opt_block = _chain_db->fetch_block_by_id(id.item_hash); if( !opt_block ) elog("Couldn't find block ${id} -- corresponding ID in our chain is ${id2}", ("id", id.item_hash)("id2", _chain_db->get_block_id_for_num(block_header::num_from_id(id.item_hash)))); FC_ASSERT( opt_block.valid() ); // ilog("Serving up block #${num}", ("num", opt_block->block_num())); return block_message(std::move(*opt_block)); } return trx_message( _chain_db->get_recent_transaction( id.item_hash ) ); } FC_CAPTURE_AND_RETHROW( (id) ) }
/* ===================================================== */ void handle_get_block( const connection_ptr& con, chan_data& cdat, const get_block_message& msg ) { try { // TODO: charge POW for this... auto block = _name_db.fetch_block( msg.block_id ); con->send( network::message( block_message( std::move(block) ), _chan_id ) ); } FC_RETHROW_EXCEPTIONS( warn, "", ("msg",msg) ) }
void chain_client::broadcast_block( const trx_block& blk ) { my->_chain_con.send( block_message( blk ) ); }