-
Notifications
You must be signed in to change notification settings - Fork 13
/
file_utils.go
84 lines (69 loc) · 2.75 KB
/
file_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package e2e
import (
"fmt"
"github.com/orbs-network/orbs-network-go/config"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/orbs-network/scribe/log"
"io/ioutil"
"os"
"path/filepath"
)
const blockStorageDataDirPrefix = "/tmp/orbs/e2e"
const CannedBlocksFileMinHeight = 500
func CopyFile(sourcePath string, targetPath string) error {
rawBlocks, err := ioutil.ReadFile(sourcePath)
if err != nil {
return err
}
err = ioutil.WriteFile(targetPath, rawBlocks, 0644)
if err != nil {
return err
}
return nil
}
func GetNodesDataDirs(virtualChainId primitives.VirtualChainId) ([]string, error) {
nodeFolders, err := ioutil.ReadDir(getVirtualChainDataDir(virtualChainId))
if err != nil {
return nil, err
}
var nodeDataDirs []string
for _, nodeFolder := range nodeFolders {
nodeDataDirs = append(nodeDataDirs, filepath.Join(getVirtualChainDataDir(virtualChainId), nodeFolder.Name(), "blocks"))
}
return nodeDataDirs, nil
}
func getVirtualChainDataDir(virtualChainId primitives.VirtualChainId) string {
return filepath.Join(blockStorageDataDirPrefix, vChainPathComponent(virtualChainId))
}
func getProcessorArtifactPath(virtualChainId primitives.VirtualChainId) (string, string) {
dir := filepath.Join(os.TempDir(), "orbs", "processorArtifacts", vChainPathComponent(virtualChainId))
return filepath.Join(dir, "processor-artifacts"), dir
}
func cleanNativeProcessorCache(virtualChainId primitives.VirtualChainId) {
_, dirToCleanup := getProcessorArtifactPath(virtualChainId)
_ = os.RemoveAll(dirToCleanup)
}
func cleanBlockStorage(virtualChainId primitives.VirtualChainId) {
_ = os.RemoveAll(getVirtualChainDataDir(virtualChainId))
}
func deployBlockStorageFiles(targetDir string, logger log.Logger) {
err := os.MkdirAll(targetDir, os.ModePerm)
if err != nil {
panic(fmt.Sprintf("could not create directory %s: %e", targetDir, err))
}
sourceBlocksFilePath := filepath.Join(config.GetCurrentSourceFileDirPath(), "_data", "blocks")
targetBlocksFilePath := filepath.Join(targetDir, "blocks")
logger.Info("copying blocks file", log.String("source", sourceBlocksFilePath), log.String("target", targetBlocksFilePath))
err = CopyFile(sourceBlocksFilePath, targetBlocksFilePath)
if err != nil {
panic(fmt.Sprintf("could not copy files %s -> %s", sourceBlocksFilePath, targetBlocksFilePath))
}
}
func vChainPathComponent(virtualChainId primitives.VirtualChainId) string {
return fmt.Sprintf("vcid_%d", virtualChainId)
}