Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
739066ec
Commit
739066ec
authored
Feb 05, 2015
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p/discover: add some helper functions
parent
12224c7f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
20 deletions
+41
-20
table.go
p2p/discover/table.go
+29
-8
table_test.go
p2p/discover/table_test.go
+3
-3
udp.go
p2p/discover/udp.go
+2
-2
udp_test.go
p2p/discover/udp_test.go
+7
-7
No files found.
p2p/discover/table.go
View file @
739066ec
...
...
@@ -87,6 +87,16 @@ func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr) *Table {
return
tab
}
// Self returns the local node ID.
func
(
tab
*
Table
)
Self
()
NodeID
{
return
tab
.
self
.
ID
}
// Close terminates the network listener.
func
(
tab
*
Table
)
Close
()
{
tab
.
net
.
close
()
}
// Bootstrap sets the bootstrap nodes. These nodes are used to connect
// to the network if the table is empty. Bootstrap will also attempt to
// fill the table by performing random lookup operations on the
...
...
@@ -319,27 +329,38 @@ func (n NodeID) GoString() string {
// HexID converts a hex string to a NodeID.
// The string may be prefixed with 0x.
func
HexID
(
in
string
)
NodeID
{
func
HexID
(
in
string
)
(
NodeID
,
error
)
{
if
strings
.
HasPrefix
(
in
,
"0x"
)
{
in
=
in
[
2
:
]
}
var
id
NodeID
b
,
err
:=
hex
.
DecodeString
(
in
)
if
err
!=
nil
{
panic
(
err
)
return
id
,
err
}
else
if
len
(
b
)
!=
len
(
id
)
{
panic
(
"wrong length"
)
return
id
,
fmt
.
Errorf
(
"wrong length, need %d hex bytes"
,
len
(
id
)
)
}
copy
(
id
[
:
],
b
)
return
id
,
nil
}
// MustHexID converts a hex string to a NodeID.
// It panics if the string is not a valid NodeID.
func
MustHexID
(
in
string
)
NodeID
{
id
,
err
:=
HexID
(
in
)
if
err
!=
nil
{
panic
(
err
)
}
return
id
}
func
newNodeID
(
priv
*
ecdsa
.
PrivateKey
)
(
id
NodeID
)
{
pubkey
:=
elliptic
.
Marshal
(
priv
.
Curve
,
priv
.
X
,
priv
.
Y
)
if
len
(
pubkey
)
-
1
!=
len
(
id
)
{
panic
(
fmt
.
Errorf
(
"invalid key: need %d bit pubkey, got %d bits"
,
(
len
(
id
)
+
1
)
*
8
,
len
(
pubkey
)))
func
PubkeyID
(
pub
*
ecdsa
.
PublicKey
)
NodeID
{
var
id
NodeID
pbytes
:=
elliptic
.
Marshal
(
pub
.
Curve
,
pub
.
X
,
pub
.
Y
)
if
len
(
pbytes
)
-
1
!=
len
(
id
)
{
panic
(
fmt
.
Errorf
(
"invalid key: need %d bit pubkey, got %d bits"
,
(
len
(
id
)
+
1
)
*
8
,
len
(
pbytes
)))
}
copy
(
id
[
:
],
p
ubkey
[
1
:
])
copy
(
id
[
:
],
p
bytes
[
1
:
])
return
id
}
...
...
p2p/discover/table_test.go
View file @
739066ec
...
...
@@ -22,8 +22,8 @@ var (
func
TestHexID
(
t
*
testing
.
T
)
{
ref
:=
NodeID
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
128
,
106
,
217
,
182
,
31
,
165
,
174
,
1
,
67
,
7
,
235
,
220
,
150
,
66
,
83
,
173
,
205
,
159
,
44
,
10
,
57
,
42
,
161
,
26
,
188
}
id1
:=
HexID
(
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc"
)
id2
:=
HexID
(
"000000000000000000000000000000000000000000000000000000000000000000000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc"
)
id1
:=
Must
HexID
(
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc"
)
id2
:=
Must
HexID
(
"000000000000000000000000000000000000000000000000000000000000000000000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc"
)
if
id1
!=
ref
{
t
.
Errorf
(
"wrong id1
\n
got %v
\n
want %v"
,
id1
[
:
],
ref
[
:
])
...
...
@@ -41,7 +41,7 @@ func TestNodeID_recover(t *testing.T) {
t
.
Fatalf
(
"signing error: %v"
,
err
)
}
pub
:=
newNodeID
(
prv
)
pub
:=
PubkeyID
(
&
prv
.
PublicKey
)
recpub
,
err
:=
recoverNodeID
(
hash
,
sig
)
if
err
!=
nil
{
t
.
Fatalf
(
"recovery error: %v"
,
err
)
...
...
p2p/discover/udp.go
View file @
739066ec
...
...
@@ -120,8 +120,8 @@ func ListenUDP(priv *ecdsa.PrivateKey, laddr string) (*Table, error) {
if
err
!=
nil
{
return
nil
,
err
}
net
.
Table
=
newTable
(
net
,
newNodeID
(
priv
),
realaddr
)
log
.
Debug
Detail
f
(
"Listening on %v, my ID %x
\n
"
,
realaddr
,
net
.
self
.
ID
[
:
])
net
.
Table
=
newTable
(
net
,
PubkeyID
(
&
priv
.
PublicKey
),
realaddr
)
log
.
Debugf
(
"Listening on %v, my ID %x
\n
"
,
realaddr
,
net
.
self
.
ID
[
:
])
return
net
.
Table
,
nil
}
...
...
p2p/discover/udp_test.go
View file @
739066ec
...
...
@@ -19,8 +19,8 @@ func TestUDP_ping(t *testing.T) {
n1
,
_
:=
ListenUDP
(
newkey
(),
"127.0.0.1:0"
)
n2
,
_
:=
ListenUDP
(
newkey
(),
"127.0.0.1:0"
)
defer
n1
.
net
.
c
lose
()
defer
n2
.
net
.
c
lose
()
defer
n1
.
C
lose
()
defer
n2
.
C
lose
()
if
err
:=
n1
.
net
.
ping
(
n2
.
self
);
err
!=
nil
{
t
.
Fatalf
(
"ping error: %v"
,
err
)
...
...
@@ -49,8 +49,8 @@ func TestUDP_findnode(t *testing.T) {
n1
,
_
:=
ListenUDP
(
newkey
(),
"127.0.0.1:0"
)
n2
,
_
:=
ListenUDP
(
newkey
(),
"127.0.0.1:0"
)
defer
n1
.
net
.
c
lose
()
defer
n2
.
net
.
c
lose
()
defer
n1
.
C
lose
()
defer
n2
.
C
lose
()
entry
:=
&
Node
{
ID
:
NodeID
{
1
},
Addr
:
&
net
.
UDPAddr
{
IP
:
net
.
IP
{
1
,
2
,
3
,
4
},
Port
:
15
}}
n2
.
add
([]
*
Node
{
entry
})
...
...
@@ -77,7 +77,7 @@ func TestUDP_replytimeout(t *testing.T) {
defer
fd
.
Close
()
n1
,
_
:=
ListenUDP
(
newkey
(),
"127.0.0.1:0"
)
defer
n1
.
net
.
c
lose
()
defer
n1
.
C
lose
()
n2
:=
n1
.
bumpOrAdd
(
randomID
(
n1
.
self
.
ID
,
10
),
fd
.
LocalAddr
()
.
(
*
net
.
UDPAddr
))
if
err
:=
n1
.
net
.
ping
(
n2
);
err
!=
errTimeout
{
...
...
@@ -97,8 +97,8 @@ func TestUDP_findnodeMultiReply(t *testing.T) {
n1
,
_
:=
ListenUDP
(
newkey
(),
"127.0.0.1:0"
)
n2
,
_
:=
ListenUDP
(
newkey
(),
"127.0.0.1:0"
)
udp2
:=
n2
.
net
.
(
*
udp
)
defer
n1
.
net
.
c
lose
()
defer
n2
.
net
.
c
lose
()
defer
n1
.
C
lose
()
defer
n2
.
C
lose
()
nodes
:=
make
([]
*
Node
,
bucketSize
)
for
i
:=
range
nodes
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment