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

import (
	"fmt"
	"sync"

7
	"github.com/ipfs/go-graphsync"
8
	"github.com/ipfs/go-graphsync/metadata"
9
	logging "github.com/ipfs/go-log"
10

11
	blocks "github.com/ipfs/go-block-format"
12 13
	"github.com/ipfs/go-graphsync/linktracker"
	"github.com/ipld/go-ipld-prime"
14
	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
15 16
)

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

19 20 21 22
type responseCacheMessage interface {
	handle(rc *ResponseCache)
}

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

31 32
// ResponseCache maintains a store of unverified blocks and response
// data about links for loading, and prunes blocks as needed.
33 34 35 36 37 38 39
type ResponseCache struct {
	responseCacheLk sync.RWMutex

	linkTracker          *linktracker.LinkTracker
	unverifiedBlockStore UnverifiedBlockStore
}

40
// New initializes a new ResponseCache using the given unverified block store.
41 42 43 44 45 46 47
func New(unverifiedBlockStore UnverifiedBlockStore) *ResponseCache {
	return &ResponseCache{
		linkTracker:          linktracker.New(),
		unverifiedBlockStore: unverifiedBlockStore,
	}
}

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

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

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

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

	for _, block := range blks {
78
		log.Debugf("Received block from network: %s", block.Cid().String())
79 80 81 82 83
		rc.unverifiedBlockStore.AddUnverifiedBlock(cidlink.Link{Cid: block.Cid()}, block.RawData())
	}

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

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

	rc.responseCacheLk.Unlock()
}