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
3d12baae
Commit
3d12baae
authored
Oct 06, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Obviated need for `.ID.Pretty()` all over the place.
parent
e0a96157
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
15 deletions
+15
-15
bitswap.go
bitswap.go
+5
-5
bitswap_test.go
bitswap_test.go
+10
-10
No files found.
bitswap.go
View file @
3d12baae
...
...
@@ -61,7 +61,7 @@ type bitswap struct {
//
// TODO ensure only one active request per key
func
(
bs
*
bitswap
)
Block
(
parent
context
.
Context
,
k
u
.
Key
)
(
*
blocks
.
Block
,
error
)
{
u
.
DOut
(
"Get Block %v
\n
"
,
k
.
Pretty
()
)
u
.
DOut
(
"Get Block %v
\n
"
,
k
)
ctx
,
cancelFunc
:=
context
.
WithCancel
(
parent
)
bs
.
wantlist
.
Add
(
k
)
...
...
@@ -110,7 +110,7 @@ func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error)
// HasBlock announces the existance of a block to bitswap, potentially sending
// it to peers (Partners) whose WantLists include it.
func
(
bs
*
bitswap
)
HasBlock
(
ctx
context
.
Context
,
blk
blocks
.
Block
)
error
{
u
.
DOut
(
"Has Block %v
\n
"
,
blk
.
Key
()
.
Pretty
()
)
u
.
DOut
(
"Has Block %v
\n
"
,
blk
.
Key
())
bs
.
wantlist
.
Remove
(
blk
.
Key
())
bs
.
sendToPeersThatWant
(
ctx
,
blk
)
return
bs
.
routing
.
Provide
(
ctx
,
blk
.
Key
())
...
...
@@ -119,7 +119,7 @@ func (bs *bitswap) HasBlock(ctx context.Context, blk blocks.Block) error {
// TODO(brian): handle errors
func
(
bs
*
bitswap
)
ReceiveMessage
(
ctx
context
.
Context
,
p
*
peer
.
Peer
,
incoming
bsmsg
.
BitSwapMessage
)
(
*
peer
.
Peer
,
bsmsg
.
BitSwapMessage
)
{
u
.
DOut
(
"ReceiveMessage from %v
\n
"
,
p
.
Key
()
.
Pretty
()
)
u
.
DOut
(
"ReceiveMessage from %v
\n
"
,
p
.
Key
())
if
p
==
nil
{
// TODO propagate the error upward
...
...
@@ -173,10 +173,10 @@ func (bs *bitswap) send(ctx context.Context, p *peer.Peer, m bsmsg.BitSwapMessag
}
func
(
bs
*
bitswap
)
sendToPeersThatWant
(
ctx
context
.
Context
,
block
blocks
.
Block
)
{
u
.
DOut
(
"Sending %v to peers that want it
\n
"
,
block
.
Key
()
.
Pretty
()
)
u
.
DOut
(
"Sending %v to peers that want it
\n
"
,
block
.
Key
())
for
_
,
p
:=
range
bs
.
strategy
.
Peers
()
{
if
bs
.
strategy
.
BlockIsWantedByPeer
(
block
.
Key
(),
p
)
{
u
.
DOut
(
"%v wants %v
\n
"
,
p
.
Key
()
.
Pretty
()
,
block
.
Key
()
.
Pretty
()
)
u
.
DOut
(
"%v wants %v
\n
"
,
p
,
block
.
Key
())
if
bs
.
strategy
.
ShouldSendBlockToPeer
(
block
.
Key
(),
p
)
{
message
:=
bsmsg
.
New
()
message
.
AppendBlock
(
block
)
...
...
bitswap_test.go
View file @
3d12baae
...
...
@@ -160,49 +160,49 @@ func TestSendToWantingPeer(t *testing.T) {
w
:=
sg
.
Next
()
o
:=
sg
.
Next
()
t
.
Logf
(
"Session %v
\n
"
,
me
.
peer
.
Key
()
.
Pretty
()
)
t
.
Logf
(
"Session %v
\n
"
,
w
.
peer
.
Key
()
.
Pretty
()
)
t
.
Logf
(
"Session %v
\n
"
,
o
.
peer
.
Key
()
.
Pretty
()
)
t
.
Logf
(
"Session %v
\n
"
,
me
.
peer
)
t
.
Logf
(
"Session %v
\n
"
,
w
.
peer
)
t
.
Logf
(
"Session %v
\n
"
,
o
.
peer
)
alpha
:=
bg
.
Next
()
const
timeout
=
1
*
time
.
Millisecond
// FIXME don't depend on time
t
.
Logf
(
"Peer %v attempts to get %v. NB: not available
\n
"
,
w
.
peer
.
Key
()
.
Pretty
()
,
alpha
.
Key
()
.
Pretty
()
)
t
.
Logf
(
"Peer %v attempts to get %v. NB: not available
\n
"
,
w
.
peer
,
alpha
.
Key
())
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
_
,
err
:=
w
.
exchange
.
Block
(
ctx
,
alpha
.
Key
())
if
err
==
nil
{
t
.
Fatalf
(
"Expected %v to NOT be available"
,
alpha
.
Key
()
.
Pretty
()
)
t
.
Fatalf
(
"Expected %v to NOT be available"
,
alpha
.
Key
())
}
beta
:=
bg
.
Next
()
t
.
Logf
(
"Peer %v announes availability of %v
\n
"
,
w
.
peer
.
Key
()
.
Pretty
(),
beta
.
Key
()
.
Prett
y
())
t
.
Logf
(
"Peer %v announes availability of %v
\n
"
,
w
.
peer
,
beta
.
Ke
y
())
ctx
,
_
=
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
if
err
:=
w
.
blockstore
.
Put
(
beta
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
w
.
exchange
.
HasBlock
(
ctx
,
beta
)
t
.
Logf
(
"%v gets %v from %v and discovers it wants %v
\n
"
,
me
.
peer
.
Key
()
.
Pretty
(),
beta
.
Key
()
.
Pretty
(),
w
.
peer
.
Key
()
.
Pretty
()
,
alpha
.
Key
()
.
Pretty
()
)
t
.
Logf
(
"%v gets %v from %v and discovers it wants %v
\n
"
,
me
.
peer
,
beta
.
Key
(),
w
.
peer
,
alpha
.
Key
())
ctx
,
_
=
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
if
_
,
err
:=
me
.
exchange
.
Block
(
ctx
,
beta
.
Key
());
err
!=
nil
{
t
.
Fatal
(
err
)
}
t
.
Logf
(
"%v announces availability of %v
\n
"
,
o
.
peer
.
Key
()
.
Pretty
()
,
alpha
.
Key
()
.
Pretty
()
)
t
.
Logf
(
"%v announces availability of %v
\n
"
,
o
.
peer
,
alpha
.
Key
())
ctx
,
_
=
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
if
err
:=
o
.
blockstore
.
Put
(
alpha
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
o
.
exchange
.
HasBlock
(
ctx
,
alpha
)
t
.
Logf
(
"%v requests %v
\n
"
,
me
.
peer
.
Key
()
.
Pretty
()
,
alpha
.
Key
()
.
Pretty
()
)
t
.
Logf
(
"%v requests %v
\n
"
,
me
.
peer
,
alpha
.
Key
())
ctx
,
_
=
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
if
_
,
err
:=
me
.
exchange
.
Block
(
ctx
,
alpha
.
Key
());
err
!=
nil
{
t
.
Fatal
(
err
)
}
t
.
Logf
(
"%v should now have %v
\n
"
,
w
.
peer
.
Key
()
.
Pretty
()
,
alpha
.
Key
()
.
Pretty
()
)
t
.
Logf
(
"%v should now have %v
\n
"
,
w
.
peer
,
alpha
.
Key
())
block
,
err
:=
w
.
blockstore
.
Get
(
alpha
.
Key
())
if
err
!=
nil
{
t
.
Fatal
(
"Should not have received an error"
)
...
...
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