BTS witness_node读书笔记

根目录下各文件夹:
app:

chian:

db:

deterministic_openssl_rand:

egenesis:

net:

plugins:

utilities:

wallet:

witness初始化过程:
1. app::application* node = new app::application();创建一个application对象,其中my->_self包含自己 app -> details

2. fc::oexception unhandled_exception;猜测类似开关

3.   bpo::options_description app_options("Graphene Witness Node");
      bpo::options_description cfg_options("Graphene Witness Node");
      app_options.add_options()
            ("help,h", "Print this help message and exit.")
            ("data-dir,d", bpo::value<boost::filesystem::path>()->default_value("witness_node_data_dir"), "Directory containing databases, configuration file, etc.")
            ("version,v", "Display version information")
            ;增加cli选项
4.   auto witness_plug = node->register_plugin<witness_plugin::witness_plugin>();
      auto debug_witness_plug = node->register_plugin<debug_witness_plugin::debug_witness_plugin>();
      auto history_plug = node->register_plugin<account_history::account_history_plugin>();
      auto elasticsearch_plug = node->register_plugin<elasticsearch::elasticsearch_plugin>();
      auto market_history_plug = node->register_plugin<market_history::market_history_plugin>();
      auto delayed_plug = node->register_plugin<delayed_node::delayed_node_plugin>();
      auto snapshot_plug = node->register_plugin<snapshot_plugin::snapshot_plugin>();
      auto es_objects_plug = node->register_plugin<es_objects::es_objects_plugin>();
      auto grouped_orders_plug = node->register_plugin<grouped_orders::grouped_orders_plugin>();
注册节点插件, 主要是cli cfg的选项读取
注册witness_plugin有个例子:
("enable-stale-production", bpo::bool_switch()->notifier([this](bool e){_production_enabled = e;}), "Enable block production, even if the chain is stale.") cli键入了enable-stale-production之后bool_switch会置true,并运行匿名函数配置_production_enabled,函数的第三个参数是说明文字
my->_available_plugins[p->plugin_name()] = p;添加plugin
void witness_plugin::plugin_set_program_options(command_line_options, config_file_options)
     auto default_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(std::string("nathan")));
         self.my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 );  Creates a new EC_KEY object using a named curve as underlying EC_GROUP object.
         BN_bin2bn( (const unsigned char*)&secret, 32, bn );  产生priK, BN_bin2bn() converts the positive integer in big-endian form of length len at s into a BIGNUM and places it in ret. If ret is NULL, a new BIGNUM is created.
              EC_KEY_regenerate_key(self.my->_key,bn)
                   EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx) Computes r(pub_key) = generator * n(priv_key) + q(NULL) * m(NULL)
                   EC_KEY_set_private_key(eckey,priv_key); 配置私钥
                   EC_KEY_set_public_key(eckey,pub_key); 配置公钥
              default_priv_key.get_public_key(),  提取公钥
             graphene::utilities::key_to_wif(default_priv_key) 提取私钥并作wif转化
                 (0x80 + "privK") + HASH(HASH(0x80 + "privK"))(取前四位)
                 wif_key(32+5bytes)
5. 创建,读取config.ini
create_new_config_file( config_ini_path, data_dir, cfg_options );
     fc::optional<fc::logging_config> logging_config = load_logging_config_from_ini_file(config_ini_path);     利用peroperty_tree读取默认的config信息,并返回给个下一个函数调用
    fc::configure_logging(*logging_config);  建立static std::unordered_map<std::string,appender::ptr> lm;
static std::unordered_map<std::string,appender_factory::ptr> lm;两个静态无序map,存储config.ini中[***.***]配置
ps. [logger.*]打印log等级,一共有6个等级
all, debug, info, warn, error, off
load_config_file( config_ini_path, cfg_options, options );
     bpo::store(bpo::parse_config_file<char>(config_ini_path.preferred_string().c_str(), unique_options, true), options); 将上一个函数cfg_options包含的变量名称从config_ini_path指代的config.ini文件读取(bpo::parse_config_file),将内容传入options(bpo::store)
意味着配置可以同cli指定的同时,也可从config.ini读取
6. bpo::notify(options); 执行所有设置中改变的函数变量
7. node->initialize(data_dir, options);
     1. options.count("create-genesis-json")   genesis_state_type genesis_state = detail::create_example_genesis(); 创建默认genesis.json
         initial_state.initial_committee_candidates.push_back({name}); 添加委员会名单
         initial_state.initial_witness_candidates.push_back({name, nathan_key.get_public_key()}); 添加见证者名单
     2. options.count("io-threads")  fc::asio::default_io_service_scope::set_num_threads(num_threads); 设置io_thread数量
     3. options.count("plugins") boost::split(wanted, options.at("plugins").as<std::string>(), [](char c){return c == ' ';});
     wanted.push_back("witness");
     wanted.push_back("account_history");
     wanted.push_back("market_history");
     wanted.push_back("grouped_orders");    向wanted容器添加plugin
     enable_plugin(it);    使能plguin
         my->_active_plugins[name]->plugin_set_app(this);
              _app = app;    在每个默认或-plugin=<plugin_name>的plugin下注册创建的witness节点的application,对于节点application来说plugin加入my->_active_plugins数组
8. node->initialize_plugins( options );
         entry.second->plugin_initialize( options );
              LOAD_VALUE_SET(options, "witness-id", _witnesses, chain::witness_id_type) 将options中"witness-id"的作json解码,格式符合chain::witness_id_type.     ps.原来的字符形式转化为结构体chain::witness_id_type
         options.count("private-key")
         auto key_id_to_wif_pair = graphene::app::dejsonify<std::pair<chain::public_key_type, std::string> >(key_id_to_wif_pair_string, 5);     从["TEST6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV","5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"]字符串读取符合格式std::pair<chain::public_key_type, std::string>的变形
         ilog("Public Key: ${public}", ("public", key_id_to_wif_pair.first));    打印公钥
         fc::optional<fc::ecc::private_key> private_key = graphene::utilities::wif_to_key(key_id_to_wif_pair.second);    从wif_key获取私钥
         _private_keys[key_id_to_wif_pair.first] = *private_key;     _private_keys[]map形成公钥/私钥对
9.  node->startup();
         my->startup();
              _chain_db->open( _data_dir / "blockchain", initial_state, GRAPHENE_CURRENT_DB_VERSION );
                   object_database::open(data_dir);     open遍历bitshares-core-testnet/data/testnet/blockchain/object_database文件下的子文件
                        _index[space][type]->open( _data_dir / "object_database" / fc::to_string(space)/fc::to_string(type) );
                   这应该是根据objectid读取对应object 用fc::raw::unpack进行解码
                   init_genesis(genesis_loader()); create global_property_id_type特性
                   reindex( data_dir );     下载网络区块
                        const auto last_block_num = last_block->block_num(); 获取网络的最大区块号
                        if( i % 10000 == 0 ) std::cerr << "   " << double(i*100)/last_block_num << "%   "<<i << " of " <<last_block_num<<"   \n";     打印获取的区块进度
                        if( i == flush_point )
                       {
                             ilog( "Writing database to disk at block ${i}", ("i",i) );
                              flush();
                             ilog( "Done" );
                       }     内存中仅保留1000个最近的区块,其他区块保存到硬盘,     head_block_num() >= undo_point当前本地账本在读取远程账本的时候会使能_fork_db(分叉数据库)进行下载以及_undo_db, 否则两个db均不使能
                        fc::optional< signed_block > block = _block_id_to_block.fetch_by_number(i);     fetch当前区块号的block

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值