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
8de8f61d
Commit
8de8f61d
authored
Apr 27, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p/discover: write the basic tests, catch RLP bug
parent
0201c04b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
8 deletions
+150
-8
database.go
p2p/discover/database.go
+13
-7
database_test.go
p2p/discover/database_test.go
+136
-0
table.go
p2p/discover/table.go
+1
-1
No files found.
p2p/discover/database.go
View file @
8de8f61d
...
@@ -14,6 +14,9 @@ import (
...
@@ -14,6 +14,9 @@ import (
"github.com/syndtr/goleveldb/leveldb/storage"
"github.com/syndtr/goleveldb/leveldb/storage"
)
)
// Special node ID to use as a nil element.
var
nodeDBNilNodeID
=
NodeID
{}
// nodeDB stores all nodes we know about.
// nodeDB stores all nodes we know about.
type
nodeDB
struct
{
type
nodeDB
struct
{
lvl
*
leveldb
.
DB
lvl
*
leveldb
.
DB
...
@@ -27,7 +30,7 @@ var (
...
@@ -27,7 +30,7 @@ var (
nodeDBDiscoverRoot
=
":discover"
nodeDBDiscoverRoot
=
":discover"
nodeDBDiscoverPing
=
nodeDBDiscoverRoot
+
":lastping"
nodeDBDiscoverPing
=
nodeDBDiscoverRoot
+
":lastping"
nodeDBDiscover
Bond
=
nodeDBDiscoverRoot
+
":lastbond
"
nodeDBDiscover
Pong
=
nodeDBDiscoverRoot
+
":lastpong
"
)
)
// newNodeDB creates a new node database for storing and retrieving infos about
// newNodeDB creates a new node database for storing and retrieving infos about
...
@@ -91,6 +94,9 @@ func newPersistentNodeDB(path string) (*nodeDB, error) {
...
@@ -91,6 +94,9 @@ func newPersistentNodeDB(path string) (*nodeDB, error) {
// makeKey generates the leveldb key-blob from a node id and its particular
// makeKey generates the leveldb key-blob from a node id and its particular
// field of interest.
// field of interest.
func
makeKey
(
id
NodeID
,
field
string
)
[]
byte
{
func
makeKey
(
id
NodeID
,
field
string
)
[]
byte
{
if
bytes
.
Equal
(
id
[
:
],
nodeDBNilNodeID
[
:
])
{
return
[]
byte
(
field
)
}
return
append
(
nodeDBItemPrefix
,
append
(
id
[
:
],
field
...
)
...
)
return
append
(
nodeDBItemPrefix
,
append
(
id
[
:
],
field
...
)
...
)
}
}
...
@@ -176,14 +182,14 @@ func (db *nodeDB) updateLastPing(id NodeID, instance time.Time) error {
...
@@ -176,14 +182,14 @@ func (db *nodeDB) updateLastPing(id NodeID, instance time.Time) error {
return
db
.
storeInt64
(
makeKey
(
id
,
nodeDBDiscoverPing
),
instance
.
Unix
())
return
db
.
storeInt64
(
makeKey
(
id
,
nodeDBDiscoverPing
),
instance
.
Unix
())
}
}
// last
Bond retrieves the time of the last successful bonding with a
remote node.
// last
Pong retrieves the time of the last successful contact from
remote node.
func
(
db
*
nodeDB
)
last
Bond
(
id
NodeID
)
time
.
Time
{
func
(
db
*
nodeDB
)
last
Pong
(
id
NodeID
)
time
.
Time
{
return
time
.
Unix
(
db
.
fetchInt64
(
makeKey
(
id
,
nodeDBDiscover
Bond
)),
0
)
return
time
.
Unix
(
db
.
fetchInt64
(
makeKey
(
id
,
nodeDBDiscover
Pong
)),
0
)
}
}
// updateLast
Bond updates the last time we successfully bound to a remote node
.
// updateLast
Pong updates the last time a remote node successfully contacted
.
func
(
db
*
nodeDB
)
updateLast
Bond
(
id
NodeID
,
instance
time
.
Time
)
error
{
func
(
db
*
nodeDB
)
updateLast
Pong
(
id
NodeID
,
instance
time
.
Time
)
error
{
return
db
.
storeInt64
(
makeKey
(
id
,
nodeDBDiscover
Bond
),
instance
.
Unix
())
return
db
.
storeInt64
(
makeKey
(
id
,
nodeDBDiscover
Pong
),
instance
.
Unix
())
}
}
// querySeeds retrieves a batch of nodes to be used as potential seed servers
// querySeeds retrieves a batch of nodes to be used as potential seed servers
...
...
p2p/discover/database_test.go
0 → 100644
View file @
8de8f61d
package
discover
import
(
"bytes"
"net"
"testing"
"time"
)
var
nodeDBKeyTests
=
[]
struct
{
id
NodeID
field
string
key
[]
byte
}{
{
id
:
NodeID
{},
field
:
"version"
,
key
:
[]
byte
{
0x76
,
0x65
,
0x72
,
0x73
,
0x69
,
0x6f
,
0x6e
},
// field
},
{
id
:
MustHexID
(
"0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"
),
field
:
":discover"
,
key
:
[]
byte
{
0x6e
,
0x3a
,
// prefix
0x1d
,
0xd9
,
0xd6
,
0x5c
,
0x45
,
0x52
,
0xb5
,
0xeb
,
// node id
0x43
,
0xd5
,
0xad
,
0x55
,
0xa2
,
0xee
,
0x3f
,
0x56
,
//
0xc6
,
0xcb
,
0xc1
,
0xc6
,
0x4a
,
0x5c
,
0x8d
,
0x65
,
//
0x9f
,
0x51
,
0xfc
,
0xd5
,
0x1b
,
0xac
,
0xe2
,
0x43
,
//
0x51
,
0x23
,
0x2b
,
0x8d
,
0x78
,
0x21
,
0x61
,
0x7d
,
//
0x2b
,
0x29
,
0xb5
,
0x4b
,
0x81
,
0xcd
,
0xef
,
0xb9
,
//
0xb3
,
0xe9
,
0xc3
,
0x7d
,
0x7f
,
0xd5
,
0xf6
,
0x32
,
//
0x70
,
0xbc
,
0xc9
,
0xe1
,
0xa6
,
0xf6
,
0xa4
,
0x39
,
//
0x3a
,
0x64
,
0x69
,
0x73
,
0x63
,
0x6f
,
0x76
,
0x65
,
0x72
,
// field
},
},
}
func
TestNodeDBKeys
(
t
*
testing
.
T
)
{
for
i
,
tt
:=
range
nodeDBKeyTests
{
if
key
:=
makeKey
(
tt
.
id
,
tt
.
field
);
!
bytes
.
Equal
(
key
,
tt
.
key
)
{
t
.
Errorf
(
"make test %d: key mismatch: have 0x%x, want 0x%x"
,
i
,
key
,
tt
.
key
)
}
id
,
field
:=
splitKey
(
tt
.
key
)
if
!
bytes
.
Equal
(
id
[
:
],
tt
.
id
[
:
])
{
t
.
Errorf
(
"split test %d: id mismatch: have 0x%x, want 0x%x"
,
i
,
id
,
tt
.
id
)
}
if
field
!=
tt
.
field
{
t
.
Errorf
(
"split test %d: field mismatch: have 0x%x, want 0x%x"
,
i
,
field
,
tt
.
field
)
}
}
}
var
nodeDBInt64Tests
=
[]
struct
{
key
[]
byte
value
int64
}{
{
key
:
[]
byte
{
0x01
},
value
:
1
},
{
key
:
[]
byte
{
0x02
},
value
:
2
},
{
key
:
[]
byte
{
0x03
},
value
:
3
},
}
func
TestNodeDBInt64
(
t
*
testing
.
T
)
{
db
,
_
:=
newNodeDB
(
""
)
tests
:=
nodeDBInt64Tests
for
i
:=
0
;
i
<
len
(
tests
);
i
++
{
// Insert the next value
if
err
:=
db
.
storeInt64
(
tests
[
i
]
.
key
,
tests
[
i
]
.
value
);
err
!=
nil
{
t
.
Errorf
(
"test %d: failed to store value: %v"
,
i
,
err
)
}
// Check all existing and non existing values
for
j
:=
0
;
j
<
len
(
tests
);
j
++
{
num
:=
db
.
fetchInt64
(
tests
[
j
]
.
key
)
switch
{
case
j
<=
i
&&
num
!=
tests
[
j
]
.
value
:
t
.
Errorf
(
"test %d, item %d: value mismatch: have %v, want %v"
,
i
,
j
,
num
,
tests
[
j
]
.
value
)
case
j
>
i
&&
num
!=
0
:
t
.
Errorf
(
"test %d, item %d: value mismatch: have %v, want %v"
,
i
,
j
,
num
,
0
)
}
}
}
}
func
TestNodeDBFetchStore
(
t
*
testing
.
T
)
{
node
:=
&
Node
{
ID
:
MustHexID
(
"0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"
),
IP
:
net
.
IP
([]
byte
{
192
,
168
,
0
,
1
}),
DiscPort
:
31313
,
TCPPort
:
30303
,
}
inst
:=
time
.
Now
()
db
,
_
:=
newNodeDB
(
""
)
// Check fetch/store operations on the startup object
if
stored
:=
db
.
startup
();
stored
.
Unix
()
!=
0
{
t
.
Errorf
(
"startup: non-existing object: %v"
,
stored
)
}
if
err
:=
db
.
updateStartup
(
inst
);
err
!=
nil
{
t
.
Errorf
(
"startup: failed to update: %v"
,
err
)
}
if
stored
:=
db
.
startup
();
stored
.
Unix
()
!=
inst
.
Unix
()
{
t
.
Errorf
(
"startup: value mismatch: have %v, want %v"
,
stored
,
inst
)
}
// Check fetch/store operations on a node ping object
if
stored
:=
db
.
lastPing
(
node
.
ID
);
stored
.
Unix
()
!=
0
{
t
.
Errorf
(
"ping: non-existing object: %v"
,
stored
)
}
if
err
:=
db
.
updateLastPing
(
node
.
ID
,
inst
);
err
!=
nil
{
t
.
Errorf
(
"ping: failed to update: %v"
,
err
)
}
if
stored
:=
db
.
lastPing
(
node
.
ID
);
stored
.
Unix
()
!=
inst
.
Unix
()
{
t
.
Errorf
(
"ping: value mismatch: have %v, want %v"
,
stored
,
inst
)
}
// Check fetch/store operations on a node pong object
if
stored
:=
db
.
lastPong
(
node
.
ID
);
stored
.
Unix
()
!=
0
{
t
.
Errorf
(
"pong: non-existing object: %v"
,
stored
)
}
if
err
:=
db
.
updateLastPong
(
node
.
ID
,
inst
);
err
!=
nil
{
t
.
Errorf
(
"pong: failed to update: %v"
,
err
)
}
if
stored
:=
db
.
lastPong
(
node
.
ID
);
stored
.
Unix
()
!=
inst
.
Unix
()
{
t
.
Errorf
(
"pong: value mismatch: have %v, want %v"
,
stored
,
inst
)
}
// Check fetch/store operations on an actual node object
if
stored
:=
db
.
node
(
node
.
ID
);
stored
!=
nil
{
t
.
Errorf
(
"node: non-existing object: %v"
,
stored
)
}
if
err
:=
db
.
updateNode
(
node
);
err
!=
nil
{
t
.
Errorf
(
"node: failed to update: %v"
,
err
)
}
if
stored
:=
db
.
node
(
node
.
ID
);
stored
==
nil
{
t
.
Errorf
(
"node: not found"
)
}
else
if
!
bytes
.
Equal
(
stored
.
ID
[
:
],
node
.
ID
[
:
])
||
!
bytes
.
Equal
(
stored
.
IP
,
node
.
IP
)
||
stored
.
DiscPort
!=
node
.
DiscPort
||
stored
.
TCPPort
!=
node
.
TCPPort
{
t
.
Errorf
(
"node: data mismatch: have %v, want %v"
,
stored
,
node
)
}
}
p2p/discover/table.go
View file @
8de8f61d
...
@@ -307,7 +307,7 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd
...
@@ -307,7 +307,7 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd
TCPPort
:
int
(
tcpPort
),
TCPPort
:
int
(
tcpPort
),
}
}
tab
.
db
.
updateNode
(
w
.
n
)
tab
.
db
.
updateNode
(
w
.
n
)
tab
.
db
.
updateLast
Bond
(
id
,
time
.
Now
())
tab
.
db
.
updateLast
Pong
(
id
,
time
.
Now
())
close
(
w
.
done
)
close
(
w
.
done
)
}
}
...
...
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