responsecache.go 2.85 KB
Newer Older
1 2 3 4 5 6
package responsecache

import (
	"fmt"
	"sync"

7
	blocks "github.com/ipfs/go-block-format"
Hannah Howard's avatar
Hannah Howard committed
8
	logging "github.com/ipfs/go-log"
9
	"github.com/ipld/go-ipld-prime"
10
	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Hannah Howard's avatar
Hannah Howard committed
11 12 13 14

	"github.com/ipfs/go-graphsync"
	"github.com/ipfs/go-graphsync/linktracker"
	"github.com/ipfs/go-graphsync/metadata"
15 16
)

17 18
var log = logging.Logger("graphsync")

19 20
// UnverifiedBlockStore is an interface for storing blocks
// as they come in and removing them as they are verified
21 22 23 24 25 26
type UnverifiedBlockStore interface {
	PruneBlocks(func(ipld.Link) bool)
	VerifyBlock(ipld.Link) ([]byte, error)
	AddUnverifiedBlock(ipld.Link, []byte)
}

27 28
// ResponseCache maintains a store of unverified blocks and response
// data about links for loading, and prunes blocks as needed.
29 30 31 32 33 34 35
type ResponseCache struct {
	responseCacheLk sync.RWMutex

	linkTracker          *linktracker.LinkTracker
	unverifiedBlockStore UnverifiedBlockStore
}

36
// New initializes a new ResponseCache using the given unverified block store.
37 38 39 40 41 42 43
func New(unverifiedBlockStore UnverifiedBlockStore) *ResponseCache {
	return &ResponseCache{
		linkTracker:          linktracker.New(),
		unverifiedBlockStore: unverifiedBlockStore,
	}
}

44 45
// FinishRequest indicate there is no more need to track blocks tied to this
// response
46
func (rc *ResponseCache) FinishRequest(requestID graphsync.RequestID) {
47 48 49 50 51 52 53 54 55
	rc.responseCacheLk.Lock()
	rc.linkTracker.FinishRequest(requestID)

	rc.unverifiedBlockStore.PruneBlocks(func(link ipld.Link) bool {
		return rc.linkTracker.BlockRefCount(link) == 0
	})
	rc.responseCacheLk.Unlock()
}

56
// AttemptLoad attempts to laod the given block from the cache
57
func (rc *ResponseCache) AttemptLoad(requestID graphsync.RequestID, link ipld.Link) ([]byte, error) {
58 59 60
	rc.responseCacheLk.Lock()
	defer rc.responseCacheLk.Unlock()
	if rc.linkTracker.IsKnownMissingLink(requestID, link) {
61
		return nil, fmt.Errorf("Remote Peer Is Missing Block: %s", link.String())
62 63 64 65 66
	}
	data, _ := rc.unverifiedBlockStore.VerifyBlock(link)
	return data, nil
}

67 68
// ProcessResponse processes incoming response data, adding unverified blocks,
// and tracking link metadata from a remote peer
69
func (rc *ResponseCache) ProcessResponse(responses map[graphsync.RequestID]metadata.Metadata,
70 71 72 73
	blks []blocks.Block) {
	rc.responseCacheLk.Lock()

	for _, block := range blks {
74
		log.Debugf("Received block from network: %s", block.Cid().String())
75 76 77 78 79
		rc.unverifiedBlockStore.AddUnverifiedBlock(cidlink.Link{Cid: block.Cid()}, block.RawData())
	}

	for requestID, md := range responses {
		for _, item := range md {
80
			log.Debugf("Traverse link %s on request ID %d", item.Link.String(), requestID)
81
			rc.linkTracker.RecordLinkTraversal(requestID, cidlink.Link{Cid: item.Link}, item.BlockPresent)
82 83 84 85 86 87 88 89 90 91
		}
	}

	// prune unused blocks right away
	rc.unverifiedBlockStore.PruneBlocks(func(link ipld.Link) bool {
		return rc.linkTracker.BlockRefCount(link) == 0
	})

	rc.responseCacheLk.Unlock()
}