Commit 35b1c6b1 authored by hannahhoward's avatar hannahhoward

fix(responsecache): improve error message

Improve Error message on missing blocks, and also add missing comments
parent 7050142c
...@@ -17,12 +17,16 @@ type responseCacheMessage interface { ...@@ -17,12 +17,16 @@ type responseCacheMessage interface {
handle(rc *ResponseCache) handle(rc *ResponseCache)
} }
// UnverifiedBlockStore is an interface for storing blocks
// as they come in and removing them as they are verified
type UnverifiedBlockStore interface { type UnverifiedBlockStore interface {
PruneBlocks(func(ipld.Link) bool) PruneBlocks(func(ipld.Link) bool)
VerifyBlock(ipld.Link) ([]byte, error) VerifyBlock(ipld.Link) ([]byte, error)
AddUnverifiedBlock(ipld.Link, []byte) AddUnverifiedBlock(ipld.Link, []byte)
} }
// ResponseCache maintains a store of unverified blocks and response
// data about links for loading, and prunes blocks as needed.
type ResponseCache struct { type ResponseCache struct {
responseCacheLk sync.RWMutex responseCacheLk sync.RWMutex
...@@ -30,6 +34,7 @@ type ResponseCache struct { ...@@ -30,6 +34,7 @@ type ResponseCache struct {
unverifiedBlockStore UnverifiedBlockStore unverifiedBlockStore UnverifiedBlockStore
} }
// New initializes a new ResponseCache using the given unverified block store.
func New(unverifiedBlockStore UnverifiedBlockStore) *ResponseCache { func New(unverifiedBlockStore UnverifiedBlockStore) *ResponseCache {
return &ResponseCache{ return &ResponseCache{
linkTracker: linktracker.New(), linkTracker: linktracker.New(),
...@@ -37,6 +42,8 @@ func New(unverifiedBlockStore UnverifiedBlockStore) *ResponseCache { ...@@ -37,6 +42,8 @@ func New(unverifiedBlockStore UnverifiedBlockStore) *ResponseCache {
} }
} }
// FinishRequest indicate there is no more need to track blocks tied to this
// response
func (rc *ResponseCache) FinishRequest(requestID gsmsg.GraphSyncRequestID) { func (rc *ResponseCache) FinishRequest(requestID gsmsg.GraphSyncRequestID) {
rc.responseCacheLk.Lock() rc.responseCacheLk.Lock()
rc.linkTracker.FinishRequest(requestID) rc.linkTracker.FinishRequest(requestID)
...@@ -47,16 +54,19 @@ func (rc *ResponseCache) FinishRequest(requestID gsmsg.GraphSyncRequestID) { ...@@ -47,16 +54,19 @@ func (rc *ResponseCache) FinishRequest(requestID gsmsg.GraphSyncRequestID) {
rc.responseCacheLk.Unlock() rc.responseCacheLk.Unlock()
} }
// AttemptLoad attempts to laod the given block from the cache
func (rc *ResponseCache) AttemptLoad(requestID gsmsg.GraphSyncRequestID, link ipld.Link) ([]byte, error) { func (rc *ResponseCache) AttemptLoad(requestID gsmsg.GraphSyncRequestID, link ipld.Link) ([]byte, error) {
rc.responseCacheLk.Lock() rc.responseCacheLk.Lock()
defer rc.responseCacheLk.Unlock() defer rc.responseCacheLk.Unlock()
if rc.linkTracker.IsKnownMissingLink(requestID, link) { if rc.linkTracker.IsKnownMissingLink(requestID, link) {
return nil, fmt.Errorf("Remote Peer Is Missing Block") return nil, fmt.Errorf("Remote Peer Is Missing Block: %s", link.String())
} }
data, _ := rc.unverifiedBlockStore.VerifyBlock(link) data, _ := rc.unverifiedBlockStore.VerifyBlock(link)
return data, nil return data, nil
} }
// ProcessResponse processes incoming response data, adding unverified blocks,
// and tracking link metadata from a remote peer
func (rc *ResponseCache) ProcessResponse(responses map[gsmsg.GraphSyncRequestID]metadata.Metadata, func (rc *ResponseCache) ProcessResponse(responses map[gsmsg.GraphSyncRequestID]metadata.Metadata,
blks []blocks.Block) { blks []blocks.Block) {
rc.responseCacheLk.Lock() rc.responseCacheLk.Lock()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment