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
dms3
go-bitswap
Commits
f56f5506
Commit
f56f5506
authored
Nov 23, 2014
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add a test to blockservice to demonstate GetBlocks failure.
parent
fe048093
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
106 additions
and
104 deletions
+106
-104
bitswap.go
bitswap.go
+3
-16
bitswap_test.go
bitswap_test.go
+2
-88
testutils.go
testutils.go
+101
-0
No files found.
bitswap.go
View file @
f56f5506
...
@@ -262,7 +262,6 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
...
@@ -262,7 +262,6 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
}
}
}
}
first
:=
true
for
_
,
key
:=
range
incoming
.
Wantlist
()
{
for
_
,
key
:=
range
incoming
.
Wantlist
()
{
// TODO: might be better to check if we have the block before checking
// TODO: might be better to check if we have the block before checking
// if we should send it to someone
// if we should send it to someone
...
@@ -273,12 +272,10 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
...
@@ -273,12 +272,10 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
// Create a separate message to send this block in
// Create a separate message to send this block in
blkmsg
:=
bsmsg
.
New
()
blkmsg
:=
bsmsg
.
New
()
if
first
{
// TODO: only send this the
first
time
for
_
,
k
:=
range
bs
.
wantlist
.
Keys
()
{
for
_
,
k
:=
range
bs
.
wantlist
.
Keys
()
{
blkmsg
.
AddWanted
(
k
)
blkmsg
.
AddWanted
(
k
)
}
}
first
=
false
}
blkmsg
.
AddBlock
(
block
)
blkmsg
.
AddBlock
(
block
)
bs
.
strategy
.
MessageSent
(
p
,
blkmsg
)
bs
.
strategy
.
MessageSent
(
p
,
blkmsg
)
...
@@ -287,16 +284,6 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
...
@@ -287,16 +284,6 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
}
}
}
}
// If they send us a block, we should guarantee that we send
// them our updated want list one way or another
if
len
(
incoming
.
Blocks
())
>
0
&&
first
{
message
:=
bsmsg
.
New
()
for
_
,
k
:=
range
bs
.
wantlist
.
Keys
()
{
message
.
AddWanted
(
k
)
}
return
p
,
message
}
return
nil
,
nil
return
nil
,
nil
}
}
...
...
bitswap_test.go
View file @
f56f5506
package
bitswap
_test
package
bitswap
import
(
import
(
"bytes"
"bytes"
...
@@ -7,13 +7,8 @@ import (
...
@@ -7,13 +7,8 @@ import (
"time"
"time"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
.
"github.com/jbenet/go-ipfs/exchange/bitswap"
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
ds_sync
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
blocks
"github.com/jbenet/go-ipfs/blocks"
blocks
"github.com/jbenet/go-ipfs/blocks"
blockstore
"github.com/jbenet/go-ipfs/blocks/blockstore"
exchange
"github.com/jbenet/go-ipfs/exchange"
tn
"github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
tn
"github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
peer
"github.com/jbenet/go-ipfs/peer"
peer
"github.com/jbenet/go-ipfs/peer"
mock
"github.com/jbenet/go-ipfs/routing/mock"
mock
"github.com/jbenet/go-ipfs/routing/mock"
...
@@ -170,7 +165,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
...
@@ -170,7 +165,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
}
}
}
}
func
getOrFail
(
bitswap
i
nstance
,
b
*
blocks
.
Block
,
t
*
testing
.
T
,
wg
*
sync
.
WaitGroup
)
{
func
getOrFail
(
bitswap
I
nstance
,
b
*
blocks
.
Block
,
t
*
testing
.
T
,
wg
*
sync
.
WaitGroup
)
{
if
_
,
err
:=
bitswap
.
blockstore
.
Get
(
b
.
Key
());
err
!=
nil
{
if
_
,
err
:=
bitswap
.
blockstore
.
Get
(
b
.
Key
());
err
!=
nil
{
_
,
err
:=
bitswap
.
exchange
.
GetBlock
(
context
.
Background
(),
b
.
Key
())
_
,
err
:=
bitswap
.
exchange
.
GetBlock
(
context
.
Background
(),
b
.
Key
())
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -246,84 +241,3 @@ func TestSendToWantingPeer(t *testing.T) {
...
@@ -246,84 +241,3 @@ func TestSendToWantingPeer(t *testing.T) {
t
.
Fatal
(
"Expected to receive alpha from me"
)
t
.
Fatal
(
"Expected to receive alpha from me"
)
}
}
}
}
func
NewBlockGenerator
()
BlockGenerator
{
return
BlockGenerator
{}
}
type
BlockGenerator
struct
{
seq
int
}
func
(
bg
*
BlockGenerator
)
Next
()
*
blocks
.
Block
{
bg
.
seq
++
return
blocks
.
NewBlock
([]
byte
(
string
(
bg
.
seq
)))
}
func
(
bg
*
BlockGenerator
)
Blocks
(
n
int
)
[]
*
blocks
.
Block
{
blocks
:=
make
([]
*
blocks
.
Block
,
0
)
for
i
:=
0
;
i
<
n
;
i
++
{
b
:=
bg
.
Next
()
blocks
=
append
(
blocks
,
b
)
}
return
blocks
}
func
NewSessionGenerator
(
net
tn
.
Network
,
rs
mock
.
RoutingServer
)
SessionGenerator
{
return
SessionGenerator
{
net
:
net
,
rs
:
rs
,
seq
:
0
,
}
}
type
SessionGenerator
struct
{
seq
int
net
tn
.
Network
rs
mock
.
RoutingServer
}
func
(
g
*
SessionGenerator
)
Next
()
instance
{
g
.
seq
++
return
session
(
g
.
net
,
g
.
rs
,
[]
byte
(
string
(
g
.
seq
)))
}
func
(
g
*
SessionGenerator
)
Instances
(
n
int
)
[]
instance
{
instances
:=
make
([]
instance
,
0
)
for
j
:=
0
;
j
<
n
;
j
++
{
inst
:=
g
.
Next
()
instances
=
append
(
instances
,
inst
)
}
return
instances
}
type
instance
struct
{
peer
peer
.
Peer
exchange
exchange
.
Interface
blockstore
blockstore
.
Blockstore
}
// session creates a test bitswap session.
//
// NB: It's easy make mistakes by providing the same peer ID to two different
// sessions. To safeguard, use the SessionGenerator to generate sessions. It's
// just a much better idea.
func
session
(
net
tn
.
Network
,
rs
mock
.
RoutingServer
,
id
peer
.
ID
)
instance
{
p
:=
peer
.
WithID
(
id
)
adapter
:=
net
.
Adapter
(
p
)
htc
:=
rs
.
Client
(
p
)
bstore
:=
blockstore
.
NewBlockstore
(
ds_sync
.
MutexWrap
(
ds
.
NewMapDatastore
()))
const
alwaysSendToPeer
=
true
ctx
:=
context
.
TODO
()
bs
:=
New
(
ctx
,
p
,
adapter
,
htc
,
bstore
,
alwaysSendToPeer
)
return
instance
{
peer
:
p
,
exchange
:
bs
,
blockstore
:
bstore
,
}
}
testutils.go
0 → 100644
View file @
f56f5506
package
bitswap
import
(
"code.google.com/p/go.net/context"
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
ds_sync
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
"github.com/jbenet/go-ipfs/blocks"
"github.com/jbenet/go-ipfs/blocks/blockstore"
"github.com/jbenet/go-ipfs/exchange"
tn
"github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
"github.com/jbenet/go-ipfs/peer"
"github.com/jbenet/go-ipfs/routing/mock"
)
/*
TODO: This whole file needs somewhere better to live.
The issue is that its very difficult to move it somewhere else
without creating circular dependencies.
Additional thought required.
*/
func
NewBlockGenerator
()
BlockGenerator
{
return
BlockGenerator
{}
}
type
BlockGenerator
struct
{
seq
int
}
func
(
bg
*
BlockGenerator
)
Next
()
*
blocks
.
Block
{
bg
.
seq
++
return
blocks
.
NewBlock
([]
byte
(
string
(
bg
.
seq
)))
}
func
(
bg
*
BlockGenerator
)
Blocks
(
n
int
)
[]
*
blocks
.
Block
{
blocks
:=
make
([]
*
blocks
.
Block
,
0
)
for
i
:=
0
;
i
<
n
;
i
++
{
b
:=
bg
.
Next
()
blocks
=
append
(
blocks
,
b
)
}
return
blocks
}
func
NewSessionGenerator
(
net
tn
.
Network
,
rs
mock
.
RoutingServer
)
SessionGenerator
{
return
SessionGenerator
{
net
:
net
,
rs
:
rs
,
seq
:
0
,
}
}
type
SessionGenerator
struct
{
seq
int
net
tn
.
Network
rs
mock
.
RoutingServer
}
func
(
g
*
SessionGenerator
)
Next
()
Instance
{
g
.
seq
++
return
session
(
g
.
net
,
g
.
rs
,
[]
byte
(
string
(
g
.
seq
)))
}
func
(
g
*
SessionGenerator
)
Instances
(
n
int
)
[]
Instance
{
instances
:=
make
([]
Instance
,
0
)
for
j
:=
0
;
j
<
n
;
j
++
{
inst
:=
g
.
Next
()
instances
=
append
(
instances
,
inst
)
}
return
instances
}
type
Instance
struct
{
Peer
peer
.
Peer
Exchange
exchange
.
Interface
Blockstore
blockstore
.
Blockstore
}
// session creates a test bitswap session.
//
// NB: It's easy make mistakes by providing the same peer ID to two different
// sessions. To safeguard, use the SessionGenerator to generate sessions. It's
// just a much better idea.
func
session
(
net
tn
.
Network
,
rs
mock
.
RoutingServer
,
id
peer
.
ID
)
Instance
{
p
:=
peer
.
WithID
(
id
)
adapter
:=
net
.
Adapter
(
p
)
htc
:=
rs
.
Client
(
p
)
bstore
:=
blockstore
.
NewBlockstore
(
ds_sync
.
MutexWrap
(
ds
.
NewMapDatastore
()))
const
alwaysSendToPeer
=
true
ctx
:=
context
.
TODO
()
bs
:=
New
(
ctx
,
p
,
adapter
,
htc
,
bstore
,
alwaysSendToPeer
)
return
Instance
{
Peer
:
p
,
Exchange
:
bs
,
Blockstore
:
bstore
,
}
}
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