lotus/retrieval/verify.go

143 lines
3.3 KiB
Go
Raw Normal View History

2019-08-26 18:23:11 +00:00
package retrieval
2019-08-27 22:10:23 +00:00
import (
"context"
"io"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-unixfs"
pb "github.com/ipfs/go-unixfs/pb"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-lotus/build"
)
2019-08-26 18:23:11 +00:00
type BlockVerifier interface {
2019-08-27 22:10:23 +00:00
Verify(context.Context, blocks.Block, io.Writer) (internal bool, err error)
2019-08-26 18:23:11 +00:00
}
type OptimisticVerifier struct {
}
2019-08-27 22:10:23 +00:00
func (o *OptimisticVerifier) Verify(context.Context, blocks.Block, io.Writer) (bool, error) {
2019-08-26 18:23:11 +00:00
// It's probably fine
return false, nil
}
2019-08-27 22:10:23 +00:00
type UnixFs0Verifier struct {
Root cid.Cid
rootBlk blocks.Block
expect int
seen int
sub *UnixFs0Verifier
}
func (b *UnixFs0Verifier) verify(ctx context.Context, blk blocks.Block, out io.Writer) (last bool, internal bool, err error) {
if b.sub != nil {
subLast, internal, err := b.sub.verify(ctx, blk, out)
if err != nil {
return false, false, err
}
if subLast {
b.sub = nil
b.seen++
}
return b.seen == b.expect, internal, nil
}
if b.seen >= b.expect { // this is probably impossible
return false, false, xerrors.New("unixfs verifier: too many nodes in level")
}
links, err := b.checkInternal(blk, out)
if err != nil {
return false, false, err
}
if links > 0 { // TODO: check if all links are intermediate (or all aren't)
if links > build.UnixfsLinksPerLevel {
return false, false, xerrors.New("unixfs verifier: too many links in intermediate node")
}
if b.seen+1 == b.expect && links != build.UnixfsLinksPerLevel {
return false, false, xerrors.New("unixfs verifier: too few nodes in level")
}
b.sub = &UnixFs0Verifier{
Root: blk.Cid(),
rootBlk: blk,
expect: links,
}
// don't mark as seen yet
return false, true, nil
}
b.seen++
return b.seen == b.expect, false, nil
}
func (b *UnixFs0Verifier) checkInternal(blk blocks.Block, out io.Writer) (int, error) {
nd, err := ipld.Decode(blk)
if err != nil {
2019-08-28 21:11:29 +00:00
log.Warnf("IPLD Decode failed: %s", err)
2019-08-27 22:10:23 +00:00
return 0, err
}
// TODO: check size
2019-08-28 21:11:29 +00:00
switch nd := nd.(type) {
2019-08-27 22:10:23 +00:00
case *merkledag.ProtoNode:
2019-08-28 21:11:29 +00:00
fsn, err := unixfs.FSNodeFromBytes(nd.Data())
2019-08-27 22:10:23 +00:00
if err != nil {
2019-08-28 21:11:29 +00:00
log.Warnf("unixfs.FSNodeFromBytes failed: %s", err)
2019-08-27 22:10:23 +00:00
return 0, err
}
if fsn.Type() != pb.Data_File {
return 0, xerrors.New("internal nodes must be a file")
}
if len(fsn.Data()) > 0 {
return 0, xerrors.New("internal node with data")
}
if len(nd.Links()) == 0 {
return 0, xerrors.New("internal node with no links")
}
return len(nd.Links()), nil
case *merkledag.RawNode:
// TODO: do we check the hash before writing?
_, err := out.Write(nd.RawData())
return 0, err
default:
return 0, xerrors.New("verifier: unknown node type")
}
}
func (b *UnixFs0Verifier) Verify(ctx context.Context, blk blocks.Block, w io.Writer) (bool, error) {
// root is special
if b.rootBlk == nil {
if !b.Root.Equals(blk.Cid()) {
return false, xerrors.Errorf("unixfs verifier: root block CID didn't match: valid %s, got %s", b.Root, blk.Cid())
}
b.rootBlk = blk
links, err := b.checkInternal(blk, w)
if err != nil {
return false, err
}
b.expect = links
return links != 0, nil
}
_, internal, err := b.verify(ctx, blk, w)
return internal, err
}
2019-08-26 18:23:11 +00:00
var _ BlockVerifier = &OptimisticVerifier{}
2019-08-27 22:10:23 +00:00
var _ BlockVerifier = &UnixFs0Verifier{}