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
301a925a
Commit
301a925a
authored
Jul 04, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gofmt
parent
dcdb1e32
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
65 deletions
+61
-65
coding.go
coding.go
+42
-43
merkledag.go
merkledag.go
+19
-22
No files found.
coding.go
View file @
301a925a
package
merkledag
import
(
"fmt"
mh
"github.com/jbenet/go-multihash"
"fmt"
mh
"github.com/jbenet/go-multihash"
)
// for now, we use a PBNode intermediate thing.
// because native go objects are nice.
func
(
n
*
Node
)
Unmarshal
(
encoded
[]
byte
)
error
{
var
pbn
PBNode
if
err
:=
pbn
.
Unmarshal
(
encoded
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Unmarshal failed. %v"
,
err
)
}
pbnl
:=
pbn
.
GetLinks
()
n
.
Links
=
make
([]
*
Link
,
len
(
pbnl
))
for
i
,
l
:=
range
(
pbnl
)
{
n
.
Links
[
i
]
=
&
Link
{
Name
:
l
.
GetName
(),
Size
:
l
.
GetTsize
()}
h
,
err
:=
mh
.
Cast
(
l
.
GetHash
())
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Link hash is not valid multihash. %v"
,
err
)
}
n
.
Links
[
i
]
.
Hash
=
h
}
n
.
Data
=
pbn
.
GetData
()
return
nil
var
pbn
PBNode
if
err
:=
pbn
.
Unmarshal
(
encoded
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Unmarshal failed. %v"
,
err
)
}
pbnl
:=
pbn
.
GetLinks
()
n
.
Links
=
make
([]
*
Link
,
len
(
pbnl
))
for
i
,
l
:=
range
pbnl
{
n
.
Links
[
i
]
=
&
Link
{
Name
:
l
.
GetName
(),
Size
:
l
.
GetTsize
()}
h
,
err
:=
mh
.
Cast
(
l
.
GetHash
())
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Link hash is not valid multihash. %v"
,
err
)
}
n
.
Links
[
i
]
.
Hash
=
h
}
n
.
Data
=
pbn
.
GetData
()
return
nil
}
func
(
n
*
Node
)
MarshalTo
(
encoded
[]
byte
)
error
{
pbn
:=
n
.
getPBNode
()
if
_
,
err
:=
pbn
.
MarshalTo
(
encoded
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Marshal failed. %v"
,
err
)
}
return
nil
pbn
:=
n
.
getPBNode
()
if
_
,
err
:=
pbn
.
MarshalTo
(
encoded
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Marshal failed. %v"
,
err
)
}
return
nil
}
func
(
n
*
Node
)
Marshal
()
([]
byte
,
error
)
{
pbn
:=
n
.
getPBNode
()
data
,
err
:=
pbn
.
Marshal
()
if
err
!=
nil
{
return
data
,
fmt
.
Errorf
(
"Marshal failed. %v"
,
err
)
}
return
data
,
nil
pbn
:=
n
.
getPBNode
()
data
,
err
:=
pbn
.
Marshal
()
if
err
!=
nil
{
return
data
,
fmt
.
Errorf
(
"Marshal failed. %v"
,
err
)
}
return
data
,
nil
}
func
(
n
*
Node
)
getPBNode
()
*
PBNode
{
pbn
:=
&
PBNode
{}
pbn
.
Links
=
make
([]
*
PBLink
,
len
(
n
.
Links
))
for
i
,
l
:=
range
(
n
.
Links
)
{
pbn
.
Links
[
i
]
=
&
PBLink
{}
pbn
.
Links
[
i
]
.
Name
=
&
l
.
Name
pbn
.
Links
[
i
]
.
Tsize
=
&
l
.
Size
pbn
.
Links
[
i
]
.
Hash
=
[]
byte
(
l
.
Hash
)
}
pbn
.
Data
=
n
.
Data
return
pbn
pbn
:=
&
PBNode
{}
pbn
.
Links
=
make
([]
*
PBLink
,
len
(
n
.
Links
))
for
i
,
l
:=
range
n
.
Links
{
pbn
.
Links
[
i
]
=
&
PBLink
{}
pbn
.
Links
[
i
]
.
Name
=
&
l
.
Name
pbn
.
Links
[
i
]
.
Tsize
=
&
l
.
Size
pbn
.
Links
[
i
]
.
Hash
=
[]
byte
(
l
.
Hash
)
}
pbn
.
Data
=
n
.
Data
return
pbn
}
merkledag.go
View file @
301a925a
package
merkledag
import
(
mh
"github.com/jbenet/go-multihash"
mh
"github.com/jbenet/go-multihash"
)
// A node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
type
Node
struct
{
Links
[]
*
Link
Data
[]
byte
Links
[]
*
Link
Data
[]
byte
}
// An IPFS Merkle DAG Link
type
Link
struct
{
// utf string name. should be unique per object
Name
string
// utf8
// utf string name. should be unique per object
Name
string
// utf8
// cumulative size of target object
Size
uint64
// cumulative size of target object
Size
uint64
// multihash of the target object
Hash
mh
.
Multihash
// multihash of the target object
Hash
mh
.
Multihash
}
type
EncodedNode
[]
byte
func
(
n
*
Node
)
Size
()
(
uint64
,
error
)
{
d
,
err
:=
n
.
Marshal
()
if
err
!=
nil
{
return
0
,
err
}
s
:=
uint64
(
len
(
d
))
for
_
,
l
:=
range
(
n
.
Links
)
{
s
+=
l
.
Size
}
return
s
,
nil
d
,
err
:=
n
.
Marshal
()
if
err
!=
nil
{
return
0
,
err
}
s
:=
uint64
(
len
(
d
))
for
_
,
l
:=
range
n
.
Links
{
s
+=
l
.
Size
}
return
s
,
nil
}
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