testbridge.go 3.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package testbridge

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"

	cid "github.com/ipfs/go-cid"
	ipldbridge "github.com/ipfs/go-graphsync/ipldbridge"
	ipld "github.com/ipld/go-ipld-prime"
13 14 15
	"github.com/ipld/go-ipld-prime/encoding/dagjson"
	"github.com/ipld/go-ipld-prime/fluent"
	free "github.com/ipld/go-ipld-prime/impl/free"
16
	"github.com/ipld/go-ipld-prime/linking/cid"
17 18 19 20 21 22 23 24 25 26
	multihash "github.com/multiformats/go-multihash"
)

type mockIPLDBridge struct {
}

// NewMockIPLDBridge returns an IPLD bridge that works with MockSelectors
func NewMockIPLDBridge() ipldbridge.IPLDBridge {
	return &mockIPLDBridge{}
}
27 28 29 30 31 32 33 34 35 36 37
func (mb *mockIPLDBridge) BuildNode(buildFn func(ipldbridge.NodeBuilder) ipld.Node) (ipld.Node, error) {
	var node ipld.Node
	err := fluent.Recover(func() {
		nb := fluent.WrapNodeBuilder(free.NodeBuilder())
		node = buildFn(nb)
	})
	if err != nil {
		return nil, err
	}
	return node, nil
}
38 39

func (mb *mockIPLDBridge) ValidateSelectorSpec(cidRootedSelector ipld.Node) []error {
40 41
	spec, ok := cidRootedSelector.(*mockSelectorSpec)
	if !ok || spec.failValidation {
42 43 44 45 46 47 48
		return []error{fmt.Errorf("not a selector")}
	}
	return nil
}

func (mb *mockIPLDBridge) EncodeNode(node ipld.Node) ([]byte, error) {
	spec, ok := node.(*mockSelectorSpec)
49 50 51 52 53 54 55
	if ok {
		if !spec.failEncode {
			data, err := json.Marshal(spec.cidsVisited)
			if err != nil {
				return nil, err
			}
			return data, nil
56
		}
57 58 59 60 61 62
		return nil, fmt.Errorf("format not supported")
	}
	var buffer bytes.Buffer
	err := dagjson.Encoder(node, &buffer)
	if err != nil {
		return nil, err
63
	}
64
	return buffer.Bytes(), nil
65 66 67 68 69 70
}

func (mb *mockIPLDBridge) DecodeNode(data []byte) (ipld.Node, error) {
	var cidsVisited []cid.Cid
	err := json.Unmarshal(data, &cidsVisited)
	if err == nil {
71
		return &mockSelectorSpec{cidsVisited, false, false}, nil
72
	}
73 74
	reader := bytes.NewReader(data)
	return dagjson.Decoder(free.NodeBuilder(), reader)
75 76 77 78
}

func (mb *mockIPLDBridge) DecodeSelectorSpec(cidRootedSelector ipld.Node) (ipld.Node, ipldbridge.Selector, error) {
	spec, ok := cidRootedSelector.(*mockSelectorSpec)
79
	if !ok || spec.failValidation {
80 81 82 83 84
		return nil, nil, fmt.Errorf("not a selector")
	}
	return nil, newMockSelector(spec), nil
}

85
func (mb *mockIPLDBridge) Traverse(ctx context.Context, loader ipldbridge.Loader, root ipld.Node, s ipldbridge.Selector, fn ipldbridge.AdvVisitFn) error {
86 87 88 89 90 91
	ms, ok := s.(*mockSelector)
	if !ok {
		return fmt.Errorf("not supported")
	}
	var lastErr error
	for _, lnk := range ms.cidsVisited {
92 93

		node, err := loadNode(lnk, loader)
94 95 96 97 98
		if err != nil {
			lastErr = err
		} else {
			fn(ipldbridge.TraversalProgress{}, node, 0)
		}
99 100 101 102 103
		select {
		case <-ctx.Done():
			return lastErr
		default:
		}
104 105 106
	}
	return lastErr
}
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

func loadNode(lnk cid.Cid, loader ipldbridge.Loader) (ipld.Node, error) {
	r, err := loader(cidlink.Link{Cid: lnk}, ipldbridge.LinkContext{})
	if err != nil {
		return nil, err
	}
	var buffer bytes.Buffer
	io.Copy(&buffer, r)
	data := buffer.Bytes()
	hash, err := multihash.Sum(data, lnk.Prefix().MhType, lnk.Prefix().MhLength)
	if err != nil {
		return nil, err
	}
	if hash.B58String() != lnk.Hash().B58String() {
		return nil, fmt.Errorf("hash mismatch")
	}
	return NewMockBlockNode(data), nil
}