2016-09-11 11:44:14 +00:00
|
|
|
package geth
|
|
|
|
|
2016-09-15 03:08:06 +00:00
|
|
|
/*
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
extern bool StatusServiceSignalEvent(const char *jsonEvent);
|
|
|
|
*/
|
2016-09-11 11:44:14 +00:00
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-12-18 20:36:17 +00:00
|
|
|
"encoding/json"
|
2016-09-11 11:44:14 +00:00
|
|
|
"io"
|
2017-04-06 19:36:55 +00:00
|
|
|
"io/ioutil"
|
2016-09-11 11:44:14 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-14 22:57:07 +00:00
|
|
|
"strings"
|
2016-09-11 11:44:14 +00:00
|
|
|
"sync"
|
2017-05-03 14:24:48 +00:00
|
|
|
"testing"
|
2016-09-11 11:44:14 +00:00
|
|
|
"time"
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2017-03-21 16:27:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2016-11-05 17:12:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-03-15 21:03:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-04-06 19:36:55 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2017-03-14 13:17:32 +00:00
|
|
|
var (
|
|
|
|
muPrepareTestNode sync.Mutex
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// RootDir is the main application directory
|
|
|
|
RootDir string
|
|
|
|
|
|
|
|
// TestDataDir is data directory used for tests
|
|
|
|
TestDataDir string
|
2017-03-14 13:17:32 +00:00
|
|
|
)
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
func init() {
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup root directory
|
|
|
|
RootDir = filepath.Dir(pwd)
|
|
|
|
if strings.HasSuffix(RootDir, "geth") || strings.HasSuffix(RootDir, "cmd") { // we need to hop one more level
|
|
|
|
RootDir = filepath.Join(RootDir, "..")
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup auxiliary directories
|
|
|
|
TestDataDir = filepath.Join(RootDir, ".ethereumtest")
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// NodeNotificationHandler defines a handler able to process incoming node events.
|
|
|
|
// Events are encoded as JSON strings.
|
2016-09-15 03:08:06 +00:00
|
|
|
type NodeNotificationHandler func(jsonEvent string)
|
|
|
|
|
2017-01-12 17:56:36 +00:00
|
|
|
var notificationHandler NodeNotificationHandler = TriggerDefaultNodeNotificationHandler
|
2016-09-15 03:08:06 +00:00
|
|
|
|
2017-01-12 17:56:36 +00:00
|
|
|
// SetDefaultNodeNotificationHandler sets notification handler to invoke on SendSignal
|
2016-09-15 03:08:06 +00:00
|
|
|
func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) {
|
|
|
|
notificationHandler = fn
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:56:36 +00:00
|
|
|
// TriggerDefaultNodeNotificationHandler triggers default notification handler (helpful in tests)
|
|
|
|
func TriggerDefaultNodeNotificationHandler(jsonEvent string) {
|
2017-05-02 14:35:37 +00:00
|
|
|
log.Info("notification received (default notification handler)", "event", jsonEvent)
|
2017-01-12 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendSignal sends application signal (JSON, normally) upwards to application (via default notification handler)
|
2016-12-18 20:36:17 +00:00
|
|
|
func SendSignal(signal SignalEnvelope) {
|
|
|
|
data, _ := json.Marshal(&signal)
|
|
|
|
C.StatusServiceSignalEvent(C.CString(string(data)))
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
//export NotifyNode
|
2017-05-03 14:24:48 +00:00
|
|
|
func NotifyNode(jsonEvent *C.char) { // nolint: golint
|
2016-09-15 03:08:06 +00:00
|
|
|
notificationHandler(C.GoString(jsonEvent))
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:36:17 +00:00
|
|
|
//export TriggerTestSignal
|
2017-05-03 14:24:48 +00:00
|
|
|
func TriggerTestSignal() { // nolint: golint
|
2016-09-15 03:08:06 +00:00
|
|
|
C.StatusServiceSignalEvent(C.CString(`{"answer": 42}`))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 13:17:32 +00:00
|
|
|
// TestConfig contains shared (among different test packages) parameters
|
|
|
|
type TestConfig struct {
|
|
|
|
Node struct {
|
2017-03-14 22:57:07 +00:00
|
|
|
SyncSeconds time.Duration
|
2017-03-14 13:17:32 +00:00
|
|
|
HTTPPort int
|
|
|
|
WSPort int
|
|
|
|
}
|
2017-03-14 22:57:07 +00:00
|
|
|
Account1 struct {
|
|
|
|
Address string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
Account2 struct {
|
2017-03-14 13:17:32 +00:00
|
|
|
Address string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTestConfig loads test configuration values from disk
|
|
|
|
func LoadTestConfig() (*TestConfig, error) {
|
|
|
|
var testConfig TestConfig
|
|
|
|
|
2017-04-06 19:36:55 +00:00
|
|
|
configData := string(static.MustAsset("config/test-data.json"))
|
2017-03-14 13:17:32 +00:00
|
|
|
if err := json.Unmarshal([]byte(configData), &testConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &testConfig, nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// LoadFromFile is useful for loading test data, from testdata/filename into a variable
|
|
|
|
// nolint: errcheck
|
2016-09-11 11:44:14 +00:00
|
|
|
func LoadFromFile(filename string) string {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
io.Copy(buf, f)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
return string(buf.Bytes())
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// PrepareTestNode initializes node manager and start a test node (only once!)
|
2016-09-11 11:44:14 +00:00
|
|
|
func PrepareTestNode() (err error) {
|
|
|
|
muPrepareTestNode.Lock()
|
|
|
|
defer muPrepareTestNode.Unlock()
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
manager := NodeManagerInstance()
|
|
|
|
if manager.NodeInited() {
|
2016-09-11 11:44:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:56:36 +00:00
|
|
|
defer HaltOnPanic()
|
|
|
|
|
2017-03-14 22:57:07 +00:00
|
|
|
testConfig, err := LoadTestConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
syncRequired := false
|
2017-05-03 14:24:48 +00:00
|
|
|
if _, err = os.Stat(TestDataDir); os.IsNotExist(err) {
|
2016-09-11 11:44:14 +00:00
|
|
|
syncRequired = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare node directory
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = os.MkdirAll(filepath.Join(TestDataDir, "keystore"), os.ModePerm); err != nil {
|
2017-05-02 14:35:37 +00:00
|
|
|
log.Warn("make node failed", "error", err)
|
2016-09-11 11:44:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-09 22:16:05 +00:00
|
|
|
// import test accounts (with test ether on it)
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = ImportTestAccount(filepath.Join(TestDataDir, "keystore"), "test-account1.pk"); err != nil {
|
2017-03-21 16:27:59 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = ImportTestAccount(filepath.Join(TestDataDir, "keystore"), "test-account2.pk"); err != nil {
|
2017-03-21 16:27:59 +00:00
|
|
|
panic(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start geth node and wait for it to initialize
|
2017-05-03 14:24:48 +00:00
|
|
|
config, err := params.NewNodeConfig(filepath.Join(TestDataDir, "data"), params.TestNetworkID)
|
2017-03-15 21:03:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-28 08:49:15 +00:00
|
|
|
config.KeyStoreDir = filepath.Join(TestDataDir, "keystore")
|
2017-03-15 21:03:01 +00:00
|
|
|
config.HTTPPort = testConfig.Node.HTTPPort // to avoid conflicts with running app, using different port in tests
|
|
|
|
config.WSPort = testConfig.Node.WSPort // ditto
|
2017-03-18 19:00:40 +00:00
|
|
|
config.LogEnabled = true
|
2017-03-15 21:03:01 +00:00
|
|
|
|
|
|
|
err = CreateAndRunNode(config)
|
2017-01-12 17:56:36 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
manager = NodeManagerInstance()
|
|
|
|
if !manager.NodeInited() {
|
2016-09-15 03:08:06 +00:00
|
|
|
panic(ErrInvalidGethNode)
|
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
if service, err := manager.RPCClient(); err != nil || service == nil {
|
2016-09-15 03:08:06 +00:00
|
|
|
panic(ErrInvalidGethNode)
|
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
if service, err := manager.WhisperService(); err != nil || service == nil {
|
2016-09-15 03:08:06 +00:00
|
|
|
panic(ErrInvalidGethNode)
|
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
if service, err := manager.LightEthereumService(); err != nil || service == nil {
|
2016-09-15 03:08:06 +00:00
|
|
|
panic(ErrInvalidGethNode)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if syncRequired {
|
2017-05-02 14:35:37 +00:00
|
|
|
log.Warn("Sync is required", "duration", testConfig.Node.SyncSeconds)
|
2017-03-14 22:57:07 +00:00
|
|
|
time.Sleep(testConfig.Node.SyncSeconds * time.Second) // LES syncs headers, so that we are up do date when it is done
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// MakeTestCompleteTxHandler returns node notification handler to be used in test
|
|
|
|
// basically notification handler completes a transaction (that is enqueued after
|
|
|
|
// the handler has been installed)
|
|
|
|
func MakeTestCompleteTxHandler(t *testing.T, txHash *common.Hash, completed chan struct{}) (handler func(jsonEvent string), err error) {
|
|
|
|
testConfig, err := LoadTestConfig()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = func(jsonEvent string) {
|
|
|
|
var envelope SignalEnvelope
|
|
|
|
if err := json.Unmarshal([]byte(jsonEvent), &envelope); err != nil {
|
|
|
|
t.Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if envelope.Type == EventTransactionQueued {
|
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
|
|
|
|
t.Logf("Transaction queued (will be completed shortly): {id: %s}\n", event["id"].(string))
|
|
|
|
|
|
|
|
if err := SelectAccount(testConfig.Account1.Address, testConfig.Account1.Password); err != nil {
|
|
|
|
t.Errorf("cannot select account: %v", testConfig.Account1.Address)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if *txHash, err = CompleteTransaction(event["id"].(string), testConfig.Account1.Password); err != nil {
|
|
|
|
t.Errorf("cannot complete queued transaction[%v]: %v", event["id"], err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Contract created: https://testnet.etherscan.io/tx/%s", txHash.Hex())
|
|
|
|
close(completed) // so that timeout is aborted
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
return
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PanicAfter throws panic() after waitSeconds, unless abort channel receives notification
|
2016-10-13 18:02:48 +00:00
|
|
|
func PanicAfter(waitSeconds time.Duration, abort chan struct{}, desc string) {
|
2016-09-11 11:44:14 +00:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-abort:
|
|
|
|
return
|
2016-10-13 18:02:48 +00:00
|
|
|
case <-time.After(waitSeconds):
|
2016-09-15 03:08:06 +00:00
|
|
|
panic("whatever you were doing takes toooo long: " + desc)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// FromAddress converts account address from string to common.Address.
|
|
|
|
// The function is useful to format "From" field of send transaction struct.
|
2016-11-05 17:12:24 +00:00
|
|
|
func FromAddress(accountAddress string) common.Address {
|
2017-02-27 12:52:10 +00:00
|
|
|
from, err := ParseAccountString(accountAddress)
|
2016-11-05 17:12:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return from.Address
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// ToAddress converts account address from string to *common.Address.
|
|
|
|
// The function is useful to format "To" field of send transaction struct.
|
2016-11-05 17:12:24 +00:00
|
|
|
func ToAddress(accountAddress string) *common.Address {
|
2017-02-27 12:52:10 +00:00
|
|
|
to, err := ParseAccountString(accountAddress)
|
2016-11-05 17:12:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &to.Address
|
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
|
2017-02-27 12:52:10 +00:00
|
|
|
// ParseAccountString parses hex encoded string and returns is as accounts.Account.
|
|
|
|
func ParseAccountString(account string) (accounts.Account, error) {
|
2016-12-07 21:07:08 +00:00
|
|
|
// valid address, convert to account
|
|
|
|
if common.IsHexAddress(account) {
|
|
|
|
return accounts.Account{Address: common.HexToAddress(account)}, nil
|
|
|
|
}
|
|
|
|
|
2017-02-27 12:52:10 +00:00
|
|
|
return accounts.Account{}, ErrInvalidAccountAddressOrKey
|
2016-12-07 21:07:08 +00:00
|
|
|
}
|
2017-03-21 16:27:59 +00:00
|
|
|
|
|
|
|
// AddressToDecryptedAccount tries to load and decrypt account with a given password
|
|
|
|
func AddressToDecryptedAccount(address, password string) (accounts.Account, *keystore.Key, error) {
|
|
|
|
nodeManager := NodeManagerInstance()
|
|
|
|
keyStore, err := nodeManager.AccountKeyStore()
|
|
|
|
if err != nil {
|
|
|
|
return accounts.Account{}, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := ParseAccountString(address)
|
|
|
|
if err != nil {
|
|
|
|
return accounts.Account{}, nil, ErrAddressToAccountMappingFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyStore.AccountDecryptedKey(account, password)
|
|
|
|
}
|
2017-04-09 22:16:05 +00:00
|
|
|
|
|
|
|
// ImportTestAccount checks if test account exists in keystore, and if not
|
|
|
|
// tries to import it (from static resources, see "static/keys" folder)
|
|
|
|
func ImportTestAccount(keystoreDir, accountFile string) error {
|
|
|
|
// make sure that keystore folder exists
|
|
|
|
if _, err := os.Stat(keystoreDir); os.IsNotExist(err) {
|
2017-05-03 14:24:48 +00:00
|
|
|
os.MkdirAll(keystoreDir, os.ModePerm) // nolint: errcheck
|
2017-04-09 22:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dst := filepath.Join(keystoreDir, accountFile)
|
|
|
|
if _, err := os.Stat(dst); os.IsNotExist(err) {
|
|
|
|
err = ioutil.WriteFile(dst, static.MustAsset("keys/"+accountFile), 0644)
|
|
|
|
if err != nil {
|
2017-05-02 14:35:37 +00:00
|
|
|
log.Warn("cannot copy test account PK", "error", err)
|
2017-04-09 22:16:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|