diff --git a/Dockerfile b/Dockerfile index 3e630171..a2add713 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20.5 as builder +FROM golang:1.20.7 as builder WORKDIR /multiversx diff --git a/cmd/chainsimulator/config/config.toml b/cmd/chainsimulator/config/config.toml index e36c7b2b..b2d25933 100644 --- a/cmd/chainsimulator/config/config.toml +++ b/cmd/chainsimulator/config/config.toml @@ -13,4 +13,4 @@ log-file-life-span-in-mb = 1024 # 1GB log-file-life-span-in-sec = 432000 # 5 days log-file-prefix = "chain-simulator" - logs-path = "logs" \ No newline at end of file + logs-path = "logs" diff --git a/cmd/chainsimulator/main.go b/cmd/chainsimulator/main.go index 55781fb4..483da014 100644 --- a/cmd/chainsimulator/main.go +++ b/cmd/chainsimulator/main.go @@ -20,6 +20,7 @@ import ( "github.com/multiversx/mx-chain-simulator-go/pkg/facade" endpoints "github.com/multiversx/mx-chain-simulator-go/pkg/proxy/api" "github.com/multiversx/mx-chain-simulator-go/pkg/proxy/configs" + "github.com/multiversx/mx-chain-simulator-go/pkg/proxy/configs/git" "github.com/multiversx/mx-chain-simulator-go/pkg/proxy/creator" "github.com/urfave/cli" ) @@ -88,7 +89,8 @@ func startChainSimulator(ctx *cli.Context) error { return fmt.Errorf("%w while initializing the logger", err) } - configsFetcher, err := configs.NewConfigsFetcher(cfg.Config.Simulator.MxChainRepo, cfg.Config.Simulator.MxProxyRepo) + gitFetcher := git.NewGitFetcher() + configsFetcher, err := configs.NewConfigsFetcher(cfg.Config.Simulator.MxChainRepo, cfg.Config.Simulator.MxProxyRepo, gitFetcher) if err != nil { return err } @@ -132,7 +134,6 @@ func startChainSimulator(ctx *cli.Context) error { PathToProxyConfig: proxyConfigs, ServerPort: cfg.Config.Simulator.ServerPort, RestApiInterfaces: restApiInterfaces, - AddressConverter: metaNode.GetCoreComponents().AddressPubKeyConverter(), InitialWallets: simulator.GetInitialWalletKeys().ShardWallets, }) if err != nil { diff --git a/go.mod b/go.mod index fcfc95b2..616bee6f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/gin-gonic/gin v1.9.1 github.com/multiversx/mx-chain-core-go v1.2.18 - github.com/multiversx/mx-chain-go v1.6.4-0.20231115094151-91e9d6c7bc10 + github.com/multiversx/mx-chain-go v1.6.4-0.20231115131018-5d3fbefc1db3 github.com/multiversx/mx-chain-logger-go v1.0.13 github.com/multiversx/mx-chain-proxy-go v1.1.41 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 51f86e52..3ff76617 100644 --- a/go.sum +++ b/go.sum @@ -398,8 +398,8 @@ github.com/multiversx/mx-chain-crypto-go v1.2.9 h1:OEfF2kOQrtzUl273Z3DEcshjlTVUf github.com/multiversx/mx-chain-crypto-go v1.2.9/go.mod h1:fkaWKp1rbQN9wPKya5jeoRyC+c/SyN/NfggreyeBw+8= github.com/multiversx/mx-chain-es-indexer-go v1.4.13 h1:3Ayaw9bSpeNOF+Z3L/11MN1rIJH8Rc6dqtt+o4Wfdno= github.com/multiversx/mx-chain-es-indexer-go v1.4.13/go.mod h1:g0REyU8rqJfoBq6mIfqEi6IdpLofECLEvKKGMJO8ZhM= -github.com/multiversx/mx-chain-go v1.6.4-0.20231115094151-91e9d6c7bc10 h1:mIEOk7UxF81y6U2iL29STG/qG1vGo02gJIz8FaG6i2Q= -github.com/multiversx/mx-chain-go v1.6.4-0.20231115094151-91e9d6c7bc10/go.mod h1:1Vt+2H+Sx0Ig4oElq703pEGHs4cF8Cg59CG6PDtaxig= +github.com/multiversx/mx-chain-go v1.6.4-0.20231115131018-5d3fbefc1db3 h1:u/xo0lRnm7sPobTAR01OvracjiJxON0vyZhExcwurTc= +github.com/multiversx/mx-chain-go v1.6.4-0.20231115131018-5d3fbefc1db3/go.mod h1:1Vt+2H+Sx0Ig4oElq703pEGHs4cF8Cg59CG6PDtaxig= github.com/multiversx/mx-chain-logger-go v1.0.13 h1:eru/TETo0MkO4ZTnXsQDKf4PBRpAXmqjT02klNT/JnY= github.com/multiversx/mx-chain-logger-go v1.0.13/go.mod h1:MZJhTAtZTJxT+yK2EHc4ZW3YOHUc1UdjCD0iahRNBZk= github.com/multiversx/mx-chain-proxy-go v1.1.41 h1:u5LTek2keNvd25jrOmHLp0N4AFwYFImBnrs+GR7IGRY= diff --git a/pkg/proxy/configs/configs.go b/pkg/proxy/configs/configs.go index 4bf20287..287af124 100644 --- a/pkg/proxy/configs/configs.go +++ b/pkg/proxy/configs/configs.go @@ -1,7 +1,6 @@ package configs import ( - "encoding/hex" "encoding/pem" "os" "os/exec" @@ -24,7 +23,6 @@ type ArgsProxyConfigs struct { ServerPort int RestApiInterfaces map[uint32]string InitialWallets map[uint32]*dtos.WalletKey - AddressConverter core.PubkeyConverter } // ArgsOutputConfig holds the output arguments for proxy configs @@ -62,7 +60,7 @@ func CreateProxyConfigs(args ArgsProxyConfigs) (*ArgsOutputConfig, error) { } pemFile := path.Join(newConfigsPath, "walletKey.pem") - err = generatePemFromInitialAddress(pemFile, args.InitialWallets, args.AddressConverter) + err = generatePemFromInitialAddress(pemFile, args.InitialWallets) if err != nil { return nil, err } @@ -74,22 +72,16 @@ func CreateProxyConfigs(args ArgsProxyConfigs) (*ArgsOutputConfig, error) { }, nil } -func generatePemFromInitialAddress(fileName string, initialAddresses map[uint32]*dtos.WalletKey, addressConverter core.PubkeyConverter) error { - file, errO := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, core.FileModeReadWrite) - if errO != nil { - return errO +func generatePemFromInitialAddress(fileName string, initialAddresses map[uint32]*dtos.WalletKey) error { + file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, core.FileModeReadWrite) + if err != nil { + return err } for _, wallet := range initialAddresses { - addressBytes, err := addressConverter.Decode(wallet.Address) - if err != nil { - return err - } - - skBytes := append([]byte(wallet.PrivateKeyHex), []byte(hex.EncodeToString(addressBytes))...) blk := pem.Block{ Type: "PRIVATE KEY for " + wallet.Address, - Bytes: skBytes, + Bytes: []byte(wallet.PrivateKeyHex), } err = pem.Encode(file, &blk) diff --git a/pkg/proxy/configs/configsFetcher.go b/pkg/proxy/configs/configsFetcher.go index 7deb7f70..f7e2af5f 100644 --- a/pkg/proxy/configs/configsFetcher.go +++ b/pkg/proxy/configs/configsFetcher.go @@ -1,12 +1,8 @@ package configs import ( - "fmt" - "io" "os" - "os/exec" "path" - "path/filepath" "runtime/debug" "strings" @@ -21,14 +17,16 @@ const ( ) type fetcher struct { + gitFetcher GitHandler mxChainNodeRepo string mxChainProxy string } -func NewConfigsFetcher(mxChainNodeRepo, mxChainProxy string) (*fetcher, error) { +func NewConfigsFetcher(mxChainNodeRepo, mxChainProxy string, git GitHandler) (*fetcher, error) { return &fetcher{ mxChainNodeRepo: mxChainNodeRepo, mxChainProxy: mxChainProxy, + gitFetcher: git, }, nil } @@ -66,12 +64,12 @@ func (f *fetcher) FetchNodeConfigs(info *debug.BuildInfo, pathWhereToPutConfigs func (f *fetcher) fetchConfigFolder(repo string, version string, pathWhereToSaveConfig string, app string) error { pathToRepo := path.Join(os.TempDir(), "repo") - err := cloneRepository(repo, pathToRepo) + err := f.gitFetcher.Clone(repo, pathToRepo) if err != nil { return err } - err = checkoutCommit(pathToRepo, version) + err = f.gitFetcher.Checkout(pathToRepo, version) if err != nil { return err } @@ -103,69 +101,10 @@ func extractVersionOrCommit(versionStr string) string { return versionStr } -func cloneRepository(repoURL, destDir string) error { - cmd := exec.Command("git", "clone", repoURL, destDir) - res, err := cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("%s-%s", string(res), err.Error()) - } - - return nil -} - -func checkoutCommit(repoDir, commitHash string) error { - cmd := exec.Command("git", "checkout", commitHash) - cmd.Dir = repoDir - - res, err := cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("%s-%s", string(res), err.Error()) - } - - return nil -} - -func copyFolderWithAllFiles(src, dst string) error { - return filepath.Walk(src, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - relPath, err := filepath.Rel(src, path) - if err != nil { - return err - } - - if info.IsDir() { - return os.MkdirAll(filepath.Join(dst, relPath), info.Mode()) - } - - in, err := os.Open(path) - if err != nil { - return err - } - defer in.Close() - - out, err := os.Create(filepath.Join(dst, relPath)) - if err != nil { - return err - } - defer out.Close() - - _, err = io.Copy(out, in) - if err != nil { - return err - } - - return out.Close() - }) -} - func folderExists(folderPath string) (bool, error) { _, err := os.Stat(folderPath) if os.IsNotExist(err) { return false, nil - } else if err != nil { - return false, err } return true, nil diff --git a/pkg/proxy/configs/configsFetcher_test.go b/pkg/proxy/configs/configsFetcher_test.go index 493043ff..eb482fde 100644 --- a/pkg/proxy/configs/configsFetcher_test.go +++ b/pkg/proxy/configs/configsFetcher_test.go @@ -1,10 +1,10 @@ package configs import ( - "os" "runtime/debug" "testing" + "github.com/multiversx/mx-chain-simulator-go/testscommon" "github.com/stretchr/testify/require" ) @@ -14,12 +14,12 @@ const ( ) func TestConfigsFetcher(t *testing.T) { - cf, _ := NewConfigsFetcher(mxNodeRepo, mxProxyRepo) - debug.ReadBuildInfo() - - defer func() { - _ = os.RemoveAll("./test") - }() + dir := t.TempDir() + cf, _ := NewConfigsFetcher(mxNodeRepo, mxProxyRepo, &testscommon.GitFetcherStub{ + CloneCalled: func(r, d string) error { + return nil + }, + }) err := cf.FetchProxyConfigs(&debug.BuildInfo{ Deps: []*debug.Module{ @@ -32,7 +32,7 @@ func TestConfigsFetcher(t *testing.T) { Version: "v1.1.41", }, }, - }, "./test") + }, dir) require.Nil(t, err) } diff --git a/pkg/proxy/configs/copy.go b/pkg/proxy/configs/copy.go new file mode 100644 index 00000000..f0307a7e --- /dev/null +++ b/pkg/proxy/configs/copy.go @@ -0,0 +1,44 @@ +package configs + +import ( + "io" + "os" + "path/filepath" +) + +func copyFolderWithAllFiles(src, dst string) error { + return filepath.Walk(src, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + relPath, err := filepath.Rel(src, path) + if err != nil { + return err + } + + if info.IsDir() { + return os.MkdirAll(filepath.Join(dst, relPath), info.Mode()) + } + + in, err := os.Open(path) + if err != nil { + return err + } + defer func() { + _ = in.Close() + }() + + out, err := os.Create(filepath.Join(dst, relPath)) + if err != nil { + return err + } + + _, err = io.Copy(out, in) + if err != nil { + _ = out.Close() + return err + } + + return out.Close() + }) +} diff --git a/pkg/proxy/configs/copy_test.go b/pkg/proxy/configs/copy_test.go new file mode 100644 index 00000000..163bf7d6 --- /dev/null +++ b/pkg/proxy/configs/copy_test.go @@ -0,0 +1,44 @@ +package configs + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCopyFolderWithAllFiles(t *testing.T) { + src := t.TempDir() + dst := t.TempDir() + + var err error + files := []string{"file1.txt", "file2.txt", "dir/file3.txt"} + for _, file := range files { + path := filepath.Join(src, file) + err = os.MkdirAll(filepath.Dir(path), 0755) + require.Nil(t, err) + + err = os.WriteFile(path, []byte("Hello, World!"), 0644) + require.Nil(t, err) + } + + err = copyFolderWithAllFiles(src, dst) + require.Nil(t, err) + + for _, file := range files { + _, err = os.Stat(filepath.Join(dst, file)) + if os.IsNotExist(err) { + t.Errorf("file %s was not copied", file) + } else if err != nil { + t.Error(err) + } + } +} + +func TestCopyFolderWithAllFilesNonExistentSrc(t *testing.T) { + dst := t.TempDir() + + err := copyFolderWithAllFiles("/path/to/non/existent/directory", dst) + require.NotNil(t, err) +} diff --git a/pkg/proxy/configs/git/fetcher.go b/pkg/proxy/configs/git/fetcher.go new file mode 100644 index 00000000..2a6bbbed --- /dev/null +++ b/pkg/proxy/configs/git/fetcher.go @@ -0,0 +1,37 @@ +package git + +import ( + "fmt" + "os/exec" +) + +type gitFetcher struct{} + +// NewGitFetcher will create a new instance of gitFetcher +func NewGitFetcher() *gitFetcher { + return &gitFetcher{} +} + +// Clone will clone the provided git repository in the provided destination dir +func (gf *gitFetcher) Clone(repoURL, destDir string) error { + cmd := exec.Command("git", "clone", repoURL, destDir) + res, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("%s-%s", string(res), err.Error()) + } + + return nil +} + +// Checkout will checkout or commit hash from the provided repository directory +func (gf *gitFetcher) Checkout(repoDir string, commitHashOrBranch string) error { + cmd := exec.Command("git", "checkout", commitHashOrBranch) + cmd.Dir = repoDir + + res, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("%s-%s", string(res), err.Error()) + } + + return nil +} diff --git a/pkg/proxy/configs/interface.go b/pkg/proxy/configs/interface.go new file mode 100644 index 00000000..80cc454a --- /dev/null +++ b/pkg/proxy/configs/interface.go @@ -0,0 +1,7 @@ +package configs + +// GitHandler defines what a git handler should be able to do +type GitHandler interface { + Clone(repoURL, destDir string) error + Checkout(repoDir string, commitHashOrBranch string) error +} diff --git a/testscommon/gitFetcherStub.go b/testscommon/gitFetcherStub.go new file mode 100644 index 00000000..9deb154b --- /dev/null +++ b/testscommon/gitFetcherStub.go @@ -0,0 +1,17 @@ +package testscommon + +type GitFetcherStub struct { + CloneCalled func(r, d string) error +} + +func (gf *GitFetcherStub) Clone(repo, dir string) error { + if gf.CloneCalled != nil { + return gf.CloneCalled(repo, dir) + } + + return nil +} + +func (gf *GitFetcherStub) Checkout(_, _ string) error { + return nil +}