摘要:
插件功能:
1.负责端点的连接与断开,状态管理;
2.请求块,验证块,处理块;
详情:
定义类:
net_plugin: 网络插件类
//连接端点 endpoint
string connect( const string& endpoint );
//断开与endpoint的连接
string disconnect( const string& endpoint );
//返回与endpoint的状态
optional<connection_status> status( const string& endpoint ) const;
//返回所有的连接
vector<connection_status> connections() const;
sync_manager: 同步管理类
//返回状态string
constexpr static auto stage_str( stages s );
//设置状态
void set_state( stages s );
//头块号是否是同步请求的
bool is_sync_required( uint32_t fork_head_block_num );
// 请求下一个块
/* 1.如果当前块不存在:使用传进来的conn
* 2.如果当前连接数为0:重置同步源
* 3.如果找不到同步源对应的连接:拿连接集合中的第一个
* 4.如果上一个请求的块号!=已知请求的块号(说明同步不一致):请求同步块
*/
void request_next_chunk( std::unique_lock<std::mutex> g_sync,
const connection_ptr& conn = connection_ptr() );
//开启同步
start_sync( const connection_ptr& c, uint32_t target );
//捕获块验证(先验证是否是最新的块:是->设置最新块 否->不处理)
verify_catchup( const connection_ptr& c, uint32_t num, const block_id_type& id );
//发送握手
static void send_handshakes();
//同步peer
bool syncing_with_peer() const { return sync_state == lib_catchup; }
//同步重置块号(如果上一个确定的块号 > 已知同步的块号: 赋值给已知同步的块号)
void sync_reset_lib_num( const connection_ptr& conn );
//同步重新指定fetch
void sync_reassign_fetch( const connection_ptr& c, go_away_reason reason );
// 拒绝块(如果拒绝的数超过了最大块数限制,重置同步源)
void rejected_block( const connection_ptr& c, uint32_t blk_num );
// 同步接收块
void sync_recv_block( const connection_ptr& c, const block_id_type& blk_id,
uint32_t blk_num, bool blk_applied );
// 同步更新指定块(如果指定的块号不是默认下一个要请求的块号,就赋值给它)
void sync_update_expected( const connection_ptr& c, const block_id_type& blk_id,
uint32_t blk_num, bool blk_applied );
//接收握手消息
void recv_handshake( const connection_ptr& c, const handshake_message& msg );
//同步接收通知信息
void sync_recv_notice( const connection_ptr& c, const notice_message& msg );
dispatch_manager: 递送管理器
//确定多线程执行顺序
boost::asio::io_context:;strand strand;
//广播传输块
void bcast_transaction( const packed_transaction& trx );
//拒绝传输块
void rejected_transaction( const packed_transaction_ptr& trx, uint32_t head_blk_num );
// 广播块(线程安全,先看看块id存不存在,不存在就广播)
void bcast_block( const signed_block_ptr& b, const block_id_type& id );
//广播通知
void bcast_notice( const block_id_type& id );
//拒绝块
void rejected_block( const block_id_type& id );
//接受块
void recv_block( const connection_ptr& conn, const block_id_type& msg, uint32_t bnum );
//传输过期块(删掉到目前位置已过期的传输块)
void expire_blocks( uint32_t bnum );
net_plugin_impl:网络插件的实现
//更新链信息(只由主程序线程调用)
void update_chain_info();
//获得链信息
std::tuple<uint32_t, uint32_t, uint32_t, block_id_type, block_id_type,
block_id_type> get_chain_info() const;
//开始监听
void start_listen_loop();
//接受块
void on_accepted_block( const block_state_ptr& bs );
//预接受块
void on_pre_accepted_block( cosnt signed_block_ptr& bs );
//传输问讯
void transaction_ack( const std::pair<fc::exception_ptr, transaction_metadata_ptr>& );
//固定块,调用update_chain_info
void on_irreversible_block( const block_state_ptr& blk );
//开启连接计时器
void start_conn_timer(boost::asio::steady_timer::duration du,
std::weak_ptr<connection> from_connection);
//开启过期计时器(线程安全)
void start_expire_timer();
//开启监视器
void start_monitors();
//过期
void expire();
//连接管理其(1ms检查一次,如果连接不可用,删除它)
void connection_monitor( std::weak_ptr<connection> from_connection, bool reschedule );
//节点心跳滴答
void ticker();
//验证握手信息,判定是否可以连接
bool authenticate_peer( const handshake_message& msg ) const;
//获得公钥
chain::public_key_type get_authentication_key() const;
//合约签名(签名器, 签名摘要)
chain::signature_type sign_compact( const chain::public_key_type& signer,
const fc::sha256& digest ) const;
//返回协议版本
constexpr uint16_t to_protocol_version(uint16_t v);
//根据host找到连接
connection_ptr find_connection( const string& host ) const;
定义协议:
net_message:
handshake_message //握手消息
chain_size_message //链大小消息
go_away_message //离开信息
time_message //时间信息
notice_message //通知消息
request_message //请求消息
sync_request_message //同步请求消息
signed_block //已签名块
packed_transaction //已打包的传输块
定义表:
node_transaction_index:传输表
主键 | 分索引1 | 分索引2 |
id | 传输块id | 连接id |
过期时间 | ||
块号 |
peer_block_state_index:节点块表
主键 | 分索引1 | 分索引2 |
id | 连接id | 块id |
块id | 块id | 块是否存在 |
块号 |