Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
06c567d1
提交
06c567d1
authored
7月 11, 2017
作者:
Jeromy Johnson
提交者:
GitHub
7月 11, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4060 from ipfs/feat/pluggable-ipld-decoding
Change IPFS to use the new pluggable Block to IPLD decoding framework.
上级
636a71eb
35984c2c
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
51 行增加
和
29 行删除
+51
-29
coding.go
merkledag/coding.go
+27
-0
merkledag.go
merkledag/merkledag.go
+12
-29
raw.go
merkledag/raw.go
+12
-0
没有找到文件。
merkledag/coding.go
浏览文件 @
06c567d1
...
...
@@ -3,6 +3,9 @@ package merkledag
import
(
"fmt"
"sort"
"strings"
"gx/ipfs/QmVA4mafxbfH5aEvNz8fyoxC6J1xhAtw88B4GerPznSZBg/go-block-format"
pb
"github.com/ipfs/go-ipfs/merkledag/pb"
...
...
@@ -108,3 +111,27 @@ func DecodeProtobuf(encoded []byte) (*ProtoNode, error) {
}
return
n
,
nil
}
// DecodeProtobufBlock is a block decoder for protobuf IPLD nodes conforming to
// node.DecodeBlockFunc
func
DecodeProtobufBlock
(
b
blocks
.
Block
)
(
node
.
Node
,
error
)
{
c
:=
b
.
Cid
()
if
c
.
Type
()
!=
cid
.
DagProtobuf
{
return
nil
,
fmt
.
Errorf
(
"this function can only decode protobuf nodes"
)
}
decnd
,
err
:=
DecodeProtobuf
(
b
.
RawData
())
if
err
!=
nil
{
if
strings
.
Contains
(
err
.
Error
(),
"Unmarshal failed"
)
{
return
nil
,
fmt
.
Errorf
(
"The block referred to by '%s' was not a valid merkledag node"
,
c
)
}
return
nil
,
fmt
.
Errorf
(
"Failed to decode Protocol Buffers: %v"
,
err
)
}
decnd
.
cached
=
c
decnd
.
Prefix
=
c
.
Prefix
()
return
decnd
,
nil
}
// Type assertion
var
_
node
.
DecodeBlockFunc
=
DecodeProtobufBlock
merkledag/merkledag.go
浏览文件 @
06c567d1
...
...
@@ -4,7 +4,6 @@ package merkledag
import
(
"context"
"fmt"
"strings"
"sync"
bserv
"github.com/ipfs/go-ipfs/blockservice"
...
...
@@ -16,6 +15,15 @@ import (
ipldcbor
"gx/ipfs/QmemYymP73eVdTUUMZEiSpiHeZQKNJdT5dP2iuHssZh1sR/go-ipld-cbor"
)
// TODO: We should move these registrations elsewhere. Really, most of the IPLD
// functionality should go in a `go-ipld` repo but that will take a lot of work
// and design.
func
init
()
{
node
.
Register
(
cid
.
DagProtobuf
,
DecodeProtobufBlock
)
node
.
Register
(
cid
.
Raw
,
DecodeRawBlock
)
node
.
Register
(
cid
.
DagCBOR
,
ipldcbor
.
DecodeBlock
)
}
var
ErrNotFound
=
fmt
.
Errorf
(
"merkledag: not found"
)
// DAGService is an IPFS Merkle DAG service.
...
...
@@ -94,32 +102,7 @@ func (n *dagService) Get(ctx context.Context, c *cid.Cid) (node.Node, error) {
return
nil
,
fmt
.
Errorf
(
"Failed to get block for %s: %v"
,
c
,
err
)
}
return
decodeBlock
(
b
)
}
func
decodeBlock
(
b
blocks
.
Block
)
(
node
.
Node
,
error
)
{
c
:=
b
.
Cid
()
switch
c
.
Type
()
{
case
cid
.
DagProtobuf
:
decnd
,
err
:=
DecodeProtobuf
(
b
.
RawData
())
if
err
!=
nil
{
if
strings
.
Contains
(
err
.
Error
(),
"Unmarshal failed"
)
{
return
nil
,
fmt
.
Errorf
(
"The block referred to by '%s' was not a valid merkledag node"
,
c
)
}
return
nil
,
fmt
.
Errorf
(
"Failed to decode Protocol Buffers: %v"
,
err
)
}
decnd
.
cached
=
b
.
Cid
()
decnd
.
Prefix
=
b
.
Cid
()
.
Prefix
()
return
decnd
,
nil
case
cid
.
Raw
:
return
NewRawNodeWPrefix
(
b
.
RawData
(),
b
.
Cid
()
.
Prefix
())
case
cid
.
DagCBOR
:
return
ipldcbor
.
Decode
(
b
.
RawData
())
default
:
return
nil
,
fmt
.
Errorf
(
"unrecognized object type: %s"
,
c
.
Type
())
}
return
node
.
Decode
(
b
)
}
// GetLinks return the links for the node, the node doesn't necessarily have
...
...
@@ -174,7 +157,7 @@ func (sg *sesGetter) Get(ctx context.Context, c *cid.Cid) (node.Node, error) {
return
nil
,
err
}
return
decodeBlock
(
blk
)
return
node
.
Decode
(
blk
)
}
// FetchGraph fetches all nodes that are children of the given node
...
...
@@ -235,7 +218,7 @@ func (ds *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *Node
return
}
nd
,
err
:=
decodeBlock
(
b
)
nd
,
err
:=
node
.
Decode
(
b
)
if
err
!=
nil
{
out
<-
&
NodeOption
{
Err
:
err
}
return
...
...
merkledag/raw.go
浏览文件 @
06c567d1
package
merkledag
import
(
"fmt"
"gx/ipfs/QmVA4mafxbfH5aEvNz8fyoxC6J1xhAtw88B4GerPznSZBg/go-block-format"
u
"gx/ipfs/QmSU6eubNdhXjFBJBSksTp8kv8YRub8mGAPv8tVJHmL2EU/go-ipfs-util"
...
...
@@ -22,6 +23,17 @@ func NewRawNode(data []byte) *RawNode {
return
&
RawNode
{
blk
}
}
// DecodeRawBlock is a block decoder for raw IPLD nodes conforming to `node.DecodeBlockFunc`.
func
DecodeRawBlock
(
block
blocks
.
Block
)
(
node
.
Node
,
error
)
{
if
block
.
Cid
()
.
Type
()
!=
cid
.
Raw
{
return
nil
,
fmt
.
Errorf
(
"raw nodes cannot be decoded from non-raw blocks: %d"
,
block
.
Cid
()
.
Type
())
}
// Once you "share" a block, it should be immutable. Therefore, we can just use this block as-is.
return
&
RawNode
{
block
},
nil
}
var
_
node
.
DecodeBlockFunc
=
DecodeRawBlock
// NewRawNodeWPrefix creates a RawNode with the hash function
// specified in prefix.
func
NewRawNodeWPrefix
(
data
[]
byte
,
prefix
cid
.
Prefix
)
(
*
RawNode
,
error
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论