Skip to content

Commit

Permalink
catchpointdump: support p2p nodes (#6266)
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy authored Mar 5, 2025
1 parent f468e57 commit d44aa2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
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 @@ func (cs *CatchpointCatchupService) checkLedgerDownload() error {
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
}
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)
// 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 @@ import (
"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 @@ func printDownloadProgressLine(progress int, barLength int, url string, dld int6
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)
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return
Expand All @@ -174,7 +176,7 @@ func getRemoteDataStream(url string, hint string) (result io.ReadCloser, ctxCanc
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)
if err != nil {
return
}
Expand Down Expand Up @@ -229,13 +231,28 @@ func doDownloadCatchpoint(url string, wdReader util.WatchdogStreamReader, out io
}
}

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

// 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
}
} else {
httpClient = http.DefaultClient
catchpointURL = "http://" + addr + catchpointURL
}

catchpointStream, catchpointCtxCancel, err := getRemoteDataStream(addr, catchpointURL, httpClient, "catchpoint")
defer catchpointCtxCancel()
if err != nil {
return "", err
Expand Down

0 comments on commit d44aa2d

Please sign in to comment.