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
1aaa88fa
Commit
1aaa88fa
authored
Sep 19, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(bitswap:message) implement FromNet
parent
27386c5c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
5 deletions
+85
-5
message/message.go
message/message.go
+11
-5
message/message_test.go
message/message_test.go
+74
-0
No files found.
message/message.go
View file @
1aaa88fa
package
message
import
(
"errors"
netmsg
"github.com/jbenet/go-ipfs/net/message"
proto
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
blocks
"github.com/jbenet/go-ipfs/blocks"
netmsg
"github.com/jbenet/go-ipfs/net/message"
nm
"github.com/jbenet/go-ipfs/net/message"
peer
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
...
...
@@ -68,7 +66,15 @@ func (m *message) AppendBlock(b blocks.Block) {
}
func
FromNet
(
nmsg
netmsg
.
NetMessage
)
(
BitSwapMessage
,
error
)
{
return
nil
,
errors
.
New
(
"TODO implement"
)
pb
:=
new
(
PBMessage
)
if
err
:=
proto
.
Unmarshal
(
nmsg
.
Data
(),
pb
);
err
!=
nil
{
return
nil
,
err
}
m
,
err
:=
newMessageFromProto
(
*
pb
)
if
err
!=
nil
{
return
nil
,
err
}
return
m
,
nil
}
func
(
m
*
message
)
ToProto
()
*
PBMessage
{
...
...
message/message_test.go
View file @
1aaa88fa
...
...
@@ -4,6 +4,7 @@ import (
"bytes"
"testing"
peer
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
testutil
"github.com/jbenet/go-ipfs/util/testutil"
)
...
...
@@ -88,6 +89,79 @@ func TestCopyProtoByValue(t *testing.T) {
}
}
func
TestToNetMethodSetsPeer
(
t
*
testing
.
T
)
{
m
:=
New
()
p
:=
&
peer
.
Peer
{
ID
:
[]
byte
(
"X"
)}
netmsg
,
err
:=
m
.
ToNet
(
p
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
(
netmsg
.
Peer
()
.
Key
()
==
p
.
Key
())
{
t
.
Fatal
(
"Peer key is different"
)
}
}
func
TestToNetFromNetPreservesWantList
(
t
*
testing
.
T
)
{
original
:=
New
()
original
.
AppendWanted
(
u
.
Key
(
"M"
))
original
.
AppendWanted
(
u
.
Key
(
"B"
))
original
.
AppendWanted
(
u
.
Key
(
"D"
))
original
.
AppendWanted
(
u
.
Key
(
"T"
))
original
.
AppendWanted
(
u
.
Key
(
"F"
))
netmsg
,
err
:=
original
.
ToNet
(
&
peer
.
Peer
{
ID
:
[]
byte
(
"X"
)})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
copied
,
err
:=
FromNet
(
netmsg
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
keys
:=
make
(
map
[
u
.
Key
]
bool
)
for
_
,
k
:=
range
copied
.
Wantlist
()
{
keys
[
k
]
=
true
}
for
_
,
k
:=
range
original
.
Wantlist
()
{
if
_
,
ok
:=
keys
[
k
];
!
ok
{
t
.
Fatalf
(
"Key Missing:
\"
%v
\"
"
,
k
)
}
}
}
func
TestToAndFromNetMessage
(
t
*
testing
.
T
)
{
original
:=
New
()
original
.
AppendBlock
(
testutil
.
NewBlockOrFail
(
t
,
"W"
))
original
.
AppendBlock
(
testutil
.
NewBlockOrFail
(
t
,
"E"
))
original
.
AppendBlock
(
testutil
.
NewBlockOrFail
(
t
,
"F"
))
original
.
AppendBlock
(
testutil
.
NewBlockOrFail
(
t
,
"M"
))
p
:=
&
peer
.
Peer
{
ID
:
[]
byte
(
"X"
)}
netmsg
,
err
:=
original
.
ToNet
(
p
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
m2
,
err
:=
FromNet
(
netmsg
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
keys
:=
make
(
map
[
u
.
Key
]
bool
)
for
_
,
b
:=
range
m2
.
Blocks
()
{
keys
[
b
.
Key
()]
=
true
}
for
_
,
b
:=
range
original
.
Blocks
()
{
if
_
,
ok
:=
keys
[
b
.
Key
()];
!
ok
{
t
.
Fail
()
}
}
}
func
contains
(
s
[]
string
,
x
string
)
bool
{
for
_
,
a
:=
range
s
{
if
a
==
x
{
...
...
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