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

import (
	"fmt"
	"sync"

	"github.com/ipfs/go-graphsync/metadata"
8
	logging "github.com/ipfs/go-log"
9 10 11 12 13 14 15 16

	"github.com/ipfs/go-block-format"
	"github.com/ipfs/go-graphsync/linktracker"
	gsmsg "github.com/ipfs/go-graphsync/message"
	"github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/linking/cid"
)

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 51 52 53 54 55 56 57 58 59
func (rc *ResponseCache) FinishRequest(requestID gsmsg.GraphSyncRequestID) {
	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 62 63 64
func (rc *ResponseCache) AttemptLoad(requestID gsmsg.GraphSyncRequestID, link ipld.Link) ([]byte, error) {
	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 74 75 76 77
func (rc *ResponseCache) ProcessResponse(responses map[gsmsg.GraphSyncRequestID]metadata.Metadata,
	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()
}