lotus/retrieval/impl/verify.go

142 lines
3.2 KiB
Go
Raw Normal View History

package retrievalimpl
2019-08-26 18:23:11 +00:00
2019-08-27 22:10:23 +00:00
import (
"context"
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/lotus/build"
2019-08-27 22:10:23 +00:00
)
2019-08-26 18:23:11 +00:00
type BlockVerifier interface {
Verify(context.Context, blocks.Block) (internal bool, err error)
2019-08-26 18:23:11 +00:00
}
type OptimisticVerifier struct {
}
func (o *OptimisticVerifier) Verify(context.Context, blocks.Block) (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) (last bool, internal bool, err error) {
2019-08-27 22:10:23 +00:00
if b.sub != nil {
2019-08-28 23:01:28 +00:00
// TODO: check links here (iff b.sub.sub == nil)
subLast, internal, err := b.sub.verify(ctx, blk)
2019-08-27 22:10:23 +00:00
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)
2019-08-27 22:10:23 +00:00
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) (int, error) {
2019-08-27 22:10:23 +00:00
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:
return 0, nil
2019-08-27 22:10:23 +00:00
default:
return 0, xerrors.New("verifier: unknown node type")
}
}
func (b *UnixFs0Verifier) Verify(ctx context.Context, blk blocks.Block) (bool, error) {
2019-08-27 22:10:23 +00:00
// 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)
2019-08-27 22:10:23 +00:00
if err != nil {
return false, err
}
b.expect = links
return links != 0, nil
}
_, internal, err := b.verify(ctx, blk)
2019-08-27 22:10:23 +00:00
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{}