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"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-14 22:57:07 +00:00
|
|
|
"strings"
|
2016-09-11 11:44:14 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2016-11-05 17:12:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
|
)
|
|
|
|
|
2017-03-14 13:17:32 +00:00
|
|
|
var (
|
|
|
|
muPrepareTestNode sync.Mutex
|
2017-03-14 22:57:07 +00:00
|
|
|
RootDir string
|
|
|
|
DataDir string
|
|
|
|
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
|
|
|
|
DataDir = filepath.Join(RootDir, "data")
|
|
|
|
TestDataDir = filepath.Join(RootDir, ".ethereumtest")
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
glog.V(logger.Info).Infof("notification received (default notification handler): %s\n", jsonEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func NotifyNode(jsonEvent *C.char) {
|
2016-09-15 03:08:06 +00:00
|
|
|
notificationHandler(C.GoString(jsonEvent))
|
|
|
|
}
|
|
|
|
|
2016-12-18 20:36:17 +00:00
|
|
|
//export TriggerTestSignal
|
2016-09-15 03:08:06 +00:00
|
|
|
func TriggerTestSignal() {
|
|
|
|
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
|
|
|
|
|
|
|
|
configData := LoadFromFile(filepath.Join(DataDir, "test-data.json"))
|
|
|
|
if err := json.Unmarshal([]byte(configData), &testConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &testConfig, nil
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
func CopyFile(dst, src string) error {
|
|
|
|
s, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
d, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer d.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(d, s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromFile is usefull for loading test data, from testdata/filename into a variable
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-09-28 08:07:30 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(TestDataDir, "testnet")); os.IsNotExist(err) {
|
2016-09-11 11:44:14 +00:00
|
|
|
syncRequired = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare node directory
|
2017-03-14 13:17:32 +00:00
|
|
|
if err := os.MkdirAll(filepath.Join(TestDataDir, "testnet", "keystore"), os.ModePerm); err != nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
glog.V(logger.Warn).Infoln("make node failed:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// import test account (with test ether on it)
|
2016-09-27 13:51:08 +00:00
|
|
|
dst := filepath.Join(TestDataDir, "testnet", "keystore", "test-account.pk")
|
2016-09-11 11:44:14 +00:00
|
|
|
if _, err := os.Stat(dst); os.IsNotExist(err) {
|
2017-03-14 13:17:32 +00:00
|
|
|
err = CopyFile(dst, filepath.Join(RootDir, "data", "test-account.pk"))
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(logger.Warn).Infof("cannot copy test account PK: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start geth node and wait for it to initialize
|
2017-01-26 00:39:20 +00:00
|
|
|
err = CreateAndRunNode(&NodeConfig{
|
2017-03-14 13:17:32 +00:00
|
|
|
DataDir: TestDataDir,
|
2017-01-26 15:48:48 +00:00
|
|
|
IPCEnabled: false,
|
2017-03-14 22:57:07 +00:00
|
|
|
HTTPPort: testConfig.Node.HTTPPort, // to avoid conflicts with running app, using different port in tests
|
2017-01-26 15:48:48 +00:00
|
|
|
WSEnabled: false,
|
2017-03-14 22:57:07 +00:00
|
|
|
WSPort: testConfig.Node.WSPort, // ditto
|
2017-01-26 00:39:20 +00:00
|
|
|
TLSEnabled: false,
|
|
|
|
})
|
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-03-14 22:57:07 +00:00
|
|
|
glog.V(logger.Warn).Infof("Sync is required, it will take %d seconds", testConfig.Node.SyncSeconds)
|
|
|
|
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
|
|
|
} else {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveTestNode() {
|
2016-09-27 13:51:08 +00:00
|
|
|
err := os.RemoveAll(TestDataDir)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(logger.Warn).Infof("could not clean up temporary datadir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|