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
0201c04b
Commit
0201c04b
authored
Apr 27, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p/discovery: fix issues raised in the nodeDb PR
parent
8646365b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
36 deletions
+32
-36
database.go
p2p/discover/database.go
+30
-33
table.go
p2p/discover/table.go
+2
-3
No files found.
p2p/discover/database.go
View file @
0201c04b
...
...
@@ -70,7 +70,10 @@ func newPersistentNodeDB(path string) (*nodeDB, error) {
switch
err
{
case
leveldb
.
ErrNotFound
:
// Version not found (i.e. empty cache), insert it
err
=
db
.
Put
(
nodeDBVersionKey
,
currentVer
,
nil
)
if
err
:=
db
.
Put
(
nodeDBVersionKey
,
currentVer
,
nil
);
err
!=
nil
{
db
.
Close
()
return
nil
,
err
}
case
nil
:
// Version present, flush if different
...
...
@@ -82,22 +85,17 @@ func newPersistentNodeDB(path string) (*nodeDB, error) {
return
newPersistentNodeDB
(
path
)
}
}
// Clean up in case of an error
if
err
!=
nil
{
db
.
Close
()
return
nil
,
err
}
return
&
nodeDB
{
lvl
:
db
},
nil
}
//
key generates the leveldb key-blob from a node id and its particular field of
// interest.
func
(
db
*
nodeDB
)
k
ey
(
id
NodeID
,
field
string
)
[]
byte
{
//
makeKey generates the leveldb key-blob from a node id and its particular
//
field of
interest.
func
makeK
ey
(
id
NodeID
,
field
string
)
[]
byte
{
return
append
(
nodeDBItemPrefix
,
append
(
id
[
:
],
field
...
)
...
)
}
// splitKey tries to split a database key into a node id and a field part.
func
(
db
*
nodeDB
)
splitKey
(
key
[]
byte
)
(
id
NodeID
,
field
string
)
{
func
splitKey
(
key
[]
byte
)
(
id
NodeID
,
field
string
)
{
// If the key is not of a node, return it plainly
if
!
bytes
.
HasPrefix
(
key
,
nodeDBItemPrefix
)
{
return
NodeID
{},
string
(
key
)
...
...
@@ -110,27 +108,26 @@ func (db *nodeDB) splitKey(key []byte) (id NodeID, field string) {
return
id
,
field
}
// fetch
Time retrieves a time instance (encoded as a unix timestamp) associated
//
with a particular
database key.
func
(
db
*
nodeDB
)
fetch
Time
(
key
[]
byte
)
time
.
Time
{
// fetch
Int64 retrieves an integer instance associated with a particular
// database key.
func
(
db
*
nodeDB
)
fetch
Int64
(
key
[]
byte
)
int64
{
blob
,
err
:=
db
.
lvl
.
Get
(
key
,
nil
)
if
err
!=
nil
{
return
time
.
Time
{}
return
0
}
va
r
unix
int64
if
err
:=
rlp
.
DecodeBytes
(
blob
,
&
unix
);
err
!=
nil
{
return
time
.
Time
{}
va
l
,
read
:=
binary
.
Varint
(
blob
)
if
read
<=
0
{
return
0
}
return
time
.
Unix
(
unix
,
0
)
return
val
}
// store
Time
update a specific database entry to the current time instance as a
// store
Int64
update a specific database entry to the current time instance as a
// unix timestamp.
func
(
db
*
nodeDB
)
storeTime
(
key
[]
byte
,
instance
time
.
Time
)
error
{
blob
,
err
:=
rlp
.
EncodeToBytes
(
instance
.
Unix
())
if
err
!=
nil
{
return
err
}
func
(
db
*
nodeDB
)
storeInt64
(
key
[]
byte
,
n
int64
)
error
{
blob
:=
make
([]
byte
,
binary
.
MaxVarintLen64
)
blob
=
blob
[
:
binary
.
PutVarint
(
blob
,
n
)]
return
db
.
lvl
.
Put
(
key
,
blob
,
nil
)
}
...
...
@@ -138,17 +135,17 @@ func (db *nodeDB) storeTime(key []byte, instance time.Time) error {
// purpose is to prevent contacting potential seed nodes multiple times in the
// same boot cycle.
func
(
db
*
nodeDB
)
startup
()
time
.
Time
{
return
db
.
fetchTime
(
nodeDBStartupKey
)
return
time
.
Unix
(
db
.
fetchInt64
(
nodeDBStartupKey
),
0
)
}
// updateStartup updates the bootstrap initiation time to the one specified.
func
(
db
*
nodeDB
)
updateStartup
(
instance
time
.
Time
)
error
{
return
db
.
store
Time
(
nodeDBStartupKey
,
instance
)
return
db
.
store
Int64
(
nodeDBStartupKey
,
instance
.
Unix
()
)
}
// node retrieves a node with a given id from the database.
func
(
db
*
nodeDB
)
node
(
id
NodeID
)
*
Node
{
blob
,
err
:=
db
.
lvl
.
Get
(
db
.
k
ey
(
id
,
nodeDBDiscoverRoot
),
nil
)
blob
,
err
:=
db
.
lvl
.
Get
(
makeK
ey
(
id
,
nodeDBDiscoverRoot
),
nil
)
if
err
!=
nil
{
return
nil
}
...
...
@@ -165,28 +162,28 @@ func (db *nodeDB) updateNode(node *Node) error {
if
err
!=
nil
{
return
err
}
return
db
.
lvl
.
Put
(
db
.
k
ey
(
node
.
ID
,
nodeDBDiscoverRoot
),
blob
,
nil
)
return
db
.
lvl
.
Put
(
makeK
ey
(
node
.
ID
,
nodeDBDiscoverRoot
),
blob
,
nil
)
}
// lastPing retrieves the time of the last ping packet send to a remote node,
// requesting binding.
func
(
db
*
nodeDB
)
lastPing
(
id
NodeID
)
time
.
Time
{
return
db
.
fetchTime
(
db
.
key
(
id
,
nodeDBDiscoverPing
)
)
return
time
.
Unix
(
db
.
fetchInt64
(
makeKey
(
id
,
nodeDBDiscoverPing
)),
0
)
}
// updateLastPing updates the last time we tried contacting a remote node.
func
(
db
*
nodeDB
)
updateLastPing
(
id
NodeID
,
instance
time
.
Time
)
error
{
return
db
.
store
Time
(
db
.
key
(
id
,
nodeDBDiscoverPing
),
instance
)
return
db
.
store
Int64
(
makeKey
(
id
,
nodeDBDiscoverPing
),
instance
.
Unix
()
)
}
// lastBond retrieves the time of the last successful bonding with a remote node.
func
(
db
*
nodeDB
)
lastBond
(
id
NodeID
)
time
.
Time
{
return
db
.
fetchTime
(
db
.
key
(
id
,
nodeDBDiscoverBond
)
)
return
time
.
Unix
(
db
.
fetchInt64
(
makeKey
(
id
,
nodeDBDiscoverBond
)),
0
)
}
// updateLastBond updates the last time we successfully bound to a remote node.
func
(
db
*
nodeDB
)
updateLastBond
(
id
NodeID
,
instance
time
.
Time
)
error
{
return
db
.
store
Time
(
db
.
key
(
id
,
nodeDBDiscoverBond
),
instance
)
return
db
.
store
Int64
(
makeKey
(
id
,
nodeDBDiscoverBond
),
instance
.
Unix
()
)
}
// querySeeds retrieves a batch of nodes to be used as potential seed servers
...
...
@@ -208,7 +205,7 @@ func (db *nodeDB) querySeeds(n int) []*Node {
nodes
:=
make
([]
*
Node
,
0
,
n
)
for
len
(
nodes
)
<
n
&&
it
.
Next
()
{
// Iterate until a discovery node is found
id
,
field
:=
db
.
splitKey
(
it
.
Key
())
id
,
field
:=
splitKey
(
it
.
Key
())
if
field
!=
nodeDBDiscoverRoot
{
continue
}
...
...
p2p/discover/table.go
View file @
0201c04b
...
...
@@ -62,13 +62,12 @@ type bucket struct {
}
func
newTable
(
t
transport
,
ourID
NodeID
,
ourAddr
*
net
.
UDPAddr
,
nodeDBPath
string
)
*
Table
{
// If no
seed cach
e was given, use an in-memory one
// If no
node databas
e was given, use an in-memory one
db
,
err
:=
newNodeDB
(
nodeDBPath
)
if
err
!=
nil
{
glog
.
V
(
logger
.
Warn
)
.
Infoln
(
"Failed to open node database:"
,
err
)
db
,
_
=
newNodeDB
(
""
)
}
// Create the bootstrap table
tab
:=
&
Table
{
net
:
t
,
db
:
db
,
...
...
@@ -90,7 +89,7 @@ func (tab *Table) Self() *Node {
return
tab
.
self
}
// Close terminates the network listener and flushes the
seed cach
e.
// Close terminates the network listener and flushes the
node databas
e.
func
(
tab
*
Table
)
Close
()
{
tab
.
net
.
close
()
tab
.
db
.
close
()
...
...
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