rename validatedResponse to private
This commit is contained in:
parent
0e4d5cb67b
commit
3f6c418dc6
@ -48,7 +48,7 @@ func NewClient(
|
|||||||
// Main logic of the client request service. The provided `Request`
|
// Main logic of the client request service. The provided `Request`
|
||||||
// is sent to the `singlePeer` if one is indicated or to all available
|
// is sent to the `singlePeer` if one is indicated or to all available
|
||||||
// ones otherwise. The response is processed and validated according
|
// ones otherwise. The response is processed and validated according
|
||||||
// to the `Request` options. Either a `ValidatedResponse` is returned
|
// to the `Request` options. Either a `validatedResponse` is returned
|
||||||
// (which can be safely accessed), or an `error` that may represent
|
// (which can be safely accessed), or an `error` that may represent
|
||||||
// either a response error status, a failed validation or an internal
|
// either a response error status, a failed validation or an internal
|
||||||
// error.
|
// error.
|
||||||
@ -66,7 +66,7 @@ func (client *BlockSync) doRequest(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
req *Request,
|
req *Request,
|
||||||
singlePeer *peer.ID,
|
singlePeer *peer.ID,
|
||||||
) (*ValidatedResponse, error) {
|
) (*validatedResponse, error) {
|
||||||
// Validate request.
|
// Validate request.
|
||||||
if req.Length == 0 {
|
if req.Length == 0 {
|
||||||
return nil, xerrors.Errorf("invalid request of length 0")
|
return nil, xerrors.Errorf("invalid request of length 0")
|
||||||
@ -138,7 +138,7 @@ func (client *BlockSync) doRequest(
|
|||||||
|
|
||||||
// Process and validate response. Check the status and that the information
|
// Process and validate response. Check the status and that the information
|
||||||
// returned matches the request (and its integrity). Extract the information
|
// returned matches the request (and its integrity). Extract the information
|
||||||
// into a `ValidatedResponse` for the external-facing APIs to select what they
|
// into a `validatedResponse` for the external-facing APIs to select what they
|
||||||
// want.
|
// want.
|
||||||
//
|
//
|
||||||
// We are conflating in the single error returned both status and validation
|
// We are conflating in the single error returned both status and validation
|
||||||
@ -148,7 +148,7 @@ func (client *BlockSync) processResponse(
|
|||||||
req *Request,
|
req *Request,
|
||||||
res *Response,
|
res *Response,
|
||||||
// FIXME: Add the `peer` as argument once we implement penalties.
|
// FIXME: Add the `peer` as argument once we implement penalties.
|
||||||
) (*ValidatedResponse, error) {
|
) (*validatedResponse, error) {
|
||||||
err := res.statusToError()
|
err := res.statusToError()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("status error: %s", err)
|
return nil, xerrors.Errorf("status error: %s", err)
|
||||||
@ -175,25 +175,25 @@ func (client *BlockSync) processResponse(
|
|||||||
return nil, xerrors.Errorf("got less than requested without a proper status: %s", res.Status)
|
return nil, xerrors.Errorf("got less than requested without a proper status: %s", res.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
validRes := &ValidatedResponse{}
|
validRes := &validatedResponse{}
|
||||||
if options.IncludeHeaders {
|
if options.IncludeHeaders {
|
||||||
// Check for valid block sets and extract them into `TipSet`s.
|
// Check for valid block sets and extract them into `TipSet`s.
|
||||||
validRes.Tipsets = make([]*types.TipSet, resLength)
|
validRes.tipsets = make([]*types.TipSet, resLength)
|
||||||
for i := 0; i < resLength; i++ {
|
for i := 0; i < resLength; i++ {
|
||||||
validRes.Tipsets[i], err = types.NewTipSet(res.Chain[i].Blocks)
|
validRes.tipsets[i], err = types.NewTipSet(res.Chain[i].Blocks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("invalid tipset blocks at height (head - %d): %w", i, err)
|
return nil, xerrors.Errorf("invalid tipset blocks at height (head - %d): %w", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the returned head matches the one requested.
|
// Check that the returned head matches the one requested.
|
||||||
if !types.CidArrsEqual(validRes.Tipsets[0].Cids(), req.Head) {
|
if !types.CidArrsEqual(validRes.tipsets[0].Cids(), req.Head) {
|
||||||
return nil, xerrors.Errorf("returned chain head does not match request")
|
return nil, xerrors.Errorf("returned chain head does not match request")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check `TipSet` are connected (valid chain).
|
// Check `TipSet` are connected (valid chain).
|
||||||
for i := 0; i < len(validRes.Tipsets) - 1; i++ {
|
for i := 0; i < len(validRes.tipsets) - 1; i++ {
|
||||||
if validRes.Tipsets[i].IsChildOf(validRes.Tipsets[i+1]) == false {
|
if validRes.tipsets[i].IsChildOf(validRes.tipsets[i+1]) == false {
|
||||||
return nil, fmt.Errorf("tipsets are not connected at height (head - %d)/(head - %d)",
|
return nil, fmt.Errorf("tipsets are not connected at height (head - %d)/(head - %d)",
|
||||||
i, i+1)
|
i, i+1)
|
||||||
// FIXME: Maybe give more information here, like CIDs.
|
// FIXME: Maybe give more information here, like CIDs.
|
||||||
@ -202,12 +202,12 @@ func (client *BlockSync) processResponse(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if options.IncludeMessages {
|
if options.IncludeMessages {
|
||||||
validRes.Messages = make([]*CompactedMessages, resLength)
|
validRes.messages = make([]*CompactedMessages, resLength)
|
||||||
for i := 0; i < resLength; i++ {
|
for i := 0; i < resLength; i++ {
|
||||||
if res.Chain[i].Messages == nil {
|
if res.Chain[i].Messages == nil {
|
||||||
return nil, xerrors.Errorf("no messages included for tipset at height (head - %d): %w", i)
|
return nil, xerrors.Errorf("no messages included for tipset at height (head - %d): %w", i)
|
||||||
}
|
}
|
||||||
validRes.Messages[i] = res.Chain[i].Messages
|
validRes.messages[i] = res.Chain[i].Messages
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.IncludeHeaders {
|
if options.IncludeHeaders {
|
||||||
@ -276,7 +276,7 @@ func (client *BlockSync) GetBlocks(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return validRes.Tipsets, nil
|
return validRes.tipsets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *BlockSync) GetFullTipSet(
|
func (client *BlockSync) GetFullTipSet(
|
||||||
@ -321,7 +321,7 @@ func (client *BlockSync) GetChainMessages(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return validRes.Messages, nil
|
return validRes.messages, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a request to a peer. Write request in the stream and read the
|
// Send a request to a peer. Write request in the stream and read the
|
||||||
|
@ -153,27 +153,28 @@ type CompactedMessages struct {
|
|||||||
|
|
||||||
// Response that has been validated according to the protocol
|
// Response that has been validated according to the protocol
|
||||||
// and can be safely accessed.
|
// and can be safely accessed.
|
||||||
// FIXME: Maybe rename to verified, keep consistent naming.
|
type validatedResponse struct {
|
||||||
type ValidatedResponse struct {
|
tipsets []*types.TipSet
|
||||||
Tipsets []*types.TipSet
|
// List of all messages per tipset (grouped by tipset,
|
||||||
Messages []*CompactedMessages
|
// not by block, hence a single index like `tipsets`).
|
||||||
|
messages []*CompactedMessages
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decompress messages and form full tipsets with them. The headers
|
// Decompress messages and form full tipsets with them. The headers
|
||||||
// need to have been requested as well.
|
// need to have been requested as well.
|
||||||
func (res *ValidatedResponse) toFullTipSets() ([]*store.FullTipSet) {
|
func (res *validatedResponse) toFullTipSets() ([]*store.FullTipSet) {
|
||||||
if len(res.Tipsets) == 0 {
|
if len(res.tipsets) == 0 {
|
||||||
// This decompression can only be done if both headers and
|
// This decompression can only be done if both headers and
|
||||||
// messages are returned in the response.
|
// messages are returned in the response.
|
||||||
// FIXME: Do we need to check the messages are present also? The validation
|
// FIXME: Do we need to check the messages are present also? The validation
|
||||||
// would seem to imply this is unnecessary, can be added just in case.
|
// would seem to imply this is unnecessary, can be added just in case.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ftsList := make([]*store.FullTipSet, len(res.Tipsets))
|
ftsList := make([]*store.FullTipSet, len(res.tipsets))
|
||||||
for tipsetIdx := range res.Tipsets {
|
for tipsetIdx := range res.tipsets {
|
||||||
fts := &store.FullTipSet{} // FIXME: We should use the `NewFullTipSet` API.
|
fts := &store.FullTipSet{} // FIXME: We should use the `NewFullTipSet` API.
|
||||||
msgs := res.Messages[tipsetIdx]
|
msgs := res.messages[tipsetIdx]
|
||||||
for blockIdx, b := range res.Tipsets[tipsetIdx].Blocks() {
|
for blockIdx, b := range res.tipsets[tipsetIdx].Blocks() {
|
||||||
fb := &types.FullBlock{
|
fb := &types.FullBlock{
|
||||||
Header: b,
|
Header: b,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user