Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
miiu96 committed Nov 15, 2023
1 parent 514e09a commit e47d2fd
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20.5 as builder
FROM golang:1.20.7 as builder


WORKDIR /multiversx
Expand Down
2 changes: 1 addition & 1 deletion cmd/chainsimulator/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
logs-path = "logs"
5 changes: 3 additions & 2 deletions cmd/chainsimulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
20 changes: 6 additions & 14 deletions pkg/proxy/configs/configs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package configs

import (
"encoding/hex"
"encoding/pem"
"os"
"os/exec"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
71 changes: 5 additions & 66 deletions pkg/proxy/configs/configsFetcher.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package configs

import (
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime/debug"
"strings"

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions pkg/proxy/configs/configsFetcher_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package configs

import (
"os"
"runtime/debug"
"testing"

"github.com/multiversx/mx-chain-simulator-go/testscommon"
"github.com/stretchr/testify/require"
)

Expand All @@ -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{
Expand All @@ -32,7 +32,7 @@ func TestConfigsFetcher(t *testing.T) {
Version: "v1.1.41",
},
},
}, "./test")
}, dir)
require.Nil(t, err)

}
44 changes: 44 additions & 0 deletions pkg/proxy/configs/copy.go
Original file line number Diff line number Diff line change
@@ -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()
})
}
44 changes: 44 additions & 0 deletions pkg/proxy/configs/copy_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
37 changes: 37 additions & 0 deletions pkg/proxy/configs/git/fetcher.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit e47d2fd

Please sign in to comment.