diff --git a/cmd/nitro/init.go b/cmd/nitro/init.go index 9c23cdf852..3b97b45a3e 100644 --- a/cmd/nitro/init.go +++ b/cmd/nitro/init.go @@ -259,13 +259,18 @@ func setLatestSnapshotUrl(ctx context.Context, initConfig *conf.InitConfig, chai if err != nil { return fmt.Errorf("failed to parse latest mirror \"%s\": %w", initConfig.LatestBase, err) } - latestDateUrl := baseUrl.JoinPath(chain, "latest-"+initConfig.Latest+".txt").String() - latestDateBytes, err := httpGet(ctx, latestDateUrl) + latestFileUrl := baseUrl.JoinPath(chain, "latest-"+initConfig.Latest+".txt").String() + latestFileBytes, err := httpGet(ctx, latestFileUrl) if err != nil { - return fmt.Errorf("failed to get latest snapshot at \"%s\": %w", latestDateUrl, err) + return fmt.Errorf("failed to get latest file at \"%s\": %w", latestFileUrl, err) + } + latestFile := strings.TrimSpace(string(latestFileBytes)) + containsScheme := regexp.MustCompile("https?://") + if containsScheme.MatchString(latestFile) { + initConfig.Url = latestFile + } else { + initConfig.Url = baseUrl.JoinPath(latestFile).String() } - latestDate := strings.TrimSpace(string(latestDateBytes)) - initConfig.Url = baseUrl.JoinPath(chain, latestDate, initConfig.Latest+".tar").String() log.Info("Set latest snapshot url", "url", initConfig.Url) return nil } diff --git a/cmd/nitro/init_test.go b/cmd/nitro/init_test.go index 47ab4b4491..b0f5011ecd 100644 --- a/cmd/nitro/init_test.go +++ b/cmd/nitro/init_test.go @@ -123,35 +123,65 @@ func TestSetLatestSnapshotUrl(t *testing.T) { const ( chain = "arb1" snapshotKind = "archive" - latestDate = "2024/21" latestFile = "latest-" + snapshotKind + ".txt" dirPerm = 0700 filePerm = 0600 ) - // Create latest file - serverDir := t.TempDir() - err := os.Mkdir(filepath.Join(serverDir, chain), dirPerm) - Require(t, err) - err = os.WriteFile(filepath.Join(serverDir, chain, latestFile), []byte(latestDate), filePerm) - Require(t, err) - - // Start HTTP server - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - addr := "http://" + startFileServer(t, ctx, serverDir) + testCases := []struct { + name string + latestContents string + wantUrl func(string) string + }{ + { + name: "latest file with path", + latestContents: "/arb1/2024/21/archive.tar.gz", + wantUrl: func(serverAddr string) string { return serverAddr + "/arb1/2024/21/archive.tar.gz" }, + }, + { + name: "latest file with rootless path", + latestContents: "arb1/2024/21/archive.tar.gz", + wantUrl: func(serverAddr string) string { return serverAddr + "/arb1/2024/21/archive.tar.gz" }, + }, + { + name: "latest file with http url", + latestContents: "http://some.domain.com/arb1/2024/21/archive.tar.gz", + wantUrl: func(serverAddr string) string { return "http://some.domain.com/arb1/2024/21/archive.tar.gz" }, + }, + { + name: "latest file with https url", + latestContents: "https://some.domain.com/arb1/2024/21/archive.tar.gz", + wantUrl: func(serverAddr string) string { return "https://some.domain.com/arb1/2024/21/archive.tar.gz" }, + }, + } - // Set latest snapshot URL - initConfig := conf.InitConfigDefault - initConfig.Latest = snapshotKind - initConfig.LatestBase = addr - err = setLatestSnapshotUrl(ctx, &initConfig, chain) - Require(t, err) - - // Check url - want := fmt.Sprintf("%s/%s/%s/archive.tar", addr, chain, latestDate) - if initConfig.Url != want { - t.Errorf("initConfig.Url = %s; want: %s", initConfig.Url, want) + for _, testCase := range testCases { + t.Log("running test case", testCase.name) + + // Create latest file + serverDir := t.TempDir() + err := os.Mkdir(filepath.Join(serverDir, chain), dirPerm) + Require(t, err) + err = os.WriteFile(filepath.Join(serverDir, chain, latestFile), []byte(testCase.latestContents), filePerm) + Require(t, err) + + // Start HTTP server + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + addr := "http://" + startFileServer(t, ctx, serverDir) + + // Set latest snapshot URL + initConfig := conf.InitConfigDefault + initConfig.Latest = snapshotKind + initConfig.LatestBase = addr + err = setLatestSnapshotUrl(ctx, &initConfig, chain) + Require(t, err) + + // Check url + want := testCase.wantUrl(addr) + if initConfig.Url != want { + t.Fatalf("initConfig.Url = %s; want: %s", initConfig.Url, want) + } } }