From bc68a80f4639d6f9ea94ba7aea39a1b705fa456b Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Wed, 8 Jun 2022 11:51:22 +0200 Subject: [PATCH 01/15] carry on with Unit tests with new transaction logic --- blockchain/ethereum/testing/ethereum_test.go | 4 +- blockchain/testutils/setup.go | 465 ++++++++++--------- blockchain/testutils/setup_test.go | 29 ++ 3 files changed, 269 insertions(+), 229 deletions(-) create mode 100644 blockchain/testutils/setup_test.go diff --git a/blockchain/ethereum/testing/ethereum_test.go b/blockchain/ethereum/testing/ethereum_test.go index 75672056..a55efd9c 100644 --- a/blockchain/ethereum/testing/ethereum_test.go +++ b/blockchain/ethereum/testing/ethereum_test.go @@ -82,14 +82,14 @@ func TestEthereum_HardhatNode(t *testing.T) { t.Logf("deploy account: %v", deployAccount.Address.String()) - err = testutils.StartHardHatNode(eth) + err, _ = testutils.StartHardHatNode(eth) if err != nil { t.Fatalf("error starting hardhat node: %v", err) } t.Logf("waiting on hardhat node to start...") - err = testutils.WaitForHardHatNode(ctx) + err = testutils.WaitHardHatNodeToStart(ctx) if err != nil { t.Fatalf("error: %v", err) } diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 0489c1dd..26d66d56 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -7,12 +7,10 @@ import ( "crypto/ecdsa" "encoding/json" "fmt" - "github.com/MadBase/MadNet/logging" - "github.com/sirupsen/logrus" + "github.com/MadBase/MadNet/config" "io" "io/ioutil" "log" - "math" "math/big" "net/http" "os" @@ -35,6 +33,13 @@ import ( "github.com/stretchr/testify/assert" ) +var ( + scriptDeploy = "deploy" + scriptRegisterValidator = "register_test" + scriptStartHardHatNode = "hardhat_node" + scriptInit = "init" +) + // SetupPrivateKeys computes deterministic private keys for testing func SetupPrivateKeys(n int) []*ecdsa.PrivateKey { if (n < 1) || (n >= 256) { @@ -60,7 +65,7 @@ func SetupPrivateKeys(n int) []*ecdsa.PrivateKey { return privKeyArray } -// SetAccounts derives the associated addresses from private keys +// SetupAccounts derives the associated addresses from private keys func SetupAccounts(privKeys []*ecdsa.PrivateKey) []accounts.Account { accountsArray := []accounts.Account{} for _, pk := range privKeys { @@ -71,29 +76,6 @@ func SetupAccounts(privKeys []*ecdsa.PrivateKey) []accounts.Account { return accountsArray } -func GetMadnetRootPath() []string { - - rootPath := []string{string(os.PathSeparator)} - - cmd := exec.Command("go", "list", "-m", "-f", "'{{.Dir}}'", "github.com/MadBase/MadNet") - stdout, err := cmd.Output() - if err != nil { - log.Printf("Error getting project root path: %v", err) - return rootPath - } - - path := string(stdout) - path = strings.ReplaceAll(path, "'", "") - path = strings.ReplaceAll(path, "\n", "") - - pathNodes := strings.Split(path, string(os.PathSeparator)) - for _, pathNode := range pathNodes { - rootPath = append(rootPath, pathNode) - } - - return rootPath -} - func InitializePrivateKeysAndAccounts(n int) ([]*ecdsa.PrivateKey, []accounts.Account) { _, pKey, err := GetOwnerAccount() if err != nil { @@ -109,8 +91,9 @@ func InitializePrivateKeysAndAccounts(n int) ([]*ecdsa.PrivateKey, []accounts.Ac return privateKeys, accounts } -func ReadFromFileOnRoot(filePath string, configVar string) (string, error) { - rootPath := GetMadnetRootPath() +// GetConfigurationKeyValueFromFile given a file following the pattern `key = value`, it returns the value of `key` +func GetConfigurationKeyValueFromFile(filePath string, key string) (string, error) { + rootPath := getProjectRootPath() rootPath = append(rootPath, filePath) fileFullPath := filepath.Join(rootPath...) @@ -120,48 +103,52 @@ func ReadFromFileOnRoot(filePath string, configVar string) (string, error) { } defer f.Close() - // Splits on newlines by default. scanner := bufio.NewScanner(f) - var defaultAccount string - - // https://golang.org/pkg/bufio/#Scanner.Scan + var value string for scanner.Scan() { - if strings.Contains(scanner.Text(), configVar) { - defaultAccount = scanner.Text() + if strings.Contains(scanner.Text(), key) { + value = scanner.Text() break } } - splits := strings.Split(defaultAccount, "=") + splits := strings.Split(value, "=") return strings.Trim(splits[1], " \""), nil } +func getConfigurationProperty(key string) string { + value, err := GetConfigurationKeyValueFromFile("scripts/base-files/baseConfig", key) + if err != nil { + return "" + } + return value +} + func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { - rootPath := GetMadnetRootPath() + rootPath := getProjectRootPath() - // open config file owner.toml - acctAddress, err := ReadFromFileOnRoot("scripts/base-files/owner.toml", "defaultAccount") + // Account + acctAddress, err := GetConfigurationKeyValueFromFile("scripts/base-files/owner.toml", "defaultAccount") if err != nil { + log.Printf("Error retrieving default account address. %v", err) return nil, nil, err } acctAddressLowerCase := strings.ToLower(acctAddress) - // open password file + // Password passwordPath := append(rootPath, "scripts") passwordPath = append(passwordPath, "base-files") passwordPath = append(passwordPath, "passwordFile") passwordFullPath := filepath.Join(passwordPath...) - fileContent, err := ioutil.ReadFile(passwordFullPath) + passwordFileContent, err := ioutil.ReadFile(passwordFullPath) if err != nil { - //log.Errorf("error opening passsword file: %v", err) - panic(err) + log.Printf("Error opening password file. %v", err) + return nil, nil, err } + password := string(passwordFileContent) - // Convert []byte to string - password := string(fileContent) - - // open wallet json file + // Wallet walletPath := append(rootPath, "scripts") walletPath = append(walletPath, "base-files") walletPath = append(walletPath, acctAddressLowerCase) @@ -169,246 +156,211 @@ func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { jsonBytes, err := ioutil.ReadFile(walletFullPath) if err != nil { - panic(err) + log.Printf("Error opening %v file. %v", acctAddressLowerCase, err) + return nil, nil, err } key, err := keystore.DecryptKey(jsonBytes, password) if err != nil { - panic(err) + log.Printf("Error decrypting jsonBytes. %v", err) + return nil, nil, err } return &key.Address, key.PrivateKey, nil } -func ConnectSimulatorEndpoint(t *testing.T, privateKeys []*ecdsa.PrivateKey, blockInterval time.Duration) ethereum.Network { - eth, err := ethereum.NewSimulator( - privateKeys, - 0, - big.NewInt(math.MaxInt64), - math.MaxInt64) +func ConnectSimulatorEndpoint(t *testing.T, cleanStart bool) ethereum.Network { - assert.Nil(t, err, "Failed to build Ethereum endpoint...") - // assert.True(t, eth.IsEthereumAccessible(), "Web3 endpoint is not available.") + details, err := ethereum.NewEndpoint( + getConfigurationProperty("endpoint"), + "../../assets/test/keys", + "../../assets/test/passcodes.txt", + "0x546F99F244b7B58B855330AE0E2BC1b30b41302F", + 1, + 500, + ) + assert.Nilf(t, err, "Failed to build Ethereum endpoint...") ctx, cancel := context.WithCancel(context.Background()) defer cancel() - // Unlock the default account and use it to deploy contracts - deployAccount := eth.GetDefaultAccount() - err = eth.UnlockAccount(deployAccount) - assert.Nil(t, err, "Failed to unlock default account") - - t.Logf("deploy account: %v", deployAccount.Address.String()) - - err = StartHardHatNode(eth) - if err != nil { - eth.Close() - t.Fatalf("error starting hardhat node: %v", err) - } - - t.Logf("waiting on hardhat node to start...") - - err = WaitForHardHatNode(ctx) - if err != nil { - eth.Close() - t.Fatalf("error: %v", err) - } - - t.Logf("deploying contracts..") - - err = StartDeployScripts(eth, ctx) + isRunning, err := isHardHatRunning() if err != nil { - eth.Close() - t.Fatalf("error deploying: %v", err) + return nil } - validatorAddresses := make([]string, 0) - for _, acct := range eth.GetKnownAccounts() { - validatorAddresses = append(validatorAddresses, acct.Address.String()) - } + if !isRunning || (isRunning && cleanStart) { + err = startHardHatNode(details) + if err != nil { + details.Close() + assert.Nilf(t, err, "Error starting HardHat node.") + } - err = RegisterValidators(eth, validatorAddresses) - assert.Nil(t, err) + err = waitForHardHatNode(ctx) + if err != nil { + details.Close() + assert.Nilf(t, err, "Error waiting for HardHat node to start") + } - // unlock accounts - for _, account := range eth.GetKnownAccounts() { - err := eth.UnlockAccount(account) - assert.Nil(t, err) - } + err = deployContracts(details, ctx, config.Configuration.Ethereum.FactoryAddress) + if err != nil { + details.Close() + assert.Nilf(t, err, "Error deploying contracts: %v") + } - // fund accounts - for _, account := range eth.GetKnownAccounts()[1:] { - txn, err := eth.TransferEther(deployAccount.Address, account.Address, big.NewInt(100000000000000000)) - assert.Nil(t, err) - assert.NotNil(t, txn) - if txn == nil { - // this shouldn't be needed, but is - eth.Close() - t.Fatal("could not transfer ether") + validatorAddresses := make([]string, 0) + for _, acct := range details.GetKnownAccounts() { + validatorAddresses = append(validatorAddresses, acct.Address.String()) + } + err = registerValidators(details, validatorAddresses) + if err != nil { + details.Close() + assert.Nilf(t, err, "Error registering validators: %v") } - watcher := transaction.NewWatcher(eth.GetClient(), transaction.NewKnownSelectors(), eth.GetFinalityDelay()) - watcher.StartLoop() - rcpt, err := watcher.SubscribeAndWait(ctx, txn) - assert.Nil(t, err) - assert.NotNil(t, rcpt) - } + // Fund accounts + for _, account := range details.GetKnownAccounts()[1:] { + txn, err := details.TransferEther(details.GetDefaultAccount().Address, account.Address, big.NewInt(100000000000000000)) + assert.Nilf(t, err, "Error in TrasferEther transaction") + assert.NotNilf(t, txn, "Expected transaction not to be nil") - return eth -} + watcher := transaction.NewWatcher(details, transaction.NewKnownSelectors(), details.GetFinalityDelay()) + watcher.StartLoop() -func Setup(finalityDelay uint64, numAccounts int, registryAddress common.Address) (ethereum.Network, *logrus.Logger, error) { - logger := logging.GetLogger("test") - logger.SetLevel(logrus.TraceLevel) - ecdsaPrivateKeys, _ := InitializePrivateKeysAndAccounts(numAccounts) - eth, err := ethereum.NewSimulator( - ecdsaPrivateKeys, - 6, - 10*time.Second, - 30*time.Second, - 0, - big.NewInt(math.MaxInt64), - 50, - math.MaxInt64) - if err != nil { - return nil, logger, err + rcpt, err := watcher.SubscribeAndWait(ctx, txn) + assert.Nilf(t, err, "Error SubscribeAndWait watcher") + assert.NotNilf(t, rcpt, "Receipt expected to be not nil") + } } - eth.SetFinalityDelay(finalityDelay) - knownSelectors := transaction.NewKnownSelectors() - transaction := transaction.NewWatcher(eth.GetClient(), knownSelectors, 5) - transaction.SetNumOfConfirmationBlocks(finalityDelay) - - //todo: redeploy and get the registryAddress here - err = eth.Contracts().LookupContracts(context.Background(), registryAddress) - if err != nil { - return nil, logger, err - } - return eth, logger, nil + return details } -func StartHardHatNode(eth *ethereum.Details) error { - rootPath := GetMadnetRootPath() - scriptPath := append(rootPath, "scripts") - scriptPath = append(scriptPath, "main.sh") - scriptPathJoined := filepath.Join(scriptPath...) - fmt.Println("scriptPathJoined2: ", scriptPathJoined) +//func Setup(finalityDelay uint64, numAccounts int, registryAddress common.Address) (ethereum.Network, *logrus.Logger, error) { +// logger := logging.GetLogger("test") +// logger.SetLevel(logrus.TraceLevel) +// ecdsaPrivateKeys, _ := InitializePrivateKeysAndAccounts(numAccounts) +// eth, err := ethereum.NewSimulator( +// ecdsaPrivateKeys, +// 6, +// 10*time.Second, +// 30*time.Second, +// 0, +// big.NewInt(math.MaxInt64), +// 50, +// math.MaxInt64) +// if err != nil { +// return nil, logger, err +// } +// +// eth.SetFinalityDelay(finalityDelay) +// knownSelectors := transaction.NewKnownSelectors() +// transaction := transaction.NewWatcher(eth.GetClient(), knownSelectors, 5) +// transaction.SetNumOfConfirmationBlocks(finalityDelay) +// +// //todo: redeploy and get the registryAddress here +// err = eth.Contracts().LookupContracts(context.Background(), registryAddress) +// if err != nil { +// return nil, logger, err +// } +// return eth, logger, nil +//} + +func startHardHatNode(details *ethereum.Details) error { + + rootPath := getProjectRootPath() + scriptPath := getMainScriptPath() cmd := exec.Cmd{ - Path: scriptPathJoined, - Args: []string{scriptPathJoined, "hardhat_node"}, + Path: scriptPath, + Args: []string{scriptPath, scriptStartHardHatNode}, Dir: filepath.Join(rootPath...), } setCommandStdOut(&cmd) err := cmd.Start() - - // if there is an error with our execution - // handle it here if err != nil { - return fmt.Errorf("could not run hardhat node: %s", err) + log.Printf("Could not execute %s script: %v", scriptStartHardHatNode, err) + return err } - eth.SetClose(func() error { - fmt.Printf("closing hardhat node %v..\n", cmd.Process.Pid) - err := cmd.Process.Signal(syscall.SIGTERM) - if err != nil { - return err - } - - _, err = cmd.Process.Wait() - if err != nil { - return err - } - - fmt.Printf("hardhat node closed\n") - return nil - }) + details.GetInternalClient().Close() return nil } -// setCommandStdOut If ENABLE_SCRIPT_LOG env variable is set as 'true' the command will show scripts logs -func setCommandStdOut(cmd *exec.Cmd) { +func StopHardHatNode(cmd exec.Cmd) error { - flagValue, found := os.LookupEnv("ENABLE_SCRIPT_LOG") - enabled, err := strconv.ParseBool(flagValue) + fmt.Printf("closing hardhat node %v..\n", cmd.Process.Pid) + err := cmd.Process.Signal(syscall.SIGTERM) + if err != nil { + log.Printf("Error waiting sending SIGTERM signal to HardHat process: %v", err) + return err + } - if err == nil && found && enabled { - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - } else { - cmd.Stdout = io.Discard - cmd.Stderr = io.Discard + _, err = cmd.Process.Wait() + if err != nil { + log.Printf("Error waiting HardHat process to stop: %v", err) + return err } + + fmt.Printf("hardhat node closed\n") + return nil } func InitializeValidatorFiles(n int) error { - rootPath := GetMadnetRootPath() - scriptPath := append(rootPath, "scripts") - scriptPath = append(scriptPath, "main.sh") - scriptPathJoined := filepath.Join(scriptPath...) - fmt.Println("scriptPathJoined2: ", scriptPathJoined) + rootPath := getProjectRootPath() + scriptPath := getMainScriptPath() cmd := exec.Cmd{ - Path: scriptPathJoined, - Args: []string{scriptPathJoined, "init", strconv.Itoa(n)}, + Path: scriptPath, + Args: []string{scriptPath, scriptInit, strconv.Itoa(n)}, Dir: filepath.Join(rootPath...), } setCommandStdOut(&cmd) err := cmd.Start() if err != nil { - return fmt.Errorf("could not generate validator files: %s", err) + log.Printf("Could not execute %s script: %v", scriptInit, err) + return err } return nil } -func StartDeployScripts(eth *ethereum.Details, ctx context.Context) error { +func deployContracts(eth *ethereum.Details, ctx context.Context, factoryAddress string) error { - rootPath := GetMadnetRootPath() - scriptPath := append(rootPath, "scripts") - scriptPath = append(scriptPath, "main.sh") - scriptPathJoined := filepath.Join(scriptPath...) - fmt.Println("scriptPathJoined: ", scriptPathJoined) + rootPath := getProjectRootPath() + scriptPath := getMainScriptPath() err := os.Setenv("SKIP_REGISTRATION", "1") if err != nil { + log.Printf("Error setting environment variable: %v", err) return err } cmd := exec.Cmd{ - Path: scriptPathJoined, - Args: []string{scriptPathJoined, "deploy"}, + Path: scriptPath, + Args: []string{scriptPath, scriptDeploy}, Dir: filepath.Join(rootPath...), } setCommandStdOut(&cmd) err = cmd.Run() - - // if there is an error with our execution - // handle it here - if err != nil { - log.Printf("Could not execute deploy script: %s", err) - return err - } - - // inits contracts - factory, err := ReadFromFileOnRoot("scripts/generated/factoryState", "defaultFactoryAddress") if err != nil { + log.Printf("Could not execute %s script: %v", scriptDeploy, err) return err } addr := common.Address{} - copy(addr[:], common.FromHex(factory)) + copy(addr[:], common.FromHex(factoryAddress)) eth.Contracts().Initialize(ctx, addr) return nil } -func WaitForHardHatNode(ctx context.Context) error { +func waitForHardHatNode(ctx context.Context) error { c := http.Client{} msg := ðereum.JsonRPCMessage{ Version: "2.0", @@ -416,69 +368,79 @@ func WaitForHardHatNode(ctx context.Context) error { Method: "eth_chainId", Params: make([]byte, 0), } - var err error - if msg.Params, err = json.Marshal(make([]string, 0)); err != nil { - panic(err) + + params, err := json.Marshal(make([]string, 0)) + if err != nil { + log.Printf("could not run hardhat node: %v", err) + return err } + msg.Params = params var buff bytes.Buffer err = json.NewEncoder(&buff).Encode(msg) if err != nil { + log.Printf("Error creating a buffer json encoder: %v", err) return err } - reader := bytes.NewReader(buff.Bytes()) - for { select { case <-ctx.Done(): return ctx.Err() case <-time.After(time.Second): - resp, err := c.Post( - "http://127.0.0.1:8545", + body := bytes.NewReader(buff.Bytes()) + _, err := c.Post( + config.Configuration.Ethereum.Endpoint, "application/json", - reader, + body, ) if err != nil { continue } - _, err = io.ReadAll(resp.Body) - if err == nil { - return nil - } + log.Printf("HardHat node started correctly") + return nil } + } +} +func isHardHatRunning() (bool, error) { + var client = http.Client{Timeout: 2 * time.Second} + resp, err := client.Head(config.Configuration.Ethereum.Endpoint) + if err != nil { + return false, err } + resp.Body.Close() + + if resp.StatusCode >= 200 && resp.StatusCode <= 299 { + return true, nil + } + + return false, nil } -func RegisterValidators(eth *ethereum.Details, validatorAddresses []string) error { +func registerValidators(eth *ethereum.Details, validatorAddresses []string) error { - rootPath := GetMadnetRootPath() - scriptPath := append(rootPath, "scripts") - scriptPath = append(scriptPath, "main.sh") - scriptPathJoined := filepath.Join(scriptPath...) - fmt.Println("scriptPathJoined: ", scriptPathJoined) + rootPath := getProjectRootPath() + scriptPath := getMainScriptPath() args := []string{ - scriptPathJoined, - "register_test", + scriptPath, + scriptRegisterValidator, eth.Contracts().ContractFactoryAddress().String(), } args = append(args, validatorAddresses...) cmd := exec.Cmd{ - Path: scriptPathJoined, + Path: scriptPath, Args: args, Dir: filepath.Join(rootPath...), } setCommandStdOut(&cmd) err := cmd.Run() - - // if there is an error with our execution - // handle it here if err != nil { - return fmt.Errorf("could not execute deploy script: %s", err) + log.Printf("Could not execute %s script: %v", scriptRegisterValidator, err) + return err } return nil @@ -588,3 +550,52 @@ func SetBlockInterval(t *testing.T, eth ethereum.Network, intervalInMilliSeconds panic(err) } } + +// getProjectRootPath returns the project root path +func getProjectRootPath() []string { + + rootPath := []string{string(os.PathSeparator)} + + cmd := exec.Command("go", "list", "-m", "-f", "'{{.Dir}}'", "github.com/MadBase/MadNet") + stdout, err := cmd.Output() + if err != nil { + log.Printf("Error getting project root path: %v", err) + return rootPath + } + + path := string(stdout) + path = strings.ReplaceAll(path, "'", "") + path = strings.ReplaceAll(path, "\n", "") + + pathNodes := strings.Split(path, string(os.PathSeparator)) + for _, pathNode := range pathNodes { + rootPath = append(rootPath, pathNode) + } + + return rootPath +} + +// getMainScriptPath return the path of the main.sh script +func getMainScriptPath() string { + rootPath := getProjectRootPath() + scriptPath := append(rootPath, "scripts") + scriptPath = append(scriptPath, "main.sh") + scriptPathJoined := filepath.Join(scriptPath...) + + return scriptPathJoined +} + +// setCommandStdOut If ENABLE_SCRIPT_LOG env variable is set as 'true' the command will show scripts logs +func setCommandStdOut(cmd *exec.Cmd) { + + flagValue, found := os.LookupEnv("ENABLE_SCRIPT_LOG") + enabled, err := strconv.ParseBool(flagValue) + + if err == nil && found && enabled { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + } else { + cmd.Stdout = io.Discard + cmd.Stderr = io.Discard + } +} diff --git a/blockchain/testutils/setup_test.go b/blockchain/testutils/setup_test.go new file mode 100644 index 00000000..ebeb0c18 --- /dev/null +++ b/blockchain/testutils/setup_test.go @@ -0,0 +1,29 @@ +package testutils + +import ( + "github.com/MadBase/MadNet/blockchain/ethereum" + "reflect" + "testing" +) + +func TestConnectSimulatorEndpoint(t *testing.T) { + + tests := []struct { + name string + cleanStart bool + want ethereum.Network + }{ + { + name: "HardHat not running", + cleanStart: false, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ConnectSimulatorEndpoint(t, tt.cleanStart); !reflect.DeepEqual(got, tt.want) { + t.Errorf("ConnectSimulatorEndpoint() = %v, want %v", got, tt.want) + } + }) + } +} From ec93bb478e3d905fc55973c3518db1a662f93dc3 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Wed, 8 Jun 2022 18:56:14 +0200 Subject: [PATCH 02/15] Renaming ConnectSimulatorEndpoint to GetEthereumNetwork Improve Hardhat logic --- blockchain/ethereum/testing/ethereum_test.go | 287 ++++++--------- .../executor/tasks/dkg/completion_test.go | 20 +- .../dkg/dispute_share_distribution_test.go | 4 +- .../tasks/dkg/gpkj_submission_test.go | 4 +- .../executor/tasks/dkg/mpk_submission_test.go | 4 +- .../executor/tasks/dkg/register_test.go | 16 +- .../tasks/dkg/share_distribution_test.go | 4 +- .../tasks/dkg/testutils/advance_phases.go | 2 +- blockchain/testutils/setup.go | 346 ++++++++++-------- blockchain/testutils/setup_test.go | 6 +- 10 files changed, 324 insertions(+), 369 deletions(-) diff --git a/blockchain/ethereum/testing/ethereum_test.go b/blockchain/ethereum/testing/ethereum_test.go index a55efd9c..8338ca74 100644 --- a/blockchain/ethereum/testing/ethereum_test.go +++ b/blockchain/ethereum/testing/ethereum_test.go @@ -3,199 +3,122 @@ package ethereum import ( - "context" - "errors" - "fmt" - "io/fs" - "math" - "math/big" - "net" - "testing" - "time" - - "github.com/MadBase/MadNet/blockchain/ethereum" "github.com/MadBase/MadNet/blockchain/testutils" + "testing" - "github.com/MadBase/MadNet/logging" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) -func setupEthereum(t *testing.T, n int) ethereum.Network { - logging.GetLogger("ethereum").SetLevel(logrus.InfoLevel) - - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 1000*time.Millisecond) - assert.NotNil(t, eth) - - acct := eth.GetDefaultAccount() - assert.Nil(t, eth.UnlockAccount(acct)) - - return eth -} - func TestEthereum_AccountsFound(t *testing.T) { - eth := setupEthereum(t, 4) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() accountList := eth.GetKnownAccounts() - for _, acct := range accountList { - - err := eth.UnlockAccount(acct) - assert.Nilf(t, err, "Not able to unlock account: %v", acct.Address) - - _, err = eth.GetAccountKeys(acct.Address) + _, err := eth.GetAccountKeys(acct.Address) assert.Nilf(t, err, "Not able to get keys for account: %v", acct.Address) } - } -func TestEthereum_HardhatNode(t *testing.T) { - privateKeys, _ := testutils.InitializePrivateKeysAndAccounts(4) - - eth, err := ethereum.NewSimulator( - privateKeys, - 6, - 10*time.Second, - 30*time.Second, - 0, - big.NewInt(math.MaxInt64), - 50, - math.MaxInt64) - defer func() { - err := eth.Close() - if err != nil { - t.Fatalf("error closing eth: %v", err) - } - }() - - assert.Nil(t, err, "Failed to build Ethereum endpoint...") - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - // Unlock the default account and use it to deploy contracts - deployAccount := eth.GetDefaultAccount() - err = eth.UnlockAccount(deployAccount) - assert.Nil(t, err, "Failed to unlock default account") - - t.Logf("deploy account: %v", deployAccount.Address.String()) - - err, _ = testutils.StartHardHatNode(eth) - if err != nil { - t.Fatalf("error starting hardhat node: %v", err) - } - - t.Logf("waiting on hardhat node to start...") - - err = testutils.WaitHardHatNodeToStart(ctx) - if err != nil { - t.Fatalf("error: %v", err) - } - - t.Logf("done testing") -} - -func TestEthereum_NewEthereumEndpoint(t *testing.T) { - - eth := setupEthereum(t, 4) - defer eth.Close() - - type args struct { - endpoint string - pathKeystore string - pathPasscodes string - defaultAccount string - timeout time.Duration - retryCount int - retryDelay time.Duration - finalityDelay int - txFeePercentageToIncrease int - getTxMaxGasFeeAllowedInGwei uint64 - } - tests := []struct { - name string - args args - want bool - wantErr assert.ErrorAssertionFunc - }{ - - { - name: "Create new ethereum endpoint failing with passcode file not found", - args: args{"", "", "", "", 0, 0, 0, 0, 0, 0}, - want: false, - wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { - _, ok := err.(*fs.PathError) - if !ok { - t.Errorf("Failing test with an unexpected error") - } - return ok - }, - }, - { - name: "Create new ethereum endpoint failing with specified account not found", - args: args{"", "", "../assets/test/passcodes.txt", "", 0, 0, 0, 0, 0, 0}, - want: false, - wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { - if !errors.Is(err, ethereum.ErrAccountNotFound) { - t.Errorf("Failing test with an unexpected error") - } - return true - }, - }, - { - name: "Create new ethereum endpoint failing on Dial Context", - args: args{ - eth.GetEndpoint(), - "../assets/test/keys", - "../assets/test/passcodes.txt", - eth.GetDefaultAccount().Address.String(), - eth.Timeout(), - eth.RetryCount(), - eth.RetryDelay(), - int(eth.GetFinalityDelay()), - eth.GetTxFeePercentageToIncrease(), - eth.GetTxMaxGasFeeAllowedInGwei(), - }, - want: false, - wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { - _, ok := err.(*net.OpError) - if !ok { - t.Errorf("Failing test with an unexpected error") - } - return ok - }, - }, - { - name: "Create new ethereum endpoint returning EthereumDetails", - args: args{ - "http://localhost:8545", - "../assets/test/keys", - "../assets/test/passcodes.txt", - eth.GetDefaultAccount().Address.String(), - eth.Timeout(), - eth.RetryCount(), - eth.RetryDelay(), - int(eth.GetFinalityDelay()), - eth.GetTxFeePercentageToIncrease(), - eth.GetTxMaxGasFeeAllowedInGwei(), - }, - want: true, - wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { - return true - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := ethereum.NewEndpoint(tt.args.endpoint, tt.args.pathKeystore, tt.args.pathPasscodes, tt.args.defaultAccount, tt.args.timeout, tt.args.retryCount, tt.args.retryDelay, tt.args.txFeePercentageToIncrease, tt.args.getTxMaxGasFeeAllowedInGwei) - if !tt.wantErr(t, err, fmt.Sprintf("NewEthereumEndpoint(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v)", tt.args.endpoint, tt.args.pathKeystore, tt.args.pathPasscodes, tt.args.defaultAccount, tt.args.timeout, tt.args.retryCount, tt.args.retryDelay, tt.args.txFeePercentageToIncrease, tt.args.getTxMaxGasFeeAllowedInGwei)) { - return - } - if tt.want { - assert.NotNilf(t, got, "Ethereum Details must not be nil") - } - }) - } -} +//func TestEthereum_NewEthereumEndpoint(t *testing.T) { +// +// eth := setupEthereum(t, 4) +// defer eth.Close() +// +// type args struct { +// endpoint string +// pathKeystore string +// pathPasscodes string +// defaultAccount string +// timeout time.Duration +// retryCount int +// retryDelay time.Duration +// finalityDelay int +// txFeePercentageToIncrease int +// getTxMaxGasFeeAllowedInGwei uint64 +// } +// tests := []struct { +// name string +// args args +// want bool +// wantErr assert.ErrorAssertionFunc +// }{ +// +// { +// name: "Create new ethereum endpoint failing with passcode file not found", +// args: args{"", "", "", "", 0, 0, 0, 0, 0, 0}, +// want: false, +// wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { +// _, ok := err.(*fs.PathError) +// if !ok { +// t.Errorf("Failing test with an unexpected error") +// } +// return ok +// }, +// }, +// { +// name: "Create new ethereum endpoint failing with specified account not found", +// args: args{"", "", "../assets/test/passcodes.txt", "", 0, 0, 0, 0, 0, 0}, +// want: false, +// wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { +// if !errors.Is(err, ethereum.ErrAccountNotFound) { +// t.Errorf("Failing test with an unexpected error") +// } +// return true +// }, +// }, +// { +// name: "Create new ethereum endpoint failing on Dial Context", +// args: args{ +// eth.GetEndpoint(), +// "../assets/test/keys", +// "../assets/test/passcodes.txt", +// eth.GetDefaultAccount().Address.String(), +// eth.Timeout(), +// eth.RetryCount(), +// eth.RetryDelay(), +// int(eth.GetFinalityDelay()), +// eth.GetTxFeePercentageToIncrease(), +// eth.GetTxMaxGasFeeAllowedInGwei(), +// }, +// want: false, +// wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { +// _, ok := err.(*net.OpError) +// if !ok { +// t.Errorf("Failing test with an unexpected error") +// } +// return ok +// }, +// }, +// { +// name: "Create new ethereum endpoint returning EthereumDetails", +// args: args{ +// "http://localhost:8545", +// "../assets/test/keys", +// "../assets/test/passcodes.txt", +// eth.GetDefaultAccount().Address.String(), +// eth.Timeout(), +// eth.RetryCount(), +// eth.RetryDelay(), +// int(eth.GetFinalityDelay()), +// eth.GetTxFeePercentageToIncrease(), +// eth.GetTxMaxGasFeeAllowedInGwei(), +// }, +// want: true, +// wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { +// return true +// }, +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// got, err := ethereum.NewEndpoint(tt.args.endpoint, tt.args.pathKeystore, tt.args.pathPasscodes, tt.args.defaultAccount, tt.args.timeout, tt.args.retryCount, tt.args.retryDelay, tt.args.txFeePercentageToIncrease, tt.args.getTxMaxGasFeeAllowedInGwei) +// if !tt.wantErr(t, err, fmt.Sprintf("NewEthereumEndpoint(%v, %v, %v, %v, %v, %v, %v, %v, %v, %v, %v)", tt.args.endpoint, tt.args.pathKeystore, tt.args.pathPasscodes, tt.args.defaultAccount, tt.args.timeout, tt.args.retryCount, tt.args.retryDelay, tt.args.txFeePercentageToIncrease, tt.args.getTxMaxGasFeeAllowedInGwei)) { +// return +// } +// if tt.want { +// assert.NotNilf(t, got, "Ethereum Details must not be nil") +// } +// }) +// } +//} diff --git a/blockchain/executor/tasks/dkg/completion_test.go b/blockchain/executor/tasks/dkg/completion_test.go index b7a551c6..eb301360 100644 --- a/blockchain/executor/tasks/dkg/completion_test.go +++ b/blockchain/executor/tasks/dkg/completion_test.go @@ -4,14 +4,12 @@ package dkg_test import ( "context" - "testing" - "time" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg" "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/monitor/events" "github.com/MadBase/MadNet/blockchain/testutils" + "testing" "github.com/MadBase/MadNet/logging" "github.com/sirupsen/logrus" @@ -22,7 +20,7 @@ import ( func TestCompletion_Group_1_AllGood(t *testing.T) { n := 4 - err := testutils.InitializeValidatorFiles(5) + err := testutils.RunScriptInitializeValidator(5) assert.Nil(t, err) suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) @@ -40,7 +38,6 @@ func TestCompletion_Group_1_AllGood(t *testing.T) { err = suite.GpkjSubmissionTasks[idx].DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, suite.GpkjSubmissionTasks[idx].Success) } @@ -106,7 +103,6 @@ func TestCompletion_Group_1_StartFromCompletion(t *testing.T) { if amILeading { hasLeader = true err = task.DoWork(ctx, logger, eth) - eth.Commit() assert.Nil(t, err) assert.False(t, task.ShouldRetry(ctx, logger, eth)) assert.True(t, task.Success) @@ -135,11 +131,9 @@ func TestCompletion_Group_1_StartFromCompletion(t *testing.T) { // This test is meant to raise an error resulting from an invalid argument // for the Ethereum interface. func TestCompletion_Group_2_Bad1(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -158,11 +152,9 @@ func TestCompletion_Group_2_Bad1(t *testing.T) { // We test to ensure that everything behaves correctly. func TestCompletion_Group_2_Bad2(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -200,7 +192,6 @@ func TestCompletion_Group_2_Bad3(t *testing.T) { err = tasksVec[idx].DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, tasksVec[idx].Success) } @@ -221,7 +212,6 @@ func TestCompletion_Group_2_Bad3(t *testing.T) { // Advance to end of Completion phase testutils.AdvanceTo(t, eth, completionEnd) - eth.Commit() err = completionTasks[0].Initialize(ctx, logger, eth) if err != nil { @@ -286,7 +276,6 @@ func TestCompletion_Group_3_ShouldRetry_returnsTrue(t *testing.T) { err = suite.GpkjSubmissionTasks[idx].DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, suite.GpkjSubmissionTasks[idx].Success) } @@ -306,7 +295,6 @@ func TestCompletion_Group_3_ShouldRetry_returnsTrue(t *testing.T) { // Advance to Completion phase testutils.AdvanceTo(t, eth, completionStart) - eth.Commit() err = completionTasks[0].Initialize(ctx, logger, eth) assert.Nil(t, err) diff --git a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go index 4a343d51..c52bbe8b 100644 --- a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go @@ -160,7 +160,7 @@ func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -187,7 +187,7 @@ func TestDisputeShareDistributionTask_Group_2_Bad2(t *testing.T) { ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) defer eth.Close() accts := eth.GetKnownAccounts() diff --git a/blockchain/executor/tasks/dkg/gpkj_submission_test.go b/blockchain/executor/tasks/dkg/gpkj_submission_test.go index e56b0d5e..508a4d71 100644 --- a/blockchain/executor/tasks/dkg/gpkj_submission_test.go +++ b/blockchain/executor/tasks/dkg/gpkj_submission_test.go @@ -65,7 +65,7 @@ func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -91,7 +91,7 @@ func TestGPKjSubmission_Group_1_Bad2(t *testing.T) { ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/mpk_submission_test.go b/blockchain/executor/tasks/dkg/mpk_submission_test.go index 4a7d1666..2942d2c2 100644 --- a/blockchain/executor/tasks/dkg/mpk_submission_test.go +++ b/blockchain/executor/tasks/dkg/mpk_submission_test.go @@ -116,7 +116,7 @@ func TestMPKSubmission_Group_1_Bad2(t *testing.T) { ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -141,7 +141,7 @@ func TestMPKSubmission_Group_2_Bad4(t *testing.T) { ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/register_test.go b/blockchain/executor/tasks/dkg/register_test.go index f9609b1b..56a2db77 100644 --- a/blockchain/executor/tasks/dkg/register_test.go +++ b/blockchain/executor/tasks/dkg/register_test.go @@ -31,7 +31,7 @@ func TestRegisterTask_Group_1_Task(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 500*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 500*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -110,7 +110,7 @@ func TestRegisterTask_Group_1_Good2(t *testing.T) { n := 6 ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) assert.NotNil(t, eth) defer eth.Close() @@ -199,7 +199,7 @@ func TestRegisterTask_Group_1_Bad1(t *testing.T) { n := 5 ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) assert.NotNil(t, eth) defer eth.Close() @@ -259,7 +259,7 @@ func TestRegisterTask_Group_2_Bad2(t *testing.T) { n := 7 ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) assert.NotNil(t, eth) defer eth.Close() @@ -316,7 +316,7 @@ func TestRegisterTask_Group_2_Bad4(t *testing.T) { n := 3 ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) assert.NotNil(t, eth) defer eth.Close() @@ -347,7 +347,7 @@ func TestRegisterTask_Group_2_Bad5(t *testing.T) { n := 5 ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) assert.NotNil(t, eth) defer eth.Close() @@ -412,7 +412,7 @@ func TestRegisterTask_Group_3_ShouldRetryFalse(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 500*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 500*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -500,7 +500,7 @@ func TestRegisterTask_Group_3_ShouldRetryTrue(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 500*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 500*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/share_distribution_test.go b/blockchain/executor/tasks/dkg/share_distribution_test.go index dfe2b4e3..3ee85114 100644 --- a/blockchain/executor/tasks/dkg/share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/share_distribution_test.go @@ -296,7 +296,7 @@ func TestShareDistribution_Group_2_Bad6(t *testing.T) { ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -320,7 +320,7 @@ func TestShareDistribution_Group_3_Bad7(t *testing.T) { ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/testutils/advance_phases.go b/blockchain/executor/tasks/dkg/testutils/advance_phases.go index 86423e27..4bb1be20 100644 --- a/blockchain/executor/tasks/dkg/testutils/advance_phases.go +++ b/blockchain/executor/tasks/dkg/testutils/advance_phases.go @@ -116,7 +116,7 @@ func InitializeETHDKG(eth ethereum.Network, callOpts *bind.TransactOpts, ctx con func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators int, phaseLength uint16) *TestSuite { ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.ConnectSimulatorEndpoint(t, ecdsaPrivateKeys, 1000*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 1000*time.Millisecond) assert.NotNil(t, eth) ctx := context.Background() diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 26d66d56..39f596c1 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -6,14 +6,14 @@ import ( "context" "crypto/ecdsa" "encoding/json" - "fmt" - "github.com/MadBase/MadNet/config" + "github.com/MadBase/MadNet/blockchain/transaction" + "github.com/MadBase/MadNet/logging" "io" "io/ioutil" "log" "math/big" "net/http" - "os" + os "os" "os/exec" "path/filepath" "strconv" @@ -23,8 +23,6 @@ import ( "time" "github.com/MadBase/MadNet/blockchain/ethereum" - "github.com/MadBase/MadNet/blockchain/transaction" - "github.com/MadBase/MadNet/utils" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -38,8 +36,194 @@ var ( scriptRegisterValidator = "register_test" scriptStartHardHatNode = "hardhat_node" scriptInit = "init" + hardHatProcessId = "HARDHAT_PROCESS_ID" ) +func getEthereumDetails() (*ethereum.Details, error) { + details, err := ethereum.NewEndpoint( + getConfigurationProperty("endpoint"), + "../../assets/test/keys", + "../../assets/test/passcodes.txt", + "0x546F99F244b7B58B855330AE0E2BC1b30b41302F", + 1, + 500, + 0, + ) + return details, err +} + +func waitForHardHatNode(ctx context.Context) error { + c := http.Client{} + msg := ðereum.JsonRPCMessage{ + Version: "2.0", + ID: []byte("1"), + Method: "eth_chainId", + Params: make([]byte, 0), + } + + params, err := json.Marshal(make([]string, 0)) + if err != nil { + log.Printf("could not run hardhat node: %v", err) + return err + } + msg.Params = params + + var buff bytes.Buffer + err = json.NewEncoder(&buff).Encode(msg) + if err != nil { + log.Printf("Error creating a buffer json encoder: %v", err) + return err + } + + for { + select { + case <-ctx.Done(): + return ctx.Err() + case <-time.After(time.Second): + body := bytes.NewReader(buff.Bytes()) + _, err := c.Post( + getConfigurationProperty("endpoint"), + "application/json", + body, + ) + if err != nil { + continue + } + log.Printf("HardHat node started correctly") + return nil + } + } +} + +func isHardHatRunning() (bool, error) { + var client = http.Client{Timeout: 2 * time.Second} + resp, err := client.Head(getConfigurationProperty("endpoint")) + if err != nil { + return false, err + } + resp.Body.Close() + + if resp.StatusCode >= 200 && resp.StatusCode <= 299 { + return true, nil + } + + return false, nil +} + +func startHardHat(t *testing.T, ctx context.Context) *ethereum.Details { + + log.Printf("Starting HardHat ...") + err := runScriptHardHatNode() + assert.Nilf(t, err, "Error starting hardhat node") + + err = waitForHardHatNode(ctx) + assert.Nilf(t, err, "Failed to wait for hardhat to be up and running") + + details, err := getEthereumDetails() + assert.Nilf(t, err, "Failed to build Ethereum endpoint") + assert.NotNilf(t, details, "Ethereum network should not be Nil") + + log.Printf("Deploying contracts ...") + err = runScriptDeployContracts(details, ctx, getConfigurationProperty("factoryAddress")) + if err != nil { + details.Close() + assert.Nilf(t, err, "Error deploying contracts: %v") + } + + log.Printf("Registering validators ...") + validatorAddresses := make([]string, 0) + knownAccounts := details.GetKnownAccounts() + for _, acct := range knownAccounts { + validatorAddresses = append(validatorAddresses, acct.Address.String()) + } + err = runScriptRegisterValidators(details, validatorAddresses) + if err != nil { + details.Close() + assert.Nilf(t, err, "Error registering validators: %v") + } + logger := logging.GetLogger("test").WithField("test", 0) + + log.Printf("Funding accounts ...") + for _, account := range knownAccounts[1:] { + watcher := transaction.WatcherFromNetwork(details) + watcher.StartLoop() + + txn, err := ethereum.TransferEther(details, logger, details.GetDefaultAccount().Address, account.Address, big.NewInt(100000000000000000)) + assert.Nilf(t, err, "Error in TrasferEther transaction") + assert.NotNilf(t, txn, "Expected transaction not to be nil") + } + return details +} + +func stopHardHat() error { + log.Printf("Stopping HardHat running instance ...") + isRunning, _ := isHardHatRunning() + if !isRunning { + return nil + } + + pid, _ := strconv.Atoi(os.Getenv(hardHatProcessId)) + process, err := os.FindProcess(pid) + if err != nil { + log.Printf("Error finding HardHat pid: %v", err) + return err + } + + err = process.Signal(syscall.SIGTERM) + if err != nil { + log.Printf("Error waiting sending SIGTERM signal to HardHat process: %v", err) + return err + } + + _, err = process.Wait() + if err != nil { + log.Printf("Error waiting HardHat process to stop: %v", err) + return err + } + + log.Printf("HardHat node has been stopped") + return nil +} + +func GetEthereumNetwork(t *testing.T, cleanStart bool) ethereum.Network { + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + isRunning, _ := isHardHatRunning() + if !isRunning { + log.Printf("Hardhat is not running. Start new HardHat") + details := startHardHat(t, ctx) + assert.NotNilf(t, details, "Expected details to be not nil") + return details + } + + if cleanStart { + err := stopHardHat() + assert.Nilf(t, err, "Failed to stopHardHat") + + details := startHardHat(t, ctx) + assert.NotNilf(t, details, "Expected details to be not nil") + return details + } + + network, err := getEthereumDetails() + assert.Nilf(t, err, "Failed to build Ethereum endpoint") + assert.NotNilf(t, network, "Ethereum network should not be Nil") + + return network +} + +// ======================================================== +// ======================================================== +// ======================================================== +// ======================================================== +// ======================================================== +// ======================================================== +// ======================================================== +// ======================================================== +// ======================================================== + // SetupPrivateKeys computes deterministic private keys for testing func SetupPrivateKeys(n int) []*ecdsa.PrivateKey { if (n < 1) || (n >= 256) { @@ -169,73 +353,6 @@ func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { return &key.Address, key.PrivateKey, nil } -func ConnectSimulatorEndpoint(t *testing.T, cleanStart bool) ethereum.Network { - - details, err := ethereum.NewEndpoint( - getConfigurationProperty("endpoint"), - "../../assets/test/keys", - "../../assets/test/passcodes.txt", - "0x546F99F244b7B58B855330AE0E2BC1b30b41302F", - 1, - 500, - ) - assert.Nilf(t, err, "Failed to build Ethereum endpoint...") - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - isRunning, err := isHardHatRunning() - if err != nil { - return nil - } - - if !isRunning || (isRunning && cleanStart) { - err = startHardHatNode(details) - if err != nil { - details.Close() - assert.Nilf(t, err, "Error starting HardHat node.") - } - - err = waitForHardHatNode(ctx) - if err != nil { - details.Close() - assert.Nilf(t, err, "Error waiting for HardHat node to start") - } - - err = deployContracts(details, ctx, config.Configuration.Ethereum.FactoryAddress) - if err != nil { - details.Close() - assert.Nilf(t, err, "Error deploying contracts: %v") - } - - validatorAddresses := make([]string, 0) - for _, acct := range details.GetKnownAccounts() { - validatorAddresses = append(validatorAddresses, acct.Address.String()) - } - err = registerValidators(details, validatorAddresses) - if err != nil { - details.Close() - assert.Nilf(t, err, "Error registering validators: %v") - } - - // Fund accounts - for _, account := range details.GetKnownAccounts()[1:] { - txn, err := details.TransferEther(details.GetDefaultAccount().Address, account.Address, big.NewInt(100000000000000000)) - assert.Nilf(t, err, "Error in TrasferEther transaction") - assert.NotNilf(t, txn, "Expected transaction not to be nil") - - watcher := transaction.NewWatcher(details, transaction.NewKnownSelectors(), details.GetFinalityDelay()) - watcher.StartLoop() - - rcpt, err := watcher.SubscribeAndWait(ctx, txn) - assert.Nilf(t, err, "Error SubscribeAndWait watcher") - assert.NotNilf(t, rcpt, "Receipt expected to be not nil") - } - } - - return details -} - //func Setup(finalityDelay uint64, numAccounts int, registryAddress common.Address) (ethereum.Network, *logrus.Logger, error) { // logger := logging.GetLogger("test") // logger.SetLevel(logrus.TraceLevel) @@ -266,7 +383,7 @@ func ConnectSimulatorEndpoint(t *testing.T, cleanStart bool) ethereum.Network { // return eth, logger, nil //} -func startHardHatNode(details *ethereum.Details) error { +func runScriptHardHatNode() error { rootPath := getProjectRootPath() scriptPath := getMainScriptPath() @@ -284,31 +401,16 @@ func startHardHatNode(details *ethereum.Details) error { return err } - details.GetInternalClient().Close() - - return nil -} - -func StopHardHatNode(cmd exec.Cmd) error { - - fmt.Printf("closing hardhat node %v..\n", cmd.Process.Pid) - err := cmd.Process.Signal(syscall.SIGTERM) - if err != nil { - log.Printf("Error waiting sending SIGTERM signal to HardHat process: %v", err) - return err - } - - _, err = cmd.Process.Wait() + err = os.Setenv(hardHatProcessId, strconv.Itoa(cmd.Process.Pid)) if err != nil { - log.Printf("Error waiting HardHat process to stop: %v", err) + log.Printf("Error setting environment variable: %v", err) return err } - fmt.Printf("hardhat node closed\n") return nil } -func InitializeValidatorFiles(n int) error { +func RunScriptInitializeValidator(n int) error { rootPath := getProjectRootPath() scriptPath := getMainScriptPath() @@ -329,7 +431,7 @@ func InitializeValidatorFiles(n int) error { return nil } -func deployContracts(eth *ethereum.Details, ctx context.Context, factoryAddress string) error { +func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context, factoryAddress string) error { rootPath := getProjectRootPath() scriptPath := getMainScriptPath() @@ -360,65 +462,7 @@ func deployContracts(eth *ethereum.Details, ctx context.Context, factoryAddress return nil } -func waitForHardHatNode(ctx context.Context) error { - c := http.Client{} - msg := ðereum.JsonRPCMessage{ - Version: "2.0", - ID: []byte("1"), - Method: "eth_chainId", - Params: make([]byte, 0), - } - - params, err := json.Marshal(make([]string, 0)) - if err != nil { - log.Printf("could not run hardhat node: %v", err) - return err - } - msg.Params = params - - var buff bytes.Buffer - err = json.NewEncoder(&buff).Encode(msg) - if err != nil { - log.Printf("Error creating a buffer json encoder: %v", err) - return err - } - - for { - select { - case <-ctx.Done(): - return ctx.Err() - case <-time.After(time.Second): - body := bytes.NewReader(buff.Bytes()) - _, err := c.Post( - config.Configuration.Ethereum.Endpoint, - "application/json", - body, - ) - if err != nil { - continue - } - log.Printf("HardHat node started correctly") - return nil - } - } -} - -func isHardHatRunning() (bool, error) { - var client = http.Client{Timeout: 2 * time.Second} - resp, err := client.Head(config.Configuration.Ethereum.Endpoint) - if err != nil { - return false, err - } - resp.Body.Close() - - if resp.StatusCode >= 200 && resp.StatusCode <= 299 { - return true, nil - } - - return false, nil -} - -func registerValidators(eth *ethereum.Details, validatorAddresses []string) error { +func runScriptRegisterValidators(eth *ethereum.Details, validatorAddresses []string) error { rootPath := getProjectRootPath() scriptPath := getMainScriptPath() diff --git a/blockchain/testutils/setup_test.go b/blockchain/testutils/setup_test.go index ebeb0c18..b20e730f 100644 --- a/blockchain/testutils/setup_test.go +++ b/blockchain/testutils/setup_test.go @@ -15,14 +15,14 @@ func TestConnectSimulatorEndpoint(t *testing.T) { }{ { name: "HardHat not running", - cleanStart: false, + cleanStart: true, want: nil, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := ConnectSimulatorEndpoint(t, tt.cleanStart); !reflect.DeepEqual(got, tt.want) { - t.Errorf("ConnectSimulatorEndpoint() = %v, want %v", got, tt.want) + if got := GetEthereumNetwork(t, tt.cleanStart); !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetEthereumNetwork() = %v, want %v", got, tt.want) } }) } From 4102a3610c08898300f0833bfe33adf2b9a3c561 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Thu, 9 Jun 2022 11:27:04 +0200 Subject: [PATCH 03/15] Removing HH references Removing hardcoded code Improved logic cleaning up refactoring --- .../executor/tasks/dkg/completion_test.go | 2 +- .../executor/tasks/dkg/dispute_gpkj_test.go | 6 - .../tasks/dkg/dispute_missing_gpkj_test.go | 13 --- .../dkg/dispute_missing_key_shares_test.go | 5 - .../dkg/dispute_missing_registration_test.go | 4 - ...dispute_missing_share_distribution_test.go | 6 - .../dkg/dispute_share_distribution_test.go | 24 +--- .../tasks/dkg/gpkj_submission_test.go | 17 +-- .../executor/tasks/dkg/mpk_submission_test.go | 15 +-- .../executor/tasks/dkg/register_test.go | 38 ++---- .../tasks/dkg/share_distribution_test.go | 24 +--- .../tasks/dkg/testutils/advance_phases.go | 21 +--- blockchain/testutils/setup.go | 110 +++++++++--------- 13 files changed, 91 insertions(+), 194 deletions(-) diff --git a/blockchain/executor/tasks/dkg/completion_test.go b/blockchain/executor/tasks/dkg/completion_test.go index eb301360..df672701 100644 --- a/blockchain/executor/tasks/dkg/completion_test.go +++ b/blockchain/executor/tasks/dkg/completion_test.go @@ -20,7 +20,7 @@ import ( func TestCompletion_Group_1_AllGood(t *testing.T) { n := 4 - err := testutils.RunScriptInitializeValidator(5) + err := testutils.RunScriptInit(5) assert.Nil(t, err) suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) diff --git a/blockchain/executor/tasks/dkg/dispute_gpkj_test.go b/blockchain/executor/tasks/dkg/dispute_gpkj_test.go index 6d6d2b3d..a3733cdc 100644 --- a/blockchain/executor/tasks/dkg/dispute_gpkj_test.go +++ b/blockchain/executor/tasks/dkg/dispute_gpkj_test.go @@ -41,7 +41,6 @@ func TestGPKjDispute_NoBadGPKj(t *testing.T) { err = gpkjSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, gpkjSubmissionTask.Success) // event @@ -74,7 +73,6 @@ func TestGPKjDispute_NoBadGPKj(t *testing.T) { err = disputeBadGPKjTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, disputeBadGPKjTask.Success) } @@ -120,7 +118,6 @@ func TestGPKjDispute_1Invalid(t *testing.T) { err = gpkjSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, gpkjSubmissionTask.Success) // event @@ -153,7 +150,6 @@ func TestGPKjDispute_1Invalid(t *testing.T) { err = disputeBadGPKjTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, disputeBadGPKjTask.Success) } @@ -187,7 +183,6 @@ func TestGPKjDispute_GoodMaliciousAccusation(t *testing.T) { err = gpkjSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, gpkjSubmissionTask.Success) // event @@ -225,7 +220,6 @@ func TestGPKjDispute_GoodMaliciousAccusation(t *testing.T) { err = disputeBadGPKjTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, disputeBadGPKjTask.Success) } diff --git a/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go b/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go index 19971c6f..38633312 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go @@ -36,8 +36,6 @@ func TestDisputeMissingGPKjTask_Group_1_FourUnsubmittedGPKj_DoWork_Success(t *te assert.Nil(t, err) err = gpkjSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - - eth.Commit() assert.True(t, gpkjSubmissionTask.Success) // event @@ -57,8 +55,6 @@ func TestDisputeMissingGPKjTask_Group_1_FourUnsubmittedGPKj_DoWork_Success(t *te assert.Nil(t, err) err = disputeMissingGPKjTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - - eth.Commit() assert.True(t, disputeMissingGPKjTask.Success) } @@ -90,8 +86,6 @@ func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_False(t *testing.T) { assert.Nil(t, err) err = gpkjSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - - eth.Commit() assert.True(t, gpkjSubmissionTask.Success) // event @@ -112,8 +106,6 @@ func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_False(t *testing.T) { assert.Nil(t, err) err = disputeMissingGPKjTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - - eth.Commit() assert.True(t, disputeMissingGPKjTask.Success) shouldRetry := disputeMissingGPKjTask.ShouldRetry(ctx, logger, eth) @@ -143,7 +135,6 @@ func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_True(t *testing.T) { err = gpkjSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, gpkjSubmissionTask.Success) // event @@ -186,7 +177,6 @@ func TestDisputeMissingGPKjTask_Group_2_ShouldAccuseOneValidatorWhoDidNotDistrib err = disputeMissingGPKjTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, disputeMissingGPKjTask.Success) // disputeGPKj @@ -197,7 +187,6 @@ func TestDisputeMissingGPKjTask_Group_2_ShouldAccuseOneValidatorWhoDidNotDistrib err = disputeGPKjTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, disputeGPKjTask.Success) } @@ -236,7 +225,6 @@ func TestDisputeMissingGPKjTask_Group_2_ShouldAccuseTwoValidatorWhoDidNotDistrib err = disputeMissingGPKjTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, disputeMissingGPKjTask.Success) // disputeGPKj @@ -247,7 +235,6 @@ func TestDisputeMissingGPKjTask_Group_2_ShouldAccuseTwoValidatorWhoDidNotDistrib err = disputeGPKjTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, disputeGPKjTask.Success) } diff --git a/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go b/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go index 493a1433..452e50dd 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go @@ -37,7 +37,6 @@ func TestDisputeMissingKeySharesTask_FourUnsubmittedKeyShare_DoWork_Success(t *t err = keyshareSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, keyshareSubmissionTask.Success) // event @@ -65,7 +64,6 @@ func TestDisputeMissingKeySharesTask_FourUnsubmittedKeyShare_DoWork_Success(t *t err = disputeMissingKeyshareTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, disputeMissingKeyshareTask.Success) } @@ -99,7 +97,6 @@ func TestDisputeMissingKeySharesTask_ShouldRetry_False(t *testing.T) { err = keyshareSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, keyshareSubmissionTask.Success) // event @@ -129,7 +126,6 @@ func TestDisputeMissingKeySharesTask_ShouldRetry_False(t *testing.T) { assert.True(t, disputeMissingKeyshareTask.Success) - eth.Commit() shouldRetry := disputeMissingKeyshareTask.ShouldRetry(ctx, logger, eth) assert.False(t, shouldRetry) } @@ -158,7 +154,6 @@ func TestDisputeMissingKeySharesTask_ShouldRetry_True(t *testing.T) { err = keyshareSubmissionTask.DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, keyshareSubmissionTask.Success) // event diff --git a/blockchain/executor/tasks/dkg/dispute_missing_registration_test.go b/blockchain/executor/tasks/dkg/dispute_missing_registration_test.go index 5cae1276..c6df0e28 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_registration_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_registration_test.go @@ -30,7 +30,6 @@ func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessOneParticipantAccus err = suite.DispMissingRegTasks[idx].DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, suite.DispMissingRegTasks[idx].Success) } @@ -60,7 +59,6 @@ func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessThreeParticipantAcc err = suite.DispMissingRegTasks[idx].DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, suite.DispMissingRegTasks[idx].Success) } @@ -90,7 +88,6 @@ func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessAllParticipantsAreB err = suite.DispMissingRegTasks[idx].DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, suite.DispMissingRegTasks[idx].Success) } @@ -136,7 +133,6 @@ func TestDisputeMissingRegistrationTask_Group_2_ShouldNotRetryAfterSuccessfullyA err = suite.DispMissingRegTasks[idx].DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, suite.DispMissingRegTasks[idx].Success) } diff --git a/blockchain/executor/tasks/dkg/dispute_missing_share_distribution_test.go b/blockchain/executor/tasks/dkg/dispute_missing_share_distribution_test.go index ca5ea1c0..12089ebc 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_share_distribution_test.go @@ -28,7 +28,6 @@ func TestDisputeMissingShareDistributionTask_Group_1_ShouldAccuseOneValidatorWho err = task.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, task.Success) } @@ -55,7 +54,6 @@ func TestDisputeMissingShareDistributionTask_Group_1_ShouldAccuseAllValidatorsWh err = task.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, task.Success) } @@ -94,7 +92,6 @@ func TestDisputeMissingShareDistributionTask_Group_1_ShouldNotAccuseValidatorsWh assert.Nil(t, err) } - suite.Eth.Commit() if idx == n-1 { assert.False(t, task.Success) } else { @@ -143,7 +140,6 @@ func TestDisputeMissingShareDistributionTask_Group_2_DisputeMissingShareDistribu err = task.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, task.Success) } @@ -172,7 +168,6 @@ func TestDisputeMissingShareDistributionTask_Group_2_ShouldAccuseOneValidatorWho err = disputeMissingShareDistTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, disputeMissingShareDistTask.Success) // disputeShareDist @@ -183,7 +178,6 @@ func TestDisputeMissingShareDistributionTask_Group_2_ShouldAccuseOneValidatorWho err = disputeShareDistTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, disputeShareDistTask.Success) } diff --git a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go index c52bbe8b..ccd05f14 100644 --- a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go @@ -4,15 +4,13 @@ package dkg_test import ( "context" - "math/big" - "testing" - "time" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg" dkgState "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/monitor/events" "github.com/MadBase/MadNet/blockchain/testutils" + "math/big" + "testing" "github.com/MadBase/MadNet/logging" "github.com/ethereum/go-ethereum/common" @@ -47,7 +45,6 @@ func TestDisputeShareDistributionTask_Group_1_GoodAllValid(t *testing.T) { err = task.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, task.Success) } @@ -99,7 +96,6 @@ func TestDisputeShareDistributionTask_Group_1_GoodMaliciousShare(t *testing.T) { err = task.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, task.Success) // event @@ -132,7 +128,6 @@ func TestDisputeShareDistributionTask_Group_1_GoodMaliciousShare(t *testing.T) { err = disputeShareDistributionTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, disputeShareDistributionTask.Success) } @@ -156,11 +151,10 @@ func TestDisputeShareDistributionTask_Group_1_GoodMaliciousShare(t *testing.T) { // This test is meant to raise an error resulting from an invalid argument // for the Ethereum interface. func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -183,11 +177,9 @@ func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { // this should raise an error resulting from not successfully completing // ShareDistribution phase. func TestDisputeShareDistributionTask_Group_2_Bad2(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() accts := eth.GetKnownAccounts() @@ -235,7 +227,6 @@ func TestDisputeShareDistributionTask_Group_2_DoRetry_returnsFalse(t *testing.T) err = task.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, task.Success) } @@ -280,14 +271,9 @@ func TestDisputeShareDistributionTask_Group_2_DoRetry_returnsTrue(t *testing.T) err = task.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, task.Success) } - suite.Eth.Commit() - suite.Eth.Commit() - suite.Eth.Commit() - // Do Should Retry for idx := 0; idx < n; idx++ { task := suite.DisputeShareDistTasks[idx] diff --git a/blockchain/executor/tasks/dkg/gpkj_submission_test.go b/blockchain/executor/tasks/dkg/gpkj_submission_test.go index 508a4d71..2ac3993a 100644 --- a/blockchain/executor/tasks/dkg/gpkj_submission_test.go +++ b/blockchain/executor/tasks/dkg/gpkj_submission_test.go @@ -4,15 +4,13 @@ package dkg_test import ( "context" - "math/big" - "testing" - "time" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg" dkgState "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/monitor/interfaces" "github.com/MadBase/MadNet/blockchain/testutils" + "math/big" + "testing" "github.com/MadBase/MadNet/logging" "github.com/sirupsen/logrus" @@ -38,7 +36,6 @@ func TestGPKjSubmission_Group_1_GoodAllValid(t *testing.T) { err = tasksVec[idx].DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, tasksVec[idx].Success) } @@ -61,11 +58,9 @@ func TestGPKjSubmission_Group_1_GoodAllValid(t *testing.T) { // Here, we submit nil for the state interface; // this should raise an error. func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -87,11 +82,9 @@ func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { // Here, we should raise an error because we did not successfully complete // the key share submission phase. func TestGPKjSubmission_Group_1_Bad2(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -141,7 +134,6 @@ func TestGPKjSubmission_Group_2_Bad3(t *testing.T) { err := tasksVec[idx].Initialize(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() } // Do GPKj Submission task; this will fail because invalid submission; @@ -173,7 +165,6 @@ func TestGPKjSubmission_Group_2_ShouldRetry_returnsFalse(t *testing.T) { err = tasksVec[idx].DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, tasksVec[idx].Success) shouldRetry := tasksVec[idx].ShouldRetry(ctx, logger, eth) diff --git a/blockchain/executor/tasks/dkg/mpk_submission_test.go b/blockchain/executor/tasks/dkg/mpk_submission_test.go index 2942d2c2..5e1cf933 100644 --- a/blockchain/executor/tasks/dkg/mpk_submission_test.go +++ b/blockchain/executor/tasks/dkg/mpk_submission_test.go @@ -4,14 +4,12 @@ package dkg_test import ( "context" - "math/big" - "testing" - "time" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg" dkgState "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/testutils" + "math/big" + "testing" "github.com/MadBase/MadNet/logging" "github.com/sirupsen/logrus" @@ -98,7 +96,6 @@ func TestMPKSubmission_Group_1_Bad1(t *testing.T) { task := suite.MpkSubmissionTasks[0] err := task.Initialize(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() // Advance to gpkj submission phase; note we did *not* submit MPK testutils.AdvanceTo(t, eth, task.Start+dkgStates[0].PhaseLength) @@ -112,11 +109,9 @@ func TestMPKSubmission_Group_1_Bad1(t *testing.T) { // We force an error. // This is caused by submitting invalid state information (state is nil). func TestMPKSubmission_Group_1_Bad2(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -137,11 +132,9 @@ func TestMPKSubmission_Group_1_Bad2(t *testing.T) { // This is caused by submitting invalid state information by not successfully // completing KeyShareSubmission phase. func TestMPKSubmission_Group_2_Bad4(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/register_test.go b/blockchain/executor/tasks/dkg/register_test.go index 56a2db77..9cd95ab0 100644 --- a/blockchain/executor/tasks/dkg/register_test.go +++ b/blockchain/executor/tasks/dkg/register_test.go @@ -22,16 +22,13 @@ import ( ) func TestRegisterTask_Group_1_Task(t *testing.T) { - n := 5 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) - //This shouldn't be needed //tr := &objects.TypeRegistry{} //tr.RegisterInstanceType(&RegisterTask{}) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 500*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -108,9 +105,8 @@ func TestRegisterTask_Group_1_Task(t *testing.T) { // This test calls Initialize and DoWork. func TestRegisterTask_Group_1_Good2(t *testing.T) { n := 6 - ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + _, accounts := testutils.InitializePrivateKeysAndAccounts(n) + eth := testutils.GetEthereumNetwork(t, false) assert.NotNil(t, eth) defer eth.Close() @@ -170,7 +166,6 @@ func TestRegisterTask_Group_1_Good2(t *testing.T) { err = tasksVec[idx].DoWork(ctx, logger, eth) assert.Nil(t, err) - eth.Commit() assert.True(t, tasksVec[idx].Success) } @@ -197,9 +192,8 @@ func TestRegisterTask_Group_1_Good2(t *testing.T) { // This should raise an error and not allow that participant to proceed. func TestRegisterTask_Group_1_Bad1(t *testing.T) { n := 5 - ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + _, accounts := testutils.InitializePrivateKeysAndAccounts(n) + eth := testutils.GetEthereumNetwork(t, false) assert.NotNil(t, eth) defer eth.Close() @@ -257,9 +251,8 @@ func TestRegisterTask_Group_1_Bad1(t *testing.T) { // This should raise an error and not allow that participant to proceed. func TestRegisterTask_Group_2_Bad2(t *testing.T) { n := 7 - ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + _, accounts := testutils.InitializePrivateKeysAndAccounts(n) + eth := testutils.GetEthereumNetwork(t, false) assert.NotNil(t, eth) defer eth.Close() @@ -313,10 +306,7 @@ func TestRegisterTask_Group_2_Bad2(t *testing.T) { // The initialization should fail because we dont allow less than 4 validators func TestRegisterTask_Group_2_Bad4(t *testing.T) { - n := 3 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) - - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) assert.NotNil(t, eth) defer eth.Close() @@ -324,8 +314,6 @@ func TestRegisterTask_Group_2_Bad4(t *testing.T) { accounts := eth.GetKnownAccounts() owner := accounts[0] - err := eth.UnlockAccount(owner) - assert.Nil(t, err) // Start EthDKG ownerOpts, err := eth.GetTransactionOpts(ctx, owner) @@ -345,9 +333,8 @@ func TestRegisterTask_Group_2_Bad4(t *testing.T) { // This should raise an error. func TestRegisterTask_Group_2_Bad5(t *testing.T) { n := 5 - ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + _, accounts := testutils.InitializePrivateKeysAndAccounts(n) + eth := testutils.GetEthereumNetwork(t, false) assert.NotNil(t, eth) defer eth.Close() @@ -412,7 +399,7 @@ func TestRegisterTask_Group_3_ShouldRetryFalse(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 500*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -481,7 +468,6 @@ func TestRegisterTask_Group_3_ShouldRetryFalse(t *testing.T) { err = registrationTask.DoWork(ctx, log, eth) assert.Nil(t, err) - eth.Commit() retry := registrationTask.ShouldRetry(ctx, log, eth) assert.False(t, retry) } @@ -500,7 +486,7 @@ func TestRegisterTask_Group_3_ShouldRetryTrue(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 500*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/share_distribution_test.go b/blockchain/executor/tasks/dkg/share_distribution_test.go index 3ee85114..61ef33fc 100644 --- a/blockchain/executor/tasks/dkg/share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/share_distribution_test.go @@ -4,10 +4,6 @@ package dkg_test import ( "context" - "math/big" - "testing" - "time" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg" "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" @@ -16,6 +12,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "math/big" + "testing" ) //Here we test the happy path. @@ -37,7 +35,6 @@ func TestShareDistribution_Group_1_Good(t *testing.T) { err = shareDistributionTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, shareDistributionTask.Success) } @@ -87,8 +84,6 @@ func TestShareDistribution_Group_1_Bad1(t *testing.T) { task.DoWork(ctx, logger, suite.Eth) - suite.Eth.Commit() - // The last task should have failed if idx == badIdx { assert.False(t, task.Success) @@ -151,8 +146,6 @@ func TestShareDistribution_Group_1_Bad2(t *testing.T) { task.DoWork(ctx, logger, suite.Eth) - suite.Eth.Commit() - // The last task should have failed if idx == badIdx { assert.False(t, task.Success) @@ -217,8 +210,6 @@ func TestShareDistribution_Group_2_Bad4(t *testing.T) { task.DoWork(ctx, logger, eth) - eth.Commit() - // The last task should have failed if idx == badCommitmentIdx { assert.False(t, task.Success) @@ -269,8 +260,6 @@ func TestShareDistribution_Group_2_Bad5(t *testing.T) { task.DoWork(ctx, logger, eth) - eth.Commit() - // The last task should have failed if idx == badShareIdx { assert.False(t, task.Success) @@ -292,11 +281,9 @@ func TestShareDistribution_Group_2_Bad5(t *testing.T) { // We begin by submitting invalid information; // we submit nil state information func TestShareDistribution_Group_2_Bad6(t *testing.T) { - n := 5 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -316,11 +303,9 @@ func TestShareDistribution_Group_2_Bad6(t *testing.T) { // We test to ensure that everything behaves correctly. // We submit invalid state information (again). func TestShareDistribution_Group_3_Bad7(t *testing.T) { - n := 4 - ecdsaPrivateKeys, _ := testutils.InitializePrivateKeysAndAccounts(n) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -379,7 +364,6 @@ func TestShareDistribution_Group_3_ShouldRetryFalse(t *testing.T) { err = task.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - suite.Eth.Commit() assert.True(t, task.Success) shouldRetry := task.ShouldRetry(ctx, logger, suite.Eth) diff --git a/blockchain/executor/tasks/dkg/testutils/advance_phases.go b/blockchain/executor/tasks/dkg/testutils/advance_phases.go index 4bb1be20..bad9efae 100644 --- a/blockchain/executor/tasks/dkg/testutils/advance_phases.go +++ b/blockchain/executor/tasks/dkg/testutils/advance_phases.go @@ -6,11 +6,6 @@ import ( "context" "crypto/ecdsa" "errors" - "math/big" - "strings" - "testing" - "time" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg" "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/utils" @@ -21,6 +16,9 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/core/types" + "math/big" + "strings" + "testing" "github.com/MadBase/MadNet/crypto/bn256" "github.com/MadBase/MadNet/crypto/bn256/cloudflare" @@ -116,7 +114,8 @@ func InitializeETHDKG(eth ethereum.Network, callOpts *bind.TransactOpts, ctx con func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators int, phaseLength uint16) *TestSuite { ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 1000*time.Millisecond) + //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 1000*time.Millisecond) + eth := testutils.GetEthereumNetwork(t, false) assert.NotNil(t, eth) ctx := context.Background() @@ -186,8 +185,6 @@ func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators err = regTasks[idx].DoWork(ctx, logger, eth) assert.Nil(t, err) - - eth.Commit() assert.True(t, regTasks[idx].Success) } @@ -286,8 +283,6 @@ func StartFromShareDistributionPhase(t *testing.T, n int, undistributedSharesIdx err = shareDistTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - - suite.Eth.Commit() assert.True(t, shareDistTask.Success) // event @@ -362,8 +357,6 @@ func StartFromKeyShareSubmissionPhase(t *testing.T, n int, undistributedShares i err = keyshareSubmissionTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - - suite.Eth.Commit() assert.True(t, keyshareSubmissionTask.Success) // event @@ -427,8 +420,6 @@ func StartFromMPKSubmissionPhase(t *testing.T, n int, phaseLength uint16) *TestS } } - eth.Commit() - height, err := suite.Eth.GetCurrentHeight(ctx) assert.Nil(t, err) @@ -495,8 +486,6 @@ func StartFromGPKjPhase(t *testing.T, n int, undistributedGPKjIdx []int, badGPKj err = gpkjSubTask.DoWork(ctx, logger, suite.Eth) assert.Nil(t, err) - - suite.Eth.Commit() assert.True(t, gpkjSubTask.Success) // event diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 39f596c1..64fdecc4 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -1,7 +1,6 @@ package testutils import ( - "bufio" "bytes" "context" "crypto/ecdsa" @@ -36,16 +35,30 @@ var ( scriptRegisterValidator = "register_test" scriptStartHardHatNode = "hardhat_node" scriptInit = "init" - hardHatProcessId = "HARDHAT_PROCESS_ID" + scriptClean = "clean" + + envHardHatProcessId = "HARDHAT_PROCESS_ID" + + configEndpoint = "http://localhost:8545" + configDefaultAccount = "0x546f99f244b7b58b855330ae0e2bc1b30b41302f" + configFactoryAddress = "0x0b1f9c2b7bed6db83295c7b5158e3806d67ec5bc" + configFinalityDelay = uint64(1) ) func getEthereumDetails() (*ethereum.Details, error) { + + getProjectRootPath() + rootPath := getProjectRootPath() + + assetKey := append(rootPath, "assets", "test", "keys") + assetPasscode := append(rootPath, "assets", "test", "passcodes.txt") + details, err := ethereum.NewEndpoint( - getConfigurationProperty("endpoint"), - "../../assets/test/keys", - "../../assets/test/passcodes.txt", - "0x546F99F244b7B58B855330AE0E2BC1b30b41302F", - 1, + configEndpoint, + filepath.Join(filepath.Join(assetKey...)), + filepath.Join(filepath.Join(assetPasscode...)), + configDefaultAccount, + configFinalityDelay, 500, 0, ) @@ -82,7 +95,7 @@ func waitForHardHatNode(ctx context.Context) error { case <-time.After(time.Second): body := bytes.NewReader(buff.Bytes()) _, err := c.Post( - getConfigurationProperty("endpoint"), + configEndpoint, "application/json", body, ) @@ -97,7 +110,7 @@ func waitForHardHatNode(ctx context.Context) error { func isHardHatRunning() (bool, error) { var client = http.Client{Timeout: 2 * time.Second} - resp, err := client.Head(getConfigurationProperty("endpoint")) + resp, err := client.Head(configEndpoint) if err != nil { return false, err } @@ -124,7 +137,7 @@ func startHardHat(t *testing.T, ctx context.Context) *ethereum.Details { assert.NotNilf(t, details, "Ethereum network should not be Nil") log.Printf("Deploying contracts ...") - err = runScriptDeployContracts(details, ctx, getConfigurationProperty("factoryAddress")) + err = runScriptDeployContracts(details, ctx) if err != nil { details.Close() assert.Nilf(t, err, "Error deploying contracts: %v") @@ -162,7 +175,7 @@ func stopHardHat() error { return nil } - pid, _ := strconv.Atoi(os.Getenv(hardHatProcessId)) + pid, _ := strconv.Atoi(os.Getenv(envHardHatProcessId)) process, err := os.FindProcess(pid) if err != nil { log.Printf("Error finding HardHat pid: %v", err) @@ -275,48 +288,11 @@ func InitializePrivateKeysAndAccounts(n int) ([]*ecdsa.PrivateKey, []accounts.Ac return privateKeys, accounts } -// GetConfigurationKeyValueFromFile given a file following the pattern `key = value`, it returns the value of `key` -func GetConfigurationKeyValueFromFile(filePath string, key string) (string, error) { - rootPath := getProjectRootPath() - rootPath = append(rootPath, filePath) - fileFullPath := filepath.Join(rootPath...) - - f, err := os.Open(fileFullPath) - if err != nil { - return "", err - } - defer f.Close() - - scanner := bufio.NewScanner(f) - var value string - for scanner.Scan() { - if strings.Contains(scanner.Text(), key) { - value = scanner.Text() - break - } - } - - splits := strings.Split(value, "=") - return strings.Trim(splits[1], " \""), nil -} - -func getConfigurationProperty(key string) string { - value, err := GetConfigurationKeyValueFromFile("scripts/base-files/baseConfig", key) - if err != nil { - return "" - } - return value -} - func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { rootPath := getProjectRootPath() // Account - acctAddress, err := GetConfigurationKeyValueFromFile("scripts/base-files/owner.toml", "defaultAccount") - if err != nil { - log.Printf("Error retrieving default account address. %v", err) - return nil, nil, err - } + acctAddress := configDefaultAccount acctAddressLowerCase := strings.ToLower(acctAddress) // Password @@ -401,7 +377,7 @@ func runScriptHardHatNode() error { return err } - err = os.Setenv(hardHatProcessId, strconv.Itoa(cmd.Process.Pid)) + err = os.Setenv(envHardHatProcessId, strconv.Itoa(cmd.Process.Pid)) if err != nil { log.Printf("Error setting environment variable: %v", err) return err @@ -410,7 +386,12 @@ func runScriptHardHatNode() error { return nil } -func RunScriptInitializeValidator(n int) error { +func RunScriptInit(n int) error { + + err := runScriptClean() + if err != nil { + return err + } rootPath := getProjectRootPath() scriptPath := getMainScriptPath() @@ -422,7 +403,7 @@ func RunScriptInitializeValidator(n int) error { } setCommandStdOut(&cmd) - err := cmd.Start() + err = cmd.Start() if err != nil { log.Printf("Could not execute %s script: %v", scriptInit, err) return err @@ -431,7 +412,28 @@ func RunScriptInitializeValidator(n int) error { return nil } -func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context, factoryAddress string) error { +func runScriptClean() error { + + rootPath := getProjectRootPath() + scriptPath := getMainScriptPath() + + cmd := exec.Cmd{ + Path: scriptPath, + Args: []string{scriptPath, scriptClean}, + Dir: filepath.Join(rootPath...), + } + + setCommandStdOut(&cmd) + err := cmd.Start() + if err != nil { + log.Printf("Could not execute %s script: %v", scriptClean, err) + return err + } + + return nil +} + +func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context) error { rootPath := getProjectRootPath() scriptPath := getMainScriptPath() @@ -456,7 +458,7 @@ func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context, factor } addr := common.Address{} - copy(addr[:], common.FromHex(factoryAddress)) + copy(addr[:], common.FromHex(configFactoryAddress)) eth.Contracts().Initialize(ctx, addr) return nil From 96a873147c3af8d841cb3c2e26b89ac2a23e0771 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Fri, 10 Jun 2022 11:43:35 +0200 Subject: [PATCH 04/15] Integrate tx watcher --- .../executor/tasks/dkg/completion_test.go | 18 +++--- .../executor/tasks/dkg/dispute_gpkj_test.go | 6 +- .../tasks/dkg/dispute_missing_gpkj_test.go | 6 +- .../dkg/dispute_missing_key_shares_test.go | 12 ++-- .../dkg/dispute_share_distribution_test.go | 6 +- .../tasks/dkg/gpkj_submission_test.go | 4 +- .../executor/tasks/dkg/mpk_submission_test.go | 6 +- .../executor/tasks/dkg/register_test.go | 18 +++--- .../tasks/dkg/share_distribution_test.go | 4 +- .../tasks/dkg/testutils/advance_phases.go | 28 +++++----- blockchain/testutils/setup.go | 55 +++++++++---------- blockchain/transaction/transaction.go | 46 ++++++++-------- blockchain/transaction/transaction_test.go | 2 +- 13 files changed, 102 insertions(+), 109 deletions(-) diff --git a/blockchain/executor/tasks/dkg/completion_test.go b/blockchain/executor/tasks/dkg/completion_test.go index df672701..07fb6f47 100644 --- a/blockchain/executor/tasks/dkg/completion_test.go +++ b/blockchain/executor/tasks/dkg/completion_test.go @@ -9,18 +9,16 @@ import ( dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/monitor/events" "github.com/MadBase/MadNet/blockchain/testutils" - "testing" - "github.com/MadBase/MadNet/logging" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "testing" ) // We complete everything correctly, happy path func TestCompletion_Group_1_AllGood(t *testing.T) { n := 4 - - err := testutils.RunScriptInit(5) + err := testutils.RunScriptInit(n) assert.Nil(t, err) suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) @@ -56,7 +54,7 @@ func TestCompletion_Group_1_AllGood(t *testing.T) { } // Advance to Completion phase - testutils.AdvanceTo(t, eth, completionStart) + testutils.AdvanceTo(eth, completionStart) for idx := 0; idx < n; idx++ { state := dkgStates[idx] @@ -133,7 +131,7 @@ func TestCompletion_Group_1_StartFromCompletion(t *testing.T) { func TestCompletion_Group_2_Bad1(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -154,7 +152,7 @@ func TestCompletion_Group_2_Bad1(t *testing.T) { func TestCompletion_Group_2_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -208,10 +206,10 @@ func TestCompletion_Group_2_Bad3(t *testing.T) { } // Advance to Completion phase - testutils.AdvanceTo(t, eth, completionStart) + testutils.AdvanceTo(eth, completionStart) // Advance to end of Completion phase - testutils.AdvanceTo(t, eth, completionEnd) + testutils.AdvanceTo(eth, completionEnd) err = completionTasks[0].Initialize(ctx, logger, eth) if err != nil { @@ -294,7 +292,7 @@ func TestCompletion_Group_3_ShouldRetry_returnsTrue(t *testing.T) { } // Advance to Completion phase - testutils.AdvanceTo(t, eth, completionStart) + testutils.AdvanceTo(eth, completionStart) err = completionTasks[0].Initialize(ctx, logger, eth) assert.Nil(t, err) diff --git a/blockchain/executor/tasks/dkg/dispute_gpkj_test.go b/blockchain/executor/tasks/dkg/dispute_gpkj_test.go index a3733cdc..48f436cd 100644 --- a/blockchain/executor/tasks/dkg/dispute_gpkj_test.go +++ b/blockchain/executor/tasks/dkg/dispute_gpkj_test.go @@ -60,7 +60,7 @@ func TestGPKjDispute_NoBadGPKj(t *testing.T) { assert.Nil(t, err) disputePhaseAt := currentHeight + suite.DKGStates[0].ConfirmationLength - testutils.AdvanceTo(t, eth, disputePhaseAt) + testutils.AdvanceTo(eth, disputePhaseAt) // Do dispute bad gpkj task for idx := 0; idx < n; idx++ { @@ -137,7 +137,7 @@ func TestGPKjDispute_1Invalid(t *testing.T) { assert.Nil(t, err) disputePhaseAt := currentHeight + suite.DKGStates[0].ConfirmationLength - testutils.AdvanceTo(t, eth, disputePhaseAt) + testutils.AdvanceTo(eth, disputePhaseAt) // Do dispute bad gpkj task for idx := 0; idx < n; idx++ { @@ -202,7 +202,7 @@ func TestGPKjDispute_GoodMaliciousAccusation(t *testing.T) { assert.Nil(t, err) disputePhaseAt := currentHeight + suite.DKGStates[0].ConfirmationLength - testutils.AdvanceTo(t, eth, disputePhaseAt) + testutils.AdvanceTo(eth, disputePhaseAt) // Do dispute bad gpkj task badAccuserIdx := 0 diff --git a/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go b/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go index 38633312..6d84be0b 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go @@ -45,7 +45,7 @@ func TestDisputeMissingGPKjTask_Group_1_FourUnsubmittedGPKj_DoWork_Success(t *te } } - testutils.AdvanceTo(t, eth, disputeGPKjStartBlock) + testutils.AdvanceTo(eth, disputeGPKjStartBlock) // Do dispute missing gpkj task for idx := 0; idx < n; idx++ { @@ -96,7 +96,7 @@ func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_False(t *testing.T) { } nextPhaseAt := currentHeight + dkgStates[0].PhaseLength - testutils.AdvanceTo(t, eth, nextPhaseAt) + testutils.AdvanceTo(eth, nextPhaseAt) // Do dispute missing gpkj task for idx := 0; idx < n; idx++ { @@ -145,7 +145,7 @@ func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_True(t *testing.T) { } nextPhaseAt := currentHeight + dkgStates[0].PhaseLength - testutils.AdvanceTo(t, eth, nextPhaseAt) + testutils.AdvanceTo(eth, nextPhaseAt) // Do dispute missing gpkj task for idx := 0; idx < n; idx++ { diff --git a/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go b/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go index 452e50dd..73b1de02 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go @@ -25,7 +25,7 @@ func TestDisputeMissingKeySharesTask_FourUnsubmittedKeyShare_DoWork_Success(t *t logger := logging.GetLogger("test").WithField("Validator", "") // skip DisputeShareDistribution and move to KeyShareSubmission phase - testutils.AdvanceTo(t, eth, suite.KeyshareSubmissionTasks[0].Start) + testutils.AdvanceTo(eth, suite.KeyshareSubmissionTasks[0].Start) // Do key share submission task for idx := 0; idx < n-unsubmittedKeyShares; idx++ { @@ -53,7 +53,7 @@ func TestDisputeMissingKeySharesTask_FourUnsubmittedKeyShare_DoWork_Success(t *t // advance into the end of KeyShareSubmission phase, // which is the start of DisputeMissingKeyShares phase - testutils.AdvanceTo(t, eth, suite.KeyshareSubmissionTasks[0].End) + testutils.AdvanceTo(eth, suite.KeyshareSubmissionTasks[0].End) // Do dispute missing key share task for idx := 0; idx < n; idx++ { @@ -85,7 +85,7 @@ func TestDisputeMissingKeySharesTask_ShouldRetry_False(t *testing.T) { logger := logging.GetLogger("test").WithField("Validator", "") // skip DisputeShareDistribution and move to KeyShareSubmission phase - testutils.AdvanceTo(t, eth, suite.KeyshareSubmissionTasks[0].Start) + testutils.AdvanceTo(eth, suite.KeyshareSubmissionTasks[0].Start) // Do key share submission task for idx := 0; idx < n-unsubmittedKeyShares; idx++ { @@ -113,7 +113,7 @@ func TestDisputeMissingKeySharesTask_ShouldRetry_False(t *testing.T) { // advance into the end of KeyShareSubmission phase, // which is the start of DisputeMissingKeyShares phase - testutils.AdvanceTo(t, eth, suite.KeyshareSubmissionTasks[0].End) + testutils.AdvanceTo(eth, suite.KeyshareSubmissionTasks[0].End) // Do dispute missing key share task for idx := 0; idx < n; idx++ { @@ -142,7 +142,7 @@ func TestDisputeMissingKeySharesTask_ShouldRetry_True(t *testing.T) { logger := logging.GetLogger("test").WithField("Validator", "") // skip DisputeShareDistribution and move to KeyShareSubmission phase - testutils.AdvanceTo(t, eth, suite.KeyshareSubmissionTasks[0].Start) + testutils.AdvanceTo(eth, suite.KeyshareSubmissionTasks[0].Start) // Do key share submission task for idx := 0; idx < n-unsubmittedKeyShares; idx++ { @@ -170,7 +170,7 @@ func TestDisputeMissingKeySharesTask_ShouldRetry_True(t *testing.T) { // advance into the end of KeyShareSubmission phase, // which is the start of DisputeMissingKeyShares phase - testutils.AdvanceTo(t, eth, suite.KeyshareSubmissionTasks[0].End) + testutils.AdvanceTo(eth, suite.KeyshareSubmissionTasks[0].End) // Do dispute missing key share task for idx := 0; idx < n; idx++ { diff --git a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go index ccd05f14..e3952f3b 100644 --- a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go @@ -114,7 +114,7 @@ func TestDisputeShareDistributionTask_Group_1_GoodMaliciousShare(t *testing.T) { currentHeight, err := suite.Eth.GetCurrentHeight(ctx) assert.Nil(t, err) nextPhaseAt := currentHeight + suite.DKGStates[0].ConfirmationLength - testutils.AdvanceTo(t, suite.Eth, nextPhaseAt) + testutils.AdvanceTo(suite.Eth, nextPhaseAt) // Do Share Dispute task for idx := 0; idx < n; idx++ { @@ -154,7 +154,7 @@ func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -179,7 +179,7 @@ func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { func TestDisputeShareDistributionTask_Group_2_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() accts := eth.GetKnownAccounts() diff --git a/blockchain/executor/tasks/dkg/gpkj_submission_test.go b/blockchain/executor/tasks/dkg/gpkj_submission_test.go index 2ac3993a..d6408880 100644 --- a/blockchain/executor/tasks/dkg/gpkj_submission_test.go +++ b/blockchain/executor/tasks/dkg/gpkj_submission_test.go @@ -60,7 +60,7 @@ func TestGPKjSubmission_Group_1_GoodAllValid(t *testing.T) { func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -84,7 +84,7 @@ func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { func TestGPKjSubmission_Group_1_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/mpk_submission_test.go b/blockchain/executor/tasks/dkg/mpk_submission_test.go index 5e1cf933..1fce188b 100644 --- a/blockchain/executor/tasks/dkg/mpk_submission_test.go +++ b/blockchain/executor/tasks/dkg/mpk_submission_test.go @@ -98,7 +98,7 @@ func TestMPKSubmission_Group_1_Bad1(t *testing.T) { assert.Nil(t, err) // Advance to gpkj submission phase; note we did *not* submit MPK - testutils.AdvanceTo(t, eth, task.Start+dkgStates[0].PhaseLength) + testutils.AdvanceTo(eth, task.Start+dkgStates[0].PhaseLength) // Do MPK Submission task @@ -111,7 +111,7 @@ func TestMPKSubmission_Group_1_Bad1(t *testing.T) { func TestMPKSubmission_Group_1_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -134,7 +134,7 @@ func TestMPKSubmission_Group_1_Bad2(t *testing.T) { func TestMPKSubmission_Group_2_Bad4(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/register_test.go b/blockchain/executor/tasks/dkg/register_test.go index 9cd95ab0..313d1901 100644 --- a/blockchain/executor/tasks/dkg/register_test.go +++ b/blockchain/executor/tasks/dkg/register_test.go @@ -28,7 +28,7 @@ func TestRegisterTask_Group_1_Task(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 5) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -106,7 +106,7 @@ func TestRegisterTask_Group_1_Task(t *testing.T) { func TestRegisterTask_Group_1_Good2(t *testing.T) { n := 6 _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 6) assert.NotNil(t, eth) defer eth.Close() @@ -193,7 +193,7 @@ func TestRegisterTask_Group_1_Good2(t *testing.T) { func TestRegisterTask_Group_1_Bad1(t *testing.T) { n := 5 _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 5) assert.NotNil(t, eth) defer eth.Close() @@ -252,7 +252,7 @@ func TestRegisterTask_Group_1_Bad1(t *testing.T) { func TestRegisterTask_Group_2_Bad2(t *testing.T) { n := 7 _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 7) assert.NotNil(t, eth) defer eth.Close() @@ -306,7 +306,7 @@ func TestRegisterTask_Group_2_Bad2(t *testing.T) { // The initialization should fail because we dont allow less than 4 validators func TestRegisterTask_Group_2_Bad4(t *testing.T) { - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 3) assert.NotNil(t, eth) defer eth.Close() @@ -334,7 +334,7 @@ func TestRegisterTask_Group_2_Bad4(t *testing.T) { func TestRegisterTask_Group_2_Bad5(t *testing.T) { n := 5 _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 5) assert.NotNil(t, eth) defer eth.Close() @@ -356,7 +356,7 @@ func TestRegisterTask_Group_2_Bad5(t *testing.T) { assert.NotNil(t, event) // Do share distribution; afterward, we confirm who is valid and who is not - testutils.AdvanceTo(t, eth, event.StartBlock.Uint64()+event.PhaseLength.Uint64()) + testutils.AdvanceTo(eth, event.StartBlock.Uint64()+event.PhaseLength.Uint64()) // get validator addresses ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) @@ -399,7 +399,7 @@ func TestRegisterTask_Group_3_ShouldRetryFalse(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 5) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -486,7 +486,7 @@ func TestRegisterTask_Group_3_ShouldRetryTrue(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 5) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/share_distribution_test.go b/blockchain/executor/tasks/dkg/share_distribution_test.go index 61ef33fc..17577d2c 100644 --- a/blockchain/executor/tasks/dkg/share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/share_distribution_test.go @@ -283,7 +283,7 @@ func TestShareDistribution_Group_2_Bad5(t *testing.T) { func TestShareDistribution_Group_2_Bad6(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 5) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -305,7 +305,7 @@ func TestShareDistribution_Group_2_Bad6(t *testing.T) { func TestShareDistribution_Group_3_Bad7(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/testutils/advance_phases.go b/blockchain/executor/tasks/dkg/testutils/advance_phases.go index bad9efae..7afee90f 100644 --- a/blockchain/executor/tasks/dkg/testutils/advance_phases.go +++ b/blockchain/executor/tasks/dkg/testutils/advance_phases.go @@ -13,6 +13,8 @@ import ( "github.com/MadBase/MadNet/blockchain/testutils" "github.com/MadBase/MadNet/blockchain/transaction" "github.com/MadBase/MadNet/bridge/bindings" + "github.com/MadBase/MadNet/crypto/bn256" + "github.com/MadBase/MadNet/crypto/bn256/cloudflare" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/core/types" @@ -20,9 +22,6 @@ import ( "strings" "testing" - "github.com/MadBase/MadNet/crypto/bn256" - "github.com/MadBase/MadNet/crypto/bn256/cloudflare" - "github.com/MadBase/MadNet/blockchain/ethereum" "github.com/MadBase/MadNet/logging" "github.com/stretchr/testify/assert" @@ -75,7 +74,6 @@ func SetETHDKGPhaseLength(length uint16, eth ethereum.Network, callOpts *bind.Tr if rcpt == nil { return nil, nil, errors.New("non existent receipt for tx ContractFactory.CallAny(ethdkg, setPhaseLength(...))") } - return txn, rcpt, nil } @@ -115,7 +113,7 @@ func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 1000*time.Millisecond) - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, n) assert.NotNil(t, eth) ctx := context.Background() @@ -218,10 +216,10 @@ func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators } // skip all the way to ShareDistribution phase - testutils.AdvanceTo(t, eth, shareDistributionTasks[0].Start) + testutils.AdvanceTo(eth, shareDistributionTasks[0].Start) } else { // this means some validators did not register, and the next phase is DisputeMissingRegistration - testutils.AdvanceTo(t, eth, dkgStates[0].PhaseStart+dkgStates[0].PhaseLength) + testutils.AdvanceTo(eth, dkgStates[0].PhaseStart+dkgStates[0].PhaseLength) } return &TestSuite{ @@ -325,10 +323,10 @@ func StartFromShareDistributionPhase(t *testing.T, n int, undistributedSharesIdx suite.DisputeMissingKeyshareTasks = disputeMissingKeySharesTasks // skip all the way to DisputeShareDistribution phase - testutils.AdvanceTo(t, suite.Eth, dispShareDistStartBlock) + testutils.AdvanceTo(suite.Eth, dispShareDistStartBlock) } else { // this means some validators did not distribute shares, and the next phase is DisputeMissingShareDistribution - testutils.AdvanceTo(t, suite.Eth, suite.DKGStates[0].PhaseStart+suite.DKGStates[0].PhaseLength) + testutils.AdvanceTo(suite.Eth, suite.DKGStates[0].PhaseStart+suite.DKGStates[0].PhaseLength) } return suite @@ -340,7 +338,7 @@ func StartFromKeyShareSubmissionPhase(t *testing.T, n int, undistributedShares i logger := logging.GetLogger("test").WithField("Validator", "") keyshareSubmissionStartBlock := suite.KeyshareSubmissionTasks[0].Start - testutils.AdvanceTo(t, suite.Eth, keyshareSubmissionStartBlock) + testutils.AdvanceTo(suite.Eth, keyshareSubmissionStartBlock) // Do key share submission task for idx := 0; idx < n; idx++ { @@ -389,10 +387,10 @@ func StartFromKeyShareSubmissionPhase(t *testing.T, n int, undistributedShares i } // skip all the way to MPKSubmission phase - testutils.AdvanceTo(t, suite.Eth, mpkSubmissionTaskStart) + testutils.AdvanceTo(suite.Eth, mpkSubmissionTaskStart) } else { // this means some validators did not submit key shares, and the next phase is DisputeMissingKeyShares - testutils.AdvanceTo(t, suite.Eth, suite.DKGStates[0].PhaseStart+suite.DKGStates[0].PhaseLength) + testutils.AdvanceTo(suite.Eth, suite.DKGStates[0].PhaseStart+suite.DKGStates[0].PhaseLength) } suite.MpkSubmissionTasks = mpkSubmissionTasks @@ -522,10 +520,10 @@ func StartFromGPKjPhase(t *testing.T, n int, undistributedGPKjIdx []int, badGPKj suite.CompletionTasks = completionTasks // skip all the way to DisputeGPKj phase - testutils.AdvanceTo(t, suite.Eth, dispGPKjStartBlock) + testutils.AdvanceTo(suite.Eth, dispGPKjStartBlock) } else { // this means some validators did not submit their GPKjs, and the next phase is DisputeMissingGPKj - testutils.AdvanceTo(t, suite.Eth, suite.DKGStates[0].PhaseStart+suite.DKGStates[0].PhaseLength) + testutils.AdvanceTo(suite.Eth, suite.DKGStates[0].PhaseStart+suite.DKGStates[0].PhaseLength) } return suite @@ -535,7 +533,7 @@ func StartFromCompletion(t *testing.T, n int, phaseLength uint16) *TestSuite { suite := StartFromGPKjPhase(t, n, []int{}, []int{}, phaseLength) // move to Completion phase - testutils.AdvanceTo(t, suite.Eth, suite.CompletionTasks[0].Start+suite.DKGStates[0].ConfirmationLength) + testutils.AdvanceTo(suite.Eth, suite.CompletionTasks[0].Start+suite.DKGStates[0].ConfirmationLength) return suite } diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 64fdecc4..1ae2b872 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -5,14 +5,13 @@ import ( "context" "crypto/ecdsa" "encoding/json" - "github.com/MadBase/MadNet/blockchain/transaction" "github.com/MadBase/MadNet/logging" "io" "io/ioutil" "log" "math/big" "net/http" - os "os" + "os" "os/exec" "path/filepath" "strconv" @@ -38,6 +37,7 @@ var ( scriptClean = "clean" envHardHatProcessId = "HARDHAT_PROCESS_ID" + envSkipRegistration = "SKIP_REGISTRATION" configEndpoint = "http://localhost:8545" configDefaultAccount = "0x546f99f244b7b58b855330ae0e2bc1b30b41302f" @@ -123,7 +123,7 @@ func isHardHatRunning() (bool, error) { return false, nil } -func startHardHat(t *testing.T, ctx context.Context) *ethereum.Details { +func startHardHat(t *testing.T, ctx context.Context, validatorsCount int) *ethereum.Details { log.Printf("Starting HardHat ...") err := runScriptHardHatNode() @@ -143,12 +143,13 @@ func startHardHat(t *testing.T, ctx context.Context) *ethereum.Details { assert.Nilf(t, err, "Error deploying contracts: %v") } - log.Printf("Registering validators ...") validatorAddresses := make([]string, 0) knownAccounts := details.GetKnownAccounts() - for _, acct := range knownAccounts { + for _, acct := range knownAccounts[:validatorsCount] { validatorAddresses = append(validatorAddresses, acct.Address.String()) } + + log.Printf("Registering %d validators ...", len(validatorAddresses)) err = runScriptRegisterValidators(details, validatorAddresses) if err != nil { details.Close() @@ -158,8 +159,8 @@ func startHardHat(t *testing.T, ctx context.Context) *ethereum.Details { log.Printf("Funding accounts ...") for _, account := range knownAccounts[1:] { - watcher := transaction.WatcherFromNetwork(details) - watcher.StartLoop() + //watcher := transaction.WatcherFromNetwork(details) + //watcher.StartLoop() txn, err := ethereum.TransferEther(details, logger, details.GetDefaultAccount().Address, account.Address, big.NewInt(100000000000000000)) assert.Nilf(t, err, "Error in TrasferEther transaction") @@ -198,7 +199,7 @@ func stopHardHat() error { return nil } -func GetEthereumNetwork(t *testing.T, cleanStart bool) ethereum.Network { +func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int) ethereum.Network { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -206,7 +207,7 @@ func GetEthereumNetwork(t *testing.T, cleanStart bool) ethereum.Network { isRunning, _ := isHardHatRunning() if !isRunning { log.Printf("Hardhat is not running. Start new HardHat") - details := startHardHat(t, ctx) + details := startHardHat(t, ctx, validatorsCount) assert.NotNilf(t, details, "Expected details to be not nil") return details } @@ -215,7 +216,7 @@ func GetEthereumNetwork(t *testing.T, cleanStart bool) ethereum.Network { err := stopHardHat() assert.Nilf(t, err, "Failed to stopHardHat") - details := startHardHat(t, ctx) + details := startHardHat(t, ctx, validatorsCount) assert.NotNilf(t, details, "Expected details to be not nil") return details } @@ -438,7 +439,7 @@ func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context) error rootPath := getProjectRootPath() scriptPath := getMainScriptPath() - err := os.Setenv("SKIP_REGISTRATION", "1") + err := os.Setenv(envSkipRegistration, "1") if err != nil { log.Printf("Error setting environment variable: %v", err) return err @@ -492,8 +493,8 @@ func runScriptRegisterValidators(eth *ethereum.Details, validatorAddresses []str return nil } -// send a command to the hardhat server via an RPC call -func SendHardhatCommand(command string, params ...interface{}) error { +// sendHardhatCommand sends a command to the hardhat server via an RPC call +func sendHardhatCommand(command string, params ...interface{}) error { commandJson := ðereum.JsonRPCMessage{ Version: "2.0", @@ -506,7 +507,6 @@ func SendHardhatCommand(command string, params ...interface{}) error { if err != nil { return err } - commandJson.Params = paramsJson c := http.Client{} @@ -516,14 +516,12 @@ func SendHardhatCommand(command string, params ...interface{}) error { return err } - reader := bytes.NewReader(buff.Bytes()) - + body := bytes.NewReader(buff.Bytes()) resp, err := c.Post( - "http://127.0.0.1:8545", + configEndpoint, "application/json", - reader, + body, ) - if err != nil { return err } @@ -535,18 +533,18 @@ func SendHardhatCommand(command string, params ...interface{}) error { return nil } -// mine a certain number of hardhat blocks +// MineBlocks mines a certain number of hardhat blocks func MineBlocks(t *testing.T, eth ethereum.Network, blocksToMine uint64) { var blocksToMineString = "0x" + strconv.FormatUint(blocksToMine, 16) log.Printf("hardhat_mine %v blocks ", blocksToMine) - err := SendHardhatCommand("hardhat_mine", blocksToMineString) + err := sendHardhatCommand("hardhat_mine", blocksToMineString) if err != nil { panic(err) } } // advance to a certain block number -func AdvanceTo(t *testing.T, eth ethereum.Network, target uint64) { +func AdvanceTo(eth ethereum.Network, target uint64) { currentBlock, err := eth.GetCurrentHeight(context.Background()) if err != nil { panic(err) @@ -559,17 +557,16 @@ func AdvanceTo(t *testing.T, eth ethereum.Network, target uint64) { log.Printf("hardhat_mine %v blocks to target height %v", blocksToMine, target) - err = SendHardhatCommand("hardhat_mine", blocksToMineString) + err = sendHardhatCommand("hardhat_mine", blocksToMineString) if err != nil { panic(err) } } -// The the Base fee for the next hardhat block. Can be used to make tx stale. -func SetNextBlockBaseFee(t *testing.T, eth ethereum.Network, target uint64) { +// SetNextBlockBaseFee The Base fee for the next hardhat block. Can be used to make tx stale. +func SetNextBlockBaseFee(target uint64) { log.Printf("Setting hardhat_setNextBlockBaseFeePerGas to %v", target) - - err := SendHardhatCommand("hardhat_setNextBlockBaseFeePerGas", "0x"+strconv.FormatUint(target, 16)) + err := sendHardhatCommand("hardhat_setNextBlockBaseFeePerGas", "0x"+strconv.FormatUint(target, 16)) if err != nil { panic(err) } @@ -579,7 +576,7 @@ func SetNextBlockBaseFee(t *testing.T, eth ethereum.Network, target uint64) { func SetAutoMine(t *testing.T, eth ethereum.Network, autoMine bool) { log.Printf("Setting Automine to %v", autoMine) - err := SendHardhatCommand("evm_setAutomine", autoMine) + err := sendHardhatCommand("evm_setAutomine", autoMine) if err != nil { panic(err) } @@ -591,7 +588,7 @@ func SetAutoMine(t *testing.T, eth ethereum.Network, autoMine bool) { func SetBlockInterval(t *testing.T, eth ethereum.Network, intervalInMilliSeconds uint64) { SetAutoMine(t, eth, false) log.Printf("Setting block interval to %v seconds", intervalInMilliSeconds) - err := SendHardhatCommand("evm_setIntervalMining", intervalInMilliSeconds) + err := sendHardhatCommand("evm_setIntervalMining", intervalInMilliSeconds) if err != nil { panic(err) } diff --git a/blockchain/transaction/transaction.go b/blockchain/transaction/transaction.go index 2abeb7e7..a3ccf433 100644 --- a/blockchain/transaction/transaction.go +++ b/blockchain/transaction/transaction.go @@ -214,25 +214,24 @@ type WatcherBackend struct { requestChannel <-chan subscribeRequest // Channel used to send request to this backend service } -func (b *WatcherBackend) Loop() { +func (wb *WatcherBackend) Loop() { poolingTime := time.After(constants.TxPollingTime) for { select { - case req, ok := <-b.requestChannel: + case req, ok := <-wb.requestChannel: if !ok { - b.logger.Debugf("request channel closed, exiting") + wb.logger.Debugf("request channel closed, exiting") return } if req.responseChannel == nil { - b.logger.Debug("Invalid request for txn without a response channel, ignoring") + wb.logger.Debug("Invalid request for txn without a response channel, ignoring") continue } - b.queue(req) + wb.queue(req) case <-poolingTime: - b.collectReceipts() - poolingTime = time.After(constants.TxPollingTime) + wb.collectReceipts() } } } @@ -307,6 +306,7 @@ func (wb *WatcherBackend) queue(req subscribeRequest) { retryGroup: wb.retryGroupId, } // initialize the retry group + wb.retryGroups = make(map[uint64]uint64) wb.retryGroups[wb.retryGroupId] = 1 // increasing the monotonically ID wb.retryGroupId++ @@ -661,15 +661,15 @@ func WatcherFromNetwork(network ethereum.Network) *Watcher { } // Start the transaction watcher service -func (f *Watcher) StartLoop() { - go f.backend.Loop() +func (w *Watcher) StartLoop() { + go w.backend.Loop() } // Close the transaction watcher service -func (f *Watcher) Close() { - f.logger.Debug("closing request channel...") - close(f.requestChannel) - f.closeMainContext() +func (w *Watcher) Close() { + w.logger.Debug("closing request channel...") + close(w.requestChannel) + w.closeMainContext() } // Subscribe a transaction to be watched by the transaction watcher service. If @@ -678,14 +678,14 @@ func (f *Watcher) Close() { // final tx hash in the receipt can be different from the initial txn sent. This // can happen if the txn got stale and the watcher did a transaction replace // with higher fees. -func (tw *Watcher) Subscribe(ctx context.Context, txn *types.Transaction) (<-chan *objects.ReceiptResponse, error) { - tw.logger.WithField("Txn", txn.Hash().Hex()).Debug("Subscribing for a transaction") - respChannel := NewResponseChannel[SubscribeResponse](tw.logger) +func (w *Watcher) Subscribe(ctx context.Context, txn *types.Transaction) (<-chan *objects.ReceiptResponse, error) { + w.logger.WithField("Txn", txn.Hash().Hex()).Debug("Subscribing for a transaction") + respChannel := NewResponseChannel[SubscribeResponse](w.logger) defer respChannel.CloseChannel() req := subscribeRequest{txn: txn, responseChannel: respChannel} select { - case tw.requestChannel <- req: + case w.requestChannel <- req: case <-ctx.Done(): return nil, &ErrInvalidTransactionRequest{fmt.Sprintf("context cancelled reqChannel: %v", ctx.Err())} } @@ -700,7 +700,7 @@ func (tw *Watcher) Subscribe(ctx context.Context, txn *types.Transaction) (<-cha // function that wait for a transaction receipt. This is blocking function that // will wait for a response in the input receiptResponseChannel -func (f *Watcher) Wait(ctx context.Context, receiptResponseChannel <-chan *objects.ReceiptResponse) (*types.Receipt, error) { +func (w *Watcher) Wait(ctx context.Context, receiptResponseChannel <-chan *objects.ReceiptResponse) (*types.Receipt, error) { select { case receiptResponse := <-receiptResponseChannel: return receiptResponse.Receipt, receiptResponse.Err @@ -710,15 +710,15 @@ func (f *Watcher) Wait(ctx context.Context, receiptResponseChannel <-chan *objec } // Queue a transaction and wait for its receipt -func (f *Watcher) SubscribeAndWait(ctx context.Context, txn *types.Transaction) (*types.Receipt, error) { - receiptResponseChannel, err := f.Subscribe(ctx, txn) +func (w *Watcher) SubscribeAndWait(ctx context.Context, txn *types.Transaction) (*types.Receipt, error) { + receiptResponseChannel, err := w.Subscribe(ctx, txn) if err != nil { return nil, err } - return f.Wait(ctx, receiptResponseChannel) + return w.Wait(ctx, receiptResponseChannel) } -func (f *Watcher) Status(ctx context.Context) error { - f.logger.Error("Status function not implemented yet") +func (w *Watcher) Status(ctx context.Context) error { + w.logger.Error("Status function not implemented yet") return nil } diff --git a/blockchain/transaction/transaction_test.go b/blockchain/transaction/transaction_test.go index a8681c76..202f6db7 100644 --- a/blockchain/transaction/transaction_test.go +++ b/blockchain/transaction/transaction_test.go @@ -170,7 +170,7 @@ func TestSubscribeAndWaitForStaleTx(t *testing.T) { testutils.SetBlockInterval(t, eth, 500) // setting base fee to 10k GWei - testutils.SetNextBlockBaseFee(t, eth, 10_000_000_000_000) + testutils.SetNextBlockBaseFee(10_000_000_000_000) accounts := eth.GetKnownAccounts() assert.Equal(t, numAccounts, len(accounts)) From bfe56fe24f419a9ecd5d129a75eee1fa6acb9cdb Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Mon, 13 Jun 2022 08:46:11 +0200 Subject: [PATCH 05/15] Removing bash sceripting --- .../tasks/dkg/testutils/advance_phases.go | 13 +- blockchain/testutils/cmd/deploy.go | 161 ++++++++++++++++++ blockchain/testutils/cmd/executor.go | 65 +++++++ blockchain/testutils/cmd/register.go | 37 ++++ blockchain/testutils/cmd/validator.go | 21 +++ blockchain/testutils/setup.go | 61 ++++--- blockchain/transaction/transaction.go | 3 +- 7 files changed, 331 insertions(+), 30 deletions(-) create mode 100644 blockchain/testutils/cmd/deploy.go create mode 100644 blockchain/testutils/cmd/executor.go create mode 100644 blockchain/testutils/cmd/register.go create mode 100644 blockchain/testutils/cmd/validator.go diff --git a/blockchain/executor/tasks/dkg/testutils/advance_phases.go b/blockchain/executor/tasks/dkg/testutils/advance_phases.go index 7afee90f..734d9823 100644 --- a/blockchain/executor/tasks/dkg/testutils/advance_phases.go +++ b/blockchain/executor/tasks/dkg/testutils/advance_phases.go @@ -10,7 +10,7 @@ import ( "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/utils" "github.com/MadBase/MadNet/blockchain/monitor/events" - "github.com/MadBase/MadNet/blockchain/testutils" + testutils "github.com/MadBase/MadNet/blockchain/testutils" "github.com/MadBase/MadNet/blockchain/transaction" "github.com/MadBase/MadNet/bridge/bindings" "github.com/MadBase/MadNet/crypto/bn256" @@ -47,6 +47,7 @@ type TestSuite struct { } func SetETHDKGPhaseLength(length uint16, eth ethereum.Network, callOpts *bind.TransactOpts, ctx context.Context) (*types.Transaction, *types.Receipt, error) { + // Shorten ethdkg phase for testing purposes ethdkgABI, err := abi.JSON(strings.NewReader(bindings.ETHDKGMetaData.ABI)) if err != nil { @@ -67,7 +68,10 @@ func SetETHDKGPhaseLength(length uint16, eth ethereum.Network, callOpts *bind.Tr } watcher := transaction.WatcherFromNetwork(eth) - rcpt, err := watcher.SubscribeAndWait(ctx, txn) + c, err := watcher.Subscribe(ctx, txn) + testutils.MineBlocks(eth, 12) + rcpt, err := watcher.Wait(ctx, c) + if err != nil { return nil, nil, err } @@ -98,7 +102,10 @@ func InitializeETHDKG(eth ethereum.Network, callOpts *bind.TransactOpts, ctx con } watcher := transaction.WatcherFromNetwork(eth) - rcpt, err := watcher.SubscribeAndWait(ctx, txn) + c, err := watcher.Subscribe(ctx, txn) + testutils.MineBlocks(eth, 12) + rcpt, err := watcher.Wait(ctx, c) + if err != nil { return nil, nil, err } diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go new file mode 100644 index 00000000..b596228e --- /dev/null +++ b/blockchain/testutils/cmd/deploy.go @@ -0,0 +1,161 @@ +package cmd + +import ( + "github.com/MadBase/MadNet/blockchain/testutils" + "log" + "os" + "path/filepath" +) + +func RunDeploy() error { + + hardhatNodeBaseCmd := "npx hardhat --network dev" + factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this + rootPath := testutils.GetProjectRootPath() + bridgeDir := testutils.GetBridgePath() + workingDir := createTempFolder() + defer os.Remove(workingDir) + + err := executeCommand(bridgeDir, "npx hardhat setHardhatIntervalMining --network dev --enable-auto-mine") + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + //cp ../scripts/base-files/deploymentList ../scripts/generated/deploymentList + //cp ../scripts/base-files/deploymentArgsTemplate ../scripts/generated/deploymentArgsTemplate + deploymentList := append(rootPath, "scripts", "base-files", "deploymentList") + deploymentArgsTemplate := append(rootPath, "scripts", "base-files", "deploymentArgsTemplate") + ownerToml := append(rootPath, "scripts", "base-files", "owner.toml") + _, err = copyFileToFolder(filepath.Join(filepath.Join(deploymentList...)), workingDir) + if err != nil { + log.Printf("File deploymentList copied in %s", workingDir) + return err + } + _, err = copyFileToFolder(filepath.Join(filepath.Join(deploymentArgsTemplate...)), workingDir) + if err != nil { + log.Printf("File deploymentArgsTemplate copied in %s", workingDir) + return err + } + _, err = copyFileToFolder(filepath.Join(filepath.Join(ownerToml...)), workingDir) + if err != nil { + log.Printf("File deploymentArgsTemplate copied in %s", workingDir) + return err + } + + //npx hardhat --network "$NETWORK" --show-stack-traces deployContracts --input-folder ../scripts/generated + err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "--show-stack-traces deployContracts --input-folder", workingDir) + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + //addr="$(grep -Pzo "\[$NETWORK\]\ndefaultFactoryAddress = \".*\"\n" ../scripts/generated/factoryState | grep -a "defaultFactoryAddress = .*" | awk '{print $NF}')" + // TODO - check how to create factoryAddress variable + + //export FACTORY_ADDRESS=$addr + //if [[ -z "${FACTORY_ADDRESS}" ]]; then + //echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Exiting script!" + //exit 1 + //fi + // TODO - unnecessary check + + //for filePath in $(ls ../scripts/generated/config | xargs); do + // sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/config/$filePath" > "../scripts/generated/config/$filePath".bk &&\ + // mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" + //done + // TODO - get the right path + //err = filepath.Walk("GENERATED CONFIG FILE TO BE DEFINED IN CURRENT WORKDIR", func(path string, info os.FileInfo, err error) error { + // if err != nil { + // log.Fatalf(err.Error()) + // return err + // } + // fmt.Printf("File Name: %s\n", info.Name()) + // return nil + //}) + //if err != nil { + // return err + //} + // TODO - I think un necessary + //for filePath in $(ls ../scripts/generated/config | xargs); do + //sed -e "s/registryAddress = .*/registryAddress = AAAAAAAAA/" "../scripts/generated/config/validator1.toml" > "../scripts/generated/config/$filePath".bk &&\ + //mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" + //done + // + //cp ../scripts/base-files/owner.toml ../scripts/generated/owner.toml + //sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/owner.toml" > "../scripts/generated/owner.toml".bk &&\ + //mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" + + // npx hardhat fundValidators --network $NETWORK + err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "fundValidators") + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + // + //if [[ ! -z "${SKIP_REGISTRATION}" ]]; then + //echo "SKIPPING VALIDATOR REGISTRATION" + //exit 0 + //fi + _, isSet := os.LookupEnv("SKIP_REGISTRATION") + if isSet { + return nil + } + + // + //FACTORY_ADDRESS="$(echo "$addr" | sed -e 's/^"//' -e 's/"$//')" + // + //if [[ -z "${FACTORY_ADDRESS}" ]]; then + //echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Not starting the registration!" + //exit 1 + //fi + // + //cd $BRIDGE_DIR + //cd $CURRENT_WD + //npx hardhat setHardhatIntervalMining --network $NETWORK --interval 1000 + err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setHardhatIntervalMining --interval 1000") + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + //./scripts/main.sh register + err = RunRegister() + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + // + //cd $BRIDGE_DIR + //npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 10 + err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + //npx hardhat setHardhatIntervalMining --network $NETWORK + err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setHardhatIntervalMining") + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + //cd $CURRENT_WD + // + //if [[ -n "${AUTO_START_VALIDATORS}" ]]; then + //if command -v gnome-terminal &>/dev/null; then + //i=1 + //for filePath in $(ls ./scripts/generated/config | xargs); do + //gnome-terminal --tab --title="Validator $i" -- bash -c "./scripts/main.sh validator $i" + //i=$((i + 1)) + //done + //exit 0 + //fi + //echo -e "failed to auto start validators terminals, manually open a terminal for each validator and execute" + //fi + + return nil +} diff --git a/blockchain/testutils/cmd/executor.go b/blockchain/testutils/cmd/executor.go new file mode 100644 index 00000000..de0022e3 --- /dev/null +++ b/blockchain/testutils/cmd/executor.go @@ -0,0 +1,65 @@ +package cmd + +import ( + "fmt" + "io" + "io/ioutil" + "log" + "os" + "os/exec" + "strings" +) + +func executeCommand(dir string, command ...string) error { + args := strings.Split(strings.Join(command, " "), " ") + cmd := exec.Cmd{ + Args: args, + Dir: dir, + Stdin: os.Stdout, + Stdout: os.Stdin, + Stderr: os.Stderr, + } + + err := cmd.Start() + if err != nil { + log.Printf("Could not execute command: %v", args) + return err + } + + return nil +} + +func createTempFolder() string { + // create tmp folder + file, err := ioutil.TempFile("dir", "prefix") + if err != nil { + log.Fatal(err) + } + + return file.Name() // For example "dir/prefix054003078" +} + +func copyFileToFolder(src, dst string) (int64, error) { + sourceFileStat, err := os.Stat(src) + if err != nil { + return 0, err + } + + if !sourceFileStat.Mode().IsRegular() { + return 0, fmt.Errorf("%s is not a regular file", src) + } + + source, err := os.Open(src) + if err != nil { + return 0, err + } + defer source.Close() + + destination, err := os.Create(dst) + if err != nil { + return 0, err + } + defer destination.Close() + nBytes, err := io.Copy(destination, source) + return nBytes, err +} diff --git a/blockchain/testutils/cmd/register.go b/blockchain/testutils/cmd/register.go new file mode 100644 index 00000000..a9d7a43c --- /dev/null +++ b/blockchain/testutils/cmd/register.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "github.com/MadBase/MadNet/blockchain/testutils" + "io/ioutil" + "log" + "path/filepath" + "strings" +) + +func RunRegister() error { + + bridgeDir := testutils.GetBridgePath() + rootDir := testutils.GetProjectRootPath() + factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this + // TODO - get the right path + keys := append(rootDir, "scripts", "generated", "keystores", "keys") + + // Build validator names + files, err := ioutil.ReadDir(filepath.Join(keys...)) + validators := make([]string, 0) + if err != nil { + return err + } + for _, file := range files { + validators = append(validators, file.Name()) + } + + // Register validator + err = executeCommand(bridgeDir, "npx hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + return nil +} diff --git a/blockchain/testutils/cmd/validator.go b/blockchain/testutils/cmd/validator.go new file mode 100644 index 00000000..1f11d208 --- /dev/null +++ b/blockchain/testutils/cmd/validator.go @@ -0,0 +1,21 @@ +package cmd + +import ( + "fmt" + "github.com/MadBase/MadNet/blockchain/testutils" + "path/filepath" +) + +func RunValidator(validatorIndex int) error { + + //./madnet --config ./scripts/generated/config/validator$1.toml validator + rootDir := filepath.Join(testutils.GetProjectRootPath()...) + + validatorConfig := append(rootDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) + + err := executeCommand(rootDir, "./madnet --config", filepath.Join(validatorConfig...), "validator") + if err != nil { + return err + } + return nil +} diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 1ae2b872..08366f80 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -47,8 +47,8 @@ var ( func getEthereumDetails() (*ethereum.Details, error) { - getProjectRootPath() - rootPath := getProjectRootPath() + GetProjectRootPath() + rootPath := GetProjectRootPath() assetKey := append(rootPath, "assets", "test", "keys") assetPasscode := append(rootPath, "assets", "test", "passcodes.txt") @@ -290,7 +290,7 @@ func InitializePrivateKeysAndAccounts(n int) ([]*ecdsa.PrivateKey, []accounts.Ac } func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { - rootPath := getProjectRootPath() + rootPath := GetProjectRootPath() // Account acctAddress := configDefaultAccount @@ -362,8 +362,8 @@ func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { func runScriptHardHatNode() error { - rootPath := getProjectRootPath() - scriptPath := getMainScriptPath() + rootPath := GetProjectRootPath() + scriptPath := GetMainScriptPath() cmd := exec.Cmd{ Path: scriptPath, @@ -371,7 +371,7 @@ func runScriptHardHatNode() error { Dir: filepath.Join(rootPath...), } - setCommandStdOut(&cmd) + SetCommandStdOut(&cmd) err := cmd.Start() if err != nil { log.Printf("Could not execute %s script: %v", scriptStartHardHatNode, err) @@ -394,8 +394,8 @@ func RunScriptInit(n int) error { return err } - rootPath := getProjectRootPath() - scriptPath := getMainScriptPath() + rootPath := GetProjectRootPath() + scriptPath := GetMainScriptPath() cmd := exec.Cmd{ Path: scriptPath, @@ -403,7 +403,7 @@ func RunScriptInit(n int) error { Dir: filepath.Join(rootPath...), } - setCommandStdOut(&cmd) + SetCommandStdOut(&cmd) err = cmd.Start() if err != nil { log.Printf("Could not execute %s script: %v", scriptInit, err) @@ -415,8 +415,8 @@ func RunScriptInit(n int) error { func runScriptClean() error { - rootPath := getProjectRootPath() - scriptPath := getMainScriptPath() + rootPath := GetProjectRootPath() + scriptPath := GetMainScriptPath() cmd := exec.Cmd{ Path: scriptPath, @@ -424,7 +424,7 @@ func runScriptClean() error { Dir: filepath.Join(rootPath...), } - setCommandStdOut(&cmd) + SetCommandStdOut(&cmd) err := cmd.Start() if err != nil { log.Printf("Could not execute %s script: %v", scriptClean, err) @@ -436,8 +436,8 @@ func runScriptClean() error { func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context) error { - rootPath := getProjectRootPath() - scriptPath := getMainScriptPath() + rootPath := GetProjectRootPath() + scriptPath := GetMainScriptPath() err := os.Setenv(envSkipRegistration, "1") if err != nil { @@ -451,7 +451,7 @@ func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context) error Dir: filepath.Join(rootPath...), } - setCommandStdOut(&cmd) + SetCommandStdOut(&cmd) err = cmd.Run() if err != nil { log.Printf("Could not execute %s script: %v", scriptDeploy, err) @@ -467,8 +467,8 @@ func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context) error func runScriptRegisterValidators(eth *ethereum.Details, validatorAddresses []string) error { - rootPath := getProjectRootPath() - scriptPath := getMainScriptPath() + rootPath := GetProjectRootPath() + scriptPath := GetMainScriptPath() args := []string{ scriptPath, @@ -483,7 +483,7 @@ func runScriptRegisterValidators(eth *ethereum.Details, validatorAddresses []str Dir: filepath.Join(rootPath...), } - setCommandStdOut(&cmd) + SetCommandStdOut(&cmd) err := cmd.Run() if err != nil { log.Printf("Could not execute %s script: %v", scriptRegisterValidator, err) @@ -534,7 +534,7 @@ func sendHardhatCommand(command string, params ...interface{}) error { } // MineBlocks mines a certain number of hardhat blocks -func MineBlocks(t *testing.T, eth ethereum.Network, blocksToMine uint64) { +func MineBlocks(eth ethereum.Network, blocksToMine uint64) { var blocksToMineString = "0x" + strconv.FormatUint(blocksToMine, 16) log.Printf("hardhat_mine %v blocks ", blocksToMine) err := sendHardhatCommand("hardhat_mine", blocksToMineString) @@ -594,8 +594,8 @@ func SetBlockInterval(t *testing.T, eth ethereum.Network, intervalInMilliSeconds } } -// getProjectRootPath returns the project root path -func getProjectRootPath() []string { +// GetProjectRootPath returns the project root path +func GetProjectRootPath() []string { rootPath := []string{string(os.PathSeparator)} @@ -618,9 +618,9 @@ func getProjectRootPath() []string { return rootPath } -// getMainScriptPath return the path of the main.sh script -func getMainScriptPath() string { - rootPath := getProjectRootPath() +// GetMainScriptPath return the path of the main.sh script +func GetMainScriptPath() string { + rootPath := GetProjectRootPath() scriptPath := append(rootPath, "scripts") scriptPath = append(scriptPath, "main.sh") scriptPathJoined := filepath.Join(scriptPath...) @@ -628,8 +628,17 @@ func getMainScriptPath() string { return scriptPathJoined } -// setCommandStdOut If ENABLE_SCRIPT_LOG env variable is set as 'true' the command will show scripts logs -func setCommandStdOut(cmd *exec.Cmd) { +// GetBridgePath return the path of the main.sh script +func GetBridgePath() string { + rootPath := GetProjectRootPath() + scriptPath := append(rootPath, "bridge") + scriptPathJoined := filepath.Join(scriptPath...) + + return scriptPathJoined +} + +// SetCommandStdOut If ENABLE_SCRIPT_LOG env variable is set as 'true' the command will show scripts logs +func SetCommandStdOut(cmd *exec.Cmd) { flagValue, found := os.LookupEnv("ENABLE_SCRIPT_LOG") enabled, err := strconv.ParseBool(flagValue) diff --git a/blockchain/transaction/transaction.go b/blockchain/transaction/transaction.go index a3ccf433..3f73165f 100644 --- a/blockchain/transaction/transaction.go +++ b/blockchain/transaction/transaction.go @@ -232,6 +232,7 @@ func (wb *WatcherBackend) Loop() { case <-poolingTime: wb.collectReceipts() + poolingTime = time.After(constants.TxPollingTime) } } } @@ -306,7 +307,6 @@ func (wb *WatcherBackend) queue(req subscribeRequest) { retryGroup: wb.retryGroupId, } // initialize the retry group - wb.retryGroups = make(map[uint64]uint64) wb.retryGroups[wb.retryGroupId] = 1 // increasing the monotonically ID wb.retryGroupId++ @@ -638,6 +638,7 @@ func NewWatcher(client ethereum.Network, selectMap interfaces.ISelectorMap, txCo client: client, logger: logger.WithField("Component", "TransactionWatcherBackend"), monitoredTxns: make(map[common.Hash]info), + retryGroups: make(map[uint64]uint64), receiptCache: make(map[common.Hash]receipt), aggregates: make(map[objects.FuncSelector]Profile), knownSelectors: selectMap, From 164b8b76a055b4a7b8fb112dd8d283b93b53c920 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Mon, 13 Jun 2022 10:56:54 +0200 Subject: [PATCH 06/15] Keep removing bash scripting --- .../executor/tasks/dkg/completion_test.go | 7 +- blockchain/testutils/cmd/clean.go | 47 ++++++++ blockchain/testutils/cmd/deploy.go | 18 ++- blockchain/testutils/cmd/executor.go | 25 ++++- blockchain/testutils/cmd/git-hooks.go | 19 ++++ blockchain/testutils/cmd/init.go | 103 ++++++++++++++++++ blockchain/testutils/cmd/validator.go | 7 +- blockchain/testutils/setup.go | 65 ++++++----- 8 files changed, 244 insertions(+), 47 deletions(-) create mode 100644 blockchain/testutils/cmd/clean.go create mode 100644 blockchain/testutils/cmd/git-hooks.go create mode 100644 blockchain/testutils/cmd/init.go diff --git a/blockchain/executor/tasks/dkg/completion_test.go b/blockchain/executor/tasks/dkg/completion_test.go index 07fb6f47..d7857da2 100644 --- a/blockchain/executor/tasks/dkg/completion_test.go +++ b/blockchain/executor/tasks/dkg/completion_test.go @@ -9,16 +9,21 @@ import ( dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/monitor/events" "github.com/MadBase/MadNet/blockchain/testutils" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "github.com/MadBase/MadNet/logging" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "os" "testing" ) // We complete everything correctly, happy path func TestCompletion_Group_1_AllGood(t *testing.T) { + workingDir := cmd.CreateTempFolder() + defer os.Remove(workingDir) + n := 4 - err := testutils.RunScriptInit(n) + err := testutils.Init(workingDir, n) assert.Nil(t, err) suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) diff --git a/blockchain/testutils/cmd/clean.go b/blockchain/testutils/cmd/clean.go new file mode 100644 index 00000000..b138d476 --- /dev/null +++ b/blockchain/testutils/cmd/clean.go @@ -0,0 +1,47 @@ +package cmd + +import ( + "github.com/MadBase/MadNet/blockchain/testutils" + "os" + "path/filepath" +) + +func RunClean(workingDir string) error { + + // TODO - not needed? + // Remove folder content + //rootPath := testutils.GetProjectRootPath() + //generatedFolder := append(rootPath,"scripts","generated") + //err := os.RemoveAll(filepath.Join(generatedFolder...)) + //if err != nil { + // return err + //} + + // Create folders + folders := []string{ + filepath.Join("scripts", "generated", "monitorDBs"), + filepath.Join("scripts", "generated", "config"), + filepath.Join("scripts", "generated", "keystores", "keys"), + filepath.Join("scripts", "generated", "keystores", "passcodes.txt"), + } + for _, folder := range folders { + if err := os.Mkdir(filepath.Join(workingDir, folder), os.ModePerm); err != nil { + return err + } + } + + // Copy config files + rootPath := testutils.GetProjectRootPath() + srcGenesis := append(rootPath, "scripts", "base-file", "genesis.json") + _, err := CopyFileToFolder(filepath.Join(srcGenesis...), filepath.Join(workingDir, "scripts", "generated")) + if err != nil { + return err + } + srcKey := append(rootPath, "scripts", "base-file", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f") + _, err = CopyFileToFolder(filepath.Join(srcKey...), filepath.Join(workingDir, "scripts", "generated", "keystores", "keys")) + if err != nil { + return err + } + + return nil +} diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go index b596228e..3eab9d0e 100644 --- a/blockchain/testutils/cmd/deploy.go +++ b/blockchain/testutils/cmd/deploy.go @@ -2,19 +2,18 @@ package cmd import ( "github.com/MadBase/MadNet/blockchain/testutils" + "io/ioutil" "log" "os" "path/filepath" ) -func RunDeploy() error { +func RunDeploy(workingDir string) error { hardhatNodeBaseCmd := "npx hardhat --network dev" factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this rootPath := testutils.GetProjectRootPath() bridgeDir := testutils.GetBridgePath() - workingDir := createTempFolder() - defer os.Remove(workingDir) err := executeCommand(bridgeDir, "npx hardhat setHardhatIntervalMining --network dev --enable-auto-mine") if err != nil { @@ -27,17 +26,17 @@ func RunDeploy() error { deploymentList := append(rootPath, "scripts", "base-files", "deploymentList") deploymentArgsTemplate := append(rootPath, "scripts", "base-files", "deploymentArgsTemplate") ownerToml := append(rootPath, "scripts", "base-files", "owner.toml") - _, err = copyFileToFolder(filepath.Join(filepath.Join(deploymentList...)), workingDir) + _, err = CopyFileToFolder(filepath.Join(filepath.Join(deploymentList...)), workingDir) if err != nil { log.Printf("File deploymentList copied in %s", workingDir) return err } - _, err = copyFileToFolder(filepath.Join(filepath.Join(deploymentArgsTemplate...)), workingDir) + _, err = CopyFileToFolder(filepath.Join(filepath.Join(deploymentArgsTemplate...)), workingDir) if err != nil { log.Printf("File deploymentArgsTemplate copied in %s", workingDir) return err } - _, err = copyFileToFolder(filepath.Join(filepath.Join(ownerToml...)), workingDir) + _, err = CopyFileToFolder(filepath.Join(filepath.Join(ownerToml...)), workingDir) if err != nil { log.Printf("File deploymentArgsTemplate copied in %s", workingDir) return err @@ -156,6 +155,13 @@ func RunDeploy() error { //fi //echo -e "failed to auto start validators terminals, manually open a terminal for each validator and execute" //fi + generatedValidatorConfigFiles := append(rootPath, "scripts", "generated", "config") + files, _ := ioutil.ReadDir(filepath.Join(generatedValidatorConfigFiles...)) + err = RunValidator(len(files)) + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } return nil } diff --git a/blockchain/testutils/cmd/executor.go b/blockchain/testutils/cmd/executor.go index de0022e3..34dbc0c8 100644 --- a/blockchain/testutils/cmd/executor.go +++ b/blockchain/testutils/cmd/executor.go @@ -29,7 +29,28 @@ func executeCommand(dir string, command ...string) error { return nil } -func createTempFolder() string { +func executeCommandWithOutput(dir string, command ...string) (string, error) { + args := strings.Split(strings.Join(command, " "), " ") + cmd := exec.Cmd{ + Args: args, + Dir: dir, + Stdin: os.Stdout, + Stdout: os.Stdin, + Stderr: os.Stderr, + } + + err := cmd.Start() + if err != nil { + log.Printf("Could not execute command: %v", args) + return "nil", err + } + + stdout, err := cmd.Output() + return string(stdout), err + +} + +func CreateTempFolder() string { // create tmp folder file, err := ioutil.TempFile("dir", "prefix") if err != nil { @@ -39,7 +60,7 @@ func createTempFolder() string { return file.Name() // For example "dir/prefix054003078" } -func copyFileToFolder(src, dst string) (int64, error) { +func CopyFileToFolder(src, dst string) (int64, error) { sourceFileStat, err := os.Stat(src) if err != nil { return 0, err diff --git a/blockchain/testutils/cmd/git-hooks.go b/blockchain/testutils/cmd/git-hooks.go new file mode 100644 index 00000000..d9f0ff73 --- /dev/null +++ b/blockchain/testutils/cmd/git-hooks.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "github.com/MadBase/MadNet/blockchain/testutils" + "log" + "path/filepath" +) + +func RunGitHooks() error { + + rootPath := testutils.GetProjectRootPath() + err := executeCommand(filepath.Join(rootPath...), "git config core.hooksPath scripts/githooks 2>/dev/null") + if err != nil { + log.Printf("Could not execute script: %v", err) + return err + } + + return nil +} diff --git a/blockchain/testutils/cmd/init.go b/blockchain/testutils/cmd/init.go new file mode 100644 index 00000000..4a613a9c --- /dev/null +++ b/blockchain/testutils/cmd/init.go @@ -0,0 +1,103 @@ +package cmd + +import ( + "errors" + "fmt" + "github.com/MadBase/MadNet/blockchain/testutils" + "os" + "path/filepath" +) + +func RunInit(workingDir string, numbersOfValidator int) error { + + err := RunGitHooks() + if err != nil { + return err + } + + LA := 4242 + PA := 4343 + DA := 4444 + LSA := 8884 + + //# Check that number of validators is valid + //if ! [[ $1 =~ $re ]] || [[ $1 -lt 4 ]] || [[ $1 -gt 32 ]]; then + //echo -e "Invalid number of validators [4-32]" + //exit 1 + //fi + if numbersOfValidator < 4 || numbersOfValidator > 32 { + return errors.New("number of possible validators can be from 4 up to 32") + } + + // TODO - Not necessary + //if [ -f "./scripts/generated/genesis.json" ]; then + //echo -e "Generated files already exist, run clean" + //exit 1 + //fi + //CLEAN_UP + + rootPath := testutils.GetProjectRootPath() + for i := 1; i < numbersOfValidator; i++ { + // TODO change to working direcotir password file + output, err := executeCommandWithOutput(filepath.Join(rootPath...), "ethkey generate --passwordfile ./scripts/base-files/passwordFile | cut -d' ' -f2") + if err != nil { + return err + } + ADDRESS := output + + output, err = executeCommandWithOutput(filepath.Join(rootPath...), "hexdump -n 16 -e '4/4 \"%08X\" 1 \"\\n\"' /dev/urandom") + if err != nil { + return err + } + PK := output + + // TODO - use temp working dir and validator number + sedCommand := fmt.Sprintf(` + sed -e 's/defaultAccount = .*/defaultAccount = \"'"%s"'\"/' ./scripts/base-files/baseConfig | + sed -e 's/rewardAccount = .*/rewardAccount = \"'"%s"'\"/' | + sed -e 's/listeningAddress = .*/listeningAddress = \"0.0.0.0:'"%s"'\"/' | + sed -e 's/p2pListeningAddress = .*/p2pListeningAddress = \"0.0.0.0:'"%s"'\"/' | + sed -e 's/discoveryListeningAddress = .*/discoveryListeningAddress = \"0.0.0.0:'"%s"'\"/' | + sed -e 's/localStateListeningAddress = .*/localStateListeningAddress = \"0.0.0.0:'"%s"'\"/' | + sed -e 's/passcodes = .*/passcodes = \"scripts\/generated\/keystores\/passcodes.txt\"/' | + sed -e 's/keystore = .*/keystore = \"scripts\/generated\/keystores\/keys\"/' | + sed -e 's/stateDB = .*/stateDB = \"scripts\/generated\/stateDBs\/validator'"%d"'\/\"/' | + sed -e 's/monitorDB = .*/monitorDB = \"scripts\/generated\/monitorDBs\/validator'"%d"'\/\"/' | + sed -e 's/privateKey = .*/privateKey = \"'"%s"'\"/' > ./scripts/generated/config/validator%d.toml`, + ADDRESS, ADDRESS, LA, PA, DA, LSA, i, i, PK, i) + err = executeCommand(filepath.Join(rootPath...), sedCommand) + if err != nil { + return err + } + + //echo "$ADDRESS=abc123" >> ./scripts/generated/keystores/passcodes.txt + f, err := os.Create(filepath.Join(workingDir, "scripts", "generated", "keystores", "passcodes.txt")) + if err != nil { + return err + } + defer f.Close() + _, err = f.WriteString(fmt.Sprintf("%s=abc123", ADDRESS)) + if err != nil { + return err + } + // TODO - does this exists? + //mv ./keyfile.json ./scripts/generated/keystores/keys/$ADDRESS + + // Genesis + genesisPath := filepath.Join(workingDir, "scripts", "generated", "genesis.json") + jqCommand := fmt.Sprintf(` + jq '.alloc += {"'"$(echo %s | cut -c3-)"'": {balance:"10000000000000000000000"}}' %s > %s.tmp && mv %s.tmp %s + `, ADDRESS, genesisPath, genesisPath, genesisPath, genesisPath) + err = executeCommand(filepath.Join(rootPath...), jqCommand) + if err != nil { + return err + } + + LA = LA + 1 + PA = PA + 1 + DA = DA + 1 + LSA = LSA + 1 + } + + return nil +} diff --git a/blockchain/testutils/cmd/validator.go b/blockchain/testutils/cmd/validator.go index 1f11d208..3a97f6aa 100644 --- a/blockchain/testutils/cmd/validator.go +++ b/blockchain/testutils/cmd/validator.go @@ -8,12 +8,11 @@ import ( func RunValidator(validatorIndex int) error { - //./madnet --config ./scripts/generated/config/validator$1.toml validator - rootDir := filepath.Join(testutils.GetProjectRootPath()...) + rootDir := testutils.GetProjectRootPath() + validatorConfigPath := append(rootDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) - validatorConfig := append(rootDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) + err := executeCommand(filepath.Join(rootDir...), "./madnet --config", filepath.Join(validatorConfigPath...), "validator") - err := executeCommand(rootDir, "./madnet --config", filepath.Join(validatorConfig...), "validator") if err != nil { return err } diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 08366f80..3c308949 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -5,6 +5,7 @@ import ( "context" "crypto/ecdsa" "encoding/json" + cmd2 "github.com/MadBase/MadNet/blockchain/testutils/cmd" "github.com/MadBase/MadNet/logging" "io" "io/ioutil" @@ -387,53 +388,49 @@ func runScriptHardHatNode() error { return nil } -func RunScriptInit(n int) error { +func Init(workingDir string, n int) error { - err := runScriptClean() + err := cmd2.RunClean(workingDir) if err != nil { return err } - rootPath := GetProjectRootPath() - scriptPath := GetMainScriptPath() - - cmd := exec.Cmd{ - Path: scriptPath, - Args: []string{scriptPath, scriptInit, strconv.Itoa(n)}, - Dir: filepath.Join(rootPath...), - } - - SetCommandStdOut(&cmd) - err = cmd.Start() + err = cmd2.RunInit(workingDir, n) if err != nil { - log.Printf("Could not execute %s script: %v", scriptInit, err) return err } - return nil -} - -func runScriptClean() error { - - rootPath := GetProjectRootPath() - scriptPath := GetMainScriptPath() - - cmd := exec.Cmd{ - Path: scriptPath, - Args: []string{scriptPath, scriptClean}, - Dir: filepath.Join(rootPath...), - } - - SetCommandStdOut(&cmd) - err := cmd.Start() - if err != nil { - log.Printf("Could not execute %s script: %v", scriptClean, err) - return err - } + // TODO - change this to be global + //SetCommandStdOut(&cmd) + //err = cmd.Start() + //if err != nil { + // return err + //} return nil } +//func runScriptClean() error { +// +// rootPath := GetProjectRootPath() +// scriptPath := GetMainScriptPath() +// +// cmd := exec.Cmd{ +// Path: scriptPath, +// Args: []string{scriptPath, scriptClean}, +// Dir: filepath.Join(rootPath...), +// } +// +// SetCommandStdOut(&cmd) +// err := cmd.Start() +// if err != nil { +// log.Printf("Could not execute %s script: %v", scriptClean, err) +// return err +// } +// +// return nil +//} + func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context) error { rootPath := GetProjectRootPath() From da041824d743b090f66bc32287dcd913978910da Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Mon, 13 Jun 2022 11:49:08 +0200 Subject: [PATCH 07/15] Keep removing bash scripting --- blockchain/testutils/cmd/clean.go | 8 +- blockchain/testutils/cmd/deploy.go | 28 +-- blockchain/testutils/cmd/executor.go | 37 ++- blockchain/testutils/cmd/git-hooks.go | 3 +- blockchain/testutils/cmd/hardhat.go | 126 ++++++++++ blockchain/testutils/cmd/init.go | 14 +- blockchain/testutils/cmd/register.go | 6 +- blockchain/testutils/cmd/validator.go | 4 +- blockchain/testutils/setup.go | 317 +++----------------------- 9 files changed, 212 insertions(+), 331 deletions(-) create mode 100644 blockchain/testutils/cmd/hardhat.go diff --git a/blockchain/testutils/cmd/clean.go b/blockchain/testutils/cmd/clean.go index b138d476..c62aba36 100644 --- a/blockchain/testutils/cmd/clean.go +++ b/blockchain/testutils/cmd/clean.go @@ -32,13 +32,13 @@ func RunClean(workingDir string) error { // Copy config files rootPath := testutils.GetProjectRootPath() - srcGenesis := append(rootPath, "scripts", "base-file", "genesis.json") - _, err := CopyFileToFolder(filepath.Join(srcGenesis...), filepath.Join(workingDir, "scripts", "generated")) + srcGenesis := filepath.Join(rootPath, "scripts", "base-file", "genesis.json") + _, err := CopyFileToFolder(srcGenesis, filepath.Join(workingDir, "scripts", "generated")) if err != nil { return err } - srcKey := append(rootPath, "scripts", "base-file", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f") - _, err = CopyFileToFolder(filepath.Join(srcKey...), filepath.Join(workingDir, "scripts", "generated", "keystores", "keys")) + srcKey := filepath.Join(rootPath, "scripts", "base-file", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f") + _, err = CopyFileToFolder(srcKey, filepath.Join(workingDir, "scripts", "generated", "keystores", "keys")) if err != nil { return err } diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go index 3eab9d0e..b569a4f0 100644 --- a/blockchain/testutils/cmd/deploy.go +++ b/blockchain/testutils/cmd/deploy.go @@ -15,7 +15,7 @@ func RunDeploy(workingDir string) error { rootPath := testutils.GetProjectRootPath() bridgeDir := testutils.GetBridgePath() - err := executeCommand(bridgeDir, "npx hardhat setHardhatIntervalMining --network dev --enable-auto-mine") + _, err := executeCommand(bridgeDir, "npx hardhat setHardhatIntervalMining --network dev --enable-auto-mine") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -23,27 +23,27 @@ func RunDeploy(workingDir string) error { //cp ../scripts/base-files/deploymentList ../scripts/generated/deploymentList //cp ../scripts/base-files/deploymentArgsTemplate ../scripts/generated/deploymentArgsTemplate - deploymentList := append(rootPath, "scripts", "base-files", "deploymentList") - deploymentArgsTemplate := append(rootPath, "scripts", "base-files", "deploymentArgsTemplate") - ownerToml := append(rootPath, "scripts", "base-files", "owner.toml") - _, err = CopyFileToFolder(filepath.Join(filepath.Join(deploymentList...)), workingDir) + deploymentList := filepath.Join(rootPath, "scripts", "base-files", "deploymentList") + deploymentArgsTemplate := filepath.Join(rootPath, "scripts", "base-files", "deploymentArgsTemplate") + ownerToml := filepath.Join(rootPath, "scripts", "base-files", "owner.toml") + _, err = CopyFileToFolder(deploymentList, workingDir) if err != nil { log.Printf("File deploymentList copied in %s", workingDir) return err } - _, err = CopyFileToFolder(filepath.Join(filepath.Join(deploymentArgsTemplate...)), workingDir) + _, err = CopyFileToFolder(deploymentArgsTemplate, workingDir) if err != nil { log.Printf("File deploymentArgsTemplate copied in %s", workingDir) return err } - _, err = CopyFileToFolder(filepath.Join(filepath.Join(ownerToml...)), workingDir) + _, err = CopyFileToFolder(ownerToml, workingDir) if err != nil { log.Printf("File deploymentArgsTemplate copied in %s", workingDir) return err } //npx hardhat --network "$NETWORK" --show-stack-traces deployContracts --input-folder ../scripts/generated - err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "--show-stack-traces deployContracts --input-folder", workingDir) + _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "--show-stack-traces deployContracts --input-folder", workingDir) if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -86,7 +86,7 @@ func RunDeploy(workingDir string) error { //mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" // npx hardhat fundValidators --network $NETWORK - err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "fundValidators") + _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "fundValidators") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -113,7 +113,7 @@ func RunDeploy(workingDir string) error { //cd $BRIDGE_DIR //cd $CURRENT_WD //npx hardhat setHardhatIntervalMining --network $NETWORK --interval 1000 - err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setHardhatIntervalMining --interval 1000") + _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setHardhatIntervalMining --interval 1000") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -129,14 +129,14 @@ func RunDeploy(workingDir string) error { // //cd $BRIDGE_DIR //npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 10 - err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) + _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) if err != nil { log.Printf("Could not execute script: %v", err) return err } //npx hardhat setHardhatIntervalMining --network $NETWORK - err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setHardhatIntervalMining") + _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setHardhatIntervalMining") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -155,8 +155,8 @@ func RunDeploy(workingDir string) error { //fi //echo -e "failed to auto start validators terminals, manually open a terminal for each validator and execute" //fi - generatedValidatorConfigFiles := append(rootPath, "scripts", "generated", "config") - files, _ := ioutil.ReadDir(filepath.Join(generatedValidatorConfigFiles...)) + generatedValidatorConfigFiles := filepath.Join(rootPath, "scripts", "generated", "config") + files, _ := ioutil.ReadDir(generatedValidatorConfigFiles) err = RunValidator(len(files)) if err != nil { log.Printf("Could not execute script: %v", err) diff --git a/blockchain/testutils/cmd/executor.go b/blockchain/testutils/cmd/executor.go index 34dbc0c8..5af4e4ad 100644 --- a/blockchain/testutils/cmd/executor.go +++ b/blockchain/testutils/cmd/executor.go @@ -7,29 +7,28 @@ import ( "log" "os" "os/exec" + "strconv" "strings" ) -func executeCommand(dir string, command ...string) error { - args := strings.Split(strings.Join(command, " "), " ") - cmd := exec.Cmd{ - Args: args, - Dir: dir, - Stdin: os.Stdout, - Stdout: os.Stdin, - Stderr: os.Stderr, - } +// TODO - double check github action will pick this up - err := cmd.Start() - if err != nil { - log.Printf("Could not execute command: %v", args) - return err - } +// SetCommandStdOut If ENABLE_SCRIPT_LOG env variable is set as 'true' the command will show scripts logs +func SetCommandStdOut(cmd *exec.Cmd) { - return nil + flagValue, found := os.LookupEnv("ENABLE_SCRIPT_LOG") + enabled, err := strconv.ParseBool(flagValue) + + if err == nil && found && enabled { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + } else { + cmd.Stdout = io.Discard + cmd.Stderr = io.Discard + } } -func executeCommandWithOutput(dir string, command ...string) (string, error) { +func executeCommand(dir string, command ...string) (exec.Cmd, error) { args := strings.Split(strings.Join(command, " "), " ") cmd := exec.Cmd{ Args: args, @@ -42,12 +41,10 @@ func executeCommandWithOutput(dir string, command ...string) (string, error) { err := cmd.Start() if err != nil { log.Printf("Could not execute command: %v", args) - return "nil", err + return exec.Cmd{}, err } - stdout, err := cmd.Output() - return string(stdout), err - + return cmd, nil } func CreateTempFolder() string { diff --git a/blockchain/testutils/cmd/git-hooks.go b/blockchain/testutils/cmd/git-hooks.go index d9f0ff73..f73430fa 100644 --- a/blockchain/testutils/cmd/git-hooks.go +++ b/blockchain/testutils/cmd/git-hooks.go @@ -3,13 +3,12 @@ package cmd import ( "github.com/MadBase/MadNet/blockchain/testutils" "log" - "path/filepath" ) func RunGitHooks() error { rootPath := testutils.GetProjectRootPath() - err := executeCommand(filepath.Join(rootPath...), "git config core.hooksPath scripts/githooks 2>/dev/null") + _, err := executeCommand(rootPath, "git config core.hooksPath scripts/githooks 2>/dev/null") if err != nil { log.Printf("Could not execute script: %v", err) return err diff --git a/blockchain/testutils/cmd/hardhat.go b/blockchain/testutils/cmd/hardhat.go new file mode 100644 index 00000000..05f66e67 --- /dev/null +++ b/blockchain/testutils/cmd/hardhat.go @@ -0,0 +1,126 @@ +package cmd + +import ( + "bytes" + "context" + "encoding/json" + "github.com/MadBase/MadNet/blockchain/ethereum" + "github.com/MadBase/MadNet/blockchain/testutils" + "log" + "net/http" + "os" + "strconv" + "syscall" + "time" +) + +var ( + configEndpoint = "http://localhost:8545" + scriptStartHardHatNode = "hardhat_node" + envHardHatProcessId = "HARDHAT_PROCESS_ID" +) + +func IsHardHatRunning() (bool, error) { + var client = http.Client{Timeout: 2 * time.Second} + resp, err := client.Head(configEndpoint) + if err != nil { + return false, err + } + resp.Body.Close() + + if resp.StatusCode >= 200 && resp.StatusCode <= 299 { + return true, nil + } + + return false, nil +} + +func RunHardHatNode() error { + + bridgePath := testutils.GetBridgePath() + cmd, err := executeCommand(bridgePath, "npx hardhat node --show-stack-traces") + if err != nil { + return err + } + + err = os.Setenv(envHardHatProcessId, strconv.Itoa(cmd.Process.Pid)) + if err != nil { + log.Printf("Error setting environment variable: %v", err) + return err + } + + return nil +} + +func WaitForHardHatNode(ctx context.Context) error { + c := http.Client{} + msg := ðereum.JsonRPCMessage{ + Version: "2.0", + ID: []byte("1"), + Method: "eth_chainId", + Params: make([]byte, 0), + } + + params, err := json.Marshal(make([]string, 0)) + if err != nil { + log.Printf("could not run hardhat node: %v", err) + return err + } + msg.Params = params + + var buff bytes.Buffer + err = json.NewEncoder(&buff).Encode(msg) + if err != nil { + log.Printf("Error creating a buffer json encoder: %v", err) + return err + } + + for { + select { + case <-ctx.Done(): + return ctx.Err() + case <-time.After(time.Second): + body := bytes.NewReader(buff.Bytes()) + _, err := c.Post( + configEndpoint, + "application/json", + body, + ) + if err != nil { + continue + } + log.Printf("HardHat node started correctly") + return nil + } + } +} + +func StopHardHat() error { + log.Printf("Stopping HardHat running instance ...") + isRunning, _ := IsHardHatRunning() + if !isRunning { + return nil + } + + pid, _ := strconv.Atoi(os.Getenv(envHardHatProcessId)) + process, err := os.FindProcess(pid) + if err != nil { + log.Printf("Error finding HardHat pid: %v", err) + return err + } + + err = process.Signal(syscall.SIGTERM) + if err != nil { + log.Printf("Error waiting sending SIGTERM signal to HardHat process: %v", err) + return err + } + + _, err = process.Wait() + if err != nil { + log.Printf("Error waiting HardHat process to stop: %v", err) + return err + } + + log.Printf("HardHat node has been stopped") + return nil +} diff --git a/blockchain/testutils/cmd/init.go b/blockchain/testutils/cmd/init.go index 4a613a9c..200a6ff8 100644 --- a/blockchain/testutils/cmd/init.go +++ b/blockchain/testutils/cmd/init.go @@ -39,17 +39,19 @@ func RunInit(workingDir string, numbersOfValidator int) error { rootPath := testutils.GetProjectRootPath() for i := 1; i < numbersOfValidator; i++ { // TODO change to working direcotir password file - output, err := executeCommandWithOutput(filepath.Join(rootPath...), "ethkey generate --passwordfile ./scripts/base-files/passwordFile | cut -d' ' -f2") + cmd, err := executeCommand(rootPath, "ethkey generate --passwordfile ./scripts/base-files/passwordFile | cut -d' ' -f2") if err != nil { return err } - ADDRESS := output + stdout, err := cmd.Output() + ADDRESS := stdout - output, err = executeCommandWithOutput(filepath.Join(rootPath...), "hexdump -n 16 -e '4/4 \"%08X\" 1 \"\\n\"' /dev/urandom") + cmd, err = executeCommand(rootPath, "hexdump -n 16 -e '4/4 \"%08X\" 1 \"\\n\"' /dev/urandom") if err != nil { return err } - PK := output + stdout, err = cmd.Output() + PK := stdout // TODO - use temp working dir and validator number sedCommand := fmt.Sprintf(` @@ -65,7 +67,7 @@ func RunInit(workingDir string, numbersOfValidator int) error { sed -e 's/monitorDB = .*/monitorDB = \"scripts\/generated\/monitorDBs\/validator'"%d"'\/\"/' | sed -e 's/privateKey = .*/privateKey = \"'"%s"'\"/' > ./scripts/generated/config/validator%d.toml`, ADDRESS, ADDRESS, LA, PA, DA, LSA, i, i, PK, i) - err = executeCommand(filepath.Join(rootPath...), sedCommand) + _, err = executeCommand(rootPath, sedCommand) if err != nil { return err } @@ -88,7 +90,7 @@ func RunInit(workingDir string, numbersOfValidator int) error { jqCommand := fmt.Sprintf(` jq '.alloc += {"'"$(echo %s | cut -c3-)"'": {balance:"10000000000000000000000"}}' %s > %s.tmp && mv %s.tmp %s `, ADDRESS, genesisPath, genesisPath, genesisPath, genesisPath) - err = executeCommand(filepath.Join(rootPath...), jqCommand) + _, err = executeCommand(rootPath, jqCommand) if err != nil { return err } diff --git a/blockchain/testutils/cmd/register.go b/blockchain/testutils/cmd/register.go index a9d7a43c..d88a01d7 100644 --- a/blockchain/testutils/cmd/register.go +++ b/blockchain/testutils/cmd/register.go @@ -14,10 +14,10 @@ func RunRegister() error { rootDir := testutils.GetProjectRootPath() factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this // TODO - get the right path - keys := append(rootDir, "scripts", "generated", "keystores", "keys") + keys := filepath.Join(rootDir, "scripts", "generated", "keystores", "keys") // Build validator names - files, err := ioutil.ReadDir(filepath.Join(keys...)) + files, err := ioutil.ReadDir(keys) validators := make([]string, 0) if err != nil { return err @@ -27,7 +27,7 @@ func RunRegister() error { } // Register validator - err = executeCommand(bridgeDir, "npx hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) + _, err = executeCommand(bridgeDir, "npx hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) if err != nil { log.Printf("Could not execute script: %v", err) return err diff --git a/blockchain/testutils/cmd/validator.go b/blockchain/testutils/cmd/validator.go index 3a97f6aa..8f388b02 100644 --- a/blockchain/testutils/cmd/validator.go +++ b/blockchain/testutils/cmd/validator.go @@ -9,9 +9,9 @@ import ( func RunValidator(validatorIndex int) error { rootDir := testutils.GetProjectRootPath() - validatorConfigPath := append(rootDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) + validatorConfigPath := filepath.Join(rootDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) - err := executeCommand(filepath.Join(rootDir...), "./madnet --config", filepath.Join(validatorConfigPath...), "validator") + _, err := executeCommand(rootDir, "./madnet --config", validatorConfigPath, "validator") if err != nil { return err diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 3c308949..d3d39bf4 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -5,8 +5,15 @@ import ( "context" "crypto/ecdsa" "encoding/json" - cmd2 "github.com/MadBase/MadNet/blockchain/testutils/cmd" + "github.com/MadBase/MadNet/blockchain/ethereum" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "github.com/MadBase/MadNet/logging" + "github.com/MadBase/MadNet/utils" + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/accounts/keystore" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/stretchr/testify/assert" "io" "io/ioutil" "log" @@ -17,29 +24,10 @@ import ( "path/filepath" "strconv" "strings" - "syscall" "testing" - "time" - - "github.com/MadBase/MadNet/blockchain/ethereum" - "github.com/MadBase/MadNet/utils" - "github.com/ethereum/go-ethereum/accounts" - "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" - "github.com/stretchr/testify/assert" ) var ( - scriptDeploy = "deploy" - scriptRegisterValidator = "register_test" - scriptStartHardHatNode = "hardhat_node" - scriptInit = "init" - scriptClean = "clean" - - envHardHatProcessId = "HARDHAT_PROCESS_ID" - envSkipRegistration = "SKIP_REGISTRATION" - configEndpoint = "http://localhost:8545" configDefaultAccount = "0x546f99f244b7b58b855330ae0e2bc1b30b41302f" configFactoryAddress = "0x0b1f9c2b7bed6db83295c7b5158e3806d67ec5bc" @@ -48,16 +36,14 @@ var ( func getEthereumDetails() (*ethereum.Details, error) { - GetProjectRootPath() rootPath := GetProjectRootPath() - - assetKey := append(rootPath, "assets", "test", "keys") - assetPasscode := append(rootPath, "assets", "test", "passcodes.txt") + assetKey := filepath.Join(rootPath, "assets", "test", "keys") + assetPasscode := filepath.Join(rootPath, "assets", "test", "passcodes.txt") details, err := ethereum.NewEndpoint( configEndpoint, - filepath.Join(filepath.Join(assetKey...)), - filepath.Join(filepath.Join(assetPasscode...)), + assetKey, + assetPasscode, configDefaultAccount, configFinalityDelay, 500, @@ -66,71 +52,13 @@ func getEthereumDetails() (*ethereum.Details, error) { return details, err } -func waitForHardHatNode(ctx context.Context) error { - c := http.Client{} - msg := ðereum.JsonRPCMessage{ - Version: "2.0", - ID: []byte("1"), - Method: "eth_chainId", - Params: make([]byte, 0), - } - - params, err := json.Marshal(make([]string, 0)) - if err != nil { - log.Printf("could not run hardhat node: %v", err) - return err - } - msg.Params = params - - var buff bytes.Buffer - err = json.NewEncoder(&buff).Encode(msg) - if err != nil { - log.Printf("Error creating a buffer json encoder: %v", err) - return err - } - - for { - select { - case <-ctx.Done(): - return ctx.Err() - case <-time.After(time.Second): - body := bytes.NewReader(buff.Bytes()) - _, err := c.Post( - configEndpoint, - "application/json", - body, - ) - if err != nil { - continue - } - log.Printf("HardHat node started correctly") - return nil - } - } -} - -func isHardHatRunning() (bool, error) { - var client = http.Client{Timeout: 2 * time.Second} - resp, err := client.Head(configEndpoint) - if err != nil { - return false, err - } - resp.Body.Close() - - if resp.StatusCode >= 200 && resp.StatusCode <= 299 { - return true, nil - } - - return false, nil -} - -func startHardHat(t *testing.T, ctx context.Context, validatorsCount int) *ethereum.Details { +func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workingDir string) *ethereum.Details { log.Printf("Starting HardHat ...") - err := runScriptHardHatNode() + err := cmd.RunHardHatNode() assert.Nilf(t, err, "Error starting hardhat node") - err = waitForHardHatNode(ctx) + err = cmd.WaitForHardHatNode(ctx) assert.Nilf(t, err, "Failed to wait for hardhat to be up and running") details, err := getEthereumDetails() @@ -138,11 +66,14 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int) *ether assert.NotNilf(t, details, "Ethereum network should not be Nil") log.Printf("Deploying contracts ...") - err = runScriptDeployContracts(details, ctx) + err = cmd.RunDeploy(workingDir) if err != nil { details.Close() assert.Nilf(t, err, "Error deploying contracts: %v") } + addr := common.Address{} + copy(addr[:], common.FromHex(configFactoryAddress)) + details.Contracts().Initialize(ctx, addr) validatorAddresses := make([]string, 0) knownAccounts := details.GetKnownAccounts() @@ -151,7 +82,7 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int) *ether } log.Printf("Registering %d validators ...", len(validatorAddresses)) - err = runScriptRegisterValidators(details, validatorAddresses) + err = cmd.RunRegister() if err != nil { details.Close() assert.Nilf(t, err, "Error registering validators: %v") @@ -160,9 +91,6 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int) *ether log.Printf("Funding accounts ...") for _, account := range knownAccounts[1:] { - //watcher := transaction.WatcherFromNetwork(details) - //watcher.StartLoop() - txn, err := ethereum.TransferEther(details, logger, details.GetDefaultAccount().Address, account.Address, big.NewInt(100000000000000000)) assert.Nilf(t, err, "Error in TrasferEther transaction") assert.NotNilf(t, txn, "Expected transaction not to be nil") @@ -170,54 +98,24 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int) *ether return details } -func stopHardHat() error { - log.Printf("Stopping HardHat running instance ...") - isRunning, _ := isHardHatRunning() - if !isRunning { - return nil - } - - pid, _ := strconv.Atoi(os.Getenv(envHardHatProcessId)) - process, err := os.FindProcess(pid) - if err != nil { - log.Printf("Error finding HardHat pid: %v", err) - return err - } - - err = process.Signal(syscall.SIGTERM) - if err != nil { - log.Printf("Error waiting sending SIGTERM signal to HardHat process: %v", err) - return err - } - - _, err = process.Wait() - if err != nil { - log.Printf("Error waiting HardHat process to stop: %v", err) - return err - } - - log.Printf("HardHat node has been stopped") - return nil -} - -func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int) ethereum.Network { +func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, workingDir string) ethereum.Network { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - isRunning, _ := isHardHatRunning() + isRunning, _ := cmd.IsHardHatRunning() if !isRunning { log.Printf("Hardhat is not running. Start new HardHat") - details := startHardHat(t, ctx, validatorsCount) + details := startHardHat(t, ctx, validatorsCount, workingDir) assert.NotNilf(t, details, "Expected details to be not nil") return details } if cleanStart { - err := stopHardHat() + err := cmd.StopHardHat() assert.Nilf(t, err, "Failed to stopHardHat") - details := startHardHat(t, ctx, validatorsCount) + details := startHardHat(t, ctx, validatorsCount, workingDir) assert.NotNilf(t, details, "Expected details to be not nil") return details } @@ -298,12 +196,8 @@ func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { acctAddressLowerCase := strings.ToLower(acctAddress) // Password - passwordPath := append(rootPath, "scripts") - passwordPath = append(passwordPath, "base-files") - passwordPath = append(passwordPath, "passwordFile") - passwordFullPath := filepath.Join(passwordPath...) - - passwordFileContent, err := ioutil.ReadFile(passwordFullPath) + passwordFull := filepath.Join(rootPath, "scripts", "base-files", "passwordFile") + passwordFileContent, err := ioutil.ReadFile(passwordFull) if err != nil { log.Printf("Error opening password file. %v", err) return nil, nil, err @@ -311,12 +205,8 @@ func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { password := string(passwordFileContent) // Wallet - walletPath := append(rootPath, "scripts") - walletPath = append(walletPath, "base-files") - walletPath = append(walletPath, acctAddressLowerCase) - walletFullPath := filepath.Join(walletPath...) - - jsonBytes, err := ioutil.ReadFile(walletFullPath) + walletFull := filepath.Join(rootPath, "scripts", "base-files", acctAddressLowerCase) + jsonBytes, err := ioutil.ReadFile(walletFull) if err != nil { log.Printf("Error opening %v file. %v", acctAddressLowerCase, err) return nil, nil, err @@ -361,41 +251,14 @@ func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { // return eth, logger, nil //} -func runScriptHardHatNode() error { - - rootPath := GetProjectRootPath() - scriptPath := GetMainScriptPath() - - cmd := exec.Cmd{ - Path: scriptPath, - Args: []string{scriptPath, scriptStartHardHatNode}, - Dir: filepath.Join(rootPath...), - } - - SetCommandStdOut(&cmd) - err := cmd.Start() - if err != nil { - log.Printf("Could not execute %s script: %v", scriptStartHardHatNode, err) - return err - } - - err = os.Setenv(envHardHatProcessId, strconv.Itoa(cmd.Process.Pid)) - if err != nil { - log.Printf("Error setting environment variable: %v", err) - return err - } - - return nil -} - func Init(workingDir string, n int) error { - err := cmd2.RunClean(workingDir) + err := cmd.RunClean(workingDir) if err != nil { return err } - err = cmd2.RunInit(workingDir, n) + err = cmd.RunInit(workingDir, n) if err != nil { return err } @@ -410,86 +273,6 @@ func Init(workingDir string, n int) error { return nil } -//func runScriptClean() error { -// -// rootPath := GetProjectRootPath() -// scriptPath := GetMainScriptPath() -// -// cmd := exec.Cmd{ -// Path: scriptPath, -// Args: []string{scriptPath, scriptClean}, -// Dir: filepath.Join(rootPath...), -// } -// -// SetCommandStdOut(&cmd) -// err := cmd.Start() -// if err != nil { -// log.Printf("Could not execute %s script: %v", scriptClean, err) -// return err -// } -// -// return nil -//} - -func runScriptDeployContracts(eth *ethereum.Details, ctx context.Context) error { - - rootPath := GetProjectRootPath() - scriptPath := GetMainScriptPath() - - err := os.Setenv(envSkipRegistration, "1") - if err != nil { - log.Printf("Error setting environment variable: %v", err) - return err - } - - cmd := exec.Cmd{ - Path: scriptPath, - Args: []string{scriptPath, scriptDeploy}, - Dir: filepath.Join(rootPath...), - } - - SetCommandStdOut(&cmd) - err = cmd.Run() - if err != nil { - log.Printf("Could not execute %s script: %v", scriptDeploy, err) - return err - } - - addr := common.Address{} - copy(addr[:], common.FromHex(configFactoryAddress)) - eth.Contracts().Initialize(ctx, addr) - - return nil -} - -func runScriptRegisterValidators(eth *ethereum.Details, validatorAddresses []string) error { - - rootPath := GetProjectRootPath() - scriptPath := GetMainScriptPath() - - args := []string{ - scriptPath, - scriptRegisterValidator, - eth.Contracts().ContractFactoryAddress().String(), - } - args = append(args, validatorAddresses...) - - cmd := exec.Cmd{ - Path: scriptPath, - Args: args, - Dir: filepath.Join(rootPath...), - } - - SetCommandStdOut(&cmd) - err := cmd.Run() - if err != nil { - log.Printf("Could not execute %s script: %v", scriptRegisterValidator, err) - return err - } - - return nil -} - // sendHardhatCommand sends a command to the hardhat server via an RPC call func sendHardhatCommand(command string, params ...interface{}) error { @@ -592,7 +375,7 @@ func SetBlockInterval(t *testing.T, eth ethereum.Network, intervalInMilliSeconds } // GetProjectRootPath returns the project root path -func GetProjectRootPath() []string { +func GetProjectRootPath() string { rootPath := []string{string(os.PathSeparator)} @@ -600,7 +383,7 @@ func GetProjectRootPath() []string { stdout, err := cmd.Output() if err != nil { log.Printf("Error getting project root path: %v", err) - return rootPath + return "" } path := string(stdout) @@ -612,39 +395,13 @@ func GetProjectRootPath() []string { rootPath = append(rootPath, pathNode) } - return rootPath -} - -// GetMainScriptPath return the path of the main.sh script -func GetMainScriptPath() string { - rootPath := GetProjectRootPath() - scriptPath := append(rootPath, "scripts") - scriptPath = append(scriptPath, "main.sh") - scriptPathJoined := filepath.Join(scriptPath...) - - return scriptPathJoined + return filepath.Join(rootPath...) } -// GetBridgePath return the path of the main.sh script +// GetBridgePath return the bridge folder path func GetBridgePath() string { rootPath := GetProjectRootPath() - scriptPath := append(rootPath, "bridge") - scriptPathJoined := filepath.Join(scriptPath...) - - return scriptPathJoined -} - -// SetCommandStdOut If ENABLE_SCRIPT_LOG env variable is set as 'true' the command will show scripts logs -func SetCommandStdOut(cmd *exec.Cmd) { + bridgePath := filepath.Join(rootPath, "bridge") - flagValue, found := os.LookupEnv("ENABLE_SCRIPT_LOG") - enabled, err := strconv.ParseBool(flagValue) - - if err == nil && found && enabled { - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - } else { - cmd.Stdout = io.Discard - cmd.Stderr = io.Discard - } + return bridgePath } From dc29c5787d14d39465c4c344867d8deff006f2d6 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Tue, 14 Jun 2022 14:30:03 +0200 Subject: [PATCH 08/15] removing bash script copying necessary files at the beginning golang rather than bash --- blockchain/ethereum/testing/ethereum_test.go | 2 +- .../executor/tasks/dkg/completion_test.go | 26 ++- .../dkg/dispute_share_distribution_test.go | 4 +- .../tasks/dkg/gpkj_submission_test.go | 4 +- .../executor/tasks/dkg/mpk_submission_test.go | 4 +- .../executor/tasks/dkg/register_test.go | 16 +- .../tasks/dkg/share_distribution_test.go | 4 +- .../tasks/dkg/testutils/advance_phases.go | 2 +- blockchain/testutils/cmd/clean.go | 39 +---- blockchain/testutils/cmd/deploy.go | 18 +- blockchain/testutils/cmd/executor.go | 162 ++++++++++++++++-- blockchain/testutils/cmd/git-hooks.go | 6 +- blockchain/testutils/cmd/hardhat.go | 5 +- blockchain/testutils/cmd/init.go | 91 ++++------ blockchain/testutils/cmd/register.go | 7 +- blockchain/testutils/cmd/setup.go | 76 ++++++++ blockchain/testutils/cmd/validator.go | 5 +- blockchain/testutils/setup.go | 47 +---- blockchain/testutils/setup_test.go | 2 +- 19 files changed, 326 insertions(+), 194 deletions(-) create mode 100644 blockchain/testutils/cmd/setup.go diff --git a/blockchain/ethereum/testing/ethereum_test.go b/blockchain/ethereum/testing/ethereum_test.go index 8338ca74..68e4bfb0 100644 --- a/blockchain/ethereum/testing/ethereum_test.go +++ b/blockchain/ethereum/testing/ethereum_test.go @@ -10,7 +10,7 @@ import ( ) func TestEthereum_AccountsFound(t *testing.T) { - eth := testutils.GetEthereumNetwork(t, false) + eth := testutils.GetEthereumNetwork(t, false, 4, "") defer eth.Close() accountList := eth.GetKnownAccounts() diff --git a/blockchain/executor/tasks/dkg/completion_test.go b/blockchain/executor/tasks/dkg/completion_test.go index d7857da2..901c2d10 100644 --- a/blockchain/executor/tasks/dkg/completion_test.go +++ b/blockchain/executor/tasks/dkg/completion_test.go @@ -19,11 +19,14 @@ import ( // We complete everything correctly, happy path func TestCompletion_Group_1_AllGood(t *testing.T) { - workingDir := cmd.CreateTempFolder() + workingDir, err := cmd.CreateTempFolder() + if err != nil { + assert.Fail(t, "Failing creating temporary folder", err) + } defer os.Remove(workingDir) n := 4 - err := testutils.Init(workingDir, n) + err = testutils.Init(workingDir, n) assert.Nil(t, err) suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) @@ -134,9 +137,15 @@ func TestCompletion_Group_1_StartFromCompletion(t *testing.T) { // This test is meant to raise an error resulting from an invalid argument // for the Ethereum interface. func TestCompletion_Group_2_Bad1(t *testing.T) { + workingDir, err := cmd.CreateTempFolder() + if err != nil { + assert.Fail(t, "Failing creating temporary folder", err) + } + defer os.Remove(workingDir) + logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, workingDir) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -149,15 +158,20 @@ func TestCompletion_Group_2_Bad1(t *testing.T) { task := dkg.NewCompletionTask(state, 1, 100) log := logger.WithField("TaskID", "foo") - err := task.Initialize(ctx, log, eth) + err = task.Initialize(ctx, log, eth) assert.NotNil(t, err) } // We test to ensure that everything behaves correctly. func TestCompletion_Group_2_Bad2(t *testing.T) { + workingDir, err := cmd.CreateTempFolder() + if err != nil { + assert.Fail(t, "Failing creating temporary folder", err) + } + defer os.Remove(workingDir) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, workingDir) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -170,7 +184,7 @@ func TestCompletion_Group_2_Bad2(t *testing.T) { log := logger.WithField("TaskID", "foo") task := dkg.NewCompletionTask(state, 1, 100) - err := task.Initialize(ctx, log, eth) + err = task.Initialize(ctx, log, eth) if err == nil { t.Fatal("Should have raised error") } diff --git a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go index e3952f3b..2fad07be 100644 --- a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go @@ -154,7 +154,7 @@ func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -179,7 +179,7 @@ func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { func TestDisputeShareDistributionTask_Group_2_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, "") defer eth.Close() accts := eth.GetKnownAccounts() diff --git a/blockchain/executor/tasks/dkg/gpkj_submission_test.go b/blockchain/executor/tasks/dkg/gpkj_submission_test.go index d6408880..d86be507 100644 --- a/blockchain/executor/tasks/dkg/gpkj_submission_test.go +++ b/blockchain/executor/tasks/dkg/gpkj_submission_test.go @@ -60,7 +60,7 @@ func TestGPKjSubmission_Group_1_GoodAllValid(t *testing.T) { func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -84,7 +84,7 @@ func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { func TestGPKjSubmission_Group_1_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/mpk_submission_test.go b/blockchain/executor/tasks/dkg/mpk_submission_test.go index 1fce188b..9e101e8e 100644 --- a/blockchain/executor/tasks/dkg/mpk_submission_test.go +++ b/blockchain/executor/tasks/dkg/mpk_submission_test.go @@ -111,7 +111,7 @@ func TestMPKSubmission_Group_1_Bad1(t *testing.T) { func TestMPKSubmission_Group_1_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -134,7 +134,7 @@ func TestMPKSubmission_Group_1_Bad2(t *testing.T) { func TestMPKSubmission_Group_2_Bad4(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/register_test.go b/blockchain/executor/tasks/dkg/register_test.go index 313d1901..52ce7650 100644 --- a/blockchain/executor/tasks/dkg/register_test.go +++ b/blockchain/executor/tasks/dkg/register_test.go @@ -28,7 +28,7 @@ func TestRegisterTask_Group_1_Task(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 5) + eth := testutils.GetEthereumNetwork(t, false, 5, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -106,7 +106,7 @@ func TestRegisterTask_Group_1_Task(t *testing.T) { func TestRegisterTask_Group_1_Good2(t *testing.T) { n := 6 _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false, 6) + eth := testutils.GetEthereumNetwork(t, false, 6, "") assert.NotNil(t, eth) defer eth.Close() @@ -193,7 +193,7 @@ func TestRegisterTask_Group_1_Good2(t *testing.T) { func TestRegisterTask_Group_1_Bad1(t *testing.T) { n := 5 _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false, 5) + eth := testutils.GetEthereumNetwork(t, false, 5, "") assert.NotNil(t, eth) defer eth.Close() @@ -252,7 +252,7 @@ func TestRegisterTask_Group_1_Bad1(t *testing.T) { func TestRegisterTask_Group_2_Bad2(t *testing.T) { n := 7 _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false, 7) + eth := testutils.GetEthereumNetwork(t, false, 7, "") assert.NotNil(t, eth) defer eth.Close() @@ -306,7 +306,7 @@ func TestRegisterTask_Group_2_Bad2(t *testing.T) { // The initialization should fail because we dont allow less than 4 validators func TestRegisterTask_Group_2_Bad4(t *testing.T) { - eth := testutils.GetEthereumNetwork(t, false, 3) + eth := testutils.GetEthereumNetwork(t, false, 3, "") assert.NotNil(t, eth) defer eth.Close() @@ -334,7 +334,7 @@ func TestRegisterTask_Group_2_Bad4(t *testing.T) { func TestRegisterTask_Group_2_Bad5(t *testing.T) { n := 5 _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false, 5) + eth := testutils.GetEthereumNetwork(t, false, 5, "") assert.NotNil(t, eth) defer eth.Close() @@ -399,7 +399,7 @@ func TestRegisterTask_Group_3_ShouldRetryFalse(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 5) + eth := testutils.GetEthereumNetwork(t, false, 5, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -486,7 +486,7 @@ func TestRegisterTask_Group_3_ShouldRetryTrue(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 5) + eth := testutils.GetEthereumNetwork(t, false, 5, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/share_distribution_test.go b/blockchain/executor/tasks/dkg/share_distribution_test.go index 17577d2c..1a407b19 100644 --- a/blockchain/executor/tasks/dkg/share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/share_distribution_test.go @@ -283,7 +283,7 @@ func TestShareDistribution_Group_2_Bad5(t *testing.T) { func TestShareDistribution_Group_2_Bad6(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 5) + eth := testutils.GetEthereumNetwork(t, false, 5, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -305,7 +305,7 @@ func TestShareDistribution_Group_2_Bad6(t *testing.T) { func TestShareDistribution_Group_3_Bad7(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4) + eth := testutils.GetEthereumNetwork(t, false, 4, "") defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/testutils/advance_phases.go b/blockchain/executor/tasks/dkg/testutils/advance_phases.go index 734d9823..fdb47d73 100644 --- a/blockchain/executor/tasks/dkg/testutils/advance_phases.go +++ b/blockchain/executor/tasks/dkg/testutils/advance_phases.go @@ -120,7 +120,7 @@ func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 1000*time.Millisecond) - eth := testutils.GetEthereumNetwork(t, false, n) + eth := testutils.GetEthereumNetwork(t, false, n, "") assert.NotNil(t, eth) ctx := context.Background() diff --git a/blockchain/testutils/cmd/clean.go b/blockchain/testutils/cmd/clean.go index c62aba36..e4691529 100644 --- a/blockchain/testutils/cmd/clean.go +++ b/blockchain/testutils/cmd/clean.go @@ -1,47 +1,18 @@ package cmd import ( - "github.com/MadBase/MadNet/blockchain/testutils" + "fmt" "os" "path/filepath" ) func RunClean(workingDir string) error { - // TODO - not needed? - // Remove folder content - //rootPath := testutils.GetProjectRootPath() - //generatedFolder := append(rootPath,"scripts","generated") - //err := os.RemoveAll(filepath.Join(generatedFolder...)) - //if err != nil { - // return err - //} - - // Create folders - folders := []string{ - filepath.Join("scripts", "generated", "monitorDBs"), - filepath.Join("scripts", "generated", "config"), - filepath.Join("scripts", "generated", "keystores", "keys"), - filepath.Join("scripts", "generated", "keystores", "passcodes.txt"), - } - for _, folder := range folders { - if err := os.Mkdir(filepath.Join(workingDir, folder), os.ModePerm); err != nil { - return err - } - } - - // Copy config files - rootPath := testutils.GetProjectRootPath() - srcGenesis := filepath.Join(rootPath, "scripts", "base-file", "genesis.json") - _, err := CopyFileToFolder(srcGenesis, filepath.Join(workingDir, "scripts", "generated")) + //Remove content + rootPath := GetProjectRootPath() + err := os.Remove(filepath.Join(rootPath, "keyfile.json")) if err != nil { - return err + fmt.Print("Trying to remove a file that can or cannot be here. Not a problem") } - srcKey := filepath.Join(rootPath, "scripts", "base-file", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f") - _, err = CopyFileToFolder(srcKey, filepath.Join(workingDir, "scripts", "generated", "keystores", "keys")) - if err != nil { - return err - } - return nil } diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go index b569a4f0..876975ba 100644 --- a/blockchain/testutils/cmd/deploy.go +++ b/blockchain/testutils/cmd/deploy.go @@ -1,7 +1,6 @@ package cmd import ( - "github.com/MadBase/MadNet/blockchain/testutils" "io/ioutil" "log" "os" @@ -10,12 +9,11 @@ import ( func RunDeploy(workingDir string) error { - hardhatNodeBaseCmd := "npx hardhat --network dev" factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this - rootPath := testutils.GetProjectRootPath() - bridgeDir := testutils.GetBridgePath() + rootPath := GetProjectRootPath() + bridgeDir := GetBridgePath() - _, err := executeCommand(bridgeDir, "npx hardhat setHardhatIntervalMining --network dev --enable-auto-mine") + _, _, err := executeCommand(bridgeDir, "npx hardhat setHardhatIntervalMining --network dev --enable-auto-mine") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -43,7 +41,7 @@ func RunDeploy(workingDir string) error { } //npx hardhat --network "$NETWORK" --show-stack-traces deployContracts --input-folder ../scripts/generated - _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "--show-stack-traces deployContracts --input-folder", workingDir) + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --show-stack-traces deployContracts --input-folder", workingDir) if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -86,7 +84,7 @@ func RunDeploy(workingDir string) error { //mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" // npx hardhat fundValidators --network $NETWORK - _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "fundValidators") + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev fundValidators") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -113,7 +111,7 @@ func RunDeploy(workingDir string) error { //cd $BRIDGE_DIR //cd $CURRENT_WD //npx hardhat setHardhatIntervalMining --network $NETWORK --interval 1000 - _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setHardhatIntervalMining --interval 1000") + _, _, err = executeCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining --interval 1000") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -129,14 +127,14 @@ func RunDeploy(workingDir string) error { // //cd $BRIDGE_DIR //npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 10 - _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) + _, _, err = executeCommand(bridgeDir, "npx", "hardhat setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) if err != nil { log.Printf("Could not execute script: %v", err) return err } //npx hardhat setHardhatIntervalMining --network $NETWORK - _, err = executeCommand(bridgeDir, hardhatNodeBaseCmd, "setHardhatIntervalMining") + _, _, err = executeCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining") if err != nil { log.Printf("Could not execute script: %v", err) return err diff --git a/blockchain/testutils/cmd/executor.go b/blockchain/testutils/cmd/executor.go index 5af4e4ad..e8baca66 100644 --- a/blockchain/testutils/cmd/executor.go +++ b/blockchain/testutils/cmd/executor.go @@ -1,12 +1,16 @@ package cmd import ( + "crypto/rand" + "encoding/hex" "fmt" "io" "io/ioutil" "log" "os" "os/exec" + "path/filepath" + "regexp" "strconv" "strings" ) @@ -28,35 +32,48 @@ func SetCommandStdOut(cmd *exec.Cmd) { } } -func executeCommand(dir string, command ...string) (exec.Cmd, error) { - args := strings.Split(strings.Join(command, " "), " ") - cmd := exec.Cmd{ - Args: args, - Dir: dir, - Stdin: os.Stdout, - Stdout: os.Stdin, - Stderr: os.Stderr, +func executeCommand(dir, command string, args ...string) (*exec.Cmd, []byte, error) { + cmdArgs := strings.Split(strings.Join(args, " "), " ") + + cmd := exec.Command(command, cmdArgs...) + cmd.Dir = dir + output, err := cmd.Output() + + if err != nil { + fmt.Printf("Error executing command: %v in dir: %v. %v", command, dir, err) + return &exec.Cmd{}, nil, err } + return cmd, output, err + +} + +func runCommand(dir, command string, args ...string) (*exec.Cmd, []byte, error) { + cmdArgs := strings.Split(strings.Join(args, " "), " ") + + cmd := exec.Command(command, cmdArgs...) + cmd.Dir = dir err := cmd.Start() if err != nil { - log.Printf("Could not execute command: %v", args) - return exec.Cmd{}, err + fmt.Printf("Error executing command: %v in dir: %v. %v", command, dir, err) + return &exec.Cmd{}, nil, err } + return cmd, nil, err - return cmd, nil } -func CreateTempFolder() string { - // create tmp folder - file, err := ioutil.TempFile("dir", "prefix") +// TODO - make it wait() +// CreateTempFolder creates a test working folder in the OS temporary resources folder +func CreateTempFolder() (string, error) { + file, err := ioutil.TempDir("", "unittest") if err != nil { - log.Fatal(err) + return "", err } - return file.Name() // For example "dir/prefix054003078" + return file, nil } +// TODO - make it wait() func CopyFileToFolder(src, dst string) (int64, error) { sourceFileStat, err := os.Stat(src) if err != nil { @@ -73,6 +90,7 @@ func CopyFileToFolder(src, dst string) (int64, error) { } defer source.Close() + _, err = os.Create(dst) destination, err := os.Create(dst) if err != nil { return 0, err @@ -81,3 +99,115 @@ func CopyFileToFolder(src, dst string) (int64, error) { nBytes, err := io.Copy(destination, source) return nBytes, err } + +// GetProjectRootPath returns the project root path +func GetProjectRootPath() string { + + rootPath := []string{string(os.PathSeparator)} + + cmd := exec.Command("go", "list", "-m", "-f", "'{{.Dir}}'", "github.com/MadBase/MadNet") + stdout, err := cmd.Output() + if err != nil { + log.Printf("Error getting project root path: %v", err) + return "" + } + path := string(stdout) + path = strings.ReplaceAll(path, "'", "") + path = strings.ReplaceAll(path, "\n", "") + + pathNodes := strings.Split(path, string(os.PathSeparator)) + for _, pathNode := range pathNodes { + rootPath = append(rootPath, pathNode) + } + + return filepath.Join(rootPath...) +} + +// GetBridgePath return the bridge folder path +func GetBridgePath() string { + rootPath := GetProjectRootPath() + bridgePath := filepath.Join(rootPath, "bridge") + + return bridgePath +} + +//RandomHex hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/urandom +func RandomHex(n int) (string, error) { + bytes := make([]byte, n) + if _, err := rand.Read(bytes); err != nil { + return "", err + } + + return strings.ToUpper(hex.EncodeToString(bytes)), nil +} + +func ReplaceConfigurationFile(workingDir, address, privateKey string, listeningPort, p2pPort, discoveryPort, localStatePort, index int) error { + baseConfigFile, err := os.ReadFile(filepath.Join(workingDir, "scripts", "base-files", "baseConfig")) + if err != nil { + log.Fatalf("Error reading base configuration file - %v", err) + return err + } + fileContent := string(baseConfigFile) + validatorFileName := "validator" + strconv.Itoa(index) + + regex := regexp.MustCompile(`defaultAccount = .*`) + result := regex.ReplaceAllString(fileContent, "defaultAccount = \""+address+"\"") + regex = regexp.MustCompile(`rewardAccount = .*`) + result = regex.ReplaceAllString(result, "rewardAccount = \""+address+"\"") + regex = regexp.MustCompile(`listeningAddress = .*`) + result = regex.ReplaceAllString(result, "listeningAddress = \"0.0.0.0:"+strconv.Itoa(listeningPort)+"\"") + regex = regexp.MustCompile(`p2pListeningAddress = .*`) + result = regex.ReplaceAllString(result, "p2pListeningAddress = \"0.0.0.0:"+strconv.Itoa(p2pPort)+"\"") + regex = regexp.MustCompile(`discoveryListeningAddress = .*`) + result = regex.ReplaceAllString(result, "discoveryListeningAddress = \"0.0.0.0:"+strconv.Itoa(discoveryPort)+"\"") + regex = regexp.MustCompile(`localStateListeningAddress = .*`) + result = regex.ReplaceAllString(result, "localStateListeningAddress = \"0.0.0.0:"+strconv.Itoa(localStatePort)+"\"") + regex = regexp.MustCompile(`passcodes = .*`) + result = regex.ReplaceAllString(result, "passcodes = \""+filepath.Join("scripts", "generated", "keystores", "passcodes.txt")+"\"") // TODO - check file path root project or working dir + regex = regexp.MustCompile(`keystore = .*`) + result = regex.ReplaceAllString(result, "keystore = \""+filepath.Join("scripts", "generated", "keystores", "keys")+"\"") + regex = regexp.MustCompile(`stateDB = .*`) + result = regex.ReplaceAllString(result, "stateDB = \""+filepath.Join("scripts", "generated", "stateDBs", validatorFileName)+"\"") + regex = regexp.MustCompile(`monitorDB = .*`) + result = regex.ReplaceAllString(result, "monitorDB = \""+filepath.Join("scripts", "generated", "monitorDBs", validatorFileName)+"\"") + regex = regexp.MustCompile(`privateKey = .*`) + result = regex.ReplaceAllString(result, "privateKey = \""+privateKey+"\"") + + f, err := os.Create(filepath.Join(workingDir, "scripts", "generated", "config", validatorFileName+".toml")) + if err != nil { + log.Fatalf("Error creating validator configuration file - %v", err) + return err + } + _, err = fmt.Fprintf(f, "%s", result) + if err != nil { + log.Fatalf("Error writing on validator configuration file - %v", err) + return err + } + defer f.Close() + return nil +} + +func ReplaceGenesisBalance(workingDir string) error { + genesisFilePath := filepath.Join(workingDir, "scripts", "base-files", "genesis.json") + genesisConfigFile, err := os.ReadFile(genesisFilePath) + if err != nil { + log.Fatalf("Error reading base configuration file - %v", err) + return err + } + fileContent := string(genesisConfigFile) + regex := regexp.MustCompile(`balance.*`) + result := regex.ReplaceAllString(fileContent, "balance\": \"10000000000000000000000\" }") + + f, err := os.Create(genesisFilePath) + if err != nil { + log.Fatalf("Error creating modified genesis.json file - %v", err) + return err + } + _, err = fmt.Fprintf(f, "%s", result) + if err != nil { + log.Fatalf("Error writing on new genesis.json file - %v", err) + return err + } + defer f.Close() + return nil +} diff --git a/blockchain/testutils/cmd/git-hooks.go b/blockchain/testutils/cmd/git-hooks.go index f73430fa..6e22e514 100644 --- a/blockchain/testutils/cmd/git-hooks.go +++ b/blockchain/testutils/cmd/git-hooks.go @@ -1,18 +1,16 @@ package cmd import ( - "github.com/MadBase/MadNet/blockchain/testutils" "log" ) func RunGitHooks() error { - rootPath := testutils.GetProjectRootPath() - _, err := executeCommand(rootPath, "git config core.hooksPath scripts/githooks 2>/dev/null") + rootPath := GetProjectRootPath() + _, _, err := executeCommand(rootPath, "git", "config core.hooksPath scripts/githooks") if err != nil { log.Printf("Could not execute script: %v", err) return err } - return nil } diff --git a/blockchain/testutils/cmd/hardhat.go b/blockchain/testutils/cmd/hardhat.go index 05f66e67..8353e48d 100644 --- a/blockchain/testutils/cmd/hardhat.go +++ b/blockchain/testutils/cmd/hardhat.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "github.com/MadBase/MadNet/blockchain/ethereum" - "github.com/MadBase/MadNet/blockchain/testutils" "log" "net/http" "os" @@ -37,8 +36,8 @@ func IsHardHatRunning() (bool, error) { func RunHardHatNode() error { - bridgePath := testutils.GetBridgePath() - cmd, err := executeCommand(bridgePath, "npx hardhat node --show-stack-traces") + bridgePath := GetBridgePath() + cmd, _, err := runCommand(bridgePath, "npx", "hardhat", "node", "--show-stack-traces") if err != nil { return err } diff --git a/blockchain/testutils/cmd/init.go b/blockchain/testutils/cmd/init.go index 200a6ff8..c2109a0e 100644 --- a/blockchain/testutils/cmd/init.go +++ b/blockchain/testutils/cmd/init.go @@ -3,102 +3,85 @@ package cmd import ( "errors" "fmt" - "github.com/MadBase/MadNet/blockchain/testutils" "os" "path/filepath" + "strings" ) func RunInit(workingDir string, numbersOfValidator int) error { - err := RunGitHooks() + // Resources setup + err := RunSetup(workingDir) if err != nil { return err } - LA := 4242 - PA := 4343 - DA := 4444 - LSA := 8884 + err = RunGitHooks() + if err != nil { + return err + } - //# Check that number of validators is valid - //if ! [[ $1 =~ $re ]] || [[ $1 -lt 4 ]] || [[ $1 -gt 32 ]]; then - //echo -e "Invalid number of validators [4-32]" - //exit 1 - //fi + // Ports + listeningPort := 4242 + p2pPort := 4343 + discoveryPort := 4444 + localStatatePort := 8884 + + // Validator instance check if numbersOfValidator < 4 || numbersOfValidator > 32 { return errors.New("number of possible validators can be from 4 up to 32") } - // TODO - Not necessary - //if [ -f "./scripts/generated/genesis.json" ]; then - //echo -e "Generated files already exist, run clean" - //exit 1 - //fi - //CLEAN_UP - - rootPath := testutils.GetProjectRootPath() + rootPath := GetProjectRootPath() for i := 1; i < numbersOfValidator; i++ { - // TODO change to working direcotir password file - cmd, err := executeCommand(rootPath, "ethkey generate --passwordfile ./scripts/base-files/passwordFile | cut -d' ' -f2") + + passwordFilePath := filepath.Join(workingDir, "scripts", "base-files", "passwordFile") + _, stdout, err := executeCommand(rootPath, "ethkey", "generate --passwordfile "+passwordFilePath) if err != nil { return err } - stdout, err := cmd.Output() - ADDRESS := stdout + address := string(stdout[:]) + address = strings.ReplaceAll(address, "Address: ", "") + address = strings.ReplaceAll(address, "\n", "") + + // Remove keyfile.json + err = os.Remove(filepath.Join(rootPath, "keyfile.json")) + if err != nil { + fmt.Print("Trying to remove keyfile.json that can or cannot be here. Not a problem") + } - cmd, err = executeCommand(rootPath, "hexdump -n 16 -e '4/4 \"%08X\" 1 \"\\n\"' /dev/urandom") + // Generate private key + privateKey, err := RandomHex(16) if err != nil { return err } - stdout, err = cmd.Output() - PK := stdout - // TODO - use temp working dir and validator number - sedCommand := fmt.Sprintf(` - sed -e 's/defaultAccount = .*/defaultAccount = \"'"%s"'\"/' ./scripts/base-files/baseConfig | - sed -e 's/rewardAccount = .*/rewardAccount = \"'"%s"'\"/' | - sed -e 's/listeningAddress = .*/listeningAddress = \"0.0.0.0:'"%s"'\"/' | - sed -e 's/p2pListeningAddress = .*/p2pListeningAddress = \"0.0.0.0:'"%s"'\"/' | - sed -e 's/discoveryListeningAddress = .*/discoveryListeningAddress = \"0.0.0.0:'"%s"'\"/' | - sed -e 's/localStateListeningAddress = .*/localStateListeningAddress = \"0.0.0.0:'"%s"'\"/' | - sed -e 's/passcodes = .*/passcodes = \"scripts\/generated\/keystores\/passcodes.txt\"/' | - sed -e 's/keystore = .*/keystore = \"scripts\/generated\/keystores\/keys\"/' | - sed -e 's/stateDB = .*/stateDB = \"scripts\/generated\/stateDBs\/validator'"%d"'\/\"/' | - sed -e 's/monitorDB = .*/monitorDB = \"scripts\/generated\/monitorDBs\/validator'"%d"'\/\"/' | - sed -e 's/privateKey = .*/privateKey = \"'"%s"'\"/' > ./scripts/generated/config/validator%d.toml`, - ADDRESS, ADDRESS, LA, PA, DA, LSA, i, i, PK, i) - _, err = executeCommand(rootPath, sedCommand) + // Validator configuration file + err = ReplaceConfigurationFile(workingDir, address, privateKey, listeningPort, p2pPort, discoveryPort, localStatatePort, i) if err != nil { return err } - //echo "$ADDRESS=abc123" >> ./scripts/generated/keystores/passcodes.txt f, err := os.Create(filepath.Join(workingDir, "scripts", "generated", "keystores", "passcodes.txt")) if err != nil { return err } defer f.Close() - _, err = f.WriteString(fmt.Sprintf("%s=abc123", ADDRESS)) + _, err = f.WriteString(fmt.Sprintf("%s=abc123", address)) if err != nil { return err } - // TODO - does this exists? - //mv ./keyfile.json ./scripts/generated/keystores/keys/$ADDRESS // Genesis - genesisPath := filepath.Join(workingDir, "scripts", "generated", "genesis.json") - jqCommand := fmt.Sprintf(` - jq '.alloc += {"'"$(echo %s | cut -c3-)"'": {balance:"10000000000000000000000"}}' %s > %s.tmp && mv %s.tmp %s - `, ADDRESS, genesisPath, genesisPath, genesisPath, genesisPath) - _, err = executeCommand(rootPath, jqCommand) + err = ReplaceGenesisBalance(workingDir) if err != nil { return err } - LA = LA + 1 - PA = PA + 1 - DA = DA + 1 - LSA = LSA + 1 + listeningPort += 1 + p2pPort += 1 + discoveryPort += 1 + localStatatePort += 1 } return nil diff --git a/blockchain/testutils/cmd/register.go b/blockchain/testutils/cmd/register.go index d88a01d7..94295da0 100644 --- a/blockchain/testutils/cmd/register.go +++ b/blockchain/testutils/cmd/register.go @@ -1,7 +1,6 @@ package cmd import ( - "github.com/MadBase/MadNet/blockchain/testutils" "io/ioutil" "log" "path/filepath" @@ -10,8 +9,8 @@ import ( func RunRegister() error { - bridgeDir := testutils.GetBridgePath() - rootDir := testutils.GetProjectRootPath() + bridgeDir := GetBridgePath() + rootDir := GetProjectRootPath() factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this // TODO - get the right path keys := filepath.Join(rootDir, "scripts", "generated", "keystores", "keys") @@ -27,7 +26,7 @@ func RunRegister() error { } // Register validator - _, err = executeCommand(bridgeDir, "npx hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) if err != nil { log.Printf("Could not execute script: %v", err) return err diff --git a/blockchain/testutils/cmd/setup.go b/blockchain/testutils/cmd/setup.go new file mode 100644 index 00000000..6e7bc541 --- /dev/null +++ b/blockchain/testutils/cmd/setup.go @@ -0,0 +1,76 @@ +package cmd + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path/filepath" +) + +func RunSetup(workingDir string) error { + + // Create directories + folders := []string{ + filepath.Join("scripts", "base-files"), + filepath.Join("scripts", "generated", "monitorDBs"), + filepath.Join("scripts", "generated", "config"), + filepath.Join("scripts", "generated", "keystores"), + filepath.Join("assets", "test", "keys"), + } + for _, folder := range folders { + if err := os.MkdirAll(filepath.Join(workingDir, folder), os.ModePerm); err != nil { + fmt.Printf("Error creating configuration folders: %v", err) + return err + } + } + + // Copy configuration files + rootPath := GetProjectRootPath() + configurationFileDir := filepath.Join(rootPath, "scripts", "base-files") + files, err := ioutil.ReadDir(configurationFileDir) + if err != nil { + log.Fatalf("Error reading configuaration file dir path: %s", configurationFileDir) + return err + } + for _, file := range files { + fmt.Println(file.Name(), file.IsDir()) + src := filepath.Join(configurationFileDir, file.Name()) + dst := filepath.Join(workingDir, "scripts", "base-files", file.Name()) + _, err = CopyFileToFolder(src, dst) + if err != nil { + log.Fatalf("Error copying config file to working directory", err) + return err + } + } + + // Copy asset files + assetFileDir := filepath.Join(rootPath, "assets", "test", "keys") + files, err = ioutil.ReadDir(assetFileDir) + if err != nil { + log.Fatalf("Error reading asset file dir path: %s", assetFileDir) + return err + } + for _, file := range files { + fmt.Println(file.Name(), file.IsDir()) + src := filepath.Join(assetFileDir, file.Name()) + dst := filepath.Join(workingDir, "assets", "test", "keys", file.Name()) + _, err = CopyFileToFolder(src, dst) + if err != nil { + log.Fatalf("Error copying assets file to working directory: %v", err) + return err + } + } + _, err = CopyFileToFolder(filepath.Join(rootPath, "assets", "test", "blockheaders.txt"), filepath.Join(workingDir, "assets", "test", "blockheaders.txt")) + if err != nil { + log.Fatalf("Error reading asset blockheaders: %s", assetFileDir) + return err + } + _, err = CopyFileToFolder(filepath.Join(rootPath, "assets", "test", "passcodes.txt"), filepath.Join(workingDir, "assets", "test", "passcodes.txt")) + if err != nil { + log.Fatalf("Error reading asset passcodes: %s", assetFileDir) + return err + } + + return nil +} diff --git a/blockchain/testutils/cmd/validator.go b/blockchain/testutils/cmd/validator.go index 8f388b02..7b98ad0c 100644 --- a/blockchain/testutils/cmd/validator.go +++ b/blockchain/testutils/cmd/validator.go @@ -2,16 +2,15 @@ package cmd import ( "fmt" - "github.com/MadBase/MadNet/blockchain/testutils" "path/filepath" ) func RunValidator(validatorIndex int) error { - rootDir := testutils.GetProjectRootPath() + rootDir := GetProjectRootPath() validatorConfigPath := filepath.Join(rootDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) - _, err := executeCommand(rootDir, "./madnet --config", validatorConfigPath, "validator") + _, _, err := executeCommand(rootDir, "./madnet", "--config", validatorConfigPath, "validator") if err != nil { return err diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index d3d39bf4..52328b2b 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -19,8 +19,6 @@ import ( "log" "math/big" "net/http" - "os" - "os/exec" "path/filepath" "strconv" "strings" @@ -34,11 +32,10 @@ var ( configFinalityDelay = uint64(1) ) -func getEthereumDetails() (*ethereum.Details, error) { +func getEthereumDetails(workingDir string) (*ethereum.Details, error) { - rootPath := GetProjectRootPath() - assetKey := filepath.Join(rootPath, "assets", "test", "keys") - assetPasscode := filepath.Join(rootPath, "assets", "test", "passcodes.txt") + assetKey := filepath.Join(workingDir, "assets", "test", "keys") + assetPasscode := filepath.Join(workingDir, "assets", "test", "passcodes.txt") details, err := ethereum.NewEndpoint( configEndpoint, @@ -61,7 +58,7 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workin err = cmd.WaitForHardHatNode(ctx) assert.Nilf(t, err, "Failed to wait for hardhat to be up and running") - details, err := getEthereumDetails() + details, err := getEthereumDetails(workingDir) assert.Nilf(t, err, "Failed to build Ethereum endpoint") assert.NotNilf(t, details, "Ethereum network should not be Nil") @@ -120,7 +117,7 @@ func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, work return details } - network, err := getEthereumDetails() + network, err := getEthereumDetails(workingDir) assert.Nilf(t, err, "Failed to build Ethereum endpoint") assert.NotNilf(t, network, "Ethereum network should not be Nil") @@ -189,7 +186,7 @@ func InitializePrivateKeysAndAccounts(n int) ([]*ecdsa.PrivateKey, []accounts.Ac } func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { - rootPath := GetProjectRootPath() + rootPath := cmd.GetProjectRootPath() // Account acctAddress := configDefaultAccount @@ -373,35 +370,3 @@ func SetBlockInterval(t *testing.T, eth ethereum.Network, intervalInMilliSeconds panic(err) } } - -// GetProjectRootPath returns the project root path -func GetProjectRootPath() string { - - rootPath := []string{string(os.PathSeparator)} - - cmd := exec.Command("go", "list", "-m", "-f", "'{{.Dir}}'", "github.com/MadBase/MadNet") - stdout, err := cmd.Output() - if err != nil { - log.Printf("Error getting project root path: %v", err) - return "" - } - - path := string(stdout) - path = strings.ReplaceAll(path, "'", "") - path = strings.ReplaceAll(path, "\n", "") - - pathNodes := strings.Split(path, string(os.PathSeparator)) - for _, pathNode := range pathNodes { - rootPath = append(rootPath, pathNode) - } - - return filepath.Join(rootPath...) -} - -// GetBridgePath return the bridge folder path -func GetBridgePath() string { - rootPath := GetProjectRootPath() - bridgePath := filepath.Join(rootPath, "bridge") - - return bridgePath -} diff --git a/blockchain/testutils/setup_test.go b/blockchain/testutils/setup_test.go index b20e730f..56026b1e 100644 --- a/blockchain/testutils/setup_test.go +++ b/blockchain/testutils/setup_test.go @@ -21,7 +21,7 @@ func TestConnectSimulatorEndpoint(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := GetEthereumNetwork(t, tt.cleanStart); !reflect.DeepEqual(got, tt.want) { + if got := GetEthereumNetwork(t, tt.cleanStart, 8, ""); !reflect.DeepEqual(got, tt.want) { t.Errorf("GetEthereumNetwork() = %v, want %v", got, tt.want) } }) From 6bfb1ea624c10779becd6f74802c9036ef00f45d Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Tue, 14 Jun 2022 17:18:11 +0200 Subject: [PATCH 09/15] Adding test working directory Remove not used anymore RunClean script Refactoring better logging copying missing conf and assets files --- .../executor/tasks/dkg/completion_test.go | 23 +++++------ .../executor/tasks/dkg/dispute_gpkj_test.go | 10 +++-- .../tasks/dkg/dispute_missing_gpkj_test.go | 16 +++++--- .../dkg/dispute_missing_key_shares_test.go | 10 +++-- .../dkg/dispute_missing_registration_test.go | 16 +++++--- ...dispute_missing_share_distribution_test.go | 19 ++++++--- .../dkg/dispute_share_distribution_test.go | 13 ++++-- .../tasks/dkg/gpkj_submission_test.go | 13 ++++-- .../tasks/dkg/keyshare_submission_test.go | 10 +++-- .../executor/tasks/dkg/mpk_submission_test.go | 16 +++++--- .../tasks/dkg/share_distribution_test.go | 22 ++++++---- .../tasks/dkg/testutils/advance_phases.go | 24 +++++------ blockchain/testutils/cmd/clean.go | 18 -------- blockchain/testutils/cmd/deploy.go | 41 +++++-------------- blockchain/testutils/cmd/executor.go | 12 +++++- blockchain/testutils/cmd/init.go | 16 +++++--- blockchain/testutils/cmd/register.go | 8 ++-- blockchain/testutils/cmd/setup.go | 13 +++++- blockchain/testutils/cmd/validator.go | 6 +-- blockchain/testutils/setup.go | 9 +--- 20 files changed, 173 insertions(+), 142 deletions(-) delete mode 100644 blockchain/testutils/cmd/clean.go diff --git a/blockchain/executor/tasks/dkg/completion_test.go b/blockchain/executor/tasks/dkg/completion_test.go index 901c2d10..c7cbfcd9 100644 --- a/blockchain/executor/tasks/dkg/completion_test.go +++ b/blockchain/executor/tasks/dkg/completion_test.go @@ -19,17 +19,12 @@ import ( // We complete everything correctly, happy path func TestCompletion_Group_1_AllGood(t *testing.T) { - workingDir, err := cmd.CreateTempFolder() - if err != nil { - assert.Fail(t, "Failing creating temporary folder", err) - } - defer os.Remove(workingDir) - + workingDir := cmd.CreateTestWorkingFolder() n := 4 - err = testutils.Init(workingDir, n) + err := testutils.Init(workingDir, n) assert.Nil(t, err) - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -88,8 +83,9 @@ func TestCompletion_Group_1_AllGood(t *testing.T) { } func TestCompletion_Group_1_StartFromCompletion(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromCompletion(t, n, 100) + suite := dkgTestUtils.StartFromCompletion(t, n, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -192,8 +188,9 @@ func TestCompletion_Group_2_Bad2(t *testing.T) { // We complete everything correctly, but we do not complete in time func TestCompletion_Group_2_Bad3(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -241,8 +238,9 @@ func TestCompletion_Group_2_Bad3(t *testing.T) { } func TestCompletion_Group_3_ShouldRetry_returnsFalse(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromCompletion(t, n, 40) + suite := dkgTestUtils.StartFromCompletion(t, n, 40, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -278,8 +276,9 @@ func TestCompletion_Group_3_ShouldRetry_returnsFalse(t *testing.T) { } func TestCompletion_Group_3_ShouldRetry_returnsTrue(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth diff --git a/blockchain/executor/tasks/dkg/dispute_gpkj_test.go b/blockchain/executor/tasks/dkg/dispute_gpkj_test.go index 48f436cd..f8421cd8 100644 --- a/blockchain/executor/tasks/dkg/dispute_gpkj_test.go +++ b/blockchain/executor/tasks/dkg/dispute_gpkj_test.go @@ -4,6 +4,7 @@ package dkg_test import ( "context" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "math/big" "testing" @@ -20,9 +21,10 @@ import ( // We test to ensure that everything behaves correctly. func TestGPKjDispute_NoBadGPKj(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 unsubmittedGPKj := 0 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -84,9 +86,10 @@ func TestGPKjDispute_NoBadGPKj(t *testing.T) { // Here, we have a malicious gpkj submission. func TestGPKjDispute_1Invalid(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 unsubmittedGPKj := 0 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -162,9 +165,10 @@ func TestGPKjDispute_1Invalid(t *testing.T) { // We test to ensure that everything behaves correctly. // Here, we have a malicious accusation. func TestGPKjDispute_GoodMaliciousAccusation(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 unsubmittedGPKj := 0 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() diff --git a/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go b/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go index 6d84be0b..a38b5155 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_gpkj_test.go @@ -4,6 +4,7 @@ package dkg_test import ( "context" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "testing" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" @@ -14,9 +15,10 @@ import ( ) func TestDisputeMissingGPKjTask_Group_1_FourUnsubmittedGPKj_DoWork_Success(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 10 unsubmittedGPKj := 4 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -66,9 +68,10 @@ func TestDisputeMissingGPKjTask_Group_1_FourUnsubmittedGPKj_DoWork_Success(t *te } func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_False(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 unsubmittedKeyShares := 1 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 300) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 300, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -114,9 +117,10 @@ func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_False(t *testing.T) { } func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_True(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 unsubmittedKeyShares := 1 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -160,8 +164,9 @@ func TestDisputeMissingGPKjTask_Group_1_ShouldRetry_True(t *testing.T) { } func TestDisputeMissingGPKjTask_Group_2_ShouldAccuseOneValidatorWhoDidNotDistributeGPKjAndAnotherSubmittedBadGPKj(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromGPKjPhase(t, n, []int{4}, []int{3}, 100) + suite := dkgTestUtils.StartFromGPKjPhase(t, n, []int{4}, []int{3}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -208,8 +213,9 @@ func TestDisputeMissingGPKjTask_Group_2_ShouldAccuseOneValidatorWhoDidNotDistrib } func TestDisputeMissingGPKjTask_Group_2_ShouldAccuseTwoValidatorWhoDidNotDistributeGPKjAndAnotherTwoSubmittedBadGPKj(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromGPKjPhase(t, n, []int{3, 4}, []int{1, 2}, 100) + suite := dkgTestUtils.StartFromGPKjPhase(t, n, []int{3, 4}, []int{1, 2}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() diff --git a/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go b/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go index 73b1de02..c2ef7ad7 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_key_shares_test.go @@ -4,6 +4,7 @@ package dkg_test import ( "context" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "testing" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" @@ -14,9 +15,10 @@ import ( ) func TestDisputeMissingKeySharesTask_FourUnsubmittedKeyShare_DoWork_Success(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 unsubmittedKeyShares := 4 - suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100) + suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -75,9 +77,10 @@ func TestDisputeMissingKeySharesTask_FourUnsubmittedKeyShare_DoWork_Success(t *t } func TestDisputeMissingKeySharesTask_ShouldRetry_False(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 unsubmittedKeyShares := 1 - suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 40) + suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 40, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -132,9 +135,10 @@ func TestDisputeMissingKeySharesTask_ShouldRetry_False(t *testing.T) { } func TestDisputeMissingKeySharesTask_ShouldRetry_True(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 unsubmittedKeyShares := 1 - suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100) + suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth diff --git a/blockchain/executor/tasks/dkg/dispute_missing_registration_test.go b/blockchain/executor/tasks/dkg/dispute_missing_registration_test.go index c6df0e28..4d5507d7 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_registration_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_registration_test.go @@ -4,6 +4,7 @@ package dkg_test import ( "context" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "testing" "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" @@ -12,9 +13,10 @@ import ( ) func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessOneParticipantAccused(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 d := 1 - suite := testutils.StartFromRegistrationOpenPhase(t, n, d, 100) + suite := testutils.StartFromRegistrationOpenPhase(t, n, d, 100, workingDir) defer suite.Eth.Close() ctx, cancel := context.WithCancel(context.Background()) @@ -41,9 +43,10 @@ func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessOneParticipantAccus } func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessThreeParticipantAccused(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 d := 3 - suite := testutils.StartFromRegistrationOpenPhase(t, n, d, 100) + suite := testutils.StartFromRegistrationOpenPhase(t, n, d, 100, workingDir) defer suite.Eth.Close() ctx, cancel := context.WithCancel(context.Background()) @@ -70,9 +73,10 @@ func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessThreeParticipantAcc } func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessAllParticipantsAreBad(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 d := 5 - suite := testutils.StartFromRegistrationOpenPhase(t, n, d, 100) + suite := testutils.StartFromRegistrationOpenPhase(t, n, d, 100, workingDir) defer suite.Eth.Close() ctx, cancel := context.WithCancel(context.Background()) @@ -99,7 +103,8 @@ func TestDisputeMissingRegistrationTask_Group_1_DoTaskSuccessAllParticipantsAreB } func TestDisputeMissingRegistrationTask_Group_2_ShouldRetryTrue(t *testing.T) { - suite := testutils.StartFromRegistrationOpenPhase(t, 5, 1, 100) + workingDir := cmd.CreateTestWorkingFolder() + suite := testutils.StartFromRegistrationOpenPhase(t, 5, 1, 100, workingDir) defer suite.Eth.Close() ctx, cancel := context.WithCancel(context.Background()) @@ -118,7 +123,8 @@ func TestDisputeMissingRegistrationTask_Group_2_ShouldRetryTrue(t *testing.T) { } func TestDisputeMissingRegistrationTask_Group_2_ShouldNotRetryAfterSuccessfullyAccusingAllMissingParticipants(t *testing.T) { - suite := testutils.StartFromRegistrationOpenPhase(t, 5, 0, 100) + workingDir := cmd.CreateTestWorkingFolder() + suite := testutils.StartFromRegistrationOpenPhase(t, 5, 0, 100, workingDir) defer suite.Eth.Close() ctx, cancel := context.WithCancel(context.Background()) diff --git a/blockchain/executor/tasks/dkg/dispute_missing_share_distribution_test.go b/blockchain/executor/tasks/dkg/dispute_missing_share_distribution_test.go index 12089ebc..6b3a0f36 100644 --- a/blockchain/executor/tasks/dkg/dispute_missing_share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/dispute_missing_share_distribution_test.go @@ -4,6 +4,7 @@ package dkg_test import ( "context" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "testing" "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" @@ -13,8 +14,9 @@ import ( ) func TestDisputeMissingShareDistributionTask_Group_1_ShouldAccuseOneValidatorWhoDidNotDistributeShares(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := testutils.StartFromShareDistributionPhase(t, n, []int{4}, []int{}, 100) + suite := testutils.StartFromShareDistributionPhase(t, n, []int{4}, []int{}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -39,8 +41,9 @@ func TestDisputeMissingShareDistributionTask_Group_1_ShouldAccuseOneValidatorWho } func TestDisputeMissingShareDistributionTask_Group_1_ShouldAccuseAllValidatorsWhoDidNotDistributeShares(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := testutils.StartFromShareDistributionPhase(t, n, []int{0, 1, 2, 3, 4}, []int{}, 100) + suite := testutils.StartFromShareDistributionPhase(t, n, []int{0, 1, 2, 3, 4}, []int{}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -65,8 +68,9 @@ func TestDisputeMissingShareDistributionTask_Group_1_ShouldAccuseAllValidatorsWh } func TestDisputeMissingShareDistributionTask_Group_1_ShouldNotAccuseValidatorsWhoDidDistributeShares(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := testutils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100) + suite := testutils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -107,8 +111,9 @@ func TestDisputeMissingShareDistributionTask_Group_1_ShouldNotAccuseValidatorsWh } func TestDisputeMissingShareDistributionTask_Group_2_DisputeMissingShareDistributionTask_ShouldRetryTrue(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := testutils.StartFromShareDistributionPhase(t, n, []int{0}, []int{}, 100) + suite := testutils.StartFromShareDistributionPhase(t, n, []int{0}, []int{}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -125,8 +130,9 @@ func TestDisputeMissingShareDistributionTask_Group_2_DisputeMissingShareDistribu } func TestDisputeMissingShareDistributionTask_Group_2_DisputeMissingShareDistributionTask_ShouldRetryFalse(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := testutils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100) + suite := testutils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -151,8 +157,9 @@ func TestDisputeMissingShareDistributionTask_Group_2_DisputeMissingShareDistribu } func TestDisputeMissingShareDistributionTask_Group_2_ShouldAccuseOneValidatorWhoDidNotDistributeSharesAndAnotherSubmittedBadShares(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := testutils.StartFromShareDistributionPhase(t, n, []int{4}, []int{3}, 100) + suite := testutils.StartFromShareDistributionPhase(t, n, []int{4}, []int{3}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() diff --git a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go index 2fad07be..ffcd4628 100644 --- a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go @@ -9,6 +9,7 @@ import ( dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/monitor/events" "github.com/MadBase/MadNet/blockchain/testutils" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "math/big" "testing" @@ -20,8 +21,9 @@ import ( // We test to ensure that everything behaves correctly. func TestDisputeShareDistributionTask_Group_1_GoodAllValid(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100) + suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -68,8 +70,9 @@ func TestDisputeShareDistributionTask_Group_1_GoodAllValid(t *testing.T) { // This causes another validator to submit a dispute against him, // causing a stake-slashing event. func TestDisputeShareDistributionTask_Group_1_GoodMaliciousShare(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -204,8 +207,9 @@ func TestDisputeShareDistributionTask_Group_2_Bad2(t *testing.T) { } func TestDisputeShareDistributionTask_Group_2_DoRetry_returnsFalse(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100) + suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() logger := logging.GetLogger("test").WithField("Validator", "") @@ -247,8 +251,9 @@ func TestDisputeShareDistributionTask_Group_2_DoRetry_returnsFalse(t *testing.T) } func TestDisputeShareDistributionTask_Group_2_DoRetry_returnsTrue(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100) + suite := dkgTestUtils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() logger := logging.GetLogger("test").WithField("Validator", "") diff --git a/blockchain/executor/tasks/dkg/gpkj_submission_test.go b/blockchain/executor/tasks/dkg/gpkj_submission_test.go index d86be507..b94f08d8 100644 --- a/blockchain/executor/tasks/dkg/gpkj_submission_test.go +++ b/blockchain/executor/tasks/dkg/gpkj_submission_test.go @@ -9,6 +9,7 @@ import ( dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/monitor/interfaces" "github.com/MadBase/MadNet/blockchain/testutils" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "math/big" "testing" @@ -19,8 +20,9 @@ import ( //We test to ensure that everything behaves correctly. func TestGPKjSubmission_Group_1_GoodAllValid(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -121,8 +123,9 @@ func TestGPKjSubmission_Group_2_Bad3(t *testing.T) { // that is, attempting to verify initialMessage with the signature // and public key should fail verification. // This should raise an error, as this is not allowed by the protocol. + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -150,8 +153,9 @@ func TestGPKjSubmission_Group_2_Bad3(t *testing.T) { } func TestGPKjSubmission_Group_2_ShouldRetry_returnsFalse(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -173,8 +177,9 @@ func TestGPKjSubmission_Group_2_ShouldRetry_returnsFalse(t *testing.T) { } func TestGPKjSubmission_Group_2_ShouldRetry_returnsTrue(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth diff --git a/blockchain/executor/tasks/dkg/keyshare_submission_test.go b/blockchain/executor/tasks/dkg/keyshare_submission_test.go index 8d16b77b..dc5235dc 100644 --- a/blockchain/executor/tasks/dkg/keyshare_submission_test.go +++ b/blockchain/executor/tasks/dkg/keyshare_submission_test.go @@ -4,6 +4,7 @@ package dkg_test import ( "context" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "math/big" "testing" @@ -16,8 +17,9 @@ import ( // We test to ensure that everything behaves correctly. func TestKeyShareSubmission_GoodAllValid(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := testutils.StartFromKeyShareSubmissionPhase(t, n, 0, 100) + suite := testutils.StartFromKeyShareSubmissionPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -48,9 +50,10 @@ func TestKeyShareSubmission_GoodAllValid(t *testing.T) { // This comes from invalid SecretValue in state. // In practice, this should never arise, though. func TestKeyShareSubmission_Bad3(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 var phaseLength uint16 = 100 - suite := testutils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, phaseLength) + suite := testutils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, phaseLength, workingDir) defer suite.Eth.Close() //accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -73,9 +76,10 @@ func TestKeyShareSubmission_Bad3(t *testing.T) { // Here, we mess up KeyShare information before submission // so that we raise an error on submission. func TestKeyShareSubmission_Bad4(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 var phaseLength uint16 = 100 - suite := testutils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, phaseLength) + suite := testutils.StartFromShareDistributionPhase(t, n, []int{}, []int{}, phaseLength, workingDir) defer suite.Eth.Close() //accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() diff --git a/blockchain/executor/tasks/dkg/mpk_submission_test.go b/blockchain/executor/tasks/dkg/mpk_submission_test.go index 9e101e8e..43931d8b 100644 --- a/blockchain/executor/tasks/dkg/mpk_submission_test.go +++ b/blockchain/executor/tasks/dkg/mpk_submission_test.go @@ -8,6 +8,7 @@ import ( dkgState "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/testutils" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "math/big" "testing" @@ -18,8 +19,9 @@ import ( //We test to ensure that everything behaves correctly. func TestMPKSubmission_Group_1_GoodAllValid(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -85,8 +87,9 @@ func TestMPKSubmission_Group_1_Bad1(t *testing.T) { // to attempt to submit the mpk. // This should result in an error. // EthDKG restart should be required. + workingDir := cmd.CreateTestWorkingFolder() n := 6 - suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -152,8 +155,9 @@ func TestMPKSubmission_Group_2_Bad4(t *testing.T) { } func TestMPKSubmission_Group_2_ShouldRetry_returnsFalse(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 40) + suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 40, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -190,8 +194,9 @@ func TestMPKSubmission_Group_2_ShouldRetry_returnsFalse(t *testing.T) { } func TestMPKSubmission_Group_2_ShouldRetry_returnsTrue(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth @@ -209,8 +214,9 @@ func TestMPKSubmission_Group_2_ShouldRetry_returnsTrue(t *testing.T) { } func TestMPKSubmission_Group_2_LeaderElection(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromKeyShareSubmissionPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth diff --git a/blockchain/executor/tasks/dkg/share_distribution_test.go b/blockchain/executor/tasks/dkg/share_distribution_test.go index 1a407b19..f2575dbc 100644 --- a/blockchain/executor/tasks/dkg/share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/share_distribution_test.go @@ -8,6 +8,7 @@ import ( "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" "github.com/MadBase/MadNet/blockchain/testutils" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "github.com/MadBase/MadNet/logging" "github.com/ethereum/go-ethereum/common" "github.com/sirupsen/logrus" @@ -18,8 +19,9 @@ import ( //Here we test the happy path. func TestShareDistribution_Group_1_Good(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -44,8 +46,9 @@ func TestShareDistribution_Group_1_Good(t *testing.T) { // One validator attempts to submit invalid commitments (invalid elliptic curve point). // This should result in a failed submission. func TestShareDistribution_Group_1_Bad1(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -106,8 +109,9 @@ func TestShareDistribution_Group_1_Bad1(t *testing.T) { // One validator attempts to submit invalid commitments (identity element). // This should result in a failed submission. func TestShareDistribution_Group_1_Bad2(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 4 - suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -168,8 +172,9 @@ func TestShareDistribution_Group_1_Bad2(t *testing.T) { // One validator attempts to submit invalid commitments (incorrect commitment length) // This should result in a failed submission. func TestShareDistribution_Group_2_Bad4(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -232,8 +237,9 @@ func TestShareDistribution_Group_2_Bad4(t *testing.T) { // One validator attempts to submit invalid commitments (incorrect encrypted shares length) // This should result in a failed submission. func TestShareDistribution_Group_2_Bad5(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 6 - suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -325,8 +331,9 @@ func TestShareDistribution_Group_3_Bad7(t *testing.T) { } func TestShareDistribution_Group_3_ShouldRetryTrue(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() @@ -347,8 +354,9 @@ func TestShareDistribution_Group_3_ShouldRetryTrue(t *testing.T) { } func TestShareDistribution_Group_3_ShouldRetryFalse(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 - suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100) + suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) defer suite.Eth.Close() accounts := suite.Eth.GetKnownAccounts() ctx := context.Background() diff --git a/blockchain/executor/tasks/dkg/testutils/advance_phases.go b/blockchain/executor/tasks/dkg/testutils/advance_phases.go index fdb47d73..0c0228b5 100644 --- a/blockchain/executor/tasks/dkg/testutils/advance_phases.go +++ b/blockchain/executor/tasks/dkg/testutils/advance_phases.go @@ -116,11 +116,11 @@ func InitializeETHDKG(eth ethereum.Network, callOpts *bind.TransactOpts, ctx con return txn, rcpt, nil } -func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators int, phaseLength uint16) *TestSuite { +func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators int, phaseLength uint16, workingDir string) *TestSuite { ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 1000*time.Millisecond) - eth := testutils.GetEthereumNetwork(t, false, n, "") + eth := testutils.GetEthereumNetwork(t, false, n, workingDir) assert.NotNil(t, eth) ctx := context.Background() @@ -241,8 +241,8 @@ func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators } } -func StartFromShareDistributionPhase(t *testing.T, n int, undistributedSharesIdx []int, badSharesIdx []int, phaseLength uint16) *TestSuite { - suite := StartFromRegistrationOpenPhase(t, n, 0, phaseLength) +func StartFromShareDistributionPhase(t *testing.T, n int, undistributedSharesIdx []int, badSharesIdx []int, phaseLength uint16, workingDir string) *TestSuite { + suite := StartFromRegistrationOpenPhase(t, n, 0, phaseLength, workingDir) ctx := context.Background() logger := logging.GetLogger("test").WithField("Validator", "") @@ -339,8 +339,8 @@ func StartFromShareDistributionPhase(t *testing.T, n int, undistributedSharesIdx return suite } -func StartFromKeyShareSubmissionPhase(t *testing.T, n int, undistributedShares int, phaseLength uint16) *TestSuite { - suite := StartFromShareDistributionPhase(t, n, []int{}, []int{}, phaseLength) +func StartFromKeyShareSubmissionPhase(t *testing.T, n int, undistributedShares int, phaseLength uint16, workingDir string) *TestSuite { + suite := StartFromShareDistributionPhase(t, n, []int{}, []int{}, phaseLength, workingDir) ctx := context.Background() logger := logging.GetLogger("test").WithField("Validator", "") @@ -405,8 +405,8 @@ func StartFromKeyShareSubmissionPhase(t *testing.T, n int, undistributedShares i return suite } -func StartFromMPKSubmissionPhase(t *testing.T, n int, phaseLength uint16) *TestSuite { - suite := StartFromKeyShareSubmissionPhase(t, n, 0, phaseLength) +func StartFromMPKSubmissionPhase(t *testing.T, n int, phaseLength uint16, workingDir string) *TestSuite { + suite := StartFromKeyShareSubmissionPhase(t, n, 0, phaseLength, workingDir) ctx := context.Background() logger := logging.GetLogger("test").WithField("Validator", "") dkgStates := suite.DKGStates @@ -448,8 +448,8 @@ func StartFromMPKSubmissionPhase(t *testing.T, n int, phaseLength uint16) *TestS return suite } -func StartFromGPKjPhase(t *testing.T, n int, undistributedGPKjIdx []int, badGPKjIdx []int, phaseLength uint16) *TestSuite { - suite := StartFromMPKSubmissionPhase(t, n, phaseLength) +func StartFromGPKjPhase(t *testing.T, n int, undistributedGPKjIdx []int, badGPKjIdx []int, phaseLength uint16, workingDir string) *TestSuite { + suite := StartFromMPKSubmissionPhase(t, n, phaseLength, workingDir) ctx := context.Background() logger := logging.GetLogger("test").WithField("Validator", "") @@ -536,8 +536,8 @@ func StartFromGPKjPhase(t *testing.T, n int, undistributedGPKjIdx []int, badGPKj return suite } -func StartFromCompletion(t *testing.T, n int, phaseLength uint16) *TestSuite { - suite := StartFromGPKjPhase(t, n, []int{}, []int{}, phaseLength) +func StartFromCompletion(t *testing.T, n int, phaseLength uint16, workingDir string) *TestSuite { + suite := StartFromGPKjPhase(t, n, []int{}, []int{}, phaseLength, workingDir) // move to Completion phase testutils.AdvanceTo(suite.Eth, suite.CompletionTasks[0].Start+suite.DKGStates[0].ConfirmationLength) diff --git a/blockchain/testutils/cmd/clean.go b/blockchain/testutils/cmd/clean.go deleted file mode 100644 index e4691529..00000000 --- a/blockchain/testutils/cmd/clean.go +++ /dev/null @@ -1,18 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - "path/filepath" -) - -func RunClean(workingDir string) error { - - //Remove content - rootPath := GetProjectRootPath() - err := os.Remove(filepath.Join(rootPath, "keyfile.json")) - if err != nil { - fmt.Print("Trying to remove a file that can or cannot be here. Not a problem") - } - return nil -} diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go index 876975ba..787875f5 100644 --- a/blockchain/testutils/cmd/deploy.go +++ b/blockchain/testutils/cmd/deploy.go @@ -10,38 +10,17 @@ import ( func RunDeploy(workingDir string) error { factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this - rootPath := GetProjectRootPath() + //rootPath := GetProjectRootPath() bridgeDir := GetBridgePath() - _, _, err := executeCommand(bridgeDir, "npx hardhat setHardhatIntervalMining --network dev --enable-auto-mine") + _, _, err := runCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining --network dev --enable-auto-mine") if err != nil { log.Printf("Could not execute script: %v", err) return err } - //cp ../scripts/base-files/deploymentList ../scripts/generated/deploymentList - //cp ../scripts/base-files/deploymentArgsTemplate ../scripts/generated/deploymentArgsTemplate - deploymentList := filepath.Join(rootPath, "scripts", "base-files", "deploymentList") - deploymentArgsTemplate := filepath.Join(rootPath, "scripts", "base-files", "deploymentArgsTemplate") - ownerToml := filepath.Join(rootPath, "scripts", "base-files", "owner.toml") - _, err = CopyFileToFolder(deploymentList, workingDir) - if err != nil { - log.Printf("File deploymentList copied in %s", workingDir) - return err - } - _, err = CopyFileToFolder(deploymentArgsTemplate, workingDir) - if err != nil { - log.Printf("File deploymentArgsTemplate copied in %s", workingDir) - return err - } - _, err = CopyFileToFolder(ownerToml, workingDir) - if err != nil { - log.Printf("File deploymentArgsTemplate copied in %s", workingDir) - return err - } - //npx hardhat --network "$NETWORK" --show-stack-traces deployContracts --input-folder ../scripts/generated - _, _, err = executeCommand(bridgeDir, "npx", "hardhat --show-stack-traces deployContracts --input-folder", workingDir) + _, _, err = runCommand(bridgeDir, "npx", "hardhat --show-stack-traces deployContracts --input-folder", workingDir) if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -84,7 +63,7 @@ func RunDeploy(workingDir string) error { //mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" // npx hardhat fundValidators --network $NETWORK - _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev fundValidators") + _, _, err = runCommand(bridgeDir, "npx", "hardhat --network dev fundValidators") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -111,14 +90,14 @@ func RunDeploy(workingDir string) error { //cd $BRIDGE_DIR //cd $CURRENT_WD //npx hardhat setHardhatIntervalMining --network $NETWORK --interval 1000 - _, _, err = executeCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining --interval 1000") + _, _, err = runCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining --interval 1000") if err != nil { log.Printf("Could not execute script: %v", err) return err } //./scripts/main.sh register - err = RunRegister() + err = RunRegister(workingDir) if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -127,14 +106,14 @@ func RunDeploy(workingDir string) error { // //cd $BRIDGE_DIR //npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 10 - _, _, err = executeCommand(bridgeDir, "npx", "hardhat setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) + _, _, err = runCommand(bridgeDir, "npx", "hardhat setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) if err != nil { log.Printf("Could not execute script: %v", err) return err } //npx hardhat setHardhatIntervalMining --network $NETWORK - _, _, err = executeCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining") + _, _, err = runCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining") if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -153,9 +132,9 @@ func RunDeploy(workingDir string) error { //fi //echo -e "failed to auto start validators terminals, manually open a terminal for each validator and execute" //fi - generatedValidatorConfigFiles := filepath.Join(rootPath, "scripts", "generated", "config") + generatedValidatorConfigFiles := filepath.Join(workingDir, "scripts", "generated", "config") files, _ := ioutil.ReadDir(generatedValidatorConfigFiles) - err = RunValidator(len(files)) + err = RunValidator(workingDir, len(files)) if err != nil { log.Printf("Could not execute script: %v", err) return err diff --git a/blockchain/testutils/cmd/executor.go b/blockchain/testutils/cmd/executor.go index e8baca66..b00a4cdb 100644 --- a/blockchain/testutils/cmd/executor.go +++ b/blockchain/testutils/cmd/executor.go @@ -40,7 +40,7 @@ func executeCommand(dir, command string, args ...string) (*exec.Cmd, []byte, err output, err := cmd.Output() if err != nil { - fmt.Printf("Error executing command: %v in dir: %v. %v", command, dir, err) + fmt.Printf("Error executing command: %v %v in dir: %v. %v", command, cmdArgs, dir, err) return &exec.Cmd{}, nil, err } @@ -55,7 +55,7 @@ func runCommand(dir, command string, args ...string) (*exec.Cmd, []byte, error) cmd.Dir = dir err := cmd.Start() if err != nil { - fmt.Printf("Error executing command: %v in dir: %v. %v", command, dir, err) + fmt.Printf("Error executing command: %v %v in dir: %v. %v", command, cmdArgs, dir, err) return &exec.Cmd{}, nil, err } return cmd, nil, err @@ -211,3 +211,11 @@ func ReplaceGenesisBalance(workingDir string) error { defer f.Close() return nil } + +func CreateTestWorkingFolder() string { + workingDir, err := CreateTempFolder() + if err != nil { + log.Fatalf("Error creating test working directory %v", err) + } + return workingDir +} diff --git a/blockchain/testutils/cmd/init.go b/blockchain/testutils/cmd/init.go index c2109a0e..1db9135a 100644 --- a/blockchain/testutils/cmd/init.go +++ b/blockchain/testutils/cmd/init.go @@ -44,12 +44,6 @@ func RunInit(workingDir string, numbersOfValidator int) error { address = strings.ReplaceAll(address, "Address: ", "") address = strings.ReplaceAll(address, "\n", "") - // Remove keyfile.json - err = os.Remove(filepath.Join(rootPath, "keyfile.json")) - if err != nil { - fmt.Print("Trying to remove keyfile.json that can or cannot be here. Not a problem") - } - // Generate private key privateKey, err := RandomHex(16) if err != nil { @@ -78,6 +72,16 @@ func RunInit(workingDir string, numbersOfValidator int) error { return err } + // Keyfile.json + _, err = CopyFileToFolder(filepath.Join(rootPath, "keyfile.json"), filepath.Join(workingDir, "scripts", "generated", "keystores", "keys", address)) + if err != nil { + fmt.Print("Error copying keyfile.json into generated folder") + } + err = os.Remove(filepath.Join(rootPath, "keyfile.json")) + if err != nil { + fmt.Print("Trying to remove keyfile.json ") + } + listeningPort += 1 p2pPort += 1 discoveryPort += 1 diff --git a/blockchain/testutils/cmd/register.go b/blockchain/testutils/cmd/register.go index 94295da0..489305e6 100644 --- a/blockchain/testutils/cmd/register.go +++ b/blockchain/testutils/cmd/register.go @@ -7,13 +7,11 @@ import ( "strings" ) -func RunRegister() error { +func RunRegister(workingDir string) error { bridgeDir := GetBridgePath() - rootDir := GetProjectRootPath() factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this - // TODO - get the right path - keys := filepath.Join(rootDir, "scripts", "generated", "keystores", "keys") + keys := filepath.Join(workingDir, "scripts", "generated", "keystores", "keys") // Build validator names files, err := ioutil.ReadDir(keys) @@ -26,7 +24,7 @@ func RunRegister() error { } // Register validator - _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) + _, _, err = runCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) if err != nil { log.Printf("Could not execute script: %v", err) return err diff --git a/blockchain/testutils/cmd/setup.go b/blockchain/testutils/cmd/setup.go index 6e7bc541..df2c0b7c 100644 --- a/blockchain/testutils/cmd/setup.go +++ b/blockchain/testutils/cmd/setup.go @@ -16,6 +16,7 @@ func RunSetup(workingDir string) error { filepath.Join("scripts", "generated", "monitorDBs"), filepath.Join("scripts", "generated", "config"), filepath.Join("scripts", "generated", "keystores"), + filepath.Join("scripts", "generated", "keystores", "keys"), filepath.Join("assets", "test", "keys"), } for _, folder := range folders { @@ -68,7 +69,17 @@ func RunSetup(workingDir string) error { } _, err = CopyFileToFolder(filepath.Join(rootPath, "assets", "test", "passcodes.txt"), filepath.Join(workingDir, "assets", "test", "passcodes.txt")) if err != nil { - log.Fatalf("Error reading asset passcodes: %s", assetFileDir) + log.Fatalf("Error reading asset passcodes.txt: %s", assetFileDir) + return err + } + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "genesis.json"), filepath.Join(workingDir, "scripts", "generated", "genesis.json")) + if err != nil { + log.Fatalf("Error reading asset genesis.json: %s", assetFileDir) + return err + } + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f"), filepath.Join(workingDir, "scripts", "generated", "keystores", "keys", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f")) + if err != nil { + log.Fatalf("Error reading asset 0x546f99f244b7b58b855330ae0e2bc1b30b41302f: %s", assetFileDir) return err } diff --git a/blockchain/testutils/cmd/validator.go b/blockchain/testutils/cmd/validator.go index 7b98ad0c..5250af52 100644 --- a/blockchain/testutils/cmd/validator.go +++ b/blockchain/testutils/cmd/validator.go @@ -5,12 +5,12 @@ import ( "path/filepath" ) -func RunValidator(validatorIndex int) error { +func RunValidator(workingDir string, validatorIndex int) error { rootDir := GetProjectRootPath() - validatorConfigPath := filepath.Join(rootDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) + validatorConfigPath := filepath.Join(workingDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) - _, _, err := executeCommand(rootDir, "./madnet", "--config", validatorConfigPath, "validator") + _, _, err := runCommand(rootDir, "./madnet", "--config", validatorConfigPath, "validator") if err != nil { return err diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 52328b2b..291b127f 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -79,7 +79,7 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workin } log.Printf("Registering %d validators ...", len(validatorAddresses)) - err = cmd.RunRegister() + err = cmd.RunRegister("") if err != nil { details.Close() assert.Nilf(t, err, "Error registering validators: %v") @@ -250,12 +250,7 @@ func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { func Init(workingDir string, n int) error { - err := cmd.RunClean(workingDir) - if err != nil { - return err - } - - err = cmd.RunInit(workingDir, n) + err := cmd.RunInit(workingDir, n) if err != nil { return err } From 940d7e89538ebb53387c8ef62b9895520eb740d0 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Wed, 15 Jun 2022 10:35:50 +0200 Subject: [PATCH 10/15] Fixing deploy scripts --- blockchain/testutils/cmd/deploy.go | 9 +++++---- blockchain/testutils/cmd/executor.go | 25 +++++++++++++++++++++++++ blockchain/testutils/cmd/setup.go | 17 +++++++++++++++-- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go index 787875f5..7741a14c 100644 --- a/blockchain/testutils/cmd/deploy.go +++ b/blockchain/testutils/cmd/deploy.go @@ -10,9 +10,7 @@ import ( func RunDeploy(workingDir string) error { factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this - //rootPath := GetProjectRootPath() bridgeDir := GetBridgePath() - _, _, err := runCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining --network dev --enable-auto-mine") if err != nil { log.Printf("Could not execute script: %v", err) @@ -20,7 +18,7 @@ func RunDeploy(workingDir string) error { } //npx hardhat --network "$NETWORK" --show-stack-traces deployContracts --input-folder ../scripts/generated - _, _, err = runCommand(bridgeDir, "npx", "hardhat --show-stack-traces deployContracts --input-folder", workingDir) + _, _, err = runCommand(bridgeDir, "npx", "hardhat --show-stack-traces deployContracts --input-folder", filepath.Join(workingDir, "scripts", "generated")) if err != nil { log.Printf("Could not execute script: %v", err) return err @@ -57,10 +55,13 @@ func RunDeploy(workingDir string) error { //sed -e "s/registryAddress = .*/registryAddress = AAAAAAAAA/" "../scripts/generated/config/validator1.toml" > "../scripts/generated/config/$filePath".bk &&\ //mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" //done - // //cp ../scripts/base-files/owner.toml ../scripts/generated/owner.toml //sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/owner.toml" > "../scripts/generated/owner.toml".bk &&\ //mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" + err = ReplaceOwnerRegistryAddress(workingDir, factoryAddress) + if err != nil { + log.Fatalf("ERROR - %v", err) + } // npx hardhat fundValidators --network $NETWORK _, _, err = runCommand(bridgeDir, "npx", "hardhat --network dev fundValidators") diff --git a/blockchain/testutils/cmd/executor.go b/blockchain/testutils/cmd/executor.go index b00a4cdb..e33ab293 100644 --- a/blockchain/testutils/cmd/executor.go +++ b/blockchain/testutils/cmd/executor.go @@ -212,6 +212,31 @@ func ReplaceGenesisBalance(workingDir string) error { return nil } +func ReplaceOwnerRegistryAddress(workingDir, factoryAddress string) error { + ownerFilePath := filepath.Join(workingDir, "scripts", "generated", "owner.toml") + ownerFile, err := os.ReadFile(ownerFilePath) + if err != nil { + log.Fatalf("Error reading base configuration file - %v", err) + return err + } + fileContent := string(ownerFile) + regex := regexp.MustCompile(`registryAddress = .*`) + result := regex.ReplaceAllString(fileContent, "registryAddress = \""+factoryAddress+"\"") + + f, err := os.Create(ownerFilePath) + if err != nil { + log.Fatalf("Error creating modified genesis.json file - %v", err) + return err + } + _, err = fmt.Fprintf(f, "%s", result) + if err != nil { + log.Fatalf("Error writing on new genesis.json file - %v", err) + return err + } + defer f.Close() + return nil +} + func CreateTestWorkingFolder() string { workingDir, err := CreateTempFolder() if err != nil { diff --git a/blockchain/testutils/cmd/setup.go b/blockchain/testutils/cmd/setup.go index df2c0b7c..c500e1c6 100644 --- a/blockchain/testutils/cmd/setup.go +++ b/blockchain/testutils/cmd/setup.go @@ -35,7 +35,6 @@ func RunSetup(workingDir string) error { return err } for _, file := range files { - fmt.Println(file.Name(), file.IsDir()) src := filepath.Join(configurationFileDir, file.Name()) dst := filepath.Join(workingDir, "scripts", "base-files", file.Name()) _, err = CopyFileToFolder(src, dst) @@ -53,7 +52,6 @@ func RunSetup(workingDir string) error { return err } for _, file := range files { - fmt.Println(file.Name(), file.IsDir()) src := filepath.Join(assetFileDir, file.Name()) dst := filepath.Join(workingDir, "assets", "test", "keys", file.Name()) _, err = CopyFileToFolder(src, dst) @@ -82,6 +80,21 @@ func RunSetup(workingDir string) error { log.Fatalf("Error reading asset 0x546f99f244b7b58b855330ae0e2bc1b30b41302f: %s", assetFileDir) return err } + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentList"), filepath.Join(workingDir, "scripts", "generated", "deploymentList")) + if err != nil { + log.Fatalf("Error reading asset deploymentList: %s", assetFileDir) + return err + } + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentArgsTemplate"), filepath.Join(workingDir, "scripts", "generated", "deploymentArgsTemplate")) + if err != nil { + log.Fatalf("Error reading asset deploymentArgsTemplate: %s", assetFileDir) + return err + } + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "owner.toml"), filepath.Join(workingDir, "scripts", "generated", "owner.toml")) + if err != nil { + log.Fatalf("Error reading asset owner.toml: %s", assetFileDir) + return err + } return nil } From 6b1f1a4dfc33c8261d610ebd236e02905d349874 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Thu, 16 Jun 2022 12:15:59 +0200 Subject: [PATCH 11/15] continuing replacing scripts deploy script works --- blockchain/testutils/cmd/deploy.go | 126 ++++++--------------------- blockchain/testutils/cmd/executor.go | 44 ++++++++-- blockchain/testutils/cmd/register.go | 10 +-- blockchain/testutils/setup.go | 13 +-- 4 files changed, 79 insertions(+), 114 deletions(-) diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go index 7741a14c..b9d6b163 100644 --- a/blockchain/testutils/cmd/deploy.go +++ b/blockchain/testutils/cmd/deploy.go @@ -2,144 +2,72 @@ package cmd import ( "io/ioutil" - "log" "os" "path/filepath" + "strings" ) -func RunDeploy(workingDir string) error { +func RunDeploy(workingDir string) (string, error) { - factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this bridgeDir := GetBridgePath() - _, _, err := runCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining --network dev --enable-auto-mine") + _, _, err := executeCommand(bridgeDir, "npx", "hardhat --network dev setHardhatIntervalMining --enable-auto-mine") if err != nil { - log.Printf("Could not execute script: %v", err) - return err + return "", err } - //npx hardhat --network "$NETWORK" --show-stack-traces deployContracts --input-folder ../scripts/generated - _, _, err = runCommand(bridgeDir, "npx", "hardhat --show-stack-traces deployContracts --input-folder", filepath.Join(workingDir, "scripts", "generated")) + _, output, err := executeCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces deployContracts --input-folder", filepath.Join(workingDir, "scripts", "generated")) if err != nil { - log.Printf("Could not execute script: %v", err) - return err + return "", err } + firstLogLine := strings.Split(string(output), "\n")[0] + addressLine := strings.Split(firstLogLine, ":") + factoryAddress := strings.TrimSpace(addressLine[len(addressLine)-1]) - //addr="$(grep -Pzo "\[$NETWORK\]\ndefaultFactoryAddress = \".*\"\n" ../scripts/generated/factoryState | grep -a "defaultFactoryAddress = .*" | awk '{print $NF}')" - // TODO - check how to create factoryAddress variable - - //export FACTORY_ADDRESS=$addr - //if [[ -z "${FACTORY_ADDRESS}" ]]; then - //echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Exiting script!" - //exit 1 - //fi - // TODO - unnecessary check - - //for filePath in $(ls ../scripts/generated/config | xargs); do - // sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/config/$filePath" > "../scripts/generated/config/$filePath".bk &&\ - // mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" - //done - // TODO - get the right path - //err = filepath.Walk("GENERATED CONFIG FILE TO BE DEFINED IN CURRENT WORKDIR", func(path string, info os.FileInfo, err error) error { - // if err != nil { - // log.Fatalf(err.Error()) - // return err - // } - // fmt.Printf("File Name: %s\n", info.Name()) - // return nil - //}) - //if err != nil { - // return err - //} - // TODO - I think un necessary - //for filePath in $(ls ../scripts/generated/config | xargs); do - //sed -e "s/registryAddress = .*/registryAddress = AAAAAAAAA/" "../scripts/generated/config/validator1.toml" > "../scripts/generated/config/$filePath".bk &&\ - //mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" - //done - //cp ../scripts/base-files/owner.toml ../scripts/generated/owner.toml - //sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/owner.toml" > "../scripts/generated/owner.toml".bk &&\ - //mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" err = ReplaceOwnerRegistryAddress(workingDir, factoryAddress) if err != nil { - log.Fatalf("ERROR - %v", err) + return "", err + } + err = ReplaceValidatorsRegistryAddress(workingDir, factoryAddress) + if err != nil { + return "", err } - // npx hardhat fundValidators --network $NETWORK - _, _, err = runCommand(bridgeDir, "npx", "hardhat --network dev fundValidators") + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev fundValidators --config-path", filepath.Join(workingDir, "scripts", "generated", "config")) if err != nil { - log.Printf("Could not execute script: %v", err) - return err + return "", err } - // - //if [[ ! -z "${SKIP_REGISTRATION}" ]]; then - //echo "SKIPPING VALIDATOR REGISTRATION" - //exit 0 - //fi _, isSet := os.LookupEnv("SKIP_REGISTRATION") if isSet { - return nil + return "", nil } - // - //FACTORY_ADDRESS="$(echo "$addr" | sed -e 's/^"//' -e 's/"$//')" - // - //if [[ -z "${FACTORY_ADDRESS}" ]]; then - //echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Not starting the registration!" - //exit 1 - //fi - // - //cd $BRIDGE_DIR - //cd $CURRENT_WD - //npx hardhat setHardhatIntervalMining --network $NETWORK --interval 1000 - _, _, err = runCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining --interval 1000") + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev setHardhatIntervalMining --interval 1000") if err != nil { - log.Printf("Could not execute script: %v", err) - return err + return "", err } - //./scripts/main.sh register - err = RunRegister(workingDir) + err = RunRegister(workingDir, factoryAddress) if err != nil { - log.Printf("Could not execute script: %v", err) - return err + return "", err } - // - //cd $BRIDGE_DIR - //npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 10 - _, _, err = runCommand(bridgeDir, "npx", "hardhat setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev setMinEthereumBlocksPerSnapshot --block-num 10 --factory-address", factoryAddress) if err != nil { - log.Printf("Could not execute script: %v", err) - return err + return "", err } - //npx hardhat setHardhatIntervalMining --network $NETWORK - _, _, err = runCommand(bridgeDir, "npx", "hardhat setHardhatIntervalMining") + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev setHardhatIntervalMining") if err != nil { - log.Printf("Could not execute script: %v", err) - return err + return "", err } - //cd $CURRENT_WD - // - //if [[ -n "${AUTO_START_VALIDATORS}" ]]; then - //if command -v gnome-terminal &>/dev/null; then - //i=1 - //for filePath in $(ls ./scripts/generated/config | xargs); do - //gnome-terminal --tab --title="Validator $i" -- bash -c "./scripts/main.sh validator $i" - //i=$((i + 1)) - //done - //exit 0 - //fi - //echo -e "failed to auto start validators terminals, manually open a terminal for each validator and execute" - //fi generatedValidatorConfigFiles := filepath.Join(workingDir, "scripts", "generated", "config") files, _ := ioutil.ReadDir(generatedValidatorConfigFiles) err = RunValidator(workingDir, len(files)) if err != nil { - log.Printf("Could not execute script: %v", err) - return err + return "", err } - return nil + return factoryAddress, nil } diff --git a/blockchain/testutils/cmd/executor.go b/blockchain/testutils/cmd/executor.go index e33ab293..4b159cd0 100644 --- a/blockchain/testutils/cmd/executor.go +++ b/blockchain/testutils/cmd/executor.go @@ -37,15 +37,16 @@ func executeCommand(dir, command string, args ...string) (*exec.Cmd, []byte, err cmd := exec.Command(command, cmdArgs...) cmd.Dir = dir + cmd.Stderr = os.Stderr output, err := cmd.Output() if err != nil { - fmt.Printf("Error executing command: %v %v in dir: %v. %v", command, cmdArgs, dir, err) - return &exec.Cmd{}, nil, err + log.Printf("Error executing command: %v %v in dir: %v. %v", command, cmdArgs, dir, string(output)) + return &exec.Cmd{}, output, err } + log.Printf("Command Executed: %v %s in dir: %v. \n%s\n", command, cmdArgs, dir, string(output)) return cmd, output, err - } func runCommand(dir, command string, args ...string) (*exec.Cmd, []byte, error) { @@ -58,8 +59,8 @@ func runCommand(dir, command string, args ...string) (*exec.Cmd, []byte, error) fmt.Printf("Error executing command: %v %v in dir: %v. %v", command, cmdArgs, dir, err) return &exec.Cmd{}, nil, err } - return cmd, nil, err + return cmd, nil, err } // TODO - make it wait() @@ -225,7 +226,7 @@ func ReplaceOwnerRegistryAddress(workingDir, factoryAddress string) error { f, err := os.Create(ownerFilePath) if err != nil { - log.Fatalf("Error creating modified genesis.json file - %v", err) + log.Fatalf("Error creating file %v - %v", ownerFilePath, err) return err } _, err = fmt.Fprintf(f, "%s", result) @@ -237,6 +238,39 @@ func ReplaceOwnerRegistryAddress(workingDir, factoryAddress string) error { return nil } +func ReplaceValidatorsRegistryAddress(workingDir, factoryAddress string) error { + filePath := filepath.Join(workingDir, "scripts", "generated", "config") + files, err := ioutil.ReadDir(filePath) + if err != nil { + return err + } + + for _, file := range files { + fileBytes, err := os.ReadFile(filepath.Join(filePath, file.Name())) + if err != nil { + log.Fatalf("Error reading base configuration file - %v", err) + return err + } + fileContent := string(fileBytes) + regex := regexp.MustCompile(`registryAddress = .*`) + result := regex.ReplaceAllString(fileContent, "registryAddress = \""+factoryAddress+"\"") + + f, err := os.Create(filepath.Join(filePath, file.Name())) + if err != nil { + log.Fatalf("Error creating file %v - %v", filePath, err) + return err + } + _, err = fmt.Fprintf(f, "%s", result) + if err != nil { + log.Fatalf("Error writing on new genesis.json file - %v", err) + return err + } + defer f.Close() + } + + return nil +} + func CreateTestWorkingFolder() string { workingDir, err := CreateTempFolder() if err != nil { diff --git a/blockchain/testutils/cmd/register.go b/blockchain/testutils/cmd/register.go index 489305e6..4d720833 100644 --- a/blockchain/testutils/cmd/register.go +++ b/blockchain/testutils/cmd/register.go @@ -2,15 +2,13 @@ package cmd import ( "io/ioutil" - "log" "path/filepath" "strings" ) -func RunRegister(workingDir string) error { +func RunRegister(workingDir, factoryAddress string) error { bridgeDir := GetBridgePath() - factoryAddress := "0x0b1F9c2b7bED6Db83295c7B5158E3806d67eC5bc" // TODO - how to calculate this keys := filepath.Join(workingDir, "scripts", "generated", "keystores", "keys") // Build validator names @@ -20,13 +18,15 @@ func RunRegister(workingDir string) error { return err } for _, file := range files { + if strings.HasPrefix(file.Name(), "0x546f99f244b") { + continue + } validators = append(validators, file.Name()) } // Register validator - _, _, err = runCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) if err != nil { - log.Printf("Could not execute script: %v", err) return err } diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 291b127f..7fc75412 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -28,13 +28,14 @@ import ( var ( configEndpoint = "http://localhost:8545" configDefaultAccount = "0x546f99f244b7b58b855330ae0e2bc1b30b41302f" - configFactoryAddress = "0x0b1f9c2b7bed6db83295c7b5158e3806d67ec5bc" configFinalityDelay = uint64(1) ) func getEthereumDetails(workingDir string) (*ethereum.Details, error) { assetKey := filepath.Join(workingDir, "assets", "test", "keys") + + // TODO - create file rather tahn copy assetPasscode := filepath.Join(workingDir, "assets", "test", "passcodes.txt") details, err := ethereum.NewEndpoint( @@ -63,15 +64,17 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workin assert.NotNilf(t, details, "Ethereum network should not be Nil") log.Printf("Deploying contracts ...") - err = cmd.RunDeploy(workingDir) + factoryAddress, err := cmd.RunDeploy(workingDir) if err != nil { details.Close() - assert.Nilf(t, err, "Error deploying contracts: %v") + assert.Nilf(t, err, "Error deploying contracts: %v", err) + return nil } addr := common.Address{} - copy(addr[:], common.FromHex(configFactoryAddress)) + copy(addr[:], common.FromHex(factoryAddress)) details.Contracts().Initialize(ctx, addr) + // TODO - do I need this? validatorAddresses := make([]string, 0) knownAccounts := details.GetKnownAccounts() for _, acct := range knownAccounts[:validatorsCount] { @@ -79,7 +82,7 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workin } log.Printf("Registering %d validators ...", len(validatorAddresses)) - err = cmd.RunRegister("") + //err = cmd.RunRegister("", factoryAddress) if err != nil { details.Close() assert.Nilf(t, err, "Error registering validators: %v") From 22614a6286f26169e8ddbc677fad209d6075e2f5 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Fri, 17 Jun 2022 14:44:49 +0200 Subject: [PATCH 12/15] continuing replacing scripts deploy script works --- .../executor/tasks/dkg/completion_test.go | 6 +- .../dkg/dispute_share_distribution_test.go | 4 +- .../tasks/dkg/gpkj_submission_test.go | 4 +- .../executor/tasks/dkg/mpk_submission_test.go | 4 +- .../executor/tasks/dkg/register_test.go | 925 +++++++++--------- .../tasks/dkg/share_distribution_test.go | 8 +- .../executor/tasks/dkg/state/validate_test.go | 4 +- .../tasks/dkg/testutils/advance_phases.go | 3 +- .../executor/tasks/dkg/testutils/setup.go | 4 +- blockchain/testutils/cmd/deploy.go | 18 +- blockchain/testutils/cmd/register.go | 24 +- blockchain/testutils/cmd/validator.go | 25 +- blockchain/testutils/setup.go | 111 +-- cmd/main.go | 2 + scripts/base-scripts/deploy.sh | 64 +- scripts/base-scripts/register.sh | 3 + 16 files changed, 605 insertions(+), 604 deletions(-) diff --git a/blockchain/executor/tasks/dkg/completion_test.go b/blockchain/executor/tasks/dkg/completion_test.go index c7cbfcd9..0396269a 100644 --- a/blockchain/executor/tasks/dkg/completion_test.go +++ b/blockchain/executor/tasks/dkg/completion_test.go @@ -20,7 +20,7 @@ import ( // We complete everything correctly, happy path func TestCompletion_Group_1_AllGood(t *testing.T) { workingDir := cmd.CreateTestWorkingFolder() - n := 4 + n := 5 err := testutils.Init(workingDir, n) assert.Nil(t, err) @@ -141,7 +141,7 @@ func TestCompletion_Group_2_Bad1(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4, workingDir) + eth := testutils.GetEthereumNetwork(t, false, 4, workingDir, nil) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -167,7 +167,7 @@ func TestCompletion_Group_2_Bad2(t *testing.T) { defer os.Remove(workingDir) logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4, workingDir) + eth := testutils.GetEthereumNetwork(t, false, 4, workingDir, nil) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go index ffcd4628..7214f02e 100644 --- a/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/dispute_share_distribution_test.go @@ -157,7 +157,7 @@ func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 333*time.Millisecond) - eth := testutils.GetEthereumNetwork(t, false, 4, "") + eth := testutils.GetEthereumNetwork(t, false, 4, "", nil) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -182,7 +182,7 @@ func TestDisputeShareDistributionTask_Group_1_Bad1(t *testing.T) { func TestDisputeShareDistributionTask_Group_2_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4, "") + eth := testutils.GetEthereumNetwork(t, false, 4, "", nil) defer eth.Close() accts := eth.GetKnownAccounts() diff --git a/blockchain/executor/tasks/dkg/gpkj_submission_test.go b/blockchain/executor/tasks/dkg/gpkj_submission_test.go index b94f08d8..a0f16dd7 100644 --- a/blockchain/executor/tasks/dkg/gpkj_submission_test.go +++ b/blockchain/executor/tasks/dkg/gpkj_submission_test.go @@ -62,7 +62,7 @@ func TestGPKjSubmission_Group_1_GoodAllValid(t *testing.T) { func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4, "") + eth := testutils.GetEthereumNetwork(t, false, 4, "", nil) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -86,7 +86,7 @@ func TestGPKjSubmission_Group_1_Bad1(t *testing.T) { func TestGPKjSubmission_Group_1_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4, "") + eth := testutils.GetEthereumNetwork(t, false, 4, "", nil) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/mpk_submission_test.go b/blockchain/executor/tasks/dkg/mpk_submission_test.go index 43931d8b..aaaaf6d4 100644 --- a/blockchain/executor/tasks/dkg/mpk_submission_test.go +++ b/blockchain/executor/tasks/dkg/mpk_submission_test.go @@ -114,7 +114,7 @@ func TestMPKSubmission_Group_1_Bad1(t *testing.T) { func TestMPKSubmission_Group_1_Bad2(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4, "") + eth := testutils.GetEthereumNetwork(t, false, 4, "", nil) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -137,7 +137,7 @@ func TestMPKSubmission_Group_1_Bad2(t *testing.T) { func TestMPKSubmission_Group_2_Bad4(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 4, "") + eth := testutils.GetEthereumNetwork(t, false, 4, "", nil) defer eth.Close() acct := eth.GetKnownAccounts()[0] diff --git a/blockchain/executor/tasks/dkg/register_test.go b/blockchain/executor/tasks/dkg/register_test.go index 52ce7650..a08d2e8b 100644 --- a/blockchain/executor/tasks/dkg/register_test.go +++ b/blockchain/executor/tasks/dkg/register_test.go @@ -4,12 +4,9 @@ package dkg_test import ( "context" - "math/big" "testing" "time" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" dkgUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/utils" "github.com/MadBase/MadNet/blockchain/monitor/events" @@ -28,7 +25,7 @@ func TestRegisterTask_Group_1_Task(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 5, "") + eth := testutils.GetEthereumNetwork(t, false, 5, "", nil) defer eth.Close() acct := eth.GetKnownAccounts()[0] @@ -101,463 +98,463 @@ func TestRegisterTask_Group_1_Task(t *testing.T) { assert.Nil(t, err) } -// We attempt valid registration. Everything should succeed. -// This test calls Initialize and DoWork. -func TestRegisterTask_Group_1_Good2(t *testing.T) { - n := 6 - _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false, 6, "") - assert.NotNil(t, eth) - defer eth.Close() - - ctx := context.Background() - - owner := accounts[0] - - // Start EthDKG - ownerOpts, err := eth.GetTransactionOpts(ctx, owner) - assert.Nil(t, err) - - // Shorten ethdkg phase for testing purposes - txn, rcpt, err := dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) - assert.Nil(t, err) - - t.Logf("Updating phase length used %v gas vs %v", rcpt.GasUsed, txn.Cost()) - - // Kick off ethdkg - txn, rcpt, err = dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) - assert.Nil(t, err) - - t.Logf("Updating phase length used %v gas vs %v", rcpt.GasUsed, txn.Cost()) - - event, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) - assert.Nil(t, err) - assert.NotNil(t, event) - - // get validator addresses - //ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) - //defer cf() - logger := logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") - callOpts, err := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) - assert.Nil(t, err) - validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) - assert.Nil(t, err) - - // Do Register task - tasksVec := make([]*dkg.RegisterTask, n) - dkgStates := make([]*state.DkgState, n) - for idx := 0; idx < n; idx++ { - logger := logging.GetLogger("test").WithField("Validator", accounts[idx].Address.String()) - state, registrationTask, _ := events.UpdateStateOnRegistrationOpened( - accounts[idx], - event.StartBlock.Uint64(), - event.PhaseLength.Uint64(), - event.ConfirmationLength.Uint64(), - event.Nonce.Uint64(), - true, - validatorAddresses, - ) - - dkgStates[idx] = state - tasksVec[idx] = registrationTask - - err = tasksVec[idx].Initialize(ctx, logger, eth) - assert.Nil(t, err) - err = tasksVec[idx].DoWork(ctx, logger, eth) - assert.Nil(t, err) - - assert.True(t, tasksVec[idx].Success) - } - - // Check public keys are present and valid; last will be invalid - for idx, acct := range accounts { - callOpts, err := eth.GetCallOpts(context.Background(), acct) - assert.Nil(t, err) - p, err := eth.Contracts().Ethdkg().GetParticipantInternalState(callOpts, acct.Address) - assert.Nil(t, err) - - // check points - publicKey := dkgStates[idx].TransportPublicKey - if (publicKey[0].Cmp(p.PublicKey[0]) != 0) || (publicKey[1].Cmp(p.PublicKey[1]) != 0) { - t.Fatal("Invalid public key") - } - if p.Phase != uint8(state.RegistrationOpen) { - t.Fatal("Invalid participant phase") - } - - } -} - -// We attempt to submit an invalid transport public key (a point not on the curve). -// This should raise an error and not allow that participant to proceed. -func TestRegisterTask_Group_1_Bad1(t *testing.T) { - n := 5 - _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false, 5, "") - assert.NotNil(t, eth) - defer eth.Close() - - ctx := context.Background() - - owner := accounts[0] - ownerOpts, err := eth.GetTransactionOpts(ctx, owner) - assert.Nil(t, err) - - // Shorten ethdkg phase for testing purposes - _, _, err = dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) - assert.Nil(t, err) - - // Start EthDKG - _, rcpt, err := dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) - assert.Nil(t, err) - - event, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) - assert.Nil(t, err) - assert.NotNil(t, event) - - // get validator addresses - ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) - defer cf() - logger := logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") - callOpts, err := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) - assert.Nil(t, err) - validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) - assert.Nil(t, err) - - // Do Register task - state, registrationTask, _ := events.UpdateStateOnRegistrationOpened( - accounts[0], - event.StartBlock.Uint64(), - event.PhaseLength.Uint64(), - event.ConfirmationLength.Uint64(), - event.Nonce.Uint64(), - true, - validatorAddresses, - ) - - logger = logging.GetLogger("test").WithField("Validator", accounts[0].Address.String()) - - err = registrationTask.Initialize(ctx, logger, eth) - assert.Nil(t, err) - // Mess up private key - state.TransportPrivateKey = big.NewInt(0) - // Mess up public key; this should fail because it is invalid - state.TransportPublicKey = [2]*big.Int{big.NewInt(0), big.NewInt(1)} - err = registrationTask.DoWork(ctx, logger, eth) - assert.NotNil(t, err) -} - -// We attempt to submit an invalid transport public key (submit identity element). -// This should raise an error and not allow that participant to proceed. -func TestRegisterTask_Group_2_Bad2(t *testing.T) { - n := 7 - _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false, 7, "") - assert.NotNil(t, eth) - defer eth.Close() - - ctx := context.Background() - owner := accounts[0] - ownerOpts, err := eth.GetTransactionOpts(ctx, owner) - assert.Nil(t, err) - - // Shorten ethdkg phase for testing purposes - _, _, err = dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) - assert.Nil(t, err) - - // Start EthDKG - _, rcpt, err := dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) - assert.Nil(t, err) - - event, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) - assert.Nil(t, err) - assert.NotNil(t, event) - - // get validator addresses - ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) - defer cf() - logger := logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") - callOpts, err := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) - assert.Nil(t, err) - validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) - assert.Nil(t, err) - - // Do Register task - state, registrationTask, _ := events.UpdateStateOnRegistrationOpened( - accounts[0], - event.StartBlock.Uint64(), - event.PhaseLength.Uint64(), - event.ConfirmationLength.Uint64(), - event.Nonce.Uint64(), - true, - validatorAddresses, - ) - logger = logging.GetLogger("test").WithField("Validator", accounts[0].Address.String()) - - err = registrationTask.Initialize(ctx, logger, eth) - assert.Nil(t, err) - // Mess up private key - state.TransportPrivateKey = big.NewInt(0) - // Mess up public key; this should fail because it is invalid (the identity element) - state.TransportPublicKey = [2]*big.Int{big.NewInt(0), big.NewInt(0)} - err = registrationTask.DoWork(ctx, logger, eth) - assert.NotNil(t, err) -} - -// The initialization should fail because we dont allow less than 4 validators -func TestRegisterTask_Group_2_Bad4(t *testing.T) { - eth := testutils.GetEthereumNetwork(t, false, 3, "") - assert.NotNil(t, eth) - defer eth.Close() - - ctx := context.Background() - - accounts := eth.GetKnownAccounts() - owner := accounts[0] - - // Start EthDKG - ownerOpts, err := eth.GetTransactionOpts(ctx, owner) - assert.Nil(t, err) - - // Shorten ethdkg phase for testing purposes - _, _, err = dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) - assert.Nil(t, err) - - // Start EthDKG - _, _, err = dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) - assert.NotNil(t, err) -} - -// We attempt invalid registration. -// Here, we try to register after registration has closed. -// This should raise an error. -func TestRegisterTask_Group_2_Bad5(t *testing.T) { - n := 5 - _, accounts := testutils.InitializePrivateKeysAndAccounts(n) - eth := testutils.GetEthereumNetwork(t, false, 5, "") - assert.NotNil(t, eth) - defer eth.Close() - - ctx := context.Background() - owner := accounts[0] - ownerOpts, err := eth.GetTransactionOpts(ctx, owner) - assert.Nil(t, err) - - // Shorten ethdkg phase for testing purposes - _, _, err = dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) - assert.Nil(t, err) - - // Start EthDKG - _, rcpt, err := dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) - assert.Nil(t, err) - - event, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) - assert.Nil(t, err) - assert.NotNil(t, event) - - // Do share distribution; afterward, we confirm who is valid and who is not - testutils.AdvanceTo(eth, event.StartBlock.Uint64()+event.PhaseLength.Uint64()) - - // get validator addresses - ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) - defer cf() - logger := logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") - callOpts, err := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) - assert.Nil(t, err) - validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) - assert.Nil(t, err) - - // Do Register task - _, registrationTask, _ := events.UpdateStateOnRegistrationOpened( - accounts[0], - event.StartBlock.Uint64(), - event.PhaseLength.Uint64(), - event.ConfirmationLength.Uint64(), - event.Nonce.Uint64(), - true, - validatorAddresses, - ) - logger = logging.GetLogger("test").WithField("Validator", accounts[0].Address.String()) - - err = registrationTask.Initialize(ctx, logger, eth) - assert.Nil(t, err) - err = registrationTask.DoWork(ctx, logger, eth) - assert.NotNil(t, err) -} - -// ShouldRetry() return false because the registration was successful -func TestRegisterTask_Group_3_ShouldRetryFalse(t *testing.T) { - n := 5 - ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - - t.Logf("ecdsaPrivateKeys:%v", ecdsaPrivateKeys) - - //This shouldn't be needed - //tr := &objects.TypeRegistry{} - // - //tr.RegisterInstanceType(&RegisterTask{}) - - logger := logging.GetLogger("ethereum") - logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 5, "") - defer eth.Close() - - acct := eth.GetKnownAccounts()[0] - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - c := eth.Contracts() - - // Check status - callOpts, err := eth.GetCallOpts(ctx, acct) - assert.Nil(t, err) - valid, err := c.ValidatorPool().IsValidator(callOpts, acct.Address) - assert.Nil(t, err, "Failed checking validator status") - assert.True(t, valid) - - // Kick off EthDKG - txnOpts, err := eth.GetTransactionOpts(ctx, eth.GetDefaultAccount()) - assert.Nil(t, err) - - // var ( - // txn *types.Transaction - // rcpt *types.Receipt - // ) - - // Shorten ethdkg phase for testing purposes - txn, rcpt, err := dkgTestUtils.SetETHDKGPhaseLength(4, eth, txnOpts, ctx) - assert.Nil(t, err) - - t.Logf("Updating phase length used %v gas vs %v", rcpt.GasUsed, txn.Cost()) - - // Start EthDKG - _, rcpt, err = dkgTestUtils.InitializeETHDKG(eth, txnOpts, ctx) - assert.Nil(t, err) - assert.NotNil(t, rcpt) - - t.Logf("Kicking off EthDKG used %v gas", rcpt.GasUsed) - t.Logf("registration opens:%v", rcpt.BlockNumber) - - openEvent, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) - assert.Nil(t, err) - assert.NotNil(t, openEvent) - - // get validator addresses - ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) - defer cf() - validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) - assert.Nil(t, err) - - // Do Register task - _, registrationTask, _ := events.UpdateStateOnRegistrationOpened( - accounts[0], - openEvent.StartBlock.Uint64(), - openEvent.PhaseLength.Uint64(), - openEvent.ConfirmationLength.Uint64(), - openEvent.Nonce.Uint64(), - true, - validatorAddresses, - ) - - log := logger.WithField("TaskID", "foo") - - err = registrationTask.Initialize(ctx, log, eth) - assert.Nil(t, err) - - err = registrationTask.DoWork(ctx, log, eth) - assert.Nil(t, err) - - retry := registrationTask.ShouldRetry(ctx, log, eth) - assert.False(t, retry) -} - -// ShouldRetry() return true because the registration was unsuccessful -func TestRegisterTask_Group_3_ShouldRetryTrue(t *testing.T) { - n := 5 - ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) - - t.Logf("ecdsaPrivateKeys:%v", ecdsaPrivateKeys) - - //This shouldn't be needed - //tr := &objects.TypeRegistry{} - // - //tr.RegisterInstanceType(&RegisterTask{}) - - logger := logging.GetLogger("ethereum") - logger.SetLevel(logrus.DebugLevel) - eth := testutils.GetEthereumNetwork(t, false, 5, "") - defer eth.Close() - - acct := eth.GetKnownAccounts()[0] - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - c := eth.Contracts() - - // Check status - callOpts, err := eth.GetCallOpts(ctx, acct) - assert.Nil(t, err) - valid, err := c.ValidatorPool().IsValidator(callOpts, acct.Address) - assert.Nil(t, err, "Failed checking validator status") - assert.True(t, valid) - - // Kick off EthDKG - txnOpts, err := eth.GetTransactionOpts(ctx, eth.GetDefaultAccount()) - assert.Nil(t, err) - - var ( - txn *types.Transaction - rcpt *types.Receipt - ) - - // Shorten ethdkg phase for testing purposes - txn, rcpt, err = dkgTestUtils.SetETHDKGPhaseLength(4, eth, txnOpts, ctx) - assert.Nil(t, err) - - t.Logf("Updating phase length used %v gas vs %v", rcpt.GasUsed, txn.Cost()) - - // Start EthDKG - _, rcpt, err = dkgTestUtils.InitializeETHDKG(eth, txnOpts, ctx) - assert.Nil(t, err) - - t.Logf("Kicking off EthDKG used %v gas", rcpt.GasUsed) - t.Logf("registration opens:%v", rcpt.BlockNumber) - - openEvent, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) - assert.Nil(t, err) - assert.NotNil(t, openEvent) - - // get validator addresses - ctx, cf := context.WithTimeout(context.Background(), 30*time.Second) - defer cf() - //logger = logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") - //callOpts := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) - validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) - assert.Nil(t, err) - - // Do Register task - state, registrationTask, _ := events.UpdateStateOnRegistrationOpened( - accounts[0], - openEvent.StartBlock.Uint64(), - openEvent.PhaseLength.Uint64(), - openEvent.ConfirmationLength.Uint64(), - openEvent.Nonce.Uint64(), - true, - validatorAddresses, - ) - - log := logger.WithField("TaskID", "foo") - - err = registrationTask.Initialize(ctx, log, eth) - assert.Nil(t, err) - - state.TransportPublicKey[0] = big.NewInt(0) - state.TransportPublicKey[0] = big.NewInt(0) - err = registrationTask.DoWork(ctx, log, eth) - assert.NotNil(t, err) - - retry := registrationTask.ShouldRetry(ctx, log, eth) - assert.True(t, retry) -} +//// We attempt valid registration. Everything should succeed. +//// This test calls Initialize and DoWork. +//func TestRegisterTask_Group_1_Good2(t *testing.T) { +// n := 6 +// _, accounts := testutils.InitializePrivateKeysAndAccounts(n) +// eth := testutils.GetEthereumNetwork(t, false, 6, "", nil, nil) +// assert.NotNil(t, eth) +// defer eth.Close() +// +// ctx := context.Background() +// +// owner := accounts[0] +// +// // Start EthDKG +// ownerOpts, err := eth.GetTransactionOpts(ctx, owner) +// assert.Nil(t, err) +// +// // Shorten ethdkg phase for testing purposes +// txn, rcpt, err := dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// t.Logf("Updating phase length used %v gas vs %v", rcpt.GasUsed, txn.Cost()) +// +// // Kick off ethdkg +// txn, rcpt, err = dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// t.Logf("Updating phase length used %v gas vs %v", rcpt.GasUsed, txn.Cost()) +// +// event, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) +// assert.Nil(t, err) +// assert.NotNil(t, event) +// +// // get validator addresses +// //ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) +// //defer cf() +// logger := logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") +// callOpts, err := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) +// assert.Nil(t, err) +// validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) +// assert.Nil(t, err) +// +// // Do Register task +// tasksVec := make([]*dkg.RegisterTask, n) +// dkgStates := make([]*state.DkgState, n) +// for idx := 0; idx < n; idx++ { +// logger := logging.GetLogger("test").WithField("Validator", accounts[idx].Address.String()) +// state, registrationTask, _ := events.UpdateStateOnRegistrationOpened( +// accounts[idx], +// event.StartBlock.Uint64(), +// event.PhaseLength.Uint64(), +// event.ConfirmationLength.Uint64(), +// event.Nonce.Uint64(), +// true, +// validatorAddresses, +// ) +// +// dkgStates[idx] = state +// tasksVec[idx] = registrationTask +// +// err = tasksVec[idx].Initialize(ctx, logger, eth) +// assert.Nil(t, err) +// err = tasksVec[idx].DoWork(ctx, logger, eth) +// assert.Nil(t, err) +// +// assert.True(t, tasksVec[idx].Success) +// } +// +// // Check public keys are present and valid; last will be invalid +// for idx, acct := range accounts { +// callOpts, err := eth.GetCallOpts(context.Background(), acct) +// assert.Nil(t, err) +// p, err := eth.Contracts().Ethdkg().GetParticipantInternalState(callOpts, acct.Address) +// assert.Nil(t, err) +// +// // check points +// publicKey := dkgStates[idx].TransportPublicKey +// if (publicKey[0].Cmp(p.PublicKey[0]) != 0) || (publicKey[1].Cmp(p.PublicKey[1]) != 0) { +// t.Fatal("Invalid public key") +// } +// if p.Phase != uint8(state.RegistrationOpen) { +// t.Fatal("Invalid participant phase") +// } +// +// } +//} +// +//// We attempt to submit an invalid transport public key (a point not on the curve). +//// This should raise an error and not allow that participant to proceed. +//func TestRegisterTask_Group_1_Bad1(t *testing.T) { +// n := 5 +// _, accounts := testutils.InitializePrivateKeysAndAccounts(n) +// eth := testutils.GetEthereumNetwork(t, false, 5, "") +// assert.NotNil(t, eth) +// defer eth.Close() +// +// ctx := context.Background() +// +// owner := accounts[0] +// ownerOpts, err := eth.GetTransactionOpts(ctx, owner) +// assert.Nil(t, err) +// +// // Shorten ethdkg phase for testing purposes +// _, _, err = dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// // Start EthDKG +// _, rcpt, err := dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// event, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) +// assert.Nil(t, err) +// assert.NotNil(t, event) +// +// // get validator addresses +// ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) +// defer cf() +// logger := logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") +// callOpts, err := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) +// assert.Nil(t, err) +// validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) +// assert.Nil(t, err) +// +// // Do Register task +// state, registrationTask, _ := events.UpdateStateOnRegistrationOpened( +// accounts[0], +// event.StartBlock.Uint64(), +// event.PhaseLength.Uint64(), +// event.ConfirmationLength.Uint64(), +// event.Nonce.Uint64(), +// true, +// validatorAddresses, +// ) +// +// logger = logging.GetLogger("test").WithField("Validator", accounts[0].Address.String()) +// +// err = registrationTask.Initialize(ctx, logger, eth) +// assert.Nil(t, err) +// // Mess up private key +// state.TransportPrivateKey = big.NewInt(0) +// // Mess up public key; this should fail because it is invalid +// state.TransportPublicKey = [2]*big.Int{big.NewInt(0), big.NewInt(1)} +// err = registrationTask.DoWork(ctx, logger, eth) +// assert.NotNil(t, err) +//} +// +//// We attempt to submit an invalid transport public key (submit identity element). +//// This should raise an error and not allow that participant to proceed. +//func TestRegisterTask_Group_2_Bad2(t *testing.T) { +// n := 7 +// _, accounts := testutils.InitializePrivateKeysAndAccounts(n) +// eth := testutils.GetEthereumNetwork(t, false, 7, "") +// assert.NotNil(t, eth) +// defer eth.Close() +// +// ctx := context.Background() +// owner := accounts[0] +// ownerOpts, err := eth.GetTransactionOpts(ctx, owner) +// assert.Nil(t, err) +// +// // Shorten ethdkg phase for testing purposes +// _, _, err = dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// // Start EthDKG +// _, rcpt, err := dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// event, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) +// assert.Nil(t, err) +// assert.NotNil(t, event) +// +// // get validator addresses +// ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) +// defer cf() +// logger := logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") +// callOpts, err := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) +// assert.Nil(t, err) +// validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) +// assert.Nil(t, err) +// +// // Do Register task +// state, registrationTask, _ := events.UpdateStateOnRegistrationOpened( +// accounts[0], +// event.StartBlock.Uint64(), +// event.PhaseLength.Uint64(), +// event.ConfirmationLength.Uint64(), +// event.Nonce.Uint64(), +// true, +// validatorAddresses, +// ) +// logger = logging.GetLogger("test").WithField("Validator", accounts[0].Address.String()) +// +// err = registrationTask.Initialize(ctx, logger, eth) +// assert.Nil(t, err) +// // Mess up private key +// state.TransportPrivateKey = big.NewInt(0) +// // Mess up public key; this should fail because it is invalid (the identity element) +// state.TransportPublicKey = [2]*big.Int{big.NewInt(0), big.NewInt(0)} +// err = registrationTask.DoWork(ctx, logger, eth) +// assert.NotNil(t, err) +//} +// +//// The initialization should fail because we dont allow less than 4 validators +//func TestRegisterTask_Group_2_Bad4(t *testing.T) { +// eth := testutils.GetEthereumNetwork(t, false, 3, "") +// assert.NotNil(t, eth) +// defer eth.Close() +// +// ctx := context.Background() +// +// accounts := eth.GetKnownAccounts() +// owner := accounts[0] +// +// // Start EthDKG +// ownerOpts, err := eth.GetTransactionOpts(ctx, owner) +// assert.Nil(t, err) +// +// // Shorten ethdkg phase for testing purposes +// _, _, err = dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// // Start EthDKG +// _, _, err = dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) +// assert.NotNil(t, err) +//} +// +//// We attempt invalid registration. +//// Here, we try to register after registration has closed. +//// This should raise an error. +//func TestRegisterTask_Group_2_Bad5(t *testing.T) { +// n := 5 +// _, accounts := testutils.InitializePrivateKeysAndAccounts(n) +// eth := testutils.GetEthereumNetwork(t, false, 5, "") +// assert.NotNil(t, eth) +// defer eth.Close() +// +// ctx := context.Background() +// owner := accounts[0] +// ownerOpts, err := eth.GetTransactionOpts(ctx, owner) +// assert.Nil(t, err) +// +// // Shorten ethdkg phase for testing purposes +// _, _, err = dkgTestUtils.SetETHDKGPhaseLength(100, eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// // Start EthDKG +// _, rcpt, err := dkgTestUtils.InitializeETHDKG(eth, ownerOpts, ctx) +// assert.Nil(t, err) +// +// event, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) +// assert.Nil(t, err) +// assert.NotNil(t, event) +// +// // Do share distribution; afterward, we confirm who is valid and who is not +// testutils.AdvanceTo(eth, event.StartBlock.Uint64()+event.PhaseLength.Uint64()) +// +// // get validator addresses +// ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) +// defer cf() +// logger := logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") +// callOpts, err := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) +// assert.Nil(t, err) +// validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) +// assert.Nil(t, err) +// +// // Do Register task +// _, registrationTask, _ := events.UpdateStateOnRegistrationOpened( +// accounts[0], +// event.StartBlock.Uint64(), +// event.PhaseLength.Uint64(), +// event.ConfirmationLength.Uint64(), +// event.Nonce.Uint64(), +// true, +// validatorAddresses, +// ) +// logger = logging.GetLogger("test").WithField("Validator", accounts[0].Address.String()) +// +// err = registrationTask.Initialize(ctx, logger, eth) +// assert.Nil(t, err) +// err = registrationTask.DoWork(ctx, logger, eth) +// assert.NotNil(t, err) +//} +// +//// ShouldRetry() return false because the registration was successful +//func TestRegisterTask_Group_3_ShouldRetryFalse(t *testing.T) { +// n := 5 +// ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) +// +// t.Logf("ecdsaPrivateKeys:%v", ecdsaPrivateKeys) +// +// //This shouldn't be needed +// //tr := &objects.TypeRegistry{} +// // +// //tr.RegisterInstanceType(&RegisterTask{}) +// +// logger := logging.GetLogger("ethereum") +// logger.SetLevel(logrus.DebugLevel) +// eth := testutils.GetEthereumNetwork(t, false, 5, "") +// defer eth.Close() +// +// acct := eth.GetKnownAccounts()[0] +// +// ctx, cancel := context.WithCancel(context.Background()) +// defer cancel() +// +// c := eth.Contracts() +// +// // Check status +// callOpts, err := eth.GetCallOpts(ctx, acct) +// assert.Nil(t, err) +// valid, err := c.ValidatorPool().IsValidator(callOpts, acct.Address) +// assert.Nil(t, err, "Failed checking validator status") +// assert.True(t, valid) +// +// // Kick off EthDKG +// txnOpts, err := eth.GetTransactionOpts(ctx, eth.GetDefaultAccount()) +// assert.Nil(t, err) +// +// // var ( +// // txn *types.Transaction +// // rcpt *types.Receipt +// // ) +// +// // Shorten ethdkg phase for testing purposes +// txn, rcpt, err := dkgTestUtils.SetETHDKGPhaseLength(4, eth, txnOpts, ctx) +// assert.Nil(t, err) +// +// t.Logf("Updating phase length used %v gas vs %v", rcpt.GasUsed, txn.Cost()) +// +// // Start EthDKG +// _, rcpt, err = dkgTestUtils.InitializeETHDKG(eth, txnOpts, ctx) +// assert.Nil(t, err) +// assert.NotNil(t, rcpt) +// +// t.Logf("Kicking off EthDKG used %v gas", rcpt.GasUsed) +// t.Logf("registration opens:%v", rcpt.BlockNumber) +// +// openEvent, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) +// assert.Nil(t, err) +// assert.NotNil(t, openEvent) +// +// // get validator addresses +// ctx, cf := context.WithTimeout(context.Background(), 10*time.Second) +// defer cf() +// validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) +// assert.Nil(t, err) +// +// // Do Register task +// _, registrationTask, _ := events.UpdateStateOnRegistrationOpened( +// accounts[0], +// openEvent.StartBlock.Uint64(), +// openEvent.PhaseLength.Uint64(), +// openEvent.ConfirmationLength.Uint64(), +// openEvent.Nonce.Uint64(), +// true, +// validatorAddresses, +// ) +// +// log := logger.WithField("TaskID", "foo") +// +// err = registrationTask.Initialize(ctx, log, eth) +// assert.Nil(t, err) +// +// err = registrationTask.DoWork(ctx, log, eth) +// assert.Nil(t, err) +// +// retry := registrationTask.ShouldRetry(ctx, log, eth) +// assert.False(t, retry) +//} +// +//// ShouldRetry() return true because the registration was unsuccessful +//func TestRegisterTask_Group_3_ShouldRetryTrue(t *testing.T) { +// n := 5 +// ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) +// +// t.Logf("ecdsaPrivateKeys:%v", ecdsaPrivateKeys) +// +// //This shouldn't be needed +// //tr := &objects.TypeRegistry{} +// // +// //tr.RegisterInstanceType(&RegisterTask{}) +// +// logger := logging.GetLogger("ethereum") +// logger.SetLevel(logrus.DebugLevel) +// eth := testutils.GetEthereumNetwork(t, false, 5, "") +// defer eth.Close() +// +// acct := eth.GetKnownAccounts()[0] +// +// ctx, cancel := context.WithCancel(context.Background()) +// defer cancel() +// +// c := eth.Contracts() +// +// // Check status +// callOpts, err := eth.GetCallOpts(ctx, acct) +// assert.Nil(t, err) +// valid, err := c.ValidatorPool().IsValidator(callOpts, acct.Address) +// assert.Nil(t, err, "Failed checking validator status") +// assert.True(t, valid) +// +// // Kick off EthDKG +// txnOpts, err := eth.GetTransactionOpts(ctx, eth.GetDefaultAccount()) +// assert.Nil(t, err) +// +// var ( +// txn *types.Transaction +// rcpt *types.Receipt +// ) +// +// // Shorten ethdkg phase for testing purposes +// txn, rcpt, err = dkgTestUtils.SetETHDKGPhaseLength(4, eth, txnOpts, ctx) +// assert.Nil(t, err) +// +// t.Logf("Updating phase length used %v gas vs %v", rcpt.GasUsed, txn.Cost()) +// +// // Start EthDKG +// _, rcpt, err = dkgTestUtils.InitializeETHDKG(eth, txnOpts, ctx) +// assert.Nil(t, err) +// +// t.Logf("Kicking off EthDKG used %v gas", rcpt.GasUsed) +// t.Logf("registration opens:%v", rcpt.BlockNumber) +// +// openEvent, err := dkgTestUtils.GetETHDKGRegistrationOpened(rcpt.Logs, eth) +// assert.Nil(t, err) +// assert.NotNil(t, openEvent) +// +// // get validator addresses +// ctx, cf := context.WithTimeout(context.Background(), 30*time.Second) +// defer cf() +// //logger = logging.GetLogger("test").WithField("action", "GetValidatorAddressesFromPool") +// //callOpts := eth.GetCallOpts(ctx, eth.GetDefaultAccount()) +// validatorAddresses, err := dkgUtils.GetValidatorAddressesFromPool(callOpts, eth, logger.WithField("action", "GetValidatorAddressesFromPool")) +// assert.Nil(t, err) +// +// // Do Register task +// state, registrationTask, _ := events.UpdateStateOnRegistrationOpened( +// accounts[0], +// openEvent.StartBlock.Uint64(), +// openEvent.PhaseLength.Uint64(), +// openEvent.ConfirmationLength.Uint64(), +// openEvent.Nonce.Uint64(), +// true, +// validatorAddresses, +// ) +// +// log := logger.WithField("TaskID", "foo") +// +// err = registrationTask.Initialize(ctx, log, eth) +// assert.Nil(t, err) +// +// state.TransportPublicKey[0] = big.NewInt(0) +// state.TransportPublicKey[0] = big.NewInt(0) +// err = registrationTask.DoWork(ctx, log, eth) +// assert.NotNil(t, err) +// +// retry := registrationTask.ShouldRetry(ctx, log, eth) +// assert.True(t, retry) +//} diff --git a/blockchain/executor/tasks/dkg/share_distribution_test.go b/blockchain/executor/tasks/dkg/share_distribution_test.go index f2575dbc..ba293e43 100644 --- a/blockchain/executor/tasks/dkg/share_distribution_test.go +++ b/blockchain/executor/tasks/dkg/share_distribution_test.go @@ -4,14 +4,10 @@ package dkg_test import ( "context" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg" - "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/state" dkgTestUtils "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/testutils" - "github.com/MadBase/MadNet/blockchain/testutils" "github.com/MadBase/MadNet/blockchain/testutils/cmd" "github.com/MadBase/MadNet/logging" "github.com/ethereum/go-ethereum/common" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "math/big" "testing" @@ -286,7 +282,7 @@ func TestShareDistribution_Group_2_Bad5(t *testing.T) { // We begin by submitting invalid information; // we submit nil state information -func TestShareDistribution_Group_2_Bad6(t *testing.T) { +/*func TestShareDistribution_Group_2_Bad6(t *testing.T) { logger := logging.GetLogger("ethereum") logger.SetLevel(logrus.DebugLevel) eth := testutils.GetEthereumNetwork(t, false, 5, "") @@ -330,7 +326,9 @@ func TestShareDistribution_Group_3_Bad7(t *testing.T) { } } +*/ func TestShareDistribution_Group_3_ShouldRetryTrue(t *testing.T) { + workingDir := cmd.CreateTestWorkingFolder() n := 5 suite := dkgTestUtils.StartFromRegistrationOpenPhase(t, n, 0, 100, workingDir) diff --git a/blockchain/executor/tasks/dkg/state/validate_test.go b/blockchain/executor/tasks/dkg/state/validate_test.go index 77f7faf2..aa9711d7 100644 --- a/blockchain/executor/tasks/dkg/state/validate_test.go +++ b/blockchain/executor/tasks/dkg/state/validate_test.go @@ -174,8 +174,8 @@ func TestMath_VerifyDistributedSharesBad3(t *testing.T) { threshold := state.ThresholdForUserCount(n) // Setup keys - ecdsaPrivKeys := testutils.SetupPrivateKeys(n) - accountsArray := testutils.SetupAccounts(ecdsaPrivKeys) + ecdsaPrivKeys := testutils.GeneratePrivateKeys(n) + accountsArray := testutils.GenerateAccounts(ecdsaPrivKeys) // Validator Setup dkgIdx := 0 diff --git a/blockchain/executor/tasks/dkg/testutils/advance_phases.go b/blockchain/executor/tasks/dkg/testutils/advance_phases.go index 0c0228b5..12f9c7af 100644 --- a/blockchain/executor/tasks/dkg/testutils/advance_phases.go +++ b/blockchain/executor/tasks/dkg/testutils/advance_phases.go @@ -120,7 +120,8 @@ func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 1000*time.Millisecond) - eth := testutils.GetEthereumNetwork(t, false, n, workingDir) + //eth := testutils.GetEthereumNetwork(t, false, n, workingDir) + eth := testutils.GetEthereumNetwork(t, false, n, workingDir, accounts) assert.NotNil(t, eth) ctx := context.Background() diff --git a/blockchain/executor/tasks/dkg/testutils/setup.go b/blockchain/executor/tasks/dkg/testutils/setup.go index 6c5329ec..eb9695c7 100644 --- a/blockchain/executor/tasks/dkg/testutils/setup.go +++ b/blockchain/executor/tasks/dkg/testutils/setup.go @@ -66,8 +66,8 @@ func InitializeNewNonDetDkgStateInfo(n int) ([]*state.DkgState, []*ecdsa.Private func InitializeNewDkgStateInfo(n int, deterministicShares bool) ([]*state.DkgState, []*ecdsa.PrivateKey) { // Get private keys for validators - privKeys := testutils.SetupPrivateKeys(n) - accountsArray := testutils.SetupAccounts(privKeys) + privKeys := testutils.GeneratePrivateKeys(n) + accountsArray := testutils.GenerateAccounts(privKeys) dkgStates := []*state.DkgState{} threshold := crypto.CalcThreshold(n) diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go index b9d6b163..77acad65 100644 --- a/blockchain/testutils/cmd/deploy.go +++ b/blockchain/testutils/cmd/deploy.go @@ -1,7 +1,7 @@ package cmd import ( - "io/ioutil" + "github.com/ethereum/go-ethereum/common" "os" "path/filepath" "strings" @@ -42,12 +42,12 @@ func RunDeploy(workingDir string) (string, error) { return "", nil } - _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev setHardhatIntervalMining --interval 1000") + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev setHardhatIntervalMining --interval 100") if err != nil { return "", err } - err = RunRegister(workingDir, factoryAddress) + err = RunRegister(factoryAddress, []common.Address{}) if err != nil { return "", err } @@ -62,12 +62,12 @@ func RunDeploy(workingDir string) (string, error) { return "", err } - generatedValidatorConfigFiles := filepath.Join(workingDir, "scripts", "generated", "config") - files, _ := ioutil.ReadDir(generatedValidatorConfigFiles) - err = RunValidator(workingDir, len(files)) - if err != nil { - return "", err - } + //generatedValidatorConfigFiles := filepath.Join(workingDir, "scripts", "generated", "config") + //files, _ := ioutil.ReadDir(generatedValidatorConfigFiles) + //err = RunValidator(workingDir, len(files)) + //if err != nil { + // return "", err + //} return factoryAddress, nil } diff --git a/blockchain/testutils/cmd/register.go b/blockchain/testutils/cmd/register.go index 4d720833..bb9cf77e 100644 --- a/blockchain/testutils/cmd/register.go +++ b/blockchain/testutils/cmd/register.go @@ -1,31 +1,23 @@ package cmd import ( - "io/ioutil" - "path/filepath" + "github.com/ethereum/go-ethereum/common" "strings" ) -func RunRegister(workingDir, factoryAddress string) error { +func RunRegister(factoryAddress string, validators []common.Address) error { bridgeDir := GetBridgePath() - keys := filepath.Join(workingDir, "scripts", "generated", "keystores", "keys") - // Build validator names - files, err := ioutil.ReadDir(keys) - validators := make([]string, 0) - if err != nil { - return err - } - for _, file := range files { - if strings.HasPrefix(file.Name(), "0x546f99f244b") { - continue - } - validators = append(validators, file.Name()) + // iterate on validators and apass it as string array + + validatorAddresses := make([]string, 0) + for _, validatorAddress := range validators { + validatorAddresses = append(validatorAddresses, validatorAddress.String()) } // Register validator - _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) + _, _, err := executeCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validatorAddresses, " ")) if err != nil { return err } diff --git a/blockchain/testutils/cmd/validator.go b/blockchain/testutils/cmd/validator.go index 5250af52..dc75acc7 100644 --- a/blockchain/testutils/cmd/validator.go +++ b/blockchain/testutils/cmd/validator.go @@ -1,16 +1,35 @@ package cmd import ( - "fmt" + "io/ioutil" + "log" "path/filepath" + "strconv" ) func RunValidator(workingDir string, validatorIndex int) error { rootDir := GetProjectRootPath() - validatorConfigPath := filepath.Join(workingDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) + //validatorConfigPath := filepath.Join(workingDir, "scripts", "generated", "config", fmt.Sprintf("validator%d.toml", validatorIndex)) - _, _, err := runCommand(rootDir, "./madnet", "--config", validatorConfigPath, "validator") + // TODO - this will be runCommand() once fixed + configurationFileDir := filepath.Join(workingDir, "scripts", "generated", "config") + files, err := ioutil.ReadDir(configurationFileDir) + for _, file := range files { + src := filepath.Join(configurationFileDir, file.Name()) + dst := filepath.Join(rootDir, "scripts", "aaa", file.Name()) + _, err := CopyFileToFolder(src, dst) + if err != nil { + log.Fatalf("Error copying config file to working directory", err) + return err + } + } + _, _, err = executeCommand(rootDir, "make", "build") + + for i := 0; i < validatorIndex; i++ { + validatorI := "validatorI" + strconv.Itoa(i) + _, _, err = executeCommand(rootDir, "./madnet", "--config", filepath.Join("scripts", "aaa", validatorI), "validator") + } if err != nil { return err diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index 7fc75412..f849fd34 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -7,42 +7,40 @@ import ( "encoding/json" "github.com/MadBase/MadNet/blockchain/ethereum" "github.com/MadBase/MadNet/blockchain/testutils/cmd" - "github.com/MadBase/MadNet/logging" "github.com/MadBase/MadNet/utils" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/google/uuid" "github.com/stretchr/testify/assert" "io" - "io/ioutil" "log" "math/big" "net/http" - "path/filepath" "strconv" - "strings" "testing" ) var ( - configEndpoint = "http://localhost:8545" - configDefaultAccount = "0x546f99f244b7b58b855330ae0e2bc1b30b41302f" - configFinalityDelay = uint64(1) + configEndpoint = "http://localhost:8545" + ownerAccountAddress = "0x546f99f244b7b58b855330ae0e2bc1b30b41302f" + password = "abc123" + configFinalityDelay = uint64(1) ) -func getEthereumDetails(workingDir string) (*ethereum.Details, error) { +func getEthereumDetails(accounts []accounts.Account) (*ethereum.Details, error) { - assetKey := filepath.Join(workingDir, "assets", "test", "keys") + //root := cmd.GetProjectRootPath() + //assetKey := filepath.Join(root, "assets", "test", "keys") + //assetPasscode := filepath.Join(root, "assets", "test", "passcodes.txt") + //assetKey := filepath.Join(workingDir, "scripts", "generated", "keystores", "keys") + //assetPasscode := filepath.Join(workingDir, "scripts", "generated", "keystores", "passcodes.txt") - // TODO - create file rather tahn copy - assetPasscode := filepath.Join(workingDir, "assets", "test", "passcodes.txt") - - details, err := ethereum.NewEndpoint( + // TODO - use mock + details, err := ethereum.NewEndpointWithAccount( configEndpoint, - assetKey, - assetPasscode, - configDefaultAccount, + accounts, configFinalityDelay, 500, 0, @@ -50,7 +48,7 @@ func getEthereumDetails(workingDir string) (*ethereum.Details, error) { return details, err } -func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workingDir string) *ethereum.Details { +func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workingDir string, accounts []accounts.Account) *ethereum.Details { log.Printf("Starting HardHat ...") err := cmd.RunHardHatNode() @@ -59,7 +57,7 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workin err = cmd.WaitForHardHatNode(ctx) assert.Nilf(t, err, "Failed to wait for hardhat to be up and running") - details, err := getEthereumDetails(workingDir) + details, err := getEthereumDetails(accounts) assert.Nilf(t, err, "Failed to build Ethereum endpoint") assert.NotNilf(t, details, "Ethereum network should not be Nil") @@ -82,23 +80,23 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workin } log.Printf("Registering %d validators ...", len(validatorAddresses)) - //err = cmd.RunRegister("", factoryAddress) + err = cmd.RunRegister(factoryAddress, nil) if err != nil { details.Close() assert.Nilf(t, err, "Error registering validators: %v") } - logger := logging.GetLogger("test").WithField("test", 0) + //logger := logging.GetLogger("test").WithField("test", 0) - log.Printf("Funding accounts ...") - for _, account := range knownAccounts[1:] { - txn, err := ethereum.TransferEther(details, logger, details.GetDefaultAccount().Address, account.Address, big.NewInt(100000000000000000)) - assert.Nilf(t, err, "Error in TrasferEther transaction") - assert.NotNilf(t, txn, "Expected transaction not to be nil") - } + //log.Printf("Funding accounts ...") + //for _, account := range knownAccounts[1:] { + // txn, err := ethereum.TransferEther(details, logger, details.GetDefaultAccount().Address, account.Address, big.NewInt(100000000000000000)) + // assert.Nilf(t, err, "Error in TrasferEther transaction") + // assert.NotNilf(t, txn, "Expected transaction not to be nil") + //} return details } -func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, workingDir string) ethereum.Network { +func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, workingDir string, accounts []accounts.Account) ethereum.Network { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -106,7 +104,7 @@ func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, work isRunning, _ := cmd.IsHardHatRunning() if !isRunning { log.Printf("Hardhat is not running. Start new HardHat") - details := startHardHat(t, ctx, validatorsCount, workingDir) + details := startHardHat(t, ctx, validatorsCount, workingDir, accounts) assert.NotNilf(t, details, "Expected details to be not nil") return details } @@ -115,12 +113,13 @@ func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, work err := cmd.StopHardHat() assert.Nilf(t, err, "Failed to stopHardHat") - details := startHardHat(t, ctx, validatorsCount, workingDir) + details := startHardHat(t, ctx, validatorsCount, workingDir, accounts) assert.NotNilf(t, details, "Expected details to be not nil") return details } - network, err := getEthereumDetails(workingDir) + //network, err := getEthereumDetails(workingDir) + network, err := getEthereumDetails(accounts) assert.Nilf(t, err, "Failed to build Ethereum endpoint") assert.NotNilf(t, network, "Ethereum network should not be Nil") @@ -137,8 +136,8 @@ func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, work // ======================================================== // ======================================================== -// SetupPrivateKeys computes deterministic private keys for testing -func SetupPrivateKeys(n int) []*ecdsa.PrivateKey { +// GeneratePrivateKeys computes deterministic private keys for testing +func GeneratePrivateKeys(n int) []*ecdsa.PrivateKey { if (n < 1) || (n >= 256) { panic("invalid number for accounts") } @@ -162,8 +161,8 @@ func SetupPrivateKeys(n int) []*ecdsa.PrivateKey { return privKeyArray } -// SetupAccounts derives the associated addresses from private keys -func SetupAccounts(privKeys []*ecdsa.PrivateKey) []accounts.Account { +// GenerateAccounts derives the associated addresses from private keys +func GenerateAccounts(privKeys []*ecdsa.PrivateKey) []accounts.Account { accountsArray := []accounts.Account{} for _, pk := range privKeys { commonAddr := crypto.PubkeyToAddress(pk.PublicKey) @@ -176,48 +175,28 @@ func SetupAccounts(privKeys []*ecdsa.PrivateKey) []accounts.Account { func InitializePrivateKeysAndAccounts(n int) ([]*ecdsa.PrivateKey, []accounts.Account) { _, pKey, err := GetOwnerAccount() if err != nil { + // TODO - don't think panic is the right solution here panic(err) } - //t.Logf("owner: %v, pvKey: %v", account.Address.String(), key.PrivateKey) privateKeys := []*ecdsa.PrivateKey{pKey} - randomPrivateKeys := SetupPrivateKeys(n - 1) + randomPrivateKeys := GeneratePrivateKeys(n - 1) privateKeys = append(privateKeys, randomPrivateKeys...) - accounts := SetupAccounts(privateKeys) + accounts := GenerateAccounts(privateKeys) return privateKeys, accounts } func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { - rootPath := cmd.GetProjectRootPath() - - // Account - acctAddress := configDefaultAccount - acctAddressLowerCase := strings.ToLower(acctAddress) - - // Password - passwordFull := filepath.Join(rootPath, "scripts", "base-files", "passwordFile") - passwordFileContent, err := ioutil.ReadFile(passwordFull) - if err != nil { - log.Printf("Error opening password file. %v", err) - return nil, nil, err - } - password := string(passwordFileContent) - - // Wallet - walletFull := filepath.Join(rootPath, "scripts", "base-files", acctAddressLowerCase) - jsonBytes, err := ioutil.ReadFile(walletFull) - if err != nil { - log.Printf("Error opening %v file. %v", acctAddressLowerCase, err) - return nil, nil, err - } - key, err := keystore.DecryptKey(jsonBytes, password) - if err != nil { - log.Printf("Error decrypting jsonBytes. %v", err) - return nil, nil, err + id, _ := uuid.Parse("6b2a0716-b444-46c3-a1e3-2936ddd8ecc5") + pk, _ := crypto.HexToECDSA("6aea45ee1273170fb525da34015e4f20ba39fe792f486ba74020bcacc9badfc1") + addr := common.HexToAddress("0x546F99F244b7B58B855330AE0E2BC1b30b41302F") + key := &keystore.Key{ + Id: id, + Address: addr, + PrivateKey: pk, } - return &key.Address, key.PrivateKey, nil } @@ -255,6 +234,7 @@ func Init(workingDir string, n int) error { err := cmd.RunInit(workingDir, n) if err != nil { + log.Fatal("----- INIT FAILED ----") return err } @@ -268,6 +248,7 @@ func Init(workingDir string, n int) error { return nil } +// TODO - check this one // sendHardhatCommand sends a command to the hardhat server via an RPC call func sendHardhatCommand(command string, params ...interface{}) error { @@ -347,7 +328,7 @@ func SetNextBlockBaseFee(target uint64) { } } -// Enable/disable hardhat autoMine +// Enable/disable hardhat autoMine TODO - check this one func SetAutoMine(t *testing.T, eth ethereum.Network, autoMine bool) { log.Printf("Setting Automine to %v", autoMine) diff --git a/cmd/main.go b/cmd/main.go index b6ca3f6e..ccf33995 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -209,6 +209,8 @@ func main() { cFlags.IntVarP(intPtr, o.name, o.short, 0, o.usage) } else if uint64Ptr, ok := o.value.(*uint64); ok { cFlags.Uint64VarP(uint64Ptr, o.name, o.short, 0, o.usage) + } else if uint32Ptr, ok := o.value.(*uint32); ok { + cFlags.Uint32VarP(uint32Ptr, o.name, o.short, 0, o.usage) } else if boolPtr, ok := o.value.(*bool); ok { cFlags.BoolVarP(boolPtr, o.name, o.short, false, o.usage) } else { diff --git a/scripts/base-scripts/deploy.sh b/scripts/base-scripts/deploy.sh index 2420de3e..13e4d2c7 100755 --- a/scripts/base-scripts/deploy.sh +++ b/scripts/base-scripts/deploy.sh @@ -11,52 +11,60 @@ cd $BRIDGE_DIR # if on hardhat network this switches automine on to deploy faster npx hardhat setHardhatIntervalMining --network $NETWORK --enable-auto-mine + # TODO - NO NEEDED # Copy the deployList to the generated folder so we have deploymentList and deploymentArgsTemplate in the same folder cp ../scripts/base-files/deploymentList ../scripts/generated/deploymentList cp ../scripts/base-files/deploymentArgsTemplate ../scripts/generated/deploymentArgsTemplate + # TODO - NO NEEDED +#It can be the one in base-file. no need to be copied and not linked in genereater npx hardhat --network "$NETWORK" --show-stack-traces deployContracts --input-folder ../scripts/generated addr="$(grep -Pzo "\[$NETWORK\]\ndefaultFactoryAddress = \".*\"\n" ../scripts/generated/factoryState | grep -a "defaultFactoryAddress = .*" | awk '{print $NF}')" -export FACTORY_ADDRESS=$addr -if [[ -z "${FACTORY_ADDRESS}" ]]; then - echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Exiting script!" - exit 1 -fi - -for filePath in $(ls ../scripts/generated/config | xargs); do - sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/config/$filePath" > "../scripts/generated/config/$filePath".bk &&\ - mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" -done - -cp ../scripts/base-files/owner.toml ../scripts/generated/owner.toml -sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/owner.toml" > "../scripts/generated/owner.toml".bk &&\ -mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" -# funds validator accounts -npx hardhat fundValidators --network $NETWORK -cd $CURRENT_WD +#export FACTORY_ADDRESS=$addr +#if [[ -z "${FACTORY_ADDRESS}" ]]; then +# echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Exiting script!" +# exit 1 +#fi +# +#for filePath in $(ls ../scripts/generated/config | xargs); do +# sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/config/$filePath" > "../scripts/generated/config/$filePath".bk &&\ +# mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" +#done -if [[ ! -z "${SKIP_REGISTRATION}" ]]; then - echo "SKIPPING VALIDATOR REGISTRATION" - exit 0 -fi +#cp ../scripts/base-files/owner.toml ../scripts/generated/owner.toml +#sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/owner.toml" > "../scripts/generated/owner.toml".bk &&\ +#mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" -FACTORY_ADDRESS="$(echo "$addr" | sed -e 's/^"//' -e 's/"$//')" -if [[ -z "${FACTORY_ADDRESS}" ]]; then - echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Not starting the registration!" - exit 1 -fi + # TODO - NO NEEDED +# golang base - TransferEther() from owner to +# funds validator accounts +#npx hardhat fundValidators --network $NETWORK +#cd $CURRENT_WD +# +#if [[ ! -z "${SKIP_REGISTRATION}" ]]; then +# echo "SKIPPING VALIDATOR REGISTRATION" +# exit 0 +#fi +# +#FACTORY_ADDRESS="$(echo "$addr" | sed -e 's/^"//' -e 's/"$//')" +# +#if [[ -z "${FACTORY_ADDRESS}" ]]; then +# echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Not starting the registration!" +# exit 1 +#fi +# cd $BRIDGE_DIR -npx hardhat setHardhatIntervalMining --network $NETWORK --interval 1000 +npx hardhat setHardhatIntervalMining --network $NETWORK --interval 100 cd $CURRENT_WD ./scripts/main.sh register cd $BRIDGE_DIR -npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 10 +npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 1 npx hardhat setHardhatIntervalMining --network $NETWORK cd $CURRENT_WD diff --git a/scripts/base-scripts/register.sh b/scripts/base-scripts/register.sh index 14671ae7..29367d84 100755 --- a/scripts/base-scripts/register.sh +++ b/scripts/base-scripts/register.sh @@ -14,6 +14,9 @@ if [[ -z "${FACTORY_ADDRESS}" ]]; then exit 1 fi + + # TODO - NO NEEDED +# CONTROL on which account will be validators (once we got the address of the factory) npx hardhat --network "$NETWORK" --show-stack-traces registerValidators --factory-address "$FACTORY_ADDRESS" $ADDRESSES From 38db03c7a9b164413aac0c052b0246d50e9b1261 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Fri, 17 Jun 2022 14:45:53 +0200 Subject: [PATCH 13/15] continuing replacing scripts --- blockchain/ethereum/ethereum.go | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/blockchain/ethereum/ethereum.go b/blockchain/ethereum/ethereum.go index 849677d5..32e5cc34 100644 --- a/blockchain/ethereum/ethereum.go +++ b/blockchain/ethereum/ethereum.go @@ -192,6 +192,62 @@ func NewEndpoint( return eth, nil } +// NewEndpointWithAccount creates a new Ethereum abstraction +func NewEndpointWithAccount(endpoint string, accountInput []accounts.Account, finalityDelay uint64, txMaxGasFeeAllowedInGwei uint64, endpointMinimumPeers uint32) (*Details, error) { + + logger := logging.GetLogger("ethereum") + ownerAccount := accountInput[0] + + if txMaxGasFeeAllowedInGwei < constants.EthereumMinGasFeeAllowedInGwei { + return nil, fmt.Errorf("txMaxGasFeeAllowedInGwei should be greater than %v Gwei", constants.EthereumMinGasFeeAllowedInGwei) + } + + txMaxGasFeeAllowedInWei := new(big.Int).Mul(new(big.Int).SetUint64(txMaxGasFeeAllowedInGwei), new(big.Int).SetUint64(1_000_000_000)) + + accountList := make(map[common.Address]accounts.Account) + passCodes := make(map[common.Address]string) + for _, acc := range accountInput { + accountList[acc.Address] = acc + passCodes[acc.Address] = "abc123" + } + + eth := &Details{ + endpoint: endpoint, + logger: logger, + accounts: accountList, + keys: make(map[common.Address]*keystore.Key), + passCodes: passCodes, + finalityDelay: finalityDelay, + txMaxGasFeeAllowed: txMaxGasFeeAllowedInWei, + } + + eth.contracts = NewContractDetails(eth) + eth.setDefaultAccount(ownerAccount) + if err := eth.unlockAccount(ownerAccount); err != nil { + return nil, fmt.Errorf("Could not unlock account: %v", err) + } + + // Low level rpc client + ctx, cancel := context.WithTimeout(context.Background(), constants.MonitorTimeout) + defer cancel() + rpcClient, rpcErr := rpc.DialContext(ctx, endpoint) + if rpcErr != nil { + return nil, fmt.Errorf("Error in NewEndpoint at rpc.DialContext: %v", rpcErr) + } + eth.rpcClient = rpcClient + ethClient := ethclient.NewClient(rpcClient) + eth.client = ethClient + + chainId, err := ethClient.ChainID(ctx) + if err != nil { + return nil, fmt.Errorf("Error in NewEndpoint at ethClient.ChainID: %v", err) + } + eth.chainID = chainId + + logger.Debug("Completed initialization") + return eth, nil +} + //LoadAccounts Scans the directory specified and loads all the accounts found func (eth *Details) loadAccounts(directoryPath string) { logger := eth.logger From 84d3d230b4ce7fc85f405b43b9d8796aca49c524 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Fri, 24 Jun 2022 17:59:59 +0200 Subject: [PATCH 14/15] continuing replacing scripts --- blockchain/ethereum/ethereum.go | 110 ++++++++++-- .../executor/tasks/dkg/completion_test.go | 8 +- .../tasks/dkg/testutils/advance_phases.go | 41 ++++- blockchain/testutils/cmd/deploy.go | 16 +- blockchain/testutils/cmd/executor.go | 2 +- blockchain/testutils/cmd/init.go | 77 ++++---- blockchain/testutils/cmd/register.go | 17 +- blockchain/testutils/cmd/setup.go | 168 ++++++++++-------- blockchain/testutils/setup.go | 113 ++++++++---- scripts/base-scripts/deploy.sh | 64 +++---- 10 files changed, 399 insertions(+), 217 deletions(-) diff --git a/blockchain/ethereum/ethereum.go b/blockchain/ethereum/ethereum.go index 32e5cc34..23d50133 100644 --- a/blockchain/ethereum/ethereum.go +++ b/blockchain/ethereum/ethereum.go @@ -3,12 +3,15 @@ package ethereum import ( "bufio" "context" + "crypto/ecdsa" "encoding/json" "errors" "fmt" "io/ioutil" + "log" "math/big" "os" + "path/filepath" "strings" "time" @@ -193,10 +196,18 @@ func NewEndpoint( } // NewEndpointWithAccount creates a new Ethereum abstraction -func NewEndpointWithAccount(endpoint string, accountInput []accounts.Account, finalityDelay uint64, txMaxGasFeeAllowedInGwei uint64, endpointMinimumPeers uint32) (*Details, error) { +func NewEndpointWithAccount(endpoint string, workingDir string, accountInput map[accounts.Account]*ecdsa.PrivateKey, finalityDelay uint64, txMaxGasFeeAllowedInGwei uint64, endpointMinimumPeers uint32) (*Details, error) { logger := logging.GetLogger("ethereum") - ownerAccount := accountInput[0] + log.Printf("********** At this point account pk map addresses looks like this") + var ownerAccount accounts.Account + for k := range accountInput { + log.Printf("%s", k.Address.String()) + if k.Address.String() == "0x546F99F244b7B58B855330AE0E2BC1b30b41302F" { + ownerAccount = k + } + } + log.Printf("********** 4 - Owner account address in NewEndpointWithAccount() method %s", ownerAccount.Address.String()) if txMaxGasFeeAllowedInGwei < constants.EthereumMinGasFeeAllowedInGwei { return nil, fmt.Errorf("txMaxGasFeeAllowedInGwei should be greater than %v Gwei", constants.EthereumMinGasFeeAllowedInGwei) @@ -206,10 +217,6 @@ func NewEndpointWithAccount(endpoint string, accountInput []accounts.Account, fi accountList := make(map[common.Address]accounts.Account) passCodes := make(map[common.Address]string) - for _, acc := range accountInput { - accountList[acc.Address] = acc - passCodes[acc.Address] = "abc123" - } eth := &Details{ endpoint: endpoint, @@ -221,11 +228,34 @@ func NewEndpointWithAccount(endpoint string, accountInput []accounts.Account, fi txMaxGasFeeAllowed: txMaxGasFeeAllowedInWei, } + keystorePath := filepath.Join(workingDir, "scripts", "generated", "keystores", "keys") + eth.keystore = keystore.NewKeyStore(keystorePath, keystore.StandardScryptN, keystore.StandardScryptP) + + // Load accounts + passCodes + eth.loadAccounts(keystorePath) + //eth.loadWallets(accountInput, workingDir) + + //for k := range accountInput { + // accountList[k.Address] = k + // passCodes[k.Address] = "abc123" + //} + //passcode, _ := eth.passCodes[ownerAccount.Address] + + eth.addEthereumPasscodes(accountInput) + + log.Printf("********** 7 - Unlock owner account %s with passphrase %s", ownerAccount.Address.String(), eth.passCodes[ownerAccount.Address]) + err := eth.keystore.Unlock(ownerAccount, eth.passCodes[ownerAccount.Address]) + if err != nil { + return nil, err + } + eth.contracts = NewContractDetails(eth) eth.setDefaultAccount(ownerAccount) - if err := eth.unlockAccount(ownerAccount); err != nil { - return nil, fmt.Errorf("Could not unlock account: %v", err) - } + // TODO - what is the difference with the one above + //err := eth.unlockAccount(ownerAccount) + //if err != nil { + // return nil, fmt.Errorf("Could not unlock account: %v", err) + //} // Low level rpc client ctx, cancel := context.WithTimeout(context.Background(), constants.MonitorTimeout) @@ -249,18 +279,19 @@ func NewEndpointWithAccount(endpoint string, accountInput []accounts.Account, fi } //LoadAccounts Scans the directory specified and loads all the accounts found -func (eth *Details) loadAccounts(directoryPath string) { - logger := eth.logger +func (eth *Details) loadAccounts(keystorePath string) { - logger.Infof("LoadAccounts(\"%v\")...", directoryPath) - ks := keystore.NewKeyStore(directoryPath, keystore.StandardScryptN, keystore.StandardScryptP) + log.Printf("********** 5 - Load accounts from %s", keystorePath) + ks := keystore.NewKeyStore(keystorePath, keystore.StandardScryptN, keystore.StandardScryptP) accts := make(map[common.Address]accounts.Account, 10) acctIndex := make(map[common.Address]int, 10) + log.Printf("********** 6 - Keystore contains %d wallets", len(ks.Wallets())) + var index int for _, wallet := range ks.Wallets() { for _, account := range wallet.Accounts() { - logger.Infof("... found account %v", account.Address.Hex()) + log.Printf(" accts[%s] = account value", account.Address.String()) accts[account.Address] = account acctIndex[account.Address] = index index++ @@ -272,6 +303,26 @@ func (eth *Details) loadAccounts(directoryPath string) { eth.keystore = ks } +//LoadAccounts Scans the directory specified and loads all the accounts found +func (eth *Details) loadWallets(accountMap map[accounts.Account]*ecdsa.PrivateKey, workingDir string) { + + ks := keystore.NewKeyStore(workingDir, keystore.StandardScryptN, keystore.StandardScryptP) + accts := make(map[common.Address]accounts.Account, 10) + acctIndex := make(map[common.Address]int, 10) + + var index int + for k := range accountMap { + accts[k.Address] = k + acctIndex[k.Address] = index + acctIndex[k.Address] = index + index++ + } + + eth.accounts = accts + eth.accountIndex = acctIndex + eth.keystore = ks +} + // LoadPasscodes loads the specified passcode file func (eth *Details) loadPassCodes(filePath string) error { logger := eth.logger @@ -306,20 +357,43 @@ func (eth *Details) loadPassCodes(filePath string) error { return nil } +// LoadPasscodes loads the specified passcode file +func (eth *Details) addEthereumPasscodes(accountMap map[accounts.Account]*ecdsa.PrivateKey) { + passcodes := make(map[common.Address]string) + for k := range accountMap { + passcodes[common.HexToAddress(k.Address.String())] = "abc123" + } + eth.passCodes = passcodes +} + // UnlockAccount unlocks the previously loaded account using the previously loaded passCodes func (eth *Details) unlockAccount(acct accounts.Account) error { eth.logger.Infof("Unlocking account address:%v", acct.Address.String()) + for k, v := range eth.passCodes { + log.Printf("%s=%s", k.String(), v) + } passCode, passCodeFound := eth.passCodes[acct.Address] if !passCodeFound { return ErrPassCodeNotFound } - err := eth.keystore.Unlock(acct, passCode) - if err != nil { - return err - } + //addr := common.Address{} + //addr.SetBytes([]byte("546F99F244b7B58B855330AE0E2BC1b30b41302F")) + // + //create a DkgState obj + //acct = accounts.Account{ + // Address: addr, + // URL: accounts.URL{ + // Scheme: "http", + // Path: "", + // }, + //} + //err := eth.keystore.Unlock(acct, "abc123") + //if err != nil { + // return err + //} // Open the account key file keyJSON, err := ioutil.ReadFile(acct.URL.Path) diff --git a/blockchain/executor/tasks/dkg/completion_test.go b/blockchain/executor/tasks/dkg/completion_test.go index 0396269a..6a580878 100644 --- a/blockchain/executor/tasks/dkg/completion_test.go +++ b/blockchain/executor/tasks/dkg/completion_test.go @@ -19,12 +19,10 @@ import ( // We complete everything correctly, happy path func TestCompletion_Group_1_AllGood(t *testing.T) { - workingDir := cmd.CreateTestWorkingFolder() - n := 5 - err := testutils.Init(workingDir, n) + n := 4 + _, err := testutils.BuildTestEnvironment(t, n) assert.Nil(t, err) - - suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100, workingDir) + suite := dkgTestUtils.StartFromMPKSubmissionPhase(t, n, 100) defer suite.Eth.Close() ctx := context.Background() eth := suite.Eth diff --git a/blockchain/executor/tasks/dkg/testutils/advance_phases.go b/blockchain/executor/tasks/dkg/testutils/advance_phases.go index 12f9c7af..d0b835c1 100644 --- a/blockchain/executor/tasks/dkg/testutils/advance_phases.go +++ b/blockchain/executor/tasks/dkg/testutils/advance_phases.go @@ -11,13 +11,16 @@ import ( "github.com/MadBase/MadNet/blockchain/executor/tasks/dkg/utils" "github.com/MadBase/MadNet/blockchain/monitor/events" testutils "github.com/MadBase/MadNet/blockchain/testutils" + "github.com/MadBase/MadNet/blockchain/testutils/cmd" "github.com/MadBase/MadNet/blockchain/transaction" "github.com/MadBase/MadNet/bridge/bindings" "github.com/MadBase/MadNet/crypto/bn256" "github.com/MadBase/MadNet/crypto/bn256/cloudflare" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/core/types" + "log" "math/big" "strings" "testing" @@ -117,20 +120,41 @@ func InitializeETHDKG(eth ethereum.Network, callOpts *bind.TransactOpts, ctx con } func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators int, phaseLength uint16, workingDir string) *TestSuite { - ecdsaPrivateKeys, accounts := testutils.InitializePrivateKeysAndAccounts(n) + //ecdsaPrivateKeys, accountList := testutils.InitializePrivateKeysAndAccounts(n) + log.Printf("********** 0 - Testing with %d validators", n) + + accountMap := testutils.InitializePrivateKeysAndAccountsMap(n) + + log.Printf("*** running init") + err := cmd.RunInit(workingDir, n, accountMap) + assert.Nil(t, err) //eth := testutils.GetEthereumNetwork(t, ecdsaPrivateKeys, 1000*time.Millisecond) //eth := testutils.GetEthereumNetwork(t, false, n, workingDir) - eth := testutils.GetEthereumNetwork(t, false, n, workingDir, accounts) + eth := testutils.GetEthereumNetwork(t, false, n, workingDir, accountMap) assert.NotNil(t, eth) + log.Printf("********** 8 - Got ETH network") + log.Printf("********** 9 - eth default account (owner) address: %s", eth.GetDefaultAccount().Address.String()) ctx := context.Background() - owner := accounts[0] + var owner = eth.GetDefaultAccount() + + accountList := make([]accounts.Account, 0) + ecdsaPrivateKeys := make([]*ecdsa.PrivateKey, 0) + for a, k := range accountMap { + accountList = append(accountList, a) + ecdsaPrivateKeys = append(ecdsaPrivateKeys, k) + } // Start EthDKG ownerOpts, err := eth.GetTransactionOpts(ctx, owner) assert.Nil(t, err) + log.Printf("********** 10 - Owner opts %v", ownerOpts) + log.Printf("********** 11 - at this point account map looks like this") + for v := range accountMap { + log.Printf(" %s", v.Address.String()) + } // Shorten ethdkg phase for testing purposes _, _, err = SetETHDKGPhaseLength(phaseLength, eth, ownerOpts, ctx) assert.Nil(t, err) @@ -153,19 +177,20 @@ func StartFromRegistrationOpenPhase(t *testing.T, n int, unregisteredValidators assert.Nil(t, err) assert.Equal(t, uint8(state.RegistrationOpen), phase) - valCount, err := eth.Contracts().ValidatorPool().GetValidatorsCount(callOpts) - assert.Nil(t, err) - assert.Equal(t, uint64(n), valCount.Uint64()) + // TODO - remove owner + //valCount, err := eth.Contracts().ValidatorPool().GetValidatorsCount(callOpts) + //assert.Nil(t, err) + //assert.Equal(t, uint64(n), valCount.Uint64()) // Do Register task regTasks := make([]*dkg.RegisterTask, n) dispMissingRegTasks := make([]*dkg.DisputeMissingRegistrationTask, n) dkgStates := make([]*state.DkgState, n) for idx := 0; idx < n; idx++ { - logger := logging.GetLogger("test").WithField("Validator", accounts[idx].Address.String()) + logger := logging.GetLogger("test").WithField("Validator", accountList[idx].Address.String()) // Set Registration success to true state, regTask, dispMissingRegTask := events.UpdateStateOnRegistrationOpened( - accounts[idx], + accountList[idx], event.StartBlock.Uint64(), event.PhaseLength.Uint64(), event.ConfirmationLength.Uint64(), diff --git a/blockchain/testutils/cmd/deploy.go b/blockchain/testutils/cmd/deploy.go index 77acad65..dfa9b732 100644 --- a/blockchain/testutils/cmd/deploy.go +++ b/blockchain/testutils/cmd/deploy.go @@ -1,13 +1,14 @@ package cmd import ( - "github.com/ethereum/go-ethereum/common" + "crypto/ecdsa" + "github.com/ethereum/go-ethereum/accounts" "os" "path/filepath" "strings" ) -func RunDeploy(workingDir string) (string, error) { +func RunDeploy(workingDir string, accountPrivateKeyMap map[accounts.Account]*ecdsa.PrivateKey) (string, error) { bridgeDir := GetBridgePath() _, _, err := executeCommand(bridgeDir, "npx", "hardhat --network dev setHardhatIntervalMining --enable-auto-mine") @@ -32,6 +33,8 @@ func RunDeploy(workingDir string) (string, error) { return "", err } + // Replace filename + _, _, err = executeCommand(bridgeDir, "npx", "hardhat --network dev fundValidators --config-path", filepath.Join(workingDir, "scripts", "generated", "config")) if err != nil { return "", err @@ -47,7 +50,14 @@ func RunDeploy(workingDir string) (string, error) { return "", err } - err = RunRegister(factoryAddress, []common.Address{}) + var validatorsAddressList []string + for k := range accountPrivateKeyMap { + if k.Address.String() != "0x546F99F244b7B58B855330AE0E2BC1b30b41302F" { + validatorsAddressList = append(validatorsAddressList, k.Address.String()) + } + } + + err = RunRegister(factoryAddress, validatorsAddressList) if err != nil { return "", err } diff --git a/blockchain/testutils/cmd/executor.go b/blockchain/testutils/cmd/executor.go index 4b159cd0..b749aeb6 100644 --- a/blockchain/testutils/cmd/executor.go +++ b/blockchain/testutils/cmd/executor.go @@ -199,7 +199,7 @@ func ReplaceGenesisBalance(workingDir string) error { regex := regexp.MustCompile(`balance.*`) result := regex.ReplaceAllString(fileContent, "balance\": \"10000000000000000000000\" }") - f, err := os.Create(genesisFilePath) + f, err := os.Create(filepath.Join(workingDir, "scripts", "generated", "genesis.json")) if err != nil { log.Fatalf("Error creating modified genesis.json file - %v", err) return err diff --git a/blockchain/testutils/cmd/init.go b/blockchain/testutils/cmd/init.go index 1db9135a..e5c62acb 100644 --- a/blockchain/testutils/cmd/init.go +++ b/blockchain/testutils/cmd/init.go @@ -3,77 +3,82 @@ package cmd import ( "errors" "fmt" + "io/ioutil" + "log" "os" "path/filepath" "strings" ) -func RunInit(workingDir string, numbersOfValidator int) error { - - // Resources setup - err := RunSetup(workingDir) - if err != nil { - return err - } +var ( + password = "abc123" +) - err = RunGitHooks() - if err != nil { - return err - } +func RunInit(workingDir string, numbersOfValidator int) ([]string, error) { // Ports listeningPort := 4242 p2pPort := 4343 discoveryPort := 4444 - localStatatePort := 8884 + localStataPort := 8884 // Validator instance check if numbersOfValidator < 4 || numbersOfValidator > 32 { - return errors.New("number of possible validators can be from 4 up to 32") + return nil, errors.New("number of possible validators can be from 4 up to 32") } + // Build validator configuration files rootPath := GetProjectRootPath() - for i := 1; i < numbersOfValidator; i++ { + tempFile := temporaryFile() + passcodeFilePath := filepath.Join(workingDir, "keystores", "passcodes.txt") + passcodesFile, err := os.Create(passcodeFilePath) + if err != nil { + return nil, err + } + defer passcodesFile.Close() - passwordFilePath := filepath.Join(workingDir, "scripts", "base-files", "passwordFile") - _, stdout, err := executeCommand(rootPath, "ethkey", "generate --passwordfile "+passwordFilePath) + validatorAddresses := make([]string, 0) + for i := 0; i < numbersOfValidator; i++ { + + _, stdout, err := executeCommand(rootPath, "ethkey", "generate --passwordfile "+tempFile) if err != nil { - return err + return nil, err } address := string(stdout[:]) address = strings.ReplaceAll(address, "Address: ", "") address = strings.ReplaceAll(address, "\n", "") + validatorAddresses = append(validatorAddresses, address) // Generate private key - privateKey, err := RandomHex(16) + privateKey, err := RandomHex(16) // TODO - is this right? if err != nil { - return err + return nil, err } // Validator configuration file - err = ReplaceConfigurationFile(workingDir, address, privateKey, listeningPort, p2pPort, discoveryPort, localStatatePort, i) + err = ReplaceConfigurationFile(workingDir, address, privateKey, listeningPort, p2pPort, discoveryPort, localStataPort, i) if err != nil { - return err + return nil, err } - f, err := os.Create(filepath.Join(workingDir, "scripts", "generated", "keystores", "passcodes.txt")) + // Passcode file + passcodesFile, err := os.OpenFile(passcodeFilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) if err != nil { - return err + panic(err) } - defer f.Close() - _, err = f.WriteString(fmt.Sprintf("%s=abc123", address)) + _, err = passcodesFile.WriteString(fmt.Sprintf("%s=%s\n", address, password)) if err != nil { - return err + return nil, err } // Genesis err = ReplaceGenesisBalance(workingDir) if err != nil { - return err + return nil, err } // Keyfile.json - _, err = CopyFileToFolder(filepath.Join(rootPath, "keyfile.json"), filepath.Join(workingDir, "scripts", "generated", "keystores", "keys", address)) + _, err = CopyFileToFolder(filepath.Join(rootPath, "keyfile.json"), filepath.Join(workingDir, "keystores", "keys", address)) if err != nil { fmt.Print("Error copying keyfile.json into generated folder") } @@ -85,8 +90,20 @@ func RunInit(workingDir string, numbersOfValidator int) error { listeningPort += 1 p2pPort += 1 discoveryPort += 1 - localStatatePort += 1 + localStataPort += 1 } - return nil + return validatorAddresses, nil +} + +func temporaryFile() string { + f, err := ioutil.TempFile("", "") + if err != nil { + log.Fatal(err) + } + _, err = f.WriteString(password) + if err != nil { + log.Fatal(err) + } + return f.Name() } diff --git a/blockchain/testutils/cmd/register.go b/blockchain/testutils/cmd/register.go index bb9cf77e..6b7f2666 100644 --- a/blockchain/testutils/cmd/register.go +++ b/blockchain/testutils/cmd/register.go @@ -1,23 +1,26 @@ package cmd import ( - "github.com/ethereum/go-ethereum/common" "strings" ) -func RunRegister(factoryAddress string, validators []common.Address) error { +func RunRegister(factoryAddress string, validators []string) error { bridgeDir := GetBridgePath() // iterate on validators and apass it as string array - validatorAddresses := make([]string, 0) - for _, validatorAddress := range validators { - validatorAddresses = append(validatorAddresses, validatorAddress.String()) - } + //validatorAddresses := make([]string, 0) + //for _, validatorAddress := range validators { + // validatorAddresses = append(validatorAddresses, validatorAddress.String()) + //} + //validatorAddresses = append(validatorAddresses, "0x61Ae54Fb4DB3d5b0f43Bd24553f69262c5Bc174d") + //validatorAddresses = append(validatorAddresses, "0x671496a9eb9cd271c05A07Bb71d52656e3c57817") + //validatorAddresses = append(validatorAddresses, "0x913cFad222B2152D5781Aae072113160eA3891Ab") + //validatorAddresses = append(validatorAddresses, "0xE3179f6517f1e5752af5C67Ba0259fA883A315E3") // Register validator - _, _, err := executeCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validatorAddresses, " ")) + _, _, err := executeCommand(bridgeDir, "npx", "hardhat --network dev --show-stack-traces registerValidators --factory-address", factoryAddress, strings.Join(validators, " ")) if err != nil { return err } diff --git a/blockchain/testutils/cmd/setup.go b/blockchain/testutils/cmd/setup.go index c500e1c6..1d6a3fec 100644 --- a/blockchain/testutils/cmd/setup.go +++ b/blockchain/testutils/cmd/setup.go @@ -1,100 +1,110 @@ package cmd import ( - "fmt" - "io/ioutil" - "log" - "os" "path/filepath" ) func RunSetup(workingDir string) error { - // Create directories - folders := []string{ - filepath.Join("scripts", "base-files"), - filepath.Join("scripts", "generated", "monitorDBs"), - filepath.Join("scripts", "generated", "config"), - filepath.Join("scripts", "generated", "keystores"), - filepath.Join("scripts", "generated", "keystores", "keys"), - filepath.Join("assets", "test", "keys"), - } - for _, folder := range folders { - if err := os.MkdirAll(filepath.Join(workingDir, folder), os.ModePerm); err != nil { - fmt.Printf("Error creating configuration folders: %v", err) - return err - } - } - - // Copy configuration files rootPath := GetProjectRootPath() - configurationFileDir := filepath.Join(rootPath, "scripts", "base-files") - files, err := ioutil.ReadDir(configurationFileDir) + _, err := CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentList"), filepath.Join(workingDir, "deploymentList")) if err != nil { - log.Fatalf("Error reading configuaration file dir path: %s", configurationFileDir) return err } - for _, file := range files { - src := filepath.Join(configurationFileDir, file.Name()) - dst := filepath.Join(workingDir, "scripts", "base-files", file.Name()) - _, err = CopyFileToFolder(src, dst) - if err != nil { - log.Fatalf("Error copying config file to working directory", err) - return err - } - } - - // Copy asset files - assetFileDir := filepath.Join(rootPath, "assets", "test", "keys") - files, err = ioutil.ReadDir(assetFileDir) + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentArgsTemplate"), filepath.Join(workingDir, "deploymentList")) if err != nil { - log.Fatalf("Error reading asset file dir path: %s", assetFileDir) return err } - for _, file := range files { - src := filepath.Join(assetFileDir, file.Name()) - dst := filepath.Join(workingDir, "assets", "test", "keys", file.Name()) - _, err = CopyFileToFolder(src, dst) - if err != nil { - log.Fatalf("Error copying assets file to working directory: %v", err) - return err - } - } - _, err = CopyFileToFolder(filepath.Join(rootPath, "assets", "test", "blockheaders.txt"), filepath.Join(workingDir, "assets", "test", "blockheaders.txt")) + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentList"), filepath.Join(workingDir, "deploymentList")) if err != nil { - log.Fatalf("Error reading asset blockheaders: %s", assetFileDir) - return err - } - _, err = CopyFileToFolder(filepath.Join(rootPath, "assets", "test", "passcodes.txt"), filepath.Join(workingDir, "assets", "test", "passcodes.txt")) - if err != nil { - log.Fatalf("Error reading asset passcodes.txt: %s", assetFileDir) - return err - } - _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "genesis.json"), filepath.Join(workingDir, "scripts", "generated", "genesis.json")) - if err != nil { - log.Fatalf("Error reading asset genesis.json: %s", assetFileDir) - return err - } - _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f"), filepath.Join(workingDir, "scripts", "generated", "keystores", "keys", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f")) - if err != nil { - log.Fatalf("Error reading asset 0x546f99f244b7b58b855330ae0e2bc1b30b41302f: %s", assetFileDir) - return err - } - _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentList"), filepath.Join(workingDir, "scripts", "generated", "deploymentList")) - if err != nil { - log.Fatalf("Error reading asset deploymentList: %s", assetFileDir) - return err - } - _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentArgsTemplate"), filepath.Join(workingDir, "scripts", "generated", "deploymentArgsTemplate")) - if err != nil { - log.Fatalf("Error reading asset deploymentArgsTemplate: %s", assetFileDir) - return err - } - _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "owner.toml"), filepath.Join(workingDir, "scripts", "generated", "owner.toml")) - if err != nil { - log.Fatalf("Error reading asset owner.toml: %s", assetFileDir) return err } + //// Create directories + //folders := []string{ + // filepath.Join("scripts", "base-files"), + // filepath.Join("scripts", "generated", "monitorDBs"), + // filepath.Join("scripts", "generated", "config"), + // filepath.Join("scripts", "generated", "keystores"), + // filepath.Join("scripts", "generated", "keystores", "keys"), + // filepath.Join("assets", "test", "keys"), + //} + //for _, folder := range folders { + // if err := os.MkdirAll(filepath.Join(workingDir, folder), os.ModePerm); err != nil { + // fmt.Printf("Error creating configuration folders: %v", err) + // return err + // } + //} + // + //// Copy configuration files + //rootPath := GetProjectRootPath() + //configurationFileDir := filepath.Join(rootPath, "scripts", "base-files") + //files, err := ioutil.ReadDir(configurationFileDir) + //if err != nil { + // log.Fatalf("Error reading configuaration file dir path: %s", configurationFileDir) + // return err + //} + //for _, file := range files { + // src := filepath.Join(configurationFileDir, file.Name()) + // dst := filepath.Join(workingDir, "scripts", "base-files", file.Name()) + // _, err = CopyFileToFolder(src, dst) + // if err != nil { + // log.Fatalf("Error copying config file to working directory", err) + // return err + // } + //} + // + //// Copy asset files + //assetFileDir := filepath.Join(rootPath, "assets", "test", "keys") + //files, err = ioutil.ReadDir(assetFileDir) + //if err != nil { + // log.Fatalf("Error reading asset file dir path: %s", assetFileDir) + // return err + //} + //for _, file := range files { + // src := filepath.Join(assetFileDir, file.Name()) + // dst := filepath.Join(workingDir, "assets", "test", "keys", file.Name()) + // _, err = CopyFileToFolder(src, dst) + // if err != nil { + // log.Fatalf("Error copying assets file to working directory: %v", err) + // return err + // } + //} + //_, err = CopyFileToFolder(filepath.Join(rootPath, "assets", "test", "blockheaders.txt"), filepath.Join(workingDir, "assets", "test", "blockheaders.txt")) + //if err != nil { + // log.Fatalf("Error reading asset blockheaders: %s", assetFileDir) + // return err + //} + //_, err = CopyFileToFolder(filepath.Join(rootPath, "assets", "test", "passcodes.txt"), filepath.Join(workingDir, "assets", "test", "passcodes.txt")) + //if err != nil { + // log.Fatalf("Error reading asset passcodes.txt: %s", assetFileDir) + // return err + //} + //_, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "genesis.json"), filepath.Join(workingDir, "scripts", "generated", "genesis.json")) + //if err != nil { + // log.Fatalf("Error reading asset genesis.json: %s", assetFileDir) + // return err + //} + //_, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f"), filepath.Join(workingDir, "scripts", "generated", "keystores", "keys", "0x546f99f244b7b58b855330ae0e2bc1b30b41302f")) + //if err != nil { + // log.Fatalf("Error reading asset 0x546f99f244b7b58b855330ae0e2bc1b30b41302f: %s", assetFileDir) + // return err + //} + //_, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentList"), filepath.Join(workingDir, "scripts", "generated", "deploymentList")) + //if err != nil { + // log.Fatalf("Error reading asset deploymentList: %s", assetFileDir) + // return err + //} + //_, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentArgsTemplate"), filepath.Join(workingDir, "scripts", "generated", "deploymentArgsTemplate")) + //if err != nil { + // log.Fatalf("Error reading asset deploymentArgsTemplate: %s", assetFileDir) + // return err + //} + //_, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "owner.toml"), filepath.Join(workingDir, "scripts", "generated", "owner.toml")) + //if err != nil { + // log.Fatalf("Error reading asset owner.toml: %s", assetFileDir) + // return err + //} + return nil } diff --git a/blockchain/testutils/setup.go b/blockchain/testutils/setup.go index f849fd34..30d27bcd 100644 --- a/blockchain/testutils/setup.go +++ b/blockchain/testutils/setup.go @@ -19,17 +19,50 @@ import ( "math/big" "net/http" "strconv" + "strings" "testing" ) var ( - configEndpoint = "http://localhost:8545" - ownerAccountAddress = "0x546f99f244b7b58b855330ae0e2bc1b30b41302f" - password = "abc123" + configEndpoint = "http://localhost:8545" + //ownerAccountAddress = "0x546f99f244b7b58b855330ae0e2bc1b30b41302f" + //password = "abc123" configFinalityDelay = uint64(1) ) -func getEthereumDetails(accounts []accounts.Account) (*ethereum.Details, error) { +func BuildTestEnvironment(t *testing.T, validatorsCount int) ([]string, error) { + + workingDir := cmd.CreateTestWorkingFolder() + + err := cmd.RunSetup(workingDir) + assert.Nil(t, err) + + validatorAddresses, err := cmd.RunInit(workingDir, validatorsCount) + assert.Nil(t, err) + + return validatorAddresses, nil +} + +func getEthereumDetails(accountMap map[accounts.Account]*ecdsa.PrivateKey, workingdir string) (*ethereum.Details, error) { + + //root := cmd.GetProjectRootPath() + //assetKey := filepath.Join(root, "assets", "test", "keys") + //assetPasscode := filepath.Join(root, "assets", "test", "passcodes.txt") + //assetKey := filepath.Join(workingDir, "scripts", "generated", "keystores", "keys") + //assetPasscode := filepath.Join(workingDir, "scripts", "generated", "keystores", "passcodes.txt") + + details, err := ethereum.NewEndpointWithAccount( + configEndpoint, + workingdir, + accountMap, + configFinalityDelay, + 500, + 0, + ) + return details, err +} + +func getEthereumDetailsMap(workingDir string, accountsMap map[accounts.Account]*ecdsa.PrivateKey) (*ethereum.Details, error) { //root := cmd.GetProjectRootPath() //assetKey := filepath.Join(root, "assets", "test", "keys") @@ -40,7 +73,8 @@ func getEthereumDetails(accounts []accounts.Account) (*ethereum.Details, error) // TODO - use mock details, err := ethereum.NewEndpointWithAccount( configEndpoint, - accounts, + workingDir, + accountsMap, configFinalityDelay, 500, 0, @@ -48,7 +82,7 @@ func getEthereumDetails(accounts []accounts.Account) (*ethereum.Details, error) return details, err } -func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workingDir string, accounts []accounts.Account) *ethereum.Details { +func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workingDir string, accountPrivateKeyMap map[accounts.Account]*ecdsa.PrivateKey) *ethereum.Details { log.Printf("Starting HardHat ...") err := cmd.RunHardHatNode() @@ -57,12 +91,12 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workin err = cmd.WaitForHardHatNode(ctx) assert.Nilf(t, err, "Failed to wait for hardhat to be up and running") - details, err := getEthereumDetails(accounts) + details, err := getEthereumDetails(accountPrivateKeyMap, workingDir) assert.Nilf(t, err, "Failed to build Ethereum endpoint") assert.NotNilf(t, details, "Ethereum network should not be Nil") log.Printf("Deploying contracts ...") - factoryAddress, err := cmd.RunDeploy(workingDir) + factoryAddress, err := cmd.RunDeploy(workingDir, accountPrivateKeyMap) if err != nil { details.Close() assert.Nilf(t, err, "Error deploying contracts: %v", err) @@ -80,23 +114,10 @@ func startHardHat(t *testing.T, ctx context.Context, validatorsCount int, workin } log.Printf("Registering %d validators ...", len(validatorAddresses)) - err = cmd.RunRegister(factoryAddress, nil) - if err != nil { - details.Close() - assert.Nilf(t, err, "Error registering validators: %v") - } - //logger := logging.GetLogger("test").WithField("test", 0) - - //log.Printf("Funding accounts ...") - //for _, account := range knownAccounts[1:] { - // txn, err := ethereum.TransferEther(details, logger, details.GetDefaultAccount().Address, account.Address, big.NewInt(100000000000000000)) - // assert.Nilf(t, err, "Error in TrasferEther transaction") - // assert.NotNilf(t, txn, "Expected transaction not to be nil") - //} return details } -func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, workingDir string, accounts []accounts.Account) ethereum.Network { +func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, workingDir string, accountPrivateKeyMap map[accounts.Account]*ecdsa.PrivateKey) ethereum.Network { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -104,7 +125,7 @@ func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, work isRunning, _ := cmd.IsHardHatRunning() if !isRunning { log.Printf("Hardhat is not running. Start new HardHat") - details := startHardHat(t, ctx, validatorsCount, workingDir, accounts) + details := startHardHat(t, ctx, validatorsCount, workingDir, accountPrivateKeyMap) assert.NotNilf(t, details, "Expected details to be not nil") return details } @@ -113,13 +134,12 @@ func GetEthereumNetwork(t *testing.T, cleanStart bool, validatorsCount int, work err := cmd.StopHardHat() assert.Nilf(t, err, "Failed to stopHardHat") - details := startHardHat(t, ctx, validatorsCount, workingDir, accounts) + details := startHardHat(t, ctx, validatorsCount, workingDir, accountPrivateKeyMap) assert.NotNilf(t, details, "Expected details to be not nil") return details } - //network, err := getEthereumDetails(workingDir) - network, err := getEthereumDetails(accounts) + network, err := getEthereumDetailsMap(workingDir, accountPrivateKeyMap) assert.Nilf(t, err, "Failed to build Ethereum endpoint") assert.NotNilf(t, network, "Ethereum network should not be Nil") @@ -172,6 +192,38 @@ func GenerateAccounts(privKeys []*ecdsa.PrivateKey) []accounts.Account { return accountsArray } +func InitializePrivateKeysAndAccountsMap(n int) map[accounts.Account]*ecdsa.PrivateKey { + _, ownerPrivateKey, err := GetOwnerAccount() + if err != nil { + // TODO - don't think panic is the right solution here + panic(err) + } + + privateKeys := []*ecdsa.PrivateKey{ownerPrivateKey} + randomPrivateKeys := GeneratePrivateKeys(n) + log.Printf("********** 2 - Generated %d private keys", n) + + privateKeys = append(privateKeys, randomPrivateKeys...) + accountsMap := GenerateAccountsMap(privateKeys) + log.Printf("********** 3 - Generated account Map with %d elements\n", len(accountsMap)) + + for k, v := range accountsMap { + log.Printf(" Account Address: %s - Private Key %s\n", k.Address.String(), v) + } + return accountsMap +} + +// GenerateAccountsMap derives the associated addresses from private keys +func GenerateAccountsMap(privKeys []*ecdsa.PrivateKey) map[accounts.Account]*ecdsa.PrivateKey { + accountPrivateKeyMap := make(map[accounts.Account]*ecdsa.PrivateKey) + for _, pk := range privKeys { + commonAddr := crypto.PubkeyToAddress(pk.PublicKey) + accountValue := accounts.Account{Address: commonAddr} + accountPrivateKeyMap[accountValue] = pk + } + return accountPrivateKeyMap +} + func InitializePrivateKeysAndAccounts(n int) ([]*ecdsa.PrivateKey, []accounts.Account) { _, pKey, err := GetOwnerAccount() if err != nil { @@ -188,15 +240,16 @@ func InitializePrivateKeysAndAccounts(n int) ([]*ecdsa.PrivateKey, []accounts.Ac } func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { - + defaultOwnerAccount := strings.ToLower("0x546F99F244b7B58B855330AE0E2BC1b30b41302F") id, _ := uuid.Parse("6b2a0716-b444-46c3-a1e3-2936ddd8ecc5") pk, _ := crypto.HexToECDSA("6aea45ee1273170fb525da34015e4f20ba39fe792f486ba74020bcacc9badfc1") - addr := common.HexToAddress("0x546F99F244b7B58B855330AE0E2BC1b30b41302F") + addr := common.HexToAddress(defaultOwnerAccount) key := &keystore.Key{ Id: id, Address: addr, PrivateKey: pk, } + log.Printf("********** 1 - OWNER ACCOUNT\n Address:%s\n PK: %s\n\n", &key.Address, key.PrivateKey) return &key.Address, key.PrivateKey, nil } @@ -232,9 +285,9 @@ func GetOwnerAccount() (*common.Address, *ecdsa.PrivateKey, error) { func Init(workingDir string, n int) error { - err := cmd.RunInit(workingDir, n) + // Resources setup + err := cmd.RunSetup(workingDir) if err != nil { - log.Fatal("----- INIT FAILED ----") return err } diff --git a/scripts/base-scripts/deploy.sh b/scripts/base-scripts/deploy.sh index 13e4d2c7..2420de3e 100755 --- a/scripts/base-scripts/deploy.sh +++ b/scripts/base-scripts/deploy.sh @@ -11,60 +11,52 @@ cd $BRIDGE_DIR # if on hardhat network this switches automine on to deploy faster npx hardhat setHardhatIntervalMining --network $NETWORK --enable-auto-mine - # TODO - NO NEEDED # Copy the deployList to the generated folder so we have deploymentList and deploymentArgsTemplate in the same folder cp ../scripts/base-files/deploymentList ../scripts/generated/deploymentList cp ../scripts/base-files/deploymentArgsTemplate ../scripts/generated/deploymentArgsTemplate - # TODO - NO NEEDED -#It can be the one in base-file. no need to be copied and not linked in genereater npx hardhat --network "$NETWORK" --show-stack-traces deployContracts --input-folder ../scripts/generated addr="$(grep -Pzo "\[$NETWORK\]\ndefaultFactoryAddress = \".*\"\n" ../scripts/generated/factoryState | grep -a "defaultFactoryAddress = .*" | awk '{print $NF}')" -#export FACTORY_ADDRESS=$addr -#if [[ -z "${FACTORY_ADDRESS}" ]]; then -# echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Exiting script!" -# exit 1 -#fi -# -#for filePath in $(ls ../scripts/generated/config | xargs); do -# sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/config/$filePath" > "../scripts/generated/config/$filePath".bk &&\ -# mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" -#done +export FACTORY_ADDRESS=$addr +if [[ -z "${FACTORY_ADDRESS}" ]]; then + echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Exiting script!" + exit 1 +fi + +for filePath in $(ls ../scripts/generated/config | xargs); do + sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/config/$filePath" > "../scripts/generated/config/$filePath".bk &&\ + mv "../scripts/generated/config/$filePath".bk "../scripts/generated/config/$filePath" +done + +cp ../scripts/base-files/owner.toml ../scripts/generated/owner.toml +sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/owner.toml" > "../scripts/generated/owner.toml".bk &&\ +mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" +# funds validator accounts +npx hardhat fundValidators --network $NETWORK +cd $CURRENT_WD -#cp ../scripts/base-files/owner.toml ../scripts/generated/owner.toml -#sed -e "s/registryAddress = .*/registryAddress = $FACTORY_ADDRESS/" "../scripts/generated/owner.toml" > "../scripts/generated/owner.toml".bk &&\ -#mv "../scripts/generated/owner.toml".bk "../scripts/generated/owner.toml" +if [[ ! -z "${SKIP_REGISTRATION}" ]]; then + echo "SKIPPING VALIDATOR REGISTRATION" + exit 0 +fi +FACTORY_ADDRESS="$(echo "$addr" | sed -e 's/^"//' -e 's/"$//')" +if [[ -z "${FACTORY_ADDRESS}" ]]; then + echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Not starting the registration!" + exit 1 +fi - # TODO - NO NEEDED -# golang base - TransferEther() from owner to -# funds validator accounts -#npx hardhat fundValidators --network $NETWORK -#cd $CURRENT_WD -# -#if [[ ! -z "${SKIP_REGISTRATION}" ]]; then -# echo "SKIPPING VALIDATOR REGISTRATION" -# exit 0 -#fi -# -#FACTORY_ADDRESS="$(echo "$addr" | sed -e 's/^"//' -e 's/"$//')" -# -#if [[ -z "${FACTORY_ADDRESS}" ]]; then -# echo "It was not possible to find Factory Address in the environment variable FACTORY_ADDRESS! Not starting the registration!" -# exit 1 -#fi -# cd $BRIDGE_DIR -npx hardhat setHardhatIntervalMining --network $NETWORK --interval 100 +npx hardhat setHardhatIntervalMining --network $NETWORK --interval 1000 cd $CURRENT_WD ./scripts/main.sh register cd $BRIDGE_DIR -npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 1 +npx hardhat --network $NETWORK setMinEthereumBlocksPerSnapshot --factory-address $FACTORY_ADDRESS --block-num 10 npx hardhat setHardhatIntervalMining --network $NETWORK cd $CURRENT_WD From b0912a579d7afc51eea58ad1ae66526317531393 Mon Sep 17 00:00:00 2001 From: stuckDaemon Date: Fri, 24 Jun 2022 18:02:54 +0200 Subject: [PATCH 15/15] continuing replacing scripts --- blockchain/testutils/cmd/setup.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain/testutils/cmd/setup.go b/blockchain/testutils/cmd/setup.go index 1d6a3fec..631d61cf 100644 --- a/blockchain/testutils/cmd/setup.go +++ b/blockchain/testutils/cmd/setup.go @@ -11,11 +11,11 @@ func RunSetup(workingDir string) error { if err != nil { return err } - _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentArgsTemplate"), filepath.Join(workingDir, "deploymentList")) + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentArgsTemplate"), filepath.Join(workingDir, "deploymentArgsTemplate")) if err != nil { return err } - _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "deploymentList"), filepath.Join(workingDir, "deploymentList")) + _, err = CopyFileToFolder(filepath.Join(rootPath, "scripts", "base-files", "genesis.json"), filepath.Join(workingDir, "genesis.json")) if err != nil { return err }