Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
c1fca725
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c1fca725
编写于
11月 24, 2014
作者:
F
Felix Lange
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
p2p: use package rlp
上级
205af02a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
31 addition
and
67 deletion
+31
-67
p2p/message.go
p2p/message.go
+27
-66
p2p/message_test.go
p2p/message_test.go
+3
-0
p2p/peer_test.go
p2p/peer_test.go
+1
-1
未找到文件。
p2p/message.go
浏览文件 @
c1fca725
...
...
@@ -3,12 +3,12 @@ package p2p
import
(
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"math/big"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/rlp"
)
// Msg defines the structure of a p2p message.
...
...
@@ -43,16 +43,10 @@ func encodePayload(params ...interface{}) []byte {
// Data returns the decoded RLP payload items in a message.
func
(
msg
Msg
)
Data
()
(
*
ethutil
.
Value
,
error
)
{
// TODO: avoid copying when we have a better RLP decoder
buf
:=
new
(
bytes
.
Buffer
)
var
s
[]
interface
{}
if
_
,
err
:=
buf
.
ReadFrom
(
msg
.
Payload
);
err
!=
nil
{
return
nil
,
err
}
for
buf
.
Len
()
>
0
{
s
=
append
(
s
,
ethutil
.
DecodeWithReader
(
buf
))
}
return
ethutil
.
NewValue
(
s
),
nil
s
:=
rlp
.
NewListStream
(
msg
.
Payload
,
uint64
(
msg
.
Size
))
var
v
[]
interface
{}
err
:=
s
.
Decode
(
&
v
)
return
ethutil
.
NewValue
(
v
),
err
}
// Discard reads any remaining payload data into a black hole.
...
...
@@ -137,13 +131,9 @@ func makeListHeader(length uint32) []byte {
return
append
([]
byte
{
lenb
},
enc
...
)
}
type
byteReader
interface
{
io
.
Reader
io
.
ByteReader
}
// readMsg reads a message header from r.
func
readMsg
(
r
byteReader
)
(
msg
Msg
,
err
error
)
{
// It takes an rlp.ByteReader to ensure that the decoding doesn't buffer.
func
readMsg
(
r
rlp
.
ByteReader
)
(
msg
Msg
,
err
error
)
{
// read magic and payload size
start
:=
make
([]
byte
,
8
)
if
_
,
err
=
io
.
ReadFull
(
r
,
start
);
err
!=
nil
{
...
...
@@ -155,64 +145,35 @@ func readMsg(r byteReader) (msg Msg, err error) {
size
:=
binary
.
BigEndian
.
Uint32
(
start
[
4
:
])
// decode start of RLP message to get the message code
_
,
hdrlen
,
err
:=
readListHeader
(
r
)
if
err
!=
nil
{
posr
:=
&
postrack
{
r
,
0
}
s
:=
rlp
.
NewStream
(
posr
)
if
_
,
err
:=
s
.
List
();
err
!=
nil
{
return
msg
,
err
}
code
,
codelen
,
err
:=
readMsgCode
(
r
)
code
,
err
:=
s
.
Uint
(
)
if
err
!=
nil
{
return
msg
,
err
}
payloadsize
:=
size
-
posr
.
p
return
Msg
{
code
,
payloadsize
,
io
.
LimitReader
(
r
,
int64
(
payloadsize
))},
nil
}
rlpsize
:=
size
-
hdrlen
-
codelen
return
Msg
{
Code
:
code
,
Size
:
rlpsize
,
Payload
:
io
.
LimitReader
(
r
,
int64
(
rlpsize
)),
},
nil
// postrack wraps an rlp.ByteReader with a position counter.
type
postrack
struct
{
r
rlp
.
ByteReader
p
uint32
}
// readListHeader reads an RLP list header from r.
func
readListHeader
(
r
byteReader
)
(
len
uint64
,
hdrlen
uint32
,
err
error
)
{
b
,
err
:=
r
.
ReadByte
()
if
err
!=
nil
{
return
0
,
0
,
err
}
if
b
<
0xC0
{
return
0
,
0
,
fmt
.
Errorf
(
"expected list start byte >= 0xC0, got %x"
,
b
)
}
else
if
b
<
0xF7
{
len
=
uint64
(
b
-
0xc0
)
hdrlen
=
1
}
else
{
lenlen
:=
b
-
0xF7
lenbuf
:=
make
([]
byte
,
8
)
if
_
,
err
:=
io
.
ReadFull
(
r
,
lenbuf
[
8
-
lenlen
:
]);
err
!=
nil
{
return
0
,
0
,
err
}
len
=
binary
.
BigEndian
.
Uint64
(
lenbuf
)
hdrlen
=
1
+
uint32
(
lenlen
)
}
return
len
,
hdrlen
,
nil
func
(
r
*
postrack
)
Read
(
buf
[]
byte
)
(
int
,
error
)
{
n
,
err
:=
r
.
r
.
Read
(
buf
)
r
.
p
+=
uint32
(
n
)
return
n
,
err
}
// readUint reads an RLP-encoded unsigned integer from r.
func
readMsgCode
(
r
byteReader
)
(
code
uint64
,
codelen
uint32
,
err
error
)
{
b
,
err
:=
r
.
ReadByte
()
if
err
!=
nil
{
return
0
,
0
,
err
}
if
b
<
0x80
{
return
uint64
(
b
),
1
,
nil
}
else
if
b
<
0x89
{
// max length for uint64 is 8 bytes
codelen
=
uint32
(
b
-
0x80
)
if
codelen
==
0
{
return
0
,
1
,
nil
}
buf
:=
make
([]
byte
,
8
)
if
_
,
err
:=
io
.
ReadFull
(
r
,
buf
[
8
-
codelen
:
]);
err
!=
nil
{
return
0
,
0
,
err
}
return
binary
.
BigEndian
.
Uint64
(
buf
),
codelen
,
nil
func
(
r
*
postrack
)
ReadByte
()
(
byte
,
error
)
{
b
,
err
:=
r
.
r
.
ReadByte
()
if
err
==
nil
{
r
.
p
++
}
return
0
,
0
,
fmt
.
Errorf
(
"bad RLP type for message code: %x"
,
b
)
return
b
,
err
}
p2p/message_test.go
浏览文件 @
c1fca725
...
...
@@ -46,6 +46,9 @@ func TestEncodeDecodeMsg(t *testing.T) {
if
err
!=
nil
{
t
.
Fatalf
(
"first payload item decode error: %v"
,
err
)
}
if
v
:=
data
.
Len
();
v
!=
2
{
t
.
Errorf
(
"incorrect data.Len(): got %v, expected %d"
,
v
,
1
)
}
if
v
:=
data
.
Get
(
0
)
.
Uint
();
v
!=
1
{
t
.
Errorf
(
"incorrect data[0]: got %v, expected %d"
,
v
,
1
)
}
...
...
p2p/peer_test.go
浏览文件 @
c1fca725
...
...
@@ -57,7 +57,7 @@ func TestPeerProtoReadMsg(t *testing.T) {
if
err
!=
nil
{
t
.
Errorf
(
"data decoding error: %v"
,
err
)
}
expdata
:=
[]
interface
{}{
1
,
[]
byte
{
0x30
,
0x30
,
0x30
}}
expdata
:=
[]
interface
{}{
[]
byte
{
0x01
}
,
[]
byte
{
0x30
,
0x30
,
0x30
}}
if
!
reflect
.
DeepEqual
(
data
.
Slice
(),
expdata
)
{
t
.
Errorf
(
"incorrect msg data %#v"
,
data
.
Slice
())
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录