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-block-format
Commits
09c41e5b
Commit
09c41e5b
authored
Oct 07, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cid: integrate cid into bitswap and blockstores
License: MIT Signed-off-by:
Jeromy
<
why@ipfs.io
>
parent
dd1aa730
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
25 deletions
+23
-25
blocks.go
blocks.go
+14
-19
blocks_test.go
blocks_test.go
+9
-6
No files found.
blocks.go
View file @
09c41e5b
...
...
@@ -6,8 +6,6 @@ import (
"errors"
"fmt"
key
"gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
mh
"gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid
"gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
u
"gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
...
...
@@ -18,37 +16,39 @@ var ErrWrongHash = errors.New("data did not match given hash!")
type
Block
interface
{
Multihash
()
mh
.
Multihash
RawData
()
[]
byte
Key
()
key
.
Key
Cid
()
*
cid
.
Cid
String
()
string
Loggable
()
map
[
string
]
interface
{}
}
// Block is a singular block of data in ipfs
type
BasicBlock
struct
{
multihash
mh
.
Multihash
data
[]
byte
cid
*
cid
.
Cid
data
[]
byte
}
// NewBlock creates a Block object from opaque data. It will hash the data.
func
NewBlock
(
data
[]
byte
)
*
BasicBlock
{
return
&
BasicBlock
{
data
:
data
,
multihash
:
u
.
Hash
(
data
)}
// TODO: fix assumptions
return
&
BasicBlock
{
data
:
data
,
cid
:
cid
.
NewCidV0
(
u
.
Hash
(
data
))}
}
// NewBlockWithHash creates a new block when the hash of the data
// is already known, this is used to save time in situations where
// we are able to be confident that the data is correct
func
NewBlockWith
Hash
(
data
[]
byte
,
h
mh
.
Multihash
)
(
*
BasicBlock
,
error
)
{
func
NewBlockWith
Cid
(
data
[]
byte
,
c
*
cid
.
Cid
)
(
*
BasicBlock
,
error
)
{
if
u
.
Debug
{
chk
:=
u
.
Hash
(
data
)
if
string
(
chk
)
!=
string
(
h
)
{
// TODO: fix assumptions
chkc
:=
cid
.
NewCidV0
(
u
.
Hash
(
data
))
if
!
chkc
.
Equals
(
c
)
{
return
nil
,
ErrWrongHash
}
}
return
&
BasicBlock
{
data
:
data
,
multihash
:
h
},
nil
return
&
BasicBlock
{
data
:
data
,
cid
:
c
},
nil
}
func
(
b
*
BasicBlock
)
Multihash
()
mh
.
Multihash
{
return
b
.
multih
ash
return
b
.
cid
.
H
ash
()
}
func
(
b
*
BasicBlock
)
RawData
()
[]
byte
{
...
...
@@ -56,20 +56,15 @@ func (b *BasicBlock) RawData() []byte {
}
func
(
b
*
BasicBlock
)
Cid
()
*
cid
.
Cid
{
return
cid
.
NewCidV0
(
b
.
multihash
)
}
// Key returns the block's Multihash as a Key value.
func
(
b
*
BasicBlock
)
Key
()
key
.
Key
{
return
key
.
Key
(
b
.
multihash
)
return
b
.
cid
}
func
(
b
*
BasicBlock
)
String
()
string
{
return
fmt
.
Sprintf
(
"[Block %s]"
,
b
.
Key
())
return
fmt
.
Sprintf
(
"[Block %s]"
,
b
.
Cid
())
}
func
(
b
*
BasicBlock
)
Loggable
()
map
[
string
]
interface
{}
{
return
map
[
string
]
interface
{}{
"block"
:
b
.
Key
()
.
String
(),
"block"
:
b
.
Cid
()
.
String
(),
}
}
blocks_test.go
View file @
09c41e5b
...
...
@@ -5,6 +5,7 @@ import (
"testing"
mh
"gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid
"gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
u
"gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
)
...
...
@@ -44,12 +45,12 @@ func TestHash(t *testing.T) {
}
}
func
Test
Key
(
t
*
testing
.
T
)
{
func
Test
Cid
(
t
*
testing
.
T
)
{
data
:=
[]
byte
(
"yet another data"
)
block
:=
NewBlock
(
data
)
key
:=
block
.
Key
()
c
:=
block
.
Cid
()
if
!
bytes
.
Equal
(
block
.
Multihash
(),
key
.
ToMultih
ash
())
{
if
!
bytes
.
Equal
(
block
.
Multihash
(),
c
.
H
ash
())
{
t
.
Error
(
"key contains wrong data"
)
}
}
...
...
@@ -66,8 +67,10 @@ func TestManualHash(t *testing.T) {
t
.
Fatal
(
err
)
}
c
:=
cid
.
NewCidV0
(
hash
)
u
.
Debug
=
false
block
,
err
:=
NewBlockWith
Hash
(
data
,
hash
)
block
,
err
:=
NewBlockWith
Cid
(
data
,
c
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -77,7 +80,7 @@ func TestManualHash(t *testing.T) {
}
data
[
5
]
=
byte
((
uint32
(
data
[
5
])
+
5
)
%
256
)
// Transfrom hash to be different
block
,
err
=
NewBlockWith
Hash
(
data
,
hash
)
block
,
err
=
NewBlockWith
Cid
(
data
,
c
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -88,7 +91,7 @@ func TestManualHash(t *testing.T) {
u
.
Debug
=
true
block
,
err
=
NewBlockWith
Hash
(
data
,
hash
)
block
,
err
=
NewBlockWith
Cid
(
data
,
c
)
if
err
!=
ErrWrongHash
{
t
.
Fatal
(
err
)
}
...
...
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