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
d2e4bad4
Commit
d2e4bad4
authored
Sep 19, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(bitswap:message) don't use proto internally
parent
c1873b89
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
24 deletions
+56
-24
message/message.go
message/message.go
+29
-23
message/message_test.go
message/message_test.go
+27
-1
No files found.
message/message.go
View file @
d2e4bad4
...
...
@@ -26,45 +26,45 @@ type Exportable interface {
// message wraps a proto message for convenience
type
message
struct
{
pb
PBMessage
}
func
newMessageFromProto
(
pb
PBMessage
)
*
message
{
return
&
message
{
pb
:
pb
}
wantlist
[]
u
.
Key
blocks
[]
blocks
.
Block
}
func
New
()
*
message
{
return
new
(
message
)
}
func
newMessageFromProto
(
pbm
PBMessage
)
(
BitSwapMessage
,
error
)
{
m
:=
New
()
for
_
,
s
:=
range
pbm
.
GetWantlist
()
{
m
.
AppendWanted
(
u
.
Key
(
s
))
}
for
_
,
d
:=
range
pbm
.
GetBlocks
()
{
b
,
err
:=
blocks
.
NewBlock
(
d
)
if
err
!=
nil
{
return
nil
,
err
}
m
.
AppendBlock
(
*
b
)
}
return
m
,
nil
}
// TODO(brian): convert these into keys
func
(
m
*
message
)
Wantlist
()
[]
u
.
Key
{
wl
:=
make
([]
u
.
Key
,
len
(
m
.
pb
.
Wantlist
))
for
_
,
str
:=
range
m
.
pb
.
Wantlist
{
wl
=
append
(
wl
,
u
.
Key
(
str
))
}
return
wl
return
m
.
wantlist
}
// TODO(brian): convert these into blocks
func
(
m
*
message
)
Blocks
()
[]
blocks
.
Block
{
bs
:=
make
([]
blocks
.
Block
,
len
(
m
.
pb
.
Blocks
))
for
_
,
data
:=
range
m
.
pb
.
Blocks
{
b
,
err
:=
blocks
.
NewBlock
(
data
)
if
err
!=
nil
{
continue
}
bs
=
append
(
bs
,
*
b
)
}
return
bs
return
m
.
blocks
}
func
(
m
*
message
)
AppendWanted
(
k
u
.
Key
)
{
m
.
pb
.
W
antlist
=
append
(
m
.
pb
.
W
antlist
,
string
(
k
)
)
m
.
w
antlist
=
append
(
m
.
w
antlist
,
k
)
}
func
(
m
*
message
)
AppendBlock
(
b
blocks
.
Block
)
{
m
.
pb
.
B
locks
=
append
(
m
.
pb
.
B
locks
,
b
.
Data
)
m
.
b
locks
=
append
(
m
.
b
locks
,
b
)
}
func
FromNet
(
nmsg
netmsg
.
NetMessage
)
(
BitSwapMessage
,
error
)
{
...
...
@@ -72,8 +72,14 @@ func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) {
}
func
(
m
*
message
)
ToProto
()
*
PBMessage
{
cp
:=
m
.
pb
return
&
cp
pb
:=
new
(
PBMessage
)
for
_
,
k
:=
range
m
.
Wantlist
()
{
pb
.
Wantlist
=
append
(
pb
.
Wantlist
,
string
(
k
))
}
for
_
,
b
:=
range
m
.
Blocks
()
{
pb
.
Blocks
=
append
(
pb
.
Blocks
,
b
.
Data
)
}
return
pb
}
func
(
m
*
message
)
ToNet
(
p
*
peer
.
Peer
)
(
nm
.
NetMessage
,
error
)
{
...
...
message/message_test.go
View file @
d2e4bad4
...
...
@@ -25,7 +25,10 @@ func TestNewMessageFromProto(t *testing.T) {
if
!
contains
(
protoMessage
.
Wantlist
,
str
)
{
t
.
Fail
()
}
m
:=
newMessageFromProto
(
*
protoMessage
)
m
,
err
:=
newMessageFromProto
(
*
protoMessage
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
contains
(
m
.
ToProto
()
.
GetWantlist
(),
str
)
{
t
.
Fail
()
}
...
...
@@ -52,6 +55,29 @@ func TestAppendBlock(t *testing.T) {
}
}
func
TestWantlist
(
t
*
testing
.
T
)
{
keystrs
:=
[]
string
{
"foo"
,
"bar"
,
"baz"
,
"bat"
}
m
:=
New
()
for
_
,
s
:=
range
keystrs
{
m
.
AppendWanted
(
u
.
Key
(
s
))
}
exported
:=
m
.
Wantlist
()
for
_
,
k
:=
range
exported
{
present
:=
false
for
_
,
s
:=
range
keystrs
{
if
s
==
string
(
k
)
{
present
=
true
}
}
if
!
present
{
t
.
Logf
(
"%v isn't in original list"
,
string
(
k
))
t
.
Fail
()
}
}
}
func
TestCopyProtoByValue
(
t
*
testing
.
T
)
{
const
str
=
"foo"
m
:=
New
()
...
...
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