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"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2016-12-07 21:07:08 +00:00
|
|
|
"strconv"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var muPrepareTestNode sync.Mutex
|
|
|
|
|
|
|
|
const (
|
2016-09-27 13:51:08 +00:00
|
|
|
TestDataDir = "../.ethereumtest"
|
2016-11-05 17:12:24 +00:00
|
|
|
TestNodeSyncSeconds = 120
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2016-09-15 03:08:06 +00:00
|
|
|
type NodeNotificationHandler func(jsonEvent string)
|
|
|
|
|
|
|
|
var notificationHandler NodeNotificationHandler = func(jsonEvent string) { // internal signal handler (used in tests)
|
|
|
|
glog.V(logger.Info).Infof("notification received (default notification handler): %s\n", jsonEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetDefaultNodeNotificationHandler(fn NodeNotificationHandler) {
|
|
|
|
notificationHandler = fn
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
// export TriggerTestSignal
|
|
|
|
func TriggerTestSignal() {
|
|
|
|
C.StatusServiceSignalEvent(C.CString(`{"answer": 42}`))
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-09-27 13:51:08 +00:00
|
|
|
dataDir, err := PreprocessDataDir(TestDataDir)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
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) {
|
|
|
|
err = CopyFile(dst, filepath.Join("../data", "test-account.pk"))
|
|
|
|
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
|
|
|
|
// internally once.Do() is used, so call below is thread-safe
|
2016-09-15 03:08:06 +00:00
|
|
|
CreateAndRunNode(dataDir, 8546) // to avoid conflicts with running react-native app, run on different port
|
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 {
|
2016-09-27 13:51:08 +00:00
|
|
|
glog.V(logger.Warn).Infof("Sync is required, it will take %d seconds", TestNodeSyncSeconds)
|
|
|
|
time.Sleep(TestNodeSyncSeconds * 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PreprocessDataDir(dataDir string) (string, error) {
|
|
|
|
testDataDir := path.Join(dataDir, "testnet", "keystore")
|
|
|
|
if _, err := os.Stat(testDataDir); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(testDataDir, 0755); err != nil {
|
|
|
|
return dataDir, ErrDataDirPreprocessingFailed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy over static peer nodes list (LES auto-discovery is not stable yet)
|
|
|
|
dst := filepath.Join(dataDir, "testnet", "static-nodes.json")
|
|
|
|
if _, err := os.Stat(dst); os.IsNotExist(err) {
|
|
|
|
src := filepath.Join("../data", "static-nodes.json")
|
|
|
|
if err := CopyFile(dst, src); err != nil {
|
|
|
|
return dataDir, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataDir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-12-07 21:07:08 +00:00
|
|
|
accountManager, err := NodeManagerInstance().AccountManager()
|
2016-11-05 17:12:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
from, err := ParseAccountString(accountManager, accountAddress)
|
2016-11-05 17:12:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return from.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToAddress(accountAddress string) *common.Address {
|
2016-12-07 21:07:08 +00:00
|
|
|
accountManager, err := NodeManagerInstance().AccountManager()
|
2016-11-05 17:12:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
to, err := ParseAccountString(accountManager, 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
|
|
|
|
|
|
|
// parseAccount parses hex encoded string or key index in the accounts key store
|
|
|
|
// and converts it to an internal account representation.
|
|
|
|
func ParseAccountString(accman *accounts.Manager, account string) (accounts.Account, error) {
|
|
|
|
// valid address, convert to account
|
|
|
|
if common.IsHexAddress(account) {
|
|
|
|
return accounts.Account{Address: common.HexToAddress(account)}, nil
|
|
|
|
}
|
|
|
|
// valid key index, return account referenced by that key
|
|
|
|
index, err := strconv.Atoi(account)
|
|
|
|
if err != nil {
|
|
|
|
return accounts.Account{}, ErrInvalidAccountAddressOrKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return accman.AccountByIndex(index)
|
|
|
|
}
|