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
af923c96
Commit
af923c96
authored
Apr 23, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p/discovery: use the seed table for finding nodes, auto drop stale ones
parent
5f735d6f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
5 deletions
+38
-5
node.go
p2p/discover/node.go
+30
-3
table.go
p2p/discover/table.go
+8
-2
No files found.
p2p/discover/node.go
View file @
af923c96
...
...
@@ -325,12 +325,13 @@ func newNodeDB(path string, version int64) (db *nodeDB, err error) {
if
path
==
""
{
db
.
ldb
,
err
=
leveldb
.
Open
(
storage
.
NewMemStorage
(),
opts
)
}
else
{
db
.
ldb
,
err
=
open
L
DB
(
path
,
opts
,
version
)
db
.
ldb
,
err
=
open
Node
DB
(
path
,
opts
,
version
)
}
return
db
,
err
}
func
openLDB
(
path
string
,
opts
*
opt
.
Options
,
version
int64
)
(
*
leveldb
.
DB
,
error
)
{
// openNodeDB opens a persistent seed cache, flushing old versions.
func
openNodeDB
(
path
string
,
opts
*
opt
.
Options
,
version
int64
)
(
*
leveldb
.
DB
,
error
)
{
ldb
,
err
:=
leveldb
.
OpenFile
(
path
,
opts
)
if
_
,
iscorrupted
:=
err
.
(
leveldb
.
ErrCorrupted
);
iscorrupted
{
ldb
,
err
=
leveldb
.
RecoverFile
(
path
,
opts
)
...
...
@@ -353,7 +354,7 @@ func openLDB(path string, opts *opt.Options, version int64) (*leveldb.DB, error)
if
err
=
os
.
RemoveAll
(
path
);
err
!=
nil
{
return
nil
,
err
}
return
open
L
DB
(
path
,
opts
,
version
)
return
open
Node
DB
(
path
,
opts
,
version
)
}
if
err
!=
nil
{
ldb
.
Close
()
...
...
@@ -362,6 +363,7 @@ func openLDB(path string, opts *opt.Options, version int64) (*leveldb.DB, error)
return
ldb
,
err
}
// get retrieves a node with a given id from the seed da
func
(
db
*
nodeDB
)
get
(
id
NodeID
)
*
Node
{
v
,
err
:=
db
.
ldb
.
Get
(
id
[
:
],
nil
)
if
err
!=
nil
{
...
...
@@ -374,6 +376,24 @@ func (db *nodeDB) get(id NodeID) *Node {
return
n
}
// list retrieves a batch of nodes from the database.
func
(
db
*
nodeDB
)
list
(
n
int
)
[]
*
Node
{
it
:=
db
.
ldb
.
NewIterator
(
nil
,
nil
)
defer
it
.
Release
()
nodes
:=
make
([]
*
Node
,
0
,
n
)
for
i
:=
0
;
i
<
n
&&
it
.
Next
();
i
++
{
var
id
NodeID
copy
(
id
[
:
],
it
.
Key
())
if
node
:=
db
.
get
(
id
);
node
!=
nil
{
nodes
=
append
(
nodes
,
node
)
}
}
return
nodes
}
// update inserts - potentially overwriting - a node in the seed database.
func
(
db
*
nodeDB
)
update
(
n
*
Node
)
error
{
v
,
err
:=
rlp
.
EncodeToBytes
(
n
)
if
err
!=
nil
{
...
...
@@ -382,12 +402,19 @@ func (db *nodeDB) update(n *Node) error {
return
db
.
ldb
.
Put
(
n
.
ID
[
:
],
v
,
nil
)
}
// add inserts a new node into the seed database.
func
(
db
*
nodeDB
)
add
(
id
NodeID
,
addr
*
net
.
UDPAddr
,
tcpPort
uint16
)
*
Node
{
n
:=
&
Node
{
ID
:
id
,
IP
:
addr
.
IP
,
DiscPort
:
addr
.
Port
,
TCPPort
:
int
(
tcpPort
)}
db
.
update
(
n
)
return
n
}
// delete removes a node from the database.
func
(
db
*
nodeDB
)
delete
(
id
NodeID
)
error
{
return
db
.
ldb
.
Delete
(
id
[
:
],
nil
)
}
// close flushes and closes the database files.
func
(
db
*
nodeDB
)
close
()
{
db
.
ldb
.
Close
()
}
p2p/discover/table.go
View file @
af923c96
...
...
@@ -177,8 +177,14 @@ func (tab *Table) refresh() {
result
:=
tab
.
Lookup
(
randomID
(
tab
.
self
.
ID
,
ld
))
if
len
(
result
)
==
0
{
// bootstrap the table with a self lookup
all
:=
tab
.
bondall
(
tab
.
nursery
)
// Pick a batch of previously know seeds to lookup with and discard them (will come back if they are still live)
seeds
:=
tab
.
db
.
list
(
10
)
for
_
,
seed
:=
range
seeds
{
glog
.
V
(
logger
.
Debug
)
.
Infoln
(
"Seeding network with:"
,
seed
)
tab
.
db
.
delete
(
seed
.
ID
)
}
// Bootstrap the table with a self lookup
all
:=
tab
.
bondall
(
append
(
tab
.
nursery
,
seeds
...
))
tab
.
mutex
.
Lock
()
tab
.
add
(
all
)
tab
.
mutex
.
Unlock
()
...
...
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