fix(storageminer): lint/mod-tidy/extra checks

This commit is contained in:
hannahhoward 2021-12-10 12:49:53 -08:00
parent cddf63efe9
commit 177282d9f9

View File

@ -3,6 +3,7 @@ package impl
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -560,11 +561,18 @@ func (sm *StorageMinerAPI) MarketDataTransferUpdates(ctx context.Context) (<-cha
} }
func (sm *StorageMinerAPI) MarketDataTransferDiagnostics(ctx context.Context, mpid peer.ID) (*api.TransferDiagnostics, error) { func (sm *StorageMinerAPI) MarketDataTransferDiagnostics(ctx context.Context, mpid peer.ID) (*api.TransferDiagnostics, error) {
gsTransport, ok := sm.Transport.(*gst.Transport)
if !ok {
return nil, errors.New("api only works for graphsync as transport")
}
graphsyncConcrete, ok := sm.StagingGraphsync.(*gsimpl.GraphSync)
if !ok {
return nil, errors.New("api only works for non-mock graphsync implementation")
}
// gather information about active transport channels // gather information about active transport channels
transportChannels := sm.Transport.(*gst.Transport).ChannelsForPeer(mpid) transportChannels := gsTransport.ChannelsForPeer(mpid)
// gather information about graphsync state for peer // gather information about graphsync state for peer
gsPeerState := sm.StagingGraphsync.(*gsimpl.GraphSync).PeerState(mpid) gsPeerState := graphsyncConcrete.PeerState(mpid)
sendingTransfers := sm.generateTransfers(ctx, transportChannels.SendingChannels, gsPeerState.IncomingState) sendingTransfers := sm.generateTransfers(ctx, transportChannels.SendingChannels, gsPeerState.IncomingState)
receivingTransfers := sm.generateTransfers(ctx, transportChannels.ReceivingChannels, gsPeerState.OutgoingState) receivingTransfers := sm.generateTransfers(ctx, transportChannels.ReceivingChannels, gsPeerState.OutgoingState)
@ -598,10 +606,10 @@ func (sm *StorageMinerAPI) generateTransfers(ctx context.Context,
channelState = &cs channelState = &cs
} }
// add the current request for this channel // add the current request for this channel
tc.convertTransfer(&channelID, channelState, baseDiagnostics, channelRequests.Current, true) tc.convertTransfer(channelID, true, channelState, baseDiagnostics, channelRequests.Current, true)
for _, requestID := range channelRequests.Previous { for _, requestID := range channelRequests.Previous {
// add any previous requests that were cancelled for a restart // add any previous requests that were cancelled for a restart
tc.convertTransfer(&channelID, channelState, baseDiagnostics, requestID, false) tc.convertTransfer(channelID, true, channelState, baseDiagnostics, requestID, false)
} }
} }
@ -619,7 +627,7 @@ type transferConverter struct {
} }
// convert transfer assembles transfer and diagnostic data for a given graphsync/data-transfer request // convert transfer assembles transfer and diagnostic data for a given graphsync/data-transfer request
func (tc *transferConverter) convertTransfer(channelID *datatransfer.ChannelID, channelState *api.DataTransferChannel, baseDiagnostics []string, func (tc *transferConverter) convertTransfer(channelID datatransfer.ChannelID, hasChannelID bool, channelState *api.DataTransferChannel, baseDiagnostics []string,
requestID graphsync.RequestID, isCurrentChannelRequest bool) { requestID graphsync.RequestID, isCurrentChannelRequest bool) {
diagnostics := baseDiagnostics diagnostics := baseDiagnostics
state, hasState := tc.requestStates[requestID] state, hasState := tc.requestStates[requestID]
@ -627,19 +635,23 @@ func (tc *transferConverter) convertTransfer(channelID *datatransfer.ChannelID,
if !hasState { if !hasState {
stateString = "no graphsync state found" stateString = "no graphsync state found"
} }
if channelID == nil { var channelIDPtr *datatransfer.ChannelID
if hasChannelID {
diagnostics = append(diagnostics, fmt.Sprintf("No data transfer channel id for GraphSync request ID %d", requestID)) diagnostics = append(diagnostics, fmt.Sprintf("No data transfer channel id for GraphSync request ID %d", requestID))
} else if isCurrentChannelRequest && !hasState { } else {
channelIDPtr = &channelID
if isCurrentChannelRequest && !hasState {
diagnostics = append(diagnostics, fmt.Sprintf("No current request state for data transfer channel id %s", channelID)) diagnostics = append(diagnostics, fmt.Sprintf("No current request state for data transfer channel id %s", channelID))
} else if !isCurrentChannelRequest && hasState { } else if !isCurrentChannelRequest && hasState {
diagnostics = append(diagnostics, fmt.Sprintf("Graphsync request %d is a previous request on data transfer channel id %s that was restarted, but it is still running", requestID, channelID)) diagnostics = append(diagnostics, fmt.Sprintf("Graphsync request %d is a previous request on data transfer channel id %s that was restarted, but it is still running", requestID, channelID))
} }
}
diagnostics = append(diagnostics, tc.gsDiagnostics[requestID]...) diagnostics = append(diagnostics, tc.gsDiagnostics[requestID]...)
transfer := &api.GraphSyncDataTransfer{ transfer := &api.GraphSyncDataTransfer{
RequestID: requestID, RequestID: requestID,
RequestState: stateString, RequestState: stateString,
IsCurrentChannelRequest: isCurrentChannelRequest, IsCurrentChannelRequest: isCurrentChannelRequest,
ChannelID: channelID, ChannelID: channelIDPtr,
ChannelState: channelState, ChannelState: channelState,
Diagnostics: diagnostics, Diagnostics: diagnostics,
} }
@ -650,12 +662,12 @@ func (tc *transferConverter) convertTransfer(channelID *datatransfer.ChannelID,
func (tc *transferConverter) collectRemainingTransfers() { func (tc *transferConverter) collectRemainingTransfers() {
for requestID := range tc.requestStates { for requestID := range tc.requestStates {
if _, ok := tc.matchedRequests[requestID]; !ok { if _, ok := tc.matchedRequests[requestID]; !ok {
tc.convertTransfer(nil, nil, nil, requestID, false) tc.convertTransfer(datatransfer.ChannelID{}, false, nil, nil, requestID, false)
} }
} }
for requestID := range tc.gsDiagnostics { for requestID := range tc.gsDiagnostics {
if _, ok := tc.matchedRequests[requestID]; !ok { if _, ok := tc.matchedRequests[requestID]; !ok {
tc.convertTransfer(nil, nil, nil, requestID, false) tc.convertTransfer(datatransfer.ChannelID{}, false, nil, nil, requestID, false)
} }
} }
} }