提交 366219e9 编写于 作者: P Phil Mesnier

Adding in implementation for sharing peer addresses between the network nodes.

also adding interpretation of a --genesis-timestamp command line option to override that provided in the genesis.json file.
format of the timestamp value is either the ISO format yyyy-mm-ddThh:mm:ss (UTC) or the word now for using the current time
as the timestamp.
上级 7c61282e
......@@ -3,6 +3,7 @@
#include <eos/chain/block_log.hpp>
#include <eos/chain/exceptions.hpp>
#include <eos/chain/producer_object.hpp>
#include <eos/chain/config.hpp>
#include <eos/native_contract/native_contract_chain_initializer.hpp>
#include <eos/native_contract/native_contract_chain_administrator.hpp>
......@@ -24,6 +25,7 @@ class chain_plugin_impl {
public:
bfs::path block_log_dir;
bfs::path genesis_file;
chain::Time genesis_timestamp;
bool readonly = false;
flat_map<uint32_t,block_id_type> loaded_checkpoints;
......@@ -44,6 +46,7 @@ void chain_plugin::set_program_options(options_description& cli, options_descrip
{
cfg.add_options()
("genesis-json", bpo::value<boost::filesystem::path>(), "File to read Genesis State from")
("genesis-timestamp", bpo::value<string>(), "override the initial timestamp in the Genesis State file")
("block-log-dir", bpo::value<bfs::path>()->default_value("blocks"),
"the location of the block log (absolute path or relative to application data dir)")
("checkpoint,c", bpo::value<vector<string>>()->composing(), "Pairs of [BLOCK_NUM,BLOCK_ID] that should be enforced as checkpoints.")
......@@ -62,6 +65,21 @@ void chain_plugin::plugin_initialize(const variables_map& options) {
if(options.count("genesis-json")) {
my->genesis_file = options.at("genesis-json").as<bfs::path>();
}
if(options.count("genesis-timestamp")) {
string tstr = options.at("genesis-timestamp").as<string>();
if (strcasecmp (tstr.c_str(), "now") == 0) {
my->genesis_timestamp = fc::time_point::now();
auto diff = my->genesis_timestamp.sec_since_epoch() % config::BlockIntervalSeconds;
if (diff > 0) {
auto delay = (config::BlockIntervalSeconds - diff);
my->genesis_timestamp += delay;
dlog ("pausing ${s} seconds to the next interval",("s",delay));
}
}
else {
my->genesis_timestamp = chain::Time::from_iso_string (tstr);
}
}
if (options.count("block-log-dir")) {
auto bld = options.at("block-log-dir").as<bfs::path>();
if(bld.is_relative())
......@@ -96,6 +114,11 @@ void chain_plugin::plugin_startup() {
auto& db = app().get_plugin<database_plugin>().db();
auto genesis = fc::json::from_file(my->genesis_file).as<native_contract::genesis_state_type>();
if (my->genesis_timestamp.sec_since_epoch() > 0) {
genesis.initial_timestamp = my->genesis_timestamp;
}
native_contract::native_contract_chain_initializer initializer(genesis);
my->fork_db = fork_database();
......@@ -109,7 +132,8 @@ void chain_plugin::plugin_startup() {
my->chain->add_checkpoints(my->loaded_checkpoints);
}
ilog("Blockchain started; head block is #${num}", ("num", my->chain->head_block_num()));
ilog("Blockchain started; head block is #${num}, genesis timestamp is ${ts}",
("num", my->chain->head_block_num())("ts", genesis.initial_timestamp.to_iso_string()));
}
void chain_plugin::plugin_shutdown() {
......
......@@ -115,7 +115,7 @@ namespace eos {
sync_request_index in_sync_state;
sync_request_index out_sync_state;
socket_ptr socket;
vector<fc::ip::endpoint> shared_peers;
std::set<fc::ip::endpoint> shared_peers;
uint32_t pending_message_size;
vector<char> pending_message_buffer;
......@@ -202,7 +202,7 @@ namespace eos {
try {
fc::optional<signed_block> sb = cc.fetch_block_by_number(num);
if (sb) {
dlog("write backlog, block #${num}",("num",num));
// dlog("write backlog, block #${num}",("num",num));
send( *sb );
}
} catch ( ... ) {
......@@ -393,14 +393,17 @@ namespace eos {
return fc::ip::endpoint (addr,ep.port());
}
void send_peer_list () {
void send_peer_list_to (connection &conn) {
peer_message pm;
pm.peers.resize(connections.size());
for (auto &c : connections) {
pm.peers.push_back (asio_to_fc(c->socket->remote_endpoint()));
fc::ip::endpoint remote = asio_to_fc(c->socket->remote_endpoint());
if (conn.shared_peers.find(remote) == conn.shared_peers.end()) {
pm.peers.push_back(remote);
}
}
for (auto c : connections) {
c->send (pm);
if (!pm.peers.empty()) {
conn.send (pm);
}
}
......@@ -437,7 +440,7 @@ namespace eos {
if (!hello) {
init_handshake();
}
dlog ("got a handshake message");
// dlog ("got a handshake message");
if (msg.node_id == hello->node_id) {
elog ("Self connection detected. Closing connection");
close(c);
......@@ -463,8 +466,9 @@ namespace eos {
}
void handle_message (connection_ptr c, const peer_message &msg) {
dlog ("got a peer message");
// dlog ("got a peer message");
for (auto fcep : msg.peers) {
c->shared_peers.insert (fcep);
tcp::endpoint ep = fc_to_asio (fcep);
if (ep == listen_endpoint || ep == public_endpoint) {
continue;
......@@ -485,29 +489,31 @@ namespace eos {
}
void handle_message (connection_ptr c, const notice_message &msg) {
dlog ("got a notice message");
// dlog ("got a notice message");
chain_controller &cc = chain_plug->chain();
for (const auto& b : msg.known_blocks) {
if (! cc.is_known_block (b)) {
c->block_state.insert((block_state){b,true,true,fc::time_point()});
c->block_state.insert((block_state){b,true,true,fc::time_point()});
}
}
for (const auto& t : msg.known_trx) {
if (!cc.is_known_transaction (t)) {
c->trx_state.insert((transaction_state){t,true,true,(uint32_t)-1,fc::time_point(),fc::time_point()});
c->trx_state.insert((transaction_state){t,true,true,(uint32_t)-1,
fc::time_point(),fc::time_point()});
}
}
}
void handle_message (connection_ptr c, const sync_request_message &msg) {
dlog ("got a sync request message for blocks ${s} to ${e}", ("s",msg.start_block)("e", msg.end_block));
// og ("got a sync request message for blocks ${s} to ${e}",
// ("s",msg.start_block)("e", msg.end_block));
sync_state req = {msg.start_block,msg.end_block,msg.start_block-1,time_point::now()};
c->out_sync_state.insert (req);
c->write_block_backlog ();
}
void handle_message (connection_ptr c, const block_summary_message &msg) {
//dlog ("got a block summary message");
// dlog ("got a block summary message");
#warning TODO: reconstruct actual block from cached transactions
chain_controller &cc = chain_plug->chain();
if (cc.is_known_block(msg.block.id())) {
......@@ -515,7 +521,7 @@ namespace eos {
block_state value = *itr.find(msg.block.id());
value.is_known=true;
c->block_state.insert (std::move(value));
//dlog ("block id ${id} is known", ("id", msg.block.id()) );
// dlog ("block id ${id} is known", ("id", msg.block.id()) );
return;
}
try {
......@@ -541,7 +547,7 @@ namespace eos {
chain_controller &cc = chain_plug->chain();
if (cc.is_known_block(msg.id())) {
dlog ("block id ${id} is known", ("id", msg.id()) );
// dlog ("block id ${id} is known", ("id", msg.id()) );
return;
}
uint32_t num = 0;
......@@ -617,7 +623,7 @@ namespace eos {
}
static void pending_txn (const SignedTransaction& txn) {
dlog ("got signaled of txn!");
// dlog ("got signaled of txn!");
}
}; // class net_plugin_impl
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册