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
92cc3f1e
Commit
92cc3f1e
authored
May 05, 2016
by
Kevin Atkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make blocks.Block an interface.
License: MIT Signed-off-by:
Kevin Atkinson
<
k@kevina.org
>
parent
3eec2656
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
11 deletions
+27
-11
blocks.go
blocks.go
+27
-11
No files found.
blocks.go
View file @
92cc3f1e
...
...
@@ -11,40 +11,56 @@ import (
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)
type
Block
interface
{
Multihash
()
mh
.
Multihash
Data
()
[]
byte
Key
()
key
.
Key
String
()
string
Loggable
()
map
[
string
]
interface
{}
}
// Block is a singular block of data in ipfs
type
Block
struct
{
M
ultihash
mh
.
Multihash
D
ata
[]
byte
type
Raw
Block
struct
{
m
ultihash
mh
.
Multihash
d
ata
[]
byte
}
// NewBlock creates a Block object from opaque data. It will hash the data.
func
NewBlock
(
data
[]
byte
)
*
Block
{
return
&
Block
{
D
ata
:
data
,
M
ultihash
:
u
.
Hash
(
data
)}
func
NewBlock
(
data
[]
byte
)
*
Raw
Block
{
return
&
Raw
Block
{
d
ata
:
data
,
m
ultihash
:
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
NewBlockWithHash
(
data
[]
byte
,
h
mh
.
Multihash
)
(
*
Block
,
error
)
{
func
NewBlockWithHash
(
data
[]
byte
,
h
mh
.
Multihash
)
(
*
Raw
Block
,
error
)
{
if
u
.
Debug
{
chk
:=
u
.
Hash
(
data
)
if
string
(
chk
)
!=
string
(
h
)
{
return
nil
,
errors
.
New
(
"Data did not match given hash!"
)
}
}
return
&
Block
{
Data
:
data
,
Multihash
:
h
},
nil
return
&
RawBlock
{
data
:
data
,
multihash
:
h
},
nil
}
func
(
b
*
RawBlock
)
Multihash
()
mh
.
Multihash
{
return
b
.
multihash
}
func
(
b
*
RawBlock
)
Data
()
[]
byte
{
return
b
.
data
}
// Key returns the block's Multihash as a Key value.
func
(
b
*
Block
)
Key
()
key
.
Key
{
return
key
.
Key
(
b
.
M
ultihash
)
func
(
b
*
Raw
Block
)
Key
()
key
.
Key
{
return
key
.
Key
(
b
.
m
ultihash
)
}
func
(
b
*
Block
)
String
()
string
{
func
(
b
*
Raw
Block
)
String
()
string
{
return
fmt
.
Sprintf
(
"[Block %s]"
,
b
.
Key
())
}
func
(
b
*
Block
)
Loggable
()
map
[
string
]
interface
{}
{
func
(
b
*
Raw
Block
)
Loggable
()
map
[
string
]
interface
{}
{
return
map
[
string
]
interface
{}{
"block"
:
b
.
Key
()
.
String
(),
}
...
...
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