2018-02-08 12:52:47 +00:00
|
|
|
package utils
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-10-25 12:08:33 +00:00
|
|
|
"errors"
|
2017-10-24 10:23:53 +00:00
|
|
|
"flag"
|
2017-10-24 15:13:19 +00:00
|
|
|
"fmt"
|
2017-10-11 14:20:51 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-03-02 09:25:30 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2017-10-11 14:20:51 +00:00
|
|
|
"strings"
|
2018-03-02 09:25:30 +00:00
|
|
|
"testing"
|
2017-10-21 17:04:07 +00:00
|
|
|
"time"
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/geth/common"
|
2018-01-25 07:26:34 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2018-01-04 12:57:50 +00:00
|
|
|
|
|
|
|
_ "github.com/stretchr/testify/suite" // required to register testify flags
|
2017-10-11 14:20:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-10-26 13:11:24 +00:00
|
|
|
networkSelected = flag.String("network", "statuschain", "-network=NETWORKID or -network=NETWORKNAME to select network used for tests")
|
2017-10-25 12:08:33 +00:00
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
// ErrNoRemoteURL is returned when network id has no associated url.
|
|
|
|
ErrNoRemoteURL = errors.New("network id requires a remote URL")
|
2017-10-24 10:23:53 +00:00
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
// ErrTimeout is returned when test times out
|
|
|
|
ErrTimeout = errors.New("timeout")
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// TestConfig defines the default config usable at package-level.
|
|
|
|
TestConfig *common.TestConfig
|
|
|
|
|
|
|
|
// RootDir is the main application directory
|
|
|
|
RootDir string
|
|
|
|
|
|
|
|
// TestDataDir is data directory used for tests
|
|
|
|
TestDataDir string
|
|
|
|
|
|
|
|
// TestNetworkNames network ID to name mapping
|
|
|
|
TestNetworkNames = map[int]string{
|
2017-10-16 21:54:56 +00:00
|
|
|
params.MainNetworkID: "Mainnet",
|
|
|
|
params.RopstenNetworkID: "Ropsten",
|
|
|
|
params.RinkebyNetworkID: "Rinkeby",
|
|
|
|
params.StatusChainNetworkID: "StatusChain",
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// setup root directory
|
2017-11-07 17:46:11 +00:00
|
|
|
const pathSeparator = string(os.PathSeparator)
|
2017-10-11 14:20:51 +00:00
|
|
|
RootDir = filepath.Dir(pwd)
|
2017-11-07 17:46:11 +00:00
|
|
|
pathDirs := strings.Split(RootDir, pathSeparator)
|
|
|
|
for i := range pathDirs {
|
|
|
|
if pathDirs[i] == "status-go" {
|
|
|
|
RootDir = filepath.Join(pathDirs[:i+1]...)
|
|
|
|
RootDir = filepath.Join(pathSeparator, RootDir)
|
|
|
|
break
|
|
|
|
}
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup auxiliary directories
|
|
|
|
TestDataDir = filepath.Join(RootDir, ".ethereumtest")
|
|
|
|
|
2017-11-20 18:21:30 +00:00
|
|
|
TestConfig, err = common.LoadTestConfig(GetNetworkID())
|
2017-10-11 14:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromFile is useful for loading test data, from testdata/filename into a variable
|
|
|
|
// nolint: errcheck
|
|
|
|
func LoadFromFile(filename string) string {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2018-01-30 11:51:48 +00:00
|
|
|
io.Copy(buf, f) // nolint: gas
|
|
|
|
f.Close() // nolint: gas
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-01-30 11:51:48 +00:00
|
|
|
return buf.String()
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
2017-10-21 17:04:07 +00:00
|
|
|
|
|
|
|
// EnsureNodeSync waits until node synchronzation is done to continue
|
2017-10-23 21:39:13 +00:00
|
|
|
// with tests afterwards. Panics in case of an error or a timeout.
|
|
|
|
func EnsureNodeSync(nodeManager common.NodeManager) {
|
2017-10-23 11:05:52 +00:00
|
|
|
nc, err := nodeManager.NodeConfig()
|
|
|
|
if err != nil {
|
2017-10-23 21:39:13 +00:00
|
|
|
panic("can't retrieve NodeConfig")
|
2017-10-23 11:05:52 +00:00
|
|
|
}
|
|
|
|
// Don't wait for any blockchain sync for the local private chain as blocks are never mined.
|
|
|
|
if nc.NetworkID == params.StatusChainNetworkID {
|
2017-10-23 21:39:13 +00:00
|
|
|
return
|
2017-10-23 11:05:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 17:04:07 +00:00
|
|
|
les, err := nodeManager.LightEthereumService()
|
|
|
|
if err != nil {
|
2017-10-23 21:39:13 +00:00
|
|
|
panic(err)
|
2017-10-21 17:04:07 +00:00
|
|
|
}
|
|
|
|
if les == nil {
|
2017-10-23 21:39:13 +00:00
|
|
|
panic("LightEthereumService is nil")
|
2017-10-21 17:04:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
// todo(@jeka): we should extract it into config
|
2017-11-08 07:35:29 +00:00
|
|
|
timeout := time.NewTimer(50 * time.Minute)
|
2017-11-07 17:46:11 +00:00
|
|
|
defer timeout.Stop()
|
2017-10-23 11:05:52 +00:00
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
2017-10-21 17:04:07 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
2017-11-07 17:46:11 +00:00
|
|
|
case <-timeout.C:
|
|
|
|
panic("timeout during node synchronization")
|
2017-10-21 17:04:07 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
downloader := les.Downloader()
|
2018-01-25 07:26:34 +00:00
|
|
|
if downloader == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if nodeManager.PeerCount() == 0 {
|
|
|
|
log.Debug("No establishished connections with a peers, continue waiting for a sync")
|
|
|
|
continue
|
2017-10-21 17:04:07 +00:00
|
|
|
}
|
2018-01-25 07:26:34 +00:00
|
|
|
if downloader.Synchronising() {
|
|
|
|
log.Debug("synchronization is in progress")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
progress := downloader.Progress()
|
|
|
|
if progress.CurrentBlock >= progress.HighestBlock {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug(
|
|
|
|
fmt.Sprintf("synchronization is not finished yet: current block %d < highest block %d",
|
|
|
|
progress.CurrentBlock, progress.HighestBlock),
|
|
|
|
)
|
|
|
|
|
2017-10-21 17:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 10:23:53 +00:00
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
// GetRemoteURLFromNetworkID returns associated network url for giving network id.
|
2017-10-26 13:11:24 +00:00
|
|
|
func GetRemoteURLFromNetworkID(id int) (url string, err error) {
|
2017-10-25 12:08:33 +00:00
|
|
|
switch id {
|
|
|
|
case params.MainNetworkID:
|
2017-10-26 13:11:24 +00:00
|
|
|
url = params.MainnetEthereumNetworkURL
|
2017-10-25 12:08:33 +00:00
|
|
|
case params.RinkebyNetworkID:
|
2017-10-26 13:11:24 +00:00
|
|
|
url = params.RinkebyEthereumNetworkURL
|
2017-10-25 12:08:33 +00:00
|
|
|
case params.RopstenNetworkID:
|
2017-10-26 13:11:24 +00:00
|
|
|
url = params.RopstenEthereumNetworkURL
|
2017-11-07 17:46:11 +00:00
|
|
|
default:
|
|
|
|
err = ErrNoRemoteURL
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
return
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:07:50 +00:00
|
|
|
// GetHeadHashFromNetworkID returns the hash associated with a given network id.
|
|
|
|
func GetHeadHashFromNetworkID(id int) string {
|
2017-10-25 12:08:33 +00:00
|
|
|
switch id {
|
|
|
|
case params.RinkebyNetworkID:
|
|
|
|
return "0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177"
|
|
|
|
case params.RopstenNetworkID:
|
|
|
|
return "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"
|
|
|
|
case params.StatusChainNetworkID:
|
2017-11-20 18:21:30 +00:00
|
|
|
return "0xe9d8920a99dc66a9557a87d51f9d14a34ec50aae04298e0f142187427d3c832e"
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
// GetRemoteURL returns the url associated with a given network id.
|
|
|
|
func GetRemoteURL() (string, error) {
|
|
|
|
return GetRemoteURLFromNetworkID(GetNetworkID())
|
2017-10-25 22:07:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
// GetHeadHash returns the hash associated with a given network id.
|
|
|
|
func GetHeadHash() string {
|
2017-10-25 22:07:50 +00:00
|
|
|
return GetHeadHashFromNetworkID(GetNetworkID())
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 10:23:53 +00:00
|
|
|
// GetNetworkID returns appropriate network id for test based on
|
|
|
|
// default or provided -network flag.
|
|
|
|
func GetNetworkID() int {
|
2017-10-24 15:13:19 +00:00
|
|
|
switch strings.ToLower(*networkSelected) {
|
|
|
|
case fmt.Sprintf("%d", params.RinkebyNetworkID), "rinkeby":
|
2017-10-24 10:23:53 +00:00
|
|
|
return params.RinkebyNetworkID
|
2017-10-25 12:08:33 +00:00
|
|
|
case fmt.Sprintf("%d", params.RopstenNetworkID), "ropsten", "testnet":
|
2017-10-24 10:23:53 +00:00
|
|
|
return params.RopstenNetworkID
|
2017-10-24 15:13:19 +00:00
|
|
|
case fmt.Sprintf("%d", params.StatusChainNetworkID), "statuschain":
|
2017-10-24 10:23:53 +00:00
|
|
|
return params.StatusChainNetworkID
|
|
|
|
}
|
|
|
|
|
|
|
|
return params.StatusChainNetworkID
|
|
|
|
}
|
2017-11-20 18:21:30 +00:00
|
|
|
|
|
|
|
// GetAccount1PKFile returns the filename for Account1 keystore based
|
|
|
|
// on the current network. This allows running the e2e tests on the
|
|
|
|
// private network w/o access to the ACCOUNT_PASSWORD env variable
|
|
|
|
func GetAccount1PKFile() string {
|
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
return "test-account1-status-chain.pk"
|
|
|
|
}
|
2018-01-24 08:25:28 +00:00
|
|
|
return "test-account1.pk"
|
2017-11-20 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccount2PKFile returns the filename for Account2 keystore based
|
|
|
|
// on the current network. This allows running the e2e tests on the
|
|
|
|
// private network w/o access to the ACCOUNT_PASSWORD env variable
|
|
|
|
func GetAccount2PKFile() string {
|
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
return "test-account2-status-chain.pk"
|
|
|
|
}
|
2018-01-24 08:25:28 +00:00
|
|
|
return "test-account2.pk"
|
2017-11-20 18:21:30 +00:00
|
|
|
}
|
2018-01-18 16:55:17 +00:00
|
|
|
|
|
|
|
// WaitClosed used to wait on a channel in tests
|
2018-02-09 13:37:56 +00:00
|
|
|
func WaitClosed(c <-chan struct{}, d time.Duration) error {
|
2018-01-18 16:55:17 +00:00
|
|
|
timer := time.NewTimer(d)
|
|
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
return nil
|
|
|
|
case <-timer.C:
|
|
|
|
return ErrTimeout
|
|
|
|
}
|
2018-03-02 09:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeTestNodeConfig defines a function to return a giving params.NodeConfig
|
|
|
|
// where specific network addresses are assigned based on provided network id.
|
|
|
|
func MakeTestNodeConfig(networkID int) (*params.NodeConfig, error) {
|
|
|
|
testDir := filepath.Join(TestDataDir, TestNetworkNames[networkID])
|
2018-01-18 16:55:17 +00:00
|
|
|
|
2018-03-02 09:25:30 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
testDir = filepath.ToSlash(testDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run tests with "INFO" log level only
|
|
|
|
// when `go test` invoked with `-v` flag
|
|
|
|
errorLevel := "ERROR"
|
|
|
|
if testing.Verbose() {
|
|
|
|
errorLevel = "INFO"
|
|
|
|
}
|
|
|
|
|
|
|
|
configJSON := `{
|
|
|
|
"NetworkId": ` + strconv.Itoa(networkID) + `,
|
|
|
|
"DataDir": "` + testDir + `",
|
|
|
|
"HTTPPort": ` + strconv.Itoa(TestConfig.Node.HTTPPort) + `,
|
|
|
|
"WSPort": ` + strconv.Itoa(TestConfig.Node.WSPort) + `,
|
|
|
|
"LogLevel": "` + errorLevel + `"
|
|
|
|
}`
|
|
|
|
|
|
|
|
nodeConfig, err := params.LoadNodeConfig(configJSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nodeConfig, nil
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|