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-merkledag
Commits
fdf8dcc9
Commit
fdf8dcc9
authored
Nov 29, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
merkledag: respond with correct cid to Cid() method
License: MIT Signed-off-by:
Jeromy
<
why@ipfs.io
>
parent
e5a1a075
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
68 additions
and
14 deletions
+68
-14
coding.go
coding.go
+3
-7
merkledag.go
merkledag.go
+1
-1
merkledag_test.go
merkledag_test.go
+35
-0
node.go
node.go
+24
-4
test/utils.go
test/utils.go
+5
-2
No files found.
coding.go
View file @
fdf8dcc9
...
@@ -7,7 +7,6 @@ import (
...
@@ -7,7 +7,6 @@ import (
pb
"github.com/ipfs/go-ipfs/merkledag/pb"
pb
"github.com/ipfs/go-ipfs/merkledag/pb"
node
"gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
node
"gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
mh
"gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid
"gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
cid
"gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
)
)
...
@@ -84,13 +83,10 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
...
@@ -84,13 +83,10 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
}
}
if
n
.
cached
==
nil
{
if
n
.
cached
==
nil
{
if
n
.
prefix
.
MhType
==
0
{
// unset
if
n
.
Prefix
.
Codec
==
0
{
// unset
n
.
prefix
.
Codec
=
cid
.
DagProtobuf
n
.
Prefix
=
defaultCidPrefix
n
.
prefix
.
MhLength
=
-
1
n
.
prefix
.
MhType
=
mh
.
SHA2_256
n
.
prefix
.
Version
=
0
}
}
c
,
err
:=
n
.
p
refix
.
Sum
(
n
.
encoded
)
c
,
err
:=
n
.
P
refix
.
Sum
(
n
.
encoded
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
merkledag.go
View file @
fdf8dcc9
...
@@ -103,7 +103,7 @@ func decodeBlock(b blocks.Block) (node.Node, error) {
...
@@ -103,7 +103,7 @@ func decodeBlock(b blocks.Block) (node.Node, error) {
}
}
decnd
.
cached
=
b
.
Cid
()
decnd
.
cached
=
b
.
Cid
()
decnd
.
p
refix
=
b
.
Cid
()
.
Prefix
()
decnd
.
P
refix
=
b
.
Cid
()
.
Prefix
()
return
decnd
,
nil
return
decnd
,
nil
case
cid
.
Raw
:
case
cid
.
Raw
:
return
NewRawNode
(
b
.
RawData
()),
nil
return
NewRawNode
(
b
.
RawData
()),
nil
...
...
merkledag_test.go
View file @
fdf8dcc9
...
@@ -11,6 +11,7 @@ import (
...
@@ -11,6 +11,7 @@ import (
"sync"
"sync"
"testing"
"testing"
blocks
"github.com/ipfs/go-ipfs/blocks"
bserv
"github.com/ipfs/go-ipfs/blockservice"
bserv
"github.com/ipfs/go-ipfs/blockservice"
bstest
"github.com/ipfs/go-ipfs/blockservice/test"
bstest
"github.com/ipfs/go-ipfs/blockservice/test"
offline
"github.com/ipfs/go-ipfs/exchange/offline"
offline
"github.com/ipfs/go-ipfs/exchange/offline"
...
@@ -450,3 +451,37 @@ func TestProtoNodeResolve(t *testing.T) {
...
@@ -450,3 +451,37 @@ func TestProtoNodeResolve(t *testing.T) {
t
.
Fatal
(
"expected tree to return []{
\"
foo
\"
}"
)
t
.
Fatal
(
"expected tree to return []{
\"
foo
\"
}"
)
}
}
}
}
func
TestCidRetention
(
t
*
testing
.
T
)
{
nd
:=
new
(
ProtoNode
)
nd
.
SetData
([]
byte
(
"fooooo"
))
pref
:=
nd
.
Cid
()
.
Prefix
()
pref
.
Version
=
1
c2
,
err
:=
pref
.
Sum
(
nd
.
RawData
())
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
blk
,
err
:=
blocks
.
NewBlockWithCid
(
nd
.
RawData
(),
c2
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
bs
:=
dstest
.
Bserv
()
_
,
err
=
bs
.
AddBlock
(
blk
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
ds
:=
NewDAGService
(
bs
)
out
,
err
:=
ds
.
Get
(
context
.
Background
(),
c2
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
out
.
Cid
()
.
Equals
(
c2
)
{
t
.
Fatal
(
"output cid didnt match"
)
}
}
node.go
View file @
fdf8dcc9
...
@@ -23,8 +23,15 @@ type ProtoNode struct {
...
@@ -23,8 +23,15 @@ type ProtoNode struct {
cached
*
cid
.
Cid
cached
*
cid
.
Cid
// prefix specifies cid version and hashing function
// Prefix specifies cid version and hashing function
prefix
cid
.
Prefix
Prefix
cid
.
Prefix
}
var
defaultCidPrefix
=
cid
.
Prefix
{
Codec
:
cid
.
DagProtobuf
,
MhLength
:
-
1
,
MhType
:
mh
.
SHA2_256
,
Version
:
0
,
}
}
type
LinkSlice
[]
*
node
.
Link
type
LinkSlice
[]
*
node
.
Link
...
@@ -222,9 +229,22 @@ func (n *ProtoNode) Loggable() map[string]interface{} {
...
@@ -222,9 +229,22 @@ func (n *ProtoNode) Loggable() map[string]interface{} {
}
}
func
(
n
*
ProtoNode
)
Cid
()
*
cid
.
Cid
{
func
(
n
*
ProtoNode
)
Cid
()
*
cid
.
Cid
{
h
:=
n
.
Multihash
()
if
n
.
encoded
!=
nil
&&
n
.
cached
!=
nil
{
return
n
.
cached
}
if
n
.
Prefix
.
Codec
==
0
{
n
.
Prefix
=
defaultCidPrefix
}
c
,
err
:=
n
.
Prefix
.
Sum
(
n
.
RawData
())
if
err
!=
nil
{
// programmer error
panic
(
err
)
}
return
cid
.
NewCidV0
(
h
)
n
.
cached
=
c
return
c
}
}
func
(
n
*
ProtoNode
)
String
()
string
{
func
(
n
*
ProtoNode
)
String
()
string
{
...
...
test/utils.go
View file @
fdf8dcc9
...
@@ -10,7 +10,10 @@ import (
...
@@ -10,7 +10,10 @@ import (
)
)
func
Mock
()
dag
.
DAGService
{
func
Mock
()
dag
.
DAGService
{
return
dag
.
NewDAGService
(
Bserv
())
}
func
Bserv
()
bsrv
.
BlockService
{
bstore
:=
blockstore
.
NewBlockstore
(
dssync
.
MutexWrap
(
ds
.
NewMapDatastore
()))
bstore
:=
blockstore
.
NewBlockstore
(
dssync
.
MutexWrap
(
ds
.
NewMapDatastore
()))
bserv
:=
bsrv
.
New
(
bstore
,
offline
.
Exchange
(
bstore
))
return
bsrv
.
New
(
bstore
,
offline
.
Exchange
(
bstore
))
return
dag
.
NewDAGService
(
bserv
)
}
}
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