Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kad-dht
Commits
0a9810a1
Commit
0a9810a1
authored
Sep 19, 2014
by
Juan Batiz-Benet
Committed by
Brian Tiger Chow
Sep 22, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dht tests pass again
parent
b0fbaeee
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
169 additions
and
143 deletions
+169
-143
Message.go
Message.go
+1
-1
dht.go
dht.go
+4
-1
dht_test.go
dht_test.go
+156
-139
ext_test.go
ext_test.go
+7
-1
handlers.go
handlers.go
+1
-1
No files found.
Message.go
View file @
0a9810a1
...
...
@@ -46,7 +46,7 @@ func peersToPBPeers(peers []*peer.Peer) []*Message_Peer {
func
(
m
*
Message
)
GetClusterLevel
()
int
{
level
:=
m
.
GetClusterLevelRaw
()
-
1
if
level
<
0
{
u
.
P
Err
(
"GetClusterLevel: no routing level specified, assuming 0
\n
"
)
u
.
D
Err
(
"GetClusterLevel: no routing level specified, assuming 0
\n
"
)
level
=
0
}
return
int
(
level
)
...
...
dht.go
View file @
0a9810a1
...
...
@@ -215,6 +215,7 @@ func (dht *IpfsDHT) putProvider(ctx context.Context, p *peer.Peer, key string) e
return
err
}
u
.
DOut
(
"[%s] putProvider: %s for %s
\n
"
,
dht
.
self
.
ID
.
Pretty
(),
p
.
ID
.
Pretty
(),
key
)
if
*
rpmes
.
Key
!=
*
pmes
.
Key
{
return
errors
.
New
(
"provider not added correctly"
)
}
...
...
@@ -393,6 +394,8 @@ func (dht *IpfsDHT) addProviders(key u.Key, peers []*Message_Peer) []*peer.Peer
continue
}
u
.
DOut
(
"[%s] adding provider: %s for %s"
,
dht
.
self
.
ID
.
Pretty
(),
p
,
key
)
// Dont add outselves to the list
if
p
.
ID
.
Equal
(
dht
.
self
.
ID
)
{
continue
...
...
@@ -464,7 +467,7 @@ func (dht *IpfsDHT) peerFromInfo(pbp *Message_Peer) (*peer.Peer, error) {
}
// create new Peer
p
:
=
&
peer
.
Peer
{
ID
:
id
}
p
=
&
peer
.
Peer
{
ID
:
id
}
p
.
AddAddress
(
maddr
)
dht
.
peerstore
.
Put
(
p
)
}
...
...
dht_test.go
View file @
0a9810a1
...
...
@@ -86,7 +86,9 @@ func makePeer(addr *ma.Multiaddr) *peer.Peer {
}
func
TestPing
(
t
*
testing
.
T
)
{
u
.
Debug
=
true
// t.Skip("skipping test to debug another")
u
.
Debug
=
false
addrA
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/2222"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -104,6 +106,8 @@ func TestPing(t *testing.T) {
defer
dhtA
.
Halt
()
defer
dhtB
.
Halt
()
defer
dhtA
.
network
.
Close
()
defer
dhtB
.
network
.
Close
()
_
,
err
=
dhtA
.
Connect
(
peerB
)
if
err
!=
nil
{
...
...
@@ -118,7 +122,9 @@ func TestPing(t *testing.T) {
}
func
TestValueGetSet
(
t
*
testing
.
T
)
{
u
.
Debug
=
true
// t.Skip("skipping test to debug another")
u
.
Debug
=
false
addrA
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/1235"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -136,6 +142,8 @@ func TestValueGetSet(t *testing.T) {
defer
dhtA
.
Halt
()
defer
dhtB
.
Halt
()
defer
dhtA
.
network
.
Close
()
defer
dhtB
.
network
.
Close
()
_
,
err
=
dhtA
.
Connect
(
peerB
)
if
err
!=
nil
{
...
...
@@ -155,140 +163,149 @@ func TestValueGetSet(t *testing.T) {
}
// func TestProvides(t *testing.T) {
// u.Debug = false
//
// _, peers, dhts := setupDHTS(4, t)
// defer func() {
// for i := 0; i < 4; i++ {
// dhts[i].Halt()
// }
// }()
//
// _, err := dhts[0].Connect(peers[1])
// if err != nil {
// t.Fatal(err)
// }
//
// _, err = dhts[1].Connect(peers[2])
// if err != nil {
// t.Fatal(err)
// }
//
// _, err = dhts[1].Connect(peers[3])
// if err != nil {
// t.Fatal(err)
// }
//
// err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
// if err != nil {
// t.Fatal(err)
// }
//
// bits, err := dhts[3].getLocal(u.Key("hello"))
// if err != nil && bytes.Equal(bits, []byte("world")) {
// t.Fatal(err)
// }
//
// err = dhts[3].Provide(u.Key("hello"))
// if err != nil {
// t.Fatal(err)
// }
//
// time.Sleep(time.Millisecond * 60)
//
// provs, err := dhts[0].FindProviders(u.Key("hello"), time.Second)
// if err != nil {
// t.Fatal(err)
// }
//
// if len(provs) != 1 {
// t.Fatal("Didnt get back providers")
// }
// }
//
// func TestLayeredGet(t *testing.T) {
// u.Debug = false
// addrs, _, dhts := setupDHTS(4, t)
// defer func() {
// for i := 0; i < 4; i++ {
// dhts[i].Halt()
// }
// }()
//
// _, err := dhts[0].Connect(addrs[1])
// if err != nil {
// t.Fatalf("Failed to connect: %s", err)
// }
//
// _, err = dhts[1].Connect(addrs[2])
// if err != nil {
// t.Fatal(err)
// }
//
// _, err = dhts[1].Connect(addrs[3])
// if err != nil {
// t.Fatal(err)
// }
//
// err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
// if err != nil {
// t.Fatal(err)
// }
//
// err = dhts[3].Provide(u.Key("hello"))
// if err != nil {
// t.Fatal(err)
// }
//
// time.Sleep(time.Millisecond * 60)
//
// val, err := dhts[0].GetValue(u.Key("hello"), time.Second)
// if err != nil {
// t.Fatal(err)
// }
//
// if string(val) != "world" {
// t.Fatal("Got incorrect value.")
// }
//
// }
//
// func TestFindPeer(t *testing.T) {
// u.Debug = false
//
// addrs, peers, dhts := setupDHTS(4, t)
// go func() {
// for i := 0; i < 4; i++ {
// dhts[i].Halt()
// }
// }()
//
// _, err := dhts[0].Connect(addrs[1])
// if err != nil {
// t.Fatal(err)
// }
//
// _, err = dhts[1].Connect(addrs[2])
// if err != nil {
// t.Fatal(err)
// }
//
// _, err = dhts[1].Connect(addrs[3])
// if err != nil {
// t.Fatal(err)
// }
//
// p, err := dhts[0].FindPeer(peers[2].ID, time.Second)
// if err != nil {
// t.Fatal(err)
// }
//
// if p == nil {
// t.Fatal("Failed to find peer.")
// }
//
// if !p.ID.Equal(peers[2].ID) {
// t.Fatal("Didnt find expected peer.")
// }
// }
func
TestProvides
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
u
.
Debug
=
false
_
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
defer
func
()
{
for
i
:=
0
;
i
<
4
;
i
++
{
dhts
[
i
]
.
Halt
()
defer
dhts
[
i
]
.
network
.
Close
()
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
peers
[
1
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
err
=
dhts
[
3
]
.
putLocal
(
u
.
Key
(
"hello"
),
[]
byte
(
"world"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
bits
,
err
:=
dhts
[
3
]
.
getLocal
(
u
.
Key
(
"hello"
))
if
err
!=
nil
&&
bytes
.
Equal
(
bits
,
[]
byte
(
"world"
))
{
t
.
Fatal
(
err
)
}
err
=
dhts
[
3
]
.
Provide
(
u
.
Key
(
"hello"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
time
.
Sleep
(
time
.
Millisecond
*
60
)
provs
,
err
:=
dhts
[
0
]
.
FindProviders
(
u
.
Key
(
"hello"
),
time
.
Second
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
len
(
provs
)
!=
1
{
t
.
Fatal
(
"Didnt get back providers"
)
}
}
func
TestLayeredGet
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
u
.
Debug
=
false
_
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
defer
func
()
{
for
i
:=
0
;
i
<
4
;
i
++
{
dhts
[
i
]
.
Halt
()
defer
dhts
[
i
]
.
network
.
Close
()
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
peers
[
1
])
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to connect: %s"
,
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
err
=
dhts
[
3
]
.
putLocal
(
u
.
Key
(
"hello"
),
[]
byte
(
"world"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
err
=
dhts
[
3
]
.
Provide
(
u
.
Key
(
"hello"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
time
.
Sleep
(
time
.
Millisecond
*
60
)
val
,
err
:=
dhts
[
0
]
.
GetValue
(
u
.
Key
(
"hello"
),
time
.
Second
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
string
(
val
)
!=
"world"
{
t
.
Fatal
(
"Got incorrect value."
)
}
}
func
TestFindPeer
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
u
.
Debug
=
false
_
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
defer
func
()
{
for
i
:=
0
;
i
<
4
;
i
++
{
dhts
[
i
]
.
Halt
()
dhts
[
i
]
.
network
.
Close
()
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
peers
[
1
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
p
,
err
:=
dhts
[
0
]
.
FindPeer
(
peers
[
2
]
.
ID
,
time
.
Second
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
p
==
nil
{
t
.
Fatal
(
"Failed to find peer."
)
}
if
!
p
.
ID
.
Equal
(
peers
[
2
]
.
ID
)
{
t
.
Fatal
(
"Didnt find expected peer."
)
}
}
ext_test.go
View file @
0a9810a1
...
...
@@ -92,6 +92,8 @@ func (f *fauxNet) SendMessage(msg.NetMessage) error {
func
(
f
*
fauxNet
)
Close
()
error
{
return
nil
}
func
TestGetFailures
(
t
*
testing
.
T
)
{
// t.Skip("skipping test because it makes a lot of output")
ctx
:=
context
.
Background
()
fn
:=
&
fauxNet
{}
fs
:=
&
fauxSender
{}
...
...
@@ -189,6 +191,8 @@ func _randPeer() *peer.Peer {
}
func
TestNotFound
(
t
*
testing
.
T
)
{
// t.Skip("skipping test because it makes a lot of output")
fn
:=
&
fauxNet
{}
fs
:=
&
fauxSender
{}
...
...
@@ -233,7 +237,7 @@ func TestNotFound(t *testing.T) {
})
v
,
err
:=
d
.
GetValue
(
u
.
Key
(
"hello"
),
time
.
Second
*
5
)
u
.
P
Out
(
"get value got %v
\n
"
,
v
)
u
.
D
Out
(
"get value got %v
\n
"
,
v
)
if
err
!=
nil
{
switch
err
{
case
u
.
ErrNotFound
:
...
...
@@ -251,6 +255,8 @@ func TestNotFound(t *testing.T) {
// If less than K nodes are in the entire network, it should fail when we make
// a GET rpc and nobody has the value
func
TestLessThanKResponses
(
t
*
testing
.
T
)
{
// t.Skip("skipping test because it makes a lot of output")
u
.
Debug
=
false
fn
:=
&
fauxNet
{}
fs
:=
&
fauxSender
{}
...
...
handlers.go
View file @
0a9810a1
...
...
@@ -176,7 +176,7 @@ func (dht *IpfsDHT) handleAddProvider(p *peer.Peer, pmes *Message) (*Message, er
dht
.
self
.
ID
.
Pretty
(),
p
.
ID
.
Pretty
(),
peer
.
ID
(
key
)
.
Pretty
())
dht
.
providers
.
AddProvider
(
key
,
p
)
return
nil
,
nil
return
pmes
,
nil
// send back same msg as confirmation.
}
// Halt stops all communications from this peer and shut down
...
...
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