Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
YottaChain
YTBP
提交
61cd39f3
Y
YTBP
项目概览
YottaChain
/
YTBP
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
YTBP
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
61cd39f3
编写于
6月 22, 2017
作者:
P
Phil Mesnier
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
start message handling
上级
9f0b1348
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
160 addition
and
79 deletion
+160
-79
plugins/net_plugin/CMakeLists.txt
plugins/net_plugin/CMakeLists.txt
+1
-1
plugins/net_plugin/net_plugin.cpp
plugins/net_plugin/net_plugin.cpp
+159
-78
未找到文件。
plugins/net_plugin/CMakeLists.txt
浏览文件 @
61cd39f3
...
...
@@ -3,7 +3,7 @@ add_library( net_plugin
net_plugin.cpp
${
HEADERS
}
)
target_link_libraries
(
net_plugin chain_plugin appbase fc
)
target_link_libraries
(
net_plugin chain_plugin
egenesis
appbase fc
)
target_include_directories
(
net_plugin PUBLIC
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/include"
)
install
(
TARGETS
...
...
plugins/net_plugin/net_plugin.cpp
浏览文件 @
61cd39f3
...
...
@@ -27,7 +27,7 @@ struct node_transaction_state {
fc
::
time_point
received
;
fc
::
time_point_sec
expires
;
vector
<
char
>
packed_transaction
;
uint
64
_t
block_num
=
-
1
;
/// block transaction was included in
uint
32
_t
block_num
=
-
1
;
/// block transaction was included in
bool
validated
=
false
;
/// whether or not our node has validated it
};
...
...
@@ -43,7 +43,7 @@ struct transaction_state {
transaction_id_type
id
;
bool
is_known_by_peer
=
false
;
///< true if we sent or received this trx to this peer or received notice from peer
bool
is_noticed_to_peer
=
false
;
///< have we sent peer noitce we know it (true if we reeive from this peer)
uint
64
_t
block_num
=
-
1
;
///< the block number the transaction was included in
uint
32
_t
block_num
=
-
1
;
///< the block number the transaction was included in
time_point
validated_time
;
///< infinity for unvalidated
time_point
requested_time
;
/// incase we fetch large trx
};
...
...
@@ -76,9 +76,9 @@ typedef multi_index_container<
* Index by start_block
*/
struct
sync_state
{
uint
64
_t
start_block
=
0
;
uint
64
_t
end_block
=
0
;
uint
64
_t
last
=
0
;
///< last sent or received
uint
32
_t
start_block
=
0
;
uint
32
_t
end_block
=
0
;
uint
32
_t
last
=
0
;
///< last sent or received
time_point
start_time
;;
///< time request made or received
};
...
...
@@ -86,7 +86,7 @@ struct by_start_block;
typedef
multi_index_container
<
sync_state
,
indexed_by
<
ordered_unique
<
tag
<
by_start_block
>
,
member
<
sync_state
,
uint
64
_t
,
&
sync_state
::
start_block
>
>
ordered_unique
<
tag
<
by_start_block
>
,
member
<
sync_state
,
uint
32
_t
,
&
sync_state
::
start_block
>
>
>
>
sync_request_index
;
...
...
@@ -98,6 +98,7 @@ public:
wlog
(
"created connection"
);
pending_message_buffer
.
resize
(
1024
*
1024
*
4
);
}
~
connection
()
{
wlog
(
"released connection"
);
}
...
...
@@ -112,7 +113,6 @@ public:
vector
<
char
>
pending_message_buffer
;
handshake_message
last_handshake
;
std
::
deque
<
net_message
>
out_queue
;
void
send
(
const
net_message
&
m
)
{
...
...
@@ -122,8 +122,12 @@ public:
}
void
send_next_message
()
{
if
(
!
out_queue
.
size
()
)
if
(
!
out_queue
.
size
()
)
{
if
(
out_sync_state
.
size
()
>
0
)
{
write_block_backlog
();
}
return
;
}
auto
&
m
=
out_queue
.
front
();
...
...
@@ -145,6 +149,29 @@ public:
}
});
}
void
write_block_backlog
(
)
{
try
{
ilog
(
"write loop sending backlog "
);
if
(
out_sync_state
.
size
()
>
0
)
{
chain_controller
&
cc
=
app
().
find_plugin
<
chain_plugin
>
()
->
chain
();
auto
ss
=
out_sync_state
.
begin
();
for
(
uint32_t
num
=
ss
->
last
+
1
;
num
<=
ss
->
end_block
;
num
++
)
{
fc
::
optional
<
signed_block
>
sb
=
cc
.
fetch_block_by_number
(
num
);
if
(
sb
)
{
send
(
*
sb
);
}
ss
.
get_node
()
->
value
().
last
=
num
;
}
out_sync_state
.
erase
(
0
);
}
}
catch
(
...
)
{
wlog
(
"write loop exception"
);
}
}
};
// class connection
...
...
@@ -163,6 +190,9 @@ class net_plugin_impl {
bool
done
=
false
;
fc
::
optional
<
handshake_message
>
hello
;
std
::
string
user_agent_name
;
chain_plugin
*
chain_plug
;
void
connect
(
const
string
&
ep
)
{
auto
host
=
ep
.
substr
(
0
,
ep
.
find
(
':'
)
);
...
...
@@ -229,15 +259,24 @@ class net_plugin_impl {
}
hello
->
network_version
=
0
;
// hello->chain_id = chain->get_chain_id(
);
chain_plug
->
get_chain_id
(
hello
->
chain_id
);
fc
::
rand_pseudo_bytes
(
hello
->
node_id
.
data
(),
hello
->
node_id
.
data_size
());
#if defined( __APPLE__ )
hello
->
os
=
"osx"
;
#elif defined( __linux__ )
hello
->
os
=
"linux"
;
#elif defined( _MSC_VER )
hello
->
os
=
"win32"
;
#else
hello
->
os
=
"other"
;
#endif
hello
->
agent
=
user_agent_name
;
}
void
update_handshake
()
{
chain_plugin
*
cp
=
app
().
find_plugin
<
chain_plugin
>
();
hello
->
last_irreversible_block_id
=
cp
->
chain
().
get_block_id_for_num
(
hello
->
last_irreversible_block_num
=
cp
->
chain
().
last_irreversible_block_num
());
hello
->
last_irreversible_block_id
=
chain_plug
->
chain
().
get_block_id_for_num
(
hello
->
last_irreversible_block_num
=
chain_plug
->
chain
().
last_irreversible_block_num
());
}
void
start_session
(
connection
*
con
)
{
...
...
@@ -253,7 +292,7 @@ class net_plugin_impl {
con
->
send
(
*
hello
);
// con->readloop_complete = bf::async( [=](){ read_loop( con ); } );
//
con->writeloop_complete = bf::async( [=](){ write_loop(
con ); } );
//
con->writeloop_complete = bf::async( [=](){ write_loop
con ); } );
}
void
start_listen_loop
()
{
...
...
@@ -288,32 +327,47 @@ class net_plugin_impl {
);
}
void
handle_message
(
connection
&
c
,
handshake_message
&
msg
)
{
ilog
(
"got a handshake message"
);
if
(
!
hello
)
{
init_handshake
();
}
i
f
(
msg
.
node_id
==
hello
->
node_id
)
{
d
log
(
"Self connection detected. Closing connection"
);
i
log
(
"got a handshake message"
);
if
(
msg
.
node_id
==
hello
->
node_id
)
{
e
log
(
"Self connection detected. Closing connection"
);
close
(
&
c
);
return
;
}
if
(
msg
.
chain_id
!=
hello
->
chain_id
)
{
dlog
(
"Peer on a different chain. Closing connection"
);
if
(
msg
.
chain_id
!=
hello
->
chain_id
)
{
elog
(
"Peer on a different chain. Closing connection"
);
close
(
&
c
);
return
;
}
if
(
msg
.
network_version
!=
hello
->
network_version
)
{
dlog
(
"Peer network id does not match "
);
if
(
msg
.
network_version
!=
hello
->
network_version
)
{
elog
(
"Peer network id does not match "
);
close
(
&
c
);
return
;
}
chain_controller
&
cc
=
chain_plug
->
chain
();
uint32_t
head
=
cc
.
head_block_num
();
if
(
msg
.
last_irreversible_block_num
>
head
)
{
uint32_t
delta
=
msg
.
last_irreversible_block_num
-
head
;
uint32_t
count
=
connections
.
size
();
uint32_t
span
=
delta
/
count
;
uint32_t
lastSpan
=
delta
-
(
span
*
(
count
-
1
));
ilog
(
"peer is ahead of head by ${d}, count = ${c}, span = ${s}, lastspan = ${ls} "
,
(
"d"
,
delta
)(
"c"
,
count
)(
"s"
,
span
)(
"ls"
,
lastSpan
));
for
(
auto
&
cx
:
connections
)
{
if
(
--
count
==
0
)
{
span
=
lastSpan
;
}
sync_state
req
=
{
head
+
1
,
head
+
span
,
0
,
time_point
::
now
()
};
cx
->
in_sync_state
.
insert
(
req
);
sync_request_message
srm
=
{
req
.
start_block
,
req
.
end_block
};
cx
->
send
(
srm
);
head
+=
span
;
}
}
c
.
last_handshake
=
msg
;
}
...
...
@@ -328,7 +382,10 @@ class net_plugin_impl {
}
void
handle_message
(
connection
&
c
,
sync_request_message
&
msg
)
{
ilog
(
"got a sync request message"
);
ilog
(
"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
,
0
,
time_point
::
now
()};
c
.
out_sync_state
.
insert
(
req
);
c
.
write_block_backlog
();
}
void
handle_message
(
connection
&
c
,
block_summary_message
&
msg
)
{
...
...
@@ -340,8 +397,67 @@ class net_plugin_impl {
}
void
handle_message
(
connection
&
c
,
signed_block
&
msg
)
{
ilog
(
"got a signed_block"
);
uint32_t
bn
=
msg
.
block_num
();
ilog
(
"got a signed_block, num = ${n}"
,
(
"n"
,
bn
));
chain_controller
&
cc
=
chain_plug
->
chain
();
if
(
cc
.
is_known_block
(
msg
.
id
()))
{
ilog
(
"block id ${id} is known"
,
(
"id"
,
msg
.
id
())
);
return
;
}
uint32_t
num
=
msg
.
block_num
();
for
(
auto
&
ss
:
c
.
in_sync_state
)
{
if
(
num
>=
ss
.
end_block
)
{
continue
;
}
const_cast
<
sync_state
&>
(
ss
).
last
=
num
;
break
;
}
// TODO: add block to global state
}
struct
msgHandler
:
public
fc
::
visitor
<
void
>
{
net_plugin_impl
&
impl
;
connection
&
c
;
msgHandler
(
net_plugin_impl
&
imp
,
connection
&
conn
)
:
impl
(
imp
),
c
(
conn
)
{}
void
operator
()(
handshake_message
&
msg
)
{
impl
.
handle_message
(
c
,
msg
);
}
void
operator
()(
peer_message
&
msg
)
{
impl
.
handle_message
(
c
,
msg
);
}
void
operator
()(
notice_message
&
msg
)
{
impl
.
handle_message
(
c
,
msg
);
}
void
operator
()(
sync_request_message
&
msg
)
{
impl
.
handle_message
(
c
,
msg
);
}
void
operator
()(
block_summary_message
&
msg
)
{
impl
.
handle_message
(
c
,
msg
);
}
void
operator
()(
SignedTransaction
&
msg
)
{
impl
.
handle_message
(
c
,
msg
);
}
void
operator
()(
signed_block
&
msg
)
{
impl
.
handle_message
(
c
,
msg
);
}
};
void
start_reading_pending_buffer
(
connection
&
c
)
{
boost
::
asio
::
async_read
(
*
c
.
socket
,
boost
::
asio
::
buffer
(
c
.
pending_message_buffer
.
data
(),
c
.
pending_message_size
),
...
...
@@ -353,37 +469,8 @@ class net_plugin_impl {
ilog
(
"received message of size: ${s}"
,
(
"s"
,
bytes_transferred
)
);
start_read_message
(
c
);
switch
(
msg
.
which
())
{
case
0
:
{
handle_message
(
c
,
msg
.
get
<
handshake_message
>
());
break
;
}
case
1
:
{
handle_message
(
c
,
msg
.
get
<
peer_message
>
());
break
;
}
case
2
:
{
break
;
handle_message
(
c
,
msg
.
get
<
notice_message
>
());
break
;
}
case
3
:
{
handle_message
(
c
,
msg
.
get
<
sync_request_message
>
());
break
;
}
case
4
:
{
handle_message
(
c
,
msg
.
get
<
block_summary_message
>
());
break
;
}
case
5
:
{
handle_message
(
c
,
msg
.
get
<
SignedTransaction
>
());
break
;
}
case
6
:
{
handle_message
(
c
,
msg
.
get
<
signed_block
>
());
break
;
}
}
msgHandler
m
(
*
this
,
c
);
msg
.
visit
(
m
);
return
;
}
catch
(
const
fc
::
exception
&
e
)
{
edump
((
e
.
to_detail_string
()
));
...
...
@@ -397,14 +484,6 @@ class net_plugin_impl {
}
void
write_loop
(
connection
*
c
)
{
try
{
c
->
send
(
handshake_message
{}
);
}
catch
(
...
)
{
wlog
(
"write loop exception"
);
}
}
void
close
(
connection
*
c
)
{
ilog
(
"close ${c}"
,
(
"c"
,
int64_t
(
c
)));
if
(
c
->
socket
)
...
...
@@ -415,7 +494,6 @@ class net_plugin_impl {
};
// class net_plugin_impl
net_plugin
::
net_plugin
()
:
my
(
new
net_plugin_impl
)
{
}
...
...
@@ -446,6 +524,9 @@ void net_plugin::plugin_initialize( const variables_map& options ) {
if
(
options
.
count
(
"remote-endpoint"
)
)
{
my
->
seed_nodes
=
options
.
at
(
"remote-endpoint"
).
as
<
vector
<
string
>
>
();
}
my
->
user_agent_name
=
"EOS Test Agent"
;
my
->
chain_plug
=
app
().
find_plugin
<
chain_plugin
>
();
}
void
net_plugin
::
plugin_startup
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录