Skip to content
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

catchpointdump: support p2p nodes #6266

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion catchup/catchpointService.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,12 +823,14 @@
for i := 0; i < cs.config.CatchupLedgerDownloadRetryAttempts; i++ {
psp, peerError := cs.blocksDownloadPeerSelector.getNextPeer()
if peerError != nil {
return err
cs.log.Debugf("checkLedgerDownload: error on getNextPeer: %s", peerError.Error())
return peerError

Check warning on line 827 in catchup/catchpointService.go

View check run for this annotation

Codecov / codecov/patch

catchup/catchpointService.go#L826-L827

Added lines #L826 - L827 were not covered by tests
}
err = ledgerFetcher.headLedger(context.Background(), psp.Peer, round)
if err == nil {
return nil
}
cs.log.Debugf("checkLedgerDownload: failed to headLedger from peer %s: %v", peerAddress(psp.Peer), err)

Check warning on line 833 in catchup/catchpointService.go

View check run for this annotation

Codecov / codecov/patch

catchup/catchpointService.go#L833

Added line #L833 was not covered by tests
// a non-nil error means that the catchpoint is not available, so we should rank it accordingly
cs.blocksDownloadPeerSelector.rankPeer(psp, peerRankNoCatchpointForRound)
}
Expand Down
29 changes: 23 additions & 6 deletions cmd/catchpointdump/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/network"
"github.com/algorand/go-algorand/network/p2p"
"github.com/algorand/go-algorand/network/p2p/peerstore"
"github.com/algorand/go-algorand/protocol"
tools "github.com/algorand/go-algorand/tools/network"
"github.com/algorand/go-algorand/util"
Expand Down Expand Up @@ -164,8 +166,8 @@
fmt.Printf(escapeCursorUp+escapeDeleteLine+outString+" %s\n", formatSize(dld))
}

func getRemoteDataStream(url string, hint string) (result io.ReadCloser, ctxCancel context.CancelFunc, err error) {
fmt.Printf("downloading %s from %s\n", hint, url)
func getRemoteDataStream(addr string, url string, client *http.Client, hint string) (result io.ReadCloser, ctxCancel context.CancelFunc, err error) {
fmt.Printf("downloading %s from %s %s\n", hint, addr, url)

Check warning on line 170 in cmd/catchpointdump/net.go

View check run for this annotation

Codecov / codecov/patch

cmd/catchpointdump/net.go#L169-L170

Added lines #L169 - L170 were not covered by tests
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return
Expand All @@ -174,7 +176,7 @@
timeoutContext, ctxCancel := context.WithTimeout(context.Background(), config.GetDefaultLocal().MaxCatchpointDownloadDuration)
request = request.WithContext(timeoutContext)
network.SetUserAgentHeader(request.Header)
response, err := http.DefaultClient.Do(request)
response, err := client.Do(request)

Check warning on line 179 in cmd/catchpointdump/net.go

View check run for this annotation

Codecov / codecov/patch

cmd/catchpointdump/net.go#L179

Added line #L179 was not covered by tests
if err != nil {
return
}
Expand Down Expand Up @@ -229,13 +231,28 @@
}
}

func buildURL(genesisID string, round int, resource string) string {
return fmt.Sprintf("/v1/%s/%s/%s", genesisID, resource, strconv.FormatUint(uint64(round), 36))

Check warning on line 235 in cmd/catchpointdump/net.go

View check run for this annotation

Codecov / codecov/patch

cmd/catchpointdump/net.go#L234-L235

Added lines #L234 - L235 were not covered by tests
}

// Downloads a catchpoint tar file and returns the path to the tar file.
func downloadCatchpoint(addr string, round int) (string, error) {
genesisID := strings.Split(networkName, ".")[0] + "-v1.0"
urlTemplate := "http://" + addr + "/v1/" + genesisID + "/%s/" + strconv.FormatUint(uint64(round), 36)
catchpointURL := fmt.Sprintf(urlTemplate, "ledger")

catchpointStream, catchpointCtxCancel, err := getRemoteDataStream(catchpointURL, "catchpoint")
// attempt to parse as p2p address first
var httpClient *http.Client
catchpointURL := buildURL(genesisID, round, "ledger")
if addrInfo, err := peerstore.PeerInfoFromAddr(addr); err == nil {
httpClient, err = p2p.MakeTestHTTPClient(addrInfo)
if err != nil {
return "", err

Check warning on line 248 in cmd/catchpointdump/net.go

View check run for this annotation

Codecov / codecov/patch

cmd/catchpointdump/net.go#L243-L248

Added lines #L243 - L248 were not covered by tests
}
} else {
httpClient = http.DefaultClient
catchpointURL = "http://" + addr + catchpointURL

Check warning on line 252 in cmd/catchpointdump/net.go

View check run for this annotation

Codecov / codecov/patch

cmd/catchpointdump/net.go#L250-L252

Added lines #L250 - L252 were not covered by tests
}

catchpointStream, catchpointCtxCancel, err := getRemoteDataStream(addr, catchpointURL, httpClient, "catchpoint")

Check warning on line 255 in cmd/catchpointdump/net.go

View check run for this annotation

Codecov / codecov/patch

cmd/catchpointdump/net.go#L255

Added line #L255 was not covered by tests
defer catchpointCtxCancel()
if err != nil {
return "", err
Expand Down