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
c15d76a4
Commit
c15d76a4
authored
Dec 28, 2017
by
Péter Szilágyi
Committed by
Felix Lange
Dec 28, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p/discv5: fix reg lookup, polish code, use logger (#15737)
parent
5369a5c5
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
139 additions
and
142 deletions
+139
-142
net.go
p2p/discv5/net.go
+24
-32
node.go
p2p/discv5/node.go
+5
-0
ticket.go
p2p/discv5/ticket.go
+108
-109
topic.go
p2p/discv5/topic.go
+2
-1
No files found.
p2p/discv5/net.go
View file @
c15d76a4
...
@@ -51,16 +51,9 @@ const (
...
@@ -51,16 +51,9 @@ const (
const
testTopic
=
"foo"
const
testTopic
=
"foo"
const
(
const
(
printDebugLogs
=
false
printTestImgLogs
=
false
printTestImgLogs
=
false
)
)
func
debugLog
(
s
string
)
{
if
printDebugLogs
{
fmt
.
Println
(
s
)
}
}
// Network manages the table and all protocol interaction.
// Network manages the table and all protocol interaction.
type
Network
struct
{
type
Network
struct
{
db
*
nodeDB
// database of known nodes
db
*
nodeDB
// database of known nodes
...
@@ -388,14 +381,14 @@ func (net *Network) loop() {
...
@@ -388,14 +381,14 @@ func (net *Network) loop() {
}
}
}()
}()
resetNextTicket
:=
func
()
{
resetNextTicket
:=
func
()
{
t
,
timeout
:=
net
.
ticketStore
.
nextFilteredTicket
()
t
icket
,
timeout
:=
net
.
ticketStore
.
nextFilteredTicket
()
if
t
!=
nextT
icket
{
if
nextTicket
!=
t
icket
{
nextTicket
=
t
nextTicket
=
t
icket
if
nextRegisterTimer
!=
nil
{
if
nextRegisterTimer
!=
nil
{
nextRegisterTimer
.
Stop
()
nextRegisterTimer
.
Stop
()
nextRegisterTime
=
nil
nextRegisterTime
=
nil
}
}
if
t
!=
nil
{
if
t
icket
!=
nil
{
nextRegisterTimer
=
time
.
NewTimer
(
timeout
)
nextRegisterTimer
=
time
.
NewTimer
(
timeout
)
nextRegisterTime
=
nextRegisterTimer
.
C
nextRegisterTime
=
nextRegisterTimer
.
C
}
}
...
@@ -423,13 +416,13 @@ loop:
...
@@ -423,13 +416,13 @@ loop:
select
{
select
{
case
<-
net
.
closeReq
:
case
<-
net
.
closeReq
:
debugLog
(
"<-net.closeReq"
)
log
.
Trace
(
"<-net.closeReq"
)
break
loop
break
loop
// Ingress packet handling.
// Ingress packet handling.
case
pkt
:=
<-
net
.
read
:
case
pkt
:=
<-
net
.
read
:
//fmt.Println("read", pkt.ev)
//fmt.Println("read", pkt.ev)
debugLog
(
"<-net.read"
)
log
.
Trace
(
"<-net.read"
)
n
:=
net
.
internNode
(
&
pkt
)
n
:=
net
.
internNode
(
&
pkt
)
prestate
:=
n
.
state
prestate
:=
n
.
state
status
:=
"ok"
status
:=
"ok"
...
@@ -444,7 +437,7 @@ loop:
...
@@ -444,7 +437,7 @@ loop:
// State transition timeouts.
// State transition timeouts.
case
timeout
:=
<-
net
.
timeout
:
case
timeout
:=
<-
net
.
timeout
:
debugLog
(
"<-net.timeout"
)
log
.
Trace
(
"<-net.timeout"
)
if
net
.
timeoutTimers
[
timeout
]
==
nil
{
if
net
.
timeoutTimers
[
timeout
]
==
nil
{
// Stale timer (was aborted).
// Stale timer (was aborted).
continue
continue
...
@@ -462,20 +455,20 @@ loop:
...
@@ -462,20 +455,20 @@ loop:
// Querying.
// Querying.
case
q
:=
<-
net
.
queryReq
:
case
q
:=
<-
net
.
queryReq
:
debugLog
(
"<-net.queryReq"
)
log
.
Trace
(
"<-net.queryReq"
)
if
!
q
.
start
(
net
)
{
if
!
q
.
start
(
net
)
{
q
.
remote
.
deferQuery
(
q
)
q
.
remote
.
deferQuery
(
q
)
}
}
// Interacting with the table.
// Interacting with the table.
case
f
:=
<-
net
.
tableOpReq
:
case
f
:=
<-
net
.
tableOpReq
:
debugLog
(
"<-net.tableOpReq"
)
log
.
Trace
(
"<-net.tableOpReq"
)
f
()
f
()
net
.
tableOpResp
<-
struct
{}{}
net
.
tableOpResp
<-
struct
{}{}
// Topic registration stuff.
// Topic registration stuff.
case
req
:=
<-
net
.
topicRegisterReq
:
case
req
:=
<-
net
.
topicRegisterReq
:
debugLog
(
"<-net.topicRegisterReq"
)
log
.
Trace
(
"<-net.topicRegisterReq"
)
if
!
req
.
add
{
if
!
req
.
add
{
net
.
ticketStore
.
removeRegisterTopic
(
req
.
topic
)
net
.
ticketStore
.
removeRegisterTopic
(
req
.
topic
)
continue
continue
...
@@ -486,7 +479,7 @@ loop:
...
@@ -486,7 +479,7 @@ loop:
// determination for new topics.
// determination for new topics.
// if topicRegisterLookupDone == nil {
// if topicRegisterLookupDone == nil {
if
topicRegisterLookupTarget
.
target
==
(
common
.
Hash
{})
{
if
topicRegisterLookupTarget
.
target
==
(
common
.
Hash
{})
{
debugLog
(
"topicRegisterLookupTarget == null"
)
log
.
Trace
(
"topicRegisterLookupTarget == null"
)
if
topicRegisterLookupTick
.
Stop
()
{
if
topicRegisterLookupTick
.
Stop
()
{
<-
topicRegisterLookupTick
.
C
<-
topicRegisterLookupTick
.
C
}
}
...
@@ -496,7 +489,7 @@ loop:
...
@@ -496,7 +489,7 @@ loop:
}
}
case
nodes
:=
<-
topicRegisterLookupDone
:
case
nodes
:=
<-
topicRegisterLookupDone
:
debugLog
(
"<-topicRegisterLookupDone"
)
log
.
Trace
(
"<-topicRegisterLookupDone"
)
net
.
ticketStore
.
registerLookupDone
(
topicRegisterLookupTarget
,
nodes
,
func
(
n
*
Node
)
[]
byte
{
net
.
ticketStore
.
registerLookupDone
(
topicRegisterLookupTarget
,
nodes
,
func
(
n
*
Node
)
[]
byte
{
net
.
ping
(
n
,
n
.
addr
())
net
.
ping
(
n
,
n
.
addr
())
return
n
.
pingEcho
return
n
.
pingEcho
...
@@ -507,7 +500,7 @@ loop:
...
@@ -507,7 +500,7 @@ loop:
topicRegisterLookupDone
=
nil
topicRegisterLookupDone
=
nil
case
<-
topicRegisterLookupTick
.
C
:
case
<-
topicRegisterLookupTick
.
C
:
debugLog
(
"<-topicRegisterLookupTick"
)
log
.
Trace
(
"<-topicRegisterLookupTick"
)
if
(
topicRegisterLookupTarget
.
target
==
common
.
Hash
{})
{
if
(
topicRegisterLookupTarget
.
target
==
common
.
Hash
{})
{
target
,
delay
:=
net
.
ticketStore
.
nextRegisterLookup
()
target
,
delay
:=
net
.
ticketStore
.
nextRegisterLookup
()
topicRegisterLookupTarget
=
target
topicRegisterLookupTarget
=
target
...
@@ -520,14 +513,14 @@ loop:
...
@@ -520,14 +513,14 @@ loop:
}
}
case
<-
nextRegisterTime
:
case
<-
nextRegisterTime
:
debugLog
(
"<-nextRegisterTime"
)
log
.
Trace
(
"<-nextRegisterTime"
)
net
.
ticketStore
.
ticketRegistered
(
*
nextTicket
)
net
.
ticketStore
.
ticketRegistered
(
*
nextTicket
)
//fmt.Println("sendTopicRegister", nextTicket.t.node.addr().String(), nextTicket.t.topics, nextTicket.idx, nextTicket.t.pong)
//fmt.Println("sendTopicRegister", nextTicket.t.node.addr().String(), nextTicket.t.topics, nextTicket.idx, nextTicket.t.pong)
net
.
conn
.
sendTopicRegister
(
nextTicket
.
t
.
node
,
nextTicket
.
t
.
topics
,
nextTicket
.
idx
,
nextTicket
.
t
.
pong
)
net
.
conn
.
sendTopicRegister
(
nextTicket
.
t
.
node
,
nextTicket
.
t
.
topics
,
nextTicket
.
idx
,
nextTicket
.
t
.
pong
)
case
req
:=
<-
net
.
topicSearchReq
:
case
req
:=
<-
net
.
topicSearchReq
:
if
refreshDone
==
nil
{
if
refreshDone
==
nil
{
debugLog
(
"<-net.topicSearchReq"
)
log
.
Trace
(
"<-net.topicSearchReq"
)
info
,
ok
:=
searchInfo
[
req
.
topic
]
info
,
ok
:=
searchInfo
[
req
.
topic
]
if
ok
{
if
ok
{
if
req
.
delay
==
time
.
Duration
(
0
)
{
if
req
.
delay
==
time
.
Duration
(
0
)
{
...
@@ -588,7 +581,7 @@ loop:
...
@@ -588,7 +581,7 @@ loop:
})
})
case
<-
statsDump
.
C
:
case
<-
statsDump
.
C
:
debugLog
(
"<-statsDump.C"
)
log
.
Trace
(
"<-statsDump.C"
)
/*r, ok := net.ticketStore.radius[testTopic]
/*r, ok := net.ticketStore.radius[testTopic]
if !ok {
if !ok {
fmt.Printf("(%x) no radius @ %v\n", net.tab.self.ID[:8], time.Now())
fmt.Printf("(%x) no radius @ %v\n", net.tab.self.ID[:8], time.Now())
...
@@ -617,7 +610,7 @@ loop:
...
@@ -617,7 +610,7 @@ loop:
// Periodic / lookup-initiated bucket refresh.
// Periodic / lookup-initiated bucket refresh.
case
<-
refreshTimer
.
C
:
case
<-
refreshTimer
.
C
:
debugLog
(
"<-refreshTimer.C"
)
log
.
Trace
(
"<-refreshTimer.C"
)
// TODO: ideally we would start the refresh timer after
// TODO: ideally we would start the refresh timer after
// fallback nodes have been set for the first time.
// fallback nodes have been set for the first time.
if
refreshDone
==
nil
{
if
refreshDone
==
nil
{
...
@@ -631,7 +624,7 @@ loop:
...
@@ -631,7 +624,7 @@ loop:
bucketRefreshTimer
.
Reset
(
bucketRefreshInterval
)
bucketRefreshTimer
.
Reset
(
bucketRefreshInterval
)
}()
}()
case
newNursery
:=
<-
net
.
refreshReq
:
case
newNursery
:=
<-
net
.
refreshReq
:
debugLog
(
"<-net.refreshReq"
)
log
.
Trace
(
"<-net.refreshReq"
)
if
newNursery
!=
nil
{
if
newNursery
!=
nil
{
net
.
nursery
=
newNursery
net
.
nursery
=
newNursery
}
}
...
@@ -641,7 +634,7 @@ loop:
...
@@ -641,7 +634,7 @@ loop:
}
}
net
.
refreshResp
<-
refreshDone
net
.
refreshResp
<-
refreshDone
case
<-
refreshDone
:
case
<-
refreshDone
:
debugLog
(
"<-net.refreshDone"
)
log
.
Trace
(
"<-net.refreshDone"
)
refreshDone
=
nil
refreshDone
=
nil
list
:=
searchReqWhenRefreshDone
list
:=
searchReqWhenRefreshDone
searchReqWhenRefreshDone
=
nil
searchReqWhenRefreshDone
=
nil
...
@@ -652,7 +645,7 @@ loop:
...
@@ -652,7 +645,7 @@ loop:
}()
}()
}
}
}
}
debugLog
(
"loop stopped"
)
log
.
Trace
(
"loop stopped"
)
log
.
Debug
(
fmt
.
Sprintf
(
"shutting down"
))
log
.
Debug
(
fmt
.
Sprintf
(
"shutting down"
))
if
net
.
conn
!=
nil
{
if
net
.
conn
!=
nil
{
...
@@ -1109,14 +1102,14 @@ func (net *Network) ping(n *Node, addr *net.UDPAddr) {
...
@@ -1109,14 +1102,14 @@ func (net *Network) ping(n *Node, addr *net.UDPAddr) {
//fmt.Println(" not sent")
//fmt.Println(" not sent")
return
return
}
}
debugLog
(
fmt
.
Sprintf
(
"ping(node = %x)"
,
n
.
ID
[
:
8
])
)
log
.
Trace
(
"Pinging remote node"
,
"node"
,
n
.
ID
)
n
.
pingTopics
=
net
.
ticketStore
.
regTopicSet
()
n
.
pingTopics
=
net
.
ticketStore
.
regTopicSet
()
n
.
pingEcho
=
net
.
conn
.
sendPing
(
n
,
addr
,
n
.
pingTopics
)
n
.
pingEcho
=
net
.
conn
.
sendPing
(
n
,
addr
,
n
.
pingTopics
)
net
.
timedEvent
(
respTimeout
,
n
,
pongTimeout
)
net
.
timedEvent
(
respTimeout
,
n
,
pongTimeout
)
}
}
func
(
net
*
Network
)
handlePing
(
n
*
Node
,
pkt
*
ingressPacket
)
{
func
(
net
*
Network
)
handlePing
(
n
*
Node
,
pkt
*
ingressPacket
)
{
debugLog
(
fmt
.
Sprintf
(
"handlePing(node = %x)"
,
n
.
ID
[
:
8
])
)
log
.
Trace
(
"Handling remote ping"
,
"node"
,
n
.
ID
)
ping
:=
pkt
.
data
.
(
*
ping
)
ping
:=
pkt
.
data
.
(
*
ping
)
n
.
TCP
=
ping
.
From
.
TCP
n
.
TCP
=
ping
.
From
.
TCP
t
:=
net
.
topictab
.
getTicket
(
n
,
ping
.
Topics
)
t
:=
net
.
topictab
.
getTicket
(
n
,
ping
.
Topics
)
...
@@ -1131,7 +1124,7 @@ func (net *Network) handlePing(n *Node, pkt *ingressPacket) {
...
@@ -1131,7 +1124,7 @@ func (net *Network) handlePing(n *Node, pkt *ingressPacket) {
}
}
func
(
net
*
Network
)
handleKnownPong
(
n
*
Node
,
pkt
*
ingressPacket
)
error
{
func
(
net
*
Network
)
handleKnownPong
(
n
*
Node
,
pkt
*
ingressPacket
)
error
{
debugLog
(
fmt
.
Sprintf
(
"handleKnownPong(node = %x)"
,
n
.
ID
[
:
8
])
)
log
.
Trace
(
"Handling known pong"
,
"node"
,
n
.
ID
)
net
.
abortTimedEvent
(
n
,
pongTimeout
)
net
.
abortTimedEvent
(
n
,
pongTimeout
)
now
:=
mclock
.
Now
()
now
:=
mclock
.
Now
()
ticket
,
err
:=
pongToTicket
(
now
,
n
.
pingTopics
,
n
,
pkt
)
ticket
,
err
:=
pongToTicket
(
now
,
n
.
pingTopics
,
n
,
pkt
)
...
@@ -1139,9 +1132,8 @@ func (net *Network) handleKnownPong(n *Node, pkt *ingressPacket) error {
...
@@ -1139,9 +1132,8 @@ func (net *Network) handleKnownPong(n *Node, pkt *ingressPacket) error {
// fmt.Printf("(%x) ticket: %+v\n", net.tab.self.ID[:8], pkt.data)
// fmt.Printf("(%x) ticket: %+v\n", net.tab.self.ID[:8], pkt.data)
net
.
ticketStore
.
addTicket
(
now
,
pkt
.
data
.
(
*
pong
)
.
ReplyTok
,
ticket
)
net
.
ticketStore
.
addTicket
(
now
,
pkt
.
data
.
(
*
pong
)
.
ReplyTok
,
ticket
)
}
else
{
}
else
{
debugLog
(
fmt
.
Sprintf
(
" error: %v"
,
err
)
)
log
.
Trace
(
"Failed to convert pong to ticket"
,
"err"
,
err
)
}
}
n
.
pingEcho
=
nil
n
.
pingEcho
=
nil
n
.
pingTopics
=
nil
n
.
pingTopics
=
nil
return
err
return
err
...
...
p2p/discv5/node.go
View file @
c15d76a4
...
@@ -273,6 +273,11 @@ func (n NodeID) GoString() string {
...
@@ -273,6 +273,11 @@ func (n NodeID) GoString() string {
return
fmt
.
Sprintf
(
"discover.HexID(
\"
%x
\"
)"
,
n
[
:
])
return
fmt
.
Sprintf
(
"discover.HexID(
\"
%x
\"
)"
,
n
[
:
])
}
}
// TerminalString returns a shortened hex string for terminal logging.
func
(
n
NodeID
)
TerminalString
()
string
{
return
hex
.
EncodeToString
(
n
[
:
8
])
}
// HexID converts a hex string to a NodeID.
// HexID converts a hex string to a NodeID.
// The string may be prefixed with 0x.
// The string may be prefixed with 0x.
func
HexID
(
in
string
)
(
NodeID
,
error
)
{
func
HexID
(
in
string
)
(
NodeID
,
error
)
{
...
...
p2p/discv5/ticket.go
View file @
c15d76a4
This diff is collapsed.
Click to expand it.
p2p/discv5/topic.go
View file @
c15d76a4
...
@@ -24,6 +24,7 @@ import (
...
@@ -24,6 +24,7 @@ import (
"time"
"time"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/log"
)
)
const
(
const
(
...
@@ -235,7 +236,7 @@ func (t *topicTable) deleteEntry(e *topicEntry) {
...
@@ -235,7 +236,7 @@ func (t *topicTable) deleteEntry(e *topicEntry) {
// It is assumed that topics and waitPeriods have the same length.
// It is assumed that topics and waitPeriods have the same length.
func
(
t
*
topicTable
)
useTicket
(
node
*
Node
,
serialNo
uint32
,
topics
[]
Topic
,
idx
int
,
issueTime
uint64
,
waitPeriods
[]
uint32
)
(
registered
bool
)
{
func
(
t
*
topicTable
)
useTicket
(
node
*
Node
,
serialNo
uint32
,
topics
[]
Topic
,
idx
int
,
issueTime
uint64
,
waitPeriods
[]
uint32
)
(
registered
bool
)
{
debugLog
(
fmt
.
Sprintf
(
"useTicket %v %v %v"
,
serialNo
,
topics
,
waitPeriods
)
)
log
.
Trace
(
"Using discovery ticket"
,
"serial"
,
serialNo
,
"topics"
,
topics
,
"waits"
,
waitPeriods
)
//fmt.Println("useTicket", serialNo, topics, waitPeriods)
//fmt.Println("useTicket", serialNo, topics, waitPeriods)
t
.
collectGarbage
()
t
.
collectGarbage
()
...
...
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