-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2345 from OffchainLabs/gligneul/split-db-snapshot
[NIT-2505] Download DB snapshot that is split between multiple files
- Loading branch information
Showing
3 changed files
with
260 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2021-2022, Offchain Labs, Inc. | ||
// For license information, see https://github.com/nitro/blob/master/LICENSE | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/offchainlabs/nitro/cmd/conf" | ||
"github.com/offchainlabs/nitro/util/testhelpers" | ||
) | ||
|
||
func TestDownloadInit(t *testing.T) { | ||
const ( | ||
archiveName = "random_data.tar.gz" | ||
dataSize = 1024 * 1024 | ||
filePerm = 0600 | ||
) | ||
|
||
// Create archive with random data | ||
serverDir := t.TempDir() | ||
data := testhelpers.RandomSlice(dataSize) | ||
checksumBytes := sha256.Sum256(data) | ||
checksum := hex.EncodeToString(checksumBytes[:]) | ||
|
||
// Write archive file | ||
archiveFile := fmt.Sprintf("%s/%s", serverDir, archiveName) | ||
err := os.WriteFile(archiveFile, data, filePerm) | ||
Require(t, err, "failed to write archive") | ||
|
||
// Write checksum file | ||
checksumFile := archiveFile + ".sha256" | ||
err = os.WriteFile(checksumFile, []byte(checksum), filePerm) | ||
Require(t, err, "failed to write checksum") | ||
|
||
// Start HTTP server | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
addr := startFileServer(t, ctx, serverDir) | ||
|
||
// Download file | ||
initConfig := conf.InitConfigDefault | ||
initConfig.Url = fmt.Sprintf("http://%s/%s", addr, archiveName) | ||
initConfig.DownloadPath = t.TempDir() | ||
receivedArchive, err := downloadInit(ctx, &initConfig) | ||
Require(t, err, "failed to download") | ||
|
||
// Check archive contents | ||
receivedData, err := os.ReadFile(receivedArchive) | ||
Require(t, err, "failed to read received archive") | ||
if !bytes.Equal(receivedData, data) { | ||
t.Error("downloaded archive is different from generated one") | ||
} | ||
} | ||
|
||
func TestDownloadInitInParts(t *testing.T) { | ||
const ( | ||
archiveName = "random_data.tar.gz" | ||
numParts = 3 | ||
partSize = 1024 * 1024 | ||
dataSize = numParts * partSize | ||
filePerm = 0600 | ||
) | ||
|
||
// Create parts with random data | ||
serverDir := t.TempDir() | ||
data := testhelpers.RandomSlice(dataSize) | ||
for i := 0; i < numParts; i++ { | ||
// Create part and checksum | ||
partData := data[partSize*i : partSize*(i+1)] | ||
checksumBytes := sha256.Sum256(partData) | ||
checksum := hex.EncodeToString(checksumBytes[:]) | ||
// Write part file | ||
partFile := fmt.Sprintf("%s/%s.part%d", serverDir, archiveName, i) | ||
err := os.WriteFile(partFile, partData, filePerm) | ||
Require(t, err, "failed to write part") | ||
// Write checksum file | ||
checksumFile := partFile + ".sha256" | ||
err = os.WriteFile(checksumFile, []byte(checksum), filePerm) | ||
Require(t, err, "failed to write checksum") | ||
} | ||
|
||
// Start HTTP server | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
addr := startFileServer(t, ctx, serverDir) | ||
|
||
// Download file | ||
initConfig := conf.InitConfigDefault | ||
initConfig.Url = fmt.Sprintf("http://%s/%s", addr, archiveName) | ||
initConfig.DownloadPath = t.TempDir() | ||
receivedArchive, err := downloadInit(ctx, &initConfig) | ||
Require(t, err, "failed to download") | ||
|
||
// check database contents | ||
receivedData, err := os.ReadFile(receivedArchive) | ||
Require(t, err, "failed to read received archive") | ||
if !bytes.Equal(receivedData, data) { | ||
t.Error("downloaded archive is different from generated one") | ||
} | ||
|
||
// Check if the function deleted the temporary files | ||
entries, err := os.ReadDir(initConfig.DownloadPath) | ||
Require(t, err, "failed to read temp dir") | ||
if len(entries) != 1 { | ||
t.Error("download function did not delete temp files") | ||
} | ||
} | ||
|
||
func startFileServer(t *testing.T, ctx context.Context, dir string) string { | ||
t.Helper() | ||
ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
Require(t, err, "failed to listen") | ||
addr := ln.Addr().String() | ||
server := &http.Server{ | ||
Addr: addr, | ||
Handler: http.FileServer(http.Dir(dir)), | ||
ReadHeaderTimeout: time.Second, | ||
} | ||
go func() { | ||
err := server.Serve(ln) | ||
if err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
t.Error("failed to shutdown server") | ||
} | ||
}() | ||
go func() { | ||
<-ctx.Done() | ||
err := server.Shutdown(ctx) | ||
Require(t, err, "failed to shutdown server") | ||
}() | ||
return addr | ||
} |