-
Notifications
You must be signed in to change notification settings - Fork 524
Adding missing test for universalFetcher.go #2175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
52d0185
9a94f9a
762fa02
626e069
99be722
46af709
867e4d1
7d32e04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,21 +90,21 @@ func (uf *universalBlockFetcher) fetchBlock(ctx context.Context, round basics.Ro | |
| return block, cert, downloadDuration, err | ||
| } | ||
|
|
||
| func processBlockBytes(fetchedBuf []byte, r basics.Round, debugStr string) (blk *bookkeeping.Block, cert *agreement.Certificate, err error) { | ||
| func processBlockBytes(fetchedBuf []byte, r basics.Round, peerAddr string) (blk *bookkeeping.Block, cert *agreement.Certificate, err error) { | ||
| var decodedEntry rpcs.EncodedBlockCert | ||
| err = protocol.Decode(fetchedBuf, &decodedEntry) | ||
| if err != nil { | ||
| err = fmt.Errorf("fetchBlock(%d): cannot decode block from peer %v: %v", r, debugStr, err) | ||
| err = makeErrCannotDecodeBlock(r, peerAddr, err) | ||
|
||
| return | ||
| } | ||
|
|
||
| if decodedEntry.Block.Round() != r { | ||
| err = fmt.Errorf("fetchBlock(%d): got wrong block from peer %v: wanted %v, got %v", r, debugStr, r, decodedEntry.Block.Round()) | ||
| err = makeErrWrongBlockFromPeer(r, decodedEntry.Block.Round(), peerAddr) | ||
| return | ||
| } | ||
|
|
||
| if decodedEntry.Certificate.Round != r { | ||
| err = fmt.Errorf("fetchBlock(%d): got wrong cert from peer %v: wanted %v, got %v", r, debugStr, r, decodedEntry.Certificate.Round) | ||
| err = makeErrWrongCertFromPeer(r, decodedEntry.Certificate.Round, peerAddr) | ||
| return | ||
| } | ||
| return &decodedEntry.Block, &decodedEntry.Certificate, nil | ||
|
|
@@ -137,15 +137,15 @@ func (w *wsFetcherClient) getBlockBytes(ctx context.Context, r basics.Round) ([] | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(blockBytes) == 0 { | ||
| if len(blockBytes) == 0 { // This case may never happen | ||
| return nil, fmt.Errorf("wsFetcherClient(%d): empty response", r) | ||
| } | ||
| return blockBytes, nil | ||
| } | ||
|
|
||
| // Address implements FetcherClient | ||
| func (w *wsFetcherClient) address() string { | ||
| return fmt.Sprintf("[ws] (%v)", w.target.GetAddress()) | ||
| return fmt.Sprintf("[ws] (%s)", w.target.GetAddress()) | ||
| } | ||
|
|
||
| // requestBlock send a request for block <round> and wait until it receives a response or a context expires. | ||
|
|
@@ -161,20 +161,20 @@ func (w *wsFetcherClient) requestBlock(ctx context.Context, round basics.Round) | |
| } | ||
| resp, err := w.target.Request(ctx, protocol.UniEnsBlockReqTag, topics) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("wsFetcherClient(%s).requestBlock(%d): Request failed, %v", w.target.GetAddress(), round, err) | ||
| return nil, makeErrWsFetcherRequestFailed(round, w.target.GetAddress(), err.Error()) | ||
| } | ||
|
|
||
| if errMsg, found := resp.Topics.GetValue(network.ErrorKey); found { | ||
| return nil, fmt.Errorf("wsFetcherClient(%s).requestBlock(%d): Request failed, %s", w.target.GetAddress(), round, string(errMsg)) | ||
| return nil, makeErrWsFetcherRequestFailed(round, w.target.GetAddress(), string(errMsg)) | ||
| } | ||
|
|
||
| blk, found := resp.Topics.GetValue(rpcs.BlockDataKey) | ||
| if !found { | ||
| return nil, fmt.Errorf("wsFetcherClient(%s): request failed: block data not found", w.target.GetAddress()) | ||
| return nil, makeErrWsFetcherRequestFailed(round, w.target.GetAddress(), "Block data not found") | ||
| } | ||
| cert, found := resp.Topics.GetValue(rpcs.CertDataKey) | ||
| if !found { | ||
| return nil, fmt.Errorf("wsFetcherClient(%s): request failed: cert data not found", w.target.GetAddress()) | ||
| return nil, makeErrWsFetcherRequestFailed(round, w.target.GetAddress(), "Cert data not found") | ||
| } | ||
|
|
||
| blockCertBytes := protocol.EncodeReflect(rpcs.PreEncodedBlockCert{ | ||
|
|
@@ -236,9 +236,9 @@ func (hf *HTTPFetcher) getBlockBytes(ctx context.Context, r basics.Round) (data | |
| bodyBytes, err := rpcs.ResponseBytes(response, hf.log, fetcherMaxBlockBytes) | ||
| hf.log.Warnf("HTTPFetcher.getBlockBytes: response status code %d from '%s'. Response body '%s' ", response.StatusCode, blockURL, string(bodyBytes)) | ||
| if err == nil { | ||
| err = fmt.Errorf("getBlockBytes error response status code %d when requesting '%s'. Response body '%s'", response.StatusCode, blockURL, string(bodyBytes)) | ||
| err = makeErrHTTPResponse(response.StatusCode, blockURL, fmt.Sprintf("Response body '%s'", string(bodyBytes))) | ||
| } else { | ||
| err = fmt.Errorf("getBlockBytes error response status code %d when requesting '%s'. %w", response.StatusCode, blockURL, err) | ||
| err = makeErrHTTPResponse(response.StatusCode, blockURL, err.Error()) | ||
| } | ||
| return nil, err | ||
| } | ||
|
|
@@ -247,7 +247,7 @@ func (hf *HTTPFetcher) getBlockBytes(ctx context.Context, r basics.Round) (data | |
| // response content type is what we'd like it to be. | ||
| contentTypes := response.Header["Content-Type"] | ||
| if len(contentTypes) != 1 { | ||
| err = fmt.Errorf("http block fetcher invalid content type count %d", len(contentTypes)) | ||
| err = errHTTPResponseContentType{contentTypeCount: len(contentTypes)} | ||
| hf.log.Warn(err) | ||
| response.Body.Close() | ||
| return nil, err | ||
|
|
@@ -259,7 +259,7 @@ func (hf *HTTPFetcher) getBlockBytes(ctx context.Context, r basics.Round) (data | |
| if contentTypes[0] != rpcs.BlockResponseContentType && contentTypes[0] != blockResponseContentTypeOld { | ||
| hf.log.Warnf("http block fetcher response has an invalid content type : %s", contentTypes[0]) | ||
| response.Body.Close() | ||
| return nil, fmt.Errorf("http block fetcher invalid content type '%s'", contentTypes[0]) | ||
| return nil, errHTTPResponseContentType{contentTypeCount: 1, contentType: contentTypes[0]} | ||
| } | ||
|
|
||
| return rpcs.ResponseBytes(response, hf.log, fetcherMaxBlockBytes) | ||
|
|
@@ -270,3 +270,108 @@ func (hf *HTTPFetcher) getBlockBytes(ctx context.Context, r basics.Round) (data | |
| func (hf *HTTPFetcher) address() string { | ||
| return hf.rootURL | ||
| } | ||
|
|
||
| type errWrongCertFromPeer struct { | ||
| round basics.Round | ||
| peer string | ||
| certRound basics.Round | ||
| } | ||
|
|
||
| func makeErrWrongCertFromPeer(round, certRound basics.Round, peer string) errWrongCertFromPeer { | ||
| return errWrongCertFromPeer{ | ||
| round: round, | ||
| peer: peer, | ||
| certRound: certRound} | ||
| } | ||
|
||
|
|
||
| func (wcfpe errWrongCertFromPeer) Error() string { | ||
| return fmt.Sprintf("processBlockBytes: got wrong cert from peer %s: wanted %d, got %d", | ||
| wcfpe.peer, wcfpe.round, wcfpe.certRound) | ||
| } | ||
|
|
||
| type errWrongBlockFromPeer struct { | ||
| round basics.Round | ||
| peer string | ||
| certRound basics.Round | ||
| } | ||
|
|
||
| func makeErrWrongBlockFromPeer(round, certRound basics.Round, peer string) errWrongBlockFromPeer { | ||
| return errWrongBlockFromPeer{ | ||
| round: round, | ||
| peer: peer, | ||
| certRound: certRound} | ||
| } | ||
|
|
||
| func (wbfpe errWrongBlockFromPeer) Error() string { | ||
| return fmt.Sprintf("processBlockBytes: got wrong block from peer %s: wanted %d, got %d", | ||
| wbfpe.peer, wbfpe.round, wbfpe.certRound) | ||
| } | ||
|
|
||
| type errCannotDecodeBlock struct { | ||
| round basics.Round | ||
| peer string | ||
| err error | ||
| } | ||
|
|
||
| func makeErrCannotDecodeBlock(round basics.Round, peer string, err error) errCannotDecodeBlock { | ||
| return errCannotDecodeBlock{ | ||
| round: round, | ||
| peer: peer, | ||
| err: err} | ||
| } | ||
|
|
||
| func (cdbe errCannotDecodeBlock) Error() string { | ||
| return fmt.Sprintf("processBlockBytes: cannot decode block %d from peer %s: %s", | ||
| cdbe.round, cdbe.peer, cdbe.err.Error()) | ||
| } | ||
|
|
||
| func (cdbe errCannotDecodeBlock) Unwrap() error { | ||
| return cdbe.err | ||
| } | ||
|
|
||
| type errWsFetcherRequestFailed struct { | ||
| round basics.Round | ||
| peer string | ||
| cause string | ||
| } | ||
|
|
||
| func makeErrWsFetcherRequestFailed(round basics.Round, peer, cause string) errWsFetcherRequestFailed { | ||
| return errWsFetcherRequestFailed{ | ||
| round: round, | ||
| peer: peer, | ||
| cause: cause} | ||
| } | ||
|
|
||
| func (wrfe errWsFetcherRequestFailed)Error () string { | ||
| return fmt.Sprintf("wsFetcherClient(%s).requestBlock(%d): Request failed: %s", | ||
| wrfe.peer, wrfe.round, wrfe.cause) | ||
| } | ||
|
|
||
| type errHTTPResponse struct { | ||
| responseStatus int | ||
| blockURL string | ||
| cause string | ||
| } | ||
|
|
||
| func makeErrHTTPResponse(responseStatus int, blockURL string, cause string) errHTTPResponse { | ||
| return errHTTPResponse{ | ||
| responseStatus: responseStatus, | ||
| blockURL: blockURL, | ||
| cause: cause} | ||
| } | ||
|
|
||
| func (hre errHTTPResponse) Error() string { | ||
| return fmt.Sprintf("HTTPFetcher.getBlockBytes: error response status code %d when requesting '%s': %s", hre.responseStatus, hre.blockURL, hre.cause) | ||
| } | ||
|
|
||
| type errHTTPResponseContentType struct { | ||
| contentTypeCount int | ||
| contentType string | ||
| } | ||
|
|
||
| func (cte errHTTPResponseContentType) Error() string { | ||
| if cte.contentTypeCount == 1 { | ||
| return fmt.Sprintf("HTTPFetcher.getBlockBytes: invalid content type: %s", cte.contentType) | ||
| } | ||
| return fmt.Sprintf("HTTPFetcher.getBlockBytes: invalid content type count: %d", cte.contentTypeCount) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add an empty line