2019-12-10 04:19:59 +00:00
|
|
|
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"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"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 {
|
2019-12-10 04:19:59 +00:00
|
|
|
Verify(context.Context, blocks.Block) (internal bool, err error)
|
2019-08-26 18:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type OptimisticVerifier struct {
|
|
|
|
}
|
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
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)
|
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
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:
|
2019-12-10 04:19:59 +00:00
|
|
|
return 0, nil
|
2019-08-27 22:10:23 +00:00
|
|
|
default:
|
|
|
|
return 0, xerrors.New("verifier: unknown node type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
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
|
2019-12-10 04:19:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
_, 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{}
|